feat(cli/sdk): expose /context usage data in non-interactive mode and SDK API#2916
feat(cli/sdk): expose /context usage data in non-interactive mode and SDK API#2916
Conversation
- Extract collectContextData() from contextCommand.ts for shared usage. - Register /context in ALLOWED_BUILTIN_COMMANDS_NON_INTERACTIVE. - Extend SDK control protocol with GET_CONTEXT_USAGE request. - Implement handleGetContextUsage in SystemController for programmatic token queries. - Expose getContextUsage() method in the TypeScript SDK Query interface.
📋 Review SummaryThis PR implements non-interactive 🔍 General Feedback
🎯 Specific Feedback🔴 Critical
🟡 High
🟢 Medium
🔵 Low
✅ Highlights
|
Code Coverage Summary
CLI Package - Full Text ReportCore Package - Full Text ReportFor detailed HTML reports, please see the 'coverage-reports-22.x-ubuntu-latest' artifact from the main CI run. |
…eature - Add missing `get_context_usage` route in ControlDispatcher (SDK calls would throw) - Fix `executionMode` defaulting: use `?? 'interactive'` to match other commands - Validate dynamic import of `collectContextData` before invoking - Preserve original error message in handleGetContextUsage catch block - Add ControlDispatcher test for get_context_usage routing - Add JSDoc comment for context command in non-interactive allowlist Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Thanks for the detailed review! Here's the resolution for each item: 🔴 Critical — Both are false positives
These were present in the original commit. 🟡 High — 1 fixed, 1 intentionally kept
🟢 Medium — Not changing in this PR
🔵 Low — 1 fixed, 1 kept
Additional fixes in 32ee12e (found during manual audit)
|
There was a problem hiding this comment.
Pull request overview
Adds a programmatic “context usage” diagnostic that works in non-interactive CLI mode and is queryable via the SDK control plane, reusing the existing /context implementation.
Changes:
- Extracts
/contextdata collection intocollectContextData()and uses it for both interactive and non-interactive output. - Extends the CLI↔SDK control protocol with a new
get_context_usagerequest and exposes it viaQuery.getContextUsage(). - Enables
/contextinALLOWED_BUILTIN_COMMANDS_NON_INTERACTIVEand routes the new control request through the non-interactive control dispatcher.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/sdk-typescript/src/types/protocol.ts | Adds get_context_usage request type + enum entry to the SDK protocol types. |
| packages/sdk-typescript/src/query/Query.ts | Exposes getContextUsage() on the TS SDK Query API. |
| packages/cli/src/ui/commands/contextCommand.ts | Extracts and exports collectContextData(); adds non-interactive /context JSON output path. |
| packages/cli/src/nonInteractiveCliCommands.ts | Allows context as a non-interactive built-in command. |
| packages/cli/src/nonInteractive/types.ts | Adds CLIControlGetContextUsageRequest to CLI non-interactive control types. |
| packages/cli/src/nonInteractive/control/controllers/systemController.ts | Implements handleGetContextUsage control handler and advertises capability. |
| packages/cli/src/nonInteractive/control/ControlDispatcher.ts | Routes get_context_usage requests to SystemController. |
| packages/cli/src/nonInteractive/control/ControlDispatcher.test.ts | Adds routing test for get_context_usage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…Usage Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
d468728 to
11b4c18
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ta filter Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
yiliang114
left a comment
There was a problem hiding this comment.
I think the only follow-ups I'd still make before merge are: letting showDetails actually change the non-interactive response shape, and adding a small regression test for the new getContextUsage() path. Other than that, this looks pretty good.
- When showDetails is false, return empty detail arrays instead of full data so /context and /context detail produce different payloads - Add unit test for Query.getContextUsage() covering request payload and response handling
- Remove leaked `type: 'context_usage'` from control response payload - Add GET_CONTEXT_USAGE to Java SDK protocol mirror (enum, interface, union type)
Summary
Enable
/contextcommand in non-interactive (SDK) mode and expose a programmaticgetContextUsage()API, so SDK consumers can query context window utilization without a terminal UI.Comparison with Claude Code
/contextcommand/contextin non-interactive modegetContextUsage()APIGET_CONTEXT_USAGEcontrol protocolcollectContextData()reusable functionBefore
/contextonly worked in interactive CLI mode — it rendered a rich TUI widgetAfter
contextCommand.ts: extracted a purecollectContextData(config, showDetails)function that returns aHistoryItemContextUsageobject with no UI side-effects/contextregistered inALLOWED_BUILTIN_COMMANDS_NON_INTERACTIVE— works in SDK/non-interactive sessions (returns JSON)GET_CONTEXT_USAGEcontrol request type in the SDK protocol, handled bySystemController.handleGetContextUsage()query.getContextUsage(showDetails?)convenience method on the TypeScript SDKQueryclassChanges
contextCommand.tscollectContextData(), addexecutionModebranchingnonInteractiveCliCommands.tscontextcommandControlDispatcher.tsget_context_usageto SystemControllersystemController.tsget_context_usagerequest with abort signal checkstypes.ts(cli + sdk)CLIControlGetContextUsageRequesttypeprotocol.tsGET_CONTEXT_USAGEenum memberQuery.tsgetContextUsage()methodControlDispatcher.test.tsget_context_usageTest plan
/contextin interactive mode — verify output unchanged/context -din interactive mode — verify detailed output unchangedquery.getContextUsage()— verify JSON response with token breakdownquery.getContextUsage(true)— verify detailed breakdown includednpm run build)