Skip to content

feat: broadcast per-session context-window usage via presence#59

Open
iRonin wants to merge 1 commit into
nicobailon:mainfrom
iRonin:feat/context-usage
Open

feat: broadcast per-session context-window usage via presence#59
iRonin wants to merge 1 commit into
nicobailon:mainfrom
iRonin:feat/context-usage

Conversation

@iRonin

@iRonin iRonin commented Jul 2, 2026

Copy link
Copy Markdown

What

Sessions now broadcast their live context-window 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)). 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

  • Source: ctx.getContextUsage() (stock SDK — { tokens, contextWindow, percent }), feature-detected so an older runtime without it simply omits the fields.
  • SessionInfo and the presence message gain optional contextPct / contextTokens / contextWindow, pushed on the existing presence syncs (identity + status heartbeat).
  • Rendered in formatSessionListRow beside the model.

Semantics

  • Protocol-additive / backward compatible: the fields are optional; an older client that never reports them just renders nothing. Zero behavior change to existing actions.
  • null = CLEAR: right after a compaction the value is unknown (before the next assistant response). The source then emits 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 %.
  • Unknown percent renders nothing: a frozen value is never displayed as a stale %.

Tests

  • format-context.test.ts — token compaction (1.4k / 144k) and the render states (percent+tokens / percent-only / nothing-when-unknown).
  • An integration test proving presence carries usage to a peer, and that a subsequent null CLEARS the value rather than freezing the old one.

Full suite green (96/96, including the new tests).

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.
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