@gotgenes/pi-subagents 5.2.0 → 5.4.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 +27 -0
- package/docs/architecture/architecture.md +20 -13
- package/docs/plans/0071-extract-session-config-assembler.md +362 -0
- package/docs/plans/0080-consolidate-agent-config-lookup.md +247 -0
- package/docs/retro/0069-create-subagent-runtime.md +43 -0
- package/docs/retro/0071-extract-session-config-assembler.md +60 -0
- package/package.json +1 -1
- package/src/agent-runner.ts +39 -164
- package/src/agent-types.ts +10 -36
- package/src/index.ts +8 -8
- package/src/session-config.ts +243 -0
- package/src/tools/agent-tool.ts +3 -3
- package/src/ui/agent-menu.ts +11 -10
- package/src/ui/agent-widget.ts +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,33 @@ 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
|
+
## [5.4.0](https://github.com/gotgenes/pi-packages/compare/pi-subagents-v5.3.0...pi-subagents-v5.4.0) (2026-05-20)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* add resolveAgentConfig with guaranteed-non-null fallback chain ([6b676a0](https://github.com/gotgenes/pi-packages/commit/6b676a0b59ebc3598d366cb5db600a8177b301e6))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Documentation
|
|
17
|
+
|
|
18
|
+
* plan consolidate getConfig/getAgentConfig into resolveAgentConfig ([#80](https://github.com/gotgenes/pi-packages/issues/80)) ([1c14b47](https://github.com/gotgenes/pi-packages/commit/1c14b4760fa67dff5dbf17306d04bbe992b38bce))
|
|
19
|
+
* **retro:** add retro notes for issue [#71](https://github.com/gotgenes/pi-packages/issues/71) ([a70e52f](https://github.com/gotgenes/pi-packages/commit/a70e52f840cf3b2f65689987dcb7316e32dc12ff))
|
|
20
|
+
|
|
21
|
+
## [5.3.0](https://github.com/gotgenes/pi-packages/compare/pi-subagents-v5.2.0...pi-subagents-v5.3.0) (2026-05-19)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Features
|
|
25
|
+
|
|
26
|
+
* add assembleSessionConfig in session-config.ts ([ee8076d](https://github.com/gotgenes/pi-packages/commit/ee8076dc2292ec957b64894af3fcd22567f23be5))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Documentation
|
|
30
|
+
|
|
31
|
+
* add [#80](https://github.com/gotgenes/pi-packages/issues/80) to architecture roadmap, mark [#69](https://github.com/gotgenes/pi-packages/issues/69) and [#71](https://github.com/gotgenes/pi-packages/issues/71) done ([5744e28](https://github.com/gotgenes/pi-packages/commit/5744e28ac993454f8cb33afb18e5247569f9f971))
|
|
32
|
+
* plan session-config assembler extraction ([#71](https://github.com/gotgenes/pi-packages/issues/71)) ([5d2cd4f](https://github.com/gotgenes/pi-packages/commit/5d2cd4f8de214a03a11688b56221679591aedafd))
|
|
33
|
+
* **retro:** add retro notes for issue [#69](https://github.com/gotgenes/pi-packages/issues/69) ([18cbbdb](https://github.com/gotgenes/pi-packages/commit/18cbbdb627f2ae63f8109c1f5597c31265738415))
|
|
34
|
+
|
|
8
35
|
## [5.2.0](https://github.com/gotgenes/pi-packages/compare/pi-subagents-v5.1.0...pi-subagents-v5.2.0) (2026-05-19)
|
|
9
36
|
|
|
10
37
|
|
|
@@ -342,14 +342,14 @@ The following issues track the work needed to bring `pi-subagents` to the same l
|
|
|
342
342
|
|
|
343
343
|
### Phase 1: Foundation
|
|
344
344
|
|
|
345
|
-
These
|
|
346
|
-
Together they eliminate module-scope mutable state
|
|
345
|
+
These issues are independent of each other and can land in any order.
|
|
346
|
+
Together they eliminate module-scope mutable state, create a testable functional core, and simplify the agent-types API.
|
|
347
347
|
|
|
348
|
-
1. **gotgenes/pi-packages#69** — Create `SubagentRuntime`
|
|
348
|
+
1. **gotgenes/pi-packages#69** ✓ — Create `SubagentRuntime`
|
|
349
349
|
- Move `defaultMaxTurns`, `graceTurns`, `agentActivity`, `currentCtx`, and widget references out of closure/module scope into a single factory-constructed object.
|
|
350
350
|
- This unblocks handler extraction (Issue #70) by giving handlers a concrete deps bag instead of closure variables.
|
|
351
351
|
|
|
352
|
-
2. **gotgenes/pi-packages#71** — Extract pure agent-session assembler from `agent-runner.ts`
|
|
352
|
+
2. **gotgenes/pi-packages#71** ✓ — Extract pure agent-session assembler from `agent-runner.ts`
|
|
353
353
|
- Split `runAgent()` into a pure configuration assembler (~200 lines) and an IO shell (~200 lines).
|
|
354
354
|
- The assembler becomes independently testable without mocking the Pi SDK.
|
|
355
355
|
|
|
@@ -357,6 +357,10 @@ Together they eliminate module-scope mutable state and create a testable functio
|
|
|
357
357
|
- Replace the `process.cwd()` call in `dispose()` with a constructor parameter.
|
|
358
358
|
- A small, mechanical prerequisite for Issue #72.
|
|
359
359
|
|
|
360
|
+
4. **gotgenes/pi-packages#80** — Consolidate `getConfig` / `getAgentConfig` into a single resolution path
|
|
361
|
+
- Replace the two overlapping lookup functions with a single `resolveAgentConfig(type): AgentConfig` that handles the unknown-type fallback internally.
|
|
362
|
+
- Eliminates the duplicated fallback chain exposed by #71 and simplifies test mock setup.
|
|
363
|
+
|
|
360
364
|
### Phase 2: Core decomposition
|
|
361
365
|
|
|
362
366
|
These build on Phase 1 and should land after it.
|
|
@@ -394,19 +398,21 @@ Small cleanups that are safest after the structural changes settle.
|
|
|
394
398
|
### Dependency graph
|
|
395
399
|
|
|
396
400
|
```text
|
|
397
|
-
#69 (SubagentRuntime)
|
|
401
|
+
#69 (SubagentRuntime) ✓ ─┬─► #70 (handler extraction)
|
|
398
402
|
│
|
|
399
|
-
|
|
403
|
+
└─► #72 (AgentManager DI) ──(optional)──► #70
|
|
404
|
+
|
|
405
|
+
#71 (pure assembler) ✓ ──► #80 (consolidate getConfig/getAgentConfig)
|
|
400
406
|
|
|
401
|
-
#
|
|
407
|
+
#76 (cwd injection) ────► #72
|
|
402
408
|
|
|
403
|
-
#
|
|
409
|
+
#80 (config lookup) ────(independent, simplifies #72 and test mocks)
|
|
404
410
|
|
|
405
|
-
#66 (type casts)
|
|
406
|
-
#77 (projectAgentsDir)
|
|
411
|
+
#66 (type casts) ◄─────(after structural changes settle)
|
|
412
|
+
#77 (projectAgentsDir) ◄─(after #66 or parallel)
|
|
407
413
|
|
|
408
|
-
#61 (transcript format)
|
|
409
|
-
#22 (parent session)
|
|
414
|
+
#61 (transcript format) ◄(after structural refactor)
|
|
415
|
+
#22 (parent session) ◄──(cross-extension, independent)
|
|
410
416
|
```
|
|
411
417
|
|
|
412
418
|
### Recommended order
|
|
@@ -414,9 +420,10 @@ Small cleanups that are safest after the structural changes settle.
|
|
|
414
420
|
The recommended sequence is:
|
|
415
421
|
|
|
416
422
|
```text
|
|
417
|
-
#69 → #71 → #76 → #72 → #70 → #66 → #77 → #61
|
|
423
|
+
#69 ✓ → #71 ✓ → #80 → #76 → #72 → #70 → #66 → #77 → #61
|
|
418
424
|
```
|
|
419
425
|
|
|
426
|
+
Issue #80 slots after #71 because it cleans up the redundant lookup that #71 exposed, and simplifies mock setup for subsequent issues.
|
|
420
427
|
Issue #22 is a parallel cross-extension track and does not gate the structural work.
|
|
421
428
|
|
|
422
429
|
## Relationship with upstream
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
---
|
|
2
|
+
issue: 71
|
|
3
|
+
issue_title: "refactor: extract pure agent-session assembler from agent-runner.ts"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Extract session-config assembler from agent-runner
|
|
7
|
+
|
|
8
|
+
## Problem Statement
|
|
9
|
+
|
|
10
|
+
`agent-runner.ts` `runAgent()` is ~390 lines (post-#69 cleanup) and mixes three concerns:
|
|
11
|
+
|
|
12
|
+
1. Configuration assembly — resolve model, detect env, build prompt extras, preload skills, build memory blocks, assemble system prompt, compute tool names (~200 lines).
|
|
13
|
+
2. Session construction — create `DefaultResourceLoader`, call `createAgentSession`, filter tools, bind extensions (~100 lines).
|
|
14
|
+
3. Runtime orchestration — subscribe to events, enforce turn limits, collect results (~90 lines).
|
|
15
|
+
|
|
16
|
+
The configuration assembly is deterministic given resolved inputs and does not need an `AgentSession`.
|
|
17
|
+
Because it is inlined in `runAgent()`, it cannot be unit-tested without mocking the entire Pi SDK (`createAgentSession`, `DefaultResourceLoader`, `SessionManager`, `SettingsManager`).
|
|
18
|
+
|
|
19
|
+
## Goals
|
|
20
|
+
|
|
21
|
+
- Extract a pure `assembleSessionConfig()` function into a new `src/session-config.ts` module.
|
|
22
|
+
- The assembler takes resolved inputs (agent config, environment info, narrow context) and returns a data object with everything `runAgent()` needs to create the session.
|
|
23
|
+
- Reduce `runAgent()` to an IO shell: call the assembler, create SDK objects, wire subscriptions, and run the event loop.
|
|
24
|
+
- Add focused unit tests for the assembler covering model resolution fallback chain, skill preloading, memory block selection (read-write vs read-only), prompt mode, tool name assembly, and disallowed-tool computation.
|
|
25
|
+
- No behavior change.
|
|
26
|
+
|
|
27
|
+
## Non-Goals
|
|
28
|
+
|
|
29
|
+
- Changing the `RunResult` shape or `RunOptions` interface.
|
|
30
|
+
- Refactoring the event subscription / turn-limit logic (stays in `runAgent()`).
|
|
31
|
+
- Extracting `resumeAgent` or `steerAgent`.
|
|
32
|
+
- Modifying the public API surface (`service.ts`).
|
|
33
|
+
|
|
34
|
+
## Background
|
|
35
|
+
|
|
36
|
+
### Prior art
|
|
37
|
+
|
|
38
|
+
`pi-permission-system` extracted `evaluate()` — a pure function of `(surface, pattern, ruleset)` — from `PermissionManager.checkPermission()`.
|
|
39
|
+
That made permission decisions independently testable without filesystem access or a manager instance.
|
|
40
|
+
This plan follows the same pattern: extract a pure core from an IO-heavy function.
|
|
41
|
+
|
|
42
|
+
### Current `runAgent()` structure
|
|
43
|
+
|
|
44
|
+
Lines 220–460 of `agent-runner.ts` break into these logical phases:
|
|
45
|
+
|
|
46
|
+
| Phase | Lines (approx) | SDK dependency |
|
|
47
|
+
| ------------------------------- | -------------- | -------------------------------------------------------- |
|
|
48
|
+
| Config + agentConfig lookup | 224–225 | None (agent-types registry) |
|
|
49
|
+
| effectiveCwd | 228 | None |
|
|
50
|
+
| detectEnv | 230 | `pi.exec` (async IO) |
|
|
51
|
+
| parentSystemPrompt | 233 | `ctx.getSystemPrompt()` |
|
|
52
|
+
| extensions / skills resolution | 237–245 | None |
|
|
53
|
+
| Skill preloading | 247–252 | `preloadSkills` (filesystem) |
|
|
54
|
+
| Tool names + memory | 254–274 | None (agent-types registry) |
|
|
55
|
+
| System prompt assembly | 277–303 | `buildAgentPrompt` (pure) |
|
|
56
|
+
| noSkills flag | 306 | None |
|
|
57
|
+
| DefaultResourceLoader | 308–320 | `DefaultResourceLoader` (SDK) |
|
|
58
|
+
| Model resolution | 323–324 | `ctx.modelRegistry` (narrow) |
|
|
59
|
+
| Thinking level | 327 | None |
|
|
60
|
+
| sessionOpts construction | 329–345 | `SessionManager`, `SettingsManager`, `getAgentDir` (SDK) |
|
|
61
|
+
| createAgentSession | 347 | SDK |
|
|
62
|
+
| Tool filtering + bindExtensions | 350–400 | `session.*` methods (SDK) |
|
|
63
|
+
| Event subscriptions + prompt | 402–460 | `session.*` methods (SDK) |
|
|
64
|
+
|
|
65
|
+
Everything above the `DefaultResourceLoader` line is configuration assembly — deterministic given resolved inputs.
|
|
66
|
+
Everything from `DefaultResourceLoader` onward is SDK orchestration.
|
|
67
|
+
|
|
68
|
+
### Modules the assembler will call
|
|
69
|
+
|
|
70
|
+
All are internal to this package — not Pi SDK:
|
|
71
|
+
|
|
72
|
+
- `agent-types.ts` — `getConfig()`, `getAgentConfig()`, `getToolNamesForType()`, `getMemoryToolNames()`, `getReadOnlyMemoryToolNames()`
|
|
73
|
+
- `prompts.ts` — `buildAgentPrompt()`
|
|
74
|
+
- `memory.ts` — `buildMemoryBlock()`, `buildReadOnlyMemoryBlock()`
|
|
75
|
+
- `skill-loader.ts` — `preloadSkills()`
|
|
76
|
+
- `default-agents.ts` — `DEFAULT_AGENTS` (fallback config)
|
|
77
|
+
|
|
78
|
+
### Relevant constraints from AGENTS.md
|
|
79
|
+
|
|
80
|
+
- Keep modules focused and composable (one concern per file).
|
|
81
|
+
- Keep Pi SDK imports out of business-logic modules.
|
|
82
|
+
- Prefer explicit configuration over hidden behavior.
|
|
83
|
+
- Business logic should be pure functions wherever possible — keep IO at the edges.
|
|
84
|
+
|
|
85
|
+
### Issue #69 status
|
|
86
|
+
|
|
87
|
+
Issue #69 (`SubagentRuntime`) is implemented.
|
|
88
|
+
Module-scope mutable state has been removed from `agent-runner.ts`.
|
|
89
|
+
`defaultMaxTurns` and `graceTurns` flow through `RunOptions`.
|
|
90
|
+
This plan builds on the post-#69 codebase.
|
|
91
|
+
|
|
92
|
+
## Design Overview
|
|
93
|
+
|
|
94
|
+
### Separation of concerns
|
|
95
|
+
|
|
96
|
+
`detectEnv()` is the only async IO call in the assembly phase — it calls `pi.exec()` to check git state.
|
|
97
|
+
The assembler is synchronous and takes `EnvInfo` as a pre-resolved parameter.
|
|
98
|
+
`runAgent()` calls `detectEnv()` first, then calls the assembler, then does SDK work.
|
|
99
|
+
|
|
100
|
+
### Narrow context interface
|
|
101
|
+
|
|
102
|
+
The assembler does not accept `ExtensionContext` — it accepts a narrow interface with only the fields it reads:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
interface AssemblerContext {
|
|
106
|
+
/** Parent working directory (overridable via options.cwd). */
|
|
107
|
+
cwd: string;
|
|
108
|
+
/** Parent's effective system prompt (for append-mode agents). */
|
|
109
|
+
parentSystemPrompt: string;
|
|
110
|
+
/** Parent's current model instance (fallback when agent config has no model). */
|
|
111
|
+
parentModel?: Model<any>;
|
|
112
|
+
/** Model registry for resolving config.model strings. */
|
|
113
|
+
modelRegistry: ModelRegistry;
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`ModelRegistry` is a narrow interface (already exists in `model-resolver.ts`):
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
interface ModelRegistry {
|
|
121
|
+
find(provider: string, modelId: string): Model<any> | undefined;
|
|
122
|
+
getAvailable?(): Model<any>[];
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Tests construct plain objects satisfying these interfaces — no SDK mocking needed.
|
|
127
|
+
|
|
128
|
+
### Assembler signature
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
function assembleSessionConfig(
|
|
132
|
+
type: SubagentType,
|
|
133
|
+
ctx: AssemblerContext,
|
|
134
|
+
options: AssemblerOptions,
|
|
135
|
+
env: EnvInfo,
|
|
136
|
+
): SessionConfig;
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`AssemblerOptions` is a narrow pick of `RunOptions`:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
interface AssemblerOptions {
|
|
143
|
+
cwd?: string;
|
|
144
|
+
isolated?: boolean;
|
|
145
|
+
model?: Model<any>;
|
|
146
|
+
thinkingLevel?: ThinkingLevel;
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Return type
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
interface SessionConfig {
|
|
154
|
+
/** Resolved working directory (options.cwd ?? ctx.cwd). */
|
|
155
|
+
effectiveCwd: string;
|
|
156
|
+
/** Fully-assembled system prompt string. */
|
|
157
|
+
systemPrompt: string;
|
|
158
|
+
/** Tool names for session creation and filtering. */
|
|
159
|
+
toolNames: string[];
|
|
160
|
+
/** Disallowed tool set from agent config (for filterActiveTools). */
|
|
161
|
+
disallowedSet: Set<string> | undefined;
|
|
162
|
+
/** Resolved extensions setting (for resource loader and tool filtering). */
|
|
163
|
+
extensions: boolean | string[];
|
|
164
|
+
/** Resolved model instance (or undefined → parent fallback). */
|
|
165
|
+
model: Model<any> | undefined;
|
|
166
|
+
/** Resolved thinking level (or undefined → inherit). */
|
|
167
|
+
thinkingLevel: ThinkingLevel | undefined;
|
|
168
|
+
/** Whether to skip skill loading in the resource loader. */
|
|
169
|
+
noSkills: boolean;
|
|
170
|
+
/** Prompt extras for transparency / debugging. */
|
|
171
|
+
extras: PromptExtras;
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### `resolveDefaultModel` moves to session-config.ts
|
|
176
|
+
|
|
177
|
+
`resolveDefaultModel()` is a pure function that resolves model strings against a registry.
|
|
178
|
+
It belongs in the assembler module alongside the other resolution logic.
|
|
179
|
+
It becomes an internal function (not exported) — its behavior is tested through `assembleSessionConfig()`.
|
|
180
|
+
|
|
181
|
+
### `filterActiveTools` stays in agent-runner.ts
|
|
182
|
+
|
|
183
|
+
`filterActiveTools()` operates on a live session's active tool list.
|
|
184
|
+
It runs twice (pre- and post-`bindExtensions`) and is an IO-layer concern.
|
|
185
|
+
It stays in `agent-runner.ts` and consumes `toolNames`, `extensions`, and `disallowedSet` from the `SessionConfig` return.
|
|
186
|
+
|
|
187
|
+
### `normalizeMaxTurns` stays in agent-runner.ts
|
|
188
|
+
|
|
189
|
+
`normalizeMaxTurns()` is used in the turn-limit subscription callback — runtime orchestration, not config assembly.
|
|
190
|
+
It stays in `agent-runner.ts`.
|
|
191
|
+
|
|
192
|
+
### What runAgent() looks like after
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
export async function runAgent(
|
|
196
|
+
ctx: ExtensionContext,
|
|
197
|
+
type: SubagentType,
|
|
198
|
+
prompt: string,
|
|
199
|
+
options: RunOptions,
|
|
200
|
+
): Promise<RunResult> {
|
|
201
|
+
const effectiveCwd = options.cwd ?? ctx.cwd;
|
|
202
|
+
const env = await detectEnv(options.pi, effectiveCwd);
|
|
203
|
+
|
|
204
|
+
const config = assembleSessionConfig(type, {
|
|
205
|
+
cwd: ctx.cwd,
|
|
206
|
+
parentSystemPrompt: ctx.getSystemPrompt(),
|
|
207
|
+
parentModel: ctx.model,
|
|
208
|
+
modelRegistry: ctx.modelRegistry,
|
|
209
|
+
}, {
|
|
210
|
+
cwd: options.cwd,
|
|
211
|
+
isolated: options.isolated,
|
|
212
|
+
model: options.model,
|
|
213
|
+
thinkingLevel: options.thinkingLevel,
|
|
214
|
+
}, env);
|
|
215
|
+
|
|
216
|
+
// SDK orchestration: create loader, session, filter tools, bind, run
|
|
217
|
+
const agentDir = getAgentDir();
|
|
218
|
+
const loader = new DefaultResourceLoader({ ... });
|
|
219
|
+
await loader.reload();
|
|
220
|
+
const { session } = await createAgentSession({ ... });
|
|
221
|
+
|
|
222
|
+
// Tool filtering (two passes), bindExtensions, subscriptions, prompt
|
|
223
|
+
// ...same as today, using config.toolNames, config.disallowedSet, etc.
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
Target: `runAgent()` drops to ~200 lines (down from ~390).
|
|
228
|
+
|
|
229
|
+
### Edge cases
|
|
230
|
+
|
|
231
|
+
- Unknown agent type: `getAgentConfig()` returns `undefined`.
|
|
232
|
+
The assembler falls back to `DEFAULT_AGENTS.get("general-purpose")` with `name: type`, matching the current `runAgent()` fallback.
|
|
233
|
+
- Empty `builtinToolNames`: `getToolNamesForType()` already falls back to `BUILTIN_TOOL_NAMES`.
|
|
234
|
+
- `isolated: true` overrides `extensions` and `skills` to `false` — same as today, now inside the assembler.
|
|
235
|
+
- Memory block selection: write-capable agents (have `write` or `edit` in effective tool set, not denied) get read-write memory; others get read-only.
|
|
236
|
+
The denylist check uses `disallowedSet` from the agent config.
|
|
237
|
+
|
|
238
|
+
## Module-Level Changes
|
|
239
|
+
|
|
240
|
+
### `src/session-config.ts` (new)
|
|
241
|
+
|
|
242
|
+
- `AssemblerContext` interface — narrow context (cwd, parentSystemPrompt, parentModel, modelRegistry).
|
|
243
|
+
- `AssemblerOptions` interface — narrow options subset (cwd, isolated, model, thinkingLevel).
|
|
244
|
+
- `SessionConfig` interface — return type with all assembled configuration.
|
|
245
|
+
- `assembleSessionConfig()` function — pure configuration assembly.
|
|
246
|
+
- `resolveDefaultModel()` — moved from `agent-runner.ts` (internal, not exported).
|
|
247
|
+
|
|
248
|
+
### `src/agent-runner.ts` (modified)
|
|
249
|
+
|
|
250
|
+
- Import `assembleSessionConfig` and `SessionConfig` from `./session-config.js`.
|
|
251
|
+
- Remove ~200 lines of configuration assembly from `runAgent()`.
|
|
252
|
+
- Replace with a call to `assembleSessionConfig()` followed by SDK orchestration using the returned `SessionConfig`.
|
|
253
|
+
- Remove `resolveDefaultModel()` (moved to session-config.ts).
|
|
254
|
+
- `filterActiveTools()`, `normalizeMaxTurns()`, `collectResponseText()`, `getLastAssistantText()`, `forwardAbortSignal()` — all stay.
|
|
255
|
+
- `RunOptions`, `RunResult`, `ToolActivity` — all stay (unchanged).
|
|
256
|
+
|
|
257
|
+
### `test/session-config.test.ts` (new)
|
|
258
|
+
|
|
259
|
+
- Unit tests for `assembleSessionConfig()` covering all assembly logic.
|
|
260
|
+
- Tests use plain objects for `AssemblerContext` — no SDK mocks.
|
|
261
|
+
- Mocks for `agent-types`, `prompts`, `memory`, `skill-loader` — simple function mocks.
|
|
262
|
+
|
|
263
|
+
### `test/agent-runner.test.ts` (modified)
|
|
264
|
+
|
|
265
|
+
- Existing tests stay as-is — they already mock the SDK and test the full `runAgent()` flow.
|
|
266
|
+
- Tests that verified assembly details (e.g., `suppresses AGENTS.md/CLAUDE.md` or `passes effective cwd to the loader`) remain valid because `runAgent()` still does the SDK orchestration.
|
|
267
|
+
- No tests are removed or rewritten.
|
|
268
|
+
|
|
269
|
+
### `test/agent-runner-extension-tools.test.ts` (unchanged)
|
|
270
|
+
|
|
271
|
+
- Tests extension-tool filtering via `filterActiveTools` — stays in `agent-runner.ts`.
|
|
272
|
+
- No impact.
|
|
273
|
+
|
|
274
|
+
## Test Impact Analysis
|
|
275
|
+
|
|
276
|
+
### New unit tests enabled by the extraction
|
|
277
|
+
|
|
278
|
+
1. Model resolution fallback chain — test that `assembleSessionConfig` returns the correct model for: explicit option model, config model string (valid/invalid), parent model fallback, and no model.
|
|
279
|
+
2. Skill preloading — test that `skills: string[]` triggers `preloadSkills` and populates `extras.skillBlocks`; `skills: false` and `skills: true` skip preloading.
|
|
280
|
+
3. Memory block selection — test read-write vs read-only memory based on tool availability and denylist interaction.
|
|
281
|
+
4. Tool name assembly — test that `getToolNamesForType` result is augmented with memory tool names when memory is configured.
|
|
282
|
+
5. Extensions / isolated interaction — test that `isolated: true` forces `extensions: false` and `skills: false`.
|
|
283
|
+
6. System prompt assembly — test that `buildAgentPrompt` is called with the correct config, extras, and env.
|
|
284
|
+
7. Disallowed tool set — test construction from `agentConfig.disallowedTools`.
|
|
285
|
+
8. Unknown type fallback — test that missing `agentConfig` triggers the general-purpose fallback.
|
|
286
|
+
9. Thinking level resolution — test explicit option vs config vs undefined.
|
|
287
|
+
|
|
288
|
+
### Existing tests that stay as-is
|
|
289
|
+
|
|
290
|
+
All tests in `test/agent-runner.test.ts`, `test/agent-runner-extension-tools.test.ts`, and `test/agent-runner-settings.test.ts` continue to pass unchanged.
|
|
291
|
+
They test the SDK orchestration layer which is not modified (only reduced in scope).
|
|
292
|
+
The assembly logic they implicitly tested is now covered more thoroughly by `test/session-config.test.ts`.
|
|
293
|
+
|
|
294
|
+
### Existing tests that could be simplified (future follow-up)
|
|
295
|
+
|
|
296
|
+
Some `agent-runner.test.ts` tests verify assembly-layer behavior through the full `runAgent()` call (e.g., checking `defaultResourceLoaderCtor` args).
|
|
297
|
+
These become redundant with the new assembler tests.
|
|
298
|
+
Simplifying them is a separate follow-up — not part of this issue's scope.
|
|
299
|
+
|
|
300
|
+
## TDD Order
|
|
301
|
+
|
|
302
|
+
1. **Red: assembler returns correct defaults for a standard agent type.**
|
|
303
|
+
Create `test/session-config.test.ts` with a test that calls `assembleSessionConfig()` for the `"Explore"` type and asserts the returned `SessionConfig` shape: `effectiveCwd`, `systemPrompt`, `toolNames`, `extensions: false`, `noSkills: true`, `disallowedSet: undefined`.
|
|
304
|
+
Mock `agent-types`, `prompts`, `memory`, `skill-loader` at the module level.
|
|
305
|
+
This fails because `session-config.ts` does not exist yet.
|
|
306
|
+
Commit: `test: add session-config assembler test for default agent type`
|
|
307
|
+
|
|
308
|
+
2. **Green: implement `assembleSessionConfig()` core path.**
|
|
309
|
+
Create `src/session-config.ts` with `AssemblerContext`, `AssemblerOptions`, `SessionConfig` interfaces and the `assembleSessionConfig()` function.
|
|
310
|
+
Implement the happy path: resolve config, compute effectiveCwd, resolve extensions/skills, build extras, build system prompt, compute toolNames, compute disallowedSet, resolve noSkills.
|
|
311
|
+
Tests go green.
|
|
312
|
+
Commit: `feat: add assembleSessionConfig in session-config.ts`
|
|
313
|
+
|
|
314
|
+
3. **Red→Green: model resolution fallback chain.**
|
|
315
|
+
Add tests for: explicit option model wins, config model string resolves via registry, invalid config model falls back to parent, no model returns undefined.
|
|
316
|
+
Move `resolveDefaultModel()` from `agent-runner.ts` to `session-config.ts` (internal).
|
|
317
|
+
Commit: `test: model resolution fallback chain in session-config`
|
|
318
|
+
|
|
319
|
+
4. **Red→Green: skill preloading paths.**
|
|
320
|
+
Add tests for: `skills: string[]` populates `extras.skillBlocks`, `skills: false` skips, `skills: true` skips preloading (loaded by resource loader instead), `isolated: true` forces skip.
|
|
321
|
+
Commit: `test: skill preloading paths in session-config`
|
|
322
|
+
|
|
323
|
+
5. **Red→Green: memory block selection.**
|
|
324
|
+
Add tests for: agent with memory + write tools → read-write block, agent with memory + read-only tools → read-only block, agent with memory + denied write tools → read-only block, agent without memory → no block.
|
|
325
|
+
Commit: `test: memory block selection in session-config`
|
|
326
|
+
|
|
327
|
+
6. **Red→Green: isolated mode, unknown type fallback, thinking level.**
|
|
328
|
+
Add tests for: `isolated: true` forces `extensions: false` and `noSkills: true`, unknown type falls back to general-purpose config, thinking level resolves from option > config > undefined.
|
|
329
|
+
Commit: `test: isolated mode, unknown type fallback, thinking level`
|
|
330
|
+
|
|
331
|
+
7. **Refactor: wire `assembleSessionConfig` into `runAgent()`.**
|
|
332
|
+
Replace the configuration assembly block in `runAgent()` with a call to `assembleSessionConfig()`.
|
|
333
|
+
Use the returned `SessionConfig` fields to construct `DefaultResourceLoader`, `createAgentSession` opts, and `filterActiveTools` args.
|
|
334
|
+
Remove `resolveDefaultModel()` from `agent-runner.ts` (already moved in step 3).
|
|
335
|
+
Run full test suite — all existing `agent-runner.test.ts` tests pass unchanged.
|
|
336
|
+
Commit: `refactor: wire assembleSessionConfig into runAgent (#71)`
|
|
337
|
+
|
|
338
|
+
8. **Verify acceptance criteria and clean up.**
|
|
339
|
+
Confirm `runAgent()` is ≤200 lines.
|
|
340
|
+
Confirm assembler tests run without mocking `AgentSession`, `ExtensionContext`, or Pi SDK types.
|
|
341
|
+
Confirm full test suite passes with no regressions.
|
|
342
|
+
Remove any dead imports.
|
|
343
|
+
Run `pnpm run check` for type safety.
|
|
344
|
+
Commit: `refactor: finalize session-config extraction (#71)`
|
|
345
|
+
|
|
346
|
+
## Risks and Mitigations
|
|
347
|
+
|
|
348
|
+
| Risk | Mitigation |
|
|
349
|
+
| ------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
350
|
+
| Assembly logic has subtle ordering dependencies (e.g., tool names must be computed before memory block selection) | The assembler mirrors the exact order from `runAgent()` today; tests verify each dependency chain explicitly. |
|
|
351
|
+
| Moving `resolveDefaultModel` changes import paths for any external consumer | `resolveDefaultModel` is not exported from the package — it is internal to `agent-runner.ts` today and internal to `session-config.ts` after the move. No external impact. |
|
|
352
|
+
| Existing `agent-runner.test.ts` tests break when assembly is delegated | The tests mock `agent-types`, `prompts`, `memory`, `skill-loader` — the assembler calls the same functions through the same module paths, so existing mocks continue to intercept. |
|
|
353
|
+
| `Model<any>` import from `@earendil-works/pi-ai` in the new module violates "keep Pi SDK imports out of business-logic modules" | `pi-ai` provides type-only interfaces (`Model`, `ThinkingLevel`) already used in `types.ts`. The constraint targets `pi-coding-agent` SDK types (`AgentSession`, `ExtensionContext`, `DefaultResourceLoader`). The assembler imports zero types from `pi-coding-agent`. |
|
|
354
|
+
| The assembler's return type becomes a wide interface (9 fields) | All fields are consumed by `runAgent()` — none are unused. The interface represents a single cohesive concept (session configuration). No consumer uses a subset; there is no narrowing opportunity. |
|
|
355
|
+
|
|
356
|
+
## Open Questions
|
|
357
|
+
|
|
358
|
+
- Should `assembleSessionConfig` also resolve `effectiveCwd` internally (trivial: `options.cwd ?? ctx.cwd`) or should the caller pre-compute it?
|
|
359
|
+
The plan assumes the assembler computes it (self-contained), but `runAgent()` also needs `effectiveCwd` for `detectEnv()` before calling the assembler.
|
|
360
|
+
Resolution: `runAgent()` computes `effectiveCwd` once, passes it as `options.cwd` (already resolved) or as a separate parameter.
|
|
361
|
+
The assembler still computes `effectiveCwd` from its inputs, which produces the same value.
|
|
362
|
+
This duplication is benign — both paths yield `options.cwd ?? ctx.cwd`.
|