pi-jar 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,13 +19,13 @@ The showcase is captured from a real pi-jar terminal session: the torch-style `
19
19
  | **Plan mode** | Read-only exploration; the agent must write a structured plan file and submit it. You review it in a split view (headings on the left, section on the right) and approve, compact-and-approve, refine or stop. |
20
20
  | **Goal mode** | Set an outcome; the agent must break it into tracked tasks and keeps working until they are done, then an **auditor** pass verifies the goal before it can be marked complete. |
21
21
  | **Roles** | Named model roles (`default`, `smol`, `slow`, `plan`, `implement`, `advisor`, `moderator`, `scout`, `worker`, `reviewer`, `task`, `commit`, plus your own) with aliases, effort, per-role fallback models and project overrides. |
22
- | **Advisor** | A second-opinion model: the agent calls `jar_advisor` for risky decisions or when stuck, `/advisor [focus]` asks on demand, and automatic gates consult it when the agent repeats the same tool call or keeps failing. |
23
- | **Usage & context** | `/usage` shows session cost and tokens per model (advisor and commit calls included) and plan-limit bars with reset times; `/context` draws a grid of what fills the context window, with a per-file, per-skill and per-tool breakdown. |
22
+ | **Advisor** | A second-opinion model: the agent calls `jar_advisor` when you ask or after two failed attempts, `/advisor [focus]` asks on demand, and opt-in automatic gates consult it when the agent repeats the same tool call or keeps failing. |
23
+ | **Usage & context** | `/usage` shows session cost and tokens per model (advisor and commit calls included) and plan-limit bars with reset times; `/context` draws a grid of what fills the context window, with a per-file, per-skill and per-tool breakdown; `/cache-breaks` names the tool, system section or message whose change forced a costly prompt-cache rewrite. |
24
24
  | **Commit** | `/jar commit [note]` drafts a message for the staged changes with the `commit` role, lets you edit it, then commits (never pushes). |
25
25
  | **Tasks & questions** | A Claude/omp-style `jar_todo` checklist with subtasks and per-task progress that the agent maintains itself, and `jar_ask` structured questions with options, multi-select and free-form answers. |
26
26
  | **Change review** | Every file the agent or its editing subagents change is remembered as it was; `/diff` (<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>D</kbd>) shows changed files (each counted once) next to a colored diff, with accept or revert per file or all at once. |
27
27
  | **Background shells** | `jar_shell` runs dev servers, watchers and long tests in the background; watch/exit events wake an idle agent or reach it at the next safe turn boundary. Completion status appears immediately in the UI even during model reasoning. Running shells get a footer row; click it (or <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>A</kbd>) to tail or kill them. |
28
- | **Subagents** | The main agent becomes a moderator over a configurable retained pool (2, 4, 6, 8 or 16; default 4). `jar_delegate` starts `scout`, `fork` or sandboxed `worktree` agents and returns immediately, so the main agent can keep responding to user steering; Pi RPC event-bus messages deliver initial and resumed turn completions without polling. `jar_subagent` peeks, steers, asks BTW questions, pauses, resumes and stops them. Workers can exchange bounded structured Q/A through `jar_discuss`; worktree changes stay isolated until stop, then safely enter `/diff`. |
28
+ | **Subagents** | The main agent becomes a moderator over a configurable retained pool (2, 4, 6, 8 or 16; default 2, or 4 in an opt-in Swarm profile). It delegates only when there are at least two independent chunks that each need about ten or more tool calls. `jar_delegate` starts `scout`, `fork` or sandboxed `worktree` agents and returns immediately, so the main agent can keep responding to user steering; Pi RPC event-bus messages deliver initial and resumed turn completions without polling. `jar_subagent` peeks, steers, asks BTW questions, pauses, resumes and stops them. Workers can exchange bounded structured Q/A through `jar_discuss`; worktree changes reach `/diff` only when the moderator stops a worker. |
29
29
  | **Sessions & history** | Recent sessions (with their goal and plan) on the welcome card, one click from resuming; <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>H</kbd> searches earlier prompts; pasted images show as chips under the composer. |
30
30
  | **Footer & themes** | Responsive footer (model, effort, session, cwd, context, RAM, cost, quota, goal, roles, branch, live subagents and shells) with unicode, [Nerd Font](https://www.nerdfonts.com) or ascii icons, and seven dark themes. |
31
31
 
@@ -59,6 +59,7 @@ Pi only delivers mouse events in its **fullscreen** TUI mode. In regular mode th
59
59
  | `/profiles` | Create or switch profiles. `/profiles new` creates (name and theme, then roles), `/profiles <name>` switches. |
60
60
  | `/advisor [focus]` | Ask the advisor for a second opinion on the current work; the answer joins the conversation. |
61
61
  | `/usage` · `/context` | Usage (cost, tokens per model, plan limits) and context-window breakdown in one tabbed panel. |
62
+ | `/cache-breaks` | The panel's **Cache** tab: costly prompt-cache breaks in this session, what changed, tokens rewritten and the estimated extra cost. See [Cache break diagnostics](#cache-break-diagnostics). |
62
63
  | `/jar commit [note]` | Draft a commit message for the staged changes with the `commit` role, edit it, and commit. Offers `git add -A` when nothing is staged. |
63
64
  | `/jar` · `/jar settings` | Visual and workflow preferences. |
64
65
  | `/jar status` | One-line status summary. |
@@ -73,7 +74,7 @@ Pi only delivers mouse events in its **fullscreen** TUI mode. In regular mode th
73
74
  | `/jar ask [question]` | Answer a question in a dialog and insert the answer into the editor. |
74
75
  | `/jar accent [preset]` · `/jar footer` · `/jar icons [unicode\|nerd\|ascii]` | Switch a loaded accent theme; toggle footer fields; pick the icon set. |
75
76
  | `/jar composer on\|off` · `/jar animations on\|off` · `/jar ui on\|off` | Toggle the composer, motion, or all pi-jar UI. |
76
- | `/jar quota on\|off` | Session-only, read-only quota lookups for supported OAuth providers. Lookups start on session boundaries (startup, prompts, model changes), never from a render: at most one per provider every 5 minutes, and failures back off up to 30 minutes. |
77
+ | `/jar quota on\|off` | Session-only, read-only quota lookups for supported OAuth providers. Lookups start on session boundaries (startup, prompts, model changes) and when `/usage` opens, never from a render: at most one per provider every 5 minutes, and failures back off up to 30 minutes. |
77
78
  | `/jar hub` | Open an installed task or subagent manager command. |
78
79
  | `/jar demo` · `/jar reset` | Labeled sample roles in the footer / back to live data. |
79
80
 
@@ -225,7 +226,7 @@ The advisor is a second model (the `advisor` role; the current model if unassign
225
226
 
226
227
  - **`jar_advisor({ question?, draft? })`** — starts a background second opinion and immediately returns a request ID. Use `action: "status"`, `"get"`, bounded `"wait"` (at most 30 seconds), or `"cancel"` with that ID; a pending receipt is **not** reviewed approval. Advice arrives at a safe model boundary or wakes an idle agent, with immediate UI completion feedback. Requests have a 60-second deadline and bounded context; `/advisor` and automatic stuck-work gates are non-blocking too. Get completed advice before consequential decisions. Available in plan mode too.
227
228
  - **`/advisor [focus]`** — ask on demand; the answer is added to the conversation (visible) for the agent's next turn.
228
- - **Gates** — when the agent makes the same tool call three times within its last eight calls, the call is blocked and the advisor's review is returned instead; after three failing tool results in a row, the advice is steered into the running turn. At most two automatic consultations per prompt.
229
+ - **Gates** (opt-in, off by default) — when the agent makes the same tool call four times within its last eight calls, the call is blocked and the advisor reviews in the background. Re-reads, `jar_shell` `output`/`wait`/`peek` polls and test-runner reruns (`npm test`, `node --test`, `pytest`, `go test`, …) never count as a loop. After three failing tool results in a row, the advice is steered into the running turn. At most two automatic consultations per prompt.
229
230
  - Settings → Pi toggles the advisor and the gates. Advisor calls are counted in `/usage`. The footer and welcome show it as a working teammate while it thinks.
230
231
 
231
232
  ### Role fallback models
@@ -250,10 +251,37 @@ This is a pi-jar enhancement: [pi-advisor](https://github.com/philipbrembeck/pi-
250
251
 
251
252
  ## Usage and context
252
253
 
253
- `/usage` and `/context` open one tabbed panel (<kbd>Tab</kbd> switches, <kbd>Esc</kbd> closes):
254
+ `/usage`, `/context` and `/cache-breaks` open one tabbed panel (<kbd>Tab</kbd> switches, <kbd>Esc</kbd> closes):
254
255
 
255
- - **Usage** — total cost, duration, prompts and responses, tokens (input, output, cache read/write); a per-model breakdown including advisor and commit calls; and, for Anthropic and OpenAI Codex subscriptions, 5-hour and weekly limit bars with reset times (lookups follow `/jar quota on|off`).
256
+ - **Usage** — total cost, duration, prompts and responses, tokens (input, output, cache read/write); a per-model breakdown including advisor and commit calls; and, for Anthropic and OpenAI Codex subscriptions, 5-hour and weekly limit bars with reset times (lookups follow `/jar quota on|off`). A failed lookup shows its reason (for example the provider's HTTP 429 rate limit) and when it retries, instead of loading indefinitely.
256
257
  - **Context** — a 10×10 grid (each cell ≈ 1% of the window) beside a legend: system prompt, tools, context files, skills, compaction summary, user and assistant messages, tool results, extension messages, free space and the autocompact buffer. Parts are estimated at ~4 characters per token and scaled to the provider-reported total when one is known; context files, skills and tools are listed individually below.
258
+ - **Cache** — the costly prompt-cache breaks of this session with totals by cause; see below.
259
+
260
+ ### Cache break diagnostics
261
+
262
+ Providers cache a prompt as an ordered prefix: tools, then the system prompt, then the messages. Change anything early in a request and the provider re-processes, and bills again, everything after it. Pi tells you *that* a call missed the cache; pi-jar tells you *what changed*. It is on by default (**`/jar settings` → Pi → Cache diagnostics**, saved per profile) and only observes: it never modifies a request or a message.
263
+
264
+ - **How it finds the change.** For every provider request pi-jar reduces the final payload (Anthropic Messages, OpenAI Responses and Chat Completions; other shapes are skipped) to an ordered list of segments: the model, each tool, each system-prompt section (`<tools>`, `<skills>`, … or markdown headings and paragraphs) and each message, labelled by role or tool name. A segment keeps only a hash and a size, never content, and the cache markers that move with every call are ignored. The first segment where the previous request is no longer a prefix of this one is the change.
265
+ - **When it matters.** The call's own usage decides. A *costly break* is a call whose cache read fell more than 4,096 tokens short of the previous prompt, or that read nothing from a prompt over 8,192 tokens. Appending messages never is one. The first call of a session, and the first after compaction or tree navigation (which rewrite the history on purpose), have nothing to compare with, and providers that never report cache reads are skipped.
266
+ - **What you see.** One warning per costly break, with the extra cost estimated from the call's own prices, and the **Cache** tab (`/cache-breaks`) with totals by cause:
267
+
268
+ ```text
269
+ cache break: tools changed (+jendral_build_get, +17 more) — rewrote 235k tokens (~$1.89)
270
+ ```
271
+
272
+ | Cause | Reading it |
273
+ | --- | --- |
274
+ | `tools changed (+a, -b, ~c)` | Tools were added, removed or redefined, or `reordered`. Activating tools mid-session (an MCP server, a tool package) rewrites the whole prompt. |
275
+ | `system prompt changed (~<skills>)` | The named sections changed (`~`), appeared (`+`) or went (`-`). |
276
+ | `message 14/87 shrank (tool result: read, 52k → 98 chars, 73 from the end)` | An earlier message was rewritten, for example by an extension that replaces old tool results with placeholders; everything after it is re-processed. `first change` in the tab gives its position in the ordered prompt. |
277
+ | `history shortened (87 → 40 messages)` | Messages were dropped from the end of the history. |
278
+ | `model changed (a → b)` | A different model or provider has its own cache. |
279
+ | `cache expired after 12m idle` | The prompt is unchanged, but more than about 5 minutes passed since the cache was last warm (Pi's cache warming counts). |
280
+ | `no prompt change found` | The prefix is identical, so the content of the prompt does not explain the miss: provider-side eviction, or a request setting outside the fingerprint such as the thinking level or `tool_choice`. |
281
+
282
+ A change is followed by `, after 12m idle` when the cache had also lapsed.
283
+ - **Where it is kept.** Each costly break is appended as one JSON line to `pi-jar-cache-breaks/<session id>.jsonl` in Pi's agent directory, so it survives `/reload` and resuming the session: `timestamp`, `request` (provider calls pi-jar has seen for the session), `kind`, `summary`, `segment` and `first` (the first changed segment), `model`, `promptTokens`, `cacheReadTokens`, `rewrittenTokens`, `costUsd` and `idleMs`. Hashes and prompt text are never written.
284
+ - **Limits.** pi-jar fingerprints the payload as it sees it; a `before_provider_request` handler of an extension loaded after pi-jar can still change it. Subagent processes do not run the diagnostic.
257
285
 
258
286
  ## Tasks, questions and history
259
287
 
@@ -266,14 +294,16 @@ This is a pi-jar enhancement: [pi-advisor](https://github.com/philipbrembeck/pi-
266
294
  Use `parent` with `append` to add a genuinely new subtask to active work; `start`, `done`, `open`, `edit`, `list`, `add` and `delete` remain available.
267
295
  - **`jar_ask`** — structured questions: numbered options with descriptions, single or multi-select, *Type your own answer* (multi-line, paste-friendly) and *Chat about this* to discuss before choosing.
268
296
  - **`/diff`** — review what the agent (and its editing subagents) changed since the first edit to each file: files with `+/−` counts on the left, a full-path header and numbered, colored diff on the right. Changed lines get a colored rail and tinted background, replaced lines highlight the changed words, code is syntax-highlighted for known languages, and collapsed regions are labeled `┄ N unchanged lines ┄`. `/` filters files; `t` switches unified and aligned side-by-side views (unified below 70 columns), `←`/`→` (or `[`/`]`) change the context from 0 lines to the whole file, `w` wraps long lines, and `n`/`p`, `Home`/`End` navigate. Large changes offer summary or explicit windowed review (`v`), with clearly labeled coarse fallback and existing safety limits—not arbitrary-file streaming. See [diff review](docs/DIFF-REVIEW.md). `a` accept (keep, stop tracking), `r` then `y` revert (restore the original, or remove a file it created), `A`/`R` for all. The footer shows `± N files · /diff` while anything is unreviewed; each file counts once no matter who edited it. Only `edit`/`write` changes inside the project are tracked; shell-made changes are not.
269
- - **Context diet (opt-in)** — `/jar settings` → **Pi** → **Context diet (prior-turn reasoning)**. Saved independently per profile; OFF by default. Removes eligible thinking blocks from completed earlier user turns only in outgoing context, never from stored history. Current reasoning signatures, user requests, tool arguments/results, and workflow state remain intact; ambiguous tool chains are left unchanged. Opaque signature character counts are diagnostics, not token savings. Existing SoL-Pi observation packing remains responsible for tool-result projection.
270
- - **`jar_shell`** — `start` (optional `name`, `watch` regex, `notify`, task/service `purpose`), `list`, `peek`, bounded `wait`, `output`, `kill`. ANSI-stripped logs have per-job caps and a 4 MiB aggregate budget; surfaced finished jobs compact to a 32 KiB tail. Live jobs retain useful tails and are never killed to reclaim logs. Watch/exit notifications are compact and coalesced; reads acknowledge observed events. Active model responses cannot be interrupted by a completion event: the UI previews status without consuming it, and model delivery waits for a safe boundary. When no useful work remains, use bounded `wait` for the relevant finite jobs rather than waiting for an automatic wake-up. Each running shell gets a footer row and an activity view (`/jar shells`) with `x` kill and `f` follow. All shells stop when the session ends.
271
- - **`jar_delegate`** — spawns retained, session-scoped subagents up to **Max subagents** in `/jar settings` (2, 4, 6, 8 or 16; default 4). Idle, paused and hibernated agents count toward this pool; over-cap batches are rejected. `scout` is fresh/read-only, `fork` inherits conversation read-only, and `worktree` forks context into a path-guarded Git worktree. Settled retained agents close their Pi process after 30 seconds of quiet while keeping their private session and workspace; `resume`/`ask` relaunch that same session. Launch returns before startup or the first turn completes, including legacy one-shot `write: true` mode.
297
+ - **Context diet (opt-in)** — `/jar settings` → **Pi** → **Context diet (prior-turn reasoning)**. Saved independently per profile; OFF by default. Removes eligible thinking blocks from completed earlier user turns only in outgoing context, never from stored history. In the same completed turn, a `read` result that a later read of the same range or a successful edit/write of the same file superseded becomes a one-line stub; each turn is rewritten only once, when it completes, so later prefix-cache hits are kept. Current reasoning signatures, user requests, tool call/result pairing and workflow state remain intact; ambiguous tool chains are left unchanged. Opaque signature character counts are diagnostics, not token savings.
298
+ - **Context budget** — `/jar settings` → **Pi** → **Context budget** (`off`, `suggest` default, `compact`) and **Context budget limit** (80k–200k, default 120k). When the last call's prompt (input plus cache) passes the limit, the footer's context metric turns amber and `suggest` shows one notice per crossing (`/compact`, or `/new` for the next task). `compact` compacts once at the next safe point: right after a `jar_todo` item completes while work remains (the run stops after that turn, compacts, then continues with a hidden note), or when the run settles; it never cuts a tool chain. The footer shows the average cost per call over the last ten calls next to the total. A prompt after a finished task list in a large context suggests `/new`; a prompt after more than five idle minutes with over 100k context warns that the provider cache has probably expired. No keep-warm requests are sent.
299
+ - **Read hygiene** — a whole-file `read` (no offset/limit) of a file over 50 KB returns its first 400 lines with a note on how to continue. Opt-in **Repeated-read stub** (`/jar settings` → **Pi**): re-reading the same range of an unchanged file returns `[unchanged since read #N (312 lines); pass force: true to re-read]`. Any edit/write of the file, a changed file revision, compaction, branch navigation or a new session clears it.
300
+ - **`jar_shell`** — `start` (optional `name`, `watch` regex, `notify`, task/service `purpose`, `waitMs` up to 30 s), `list`, `peek`, bounded `wait`, `output`, `kill`. Use plain `bash` for finite commands expected to finish in about two minutes; `jar_shell` is for services, watchers and long jobs. `start` with `waitMs` returns the exit code and output tail in one call when the job finishes in time (acknowledged, so no later notification), otherwise the output so far; Esc cancels only the wait. `output` returns only lines new since your last read (with counts of trimmed or skipped lines); `all: true` repeats the trailing window. ANSI-stripped logs have per-job caps and a 4 MiB aggregate budget; surfaced finished jobs compact to a 32 KiB tail. Live jobs retain useful tails and are never killed to reclaim logs. Watch/exit notifications are compact and coalesced; reads acknowledge observed events. Active model responses cannot be interrupted by a completion event: the UI previews status without consuming it, and model delivery waits for a safe boundary. When no useful work remains, use bounded `wait` for the relevant finite jobs rather than waiting for an automatic wake-up. Each running shell gets a footer row and an activity view (`/jar shells`) with `x` kill and `f` follow. All shells stop when the session ends.
301
+ - **`jar_delegate`** — spawns retained, session-scoped subagents up to **Max subagents** in `/jar settings` (2, 4, 6, 8 or 16; default 2). Idle, paused and hibernated agents count toward this pool; over-cap batches are rejected. `/profiles` → **Create Swarm profile** copies the current settings with a pool of 4. `scout` is fresh/read-only, `fork` inherits conversation read-only, and `worktree` forks context into a path-guarded Git worktree. Settled retained agents close their Pi process after 30 seconds of quiet while keeping their private session and workspace; `resume`/`ask` relaunch that same session. Launch returns before startup or the first turn completes, including legacy one-shot `write: true` mode. Turn and stop events carry a summary of at most 400 characters plus changed files; all events pending at one boundary arrive as one `pi-jar.subagent` message, together with the moderator's unread discussion mail. `/roles` warns when the `scout` or `reviewer` role would run on the main model while a cheaper model is available.
272
302
  - **`jar_subagent`** — `peek` reports progress, including the workspace path and changed files for recovery records; `steer` redirects a running worker; `resume` relaunches an idle/paused/hibernated agent; `report` returns an agent's latest full report (a synchronous `jar_delegate` result shares a 16k-character report budget across its runs and points here when it truncates). `ask`, `pause` and `stop` return accepted receipts immediately; answers and final handoffs arrive as `pi-jar.subagent` events. `stop` reconciles safe worktree changes; conflicts remain private and can be retried with `stop` after resolving parent drift. `discard` removes an unresolved recovery worktree without applying it. At most four active/recovery worktrees are admitted; resolve or discard one before starting another. Session shutdown reports paths of any preserved worktrees. Controls do not poll the child.
273
303
  - **Subagent capabilities** — each task can set `tools` or `inheritTools: true`; known-tool mode filters and child guards remain in force. See [tool capabilities](docs/SUBAGENT-TOOLS.md).
274
304
  - **Worktree finalization** — edits do not touch the parent merely because a worker settles or hibernates. On `jar_subagent stop`, pi-jar checks parent drift and reviewability, applies safe changes and adds them to `/diff`; conflicts remain isolated for retry or explicit discard. Failed worktree transcripts are compacted independently of finished history. Process closure is awaited before finalization, and failed multi-file application rolls back rather than leaving a partial result.
275
- - **`jar_democracy`** — **exceptional only**, for a persistent super-complex issue with several evidence-backed options after at least two distinct failed approaches. The moderator opens 2–8 options, resumes relevant idle/paused/hibernated fresh-context read-only scouts or spawns new scouts within the pool, and collects one private ballot per voter. A strict majority of invited voters wins; ties, plurality or missing/invalid ballots require user choice. Voting never implements its recommendation. A round has a 10-minute deadline and bounded rationales.
276
- - **`jar_discuss`** — session-local, parent-owned discussion broker over authenticated local IPC; no shared paper, lock or polling. `ask`/`answer` return stable IDs without echoing your text. `list` reads only relevant unread questions/answers and advances your cursor; `thread` (or `list` with `questionId`) reads one thread; `pending` shows counts without message bodies. `since: "d0"` rereads relevant mail. At most 128 messages, 1600 characters each and 128 KiB are retained; unread/unanswered questions block new posts instead of being silently lost. Messages do not wake idle agents; the next natural turn gets an unread notice.
305
+ - **`jar_council`** — **exceptional only**, for a persistent super-complex issue with several evidence-backed options after at least two distinct failed approaches. The moderator opens 2–8 options, resumes relevant idle/paused/hibernated fresh-context read-only scouts or spawns new scouts within the pool, and collects one private ballot per voter. A strict majority of invited voters wins; ties, plurality or missing/invalid ballots require user choice. Voting never implements its recommendation. A round has a 10-minute deadline and bounded rationales.
306
+ - **`jar_discuss`** — session-local, parent-owned discussion broker over authenticated local IPC; no shared paper, lock or polling. `ask`/`answer` return stable IDs without echoing your text. `list` reads only relevant unread questions/answers and advances your cursor; `thread` (or `list` with `questionId`) reads one thread; `pending` shows counts without message bodies. `since: "d0"` rereads relevant mail. At most 128 messages, 1600 characters each and 128 KiB are retained; unread/unanswered questions block new posts instead of being silently lost. Messages do not wake idle agents. Unread mail (never your own messages) arrives once, inline, at your next natural turn start, so no `list` call is needed; the moderator's also rides along with subagent events.
277
307
  - **Activity view** — one split view (<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>A</kbd>, `/jar activity`, or a click on a footer row) for subagents, background shells and other extensions' teammates: list on the left, live details on the right (auto-follows; scroll up to pause, `f` to follow again). For a subagent the details show its task, its own checklist with subtasks, and a transcript like omp's: each tool call and message is one collapsed line that expands on demand (`Tab` into it, `↑↓`, `Enter`, or click) to show arguments and output, while streaming text shows live. `s` opens a steering input: type a message and press `Enter` to redirect the running subagent. `x` stops just that one. Finished runs stay listed for review.
278
308
  - **`/jar history`** — separate, read-only timeline of the active branch (paging, search, expandable details). Pi's native transcript is untouched.
279
309
  - Pi's built-in read/shell/edit/write tool cards render compactly: collapsed, each is one padded, state-colored card with its call line and a one-line summary (edit included), and partially streamed arguments never flicker to a fallback; the full output or diff stays one click or <kbd>Ctrl</kbd>+<kbd>O</kbd> away. Every requested frame re-renders the whole transcript, so background sources (subagent streams, shell changes, context and cost updates) share one adaptive repaint budget: at most every 250 ms below 300 branch entries, 500 ms below 800, 1 s below 2,000, and only on visible state changes beyond that; a frame Pi draws on its own satisfies pending updates. User actions and state changes (a subagent or shell starting, settling or failing) still repaint at once. The open activity view repaints live updates every 100 ms, slowing to 500 ms in very large sessions. Context usage is cached: session start, tree navigation, compaction, model changes, renames and the end of a run refresh it at once, while message ends recompute it at most every 750 ms, widening up to 5 s when the measured computation is slow so sampling stays near a 1% duty cycle. `/jar perf` shows the counters. The sliding bar some terminals show in the tab while the agent works is Pi's own `OSC 9;4` progress (`terminal.showTerminalProgress` in Pi settings), not pi-jar.
@@ -284,7 +314,7 @@ This is a pi-jar enhancement: [pi-advisor](https://github.com/philipbrembeck/pi-
284
314
 
285
315
  - **Appearance** — accent, motion, rounded composer, Ember mascot, next-prompt suggestions, pi-jar UI, icons (`unicode` default, `nerd` for a [Nerd Font](https://www.nerdfonts.com) terminal like omp's nerd preset, `ascii` for plain labels).
286
316
  - **Footer** — field visibility. In fullscreen mode the session name (or `sessions` for an unnamed session) opens the session picker and each subagent/shell row opens the activity view. SoL-Pi's savings status is not repeated in the footer (it already notifies).
287
- - **Pi** — mouse clicks (Pi fullscreen mode), copy on select, goal auto rounds, **Max subagents** (explicit choice then Save; default 4), advisor, advisor gates.
317
+ - **Pi** — mouse clicks (Pi fullscreen mode), copy on select, goal auto rounds, **Max subagents** (explicit choice then Save; default 2, existing saved values are kept), advisor, advisor gates (default off), context diet, cache diagnostics (default on), context budget and its limit, repeated-read stub.
288
318
 
289
319
  pi-jar preferences are saved in `pi-jar-settings.json` in Pi's agent directory; the Pi tab writes Pi's own settings.
290
320
 
@@ -298,6 +328,8 @@ Other extensions can publish teammate roles and quota windows through Pi's publi
298
328
  pi-jar/
299
329
  ├── extensions/index.ts extension entry: wiring, welcome, footer, commands
300
330
  ├── src/
331
+ │ ├── context-budget.ts context budget, per-call cost average, topic and cold-cache hints
332
+ │ ├── cache-breaks.ts prompt-cache break diagnostics: payload fingerprints, diff, usage correlation, JSONL log; cache-breaks-view.ts is its tab
301
333
  │ ├── flame.ts pixel fire simulation
302
334
  │ ├── mascot.ts Ember's moods and sprites
303
335
  │ ├── composer.ts rounded composer, ghost text, mouse mapping
@@ -318,7 +350,7 @@ pi-jar/
318
350
  │ ├── async-process.ts cancellable async subprocess runner with deadlines and tree cleanup
319
351
  │ ├── discussion.ts session-local discussion broker and tool
320
352
  │ ├── discussion-hub.ts bounded in-memory discussion mailboxes
321
- │ ├── democracy.ts exceptional scout ballots, strict majority and user tie-break
353
+ │ ├── council.ts exceptional scout ballots, strict majority and user tie-break
322
354
  │ ├── activity-view.ts subagent/shell details view
323
355
  │ ├── perf.ts long-session render budget, context sampling and /jar perf
324
356
  │ ├── icons.ts unicode / nerd / ascii glyph sets
@@ -43,7 +43,7 @@ npm run check # tsc --noEmit
43
43
  npm test # node:test suites in tests/
44
44
  ```
45
45
 
46
- Tests isolate `PI_CODING_AGENT_DIR`, so they never touch your real preferences. Suites cover the flame simulation, welcome layout and hit-testing, composer framing/expansion/ghost text/mouse mapping, the mascot, plan mode (guards, submission, reminders, restore), the plan view, plan parsing, the goal loop, roles v2, suggestions and settings.
46
+ Tests isolate `PI_CODING_AGENT_DIR`, so they never touch your real preferences. Suites cover the flame simulation, welcome layout and hit-testing, composer framing/expansion/ghost text/mouse mapping, the mascot, plan mode (guards, submission, reminders, restore), the plan view, plan parsing, the goal loop, roles v2, suggestions, settings and the cache-break diagnostic (payload fingerprints for Anthropic and OpenAI shapes, diff classification, usage correlation, the `/cache-breaks` tab). `tests/cache-payloads.ts` builds the provider payloads those suites share.
47
47
 
48
48
  Manual smoke test (offline, throwaway agent directory, nothing sent to a model):
49
49
 
@@ -61,6 +61,7 @@ Then check:
61
61
  - `/jar activity`: select a subagent or background shell, scroll its live transcript/output, pause and resume follow, `Tab` into a subagent's transcript and expand/collapse entries, steer it with `s`, and stop/kill only the selected run;
62
62
  - `/roles`: the split manager, `n` new role, `a` alias, `s` scope;
63
63
  - `/plan`: the status shows `◆ PLAN · read-only` and a directory appears under `$TMPDIR/pi-jar/plans/`.
64
+ - `/cache-breaks`: a fresh session reports no breaks and `/usage` has a **Cache** tab; with a logged-in model, activate an extra tool (or edit an earlier message through an extension) mid-session and expect `cache break: …` after the next call, plus a line in `pi-jar-cache-breaks/<session id>.jsonl` under the temporary agent directory; **Pi → Cache diagnostics** off silences both.
64
65
 
65
66
  For pointer interaction start with `--tui-mode fullscreen` (or enable **Pi → Mouse clicks** and restart). Check that welcome actions fire once per press, that footer activity rows open their matching details, that the footer session indicator opens search, that clicking composer text moves the cursor, that clicking Ember pokes it, that plan-view headings and actions respond to clicks and the wheel, and that selecting text anywhere still copies it. In regular mode use `/jar activity` and `/jar sessions` instead.
66
67
 
@@ -34,6 +34,29 @@ A synchronous `jar_delegate` result shares a 16k-character report budget across
34
34
 
35
35
  The footer's per-tick registry listener reads `DelegateRegistry.stateSignature()` straight from the map instead of `records()` (a second sweep, a sort and two arrays per 50 ms tick).
36
36
 
37
+ ## Session cost reduction
38
+
39
+ Baseline: a 309-call, $6.34 session (`node scripts/session-cost.mjs 01a101ba-…`): 60% of cost was cache reads, average context 125k, 138 calls above 125k context costing $3.87; 109 reads (12 of one file, 7 of a `/tmp` plan), 65 `jar_shell` calls (33 start, 22 output, 8 wait), 19 subagent events (36 KB). Cost scales with calls × context, so every change cuts calls, context per call, or the fixed prompt.
40
+
41
+ | Change | Where | Effect |
42
+ | --- | --- | --- |
43
+ | Context budget (`suggest` by default at 120k; opt-in `compact` at a safe point) and average cost per call in the footer | `src/context-budget.ts` | Long sessions are compacted or split before every call re-sends 200k+ |
44
+ | Topic-switch and cold-cache hints | `src/context-budget.ts` | `/new` for unrelated work; `/compact` before re-sending a large expired context |
45
+ | `bash` for finite commands; `jar_shell start` `waitMs`; incremental `output` | `src/shells.ts` | Short jobs take one call instead of start → output → wait, and repeated reads don't resend old output |
46
+ | 400-line cap on whole reads of files over 50 KB; opt-in repeated-read stub; diet stubs superseded reads | `src/compact-tools.ts`, `src/context-diet.ts` | Smaller tool results and fewer duplicate reads in the context |
47
+ | Pool default 2 (opt-in Swarm profile: 4); delegate only for ≥2 chunks of ≥10 tool calls | `src/settings.ts`, `src/delegate.ts`, `src/profile-ui.ts` | Fewer moderation round-trips |
48
+ | Events: ≤400-char summary + changed files, one message per boundary, discussion mail inlined | `src/delegate.ts`, `src/discussion.ts` | Events shrink from ~1.9 KB to ~0.5 KB; no `list` call to read mail; no echoes of own messages |
49
+ | `/roles` warns when `scout`/`reviewer` run on the main model while a cheaper one is available | `src/model-roles.ts` | Subagent calls move to a cheaper model |
50
+ | Advisor gates off by default; loop threshold 4; re-reads, shell polls and test reruns are never loops | `src/advisor.ts` | No side calls from normal fix loops |
51
+ | Cache-break diagnostics: one notice per costly prompt-cache break, `/cache-breaks`, a per-session JSONL log | `src/cache-breaks.ts`, `src/cache-breaks-view.ts` | The tool list, system section or message that forced a rewrite is named instead of guessed |
52
+ | Rules moved into tool descriptions; `jar_plan_submit`/`jar_goal` active only in their mode | all tool modules, `src/tool-activation.ts` | Smaller fixed prompt (below) |
53
+
54
+ Fixed prompt that pi-jar contributes, measured from Pi's built system message in RPC mode (same project, same Pi 1.0.4): the `rules` section went from 7,584 to 1,675 characters and `tools` from 1,773 to 1,476; two schemas (~900 characters) are no longer sent outside their mode. Tool descriptions and schemas grew by ~1.1k characters because the guidance moved there. Net: ~5k characters (~1.3k tokens) less on every call. That is modest next to context growth, so the context budget and shell/read changes carry most of the saving.
55
+
56
+ Cache breaks: an audit of a $30 session found ~68% of the cost was cache rewrites caused by prompt-prefix changes: tools activated mid-session (the cache read fell to 0 and ~230k tokens were rewritten per call), an extension replacing old tool results with placeholders (the read stuck at a mid-history offset), and one unexplained break at a user-turn boundary. Pi reports *that* a call missed the cache, not why. `src/cache-breaks.ts` fingerprints each provider payload (model, tools, system-prompt sections, messages; hashes and sizes only), finds the first segment where the previous request stopped being a prefix of this one, and joins that with the call's usage; a call whose cache read falls more than 4,096 tokens short of the previous prompt is a costly break. It only observes, and is on by default (`cacheDiagnostics`).
57
+
58
+ Not done: Pi's compaction thresholds and the user's own settings are left alone; recommendations are in [INTEGRATIONS.md](./INTEGRATIONS.md#keeping-sessions-cheap-recommendations). The ≤ $3 field target needs a real re-run of a similar task with the new defaults.
59
+
37
60
  ## Already covered — dropped
38
61
 
39
62
  | Item | Why |
@@ -39,7 +39,7 @@ ctx.ui.setStatus("pi-jar.quota.openai-codex", JSON.stringify({
39
39
  - `used` is a percentage from 0 to 100. Either window may be omitted, and each can carry an optional `resetsAt` (epoch milliseconds).
40
40
  - `expiresAt` is required and may be at most five minutes ahead. A valid published status always wins.
41
41
  - `/jar quota on` (the default each session) lets pi-jar resolve the current Anthropic or OpenAI Codex OAuth credentials through Pi's model registry and make a **read-only** request to the provider's quota endpoint when nothing is published.
42
- - Requests have a 5 s timeout; successful results are cached for 5 minutes, while consecutive failures back off for 5, 10, 20, then 30 minutes. `/jar quota off` cancels pending requests and clears the cache. Renders only read cached values.
42
+ - Requests have a 5 s timeout; successful results are cached for 5 minutes, while consecutive failures back off for 5, 10, 20, then 30 minutes and `/usage` shows the failure reason until the next retry. `/jar quota off` cancels pending requests and clears the cache. Renders only read cached values.
43
43
  - These provider endpoints are not stable public APIs.
44
44
 
45
45
  ## Session entries owned by pi-jar
@@ -58,18 +58,35 @@ Hidden context messages (`pi-jar.plan-context`, `pi-jar.plan-reminder`, `pi-jar.
58
58
  | --- | --- |
59
59
  | `jar_todo` | branch-aware checklist: `todos` full-list writes (`content`, `status` pending/in_progress/completed, `activeForm`), plus `list`, `add`, `start`, `done`, `open`, `edit`, `delete` |
60
60
  | `jar_ask` | structured questions answered in the TUI |
61
- | `jar_plan_submit` | submit a plan file for review (plan mode only) |
62
- | `jar_goal` | `get`, `complete` (with evidence, audit phase only), `block` |
63
- | `jar_suggest` | one next-prompt suggestion shown as composer ghost text |
64
- | `jar_shell` | background shells: `start` (`command`, `name`, `watch`, `notify`), `list`, `output`, `kill` |
65
- | `jar_delegate` | up to four parallel subagents (`tasks: [{task, name?, role?}]`, `write?`) |
61
+ | `jar_plan_submit` | submit a plan file for review; active only in plan mode |
62
+ | `jar_goal` | `get`, `complete` (with evidence, audit phase only), `block`; active only while a goal runs |
63
+ | `jar_suggest` | one next-prompt suggestion shown as composer ghost text; active only when suggestions are enabled |
64
+ | `jar_shell` | background shells: `start` (`command`, `name`, `watch`, `notify`, `purpose`, `waitMs`), `list`, `peek`, `wait`, `output` (new lines; `all`), `kill` |
65
+ | `jar_delegate` | retained subagents within the configured pool (`tasks: [{task, name?, role?, mode?, tools?, inheritTools?}]`) |
66
+ | `jar_subagent` | moderator control: `peek`, `report`, `steer`, `ask`, `pause`, `resume`, `stop`, `discard` |
67
+ | `jar_council` | exceptional scout vote with strict majority; never implements |
68
+ | `jar_discuss` | parent-brokered Q/A between moderator and subagents |
66
69
  | `jar_advisor` | second opinion from the `advisor` role (`question?`, `draft?`) |
67
70
 
71
+ Mode-specific tools are deactivated when their mode is off, so their schema and rules are not sent with every request.
72
+
68
73
  `jar_shell` events arrive as a visible `pi-jar.shell` custom message that triggers (or queues) an agent turn; subagent turn ends and `jar_subagent` control completions arrive as one coalesced, visible `pi-jar.subagent` message at the moderator's next turn boundary, or as a wake-up when it is idle. `jar_delegate` runs child processes with `PI_JAR_CHILD=1`, which keeps pi-jar in the child from registering `jar_delegate` again, and publishes each running subagent through the role contract above as `pi-jar.role.delegate-<batch>-<n>`. The advisor publishes `pi-jar.role.advisor` while it is consulting, and its `/advisor` and gate answers arrive as visible `pi-jar.advisor` custom messages.
69
74
 
70
75
  ## Commands that overlap other packages
71
76
 
72
- pi-jar registers `/usage`, `/context` and `/advisor`. Pi keeps both commands when two extensions register the same name and suffixes the later one (for example `/usage:2`), so remove packages whose commands you no longer need (such as a separate usage, context or advisor extension) to keep the plain names on pi-jar.
77
+ pi-jar registers `/usage`, `/context`, `/cache-breaks` and `/advisor`. Pi keeps both commands when two extensions register the same name and suffixes the later one (for example `/usage:2`), so remove packages whose commands you no longer need (such as a separate usage, context or advisor extension) to keep the plain names on pi-jar.
78
+
79
+ ## Keeping sessions cheap (recommendations)
80
+
81
+ Every model call re-sends the whole context, so cost grows with session length far more than with the work done (see [EFFICIENCY_PLAN.md](./EFFICIENCY_PLAN.md#session-cost-reduction)). pi-jar never edits these files; apply them yourself if they fit:
82
+
83
+ - **Compact earlier.** In `~/.pi/agent/settings.json` set `"compaction": { "reserveTokens": 140000, "keepRecentTokens": 20000 }`. With a 272k-window model this compacts around 130k instead of near the limit. pi-jar's context budget (`/jar settings` → Context budget) suggests `/compact` or `/new` at the same point, or compacts at the next finished task when set to `compact`.
84
+ - **Enable heavy tool packages per project.** A package such as `pi-mono-figma` (~20 tools) sends every schema on every call; enable it only in projects that use it.
85
+ - **Load each MCP server once.** Use codebase-memory either through the lazy `mcp` proxy **or** as direct tools, not both.
86
+ - **Activate tools at startup, not mid-session.** Changing the tool list, the system prompt or an earlier message makes the provider re-process the whole prefix after it: an MCP server or tool package switched on halfway through a long session rewrote ~230k tokens on every call until the cache caught up. pi-jar announces costly cache breaks and `/cache-breaks` lists what changed (see [Cache break diagnostics](../README.md#cache-break-diagnostics)).
87
+ - **Review rarely used packages** (for example `i-have-adhd`, `pi-mono-loop`, `pi-mono-btw`) and enable them per project.
88
+ - **One task per session.** Start `/new` when the next request is unrelated; pi-jar suggests it when a finished checklist is followed by a new prompt in a large context.
89
+ - **Measure.** `node scripts/session-cost.mjs <session.jsonl | id>` (in a pi-jar checkout) reports calls, the cost split, context buckets, repeated reads, tool counts and cache misses for one session.
73
90
 
74
91
  ## Working signals and other managers
75
92
 
@@ -18,7 +18,7 @@ Subagents use Pi's standard `--tools` allowlist. Each `jar_delegate` task can cu
18
18
  - `worktree` accepts known path-guarded file tools and named web tools. Shell and arbitrary extension tools are rejected. `multi_file_edit` is included in defaults only when active in the parent; each effective edit target, default path and recognized alternate path field is validated, including traversal and existing symlink escapes.
19
19
  - Named web capabilities are `web_enable`, `web_search`, `fetch_content`, `get_search_content` and `source_check`. Newly enabled tools must still be in the child's original capability list.
20
20
  - These are tool-call restrictions, not an OS sandbox. Extensions execute trusted code; named web tools can access external services, and legacy direct-write mode is not workspace-confined.
21
- - `jar_delegate`, `jar_subagent`, and `jar_democracy` are always removed from inherited tools and rejected in explicit lists. A child cannot recursively spawn more subagents.
21
+ - `jar_delegate`, `jar_subagent`, and `jar_council` are always removed from inherited tools and rejected in explicit lists. A child cannot recursively spawn more subagents.
22
22
  - Tool capabilities are per task; a batch can mix modes and allowlists. A fork still inherits the parent's conversation context, independently of its tool list.
23
23
 
24
24
  Retained scout/fork/worktree children hibernate after 30 seconds idle: their Pi process exits, but the private child session and worktree remain for `jar_subagent resume` or `ask`. Hibernated records count against Max subagents. Unresolved worktree recovery records do not occupy a pool slot, but at most four active/recovery worktrees are admitted. `jar_subagent stop` retries reconciliation; `discard` explicitly removes an unresolved recovery workspace without applying it. On session shutdown pi-jar reports paths for worktrees it preserves on disk, including still-active worktrees that might contain edits.
package/docs/WORKFLOWS.md CHANGED
@@ -151,9 +151,9 @@ watch matches or process exits → pi-jar.shell message → agent wakes (o
151
151
  - Progress (current tool, streaming text, tools used, turns, cost) stays in the activity view. Retained `idle` and `paused` agents remain visible in the footer/activity fleet; the newest eight ordinary retired reports remain for review, while unresolved private workspaces remain recoverable until explicit finalization/session cleanup. `s` in the activity view still steers a working subagent and `x` stops the selected agent through the same registry.
152
152
  - The deprecated `write: true` mode remains a one-shot shared-workspace compatibility path. New delegated implementation should use `mode: "worktree"`.
153
153
 
154
- ### Exceptional agentic democracy
154
+ ### Exceptional agent council
155
155
 
156
- `jar_democracy` is **not** a default decomposition or decision tool. The moderator must supply `complexity: "super-complex"`, a concrete complexity justification, the persistent issue, at least two distinct failed approaches with evidence, and 2–8 uniquely identified options with tradeoffs. These gates are runtime-validated in addition to prompt guidance; the moderator is responsible for truthful evidence.
156
+ `jar_council` is **not** a default decomposition or decision tool. The moderator must supply `complexity: "super-complex"`, a concrete complexity justification, the persistent issue, at least two distinct failed approaches with evidence, and 2–8 uniquely identified options with tradeoffs. These gates are runtime-validated in addition to prompt guidance; the moderator is responsible for truthful evidence.
157
157
 
158
158
  - Nominate relevant idle/paused **scouts** with `agents`, and set the total electorate with `voters` (default 4, or 2 at a cap of 2; always bounded by the configured cap). Forks, writable workers, busy agents and duplicate nominations are rejected. Reuse preserves context; prefer fresh scouts if discussion history could bias a ballot.
159
159
  - Unfilled seats spawn fresh read-only scouts through the normal delegate controller. The same session-wide capacity applies; the moderator must explicitly stop unneeded agents if there is no room. Voting never silently kills existing workers. Scouts spawned for the round are retired once it ends, so they never keep holding pool slots; nominated scouts stay retained.
@@ -5,7 +5,7 @@ import { existsSync, readFileSync } from "node:fs";
5
5
  import { Key, truncateToWidth, type TUI, type TuiMouseEvent } from "@earendil-works/pi-tui";
6
6
  import { ACCENT_NAMES, loadedAccents, selectAccent } from "../src/accent.ts";
7
7
  import { registerAskTool } from "../src/ask-tool.ts";
8
- import { registerDemocracy } from "../src/democracy.ts";
8
+ import { registerCouncil } from "../src/council.ts";
9
9
  import type { DelegateController } from "../src/delegate.ts";
10
10
  import { ComposerStyle } from "../src/composer.ts";
11
11
  import { installCompactBuiltinTools } from "../src/compact-tools.ts";
@@ -26,6 +26,7 @@ import { ModelRoleManager } from "../src/model-roles.ts";
26
26
  import { jarCommit } from "../src/commit.ts";
27
27
  import { registerAdvisor } from "../src/advisor.ts";
28
28
  import { registerContextDiet } from "../src/context-diet.ts";
29
+ import { registerCacheBreaks } from "../src/cache-breaks.ts";
29
30
  import { registerInfoPanels } from "../src/info-panels.ts";
30
31
  import { SideUsage } from "../src/side-model.ts";
31
32
  import { PlanMode } from "../src/plan.ts";
@@ -37,6 +38,7 @@ import { manageTasks } from "../src/tasks-ui.ts";
37
38
  import { registerTaskTool, todoRow } from "../src/task-tool.ts";
38
39
  import { TASK_ENTRY, TodoStore, todoProgress, todoTotals } from "../src/tasks.ts";
39
40
  import { formatCost, sessionCost } from "../src/usage.ts";
41
+ import { branchCalls, compactForBudget, CONTEXT_CONTINUATION, ContextBudgetGuard, providerCall, resumeAfterBudget } from "../src/context-budget.ts";
40
42
  import { WorkingState } from "../src/working.ts";
41
43
  import { ChangeTracker } from "../src/changes.ts";
42
44
  import { CHILD_ENV, DelegateRegistry, registerDelegate, SUBAGENT_MESSAGE, type DelegateRun } from "../src/delegate.ts";
@@ -61,8 +63,8 @@ const VERSION = (() => {
61
63
  })();
62
64
  /** Ignore the click Pi may synthesize right after a press we already acted on. */
63
65
  const WELCOME_CLICK_DEDUPE_MS = 400;
64
- /** Entries Pi writes when a fresh session starts; a branch holding only these has no transcript yet. */
65
- const SESSION_SETUP_ENTRIES = new Set(["thinking_level_change", "model_change", "session_info"]);
66
+ /** Conversation content, unlike startup metadata or extension bookkeeping. */
67
+ const SESSION_TRANSCRIPT_ENTRIES = new Set(["message", "custom_message", "compaction", "branch_summary"]);
66
68
  export default function piJar(pi: ExtensionAPI): void {
67
69
  const delegatedChild = process.env[CHILD_ENV] === "1";
68
70
  const delegatedWorktree = delegatedChild ? process.env[CHILD_WORKTREE_ENV] : undefined;
@@ -71,7 +73,7 @@ export default function piJar(pi: ExtensionAPI): void {
71
73
  // and handlers are disabled. Remove only parent automation, not task/user context.
72
74
  const parentAutomation = new Set([
73
75
  "pi-jar.goal-context", "pi-jar.goal-continuation",
74
- "pi-jar.plan-context", "pi-jar.plan-reminder", "pi-jar.moderator-context", SUBAGENT_MESSAGE, DISCUSSION_NOTICE
76
+ "pi-jar.plan-context", "pi-jar.plan-reminder", "pi-jar.moderator-context", SUBAGENT_MESSAGE, DISCUSSION_NOTICE, CONTEXT_CONTINUATION
75
77
  ]);
76
78
  pi.on("context", (event) => {
77
79
  const messages = event.messages.filter((message) => !parentAutomation.has((message as { customType?: string }).customType ?? ""));
@@ -97,7 +99,7 @@ export default function piJar(pi: ExtensionAPI): void {
97
99
  pi.setActiveTools([...allowed].filter(name => available.has(name)));
98
100
  });
99
101
  }
100
- installCompactBuiltinTools(pi);
102
+ installCompactBuiltinTools(pi, { readCache: () => visualSettings.readCache === true });
101
103
  let demo = false;
102
104
  let visualSettings = defaultVisualSettings();
103
105
  let profileStore: ProfileStore | undefined;
@@ -130,6 +132,15 @@ export default function piJar(pi: ExtensionAPI): void {
130
132
  if (footerTui && footerSettings.quota) quotaCache?.refresh(ctx.model?.provider, welcomeStatuses(), Date.now());
131
133
  };
132
134
  let cost = 0;
135
+ /**
136
+ * Context budget: footer chip, average cost per call, prompt hints and safe-point compaction.
137
+ * Delegated children never run it; the parent session owns the budget.
138
+ */
139
+ const contextGuard = new ContextBudgetGuard(() => delegatedChild ? { ...visualSettings.contextBudget, action: "off" } : visualSettings.contextBudget);
140
+ /** jar_todo calls in flight → completed count before each ran; a raised count marks a safe point. */
141
+ const todoCalls = new Map<string, number>();
142
+ /** Bumped per session: a compaction finishing after a session change resumes nothing. */
143
+ let budgetGeneration = 0;
133
144
  let welcomeInterval: ReturnType<typeof setInterval> | undefined;
134
145
  let welcomeFrame = 0;
135
146
  let welcomeDismiss = 0;
@@ -141,6 +152,8 @@ export default function piJar(pi: ExtensionAPI): void {
141
152
  let welcomeBranch = (): string | null => null;
142
153
  let refreshWelcome: (() => void) | undefined;
143
154
  let todos: TodoStore | undefined;
155
+ /** Leaf completion: jar_todo safe points, the stop decision and the topic hint. */
156
+ const todoCounts = () => todoTotals(todos?.all() ?? []);
144
157
  let goals: GoalStore | undefined;
145
158
  const composer = new ComposerStyle();
146
159
  let footerSettings = visualSettings.footer;
@@ -308,9 +321,14 @@ export default function piJar(pi: ExtensionAPI): void {
308
321
  const modelRoles = new ModelRoleManager(pi);
309
322
  const sideUsage = new SideUsage();
310
323
  registerContextDiet(pi, () => visualSettings.contextDiet === true);
324
+ // Observe-only: explains costly prompt-cache breaks; subagent processes have no UI to tell.
325
+ const cacheBreaks = registerCacheBreaks(pi, { enabled: () => !delegatedChild && visualSettings.cacheDiagnostics !== false });
311
326
  const advisor = registerAdvisor(pi, modelRoles, { enabled: () => !delegatedChild && visualSettings.advisor, gates: () => !delegatedChild && visualSettings.advisorGates, usage: sideUsage });
312
327
  registerInfoPanels(pi, { side: sideUsage, quotaEnabled: () => quotaCache?.enabled ?? false,
313
- quota: (ctx) => quotaCache?.get(ctx.model?.provider, welcomeStatuses(), Date.now()) });
328
+ quota: (ctx) => quotaCache?.get(ctx.model?.provider, welcomeStatuses(), Date.now()),
329
+ quotaFailure: (ctx) => quotaCache?.failure(ctx.model?.provider, Date.now()),
330
+ cacheBreaks: (ctx) => cacheBreaks.state(ctx),
331
+ refreshQuota: (ctx) => { quotaCache?.refresh(ctx.model?.provider, welcomeStatuses(), Date.now()); } });
314
332
  let followingTheme: string | undefined;
315
333
  const profileEntries = (ctx: ExtensionContext): readonly unknown[] => {
316
334
  try { return ctx.sessionManager.getEntries?.() ?? sessionBranch(ctx); } catch { return sessionBranch(ctx); }
@@ -358,8 +376,9 @@ export default function piJar(pi: ExtensionAPI): void {
358
376
  discussionEnv: (key, name) => discussion?.childEnv(key, name),
359
377
  discussionRetire: (key) => discussion?.retire(key),
360
378
  discussionUnread: (key) => discussion?.unreadFor(key) ?? 0,
379
+ discussionDigest: () => discussion?.digest(),
361
380
  getMaxSubagents: () => visualSettings.maxSubagents, onController: (controller) => { delegateController = controller; } });
362
- if (!delegatedChild) registerDemocracy(pi, () => delegateController, () => visualSettings.maxSubagents);
381
+ if (!delegatedChild) registerCouncil(pi, () => delegateController, () => visualSettings.maxSubagents);
363
382
  const planMode = new PlanMode(pi, () => todos, modelRoles, updateTaskWidget);
364
383
  if (!delegatedChild) planMode.register();
365
384
  const goalLoop = new GoalLoop(pi, {
@@ -636,7 +655,7 @@ export default function piJar(pi: ExtensionAPI): void {
636
655
  sessionName: footerSessionName,
637
656
  cwd: ctx.cwd, settings: footerSettings, branch: footerData.getGitBranch(),
638
657
  context: contextUsage?.label ?? "ctx ?", goal: goalLoop.progress(), chips: footerChips(), activity: footerActivity(now),
639
- memory, cost: formatCost(cost), quota, roles, extras: live.extras,
658
+ memory, cost: formatCost(cost, contextGuard.perCall), overBudget: contextGuard.over, quota, roles, extras: live.extras,
640
659
  demo, animations, frame, motionBudget: ctx.isIdle() ? 2 : 1
641
660
  }, width, ctx.ui.theme ?? theme);
642
661
  hits = layout.hits;
@@ -808,7 +827,17 @@ export default function piJar(pi: ExtensionAPI): void {
808
827
  const restoreGoal = (ctx: ExtensionContext, branch?: readonly unknown[]) => {
809
828
  try { goals?.restore(branch ?? sessionBranch(ctx)); } catch { goals?.restore([]); }
810
829
  };
811
- pi.on("session_start", (_event, ctx) => {
830
+ /** Awaited by agent_settled (see compactForBudget), whose handler is the last pi-jar settle handler. */
831
+ const settleBudget = async (ctx: ExtensionContext) => {
832
+ const job = contextGuard.settle({ idle: ctx.isIdle(), blocked: planMode.isEnabled() });
833
+ if (!job) return;
834
+ const open = (todos?.all() ?? []).filter((item) => !item.done).map((item) => item.title);
835
+ if (!job.compact) { resumeAfterBudget(pi, ctx, open); return; }
836
+ const generation = budgetGeneration;
837
+ await compactForBudget(pi, ctx, { resume: job.resume, tokens: contextGuard.lastTokens, softTokens: visualSettings.contextBudget.softTokens, open },
838
+ () => generation === budgetGeneration);
839
+ };
840
+ pi.on("session_start", (event, ctx) => {
812
841
  sideUsage.clear();
813
842
  if (!profileStore) initializeProfiles(ctx);
814
843
  composer.setProfile(profileStore?.activeName ?? "Default");
@@ -859,6 +888,9 @@ export default function piJar(pi: ExtensionAPI): void {
859
888
  restoreTodos(ctx, branch);
860
889
  restoreGoal(ctx, branch);
861
890
  }
891
+ contextGuard.reset(branchCalls(branch, Date.now()));
892
+ todoCalls.clear();
893
+ budgetGeneration++;
862
894
  // Quota is enabled per session; no credentials or consent are persisted.
863
895
  quotaCache = new QuotaCache(
864
896
  (provider: QuotaProvider, signal) => fetchQuota(provider, (id) => ctx.modelRegistry.getProviderAuth(id), signal),
@@ -890,9 +922,12 @@ export default function piJar(pi: ExtensionAPI): void {
890
922
  }
891
923
  // Resumed sessions already have a transcript to paint. Rebuilding the animated welcome and
892
924
  // scanning other session files competes with that expensive initial render for no real benefit.
893
- // A fresh session is not empty: Pi records the startup thinking level/model as entries.
894
- const resumed = branch.some((entry) => !(typeof entry === "object" && entry !== null && "type" in entry
895
- && typeof entry.type === "string" && SESSION_SETUP_ENTRIES.has(entry.type)));
925
+ // Fresh sessions can already contain model settings and other extensions' custom metadata.
926
+ // Those entries are not a transcript; only conversation content or an explicit resume/fork
927
+ // should suppress the welcome (including resumes of sessions containing only saved state).
928
+ const resumed = event.reason === "resume" || event.reason === "fork" || branch.some((entry) =>
929
+ typeof entry === "object" && entry !== null && "type" in entry
930
+ && typeof entry.type === "string" && SESSION_TRANSCRIPT_ENTRIES.has(entry.type));
896
931
  if (!delegatedChild && !resumed) showWelcome(ctx);
897
932
  if (discussion) {
898
933
  const current = discussion;
@@ -920,6 +955,11 @@ export default function piJar(pi: ExtensionAPI): void {
920
955
  }
921
956
  if (event.source !== "interactive") return;
922
957
  dismissWelcome(ctx);
958
+ // Hints read a finished list before this prompt acknowledges (hides) it.
959
+ if (!event.streamingBehavior) {
960
+ const hint = contextGuard.prompt(Date.now(), todoCounts());
961
+ if (hint) ctx.ui.notify(hint, "info");
962
+ }
923
963
  if (!todosAcknowledged && todos?.all().every((item) => item.done)) { todosAcknowledged = true; updateTaskWidget(ctx); }
924
964
  });
925
965
  pi.on("agent_start", (_event, ctx) => {
@@ -937,20 +977,46 @@ export default function piJar(pi: ExtensionAPI): void {
937
977
  if (event.message.role === "assistant") {
938
978
  working.reportOutputTokens(event.message.usage?.output ?? 0);
939
979
  addMessageCost(event.message);
980
+ // Each provider call moves the per-call average and the budget; a crossing turns the context chip amber.
981
+ const notice = contextGuard.call(providerCall(event.message, Date.now()));
982
+ if (notice) ctx.ui.notify(notice, "warning");
940
983
  applyWorking(ctx);
941
984
  repaint("cost", "background");
942
985
  }
943
986
  });
944
- pi.on("tool_execution_start", (event, ctx) => { working.toolStart(event.toolCallId, event.toolName); applyWorking(ctx); });
945
- pi.on("tool_execution_end", (event, ctx) => { working.toolEnd(event.toolCallId); applyWorking(ctx); });
987
+ pi.on("tool_execution_start", (event, ctx) => {
988
+ if (event.toolName === "jar_todo") todoCalls.set(event.toolCallId, todoCounts().done);
989
+ working.toolStart(event.toolCallId, event.toolName); applyWorking(ctx);
990
+ });
991
+ pi.on("tool_execution_end", (event, ctx) => {
992
+ const before = todoCalls.get(event.toolCallId);
993
+ if (before !== undefined) {
994
+ todoCalls.delete(event.toolCallId);
995
+ if (todoCounts().done > before) contextGuard.todoCompleted();
996
+ }
997
+ working.toolEnd(event.toolCallId); applyWorking(ctx);
998
+ });
946
999
  pi.on("ui_prompt_start", (_event, ctx) => { working.prompt(true); applyWorking(ctx); });
947
1000
  pi.on("ui_prompt_end", (_event, ctx) => { working.prompt(false); applyWorking(ctx); });
948
1001
  // A request may span several tool turns; keep its elapsed time and reported tokens until agent_end.
949
- pi.on("turn_end", (_event, ctx) => { applyWorking(ctx); refreshQuota(ctx); });
1002
+ pi.on("turn_end", (event, ctx) => {
1003
+ applyWorking(ctx); refreshQuota(ctx);
1004
+ todoCalls.clear();
1005
+ // Safe point: every tool result of this turn is in. Stop before the next request; agent_settled compacts and resumes.
1006
+ const stop = event.message?.role === "assistant" ? event.message.stopReason : undefined;
1007
+ if (contextGuard.turnEnd({ open: () => { const { done, total } = todoCounts(); return total - done; },
1008
+ interrupted: stop === "error" || stop === "aborted" || ctx.signal?.aborted === true || ctx.hasPendingMessages?.() === true,
1009
+ blocked: planMode.isEnabled() })) ctx.abort();
1010
+ });
950
1011
  pi.on("agent_end", (_event, ctx) => { working.end(); applyWorking(ctx); sampleFooter(ctx); });
951
1012
  pi.on("agent_before_settle", (event) => { if (event.outcome === "error") composer.flash("error"); });
952
1013
  // Retries or compaction after agent_end may have added messages; otherwise the agent_end sample stands.
953
- pi.on("agent_settled", (_event, ctx) => { working.end(); applyWorking(ctx); if (contextUsage?.dirty) sampleFooter(ctx); refreshQuota(ctx); });
1014
+ pi.on("agent_settled", (_event, ctx) => {
1015
+ working.end(); applyWorking(ctx); if (contextUsage?.dirty) sampleFooter(ctx); refreshQuota(ctx);
1016
+ if (contextGuard.busy) return settleBudget(ctx);
1017
+ });
1018
+ // Pi's idle cache warming keeps the prompt cache alive; the cold-cache hint counts from its last refresh.
1019
+ pi.on("cache_warming_decision", (event) => { if (event.action === "warm") contextGuard.warmed(Date.now()); });
954
1020
  pi.on("session_tree", (_event, ctx) => {
955
1021
  let branch: readonly unknown[] = [];
956
1022
  try { branch = sessionBranch(ctx); } catch { /* keep empty */ }
@@ -959,6 +1025,7 @@ export default function piJar(pi: ExtensionAPI): void {
959
1025
  restoreTodos(ctx, branch);
960
1026
  restoreGoal(ctx, branch);
961
1027
  }
1028
+ contextGuard.reset(branchCalls(branch, Date.now()));
962
1029
  updateCost(ctx);
963
1030
  composer.refreshSession(ctx);
964
1031
  sampleFooter(ctx);
@@ -968,6 +1035,7 @@ export default function piJar(pi: ExtensionAPI): void {
968
1035
  try { branch = sessionBranch(ctx); } catch { /* keep empty */ }
969
1036
  renders?.setEntries(branch.length);
970
1037
  if (!delegatedChild) { restoreTodos(ctx, branch); restoreGoal(ctx, branch); }
1038
+ contextGuard.compacted();
971
1039
  updateCost(ctx);
972
1040
  sampleFooter(ctx);
973
1041
  });
@@ -990,6 +1058,9 @@ export default function piJar(pi: ExtensionAPI): void {
990
1058
  renders = undefined;
991
1059
  contextUsage?.dispose();
992
1060
  contextUsage = undefined;
1061
+ contextGuard.reset();
1062
+ todoCalls.clear();
1063
+ budgetGeneration++;
993
1064
  todos = undefined;
994
1065
  changes = undefined;
995
1066
  changeCount = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-jar",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "A warm, role-aware Pi terminal workspace with switchable profiles, animated UI, plan and goal workflows, tasks, change review, model roles, background shells and subagents.",
5
5
  "type": "module",
6
6
  "license": "MIT",