@llblab/pi-actors 0.23.0 → 0.24.1

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 (158) hide show
  1. package/AGENTS.md +125 -60
  2. package/BACKLOG.md +133 -176
  3. package/CHANGELOG.md +27 -0
  4. package/README.md +13 -0
  5. package/dist/fixtures/protocol/actor-message-branch.json +13 -0
  6. package/dist/fixtures/protocol/artifact-manifest.json +9 -0
  7. package/dist/fixtures/protocol/mailbox-contract.json +15 -0
  8. package/dist/fixtures/protocol/recipe-summary.json +16 -0
  9. package/dist/fixtures/protocol/room-message.json +11 -0
  10. package/dist/fixtures/protocol/room-roster.json +11 -0
  11. package/dist/fixtures/protocol/run-inbox-message.json +9 -0
  12. package/dist/fixtures/protocol/run-outbox-event.json +9 -0
  13. package/dist/fixtures/protocol/run-state.json +10 -0
  14. package/dist/index.js +3 -2
  15. package/dist/lib/actor-inspector-tui.js +5 -32
  16. package/dist/lib/actor-rooms.d.ts +8 -0
  17. package/dist/lib/actor-rooms.js +64 -20
  18. package/dist/lib/actor-worker.d.ts +13 -0
  19. package/dist/lib/actor-worker.js +86 -0
  20. package/dist/lib/async-runner.d.ts +5 -0
  21. package/dist/lib/async-runner.js +134 -0
  22. package/dist/lib/async-runs.js +9 -28
  23. package/dist/lib/conformance.d.ts +12 -0
  24. package/dist/lib/conformance.js +28 -0
  25. package/dist/lib/coordinator.d.ts +5 -0
  26. package/dist/lib/coordinator.js +574 -0
  27. package/dist/lib/locker.d.ts +5 -0
  28. package/dist/lib/locker.js +310 -0
  29. package/dist/lib/mailbox-loop.d.ts +41 -0
  30. package/dist/lib/mailbox-loop.js +62 -0
  31. package/dist/lib/observability.d.ts +2 -2
  32. package/dist/lib/observability.js +51 -53
  33. package/dist/lib/prompts.d.ts +1 -1
  34. package/dist/lib/prompts.js +1 -1
  35. package/dist/lib/recipe-references.js +25 -2
  36. package/dist/lib/recipe-utils.d.ts +5 -0
  37. package/dist/lib/recipe-utils.js +385 -0
  38. package/dist/lib/runtime-notifier.js +3 -7
  39. package/dist/lib/state-readers.d.ts +21 -0
  40. package/dist/lib/state-readers.js +74 -0
  41. package/dist/lib/tools.js +1 -1
  42. package/dist/lib/validate-recipe.d.ts +6 -0
  43. package/dist/lib/validate-recipe.js +104 -0
  44. package/dist/recipes/actor-worker.json +35 -0
  45. package/dist/recipes/coordinator-locker.json +45 -0
  46. package/dist/recipes/lens-swarm.json +66 -0
  47. package/dist/recipes/locker.json +45 -0
  48. package/dist/recipes/music-player.json +38 -0
  49. package/dist/recipes/pipeline-architect-coordinator.json +95 -0
  50. package/dist/recipes/pipeline-artifact-bundle.json +100 -0
  51. package/dist/recipes/pipeline-artifact-report.json +58 -0
  52. package/dist/recipes/pipeline-artifact-write.json +72 -0
  53. package/dist/recipes/pipeline-async-run-ops.json +70 -0
  54. package/dist/recipes/pipeline-checkpoint-continuation.json +67 -0
  55. package/dist/recipes/pipeline-development-tasking.json +81 -0
  56. package/dist/recipes/pipeline-docs-maintenance.json +80 -0
  57. package/dist/recipes/pipeline-media-library.json +59 -0
  58. package/dist/recipes/pipeline-quorum-review.json +79 -0
  59. package/dist/recipes/pipeline-release-readiness.json +110 -0
  60. package/dist/recipes/pipeline-release-summary.json +88 -0
  61. package/dist/recipes/pipeline-repo-health.json +89 -0
  62. package/dist/recipes/pipeline-research-synthesis.json +94 -0
  63. package/dist/recipes/pipeline-review-readiness.json +54 -0
  64. package/dist/recipes/pipeline-room-swarm.json +50 -0
  65. package/dist/recipes/subagent-artifact.json +32 -0
  66. package/dist/recipes/subagent-checkpoint.json +33 -0
  67. package/dist/recipes/subagent-conflict-report.json +32 -0
  68. package/dist/recipes/subagent-contradiction-map.json +33 -0
  69. package/dist/recipes/subagent-critic.json +35 -0
  70. package/dist/recipes/subagent-evidence-map.json +33 -0
  71. package/dist/recipes/subagent-followup.json +33 -0
  72. package/dist/recipes/subagent-judge.json +33 -0
  73. package/dist/recipes/subagent-merge.json +33 -0
  74. package/dist/recipes/subagent-message.json +34 -0
  75. package/dist/recipes/subagent-normalize.json +31 -0
  76. package/dist/recipes/subagent-plan.json +33 -0
  77. package/dist/recipes/subagent-prompt.json +28 -0
  78. package/dist/recipes/subagent-quorum.json +43 -0
  79. package/dist/recipes/subagent-review-coordinator.json +114 -0
  80. package/dist/recipes/subagent-review.json +37 -0
  81. package/dist/recipes/subagent-task-card.json +35 -0
  82. package/dist/recipes/subagent-tools.json +27 -0
  83. package/dist/recipes/subagent-verify.json +34 -0
  84. package/dist/recipes/subagents-prompts.json +51 -0
  85. package/dist/recipes/utility-actor-message.json +23 -0
  86. package/dist/recipes/utility-artifact-manifest.json +16 -0
  87. package/dist/recipes/utility-artifact-write.json +16 -0
  88. package/dist/recipes/utility-changelog-head.json +11 -0
  89. package/dist/recipes/utility-changelog-section.json +13 -0
  90. package/dist/recipes/utility-coordinator-lock-snapshot.json +13 -0
  91. package/dist/recipes/utility-git-log.json +11 -0
  92. package/dist/recipes/utility-git-status.json +9 -0
  93. package/dist/recipes/utility-jsonl-tail.json +10 -0
  94. package/dist/recipes/utility-markdown-index.json +14 -0
  95. package/dist/recipes/utility-package-summary.json +11 -0
  96. package/dist/recipes/utility-playlist-build.json +17 -0
  97. package/dist/recipes/utility-playlist-scan.json +11 -0
  98. package/dist/recipes/utility-run-ops-snapshot.json +17 -0
  99. package/dist/recipes/utility-run-state-files.json +13 -0
  100. package/dist/recipes/utility-run-summary.json +11 -0
  101. package/dist/recipes/utility-skill-summary.json +13 -0
  102. package/dist/recipes/utility-validate-recipe.json +13 -0
  103. package/dist/recipes/utility-validation-wrapper.json +13 -0
  104. package/dist/scripts/actor-worker.mjs +31 -0
  105. package/dist/scripts/async-runner.mjs +31 -0
  106. package/dist/scripts/build-dist.mjs +33 -0
  107. package/dist/scripts/conformance.mjs +33 -0
  108. package/dist/scripts/coordinator.mjs +31 -0
  109. package/dist/scripts/locker.mjs +33 -0
  110. package/dist/scripts/music-player.mjs +964 -0
  111. package/dist/scripts/recipe-utils.mjs +31 -0
  112. package/dist/scripts/validate-recipe.mjs +34 -0
  113. package/dist/skills/actors/SKILL.md +377 -0
  114. package/dist/skills/swarm/SKILL.md +467 -0
  115. package/dist/skills/swarm/references/development-swarm.md +596 -0
  116. package/docs/actor-messages.md +2 -2
  117. package/docs/async-runs.md +11 -0
  118. package/docs/template-recipes.md +1 -1
  119. package/fixtures/protocol/actor-message-branch.json +13 -0
  120. package/fixtures/protocol/artifact-manifest.json +9 -0
  121. package/fixtures/protocol/mailbox-contract.json +15 -0
  122. package/fixtures/protocol/recipe-summary.json +16 -0
  123. package/fixtures/protocol/room-message.json +11 -0
  124. package/fixtures/protocol/room-roster.json +11 -0
  125. package/fixtures/protocol/run-inbox-message.json +9 -0
  126. package/fixtures/protocol/run-outbox-event.json +9 -0
  127. package/fixtures/protocol/run-state.json +10 -0
  128. package/index.ts +3 -0
  129. package/lib/actor-inspector-tui.ts +11 -34
  130. package/lib/actor-rooms.ts +88 -18
  131. package/lib/actor-worker.ts +118 -0
  132. package/lib/async-runner.ts +173 -0
  133. package/lib/async-runs.ts +12 -22
  134. package/lib/conformance.ts +46 -0
  135. package/lib/coordinator.ts +664 -0
  136. package/lib/locker.ts +340 -0
  137. package/lib/mailbox-loop.ts +148 -0
  138. package/lib/observability.ts +24 -19
  139. package/lib/prompts.ts +1 -1
  140. package/lib/recipe-references.ts +37 -2
  141. package/lib/recipe-utils.ts +486 -0
  142. package/lib/runtime-notifier.ts +4 -6
  143. package/lib/state-readers.ts +93 -0
  144. package/lib/tools.ts +1 -1
  145. package/lib/validate-recipe.ts +110 -0
  146. package/package.json +10 -2
  147. package/recipes/actor-worker.json +35 -0
  148. package/recipes/pipeline-quorum-review.json +12 -7
  149. package/scripts/actor-worker.mjs +31 -0
  150. package/scripts/async-runner.mjs +11 -201
  151. package/scripts/build-dist.mjs +33 -0
  152. package/scripts/conformance.mjs +21 -35
  153. package/scripts/coordinator.mjs +15 -625
  154. package/scripts/locker.mjs +20 -332
  155. package/scripts/recipe-utils.mjs +17 -477
  156. package/scripts/validate-recipe.mjs +18 -121
  157. package/skills/actors/SKILL.md +8 -3
  158. package/skills/swarm/SKILL.md +3 -1
