@gtheys/pi-interactive-subagents 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +462 -2
- package/agents/claude-code.md +23 -0
- package/agents/planner.md +569 -0
- package/agents/reviewer.md +152 -0
- package/agents/scout.md +112 -0
- package/agents/visual-tester.md +202 -0
- package/agents/worker.md +109 -0
- package/config.json.example +6 -0
- package/config.schema.json +71 -0
- package/package.json +59 -4
- package/pi-extension/subagents/activity.ts +730 -0
- package/pi-extension/subagents/asks.ts +234 -0
- package/pi-extension/subagents/assignments.ts +395 -0
- package/pi-extension/subagents/config.ts +191 -0
- package/pi-extension/subagents/control.ts +172 -0
- package/pi-extension/subagents/dataroot.ts +52 -0
- package/pi-extension/subagents/delivered.ts +81 -0
- package/pi-extension/subagents/flow-env.ts +112 -0
- package/pi-extension/subagents/herdr.ts +323 -0
- package/pi-extension/subagents/inbox.ts +213 -0
- package/pi-extension/subagents/index.ts +5003 -0
- package/pi-extension/subagents/lock.ts +318 -0
- package/pi-extension/subagents/peer-bindings.ts +175 -0
- package/pi-extension/subagents/peers.ts +401 -0
- package/pi-extension/subagents/plan-skill.md +219 -0
- package/pi-extension/subagents/plugin/.claude-plugin/plugin.json +5 -0
- package/pi-extension/subagents/plugin/hooks/hooks.json +15 -0
- package/pi-extension/subagents/plugin/hooks/on-stop.sh +68 -0
- package/pi-extension/subagents/retention.ts +918 -0
- package/pi-extension/subagents/retirement.ts +216 -0
- package/pi-extension/subagents/session.ts +194 -0
- package/pi-extension/subagents/snapshot.ts +151 -0
- package/pi-extension/subagents/spawn.ts +237 -0
- package/pi-extension/subagents/spawnseq.ts +116 -0
- package/pi-extension/subagents/state-events.ts +152 -0
- package/pi-extension/subagents/status.ts +886 -0
- package/pi-extension/subagents/store.ts +245 -0
- package/pi-extension/subagents/subagent-done.ts +1452 -0
- package/pi-extension/subagents/surface.ts +365 -0
- package/pi-extension/subagents/task-composition.ts +113 -0
- package/pi-extension/subagents/tool-effects.ts +203 -0
- package/pi-extension/subagents/usage.ts +330 -0
- package/pi-extension/subagents/watermark.ts +126 -0
- package/pi-extension/subagents/worktrees.ts +147 -0
- package/skills/subagent-orchestration/SKILL.md +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HazAT
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,463 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-interactive-subagents
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Async subagents for [pi](https://github.com/badlogic/pi-mono) — spawn a team of AI agents that work in background tabs while you keep coding. Ask them questions mid-run, let them ask *you*, give them their own git worktrees, and never lose track of a run — even if your parent session dies.
|
|
4
|
+
|
|
5
|
+
https://github.com/user-attachments/assets/30adb156-cfb4-4c47-84ca-dd4aa80cba9f
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
Orchestrating agents usually means one of two bad deals: **block** your main session while a child runs, or **fire and forget** and lose the thread. This extension takes a third path, ported from [pi-herdsman](https://github.com/boadij/pi-herdsman) (see [Acknowledgements](#acknowledgements)):
|
|
10
|
+
|
|
11
|
+
- **Never blocks.** `subagent()` returns in milliseconds. The child runs in a background [herdr](https://github.com/pi-edubot/herdr) tab; your pane, focus, and flow never move.
|
|
12
|
+
- **Never loses work.** Results, assignments, and control messages are durable files, not screen scrapes. Kill the parent, relaunch, and `subagents_list` rediscovers every orphaned child — with a resume hint.
|
|
13
|
+
- **Talks both ways.** Children can ask blocking questions (`ask_owner`), send fire-and-forget updates (`message_owner`), and message *each other* (`message_peer`). The parent answers with `answer_subagent` and the conversation continues **in the same child session**.
|
|
14
|
+
|
|
15
|
+
## How it works
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
1. Agent calls subagent() → returns immediately ("launched in background")
|
|
19
|
+
2. Child runs in a herdr tab → widget shows live status + activity + cost
|
|
20
|
+
3. You keep working → main session fully interactive
|
|
21
|
+
4. Child finishes → result steered back, parent wakes and continues
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The widget above your input tracks every run — click a row to open that agent's tab:
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
╭─ Subagents ───────────────────────────── 2 running · 41.2k tok $0.18 ─╮
|
|
28
|
+
│ ● Scout: Auth (scout) active (bash 12s) · 12.3k tok $0.04 │
|
|
29
|
+
│ ● Worker: migrations (worker) waiting 2m · 28.9k tok $0.14 │
|
|
30
|
+
╰────────────────────────────────────────────────────────────────────────╯
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
States: `starting` → `active` (with the current tool and elapsed time) → `waiting`, plus `stalled` when a child's heartbeat goes silent — and a steer to the parent so it can react. Status comes from a child-written snapshot file, never from watching screen pixels.
|
|
34
|
+
|
|
35
|
+
### Prompt cache stability
|
|
36
|
+
|
|
37
|
+
Repeated spawns of the same role are shaped to keep the prompt prefix stable so provider-side prompt caching can latch onto it:
|
|
38
|
+
|
|
39
|
+
- **Role identity** rides the system prompt via `--append-system-prompt` flag files — stable per role and byte-identical across spawns of that role.
|
|
40
|
+
- **The first user message** is `modeHint + task + summaryInstruction`. It is dynamic, but spawns of the same role diverge only at the task text, after an identical `modeHint` prefix.
|
|
41
|
+
- **aven-context is gated off** in child sessions, so no per-spawn aven blob lands at the front of the message stream, and role identity is no longer inlined into the dynamic first message.
|
|
42
|
+
|
|
43
|
+
Known remaining sources of prefix divergence, both benign:
|
|
44
|
+
|
|
45
|
+
- The trailing `cwd` section of the system prompt differs per worktree spawn — this is pi-core behavior and out of scope here.
|
|
46
|
+
- `/skill:` follow-up prompts trail the task text, so the stable prefix ahead of them is preserved.
|
|
47
|
+
|
|
48
|
+
This contract is enforced by the request-composition tests in `test/task-composition.test.ts` (the "request composition" describe block).
|
|
49
|
+
|
|
50
|
+
## The flows
|
|
51
|
+
|
|
52
|
+
### Parallel scouting
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
subagent({ name: "Scout: Auth", agent: "scout", task: "Analyze auth module" });
|
|
56
|
+
subagent({ name: "Scout: DB", agent: "scout", task: "Map database schema" });
|
|
57
|
+
// Both return immediately; results steer back independently as they land.
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### A child that asks before it acts
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
// Inside the child:
|
|
64
|
+
ask_owner({ question: "Two migration files conflict — use v1 or v2?" });
|
|
65
|
+
// Child blocks (widget: "blocked"). Parent wakes with the question, answers:
|
|
66
|
+
answer_subagent({ name: "Worker", message: "Use v2, v1 is deprecated" });
|
|
67
|
+
// Answer arrives as the child's next user message — same session, full context.
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Answers travel through a durable mailbox (`asks/` records + reply sidecars), not pane typing — nothing gets mangled, and ingestion is acknowledged on both ends.
|
|
71
|
+
|
|
72
|
+
### Non-blocking updates from the trenches
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// Inside the child — fire-and-forget, keeps working:
|
|
76
|
+
message_owner({ message: "Schema scan done, 3 hotspots found, moving to API layer" });
|
|
77
|
+
// Parent's next status tick delivers a coalesced subagent_message steer.
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Peer-to-peer
|
|
81
|
+
|
|
82
|
+
Sibling subagents discover each other through presence files and talk directly:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
list_peers(); // who else is running?
|
|
86
|
+
message_peer({ peer: "scout-auth", message: "Found the session store — stop looking" });
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Isolated worktrees, one call
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
subagent({
|
|
93
|
+
name: "Worker: payments",
|
|
94
|
+
agent: "worker",
|
|
95
|
+
worktree: true, // or { base: "release/2.4" }
|
|
96
|
+
task: "Implement the refund flow",
|
|
97
|
+
});
|
|
98
|
+
// git worktree add → child spawned with cwd = the new worktree.
|
|
99
|
+
// Your checkout is untouched; the branch and path ride along in the result.
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Surviving the crash
|
|
103
|
+
|
|
104
|
+
Every spawn writes a durable assignment record. Parent dies? Relaunch pi and:
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
subagents_list();
|
|
108
|
+
// ● scout-auth — ORPHANED (parent gone). Resume: subagent_resume({ sessionPath: "…" })
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Children with records are deliberately **spared** when a parent shuts down. Completed runs settle into a durable result store; crash-inferred endings are marked `lost` — never invented state.
|
|
112
|
+
|
|
113
|
+
### `/iterate` and `/plan`
|
|
114
|
+
|
|
115
|
+
- `/iterate Fix the off-by-one in pagination` — forks your full conversation into a subagent, keeps your main context clean.
|
|
116
|
+
- `/plan Add dark mode` — orchestrates investigate → plan → execute → review with bundled agents (`planner`, `scout`, `worker`, `reviewer`, `visual-tester`).
|
|
117
|
+
|
|
118
|
+
## Feature tour
|
|
119
|
+
|
|
120
|
+
| Feature | What you get |
|
|
121
|
+
| --- | --- |
|
|
122
|
+
| **Background tabs** | `tab create --no-focus` — children never steal your screen; click widget rows to visit them |
|
|
123
|
+
| **Durable results** | Semantic refs (`results/<childId>.json`) — full output survives compaction and restart |
|
|
124
|
+
| **Request/ack interrupts** | `subagent_interrupt` confirms the child actually stopped its turn |
|
|
125
|
+
| **Usage rollup** | Live per-row `· 12.3k tok $0.04` in the widget, transitive tree totals in `subagents_list` |
|
|
126
|
+
| **Context retirement** | Opt-in (`contextRetirement.enabled`): settled child payloads shrink to refs under compaction pressure — long orchestrations stop bloating the parent context |
|
|
127
|
+
| **Governed spawning** | `spawning: manager-approval` frontmatter: the model can't spawn that agent on its own — your `/subagent` command is the approval |
|
|
128
|
+
| **Version gate** | Preflight rejects herdr < 0.9.1 with an actionable message instead of weird failures |
|
|
129
|
+
|
|
130
|
+
## Install
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
pi install git:github.com/HazAT/pi-interactive-subagents
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Requires [herdr](https://github.com/pi-edubot/herdr) >= 0.9.1 (tested against 0.9.5 and pi-coding-agent 0.85.1):
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
herdr pi
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
> [!CAUTION]
|
|
143
|
+
> **This extension only runs inside a herdr session.** Children live in herdr panes, so pi must be started via `herdr pi` with `herdr` on PATH and the herdr server running (version >= **0.9.1** — the preflight gate). Without that, pane and worktree operations (`subagent()`, `subagent_resume`, `subagent_interrupt`, `worktree: true`) refuse with an actionable `herdr preflight failed` error result — there is no fallback mode and nothing is silently degraded. See [Requirements](#requirements) and the version-gate row in the [Feature tour](#feature-tour).
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## Reference
|
|
148
|
+
|
|
149
|
+
### Tools
|
|
150
|
+
|
|
151
|
+
Main session:
|
|
152
|
+
|
|
153
|
+
| Tool | Description |
|
|
154
|
+
| --- | --- |
|
|
155
|
+
| `subagent` | Spawn a sub-agent in a background tab (async — returns immediately) |
|
|
156
|
+
| `subagent_interrupt` | Interrupt a running Pi-backed subagent's current turn (ack-confirmed) |
|
|
157
|
+
| `subagents_list` | Agent definitions **and** live assignment records (running / orphaned / lost, usage, resume hints) |
|
|
158
|
+
| `subagent_resume` | Resume a previous sub-agent session (async) |
|
|
159
|
+
| `answer_subagent` | Answer a child's outstanding `ask_owner` question |
|
|
160
|
+
|
|
161
|
+
Subagent-side (auto-injected, never deniable):
|
|
162
|
+
|
|
163
|
+
| Tool | Description |
|
|
164
|
+
| --- | --- |
|
|
165
|
+
| `subagent_done` | Finish and return results to the parent |
|
|
166
|
+
| `caller_ping` | Ask for help and exit — parent resumes the session with guidance |
|
|
167
|
+
| `ask_owner` | Blocking question to the parent; answer arrives in the same session |
|
|
168
|
+
| `message_owner` | Fire-and-forget update to the parent |
|
|
169
|
+
| `list_peers` / `message_peer` | Discover and message sibling subagents |
|
|
170
|
+
|
|
171
|
+
### Commands
|
|
172
|
+
|
|
173
|
+
| Command | Description |
|
|
174
|
+
| --- | --- |
|
|
175
|
+
| `/plan` | Planning pipeline — create-plan (Jira ID) / feature-plan (free text) / bundled generic workflow |
|
|
176
|
+
| `/iterate` | Fork into a subagent for quick fixes |
|
|
177
|
+
| `/subagent <agent> <task>` | Spawn a named agent directly (user-initiated — bypasses `manager-approval` gates) |
|
|
178
|
+
| `/subagents` | Open the subagent dashboard |
|
|
179
|
+
| `/subagent prune` | Run the retention sweep on demand (requires `retentionDays` config; warns if unset) |
|
|
180
|
+
|
|
181
|
+
> Note: a **bare** `/subagent prune` (no task text) is intercepted as the retention sweep. An agent literally named `prune` remains spawnable with an explicit task — `/subagent prune <task>` falls through to agent resolution.
|
|
182
|
+
|
|
183
|
+
### `subagent()` parameters
|
|
184
|
+
|
|
185
|
+
| Parameter | Type | Default | Description |
|
|
186
|
+
| --- | --- | --- | --- |
|
|
187
|
+
| `name` | string | required | Display name (widget + tab label) |
|
|
188
|
+
| `task` | string | required | Task prompt for the sub-agent |
|
|
189
|
+
| `agent` | string | — | Load defaults from an agent definition |
|
|
190
|
+
| `fork` | boolean | `false` | Force full-context fork for this spawn |
|
|
191
|
+
| `worktree` | boolean \| `{base?}` | `false` | Spawn in a fresh git worktree (`~/.herdr/worktrees/<repo>/<branch>`); removal is manual |
|
|
192
|
+
| `interactive` | boolean | derived | Don't wake the parent on stall/recovery (default: inverse of `auto-exit`) |
|
|
193
|
+
| `model` | string | — | Override agent's default model |
|
|
194
|
+
| `systemPrompt` | string | — | Append to system prompt |
|
|
195
|
+
| `skills` | string | — | Comma-separated skill names |
|
|
196
|
+
| `tools` | string | — | Comma-separated tool names |
|
|
197
|
+
| `cwd` | string | — | Working directory (mutually exclusive with `worktree`) |
|
|
198
|
+
|
|
199
|
+
### Bundled agents
|
|
200
|
+
|
|
201
|
+
| Agent | Model | Role |
|
|
202
|
+
| --- | --- | --- |
|
|
203
|
+
| **planner** | Opus (medium thinking) | Brainstorming, plans, todos |
|
|
204
|
+
| **scout** | Haiku | Fast codebase reconnaissance |
|
|
205
|
+
| **worker** | Sonnet | Implements tasks, runs tests |
|
|
206
|
+
| **reviewer** | Opus (medium thinking) | Bugs, security, correctness |
|
|
207
|
+
| **visual-tester** | Sonnet | Visual QA via Chrome CDP |
|
|
208
|
+
|
|
209
|
+
Discovery precedence: **project-local** (`.pi/agents/`) > **global** (`~/.pi/agent/agents/`) > **bundled**.
|
|
210
|
+
|
|
211
|
+
### Agent frontmatter
|
|
212
|
+
|
|
213
|
+
Full field reference with defaults, valid values, and recipes: [`docs/agent-frontmatter.md`](../docs/agent-frontmatter.md).
|
|
214
|
+
|
|
215
|
+
```markdown
|
|
216
|
+
---
|
|
217
|
+
name: my-agent
|
|
218
|
+
description: Does something specific
|
|
219
|
+
model: anthropic/claude-sonnet-4-6
|
|
220
|
+
tools: read, bash, edit, write
|
|
221
|
+
session-mode: lineage-only
|
|
222
|
+
spawning: manager-approval
|
|
223
|
+
auto-exit: true
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
You are a specialized agent that does X...
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
| Field | Description |
|
|
230
|
+
| --- | --- |
|
|
231
|
+
| `session-mode` | `standalone` (default), `lineage-only` (linked, no copied turns), `fork` (inherits conversation) |
|
|
232
|
+
| `spawning` | `true` (default), `false` (deny spawn tools), `"manager-approval"` (model calls denied; `/subagent` is the approval) |
|
|
233
|
+
| `deny-tools` | Comma-separated extension tools to deny |
|
|
234
|
+
| `auto-exit` | Shut down when the turn finishes; any user input disables it permanently |
|
|
235
|
+
| `interactive` | Override stall-ping behavior (default: inverse of `auto-exit`) |
|
|
236
|
+
| `cwd` | Default working directory |
|
|
237
|
+
| `disable-model-invocation` | Hide from listings; still invokable by explicit name |
|
|
238
|
+
|
|
239
|
+
### The `/plan` workflow
|
|
240
|
+
|
|
241
|
+
```
|
|
242
|
+
/plan IMP-7070 → create-plan skill (Jira flow)
|
|
243
|
+
/plan Add a dark mode toggle → feature-plan skill / bundled generic workflow
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Generic pipeline: investigate → interactive planning → todo review → execute → review, with tab titles tracking the phase (`🔍 → 💬 → 🔨 1/3 → 🔎 → ✅`).
|
|
247
|
+
|
|
248
|
+
### Interrupt semantics
|
|
249
|
+
|
|
250
|
+
`subagent_interrupt` is a **turn-level** interrupt, not a kill: Escape is delivered to the child, the `.ack` handshake confirms the turn stopped, the widget moves to `waiting`, and the session stays alive for follow-ups. Pi-backed children only.
|
|
251
|
+
|
|
252
|
+
### caller_ping — the escalator
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
// Inside a worker subagent:
|
|
256
|
+
caller_ping({ message: "Found two conflicting migration files — v1 or v2?" });
|
|
257
|
+
// Session EXITS. Parent gets the ping and resumes the session with guidance:
|
|
258
|
+
subagent_resume({ sessionPath: "…", message: "Use v2 — v1 is deprecated" });
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Unlike `ask_owner` (blocks, stays alive), `caller_ping` ends the session — use it when the child is done until further notice.
|
|
262
|
+
|
|
263
|
+
### Replay safety for child side-effect tools
|
|
264
|
+
|
|
265
|
+
Child side-effect tools are replay-safe across interrupted runs: each call is keyed by a durable effect memo (`tool-effects/`) plus the effect's own records (ask record, recipient inbox, `.exit` payload). On replay after a crash:
|
|
266
|
+
|
|
267
|
+
- `ask_owner` — replays the **original askId**; no second ask record, no misleading outstanding-ask error. After a formal resume wipe, the fresh ask carries an explicit orphan notice naming the dead askId.
|
|
268
|
+
- `caller_ping` / `subagent_done` — the original `.exit` payload is preserved verbatim; identical replays don't rewrite it, and a conflicting payload is reported explicitly, never overwritten.
|
|
269
|
+
- `message_owner` / `message_peer` — a memo hit or durable inbox evidence means the message is reported as already delivered, never sent twice; a send with no durable evidence is re-sent fresh with the normal delivery text (the explicit interrupted-or-conflict report exists only for the exit tools above). Additionally, a `message_peer` send durably binds its resolved recipient (childId + session file) **before** the inbox write (`peer-bindings/`), so a replay never retargets a reused peer name: if the bound recipient has died or been superseded by a new generation, the send fails closed instead of delivering to the wrong child, and a failed first write retries against the original recipient.
|
|
270
|
+
|
|
271
|
+
Replay matches **exactly**: a rephrased message or a differently-spelled peer is a new logical effect, not a replay.
|
|
272
|
+
|
|
273
|
+
Boundary: **generic tools (Bash, file edits) have no exactly-once protection.** A crash after a generic side effect may replay it — keep non-idempotent shell work out of interruptible children.
|
|
274
|
+
|
|
275
|
+
Receive-side delivery dedup (`delivered` sentinels) is a separate mechanism (PMR-KY0E) and is unchanged by this.
|
|
276
|
+
|
|
277
|
+
### Co-extension state events #state-events
|
|
278
|
+
|
|
279
|
+
Other extensions can observe subagent state without parsing the status widget,
|
|
280
|
+
importing internal stores, or reading durable records. This extension publishes
|
|
281
|
+
versioned snapshots on the Pi extension event bus
|
|
282
|
+
(`ExtensionAPI.events`). Declarations below are the public contract, reproduced
|
|
283
|
+
from `pi-extension/subagents/state-events.ts`.
|
|
284
|
+
|
|
285
|
+
```ts
|
|
286
|
+
export const SUBAGENT_STATE_CHANNEL = 'pi-my-rifle-subagents:state'
|
|
287
|
+
|
|
288
|
+
export const SUBAGENT_STATE_EVENT_VERSION = 1
|
|
289
|
+
|
|
290
|
+
export type SubagentStateStatus =
|
|
291
|
+
| 'starting'
|
|
292
|
+
| 'working'
|
|
293
|
+
| 'blocked'
|
|
294
|
+
| 'waiting'
|
|
295
|
+
| 'stalled'
|
|
296
|
+
| 'lost'
|
|
297
|
+
|
|
298
|
+
export interface SubagentStateEntry {
|
|
299
|
+
id: string
|
|
300
|
+
name: string
|
|
301
|
+
status: SubagentStateStatus
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export interface SubagentStateEvent {
|
|
305
|
+
type: 'snapshot' | 'stopped'
|
|
306
|
+
version: 1
|
|
307
|
+
incarnation: string
|
|
308
|
+
seq: number
|
|
309
|
+
emittedAt: string
|
|
310
|
+
agents: SubagentStateEntry[]
|
|
311
|
+
}
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
#### Semantics
|
|
315
|
+
|
|
316
|
+
- **Push-only full snapshots.** No deltas. Every event carries the complete set
|
|
317
|
+
of live agents, so a consumer replaces its view instead of merging.
|
|
318
|
+
- **Disappearance = settled.** A child that completed or was lost is simply
|
|
319
|
+
absent from the next snapshot. `'lost'` is a **reserved** status value that is
|
|
320
|
+
never emitted live; it stays in the union so exhaustive `switch` statements are
|
|
321
|
+
forward-stable.
|
|
322
|
+
- **Durable records stay authoritative for outcome detail.** The exit status,
|
|
323
|
+
result payload, activity file, etc. are not in this event — read the durable
|
|
324
|
+
assignment/result records (or `inspectResultRef`) for that. The state event is
|
|
325
|
+
observation only.
|
|
326
|
+
- **Envelope.** `version` (schema version, currently `1`), `incarnation` (UUID
|
|
327
|
+
per publisher module load — a `/reload` starts a new incarnation and `seq`
|
|
328
|
+
restarts at 1), `seq` (1-based, strictly monotonic **within** an incarnation),
|
|
329
|
+
`emittedAt` (ISO 8601), `type` (`'snapshot'` | `'stopped'`).
|
|
330
|
+
- **`'stopped'`.** Emitted once, with `agents: []`, when the parent session shuts
|
|
331
|
+
down cleanly. Use it to clear your view.
|
|
332
|
+
- **Ordering.** Track the latest `incarnation` you have seen and drop events from
|
|
333
|
+
older ones. Use `seq` only to detect out-of-order or duplicated delivery inside
|
|
334
|
+
the current incarnation — it is not a cross-restart cursor.
|
|
335
|
+
- **Nothing else is projected.** Per entry only `id`, `name`, `status`. Task
|
|
336
|
+
prompts, message bodies, `cwd`, session/activity file paths, claim PIDs, pane
|
|
337
|
+
and tab ids, worktree paths, and usage are deliberately excluded and are
|
|
338
|
+
covered by a key-set contract test.
|
|
339
|
+
|
|
340
|
+
#### Subscribing
|
|
341
|
+
|
|
342
|
+
Subscribe at **extension-body load**, not inside your own `session_start`
|
|
343
|
+
handler. The publisher emits its initial snapshot from the parent session's
|
|
344
|
+
`session_start`; if you wait for your own `session_start` you may miss it and see
|
|
345
|
+
nothing until the next transition.
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
// The subscriber's own copy of the contract, trimmed to the fields it reads.
|
|
349
|
+
// Type-only imports of the published declarations are fine; importing the
|
|
350
|
+
// publisher's module to READ state is not (see "Dual module instances").
|
|
351
|
+
type StateEventView = {
|
|
352
|
+
type: 'snapshot' | 'stopped'
|
|
353
|
+
incarnation: string
|
|
354
|
+
seq: number
|
|
355
|
+
agents: Array<{ id: string; name: string; status: string }>
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export default function (pi: ExtensionAPI) {
|
|
359
|
+
// Extension-body load — guarantees receipt of the initial snapshot.
|
|
360
|
+
pi.events.on('pi-my-rifle-subagents:state', (event: StateEventView) => {
|
|
361
|
+
// v1 is additive-only: read the documented keys, ignore unknown fields.
|
|
362
|
+
if (event.type === 'stopped') return // publisher session ended
|
|
363
|
+
renderAgentList(event.agents) // full snapshot: replace, never merge
|
|
364
|
+
})
|
|
365
|
+
}
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
Keep a `'lost'` arm in any exhaustive `switch` on `status` even though it never
|
|
369
|
+
arrives live — it is part of the v1 union and removing it would be a breaking
|
|
370
|
+
change.
|
|
371
|
+
|
|
372
|
+
#### Scope caveats
|
|
373
|
+
|
|
374
|
+
- **Per-process bus only.** There is no cross-process delivery. Subscribers must
|
|
375
|
+
run in the same Pi process as the publisher.
|
|
376
|
+
- **Children never publish.** Only the parent session emits; subagent sessions
|
|
377
|
+
do not publish state events even though their own `session_start` /
|
|
378
|
+
`session_shutdown` hooks run.
|
|
379
|
+
- **Dual module instances.** Pi loads this extension's entrypoint directly, while
|
|
380
|
+
a co-extension may import the same source through the workspace package root —
|
|
381
|
+
those can be distinct module instances, and only the entrypoint instance wires
|
|
382
|
+
emissions. Subscribe via `pi.events`; never read state from a module import
|
|
383
|
+
(importing the types is fine, importing the sequencer or a store is not).
|
|
384
|
+
- **Status refresh disabled.** With `status.enabled: false` the 1s status tick
|
|
385
|
+
does not run, so live kind drift (`stalled`, `waiting`, recovered) is not
|
|
386
|
+
published. Spawn, `working`, settlement and `stopped` emissions still fire —
|
|
387
|
+
they hang on the durable/Map seams, not the tick.
|
|
388
|
+
|
|
389
|
+
#### Versioning policy
|
|
390
|
+
|
|
391
|
+
`SUBAGENT_STATE_EVENT_VERSION` is `1` and **v1 is additive-only**: consumers MUST
|
|
392
|
+
ignore unknown fields, and publishers MAY add fields without bumping the version.
|
|
393
|
+
Breaking changes (removed or renamed fields, changed semantics) bump the constant.
|
|
394
|
+
A key-set contract test pins the current payload so any addition is a deliberate
|
|
395
|
+
version-policy decision rather than silent drift.
|
|
396
|
+
|
|
397
|
+
## Configuration
|
|
398
|
+
|
|
399
|
+
Config lives at `~/.pi/agent/pi-subagents/config.json` (created with defaults on first run).
|
|
400
|
+
|
|
401
|
+
| Option | Type | Default | Description |
|
|
402
|
+
| --- | --- | --- | --- |
|
|
403
|
+
| `status.enabled` | boolean | `true` | Live status widget and refresh loop |
|
|
404
|
+
| `status.hideWhenIdle` | boolean | `true` | Hide the "Subagents · N running" widget completely while no subagent exists; set `false` to keep the box visible at zero |
|
|
405
|
+
| `contextRetirement.enabled` | boolean | `false` | Retire settled subagent payloads to durable refs when compaction pressure hits |
|
|
406
|
+
| `watchdog.enabled` | boolean | `true` | Dead-turn stall watchdog: age frozen present+active snapshots into `stalled` and escalate dead child turns. `false` restores the legacy never-age behavior |
|
|
407
|
+
| `watchdog.streamStallAfterMs` | number | `150000` | Deadline (ms) for the frozen `streaming` scope — silence there means the model turn died mid-generation |
|
|
408
|
+
| `watchdog.quietStallAfterMs` | number | `1800000` | Deadline (ms) for `provider`/`tool`/`turn`/unknown scopes and sustained snapshot loss. `provider` is deliberately long-bucket: pre-first-token silence (large prompt processing, 429/529 retry backoff) emits no events, so a short provider deadline false-kills healthy children mid-request |
|
|
409
|
+
| `retentionDays` | integer | unset (no pruning) | Opt-in: prune inactive subagent data-root records whose newest activity is older than this many days. Live, current, and resumable state is never pruned. Runs automatically at session start and on demand via `/subagent prune` |
|
|
410
|
+
|
|
411
|
+
```json
|
|
412
|
+
{
|
|
413
|
+
"$schema": "https://json-schema.org/schema",
|
|
414
|
+
"status": {
|
|
415
|
+
"enabled": true,
|
|
416
|
+
"hideWhenIdle": true
|
|
417
|
+
},
|
|
418
|
+
"contextRetirement": {
|
|
419
|
+
"enabled": false
|
|
420
|
+
},
|
|
421
|
+
"watchdog": {
|
|
422
|
+
"enabled": true,
|
|
423
|
+
"streamStallAfterMs": 150000,
|
|
424
|
+
"quietStallAfterMs": 1800000
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
`retentionDays` is opt-in and intentionally absent from the shipped defaults — add it to enable pruning:
|
|
430
|
+
|
|
431
|
+
```json
|
|
432
|
+
{
|
|
433
|
+
"$schema": "https://json-schema.org/schema",
|
|
434
|
+
"retentionDays": 30
|
|
435
|
+
}
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
### Data retention policy
|
|
439
|
+
|
|
440
|
+
When `retentionDays` is set, a maintenance sweep runs automatically at session start and on demand via `/subagent prune`, removing inactive data-root records whose **newest relevant activity** is older than the configured age. The sweep reports what it removed and never aborts session startup. The automatic session-start sweep stays **silent unless it removes something** — a sweep that removes nothing (including one degraded by persistent failures, e.g. an unreadable data root) posts no notification; run `/subagent prune` on demand to surface skip reasons and failures.
|
|
441
|
+
|
|
442
|
+
Safety rules:
|
|
443
|
+
|
|
444
|
+
- **Never pruned:** `result-ref-reservations/` and `spawn-seq/` — monotonic counters; pruning them would silently reuse result refs and childIds.
|
|
445
|
+
- **Age-eligible:** `results/`, `retired/` — removed once older than the cutoff.
|
|
446
|
+
- **Session-checked:** `assignments/`, `peers/`, `asks/`, `inbox/`, `delivered/`, `interrupts/`, `tool-effects/`, `peer-bindings/` — pruned only when aged out **and** liveness is verifiably dead **and** the resume-source session file no longer exists. Live, current, resumable, unreadable, or unverifiable records are always kept; any inspection failure skips the record.
|
|
447
|
+
- **Ref expiry:** pruned `results/` records can never silently resolve to different content — reservation markers are never pruned and their target identity is immutable. The `inspectResultRef` API reports a pruned target loudly as `expired` ("result expired by retention policy"); runtime result consumers today keep their own pre-prune record path and surface a pruned record as a plain missing-record error.
|
|
448
|
+
- pi-flow run journals are never touched.
|
|
449
|
+
|
|
450
|
+
## Requirements
|
|
451
|
+
|
|
452
|
+
- [pi](https://github.com/badlogic/pi-mono) — the coding agent
|
|
453
|
+
- [herdr](https://github.com/pi-edubot/herdr) >= 0.9.1
|
|
454
|
+
|
|
455
|
+
## Acknowledgements
|
|
456
|
+
|
|
457
|
+
The coordination architecture — durable file-based channels (results, assignments, mailboxes), blocking `ask_owner` semantics, presence and peer messaging, usage rollup, context retirement, and governed spawning — is ported from **[pi-herdsman](https://github.com/boadij/pi-herdsman)** by [@boadij](https://github.com/boadij) (Apache-2.0). Herdsman proved these mechanisms in production; this package re-implements them on herdr's tab model with pi-native session files as the authority. Thank you for the blueprint.
|
|
458
|
+
|
|
459
|
+
The sub-agent status supervision and turn-only interruption features were inspired by [RepoPrompt](https://repoprompt.com/)'s sub-agent snapshot polling and run cancellation features.
|
|
460
|
+
|
|
461
|
+
## License
|
|
462
|
+
|
|
463
|
+
MIT
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: claude-code
|
|
3
|
+
description: Self-driving Claude Code session for deep investigation, experimentation, and code exploration
|
|
4
|
+
cli: claude
|
|
5
|
+
model: sonnet
|
|
6
|
+
auto-exit: true
|
|
7
|
+
spawning: false
|
|
8
|
+
deny-tools: claude
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
# Claude Code
|
|
12
|
+
|
|
13
|
+
You are a self-driving Claude Code session spawned by pi for hands-on investigation and experimentation.
|
|
14
|
+
|
|
15
|
+
You have full autonomy: bash, file access, git clone, code editing, running tests, building projects — everything a developer can do in a terminal.
|
|
16
|
+
|
|
17
|
+
## Guidelines
|
|
18
|
+
|
|
19
|
+
- Focus on the task given to you
|
|
20
|
+
- Be thorough in your investigation
|
|
21
|
+
- Report concrete findings with evidence (file paths, command output, test results)
|
|
22
|
+
- If you get stuck, explain what you tried and what failed
|
|
23
|
+
- Your final message should summarize what you accomplished and what you found
|