fix(claude): group token accounting must not depend on line adjacency#123
Open
akesling wants to merge 2 commits into
Open
fix(claude): group token accounting must not depend on line adjacency#123akesling wants to merge 2 commits into
akesling wants to merge 2 commits into
Conversation
canonicalize_message_usage and sum_usage assumed a group_id's lines were contiguous. Multi-terminal writers can interleave a split message's lines with another message's (docs/agents/formats/claude-code/ known-issues.md), splitting a group into two contiguous runs and double-counting the message total. Separately, group_id was None whenever message.id was absent, turning every content-block line of an id-less assistant message into its own accounting unit. Both functions now group by group_id globally instead of by adjacency, and group_id falls back to the entry-level request_id for assistant entries when message.id is missing (Anthropic's request ID identifies one API request per assistant message; user entries never use this fallback). toolpath-claude 0.12.0 -> 0.12.1.
|
🔍 Preview deployed: https://51cab5d6.toolpath.pages.dev |
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.
Root cause
Two holes in the message-level usage dedup, both the same class as the #106 over-counting bug:
canonicalize_message_usageandsum_usageassumed agroup_id's JSONL lines are contiguous.docs/agents/formats/claude-code/known-issues.mddocuments multi-terminal interleaved writes that split a group's lines — each contiguous fragment then contributed a full message total, over-counting the session.group_idcame only frommessage.id, which isOption. An id-less assistant message split across content-block lines repeated its full usage on every line withgroup_id = None— each line became its own accounting unit and the usage was summed N times.Fix
canonicalize_message_usagenow groups globally bygroup_idin one O(n) pass (field-wise max across all occurrences, total placed on the group's last occurrence), instead of walking contiguous runs.sum_usagededups via a precomputed last-occurrence map instead of next-line adjacency.group_idfalls back to the entry-levelrequestIdfor assistant entries whenmessage.idis absent (verified againstdocs/agents/formats/claude-code/jsonl-envelope.md: requestId is per-API-request, assistant-only, documented for deduping streamed messages). User entries never group.docs/agents/formats/claude-code/usage.mdand the CLAUDE.md token-accounting bullet updated to match.The projector needed no change: it re-expands group totals via its own group-keyed map, independent of which turn carries the total.
Tests
Three new tests (TDD, red before the fix): interleaved groups count once (was 250, now 150 for A(m1)/B(m2)/C(m1)); id-less assistant message groups by requestId (was 20, now 10); user entries never group by requestId. All existing group/projector round-trip tests unchanged and green. Full workspace suite + clippy green.
toolpath-claudebumped 0.12.0 → 0.12.1.Notes
Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.