Skip to content

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

Description

@ymichael

Summary

During a Pi run with multiple assistant/tool subturns, bb does not refresh the context-window meter until the entire agent run finishes. The meter should update after each Pi turn_end, when the assistant response and its tool results have entered the context used by the next request.

Versions and environment

  • bb 0.39.0 (packaged app), also confirmed in source at 05cc16d140e40c2e901de1046b07738fff89eb9f
  • macOS 26.5.1 (25F80)
  • Pi provider using bundled @earendil-works/pi-coding-agent 0.84.0
  • Worktree environment; browser connected remotely through bb Connect

Steps to reproduce

  1. Start a Pi thread.

  2. Send a prompt that requires more than one model/tool cycle, for example: Read package.json, run git status, then summarize what you found.

  3. While Pi performs the tool calls and subsequent assistant responses, watch the context-window meter.

  4. After the run finishes, inspect the thread's persisted event ordering:

    sqlite3 ~/.bb/bb.db "SELECT sequence, type, item_kind, json_extract(data, '$.contextWindowUsage.usedTokens') FROM events WHERE thread_id = '<thread-id>' ORDER BY sequence;"

Expected vs actual

Actual, trimmed from thr_25drjv95tn:

23|item/started|fileChange|
24|item/started|commandExecution|
25|item/completed|fileChange|
26|item/completed|commandExecution|
27|item/started|fileChange|
28|item/completed|fileChange|
29|item/started|commandExecution|
30|item/commandExecution/outputDelta||
31|item/completed|commandExecution|
32|item/started|agentMessage|
40|item/completed|agentMessage|
41|thread/tokenUsage/updated||
42|turn/completed||
43|thread/contextWindowUsage/updated||5084

The context-window update arrives only after turn/completed, so the meter remains stale throughout the tool loop. Expected: one thread/contextWindowUsage/updated snapshot after every Pi SDK turn_end, including intermediate assistant responses followed by tool results.

Evidence

What you ruled out

  • Not a duplicate of Show Context Window Usage for ACP clients #1111: context-window display exists; this bug is that Pi refreshes it only at full agent completion.
  • Not solely compact-number rounding: no context-window event is emitted during intermediate Pi subturns at all.
  • The SDK context value is not permanently stuck; persisted snapshots increase across completed runs.

Suggested priority and effort (optional)

Medium — affects Pi users during every multi-tool run; no data loss, but the live context indicator is misleading. Effort is low.

Checks

  • I reproduced this on the latest release or on main, or I say above that I could not.
  • I searched open and closed issues for the same problem.
  • If an agent wrote this, the body ends with > AGENT GENERATED: by <model> and links the thread or report.

AGENT GENERATED: by GPT-5

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions