feat(acp): report Pi context window usage - #97
Open
KorenKrita wants to merge 1 commit into
Open
Conversation
9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Forward Pi's authoritative context-window occupancy through the standard ACP
usage_updatenotification.Pi already exposes the active context via
get_session_stats.contextUsage, but pi-acp did not forward it. ACP clients therefore had to show no context data or guess from model names and cumulative token totals. This is especially visible for custom/extended-context models (for example, a configured 1M window can be shown as 258K or 200K).What changed
contextUsage.tokensto ACPusedcontextUsage.contextWindowto ACPsizesession/newsession/loadagent_settled, before resolvingsession/prompttokens: nullor invalid/non-integral valuestokens.totalget_session_statsbest-effort with a 1s RPC timeoutThe
agent_settledboundary is important on current main:agent_endmay be followed by retry, compaction, or queued continuation work.Compatibility
Older Pi versions that do not expose
contextUsagecontinue to work; no usage update is emitted. Immediately after compaction, Pi may returntokens: null, so clients retain the previous reading until a trustworthy estimate is available.This is a current-main implementation of the behavior discussed in #48, #75, and #87. Those PRs are now conflicting and/or target the older
agent_endlifecycle.Validation
npm run format -- --checknpm run typechecknpm run lintnpm test— 110 passed, 0 failednpm run buildgit diff --checkA live Pi 0.83 RPC probe returned:
{ "contextUsage": { "tokens": 100, "contextWindow": 1000000, "percent": 0.01 } }The adapter maps that to:
{ "sessionUpdate": "usage_update", "used": 100, "size": 1000000 }Tests cover initial/new and loaded sessions, both model-switch entry points,
agent_settleddelivery ordering, cancellation, null/invalid usage, RPC failures/timeouts, pending-request cleanup, and late-response handling.