@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.
Files changed (155) hide show
  1. package/AGENTS.md +22 -11
  2. package/BACKLOG.md +1 -1
  3. package/CHANGELOG.md +22 -1
  4. package/dist/index.js +10 -10
  5. package/dist/lib/async-runs.d.ts +23 -75
  6. package/dist/lib/async-runs.js +49 -801
  7. package/dist/lib/command-templates.d.ts +3 -3
  8. package/dist/lib/command-templates.js +3 -3
  9. package/dist/lib/config.d.ts +2 -2
  10. package/dist/lib/config.js +5 -5
  11. package/dist/lib/{output.d.ts → execution-output.d.ts} +3 -3
  12. package/dist/lib/{output.js → execution-output.js} +3 -3
  13. package/dist/lib/execution.js +1 -1
  14. package/dist/lib/{actor-inspector-tui.d.ts → inspector.d.ts} +3 -2
  15. package/dist/lib/{actor-inspector-tui.js → inspector.js} +3 -2
  16. package/dist/lib/mailbox-loop.d.ts +3 -3
  17. package/dist/lib/mailbox-loop.js +3 -3
  18. package/dist/lib/{actor-messages.d.ts → messages.d.ts} +3 -3
  19. package/dist/lib/{actor-messages.js → messages.js} +3 -3
  20. package/dist/lib/{actor-recipe-context.d.ts → recipes-context.d.ts} +3 -2
  21. package/dist/lib/{actor-recipe-context.js → recipes-context.js} +2 -1
  22. package/dist/lib/{recipe-discovery.d.ts → recipes-discovery.d.ts} +8 -8
  23. package/dist/lib/{recipe-discovery.js → recipes-discovery.js} +6 -6
  24. package/dist/lib/registry.d.ts +1 -1
  25. package/dist/lib/registry.js +22 -22
  26. package/dist/lib/{actor-rooms.d.ts → rooms.d.ts} +4 -4
  27. package/dist/lib/{actor-rooms.js → rooms.js} +3 -3
  28. package/dist/lib/runs-artifacts.d.ts +21 -0
  29. package/dist/lib/runs-artifacts.js +68 -0
  30. package/dist/lib/runs-control.d.ts +13 -0
  31. package/dist/lib/runs-control.js +59 -0
  32. package/dist/lib/runs-identity.d.ts +5 -0
  33. package/dist/lib/runs-identity.js +11 -0
  34. package/dist/lib/runs-index.d.ts +18 -0
  35. package/dist/lib/runs-index.js +119 -0
  36. package/dist/lib/runs-mailbox.d.ts +25 -0
  37. package/dist/lib/runs-mailbox.js +146 -0
  38. package/dist/lib/runs-messages.d.ts +15 -0
  39. package/dist/lib/runs-messages.js +179 -0
  40. package/dist/lib/runs-outbox.d.ts +41 -0
  41. package/dist/lib/runs-outbox.js +87 -0
  42. package/dist/lib/runs-process.d.ts +7 -0
  43. package/dist/lib/runs-process.js +36 -0
  44. package/dist/lib/runs-retention.d.ts +8 -0
  45. package/dist/lib/runs-retention.js +56 -0
  46. package/dist/lib/runs-start.d.ts +9 -0
  47. package/dist/lib/runs-start.js +67 -0
  48. package/dist/lib/runs-status.d.ts +8 -0
  49. package/dist/lib/runs-status.js +60 -0
  50. package/dist/lib/runtime.d.ts +2 -2
  51. package/dist/lib/runtime.js +5 -5
  52. package/dist/lib/schema.d.ts +9 -0
  53. package/dist/lib/schema.js +40 -0
  54. package/dist/lib/tools-access.d.ts +19 -0
  55. package/dist/lib/tools-access.js +44 -0
  56. package/dist/lib/tools-inspect.d.ts +11 -0
  57. package/dist/lib/tools-inspect.js +534 -0
  58. package/dist/lib/tools-local.d.ts +14 -0
  59. package/dist/lib/tools-local.js +156 -0
  60. package/dist/lib/tools-mailbox.d.ts +8 -0
  61. package/dist/lib/tools-mailbox.js +48 -0
  62. package/dist/lib/tools-message.d.ts +9 -0
  63. package/dist/lib/tools-message.js +346 -0
  64. package/dist/lib/tools-register.d.ts +18 -0
  65. package/dist/lib/tools-register.js +40 -0
  66. package/dist/lib/tools-response.d.ts +21 -0
  67. package/dist/lib/tools-response.js +219 -0
  68. package/dist/lib/tools-spawn.d.ts +12 -0
  69. package/dist/lib/tools-spawn.js +159 -0
  70. package/dist/lib/tools.d.ts +8 -39
  71. package/dist/lib/tools.js +11 -1490
  72. package/dist/recipes/utility-coordinator-lock-snapshot.json +1 -1
  73. package/dist/scripts/actor-worker.mjs +186 -7
  74. package/dist/scripts/async-runner.mjs +171 -8
  75. package/dist/scripts/build-dist.mjs +1 -1
  76. package/dist/scripts/conformance.mjs +3 -3
  77. package/dist/scripts/coordinator.mjs +733 -11
  78. package/dist/scripts/locker.mjs +345 -16
  79. package/dist/scripts/recipe-utils.mjs +478 -14
  80. package/dist/scripts/validate-recipe.mjs +110 -10
  81. package/dist/skills/actors/SKILL.md +1 -1
  82. package/dist/skills/swarm/SKILL.md +1 -1
  83. package/docs/recipe-library.md +1 -1
  84. package/docs/template-recipes.md +1 -1
  85. package/index.ts +11 -18
  86. package/lib/async-runs.ts +124 -1016
  87. package/lib/command-templates.ts +3 -3
  88. package/lib/config.ts +7 -7
  89. package/lib/{output.ts → execution-output.ts} +3 -3
  90. package/lib/execution.ts +5 -1
  91. package/lib/{actor-inspector-tui.ts → inspector.ts} +4 -3
  92. package/lib/mailbox-loop.ts +3 -3
  93. package/lib/{actor-messages.ts → messages.ts} +3 -3
  94. package/lib/{actor-recipe-context.ts → recipes-context.ts} +3 -2
  95. package/lib/{recipe-discovery.ts → recipes-discovery.ts} +19 -19
  96. package/lib/{recipe-usage.ts → recipes-usage.ts} +2 -2
  97. package/lib/registry.ts +37 -24
  98. package/lib/{actor-rooms.ts → rooms.ts} +4 -4
  99. package/lib/runs-artifacts.ts +96 -0
  100. package/lib/runs-control.ts +85 -0
  101. package/lib/runs-identity.ts +14 -0
  102. package/lib/runs-index.ts +156 -0
  103. package/lib/runs-mailbox.ts +208 -0
  104. package/lib/runs-messages.ts +252 -0
  105. package/lib/runs-outbox.ts +144 -0
  106. package/lib/runs-process.ts +51 -0
  107. package/lib/runs-retention.ts +77 -0
  108. package/lib/runs-start.ts +98 -0
  109. package/lib/runs-status.ts +78 -0
  110. package/lib/runtime.ts +6 -6
  111. package/lib/schema.ts +55 -0
  112. package/lib/tools-access.ts +68 -0
  113. package/lib/tools-inspect.ts +735 -0
  114. package/lib/tools-local.ts +234 -0
  115. package/lib/tools-mailbox.ts +56 -0
  116. package/lib/tools-message.ts +483 -0
  117. package/lib/tools-register.ts +64 -0
  118. package/lib/tools-response.ts +267 -0
  119. package/lib/tools-spawn.ts +240 -0
  120. package/lib/tools.ts +20 -2040
  121. package/package.json +1 -1
  122. package/recipes/utility-coordinator-lock-snapshot.json +1 -1
  123. package/scripts/actor-worker.mjs +186 -7
  124. package/scripts/async-runner.mjs +171 -8
  125. package/scripts/build-dist.mjs +1 -1
  126. package/scripts/conformance.mjs +3 -3
  127. package/scripts/coordinator.mjs +733 -11
  128. package/scripts/locker.mjs +345 -16
  129. package/scripts/recipe-utils.mjs +478 -14
  130. package/scripts/validate-recipe.mjs +110 -10
  131. package/skills/actors/SKILL.md +1 -1
  132. package/skills/swarm/SKILL.md +1 -1
  133. package/dist/lib/actor-worker.d.ts +0 -16
  134. package/dist/lib/actor-worker.js +0 -134
  135. package/dist/lib/async-runner.d.ts +0 -5
  136. package/dist/lib/async-runner.js +0 -134
  137. package/dist/lib/coordinator.d.ts +0 -5
  138. package/dist/lib/coordinator.js +0 -660
  139. package/dist/lib/locker.d.ts +0 -5
  140. package/dist/lib/locker.js +0 -310
  141. package/dist/lib/recipe-utils.d.ts +0 -5
  142. package/dist/lib/recipe-utils.js +0 -385
  143. package/dist/lib/validate-recipe.d.ts +0 -6
  144. package/dist/lib/validate-recipe.js +0 -104
  145. package/lib/actor-worker.ts +0 -174
  146. package/lib/async-runner.ts +0 -173
  147. package/lib/coordinator.ts +0 -742
  148. package/lib/locker.ts +0 -340
  149. package/lib/recipe-utils.ts +0 -486
  150. package/lib/validate-recipe.ts +0 -110
  151. /package/dist/lib/{recipe-references.d.ts → recipes-references.d.ts} +0 -0
  152. /package/dist/lib/{recipe-references.js → recipes-references.js} +0 -0
  153. /package/dist/lib/{recipe-usage.d.ts → recipes-usage.d.ts} +0 -0
  154. /package/dist/lib/{recipe-usage.js → recipes-usage.js} +0 -0
  155. /package/lib/{recipe-references.ts → recipes-references.ts} +0 -0
