@ferris1225/pi-subagents 4.3.32 → 4.3.34
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 +36 -0
- package/README.md +94 -2
- package/index.ts +10 -1
- package/package.json +6 -1
- package/skills/workflow/SKILL.md +230 -0
- package/src/delegation/agents.ts +1 -0
- package/src/delegation/dispatch.ts +11 -115
- package/src/delegation/prompt.ts +1 -0
- package/src/lifecycle/dispatch-core.ts +142 -0
- package/src/lifecycle/runtime.ts +28 -0
- package/src/lifecycle/thread-lifecycle.ts +36 -19
- package/src/lifecycle/thread-shared.ts +7 -0
- package/src/lifecycle/tools.ts +82 -6
- package/src/presentation/monitor.ts +17 -1
- package/src/workflow/durable.ts +195 -0
- package/src/workflow/engine.ts +660 -0
- package/src/workflow/files.ts +217 -0
- package/src/workflow/plan.ts +340 -0
- package/src/workflow/state.ts +48 -0
- package/src/workflow/tool.ts +286 -0
- package/workflows/code-review.md +57 -0
- package/workflows/migration.md +52 -0
- package/workflows/research.md +47 -0
|
@@ -0,0 +1,195 @@
|
|
|
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 { withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
18
|
+
import { readdirSync } from "node:fs";
|
|
19
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
20
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
21
|
+
import { getProjectRoot, getSubagentsRoot } from "../execution/spawn.ts";
|
|
22
|
+
import { PARKED_RECORD_MAX_AGE_MS } from "../lifecycle/durable.ts";
|
|
23
|
+
|
|
24
|
+
export const WORKFLOWS_MANIFEST_FILE_NAME = "pi-subagents-workflows.json";
|
|
25
|
+
const WORKFLOWS_MANIFEST_VERSION = 1;
|
|
26
|
+
|
|
27
|
+
export type RecordedNodeState = "pending" | "running" | "completed" | "failed" | "blocked" | "stopped";
|
|
28
|
+
|
|
29
|
+
export interface WorkflowRecordNode {
|
|
30
|
+
id: string;
|
|
31
|
+
agent: string;
|
|
32
|
+
task: string;
|
|
33
|
+
dependsOn: string[];
|
|
34
|
+
state: RecordedNodeState;
|
|
35
|
+
runId?: number;
|
|
36
|
+
error?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface WorkflowRecord {
|
|
40
|
+
workflowId: number;
|
|
41
|
+
name: string;
|
|
42
|
+
description?: string;
|
|
43
|
+
cwd: string;
|
|
44
|
+
/** How the graph stopped being live. */
|
|
45
|
+
state: "running" | "stopped" | "interrupted";
|
|
46
|
+
startedAt: number;
|
|
47
|
+
updatedAt: number;
|
|
48
|
+
stopReason?: string;
|
|
49
|
+
nodes: WorkflowRecordNode[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface WorkflowsManifest {
|
|
53
|
+
version: number;
|
|
54
|
+
records: WorkflowRecord[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const NODE_STATES = new Set<RecordedNodeState>([
|
|
58
|
+
"pending",
|
|
59
|
+
"running",
|
|
60
|
+
"completed",
|
|
61
|
+
"failed",
|
|
62
|
+
"blocked",
|
|
63
|
+
"stopped",
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
export function getWorkflowsManifestPath(configPath: string, cwd: string): string {
|
|
67
|
+
return join(getProjectRoot(configPath, cwd), WORKFLOWS_MANIFEST_FILE_NAME);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function normalizeNode(value: unknown): WorkflowRecordNode | undefined {
|
|
71
|
+
if (!value || typeof value !== "object") return undefined;
|
|
72
|
+
const raw = value as Record<string, unknown>;
|
|
73
|
+
if (typeof raw.id !== "string" || !raw.id) return undefined;
|
|
74
|
+
if (typeof raw.agent !== "string" || !raw.agent) return undefined;
|
|
75
|
+
if (typeof raw.state !== "string" || !NODE_STATES.has(raw.state as RecordedNodeState)) return undefined;
|
|
76
|
+
return {
|
|
77
|
+
id: raw.id,
|
|
78
|
+
agent: raw.agent,
|
|
79
|
+
task: typeof raw.task === "string" ? raw.task : "",
|
|
80
|
+
dependsOn: Array.isArray(raw.dependsOn)
|
|
81
|
+
? raw.dependsOn.filter((dep): dep is string => typeof dep === "string")
|
|
82
|
+
: [],
|
|
83
|
+
state: raw.state as RecordedNodeState,
|
|
84
|
+
...(typeof raw.runId === "number" && Number.isInteger(raw.runId) && raw.runId > 0 ? { runId: raw.runId } : {}),
|
|
85
|
+
...(typeof raw.error === "string" && raw.error ? { error: raw.error } : {}),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function normalizeRecord(value: unknown): WorkflowRecord | undefined {
|
|
90
|
+
if (!value || typeof value !== "object") return undefined;
|
|
91
|
+
const raw = value as Record<string, unknown>;
|
|
92
|
+
if (typeof raw.workflowId !== "number" || !Number.isInteger(raw.workflowId) || raw.workflowId < 1) return undefined;
|
|
93
|
+
if (typeof raw.name !== "string" || !raw.name) return undefined;
|
|
94
|
+
if (typeof raw.cwd !== "string" || !raw.cwd || !isAbsolute(raw.cwd)) return undefined;
|
|
95
|
+
if (raw.cwd !== resolve(raw.cwd)) return undefined;
|
|
96
|
+
if (raw.state !== "running" && raw.state !== "stopped" && raw.state !== "interrupted") return undefined;
|
|
97
|
+
if (typeof raw.startedAt !== "number" || typeof raw.updatedAt !== "number") return undefined;
|
|
98
|
+
if (!Array.isArray(raw.nodes)) return undefined;
|
|
99
|
+
const nodes = raw.nodes.map(normalizeNode);
|
|
100
|
+
if (nodes.some((node) => node === undefined)) return undefined;
|
|
101
|
+
return {
|
|
102
|
+
workflowId: raw.workflowId,
|
|
103
|
+
name: raw.name,
|
|
104
|
+
...(typeof raw.description === "string" && raw.description ? { description: raw.description } : {}),
|
|
105
|
+
cwd: raw.cwd,
|
|
106
|
+
state: raw.state,
|
|
107
|
+
startedAt: raw.startedAt,
|
|
108
|
+
updatedAt: raw.updatedAt,
|
|
109
|
+
...(typeof raw.stopReason === "string" && raw.stopReason ? { stopReason: raw.stopReason } : {}),
|
|
110
|
+
nodes: nodes as WorkflowRecordNode[],
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function readManifestRecords(path: string): Promise<WorkflowRecord[]> {
|
|
115
|
+
try {
|
|
116
|
+
const parsed = JSON.parse(await readFile(path, "utf8")) as { records?: unknown };
|
|
117
|
+
if (!Array.isArray(parsed.records)) return [];
|
|
118
|
+
return parsed.records.flatMap((record) => {
|
|
119
|
+
const normalized = normalizeRecord(record);
|
|
120
|
+
return normalized ? [normalized] : [];
|
|
121
|
+
});
|
|
122
|
+
} catch {
|
|
123
|
+
return [];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function writeManifest(path: string, records: readonly WorkflowRecord[]): Promise<void> {
|
|
128
|
+
if (records.length === 0) {
|
|
129
|
+
await rm(path, { force: true });
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
await mkdir(dirname(path), { recursive: true });
|
|
133
|
+
const temporaryPath = `${path}.${process.pid}.${Date.now()}.tmp`;
|
|
134
|
+
try {
|
|
135
|
+
const manifest: WorkflowsManifest = { version: WORKFLOWS_MANIFEST_VERSION, records: [...records] };
|
|
136
|
+
await writeFile(temporaryPath, `${JSON.stringify(manifest, null, 2)}\n`, "utf8");
|
|
137
|
+
await rename(temporaryPath, path);
|
|
138
|
+
} finally {
|
|
139
|
+
await rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function upsertWorkflowRecord(configPath: string, record: WorkflowRecord): Promise<void> {
|
|
144
|
+
const path = getWorkflowsManifestPath(configPath, record.cwd);
|
|
145
|
+
await withFileMutationQueue(path, async () => {
|
|
146
|
+
const records = await readManifestRecords(path);
|
|
147
|
+
const index = records.findIndex((candidate) => candidate.workflowId === record.workflowId);
|
|
148
|
+
const merged: WorkflowRecord = index === -1
|
|
149
|
+
? record
|
|
150
|
+
: { ...record, startedAt: records[index]!.startedAt };
|
|
151
|
+
if (index === -1) records.push(merged);
|
|
152
|
+
else records[index] = merged;
|
|
153
|
+
await writeManifest(path, records);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export async function removeWorkflowRecord(configPath: string, workflowId: number, cwd: string): Promise<void> {
|
|
158
|
+
const path = getWorkflowsManifestPath(configPath, cwd);
|
|
159
|
+
await withFileMutationQueue(path, async () => {
|
|
160
|
+
const records = await readManifestRecords(path);
|
|
161
|
+
const next = records.filter((record) => record.workflowId !== workflowId);
|
|
162
|
+
if (next.length === records.length) return;
|
|
163
|
+
await writeManifest(path, next);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Records for one checkout, newest first. */
|
|
168
|
+
export async function readWorkflowRecords(configPath: string, cwd: string): Promise<WorkflowRecord[]> {
|
|
169
|
+
const path = getWorkflowsManifestPath(configPath, cwd);
|
|
170
|
+
const records = (await readManifestRecords(path)).filter(
|
|
171
|
+
(record) => process.platform === "win32"
|
|
172
|
+
? record.cwd.toLowerCase() === resolve(cwd).toLowerCase()
|
|
173
|
+
: record.cwd === resolve(cwd),
|
|
174
|
+
);
|
|
175
|
+
return records.sort((left, right) => right.updatedAt - left.updatedAt);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Drop records no longer worth explaining. Runs at session start. */
|
|
179
|
+
export async function pruneWorkflowRecords(configPath: string, now = Date.now()): Promise<void> {
|
|
180
|
+
let projects: string[];
|
|
181
|
+
try {
|
|
182
|
+
projects = readdirSync(getSubagentsRoot(configPath), { withFileTypes: true })
|
|
183
|
+
.filter((entry) => entry.isDirectory() && !entry.isSymbolicLink())
|
|
184
|
+
.map((entry) => join(getSubagentsRoot(configPath), entry.name, WORKFLOWS_MANIFEST_FILE_NAME));
|
|
185
|
+
} catch {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
for (const path of projects) {
|
|
189
|
+
await withFileMutationQueue(path, async () => {
|
|
190
|
+
const records = await readManifestRecords(path);
|
|
191
|
+
const kept = records.filter((record) => now - record.updatedAt <= PARKED_RECORD_MAX_AGE_MS);
|
|
192
|
+
if (kept.length !== records.length) await writeManifest(path, kept);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|