@@ -0,0 +1,310 @@
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
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Minimal mailbox loop helpers.
3
+ * Zones: mailbox-consuming actors, run/branch inbox claiming, handler status transitions
4
+ * Owns small reusable primitives for recipe authors; scheduling and task policy stay outside.
5
+ */
6
+ import { type BranchInboxRecord } from "./actor-rooms.ts";
7
+ import { type RunInboxMessage } from "./async-runs.ts";
8
+ export type MailboxLoopMessage = RunInboxMessage | BranchInboxRecord;
9
+ export type MailboxLoopTarget = {
10
+ kind: "run";
11
+ runOrDir: string;
12
+ } | {
13
+ address: string;
14
+ kind: "branch";
15
+ run: string;
16
+ stateDir: string;
17
+ };
18
+ export interface MailboxLoopClaimOptions {
19
+ owner?: string;
20
+ statuses?: string[];
21
+ }
22
+ export interface MailboxLoopHandleResult {
23
+ handled: boolean;
24
+ id?: string;
25
+ message?: MailboxLoopMessage;
26
+ target: MailboxLoopTarget;
27
+ }
28
+ export interface MailboxLoopDrainOptions extends MailboxLoopClaimOptions {
29
+ maxMessages?: number;
30
+ stopOnControl?: boolean;
31
+ }
32
+ export interface MailboxLoopDrainResult {
33
+ handled: number;
34
+ stopped: boolean;
35
+ target: MailboxLoopTarget;
36
+ }
37
+ export declare function isMailboxLoopStopMessage(message: unknown): boolean;
38
+ export declare function claimMailboxLoopMessage(target: MailboxLoopTarget, options?: MailboxLoopClaimOptions): MailboxLoopMessage | undefined;
39
+ export declare function updateMailboxLoopMessageStatus(target: MailboxLoopTarget, id: string, status: "claimed" | "handled" | "failed", metadata?: Record<string, unknown>): boolean;
40
+ export declare function handleMailboxLoopOnce(target: MailboxLoopTarget, handler: (message: MailboxLoopMessage) => Promise<void> | void, options?: MailboxLoopClaimOptions): Promise<MailboxLoopHandleResult>;
41
+ export declare function drainMailboxLoopMessages(target: MailboxLoopTarget, handler: (message: MailboxLoopMessage) => Promise<void> | void, options?: MailboxLoopDrainOptions): Promise<MailboxLoopDrainResult>;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Minimal mailbox loop helpers.
3
+ * Zones: mailbox-consuming actors, run/branch inbox claiming, handler status transitions
4
+ * Owns small reusable primitives for recipe authors; scheduling and task policy stay outside.
5
+ */
6
+ import { claimBranchInboxMessage, updateBranchInboxMessageStatus, } from "./actor-rooms.js";
7
+ import { claimRunInboxMessage, updateRunInboxMessageStatus, } from "./async-runs.js";
8
+ export function isMailboxLoopStopMessage(message) {
9
+ const type = message && typeof message === "object" && "type" in message
10
+ ? message.type
11
+ : undefined;
12
+ return (type === "control.stop" ||
13
+ type === "control.cancel" ||
14
+ type === "control.kill");
15
+ }
16
+ function messageId(message) {
17
+ return typeof message?.id === "string" ? message.id : undefined;
18
+ }
19
+ export function claimMailboxLoopMessage(target, options = {}) {
20
+ const owner = options.owner ?? "mailbox-loop";
21
+ const statuses = options.statuses ?? ["queued"];
22
+ return target.kind === "run"
23
+ ? claimRunInboxMessage(target.runOrDir, owner, statuses)
24
+ : claimBranchInboxMessage(target.stateDir, target.run, target.address, owner, statuses);
25
+ }
26
+ export function updateMailboxLoopMessageStatus(target, id, status, metadata = {}) {
27
+ return target.kind === "run"
28
+ ? updateRunInboxMessageStatus(target.runOrDir, id, status, metadata)
29
+ : updateBranchInboxMessageStatus(target.stateDir, target.run, target.address, id, status, metadata);
30
+ }
31
+ export async function handleMailboxLoopOnce(target, handler, options = {}) {
32
+ const message = claimMailboxLoopMessage(target, options);
33
+ const id = messageId(message);
34
+ if (!message || !id)
35
+ return { handled: false, target };
36
+ try {
37
+ await handler(message);
38
+ updateMailboxLoopMessageStatus(target, id, "handled");
39
+ return { handled: true, id, message, target };
40
+ }
41
+ catch (error) {
42
+ updateMailboxLoopMessageStatus(target, id, "failed", {
43
+ error: error instanceof Error ? error.message : String(error),
44
+ });
45
+ throw error;
46
+ }
47
+ }
48
+ export async function drainMailboxLoopMessages(target, handler, options = {}) {
49
+ const maxMessages = Math.max(1, Math.floor(options.maxMessages ?? 100));
50
+ let handled = 0;
51
+ for (; handled < maxMessages; handled += 1) {
52
+ const result = await handleMailboxLoopOnce(target, handler, options);
53
+ if (!result.handled || !result.message) {
54
+ return { handled, stopped: false, target };
55
+ }
56
+ if (options.stopOnControl !== false &&
57
+ isMailboxLoopStopMessage(result.message)) {
58
+ return { handled: handled + 1, stopped: true, target };
59
+ }
60
+ }
61
+ return { handled, stopped: false, target };
62
+ }
@@ -88,8 +88,8 @@ export declare function renderRunStatus(summary: RunSummary, frame?: number): st
88
88
  export declare function findRunRetirementCandidates(summary: RunSummary): RunRetirementCandidate[];
89
89
  export declare function executeRunRetirements(summary: RunSummary, options: RunRetirementExecutorOptions): Promise<RunRetirementExecution[]>;
90
90
  export declare function detectRunTransitions(previous: Map<string, RunObservedStatus>, summary: RunSummary): RunTransition[];
91
- export declare function pruneRunObservationState(previousStatuses: Map<string, RunObservedStatus>, previousLineCounts: Map<string, number>, summary: RunSummary, terminalRuns?: Iterable<string>): void;
92
- export declare function detectRunOutboxEvents(previousLineCounts: Map<string, number>, summary: RunSummary): RunOutboxEvent[];
91
+ export declare function pruneRunObservationState(previousStatuses: Map<string, RunObservedStatus>, previousLineCounts: Map<string, number>, summary: RunSummary, terminalRuns?: Iterable<string>, seenEventIds?: Map<string, Set<string>>): void;
92
+ export declare function detectRunOutboxEvents(previousLineCounts: Map<string, number>, summary: RunSummary, seenEventIds?: Map<string, Set<string>>): RunOutboxEvent[];
93
93
  export declare function getRunOutboxNotificationType(event: RunOutboxEvent): RunTransitionNotificationType;
94
94
  export declare function shouldNotifyRunOutboxEvent(event: RunOutboxEvent): boolean;
