Skip to content

fix(router): statusline token dedup, translator id fallback, context-fit filter for flash models#225

Merged
steventohme merged 1 commit into
mainfrom
steven/fix-statusline-token-dedup
May 21, 2026
Merged

fix(router): statusline token dedup, translator id fallback, context-fit filter for flash models#225
steventohme merged 1 commit into
mainfrom
steven/fix-statusline-token-dedup

Conversation

@steventohme
Copy link
Copy Markdown
Collaborator

Summary

Two unrelated correctness fixes surfaced from a session where the statusline showed saved $5.84 · 387.2k in / 674 out for a session whose actual totals were $2.94 · 194.8k in / 327 out.

install/cc-statusline.sh — token/savings inflation

Claude Code writes one JSONL entry per content block of an assistant turn (e.g. text + text + tool_use → 3 entries). Every entry carries the same message.usage payload. The existing jq filter ran select(.type=="assistant") and summed per entry, double- or triple-counting every multi-block turn — observed ~2× inflation in a real qwen session, can hit 3× for turns with two text blocks before a tool call.

Dedupe on (message.id, message.usage) before summing:

  • Native Anthropic upstreams: message.id is unique per turn → collapses content-block fan-out cleanly.
  • Translator round-trips: message.id can be a constant placeholder (msg_translated); usage still differs per turn (input_tokens grows), so the composite key keeps real turns distinct. Two turns with byte-identical id AND usage would still collapse — that's a genuine retry/duplicate we want to drop.

Verified: same transcript that produced 387.2k in / saved $5.84 now produces 194.8k in / saved $2.94, matching independent jq dedup math within $0.005.

internal/translate/stream.go — empty-string id latched

if id := gjson.GetBytes(data, "id"); id.Exists() && t.messageID == "" latches the first id field present in the SSE stream. Some OpenAI-compat upstreams (observed: OpenRouter for certain models) send early chunks with "id": "" before settling on a real id. gjson reports Exists()=true, Str="" for that, so the empty string was being latched — never overwritten — and emitMessageStart fell back to the "msg_translated" placeholder, breaking any downstream that dedupes by message id.

Switched the gate to Str != "" (same for model). Strictly tightens behavior: only latches non-empty strings.

Test plan

  • Manually replayed a real transcript through the patched cc-statusline.sh and verified output matches hand-computed dedup'd totals.
  • Confirm internal/translate unit tests still pass (none currently exercise the empty-id chunk case — could add one in a follow-up).
  • Once merged, bump the gitlink in the workweave repo + copy install/cc-statusline.sh into syncrouterinstall/install_template.sh so the heredoc-embedded copy stays in lockstep.

…eam id

cc-statusline.sh: CC writes one JSONL entry per content block per assistant
turn (text/text/tool_use -> 3 entries) and every entry carries the same
message.usage. Summing per-entry inflated totals by 2-3x; a real session
showing $5.84 / 387k in was actually $2.94 / 195k in. Dedupe on
(message.id, message.usage) before summing -- handles both native Anthropic
upstreams (unique id per turn) and translator round-trips where id can be a
constant placeholder (usage still differs per turn).

internal/translate/stream.go: some OpenAI-compat upstreams send early SSE
chunks with "id": "" before settling on a real id. gjson treats that as
Exists()=true, Str="", so the original Exists() check latched the empty
string and never overwrote, forcing message_start to fall back to the
'msg_translated' placeholder. Check Str != "" instead (same for model).
@steventohme steventohme merged commit 067af14 into main May 21, 2026
7 checks passed
@steventohme steventohme deleted the steven/fix-statusline-token-dedup branch May 21, 2026 23:27
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 850436e. Configure here.

Comment thread install/cc-statusline.sh
jq -rs --argjson p "$prices" --arg requested "$requested_norm" '
[.[] | select(.type=="assistant")] |
unique_by([.message.id, .message.usage]) |
.[] |
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heredoc in install.sh not synced with statusline fix

High Severity

install/cc-statusline.sh was updated with the jq -rs slurp mode and unique_by dedup fix, but the inlined heredoc copy inside install/install.sh (lines ~1434–1459) still uses the old jq -r with bare select(.type=="assistant") — no dedup. Fresh installs via make full-setup or curl|sh will write the stale version to disk and continue double/triple-counting tokens and savings.

Fix in Cursor Fix in Web

Triggered by learned rule: install/cc-statusline.sh and install/install.sh heredoc must stay in sync

Reviewed by Cursor Bugbot for commit 850436e. Configure here.

@steventohme steventohme changed the title fix: statusline double-counted tokens; translator latched empty upstream id fix(router): statusline token dedup, translator id fallback, context-fit filter for flash models May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant