@llblab/pi-actors 0.33.0 → 0.34.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/AGENTS.md +22 -11
- package/BACKLOG.md +1 -1
- package/CHANGELOG.md +22 -1
- package/dist/index.js +10 -10
- package/dist/lib/async-runs.d.ts +23 -75
- package/dist/lib/async-runs.js +49 -801
- package/dist/lib/command-templates.d.ts +3 -3
- package/dist/lib/command-templates.js +3 -3
- package/dist/lib/config.d.ts +2 -2
- package/dist/lib/config.js +5 -5
- package/dist/lib/{output.d.ts → execution-output.d.ts} +3 -3
- package/dist/lib/{output.js → execution-output.js} +3 -3
- package/dist/lib/execution.js +1 -1
- package/dist/lib/{actor-inspector-tui.d.ts → inspector.d.ts} +3 -2
- package/dist/lib/{actor-inspector-tui.js → inspector.js} +3 -2
- package/dist/lib/mailbox-loop.d.ts +3 -3
- package/dist/lib/mailbox-loop.js +3 -3
- package/dist/lib/{actor-messages.d.ts → messages.d.ts} +3 -3
- package/dist/lib/{actor-messages.js → messages.js} +3 -3
- package/dist/lib/{actor-recipe-context.d.ts → recipes-context.d.ts} +3 -2
- package/dist/lib/{actor-recipe-context.js → recipes-context.js} +2 -1
- package/dist/lib/{recipe-discovery.d.ts → recipes-discovery.d.ts} +8 -8
- package/dist/lib/{recipe-discovery.js → recipes-discovery.js} +6 -6
- package/dist/lib/registry.d.ts +1 -1
- package/dist/lib/registry.js +22 -22
- package/dist/lib/{actor-rooms.d.ts → rooms.d.ts} +4 -4
- package/dist/lib/{actor-rooms.js → rooms.js} +3 -3
- package/dist/lib/runs-artifacts.d.ts +21 -0
- package/dist/lib/runs-artifacts.js +68 -0
- package/dist/lib/runs-control.d.ts +13 -0
- package/dist/lib/runs-control.js +59 -0
- package/dist/lib/runs-identity.d.ts +5 -0
- package/dist/lib/runs-identity.js +11 -0
- package/dist/lib/runs-index.d.ts +18 -0
- package/dist/lib/runs-index.js +119 -0
- package/dist/lib/runs-mailbox.d.ts +25 -0
- package/dist/lib/runs-mailbox.js +146 -0
- package/dist/lib/runs-messages.d.ts +15 -0
- package/dist/lib/runs-messages.js +179 -0
- package/dist/lib/runs-outbox.d.ts +41 -0
- package/dist/lib/runs-outbox.js +87 -0
- package/dist/lib/runs-process.d.ts +7 -0
- package/dist/lib/runs-process.js +36 -0
- package/dist/lib/runs-retention.d.ts +8 -0
- package/dist/lib/runs-retention.js +56 -0
- package/dist/lib/runs-start.d.ts +9 -0
- package/dist/lib/runs-start.js +67 -0
- package/dist/lib/runs-status.d.ts +8 -0
- package/dist/lib/runs-status.js +60 -0
- package/dist/lib/runtime.d.ts +2 -2
- package/dist/lib/runtime.js +5 -5
- package/dist/lib/schema.d.ts +9 -0
- package/dist/lib/schema.js +40 -0
- package/dist/lib/tools-access.d.ts +19 -0
- package/dist/lib/tools-access.js +44 -0
- package/dist/lib/tools-inspect.d.ts +11 -0
- package/dist/lib/tools-inspect.js +534 -0
- package/dist/lib/tools-local.d.ts +14 -0
- package/dist/lib/tools-local.js +156 -0
- package/dist/lib/tools-mailbox.d.ts +8 -0
- package/dist/lib/tools-mailbox.js +48 -0
- package/dist/lib/tools-message.d.ts +9 -0
- package/dist/lib/tools-message.js +346 -0
- package/dist/lib/tools-register.d.ts +18 -0
- package/dist/lib/tools-register.js +40 -0
- package/dist/lib/tools-response.d.ts +21 -0
- package/dist/lib/tools-response.js +219 -0
- package/dist/lib/tools-spawn.d.ts +12 -0
- package/dist/lib/tools-spawn.js +159 -0
- package/dist/lib/tools.d.ts +8 -39
- package/dist/lib/tools.js +11 -1490
- package/dist/recipes/utility-coordinator-lock-snapshot.json +1 -1
- package/dist/scripts/actor-worker.mjs +186 -7
- package/dist/scripts/async-runner.mjs +171 -8
- package/dist/scripts/build-dist.mjs +1 -1
- package/dist/scripts/conformance.mjs +3 -3
- package/dist/scripts/coordinator.mjs +733 -11
- package/dist/scripts/locker.mjs +345 -16
- package/dist/scripts/recipe-utils.mjs +478 -14
- package/dist/scripts/validate-recipe.mjs +110 -10
- package/dist/skills/actors/SKILL.md +1 -1
- package/dist/skills/swarm/SKILL.md +1 -1
- package/docs/recipe-library.md +1 -1
- package/docs/template-recipes.md +1 -1
- package/index.ts +11 -18
- package/lib/async-runs.ts +124 -1016
- package/lib/command-templates.ts +3 -3
- package/lib/config.ts +7 -7
- package/lib/{output.ts → execution-output.ts} +3 -3
- package/lib/execution.ts +5 -1
- package/lib/{actor-inspector-tui.ts → inspector.ts} +4 -3
- package/lib/mailbox-loop.ts +3 -3
- package/lib/{actor-messages.ts → messages.ts} +3 -3
- package/lib/{actor-recipe-context.ts → recipes-context.ts} +3 -2
- package/lib/{recipe-discovery.ts → recipes-discovery.ts} +19 -19
- package/lib/{recipe-usage.ts → recipes-usage.ts} +2 -2
- package/lib/registry.ts +37 -24
- package/lib/{actor-rooms.ts → rooms.ts} +4 -4
- package/lib/runs-artifacts.ts +96 -0
- package/lib/runs-control.ts +85 -0
- package/lib/runs-identity.ts +14 -0
- package/lib/runs-index.ts +156 -0
- package/lib/runs-mailbox.ts +208 -0
- package/lib/runs-messages.ts +252 -0
- package/lib/runs-outbox.ts +144 -0
- package/lib/runs-process.ts +51 -0
- package/lib/runs-retention.ts +77 -0
- package/lib/runs-start.ts +98 -0
- package/lib/runs-status.ts +78 -0
- package/lib/runtime.ts +6 -6
- package/lib/schema.ts +55 -0
- package/lib/tools-access.ts +68 -0
- package/lib/tools-inspect.ts +735 -0
- package/lib/tools-local.ts +234 -0
- package/lib/tools-mailbox.ts +56 -0
- package/lib/tools-message.ts +483 -0
- package/lib/tools-register.ts +64 -0
- package/lib/tools-response.ts +267 -0
- package/lib/tools-spawn.ts +240 -0
- package/lib/tools.ts +20 -2040
- package/package.json +1 -1
- package/recipes/utility-coordinator-lock-snapshot.json +1 -1
- package/scripts/actor-worker.mjs +186 -7
- package/scripts/async-runner.mjs +171 -8
- package/scripts/build-dist.mjs +1 -1
- package/scripts/conformance.mjs +3 -3
- package/scripts/coordinator.mjs +733 -11
- package/scripts/locker.mjs +345 -16
- package/scripts/recipe-utils.mjs +478 -14
- package/scripts/validate-recipe.mjs +110 -10
- package/skills/actors/SKILL.md +1 -1
- package/skills/swarm/SKILL.md +1 -1
- package/dist/lib/actor-worker.d.ts +0 -16
- package/dist/lib/actor-worker.js +0 -134
- package/dist/lib/async-runner.d.ts +0 -5
- package/dist/lib/async-runner.js +0 -134
- package/dist/lib/coordinator.d.ts +0 -5
- package/dist/lib/coordinator.js +0 -660
- package/dist/lib/locker.d.ts +0 -5
- package/dist/lib/locker.js +0 -310
- package/dist/lib/recipe-utils.d.ts +0 -5
- package/dist/lib/recipe-utils.js +0 -385
- package/dist/lib/validate-recipe.d.ts +0 -6
- package/dist/lib/validate-recipe.js +0 -104
- package/lib/actor-worker.ts +0 -174
- package/lib/async-runner.ts +0 -173
- package/lib/coordinator.ts +0 -742
- package/lib/locker.ts +0 -340
- package/lib/recipe-utils.ts +0 -486
- package/lib/validate-recipe.ts +0 -110
- /package/dist/lib/{recipe-references.d.ts → recipes-references.d.ts} +0 -0
- /package/dist/lib/{recipe-references.js → recipes-references.js} +0 -0
- /package/dist/lib/{recipe-usage.d.ts → recipes-usage.d.ts} +0 -0
- /package/dist/lib/{recipe-usage.js → recipes-usage.js} +0 -0
- /package/lib/{recipe-references.ts → recipes-references.ts} +0 -0
package/dist/lib/recipe-utils.js
DELETED
|
@@ -1,385 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Recipe utility command bundle entrypoint logic.
|
|
3
|
-
* Zones: recipe utilities, deterministic helper subcommands
|
|
4
|
-
*/
|
|
5
|
-
// @ts-nocheck
|
|
6
|
-
import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync, } from "node:fs";
|
|
7
|
-
import { dirname, extname, join, relative, resolve } from "node:path";
|
|
8
|
-
function usage() {
|
|
9
|
-
console.error(`Usage:
|
|
10
|
-
recipe-utils.mjs run-summary <state-root>
|
|
11
|
-
recipe-utils.mjs run-ops-snapshot <state-root> <run-id> [lines] [stale-minutes]
|
|
12
|
-
recipe-utils.mjs playlist <source-dir> [extensions] [max-depth] [paths|m3u|inline]
|
|
13
|
-
recipe-utils.mjs changelog-section <file> <version>
|
|
14
|
-
recipe-utils.mjs artifact-manifest <artifact-path> <title> <status> [summary]
|
|
15
|
-
recipe-utils.mjs artifact-write <artifact-path> [create|overwrite|append]
|
|
16
|
-
recipe-utils.mjs actor-message <type> [to] [from] [summary] [metadata-json] [correlation-id] [reply-to]
|
|
17
|
-
recipe-utils.mjs package-summary <package-json>
|
|
18
|
-
recipe-utils.mjs skill-summary <skill-md> [package-json]`);
|
|
19
|
-
}
|
|
20
|
-
function fail(message) {
|
|
21
|
-
console.error(message);
|
|
22
|
-
process.exit(1);
|
|
23
|
-
}
|
|
24
|
-
const ADDRESS_PATTERN = /^[A-Za-z0-9_.-]+$/;
|
|
25
|
-
const MESSAGE_TYPE_PATTERN = /^[A-Za-z][A-Za-z0-9_.:-]*$/;
|
|
26
|
-
function assertToken(value, label) {
|
|
27
|
-
const normalized = String(value ?? "").trim();
|
|
28
|
-
if (!normalized)
|
|
29
|
-
fail(`${label} is required`);
|
|
30
|
-
if (!ADDRESS_PATTERN.test(normalized))
|
|
31
|
-
fail(`${label} contains unsupported characters: ${value}`);
|
|
32
|
-
return normalized;
|
|
33
|
-
}
|
|
34
|
-
function validateActorAddress(address, label) {
|
|
35
|
-
const value = String(address ?? "").trim();
|
|
36
|
-
if (value === "coordinator")
|
|
37
|
-
return value;
|
|
38
|
-
const separator = value.indexOf(":");
|
|
39
|
-
if (separator < 0)
|
|
40
|
-
fail(`${label} must include an actor kind: ${address}`);
|
|
41
|
-
const kind = value.slice(0, separator);
|
|
42
|
-
const rest = value.slice(separator + 1);
|
|
43
|
-
if (kind === "branch") {
|
|
44
|
-
const [run, branch, ...extra] = rest.split("/");
|
|
45
|
-
if (extra.length > 0)
|
|
46
|
-
fail(`${label} branch address has too many parts: ${address}`);
|
|
47
|
-
return `branch:${assertToken(run, `${label} branch run`)}/${assertToken(branch, `${label} branch id`)}`;
|
|
48
|
-
}
|
|
49
|
-
if (["run", "session", "tool"].includes(kind))
|
|
50
|
-
return `${kind}:${assertToken(rest, label)}`;
|
|
51
|
-
fail(`${label} has unsupported actor kind: ${kind}`);
|
|
52
|
-
}
|
|
53
|
-
function validateMessageType(type) {
|
|
54
|
-
const value = String(type ?? "").trim();
|
|
55
|
-
if (!MESSAGE_TYPE_PATTERN.test(value))
|
|
56
|
-
fail(`Invalid actor message type: ${type}`);
|
|
57
|
-
return value;
|
|
58
|
-
}
|
|
59
|
-
function walkFiles(dir, maxDepth = 2, depth = 0, out = []) {
|
|
60
|
-
if (depth > maxDepth || !existsSync(dir))
|
|
61
|
-
return out;
|
|
62
|
-
for (const entry of readdirSync(dir)) {
|
|
63
|
-
const path = join(dir, entry);
|
|
64
|
-
const stat = statSync(path);
|
|
65
|
-
if (stat.isDirectory())
|
|
66
|
-
walkFiles(path, maxDepth, depth + 1, out);
|
|
67
|
-
else if (stat.isFile())
|
|
68
|
-
out.push(path);
|
|
69
|
-
}
|
|
70
|
-
return out;
|
|
71
|
-
}
|
|
72
|
-
function readJson(file) {
|
|
73
|
-
if (!existsSync(file))
|
|
74
|
-
return undefined;
|
|
75
|
-
try {
|
|
76
|
-
return JSON.parse(readFileSync(file, "utf8"));
|
|
77
|
-
}
|
|
78
|
-
catch {
|
|
79
|
-
return undefined;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function getRunStatus(run, progress, result) {
|
|
83
|
-
if (progress?.phase)
|
|
84
|
-
return progress.phase;
|
|
85
|
-
if (result?.code !== undefined)
|
|
86
|
-
return result.code === 0 ? "done" : "failed";
|
|
87
|
-
return run.status ?? "unknown";
|
|
88
|
-
}
|
|
89
|
-
function collectRunSummary(rootValue) {
|
|
90
|
-
const root = resolve(rootValue.replace(/^~(?=\/|$)/, process.env.HOME ?? "~"));
|
|
91
|
-
const files = walkFiles(root, 2).filter((file) => file.endsWith("/run.json"));
|
|
92
|
-
const rows = [];
|
|
93
|
-
for (const file of files) {
|
|
94
|
-
const run = readJson(file);
|
|
95
|
-
if (!run) {
|
|
96
|
-
rows.push({
|
|
97
|
-
run: relative(root, file),
|
|
98
|
-
status: "invalid-json",
|
|
99
|
-
recipe: "",
|
|
100
|
-
updated: "",
|
|
101
|
-
});
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
const runDir = dirname(file);
|
|
105
|
-
const progress = readJson(join(runDir, "progress.json"));
|
|
106
|
-
const result = readJson(join(runDir, "result.json"));
|
|
107
|
-
rows.push({
|
|
108
|
-
run: run.run_id ?? run.run ?? relative(root, file).split("/")[0],
|
|
109
|
-
status: getRunStatus(run, progress, result),
|
|
110
|
-
recipe: run.recipe ?? run.recipe_file ?? "",
|
|
111
|
-
updated: progress?.updatedAt ??
|
|
112
|
-
result?.completedAt ??
|
|
113
|
-
run.updated_at ??
|
|
114
|
-
run.completed_at ??
|
|
115
|
-
run.started_at ??
|
|
116
|
-
"",
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
rows.sort((a, b) => `${a.status}:${a.run}`.localeCompare(`${b.status}:${b.run}`));
|
|
120
|
-
return rows;
|
|
121
|
-
}
|
|
122
|
-
function runSummary(rootValue) {
|
|
123
|
-
console.log(JSON.stringify(collectRunSummary(rootValue), null, 2));
|
|
124
|
-
}
|
|
125
|
-
function tailJsonl(fileValue, linesValue = "80") {
|
|
126
|
-
const file = resolve(fileValue.replace(/^~(?=\/|$)/, process.env.HOME ?? "~"));
|
|
127
|
-
if (!existsSync(file))
|
|
128
|
-
return [];
|
|
129
|
-
const lines = Number.parseInt(linesValue, 10);
|
|
130
|
-
const count = Number.isFinite(lines) && lines > 0 ? lines : 80;
|
|
131
|
-
return readFileSync(file, "utf8")
|
|
132
|
-
.trimEnd()
|
|
133
|
-
.split("\n")
|
|
134
|
-
.filter(Boolean)
|
|
135
|
-
.slice(-count)
|
|
136
|
-
.map((line) => {
|
|
137
|
-
try {
|
|
138
|
-
return JSON.parse(line);
|
|
139
|
-
}
|
|
140
|
-
catch {
|
|
141
|
-
return { raw: line };
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
function runOpsSnapshot(rootValue, runIdValue = "music", linesValue = "80", staleMinutesValue = "60") {
|
|
146
|
-
const runs = collectRunSummary(rootValue);
|
|
147
|
-
const root = resolve(rootValue.replace(/^~(?=\/|$)/, process.env.HOME ?? "~"));
|
|
148
|
-
const inspectedRun = String(runIdValue || "music");
|
|
149
|
-
const messageFile = join(root, inspectedRun, "outbox.jsonl");
|
|
150
|
-
const staleMs = Number(staleMinutesValue) * 60 * 1000;
|
|
151
|
-
const now = Date.now();
|
|
152
|
-
const recommendations = runs.flatMap((run) => {
|
|
153
|
-
const updatedMs = Date.parse(run.updated || "");
|
|
154
|
-
const stale = Number.isFinite(updatedMs) &&
|
|
155
|
-
Number.isFinite(staleMs) &&
|
|
156
|
-
now - updatedMs > staleMs;
|
|
157
|
-
if (run.status === "running" && stale) {
|
|
158
|
-
return [
|
|
159
|
-
{
|
|
160
|
-
run: run.run,
|
|
161
|
-
reason: "running-stale",
|
|
162
|
-
suggested_message: {
|
|
163
|
-
to: `run:${run.run}`,
|
|
164
|
-
type: "control.stop",
|
|
165
|
-
body: "stop",
|
|
166
|
-
},
|
|
167
|
-
},
|
|
168
|
-
];
|
|
169
|
-
}
|
|
170
|
-
if (["failed", "exited", "killed"].includes(run.status)) {
|
|
171
|
-
return [
|
|
172
|
-
{
|
|
173
|
-
run: run.run,
|
|
174
|
-
reason: `terminal-${run.status}`,
|
|
175
|
-
suggested_inspect: { target: `run:${run.run}`, view: "tail" },
|
|
176
|
-
},
|
|
177
|
-
];
|
|
178
|
-
}
|
|
179
|
-
return [];
|
|
180
|
-
});
|
|
181
|
-
console.log(JSON.stringify({
|
|
182
|
-
runs,
|
|
183
|
-
inspectedRun,
|
|
184
|
-
messages: tailJsonl(messageFile, linesValue),
|
|
185
|
-
recommendations,
|
|
186
|
-
}, null, 2));
|
|
187
|
-
}
|
|
188
|
-
function playlist(sourceValue, extensionsValue = ".mp3,.ogg,.wav,.flac,.m4a", maxDepthValue = "2", outputMode = "paths") {
|
|
189
|
-
const source = resolve(sourceValue.replace(/^~(?=\/|$)/, process.env.HOME ?? "~"));
|
|
190
|
-
const maxDepth = Number.parseInt(maxDepthValue, 10);
|
|
191
|
-
const extensions = new Set(extensionsValue
|
|
192
|
-
.split(",")
|
|
193
|
-
.map((item) => item.trim().toLowerCase())
|
|
194
|
-
.filter(Boolean));
|
|
195
|
-
const files = walkFiles(source, Number.isFinite(maxDepth) ? maxDepth : 2)
|
|
196
|
-
.filter((file) => extensions.has(extname(file).toLowerCase()))
|
|
197
|
-
.sort((a, b) => a.localeCompare(b));
|
|
198
|
-
if (outputMode === "m3u")
|
|
199
|
-
console.log(["#EXTM3U", ...files].join("\n"));
|
|
200
|
-
else if (outputMode === "inline")
|
|
201
|
-
console.log(files.join("|"));
|
|
202
|
-
else
|
|
203
|
-
console.log(files.join("\n"));
|
|
204
|
-
}
|
|
205
|
-
function artifactManifest(pathValue, title = "Artifact", status = "draft", summary = "") {
|
|
206
|
-
const path = resolve(pathValue.replace(/^~(?=\/|$)/, process.env.HOME ?? "~"));
|
|
207
|
-
const exists = existsSync(path);
|
|
208
|
-
const stat = exists ? statSync(path) : undefined;
|
|
209
|
-
console.log(JSON.stringify({
|
|
210
|
-
title,
|
|
211
|
-
status,
|
|
212
|
-
path,
|
|
213
|
-
exists,
|
|
214
|
-
bytes: stat?.size ?? 0,
|
|
215
|
-
modified: stat?.mtime?.toISOString?.() ?? null,
|
|
216
|
-
summary,
|
|
217
|
-
}, null, 2));
|
|
218
|
-
}
|
|
219
|
-
function artifactWrite(pathValue, mode = "create") {
|
|
220
|
-
const path = resolve(pathValue.replace(/^~(?=\/|$)/, process.env.HOME ?? "~"));
|
|
221
|
-
if (!["create", "overwrite", "append"].includes(mode)) {
|
|
222
|
-
fail(`Invalid artifact write mode: ${mode}`);
|
|
223
|
-
}
|
|
224
|
-
if (mode === "create" && existsSync(path)) {
|
|
225
|
-
fail(`Artifact already exists: ${path}`);
|
|
226
|
-
}
|
|
227
|
-
const content = readFileSync(0, "utf8");
|
|
228
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
229
|
-
if (mode === "append")
|
|
230
|
-
appendFileSync(path, content);
|
|
231
|
-
else
|
|
232
|
-
writeFileSync(path, content, "utf8");
|
|
233
|
-
const stat = statSync(path);
|
|
234
|
-
console.log(JSON.stringify({ path, mode, bytes: stat.size, written: true }, null, 2));
|
|
235
|
-
}
|
|
236
|
-
function actorMessage(type = "event", to = "coordinator", from = "run:{run_id}", summary = "", metadataValue = "", correlationId = "", replyTo = "") {
|
|
237
|
-
const messageType = validateMessageType(type);
|
|
238
|
-
const messageTo = validateActorAddress(to, "message.to");
|
|
239
|
-
const messageFrom = validateActorAddress(from, "message.from");
|
|
240
|
-
let metadata = {};
|
|
241
|
-
if (metadataValue) {
|
|
242
|
-
try {
|
|
243
|
-
const parsed = JSON.parse(metadataValue);
|
|
244
|
-
if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
|
|
245
|
-
metadata = parsed;
|
|
246
|
-
else
|
|
247
|
-
fail("Actor message metadata must be a JSON object");
|
|
248
|
-
}
|
|
249
|
-
catch (error) {
|
|
250
|
-
fail(`Invalid actor message metadata JSON: ${error.message}`);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
const bodyText = readFileSync(0, "utf8");
|
|
254
|
-
const trimmed = bodyText.trim();
|
|
255
|
-
let body = bodyText;
|
|
256
|
-
if (trimmed) {
|
|
257
|
-
try {
|
|
258
|
-
body = JSON.parse(trimmed);
|
|
259
|
-
}
|
|
260
|
-
catch {
|
|
261
|
-
body = bodyText;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
console.log(JSON.stringify({
|
|
265
|
-
to: messageTo,
|
|
266
|
-
from: messageFrom,
|
|
267
|
-
type: messageType,
|
|
268
|
-
summary: summary || messageType,
|
|
269
|
-
body,
|
|
270
|
-
...(correlationId ? { correlation_id: correlationId } : {}),
|
|
271
|
-
...(replyTo ? { reply_to: replyTo } : {}),
|
|
272
|
-
metadata,
|
|
273
|
-
}, null, 2));
|
|
274
|
-
}
|
|
275
|
-
function packageSummary(fileValue = "package.json") {
|
|
276
|
-
const file = resolve(fileValue.replace(/^~(?=\/|$)/, process.env.HOME ?? "~"));
|
|
277
|
-
const pkg = readJson(file);
|
|
278
|
-
if (!pkg)
|
|
279
|
-
fail(`Package JSON not found or invalid: ${fileValue}`);
|
|
280
|
-
const scripts = pkg.scripts && typeof pkg.scripts === "object"
|
|
281
|
-
? Object.keys(pkg.scripts).sort()
|
|
282
|
-
: [];
|
|
283
|
-
const dependencies = pkg.dependencies && typeof pkg.dependencies === "object"
|
|
284
|
-
? Object.keys(pkg.dependencies).sort()
|
|
285
|
-
: [];
|
|
286
|
-
const devDependencies = pkg.devDependencies && typeof pkg.devDependencies === "object"
|
|
287
|
-
? Object.keys(pkg.devDependencies).sort()
|
|
288
|
-
: [];
|
|
289
|
-
console.log(JSON.stringify({
|
|
290
|
-
name: pkg.name ?? "",
|
|
291
|
-
version: pkg.version ?? "",
|
|
292
|
-
type: pkg.type ?? "",
|
|
293
|
-
private: Boolean(pkg.private),
|
|
294
|
-
packageManager: pkg.packageManager ?? "",
|
|
295
|
-
files: Array.isArray(pkg.files) ? pkg.files : [],
|
|
296
|
-
bin: pkg.bin ?? null,
|
|
297
|
-
main: pkg.main ?? "",
|
|
298
|
-
exports: pkg.exports ?? null,
|
|
299
|
-
scripts,
|
|
300
|
-
dependencyCount: dependencies.length,
|
|
301
|
-
devDependencyCount: devDependencies.length,
|
|
302
|
-
dependencies,
|
|
303
|
-
devDependencies,
|
|
304
|
-
}, null, 2));
|
|
305
|
-
}
|
|
306
|
-
function parseSkillFrontmatter(content) {
|
|
307
|
-
const frontmatter = content.match(/^---\n([\s\S]*?)\n---/)?.[1] ?? "";
|
|
308
|
-
const fields = {};
|
|
309
|
-
for (const line of frontmatter.split(/\r?\n/)) {
|
|
310
|
-
const match = line.match(/^([A-Za-z_][\w-]*):\s*(.*)$/);
|
|
311
|
-
if (match)
|
|
312
|
-
fields[match[1]] = match[2].trim();
|
|
313
|
-
}
|
|
314
|
-
return { frontmatter, fields };
|
|
315
|
-
}
|
|
316
|
-
function skillSummary(skillValue, packageValue = "package.json") {
|
|
317
|
-
const skillFile = resolve(skillValue.replace(/^~(?=\/|$)/, process.env.HOME ?? "~"));
|
|
318
|
-
const packageFile = resolve(packageValue.replace(/^~(?=\/|$)/, process.env.HOME ?? "~"));
|
|
319
|
-
const content = readFileSync(skillFile, "utf8");
|
|
320
|
-
const pkg = readJson(packageFile) ?? {};
|
|
321
|
-
const { frontmatter, fields } = parseSkillFrontmatter(content);
|
|
322
|
-
const scalarLines = frontmatter
|
|
323
|
-
.split(/\r?\n/)
|
|
324
|
-
.filter((line) => /^\w+:\s*\S/.test(line));
|
|
325
|
-
const extraColonLines = scalarLines.filter((line) => (line.match(/:/g) ?? []).length > 1);
|
|
326
|
-
const body = content.replace(/^---\n[\s\S]*?\n---\n?/, "");
|
|
327
|
-
const version = fields.version ??
|
|
328
|
-
frontmatter.match(/^\s+version:\s*([^\n]+)\s*$/m)?.[1]?.trim() ??
|
|
329
|
-
"";
|
|
330
|
-
console.log(JSON.stringify({
|
|
331
|
-
path: skillValue,
|
|
332
|
-
name: fields.name ?? "",
|
|
333
|
-
description: fields.description ?? "",
|
|
334
|
-
version,
|
|
335
|
-
packageVersion: pkg.version ?? "",
|
|
336
|
-
versionMatchesPackage: version === pkg.version,
|
|
337
|
-
frontmatterExtraColonLines: extraColonLines,
|
|
338
|
-
bodyLineCount: body.split(/\r?\n/).length,
|
|
339
|
-
headings: body.split(/\r?\n/).filter((line) => /^#{1,6}\s/.test(line)),
|
|
340
|
-
}, null, 2));
|
|
341
|
-
}
|
|
342
|
-
function changelogSection(fileValue, version) {
|
|
343
|
-
const file = resolve(fileValue);
|
|
344
|
-
const lines = readFileSync(file, "utf8").split(/\r?\n/);
|
|
345
|
-
const start = lines.findIndex((line) => line.startsWith("## ") && line.includes(version));
|
|
346
|
-
if (start < 0)
|
|
347
|
-
fail(`Version section not found: ${version}`);
|
|
348
|
-
let end = lines.length;
|
|
349
|
-
for (let index = start + 1; index < lines.length; index += 1) {
|
|
350
|
-
if (lines[index].startsWith("## ")) {
|
|
351
|
-
end = index;
|
|
352
|
-
break;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
console.log(lines.slice(start, end).join("\n").trimEnd());
|
|
356
|
-
}
|
|
357
|
-
export function runRecipeUtils(argv = process.argv.slice(2)) {
|
|
358
|
-
const [command, ...args] = argv;
|
|
359
|
-
if (!command) {
|
|
360
|
-
usage();
|
|
361
|
-
process.exit(1);
|
|
362
|
-
}
|
|
363
|
-
if (command === "run-summary")
|
|
364
|
-
runSummary(args[0] ?? "~/.pi/agent/tmp/pi-actors/runs");
|
|
365
|
-
else if (command === "run-ops-snapshot")
|
|
366
|
-
runOpsSnapshot(args[0] ?? "~/.pi/agent/tmp/pi-actors/runs", args[1] ?? "music", args[2], args[3]);
|
|
367
|
-
else if (command === "playlist")
|
|
368
|
-
playlist(args[0] ?? "~/Music", args[1], args[2], args[3]);
|
|
369
|
-
else if (command === "changelog-section")
|
|
370
|
-
changelogSection(args[0] ?? "CHANGELOG.md", args[1] ?? "Unreleased");
|
|
371
|
-
else if (command === "artifact-manifest")
|
|
372
|
-
artifactManifest(args[0] ?? "artifact.md", args[1], args[2], args[3]);
|
|
373
|
-
else if (command === "artifact-write")
|
|
374
|
-
artifactWrite(args[0] ?? "artifact.md", args[1] ?? "create");
|
|
375
|
-
else if (command === "actor-message")
|
|
376
|
-
actorMessage(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
|
|
377
|
-
else if (command === "package-summary")
|
|
378
|
-
packageSummary(args[0] ?? "package.json");
|
|
379
|
-
else if (command === "skill-summary")
|
|
380
|
-
skillSummary(args[0] ?? "skills/actors/SKILL.md", args[1] ?? "package.json");
|
|
381
|
-
else {
|
|
382
|
-
usage();
|
|
383
|
-
fail(`Unknown command: ${command}`);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Template recipe validator CLI logic.
|
|
3
|
-
* Zones: recipe validation, installed script entrypoint
|
|
4
|
-
*/
|
|
5
|
-
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
6
|
-
import { homedir } from "node:os";
|
|
7
|
-
import { resolve } from "node:path";
|
|
8
|
-
import { readResolvedRecipeConfig } from "./recipe-references.js";
|
|
9
|
-
export function validateRecipeUsage() {
|
|
10
|
-
return `Usage:
|
|
11
|
-
validate-recipe.mjs <recipe-file-or-dir> [--all]
|
|
12
|
-
|
|
13
|
-
Validates one template recipe file, or all *.json/*.md files in a directory when --all is set.`;
|
|
14
|
-
}
|
|
15
|
-
function expandPath(value) {
|
|
16
|
-
return resolve(String(value).replace(/^~(?=\/|$)/, process.env.HOME ?? homedir()));
|
|
17
|
-
}
|
|
18
|
-
function templateKind(template) {
|
|
19
|
-
if (typeof template === "string")
|
|
20
|
-
return "leaf";
|
|
21
|
-
if (Array.isArray(template))
|
|
22
|
-
return "sequence";
|
|
23
|
-
if (template && typeof template === "object") {
|
|
24
|
-
const node = template;
|
|
25
|
-
if (typeof node.template === "string")
|
|
26
|
-
return "leaf";
|
|
27
|
-
if (Array.isArray(node.template))
|
|
28
|
-
return node.parallel === true ? "parallel" : "sequence";
|
|
29
|
-
if (node.parallel === true)
|
|
30
|
-
return "parallel";
|
|
31
|
-
return "object";
|
|
32
|
-
}
|
|
33
|
-
return "unknown";
|
|
34
|
-
}
|
|
35
|
-
function recipeFiles(target, all) {
|
|
36
|
-
if (!existsSync(target))
|
|
37
|
-
throw new Error(`Recipe path not found: ${target}`);
|
|
38
|
-
const stat = statSync(target);
|
|
39
|
-
if (stat.isFile())
|
|
40
|
-
return [target];
|
|
41
|
-
if (!stat.isDirectory())
|
|
42
|
-
throw new Error(`Recipe path is not a file or directory: ${target}`);
|
|
43
|
-
if (!all)
|
|
44
|
-
throw new Error("Directory validation requires --all.");
|
|
45
|
-
return readdirSync(target)
|
|
46
|
-
.filter((file) => file.endsWith(".json") || file.endsWith(".md"))
|
|
47
|
-
.sort((a, b) => a.localeCompare(b))
|
|
48
|
-
.map((file) => resolve(target, file));
|
|
49
|
-
}
|
|
50
|
-
function validateFile(file) {
|
|
51
|
-
try {
|
|
52
|
-
const config = readResolvedRecipeConfig(file);
|
|
53
|
-
if (!config?.template)
|
|
54
|
-
throw new Error("Recipe must define a non-empty template.");
|
|
55
|
-
return {
|
|
56
|
-
file,
|
|
57
|
-
ok: true,
|
|
58
|
-
name: config.name ?? "",
|
|
59
|
-
async: Boolean(config.async),
|
|
60
|
-
args: Array.isArray(config.args) ? config.args : [],
|
|
61
|
-
defaults: config.defaults && typeof config.defaults === "object"
|
|
62
|
-
? Object.keys(config.defaults).sort()
|
|
63
|
-
: [],
|
|
64
|
-
imports: config.imports && typeof config.imports === "object"
|
|
65
|
-
? Object.keys(config.imports).sort()
|
|
66
|
-
: [],
|
|
67
|
-
mailbox: config.mailbox && typeof config.mailbox === "object"
|
|
68
|
-
? {
|
|
69
|
-
accepts: Array.isArray(config.mailbox.accepts)
|
|
70
|
-
? config.mailbox.accepts
|
|
71
|
-
: [],
|
|
72
|
-
emits: Array.isArray(config.mailbox.emits)
|
|
73
|
-
? config.mailbox.emits
|
|
74
|
-
: [],
|
|
75
|
-
}
|
|
76
|
-
: undefined,
|
|
77
|
-
template: templateKind(config.template),
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
return {
|
|
82
|
-
file,
|
|
83
|
-
ok: false,
|
|
84
|
-
error: error instanceof Error ? error.message : String(error),
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
export function validateRecipes(argv) {
|
|
89
|
-
const targetArg = argv.find((arg) => !arg.startsWith("-"));
|
|
90
|
-
const all = argv.includes("--all");
|
|
91
|
-
if (!targetArg || argv.includes("--help") || argv.includes("-h")) {
|
|
92
|
-
return { help: true, ok: Boolean(targetArg), usage: validateRecipeUsage() };
|
|
93
|
-
}
|
|
94
|
-
const files = recipeFiles(expandPath(targetArg), all);
|
|
95
|
-
const results = files.map(validateFile);
|
|
96
|
-
const failed = results.filter((result) => !result.ok).length;
|
|
97
|
-
return {
|
|
98
|
-
ok: failed === 0,
|
|
99
|
-
total: results.length,
|
|
100
|
-
passed: results.length - failed,
|
|
101
|
-
failed,
|
|
102
|
-
results,
|
|
103
|
-
};
|
|
104
|
-
}
|
package/lib/actor-worker.ts
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Canonical mailbox-backed actor worker entrypoint logic.
|
|
3
|
-
* Zones: worker demo, mailbox loop recipe runtime
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { appendFileSync, mkdirSync, writeFileSync } from "node:fs";
|
|
7
|
-
import { join } from "node:path";
|
|
8
|
-
|
|
9
|
-
import { appendRoomMessage, ensureRoomMember, readBranchInboxMessages } from "./actor-rooms.ts";
|
|
10
|
-
import { handleMailboxLoopOnce, isMailboxLoopStopMessage } from "./mailbox-loop.ts";
|
|
11
|
-
|
|
12
|
-
interface ActorWorkerArgs {
|
|
13
|
-
artifact_dir?: string;
|
|
14
|
-
branch?: string;
|
|
15
|
-
poll_ms?: string | number;
|
|
16
|
-
run?: string;
|
|
17
|
-
stale_claim_ms?: string | number;
|
|
18
|
-
state_dir?: string;
|
|
19
|
-
write_artifacts?: string | boolean;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function parseActorWorkerArgs(argv: string[]): ActorWorkerArgs {
|
|
23
|
-
const args: Record<string, string | true> = {};
|
|
24
|
-
for (let index = 0; index < argv.length; index += 1) {
|
|
25
|
-
const token = argv[index];
|
|
26
|
-
if (!token.startsWith("--")) continue;
|
|
27
|
-
const key = token.slice(2).replaceAll("-", "_");
|
|
28
|
-
const next = argv[index + 1];
|
|
29
|
-
if (!next || next.startsWith("--")) {
|
|
30
|
-
args[key] = true;
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
args[key] = next;
|
|
34
|
-
index += 1;
|
|
35
|
-
}
|
|
36
|
-
return args as ActorWorkerArgs;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function sleep(ms: number): Promise<void> {
|
|
40
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function text(value: unknown): string {
|
|
44
|
-
return typeof value === "string" ? value : JSON.stringify(value);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function enabled(value: unknown): boolean {
|
|
48
|
-
return value !== false && value !== "false" && value !== "0";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function safeName(value: unknown): string {
|
|
52
|
-
return String(value ?? "unknown").replaceAll(/[^A-Za-z0-9_.-]+/g, "_");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export async function runActorWorker(argv = process.argv.slice(2)): Promise<void> {
|
|
56
|
-
const args = parseActorWorkerArgs(argv);
|
|
57
|
-
const stateDir = String(args.state_dir ?? "");
|
|
58
|
-
const run = String(args.run ?? "");
|
|
59
|
-
const branch = String(args.branch ?? "worker");
|
|
60
|
-
const pollMs = Math.max(50, Number(args.poll_ms ?? 1000));
|
|
61
|
-
const staleClaimMs = Math.max(0, Number(args.stale_claim_ms ?? 0));
|
|
62
|
-
const artifactDir = String(args.artifact_dir ?? join(stateDir, "worker-artifacts"));
|
|
63
|
-
const writeArtifacts = enabled(args.write_artifacts ?? true);
|
|
64
|
-
|
|
65
|
-
if (!stateDir || !run) {
|
|
66
|
-
throw new Error("usage: actor-worker.mjs --state-dir <dir> --run <id> [--branch worker] [--poll-ms 1000]");
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const branchAddress = `branch:${run}/${branch}`;
|
|
70
|
-
const roomAddress = `room:${run}`;
|
|
71
|
-
const journalPath = join(stateDir, "worker-events.jsonl");
|
|
72
|
-
const statusPath = join(stateDir, "worker-status.json");
|
|
73
|
-
if (writeArtifacts) mkdirSync(artifactDir, { recursive: true });
|
|
74
|
-
|
|
75
|
-
function journal(event: string, data: Record<string, unknown> = {}): void {
|
|
76
|
-
appendFileSync(
|
|
77
|
-
journalPath,
|
|
78
|
-
`${JSON.stringify({ event, ts: new Date().toISOString(), ...data })}\n`,
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function staleClaims(): number {
|
|
83
|
-
if (staleClaimMs <= 0) return 0;
|
|
84
|
-
const cutoff = Date.now() - staleClaimMs;
|
|
85
|
-
return readBranchInboxMessages(stateDir, run, branchAddress).filter((record) => {
|
|
86
|
-
if (record.status !== "claimed" || !record.claimed_at) return false;
|
|
87
|
-
const claimedAt = Date.parse(record.claimed_at);
|
|
88
|
-
return Number.isFinite(claimedAt) && claimedAt < cutoff;
|
|
89
|
-
}).length;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function status(state: string, data: Record<string, unknown> = {}): void {
|
|
93
|
-
writeFileSync(
|
|
94
|
-
statusPath,
|
|
95
|
-
`${JSON.stringify({ artifact_dir: artifactDir, branch, run, stale_claims: staleClaims(), state, ts: new Date().toISOString(), ...data }, null, 2)}\n`,
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function writeResultArtifact(id: unknown, result: string): string | undefined {
|
|
100
|
-
if (!writeArtifacts) return undefined;
|
|
101
|
-
const path = join(artifactDir, `${safeName(id)}.txt`);
|
|
102
|
-
writeFileSync(path, `${result}\n`);
|
|
103
|
-
return path;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function room(type: string, summary: string, body: Record<string, unknown> = {}): void {
|
|
107
|
-
appendRoomMessage(stateDir, "main", {
|
|
108
|
-
body,
|
|
109
|
-
from: branchAddress,
|
|
110
|
-
summary,
|
|
111
|
-
to: roomAddress,
|
|
112
|
-
type,
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
ensureRoomMember(
|
|
117
|
-
stateDir,
|
|
118
|
-
run,
|
|
119
|
-
"main",
|
|
120
|
-
branchAddress,
|
|
121
|
-
{ display: branch, role: "worker", status: "present" },
|
|
122
|
-
`${branch} joined as mailbox worker`,
|
|
123
|
-
);
|
|
124
|
-
room("awaiting_assignment", `${branch} awaiting assignment`, { branch });
|
|
125
|
-
journal("worker.started", { artifact_dir: artifactDir, branch, run, stale_claim_ms: staleClaimMs });
|
|
126
|
-
status("awaiting_assignment", { handled: 0, stale_claim_ms: staleClaimMs });
|
|
127
|
-
|
|
128
|
-
let handled = 0;
|
|
129
|
-
|
|
130
|
-
let stopping = false;
|
|
131
|
-
while (!stopping) {
|
|
132
|
-
const result = await handleMailboxLoopOnce(
|
|
133
|
-
{ address: branchAddress, kind: "branch", run, stateDir },
|
|
134
|
-
(message) => {
|
|
135
|
-
if (isMailboxLoopStopMessage(message)) {
|
|
136
|
-
stopping = true;
|
|
137
|
-
room("actor.leave", `${branch} stopping`, { branch, reason: message.type });
|
|
138
|
-
journal("worker.stopping", { id: message.id, type: message.type });
|
|
139
|
-
status("stopping", { handled, reason: message.type });
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
room("task.claim", `${branch} claimed ${message.type}`, {
|
|
143
|
-
branch,
|
|
144
|
-
id: message.id,
|
|
145
|
-
type: message.type,
|
|
146
|
-
});
|
|
147
|
-
const result = text(message.body);
|
|
148
|
-
const artifact = writeResultArtifact(message.id, result);
|
|
149
|
-
handled += 1;
|
|
150
|
-
room("task.result", `${branch} handled ${message.type}`, {
|
|
151
|
-
artifact,
|
|
152
|
-
branch,
|
|
153
|
-
id: message.id,
|
|
154
|
-
result,
|
|
155
|
-
type: message.type,
|
|
156
|
-
});
|
|
157
|
-
room("awaiting_assignment", `${branch} awaiting assignment`, { branch });
|
|
158
|
-
journal("task.handled", { artifact, id: message.id, type: message.type });
|
|
159
|
-
status("awaiting_assignment", {
|
|
160
|
-
handled,
|
|
161
|
-
last_artifact: artifact,
|
|
162
|
-
last_id: message.id,
|
|
163
|
-
last_type: message.type,
|
|
164
|
-
stale_claim_ms: staleClaimMs,
|
|
165
|
-
});
|
|
166
|
-
},
|
|
167
|
-
{ owner: branchAddress },
|
|
168
|
-
);
|
|
169
|
-
if (!result.handled) await sleep(pollMs);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
journal("worker.done", { branch, handled, run });
|
|
173
|
-
status("done", { handled });
|
|
174
|
-
}
|