feat: broadcast per-session context-window usage via presence#59
Open
iRonin wants to merge 1 commit into
Open
Conversation
Sessions now push their live context usage (percent + token counts) on the existing presence heartbeat, so `list` shows each peer's context fill next to its model — e.g. `(claude-opus · 72% ctx (144k/200k))`. Lets an orchestrator spot a near-full peer at a glance without a separate query. Source: ctx.getContextUsage() (feature-detected; older runtimes just omit it). Protocol-additive and backward compatible: SessionInfo + the presence message gain optional contextPct/contextTokens/contextWindow. A value updates; an explicit null CLEARS (the value is unknown right after a compaction — the broker deletes the field rather than freezing a stale-high percentage); undefined leaves it untouched. Unknown percent renders nothing, so a frozen value is never shown as a stale %. Tests: format-context.test.ts (token compaction + render states) and an integration test proving presence carries usage to a peer and null clears it.
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.
What
Sessions now broadcast their live context-window usage (percent + token counts) on the existing presence heartbeat, so
listshows each peer's context fill next to its model — e.g.(claude-opus · 72% ctx (144k/200k)). An orchestrator coordinating several sessions can spot a near-full peer at a glance, without a separate query.Why
When you're driving multiple pi sessions, which peers are close to their context limit is exactly the signal you want when deciding who to hand the next task to (or who to compact). Today that means switching to each session; this surfaces it inline in the peer list.
How
ctx.getContextUsage()(stock SDK —{ tokens, contextWindow, percent }), feature-detected so an older runtime without it simply omits the fields.SessionInfoand thepresencemessage gain optionalcontextPct/contextTokens/contextWindow, pushed on the existing presence syncs (identity + status heartbeat).formatSessionListRowbeside the model.Semantics
null, and the broker DELETES the field rather than carrying the stale-high percentage forward — so a just-compacted peer never shows a misleading high%.%.Tests
format-context.test.ts— token compaction (1.4k/144k) and the render states (percent+tokens / percent-only / nothing-when-unknown).nullCLEARS the value rather than freezing the old one.Full suite green (96/96, including the new tests).