fix: normalize context usage categories to totalUsed and exclude autocompact buffer row#2242
Conversation
lsm
left a comment
There was a problem hiding this comment.
🤖 Review by glm-5.1[1m] (GLM)
Model: glm-5.1[1m] | Client: NeoKai | Provider: GLM
Recommendation: APPROVE — posted as a comment because GitHub rejects APPROVE/REQUEST_CHANGES from the PR author.
The normalization correctly fixes the reported issue (a single breakdown category exceeding totalUsed). Verified end to end:
- Normalization — non-free, non-buffer usage categories are scaled by
totalTokens / sum(usage), with the rounding residual added to the largest category so the values sum exactly tototalUsedwhile preserving relative proportions. Hand-checked the arithmetic in all three touched cases; sums are exact and no category exceedstotalUsed. - Buffer excluded — the autocompact category is filtered out of
usageEntries, so it can no longer reappear as a usage row; it is rendered via the separate buffer zone driven byautoCompactThreshold. - Threshold intact — the threshold/reserve derivation was hoisted above the breakdown build but its logic is unchanged (still uses the raw SDK category tokens), so normalization cannot corrupt
autoCompactThreshold. - Free space — recomputed as
capacity − totalUsed − buffer, which is consistent with the buffer zone (used + free + buffer = capacity) and fixes the prior inconsistency where synthesized free space ignored the buffer. - Tests — the regression test asserts all three required invariants (no category >
totalUsed, non-free sum =totalUsed, free =capacity − totalUsed − buffer); theContextUsageBarweb tests were correctly left untouched since they use hardcoded fixtures independent of the fetcher.
Verification run:
context-fetcher.test.ts— 48 passContextUsageBar.test.tsx— 54 passcontext-tracker+sdk-message-handler— 94 pass- typecheck / lint / knip — clean
The lone failure in the 1-core shard (provider-service.test.ts, ENABLE_TOOL_SEARCH env-var leak) is pre-existing and unrelated — context-fetcher.ts touches neither env vars nor provider config.
Zero findings (P0–P3).
8f30255 to
41143fe
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 41143fe81a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…compact buffer row
41143fe to
11ab790
Compare
lsm
left a comment
There was a problem hiding this comment.
🤖 Review by glm-5.1 (GLM)
Model: glm-5.1 | Client: NeoKai | Provider: GLM
Recommendation: APPROVE
Follow-up review on rebased head 11ab7904c (rebased onto dev tip e39b3d397, which now contains #2244's flaky-test fix). The 2-file effective diff vs dev is unchanged from the prior approval review (#4645684500):
packages/daemon/src/lib/agent/context-fetcher.ts: the normalization block scales non-free usage categories to sum exactly tototalUsed(preserving relative proportions, rounding error pushed to the largest category). After normalizationnonFreeSpaceTokens == totalUsed, so the autocompact-buffer-exclusion invariantfree = capacity − totalUsed − bufferholds. Capacity selection, autocompact-threshold conversion, and the[1m]suffix handling are unchanged.packages/daemon/tests/unit/1-core/agent/context-fetcher.test.ts: regression test asserts non-free sum = totalUsed, each category ≤ totalUsed, and free = capacity − totalUsed − buffer for the kimi (262144 / 90584) case.
Verdict: zero P0–P3 findings. Code is correct and approved.
Pre-merge status: mergeStateStatus CLEAN, MERGEABLE, 32 checks SUCCESS / 4 SKIPPED (dev-only), zero unresolved review conversations. Proceeding to squash merge into dev.
Normalizes non-free, non-buffer context usage categories so their token counts sum to
totalUsedwhile preserving relative proportions, then recomputes percentages against the finaltotalCapacity. Recomputes free space ascapacity - totalUsed - bufferand removes the autocompact buffer from the breakdown rows (it is rendered as a separate visual zone). Adds a regression test for categories scaled to a larger SDK window.