@gotgenes/pi-subagents 6.8.3 → 6.9.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 +14 -0
- package/docs/architecture/architecture.md +23 -18
- package/docs/plans/0114-narrow-agent-tool-menu-deps.md +279 -0
- package/docs/retro/0113-disambiguate-spawn-options.md +29 -0
- package/package.json +1 -1
- package/src/agent-manager.ts +6 -0
- package/src/index.ts +9 -31
- package/src/tools/agent-tool.ts +16 -15
- package/src/tools/helpers.ts +38 -0
- package/src/ui/agent-menu.ts +9 -2
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [6.9.0](https://github.com/gotgenes/pi-packages/compare/pi-subagents-v6.8.3...pi-subagents-v6.9.0) (2026-05-22)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* add onAgentCreated to AgentManagerObserver ([e2f1c12](https://github.com/gotgenes/pi-packages/commit/e2f1c1273ce560231cc98ef3c7e214efd2c20f47))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Documentation
|
|
17
|
+
|
|
18
|
+
* mark D2 done, update field counts in architecture.md ([#114](https://github.com/gotgenes/pi-packages/issues/114)) ([702caf4](https://github.com/gotgenes/pi-packages/commit/702caf4187bfa015b0d80361b06b6360fddc31b1))
|
|
19
|
+
* plan narrow AgentToolDeps and AgentMenuDeps ([#114](https://github.com/gotgenes/pi-packages/issues/114)) ([0f7e953](https://github.com/gotgenes/pi-packages/commit/0f7e95362043109c4113cfe3f7b848e296f118c9))
|
|
20
|
+
* **retro:** add retro notes for issue [#113](https://github.com/gotgenes/pi-packages/issues/113) ([6b3c280](https://github.com/gotgenes/pi-packages/commit/6b3c2800e6d7728b8b620172e4acb1068b65e6c4))
|
|
21
|
+
|
|
8
22
|
## [6.8.3](https://github.com/gotgenes/pi-packages/compare/pi-subagents-v6.8.2...pi-subagents-v6.8.3) (2026-05-22)
|
|
9
23
|
|
|
10
24
|
|
|
@@ -377,17 +377,17 @@ Each step is sequenced so it makes the next step easier.
|
|
|
377
377
|
|
|
378
378
|
### Current smells
|
|
379
379
|
|
|
380
|
-
| Smell | Location
|
|
381
|
-
| ------------------------------ |
|
|
382
|
-
| ~~Global mutable state~~ | ~~`agent-types.ts`~~
|
|
383
|
-
| Closure bag as class | `createNotificationSystem()`
|
|
384
|
-
| ~~Mutable state bag~~ | ~~`AgentActivity` (7 fields)~~
|
|
385
|
-
| ~~Settings relay~~ | ~~`AgentMenuDeps` (13 fields)~~
|
|
386
|
-
| ~~Post-construction mutation~~ | ~~`AgentRecord` non-transition state~~
|
|
387
|
-
| ~~Fire-and-forget callbacks~~ | ~~`AgentManagerOptions`~~
|
|
388
|
-
| ~~Duplicate `SpawnOptions`~~ | ~~`service.ts` + `agent-manager.ts`~~
|
|
389
|
-
| Type dumping ground | `types.ts`
|
|
390
|
-
| Wide dependency bags
|
|
380
|
+
| Smell | Location | Evidence |
|
|
381
|
+
| ------------------------------ | -------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
382
|
+
| ~~Global mutable state~~ | ~~`agent-types.ts`~~ | **Fixed #108**: `AgentTypeRegistry` class; `reloadCustomAgents` callback removed from `AgentToolDeps` and `AgentMenuDeps` |
|
|
383
|
+
| Closure bag as class | `createNotificationSystem()` | Returns 4 functions sharing closure state (`pendingNudges`, timers) |
|
|
384
|
+
| ~~Mutable state bag~~ | ~~`AgentActivity` (7 fields)~~ | **Fixed #110**: `AgentActivityTracker` class; `ui-observer.ts` calls transition methods; widget, notification, agent-tool use read-only accessors |
|
|
385
|
+
| ~~Settings relay~~ | ~~`AgentMenuDeps` (13 fields)~~ | **Fixed #109**: `SettingsManager` class; 6 callback fields collapsed to `settings: SettingsManager`; `AgentMenuDeps` now 8 fields |
|
|
386
|
+
| ~~Post-construction mutation~~ | ~~`AgentRecord` non-transition state~~ | **Fixed #111**: `ExecutionState`, `WorktreeState`, `NotificationState` collaborators; `pendingSteers` moved to `AgentManager`; stats encapsulated behind mutation methods |
|
|
387
|
+
| ~~Fire-and-forget callbacks~~ | ~~`AgentManagerOptions`~~ | **Fixed #112**: `AgentManagerObserver` interface; `observer` replaces 3 callbacks; `index.ts` constructs one observer object instead of 3 closure lambdas |
|
|
388
|
+
| ~~Duplicate `SpawnOptions`~~ | ~~`service.ts` + `agent-manager.ts`~~ | **Fixed #113**: internal type renamed to `AgentSpawnConfig`; public `SpawnOptions` in `service.ts` unchanged |
|
|
389
|
+
| Type dumping ground | `types.ts` | `NotificationDetails` used only by notification/renderer; ~~`DEFAULT_AGENT_NAMES` moved to `AgentTypeRegistry` (#108)~~; `AgentConfig` (21 fields) consumers use 2–4 each |
|
|
390
|
+
| ~~Wide dependency bags~~ | ~~`AgentToolDeps` (9), `AgentMenuDeps` (8)~~ | **Fixed #114**: `AgentToolDeps` 9 → 6; `AgentMenuDeps` 8 → 7; `emitEvent` removed from both; description text derived from registry; `agentActivity` narrowed to typed interfaces |
|
|
391
391
|
|
|
392
392
|
### Step A: Extract state into classes (foundation, parallel)
|
|
393
393
|
|
|
@@ -462,12 +462,17 @@ With the registry class, settings manager, and observer in place, the dependency
|
|
|
462
462
|
Internal `SpawnOptions` in `agent-manager.ts` renamed to `AgentSpawnConfig`.
|
|
463
463
|
Public `SpawnOptions` in `service.ts` is unchanged.
|
|
464
464
|
|
|
465
|
-
#### D2. Narrow `AgentToolDeps` and `AgentMenuDeps` (#114)
|
|
465
|
+
#### D2. Narrow `AgentToolDeps` and `AgentMenuDeps` (#114) ✅
|
|
466
466
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
467
|
+
**Done.**
|
|
468
|
+
`AgentToolDeps` 9 → 6: removed `emitEvent` (moved to `AgentManagerObserver.onAgentCreated`), `typeListText`, `availableTypesText` (derived inside `createAgentTool`); `agentActivity` narrowed to `AgentActivityAccess`.
|
|
469
|
+
`AgentMenuDeps` 8 → 7: removed dead `emitEvent` field; `agentActivity` narrowed to `AgentActivityReader`.
|
|
470
|
+
`buildTypeListText` extracted to `tools/helpers.ts`.
|
|
471
|
+
|
|
472
|
+
| Bag | Before | After | How |
|
|
473
|
+
| --------------- | -------- | ----- | ------------------------------------------------------------------------------------------------------------- |
|
|
474
|
+
| `AgentToolDeps` | 9 fields | 6 | `emitEvent` → observer; `typeListText`/`availableTypesText` derived from registry; `agentActivity` narrowed |
|
|
475
|
+
| `AgentMenuDeps` | 8 fields | 7 | Dead `emitEvent` removed; `agentActivity` narrowed to read-only `AgentActivityReader` |
|
|
471
476
|
|
|
472
477
|
### Step E: Decompose large files and relocate types (parallel)
|
|
473
478
|
|
|
@@ -494,8 +499,8 @@ The 654-line file splits along a natural seam.
|
|
|
494
499
|
| Closure-bag "classes" | ~~2~~ 1 (`createNotificationSystem`; settings free functions **fixed #109**) | 0 |
|
|
495
500
|
| Externally-mutated state bags | ~~2~~ ~~1~~ **0** (`AgentRecord` **fixed #111**; `AgentActivity` **fixed #110**) | 0 ✓ |
|
|
496
501
|
| `AgentManagerOptions` fields | 8 | 5 |
|
|
497
|
-
| `AgentToolDeps` fields | ~~9~~ **
|
|
498
|
-
| `AgentMenuDeps` fields | ~~13~~ **
|
|
502
|
+
| `AgentToolDeps` fields | ~~9~~ **6** (−3 text fields derived; −1 emitEvent → observer; activity narrowed) | 6 ✓ |
|
|
503
|
+
| `AgentMenuDeps` fields | ~~13~~ **7** (−6 settings #109; −1 registry #108; −1 dead emitEvent #114) | 7 ✓ |
|
|
499
504
|
| `SpawnOptions` callback fields | 1 (`onSessionCreated`) | 0 |
|
|
500
505
|
| Callbacks threaded through deps | ~~8~~ 0 remaining settings callbacks (**fixed #109**); `emitEvent` ×3 remain | 0 |
|
|
501
506
|
| Types in `types.ts` without a natural home | 4 | 0 |
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
---
|
|
2
|
+
issue: 114
|
|
3
|
+
issue_title: "refactor(pi-subagents): narrow AgentToolDeps and AgentMenuDeps"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Narrow AgentToolDeps and AgentMenuDeps
|
|
7
|
+
|
|
8
|
+
## Problem Statement
|
|
9
|
+
|
|
10
|
+
With the foundational extractions complete — `AgentTypeRegistry` (#108), `SettingsManager` (#109), `AgentActivityTracker` (#110), and `AgentManagerObserver` (#112) — the two widest dependency bags in the extension still carry fields that belong on their collaborators.
|
|
11
|
+
|
|
12
|
+
`AgentToolDeps` has 9 fields: 3 are description-text strings derivable from the registry, 1 is a raw `emitEvent` callback that the observer should own, and the `agentActivity` Map is passed as raw mutable state.
|
|
13
|
+
`AgentMenuDeps` has 8 fields: `emitEvent` is defined but never referenced in the module (dead field).
|
|
14
|
+
|
|
15
|
+
Each excess field inflates test mock construction and obscures the real dependency surface.
|
|
16
|
+
|
|
17
|
+
## Goals
|
|
18
|
+
|
|
19
|
+
- Remove `emitEvent` from both `AgentToolDeps` and `AgentMenuDeps`.
|
|
20
|
+
- Remove `typeListText` and `availableTypesText` from `AgentToolDeps` — derive them from `registry` inside `createAgentTool`.
|
|
21
|
+
- Add `onAgentCreated` to `AgentManagerObserver` so the `subagents:created` event is emitted by the observer, not the tool.
|
|
22
|
+
- Narrow `agentActivity` in both interfaces to a typed read/write interface instead of raw `Map`.
|
|
23
|
+
- Final counts: `AgentToolDeps` 9 → 6, `AgentMenuDeps` 8 → 7.
|
|
24
|
+
|
|
25
|
+
## Non-Goals
|
|
26
|
+
|
|
27
|
+
- Removing `agentDir` from `AgentToolDeps` — it is not derivable from the registry (it comes from the Pi SDK's `getAgentDir()`).
|
|
28
|
+
- Adding presentation methods to `AgentTypeRegistry` — that would mix SRP concerns.
|
|
29
|
+
- Splitting `agent-tool.ts` foreground/background (tracked in #115).
|
|
30
|
+
- Removing `personalAgentsDir` / `projectAgentsDir` from `AgentMenuDeps` — they are genuine menu-level concerns (file management UI).
|
|
31
|
+
|
|
32
|
+
## Background
|
|
33
|
+
|
|
34
|
+
### Prerequisite status
|
|
35
|
+
|
|
36
|
+
All prerequisites are implemented and merged:
|
|
37
|
+
|
|
38
|
+
| Issue | Title | Status |
|
|
39
|
+
| ----- | ---------------------------------------------- | ------- |
|
|
40
|
+
| #108 | Extract `AgentTypeRegistry` | ✅ Done |
|
|
41
|
+
| #109 | Extract `SettingsManager` | ✅ Done |
|
|
42
|
+
| #110 | `AgentActivityTracker` | ✅ Done |
|
|
43
|
+
| #112 | Replace `AgentManager` callbacks with observer | ✅ Done |
|
|
44
|
+
| #113 | Disambiguate `SpawnOptions` | ✅ Done |
|
|
45
|
+
| #118 | `SettingsManager` apply methods | ✅ Done |
|
|
46
|
+
|
|
47
|
+
### Current interfaces
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
// tools/agent-tool.ts — 9 fields
|
|
51
|
+
interface AgentToolDeps {
|
|
52
|
+
manager: AgentToolManager;
|
|
53
|
+
widget: AgentToolWidget;
|
|
54
|
+
agentActivity: Map<string, AgentActivityTracker>;
|
|
55
|
+
emitEvent: (name: string, data: unknown) => void;
|
|
56
|
+
registry: AgentTypeRegistry;
|
|
57
|
+
typeListText: string;
|
|
58
|
+
availableTypesText: string;
|
|
59
|
+
agentDir: string;
|
|
60
|
+
settings: { readonly defaultMaxTurns: number | undefined };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ui/agent-menu.ts — 8 fields
|
|
64
|
+
interface AgentMenuDeps {
|
|
65
|
+
manager: AgentMenuManager;
|
|
66
|
+
registry: AgentTypeRegistry;
|
|
67
|
+
agentActivity: Map<string, AgentActivityTracker>;
|
|
68
|
+
getModelLabel: (type: string, registry?: ModelRegistry) => string;
|
|
69
|
+
settings: AgentMenuSettings;
|
|
70
|
+
emitEvent: (name: string, data: unknown) => void; // ← dead field
|
|
71
|
+
personalAgentsDir: string;
|
|
72
|
+
projectAgentsDir: string;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Relevant design principles
|
|
77
|
+
|
|
78
|
+
- **Dependency width** (code-design skill): do not pass a shared bag to functions that only use a subset.
|
|
79
|
+
- **Output arguments**: `agentActivity` is a raw `Map` mutated via `.set()` and `.delete()` — encapsulate behind methods.
|
|
80
|
+
- **ISP**: consumers should depend on the narrowest interface they need.
|
|
81
|
+
|
|
82
|
+
## Design Overview
|
|
83
|
+
|
|
84
|
+
### 1. Move `subagents:created` to observer
|
|
85
|
+
|
|
86
|
+
The `AgentManagerObserver` interface gains an `onAgentCreated` method.
|
|
87
|
+
`AgentManager.spawn()` calls `observer.onAgentCreated(record)` after creating the record.
|
|
88
|
+
The observer implementation in `index.ts` emits `pi.events.emit("subagents:created", ...)`.
|
|
89
|
+
The tool no longer calls `deps.emitEvent(...)`.
|
|
90
|
+
|
|
91
|
+
The event payload includes `id`, `type`, `description`, and `isBackground`.
|
|
92
|
+
Since `isBackground` is known at spawn-time (it's part of the spawn config), the observer has all needed data.
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
interface AgentManagerObserver {
|
|
96
|
+
onAgentStarted(record: AgentRecord): void;
|
|
97
|
+
onAgentCompleted(record: AgentRecord): void;
|
|
98
|
+
onAgentCompacted(record: AgentRecord, info: CompactionInfo): void;
|
|
99
|
+
onAgentCreated(record: AgentRecord): void; // ← new
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 2. Derive description text from registry
|
|
104
|
+
|
|
105
|
+
Move `buildTypeListText()` from `index.ts` to `tools/helpers.ts` as a pure function that accepts the registry and `agentDir`:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
function buildTypeListText(
|
|
109
|
+
registry: AgentConfigLookup & { getDefaultAgentNames(): string[]; getUserAgentNames(): string[] },
|
|
110
|
+
agentDir: string,
|
|
111
|
+
): string;
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Inside `createAgentTool`, compute both strings from `deps.registry` and `deps.agentDir`:
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
const typeListText = buildTypeListText(deps.registry, deps.agentDir);
|
|
118
|
+
const availableTypesText = deps.registry.getAvailableTypes().join(", ");
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
This removes `typeListText` and `availableTypesText` from the interface.
|
|
122
|
+
The helper requires `getModelLabelFromConfig`, which already lives in `tools/helpers.ts`.
|
|
123
|
+
|
|
124
|
+
### 3. Narrow agentActivity to a typed interface
|
|
125
|
+
|
|
126
|
+
Instead of `Map<string, AgentActivityTracker>`, both interfaces accept a narrow typed interface matching their actual usage:
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
/** Read/write interface for agent-tool's activity tracking needs. */
|
|
130
|
+
interface AgentActivityAccess {
|
|
131
|
+
get(id: string): AgentActivityTracker | undefined;
|
|
132
|
+
set(id: string, tracker: AgentActivityTracker): void;
|
|
133
|
+
delete(id: string): void;
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The `AgentMenuDeps` only reads (`.get()`), so it gets a narrower read-only type:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
/** Read-only interface for menu's conversation viewer. */
|
|
141
|
+
interface AgentActivityReader {
|
|
142
|
+
get(id: string): AgentActivityTracker | undefined;
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The runtime's `Map<string, AgentActivityTracker>` satisfies both interfaces structurally.
|
|
147
|
+
No wrapper class is needed — `Map` already implements `get/set/delete`.
|
|
148
|
+
The benefit is that the type signature communicates the actual usage pattern.
|
|
149
|
+
|
|
150
|
+
### 4. Remove dead `emitEvent` from `AgentMenuDeps`
|
|
151
|
+
|
|
152
|
+
This field is defined in the interface but never referenced inside `agent-menu.ts`.
|
|
153
|
+
Remove it from the interface and from the construction site in `index.ts`.
|
|
154
|
+
|
|
155
|
+
### Final interfaces
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
// tools/agent-tool.ts — 6 fields (was 9)
|
|
159
|
+
interface AgentToolDeps {
|
|
160
|
+
manager: AgentToolManager;
|
|
161
|
+
widget: AgentToolWidget;
|
|
162
|
+
agentActivity: AgentActivityAccess;
|
|
163
|
+
registry: AgentTypeRegistry;
|
|
164
|
+
agentDir: string;
|
|
165
|
+
settings: { readonly defaultMaxTurns: number | undefined };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ui/agent-menu.ts — 7 fields (was 8)
|
|
169
|
+
interface AgentMenuDeps {
|
|
170
|
+
manager: AgentMenuManager;
|
|
171
|
+
registry: AgentTypeRegistry;
|
|
172
|
+
agentActivity: AgentActivityReader;
|
|
173
|
+
getModelLabel: (type: string, registry?: ModelRegistry) => string;
|
|
174
|
+
settings: AgentMenuSettings;
|
|
175
|
+
personalAgentsDir: string;
|
|
176
|
+
projectAgentsDir: string;
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Module-Level Changes
|
|
181
|
+
|
|
182
|
+
### Modified files
|
|
183
|
+
|
|
184
|
+
1. **`src/agent-manager.ts`** — Call `observer.onAgentCreated(record)` in `spawn()` after creating the record.
|
|
185
|
+
Update `AgentManagerObserver` interface to include `onAgentCreated`.
|
|
186
|
+
|
|
187
|
+
2. **`src/tools/agent-tool.ts`** — Remove `emitEvent`, `typeListText`, `availableTypesText` from `AgentToolDeps`.
|
|
188
|
+
Add `AgentActivityAccess` interface.
|
|
189
|
+
Derive description text from `registry` + `agentDir` inside `createAgentTool`.
|
|
190
|
+
Remove `deps.emitEvent(...)` call.
|
|
191
|
+
Replace `Map<string, AgentActivityTracker>` with `AgentActivityAccess`.
|
|
192
|
+
|
|
193
|
+
3. **`src/tools/helpers.ts`** — Add `buildTypeListText(registry, agentDir)` extracted from `index.ts`.
|
|
194
|
+
|
|
195
|
+
4. **`src/ui/agent-menu.ts`** — Remove `emitEvent` from `AgentMenuDeps`.
|
|
196
|
+
Add `AgentActivityReader` interface.
|
|
197
|
+
Replace `Map<string, AgentActivityTracker>` with `AgentActivityReader`.
|
|
198
|
+
|
|
199
|
+
5. **`src/index.ts`** — Remove `buildTypeListText` closure.
|
|
200
|
+
Remove `typeListText`, `availableTypesText`, `emitEvent` from the `AgentToolDeps` construction.
|
|
201
|
+
Remove `emitEvent` from the `AgentMenuDeps` construction.
|
|
202
|
+
Add `onAgentCreated` to the observer object.
|
|
203
|
+
|
|
204
|
+
6. **`test/tools/agent-tool.test.ts`** — Update `makeDeps` factory: remove `emitEvent`, `typeListText`, `availableTypesText`.
|
|
205
|
+
Update assertions that check `emitEvent` was called.
|
|
206
|
+
|
|
207
|
+
7. **`test/ui/agent-menu.test.ts`** — Update `makeDeps` factory: remove `emitEvent`.
|
|
208
|
+
|
|
209
|
+
8. **`test/agent-manager.test.ts`** — Add test for `observer.onAgentCreated` being called during spawn.
|
|
210
|
+
|
|
211
|
+
## Test Impact Analysis
|
|
212
|
+
|
|
213
|
+
1. **New tests enabled**: `observer.onAgentCreated` can be tested in `agent-manager.test.ts` — verify it fires during `spawn()` with the correct record.
|
|
214
|
+
|
|
215
|
+
2. **Tests that simplify**: `agent-tool.test.ts` mock factory drops 3 fields (`emitEvent`, `typeListText`, `availableTypesText`).
|
|
216
|
+
Tests asserting `deps.emitEvent` was called become assertions that the observer was invoked (tested at the manager level instead).
|
|
217
|
+
`agent-menu.test.ts` mock factory drops 1 field (`emitEvent`).
|
|
218
|
+
|
|
219
|
+
3. **Tests that stay**: all tool-behavior tests (spawn paths, resume, error handling) remain — they test the tool's own logic, not the narrowed plumbing.
|
|
220
|
+
|
|
221
|
+
## TDD Order
|
|
222
|
+
|
|
223
|
+
1. **Add `onAgentCreated` to observer interface and manager.**
|
|
224
|
+
Add the method to `AgentManagerObserver`.
|
|
225
|
+
Call it in `AgentManager.spawn()`.
|
|
226
|
+
Test: verify `observer.onAgentCreated` fires with the created record.
|
|
227
|
+
Commit: `feat: add onAgentCreated to AgentManagerObserver`
|
|
228
|
+
|
|
229
|
+
2. **Extract `buildTypeListText` to helpers.**
|
|
230
|
+
Move the function from `index.ts` to `tools/helpers.ts` as a pure function.
|
|
231
|
+
Test: unit test `buildTypeListText` with a mock registry.
|
|
232
|
+
Update `index.ts` to import and call the extracted helper.
|
|
233
|
+
Commit: `refactor: extract buildTypeListText to tools/helpers`
|
|
234
|
+
|
|
235
|
+
3. **Derive description text inside `createAgentTool`.**
|
|
236
|
+
Remove `typeListText` and `availableTypesText` from `AgentToolDeps`.
|
|
237
|
+
Compute them from `deps.registry` + `deps.agentDir` inside `createAgentTool`.
|
|
238
|
+
Update `index.ts` construction site.
|
|
239
|
+
Update `agent-tool.test.ts` mock factory.
|
|
240
|
+
Test: verify tool description still contains expected text.
|
|
241
|
+
Commit: `refactor: derive description text from registry in createAgentTool`
|
|
242
|
+
|
|
243
|
+
4. **Remove `emitEvent` from `AgentToolDeps`.**
|
|
244
|
+
Remove the field from the interface.
|
|
245
|
+
Remove the `deps.emitEvent(...)` call in the background spawn path.
|
|
246
|
+
Wire `onAgentCreated` in the `index.ts` observer to emit `subagents:created`.
|
|
247
|
+
Update `agent-tool.test.ts` (remove `emitEvent` from factory, remove/update related assertions).
|
|
248
|
+
Commit: `refactor: remove emitEvent from AgentToolDeps`
|
|
249
|
+
|
|
250
|
+
5. **Remove dead `emitEvent` from `AgentMenuDeps`.**
|
|
251
|
+
Remove the field from the interface.
|
|
252
|
+
Remove from `index.ts` construction site.
|
|
253
|
+
Update `agent-menu.test.ts` mock factory.
|
|
254
|
+
Commit: `refactor: remove dead emitEvent from AgentMenuDeps`
|
|
255
|
+
|
|
256
|
+
6. **Narrow `agentActivity` to typed interfaces.**
|
|
257
|
+
Add `AgentActivityAccess` interface to `agent-tool.ts`.
|
|
258
|
+
Add `AgentActivityReader` interface to `agent-menu.ts`.
|
|
259
|
+
Replace `Map<string, AgentActivityTracker>` with the narrow type in both interfaces.
|
|
260
|
+
Update test factories (no functional change — `Map` satisfies both interfaces).
|
|
261
|
+
Commit: `refactor: narrow agentActivity to typed interfaces`
|
|
262
|
+
|
|
263
|
+
## Risks and Mitigations
|
|
264
|
+
|
|
265
|
+
| Risk | Mitigation |
|
|
266
|
+
| --------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
267
|
+
| `subagents:created` event data changes shape when moved to observer | Observer receives the full `AgentRecord` which contains `id`, `type`, `description`. `isBackground` must be derivable from the record or spawn config — verify the record carries this at spawn time. |
|
|
268
|
+
| `buildTypeListText` depends on `getModelLabelFromConfig` which is in `tools/helpers.ts` | This is already the target file — no circular dependency risk. |
|
|
269
|
+
| Third-party consumers rely on `emitEvent` presence in `AgentToolDeps` | `AgentToolDeps` is not in the public `exports` — it's internal. No external API breakage. |
|
|
270
|
+
| `AgentActivityAccess` interface is structurally identical to a subset of `Map` | This is intentional — the `Map` satisfies it without a wrapper. If a non-Map implementation is needed later, the interface is already in place. |
|
|
271
|
+
|
|
272
|
+
## Open Questions
|
|
273
|
+
|
|
274
|
+
1. Should `onAgentCreated` receive the full `AgentRecord` or a narrow payload?
|
|
275
|
+
The other observer methods receive `AgentRecord` — consistency suggests the same.
|
|
276
|
+
The `isBackground` flag is needed for the event payload; verify it's available on the record at creation time (it should be, since background is set at spawn).
|
|
277
|
+
2. Should `steer-tool.ts`'s `emitEvent` (emits `subagents:steered`) also move to the observer?
|
|
278
|
+
It's a separate concern (tool-level steering) and a different event.
|
|
279
|
+
Defer to a follow-up if desired.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
---
|
|
2
|
+
issue: 113
|
|
3
|
+
issue_title: "refactor(pi-subagents): disambiguate SpawnOptions (public vs internal)"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Retro: #113 — disambiguate SpawnOptions (public vs internal)
|
|
7
|
+
|
|
8
|
+
## Final Retrospective (2026-05-21T21:10:00-04:00)
|
|
9
|
+
|
|
10
|
+
### Session summary
|
|
11
|
+
|
|
12
|
+
Renamed the internal `SpawnOptions` in `agent-manager.ts` to `AgentSpawnConfig` to disambiguate it from the public `SpawnOptions` in `service.ts`.
|
|
13
|
+
Pure mechanical rename across 4 files with zero test-count delta (652/652).
|
|
14
|
+
Released as `pi-subagents-v6.8.3`.
|
|
15
|
+
|
|
16
|
+
### Observations
|
|
17
|
+
|
|
18
|
+
#### What went well
|
|
19
|
+
|
|
20
|
+
- Completely frictionless execution — single-step plan executed exactly as written with no corrections, rework, or failed edits.
|
|
21
|
+
- The session benefited from context already loaded during the preceding #112 cycle (same package, same skills, same source files), which made planning and execution faster.
|
|
22
|
+
|
|
23
|
+
#### What caused friction (agent side)
|
|
24
|
+
|
|
25
|
+
- Nothing — the rename was purely mechanical and the plan matched reality exactly.
|
|
26
|
+
|
|
27
|
+
#### What caused friction (user side)
|
|
28
|
+
|
|
29
|
+
- Nothing — no user intervention needed.
|
package/package.json
CHANGED
package/src/agent-manager.ts
CHANGED
|
@@ -27,6 +27,8 @@ export interface AgentManagerObserver {
|
|
|
27
27
|
onAgentStarted(record: AgentRecord): void;
|
|
28
28
|
onAgentCompleted(record: AgentRecord): void;
|
|
29
29
|
onAgentCompacted(record: AgentRecord, info: CompactionInfo): void;
|
|
30
|
+
/** Fires synchronously after a background agent record is created (before startAgent). */
|
|
31
|
+
onAgentCreated(record: AgentRecord): void;
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
/** Default max concurrent background agents. */
|
|
@@ -153,6 +155,10 @@ export class AgentManager {
|
|
|
153
155
|
});
|
|
154
156
|
this.agents.set(id, record);
|
|
155
157
|
|
|
158
|
+
if (options.isBackground) {
|
|
159
|
+
this.observer?.onAgentCreated(record);
|
|
160
|
+
}
|
|
161
|
+
|
|
156
162
|
const snapshot = buildParentSnapshot(ctx, options.inheritContext);
|
|
157
163
|
const args: SpawnArgs = { snapshot, type, prompt, options };
|
|
158
164
|
|
package/src/index.ts
CHANGED
|
@@ -110,6 +110,15 @@ export default function (pi: ExtensionAPI) {
|
|
|
110
110
|
compactionCount: record.compactionCount,
|
|
111
111
|
});
|
|
112
112
|
},
|
|
113
|
+
onAgentCreated(record) {
|
|
114
|
+
// Emit created event for background agents (before startAgent / queue drain).
|
|
115
|
+
pi.events.emit("subagents:created", {
|
|
116
|
+
id: record.id,
|
|
117
|
+
type: record.type,
|
|
118
|
+
description: record.description,
|
|
119
|
+
isBackground: true,
|
|
120
|
+
});
|
|
121
|
+
},
|
|
113
122
|
};
|
|
114
123
|
|
|
115
124
|
const manager = new AgentManager({
|
|
@@ -151,33 +160,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
151
160
|
const toolStart = new ToolStartHandler(runtime);
|
|
152
161
|
pi.on("tool_execution_start", (event, ctx) => toolStart.handleToolExecutionStart(event, ctx));
|
|
153
162
|
|
|
154
|
-
/** Build the full type list text dynamically from the unified registry. */
|
|
155
|
-
const buildTypeListText = () => {
|
|
156
|
-
const defaultNames = registry.getDefaultAgentNames();
|
|
157
|
-
const userNames = registry.getUserAgentNames();
|
|
158
|
-
|
|
159
|
-
const defaultDescs = defaultNames.map((name) => {
|
|
160
|
-
const cfg = registry.resolveAgentConfig(name);
|
|
161
|
-
const modelSuffix = cfg.model ? ` (${getModelLabelFromConfig(cfg.model)})` : "";
|
|
162
|
-
return `- ${name}: ${cfg.description}${modelSuffix}`;
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const customDescs = userNames.map((name) => {
|
|
166
|
-
const cfg = registry.resolveAgentConfig(name);
|
|
167
|
-
return `- ${name}: ${cfg.description}`;
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
return [
|
|
171
|
-
"Default agents:",
|
|
172
|
-
...defaultDescs,
|
|
173
|
-
...(customDescs.length > 0 ? ["", "Custom agents:", ...customDescs] : []),
|
|
174
|
-
"",
|
|
175
|
-
`Custom agents can be defined in .pi/agents/<name>.md (project) or ${getAgentDir()}/agents/<name>.md (global) — they are picked up automatically. Project-level agents override global ones. Creating a .md file with the same name as a default agent overrides it.`,
|
|
176
|
-
].join("\n");
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
const typeListText = buildTypeListText();
|
|
180
|
-
|
|
181
163
|
// ---- Agent tool ----
|
|
182
164
|
|
|
183
165
|
pi.registerTool(defineTool(createAgentTool({
|
|
@@ -196,10 +178,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
196
178
|
markFinished: (id) => runtime.markFinished(id),
|
|
197
179
|
},
|
|
198
180
|
agentActivity: runtime.agentActivity,
|
|
199
|
-
emitEvent: (name, data) => pi.events.emit(name, data),
|
|
200
181
|
registry,
|
|
201
|
-
typeListText,
|
|
202
|
-
availableTypesText: registry.getAvailableTypes().join(", "),
|
|
203
182
|
agentDir: getAgentDir(),
|
|
204
183
|
settings,
|
|
205
184
|
})));
|
|
@@ -242,7 +221,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
242
221
|
return getModelLabelFromConfig(cfg.model);
|
|
243
222
|
},
|
|
244
223
|
settings,
|
|
245
|
-
emitEvent: (name, data) => pi.events.emit(name, data),
|
|
246
224
|
personalAgentsDir: join(getAgentDir(), 'agents'),
|
|
247
225
|
projectAgentsDir: join(process.cwd(), '.pi', 'agents'),
|
|
248
226
|
});
|
package/src/tools/agent-tool.ts
CHANGED
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
} from "../ui/agent-widget.js";
|
|
24
24
|
import { subscribeUIObserver } from "../ui/ui-observer.js";
|
|
25
25
|
import type { LifetimeUsage } from "../usage.js";
|
|
26
|
-
import { formatLifetimeTokens, textResult } from "./helpers.js";
|
|
26
|
+
import { buildTypeListText, formatLifetimeTokens, textResult } from "./helpers.js";
|
|
27
27
|
|
|
28
28
|
// ---- Agent-tool-specific helpers ----
|
|
29
29
|
|
|
@@ -91,14 +91,21 @@ export interface AgentToolWidget {
|
|
|
91
91
|
markFinished: (id: string) => void;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Narrow read/write interface for the agent-tool's agentActivity access.
|
|
96
|
+
* The full Map satisfies this structurally — no wrapper needed.
|
|
97
|
+
*/
|
|
98
|
+
export interface AgentActivityAccess {
|
|
99
|
+
get(id: string): AgentActivityTracker | undefined;
|
|
100
|
+
set(id: string, tracker: AgentActivityTracker): void;
|
|
101
|
+
delete(id: string): void;
|
|
102
|
+
}
|
|
103
|
+
|
|
94
104
|
export interface AgentToolDeps {
|
|
95
105
|
manager: AgentToolManager;
|
|
96
106
|
widget: AgentToolWidget;
|
|
97
|
-
agentActivity:
|
|
98
|
-
emitEvent: (name: string, data: unknown) => void;
|
|
107
|
+
agentActivity: AgentActivityAccess;
|
|
99
108
|
registry: AgentTypeRegistry;
|
|
100
|
-
typeListText: string;
|
|
101
|
-
availableTypesText: string;
|
|
102
109
|
agentDir: string;
|
|
103
110
|
/** Narrow settings accessor — only the default max turns is needed here. */
|
|
104
111
|
settings: { readonly defaultMaxTurns: number | undefined };
|
|
@@ -108,6 +115,8 @@ export interface AgentToolDeps {
|
|
|
108
115
|
|
|
109
116
|
/** Create the Agent tool definition (without Pi SDK wrapper). */
|
|
110
117
|
export function createAgentTool(deps: AgentToolDeps) {
|
|
118
|
+
const typeListText = buildTypeListText(deps.registry, deps.agentDir);
|
|
119
|
+
const availableTypesText = deps.registry.getAvailableTypes().join(", ");
|
|
111
120
|
return {
|
|
112
121
|
name: "Agent" as const,
|
|
113
122
|
label: "Agent",
|
|
@@ -116,7 +125,7 @@ export function createAgentTool(deps: AgentToolDeps) {
|
|
|
116
125
|
The Agent tool launches specialized agents that autonomously handle complex tasks. Each agent type has specific capabilities and tools available to it.
|
|
117
126
|
|
|
118
127
|
Available agent types:
|
|
119
|
-
${
|
|
128
|
+
${typeListText}
|
|
120
129
|
|
|
121
130
|
Guidelines:
|
|
122
131
|
- For parallel work, use run_in_background: true on each agent. Foreground calls run sequentially — only one executes at a time.
|
|
@@ -140,7 +149,7 @@ Guidelines:
|
|
|
140
149
|
description: "A short (3-5 word) description of the task (shown in UI).",
|
|
141
150
|
}),
|
|
142
151
|
subagent_type: Type.String({
|
|
143
|
-
description: `The type of specialized agent to use. Available types: ${
|
|
152
|
+
description: `The type of specialized agent to use. Available types: ${availableTypesText}. Custom agents from .pi/agents/<name>.md (project) or ${deps.agentDir}/agents/<name>.md (global) are also available.`,
|
|
144
153
|
}),
|
|
145
154
|
model: Type.Optional(
|
|
146
155
|
Type.String({
|
|
@@ -439,14 +448,6 @@ Guidelines:
|
|
|
439
448
|
deps.widget.ensureTimer();
|
|
440
449
|
deps.widget.update();
|
|
441
450
|
|
|
442
|
-
// Emit created event
|
|
443
|
-
deps.emitEvent("subagents:created", {
|
|
444
|
-
id,
|
|
445
|
-
type: subagentType,
|
|
446
|
-
description: params.description,
|
|
447
|
-
isBackground: true,
|
|
448
|
-
});
|
|
449
|
-
|
|
450
451
|
const isQueued = record?.status === "queued";
|
|
451
452
|
return textResult(
|
|
452
453
|
`Agent ${isQueued ? "queued" : "started"} in background.\n` +
|
package/src/tools/helpers.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AgentConfigLookup } from "../agent-types.js";
|
|
1
2
|
import { formatTokens } from "../ui/agent-widget.js";
|
|
2
3
|
import { getLifetimeTotal, type LifetimeUsage } from "../usage.js";
|
|
3
4
|
|
|
@@ -12,6 +13,43 @@ export function formatLifetimeTokens(o: { lifetimeUsage: LifetimeUsage }): strin
|
|
|
12
13
|
return t > 0 ? formatTokens(t) : "";
|
|
13
14
|
}
|
|
14
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Narrow registry interface needed by buildTypeListText.
|
|
18
|
+
* Extends AgentConfigLookup with the two name-listing methods.
|
|
19
|
+
*/
|
|
20
|
+
export interface TypeListRegistry extends AgentConfigLookup {
|
|
21
|
+
getDefaultAgentNames(): string[];
|
|
22
|
+
getUserAgentNames(): string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Build the full agent-type list text for the Agent tool description.
|
|
27
|
+
* Extracted from index.ts so it can be called inside createAgentTool.
|
|
28
|
+
*/
|
|
29
|
+
export function buildTypeListText(registry: TypeListRegistry, agentDir: string): string {
|
|
30
|
+
const defaultNames = registry.getDefaultAgentNames();
|
|
31
|
+
const userNames = registry.getUserAgentNames();
|
|
32
|
+
|
|
33
|
+
const defaultDescs = defaultNames.map((name) => {
|
|
34
|
+
const cfg = registry.resolveAgentConfig(name);
|
|
35
|
+
const modelSuffix = cfg.model ? ` (${getModelLabelFromConfig(cfg.model)})` : "";
|
|
36
|
+
return `- ${name}: ${cfg.description}${modelSuffix}`;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const customDescs = userNames.map((name) => {
|
|
40
|
+
const cfg = registry.resolveAgentConfig(name);
|
|
41
|
+
return `- ${name}: ${cfg.description}`;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return [
|
|
45
|
+
"Default agents:",
|
|
46
|
+
...defaultDescs,
|
|
47
|
+
...(customDescs.length > 0 ? ["", "Custom agents:", ...customDescs] : []),
|
|
48
|
+
"",
|
|
49
|
+
`Custom agents can be defined in .pi/agents/<name>.md (project) or ${agentDir}/agents/<name>.md (global) — they are picked up automatically. Project-level agents override global ones. Creating a .md file with the same name as a default agent overrides it.`,
|
|
50
|
+
].join("\n");
|
|
51
|
+
}
|
|
52
|
+
|
|
15
53
|
/** Derive a short model label from a model string. */
|
|
16
54
|
export function getModelLabelFromConfig(model: string): string {
|
|
17
55
|
// Strip provider prefix (e.g. "anthropic/claude-sonnet-4-6" → "claude-sonnet-4-6")
|
package/src/ui/agent-menu.ts
CHANGED
|
@@ -32,15 +32,22 @@ export interface AgentMenuSettings {
|
|
|
32
32
|
applyGraceTurns(n: number): { message: string; level: "info" | "warning" };
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Read-only interface for the agent-menu's agentActivity access.
|
|
37
|
+
* Only the conversation viewer needs to read a tracker by agent ID.
|
|
38
|
+
*/
|
|
39
|
+
export interface AgentActivityReader {
|
|
40
|
+
get(id: string): AgentActivityTracker | undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
35
43
|
export interface AgentMenuDeps {
|
|
36
44
|
manager: AgentMenuManager;
|
|
37
45
|
registry: AgentTypeRegistry;
|
|
38
|
-
agentActivity:
|
|
46
|
+
agentActivity: AgentActivityReader;
|
|
39
47
|
/** Resolve model label for a given agent type + registry. */
|
|
40
48
|
getModelLabel: (type: string, registry?: ModelRegistry) => string;
|
|
41
49
|
/** Settings manager — owns in-memory values and persistence. */
|
|
42
50
|
settings: AgentMenuSettings;
|
|
43
|
-
emitEvent: (name: string, data: unknown) => void;
|
|
44
51
|
personalAgentsDir: string;
|
|
45
52
|
projectAgentsDir: string;
|
|
46
53
|
}
|