Skip to content

Show context compaction events in studio code CLI#3019

Draft
sejas wants to merge 3 commits intotrunkfrom
show-context-compaction-info
Draft

Show context compaction events in studio code CLI#3019
sejas wants to merge 3 commits intotrunkfrom
show-context-compaction-info

Conversation

@sejas
Copy link
Copy Markdown
Member

@sejas sejas commented Apr 8, 2026

Related issues

  • Fixes STU-

Proposed Changes

The Claude Agent SDK emits system messages when it manages the context window, but the studio code CLI UI only handled assistant, user, and result messages. As a result, long agent turns appeared to "stop suddenly" while the SDK was silently compacting context in the background.

This PR wires up the missing message types in apps/cli/ai/ui.ts:handleMessage:

  • status: compacting — sets the loader message to "Compacting context…" so users see why the turn has paused.
  • compact_boundary — shows an info message when the SDK summarizes the transcript, including the trigger (auto/manual) and the token count before compaction.
  • microcompact_boundary — shows an info message when the SDK drops old tool-result attachments (screenshots, large tool outputs) to free tokens without summarizing the full transcript. Includes the number of attachments dropped and tokens saved.

The microcompact_boundary type isn't in the public SDK type definitions yet, so it's narrowed via a defensive cast.

Testing Instructions

  • Build the CLI: npm run cli:build
  • Run node apps/cli/dist/cli/main.mjs code
  • Start a long task that uses screenshots or many tool calls to push context towards the limit
  • Verify that during compaction the loader shows "Compacting context…", and that after compaction an info line appears describing what happened (e.g. "Context compacted (auto, 123456 tokens summarized into a shorter form).")
  • Confirm normal (non-compacting) turns are unaffected

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors? (Tests pass, no errors)

The Agent SDK emits system messages when it shrinks context (compacting
status, compact_boundary, microcompact_boundary) but the CLI UI ignored
them, so long sessions appeared to stop suddenly while the SDK was
silently managing context. Surface these events as informational
messages and as a loader status during compaction.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sejas sejas self-assigned this Apr 8, 2026
sejas and others added 2 commits April 8, 2026 21:38
Track the model's input + cache token counts against the reported
contextWindow on every result message and render the percentage in
the prompt footer alongside the existing status message, so users
can see how full the context window is before it triggers compaction.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The previous version divided result.modelUsage cumulative totals by
contextWindow, which inflated the percentage in multi-iteration turns
(each iteration's cached reads were summed, so an 11-iteration turn
could report > 100% even when no single API call was close to the
window).

Track the last assistant message's usage snapshot instead — that
reflects the actual prompt size sent on the most recent API call —
and cache the context window from result messages so the label can
update mid-turn as iterations stream in.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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