fix(dashboard): parse CodeBuddy index.json for token usage - #107
Merged
Conversation
CodeBuddy persists its transcript as a single index.json document (a JSON object with requests[].usage + messages[]), not the Claude Code JSONL schema scanTranscriptStop expects. Every line failed JSON.parse, so token/prompt stats stayed 0 for CodeBuddy sessions.
Add scanCodebuddyIndex(): sum requests[].usage.{inputTokens,outputTokens} and count user messages as prompts, dispatched when the transcript basename is index.json, with a safe fallback to the Claude JSONL scanner. Verified against a real local CodeBuddy transcript end-to-end.
jeff-r2026
added a commit
that referenced
this pull request
Jul 2, 2026
…flushed (#108) CodeBuddy writes requests[].usage into index.json shortly AFTER firing the Stop hook, so the first scan sees messages (prompts captured) but zero usage — single-turn/last-turn sessions permanently recorded 0 tokens. Add a bounded retry (up to ~1.75s, within the 60s hook timeout) that re-reads until usage appears, then returns the cumulative snapshot. Follow-up to #107.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Token/prompt usage statistics never populated for CodeBuddy sessions (always 0), while Claude sessions worked.
Root cause: token stats have a single source —
scanTranscriptStop()scanning thetranscript_pathat Stop time. That scanner is hard-bound to the Claude Code JSONL schema (line-by-lineJSON.parseof{type:"assistant", message.usage.*_tokens}). CodeBuddy's Stop hook does fire and does pass atranscript_path, but it points to a single pretty-printedindex.jsondocument (CodeBuddy's own format), so every line failsJSON.parseand tokens stay 0.CodeBuddy's
index.jsonactually contains authoritative usage:{ "messages": [{ "role": "user" | "assistant" | "tool" }], "requests": [{ "usage": { "inputTokens", "outputTokens", "totalTokens" } }] }Fix
Add
scanCodebuddyIndex():requests[].usage.{inputTokens,outputTokens}(same per-turn accumulation model as the Claude scan;input + outputmatches CodeBuddy's owntotalTokens). CodeBuddy exposes no cache-read/creation split at the request level, so those map to 0.messages[]withrole === "user".Dispatched from
scanTranscriptStop()when the transcript basename isindex.json, with a safe fallback to the existing Claude JSONL scanner (returnsnullon any parse failure). No change to the Claude path.Verification
Tested end-to-end against a real local CodeBuddy transcript:
scanTranscriptStop(index.json)→{input: 215609, output: 2967},prompts: 2(total 218576 matches the file'stotalTokens)parseHookEvent(Stop payload, "codebuddy")path attachestokens+promptsdashboard-report --stdin --tool codebuddywrites the tokens intoevents.jsonlAdded a regression test;
vitest(dashboard-collector + conversation-token-metrics, 83 tests) andtsc --noEmitpass.