@gotgenes/pi-subagents 10.2.1 → 11.0.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/CHANGELOG.md CHANGED
@@ -5,6 +5,29 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [11.0.0](https://github.com/gotgenes/pi-packages/compare/pi-subagents-v10.2.1...pi-subagents-v11.0.0) (2026-05-28)
9
+
10
+
11
+ ### ⚠ BREAKING CHANGES
12
+
13
+ * AgentSpawnConfig.onSessionCreated is replaced by AgentSpawnConfig.observer (AgentLifecycleObserver). Callers that used onSessionCreated must use observer.onSessionCreated instead.
14
+
15
+ ### Features
16
+
17
+ * add Agent.run() encapsulating full execution lifecycle ([#229](https://github.com/gotgenes/pi-packages/issues/229)) ([780cb72](https://github.com/gotgenes/pi-packages/commit/780cb72ea7a6981ee283a9de791d5fb65cabaa28))
18
+ * add AgentLifecycleObserver interface ([#229](https://github.com/gotgenes/pi-packages/issues/229)) ([c0f08a4](https://github.com/gotgenes/pi-packages/commit/c0f08a40ce36afa3a7876345709db56e192a893b))
19
+ * AgentManager.spawn() creates complete Agent, deletes startAgent ([#229](https://github.com/gotgenes/pi-packages/issues/229)) ([4d83c6d](https://github.com/gotgenes/pi-packages/commit/4d83c6d583df905054066ed841a67795753928d4))
20
+ * expand AgentInit with run-config, deps, and self-created AbortController ([#229](https://github.com/gotgenes/pi-packages/issues/229)) ([e522f23](https://github.com/gotgenes/pi-packages/commit/e522f23232e4b447ffa39e36a19cf70c7e5cbaae))
21
+
22
+
23
+ ### Documentation
24
+
25
+ * mark Phase 15 Step 4 complete, update architecture ([#229](https://github.com/gotgenes/pi-packages/issues/229)) ([29b0da8](https://github.com/gotgenes/pi-packages/commit/29b0da8435aaa5be51fc073612fc09d9a22004bc))
26
+ * plan Agent born complete — Agent.run() absorbs startAgent ([#229](https://github.com/gotgenes/pi-packages/issues/229)) ([c1588b5](https://github.com/gotgenes/pi-packages/commit/c1588b58ae697d22db1bf30b8997e5502626c33b))
27
+ * **retro:** add planning stage notes for issue [#229](https://github.com/gotgenes/pi-packages/issues/229) ([21243d5](https://github.com/gotgenes/pi-packages/commit/21243d564691daab5dcddff23809a82db56c0660))
28
+ * **retro:** add retro notes for issue [#231](https://github.com/gotgenes/pi-packages/issues/231) ([249cce0](https://github.com/gotgenes/pi-packages/commit/249cce0e1c7642d8c85da67e8f3c92f210735e7b))
29
+ * **retro:** add TDD stage notes for issue [#229](https://github.com/gotgenes/pi-packages/issues/229) ([047cd9e](https://github.com/gotgenes/pi-packages/commit/047cd9e2816f6a25d93bbba33fc93b228989f272))
30
+
8
31
  ## [10.2.1](https://github.com/gotgenes/pi-packages/compare/pi-subagents-v10.2.0...pi-subagents-v10.2.1) (2026-05-27)
9
32
 
10
33
 
@@ -266,9 +266,9 @@ src/
266
266
  │ └── session-dir.ts session directory derivation
267
267
 
268
268
  ├── lifecycle/ agent execution and state tracking
269
- │ ├── agent-manager.ts spawn, queue, abort, resume, concurrency
269
+ │ ├── agent-manager.ts collection manager + concurrency controller
270
270
  │ ├── agent-runner.ts session creation, turn loop, tool filtering
271
- │ ├── agent.ts status state machine, per-agent behavior (abort, steer, worktree)
271
+ │ ├── agent.ts owns full execution lifecycle (run, abort, steer, worktree)
272
272
  │ ├── parent-snapshot.ts immutable spawn-time parent state
273
273
  │ ├── execution-state.ts session/output phase state
274
274
  │ ├── permission-bridge.ts optional bridge to pi-permission-system registry
@@ -780,14 +780,15 @@ Worktree setup was hoisted to callers (`spawn`, `drainQueue`) to preserve the sy
780
780
  - Smell: C (relay-only dependencies)
781
781
  - Outcome: `AgentManager` loses 2 fields; `AgentManagerOptions` shrinks from 7 to 5 fields; runner is self-contained
782
782
 
783
- ### Step 4: Agent born complete — Agent.run() absorbs startAgent — [#229]
783
+ ### Step 4: Agent born complete — Agent.run() absorbs startAgent — [#229]
784
784
 
785
785
  Agent receives `runner`, `worktrees`, and a lifecycle observer at construction.
786
- Agent creates its own `NotificationState` from `parentSession.toolCallId` — no external write.
786
+ Agent creates its own `AbortController` and `NotificationState` from `parentSession.toolCallId` — no external writes.
787
787
  `Agent.run()` encapsulates the entire execution lifecycle: worktree setup, runner invocation, session-creation handling, observer wiring, worktree cleanup, and status transitions.
788
788
  `startAgent` is deleted from `AgentManager`.
789
- The `onSessionCreated` callback is removed from `AgentSpawnConfig` — Agent handles session creation internally and notifies via the lifecycle observer.
789
+ The `onSessionCreated` callback is removed from `AgentSpawnConfig` — replaced by `AgentLifecycleObserver` passed at construction.
790
790
  `SpawnArgs` is deleted — Agent has its config from construction.
791
+ The queue is simplified from `{ id, args }[]` to `string[]` (agent IDs only).
791
792
 
792
793
  `AgentManager.spawn()` becomes: create complete Agent, put in map, call `agent.run()` or queue the agent ID.
793
794
 
@@ -0,0 +1,564 @@
1
+ ---
2
+ issue: 229
3
+ issue_title: "Agent born complete: Agent.run() absorbs startAgent (Phase 15, Step 4)"
4
+ ---
5
+
6
+ # Agent born complete — Agent.run() absorbs startAgent
7
+
8
+ ## Problem Statement
9
+
10
+ `AgentManager.startAgent()` orchestrates the entire agent execution lifecycle on behalf of the agent.
11
+ The manager reaches into Agent 10 times across `spawn()` + `startAgent()`: writing `notification` and `execution` after construction, passing its own deps as method arguments, wiring callbacks through three layers, and calling completion methods with its own state.
12
+ Agent cannot run itself — it depends on the manager to push it through every phase.
13
+
14
+ The `onSessionCreated` callback threads through `AgentSpawnConfig` → `startAgent` → `RunOptions` → runner, crossing four module boundaries just to deliver a session reference to the tool boundary.
15
+
16
+ ## Goals
17
+
18
+ - Agent receives `runner`, `worktrees`, and a lifecycle observer at construction — born complete with all dependencies.
19
+ - `Agent.run()` encapsulates the full execution lifecycle: worktree setup, runner invocation, session-creation handling, observer wiring, worktree cleanup, and status transitions.
20
+ - Delete `AgentManager.startAgent()`, `SpawnArgs`, and `onSessionCreated` from `AgentSpawnConfig`.
21
+ - Zero post-construction writes from `AgentManager` to Agent (`notification`, `execution`, `promise` all set internally).
22
+ - Move `ParentSessionInfo` and `CompactionInfo` out of `agent-manager.ts` to break the type-import cycle that would arise from `agent.ts` importing from `agent-manager.ts`.
23
+ - Worktree failures propagate through the async error surface (uniform with all other run errors).
24
+
25
+ ## Non-Goals
26
+
27
+ - Extracting `ConcurrencyQueue` from `AgentManager` — deferred to #230.
28
+ - Adding `Agent.resume()` — deferred to #232.
29
+ - Removing scheduling fields from `AgentManager` (queue, runningBackground, drainQueue) — deferred to #230.
30
+ - Changing `agent-runner.ts` internals — the runner's `RunOptions.onSessionCreated` callback stays; Agent is the caller now instead of the manager.
31
+ - Restructuring `AgentInit` into nested sub-objects (identity/config/deps) — this plan adds optional fields alongside existing ones to minimize test churn; a follow-up can tighten the interface.
32
+
33
+ ## Background
34
+
35
+ ### Prerequisites (complete)
36
+
37
+ - Issue #227 (Agent with behavior) — moved abort, steer buffering, worktree setup, `completeRun`/`failRun` from manager to Agent. ✅
38
+ - Issue #228 (async startAgent) — converted `startAgent` to async/await, eliminated `.then()`/`.catch()`. ✅
39
+ - Issue #231 (runner self-contained) — moved `exec` and `registry` to `ConcreteAgentRunner` construction. ✅
40
+
41
+ ### Key modules
42
+
43
+ | Module | Role | Change |
44
+ | ----------------------- | ---------------------------------------- | ----------------------------------------------------- |
45
+ | `agent.ts` | Status machine + per-agent behavior | Gains `run()`, lifecycle observer, run-config fields |
46
+ | `agent-manager.ts` | Collection + concurrency + orchestration | Loses `startAgent`, `SpawnArgs`; `spawn()` simplified |
47
+ | `agent-runner.ts` | Session orchestration | Imports `ParentSessionInfo` from new location |
48
+ | `background-spawner.ts` | Tool boundary for background spawn | `onSessionCreated` → `observer` field |
49
+ | `foreground-runner.ts` | Tool boundary for foreground spawn | `onSessionCreated` → `observer` field |
50
+ | `record-observer.ts` | Session event → Agent stats | Imports `CompactionInfo` from new location |
51
+
52
+ ### Constraint: biome/eslint conflict
53
+
54
+ Per AGENTS.md, when both linters run on the same file, restructure code to eliminate assertions entirely with explicit `if` guards.
55
+
56
+ ## Design Overview
57
+
58
+ ### AgentLifecycleObserver interface
59
+
60
+ A per-agent observer created by `AgentManager` and passed to `Agent` at construction.
61
+ Replaces the scattered callback mechanisms (`onRunFinished`, `onSessionCreated`, `onCompact`).
62
+
63
+ ```typescript
64
+ /** Per-agent lifecycle observer — created by AgentManager for each spawn. */
65
+ interface AgentLifecycleObserver {
66
+ /** Fires when the agent transitions to running (inside run(), after markRunning). */
67
+ onStarted?(agent: Agent): void;
68
+ /** Fires when the runner creates the session — delivers the session to external consumers. */
69
+ onSessionCreated?(agent: Agent, session: AgentSession): void;
70
+ /** Fires once when the run completes or fails (for concurrency drain). */
71
+ onRunFinished?(agent: Agent): void;
72
+ /** Fires on compaction events during the run. */
73
+ onCompacted?(agent: Agent, info: CompactionInfo): void;
74
+ }
75
+ ```
76
+
77
+ All methods are optional — manager composes only the callbacks needed per agent.
78
+
79
+ ### Agent constructor changes
80
+
81
+ New optional fields on `AgentInit` (alongside existing identity + status fields):
82
+
83
+ ```typescript
84
+ interface AgentInit {
85
+ // ... existing identity + status fields unchanged ...
86
+
87
+ // Shared deps (new — required for run(), optional for tests)
88
+ runner?: AgentRunner;
89
+ worktrees?: WorktreeManager;
90
+ observer?: AgentLifecycleObserver;
91
+ getRunConfig?: () => RunConfig;
92
+
93
+ // Run config (new — required for run(), optional for tests)
94
+ snapshot?: ParentSnapshot;
95
+ prompt?: string;
96
+ model?: Model<any>;
97
+ maxTurns?: number;
98
+ isolated?: boolean;
99
+ thinkingLevel?: ThinkingLevel;
100
+ isolation?: IsolationMode;
101
+ parentSession?: ParentSessionInfo;
102
+ isBackground?: boolean;
103
+ signal?: AbortSignal;
104
+ }
105
+ ```
106
+
107
+ Fields are optional so existing tests that only test status transitions and steer buffering continue to work unchanged.
108
+ `Agent.run()` guards on the required fields (`runner`, `snapshot`, `prompt`) and throws if missing.
109
+
110
+ Agent creates its own `AbortController` in the constructor (not passed in).
111
+ Agent creates its own `NotificationState` from `parentSession?.toolCallId` in the constructor (no external write).
112
+
113
+ ### Agent.run() sketch
114
+
115
+ ```typescript
116
+ async run(): Promise<void> {
117
+ if (!this.runner || !this.snapshot) {
118
+ throw new Error("Agent not configured for execution — missing runner or snapshot");
119
+ }
120
+ this.markRunning(Date.now());
121
+ this.observer?.onStarted?.(this);
122
+ this.wireSignal(this._signal, () => this.abort());
123
+
124
+ try {
125
+ this.setupWorktree(); // internal, uses this.worktrees + this.isolation
126
+ } catch (err) {
127
+ this.markError(err);
128
+ this.observer?.onRunFinished?.(this);
129
+ return;
130
+ }
131
+
132
+ const runConfig = this._getRunConfig?.();
133
+ try {
134
+ const result = await this.runner.run(this.snapshot, this.type, this._prompt, {
135
+ context: { cwd: this.worktreeState?.path, parentSession: this._parentSession },
136
+ model: this._model,
137
+ maxTurns: this._maxTurns,
138
+ defaultMaxTurns: runConfig?.defaultMaxTurns,
139
+ graceTurns: runConfig?.graceTurns,
140
+ isolated: this._isolated,
141
+ thinkingLevel: this._thinkingLevel,
142
+ signal: this.abortController!.signal,
143
+ onSessionCreated: (session) => {
144
+ const outputFile = session.sessionManager?.getSessionFile?.() ?? undefined;
145
+ this.execution = { session, outputFile };
146
+ this.flushPendingSteers(session);
147
+ this.attachObserver(subscribeAgentObserver(session, this, {
148
+ onCompact: (r, info) => this.observer?.onCompacted?.(r, info),
149
+ }));
150
+ this.observer?.onSessionCreated?.(this, session);
151
+ },
152
+ });
153
+ this.completeRun(result);
154
+ } catch (err) {
155
+ this.failRun(err);
156
+ }
157
+ }
158
+ ```
159
+
160
+ ### AgentManager.spawn() — after
161
+
162
+ ```typescript
163
+ spawn(snapshot, type, prompt, options): string {
164
+ const id = randomUUID().slice(0, 17);
165
+
166
+ const compositeObserver = this.buildObserver(options);
167
+
168
+ const record = new Agent({
169
+ id, type,
170
+ description: options.description,
171
+ status: options.isBackground ? "queued" : "running",
172
+ startedAt: Date.now(),
173
+ invocation: options.invocation,
174
+ // Run config
175
+ snapshot, prompt,
176
+ model: options.model,
177
+ maxTurns: options.maxTurns,
178
+ isolated: options.isolated,
179
+ thinkingLevel: options.thinkingLevel,
180
+ isolation: options.isolation,
181
+ parentSession: options.parentSession,
182
+ isBackground: options.isBackground,
183
+ signal: options.signal,
184
+ // Shared deps
185
+ runner: this.runner,
186
+ worktrees: this.worktrees,
187
+ observer: compositeObserver,
188
+ getRunConfig: this.getRunConfig,
189
+ });
190
+ this.agents.set(id, record);
191
+
192
+ if (options.isBackground) this.observer?.onAgentCreated(record);
193
+
194
+ if (options.isBackground && !options.bypassQueue
195
+ && this.runningBackground >= this._getMaxConcurrent()) {
196
+ this.queue.push(id);
197
+ return id;
198
+ }
199
+
200
+ record.promise = record.run();
201
+ return id;
202
+ }
203
+ ```
204
+
205
+ ### Observer composition — `buildObserver`
206
+
207
+ ```typescript
208
+ private buildObserver(options: AgentSpawnConfig): AgentLifecycleObserver {
209
+ return {
210
+ onStarted: (agent) => {
211
+ if (options.isBackground) this.runningBackground++;
212
+ this.observer?.onAgentStarted(agent);
213
+ },
214
+ onSessionCreated: options.observer?.onSessionCreated,
215
+ onRunFinished: (agent) => {
216
+ if (options.isBackground) this.finalizeBackgroundRun(agent);
217
+ },
218
+ onCompacted: (agent, info) => {
219
+ this.observer?.onAgentCompacted(agent, info);
220
+ },
221
+ };
222
+ }
223
+ ```
224
+
225
+ ### AgentSpawnConfig changes
226
+
227
+ ```typescript
228
+ export interface AgentSpawnConfig {
229
+ description: string;
230
+ model?: Model<any>;
231
+ maxTurns?: number;
232
+ isolated?: boolean;
233
+ inheritContext?: boolean;
234
+ thinkingLevel?: ThinkingLevel;
235
+ isBackground?: boolean;
236
+ bypassQueue?: boolean;
237
+ isolation?: IsolationMode;
238
+ invocation?: AgentInvocation;
239
+ signal?: AbortSignal;
240
+ parentSession?: ParentSessionInfo;
241
+ /** Per-agent lifecycle observer — replaces onSessionCreated callback. */
242
+ observer?: AgentLifecycleObserver;
243
+ // DELETED: onSessionCreated
244
+ }
245
+ ```
246
+
247
+ ### Queue simplification
248
+
249
+ The queue changes from `{ id: string; args: SpawnArgs }[]` to `string[]` (agent IDs only).
250
+ `drainQueue` calls `record.run()` directly:
251
+
252
+ ```typescript
253
+ private drainQueue() {
254
+ while (this.queue.length > 0 && this.runningBackground < this._getMaxConcurrent()) {
255
+ const id = this.queue.shift()!;
256
+ const record = this.agents.get(id);
257
+ if (record?.status !== "queued") continue;
258
+ record.promise = record.run();
259
+ }
260
+ }
261
+ ```
262
+
263
+ The `setupWorktree` call and error handling in `drainQueue` are subsumed by `Agent.run()`.
264
+ Worktree errors propagate through the promise and surface on `record.error` via `Agent.run()`'s internal `catch` → `markError` → `observer.onRunFinished`.
265
+
266
+ ### Error surface change
267
+
268
+ The synchronous-throw contract for worktree failure in `spawn()` is replaced by a uniform async error surface:
269
+
270
+ - `spawn()` never throws (no `setupWorktree` call; Agent.run() handles it internally).
271
+ - Background worktree failures surface as `record.error` via `get_subagent_result` and `/agents`.
272
+ - Foreground worktree failures surface when `spawnAndWait` awaits the promise and checks `record.status`.
273
+ - The try/catch in `background-spawner.ts` around `manager.spawn()` becomes unreachable for worktree errors but is retained for future-proofing.
274
+
275
+ ### Type relocation
276
+
277
+ `ParentSessionInfo` and `CompactionInfo` move from `agent-manager.ts` to `types.ts` to break the circular type-import that would otherwise arise (`agent-manager.ts` imports `Agent` from `agent.ts`; `agent.ts` would import `ParentSessionInfo` from `agent-manager.ts`).
278
+
279
+ ### Tool-layer changes
280
+
281
+ `background-spawner.ts` and `foreground-runner.ts` replace `onSessionCreated` callback with `observer` field:
282
+
283
+ ```typescript
284
+ // Before (background-spawner)
285
+ onSessionCreated: (session) => {
286
+ bgState.setSession(session);
287
+ subscribeUIObserver(session, bgState);
288
+ },
289
+
290
+ // After
291
+ observer: {
292
+ onSessionCreated: (_agent, session) => {
293
+ bgState.setSession(session);
294
+ subscribeUIObserver(session, bgState);
295
+ },
296
+ },
297
+ ```
298
+
299
+ The `foreground-runner.ts` `onSessionCreated(session, record)` becomes `observer.onSessionCreated(agent, session)` — the parameters swap order (agent first for observer consistency) and `record` becomes `agent`.
300
+
301
+ ### completeRun / failRun signature change
302
+
303
+ These methods currently accept `worktrees: WorktreeManager` as a parameter (output-argument pattern inherited from the pre-#227 era).
304
+ Agent now owns `worktrees`, so the parameter is removed:
305
+
306
+ ```typescript
307
+ // Before
308
+ completeRun(result: RunResult, worktrees: WorktreeManager): void { ... }
309
+ failRun(err: unknown, worktrees: WorktreeManager): void { ... }
310
+
311
+ // After
312
+ completeRun(result: RunResult): void { ... } // uses this.worktrees internally
313
+ failRun(err: unknown): void { ... } // uses this.worktrees internally
314
+ ```
315
+
316
+ These methods also fire `this.observer?.onRunFinished?.(this)` instead of the old `fireOnRunFinished()` callback.
317
+ The `setOnRunFinished`, `_onRunFinished`, and `fireOnRunFinished` private members are deleted.
318
+
319
+ ### setupWorktree signature change
320
+
321
+ `setupWorktree` currently accepts `(worktrees, isolation)` as parameters.
322
+ It becomes a private no-arg method using `this.worktrees` and `this._isolation` internally.
323
+
324
+ ## Module-Level Changes
325
+
326
+ ### `src/types.ts`
327
+
328
+ - Add `ParentSessionInfo` interface (moved from `agent-manager.ts`).
329
+ - Add `CompactionInfo` type (moved from `agent-manager.ts`).
330
+
331
+ ### `src/lifecycle/agent.ts`
332
+
333
+ - Add `AgentLifecycleObserver` interface export.
334
+ - Expand `AgentInit` with optional run-config and deps fields: `runner`, `worktrees`, `observer`, `getRunConfig`, `snapshot`, `prompt`, `model`, `maxTurns`, `isolated`, `thinkingLevel`, `isolation`, `parentSession`, `isBackground`, `signal`.
335
+ - Remove `abortController` from `AgentInit` — Agent creates its own.
336
+ - Remove `promise` from `AgentInit` — set internally by `run()`.
337
+ - Store new fields as private readonly properties in the constructor.
338
+ - Create `AbortController` in constructor.
339
+ - Create `NotificationState` from `parentSession?.toolCallId` in constructor.
340
+ - Add `async run(): Promise<void>` method — absorbs the full `startAgent` logic.
341
+ - Change `setupWorktree` from public to private, remove parameters (uses own fields).
342
+ - Change `completeRun(result, worktrees)` → `completeRun(result)` (uses own fields).
343
+ - Change `failRun(err, worktrees)` → `failRun(err)` (uses own fields).
344
+ - Delete `setOnRunFinished`, `_onRunFinished`, `fireOnRunFinished` — replaced by observer.
345
+ - Import `subscribeAgentObserver` and `RunResult` types.
346
+ - Import `ParentSessionInfo`, `CompactionInfo` from `types.ts` (not `agent-manager.ts`).
347
+
348
+ ### `src/lifecycle/agent-manager.ts`
349
+
350
+ - Remove `ParentSessionInfo` export (moved to `types.ts`; re-export for backward compat).
351
+ - Remove `CompactionInfo` export (moved to `types.ts`; re-export for backward compat).
352
+ - Delete `startAgent` private method.
353
+ - Delete `SpawnArgs` interface.
354
+ - Rewrite `spawn()` to create complete Agent with all deps and config, call `record.run()`.
355
+ - Remove `record.notification = ...` external write (Agent creates its own).
356
+ - Add private `buildObserver(options)` method to compose per-agent lifecycle observer.
357
+ - Simplify queue from `{ id: string; args: SpawnArgs }[]` to `string[]`.
358
+ - Simplify `drainQueue()` to call `record.run()` (no `setupWorktree`, no `startAgent`, no error catch).
359
+ - Remove `onSessionCreated` from `AgentSpawnConfig`, add `observer?: AgentLifecycleObserver`.
360
+ - Remove `import { subscribeAgentObserver }` (no longer used here).
361
+ - Remove `import { NotificationState }` (no longer used here).
362
+
363
+ ### `src/lifecycle/agent-runner.ts`
364
+
365
+ - Change import of `ParentSessionInfo` from `#src/lifecycle/agent-manager` to `#src/types`.
366
+
367
+ ### `src/observation/record-observer.ts`
368
+
369
+ - Change import of `CompactionInfo` from `#src/lifecycle/agent-manager` to `#src/types`.
370
+
371
+ ### `src/tools/background-spawner.ts`
372
+
373
+ - Change import: remove `AgentSpawnConfig` (if no longer needed) or update as needed.
374
+ - Import `ParentSessionInfo` from `#src/types` (if source changes).
375
+ - Replace `onSessionCreated` callback with `observer` field in spawn call.
376
+ - The `BackgroundManagerDeps.spawn()` signature updates to match new `AgentSpawnConfig`.
377
+
378
+ ### `src/tools/foreground-runner.ts`
379
+
380
+ - Replace `onSessionCreated` callback with `observer` field.
381
+ - Update parameter destructuring: `(session, record)` → `(agent, session)`.
382
+ - Import `ParentSessionInfo` from `#src/types`.
383
+ - The `ForegroundManagerDeps.spawnAndWait()` signature updates to match new config.
384
+
385
+ ### `src/tools/agent-tool.ts`
386
+
387
+ - Import `ParentSessionInfo` from `#src/types` instead of `#src/lifecycle/agent-manager`.
388
+ - Update `AgentToolManager` interface if `AgentSpawnConfig` shape changed.
389
+
390
+ ### `src/service/service-adapter.ts`
391
+
392
+ - No significant changes — `AgentManagerLike.spawn()` already accepts `unknown` options.
393
+
394
+ ### `test/lifecycle/agent.test.ts`
395
+
396
+ - Add test suite for `Agent.run()` covering: full lifecycle, worktree setup, session creation, observer notifications, error handling, abort signal wiring.
397
+ - Update existing tests that use `completeRun(result, worktrees)` → `completeRun(result)`.
398
+ - Update existing tests that use `failRun(err, worktrees)` → `failRun(err)`.
399
+ - Update existing tests that use `setupWorktree(worktrees, isolation)` → remove (now private).
400
+ - Remove tests for `setOnRunFinished` / `fireOnRunFinished`.
401
+ - Update `AgentInit` usages that pass `abortController` — Agent creates its own.
402
+
403
+ ### `test/lifecycle/agent-manager.test.ts`
404
+
405
+ - Remove/rewrite `onSessionCreated callback receives record` describe block — callback is deleted.
406
+ - Remove/rewrite `toolCallId notification wiring` tests — notification is now Agent-internal.
407
+ - Update `execution state` tests — execution is set internally by Agent.run(), not by the manager's `onSessionCreated`.
408
+ - Update mock runner stubs: runner mock should still fire `onSessionCreated` (Agent passes it to the runner).
409
+ - Update tests that inspect `runner.run()` call args for the `onSessionCreated` field.
410
+ - Update queue-related tests to reflect simplified queue (IDs only).
411
+
412
+ ### `test/helpers/manager-stubs.ts`
413
+
414
+ - Update `createSessionRunner` — it fires `opts.onSessionCreated` which is now called by Agent.run() internally; the mock pattern stays similar but the caller changes.
415
+
416
+ ### `test/tools/background-spawner.test.ts`
417
+
418
+ - Update spawn call to use `observer` instead of `onSessionCreated`.
419
+ - Update assertions that inspect the spawn options for `onSessionCreated`.
420
+
421
+ ### `test/tools/foreground-runner.test.ts`
422
+
423
+ - Update `onSessionCreated` usage to `observer.onSessionCreated`.
424
+ - Update parameter order in callback assertions: `(agent, session)` instead of `(session, record)`.
425
+
426
+ ### `test/helpers/make-agent.ts`
427
+
428
+ - Remove `abortController` from the factory default (if present) — Agent creates its own.
429
+ - No other changes needed — run-config fields are optional.
430
+
431
+ ### `packages/pi-subagents/docs/architecture/architecture.md`
432
+
433
+ - Update Step 4 status from planned to complete.
434
+ - Update the file layout listing for `agent.ts` and `agent-manager.ts` descriptions.
435
+ - Mark `SpawnArgs` and `startAgent` as deleted in the listing.
436
+
437
+ ## Test Impact Analysis
438
+
439
+ 1. **New unit tests enabled by extraction:**
440
+ - `Agent.run()` can be tested in isolation with mock runner and worktrees — no need for the full `AgentManager` + queue + concurrency infrastructure.
441
+ - Lifecycle observer callbacks can be tested directly on a single Agent instance.
442
+ - Worktree error handling in `run()` can be tested without the manager's `spawn()` error-recovery logic.
443
+
444
+ 2. **Existing tests that become redundant:**
445
+ - `AgentManager — onSessionCreated callback receives record` — the callback mechanism is deleted.
446
+ - `AgentManager — toolCallId notification wiring` — Agent now creates its own `NotificationState`; manager-level tests for this become Agent-level tests.
447
+ - Some `agent-manager.test.ts` tests that indirectly test execution/observer wiring can be simplified to verify that `agent.run()` is called (delegation test) rather than re-testing the internal lifecycle.
448
+
449
+ 3. **Existing tests that stay as-is:**
450
+ - Queue/concurrency tests in `agent-manager.test.ts` — these test manager-level scheduling, not run internals.
451
+ - `record-observer.test.ts` — observer logic unchanged; only import path changes.
452
+ - `agent-runner.test.ts` — runner internals unchanged.
453
+ - Status transition tests in `agent.test.ts` — Agent status machine unchanged.
454
+ - `background-spawner.test.ts` / `foreground-runner.test.ts` — updated for observer pattern but same behavioral coverage.
455
+
456
+ ## TDD Order
457
+
458
+ 1. **Move `ParentSessionInfo` and `CompactionInfo` to `types.ts`.**
459
+ Add both to `types.ts`.
460
+ Re-export from `agent-manager.ts` for backward compat.
461
+ Update `agent-runner.ts` and `record-observer.ts` imports to use `types.ts`.
462
+ Verify all existing tests pass.
463
+ Commit: `refactor: move ParentSessionInfo and CompactionInfo to types.ts (#229)`
464
+
465
+ 2. **Add `AgentLifecycleObserver` interface to `agent.ts`.**
466
+ Define and export the interface with optional `onStarted`, `onSessionCreated`, `onRunFinished`, `onCompacted` methods.
467
+ No behavioral changes — just the type definition.
468
+ Commit: `feat: add AgentLifecycleObserver interface (#229)`
469
+
470
+ 3. **Expand `AgentInit` with run-config and deps fields; Agent stores them.**
471
+ Add optional fields to `AgentInit`: `runner`, `worktrees`, `observer`, `getRunConfig`, `snapshot`, `prompt`, `model`, `maxTurns`, `isolated`, `thinkingLevel`, `isolation`, `parentSession`, `isBackground`, `signal`.
472
+ Agent stores them as private properties in the constructor.
473
+ Agent creates its own `AbortController` in the constructor.
474
+ Agent creates `NotificationState` from `parentSession?.toolCallId` in the constructor.
475
+ Remove `abortController` from `AgentInit` (Agent creates its own).
476
+ Update `createTestAgent` helper and `agent.test.ts` to remove `abortController` from init; tests that need abort can use `record.abortController` directly.
477
+ Write tests: constructor stores deps and run config; constructor creates AbortController; constructor creates NotificationState when toolCallId present; constructor does not create NotificationState when toolCallId absent.
478
+ Commit: `feat: expand AgentInit with run-config, deps, and self-created AbortController (#229)`
479
+
480
+ 4. **Change `setupWorktree` to private, remove parameters.**
481
+ Make `setupWorktree` a private method that uses `this.worktrees` and `this._isolation`.
482
+ Update `AgentManager.spawn()` and `drainQueue()` call sites: `record.setupWorktree(this.worktrees, options.isolation)` → remove (will be called by `run()`).
483
+ Temporarily call `setupWorktree` from the manager's `spawn` path (keep the old call pattern working until `run()` is added in step 6).
484
+ Update `agent.test.ts` tests that called `setupWorktree` directly — convert to testing via `run()` or remove if redundant.
485
+ Commit: `refactor: make setupWorktree private, remove parameters (#229)`
486
+
487
+ 5. **Change `completeRun`/`failRun` to use own `worktrees`; replace `fireOnRunFinished` with observer.**
488
+ Remove `worktrees` parameter from `completeRun` and `failRun`.
489
+ Replace `fireOnRunFinished()` with `this.observer?.onRunFinished?.(this)`.
490
+ Delete `setOnRunFinished`, `_onRunFinished`, `fireOnRunFinished`.
491
+ Update `agent-manager.ts`: stop calling `record.setOnRunFinished(...)`, stop passing `this.worktrees` to `completeRun`/`failRun`.
492
+ Update all `agent.test.ts` tests that call `completeRun(result, worktrees)` and `failRun(err, worktrees)`.
493
+ Update `agent-manager.test.ts` tests that verify `setOnRunFinished` or concurrency drain — drain now works via the observer's `onRunFinished`.
494
+ Write test: `completeRun` calls `observer.onRunFinished`; `failRun` calls `observer.onRunFinished`.
495
+ Commit: `refactor: remove worktrees param from completeRun/failRun, replace fireOnRunFinished with observer (#229)`
496
+
497
+ 6. **Add `Agent.run()` method — absorbs `startAgent` logic.**
498
+ Implement `Agent.run()` following the design sketch above.
499
+ Write tests for `Agent.run()`:
500
+ - Happy path: run completes, status transitions, observer callbacks fire in order.
501
+ - Session creation: execution state set, steers flushed, record-observer attached.
502
+ - Worktree setup and cleanup on success.
503
+ - Worktree setup failure: markError + observer.onRunFinished.
504
+ - Runner error: failRun + observer.onRunFinished.
505
+ - Abort signal forwarding.
506
+ - RunConfig threading (defaultMaxTurns, graceTurns).
507
+ Commit: `feat: add Agent.run() encapsulating full execution lifecycle (#229)`
508
+
509
+ 7. **Rewrite `AgentManager.spawn()` to create complete Agent and call `agent.run()`.**
510
+ Delete `startAgent`.
511
+ Delete `SpawnArgs`.
512
+ Add `buildObserver` private method.
513
+ Rewrite `spawn()` to construct Agent with all fields, call `record.run()`.
514
+ Remove `record.notification = ...` external write.
515
+ Remove `subscribeAgentObserver` and `NotificationState` imports.
516
+ Simplify queue from `{ id: string; args: SpawnArgs }[]` to `string[]`.
517
+ Simplify `drainQueue()` to call `record.run()`.
518
+ Replace `onSessionCreated` with `observer` on `AgentSpawnConfig`.
519
+ Update `agent-manager.test.ts`:
520
+ - Remove `onSessionCreated callback receives record` tests.
521
+ - Update notification wiring tests (now Agent-internal).
522
+ - Update mock runners if needed.
523
+ - Verify queue/concurrency tests still pass.
524
+ Commit: `feat!: AgentManager.spawn() creates complete Agent, deletes startAgent (#229)`
525
+
526
+ 8. **Update tool-layer consumers: `background-spawner.ts`, `foreground-runner.ts`, `agent-tool.ts`.**
527
+ Replace `onSessionCreated` callback with `observer` field in both spawner and runner.
528
+ Update `foreground-runner.ts` callback: `(session, record)` → `(agent, session)`.
529
+ Update `agent-tool.ts` imports: `ParentSessionInfo` from `#src/types`.
530
+ Update narrow manager interfaces (`BackgroundManagerDeps`, `ForegroundManagerDeps`, `AgentToolManager`) to reflect new `AgentSpawnConfig` shape.
531
+ Update `background-spawner.test.ts` and `foreground-runner.test.ts`.
532
+ Commit: `refactor: update tool layer to use lifecycle observer instead of onSessionCreated (#229)`
533
+
534
+ 9. **Remove backward-compat re-exports and update architecture docs.**
535
+ Remove `ParentSessionInfo` and `CompactionInfo` re-exports from `agent-manager.ts` (if no external consumer depends on the old location).
536
+ Update `docs/architecture/architecture.md`: mark Step 4 complete, update file listings.
537
+ Commit: `docs: mark Phase 15 Step 4 complete, update architecture (#229)`
538
+
539
+ ## Risks and Mitigations
540
+
541
+ 1. **Large test surface.**
542
+ `agent.test.ts` (684 lines) and `agent-manager.test.ts` (815 lines) both need significant updates.
543
+ Mitigation: Lift-and-shift approach — new fields are optional on `AgentInit`, so most existing tests compile unchanged.
544
+ Only tests that touch `completeRun`, `failRun`, `setupWorktree`, `setOnRunFinished`, `abortController`, or `onSessionCreated` need updating.
545
+
546
+ 2. **Breaking change for `AgentSpawnConfig` consumers.**
547
+ `onSessionCreated` is deleted.
548
+ The `service-adapter.ts` and cross-extension consumers that pass `AgentSpawnConfig` may need updates.
549
+ Mitigation: `service-adapter.ts` passes `unknown` for options — no compile error.
550
+ The `observer` field is optional, so consumers that don't use `onSessionCreated` are unaffected.
551
+
552
+ 3. **Async worktree error surface changes tool-layer behavior.**
553
+ `background-spawner.ts` catch block around `manager.spawn()` becomes unreachable for worktree errors.
554
+ Mitigation: Keep the catch block for other potential errors; the agent's error status surfaces the worktree failure message to users.
555
+
556
+ 4. **`AgentInit` grows wide (15+ optional fields).**
557
+ Mitigation: Fields are optional with sensible defaults.
558
+ Follow-up #230 may restructure Agent's constructor (ConcurrencyQueue changes may motivate nesting).
559
+ Tracked as a known smell to revisit.
560
+
561
+ ## Open Questions
562
+
563
+ 1. Should the backward-compat re-exports of `ParentSessionInfo`/`CompactionInfo` from `agent-manager.ts` stay permanently, or be removed in step 9?
564
+ Decision: remove in step 9 if grep confirms no external consumers.