fix(router): statusline token dedup, translator id fallback, context-fit filter for flash models#225
Conversation
…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).
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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.
| jq -rs --argjson p "$prices" --arg requested "$requested_norm" ' | ||
| [.[] | select(.type=="assistant")] | | ||
| unique_by([.message.id, .message.usage]) | | ||
| .[] | |
There was a problem hiding this comment.
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.
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.


Summary
Two unrelated correctness fixes surfaced from a session where the statusline showed
saved $5.84 · 387.2k in / 674 outfor a session whose actual totals were$2.94 · 194.8k in / 327 out.install/cc-statusline.sh— token/savings inflationClaude Code writes one JSONL entry per content block of an assistant turn (e.g.
text+text+tool_use→ 3 entries). Every entry carries the samemessage.usagepayload. The existingjqfilter ranselect(.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:message.idis unique per turn → collapses content-block fan-out cleanly.message.idcan be a constant placeholder (msg_translated);usagestill 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.84now produces194.8k in / saved $2.94, matching independent jq dedup math within $0.005.internal/translate/stream.go— empty-stringidlatchedif id := gjson.GetBytes(data, "id"); id.Exists() && t.messageID == ""latches the firstidfield 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.gjsonreportsExists()=true, Str=""for that, so the empty string was being latched — never overwritten — andemitMessageStartfell back to the"msg_translated"placeholder, breaking any downstream that dedupes by message id.Switched the gate to
Str != ""(same formodel). Strictly tightens behavior: only latches non-empty strings.Test plan
cc-statusline.shand verified output matches hand-computed dedup'd totals.internal/translateunit tests still pass (none currently exercise the empty-idchunk case — could add one in a follow-up).install/cc-statusline.shintosyncrouterinstall/install_template.shso the heredoc-embedded copy stays in lockstep.