@ferris1225/pi-subagents 6.0.2 → 6.0.4
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 +23 -0
- package/README.md +71 -191
- package/index.ts +2 -12
- package/package.json +7 -7
- package/src/configuration/config.ts +2 -6
- package/src/configuration/role-migration.ts +3 -14
- package/src/configuration/schema.ts +10 -0
- package/src/configuration/setup.ts +75 -36
- package/src/configuration/ui.ts +355 -53
- package/src/delegation/agents.ts +0 -1
- package/src/delegation/builtin-agents.ts +6 -27
- package/src/delegation/dispatch.ts +29 -120
- package/src/delegation/prompt.ts +16 -48
- package/src/execution/spawn.ts +2 -2
- package/src/lifecycle/cancellation.ts +4 -8
- package/src/lifecycle/durable.ts +0 -18
- package/src/lifecycle/runtime.ts +7 -56
- package/src/lifecycle/thread-lifecycle.ts +5 -33
- package/src/lifecycle/thread-restore.ts +0 -3
- package/src/lifecycle/thread-shared.ts +1 -11
- package/src/lifecycle/tools.ts +12 -97
- package/src/presentation/monitor.ts +727 -758
- package/src/presentation/widget.ts +36 -134
- package/src/delegation/ownership.ts +0 -98
- package/src/delegation/phase-scope.ts +0 -186
- package/src/workflow/acceptance.ts +0 -67
- package/src/workflow/durable.ts +0 -228
- package/src/workflow/engine.ts +0 -816
- package/src/workflow/plan.ts +0 -409
- package/src/workflow/state.ts +0 -55
- package/src/workflow/templates.ts +0 -156
- package/src/workflow/tool.ts +0 -254
package/src/workflow/tool.ts
DELETED
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The subagent_workflow tool runs a built-in plan with named task inputs.
|
|
3
|
-
* Scheduling, isolation, and independent acceptance are owned by the runtime.
|
|
4
|
-
*
|
|
5
|
-
* The graph is validated before anything starts (see plan.ts), so an invalid
|
|
6
|
-
* plan costs zero child processes. Headless calls await the complete graph by
|
|
7
|
-
* default; UI calls return a launch receipt and receive one completion message.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { StringEnum } from "@earendil-works/pi-ai";
|
|
11
|
-
import type { ExtensionAPI, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
12
|
-
import { Text } from "@earendil-works/pi-tui";
|
|
13
|
-
import { Type } from "typebox";
|
|
14
|
-
import { isWriteCapableAgent, loadBuiltinAgents } from "../delegation/agents.ts";
|
|
15
|
-
import { defaultIsolationMode } from "../delegation/dispatch.ts";
|
|
16
|
-
import { findRuntimeWriterConflict } from "../delegation/ownership.ts";
|
|
17
|
-
import { loadConfig } from "../configuration/config.ts";
|
|
18
|
-
import type { SubagentRuntime } from "../lifecycle/runtime.ts";
|
|
19
|
-
import type { DispatchCore } from "../lifecycle/dispatch-core.ts";
|
|
20
|
-
import { cancelActiveWork } from "../lifecycle/cancellation.ts";
|
|
21
|
-
import { projectResultsRoot } from "../lifecycle/thread-shared.ts";
|
|
22
|
-
import { formatTaskSummary, monitor } from "../presentation/monitor.ts";
|
|
23
|
-
import { pruneWorkflowRecords } from "./durable.ts";
|
|
24
|
-
import {
|
|
25
|
-
findOwnedPhases,
|
|
26
|
-
formatWorkflowSummary,
|
|
27
|
-
formatWorkflowStatus,
|
|
28
|
-
persistInterruptedWorkflows,
|
|
29
|
-
readRetainedWorkflows,
|
|
30
|
-
startWorkflow,
|
|
31
|
-
workflowStatus,
|
|
32
|
-
type WorkflowStatusView,
|
|
33
|
-
} from "./engine.ts";
|
|
34
|
-
import { compileBuiltinWorkflow } from "./templates.ts";
|
|
35
|
-
|
|
36
|
-
const WorkflowParams = Type.Object({
|
|
37
|
-
workflow: StringEnum(["code-review", "research", "migration"] as const, {
|
|
38
|
-
description: "Built-in plan: code-review inspects a target; research answers a question; migration applies two planned slices. Every plan ends with independent acceptance.",
|
|
39
|
-
}),
|
|
40
|
-
wait: Type.Optional(Type.Boolean({
|
|
41
|
-
description: "Wait for all nodes, integration, and independent acceptance, then return the final summary. Defaults to true in one-shot print/JSON sessions and false in TUI/RPC. Explicit false returns a launch receipt and delivers one background summary.",
|
|
42
|
-
})),
|
|
43
|
-
inputs: Type.Record(Type.String(), Type.String({ minLength: 1, pattern: "\\S" }), {
|
|
44
|
-
description: "Named inputs. code-review: TARGET. research: QUESTION. migration: TARGET, CONTRACT, CORE_CHANGE, ADAPTER_CHANGE, CORE_SCOPE, CORE_TEST_SCOPE, ADAPTER_SCOPE, ADAPTER_TEST_SCOPE. Scope values are exact paths relative to cwd. Include existing evidence and done conditions in task inputs.",
|
|
45
|
-
}),
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
export interface WorkflowToolDetails {
|
|
49
|
-
workflow: WorkflowStatusView;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function launchReceipt(status: WorkflowStatusView): string {
|
|
53
|
-
const started = status.nodes.filter((node) => node.runId !== undefined).length;
|
|
54
|
-
const waiting = status.nodes.filter((node) => node.waitingFor).length;
|
|
55
|
-
const starting = status.nodes.length - started - waiting;
|
|
56
|
-
const lines = [
|
|
57
|
-
"workflow #" + status.id + " " + status.name + " started · " + status.nodes.length + " node" + (status.nodes.length === 1 ? "" : "s") + " (" + started + " dispatched, " + starting + " starting, " + waiting + " waiting on dependencies)",
|
|
58
|
-
...(status.description ? [status.description] : []),
|
|
59
|
-
"",
|
|
60
|
-
"Nodes:",
|
|
61
|
-
...status.nodes.map((node) => {
|
|
62
|
-
const parts = ["- " + node.id + " (" + node.agent + ")" + (node.kind === "acceptance" ? " · required independent acceptance" : "")];
|
|
63
|
-
if (node.runId !== undefined) parts.push("run #" + node.runId);
|
|
64
|
-
else if (node.waitingFor) parts.push("waits for " + node.waitingFor.join(", "));
|
|
65
|
-
else parts.push(node.state);
|
|
66
|
-
return parts.join(" · ");
|
|
67
|
-
}),
|
|
68
|
-
"",
|
|
69
|
-
"Every node is owned by this graph, including nodes waiting on dependencies. Do not perform or redispatch their work in main or another agent. Required independent acceptance runs after all authored work settles and integrates, including failed branches; the graph succeeds only when all work and acceptance pass. The graph sends one summary; continue disjoint work or pause this turn until it arrives, then finish the requested outcome. A launch receipt is not task completion. Do not poll.",
|
|
70
|
-
];
|
|
71
|
-
return lines.join("\n");
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** Compact tool-card view: header plus node rows. */
|
|
75
|
-
function formatWorkflowStatusLines(status: WorkflowStatusView, theme: Theme, maxNodes: number): string[] {
|
|
76
|
-
const header =
|
|
77
|
-
theme.fg("toolTitle", theme.bold("workflow #" + status.id + " ")) +
|
|
78
|
-
theme.fg("accent", status.name) +
|
|
79
|
-
" " +
|
|
80
|
-
theme.fg("dim", status.state);
|
|
81
|
-
const lines = [header];
|
|
82
|
-
for (const node of status.nodes.slice(0, maxNodes)) {
|
|
83
|
-
const id = node.runId === undefined ? "—" : "#" + node.runId;
|
|
84
|
-
const detail = node.waitingFor
|
|
85
|
-
? "waits for " + node.waitingFor.join(", ")
|
|
86
|
-
: node.error
|
|
87
|
-
? "error: " + formatTaskSummary(node.error, 60, false)
|
|
88
|
-
: node.activity
|
|
89
|
-
? node.activity
|
|
90
|
-
: node.state;
|
|
91
|
-
lines.push(
|
|
92
|
-
" " + theme.fg("dim", id.padEnd(6)) + theme.fg("accent", node.id) + " " +
|
|
93
|
-
theme.fg("dim", "(" + node.agent + ") " + detail),
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
if (status.nodes.length > maxNodes) lines.push(theme.fg("dim", " … +" + (status.nodes.length - maxNodes) + " more nodes"));
|
|
97
|
-
return lines;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function registerWorkflowTool(pi: ExtensionAPI, runtime: SubagentRuntime, core: DispatchCore): void {
|
|
101
|
-
// A graph the session ends under keeps its shape on disk for the next
|
|
102
|
-
// session's recovery notice.
|
|
103
|
-
runtime.shutdownHooks.push(() => persistInterruptedWorkflows(runtime));
|
|
104
|
-
|
|
105
|
-
pi.registerTool({
|
|
106
|
-
name: "subagent_workflow",
|
|
107
|
-
label: "Subagent Workflow",
|
|
108
|
-
description:
|
|
109
|
-
"Run a built-in workflow with named inputs: code-review, research, or migration. Custom graphs and workflow files are unsupported. The runtime schedules dependencies, integrates work, and requires an independent sentinel acceptance PASS. Pending nodes own their work; main must not duplicate it. Duplicate deliverables and writer conflicts reject the plan before dispatch. One-shot print/JSON calls wait by default; TUI/RPC calls run in the background. Set wait to override. Awaited calls return the summary once, after integration and acceptance. Use subagent for a single phase or independent tasks.",
|
|
110
|
-
exposure: "model-only",
|
|
111
|
-
executionMode: "parallel",
|
|
112
|
-
annotations: {
|
|
113
|
-
readOnlyHint: false,
|
|
114
|
-
destructiveHint: true,
|
|
115
|
-
idempotentHint: false,
|
|
116
|
-
openWorldHint: true,
|
|
117
|
-
},
|
|
118
|
-
parameters: WorkflowParams,
|
|
119
|
-
|
|
120
|
-
async execute(_toolCallId, params, signal, onUpdate, ctx) {
|
|
121
|
-
await runtime.durableRestore;
|
|
122
|
-
monitor.beginTurn();
|
|
123
|
-
const config = await loadConfig(runtime.configPath);
|
|
124
|
-
// All built-in roles are always available; the live tool metadata decides
|
|
125
|
-
// which of them is write-capable in this session.
|
|
126
|
-
const agents = loadBuiltinAgents();
|
|
127
|
-
const toolInfos = runtime.getToolInfos();
|
|
128
|
-
core.setEnvironment({ ctx, config, agents });
|
|
129
|
-
|
|
130
|
-
// Ignore empty legacy placeholders, which previously triggered ambiguous
|
|
131
|
-
// source errors. A supplied custom definition is always rejected.
|
|
132
|
-
const unsupported = Object.entries(params as Record<string, unknown>).filter(([key, value]) =>
|
|
133
|
-
key !== "workflow" && key !== "inputs" && key !== "wait" && value !== undefined && value !== null && value !== ""
|
|
134
|
-
&& !(Array.isArray(value) && value.length === 0),
|
|
135
|
-
).map(([key]) => key);
|
|
136
|
-
if (unsupported.length) throw new Error(`Custom workflow fields are unsupported: ${unsupported.join(", ")}. Use only workflow, inputs, and wait. No nodes were started.`);
|
|
137
|
-
const workflow = compileBuiltinWorkflow(params.workflow, params.inputs, ctx.cwd, agents.map((agent) => agent.name));
|
|
138
|
-
if (signal?.aborted) throw new Error("Workflow cancelled before any node started.");
|
|
139
|
-
|
|
140
|
-
// Re-using a phase another owner holds fails the first node and blocks the
|
|
141
|
-
// rest; reject the whole graph instead, with the owners named.
|
|
142
|
-
const owned = findOwnedPhases(runtime, workflow);
|
|
143
|
-
if (owned.length > 0) {
|
|
144
|
-
const lines = owned.map((owner) =>
|
|
145
|
-
"- node \"" + owner.nodeId + "\" → phase \"" + owner.phaseId + "\" is already owned by " + (owner.runId !== undefined ? "run #" + owner.runId : "workflow #" + owner.workflowId + " node " + owner.ownerNodeId) +
|
|
146
|
-
" (" + owner.agent + ", " + owner.state + ")",
|
|
147
|
-
);
|
|
148
|
-
throw new Error(
|
|
149
|
-
"Workflow \"" + workflow.name + "\" was not started: " + owned.length + " of " + workflow.nodes.length +
|
|
150
|
-
" node" + (owned.length === 1 ? " reuses" : "s reuse") + " a phase this session already owns.\n" +
|
|
151
|
-
lines.join("\n") +
|
|
152
|
-
"\nInspect the owning run or workflow with subagent_status and reuse its evidence. Main handles follow-up work. Retire an interrupted run with subagent_stop({ id: \"<run id>\" }) only when discarding its retained session is intended. Different deliverables need different task inputs. No nodes were started.",
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
for (const node of workflow.nodes) {
|
|
157
|
-
const agent = agents.find((candidate) => candidate.name === node.agent)!;
|
|
158
|
-
if (!node.scope || !isWriteCapableAgent(agent, toolInfos)) continue;
|
|
159
|
-
const conflict = findRuntimeWriterConflict(runtime, node.scope);
|
|
160
|
-
if (conflict) throw new Error(`Workflow node "${node.id}" scope ${conflict.overlap.left} overlaps ${conflict.owner} scope ${conflict.overlap.right}. No nodes were started.`);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const wait = params.wait ?? (ctx.hasUI === false);
|
|
164
|
-
const resultsRoot = projectResultsRoot(runtime.configPath, ctx.cwd);
|
|
165
|
-
const handle = startWorkflow(
|
|
166
|
-
{
|
|
167
|
-
runtime,
|
|
168
|
-
startBackground: core.startBackground,
|
|
169
|
-
resolveIsolation: (agentName) => {
|
|
170
|
-
const agent = agents.find((candidate) => candidate.name === agentName);
|
|
171
|
-
return defaultIsolationMode(
|
|
172
|
-
agentName,
|
|
173
|
-
undefined,
|
|
174
|
-
agent ? isWriteCapableAgent(agent, toolInfos) : undefined,
|
|
175
|
-
);
|
|
176
|
-
},
|
|
177
|
-
deliveryRoute: wait ? "await" : "background",
|
|
178
|
-
resultsRoot,
|
|
179
|
-
notify: (message, kind) => ctx.ui.notify(message, kind),
|
|
180
|
-
},
|
|
181
|
-
workflow,
|
|
182
|
-
);
|
|
183
|
-
if (wait) {
|
|
184
|
-
let cancellation: Promise<void> | undefined;
|
|
185
|
-
const onAbort = (): void => {
|
|
186
|
-
cancellation ??= cancelActiveWork(runtime, "Parent run was cancelled.");
|
|
187
|
-
};
|
|
188
|
-
const emitProgress = (): void => {
|
|
189
|
-
onUpdate?.({
|
|
190
|
-
content: [{ type: "text", text: formatWorkflowStatus(handle.run).join("\n") }],
|
|
191
|
-
details: { workflow: workflowStatus(handle.run) } satisfies WorkflowToolDetails,
|
|
192
|
-
});
|
|
193
|
-
};
|
|
194
|
-
const unsubscribe = onUpdate ? monitor.subscribe(emitProgress) : undefined;
|
|
195
|
-
try {
|
|
196
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
197
|
-
if (signal?.aborted) onAbort();
|
|
198
|
-
emitProgress();
|
|
199
|
-
const run = await handle.done;
|
|
200
|
-
await cancellation;
|
|
201
|
-
return {
|
|
202
|
-
content: [{ type: "text", text: formatWorkflowSummary(run, resultsRoot) }],
|
|
203
|
-
details: { workflow: workflowStatus(run) } satisfies WorkflowToolDetails,
|
|
204
|
-
isError: run.state !== "completed",
|
|
205
|
-
};
|
|
206
|
-
} finally {
|
|
207
|
-
signal?.removeEventListener("abort", onAbort);
|
|
208
|
-
unsubscribe?.();
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
const status = workflowStatus(handle.run);
|
|
212
|
-
return {
|
|
213
|
-
content: [{ type: "text", text: launchReceipt(status) }],
|
|
214
|
-
details: { workflow: status } satisfies WorkflowToolDetails,
|
|
215
|
-
};
|
|
216
|
-
},
|
|
217
|
-
|
|
218
|
-
renderCall(args, theme) {
|
|
219
|
-
return new Text(
|
|
220
|
-
theme.fg("toolTitle", theme.bold("subagent_workflow ")) + theme.fg("accent", args.workflow ?? "?"),
|
|
221
|
-
0,
|
|
222
|
-
0,
|
|
223
|
-
);
|
|
224
|
-
},
|
|
225
|
-
|
|
226
|
-
renderResult(result, options, theme) {
|
|
227
|
-
const details = result.details as WorkflowToolDetails | undefined;
|
|
228
|
-
if (!details?.workflow) return new Text(theme.fg("dim", "(no output)"), 0, 0);
|
|
229
|
-
const status = details.workflow;
|
|
230
|
-
const lines = formatWorkflowStatusLines(status, theme, options.expanded ? status.nodes.length : 6);
|
|
231
|
-
return new Text(lines.join("\n"), 0, 0);
|
|
232
|
-
},
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/** Session start: drop stale graph records and tell the user what the previous
|
|
237
|
-
* session left unfinished. */
|
|
238
|
-
export async function restoreWorkflowState(runtime: SubagentRuntime, ctx: ExtensionContext): Promise<void> {
|
|
239
|
-
await pruneWorkflowRecords(runtime.configPath).catch(() => undefined);
|
|
240
|
-
const retained = await readRetainedWorkflows(runtime, ctx.cwd).catch(() => []);
|
|
241
|
-
if (retained.length === 0) return;
|
|
242
|
-
const summary = retained
|
|
243
|
-
.slice(0, 3)
|
|
244
|
-
.map((record) => {
|
|
245
|
-
const done = record.nodes.filter((node) => node.state === "completed").length;
|
|
246
|
-
return "#" + record.id + " " + record.name + " (" + done + "/" + record.nodes.length + " nodes completed)";
|
|
247
|
-
})
|
|
248
|
-
.join("; ");
|
|
249
|
-
ctx.ui.notify(
|
|
250
|
-
"pi-subagents: " + retained.length + " unfinished workflow" + (retained.length === 1 ? "" : "s") +
|
|
251
|
-
" retained — " + summary + ". Inspect with subagent_status; main finishes the work.",
|
|
252
|
-
"info",
|
|
253
|
-
);
|
|
254
|
-
}
|