@ferris1225/pi-subagents 6.0.3 → 6.0.5
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 +42 -0
- package/README.md +83 -200
- package/index.ts +8 -13
- package/package.json +1 -1
- package/src/configuration/config.ts +2 -6
- package/src/configuration/role-migration.ts +3 -14
- package/src/configuration/setup.ts +66 -57
- package/src/configuration/ui.ts +335 -64
- package/src/delegation/agents.ts +5 -6
- package/src/delegation/builtin-agents.ts +10 -31
- package/src/delegation/dispatch.ts +52 -165
- package/src/delegation/prompt.ts +29 -82
- package/src/execution/background.ts +2 -2
- package/src/execution/run-control.ts +1 -1
- package/src/execution/spawn.ts +2 -30
- package/src/isolation/worktree.ts +0 -14
- package/src/lifecycle/cancellation.ts +4 -8
- package/src/lifecycle/completion.ts +9 -21
- package/src/lifecycle/dispatch-core.ts +7 -7
- package/src/lifecycle/durable.ts +5 -29
- package/src/lifecycle/runtime.ts +11 -63
- package/src/lifecycle/stop.ts +1 -2
- package/src/lifecycle/thread-lifecycle.ts +38 -83
- package/src/lifecycle/thread-restore.ts +1 -3
- package/src/lifecycle/thread-shared.ts +10 -27
- package/src/lifecycle/tools.ts +24 -114
- package/src/presentation/announcements.ts +8 -6
- package/src/presentation/format.ts +39 -14
- package/src/presentation/monitor.ts +697 -758
- package/src/presentation/widget.ts +34 -142
- 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/durable.ts
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Durable workflow records: what a graph had completed, what it was still
|
|
3
|
-
* running, and the plan itself, so a crashed or reloaded session can still
|
|
4
|
-
* explain what was in flight.
|
|
5
|
-
*
|
|
6
|
-
* The graph is not resumed automatically — each node is an ordinary sub-agent
|
|
7
|
-
* thread with its own durable record and retained session, and main finishes
|
|
8
|
-
* that work with its own tools. This manifest keeps the *shape* of the run
|
|
9
|
-
* (which nodes settled, which were pending, which were blocked) queryable
|
|
10
|
-
* instead of leaving a pile of unrelated parked threads.
|
|
11
|
-
*
|
|
12
|
-
* One manifest per project, written atomically, like the thread manifest; a
|
|
13
|
-
* settled workflow removes its record, so the file exists exactly while
|
|
14
|
-
* unfinished work needs explaining.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { createHash } from "node:crypto";
|
|
18
|
-
import { replaceManifestFile, withManifestMutation } from "../isolation/manifest-mutation.ts";
|
|
19
|
-
import { readdirSync } from "node:fs";
|
|
20
|
-
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
21
|
-
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
22
|
-
import { getProjectRoot, getSubagentsRoot } from "../execution/spawn.ts";
|
|
23
|
-
import { PARKED_RECORD_MAX_AGE_MS } from "../lifecycle/durable.ts";
|
|
24
|
-
import { migrateStoredRecordRoles } from "../configuration/role-migration.ts";
|
|
25
|
-
|
|
26
|
-
export const WORKFLOWS_MANIFEST_FILE_NAME = "pi-subagents-workflows.json";
|
|
27
|
-
const WORKFLOWS_MANIFEST_VERSION = 1;
|
|
28
|
-
|
|
29
|
-
export type RecordedNodeState = "pending" | "running" | "completed" | "failed" | "blocked" | "stopped";
|
|
30
|
-
|
|
31
|
-
export interface WorkflowRecordNode {
|
|
32
|
-
id: string;
|
|
33
|
-
kind?: "acceptance";
|
|
34
|
-
evidenceFile?: string;
|
|
35
|
-
resultFile?: string;
|
|
36
|
-
agent: string;
|
|
37
|
-
task: string;
|
|
38
|
-
dependsOn: string[];
|
|
39
|
-
state: RecordedNodeState;
|
|
40
|
-
runId?: number;
|
|
41
|
-
/** Identifies the run across windows and local restore renumbering. */
|
|
42
|
-
durableRunId?: string;
|
|
43
|
-
error?: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface WorkflowRecord {
|
|
47
|
-
durableId?: string;
|
|
48
|
-
workflowId: number;
|
|
49
|
-
name: string;
|
|
50
|
-
description?: string;
|
|
51
|
-
cwd: string;
|
|
52
|
-
/** How the graph stopped being live. */
|
|
53
|
-
state: "running" | "stopped" | "interrupted";
|
|
54
|
-
startedAt: number;
|
|
55
|
-
updatedAt: number;
|
|
56
|
-
stopReason?: string;
|
|
57
|
-
nodes: WorkflowRecordNode[];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
interface WorkflowsManifest {
|
|
61
|
-
version: number;
|
|
62
|
-
records: WorkflowRecord[];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const NODE_STATES = new Set<RecordedNodeState>([
|
|
66
|
-
"pending",
|
|
67
|
-
"running",
|
|
68
|
-
"completed",
|
|
69
|
-
"failed",
|
|
70
|
-
"blocked",
|
|
71
|
-
"stopped",
|
|
72
|
-
]);
|
|
73
|
-
|
|
74
|
-
export function getWorkflowsManifestPath(configPath: string, cwd: string): string {
|
|
75
|
-
return join(getProjectRoot(configPath, cwd), WORKFLOWS_MANIFEST_FILE_NAME);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function normalizeNode(value: unknown): WorkflowRecordNode | undefined {
|
|
79
|
-
if (!value || typeof value !== "object") return undefined;
|
|
80
|
-
const raw = value as Record<string, unknown>;
|
|
81
|
-
if (typeof raw.id !== "string" || !raw.id) return undefined;
|
|
82
|
-
if (typeof raw.agent !== "string" || !raw.agent) return undefined;
|
|
83
|
-
if (typeof raw.state !== "string" || !NODE_STATES.has(raw.state as RecordedNodeState)) return undefined;
|
|
84
|
-
return {
|
|
85
|
-
id: raw.id,
|
|
86
|
-
...(raw.kind === "acceptance" ? { kind: "acceptance" as const } : {}),
|
|
87
|
-
...(typeof raw.evidenceFile === "string" && raw.evidenceFile ? { evidenceFile: raw.evidenceFile } : {}),
|
|
88
|
-
...(typeof raw.resultFile === "string" && raw.resultFile ? { resultFile: raw.resultFile } : {}),
|
|
89
|
-
agent: raw.agent,
|
|
90
|
-
task: typeof raw.task === "string" ? raw.task : "",
|
|
91
|
-
dependsOn: Array.isArray(raw.dependsOn)
|
|
92
|
-
? raw.dependsOn.filter((dep): dep is string => typeof dep === "string")
|
|
93
|
-
: [],
|
|
94
|
-
state: raw.state as RecordedNodeState,
|
|
95
|
-
...(typeof raw.runId === "number" && Number.isInteger(raw.runId) && raw.runId > 0 ? { runId: raw.runId } : {}),
|
|
96
|
-
...(typeof raw.durableRunId === "string" && raw.durableRunId ? { durableRunId: raw.durableRunId } : {}),
|
|
97
|
-
...(typeof raw.error === "string" && raw.error ? { error: raw.error } : {}),
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function normalizeRecord(value: unknown): WorkflowRecord | undefined {
|
|
102
|
-
if (!value || typeof value !== "object") return undefined;
|
|
103
|
-
const raw = value as Record<string, unknown>;
|
|
104
|
-
if (typeof raw.workflowId !== "number" || !Number.isInteger(raw.workflowId) || raw.workflowId < 1) return undefined;
|
|
105
|
-
if (typeof raw.name !== "string" || !raw.name) return undefined;
|
|
106
|
-
if (typeof raw.cwd !== "string" || !raw.cwd || !isAbsolute(raw.cwd)) return undefined;
|
|
107
|
-
if (raw.cwd !== resolve(raw.cwd)) return undefined;
|
|
108
|
-
if (raw.state !== "running" && raw.state !== "stopped" && raw.state !== "interrupted") return undefined;
|
|
109
|
-
if (typeof raw.startedAt !== "number" || typeof raw.updatedAt !== "number") return undefined;
|
|
110
|
-
if (!Array.isArray(raw.nodes)) return undefined;
|
|
111
|
-
const nodes = raw.nodes.map(normalizeNode);
|
|
112
|
-
if (nodes.some((node) => node === undefined)) return undefined;
|
|
113
|
-
return {
|
|
114
|
-
...(typeof raw.durableId === "string" && raw.durableId ? { durableId: raw.durableId } : {}),
|
|
115
|
-
workflowId: raw.workflowId,
|
|
116
|
-
name: raw.name,
|
|
117
|
-
...(typeof raw.description === "string" && raw.description ? { description: raw.description } : {}),
|
|
118
|
-
cwd: raw.cwd,
|
|
119
|
-
state: raw.state,
|
|
120
|
-
startedAt: raw.startedAt,
|
|
121
|
-
updatedAt: raw.updatedAt,
|
|
122
|
-
...(typeof raw.stopReason === "string" && raw.stopReason ? { stopReason: raw.stopReason } : {}),
|
|
123
|
-
nodes: nodes as WorkflowRecordNode[],
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async function readManifest(path: string): Promise<{ records: WorkflowRecord[]; rolesMigrated: boolean }> {
|
|
128
|
-
try {
|
|
129
|
-
const parsed = JSON.parse(await readFile(path, "utf8")) as { records?: unknown };
|
|
130
|
-
if (!Array.isArray(parsed.records)) return { records: [], rolesMigrated: false };
|
|
131
|
-
let rolesMigrated = false;
|
|
132
|
-
for (const record of parsed.records) rolesMigrated = migrateStoredRecordRoles(record) || rolesMigrated;
|
|
133
|
-
const records = parsed.records.flatMap((record) => {
|
|
134
|
-
const normalized = normalizeRecord(record);
|
|
135
|
-
return normalized ? [normalized] : [];
|
|
136
|
-
});
|
|
137
|
-
return { records, rolesMigrated };
|
|
138
|
-
} catch {
|
|
139
|
-
return { records: [], rolesMigrated: false };
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async function readManifestRecords(path: string): Promise<WorkflowRecord[]> {
|
|
144
|
-
return (await readManifest(path)).records;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async function writeManifest(path: string, records: readonly WorkflowRecord[]): Promise<void> {
|
|
148
|
-
if (records.length === 0) {
|
|
149
|
-
await rm(path, { force: true });
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
await mkdir(dirname(path), { recursive: true });
|
|
153
|
-
const temporaryPath = `${path}.${process.pid}.${Date.now()}.tmp`;
|
|
154
|
-
try {
|
|
155
|
-
const manifest: WorkflowsManifest = { version: WORKFLOWS_MANIFEST_VERSION, records: [...records] };
|
|
156
|
-
await writeFile(temporaryPath, `${JSON.stringify(manifest, null, 2)}\n`, "utf8");
|
|
157
|
-
await replaceManifestFile(temporaryPath, path);
|
|
158
|
-
} finally {
|
|
159
|
-
await rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** Display ids are local to a window; old manifests retain a deterministic key. */
|
|
164
|
-
export function workflowRecordIdentity(record: WorkflowRecord): string {
|
|
165
|
-
return record.durableId ?? `legacy-${createHash("sha256").update(JSON.stringify([
|
|
166
|
-
record.cwd, record.workflowId, record.startedAt, record.name,
|
|
167
|
-
])).digest("hex")}`;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export async function upsertWorkflowRecord(configPath: string, record: WorkflowRecord): Promise<void> {
|
|
171
|
-
const path = getWorkflowsManifestPath(configPath, record.cwd);
|
|
172
|
-
await withManifestMutation(path, async () => {
|
|
173
|
-
const records = await readManifestRecords(path);
|
|
174
|
-
const identity = workflowRecordIdentity(record);
|
|
175
|
-
const index = records.findIndex((candidate) => workflowRecordIdentity(candidate) === identity);
|
|
176
|
-
const merged: WorkflowRecord = {
|
|
177
|
-
...record, durableId: identity, startedAt: index === -1 ? record.startedAt : records[index]!.startedAt,
|
|
178
|
-
};
|
|
179
|
-
if (index === -1) records.push(merged);
|
|
180
|
-
else records[index] = merged;
|
|
181
|
-
await writeManifest(path, records);
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export async function removeWorkflowRecord(configPath: string, identity: string, cwd: string): Promise<void> {
|
|
186
|
-
const path = getWorkflowsManifestPath(configPath, cwd);
|
|
187
|
-
await withManifestMutation(path, async () => {
|
|
188
|
-
const records = await readManifestRecords(path);
|
|
189
|
-
const next = records.filter((record) => workflowRecordIdentity(record) !== identity);
|
|
190
|
-
if (next.length === records.length) return;
|
|
191
|
-
await writeManifest(path, next);
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/** Records for one checkout, newest first. */
|
|
196
|
-
export async function readWorkflowRecords(configPath: string, cwd: string): Promise<WorkflowRecord[]> {
|
|
197
|
-
const path = getWorkflowsManifestPath(configPath, cwd);
|
|
198
|
-
const migrated = await withManifestMutation(path, async () => {
|
|
199
|
-
const manifest = await readManifest(path);
|
|
200
|
-
if (manifest.rolesMigrated) await writeManifest(path, manifest.records);
|
|
201
|
-
return manifest.records;
|
|
202
|
-
});
|
|
203
|
-
const records = migrated.filter(
|
|
204
|
-
(record) => process.platform === "win32"
|
|
205
|
-
? record.cwd.toLowerCase() === resolve(cwd).toLowerCase()
|
|
206
|
-
: record.cwd === resolve(cwd),
|
|
207
|
-
);
|
|
208
|
-
return records.sort((left, right) => right.updatedAt - left.updatedAt);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/** Drop records no longer worth explaining. Runs at session start. */
|
|
212
|
-
export async function pruneWorkflowRecords(configPath: string, now = Date.now()): Promise<void> {
|
|
213
|
-
let projects: string[];
|
|
214
|
-
try {
|
|
215
|
-
projects = readdirSync(getSubagentsRoot(configPath), { withFileTypes: true })
|
|
216
|
-
.filter((entry) => entry.isDirectory() && !entry.isSymbolicLink() && !entry.name.endsWith(".lock"))
|
|
217
|
-
.map((entry) => join(getSubagentsRoot(configPath), entry.name, WORKFLOWS_MANIFEST_FILE_NAME));
|
|
218
|
-
} catch {
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
for (const path of projects) {
|
|
222
|
-
await withManifestMutation(path, async () => {
|
|
223
|
-
const records = await readManifestRecords(path);
|
|
224
|
-
const kept = records.filter((record) => now - record.updatedAt <= PARKED_RECORD_MAX_AGE_MS);
|
|
225
|
-
if (kept.length !== records.length) await writeManifest(path, kept);
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
}
|