@gotgenes/pi-subagents 6.3.1 → 6.5.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 +32 -0
- package/docs/architecture/architecture.md +244 -260
- package/docs/plans/0108-extract-agent-type-registry.md +322 -0
- package/docs/plans/0109-extract-settings-manager.md +276 -0
- package/docs/retro/0108-extract-agent-type-registry.md +41 -0
- package/package.json +1 -1
- package/src/agent-manager.ts +16 -13
- package/src/agent-runner.ts +4 -0
- package/src/agent-types.ts +108 -91
- package/src/index.ts +31 -58
- package/src/runtime.ts +0 -6
- package/src/session-config.ts +5 -4
- package/src/settings.ts +94 -46
- package/src/tools/agent-tool.ts +11 -11
- package/src/tools/get-result-tool.ts +3 -1
- package/src/types.ts +0 -3
- package/src/ui/agent-menu.ts +47 -53
- package/src/ui/agent-widget.ts +10 -9
- package/src/ui/conversation-viewer.ts +4 -2
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
---
|
|
2
|
+
issue: 108
|
|
3
|
+
issue_title: "refactor(pi-subagents): extract AgentTypeRegistry class from module-scoped state"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Extract AgentTypeRegistry class
|
|
7
|
+
|
|
8
|
+
## Problem Statement
|
|
9
|
+
|
|
10
|
+
`agent-types.ts` manages a module-scoped `Map<string, AgentConfig>` mutated by `registerAgents()` and read by 12+ call sites across 6 files.
|
|
11
|
+
This is global mutable state hidden behind free functions — tests must call `registerAgents(new Map())` in `beforeEach` to reset it, and `reloadCustomAgents` is threaded as a callback through `AgentToolDeps` and `AgentMenuDeps` because there is no object to own the reload.
|
|
12
|
+
|
|
13
|
+
## Goals
|
|
14
|
+
|
|
15
|
+
- Wrap the module-scoped `agents` Map and its free functions into an injectable `AgentTypeRegistry` class.
|
|
16
|
+
- Replace the `reloadCustomAgents` callback (threaded through 2 dependency bags) with `registry.reload()`.
|
|
17
|
+
- Move `DEFAULT_AGENT_NAMES` from `types.ts` to the registry (it is a constant, not a type).
|
|
18
|
+
- Enable test isolation without module resets — each test creates its own registry instance.
|
|
19
|
+
- Use lift-and-shift: introduce the class alongside the free functions, migrate consumers incrementally, then remove the free functions.
|
|
20
|
+
|
|
21
|
+
## Non-Goals
|
|
22
|
+
|
|
23
|
+
- `SettingsManager` extraction (#109) — separate Phase 7 step.
|
|
24
|
+
- `AgentActivityTracker` extraction (#110) — separate Phase 7 step.
|
|
25
|
+
- Splitting `AgentRecord` lifecycle state (#111) — separate Phase 7 step.
|
|
26
|
+
- Narrowing `AgentConfig` (21 fields) — tracked in the architecture doc but out of scope.
|
|
27
|
+
- Moving `BUILTIN_TOOL_NAMES` — it is a constant with no Map dependency, stays as a module export.
|
|
28
|
+
|
|
29
|
+
## Background
|
|
30
|
+
|
|
31
|
+
### Architecture reference
|
|
32
|
+
|
|
33
|
+
Phase 7, Step A1 in `docs/architecture/architecture.md`.
|
|
34
|
+
Steps A1–A3 are independent and can proceed in any order.
|
|
35
|
+
This plan addresses A1 only.
|
|
36
|
+
|
|
37
|
+
### Relevant modules
|
|
38
|
+
|
|
39
|
+
| Module | Role | agent-types dependency |
|
|
40
|
+
| --------------------------- | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
41
|
+
| `agent-types.ts` | Module-scoped `agents` Map + 11 free functions | Owns the state |
|
|
42
|
+
| `default-agents.ts` | `DEFAULT_AGENTS` Map constant | Read by `registerAgents` |
|
|
43
|
+
| `custom-agents.ts` | `loadCustomAgents()` → disk scan | Imports `BUILTIN_TOOL_NAMES` |
|
|
44
|
+
| `session-config.ts` | `assembleSessionConfig()` | Imports `resolveAgentConfig`, `getToolNamesForType`, `getMemoryToolNames`, `getReadOnlyMemoryToolNames` |
|
|
45
|
+
| `agent-runner.ts` | `runAgent()` → calls `assembleSessionConfig` | No direct import (transitive via session-config) |
|
|
46
|
+
| `agent-manager.ts` | `AgentManager` → calls runner | No direct import |
|
|
47
|
+
| `tools/agent-tool.ts` | Agent tool definition | Imports `resolveAgentConfig`, `resolveType`; receives `reloadCustomAgents` via `AgentToolDeps` |
|
|
48
|
+
| `ui/agent-menu.ts` | `/agents` command handler | Imports `BUILTIN_TOOL_NAMES`, `getAllTypes`, `resolveAgentConfig`, `resolveType`; receives `reloadCustomAgents` via `AgentMenuDeps` |
|
|
49
|
+
| `ui/agent-widget.ts` | Widget + `getDisplayName` / `getPromptModeLabel` helpers | Imports `resolveAgentConfig` |
|
|
50
|
+
| `ui/conversation-viewer.ts` | Live conversation overlay | Imports `getDisplayName`, `getPromptModeLabel` from `agent-widget.ts` |
|
|
51
|
+
| `tools/get-result-tool.ts` | `get_subagent_result` tool | Imports `getDisplayName` from `agent-widget.ts` |
|
|
52
|
+
| `index.ts` | Extension entry point, wiring | Imports `registerAgents`, `getAvailableTypes`, `getDefaultAgentNames`, `getUserAgentNames`, `resolveAgentConfig`; defines `reloadCustomAgents` closure |
|
|
53
|
+
|
|
54
|
+
### Test files affected
|
|
55
|
+
|
|
56
|
+
| Test file | Current agent-types coupling | Change needed |
|
|
57
|
+
| -------------------------------------------------- | ------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
|
|
58
|
+
| `agent-types.test.ts` (333 lines) | Tests free functions directly, calls `registerAgents()` in `beforeEach` | Add class tests; eventually migrate free-function tests to class tests |
|
|
59
|
+
| `session-config.test.ts` (601 lines) | `vi.mock("../src/agent-types.js")` | Pass mock registry param, remove module mock |
|
|
60
|
+
| `agent-runner.test.ts` (302 lines) | `vi.mock("../src/agent-types.js")` | Provide mock registry in `RunOptions`, remove module mock |
|
|
61
|
+
| `agent-runner-extension-tools.test.ts` (307 lines) | `vi.mock("../src/agent-types.js")` | Same as `agent-runner.test.ts` |
|
|
62
|
+
| `tools/agent-tool.test.ts` (240 lines) | Mocks `reloadCustomAgents` in deps | Replace with `registry` in deps |
|
|
63
|
+
| `ui/agent-menu.test.ts` (184 lines) | `vi.mock("../../src/agent-types.js")`, mocks `reloadCustomAgents` in deps | Replace with `registry` in deps, remove module mock |
|
|
64
|
+
| `agent-widget.test.ts` (26 lines) | Minimal | Pass registry to widget constructor |
|
|
65
|
+
| `agent-manager.test.ts` (712 lines) | No `agent-types.js` mock | Add registry to constructor options |
|
|
66
|
+
|
|
67
|
+
### Constraints from AGENTS.md / code-design skill
|
|
68
|
+
|
|
69
|
+
- **ISP:** `session-config.ts` should accept a narrow interface (only `resolveAgentConfig` + `getToolNamesForType`), not the full registry class.
|
|
70
|
+
- **DIP:** Accept collaborators as parameters; keep IO at the edges.
|
|
71
|
+
- **Pi SDK boundaries:** Pure helpers must not import Pi SDK types.
|
|
72
|
+
- **Lift-and-shift:** Never plan a single step that rewrites an entire large test file at once.
|
|
73
|
+
|
|
74
|
+
## Design Overview
|
|
75
|
+
|
|
76
|
+
### `AgentTypeRegistry` class
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
export class AgentTypeRegistry {
|
|
80
|
+
private agents = new Map<string, AgentConfig>();
|
|
81
|
+
|
|
82
|
+
constructor(private loadUserAgents: () => Map<string, AgentConfig>) {
|
|
83
|
+
this.reload();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Re-scan custom agents from disk and merge with defaults. */
|
|
87
|
+
reload(): void { /* clear + merge DEFAULT_AGENTS + loadUserAgents() */ }
|
|
88
|
+
|
|
89
|
+
resolveAgentConfig(type: string): AgentConfig { /* ... */ }
|
|
90
|
+
resolveType(name: string): string | undefined { /* ... */ }
|
|
91
|
+
getAvailableTypes(): string[] { /* ... */ }
|
|
92
|
+
getAllTypes(): string[] { /* ... */ }
|
|
93
|
+
getDefaultAgentNames(): string[] { /* ... */ }
|
|
94
|
+
getUserAgentNames(): string[] { /* ... */ }
|
|
95
|
+
isValidType(type: string): boolean { /* ... */ }
|
|
96
|
+
getToolNamesForType(type: string): string[] { /* ... */ }
|
|
97
|
+
|
|
98
|
+
static readonly DEFAULT_AGENT_NAMES = ["general-purpose", "Explore", "Plan"] as const;
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The constructor accepts a `loadUserAgents` callback (typically `() => loadCustomAgents(process.cwd())`).
|
|
103
|
+
This keeps `process.cwd()` at the edge (in `index.ts`) and makes tests trivial — they pass `() => new Map()` or a fixture.
|
|
104
|
+
|
|
105
|
+
### What stays as free functions
|
|
106
|
+
|
|
107
|
+
- **`BUILTIN_TOOL_NAMES`** — constant array, no Map dependency.
|
|
108
|
+
Stays as a module-level export in `agent-types.ts`.
|
|
109
|
+
- **`getMemoryToolNames` / `getReadOnlyMemoryToolNames`** — pure functions over constant arrays, no Map access.
|
|
110
|
+
Stay as module-level exports.
|
|
111
|
+
|
|
112
|
+
### Narrow interface for `session-config.ts`
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
/** Narrow registry interface for session-config (ISP). */
|
|
116
|
+
export interface AgentConfigLookup {
|
|
117
|
+
resolveAgentConfig(type: string): AgentConfig;
|
|
118
|
+
getToolNamesForType(type: string): string[];
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
`assembleSessionConfig` gains a `registry: AgentConfigLookup` parameter.
|
|
123
|
+
Tests construct a plain object satisfying this interface — no class instantiation needed.
|
|
124
|
+
|
|
125
|
+
### Threading through the call chain
|
|
126
|
+
|
|
127
|
+
`session-config.ts` ← called by `agent-runner.ts` ← called by `AgentManager`:
|
|
128
|
+
|
|
129
|
+
1. `assembleSessionConfig(type, ctx, options, env, registry)` — new param.
|
|
130
|
+
2. `RunOptions` gains `registry: AgentConfigLookup`.
|
|
131
|
+
3. `AgentManagerOptions` gains `registry: AgentTypeRegistry`.
|
|
132
|
+
4. `AgentManager` stores the registry and passes it to the runner via `RunOptions`.
|
|
133
|
+
|
|
134
|
+
### Replacing `reloadCustomAgents` callback
|
|
135
|
+
|
|
136
|
+
Before:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
// index.ts
|
|
140
|
+
const reloadCustomAgents = () => {
|
|
141
|
+
const userAgents = loadCustomAgents(process.cwd());
|
|
142
|
+
registerAgents(userAgents);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// AgentToolDeps / AgentMenuDeps
|
|
146
|
+
reloadCustomAgents: () => void;
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
After:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
// index.ts
|
|
153
|
+
const registry = new AgentTypeRegistry(() => loadCustomAgents(process.cwd()));
|
|
154
|
+
|
|
155
|
+
// AgentToolDeps / AgentMenuDeps
|
|
156
|
+
registry: AgentTypeRegistry;
|
|
157
|
+
|
|
158
|
+
// Callers use:
|
|
159
|
+
deps.registry.reload();
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Display helpers (`getDisplayName`, `getPromptModeLabel`)
|
|
163
|
+
|
|
164
|
+
These functions in `agent-widget.ts` call `resolveAgentConfig(type)`.
|
|
165
|
+
After the extraction, they accept the registry (or a `resolveAgentConfig` callback) as a parameter.
|
|
166
|
+
The `AgentWidget` constructor gains a `registry` parameter and passes it to these helpers internally.
|
|
167
|
+
External callers (`conversation-viewer.ts`, `get-result-tool.ts`, etc.) pass the registry they already hold via their deps.
|
|
168
|
+
|
|
169
|
+
## Module-Level Changes
|
|
170
|
+
|
|
171
|
+
### New
|
|
172
|
+
|
|
173
|
+
No new files.
|
|
174
|
+
|
|
175
|
+
### Modified
|
|
176
|
+
|
|
177
|
+
1. **`src/agent-types.ts`**
|
|
178
|
+
- Add `AgentTypeRegistry` class with all instance methods.
|
|
179
|
+
- Add `AgentConfigLookup` interface.
|
|
180
|
+
- Keep free functions temporarily (delegation shim during migration).
|
|
181
|
+
- Final step: remove free functions, module-scoped `agents` Map, and `registerAgents`.
|
|
182
|
+
|
|
183
|
+
2. **`src/types.ts`**
|
|
184
|
+
- Remove `DEFAULT_AGENT_NAMES` constant (moved to `AgentTypeRegistry.DEFAULT_AGENT_NAMES`).
|
|
185
|
+
|
|
186
|
+
3. **`src/session-config.ts`**
|
|
187
|
+
- `assembleSessionConfig` gains a `registry: AgentConfigLookup` parameter.
|
|
188
|
+
- Remove imports of `resolveAgentConfig`, `getToolNamesForType` from `agent-types.ts`.
|
|
189
|
+
|
|
190
|
+
4. **`src/agent-runner.ts`**
|
|
191
|
+
- `RunOptions` gains `registry: AgentConfigLookup`.
|
|
192
|
+
- `runAgent` passes `options.registry` to `assembleSessionConfig`.
|
|
193
|
+
|
|
194
|
+
5. **`src/agent-manager.ts`**
|
|
195
|
+
- `AgentManagerOptions` gains `registry: AgentTypeRegistry`.
|
|
196
|
+
- `AgentManager` stores `this.registry` and passes it into `RunOptions` when calling `runner.run`.
|
|
197
|
+
|
|
198
|
+
6. **`src/tools/agent-tool.ts`**
|
|
199
|
+
- `AgentToolDeps`: add `registry: AgentTypeRegistry`, remove `reloadCustomAgents`.
|
|
200
|
+
- Replace `resolveType(...)` / `resolveAgentConfig(...)` imports with `deps.registry.resolveType(...)` / `deps.registry.resolveAgentConfig(...)`.
|
|
201
|
+
- Replace `deps.reloadCustomAgents()` with `deps.registry.reload()`.
|
|
202
|
+
|
|
203
|
+
7. **`src/ui/agent-menu.ts`**
|
|
204
|
+
- `AgentMenuDeps`: add `registry: AgentTypeRegistry`, remove `reloadCustomAgents`.
|
|
205
|
+
- Replace `getAllTypes()`/`resolveAgentConfig()`/`resolveType()` imports with `deps.registry.*` calls.
|
|
206
|
+
- `BUILTIN_TOOL_NAMES` import stays (it is a constant, not a method).
|
|
207
|
+
- Replace `deps.reloadCustomAgents()` with `deps.registry.reload()`.
|
|
208
|
+
|
|
209
|
+
8. **`src/ui/agent-widget.ts`**
|
|
210
|
+
- `AgentWidget` constructor gains a `registry: AgentTypeRegistry` (or narrow interface) parameter.
|
|
211
|
+
- `getDisplayName(type, registry)` / `getPromptModeLabel(type, registry)` gain a registry parameter.
|
|
212
|
+
- Internal render methods use `this.registry`.
|
|
213
|
+
|
|
214
|
+
9. **`src/ui/conversation-viewer.ts`**
|
|
215
|
+
- `ConversationViewer` constructor gains a registry parameter.
|
|
216
|
+
- Passes it to `getDisplayName` / `getPromptModeLabel` calls.
|
|
217
|
+
|
|
218
|
+
10. **`src/tools/get-result-tool.ts`**
|
|
219
|
+
- `GetResultToolDeps` (or equivalent) gains registry.
|
|
220
|
+
- Passes it to `getDisplayName` calls.
|
|
221
|
+
|
|
222
|
+
11. **`src/index.ts`**
|
|
223
|
+
- Construct `AgentTypeRegistry` with `() => loadCustomAgents(process.cwd())`.
|
|
224
|
+
- Pass registry to `AgentManager`, agent-tool deps, menu deps, widget, get-result-tool deps.
|
|
225
|
+
- Remove `reloadCustomAgents` closure.
|
|
226
|
+
- Remove free-function imports (`registerAgents`, `getDefaultAgentNames`, `getUserAgentNames`, `getAvailableTypes`, `resolveAgentConfig`).
|
|
227
|
+
- Use `registry.*` methods directly for `buildTypeListText`.
|
|
228
|
+
|
|
229
|
+
### Removed
|
|
230
|
+
|
|
231
|
+
- Free functions from `agent-types.ts`: `registerAgents`, `resolveType`, `resolveAgentConfig`, `getAvailableTypes`, `getAllTypes`, `getDefaultAgentNames`, `getUserAgentNames`, `isValidType`, `getToolNamesForType` (final cleanup step).
|
|
232
|
+
- Module-scoped `agents` Map and `resolveKey` helper.
|
|
233
|
+
- `DEFAULT_AGENT_NAMES` from `types.ts`.
|
|
234
|
+
- `reloadCustomAgents` field from `AgentToolDeps` and `AgentMenuDeps`.
|
|
235
|
+
|
|
236
|
+
## Test Impact Analysis
|
|
237
|
+
|
|
238
|
+
### New tests enabled
|
|
239
|
+
|
|
240
|
+
- **Isolation without module resets:** Each test creates its own `AgentTypeRegistry` with a fixture callback, eliminating cross-test state leakage and the `registerAgents(new Map())` ceremony.
|
|
241
|
+
- **Reload behavior:** Tests can verify `registry.reload()` picks up new agents without touching module state.
|
|
242
|
+
|
|
243
|
+
### Existing tests that become redundant
|
|
244
|
+
|
|
245
|
+
- The existing free-function tests in `agent-types.test.ts` become redundant once the class tests cover the same behavior.
|
|
246
|
+
They can be removed in the final cleanup step.
|
|
247
|
+
|
|
248
|
+
### Existing tests that must stay
|
|
249
|
+
|
|
250
|
+
- `session-config.test.ts` — tests `assembleSessionConfig` behavior; mock setup changes from `vi.mock("agent-types.js")` to passing a mock `AgentConfigLookup` object.
|
|
251
|
+
- `agent-runner.test.ts`, `agent-runner-extension-tools.test.ts` — test runner behavior; mock setup changes from `vi.mock("agent-types.js")` to providing mock registry in `RunOptions`.
|
|
252
|
+
- `tools/agent-tool.test.ts` — tests tool handler; deps mock changes from `reloadCustomAgents: vi.fn()` to `registry: mockRegistry`.
|
|
253
|
+
- `ui/agent-menu.test.ts` — tests menu handler; deps mock changes similarly.
|
|
254
|
+
- `agent-manager.test.ts` — must add a mock registry to constructor options.
|
|
255
|
+
|
|
256
|
+
## TDD Order
|
|
257
|
+
|
|
258
|
+
1. **Create `AgentTypeRegistry` class** — Add class to `agent-types.ts` alongside existing free functions.
|
|
259
|
+
Add `AgentConfigLookup` interface.
|
|
260
|
+
Test all methods in a new `describe('AgentTypeRegistry')` block in `agent-types.test.ts`: construction, `reload()`, `resolveAgentConfig`, `resolveType`, `getAvailableTypes`, `getAllTypes`, `getDefaultAgentNames`, `getUserAgentNames`, `isValidType`, `getToolNamesForType`.
|
|
261
|
+
- Test surface: `agent-types.test.ts` — new describe block
|
|
262
|
+
- Commit: `feat(pi-subagents): add AgentTypeRegistry class (#108)`
|
|
263
|
+
|
|
264
|
+
2. **Inject through the config-assembly chain** — `assembleSessionConfig` gains `registry: AgentConfigLookup` param.
|
|
265
|
+
`RunOptions` gains `registry: AgentConfigLookup`.
|
|
266
|
+
`AgentManagerOptions` gains `registry: AgentTypeRegistry`.
|
|
267
|
+
Construct registry in `index.ts` and pass through `AgentManager` → `runAgent` → `assembleSessionConfig`.
|
|
268
|
+
Update `session-config.test.ts` (replace `vi.mock("agent-types.js")` with mock `AgentConfigLookup` object), `agent-runner.test.ts` and `agent-runner-extension-tools.test.ts` (provide mock registry in `RunOptions`, remove `vi.mock`), `agent-manager.test.ts` (add registry to constructor options).
|
|
269
|
+
- Test surface: `session-config.test.ts`, `agent-runner.test.ts`, `agent-runner-extension-tools.test.ts`, `agent-manager.test.ts`
|
|
270
|
+
- Commit: `refactor(pi-subagents): inject registry through config-assembly chain (#108)`
|
|
271
|
+
|
|
272
|
+
3. **Inject into agent tool** — Add `registry: AgentTypeRegistry` to `AgentToolDeps`, remove `reloadCustomAgents`.
|
|
273
|
+
Replace `resolveType` / `resolveAgentConfig` module imports with `deps.registry.*` calls.
|
|
274
|
+
Replace `deps.reloadCustomAgents()` with `deps.registry.reload()`.
|
|
275
|
+
Update `index.ts` agent-tool deps and `tools/agent-tool.test.ts`.
|
|
276
|
+
- Test surface: `tools/agent-tool.test.ts`
|
|
277
|
+
- Commit: `refactor(pi-subagents): inject registry into agent tool (#108)`
|
|
278
|
+
|
|
279
|
+
4. **Inject into agent menu** — Add `registry: AgentTypeRegistry` to `AgentMenuDeps`, remove `reloadCustomAgents`.
|
|
280
|
+
Replace `getAllTypes` / `resolveAgentConfig` / `resolveType` module imports with `deps.registry.*` calls.
|
|
281
|
+
Replace `deps.reloadCustomAgents()` with `deps.registry.reload()`.
|
|
282
|
+
Update `index.ts` menu deps and `ui/agent-menu.test.ts`.
|
|
283
|
+
- Test surface: `ui/agent-menu.test.ts`
|
|
284
|
+
- Commit: `refactor(pi-subagents): inject registry into agent menu (#108)`
|
|
285
|
+
|
|
286
|
+
5. **Inject into agent widget and display helpers** — `AgentWidget` constructor gains `registry`.
|
|
287
|
+
`getDisplayName(type, registry)` and `getPromptModeLabel(type, registry)` gain registry parameters.
|
|
288
|
+
`ConversationViewer` constructor gains registry.
|
|
289
|
+
`GetResultToolDeps` gains registry for `getDisplayName` calls.
|
|
290
|
+
Update `index.ts` wiring, `agent-widget.test.ts`, and any test files that call these helpers.
|
|
291
|
+
- Test surface: `agent-widget.test.ts`, related callers
|
|
292
|
+
- Commit: `refactor(pi-subagents): inject registry into agent widget (#108)`
|
|
293
|
+
|
|
294
|
+
6. **Move `DEFAULT_AGENT_NAMES` to registry** — Add static `DEFAULT_AGENT_NAMES` property on `AgentTypeRegistry`.
|
|
295
|
+
Remove the constant from `types.ts`.
|
|
296
|
+
Grep confirms no import consumers exist — the constant is defined but unused.
|
|
297
|
+
- Test surface: `agent-types.test.ts` — add assertion for static property
|
|
298
|
+
- Commit: `refactor(pi-subagents): move DEFAULT_AGENT_NAMES to registry (#108)`
|
|
299
|
+
|
|
300
|
+
7. **Remove free-function exports** — Delete `registerAgents`, `resolveType`, `resolveAgentConfig`, `getAvailableTypes`, `getAllTypes`, `getDefaultAgentNames`, `getUserAgentNames`, `isValidType`, `getToolNamesForType`, the module-scoped `agents` Map, and `resolveKey` helper from `agent-types.ts`.
|
|
301
|
+
Remove the free-function tests from `agent-types.test.ts` (now covered by class tests).
|
|
302
|
+
Remove any remaining free-function imports from `index.ts`.
|
|
303
|
+
Verify with `pnpm run check` that no dangling references remain.
|
|
304
|
+
- Test surface: `agent-types.test.ts` — remove old describe block
|
|
305
|
+
- Commit: `refactor(pi-subagents): remove free-function exports from agent-types (#108)`
|
|
306
|
+
|
|
307
|
+
## Risks and Mitigations
|
|
308
|
+
|
|
309
|
+
| Risk | Impact | Mitigation |
|
|
310
|
+
| --------------------------------------------------------------------------------------------- | ---------------------------------------------- | -------------------------------------------------------------------------------------------- |
|
|
311
|
+
| Large blast radius — 11 source files, 8 test files | Merge conflicts if other PRs land concurrently | Lift-and-shift: free functions keep working until final removal; each step is a valid commit |
|
|
312
|
+
| `vi.mock("agent-types.js")` removal in runner tests changes behavior | Tests may expose latent bugs in session-config | Mock `AgentConfigLookup` with the same values the current `vi.mock` provides |
|
|
313
|
+
| Display helpers (`getDisplayName`, `getPromptModeLabel`) thread registry through many callers | Signature churn in UI layer | These callers already have a deps bag or constructor params — registry fits naturally |
|
|
314
|
+
| `agent-types.test.ts` (333 lines) needs migration from free-function tests to class tests | Large test rewrite in step 7 | Step 1 creates class tests first; step 7 only deletes the now-redundant free-function tests |
|
|
315
|
+
| `AgentRunner` interface change (`RunOptions` gains `registry`) | Breaks callers that construct `RunOptions` | Only `agent-manager.ts` constructs `RunOptions`; single-site change |
|
|
316
|
+
|
|
317
|
+
## Open Questions
|
|
318
|
+
|
|
319
|
+
- **`getDisplayName` / `getPromptModeLabel` placement:** These are thin display helpers that wrap `resolveAgentConfig`.
|
|
320
|
+
The plan proposes adding a registry parameter.
|
|
321
|
+
An alternative is to make them methods on the registry itself (e.g., `registry.getDisplayName(type)`), trading purity for convenience.
|
|
322
|
+
Decide during implementation based on how natural the call sites feel.
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
---
|
|
2
|
+
issue: 109
|
|
3
|
+
issue_title: "refactor(pi-subagents): extract SettingsManager class"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Extract SettingsManager class
|
|
7
|
+
|
|
8
|
+
## Problem Statement
|
|
9
|
+
|
|
10
|
+
The settings read/write/persist cycle is spread across free functions in `settings.ts` (`loadSettings`, `saveSettings`, `applySettings`, `applyAndEmitLoaded`, `saveAndEmitChanged`), a `SettingsAppliers` callback interface, and 6 settings-related fields in `AgentMenuDeps`.
|
|
11
|
+
The in-memory values live on two different objects (`SubagentRuntime` for `defaultMaxTurns`/`graceTurns`, `AgentManager` for `maxConcurrent`).
|
|
12
|
+
This is mutable state plus the methods that read and write it — a class waiting to happen.
|
|
13
|
+
|
|
14
|
+
## Goals
|
|
15
|
+
|
|
16
|
+
- Encapsulate the settings concern into a single testable `SettingsManager` class.
|
|
17
|
+
- Own all three in-memory settings values (`defaultMaxTurns`, `graceTurns`, `maxConcurrent`).
|
|
18
|
+
- Absorb the `SettingsAppliers` interface and the composite functions `applyAndEmitLoaded`, `saveAndEmitChanged`.
|
|
19
|
+
- Collapse the 6 settings-related fields in `AgentMenuDeps` to a single `settings` collaborator (13 → 8 fields).
|
|
20
|
+
- Replace `getDefaultMaxTurns` in `AgentToolDeps` with a narrow settings accessor.
|
|
21
|
+
- Move `maxConcurrent` ownership from `AgentManager` to `SettingsManager`; `AgentManager` reads via injected function.
|
|
22
|
+
- Keep pure helpers (`sanitize`, `loadSettings`, `saveSettings`, `persistToastFor`) as private/internal implementation details.
|
|
23
|
+
- This is a non-breaking refactoring change — no public API surface changes.
|
|
24
|
+
|
|
25
|
+
## Non-Goals
|
|
26
|
+
|
|
27
|
+
- Changing the persistence format (`subagents.json`) or the global-vs-project merge strategy.
|
|
28
|
+
- Extracting `AgentActivityTracker` (#110) — that is the next step in Phase 7.
|
|
29
|
+
- Changing the `SubagentsService` public API (`service.ts`).
|
|
30
|
+
- Touching `RunConfig` — it stays as-is; `SettingsManager` naturally satisfies it.
|
|
31
|
+
|
|
32
|
+
## Background
|
|
33
|
+
|
|
34
|
+
### Current module map
|
|
35
|
+
|
|
36
|
+
| Module | Settings concern |
|
|
37
|
+
| --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
38
|
+
| `settings.ts` | Free functions: `loadSettings`, `saveSettings`, `applySettings`, `applyAndEmitLoaded`, `saveAndEmitChanged`, `persistToastFor`. Types: `SubagentsSettings`, `SettingsAppliers`, `SettingsEmit`. |
|
|
39
|
+
| `runtime.ts` | `SubagentRuntime.defaultMaxTurns` and `.graceTurns` — mutable fields read by `AgentManager` via `getRunConfig`. |
|
|
40
|
+
| `agent-manager.ts` | `private maxConcurrent` — used for queue decisions (`spawn`, `drainQueue`). Exposed via `get/setMaxConcurrent`. |
|
|
41
|
+
| `index.ts` | Wires callbacks: constructs `SettingsAppliers` closures, calls `applyAndEmitLoaded` at startup, builds 6 callback fields for `AgentMenuDeps`. |
|
|
42
|
+
| `ui/agent-menu.ts` | `AgentMenuDeps` has 6 settings fields: `getDefaultMaxTurns`, `setDefaultMaxTurns`, `getGraceTurns`, `setGraceTurns`, `snapshotSettings`, `saveSettings`. `AgentMenuManager` has `getMaxConcurrent`, `setMaxConcurrent`. |
|
|
43
|
+
| `tools/agent-tool.ts` | `AgentToolDeps.getDefaultMaxTurns` — reads the runtime default for the Agent tool. |
|
|
44
|
+
|
|
45
|
+
### Architecture reference
|
|
46
|
+
|
|
47
|
+
Phase 7, Step A2 in `docs/architecture/architecture.md`.
|
|
48
|
+
Predecessor A1 (`AgentTypeRegistry`, #108) is complete.
|
|
49
|
+
|
|
50
|
+
### Applicable constraints (from AGENTS.md / code-design)
|
|
51
|
+
|
|
52
|
+
- One concern per file — the class consolidates what is currently scattered.
|
|
53
|
+
- Prefer explicit configuration over hidden behavior.
|
|
54
|
+
- Dependency inversion — consumers accept narrow interfaces, not the concrete class.
|
|
55
|
+
- No output arguments — the current `SettingsAppliers` callback pattern writes into external state; the class owns the state directly.
|
|
56
|
+
- ES2024 target; pnpm only.
|
|
57
|
+
|
|
58
|
+
## Design Overview
|
|
59
|
+
|
|
60
|
+
### SettingsManager class
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
export class SettingsManager {
|
|
64
|
+
// Private fields with built-in defaults
|
|
65
|
+
private _defaultMaxTurns: number | undefined = undefined;
|
|
66
|
+
private _graceTurns: number = 5;
|
|
67
|
+
private _maxConcurrent: number = 4; // DEFAULT_MAX_CONCURRENT
|
|
68
|
+
|
|
69
|
+
private readonly emit: SettingsEmit;
|
|
70
|
+
private readonly cwd: string;
|
|
71
|
+
|
|
72
|
+
constructor(deps: { emit: SettingsEmit; cwd: string });
|
|
73
|
+
|
|
74
|
+
// ── Property accessors with normalization ──
|
|
75
|
+
get defaultMaxTurns(): number | undefined;
|
|
76
|
+
set defaultMaxTurns(n: number | undefined); // 0 or undefined → undefined; else max(1, n)
|
|
77
|
+
|
|
78
|
+
get graceTurns(): number;
|
|
79
|
+
set graceTurns(n: number); // max(1, n)
|
|
80
|
+
|
|
81
|
+
get maxConcurrent(): number;
|
|
82
|
+
set maxConcurrent(n: number); // max(1, n)
|
|
83
|
+
|
|
84
|
+
// ── Lifecycle methods ──
|
|
85
|
+
|
|
86
|
+
/** Load merged settings (global + project), apply to in-memory, emit settings_loaded. */
|
|
87
|
+
load(): SubagentsSettings;
|
|
88
|
+
|
|
89
|
+
/** Snapshot current values for persistence (defaultMaxTurns uses 0 for unlimited). */
|
|
90
|
+
snapshot(): { maxConcurrent: number; defaultMaxTurns: number; graceTurns: number };
|
|
91
|
+
|
|
92
|
+
/** Persist snapshot, emit settings_changed, return toast. */
|
|
93
|
+
saveAndNotify(successMsg: string): { message: string; level: "info" | "warning" };
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
The setter for `defaultMaxTurns` inlines the `normalizeMaxTurns` logic (`0 → undefined`, else `Math.max(1, n)`) to avoid a new dependency from `settings.ts` → `agent-runner.ts`.
|
|
98
|
+
The `normalizeMaxTurns` export in `agent-runner.ts` stays for per-invocation normalization in the Agent tool.
|
|
99
|
+
|
|
100
|
+
### maxConcurrent ownership transfer
|
|
101
|
+
|
|
102
|
+
`AgentManager` currently owns `maxConcurrent` and calls `this.drainQueue()` in `setMaxConcurrent`.
|
|
103
|
+
After the change:
|
|
104
|
+
|
|
105
|
+
1. `SettingsManager` owns the value.
|
|
106
|
+
2. `AgentManager` accepts a `getMaxConcurrent: () => number` function (injected via `AgentManagerOptions`).
|
|
107
|
+
All internal reads (`spawn` queue check, `drainQueue` loop) use `this.getMaxConcurrent()` instead of `this.maxConcurrent`.
|
|
108
|
+
3. `AgentManager.setMaxConcurrent(n)` is replaced with `notifyConcurrencyChanged()` — it only drains the queue; the value has already been set on `SettingsManager` by the caller.
|
|
109
|
+
4. `AgentMenuManager` loses `getMaxConcurrent` and `setMaxConcurrent`; gains `notifyConcurrencyChanged`.
|
|
110
|
+
The menu reads `settings.maxConcurrent` directly for display.
|
|
111
|
+
|
|
112
|
+
### Consumer interface narrowing
|
|
113
|
+
|
|
114
|
+
Each consumer gets the narrowest type it needs:
|
|
115
|
+
|
|
116
|
+
| Consumer | Interface |
|
|
117
|
+
| -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
118
|
+
| `AgentMenuDeps.settings` | `{ maxConcurrent: number; defaultMaxTurns: number \| undefined; graceTurns: number; saveAndNotify(msg: string): { message: string; level: "info" \| "warning" } }` |
|
|
119
|
+
| `AgentToolDeps.settings` | `{ readonly defaultMaxTurns: number \| undefined }` |
|
|
120
|
+
| `AgentManagerOptions.getMaxConcurrent` | `() => number` (function, not object) |
|
|
121
|
+
| `AgentManagerOptions.getRunConfig` | Unchanged — `SettingsManager` satisfies `RunConfig` structurally. |
|
|
122
|
+
|
|
123
|
+
### RunConfig compatibility
|
|
124
|
+
|
|
125
|
+
`SettingsManager` has `defaultMaxTurns` and `graceTurns` as readable properties, so it structurally satisfies the existing `RunConfig` interface.
|
|
126
|
+
In `index.ts`, the `getRunConfig` option becomes `() => settings` (returning the `SettingsManager` instance directly).
|
|
127
|
+
|
|
128
|
+
### SubagentRuntime cleanup
|
|
129
|
+
|
|
130
|
+
`SubagentRuntime.defaultMaxTurns` and `.graceTurns` are removed.
|
|
131
|
+
These fields exist solely for settings; after the extraction, `SubagentRuntime` only retains session state and widget delegation.
|
|
132
|
+
|
|
133
|
+
## Module-Level Changes
|
|
134
|
+
|
|
135
|
+
### `src/settings.ts`
|
|
136
|
+
|
|
137
|
+
- **Add** `SettingsManager` class (constructor, 3 property accessors, `load`, `snapshot`, `saveAndNotify`).
|
|
138
|
+
- **Keep** `SubagentsSettings`, `SettingsEmit`, `loadSettings`, `saveSettings`, `sanitize`, `persistToastFor` as internal helpers (some may become unexported).
|
|
139
|
+
- **Remove** `SettingsAppliers` interface.
|
|
140
|
+
- **Remove** `applySettings`, `applyAndEmitLoaded`, `saveAndEmitChanged` functions.
|
|
141
|
+
|
|
142
|
+
### `src/runtime.ts`
|
|
143
|
+
|
|
144
|
+
- **Remove** `defaultMaxTurns` and `graceTurns` fields from `SubagentRuntime`.
|
|
145
|
+
- **Remove** `RunConfig` interface (no longer needed — consumers read from `SettingsManager` directly).
|
|
146
|
+
|
|
147
|
+
### `src/agent-manager.ts`
|
|
148
|
+
|
|
149
|
+
- **Change** `AgentManagerOptions`: remove `maxConcurrent?: number`; add `getMaxConcurrent?: () => number`.
|
|
150
|
+
- **Change** constructor: store `getMaxConcurrent` function instead of a value.
|
|
151
|
+
- **Replace** `private maxConcurrent: number` with `private readonly getMaxConcurrent: () => number`.
|
|
152
|
+
- **Replace** `setMaxConcurrent(n)` with `notifyConcurrencyChanged()` (public, just calls `drainQueue()`).
|
|
153
|
+
- **Keep** `getRunConfig` — wiring changes in `index.ts` to point at settings.
|
|
154
|
+
|
|
155
|
+
### `src/ui/agent-menu.ts`
|
|
156
|
+
|
|
157
|
+
- **Remove** from `AgentMenuDeps`: `getDefaultMaxTurns`, `setDefaultMaxTurns`, `getGraceTurns`, `setGraceTurns`, `snapshotSettings`, `saveSettings`.
|
|
158
|
+
- **Add** to `AgentMenuDeps`: `settings` with the narrow inline interface.
|
|
159
|
+
- **Remove** from `AgentMenuManager`: `getMaxConcurrent`, `setMaxConcurrent`.
|
|
160
|
+
- **Add** to `AgentMenuManager`: `notifyConcurrencyChanged: () => void`.
|
|
161
|
+
- **Update** `showSettings`: read from `deps.settings`, write to `deps.settings`, call `deps.manager.notifyConcurrencyChanged()` after concurrency change.
|
|
162
|
+
- **Update** `notifyApplied`: call `deps.settings.saveAndNotify(msg)`.
|
|
163
|
+
|
|
164
|
+
### `src/tools/agent-tool.ts`
|
|
165
|
+
|
|
166
|
+
- **Remove** `getDefaultMaxTurns` from `AgentToolDeps`.
|
|
167
|
+
- **Add** `settings: { readonly defaultMaxTurns: number | undefined }` to `AgentToolDeps`.
|
|
168
|
+
- **Update** usage: `deps.getDefaultMaxTurns()` → `deps.settings.defaultMaxTurns`.
|
|
169
|
+
|
|
170
|
+
### `src/index.ts`
|
|
171
|
+
|
|
172
|
+
- **Create** `SettingsManager` before `AgentManager`; call `.load()`.
|
|
173
|
+
- **Pass** `getMaxConcurrent: () => settings.maxConcurrent` to `AgentManager`.
|
|
174
|
+
- **Pass** `getRunConfig: () => settings` to `AgentManager`.
|
|
175
|
+
- **Pass** `settings` to `AgentMenuDeps` and `AgentToolDeps`.
|
|
176
|
+
- **Remove** the ad-hoc `applyAndEmitLoaded` call and the 6 callback fields.
|
|
177
|
+
- **Remove** `runtime.defaultMaxTurns` and `runtime.graceTurns` references.
|
|
178
|
+
|
|
179
|
+
## Test Impact Analysis
|
|
180
|
+
|
|
181
|
+
### New unit tests enabled
|
|
182
|
+
|
|
183
|
+
- **Integrated settings lifecycle**: construct → `load()` → mutate → `saveAndNotify()` → verify snapshot and events, all on a single object.
|
|
184
|
+
Previously impossible because state was scattered across free functions, callbacks, and two separate objects.
|
|
185
|
+
- **Normalization in setters**: direct tests for `set defaultMaxTurns(0) → undefined`, `set graceTurns(0) → 1`, `set maxConcurrent(0) → 1`.
|
|
186
|
+
Previously tested only indirectly through `applySettings` + callback mocks.
|
|
187
|
+
- **Snapshot consistency**: verify that `snapshot()` reflects the current in-memory state after mutations.
|
|
188
|
+
|
|
189
|
+
### Existing tests that become redundant
|
|
190
|
+
|
|
191
|
+
- `applySettings` tests — the SettingsAppliers callback pattern is removed; normalization logic moves into the class setters.
|
|
192
|
+
- `applyAndEmitLoaded` tests — absorbed into `SettingsManager.load()` tests.
|
|
193
|
+
- `saveAndEmitChanged` tests — absorbed into `SettingsManager.saveAndNotify()` tests.
|
|
194
|
+
|
|
195
|
+
### Existing tests that must stay
|
|
196
|
+
|
|
197
|
+
- All `loadSettings` / `saveSettings` / sanitizer tests — these test the I/O + validation layer, which remains as internal helpers.
|
|
198
|
+
- `persistToastFor` tests — pure function, still used internally by `saveAndNotify`.
|
|
199
|
+
- `agent-menu.test.ts` settings tests — still needed; mock shape changes from 6 fields to a settings object.
|
|
200
|
+
- `agent-tool.test.ts` — mock shape changes from `getDefaultMaxTurns` function to `settings` object.
|
|
201
|
+
- `agent-manager.test.ts` — mock shape changes from `maxConcurrent` number to `getMaxConcurrent` function.
|
|
202
|
+
|
|
203
|
+
## TDD Order
|
|
204
|
+
|
|
205
|
+
### Cycle 1: SettingsManager — constructor, defaults, get/set normalization
|
|
206
|
+
|
|
207
|
+
1. Red: test constructor produces correct defaults (`defaultMaxTurns: undefined`, `graceTurns: 5`, `maxConcurrent: 4`).
|
|
208
|
+
Test setter normalization: `defaultMaxTurns = 0 → undefined`, `graceTurns = 0 → 1`, `maxConcurrent = 0 → 1`, `defaultMaxTurns = 10 → 10`.
|
|
209
|
+
2. Green: implement `SettingsManager` class with private fields, constructor, and property accessors.
|
|
210
|
+
3. Commit: `feat: add SettingsManager class with get/set normalization`
|
|
211
|
+
|
|
212
|
+
### Cycle 2: SettingsManager — load, snapshot, saveAndNotify, events
|
|
213
|
+
|
|
214
|
+
1. Red: test `load()` reads merged settings from disk, applies to in-memory values, emits `subagents:settings_loaded`.
|
|
215
|
+
Test `snapshot()` returns current values with `defaultMaxTurns ?? 0`.
|
|
216
|
+
Test `saveAndNotify()` persists to disk, emits `subagents:settings_changed`, returns toast.
|
|
217
|
+
Test save failure returns warning-level toast.
|
|
218
|
+
2. Green: implement `load()`, `snapshot()`, `saveAndNotify()` using existing `loadSettings`, `saveSettings`, `persistToastFor`.
|
|
219
|
+
3. Commit: `feat: SettingsManager load, save, snapshot, and lifecycle events`
|
|
220
|
+
|
|
221
|
+
### Cycle 3: Narrow AgentMenuDeps — collapse 6 fields to settings
|
|
222
|
+
|
|
223
|
+
1. Red: update `makeDeps` in `agent-menu.test.ts` — replace 6 settings fields with a `settings` mock object; replace `getMaxConcurrent`/`setMaxConcurrent` on manager with `notifyConcurrencyChanged`.
|
|
224
|
+
All existing menu tests fail due to mock shape change.
|
|
225
|
+
2. Green: update `AgentMenuDeps` and `AgentMenuManager` interfaces; update `showSettings` and `notifyApplied` to use `deps.settings`.
|
|
226
|
+
3. Run `pnpm run check` to verify types.
|
|
227
|
+
4. Commit: `refactor: collapse settings fields in AgentMenuDeps to SettingsManager`
|
|
228
|
+
|
|
229
|
+
### Cycle 4: Narrow AgentToolDeps — replace getDefaultMaxTurns
|
|
230
|
+
|
|
231
|
+
1. Red: update `makeDeps` in `agent-tool.test.ts` — replace `getDefaultMaxTurns` with `settings: { defaultMaxTurns: undefined }`.
|
|
232
|
+
2. Green: update `AgentToolDeps` interface; update `createAgentTool` to read `deps.settings.defaultMaxTurns`.
|
|
233
|
+
3. Run `pnpm run check`.
|
|
234
|
+
4. Commit: `refactor: replace getDefaultMaxTurns with settings in AgentToolDeps`
|
|
235
|
+
|
|
236
|
+
### Cycle 5: Move maxConcurrent from AgentManager to SettingsManager
|
|
237
|
+
|
|
238
|
+
1. Red: update `agent-manager.test.ts` — replace `maxConcurrent` option with `getMaxConcurrent` function; replace `setMaxConcurrent` calls with `notifyConcurrencyChanged`.
|
|
239
|
+
2. Green: update `AgentManagerOptions` (replace `maxConcurrent?: number` with `getMaxConcurrent?: () => number`), update constructor, replace `private maxConcurrent` with `private readonly getMaxConcurrent`, rename `setMaxConcurrent` → `notifyConcurrencyChanged`.
|
|
240
|
+
3. Run `pnpm run check`.
|
|
241
|
+
4. Commit: `refactor: AgentManager reads maxConcurrent from SettingsManager`
|
|
242
|
+
|
|
243
|
+
### Cycle 6: Wire SettingsManager in index.ts
|
|
244
|
+
|
|
245
|
+
1. Update `index.ts`: create `SettingsManager` before `AgentManager`; call `.load()`; pass to all consumers; remove `applyAndEmitLoaded` call and ad-hoc callback closures.
|
|
246
|
+
2. Run full test suite.
|
|
247
|
+
3. Commit: `refactor: wire SettingsManager in extension init`
|
|
248
|
+
|
|
249
|
+
### Cycle 7: Remove SubagentRuntime settings fields
|
|
250
|
+
|
|
251
|
+
1. Remove `defaultMaxTurns` and `graceTurns` from `SubagentRuntime`.
|
|
252
|
+
2. Remove `RunConfig` interface from `runtime.ts` (import from `settings.ts` if still needed, or inline).
|
|
253
|
+
3. Run `pnpm run check`.
|
|
254
|
+
4. Commit: `refactor: remove settings fields from SubagentRuntime`
|
|
255
|
+
|
|
256
|
+
### Cycle 8: Remove superseded free functions and types
|
|
257
|
+
|
|
258
|
+
1. Remove `SettingsAppliers`, `applySettings`, `applyAndEmitLoaded`, `saveAndEmitChanged` from `settings.ts`.
|
|
259
|
+
2. Remove corresponding test sections from `settings.test.ts`.
|
|
260
|
+
3. Make `loadSettings`, `saveSettings` unexported if no external consumers remain (keep exported if tests import them directly for the sanitizer/IO tests).
|
|
261
|
+
4. Run full test suite.
|
|
262
|
+
5. Commit: `refactor: remove superseded settings free functions and SettingsAppliers`
|
|
263
|
+
|
|
264
|
+
## Risks and Mitigations
|
|
265
|
+
|
|
266
|
+
| Risk | Mitigation |
|
|
267
|
+
| ----------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
268
|
+
| `maxConcurrent` ownership transfer breaks queue drain timing | `notifyConcurrencyChanged()` preserves the drain-on-change behavior; `AgentManager` reads via function on every queue decision, so the value is always current. |
|
|
269
|
+
| Large mock updates in agent-menu.test.ts cause merge conflicts | Lift-and-shift: cycles 1–2 add the new class without touching existing code; cycles 3–5 migrate one consumer at a time. |
|
|
270
|
+
| `normalizeMaxTurns` logic duplicated between setter and agent-runner.ts | The setter inlines trivial normalization (`0 → undefined`, else `max(1, n)`); `normalizeMaxTurns` stays in `agent-runner.ts` for per-invocation use. Both are simple enough that duplication is cheaper than a shared dependency. |
|
|
271
|
+
| `RunConfig` removal from `runtime.ts` breaks imports | Grep all `RunConfig` imports before removing; move the type to `settings.ts` or inline at the use site if needed. |
|
|
272
|
+
|
|
273
|
+
## Open Questions
|
|
274
|
+
|
|
275
|
+
- Should `loadSettings` and `saveSettings` remain exported (for the standalone sanitizer/IO tests) or become private to the module?
|
|
276
|
+
Defer until cycle 8 — check whether any test imports them directly.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
---
|
|
2
|
+
issue: 108
|
|
3
|
+
issue_title: "refactor(pi-subagents): extract AgentTypeRegistry class from module-scoped state"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Retro: #108 — extract AgentTypeRegistry class
|
|
7
|
+
|
|
8
|
+
## Final Retrospective (2026-05-21T13:30:00Z)
|
|
9
|
+
|
|
10
|
+
### Session summary
|
|
11
|
+
|
|
12
|
+
Planned and implemented the `AgentTypeRegistry` class extraction from module-scoped state in `agent-types.ts`.
|
|
13
|
+
The lift-and-shift approach across 7 TDD steps (9 commits including plan and docs) migrated 11 source files and 11 test files while keeping 574 tests green at every commit.
|
|
14
|
+
The `reloadCustomAgents` callback was removed from `AgentToolDeps` and `AgentMenuDeps`, replaced by `deps.registry.reload()`.
|
|
15
|
+
|
|
16
|
+
### Observations
|
|
17
|
+
|
|
18
|
+
#### What went well
|
|
19
|
+
|
|
20
|
+
- The `AgentConfigLookup` narrow interface (ISP) for `session-config.ts` kept tests simple — plain objects with 2 methods, no class instantiation needed.
|
|
21
|
+
- Using `vi.spyOn` on a real `AgentTypeRegistry` instance in `agent-menu.test.ts` was cleaner than `vi.hoisted` + `vi.fn()` factories: correct types, automatic cleanup via `vi.restoreAllMocks()`.
|
|
22
|
+
- Step 2 (inject through 4-file config-assembly chain) was the riskiest single step but landed cleanly because the `vi.mock("agent-types.js")` stubs were narrowed to only the free functions that `session-config.ts` still imports (`getMemoryToolNames`, `getReadOnlyMemoryToolNames`).
|
|
23
|
+
|
|
24
|
+
#### What caused friction (agent side)
|
|
25
|
+
|
|
26
|
+
1. `missing-context` — The plan's "Test files affected" table listed 8 files but missed 3 (`prompts.test.ts`, `tools/get-result-tool.test.ts`, `conversation-viewer.test.ts`) that directly import symbols being removed in step 7.
|
|
27
|
+
The grep during planning found `prompts.test.ts` as an importer of `registerAgents` but didn't include it in the table.
|
|
28
|
+
Impact: 3 extra test files needed updating in step 7; caught by the full-suite run, not by surprise in CI.
|
|
29
|
+
|
|
30
|
+
2. `wrong-abstraction` — First `perl -0777` regex for bulk-updating 16 `ConversationViewer` constructor calls in `conversation-viewer.test.ts` failed because the character class `[^)]+` didn't match the multi-line arguments.
|
|
31
|
+
A simpler pattern targeting just the `vi.fn(),\n );` suffix worked on the second try.
|
|
32
|
+
Impact: added friction but no rework — ~2 minutes.
|
|
33
|
+
|
|
34
|
+
3. `missing-context` — Type check after step 7 revealed `promptMode: string` vs `"replace" | "append"` narrowing issue in `agent-runner-extension-tools.test.ts`.
|
|
35
|
+
The `agentConfigMock.current` object had `promptMode: "replace"` which TypeScript widened to `string` when spread into the mock `AgentConfigLookup` return.
|
|
36
|
+
Impact: one additional edit with a return-type annotation; caught by `pnpm run check` as recommended by the testing skill.
|
|
37
|
+
|
|
38
|
+
#### What caused friction (user side)
|
|
39
|
+
|
|
40
|
+
- No user-side friction observed.
|
|
41
|
+
The issue description was clear, the architecture doc had the design already sketched, and no mid-session redirects were needed.
|