Skip to content

feat(acp): report Pi context window usage - #97

Open
KorenKrita wants to merge 1 commit into
svkozak:mainfrom
KorenKrita:fix/acp-context-usage
Open

feat(acp): report Pi context window usage#97
KorenKrita wants to merge 1 commit into
svkozak:mainfrom
KorenKrita:fix/acp-context-usage

Conversation

@KorenKrita

Copy link
Copy Markdown

Summary

Forward Pi's authoritative context-window occupancy through the standard ACP usage_update notification.

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

  • map contextUsage.tokens to ACP used
  • map contextUsage.contextWindow to ACP size
  • publish usage:
    • after session/new
    • after session/load
    • after model changes
    • after agent_settled, before resolving session/prompt
  • skip updates when Pi reports tokens: null or invalid/non-integral values
  • never substitute cumulative tokens.total
  • omit cost because Pi's stats do not carry an explicit currency
  • make get_session_stats best-effort with a 1s RPC timeout
  • remove timed-out requests from the RPC pending map and swallow late responses

The agent_settled boundary is important on current main: agent_end may be followed by retry, compaction, or queued continuation work.

Compatibility

Older Pi versions that do not expose contextUsage continue to work; no usage update is emitted. Immediately after compaction, Pi may return tokens: 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_end lifecycle.

Validation

  • npm run format -- --check
  • npm run typecheck
  • npm run lint
  • npm test — 110 passed, 0 failed
  • npm run build
  • git diff --check

A 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_settled delivery ordering, cancellation, null/invalid usage, RPC failures/timeouts, pending-request cleanup, and late-response handling.

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