Skip to content

fix: normalize context usage categories to totalUsed and exclude autocompact buffer row#2242

Merged
lsm merged 1 commit into
devfrom
space/fix-context-usage-breakdown-category-tokens-exceeding
Jul 17, 2026
Merged

fix: normalize context usage categories to totalUsed and exclude autocompact buffer row#2242
lsm merged 1 commit into
devfrom
space/fix-context-usage-breakdown-category-tokens-exceeding

Conversation

@lsm

@lsm lsm commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Normalizes non-free, non-buffer context usage categories so their token counts sum to totalUsed while preserving relative proportions, then recomputes percentages against the final totalCapacity. Recomputes free space as capacity - totalUsed - buffer and 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.

@lsm lsm left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

🤖 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 to totalUsed while preserving relative proportions. Hand-checked the arithmetic in all three touched cases; sums are exact and no category exceeds totalUsed.
  • 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 by autoCompactThreshold.
  • 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); the ContextUsageBar web tests were correctly left untouched since they use hardcoded fixtures independent of the fetcher.

Verification run:

  • context-fetcher.test.ts — 48 pass
  • ContextUsageBar.test.tsx — 54 pass
  • context-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).

@lsm
lsm force-pushed the space/fix-context-usage-breakdown-category-tokens-exceeding branch from 8f30255 to 41143fe Compare July 17, 2026 05:28

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread packages/daemon/src/lib/agent/context-fetcher.ts
@lsm
lsm force-pushed the space/fix-context-usage-breakdown-category-tokens-exceeding branch from 41143fe to 11ab790 Compare July 17, 2026 11:47

@lsm lsm left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

🤖 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 to totalUsed (preserving relative proportions, rounding error pushed to the largest category). After normalization nonFreeSpaceTokens == totalUsed, so the autocompact-buffer-exclusion invariant free = capacity − totalUsed − buffer holds. 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.

@lsm
lsm merged commit 6d67574 into dev Jul 17, 2026
45 of 72 checks passed
@lsm
lsm deleted the space/fix-context-usage-breakdown-category-tokens-exceeding branch July 17, 2026 11:57
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