fix(ingest,ui): count each assistant message once, each turn once, and each event once
Three defects that all showed up as "the dashboard's numbers are wrong", plus the notification and brand work asked for alongside them. Tokens: Claude Code writes ONE JSONL line per content block of an assistant message, and every one of those lines repeats the same message-level `usage`. Both parsers summed them, so a tool-heavy session's tokens — and its cost — ran 1.5-2x high (one 4h session: $45.82 stored against $27.72 real). `lastUsageMsgId` counts a message once, carried across the transcript cache's incremental chunk boundaries. `replaceTokenUsage`'s high-water baselines mean the fixed parser cannot correct what is already stored, so `setTokenUsage` (exact, baselines cleared) is added for the deliberate re-parse path and `reconcileTokens` now covers every session with a transcript on disk rather than imported ones only. Turns: every `TurnDuration` row was stamped NOW while its dedup guard matched on the transcript's timestamp, so each hook re-inserted the session's whole turn history — 587k rows, 75% of a real events table, one session holding 68,747 copies of the same turns. The row now carries the transcript timestamp, which fixes the guard and the feed's ordering together. Same class of bug in the session metadata: `thinking_blocks` / `turn_count` / `total_turn_duration_ms` are assigned from the full parse instead of `+=`, since `extract()` returns whole-transcript totals on every call. Events: leftover checkout hook entries in `~/.claude/settings.json` double every event, every subagent row and every token figure. `stripLegacyHooks` has always removed them — but only inside `bootstrap()`, which `fastPathOk` skips on exactly the healthy installs where nothing else forces a full pass, so they survived indefinitely. `hasLegacyHooks` (pure fs) now blocks the fast path. A one-time repair at the first start after upgrading (`user_version` 3) deletes the duplicated `TurnDuration` rows and re-parses every transcript with exact writes. Marker 3 and not 2: an install that ran the earlier pass had its corrected rows re-inflated by whatever old build was still ingesting hooks. Router: switching a lane's AI Router while a pane is live only half-applies — Claude Code re-applies `env` to a running session but keeps the model it resolved at startup, so the old router's model name reaches the new endpoint and the next prompt fails with "model not found". The apply route reports `restart_required` / `run_id`, and the picker offers a Restart that respawns the pane from the lane id alone (replaying the old `--model` would re-create the mismatch). Notifications (#29): the two moments Claude is BLOCKED on the user now have their own switches and default to on — `onAwaitingInput` (a permission prompt or a question) and `onTurnComplete` (end of turn). Before, the first was unswitchable and the second hung off a preference that defaults to off, so the one thing a user walks away from went unannounced. Prefs shape/defaults/storage move to `client/src/lib/notifPrefs.ts`; Settings and the hook had private copies. Brand: the sidebar shows the SmartGift wordmark with the AgentHub caption under it, the collapsed rail shows the mark, and the favicon is vendored to match.
This commit is contained in:
@@ -391,6 +391,57 @@ function startBackgroundServices() {
|
||||
if (t.unref) t.unref();
|
||||
}
|
||||
|
||||
// One-time repair (user_version 3) of two parser bugs fixed in v1.6.16:
|
||||
//
|
||||
// 1. The transcript parser summed the `usage` of EVERY JSONL line of an
|
||||
// assistant message, and Claude Code writes one line per content block —
|
||||
// each repeating the same message-level totals. Stored tokens (and cost)
|
||||
// ran ~1.5-2x high, and `replaceTokenUsage`'s high-water baselines mean
|
||||
// the fixed parser alone can never bring them down: only the exact-write
|
||||
// reconcile can.
|
||||
// 2. Every `TurnDuration` row was stamped NOW while its dedup guard matched
|
||||
// on the transcript timestamp, so each hook re-inserted the session's
|
||||
// whole turn history — 75% of a typical events table.
|
||||
//
|
||||
// Marker 3 and not 2: an install that ran the v1.6.15 pass had its corrected
|
||||
// rows immediately re-inflated by whatever old build was still ingesting
|
||||
// hooks, so that marker cannot be trusted to mean "already correct".
|
||||
// Deferred and non-blocking; the marker is written only on success, so a
|
||||
// crash mid-run just retries on the next start.
|
||||
{
|
||||
const dbModule = require("./db");
|
||||
if (dbModule.db.pragma("user_version", { simple: true }) < 3) {
|
||||
const { reconcileTokens } = require("../scripts/import-history");
|
||||
const t = setTimeout(() => {
|
||||
Promise.resolve()
|
||||
.then(() => {
|
||||
// Duplicates differ only by their insertion time, so the earliest
|
||||
// row of each (session, payload) group is the one real turn.
|
||||
const del = dbModule.db
|
||||
.prepare(
|
||||
`DELETE FROM events WHERE event_type = 'TurnDuration' AND id NOT IN (
|
||||
SELECT MIN(id) FROM events WHERE event_type = 'TurnDuration'
|
||||
GROUP BY session_id, data
|
||||
)`
|
||||
)
|
||||
.run();
|
||||
if (del.changes > 0) {
|
||||
console.log(`Removed ${del.changes} duplicated TurnDuration event(s)`);
|
||||
}
|
||||
})
|
||||
.then(() => reconcileTokens(dbModule))
|
||||
.then((c) => {
|
||||
console.log(
|
||||
`Token re-parse: corrected ${c.sessionsTouched} session(s), ${c.modelsWritten} model row(s)`
|
||||
);
|
||||
dbModule.db.pragma("user_version = 3");
|
||||
})
|
||||
.catch((err) => console.warn("token re-parse failed:", err?.message || err));
|
||||
}, 1_000);
|
||||
if (t.unref) t.unref();
|
||||
}
|
||||
}
|
||||
|
||||
const { startUpdateScheduler } = require("./update-scheduler");
|
||||
const { broadcast } = require("./websocket");
|
||||
startUpdateScheduler({ broadcast });
|
||||
|
||||
Reference in New Issue
Block a user