@@ -1,310 +0,0 @@
1
- /**
2
- * Local coordination locker service entrypoint logic.
3
- * Zones: coordination locks, task leases, platform control endpoint
4
- */
5
- // @ts-nocheck
6
- import { spawnSync } from "node:child_process";
7
- import { createHash } from "node:crypto";
8
- import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync, } from "node:fs";
9
- import { createServer } from "node:net";
10
- import { dirname, join, resolve } from "node:path";
11
- import readline from "node:readline";
12
- function parseArgs(argv) {
13
- const args = { mode: "serve", stateDir: "", leaseMs: 600000, lines: 20 };
14
- for (let index = 0; index < argv.length; index += 1) {
15
- const arg = argv[index];
16
- if (arg === "serve" || arg === "snapshot")
17
- args.mode = arg;
18
- else if (arg === "--state-dir")
19
- args.stateDir = argv[++index] ?? "";
20
- else if (arg === "--lease-ms")
21
- args.leaseMs = Number(argv[++index] ?? args.leaseMs);
22
- else if (arg === "--lines")
23
- args.lines = Number(argv[++index] ?? args.lines);
24
- }
25
- if (!args.stateDir)
26
- throw new Error("--state-dir is required");
27
- if (!Number.isFinite(args.leaseMs) || args.leaseMs <= 0)
28
- args.leaseMs = 600000;
29
- if (!Number.isFinite(args.lines) || args.lines <= 0)
30
- args.lines = 20;
31
- return args;
32
- }
33
- export async function runLocker(argv = process.argv.slice(2)) {
34
- const { mode, stateDir, leaseMs, lines } = parseArgs(argv);
35
- const queuePath = join(stateDir, "queue.json");
36
- const locksPath = join(stateDir, "locks.json");
37
- const journalPath = join(stateDir, "journal.jsonl");
38
- const outboxPath = join(stateDir, "outbox.jsonl");
39
- const controlPath = join(stateDir, "control.fifo");
40
- mkdirSync(stateDir, { recursive: true });
41
- function readJson(path, fallback) {
42
- if (!existsSync(path))
43
- return fallback;
44
- try {
45
- return JSON.parse(readFileSync(path, "utf8"));
46
- }
47
- catch {
48
- return fallback;
49
- }
50
- }
51
- function writeJson(path, value) {
52
- mkdirSync(dirname(path), { recursive: true });
53
- writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`, "utf8");
54
- }
55
- function getControlEndpoint() {
56
- if (process.platform !== "win32")
57
- return { path: controlPath, type: "fifo" };
58
- const hash = createHash("sha256")
59
- .update(resolve(stateDir))
60
- .digest("hex")
61
- .slice(0, 20);
62
- return { path: `\\\\.\\pipe\\pi-actors-locker-${hash}`, type: "named-pipe" };
63
- }
64
- function writeControlEndpoint(endpoint) {
65
- writeJson(join(stateDir, "control.json"), endpoint);
66
- const runPath = join(stateDir, "run.json");
67
- const run = readJson(runPath, undefined);
68
- if (run && typeof run === "object")
69
- writeJson(runPath, { ...run, control: endpoint });
70
- }
71
- function journal(event, data = {}) {
72
- appendFileSync(journalPath, `${JSON.stringify({ event, ts: new Date().toISOString(), ...data })}\n`);
73
- }
74
- function outbox(type, summary, body = {}, level = "info") {
75
- appendFileSync(outboxPath, `${JSON.stringify({ to: "coordinator", from: `run:${process.env.run_id ?? "locker"}`, type, event: type, summary, body, data: body, delivery: "followup", level, ts: new Date().toISOString() })}\n`);
76
- }
77
- function now() {
78
- return Date.now();
79
- }
80
- function cleanExpiredLocks(locks) {
81
- const current = now();
82
- const kept = {};
83
- for (const [key, lock] of Object.entries(locks)) {
84
- if (Number(lock.expiresAt) > current)
85
- kept[key] = lock;
86
- else
87
- journal("lock.expired", { resource: key, owner: lock.owner });
88
- }
89
- return kept;
90
- }
91
- function normalizeMessage(line) {
92
- const trimmed = line.trim();
93
- if (!trimmed)
94
- return undefined;
95
- if (["stop", "quit", "exit", "cancel"].includes(trimmed.toLowerCase())) {
96
- return { type: "control.stop", body: {} };
97
- }
98
- try {
99
- return JSON.parse(trimmed);
100
- }
101
- catch {
102
- return { type: "lock.enqueue", body: { task: trimmed } };
103
- }
104
- }
105
- function tailJournal(count) {
106
- if (!existsSync(journalPath))
107
- return [];
108
- return readFileSync(journalPath, "utf8")
109
- .trimEnd()
110
- .split("\n")
111
- .filter(Boolean)
112
- .slice(-count)
113
- .map((line) => {
114
- try {
115
- return JSON.parse(line);
116
- }
117
- catch {
118
- return { raw: line };
119
- }
120
- });
121
- }
122
- function printSnapshot() {
123
- const locks = cleanExpiredLocks(readJson(locksPath, {}));
124
- writeJson(locksPath, locks);
125
- const queue = readJson(queuePath, { items: [] });
126
- console.log(JSON.stringify({
127
- queueDepth: Array.isArray(queue.items) ? queue.items.length : 0,
128
- queue,
129
- locks,
130
- journal: tailJournal(lines),
131
- }, null, 2));
132
- }
133
- function nextTask(queue, locks) {
134
- const items = Array.isArray(queue.items) ? queue.items : [];
135
- const index = items.findIndex((item) => {
136
- const resources = Array.isArray(item.resources) ? item.resources : [];
137
- return resources.every((resource) => !locks[resource]);
138
- });
139
- if (index < 0)
140
- return undefined;
141
- return items.splice(index, 1)[0];
142
- }
143
- function handle(message) {
144
- const type = message.type || message.event || "lock.enqueue";
145
- const body = message.body && typeof message.body === "object" ? message.body : message;
146
- let queue = readJson(queuePath, { items: [] });
147
- let locks = cleanExpiredLocks(readJson(locksPath, {}));
148
- if (type === "control.stop" || type === "control.cancel") {
149
- writeJson(locksPath, locks);
150
- journal("control.stop", {});
151
- outbox("lock.stopped", "Locker stopped", {
152
- queueDepth: queue.items?.length ?? 0,
153
- });
154
- process.exit(0);
155
- }
156
- if (type === "lock.enqueue" || type === "coord.enqueue") {
157
- const item = {
158
- id: body.id || `task-${Date.now()}`,
159
- task: body.task ?? body,
160
- resources: body.resources ?? [],
161
- enqueuedAt: new Date().toISOString(),
162
- };
163
- queue.items = [...(queue.items ?? []), item];
164
- writeJson(queuePath, queue);
165
- writeJson(locksPath, locks);
166
- journal("lock.enqueued", { id: item.id, resources: item.resources });
167
- outbox("lock.enqueued", `Queued task ${item.id}`, {
168
- id: item.id,
169
- queueDepth: queue.items.length,
170
- });
171
- return;
172
- }
173
- if (type === "lock.claim" || type === "coord.claim") {
174
- const owner = body.owner || message.from || "worker";
175
- const item = nextTask(queue, locks);
176
- if (!item) {
177
- writeJson(queuePath, queue);
178
- writeJson(locksPath, locks);
179
- outbox("lock.empty", "No claimable task", {
180
- owner,
181
- queueDepth: queue.items?.length ?? 0,
182
- });
183
- return;
184
- }
185
- for (const resource of item.resources ?? [])
186
- locks[resource] = { owner, task: item.id, expiresAt: now() + leaseMs };
187
- writeJson(queuePath, queue);
188
- writeJson(locksPath, locks);
189
- journal("lock.assigned", {
190
- id: item.id,
191
- owner,
192
- resources: item.resources,
193
- });
194
- outbox("lock.assigned", `Assigned task ${item.id}`, { owner, task: item });
195
- return;
196
- }
197
- if (type === "lock.acquire") {
198
- const resource = body.resource;
199
- const owner = body.owner || message.from || "worker";
200
- if (!resource)
201
- throw new Error("lock.acquire body.resource is required");
202
- if (locks[resource])
203
- outbox("lock.denied", `Lock denied ${resource}`, { resource, owner, current: locks[resource] }, "warning");
204
- else {
205
- locks[resource] = { owner, expiresAt: now() + leaseMs };
206
- outbox("lock.granted", `Lock granted ${resource}`, { resource, owner });
207
- }
208
- writeJson(locksPath, locks);
209
- return;
210
- }
211
- if (type === "lock.renew") {
212
- const resource = body.resource;
213
- const owner = body.owner || message.from || "worker";
214
- if (!resource)
215
- throw new Error("lock.renew body.resource is required");
216
- const current = locks[resource];
217
- if (!current) {
218
- outbox("lock.denied", `Lock renew denied ${resource}`, { resource, owner, reason: "missing" }, "warning");
219
- }
220
- else if (current.owner !== owner) {
221
- outbox("lock.denied", `Lock renew denied ${resource}`, { resource, owner, current }, "warning");
222
- }
223
- else {
224
- locks[resource] = { ...current, expiresAt: now() + leaseMs };
225
- outbox("lock.renewed", `Lock renewed ${resource}`, { resource, owner });
226
- }
227
- writeJson(locksPath, locks);
228
- return;
229
- }
230
- if (type === "lock.release") {
231
- const resource = body.resource;
232
- if (resource)
233
- delete locks[resource];
234
- writeJson(locksPath, locks);
235
- outbox("lock.released", `Lock released ${resource}`, { resource });
236
- return;
237
- }
238
- if (type === "lock.complete" ||
239
- type === "lock.fail" ||
240
- type === "coord.complete" ||
241
- type === "coord.fail") {
242
- const eventType = type.startsWith("coord.")
243
- ? type.replace("coord.", "lock.")
244
- : type;
245
- journal(eventType, body);
246
- outbox(eventType, `${eventType} ${body.id ?? ""}`.trim(), body, eventType === "lock.fail" ? "error" : "info");
247
- writeJson(locksPath, locks);
248
- writeJson(queuePath, queue);
249
- return;
250
- }
251
- journal("lock.unknown", { type, body });
252
- outbox("lock.unknown", `Unknown message ${type}`, { type, body }, "warning");
253
- }
254
- if (mode === "snapshot") {
255
- printSnapshot();
256
- process.exit(0);
257
- }
258
- function handleLine(line) {
259
- const message = normalizeMessage(line);
260
- if (!message)
261
- return;
262
- try {
263
- handle(message);
264
- }
265
- catch (error) {
266
- const text = error instanceof Error ? error.message : String(error);
267
- journal("lock.error", { error: text });
268
- outbox("lock.error", text, { error: text }, "error");
269
- }
270
- }
271
- async function serveFifo(endpoint) {
272
- if (!existsSync(endpoint.path)) {
273
- const result = spawnSync("mkfifo", [endpoint.path]);
274
- if (result.status !== 0)
275
- throw new Error(`mkfifo failed: ${result.stderr?.toString?.() ?? ""}`);
276
- }
277
- writeControlEndpoint(endpoint);
278
- while (true) {
279
- const stream = await import("node:fs").then((fs) => fs.createReadStream(endpoint.path, { encoding: "utf8" }));
280
- const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
281
- for await (const line of rl)
282
- handleLine(line);
283
- }
284
- }
285
- async function serveNamedPipe(endpoint) {
286
- rmSync(endpoint.path, { force: true });
287
- const server = createServer((socket) => {
288
- const rl = readline.createInterface({ input: socket, crlfDelay: Infinity });
289
- rl.on("line", handleLine);
290
- });
291
- await new Promise((resolveReady, rejectReady) => {
292
- server.once("error", rejectReady);
293
- server.listen(endpoint.path, () => {
294
- server.off("error", rejectReady);
295
- writeControlEndpoint(endpoint);
296
- resolveReady();
297
- });
298
- });
299
- await new Promise(() => { });
300
- }
301
- const endpoint = getControlEndpoint();
302
- writeJson(queuePath, readJson(queuePath, { items: [] }));
303
- writeJson(locksPath, cleanExpiredLocks(readJson(locksPath, {})));
304
- journal("lock.started", { leaseMs, control: endpoint.type });
305
- outbox("lock.started", "Locker ready", { leaseMs, control: endpoint.type });
306
- if (endpoint.type === "named-pipe")
307
- await serveNamedPipe(endpoint);
308
- else
309
- await serveFifo(endpoint);
310
- }
@@ -1,5 +0,0 @@
1
- /**
2
- * Recipe utility command bundle entrypoint logic.
3
- * Zones: recipe utilities, deterministic helper subcommands
4
- */
5
- export declare function runRecipeUtils(argv?: string[]): void;