Skip to content

fix(context): show post-compaction size immediately after /compact#16757

Open
paulbkim01 wants to merge 1 commit intoanomalyco:devfrom
paulbkim01:fix/5760-context-display-after-compact
Open

fix(context): show post-compaction size immediately after /compact#16757
paulbkim01 wants to merge 1 commit intoanomalyco:devfrom
paulbkim01:fix/5760-context-display-after-compact

Conversation

@paulbkim01
Copy link

Issue for this PR

Closes #5760

Type of change

  • Bug fix

What does this PR do?

After /compact runs, the sidebar and web context display continue showing
the pre-compaction token count until the next user message is sent. This makes
it appear as if compaction had no effect.

Root cause: both displays compute context size from the last assistant
message's tokens as input + output + reasoning + cache. For a compaction
summary message, input reflects the full pre-compaction history that was fed
to the summarizer — not the context the next turn will actually use. output
is the summary text (~2-5k tokens). So the display shows ~155k instead of ~5k.

Fix: when the last assistant message has summary: true, use only its
output tokens as the context size estimate. The raw per-field breakdown
(input/output/reasoning/cache) is left untouched so the context
breakdown panel still shows full compaction details.

Changed files:

  • packages/opencode/src/cli/cmd/tui/routes/session/sidebar.tsx — TUI sidebar context memo
  • packages/app/src/components/session/session-context-metrics.ts — web/desktop context metrics
  • packages/app/src/components/session/session-context-metrics.test.ts — three new test cases

How did you verify your code works?

  • Added three tests to the existing session-context-metrics.test.ts suite (all 7 pass)
  • Full typecheck passes (bun turbo typecheck, 13 packages)
  • Manually: trigger /compact on a session with substantial history, confirm
    the sidebar immediately shows the reduced count (~output tokens of the summary)
    rather than the pre-compaction total

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

After compaction, the last assistant message's `input` tokens reflect
the full pre-compaction history fed to the summarizer, not the context
the next turn will actually see. Both the TUI sidebar and the web/desktop
context display showed this inflated number, making it appear as if
compaction had no effect until the next user message was sent.

Fix: when the last assistant message has `summary: true`, use only its
`output` tokens (the summary text) as the context size estimate. The
raw per-field breakdown (input/output/reasoning/cache) is left untouched
so the context breakdown panel still shows full compaction details.

Closes anomalyco#5760
@paulbkim01 paulbkim01 requested a review from adamdotdevin as a code owner March 9, 2026 13:39
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.

Context size in sidebar not updated after /compact

1 participant