bridge: emit context-window stats in inbox pull meta#150
Merged
benvinegar merged 1 commit intomainfrom Feb 23, 2026
Merged
Conversation
Greptile SummaryExtends broker pull telemetry by persisting context-window stats from the agent and emitting them to the broker for analytics. The implementation uses atomic file writes and best-effort reads to avoid disrupting runtime.
Minor inconsistency found: Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Agent as Pi Agent Extension
participant FS as File System
participant Bridge as Broker Bridge
participant Broker as Remote Broker
Note over Agent: Event triggers (session_start, turn_end, tool_result)
Agent->>Agent: buildContextUsageSnapshot()
Agent->>Agent: Calculate tokens, costs, percentages
Agent->>FS: Write to ~/.pi/agent/context-usage.json.tmp
Agent->>FS: Atomic rename to context-usage.json
Note over FS: Snapshot persisted (best-effort)
Note over Bridge: Broker pull interval
Bridge->>FS: Read ~/.pi/agent/context-usage.json
Bridge->>Bridge: Parse and validate numeric fields
Bridge->>Bridge: Merge into buildPullMeta()
Bridge->>Broker: POST /api/inbox/pull with meta
Note over Broker: Analytics Engine ingestion
Last reviewed commit: e3f9415 |
| }; | ||
|
|
||
| pi.on("tool_result", (event: ToolResultEvent, ctx: ExtensionContext) => { | ||
| void persistSnapshotFromContext(ctx); |
There was a problem hiding this comment.
Inconsistent async handling - tool_result uses void (fire-and-forget) while turn_end and session_start use await
Suggested change
| void persistSnapshotFromContext(ctx); | |
| await persistSnapshotFromContext(ctx); |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI
This is a comment left during a code review.
Path: pi/extensions/context.ts
Line: 523
Comment:
Inconsistent async handling - `tool_result` uses `void` (fire-and-forget) while `turn_end` and `session_start` use `await`
```suggestion
await persistSnapshotFromContext(ctx);
```
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise.
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
Add context-window and session token usage stats to broker pull telemetry metadata.
This builds on the merged inbox pull
metasupport and extends baudbot to emit context pressure metrics that the broker can ingest into Analytics Engine.What changed
pi/extensions/context.ts~/.pi/agent/context-usage.json.session_startturn_endtool_resultcontext_window_used_tokenscontext_window_limit_tokenscontext_window_used_pctsession_total_tokenssession_total_cost_usdslack-bridge/broker-bridge.mjs~/.pi/agent/context-usage.json(best effort).POST /api/inbox/pullmeta:context_window_used_tokenscontext_window_limit_tokenscontext_window_used_pctsession_total_tokenssession_total_cost_usdtest/broker-bridge.integration.test.mjsmetawhen snapshot file is present.CONFIGURATION.md~/.pi/agent/context-usage.json.Validation
npm test -- test/broker-bridge.integration.test.mjsnpm run typecheckFollow-up
Broker-side mapping in
modem-dev/baudbot-servicesis still required to store/query these fields in Analytics Engine.