@gotgenes/pi-subagents 5.1.0 → 5.3.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 +37 -0
- package/README.md +176 -133
- package/docs/architecture/architecture.md +148 -92
- package/docs/decisions/0001-deferred-patches.md +11 -5
- package/docs/plans/0048-implement-subagents-api.md +2 -1
- package/docs/plans/0049-remove-group-join-output-file-rpc.md +22 -5
- package/docs/plans/0051-update-adr-0001-hard-fork.md +2 -1
- package/docs/plans/0052-remove-scheduled-subagents.md +4 -2
- package/docs/plans/0057-structured-debug-logging.md +22 -52
- package/docs/plans/0069-create-subagent-runtime.md +345 -0
- package/docs/plans/0071-extract-session-config-assembler.md +362 -0
- package/docs/retro/0049-remove-group-join-output-file-rpc.md +15 -4
- package/docs/retro/0051-update-adr-0001-hard-fork.md +7 -3
- package/docs/retro/0053-extract-model-resolution-from-execute.md +14 -4
- package/docs/retro/0054-decompose-index-into-modules.md +20 -5
- package/docs/retro/0057-structured-debug-logging.md +77 -0
- package/docs/retro/0069-create-subagent-runtime.md +43 -0
- package/package.json +1 -1
- package/src/agent-manager.ts +7 -0
- package/src/agent-runner.ts +51 -189
- package/src/debug.ts +4 -2
- package/src/index.ts +37 -28
- package/src/runtime.ts +62 -0
- package/src/session-config.ts +263 -0
- package/src/tools/agent-tool.ts +4 -2
- package/src/ui/agent-menu.ts +16 -13
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
---
|
|
2
|
+
issue: 69
|
|
3
|
+
issue_title: "refactor: eliminate module-scope mutable state in pi-subagents — create SubagentRuntime"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Create SubagentRuntime
|
|
7
|
+
|
|
8
|
+
## Problem Statement
|
|
9
|
+
|
|
10
|
+
`pi-subagents` still uses pre-refactor patterns that `pi-permission-system` eliminated in #43.
|
|
11
|
+
`agent-runner.ts` holds module-scope mutable `let` variables (`defaultMaxTurns`, `graceTurns`) with getter/setter pairs that are called from `settings.ts` via callback injection.
|
|
12
|
+
`index.ts` holds closure-scoped `let` variables (`currentCtx`, `widget`) and a `Map` (`agentActivity`) that are captured by arrow closures and cannot be tested in isolation.
|
|
13
|
+
Both patterns hide real dependencies behind module-scope and closure-scope state, making isolated testing impossible.
|
|
14
|
+
|
|
15
|
+
## Goals
|
|
16
|
+
|
|
17
|
+
- Introduce a `SubagentRuntime` interface and `createSubagentRuntime()` factory in a new `src/runtime.ts`.
|
|
18
|
+
- Move `defaultMaxTurns`, `graceTurns`, `agentActivity`, `currentCtx`, and the widget reference into the runtime.
|
|
19
|
+
- Thread `defaultMaxTurns` and `graceTurns` through `RunOptions` so `agent-runner.ts` reads them from its call-time options — not from module scope.
|
|
20
|
+
- Give `AgentManager` a config getter so it can pass runtime values in `RunOptions`.
|
|
21
|
+
- Reduce `index.ts` to a composition root that creates the runtime and passes it to factories — no closure-scoped mutable `let` variables remain.
|
|
22
|
+
- Remove the module-scope `let` declarations and getter/setter exports from `agent-runner.ts`.
|
|
23
|
+
- No behavior change; pure structural refactor.
|
|
24
|
+
|
|
25
|
+
## Non-Goals
|
|
26
|
+
|
|
27
|
+
- Refactoring `AgentManager` into an options-object constructor (follow-up cleanup).
|
|
28
|
+
- Extracting event handlers into separate files.
|
|
29
|
+
- Changing tool behavior or the `SubagentsService` interface.
|
|
30
|
+
- Changing the `SettingsAppliers` interface in `settings.ts` — the callback pattern is already clean; only the closure targets change.
|
|
31
|
+
|
|
32
|
+
## Background
|
|
33
|
+
|
|
34
|
+
### Prior art
|
|
35
|
+
|
|
36
|
+
`pi-permission-system` solved the identical problem in #43.
|
|
37
|
+
`src/runtime.ts` there defines an `ExtensionRuntime` interface with all mutable state, a `createExtensionRuntime()` factory, and pure helper functions like `refreshExtensionConfig(runtime, ctx)` that write to the runtime instead of module-scope variables.
|
|
38
|
+
The extension's `index.ts` calls `createExtensionRuntime()` once and passes the runtime to handlers and factories.
|
|
39
|
+
This plan follows the same pattern.
|
|
40
|
+
|
|
41
|
+
### Module-scope state in agent-runner.ts
|
|
42
|
+
|
|
43
|
+
Two `let` variables and four getter/setter exports:
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
let defaultMaxTurns: number | undefined;
|
|
47
|
+
let graceTurns = 5;
|
|
48
|
+
|
|
49
|
+
export function getDefaultMaxTurns(): number | undefined { ... }
|
|
50
|
+
export function setDefaultMaxTurns(n: number | undefined): void { ... }
|
|
51
|
+
export function getGraceTurns(): number { ... }
|
|
52
|
+
export function setGraceTurns(n: number): void { ... }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`runAgent` reads both from module scope during the turn-limit subscription callback:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
const maxTurns = normalizeMaxTurns(
|
|
59
|
+
options.maxTurns ?? agentConfig?.maxTurns ?? defaultMaxTurns,
|
|
60
|
+
);
|
|
61
|
+
// ...
|
|
62
|
+
} else if (softLimitReached && turnCount >= maxTurns + graceTurns) {
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Closure-scoped state in index.ts
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
const agentActivity = new Map<string, AgentActivity>();
|
|
69
|
+
let widget: AgentWidget;
|
|
70
|
+
let currentCtx: { pi: unknown; ctx: unknown } | undefined;
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`widget` is assigned *after* `AgentManager` construction, but `notifications` closes over it immediately via arrow callbacks (`(id) => widget.markFinished(id)`).
|
|
74
|
+
`currentCtx` is written by `session_start` and read by `createSubagentsService`.
|
|
75
|
+
`agentActivity` is shared across the notification system, widget, agent tool, and menu handler.
|
|
76
|
+
|
|
77
|
+
### Settings flow
|
|
78
|
+
|
|
79
|
+
`settings.ts` defines `SettingsAppliers` with three setter callbacks.
|
|
80
|
+
`applyAndEmitLoaded(appliers, emit)` loads persisted settings and calls them.
|
|
81
|
+
`index.ts` wires the appliers to `setDefaultMaxTurns` / `setGraceTurns` from `agent-runner.ts` and `manager.setMaxConcurrent`.
|
|
82
|
+
After this refactor, the appliers closure targets change to the runtime — the `SettingsAppliers` interface itself stays the same.
|
|
83
|
+
|
|
84
|
+
### Data flow for defaultMaxTurns / graceTurns
|
|
85
|
+
|
|
86
|
+
Current: `settings.ts → setDefaultMaxTurns() → module-scope let → runAgent reads module scope`.
|
|
87
|
+
|
|
88
|
+
After: `settings.ts → applier closure → runtime.defaultMaxTurns → AgentManager.getRunConfig() → RunOptions → runAgent reads options`.
|
|
89
|
+
|
|
90
|
+
### Relevant constraints from AGENTS.md
|
|
91
|
+
|
|
92
|
+
- Keep modules focused and composable (one concern per file).
|
|
93
|
+
- Prefer explicit configuration over hidden behavior.
|
|
94
|
+
- Pi SDK imports stay out of business-logic modules — `runtime.ts` must not import Pi SDK types.
|
|
95
|
+
- Do not read `process.env` / `process.cwd()` inside library functions — accept as parameter.
|
|
96
|
+
- Narrow interfaces per consumer — do not pass a shared dependency bag when a function only uses a subset.
|
|
97
|
+
|
|
98
|
+
## Design Overview
|
|
99
|
+
|
|
100
|
+
### SubagentRuntime interface
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
export interface SubagentRuntime {
|
|
104
|
+
// ── Execution config (was module-scope in agent-runner.ts) ──
|
|
105
|
+
defaultMaxTurns: number | undefined;
|
|
106
|
+
graceTurns: number;
|
|
107
|
+
|
|
108
|
+
// ── Session state (was closure-scoped in index.ts) ──
|
|
109
|
+
currentCtx: { pi: unknown; ctx: unknown } | undefined;
|
|
110
|
+
readonly agentActivity: Map<string, AgentActivity>;
|
|
111
|
+
widget: AgentWidget | null;
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
The interface is flat (no sub-objects) to match the prior art in `pi-permission-system`.
|
|
116
|
+
`agentActivity` is `readonly` because the Map itself is never replaced — only its entries change.
|
|
117
|
+
`widget` is nullable because it is constructed after `AgentManager` and assigned later.
|
|
118
|
+
|
|
119
|
+
### createSubagentRuntime factory
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
export function createSubagentRuntime(): SubagentRuntime {
|
|
123
|
+
return {
|
|
124
|
+
defaultMaxTurns: undefined,
|
|
125
|
+
graceTurns: 5,
|
|
126
|
+
currentCtx: undefined,
|
|
127
|
+
agentActivity: new Map(),
|
|
128
|
+
widget: null,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
No parameters needed — the factory returns defaults.
|
|
134
|
+
Tests construct a fresh runtime per test for isolation.
|
|
135
|
+
|
|
136
|
+
### RunConfig — narrow interface for agent-manager
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
export interface RunConfig {
|
|
140
|
+
readonly defaultMaxTurns: number | undefined;
|
|
141
|
+
readonly graceTurns: number;
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
`AgentManager` receives `getRunConfig?: () => RunConfig` as a constructor parameter.
|
|
146
|
+
When constructing `RunOptions` for `runAgent`, it calls `getRunConfig()` and spreads the values.
|
|
147
|
+
During the lift-and-shift phase (before module-scope removal), `runAgent` falls back to the module-scope values when the RunOptions fields are absent.
|
|
148
|
+
|
|
149
|
+
### RunOptions changes
|
|
150
|
+
|
|
151
|
+
Two new optional fields:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
export interface RunOptions {
|
|
155
|
+
// ... existing fields ...
|
|
156
|
+
/** Default max turns from runtime config. Overridden by per-agent maxTurns. */
|
|
157
|
+
defaultMaxTurns?: number;
|
|
158
|
+
/** Grace turns after soft limit steer. */
|
|
159
|
+
graceTurns?: number;
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
`runAgent` changes its resolution chain from:
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
const maxTurns = normalizeMaxTurns(
|
|
167
|
+
options.maxTurns ?? agentConfig?.maxTurns ?? defaultMaxTurns,
|
|
168
|
+
);
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
To:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
const maxTurns = normalizeMaxTurns(
|
|
175
|
+
options.maxTurns ?? agentConfig?.maxTurns ?? options.defaultMaxTurns,
|
|
176
|
+
);
|
|
177
|
+
const effectiveGraceTurns = options.graceTurns ?? 5;
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### normalizeMaxTurns stays in agent-runner.ts
|
|
181
|
+
|
|
182
|
+
`normalizeMaxTurns` is a pure function used by both the runtime setter logic (in `index.ts` wire-up) and `runAgent`'s maxTurns resolution.
|
|
183
|
+
It stays exported from `agent-runner.ts`.
|
|
184
|
+
|
|
185
|
+
### index.ts wire-up changes
|
|
186
|
+
|
|
187
|
+
After refactoring, the extension factory:
|
|
188
|
+
|
|
189
|
+
1. Calls `createSubagentRuntime()` to get the runtime.
|
|
190
|
+
2. Wires `applyAndEmitLoaded` appliers to write to `runtime.defaultMaxTurns` and `runtime.graceTurns` (with normalization).
|
|
191
|
+
3. Passes `getRunConfig: () => ({ defaultMaxTurns: runtime.defaultMaxTurns, graceTurns: runtime.graceTurns })` to `AgentManager`.
|
|
192
|
+
4. Uses `runtime.agentActivity` instead of a local `const agentActivity`.
|
|
193
|
+
5. Uses `runtime.currentCtx` instead of a local `let currentCtx`.
|
|
194
|
+
6. Sets `runtime.widget = new AgentWidget(...)` instead of a local `let widget`.
|
|
195
|
+
7. Arrow closures in notification deps, tool deps, and menu deps reference `runtime.widget!` / `runtime.agentActivity` / `runtime.currentCtx` by capturing `runtime` by reference.
|
|
196
|
+
|
|
197
|
+
No closure-scoped `let` variables remain.
|
|
198
|
+
|
|
199
|
+
### Edge cases
|
|
200
|
+
|
|
201
|
+
- **Widget null access**: Notification system callbacks reference `runtime.widget!.markFinished(id)`.
|
|
202
|
+
This is safe because notifications only fire after agents complete, which is always after widget construction.
|
|
203
|
+
The `!` assertion documents the invariant.
|
|
204
|
+
- **currentCtx undefined**: `getCtx: () => runtime.currentCtx` behaves identically to the current `() => currentCtx` — the arrow closure captures the runtime object by reference and reads the field at call time.
|
|
205
|
+
- **Backward compatibility during lift-and-shift**: During intermediate steps, `runAgent` falls back to module-scope state when `options.defaultMaxTurns` / `options.graceTurns` are absent, so the test suite stays green throughout.
|
|
206
|
+
|
|
207
|
+
## Module-Level Changes
|
|
208
|
+
|
|
209
|
+
### `src/runtime.ts` (new)
|
|
210
|
+
|
|
211
|
+
- `SubagentRuntime` interface — all mutable state fields.
|
|
212
|
+
- `RunConfig` interface — narrow config subset for `AgentManager`.
|
|
213
|
+
- `createSubagentRuntime()` factory — returns a fresh runtime with defaults.
|
|
214
|
+
|
|
215
|
+
### `src/agent-runner.ts` (modified)
|
|
216
|
+
|
|
217
|
+
- Add `defaultMaxTurns?: number` and `graceTurns?: number` to `RunOptions`.
|
|
218
|
+
- Update `runAgent`'s maxTurns resolution to prefer `options.defaultMaxTurns` over module scope (step 2), then remove module scope entirely (step 6).
|
|
219
|
+
- Update `graceTurns` usage in the turn-limit callback to prefer `options.graceTurns` over module scope (step 2), then remove fallback (step 6).
|
|
220
|
+
- Remove `let defaultMaxTurns`, `let graceTurns`, `getDefaultMaxTurns`, `setDefaultMaxTurns`, `getGraceTurns`, `setGraceTurns` exports (step 6).
|
|
221
|
+
- `normalizeMaxTurns` stays exported (pure function, no state dependency).
|
|
222
|
+
|
|
223
|
+
### `src/agent-manager.ts` (modified)
|
|
224
|
+
|
|
225
|
+
- Add optional `getRunConfig?: () => RunConfig` parameter to constructor.
|
|
226
|
+
- In `startAgent`, call `getRunConfig?.()` and pass `defaultMaxTurns` and `graceTurns` in the `RunOptions` object given to `runAgent`.
|
|
227
|
+
|
|
228
|
+
### `src/index.ts` (modified)
|
|
229
|
+
|
|
230
|
+
- Import `createSubagentRuntime` from `./runtime.js`.
|
|
231
|
+
- Create `const runtime = createSubagentRuntime()` at the top of the factory.
|
|
232
|
+
- Replace `const agentActivity = new Map<>()` with `runtime.agentActivity`.
|
|
233
|
+
- Replace `let widget: AgentWidget` with `runtime.widget`.
|
|
234
|
+
- Replace `let currentCtx` with `runtime.currentCtx`.
|
|
235
|
+
- Wire `applyAndEmitLoaded` appliers to `runtime.defaultMaxTurns` / `runtime.graceTurns` with normalization.
|
|
236
|
+
- Pass `getRunConfig` to `AgentManager` constructor.
|
|
237
|
+
- Update `snapshotSettings` to read from `runtime.defaultMaxTurns` / `runtime.graceTurns`.
|
|
238
|
+
- Remove imports of `getDefaultMaxTurns`, `setDefaultMaxTurns`, `getGraceTurns`, `setGraceTurns` from `agent-runner.js`.
|
|
239
|
+
- All arrow closures in notification, tool, menu, and service deps capture `runtime` by reference.
|
|
240
|
+
|
|
241
|
+
### `test/runtime.test.ts` (new)
|
|
242
|
+
|
|
243
|
+
- Factory returns expected defaults.
|
|
244
|
+
- Fields are independently mutable.
|
|
245
|
+
- Multiple instances are isolated.
|
|
246
|
+
|
|
247
|
+
### `test/agent-runner-settings.test.ts` (modified → removed or substantially rewritten)
|
|
248
|
+
|
|
249
|
+
- Current tests exercise `setDefaultMaxTurns` / `getDefaultMaxTurns` / `setGraceTurns` / `getGraceTurns` as module-scope getters/setters.
|
|
250
|
+
- After step 6 removes those exports, these tests must migrate.
|
|
251
|
+
- `normalizeMaxTurns` tests stay as-is (the function remains exported).
|
|
252
|
+
- Setter-behavior tests (clamping, unlimited marker) become tests of the normalization logic applied in `index.ts` wire-up or `runtime.test.ts`.
|
|
253
|
+
- The `runAgent` integration with `defaultMaxTurns` / `graceTurns` is tested via RunOptions in `agent-runner.test.ts`.
|
|
254
|
+
|
|
255
|
+
### `test/agent-manager.test.ts` (modified)
|
|
256
|
+
|
|
257
|
+
- Constructor calls gain `getRunConfig` parameter (or omit it — default is no-op).
|
|
258
|
+
- Existing tests pass `undefined` for `getRunConfig` (backward compatible).
|
|
259
|
+
- New tests verify that `runAgent` receives `defaultMaxTurns` / `graceTurns` from `getRunConfig`.
|
|
260
|
+
|
|
261
|
+
## Test Impact Analysis
|
|
262
|
+
|
|
263
|
+
### New unit tests enabled by the extraction
|
|
264
|
+
|
|
265
|
+
1. `test/runtime.test.ts` — `createSubagentRuntime` factory returns correct defaults, fields are independently mutable, multiple instances don't share state.
|
|
266
|
+
2. `test/agent-runner.test.ts` additions — `runAgent` uses `options.defaultMaxTurns` and `options.graceTurns` when provided, with correct fallback behavior.
|
|
267
|
+
3. `test/agent-manager.test.ts` additions — `AgentManager` calls `getRunConfig()` and passes values in `RunOptions`.
|
|
268
|
+
|
|
269
|
+
### Existing tests that become redundant
|
|
270
|
+
|
|
271
|
+
- `test/agent-runner-settings.test.ts` tests for `setDefaultMaxTurns` / `getDefaultMaxTurns` / `setGraceTurns` / `getGraceTurns` — these getter/setter pairs are removed.
|
|
272
|
+
The normalization behavior they test is preserved via `normalizeMaxTurns` (which stays) and runtime wire-up tests.
|
|
273
|
+
|
|
274
|
+
### Existing tests that stay as-is
|
|
275
|
+
|
|
276
|
+
- `test/settings.test.ts` — tests `SettingsAppliers` via mock callbacks; interface unchanged.
|
|
277
|
+
- `test/service-adapter.test.ts` — tests `AdapterDeps` via mock callbacks; `getCtx` interface unchanged.
|
|
278
|
+
- `test/agent-runner.test.ts` — existing final-output-capture and usage-callback tests are unaffected (they don't test maxTurns/graceTurns state).
|
|
279
|
+
- All other test files (agent-types, custom-agents, notification, renderer, tools, UI, etc.) — no dependency on the moved state.
|
|
280
|
+
|
|
281
|
+
## TDD Order
|
|
282
|
+
|
|
283
|
+
1. **Create `src/runtime.ts` with SubagentRuntime interface and factory.**
|
|
284
|
+
Write `test/runtime.test.ts` testing factory defaults and instance isolation.
|
|
285
|
+
Commit: `feat: add SubagentRuntime interface and factory`
|
|
286
|
+
|
|
287
|
+
2. **Add `defaultMaxTurns` and `graceTurns` to RunOptions; update `runAgent` to prefer them over module scope.**
|
|
288
|
+
In `agent-runner.ts`, add two optional fields to `RunOptions`.
|
|
289
|
+
Change maxTurns resolution to `options.maxTurns ?? agentConfig?.maxTurns ?? options.defaultMaxTurns ?? defaultMaxTurns` (backward compatible — module-scope fallback retained).
|
|
290
|
+
Change graceTurns usage to `options.graceTurns ?? graceTurns` (module-scope fallback retained).
|
|
291
|
+
Add tests in `agent-runner.test.ts` verifying that when `options.defaultMaxTurns` / `options.graceTurns` are provided, they are used.
|
|
292
|
+
Run `pnpm run check` to verify types.
|
|
293
|
+
Commit: `feat: thread defaultMaxTurns and graceTurns through RunOptions`
|
|
294
|
+
|
|
295
|
+
3. **Wire `AgentManager` to pass runtime config in RunOptions.**
|
|
296
|
+
Add `getRunConfig?: () => RunConfig` as the 5th constructor parameter (optional, backward compatible).
|
|
297
|
+
In `startAgent`, call `getRunConfig?.()` and spread into the RunOptions for `runAgent`.
|
|
298
|
+
Add agent-manager test verifying `runAgent` receives the config values.
|
|
299
|
+
Existing tests omit the param — green with no changes.
|
|
300
|
+
Commit: `refactor: agent-manager threads run config into RunOptions`
|
|
301
|
+
|
|
302
|
+
4. **Wire SubagentRuntime into index.ts — replace closure-scoped state.**
|
|
303
|
+
Import `createSubagentRuntime` and call it at the top of the factory.
|
|
304
|
+
Replace `const agentActivity`, `let widget`, and `let currentCtx` with runtime fields.
|
|
305
|
+
Wire settings appliers to `runtime.defaultMaxTurns` (via `normalizeMaxTurns`) and `runtime.graceTurns` (via `Math.max(1, n)`).
|
|
306
|
+
Pass `getRunConfig` callback to `AgentManager`.
|
|
307
|
+
Update `snapshotSettings` to read from runtime.
|
|
308
|
+
Remove imports of `getDefaultMaxTurns`, `setDefaultMaxTurns`, `getGraceTurns`, `setGraceTurns`.
|
|
309
|
+
Run full test suite.
|
|
310
|
+
Commit: `refactor: wire SubagentRuntime into extension factory`
|
|
311
|
+
|
|
312
|
+
5. **Remove module-scope state from `agent-runner.ts`.**
|
|
313
|
+
Delete `let defaultMaxTurns`, `let graceTurns`, and all four getter/setter functions.
|
|
314
|
+
Remove the module-scope fallback from `runAgent`'s resolution chain — `options.defaultMaxTurns` and `options.graceTurns` are now the sole source (with hardcoded defaults as a safety net: `undefined` and `5`).
|
|
315
|
+
Update `test/agent-runner-settings.test.ts`: remove tests for deleted getters/setters, keep `normalizeMaxTurns` tests.
|
|
316
|
+
Run `pnpm run check` and full test suite.
|
|
317
|
+
Commit: `refactor: remove module-scope mutable state from agent-runner`
|
|
318
|
+
|
|
319
|
+
6. **Final cleanup and acceptance verification.**
|
|
320
|
+
Verify acceptance criteria: `agent-runner.ts` contains no module-scope mutable state.
|
|
321
|
+
`index.ts` contains no closure-scoped `let` variables that outlive their initialization block.
|
|
322
|
+
`SubagentRuntime` interface exists with all mutable session state.
|
|
323
|
+
Tests can construct a runtime and pass it to factories without importing `index.ts`.
|
|
324
|
+
Full test suite passes.
|
|
325
|
+
Remove any dead imports or vestigial code.
|
|
326
|
+
Commit: `refactor: finalize SubagentRuntime migration (#69)`
|
|
327
|
+
|
|
328
|
+
## Risks and Mitigations
|
|
329
|
+
|
|
330
|
+
| Risk | Mitigation |
|
|
331
|
+
| --------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
332
|
+
| Backward-compatibility break during incremental migration — removing module-scope state before all consumers switch to RunOptions | Lift-and-shift: steps 2–3 introduce the new path alongside the old (module-scope fallback); step 5 removes the old path only after all consumers use the new path. |
|
|
333
|
+
| AgentManager constructor gains a 5th positional parameter — fragile and hard to read | Parameter is optional with no default behavior change. Plan notes this as a follow-up cleanup (convert to options object). |
|
|
334
|
+
| `runtime.widget!` non-null assertions in notification closures could NPE if initialization order changes | Assertion documents the invariant; widget is always constructed before any agent can complete. Add a defensive `if (!runtime.widget) return;` guard in the notification callbacks as a safety net. |
|
|
335
|
+
| `normalizeMaxTurns` stays in `agent-runner.ts` after getter/setter removal — unclear ownership | `normalizeMaxTurns` is a pure function used by the turn-limit logic in `runAgent`. It belongs in the module that uses it. If a future refactor moves turn-limit logic, the function moves with it. |
|
|
336
|
+
| Test file `agent-runner-settings.test.ts` needs substantial rewrite — risk of losing coverage | Keep `normalizeMaxTurns` tests intact (they test the same pure function). The setter/getter behavior tests are replaced by runtime factory tests and RunOptions integration tests that cover the same normalization logic. |
|
|
337
|
+
|
|
338
|
+
## Open Questions
|
|
339
|
+
|
|
340
|
+
- Should `AgentManager`'s constructor be converted from positional parameters to a named-options object?
|
|
341
|
+
This is natural cleanup but widens the blast radius.
|
|
342
|
+
Defer to a follow-up issue if the 5th positional parameter feels too fragile during implementation.
|
|
343
|
+
- Should `SubagentRuntime` include utility methods (e.g., `reset()`, `shutdown()`) for session lifecycle?
|
|
344
|
+
The issue's acceptance criteria focus on state ownership, not lifecycle methods.
|
|
345
|
+
Defer until a pattern of scattered resets emerges in practice.
|