Skip to content

feat: Session.contextBreakdown(), claude's context categories read on demand - #264

Merged
xxchan merged 3 commits into
mainfrom
cindy/context-breakdown
Oct 9, 2026
Merged

xxchan merged 3 commits into
mainfrom
cindy/context-breakdown

Conversation

@xxchan

@xxchan xxchan commented Oct 9, 2026

Copy link
Copy Markdown
Member

Closes #262.

What

Session.contextBreakdown?(): Promise<ContextBreakdown | null>: what fills the context window, by the runtime's own categories, asked of the runtime now and never recorded. Only claude has it; on every other session the member is absent and a host shows contextUsage().

  • contract (contracts/context-breakdown.ts): { tokens, contextWindow, categories: [{ name, tokens, kind, items? }] }. Names stay the runtime's words; kind is used | deferred | reserved | free | unknown (claude buffer is reserved; an unrecognized kind is kept as unknown, never dropped).
  • claude (runtimes/claude/context-breakdown.ts): get_context_usage with detail: "full" over the control channel. Memory files (by path), skills, custom agents and MCP tools (loaded under MCP tools, deferred under MCP tools (deferred)) itemize their categories. The answer is consumed before the projection, like the private get_settings readback, so it enters no record. Concurrent calls share the read in flight; a refusal rejects with claude's words; after the exit the answer is null; 10 s bound.
  • docs: claude.md (mapping and evidence), record-stream.md (read, never recorded), runtime-matrix.md (who has the member).

Evidence

claude 2.1.292 on xxwork: answered with no model call, and mid-turn in 0.5 s while a Bash tool ran (one short turn spent on the existing login). The fixture is that answer's shape, trimmed; the used categories sum to totalTokens and all but the deferred ones to maxTokens, and the test asserts both.

Tests

tests/claude/claude-context-breakdown.test.ts: projection (order, words, kinds, items, sums), the request sent, the shared read, nothing recorded, refusal, null after exit. Mutation-checked: letting the answer through to the projection, mapping buffer to used, and not settling on exit each fail a test.

🤖 Generated with Claude Code

exe.dev user and others added 3 commits October 9, 2026 06:35
… demand (#262)

claude's get_context_usage (detail full) over the control channel:
categories in claude's words and order, memory files, skills, custom
agents and MCP tools as items, consumed before the projection so it
enters no record. Absent on every other runtime.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…under the category claude has

Lookout's inspection of #264: a read during a dispose wrote to the ended
stdin and crashed the host (ERR_STREAM_WRITE_AFTER_END); with tool search
off every MCP tool says isLoaded false under the only category, MCP tools,
so its items were lost. Also document the count_tokens requests each read
sends, so hosts read on demand.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…once stdin ended

Lookout's recheck of #264: the abort fallback's kill also ends claude's
stdin without a dispose, and a read in that window crashed the host. The
reader returns null once stdin has ended, LineProcess.write drops writes
after kill(), and stdin gets an error listener so an EPIPE from a child
already gone is not an unhandled error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@xxchan
xxchan merged commit 91b2eef into main Oct 9, 2026
13 checks passed
@xxchan
xxchan deleted the cindy/context-breakdown branch October 9, 2026 07:23
@xxchan xxchan mentioned this pull request Oct 9, 2026
xxchan added a commit that referenced this pull request Oct 9, 2026
Release OAR and CLI 0.46.0. This PR changes only the two package version
fields.

**Added: `Session.contextBreakdown()`** (#264, closes #262)
- What fills the context window, by the runtime's own categories, read
on demand and never recorded: `{ tokens, contextWindow, categories: [{
name, tokens, kind, items? }] }`. `kind` is `used | deferred | reserved
| free | unknown`; names stay the runtime's words.
- claude only (`get_context_usage`, `detail: "full"`): System prompt,
System tools, MCP tools, Memory files, Skills, Custom agents, Messages,
Autocompact buffer, Free space, with memory files, skills, agents and
MCP tools itemized. Every other session has no `contextBreakdown`
member; show `contextUsage()` there.
- Each read makes claude send the provider a batch of `count_tokens`
requests (16 to 28 for a small setup), so read when the user asks, never
poll. Null from the start of a dispose and after the exit.

**Added: a request the runtime withdrew leaves `pendingRequests`**
(#266, closes #263)
- New event `app_request_cancelled { requestId }`, read off claude
`control_cancel_request` (claude cancels a pending MCP elicitation when
the turn is interrupted) and codex `serverRequest/resolved`. SessionView
drops the request from `pendingRequests` and marks its `app_request`
part `cancelled: true`.
- `appRequestText(type, body)` beside `appRequestKind`: what a question
asks, or the tool or command an approval is for, read off the native
body; undefined where OAR does not know.

**Behavior changes**
- `RuntimeEventBody` has a new kind, `app_request_cancelled`: an
exhaustive `switch` over event kinds needs a case for it.
- The `app_request` view part now carries the native `body` (it was
declared but never filled).
- The root process exit now empties `pendingRequests`; the parts stay as
they were.
- `LineProcess.write` drops writes once `kill()` has ended stdin, and
stdin gets an error listener, so a write racing a dying child (the abort
fallback's kill) no longer crashes the host with
`ERR_STREAM_WRITE_AFTER_END` or `EPIPE`.

Validation: all 13 checks passed on #264's final head (`2fa106c`) and
#266's final head (`270bc8c`); each merged tree equals its CI tree.
Lookout inspected both against claude 2.1.292 and codex 0.160.1. After
this PR passes CI and merges, tag its exact squash commit as `v0.46.0`
to trigger npm trusted publishing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: exe.dev user <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

Session.contextBreakdown(): claude's /context categories and items, read on demand

1 participant