@ferris1225/pi-subagents 4.3.34 → 5.0.1
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 +55 -359
- package/README.md +163 -553
- package/index.ts +28 -3
- package/package.json +2 -2
- package/skills/workflow/SKILL.md +46 -29
- package/src/configuration/config.ts +15 -5
- package/src/configuration/setup.ts +79 -228
- package/src/configuration/ui.ts +249 -128
- package/src/delegation/agents.ts +4 -3
- package/src/delegation/builtin-agents.ts +34 -10
- package/src/delegation/dispatch.ts +9 -6
- package/src/delegation/prompt.ts +13 -9
- package/src/execution/cli-run.ts +404 -0
- package/src/execution/process-tree.ts +1 -1
- package/src/execution/{rpc-control.ts → run-control.ts} +23 -33
- package/src/execution/spawn.ts +33 -185
- package/src/isolation/cleanup.ts +174 -0
- package/src/isolation/temp-hygiene.ts +2 -26
- package/src/lifecycle/cancellation.ts +36 -0
- package/src/lifecycle/completion.ts +1 -1
- package/src/lifecycle/dispatch-core.ts +0 -1
- package/src/lifecycle/durable.ts +5 -1
- package/src/lifecycle/runtime.ts +42 -29
- package/src/lifecycle/stop.ts +176 -0
- package/src/lifecycle/thread-lifecycle.ts +14 -14
- package/src/lifecycle/thread-restore.ts +12 -15
- package/src/lifecycle/thread-shared.ts +2 -0
- package/src/lifecycle/tools.ts +145 -181
- package/src/presentation/format.ts +2 -5
- package/src/presentation/monitor.ts +16 -1
- package/src/presentation/widget.ts +141 -175
- package/src/workflow/engine.ts +68 -34
- package/src/workflow/files.ts +10 -32
- package/src/workflow/plan.ts +61 -13
- package/src/workflow/state.ts +4 -3
- package/src/workflow/tool.ts +23 -12
- package/workflows/code-review.md +15 -10
- package/workflows/migration.md +14 -26
- package/workflows/research.md +6 -5
- package/src/execution/rpc-run.ts +0 -889
package/index.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* The heavy lifting lives in focused modules:
|
|
6
6
|
* - delegation/ — built-in roles, routing prompts, and tool contract
|
|
7
7
|
* - configuration/ — persisted settings, model routes, and setup UI
|
|
8
|
-
* - execution/ — process queue,
|
|
8
|
+
* - execution/ — process queue, native JSON CLI/control, and model handoff
|
|
9
9
|
* - lifecycle/ — durable threads, restoration, controls, and delivery
|
|
10
10
|
* - workflow/ — dependency-graph plans, discovery, and the graph runner
|
|
11
11
|
* - isolation/ — Git worktrees, recovery, and temporary-state hygiene
|
|
@@ -29,12 +29,15 @@ import { registerSubagentTool } from "./src/delegation/dispatch.ts";
|
|
|
29
29
|
import { buildDelegationDirective, installDelegationSections } from "./src/delegation/prompt.ts";
|
|
30
30
|
import { currentSubagentDepth } from "./src/execution/spawn.ts";
|
|
31
31
|
import { createDispatchCore } from "./src/lifecycle/dispatch-core.ts";
|
|
32
|
+
import { registerCancellationHooks } from "./src/lifecycle/cancellation.ts";
|
|
32
33
|
import { createRuntime } from "./src/lifecycle/runtime.ts";
|
|
33
34
|
import { bootstrapDurableState } from "./src/lifecycle/thread-restore.ts";
|
|
34
35
|
import { registerLookupTools } from "./src/lifecycle/tools.ts";
|
|
35
36
|
import { registerWorkflowTool, restoreWorkflowState } from "./src/workflow/tool.ts";
|
|
36
37
|
import { registerAnnouncements } from "./src/presentation/announcements.ts";
|
|
37
38
|
import { clearActiveRunsWidget } from "./src/presentation/widget.ts";
|
|
39
|
+
import { formatBytes, measureDurableUsage, runStorageCleanup } from "./src/isolation/cleanup.ts";
|
|
40
|
+
import { getSubagentsRoot } from "./src/execution/spawn.ts";
|
|
38
41
|
|
|
39
42
|
export default function (pi: ExtensionAPI): void {
|
|
40
43
|
const configPath = getConfigPath(getAgentDir());
|
|
@@ -73,7 +76,8 @@ export default function (pi: ExtensionAPI): void {
|
|
|
73
76
|
const dispatchCore = createDispatchCore(runtime);
|
|
74
77
|
registerSubagentTool(pi, runtime, dispatchCore);
|
|
75
78
|
registerWorkflowTool(pi, runtime, dispatchCore);
|
|
76
|
-
registerLookupTools(pi, runtime);
|
|
79
|
+
registerLookupTools(pi, runtime, dispatchCore);
|
|
80
|
+
registerCancellationHooks(pi, runtime);
|
|
77
81
|
|
|
78
82
|
pi.registerCommand("subagents-setup", {
|
|
79
83
|
description: "Configure pi-subagents: model and thinking per role",
|
|
@@ -82,6 +86,27 @@ export default function (pi: ExtensionAPI): void {
|
|
|
82
86
|
},
|
|
83
87
|
});
|
|
84
88
|
|
|
89
|
+
pi.registerCommand("subagents-cleanup", {
|
|
90
|
+
description: "Report and reclaim pi-subagents storage; /subagents-cleanup dry-run only reports",
|
|
91
|
+
handler: async (args, ctx) => {
|
|
92
|
+
const root = getSubagentsRoot(configPath);
|
|
93
|
+
if (args.trim() === "dry-run") {
|
|
94
|
+
const usage = measureDurableUsage(root);
|
|
95
|
+
const detail = usage.projects.slice(0, 5).map((project) =>
|
|
96
|
+
`${project.path.split(/[\\/]/u).at(-1)} ${formatBytes(project.bytes)} (${project.sessions} sessions, ${project.worktrees} worktrees)`);
|
|
97
|
+
ctx.ui.notify(`pi-subagents storage: ${formatBytes(usage.totalBytes)} in ${usage.projects.length} project(s)${detail.length ? " · " + detail.join(" · ") : ""}.`, "info");
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const result = await runStorageCleanup(configPath);
|
|
101
|
+
ctx.ui.notify(
|
|
102
|
+
`pi-subagents cleanup: freed ${formatBytes(result.freedBytes)} (${formatBytes(result.before.totalBytes)} → ${formatBytes(result.after.totalBytes)})`
|
|
103
|
+
+ `${result.removedProjects.length ? `; removed ${result.removedProjects.length} idle project root${result.removedProjects.length === 1 ? "" : "s"}` : ""}`
|
|
104
|
+
+ `; kept ${result.parkedRecords} parked recovery record${result.parkedRecords === 1 ? "" : "s"} and every referenced artifact.`,
|
|
105
|
+
"info",
|
|
106
|
+
);
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
85
110
|
pi.on("session_start", async (_event, ctx) => {
|
|
86
111
|
await bootstrapDurableState(runtime, ctx.cwd);
|
|
87
112
|
await restoreWorkflowState(runtime, ctx);
|
|
@@ -90,7 +115,7 @@ export default function (pi: ExtensionAPI): void {
|
|
|
90
115
|
|
|
91
116
|
// Install the routing contract as prompt sections when the host can diff them.
|
|
92
117
|
// A full systemPrompt return forces an opaque prompt and invalidates the cache.
|
|
93
|
-
pi.on("before_agent_start", async (event
|
|
118
|
+
pi.on("before_agent_start", async (event) => {
|
|
94
119
|
await runtime.durableRestore;
|
|
95
120
|
const agents = loadBuiltinAgents();
|
|
96
121
|
const sources = [...runtime.threads.values()];
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ferris1225/pi-subagents",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A managed sub-agent team for pi:
|
|
3
|
+
"version": "5.0.1",
|
|
4
|
+
"description": "A managed sub-agent team for pi: research, implementation, independent review, interactive follow-ups, and Git worktree isolation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
package/skills/workflow/SKILL.md
CHANGED
|
@@ -34,25 +34,32 @@ answer to know what phase two is, you cannot write the graph yet: run phase one
|
|
|
34
34
|
with `subagent` and write the graph afterwards. If every phase is a one-line
|
|
35
35
|
read, the graph is overhead — do it in main.
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
Choose proactively from task structure; the user does not need to name the
|
|
38
|
+
tool. Use a graph for two or more substantial phases with known dependency
|
|
39
|
+
edges and write scopes when scheduling saves coordination. Independent phases
|
|
40
|
+
without edges use a parallel `subagent` batch. Honor an explicit user choice.
|
|
41
|
+
|
|
42
|
+
After a discovery phase, reassess the remaining work: use a graph if its plan
|
|
43
|
+
is now clear, or keep the next bounded step in main or a single child. A graph
|
|
44
|
+
schedules a fixed plan; it does not decide what a discovery result means or
|
|
45
|
+
change its nodes at runtime. Never add survey, implementation, review,
|
|
46
|
+
confirmation, and gate nodes merely to follow a template.
|
|
39
47
|
|
|
40
48
|
## 2. Shape
|
|
41
49
|
|
|
42
50
|
```ts
|
|
43
51
|
subagent_workflow({
|
|
44
|
-
name: "auth-fix",
|
|
45
|
-
description: "
|
|
52
|
+
name: "auth-fix",
|
|
53
|
+
description: "Fix the refresh race and challenge the result.",
|
|
46
54
|
nodes: [
|
|
47
|
-
{ id: "map", agent: "scout", task: "
|
|
48
|
-
{ id: "fix", agent: "artisan", task: "Fix
|
|
49
|
-
scope: { paths: ["src/auth"] } },
|
|
50
|
-
{ id: "
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
dependsOn: ["fix", "tests"] },
|
|
55
|
+
{ id: "map", agent: "scout", task: "Trace the refresh race in src/auth. Cite path:line and test coverage gaps." },
|
|
56
|
+
{ id: "fix", agent: "artisan", task: "Fix the race and add its regression tests. Evidence:\n{{map}}", dependsOn: ["map"],
|
|
57
|
+
scope: { paths: ["src/auth", "test/auth.test.ts"] } },
|
|
58
|
+
{ id: "review", agent: "critic", isolation: "shared",
|
|
59
|
+
task: "Try to disprove the fix: inspect callers and edge cases, then run the documented checks. Report evidence and unresolved risks; do not edit files.\n{{fix}}",
|
|
60
|
+
dependsOn: ["fix"] },
|
|
54
61
|
],
|
|
55
|
-
})
|
|
62
|
+
});
|
|
56
63
|
```
|
|
57
64
|
|
|
58
65
|
- `id` — identifies the node in `dependsOn` and in placeholders. Unique per graph.
|
|
@@ -70,22 +77,24 @@ completion message arrives when it settles. Do not poll it.
|
|
|
70
77
|
The shapes that are worth writing, in the order they usually appear:
|
|
71
78
|
|
|
72
79
|
- **Survey → act.** One read-only node establishes the facts; writers depend on it
|
|
73
|
-
and quote it.
|
|
80
|
+
and quote it. Reuse established evidence and verify decisive or conflicting claims.
|
|
74
81
|
- **Fan-out with a join.** Independent slices of one plan run at the same time; a
|
|
75
82
|
join node depends on all of them. Declare disjoint `scope.paths` per slice —
|
|
76
83
|
the compiler rejects overlapping scopes between nodes with no dependency edge.
|
|
77
|
-
- **
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
84
|
+
- **Adversarial review and confirmation.** Reviewers seek counterexamples;
|
|
85
|
+
confirmation tries to disprove their findings against callers, invariants,
|
|
86
|
+
and focused reproductions. Use different lenses for independent reviewers.
|
|
87
|
+
Distinguish reproduced, source-supported, disproved, and unverified findings.
|
|
88
|
+
Use reviewer or critic when commands are required; scout cannot execute a reproduction.
|
|
81
89
|
- **A closing gate.** The last node runs the project's own checks. Prefer
|
|
82
90
|
commands over opinions: the repository's test suite, typecheck, build, or the
|
|
83
91
|
verification line its README names. Ask for the strongest tier it has, not the
|
|
84
92
|
fastest one.
|
|
85
93
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
94
|
+
Choose depth for the risk. A reviewer, independent confirmer, and synthesis
|
|
95
|
+
node are reasonable for a large or sensitive change; a focused review can
|
|
96
|
+
combine stages or leave synthesis to main. Every extra node should answer a
|
|
97
|
+
different question. Keep a fix and its regression tests with one owner.
|
|
89
98
|
|
|
90
99
|
## 4. What the runtime guarantees, and what it does not
|
|
91
100
|
|
|
@@ -104,7 +113,12 @@ piece of work.
|
|
|
104
113
|
- **An interrupted graph is explained, not resumed.** If the session ends
|
|
105
114
|
mid-graph, the next session's start notice and `subagent_status({ workflow })`
|
|
106
115
|
report which nodes completed and which were still running, with their run ids.
|
|
107
|
-
|
|
116
|
+
Checkpoints are written during execution, so a process crash also leaves the
|
|
117
|
+
graph shape.
|
|
118
|
+
- **Parent cancellation ends owned work.** Escape and conversation-tree navigation
|
|
119
|
+
stop unfinished graphs and runs without completion wake-ups. New/resume/fork
|
|
120
|
+
tears down the old Pi runtime and retains interruption evidence.
|
|
121
|
+
- **Capacity is pacing, not refusal.** The automatic process limit bounds simultaneous
|
|
108
122
|
child processes; a wider graph queues and starts as slots free. Queue position
|
|
109
123
|
never changes dependency order.
|
|
110
124
|
|
|
@@ -167,6 +181,9 @@ node expects to write:
|
|
|
167
181
|
A Markdown definition carries prose plus one ```json fenced block with the plan
|
|
168
182
|
(or a bare node array); JSON carries the plan object. Project definitions load
|
|
169
183
|
only in a trusted project. Same-name precedence: project > user > builtin.
|
|
184
|
+
Explicit file paths also require project trust. A call-level `name` overrides a
|
|
185
|
+
saved graph's name. Customize unfilled question, target, and slice placeholders
|
|
186
|
+
in a copy of a shipped template before execution.
|
|
170
187
|
|
|
171
188
|
Save a graph when it will run again, when a reviewer should read the plan before
|
|
172
189
|
it runs, or when the phases are long enough that you want them in version
|
|
@@ -177,8 +194,9 @@ are `<workflow-slug>.<node-id>`; an interrupted run keeps its claim until it is
|
|
|
177
194
|
retired. Re-running the same shape is therefore rejected **before any node
|
|
178
195
|
starts**, naming the owning runs. Three ways forward: retire the owner with
|
|
179
196
|
`subagent_stop({ id })` (destructive — it drops that run's retained session),
|
|
180
|
-
give
|
|
181
|
-
|
|
197
|
+
give a genuinely different deliverable its own brief and phase id. Renaming the
|
|
198
|
+
graph does not bypass exact task matching. Prefer reusing the owner's evidence
|
|
199
|
+
and finishing incomplete work in main. A new session alone does not help: a retained interrupted run is
|
|
182
200
|
restored with its claim intact.
|
|
183
201
|
|
|
184
202
|
## 9. Cost
|
|
@@ -186,8 +204,8 @@ restored with its claim intact.
|
|
|
186
204
|
Every node is a full model session with its own context, and the summary carries
|
|
187
205
|
each node's answer. Budget accordingly:
|
|
188
206
|
|
|
189
|
-
-
|
|
190
|
-
|
|
207
|
+
- Give each phase one owner. Add separate review, confirmation, and synthesis
|
|
208
|
+
when their distinct work justifies the cost; combine them for bounded tasks.
|
|
191
209
|
- Keep node answers short: conclusions, paths, `command → result`.
|
|
192
210
|
- Do not add a node to fill a free process slot, and do not split one tightly
|
|
193
211
|
coupled change across nodes.
|
|
@@ -211,16 +229,15 @@ each node's answer. Budget accordingly:
|
|
|
211
229
|
One completion message per graph:
|
|
212
230
|
|
|
213
231
|
```text
|
|
214
|
-
### workflow auth-fix completed (
|
|
232
|
+
### workflow auth-fix completed (3/3 completed)
|
|
215
233
|
- map (scout) completed · run #12 · 1m02s
|
|
216
234
|
- fix (artisan) completed · run #13 · 4m11s
|
|
217
|
-
-
|
|
218
|
-
- gate (artisan) completed · run #16 · 6m05s
|
|
235
|
+
- review (artisan) completed · run #15 · 2m40s
|
|
219
236
|
|
|
220
237
|
Node answers:
|
|
221
238
|
#### map
|
|
222
239
|
…
|
|
223
|
-
Totals:
|
|
240
|
+
Totals: 3 nodes · anthropic/claude-sonnet-4: ↓48.0k $0.8200
|
|
224
241
|
```
|
|
225
242
|
|
|
226
243
|
- Node answers are clipped here; the full report file is named when that happens.
|
|
@@ -16,7 +16,7 @@ import { dirname, join } from "node:path";
|
|
|
16
16
|
import { getAgentDir, withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
17
17
|
|
|
18
18
|
/** Full catalog of agents shipped with the package; all of them always run. */
|
|
19
|
-
export const BUILTIN_AGENT_NAMES = ["scout", "artisan"] as const;
|
|
19
|
+
export const BUILTIN_AGENT_NAMES = ["scout", "artisan", "reviewer", "critic"] as const;
|
|
20
20
|
|
|
21
21
|
/** Thinking levels accepted by pi's `--thinking` option. */
|
|
22
22
|
const THINKING_LEVEL_VALUES = ["off", "minimal", "low", "medium", "high", "xhigh", "max"] as const;
|
|
@@ -30,6 +30,8 @@ export function roleThinkingLevel(agentName: string): ThinkingLevel {
|
|
|
30
30
|
case "scout":
|
|
31
31
|
return "low";
|
|
32
32
|
case "artisan":
|
|
33
|
+
case "reviewer":
|
|
34
|
+
case "critic":
|
|
33
35
|
return "high";
|
|
34
36
|
default:
|
|
35
37
|
return DEFAULT_THINKING_LEVEL;
|
|
@@ -46,12 +48,20 @@ export interface AgentProfile {
|
|
|
46
48
|
|
|
47
49
|
export const AGENT_PROFILES: Record<(typeof BUILTIN_AGENT_NAMES)[number], AgentProfile> = {
|
|
48
50
|
scout: {
|
|
49
|
-
summary: "
|
|
50
|
-
remark: "
|
|
51
|
+
summary: "Research · read-only",
|
|
52
|
+
remark: "Bounded code, documentation, or web research with citations. No commands or edits.",
|
|
51
53
|
},
|
|
52
54
|
artisan: {
|
|
53
|
-
summary: "
|
|
54
|
-
remark: "
|
|
55
|
+
summary: "Implement · code & checks",
|
|
56
|
+
remark: "Implement one substantial change with its tests and verification.",
|
|
57
|
+
},
|
|
58
|
+
reviewer: {
|
|
59
|
+
summary: "Review · correctness",
|
|
60
|
+
remark: "Review code for actionable defects and verify findings without editing files.",
|
|
61
|
+
},
|
|
62
|
+
critic: {
|
|
63
|
+
summary: "Challenge · adversarial review",
|
|
64
|
+
remark: "Challenge changes or findings with counterexamples and independent verification.",
|
|
55
65
|
},
|
|
56
66
|
};
|
|
57
67
|
|
|
@@ -1,16 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Interactive configuration wizard for /subagents-setup.
|
|
3
|
-
*
|
|
4
|
-
* The catalog is fixed: scout and artisan are always enabled. The menu lets the
|
|
5
|
-
* user pick a model and thinking level for each role — that is the entire
|
|
6
|
-
* configuration surface.
|
|
7
|
-
*/
|
|
1
|
+
/** Role-based settings. Each field saves independently; Esc always goes back. */
|
|
8
2
|
|
|
9
3
|
import { stat } from "node:fs/promises";
|
|
10
|
-
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
11
4
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
12
5
|
import {
|
|
13
|
-
AGENT_PROFILES,
|
|
14
6
|
BUILTIN_AGENT_NAMES,
|
|
15
7
|
DEFAULT_CONFIG,
|
|
16
8
|
agentProfile,
|
|
@@ -29,12 +21,12 @@ import {
|
|
|
29
21
|
buildModelPickerItems,
|
|
30
22
|
currentModelRef,
|
|
31
23
|
findModelByRef,
|
|
32
|
-
modelRef,
|
|
33
24
|
resolveThinkingLevel,
|
|
34
25
|
supportedThinkingLevels,
|
|
35
26
|
} from "./models.ts";
|
|
36
|
-
import { promptSelectOne } from "./ui.ts";
|
|
27
|
+
import { promptRoleGrid, promptSelectOne, type RoleGridFocus, type RoleGridRow } from "./ui.ts";
|
|
37
28
|
|
|
29
|
+
const ROLE_DEFAULT = "__role_default__";
|
|
38
30
|
const THINKING_LEVEL_HINTS: Record<ThinkingLevel, string> = {
|
|
39
31
|
off: "no reasoning tokens",
|
|
40
32
|
minimal: "minimal reasoning",
|
|
@@ -54,247 +46,106 @@ async function configExists(configPath: string): Promise<boolean> {
|
|
|
54
46
|
}
|
|
55
47
|
}
|
|
56
48
|
|
|
57
|
-
|
|
58
|
-
ctx
|
|
59
|
-
title: string,
|
|
60
|
-
configuredRef: string | undefined,
|
|
61
|
-
escNote: string,
|
|
62
|
-
): Promise<string | undefined> {
|
|
63
|
-
const models = availableModelsInScope(ctx);
|
|
64
|
-
const items = buildModelPickerItems({
|
|
65
|
-
models,
|
|
66
|
-
configuredRef,
|
|
67
|
-
mainRef: currentModelRef(ctx),
|
|
68
|
-
});
|
|
69
|
-
return promptSelectOne(
|
|
70
|
-
ctx,
|
|
71
|
-
title,
|
|
72
|
-
`Type to filter by provider, model, or capability • ↑/↓ • Enter selects • Esc ${escNote}`,
|
|
73
|
-
items,
|
|
74
|
-
configuredRef ?? CURRENT_MAIN_MODEL,
|
|
75
|
-
);
|
|
49
|
+
function roleModel(ctx: ExtensionContext, config: SubagentsConfig, name: string) {
|
|
50
|
+
return findModelByRef(availableModelsInScope(ctx), config.agentModels[name]) ?? ctx.model;
|
|
76
51
|
}
|
|
77
52
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
agentModels[agentName],
|
|
90
|
-
escNote,
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function effectiveModelForChoice(
|
|
95
|
-
ctx: ExtensionContext,
|
|
96
|
-
agentName: string,
|
|
97
|
-
choice: string,
|
|
98
|
-
agentModels: Readonly<Record<string, string>>,
|
|
99
|
-
): Model<Api> | undefined {
|
|
100
|
-
const selectedModels = applyAgentModelChoice({ ...agentModels }, agentName, choice);
|
|
101
|
-
const ref = selectedModels[agentName];
|
|
102
|
-
return findModelByRef(availableModelsInScope(ctx), ref) ?? ctx.model;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** The role default is marked; picking it clears a stored override. */
|
|
106
|
-
async function pickAgentStrength(
|
|
107
|
-
ctx: ExtensionContext,
|
|
108
|
-
agentName: string,
|
|
109
|
-
model: Model<Api> | undefined,
|
|
110
|
-
current: ThinkingLevel | undefined,
|
|
111
|
-
escNote = "returns to model selection",
|
|
112
|
-
): Promise<ThinkingLevel | undefined> {
|
|
113
|
-
const supported = supportedThinkingLevels(model);
|
|
114
|
-
const roleDefault = resolveThinkingLevel(model, roleThinkingLevel(agentName));
|
|
115
|
-
if (supported.length <= 1) return roleDefault;
|
|
116
|
-
|
|
117
|
-
const currentEffective = current ? resolveThinkingLevel(model, current) : roleDefault;
|
|
118
|
-
const modelName = model ? modelRef(model) : "current main model";
|
|
119
|
-
const options = supported.map((level) => {
|
|
120
|
-
const tags = [
|
|
121
|
-
level === roleDefault ? "role default" : "",
|
|
122
|
-
current !== undefined && currentEffective === level ? "current" : "",
|
|
123
|
-
].filter(Boolean);
|
|
53
|
+
/** One row per role: every model and thinking choice stays visible together. */
|
|
54
|
+
function roleRows(ctx: ExtensionContext, config: SubagentsConfig): RoleGridRow[] {
|
|
55
|
+
const mainRef = currentModelRef(ctx) ?? "Pi default";
|
|
56
|
+
return BUILTIN_AGENT_NAMES.map((name) => {
|
|
57
|
+
const profile = agentProfile(name)!;
|
|
58
|
+
const selected = config.agentModels[name];
|
|
59
|
+
const available = !selected || Boolean(findModelByRef(availableModelsInScope(ctx), selected));
|
|
60
|
+
const preferred = config.agentThinkingLevels[name] ?? roleThinkingLevel(name);
|
|
61
|
+
const effective = resolveThinkingLevel(roleModel(ctx, config, name), preferred);
|
|
62
|
+
const clamped = effective !== preferred ? " (" + preferred + " requested)" : "";
|
|
63
|
+
const overridden = Boolean(config.agentThinkingLevels[name]);
|
|
124
64
|
return {
|
|
125
|
-
|
|
126
|
-
|
|
65
|
+
role: name,
|
|
66
|
+
thinkingOverridden: overridden,
|
|
67
|
+
model: selected ? selected + (available ? "" : " (unavailable)") : "Follow main",
|
|
68
|
+
thinking: effective + clamped,
|
|
69
|
+
description: profile.remark + " Model: " + (selected
|
|
70
|
+
? (available ? "falls back to main if it fails." : "unavailable; using main.")
|
|
71
|
+
: "follows main (" + mainRef + ") dynamically.")
|
|
72
|
+
+ " Thinking: role default " + roleThinkingLevel(name) + ", clamped to the model-supported levels. Reset restores " + name + " to Follow main and that default. * marks an override.",
|
|
127
73
|
};
|
|
128
74
|
});
|
|
129
|
-
return promptSelectOne(
|
|
130
|
-
ctx,
|
|
131
|
-
`Thinking for ${agentName}?`,
|
|
132
|
-
`${agentName} defaults to ${roleDefault} on ${modelName} • Enter selects • Esc ${escNote}`,
|
|
133
|
-
options,
|
|
134
|
-
currentEffective,
|
|
135
|
-
) as Promise<ThinkingLevel | undefined>;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function applyThinkingChoice(
|
|
139
|
-
levels: Record<string, ThinkingLevel>,
|
|
140
|
-
agentName: string,
|
|
141
|
-
strength: ThinkingLevel,
|
|
142
|
-
model: Model<Api> | undefined,
|
|
143
|
-
): Record<string, ThinkingLevel> {
|
|
144
|
-
const next = { ...levels };
|
|
145
|
-
const roleDefault = resolveThinkingLevel(model, roleThinkingLevel(agentName));
|
|
146
|
-
if (strength === roleDefault) delete next[agentName];
|
|
147
|
-
else next[agentName] = resolveThinkingLevel(model, strength);
|
|
148
|
-
return next;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
interface RoleChoice {
|
|
152
|
-
model: string;
|
|
153
|
-
strength: ThinkingLevel;
|
|
154
75
|
}
|
|
155
|
-
|
|
156
|
-
/** Model then thinking for one fixed role. Esc on the model picker backs out to
|
|
157
|
-
* the menu; Esc on the thinking picker re-asks the model. */
|
|
158
|
-
async function configureOneRole(
|
|
76
|
+
async function editSetting(
|
|
159
77
|
ctx: ExtensionContext,
|
|
160
|
-
agentName: string,
|
|
161
78
|
config: SubagentsConfig,
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
while (true) {
|
|
166
|
-
const modelChoice = await pickAgentModel(ctx, agentName, config.agentModels);
|
|
167
|
-
if (modelChoice === undefined) return undefined;
|
|
168
|
-
const model = effectiveModelForChoice(ctx, agentName, modelChoice, config.agentModels);
|
|
169
|
-
const strength = await pickAgentStrength(
|
|
170
|
-
ctx,
|
|
171
|
-
agentName,
|
|
172
|
-
model,
|
|
173
|
-
config.agentThinkingLevels[agentName],
|
|
174
|
-
);
|
|
175
|
-
if (strength === undefined) continue;
|
|
176
|
-
return { model: modelChoice, strength };
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
async function applyRoleChoice(
|
|
181
|
-
ctx: ExtensionContext,
|
|
182
|
-
configPath: string,
|
|
183
|
-
config: SubagentsConfig,
|
|
184
|
-
agentName: string,
|
|
185
|
-
choice: RoleChoice,
|
|
186
|
-
): Promise<SubagentsConfig> {
|
|
187
|
-
const agentModels = applyAgentModelChoice({ ...config.agentModels }, agentName, choice.model);
|
|
188
|
-
const model = effectiveModelForChoice(ctx, agentName, choice.model, agentModels);
|
|
79
|
+
name: string,
|
|
80
|
+
field: string,
|
|
81
|
+
): Promise<SubagentsConfig | undefined> {
|
|
189
82
|
const next: SubagentsConfig = {
|
|
190
|
-
agentModels,
|
|
191
|
-
agentThinkingLevels:
|
|
192
|
-
{ ...config.agentThinkingLevels },
|
|
193
|
-
agentName,
|
|
194
|
-
choice.strength,
|
|
195
|
-
model,
|
|
196
|
-
),
|
|
83
|
+
agentModels: { ...config.agentModels },
|
|
84
|
+
agentThinkingLevels: { ...config.agentThinkingLevels },
|
|
197
85
|
};
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** Walk both roles in order — model, then thinking — and save once at the end. */
|
|
219
|
-
async function runFullSetup(ctx: ExtensionContext, configPath: string, base: SubagentsConfig): Promise<boolean> {
|
|
220
|
-
if (!(await introduceSetup(ctx))) return false;
|
|
221
|
-
|
|
222
|
-
let agentModels = { ...base.agentModels };
|
|
223
|
-
let agentThinkingLevels = { ...base.agentThinkingLevels };
|
|
224
|
-
for (const agentName of BUILTIN_AGENT_NAMES) {
|
|
225
|
-
const profile = agentProfile(agentName);
|
|
226
|
-
if (profile) ctx.ui.notify(`${agentName}: ${profile.remark}`, "info");
|
|
227
|
-
const choice = await pickAgentModel(ctx, agentName, agentModels);
|
|
228
|
-
if (choice === undefined) return false;
|
|
229
|
-
agentModels = applyAgentModelChoice(agentModels, agentName, choice);
|
|
230
|
-
// Thinking follows the model immediately: the supported levels come from
|
|
231
|
-
// the model just picked, so this is the only moment the choice is exact.
|
|
232
|
-
const model = effectiveModelForChoice(ctx, agentName, choice, agentModels);
|
|
233
|
-
const strength = await pickAgentStrength(ctx, agentName, model, agentThinkingLevels[agentName]);
|
|
234
|
-
if (strength === undefined) return false;
|
|
235
|
-
agentThinkingLevels = applyThinkingChoice(agentThinkingLevels, agentName, strength, model);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
await saveConfig({ agentModels, agentThinkingLevels }, configPath);
|
|
239
|
-
ctx.ui.notify(`pi-subagents saved to ${configPath}.`, "info");
|
|
240
|
-
return true;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function roleRow(name: string, config: SubagentsConfig): string {
|
|
244
|
-
const profile = AGENT_PROFILES[name as keyof typeof AGENT_PROFILES];
|
|
245
|
-
const model = config.agentModels[name] ?? "current main model";
|
|
246
|
-
const thinking = config.agentThinkingLevels[name] ?? roleThinkingLevel(name);
|
|
247
|
-
return `${name} — ${profile.summary} · model: ${model} · thinking: ${thinking}`;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/** Menu: one row per fixed role showing its current route, plus a both-roles
|
|
251
|
-
* walk. Esc exits; saving happens after each change so a later Esc loses
|
|
252
|
-
* nothing. */
|
|
253
|
-
async function runMenu(ctx: ExtensionContext, configPath: string, config: SubagentsConfig): Promise<void> {
|
|
254
|
-
while (true) {
|
|
255
|
-
const rows = BUILTIN_AGENT_NAMES.map((name) => roleRow(name, config));
|
|
256
|
-
const choice = await ctx.ui.select("pi-subagents settings", [
|
|
257
|
-
...rows,
|
|
258
|
-
"Both roles — walk model and thinking for each",
|
|
259
|
-
]);
|
|
260
|
-
if (choice === undefined) return;
|
|
261
|
-
if (choice.startsWith("Both")) {
|
|
262
|
-
if (await runFullSetup(ctx, configPath, config)) return;
|
|
263
|
-
continue;
|
|
86
|
+
if (field === "reset") {
|
|
87
|
+
delete next.agentModels[name];
|
|
88
|
+
delete next.agentThinkingLevels[name];
|
|
89
|
+
} else if (field === "model") {
|
|
90
|
+
const choice = await promptSelectOne(ctx, name + " · Model",
|
|
91
|
+
"Search provider, model, or capability · Esc back without changes",
|
|
92
|
+
buildModelPickerItems({ models: availableModelsInScope(ctx), configuredRef: config.agentModels[name], mainRef: currentModelRef(ctx) }),
|
|
93
|
+
config.agentModels[name] ?? CURRENT_MAIN_MODEL);
|
|
94
|
+
if (choice === undefined) return undefined;
|
|
95
|
+
next.agentModels = applyAgentModelChoice(next.agentModels, name, choice);
|
|
96
|
+
} else if (field === "thinking") {
|
|
97
|
+
const model = roleModel(ctx, config, name);
|
|
98
|
+
const preferredDefault = roleThinkingLevel(name);
|
|
99
|
+
const effectiveDefault = resolveThinkingLevel(model, preferredDefault);
|
|
100
|
+
const supported = supportedThinkingLevels(model);
|
|
101
|
+
if (!supported.length) {
|
|
102
|
+
ctx.ui.notify("Choose a model before changing thinking; its supported levels are not available yet.", "info");
|
|
103
|
+
return undefined;
|
|
264
104
|
}
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
ctx
|
|
105
|
+
const options = [
|
|
106
|
+
{ value: ROLE_DEFAULT, label: "Role default", description: preferredDefault + (effectiveDefault !== preferredDefault ? " → " + effectiveDefault + " on this model" : "") },
|
|
107
|
+
...supported.map((level) => ({ value: level, label: level, description: THINKING_LEVEL_HINTS[level] })),
|
|
108
|
+
];
|
|
109
|
+
const current = config.agentThinkingLevels[name];
|
|
110
|
+
const choice = await promptSelectOne(ctx, name + " · Thinking",
|
|
111
|
+
"Choose reasoning effort · Role default follows model capabilities · Esc back",
|
|
112
|
+
options, current && supported.includes(current) ? current : ROLE_DEFAULT);
|
|
113
|
+
if (choice === undefined) return undefined;
|
|
114
|
+
if (choice === ROLE_DEFAULT) delete next.agentThinkingLevels[name];
|
|
115
|
+
else next.agentThinkingLevels[name] = choice as ThinkingLevel;
|
|
116
|
+
} else {
|
|
117
|
+
return undefined;
|
|
271
118
|
}
|
|
119
|
+
return next;
|
|
272
120
|
}
|
|
273
121
|
|
|
274
|
-
|
|
122
|
+
|
|
123
|
+
/** The same role overview serves first install and later edits. */
|
|
275
124
|
export async function runSetup(ctx: ExtensionContext, configPath: string = getConfigPath()): Promise<void> {
|
|
276
125
|
if (ctx.mode !== "tui") {
|
|
277
126
|
ctx.ui.notify("/subagents-setup requires Pi's interactive TUI.", "error");
|
|
278
127
|
return;
|
|
279
128
|
}
|
|
280
129
|
try {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
ctx
|
|
130
|
+
let config = await loadConfig(configPath);
|
|
131
|
+
let focus: RoleGridFocus = {};
|
|
132
|
+
let status = "Changes save immediately";
|
|
133
|
+
while (true) {
|
|
134
|
+
const choice = await promptRoleGrid(ctx, roleRows(ctx, config), focus, status);
|
|
135
|
+
if (choice === undefined) return;
|
|
136
|
+
focus = { row: choice.row, column: choice.column };
|
|
137
|
+
const next = await editSetting(ctx, config, choice.role, choice.field);
|
|
138
|
+
if (!next) continue;
|
|
139
|
+
await saveConfig(next, configPath);
|
|
140
|
+
config = next;
|
|
141
|
+
status = "Saved " + choice.role + (choice.field === "reset" ? " defaults" : " " + choice.field);
|
|
286
142
|
}
|
|
287
143
|
} catch (error) {
|
|
288
|
-
ctx.ui.notify(
|
|
144
|
+
ctx.ui.notify("pi-subagents setup failed: " + errorMessage(error), "error");
|
|
289
145
|
}
|
|
290
146
|
}
|
|
291
147
|
|
|
292
|
-
/**
|
|
293
|
-
* First start: write the shipped defaults so both roles work immediately — no
|
|
294
|
-
* model overrides means each child follows the main model — then announce the
|
|
295
|
-
* command that customizes them. The file is the marker: once it exists, later
|
|
296
|
-
* sessions stay silent.
|
|
297
|
-
*/
|
|
148
|
+
/** Write shipped defaults once so every role works immediately. */
|
|
298
149
|
export async function ensureDefaultConfig(
|
|
299
150
|
ctx: ExtensionContext,
|
|
300
151
|
configPath: string = getConfigPath(),
|
|
@@ -302,7 +153,7 @@ export async function ensureDefaultConfig(
|
|
|
302
153
|
if (await configExists(configPath)) return;
|
|
303
154
|
await saveConfig(DEFAULT_CONFIG, configPath);
|
|
304
155
|
ctx.ui.notify(
|
|
305
|
-
|
|
156
|
+
"pi-subagents: wrote " + configPath + " with all roles enabled; each follows the main model. Run /subagents-setup to pick a model and thinking level per role.",
|
|
306
157
|
"info",
|
|
307
158
|
);
|
|
308
159
|
}
|