Skip to content

feat(acp-adapter): forward context-window usage as ACP usage_update session updates#1886

Open
askie wants to merge 2 commits into
MoonshotAI:mainfrom
askie:fix/acp-usage-update
Open

feat(acp-adapter): forward context-window usage as ACP usage_update session updates#1886
askie wants to merge 2 commits into
MoonshotAI:mainfrom
askie:fix/acp-usage-update

Conversation

@askie

@askie askie commented Jul 18, 2026

Copy link
Copy Markdown

Problem

Context-window usage is visible in the interactive TUI (which subscribes in-process to agent.status.updated), but the ACP surface never emits it: the adapter's event map only translates message/thought/tool/plan events, so ACP clients (Zed, IDE integrations, headless bridges) have no way to render a live context gauge. The only ACP-visible form is the plain text of the /status and /usage slash commands.

The ACP spec already defines the wire-level counterpart: the usage_update session update variant ({ used, size }, present in @agentclientprotocol/sdk 0.23 types.gen.d.ts), which Kimi never sends.

Change

  • events-map.ts: new statusToUsageUpdateSessionUpdate() mapping agent.status.updatedusage_update with used = contextTokens, size = maxContextTokens. Returns null for status events that don't carry a usable pair (model/plan/permission-only updates, size <= 0).
  • session.ts: forward main-agent status events through that mapping inside the prompt loop, deduped per turn on an unchanged (used, size) pair — status events also fire for plan/permission toggles where the token count didn't move, and those shouldn't hit the wire. Turn-scoped dedupe on purpose: a fresh turn re-sends the current value so a client that (re)connected between turns still gets a baseline. Subagent status events are filtered out.

Testing

  • New unit test covering: baseline pair pushed, unchanged pair deduped, subagent filtered, token-less status skipped, moved pair pushed (session-prompt.test.ts).
  • Full packages/acp-adapter suite: 36 files / 308 tests pass.
  • Live verification: drove a real kimi acp session over stdio; the turn emitted usage_update {"used":23810,"size":1048576} alongside the usual chunks, and stopReason behavior is unchanged (usage_update is meta-only for clients that ignore it).

askie added 2 commits July 18, 2026 22:36
…ge_update

The ACP adapter never surfaced context-window usage on the wire even
though agent-core emits it on every context change; clients (e.g. the
grix-connector toolbar) had no way to render a live context gauge.
Forward main-agent agent.status.updated events as the spec's
usage_update session update ({used, size}), deduped per turn on an
unchanged pair.
@changeset-bot

changeset-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2381274

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@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: 238127446d

ℹ️ 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".

});
return;
}
if (event.type === 'agent.status.updated') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Forward usage updates during /compact

When an ACP client invokes /compact, runBuiltInCommand routes to runCompactCommand instead of this runTurnBody listener, while the core compaction path updates the context and emits agent.status.updated after reducing the token count. Because the new usage_update forwarding only lives in the prompt-turn listener, the client’s context gauge remains at the pre-compaction usage until another model turn happens. Please reuse the mapper in the compaction subscription or send a status snapshot after compaction completes.

Useful? React with 👍 / 👎.

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