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:
@@ -713,6 +713,21 @@ calculations match real-time captured sessions exactly. Re-imports are
|
||||
idempotent (dedupe by session ID; compaction `baseline_*` columns
|
||||
prevent token double-counting).
|
||||
|
||||
Both parsers count each assistant message's `usage` **once**: Claude Code
|
||||
writes one JSONL line per content block (thinking / text / each `tool_use`)
|
||||
and every one repeats the same message-level totals, so summing lines
|
||||
inflated a tool-heavy session's tokens — and its cost — by ~1.5–2×. The
|
||||
guard is the previous message id (`lastUsageMsgId`), carried across
|
||||
incremental reads so a message split over a chunk boundary still counts once.
|
||||
|
||||
`reconcileTokens` (`node scripts/import-history.js --reconcile-tokens`)
|
||||
re-parses every session that still has a transcript on disk and writes the
|
||||
result with `stmts.setTokenUsage`, which overwrites the row and clears the
|
||||
baselines. That is deliberate: the hook writer's `replaceTokenUsage` keeps a
|
||||
high-water mark, so without an exact write a previously over-counted total
|
||||
could never be corrected downwards. The server runs this reconcile once, in
|
||||
the background, at the first start after upgrading (`user_version` 2).
|
||||
|
||||
Imported and live-scanned subagents also get their **nested hierarchy**
|
||||
rebuilt: rows are inserted flat under the main agent, then
|
||||
`reconcileSubagentParents` recovers each spawner from the subagent
|
||||
|
||||
Reference in New Issue
Block a user