Skip to content

Fix Pi context usage updates during tool loops - #2024

Merged
ymichael merged 1 commit into
mainfrom
bb/investigate-pi-context-window-reporting-thr_ii4sx6ezx2
Aug 20, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/investigate-pi-context-window-reporting-thr_ii4sx6ezx2

Conversation

@ymichael

@ymichael ymichael commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

What was wrong

Pi sampled context-window usage only on SDK agent_end, which fires after the entire agent run. A tool-heavy run contains multiple SDK turn_end events—each after an assistant response and its tool results—so bb's context meter stayed stale throughout the tool loop even though Pi's underlying context estimate was changing. See #2023.

What changed

  • Sample and emit Pi context-window usage on every turn_end, while retaining the existing compaction_end update.
  • Stop sampling again at agent_end; that event is still forwarded normally for completion and checkpoint handling, but the final turn_end already emitted the same context snapshot.
  • Add a bridge regression test with an intermediate tool-result turn and a final response. It asserts that both usage snapshots arrive and that agent_end does not duplicate the final one.
  • Bump HOST_DAEMON_PROTOCOL_VERSION from 141 to 142 because the bundled Pi bridge's daemon-to-server event cadence changed and enrolled daemons need to update.

How you verified

  • pnpm exec turbo run test --filter=@bb/agent-runtime --force -- src/pi/bridge/__tests__/bridge.test.ts — 27 passed. The new regression fails before the fix because only the agent_end sample is emitted.
  • pnpm exec turbo run typecheck --filter=@bb/agent-runtime --filter=@bb/host-daemon-contract --force — passed.
  • pnpm exec turbo run test --filter=@bb/host-daemon-contract --force -- test/contract.test.ts — 38 passed.
  • The full host-daemon-contract suite was also run locally: 51 tests passed and its unrelated fixed gzip-byte measurement test differed under local Node 26.3.1/zlib (payload-size.test.ts); the protocol contract itself passed.

Fixes #2023

AGENT GENERATED: by GPT-5

@ymichael
ymichael force-pushed the bb/investigate-pi-context-window-reporting-thr_ii4sx6ezx2 branch from 419095b to f156cb3 Compare August 20, 2026 08:13
@ymichael
ymichael merged commit 78cd3c4 into main Aug 20, 2026
13 checks passed
@ymichael
ymichael deleted the bb/investigate-pi-context-window-reporting-thr_ii4sx6ezx2 branch August 20, 2026 08:29
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.

Pi context-window usage stays stale during multi-tool turns

1 participant