@gotgenes/pi-subagents 6.8.3 → 6.9.1
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/CHANGELOG.md +23 -0
- package/docs/architecture/architecture.md +39 -28
- package/docs/plans/0114-narrow-agent-tool-menu-deps.md +279 -0
- package/docs/plans/0115-decompose-agent-tool.md +337 -0
- package/docs/retro/0113-disambiguate-spawn-options.md +29 -0
- package/docs/retro/0114-narrow-agent-tool-menu-deps.md +38 -0
- package/package.json +1 -1
- package/src/agent-manager.ts +16 -3
- package/src/index.ts +9 -32
- package/src/tools/agent-tool.ts +48 -215
- package/src/tools/background-spawner.ts +116 -0
- package/src/tools/foreground-runner.ts +175 -0
- package/src/tools/helpers.ts +83 -1
- package/src/ui/agent-menu.ts +9 -2
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
---
|
|
2
|
+
issue: 115
|
|
3
|
+
issue_title: "refactor(pi-subagents): decompose agent-tool.ts into foreground/background modules"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Decompose agent-tool.ts into foreground/background modules
|
|
7
|
+
|
|
8
|
+
## Problem Statement
|
|
9
|
+
|
|
10
|
+
`tools/agent-tool.ts` is the largest file in the package at 579 lines.
|
|
11
|
+
The `execute` function handles three distinct execution paths — resume, background spawn, and foreground streaming — each with different dependencies.
|
|
12
|
+
Before those paths can be cleanly extracted, two upstream API gaps force the tool to work around the manager:
|
|
13
|
+
|
|
14
|
+
1. The foreground `onSessionCreated` callback loops through `manager.listAgents()` matching by session object just to discover the agent's ID — because `onSessionCreated` only receives the session, not the record.
|
|
15
|
+
2. The background path mutates `record.notification` after spawn — reaching into the record returned by `getRecord()` to attach a `NotificationState` — because the manager has no way to wire notification state at spawn time.
|
|
16
|
+
|
|
17
|
+
These workarounds would simply move into the extracted modules unchanged.
|
|
18
|
+
Fixing the API gaps first makes the extraction clean: each extracted module receives what it actually needs from the manager, without reaching through or reverse-searching.
|
|
19
|
+
|
|
20
|
+
## Goals
|
|
21
|
+
|
|
22
|
+
- Widen `onSessionCreated` to `(session, record)` so callers receive the agent ID and record directly.
|
|
23
|
+
- Accept `toolCallId` in `AgentSpawnConfig` so the manager wires `record.notification` internally for background agents.
|
|
24
|
+
- Extract the foreground execution loop into `tools/foreground-runner.ts`.
|
|
25
|
+
- Extract the background spawn path into `tools/background-spawner.ts`.
|
|
26
|
+
- Move `getStatusNote` and `buildDetails` to `tools/helpers.ts`.
|
|
27
|
+
- Keep `agent-tool.ts` as the orchestrator (~250 lines): tool definition, parameter validation, shared setup, dispatch, resume.
|
|
28
|
+
- Preserve all existing behavior.
|
|
29
|
+
|
|
30
|
+
## Non-Goals
|
|
31
|
+
|
|
32
|
+
- Extracting the resume path (~27 lines) — too small to warrant a separate file.
|
|
33
|
+
- Extracting `renderCall`/`renderResult` — tightly coupled to the tool definition.
|
|
34
|
+
- Changing `AgentToolDeps` shape — #114 already narrowed it.
|
|
35
|
+
- Removing `onSessionCreated` from `AgentSpawnConfig` entirely — it is still useful for UI observer wiring that the manager should not own.
|
|
36
|
+
|
|
37
|
+
## Background
|
|
38
|
+
|
|
39
|
+
### Prerequisite status
|
|
40
|
+
|
|
41
|
+
| Issue | Title | Status |
|
|
42
|
+
| ----- | ------------------------------------------ | ------- |
|
|
43
|
+
| #114 | Narrow `AgentToolDeps` and `AgentMenuDeps` | ✅ Done |
|
|
44
|
+
|
|
45
|
+
### Current API gaps
|
|
46
|
+
|
|
47
|
+
#### Gap 1: foreground ID discovery
|
|
48
|
+
|
|
49
|
+
The foreground path needs the agent ID *during* execution (inside `onSessionCreated`, before `spawnAndWait` resolves) to register the activity tracker in the widget.
|
|
50
|
+
The manager's internal `onSessionCreated` handler already has `id` and `record` in scope but passes only `session` to the caller's callback.
|
|
51
|
+
The tool works around this by iterating `listAgents()` and matching by session identity:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
onSessionCreated: (session) => {
|
|
55
|
+
for (const a of deps.manager.listAgents()) {
|
|
56
|
+
if (a.execution?.session === session) {
|
|
57
|
+
fgId = a.id;
|
|
58
|
+
deps.agentActivity.set(a.id, fgState);
|
|
59
|
+
// ...
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
This is a violation of Tell-Don't-Ask: the tool asks the manager for data it already has.
|
|
66
|
+
|
|
67
|
+
#### Gap 2: post-spawn notification mutation
|
|
68
|
+
|
|
69
|
+
The background path calls `manager.spawn()`, then immediately calls `manager.getRecord(id)` to mutate `record.notification`:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const id = deps.manager.spawn(ctx, subagentType, prompt, { ... });
|
|
73
|
+
const record = deps.manager.getRecord(id);
|
|
74
|
+
if (record) {
|
|
75
|
+
record.notification = new NotificationState(toolCallId);
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
This is an output argument — the tool writes back into a record it doesn't own.
|
|
80
|
+
The notification could be wired at spawn time if the manager accepted a `toolCallId`.
|
|
81
|
+
|
|
82
|
+
### Relevant design principles
|
|
83
|
+
|
|
84
|
+
- **Tell-Don't-Ask** (code-design skill): the `listAgents()` loop asks the manager for data it already has.
|
|
85
|
+
- **Output arguments** (code-design skill): writing `record.notification` after spawn mutates an object owned by the manager.
|
|
86
|
+
- **SRP**: foreground streaming and background spawning are independent concerns.
|
|
87
|
+
- **One concern per file** (AGENTS.md): the file mixes orchestration, streaming, spawning, and formatting.
|
|
88
|
+
|
|
89
|
+
## Design Overview
|
|
90
|
+
|
|
91
|
+
### Phase 1: Fix manager API gaps
|
|
92
|
+
|
|
93
|
+
#### Widen `onSessionCreated` to include record
|
|
94
|
+
|
|
95
|
+
Change the callback signature in both `AgentSpawnConfig` and the runner's `RunOptions`:
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// agent-manager.ts — AgentSpawnConfig
|
|
99
|
+
onSessionCreated?: (session: AgentSession, record: AgentRecord) => void;
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The manager's internal handler already has `record` in scope — pass it through:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// In startAgent(), existing line:
|
|
106
|
+
options.onSessionCreated?.(session);
|
|
107
|
+
// Becomes:
|
|
108
|
+
options.onSessionCreated?.(session, record);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The runner's `onSessionCreated` stays `(session: AgentSession) => void` — it doesn't know about records.
|
|
112
|
+
The manager wraps the runner callback and adds `record` before forwarding to the caller.
|
|
113
|
+
|
|
114
|
+
This lets the foreground tool callback access `record.id` directly, eliminating the `listAgents()` loop.
|
|
115
|
+
|
|
116
|
+
#### Accept `toolCallId` in `AgentSpawnConfig`
|
|
117
|
+
|
|
118
|
+
Add an optional `toolCallId` field to `AgentSpawnConfig`:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
export interface AgentSpawnConfig {
|
|
122
|
+
// ... existing fields ...
|
|
123
|
+
/** Tool call ID for background notification wiring. When set, spawn attaches NotificationState. */
|
|
124
|
+
toolCallId?: string;
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
In `AgentManager.spawn()`, after creating the record:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
if (options.toolCallId) {
|
|
132
|
+
record.notification = new NotificationState(options.toolCallId);
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
This moves the notification wiring into the manager, eliminating the post-spawn mutation in the tool.
|
|
137
|
+
|
|
138
|
+
### Phase 2: Extract modules
|
|
139
|
+
|
|
140
|
+
With the API gaps fixed, the extracted modules no longer need `listAgents` or `getRecord` or post-spawn record mutation.
|
|
141
|
+
|
|
142
|
+
#### Foreground runner
|
|
143
|
+
|
|
144
|
+
After the `onSessionCreated` widening, the foreground callback simplifies to:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
onSessionCreated: (session, record) => {
|
|
148
|
+
fgState.setSession(session);
|
|
149
|
+
unsubUI = subscribeUIObserver(session, fgState, streamUpdate);
|
|
150
|
+
fgId = record.id;
|
|
151
|
+
deps.agentActivity.set(record.id, fgState);
|
|
152
|
+
deps.widget.ensureTimer();
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The `runForeground` function receives narrow deps:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
export interface ForegroundDeps {
|
|
160
|
+
manager: { spawnAndWait: AgentToolManager["spawnAndWait"] };
|
|
161
|
+
widget: { ensureTimer(): void; markFinished(id: string): void };
|
|
162
|
+
agentActivity: AgentActivityAccess;
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
No `listAgents` needed — the record is delivered by the callback.
|
|
167
|
+
|
|
168
|
+
#### Background spawner
|
|
169
|
+
|
|
170
|
+
After the `toolCallId` change, the background path simplifies to:
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
const id = deps.manager.spawn(ctx, subagentType, prompt, {
|
|
174
|
+
...spawnConfig,
|
|
175
|
+
toolCallId,
|
|
176
|
+
});
|
|
177
|
+
// No getRecord + mutation needed — notification already wired
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
The `spawnBackground` function receives narrow deps:
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
export interface BackgroundDeps {
|
|
184
|
+
manager: { spawn: AgentToolManager["spawn"]; getRecord: AgentToolManager["getRecord"]; getMaxConcurrent: AgentToolManager["getMaxConcurrent"] };
|
|
185
|
+
widget: { ensureTimer(): void; update(): void };
|
|
186
|
+
agentActivity: AgentActivityAccess;
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
`getRecord` is still needed for building the result message (checking `status`, `execution.outputFile`), but not for mutation.
|
|
191
|
+
|
|
192
|
+
#### What stays in agent-tool.ts
|
|
193
|
+
|
|
194
|
+
- `AgentToolDeps`, `AgentToolManager`, `AgentToolWidget`, `AgentActivityAccess` interfaces.
|
|
195
|
+
- `createAgentTool` factory: tool name/label/description, parameters schema, `renderCall`, `renderResult`.
|
|
196
|
+
- Execute's shared setup: registry reload, type resolution, model resolution, config assembly, detail base.
|
|
197
|
+
- Resume path (~27 lines).
|
|
198
|
+
- Dispatch to `spawnBackground()` or `runForeground()`.
|
|
199
|
+
|
|
200
|
+
#### Helpers relocation
|
|
201
|
+
|
|
202
|
+
`getStatusNote` and `buildDetails` move to `tools/helpers.ts`.
|
|
203
|
+
Both are pure formatting functions with no dependency on `AgentToolDeps`.
|
|
204
|
+
|
|
205
|
+
### Post-extraction file sizes (estimated)
|
|
206
|
+
|
|
207
|
+
| File | Lines |
|
|
208
|
+
| ----------------------- | -------------- |
|
|
209
|
+
| `agent-tool.ts` | ~250 (was 579) |
|
|
210
|
+
| `foreground-runner.ts` | ~110 |
|
|
211
|
+
| `background-spawner.ts` | ~70 |
|
|
212
|
+
| `helpers.ts` additions | ~50 |
|
|
213
|
+
|
|
214
|
+
## Module-Level Changes
|
|
215
|
+
|
|
216
|
+
### Modified files
|
|
217
|
+
|
|
218
|
+
1. **`src/agent-manager.ts`**
|
|
219
|
+
- Change `onSessionCreated` in `AgentSpawnConfig` to `(session: AgentSession, record: AgentRecord) => void`.
|
|
220
|
+
- Pass `record` as second argument in `startAgent`'s internal `onSessionCreated` call.
|
|
221
|
+
- Add optional `toolCallId?: string` to `AgentSpawnConfig`.
|
|
222
|
+
- Wire `record.notification = new NotificationState(options.toolCallId)` in `spawn()` when present.
|
|
223
|
+
- Add `NotificationState` import.
|
|
224
|
+
|
|
225
|
+
2. **`src/tools/agent-tool.ts`**
|
|
226
|
+
- Update `onSessionCreated` callbacks to accept `(session, record)`.
|
|
227
|
+
- Remove `listAgents()` loop in foreground callback — use `record.id` directly.
|
|
228
|
+
- Pass `toolCallId` in background spawn config — remove post-spawn `getRecord` + mutation.
|
|
229
|
+
- Remove foreground block → `runForeground()` call.
|
|
230
|
+
- Remove background block → `spawnBackground()` call.
|
|
231
|
+
- Remove `getStatusNote`, `buildDetails` → imported from `helpers.ts`.
|
|
232
|
+
- Remove `listAgents` from `AgentToolManager` interface (no longer needed).
|
|
233
|
+
- Remove unused imports: `NotificationState`, `describeActivity`, `SPINNER`, `formatMs`.
|
|
234
|
+
|
|
235
|
+
3. **`src/tools/helpers.ts`**
|
|
236
|
+
- Add `getStatusNote()` and `buildDetails()` (relocated from `agent-tool.ts`).
|
|
237
|
+
|
|
238
|
+
### New files
|
|
239
|
+
|
|
240
|
+
4. **`src/tools/foreground-runner.ts`**
|
|
241
|
+
- `ForegroundDeps` interface, `runForeground()` function.
|
|
242
|
+
- Owns: spinner interval, `AgentActivityTracker` creation, `subscribeUIObserver`, streaming `onUpdate`, cleanup, result formatting via `buildDetails`/`getStatusNote`.
|
|
243
|
+
|
|
244
|
+
5. **`src/tools/background-spawner.ts`**
|
|
245
|
+
- `BackgroundDeps` interface, `spawnBackground()` function.
|
|
246
|
+
- Owns: `AgentActivityTracker` creation, `subscribeUIObserver`, activity map registration, widget update, launch message formatting.
|
|
247
|
+
|
|
248
|
+
### Test files
|
|
249
|
+
|
|
250
|
+
6. **`test/agent-manager.test.ts`**
|
|
251
|
+
- Update mock runner calls to pass `record` in `onSessionCreated`.
|
|
252
|
+
- Add test: `spawn` wires `NotificationState` when `toolCallId` is provided.
|
|
253
|
+
- Add test: `spawn` does not wire `NotificationState` when `toolCallId` is absent.
|
|
254
|
+
|
|
255
|
+
7. **`test/tools/agent-tool.test.ts`**
|
|
256
|
+
- Update `onSessionCreated` mock signatures if needed (structural — tests call through `execute`).
|
|
257
|
+
- Existing tests remain as integration tests for the dispatch path.
|
|
258
|
+
|
|
259
|
+
8. **`test/tools/helpers.test.ts`** (new or extended)
|
|
260
|
+
- Unit tests for `getStatusNote` (all status branches) and `buildDetails`.
|
|
261
|
+
|
|
262
|
+
9. **`test/tools/foreground-runner.test.ts`** (new)
|
|
263
|
+
- Spinner lifecycle, streaming updates, cleanup on success/error, result formatting, fallback note.
|
|
264
|
+
|
|
265
|
+
10. **`test/tools/background-spawner.test.ts`** (new)
|
|
266
|
+
- Activity tracker registered, widget updated, queued message, launch message format.
|
|
267
|
+
|
|
268
|
+
## Test Impact Analysis
|
|
269
|
+
|
|
270
|
+
1. **New unit tests enabled:**
|
|
271
|
+
- `foreground-runner.test.ts` tests spinner lifecycle and streaming with narrow mocks (no full `AgentToolDeps`).
|
|
272
|
+
- `background-spawner.test.ts` tests activity registration and message formatting in isolation.
|
|
273
|
+
- `helpers.test.ts` tests `getStatusNote` and `buildDetails` as pure functions.
|
|
274
|
+
- `agent-manager.test.ts` tests notification wiring at the manager level — moved from tool-level integration.
|
|
275
|
+
|
|
276
|
+
2. **Existing tests that simplify:**
|
|
277
|
+
- `agent-tool.test.ts` background tests no longer need to verify notification wiring (now the manager's job).
|
|
278
|
+
- The "registers activity in agentActivity map" test stays but becomes a dispatch-level integration test.
|
|
279
|
+
|
|
280
|
+
3. **Existing tests that must stay:**
|
|
281
|
+
- All `agent-tool.test.ts` tests exercise the full dispatch path and remain valuable as integration tests.
|
|
282
|
+
- All `agent-manager.test.ts` tests that fire `onSessionCreated` must update the mock signature to `(session, record)`.
|
|
283
|
+
|
|
284
|
+
## TDD Order
|
|
285
|
+
|
|
286
|
+
1. **Widen `onSessionCreated` callback to include record.**
|
|
287
|
+
Change `AgentSpawnConfig.onSessionCreated` signature to `(session, record)`.
|
|
288
|
+
Update `startAgent` to pass `record` as second argument.
|
|
289
|
+
Update `agent-tool.ts` foreground callback to use `record.id` instead of `listAgents()` loop.
|
|
290
|
+
Remove `listAgents` from `AgentToolManager` interface.
|
|
291
|
+
Update `agent-manager.test.ts` mock runner calls.
|
|
292
|
+
Test: verify foreground callback receives `record.id` (existing integration tests pass).
|
|
293
|
+
Commit: `refactor: widen onSessionCreated to include record`
|
|
294
|
+
|
|
295
|
+
2. **Accept `toolCallId` in `AgentSpawnConfig`.**
|
|
296
|
+
Add `toolCallId?: string` to `AgentSpawnConfig`.
|
|
297
|
+
Wire `NotificationState` in `spawn()` when `toolCallId` is provided.
|
|
298
|
+
Update `agent-tool.ts` background path to pass `toolCallId` instead of post-spawn mutation.
|
|
299
|
+
Test: `agent-manager.test.ts` — `spawn` wires notification when `toolCallId` present, skips when absent.
|
|
300
|
+
Commit: `refactor: wire NotificationState at spawn time via toolCallId`
|
|
301
|
+
|
|
302
|
+
3. **Relocate `getStatusNote` and `buildDetails` to `tools/helpers.ts`.**
|
|
303
|
+
Move both functions.
|
|
304
|
+
Update imports in `agent-tool.ts`.
|
|
305
|
+
Test: unit tests for `getStatusNote` (all branches) and `buildDetails`.
|
|
306
|
+
Commit: `refactor: move getStatusNote and buildDetails to tools/helpers`
|
|
307
|
+
|
|
308
|
+
4. **Extract `spawnBackground` into `tools/background-spawner.ts`.**
|
|
309
|
+
Define `BackgroundDeps` interface and `spawnBackground()` function.
|
|
310
|
+
Replace background block in `execute` with a call to `spawnBackground()`.
|
|
311
|
+
Remove unused imports from `agent-tool.ts`.
|
|
312
|
+
Test: `background-spawner.test.ts` — activity registration, widget update, launch message.
|
|
313
|
+
Commit: `refactor: extract background spawn to tools/background-spawner`
|
|
314
|
+
|
|
315
|
+
5. **Extract `runForeground` into `tools/foreground-runner.ts`.**
|
|
316
|
+
Define `ForegroundDeps` interface and `runForeground()` function.
|
|
317
|
+
Replace foreground block in `execute` with a call to `runForeground()`.
|
|
318
|
+
Remove unused imports from `agent-tool.ts`.
|
|
319
|
+
Test: `foreground-runner.test.ts` — spinner lifecycle, streaming, cleanup, result formatting.
|
|
320
|
+
Commit: `refactor: extract foreground execution to tools/foreground-runner`
|
|
321
|
+
|
|
322
|
+
6. **Verify integration.**
|
|
323
|
+
Run full test suite and `pnpm run check`.
|
|
324
|
+
Commit: `test: verify agent-tool decomposition integration`
|
|
325
|
+
|
|
326
|
+
## Risks and Mitigations
|
|
327
|
+
|
|
328
|
+
| Risk | Mitigation |
|
|
329
|
+
| ---------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
330
|
+
| Widening `onSessionCreated` signature is a breaking change to `AgentSpawnConfig` | `AgentSpawnConfig` is internal (not in package `exports`). The only external caller is `agent-tool.ts`. All test mocks update in the same step. |
|
|
331
|
+
| `toolCallId` on `AgentSpawnConfig` couples the manager to notification concerns | The manager already owns the record lifecycle. `NotificationState` is a record collaborator like `execution` and `worktreeState` — the manager already wires those. `toolCallId` is a data-in, not a behavior coupling. |
|
|
332
|
+
| Runner's `onSessionCreated` signature stays `(session)` while manager's is `(session, record)` | The manager wraps the runner's callback — the runner never sees the record. No change to runner interface. |
|
|
333
|
+
| Circular imports between new modules and `helpers.ts` | `helpers.ts` is a leaf module. The new modules import from it but it imports nothing from them. |
|
|
334
|
+
|
|
335
|
+
## Open Questions
|
|
336
|
+
|
|
337
|
+
None — the design is unambiguous after resolving the two API gaps.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
---
|
|
2
|
+
issue: 113
|
|
3
|
+
issue_title: "refactor(pi-subagents): disambiguate SpawnOptions (public vs internal)"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Retro: #113 — disambiguate SpawnOptions (public vs internal)
|
|
7
|
+
|
|
8
|
+
## Final Retrospective (2026-05-21T21:10:00-04:00)
|
|
9
|
+
|
|
10
|
+
### Session summary
|
|
11
|
+
|
|
12
|
+
Renamed the internal `SpawnOptions` in `agent-manager.ts` to `AgentSpawnConfig` to disambiguate it from the public `SpawnOptions` in `service.ts`.
|
|
13
|
+
Pure mechanical rename across 4 files with zero test-count delta (652/652).
|
|
14
|
+
Released as `pi-subagents-v6.8.3`.
|
|
15
|
+
|
|
16
|
+
### Observations
|
|
17
|
+
|
|
18
|
+
#### What went well
|
|
19
|
+
|
|
20
|
+
- Completely frictionless execution — single-step plan executed exactly as written with no corrections, rework, or failed edits.
|
|
21
|
+
- The session benefited from context already loaded during the preceding #112 cycle (same package, same skills, same source files), which made planning and execution faster.
|
|
22
|
+
|
|
23
|
+
#### What caused friction (agent side)
|
|
24
|
+
|
|
25
|
+
- Nothing — the rename was purely mechanical and the plan matched reality exactly.
|
|
26
|
+
|
|
27
|
+
#### What caused friction (user side)
|
|
28
|
+
|
|
29
|
+
- Nothing — no user intervention needed.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
issue: 114
|
|
3
|
+
issue_title: "refactor(pi-subagents): narrow AgentToolDeps and AgentMenuDeps"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Retro: #114 — narrow AgentToolDeps and AgentMenuDeps
|
|
7
|
+
|
|
8
|
+
## Final Retrospective (2026-05-21T21:43:48-04:00)
|
|
9
|
+
|
|
10
|
+
### Session summary
|
|
11
|
+
|
|
12
|
+
Narrowed `AgentToolDeps` from 9 to 6 fields and `AgentMenuDeps` from 8 to 7 fields.
|
|
13
|
+
Moved `subagents:created` event emission from the Agent tool to a new `AgentManagerObserver.onAgentCreated` method.
|
|
14
|
+
Extracted `buildTypeListText` to `tools/helpers.ts`, derived description text inside `createAgentTool`, removed dead `emitEvent` from `AgentMenuDeps`, and narrowed `agentActivity` to typed `AgentActivityAccess`/`AgentActivityReader` interfaces.
|
|
15
|
+
Test count increased from 638 to 660.
|
|
16
|
+
Released as `pi-subagents-v6.9.0`.
|
|
17
|
+
|
|
18
|
+
### Observations
|
|
19
|
+
|
|
20
|
+
#### What went well
|
|
21
|
+
|
|
22
|
+
- The `ask_user` gate during planning was well-targeted.
|
|
23
|
+
The first question (where to move `emitEvent`) had a clear answer.
|
|
24
|
+
The second (description-text derivation) genuinely needed user input, and the user requested more context via the "I could use more context" response — the follow-up `preview`-type question with fenced code blocks handled this cleanly.
|
|
25
|
+
- The 6-step TDD plan mapped to implementation with only one deviation (see below), caught exactly where the workflow is designed to catch it (the `pnpm run check` step).
|
|
26
|
+
- All 6 prerequisites (#108, #109, #110, #112, #113, #118) were verified as closed before planning.
|
|
27
|
+
The observer issue (#112) was correctly identified from a `gh issue list` grep despite not being explicitly numbered in the issue body (the issue said "the observer issue").
|
|
28
|
+
|
|
29
|
+
#### What caused friction (agent side)
|
|
30
|
+
|
|
31
|
+
- `missing-context` (self-identified) — Step 6 narrowed `agentActivity` from `Map<string, AgentActivityTracker>` to `AgentActivityAccess` (which exposes only `get`/`set`/`delete`), but the test in `agent-tool.test.ts` used `.has()` on the map.
|
|
32
|
+
The `pnpm run check` typecheck caught `Property 'has' does not exist on type 'AgentActivityAccess'`.
|
|
33
|
+
Fixed by replacing `.has(id)` with `.get(id) !== undefined` in the same commit.
|
|
34
|
+
Impact: one extra read + edit cycle (~30 seconds), no rework.
|
|
35
|
+
|
|
36
|
+
#### What caused friction (user side)
|
|
37
|
+
|
|
38
|
+
- Nothing — no user corrections or redirections needed during the session.
|
package/package.json
CHANGED
package/src/agent-manager.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { AgentRecord } from "./agent-record.js";
|
|
|
13
13
|
import type { AgentRunner } from "./agent-runner.js";
|
|
14
14
|
import { AgentTypeRegistry } from "./agent-types.js";
|
|
15
15
|
import { debugLog } from "./debug.js";
|
|
16
|
+
import { NotificationState } from "./notification-state.js";
|
|
16
17
|
import { buildParentSnapshot } from "./parent-snapshot.js";
|
|
17
18
|
import { subscribeRecordObserver } from "./record-observer.js";
|
|
18
19
|
import type { RunConfig } from "./runtime.js";
|
|
@@ -27,6 +28,8 @@ export interface AgentManagerObserver {
|
|
|
27
28
|
onAgentStarted(record: AgentRecord): void;
|
|
28
29
|
onAgentCompleted(record: AgentRecord): void;
|
|
29
30
|
onAgentCompacted(record: AgentRecord, info: CompactionInfo): void;
|
|
31
|
+
/** Fires synchronously after a background agent record is created (before startAgent). */
|
|
32
|
+
onAgentCreated(record: AgentRecord): void;
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
/** Default max concurrent background agents. */
|
|
@@ -70,12 +73,14 @@ export interface AgentSpawnConfig {
|
|
|
70
73
|
invocation?: AgentInvocation;
|
|
71
74
|
/** Parent abort signal — when aborted, the subagent is also stopped. */
|
|
72
75
|
signal?: AbortSignal;
|
|
73
|
-
/** Called when the agent session is created — the
|
|
74
|
-
onSessionCreated?: (session: AgentSession) => void;
|
|
76
|
+
/** Called when the agent session is created — receives the session and the agent's record. */
|
|
77
|
+
onSessionCreated?: (session: AgentSession, record: AgentRecord) => void;
|
|
75
78
|
/** Path to the parent session's JSONL file (for deriving the subagent session directory). */
|
|
76
79
|
parentSessionFile?: string;
|
|
77
80
|
/** Session ID of the parent agent (stored in the child session's parentSession header). */
|
|
78
81
|
parentSessionId?: string;
|
|
82
|
+
/** Tool call ID for background notification wiring. When set, spawn attaches NotificationState. */
|
|
83
|
+
toolCallId?: string;
|
|
79
84
|
}
|
|
80
85
|
|
|
81
86
|
export class AgentManager {
|
|
@@ -153,6 +158,14 @@ export class AgentManager {
|
|
|
153
158
|
});
|
|
154
159
|
this.agents.set(id, record);
|
|
155
160
|
|
|
161
|
+
if (options.toolCallId) {
|
|
162
|
+
record.notification = new NotificationState(options.toolCallId);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (options.isBackground) {
|
|
166
|
+
this.observer?.onAgentCreated(record);
|
|
167
|
+
}
|
|
168
|
+
|
|
156
169
|
const snapshot = buildParentSnapshot(ctx, options.inheritContext);
|
|
157
170
|
const args: SpawnArgs = { snapshot, type, prompt, options };
|
|
158
171
|
|
|
@@ -238,7 +251,7 @@ export class AgentManager {
|
|
|
238
251
|
unsubRecordObserver = subscribeRecordObserver(session, record, {
|
|
239
252
|
onCompact: (r, info) => this.observer?.onAgentCompacted(r, info),
|
|
240
253
|
});
|
|
241
|
-
options.onSessionCreated?.(session);
|
|
254
|
+
options.onSessionCreated?.(session, record);
|
|
242
255
|
},
|
|
243
256
|
})
|
|
244
257
|
.then(({ responseText, session, aborted, steered, sessionFile }) => {
|
package/src/index.ts
CHANGED
|
@@ -110,6 +110,15 @@ export default function (pi: ExtensionAPI) {
|
|
|
110
110
|
compactionCount: record.compactionCount,
|
|
111
111
|
});
|
|
112
112
|
},
|
|
113
|
+
onAgentCreated(record) {
|
|
114
|
+
// Emit created event for background agents (before startAgent / queue drain).
|
|
115
|
+
pi.events.emit("subagents:created", {
|
|
116
|
+
id: record.id,
|
|
117
|
+
type: record.type,
|
|
118
|
+
description: record.description,
|
|
119
|
+
isBackground: true,
|
|
120
|
+
});
|
|
121
|
+
},
|
|
113
122
|
};
|
|
114
123
|
|
|
115
124
|
const manager = new AgentManager({
|
|
@@ -151,33 +160,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
151
160
|
const toolStart = new ToolStartHandler(runtime);
|
|
152
161
|
pi.on("tool_execution_start", (event, ctx) => toolStart.handleToolExecutionStart(event, ctx));
|
|
153
162
|
|
|
154
|
-
/** Build the full type list text dynamically from the unified registry. */
|
|
155
|
-
const buildTypeListText = () => {
|
|
156
|
-
const defaultNames = registry.getDefaultAgentNames();
|
|
157
|
-
const userNames = registry.getUserAgentNames();
|
|
158
|
-
|
|
159
|
-
const defaultDescs = defaultNames.map((name) => {
|
|
160
|
-
const cfg = registry.resolveAgentConfig(name);
|
|
161
|
-
const modelSuffix = cfg.model ? ` (${getModelLabelFromConfig(cfg.model)})` : "";
|
|
162
|
-
return `- ${name}: ${cfg.description}${modelSuffix}`;
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const customDescs = userNames.map((name) => {
|
|
166
|
-
const cfg = registry.resolveAgentConfig(name);
|
|
167
|
-
return `- ${name}: ${cfg.description}`;
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
return [
|
|
171
|
-
"Default agents:",
|
|
172
|
-
...defaultDescs,
|
|
173
|
-
...(customDescs.length > 0 ? ["", "Custom agents:", ...customDescs] : []),
|
|
174
|
-
"",
|
|
175
|
-
`Custom agents can be defined in .pi/agents/<name>.md (project) or ${getAgentDir()}/agents/<name>.md (global) — they are picked up automatically. Project-level agents override global ones. Creating a .md file with the same name as a default agent overrides it.`,
|
|
176
|
-
].join("\n");
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
const typeListText = buildTypeListText();
|
|
180
|
-
|
|
181
163
|
// ---- Agent tool ----
|
|
182
164
|
|
|
183
165
|
pi.registerTool(defineTool(createAgentTool({
|
|
@@ -187,7 +169,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
187
169
|
resume: (id, prompt, signal) => manager.resume(id, prompt, signal),
|
|
188
170
|
getRecord: (id) => manager.getRecord(id),
|
|
189
171
|
getMaxConcurrent: () => settings.maxConcurrent,
|
|
190
|
-
listAgents: () => manager.listAgents(),
|
|
191
172
|
},
|
|
192
173
|
widget: {
|
|
193
174
|
setUICtx: (ctx) => runtime.setUICtx(ctx as UICtx),
|
|
@@ -196,10 +177,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
196
177
|
markFinished: (id) => runtime.markFinished(id),
|
|
197
178
|
},
|
|
198
179
|
agentActivity: runtime.agentActivity,
|
|
199
|
-
emitEvent: (name, data) => pi.events.emit(name, data),
|
|
200
180
|
registry,
|
|
201
|
-
typeListText,
|
|
202
|
-
availableTypesText: registry.getAvailableTypes().join(", "),
|
|
203
181
|
agentDir: getAgentDir(),
|
|
204
182
|
settings,
|
|
205
183
|
})));
|
|
@@ -242,7 +220,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
242
220
|
return getModelLabelFromConfig(cfg.model);
|
|
243
221
|
},
|
|
244
222
|
settings,
|
|
245
|
-
emitEvent: (name, data) => pi.events.emit(name, data),
|
|
246
223
|
personalAgentsDir: join(getAgentDir(), 'agents'),
|
|
247
224
|
projectAgentsDir: join(process.cwd(), '.pi', 'agents'),
|
|
248
225
|
});
|