95
95
  export declare function shouldSendRunOutboxFollowUp(event: RunOutboxEvent): boolean;
@@ -7,6 +7,7 @@ import { existsSync, readdirSync, readFileSync } from "node:fs";
7
7
  import { basename, dirname, isAbsolute, join, relative, resolve, } from "node:path";
8
8
  import * as AsyncRuns from "./async-runs.js";
9
9
  import * as Paths from "./paths.js";
10
+ import { readJsonlFileResilient } from "./state-readers.js";
10
11
  const TERMINAL = new Set([
11
12
  "done",
12
13
  "failed",
@@ -409,57 +410,45 @@ function normalizeOutboxDelivery(value) {
409
410
  function normalizeOutboxLevel(value) {
410
411
  return value === "warning" || value === "error" ? value : "info";
411
412
  }
412
- function parseOutboxLine(line, run, index) {
413
+ function parseOutboxRecord(raw, run, index) {
413
414
  if (!run.stateDir)
414
415
  return undefined;
415
- try {
416
- const raw = JSON.parse(line);
417
- if (!raw || typeof raw !== "object" || Array.isArray(raw))
418
- return undefined;
419
- const event = typeof raw.event === "string" && raw.event.trim()
420
- ? raw.event.trim()
421
- : "run.event";
422
- const summary = typeof raw.summary === "string" && raw.summary.trim()
423
- ? raw.summary.trim()
424
- : event;
425
- const ts = typeof raw.ts === "string" && raw.ts.trim()
426
- ? raw.ts.trim()
427
- : new Date(0).toISOString();
428
- const id = typeof raw.id === "string" && raw.id.trim()
429
- ? raw.id.trim()
430
- : `${run.run}:${index}`;
431
- return {
432
- ...(raw.body !== undefined ? { body: raw.body } : {}),
433
- ...(raw.data !== undefined ? { data: raw.data } : {}),
434
- delivery: normalizeOutboxDelivery(raw.delivery),
435
- event,
436
- id,
437
- level: normalizeOutboxLevel(raw.level),
438
- ...(raw.metadata &&
439
- typeof raw.metadata === "object" &&
440
- !Array.isArray(raw.metadata)
441
- ? { metadata: raw.metadata }
442
- : {}),
443
- run: run.run,
444
- stateDir: run.stateDir,
445
- summary,
446
- ts,
447
- };
448
- }
449
- catch {
450
- return undefined;
451
- }
416
+ const event = typeof raw.event === "string" && raw.event.trim()
417
+ ? raw.event.trim()
418
+ : "run.event";
419
+ const summary = typeof raw.summary === "string" && raw.summary.trim()
420
+ ? raw.summary.trim()
421
+ : event;
422
+ const ts = typeof raw.ts === "string" && raw.ts.trim()
423
+ ? raw.ts.trim()
424
+ : new Date(0).toISOString();
425
+ const id = typeof raw.id === "string" && raw.id.trim()
426
+ ? raw.id.trim()
427
+ : `${run.run}:${index}`;
428
+ return {
429
+ ...(raw.body !== undefined ? { body: raw.body } : {}),
430
+ ...(raw.data !== undefined ? { data: raw.data } : {}),
431
+ delivery: normalizeOutboxDelivery(raw.delivery),
432
+ event,
433
+ id,
434
+ level: normalizeOutboxLevel(raw.level),
435
+ ...(raw.metadata &&
436
+ typeof raw.metadata === "object" &&
437
+ !Array.isArray(raw.metadata)
438
+ ? { metadata: raw.metadata }
439
+ : {}),
440
+ run: run.run,
441
+ stateDir: run.stateDir,
442
+ summary,
443
+ ts,
444
+ };
452
445
  }
453
- function readOutboxLines(run) {
446
+ function readOutboxRecords(run) {
454
447
  if (!run.stateDir)
455
448
  return [];
456
- const path = join(run.stateDir, "outbox.jsonl");
457
- if (!existsSync(path))
458
- return [];
459
- const content = readFileSync(path, "utf8").trimEnd();
460
- return content ? content.split("\n") : [];
449
+ return readJsonlFileResilient(join(run.stateDir, "outbox.jsonl")).records;
461
450
  }
462
- export function pruneRunObservationState(previousStatuses, previousLineCounts, summary, terminalRuns = []) {
451
+ export function pruneRunObservationState(previousStatuses, previousLineCounts, summary, terminalRuns = [], seenEventIds = new Map()) {
463
452
  const activeRuns = new Set(summary.runs.map((run) => runObservationKey(run)));
464
453
  const terminalRunSet = new Set(terminalRuns);
465
454
  const terminalLineKeys = new Set(summary.runs
@@ -477,20 +466,29 @@ export function pruneRunObservationState(previousStatuses, previousLineCounts, s
477
466
  previousLineCounts.delete(key);
478
467
  }
479
468
  }
469
+ for (const key of seenEventIds.keys()) {
470
+ if (terminalLineKeys.has(key) || !activeLineKeys.has(key)) {
471
+ seenEventIds.delete(key);
472
+ }
473
+ }
480
474
  }
481
- export function detectRunOutboxEvents(previousLineCounts, summary) {
475
+ export function detectRunOutboxEvents(previousLineCounts, summary, seenEventIds = new Map()) {
482
476
  const events = [];
483
477
  for (const run of summary.runs) {
484
478
  const key = run.stateDir ?? run.run;
485
- const lines = readOutboxLines(run);
479
+ const records = readOutboxRecords(run);
486
480
  const previousCount = previousLineCounts.get(key) ?? 0;
487
- const start = Math.min(previousCount, lines.length);
488
- for (let index = start; index < lines.length; index += 1) {
489
- const event = parseOutboxLine(lines[index], run, index);
490
- if (event)
491
- events.push(event);
481
+ const start = Math.min(previousCount, records.length);
482
+ const seen = seenEventIds.get(key) ?? new Set();
483
+ for (let index = start; index < records.length; index += 1) {
484
+ const event = parseOutboxRecord(records[index], run, index);
485
+ if (!event || seen.has(event.id))
486
+ continue;
487
+ events.push(event);
488
+ seen.add(event.id);
492
489
  }
493
- previousLineCounts.set(key, lines.length);
490
+ previousLineCounts.set(key, records.length);
491
+ seenEventIds.set(key, seen);
494
492
  }
495
493
  return events;
496
494
  }
@@ -6,7 +6,7 @@
6
6
  export declare const REGISTER_TOOL_DESCRIPTION: string;
7
7
  export declare const REGISTER_TOOL_PROMPT_SNIPPET = "Register persistent command templates as agent-callable tools";
8
8
  export declare const REGISTER_TOOL_GUIDELINES: string[];
9
- export declare const ONBOARDING_SYSTEM_PROMPT = "pi-actors quick model:\n- Local-first actor memory: persist trusted local capabilities instead of rebuilding shell recipes.\n- Layers: task -> command template -> recipe/tool -> spawn -> run:<id>; tool:<name> wraps registered capabilities.\n- Command templates stay sync: string leaf, array sequence, object node; flags include args/defaults, parallel, when, timeout, delay, retry, failure, recover, repeat, output.\n- Placeholders support typed/default args plus {value??fallback} and {flag?yes:no}.\n- ~/.pi/agent/recipes/*.json is actor muscle memory: every recipe there is auto-registered as an agent tool across sessions; register_tool writes there.\n- Recipes own template directly and may declare metadata/defaults/imports/mailbox/artifacts; files >1 MiB or import depth >32 fail closed.\n- Recipe imports are local variables; imported recipes are definitions, not nested async runs; parent async:true creates one run.\n- Use spawn/message/inspect for actor-level start/send/observe; avoid runtime/FIFO/outbox vocabulary in public guidance.\n- Run state lives under ~/.pi/agent/tmp/pi-actors/runs; inspect status/tail/messages/mailbox/files/artifacts intentionally and avoid busy-polling.\n- Maintain ~/.pi/agent/recipes like MEMORY.md for capabilities: keep useful tools, curate stale ones; packaged/ad hoc recipes are lower-priority components; offer to save successful recurring patterns only after confirmation.\n- Foreground tools/templates fit short work; async recipes/runs fit subagents, services, fanout, media, and long pipelines.\n- Long fanout = parent async recipe wrapping template(parallel:true) and imports; packaged fanout recipes bubble branch completion messages; grow recurring multi-agent workflows as packaged recipes/pipelines, not ad hoc external scripts.\n- For deeper pi-actors guidance, inspect installed extension sources/docs/recipes; README and docs are not automatically in context.";
9
+ export declare const ONBOARDING_SYSTEM_PROMPT = "pi-actors quick model:\n- Local-first actor memory: persist trusted local capabilities instead of rebuilding shell recipes.\n- Layers: task -> command template -> recipe/tool -> spawn -> run:<id>; tool:<name> wraps registered capabilities.\n- Command templates stay sync: string leaf, array sequence, object node; flags include args/defaults, parallel, when, timeout, delay, retry, failure, recover, repeat, output.\n- Placeholders support typed/default args plus {value??fallback} and {flag?yes:no}.\n- ~/.pi/agent/recipes/*.json is actor muscle memory: every recipe there is auto-registered as an agent tool across sessions; register_tool writes there.\n- Recipes own template directly and may declare metadata/defaults/imports/mailbox/artifacts; files >1 MiB or import depth >32 fail closed.\n- Recipe imports are local variables; imported recipes are definitions, not nested async runs; parent async:true creates one run.\n- Use spawn/message/inspect for actor-level start/send/observe; avoid runtime/FIFO/outbox vocabulary in public guidance.\n- Run state lives under ~/.pi/agent/tmp/pi-actors/runs; inspect status/tail/messages/mailbox/files/artifacts intentionally and avoid busy-polling.\n- Maintain ~/.pi/agent/recipes like MEMORY.md for capabilities: keep useful tools, curate stale ones, and fix/remove/disable invalid recipes flagged by registry warnings; packaged/ad hoc recipes are lower-priority components; offer to save successful recurring patterns only after confirmation.\n- Foreground tools/templates fit short work; async recipes/runs fit subagents, services, fanout, media, and long pipelines.\n- Long fanout = parent async recipe wrapping template(parallel:true) and imports; packaged fanout recipes bubble branch completion messages; grow recurring multi-agent workflows as packaged recipes/pipelines, not ad hoc external scripts.\n- For deeper pi-actors guidance, inspect installed extension sources/docs/recipes; README and docs are not automatically in context.";
10
10
  export declare const REGISTER_TOOL_PARAM_DESCRIPTIONS: {
11
11
  readonly name: "Tool name in snake_case (e.g., 'transcribe')";
12
12
  readonly description: "Describe what the tool does for the LLM. Required unless deleting; omitted updates keep the old description.";
@@ -23,7 +23,7 @@ export const ONBOARDING_SYSTEM_PROMPT = `pi-actors quick model:
23
23
  - Recipe imports are local variables; imported recipes are definitions, not nested async runs; parent async:true creates one run.
24
24
  - Use spawn/message/inspect for actor-level start/send/observe; avoid runtime/FIFO/outbox vocabulary in public guidance.
25
25
  - Run state lives under ~/.pi/agent/tmp/pi-actors/runs; inspect status/tail/messages/mailbox/files/artifacts intentionally and avoid busy-polling.
26
- - Maintain ~/.pi/agent/recipes like MEMORY.md for capabilities: keep useful tools, curate stale ones; packaged/ad hoc recipes are lower-priority components; offer to save successful recurring patterns only after confirmation.
26
+ - Maintain ~/.pi/agent/recipes like MEMORY.md for capabilities: keep useful tools, curate stale ones, and fix/remove/disable invalid recipes flagged by registry warnings; packaged/ad hoc recipes are lower-priority components; offer to save successful recurring patterns only after confirmation.
27
27
  - Foreground tools/templates fit short work; async recipes/runs fit subagents, services, fanout, media, and long pipelines.
28
28
  - Long fanout = parent async recipe wrapping template(parallel:true) and imports; packaged fanout recipes bubble branch completion messages; grow recurring multi-agent workflows as packaged recipes/pipelines, not ad hoc external scripts.
29
29
  - For deeper pi-actors guidance, inspect installed extension sources/docs/recipes; README and docs are not automatically in context.`;
@@ -195,6 +195,29 @@ function parseMarkdownFrontmatterObject(lines) {
195
195
  }
196
196
  return result;
197
197
  }
198
+ function normalizeMarkdownFrontmatterField(key, value) {
199
+ if (key === "args" && typeof value === "string") {
200
+ return value
201
+ .split(",")
202
+ .map((item) => item.trim())
203
+ .filter(Boolean);
204
+ }
205
+ if (key === "defaults" && Array.isArray(value)) {
206
+ return Object.fromEntries(value
207
+ .filter((item) => typeof item === "string")
208
+ .map((item) => {
209
+ const separator = item.indexOf(":");
210
+ return separator < 0
211
+ ? [item.trim(), ""]
212
+ : [
213
+ item.slice(0, separator).trim(),
214
+ parseMarkdownScalar(item.slice(separator + 1)),
215
+ ];
216
+ })
217
+ .filter(([name]) => Boolean(name)));
218
+ }
219
+ return value;
220
+ }
198
221
  function parseMarkdownFrontmatter(value) {
199
222
  const result = {};
200
223
  const lines = value.split(/\r?\n/);
@@ -206,7 +229,7 @@ function parseMarkdownFrontmatter(value) {
206
229
  if (!match)
207
230
  continue;
208
231
  if (match[2]) {
209
- result[match[1]] = parseMarkdownScalar(match[2]);
232
+ result[match[1]] = normalizeMarkdownFrontmatterField(match[1], parseMarkdownScalar(match[2]));
210
233
  continue;
211
234
  }
212
235
  const nested = [];
@@ -214,7 +237,7 @@ function parseMarkdownFrontmatter(value) {
214
237
  index += 1;
215
238
  nested.push(lines[index]);
216
239
  }
217
- result[match[1]] = parseMarkdownFrontmatterObject(nested);
240
+ result[match[1]] = normalizeMarkdownFrontmatterField(match[1], parseMarkdownFrontmatterObject(nested));
218
241
  }
219
242
  return result;
220
243
  }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Recipe utility command bundle entrypoint logic.
3
+ * Zones: recipe utilities, deterministic helper subcommands
4
+ */
5
+ export declare function runRecipeUtils(argv?: string[]): void;