@gotgenes/pi-subagents 6.0.0 → 6.1.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.
@@ -0,0 +1,435 @@
1
+ ---
2
+ issue: 98
3
+ issue_title: "Extract AgentRecord state machine from scattered status transitions"
4
+ ---
5
+
6
+ # Extract AgentRecord state machine
7
+
8
+ ## Problem Statement
9
+
10
+ `AgentRecord` status transitions are scattered across 6 locations in `agent-manager.ts`: `startAgent()` `.then()`, `startAgent()` `.catch()`, `resume()`, `abort()`, `abortAll()`, and `drainQueue()` catch.
11
+ Each site sets `record.status` plus associated fields (`completedAt`, `result`, `error`) in ad-hoc combinations, with repeated guards like `if (record.status !== "stopped")`.
12
+
13
+ There are 15 direct `record.status = ...` writes and 17 associated field writes (`result`, `error`, `completedAt`, `startedAt`) — ~32 scattered mutation sites total.
14
+ `startAgent()` is ~130 lines partly because it manages these transitions inline.
15
+
16
+ ## Goals
17
+
18
+ - Convert `AgentRecord` from a plain interface to a class that owns and encapsulates its transition state.
19
+ - Extract status-transition methods that centralize the "don't overwrite stopped" guard.
20
+ - Reduce scattered field writes in `agent-manager.ts` to method calls.
21
+ - Encapsulate transition fields (`status`, `result`, `error`, `startedAt`, `completedAt`) behind private backing fields with getters — external code can read but not write.
22
+ - Use lift-and-shift to migrate incrementally: introduce the class alongside the interface, migrate construction sites, switch the re-export, replace writes, then encapsulate.
23
+
24
+ ## Non-Goals
25
+
26
+ - Parent snapshot extraction (architecture.md Step 2) — separate issue.
27
+ - Session-event observation / callback threading removal (architecture.md Step 3) — separate issue.
28
+ - Stat accumulation methods (`toolUses++`, `addUsage()`, `compactionCount++`) — these are running counters, not status transitions.
29
+ They could become methods in a follow-up but are out of scope here.
30
+ - Encapsulating non-transition field writes (`session`, `outputFile`, `worktree`, `worktreeResult`, `promise`, `toolCallId`, `resultConsumed`, `pendingSteers`) — these are data capture, not status transitions.
31
+
32
+ ## Prerequisites
33
+
34
+ - Issue #102 (shared test record factory) — shipped.
35
+ All 8 test files construct `AgentRecord` objects through `createTestRecord()` in `test/helpers/make-record.ts`.
36
+ Converting the interface to a class requires updating only this one factory (plus `agent-manager.ts` construction).
37
+
38
+ ## Background
39
+
40
+ ### Relevant modules
41
+
42
+ | Module | Role |
43
+ | ----------------------------- | ------------------------------------------------------------------------------ |
44
+ | `src/types.ts` | Defines `AgentRecord` interface — 20+ consumers read from it |
45
+ | `src/agent-manager.ts` | Only file that writes status-transition fields |
46
+ | `src/service.ts` | Defines `SubagentRecord` (serializable snapshot) — unchanged |
47
+ | `src/service-adapter.ts` | Converts `AgentRecord` → `SubagentRecord` via `toSubagentRecord()` — unchanged |
48
+ | `test/helpers/make-record.ts` | Shared test factory — single construction site for all tests (#102) |
49
+
50
+ ### External writes to `AgentRecord` (not status transitions)
51
+
52
+ Several files outside `agent-manager.ts` write non-transition fields:
53
+
54
+ - `tools/get-result-tool.ts` — `record.resultConsumed = true` (2 sites)
55
+ - `tools/steer-tool.ts` / `service-adapter.ts` — `record.pendingSteers` (push messages)
56
+ - `tools/agent-tool.ts` — `record.toolCallId = toolCallId`
57
+
58
+ These are data-capture writes, not status transitions, and remain public fields.
59
+
60
+ ### Code-style constraints
61
+
62
+ The code-style skill's "output arguments" rule applies directly: "If a function sets a field on a received object, it is doing work that belongs inside the owning object.
63
+ Encapsulate the mutation behind a method."
64
+ The "scattered resets" rule also applies: "When the same set of fields is reset to the same values in multiple places, extract a single method."
65
+
66
+ ## Design Overview
67
+
68
+ ### `AgentRecord` becomes a class
69
+
70
+ `AgentRecord` moves from an interface in `types.ts` to a class in a new `src/agent-record.ts` module.
71
+ `types.ts` re-exports the class so existing `import type { AgentRecord } from "./types.js"` across the codebase continues to work with no import-path changes.
72
+
73
+ The class encapsulates the 5 transition fields behind private backing fields with getters.
74
+ All other fields remain public (identity fields are `readonly`; non-transition mutable fields stay public).
75
+
76
+ ```typescript
77
+ export type AgentRecordStatus =
78
+ | "queued" | "running" | "completed" | "steered" | "aborted" | "stopped" | "error";
79
+
80
+ export class AgentRecord {
81
+ // Identity — readonly, set once at construction
82
+ readonly id: string;
83
+ readonly type: SubagentType;
84
+ readonly description: string;
85
+ readonly invocation?: AgentInvocation;
86
+
87
+ // Transition state — private backing fields, public getters
88
+ private _status: AgentRecordStatus;
89
+ get status(): AgentRecordStatus { return this._status; }
90
+
91
+ private _result?: string;
92
+ get result(): string | undefined { return this._result; }
93
+
94
+ private _error?: string;
95
+ get error(): string | undefined { return this._error; }
96
+
97
+ private _startedAt: number;
98
+ get startedAt(): number { return this._startedAt; }
99
+
100
+ private _completedAt?: number;
101
+ get completedAt(): number | undefined { return this._completedAt; }
102
+
103
+ // Non-transition mutable state — public fields
104
+ toolUses: number;
105
+ lifetimeUsage: LifetimeUsage;
106
+ compactionCount: number;
107
+ session?: AgentSession;
108
+ abortController?: AbortController;
109
+ promise?: Promise<string>;
110
+ resultConsumed?: boolean;
111
+ pendingSteers?: string[];
112
+ worktree?: { path: string; branch: string };
113
+ worktreeResult?: { hasChanges: boolean; branch?: string };
114
+ toolCallId?: string;
115
+ outputFile?: string;
116
+
117
+ constructor(init: AgentRecordInit) { /* ... */ }
118
+
119
+ // Transition methods — see below
120
+ }
121
+ ```
122
+
123
+ ### Constructor and `AgentRecordInit`
124
+
125
+ The constructor accepts a wide init bag that covers all fields.
126
+ Required fields: `id`, `type`, `description`.
127
+ All others are optional with sensible defaults (`status` defaults to `"queued"`, `toolUses` to 0, etc.).
128
+
129
+ This wide init bag serves two purposes:
130
+
131
+ 1. Production use (`agent-manager.ts`): passes the 7 fields it needs at spawn time.
132
+ 2. Test use (`createTestRecord`): sets arbitrary state (e.g., `status: "completed"`, `result: "done"`) without going through transition methods.
133
+
134
+ ```typescript
135
+ export interface AgentRecordInit {
136
+ id: string;
137
+ type: SubagentType;
138
+ description: string;
139
+ status?: AgentRecordStatus;
140
+ startedAt?: number;
141
+ completedAt?: number;
142
+ result?: string;
143
+ error?: string;
144
+ toolUses?: number;
145
+ lifetimeUsage?: LifetimeUsage;
146
+ compactionCount?: number;
147
+ abortController?: AbortController;
148
+ invocation?: AgentInvocation;
149
+ session?: AgentSession;
150
+ promise?: Promise<string>;
151
+ resultConsumed?: boolean;
152
+ pendingSteers?: string[];
153
+ worktree?: { path: string; branch: string };
154
+ worktreeResult?: { hasChanges: boolean; branch?: string };
155
+ toolCallId?: string;
156
+ outputFile?: string;
157
+ }
158
+ ```
159
+
160
+ ### Transition methods
161
+
162
+ 7 methods covering all 6 transition sites plus the resume-reset:
163
+
164
+ #### `markRunning(startedAt: number): void`
165
+
166
+ Sets `status = "running"` and `startedAt`.
167
+ Called in `startAgent()` when transitioning from queued to running.
168
+
169
+ #### `markCompleted(result: string, completedAt?: number): void`
170
+
171
+ Always sets `result` and `completedAt` (via `??=`).
172
+ Only changes `status` to `"completed"` if not already `"stopped"`.
173
+ Called in the `.then()` success path and in `resume()` try block.
174
+
175
+ #### `markAborted(result: string, completedAt?: number): void`
176
+
177
+ Same guard as `markCompleted` — preserves stopped status.
178
+ Sets `status = "aborted"`.
179
+ Called in `.then()` when runner returns `aborted: true`.
180
+
181
+ #### `markSteered(result: string, completedAt?: number): void`
182
+
183
+ Same guard as `markCompleted` — preserves stopped status.
184
+ Sets `status = "steered"`.
185
+ Called in `.then()` when runner returns `steered: true`.
186
+
187
+ #### `markError(error: unknown, completedAt?: number): void`
188
+
189
+ Always sets `error` (formatted: `Error` → `.message`, otherwise `String(...)`) and `completedAt` (via `??=`).
190
+ Only changes `status` to `"error"` if not already `"stopped"`.
191
+ Called in `.catch()`, `resume()` catch block, and `drainQueue()` catch.
192
+
193
+ #### `markStopped(completedAt?: number): void`
194
+
195
+ Always sets `status = "stopped"` and `completedAt`.
196
+ No guard — stopping is always valid.
197
+ Called in `abort()` (2 sites) and `abortAll()` (2 sites).
198
+
199
+ #### `resetForResume(startedAt: number): void`
200
+
201
+ Sets `status = "running"`, `startedAt`.
202
+ Clears `completedAt`, `result`, `error` to `undefined`.
203
+ Called in `resume()` before re-running.
204
+
205
+ ### Guard semantics: preserve current behavior
206
+
207
+ The current `.then()` and `.catch()` blocks guard only the `status` field but always set `result`/`error` and `completedAt`:
208
+
209
+ ```typescript
210
+ // Current .then() — result and completedAt are set even when stopped
211
+ if (record.status !== "stopped") {
212
+ record.status = aborted ? "aborted" : steered ? "steered" : "completed";
213
+ }
214
+ record.result = responseText;
215
+ record.completedAt ??= Date.now();
216
+ ```
217
+
218
+ This is intentional — `get-result-tool.ts` reads `record.result` from stopped records.
219
+ The transition methods preserve this behavior: data fields are always set, status is only changed when not stopped.
220
+ The `??=` on `completedAt` preserves the abort timestamp when `markStopped()` fires before `.then()`.
221
+
222
+ ### Worktree result-append: reorder, don't add a method
223
+
224
+ The `.then()` block currently appends worktree branch text to `record.result` after the transition.
225
+ With encapsulated fields, direct writes to `result` are forbidden.
226
+
227
+ Instead of adding an `appendToResult()` method, reorder the `.then()` block to compute the final result (including worktree text) before calling the transition method:
228
+
229
+ ```typescript
230
+ .then(({ responseText, session, aborted, steered, sessionFile }) => {
231
+ // Worktree cleanup first — compute final result
232
+ let finalResult = responseText;
233
+ if (record.worktree) {
234
+ const wtResult = this.worktrees.cleanup(record.worktree, options.description);
235
+ record.worktreeResult = wtResult;
236
+ if (wtResult.hasChanges && wtResult.branch) {
237
+ finalResult += `\n\n---\nChanges saved to branch ...`;
238
+ }
239
+ }
240
+
241
+ // Transition with complete result
242
+ if (aborted) record.markAborted(finalResult);
243
+ else if (steered) record.markSteered(finalResult);
244
+ else record.markCompleted(finalResult);
245
+
246
+ record.session = session;
247
+ if (sessionFile) record.outputFile = sessionFile;
248
+
249
+ detach();
250
+ // ...
251
+ })
252
+ ```
253
+
254
+ The worktree cleanup reads `record.worktree` (a public field set before the promise) and writes `record.worktreeResult` (a public field).
255
+ It does not depend on `record.status` or `record.result`, so the reorder is safe.
256
+
257
+ ### Correctness improvement in `resume()`
258
+
259
+ The current `resume()` method lacks the "don't overwrite stopped" guard:
260
+
261
+ ```typescript
262
+ // Current — no guard against abort race
263
+ record.status = "completed";
264
+ record.result = responseText;
265
+ record.completedAt = Date.now();
266
+ ```
267
+
268
+ After migration, `record.markCompleted(responseText)` includes the guard.
269
+ This closes a latent race where `abort()` sets status to `"stopped"` between `resetForResume()` and the runner returning.
270
+ The current code would overwrite `"stopped"` back to `"completed"` — the method call does not.
271
+
272
+ ### Circular import avoidance
273
+
274
+ `agent-record.ts` imports types from `types.ts` (`SubagentType`, `AgentInvocation`).
275
+ `types.ts` re-exports the class from `agent-record.ts`.
276
+
277
+ This is safe because `agent-record.ts` uses `import type` for its `types.ts` imports — these are erased at runtime, so no runtime circular dependency exists.
278
+
279
+ ## Module-Level Changes
280
+
281
+ ### New files
282
+
283
+ 1. `src/agent-record.ts` — `AgentRecord` class, `AgentRecordInit` interface, `AgentRecordStatus` type.
284
+ 2. `test/agent-record.test.ts` — unit tests for constructor and all 7 transition methods.
285
+
286
+ ### Changed files
287
+
288
+ 1. `src/types.ts` — remove `AgentRecord` interface; add re-export: `export { AgentRecord, type AgentRecordInit, type AgentRecordStatus } from "./agent-record.js"`.
289
+ 2. `src/agent-manager.ts` — import `AgentRecord` as a value from `./agent-record.js`; replace object literal construction with `new AgentRecord(...)`; replace ~32 scattered field writes with 11 transition method calls.
290
+ 3. `test/helpers/make-record.ts` — import `AgentRecord` from `../../src/agent-record.js`; construct with `new AgentRecord(...)` instead of plain object.
291
+
292
+ ### Unchanged files
293
+
294
+ All other source and test files — they use `import type { AgentRecord } from "./types.js"` and only read fields.
295
+ The re-export from `types.ts` means zero import-path changes.
296
+
297
+ ## Test Impact Analysis
298
+
299
+ ### New tests enabled by the extraction
300
+
301
+ The state machine is currently untestable in isolation — transitions are buried inside `agent-manager.ts`.
302
+ After extraction:
303
+
304
+ - Guard logic tested directly (e.g., `markCompleted` on a stopped record preserves status but sets result).
305
+ - Invalid transition sequences tested without mocking the runner.
306
+ - Error formatting (`Error` vs string) tested in isolation.
307
+ - `resetForResume` field clearing tested without the full resume flow.
308
+
309
+ ### Existing tests that stay as-is
310
+
311
+ `agent-manager.test.ts` tests verify end-to-end behavior (spawn, complete, abort, resume, queue drain, worktree).
312
+ They remain unchanged — they verify the wiring between `AgentManager` and the `AgentRecord` class.
313
+
314
+ ### No test files need updating (except the shared factory)
315
+
316
+ Issue #102 consolidated all test record construction into `createTestRecord()`.
317
+ Only that one factory changes.
318
+ All 8 consumer test files are untouched.
319
+
320
+ ## TDD Order
321
+
322
+ ### Cycle 1: AgentRecord class tests
323
+
324
+ Test surface: `test/agent-record.test.ts` (new file).
325
+
326
+ Tests cover:
327
+
328
+ - Constructor: defaults (`status` → `"queued"`, `toolUses` → 0, `lifetimeUsage` → zeros, `compactionCount` → 0), passthrough of init values (including optional transition fields like `result`, `completedAt`).
329
+ - `markRunning`: sets status and startedAt.
330
+ - `markCompleted`: sets status/result/completedAt when not stopped; preserves status but still sets result/completedAt when stopped; `completedAt` uses `??=` semantics (does not overwrite existing value).
331
+ - `markAborted` and `markSteered`: same guard behavior as `markCompleted`.
332
+ - `markError`: sets status/error/completedAt when not stopped; preserves status but still sets error/completedAt when stopped; formats `Error` objects to `.message`, non-Error to `String(...)`.
333
+ - `markStopped`: always sets status and completedAt, no guard.
334
+ - `resetForResume`: sets status to `"running"` and startedAt; clears completedAt, result, error.
335
+
336
+ Commit: `test: add AgentRecord class tests (#98)`
337
+
338
+ ### Cycle 2: AgentRecord class implementation
339
+
340
+ Source: `src/agent-record.ts` (new file).
341
+ Exports: `AgentRecord` class, `AgentRecordInit` interface, `AgentRecordStatus` type.
342
+ All fields public initially (encapsulation deferred to cycle 6).
343
+
344
+ All cycle 1 tests pass.
345
+
346
+ Commit: `feat: create AgentRecord class with transition methods (#98)`
347
+
348
+ ### Cycle 3: Lift — migrate construction sites
349
+
350
+ Changed files: `test/helpers/make-record.ts`, `src/agent-manager.ts`.
351
+
352
+ `createTestRecord`: import `AgentRecord` from `agent-record.ts`; construct with `new AgentRecord(...)`.
353
+ `agent-manager.ts`: import `AgentRecord` from `agent-record.ts` (value import); replace object literal in `spawn()` with `new AgentRecord(...)`.
354
+
355
+ At this point, both the interface (in `types.ts`) and the class (in `agent-record.ts`) coexist.
356
+ `agent-manager.ts` uses the class; all other consumers use the interface.
357
+ Scattered field writes still work because fields are public.
358
+
359
+ Run: full test suite + `pnpm run check`.
360
+
361
+ Commit: `refactor: construct AgentRecord class in agent-manager and test factory (#98)`
362
+
363
+ ### Cycle 4: Shift — switch `types.ts` from interface to re-export
364
+
365
+ Changed file: `src/types.ts`.
366
+
367
+ Remove the `AgentRecord` interface definition.
368
+ Add re-exports:
369
+
370
+ ```typescript
371
+ export { AgentRecord } from "./agent-record.js";
372
+ export type { AgentRecordInit, AgentRecordStatus } from "./agent-record.js";
373
+ ```
374
+
375
+ All `import type { AgentRecord } from "./types.js"` across the codebase now resolve to the class.
376
+ No consumer constructs `AgentRecord` as a plain object (construction was migrated in cycle 3).
377
+
378
+ Run: `pnpm run check` (catches any structural incompatibilities).
379
+
380
+ Commit: `refactor: replace AgentRecord interface with class re-export in types.ts (#98)`
381
+
382
+ ### Cycle 5: Replace scattered field writes with transition methods
383
+
384
+ Changed file: `src/agent-manager.ts`.
385
+
386
+ Replace all 11 transition sites:
387
+
388
+ | Location | Before | After |
389
+ | ---------------------- | ------------------------------------------------------------------ | ------------------------------------------------------------------- |
390
+ | `startAgent()` running | `record.status = "running"; record.startedAt = ...` | `record.markRunning(Date.now())` |
391
+ | `.then()` success | `if (!stopped) status = ...; result = ...; completedAt ??= ...` | `record.markCompleted(finalResult)` / `markAborted` / `markSteered` |
392
+ | `.catch()` error | `if (!stopped) status = "error"; error = ...; completedAt ??= ...` | `record.markError(err)` |
393
+ | `resume()` reset | `status = ...; startedAt = ...; clear 3 fields` | `record.resetForResume(Date.now())` |
394
+ | `resume()` try | `status = "completed"; result = ...; completedAt = ...` | `record.markCompleted(responseText)` |
395
+ | `resume()` catch | `status = "error"; error = ...; completedAt = ...` | `record.markError(err)` |
396
+ | `abort()` queued | `status = "stopped"; completedAt = ...` | `record.markStopped()` |
397
+ | `abort()` running | `status = "stopped"; completedAt = ...` | `record.markStopped()` |
398
+ | `abortAll()` queued | `status = "stopped"; completedAt = ...` | `record.markStopped()` |
399
+ | `abortAll()` running | `status = "stopped"; completedAt = ...` | `record.markStopped()` |
400
+ | `drainQueue()` catch | `status = "error"; error = ...; completedAt = ...` | `record.markError(err)` |
401
+
402
+ Also reorder the `.then()` block: worktree cleanup moves before the transition call so the final result includes worktree branch text (see Design Overview).
403
+
404
+ Run: full test suite.
405
+
406
+ Commit: `refactor: replace scattered status transitions with AgentRecord methods (#98)`
407
+
408
+ ### Cycle 6: Encapsulate transition fields
409
+
410
+ Changed file: `src/agent-record.ts`.
411
+
412
+ Make `status`, `result`, `error`, `startedAt`, `completedAt` private (`_status`, `_result`, etc.) with public getters.
413
+ Make `id`, `type`, `description`, `invocation` readonly.
414
+
415
+ Run: `pnpm run check` — verifies no remaining direct writes to encapsulated fields anywhere in `src/` or `test/`.
416
+ Run: full test suite.
417
+
418
+ Commit: `refactor: encapsulate AgentRecord transition state (#98)`
419
+
420
+ ## Risks and Mitigations
421
+
422
+ | Risk | Mitigation |
423
+ | ---------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
424
+ | Guard semantics drift: transition methods might not exactly match scattered guards | Each method's guard logic has dedicated tests (cycle 1); full agent-manager test suite runs after each cycle |
425
+ | Data fields on stopped records: `markCompleted` etc. must still set `result`/`completedAt` even when status is guarded | Explicit "stopped guard" test cases verify data fields are set; mirrors current `.then()` / `.catch()` behavior |
426
+ | Worktree result-append after encapsulation: direct `record.result = ...` no longer compiles | `.then()` block reordered to compute full result before calling transition method (cycle 5) |
427
+ | `types.ts` re-export introduces type-only circular dependency | `agent-record.ts` → `types.ts` imports are `import type` only (erased at runtime); no runtime cycle |
428
+ | `resume()` gains a "stopped" guard it didn't have before | This is a correctness improvement: closes a latent abort race; noted explicitly in design |
429
+
430
+ ## Open Questions
431
+
432
+ - The `markCompleted`/`markAborted`/`markSteered` split is not in the original issue's 5-method list (which only shows `markCompleted`).
433
+ The split is needed because the `.then()` block dispatches to 3 distinct terminal statuses.
434
+ - Stat accumulation (`toolUses++`, `addUsage(lifetimeUsage, ...)`, `compactionCount++`) is left as direct field writes on public fields.
435
+ These could become methods in a follow-up if the callback-threading removal (architecture.md Step 3) makes it natural.
@@ -0,0 +1,176 @@
1
+ ---
2
+ issue: 102
3
+ issue_title: "Consolidate test AgentRecord construction into a shared factory"
4
+ ---
5
+
6
+ # Consolidate test AgentRecord construction into a shared factory
7
+
8
+ ## Problem Statement
9
+
10
+ Eight test files independently construct `AgentRecord` objects using three different patterns: copy-pasted `makeRecord()`/`mockRecord()` factory functions (5 files), inline `const baseRecord: AgentRecord = { ... }` literals (2 files), and `as AgentRecord` casts.
11
+ When issue #98 converts `AgentRecord` from an interface to a class, every object-literal construction site breaks.
12
+ A shared factory confines that future breakage to a single file.
13
+
14
+ ## Goals
15
+
16
+ - Create a shared `createTestRecord()` factory in `test/helpers/make-record.ts`.
17
+ - Migrate all 7 affected test files to import the shared factory.
18
+ - No production code changes.
19
+ - No behavior changes — purely mechanical.
20
+
21
+ ## Non-Goals
22
+
23
+ - Converting `AgentRecord` to a class — that is issue #98, which depends on this change.
24
+ - Adding new test coverage — this is a refactoring of test infrastructure only.
25
+ - Touching `test/agent-manager.test.ts` — it constructs records via `manager.spawn()`, not literals.
26
+ - Consolidating other test helpers (mock sessions, mock TUI, etc.).
27
+
28
+ ## Background
29
+
30
+ ### Relevant modules
31
+
32
+ | Module | Role |
33
+ | ------------------------------------ | -------------------------------------------------------------------------------------------------------------- |
34
+ | `src/types.ts` | Defines the `AgentRecord` interface (20+ fields) |
35
+ | `test/tools/agent-tool.test.ts` | `makeRecord()` factory — 12 default fields, status "completed" |
36
+ | `test/tools/get-result-tool.test.ts` | `makeRecord()` factory — 10 default fields, status "completed" |
37
+ | `test/tools/steer-tool.test.ts` | `makeRecord()` factory — 9 default fields, status "running", includes mock session, uses `as AgentRecord` cast |
38
+ | `test/ui/agent-menu.test.ts` | `makeRecord()` factory — 10 default fields, status "completed" |
39
+ | `test/conversation-viewer.test.ts` | `mockRecord()` factory — 6 default fields, status "running", uses `as AgentRecord` cast |
40
+ | `test/notification.test.ts` | 4 inline `baseRecord` literals, status "completed" |
41
+ | `test/service-adapter.test.ts` | 4 inline `baseRecord` / `minimal` literals, mixed statuses |
42
+
43
+ ### Convention from sibling packages
44
+
45
+ `packages/pi-autoformat/test/helpers/rpc.ts` is the only existing shared test helper in the monorepo.
46
+ The pattern is a plain module under `test/helpers/` with named exports — no class, no framework.
47
+
48
+ ### Relationship to issue #98
49
+
50
+ Issue #98 plans to extract `MutableAgentRecord` as a class implementing the `AgentRecord` interface.
51
+ That plan explicitly notes: "All test files that construct `AgentRecord` literals — they create interface-compatible objects, not class instances" and lists them as unchanged.
52
+ Once this consolidation lands, issue #98's "unchanged" assumption becomes trivially true: only the shared factory needs updating if the construction API changes.
53
+
54
+ ## Design Overview
55
+
56
+ ### Shared factory: `createTestRecord()`
57
+
58
+ A single function in `test/helpers/make-record.ts` with the `Partial<AgentRecord>` override pattern already used by 5 of the 7 files:
59
+
60
+ ```typescript
61
+ import type { AgentRecord } from "../../src/types.js";
62
+
63
+ export function createTestRecord(
64
+ overrides: Partial<AgentRecord> = {},
65
+ ): AgentRecord {
66
+ return {
67
+ id: "agent-1",
68
+ type: "general-purpose",
69
+ description: "Test task",
70
+ status: "completed",
71
+ result: "All done.",
72
+ toolUses: 3,
73
+ startedAt: 1000,
74
+ completedAt: 2000,
75
+ compactionCount: 0,
76
+ lifetimeUsage: { input: 500, output: 500, cacheWrite: 0 },
77
+ ...overrides,
78
+ };
79
+ }
80
+ ```
81
+
82
+ ### Default-value decisions
83
+
84
+ The defaults match the majority pattern (6 of 7 files default to a "completed" record).
85
+ The two files that need "running" records (`steer-tool`, `conversation-viewer`) pass `{ status: "running" }` as overrides — a one-field change.
86
+
87
+ The `as AgentRecord` cast used by `steer-tool.test.ts` and `conversation-viewer.test.ts` is no longer needed: the shared factory returns a full `AgentRecord` with all required fields populated, so TypeScript is satisfied without casting.
88
+
89
+ ### Migration strategy for inline-literal files
90
+
91
+ `notification.test.ts` and `service-adapter.test.ts` construct multiple distinct inline literals — they don't have a single factory.
92
+ Each inline literal becomes a `createTestRecord({ ...specific overrides })` call.
93
+ The `baseRecord` variable declared in each `describe` block is replaced with a call to `createTestRecord()`.
94
+
95
+ For `service-adapter.test.ts`, the top-level `baseRecord` with custom values (`id: "abc-123"`, `type: "Explore"`, etc.) becomes `createTestRecord({ id: "abc-123", type: "Explore", ... })`.
96
+
97
+ ## Module-Level Changes
98
+
99
+ ### New files
100
+
101
+ 1. `test/helpers/make-record.ts` — exports `createTestRecord()`.
102
+
103
+ ### Changed files
104
+
105
+ 1. `test/tools/agent-tool.test.ts` — remove local `makeRecord()`, import `createTestRecord` from helpers.
106
+ 2. `test/tools/get-result-tool.test.ts` — remove local `makeRecord()`, import `createTestRecord` from helpers.
107
+ 3. `test/tools/steer-tool.test.ts` — remove local `makeRecord()`, import `createTestRecord` from helpers.
108
+ Replace default `status: "running"` and `session` with overrides in each call site.
109
+ 4. `test/ui/agent-menu.test.ts` — remove local `makeRecord()`, import `createTestRecord` from helpers.
110
+ 5. `test/conversation-viewer.test.ts` — remove local `mockRecord()`, import `createTestRecord` from helpers.
111
+ Replace default `status: "running"` and `startedAt: Date.now()` with overrides in each call site.
112
+ 6. `test/notification.test.ts` — replace 4 inline `baseRecord` literals with `createTestRecord()` calls.
113
+ 7. `test/service-adapter.test.ts` — replace inline `baseRecord` / `minimal` / per-test literals with `createTestRecord()` calls.
114
+
115
+ ### Unchanged files
116
+
117
+ 1. `test/agent-manager.test.ts` — constructs records via `manager.spawn()`, not literals.
118
+ 2. All production source files — no changes.
119
+
120
+ ## Test Impact Analysis
121
+
122
+ ### New tests enabled
123
+
124
+ 1. A small sanity test in `test/helpers/make-record.test.ts` verifying that `createTestRecord()` returns a valid `AgentRecord` with expected defaults and that overrides are applied.
125
+ This is optional — the factory is exercised transitively by every consumer — but it documents the contract for future maintainers (especially when #98 changes construction).
126
+
127
+ ### Existing tests that become redundant
128
+
129
+ None.
130
+ This is a pure refactoring of test infrastructure; no production behavior changes.
131
+
132
+ ### Existing tests that stay as-is
133
+
134
+ All existing test assertions stay unchanged.
135
+ Only the construction of `AgentRecord` objects in test setup code changes; the assertions that read those records are untouched.
136
+
137
+ ## TDD Order
138
+
139
+ 1. **Create shared factory and its test.**
140
+ Add `test/helpers/make-record.ts` with `createTestRecord()`.
141
+ Add `test/helpers/make-record.test.ts` verifying defaults and override behavior.
142
+ Commit: `test: add shared createTestRecord factory (#102)`
143
+
144
+ 2. **Migrate tool test files.**
145
+ Update `agent-tool.test.ts`, `get-result-tool.test.ts`, `steer-tool.test.ts` to import `createTestRecord` and remove local `makeRecord()` functions.
146
+ Run `pnpm vitest run test/tools/agent-tool.test.ts test/tools/get-result-tool.test.ts test/tools/steer-tool.test.ts` to verify.
147
+ Commit: `test: migrate tool tests to shared createTestRecord (#102)`
148
+
149
+ 3. **Migrate UI test files.**
150
+ Update `agent-menu.test.ts` and `conversation-viewer.test.ts` to import `createTestRecord` and remove local `makeRecord()`/`mockRecord()` functions.
151
+ Run `pnpm vitest run test/ui/agent-menu.test.ts test/conversation-viewer.test.ts` to verify.
152
+ Commit: `test: migrate UI tests to shared createTestRecord (#102)`
153
+
154
+ 4. **Migrate notification and service-adapter tests.**
155
+ Update `notification.test.ts` and `service-adapter.test.ts` to replace inline literals with `createTestRecord()` calls.
156
+ Run `pnpm vitest run test/notification.test.ts test/service-adapter.test.ts` to verify.
157
+ Commit: `test: migrate notification and service-adapter tests to shared createTestRecord (#102)`
158
+
159
+ 5. **Final verification.**
160
+ Run full test suite (`pnpm vitest run`) and type check (`pnpm run check`) to confirm no regressions.
161
+ Commit: not needed if steps 2–4 are clean; otherwise a fix-up commit.
162
+
163
+ ## Risks and Mitigations
164
+
165
+ | Risk | Mitigation |
166
+ | ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
167
+ | Shared defaults don't match a test's assumptions, causing silent false-passes | Each migration step runs the affected test file immediately; review each test's overrides to ensure they still express the test's intent |
168
+ | `steer-tool.test.ts` relies on `session: { fake: true }` in its factory default, which the shared factory omits | Pass `session` as an override at each call site; the mock session is test-specific and doesn't belong in shared defaults |
169
+ | `conversation-viewer.test.ts` uses `startedAt: Date.now()` which the shared factory replaces with `1000` | Replace with `createTestRecord({ status: "running" })`; `startedAt` value is not asserted in any conversation-viewer test |
170
+ | `service-adapter.test.ts` uses custom `id`, `type`, `description` values that carry semantic meaning in its assertions | Pass those values explicitly as overrides to `createTestRecord()` |
171
+ | The `as AgentRecord` cast removal changes type-checking strictness | The shared factory returns a complete object satisfying all required fields, so removing the cast is strictly safer |
172
+
173
+ ## Open Questions
174
+
175
+ - The factory name `createTestRecord` vs `makeRecord` vs `makeAgentRecord`: the plan uses `createTestRecord` to distinguish it from the production `AgentRecord` constructor that #98 will introduce.
176
+ If #98 names its constructor differently, this can be revisited.
@@ -0,0 +1,41 @@
1
+ ---
2
+ issue: 61
3
+ issue_title: "feat: port subagent transcript logging to Pi's official JSONL session format"
4
+ ---
5
+
6
+ # Retro: #61 — port subagent transcript logging to Pi's official JSONL session format
7
+
8
+ ## Final Retrospective (2026-05-20T17:15:00Z)
9
+
10
+ ### Session summary
11
+
12
+ Planned, implemented, and shipped a migration from the bespoke `output-file.ts` transcript format to Pi's official JSONL session format via `SessionManager.create()`.
13
+ The change replaced 143 lines of manual streaming code with 3 lines leveraging the SDK's native persistence, nested subagent sessions under the parent session directory with `parentSession` header linking.
14
+ Released as `pi-subagents-v6.0.0` (major version bump due to breaking transcript format change).
15
+
16
+ ### Observations
17
+
18
+ #### What went well
19
+
20
+ - The plan-to-implementation translation was clean: 6 TDD steps mapped to 7 commits (one extra `fix:` for biome lint).
21
+ No steps needed reordering or merging.
22
+ - The `ask_user` design decision gates during planning (persistence strategy, file location) produced clear answers that avoided rework during implementation.
23
+ - Research into nicobailon/pi-subagents, edxeth/pi-subagents, and HazAT/pi-interactive-subagents provided useful reference for the session directory layout, confirming the parent-relative nesting pattern.
24
+ - The biome lint catch on the unused `cwd` parameter led to a better design — incorporating `cwd` into the temp fallback path for project namespacing — rather than a mechanical underscore prefix.
25
+
26
+ #### What caused friction (agent side)
27
+
28
+ - `missing-context` — The plan listed test impact for `agent-runner.test.ts` but didn't grep for other test files mocking `SessionManager` or `ctx.sessionManager`.
29
+ Three additional files needed updating: `agent-runner-extension-tools.test.ts`, `print-mode.test.ts`, and `test/tools/agent-tool.test.ts`.
30
+ The testing skill explicitly says "grep for ALL test files that construct a compatible mock — not just factory helpers."
31
+ Impact: ~5 minutes of reactive fixes during Step 4.
32
+ Self-identified at implementation time.
33
+
34
+ - `missing-context` — The plan didn't account for the timing difference between the old synchronous `record.outputFile` assignment (immediately after `spawn()`) and the new asynchronous availability (after `SessionManager.create()` runs inside `runAgent()`).
35
+ This required adding `session.sessionManager.getSessionFile()` in the `onSessionCreated` callback — a design decision made during implementation.
36
+ Impact: minor within-step rework, no extra commit needed.
37
+
38
+ #### What caused friction (user side)
39
+
40
+ - The dependency update to 0.75.4 was a reasonable pre-plan request, but it added ~10 minutes of tangential work (diagnosing `pnpm update` resolution behavior, normalizing version specifiers).
41
+ This could have been a separate commit/session, though batching it was pragmatic since it gave the plan access to the latest SDK types.