Files
nntrivi2001 1c5521cf2e perf: rest animations, cheaper chat typing and server polls, async per-tool hooks
UI
- No animation runs forever: Sagi moods, the custom pet sprite, status dots
  (pulse-dot, pulse-slow, ping) and the chat shimmer play a few cycles and
  rest. An infinite animation kept the browser drawing 60 frames a second.
- useSagiBrain: frames that only move lastActivityAt by <1 s keep the old
  state; the 1 s tick re-renders only when the mood changes.
- ConversationView and MessageList are memoized and PaneChat passes stable
  callbacks: typing in the chat box no longer re-renders the conversation.
- The chat's 700 ms menu poll reads plain text first and scans the terminal
  cell by cell (reusing one cell) only when a row could be a menu cursor row.

Server
- GET /sessions/:id/transcript?after=N skips lines <= N before JSON.parse.
- GET /sessions/:id/outline is cached until the session's events change.
- broadcastLane uses the cached tmux.liveSessions(); tmux.hasSession answers
  "yes" from the fresh shared list instead of a synchronous spawn (50-150 ms
  per psmux call on Windows, blocking terminal echo).
- pane-mode polls every 150 ms; writeHourly writes only changed hour buckets.
- /api/metrics: agenthub_event_loop_delay_seconds{quantile}.

Hooks
- PreToolUse/PostToolUse run with "async": true (settings installer,
  plugin.json, S3 managed settings): Claude Code no longer waits a Node start
  before and after every tool call.

Committed with --no-verify at the owner's request: the pre-commit full server
suite fails on 9 tests that fail identically on 0c2f498 (lane-sync merge: no
git identity on this host; read-pool; resolveHost; codex-usage 1 ms flake).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 11:55:53 +07:00

98 lines
3.4 KiB
JavaScript

/**
* @file Per-hour token usage (`token_usage_hourly`): written from a Claude
* transcript's `tokensByHour` (`server/lib/transcript-cache.js`) by the hook
* writer, back-filled once for sessions recorded before the table existed, and
* read only by the daily cost split in `GET /api/pricing/cost` — so a session
* that runs for days books each day's spend to that day instead of its start.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { db, stmts } = require("../db");
const TranscriptCache = require("./transcript-cache");
const { longArgs } = require("./token-usage");
// What this process last wrote per session and hour bucket: a long session has
// one bucket per active hour per model, and every hook re-sent all of them.
// The upsert keeps the MAX, so skipping a bucket whose numbers did not move
// writes exactly the same rows. Bounded (oldest session dropped first); a miss
// only means one full write, never a lost one.
const WRITTEN_MAX = 500;
const written = new Map();
/** Write the hours of one transcript parse that changed (MAX per hour, see the statement). */
function writeHourly(sessionId, tokensByHour) {
let seen = written.get(sessionId);
if (seen) written.delete(sessionId);
else seen = new Map();
written.set(sessionId, seen);
if (written.size > WRITTEN_MAX) written.delete(written.keys().next().value);
for (const [key, t] of Object.entries(tokensByHour || {})) {
const sig = JSON.stringify(t);
if (seen.get(key) === sig) continue;
seen.set(key, sig);
stmts.upsertHourlyUsage.run(
sessionId,
t.hour,
t.model,
t.speed,
t.geo,
t.tier,
t.input,
t.output,
t.cacheRead,
t.cacheWrite,
t.cacheWrite1h,
t.webSearch,
t.webFetch,
t.codeExec
);
const long = longArgs(t);
if (long)
stmts.raiseLongHourly.run(...long, sessionId, t.hour, t.model, t.speed, t.geo, t.tier);
}
}
/**
* Fill the table for Claude sessions that have tokens but no hourly rows yet,
* one transcript per tick so a large history never blocks the event loop.
* Resolves with the number of sessions written.
*/
function backfillHourly() {
const rows = db
.prepare(
`SELECT s.id, s.transcript_path FROM sessions s
WHERE s.transcript_path IS NOT NULL AND s.transcript_path != ''
AND EXISTS (SELECT 1 FROM token_usage tu WHERE tu.session_id = s.id)
AND NOT EXISTS (SELECT 1 FROM token_usage_hourly h WHERE h.session_id = s.id)`
)
.all();
const cache = new TranscriptCache(1);
let written = 0;
return new Promise((resolve) => {
const next = (i) => {
if (i >= rows.length) return resolve(written);
const { id, transcript_path } = rows[i];
try {
const result = cache.extract(transcript_path);
if (result?.tokensByHour && Object.keys(result.tokensByHour).length > 0) {
db.transaction(() => writeHourly(id, result.tokensByHour))();
written++;
}
} catch {
/* an unreadable transcript keeps its start-date booking */
}
setImmediate(() => next(i + 1));
};
next(0);
});
}
/** Forget what was written — for `ids`, or every session. Whatever deletes
* hourly rows calls it, or the next hooks would skip re-writing them. */
function forgetHourly(ids = null) {
if (!ids) return written.clear();
for (const id of ids) written.delete(id);
}
module.exports = { writeHourly, backfillHourly, forgetHourly };