@llblab/pi-actors 0.20.2 → 0.21.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.
@@ -4,7 +4,7 @@
4
4
  * Owns ambient summaries, terminal events, and run outbox delivery for detached command-template runs
5
5
  */
6
6
  import { existsSync, readdirSync, readFileSync } from "node:fs";
7
- import { basename, dirname, join, relative, resolve } from "node:path";
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
10
  const TERMINAL = new Set([
@@ -15,6 +15,7 @@ const TERMINAL = new Set([
15
15
  "killed",
16
16
  ]);
17
17
  const PROC_DESCENDANT_SCAN_TTL_MS = 1000;
18
+ const RUN_STATE_DISCOVERY_MAX_DEPTH = 8;
18
19
  const procDescendantScanCache = new Map();
19
20
  function toNumber(value) {
20
21
  const number = Number(value);
@@ -34,6 +35,29 @@ function getUpdatedAt(status) {
34
35
  ? status.createdAt
35
36
  : undefined;
36
37
  }
38
+ function listRunStateDirs(stateRoot, depth = 0, seen = new Set()) {
39
+ if (!existsSync(stateRoot) || seen.has(stateRoot))
40
+ return [];
41
+ seen.add(stateRoot);
42
+ let entries;
43
+ try {
44
+ entries = readdirSync(stateRoot, { withFileTypes: true });
45
+ }
46
+ catch {
47
+ return [];
48
+ }
49
+ const result = [];
50
+ for (const entry of entries) {
51
+ if (!entry.isDirectory())
52
+ continue;
53
+ const child = join(stateRoot, entry.name);
54
+ if (existsSync(join(child, "run.json")))
55
+ result.push(child);
56
+ if (depth + 1 < RUN_STATE_DISCOVERY_MAX_DEPTH)
57
+ result.push(...listRunStateDirs(child, depth + 1, seen));
58
+ }
59
+ return result;
60
+ }
37
61
  function observeRun(stateDir) {
38
62
  try {
39
63
  const status = AsyncRuns.getRunStatus(stateDir);
@@ -90,9 +114,8 @@ export function summarizeRuns(stateRoot = Paths.getRunStateRoot(), ownerId) {
90
114
  total: 0,
91
115
  };
92
116
  }
93
- const runs = readdirSync(stateRoot, { withFileTypes: true })
94
- .filter((entry) => entry.isDirectory())
95
- .map((entry) => observeRun(join(stateRoot, entry.name)))
117
+ const runs = listRunStateDirs(stateRoot)
118
+ .map((stateDir) => observeRun(stateDir))
96
119
  .filter((run) => Boolean(run))
97
120
  .filter((run) => ownerId === undefined || run.ownerId === ownerId)
98
121
  .sort((a, b) => (b.updatedAt ?? "").localeCompare(a.updatedAt ?? ""));
@@ -148,7 +171,7 @@ function getRunningRunPidMap(stateRoot, ownerId) {
148
171
  for (const run of summarizeRunsWithoutSubagents(stateRoot, ownerId).runs) {
149
172
  if (run.status !== "running")
150
173
  continue;
151
- const status = AsyncRuns.getRunStatus(join(stateRoot, run.run));
174
+ const status = AsyncRuns.getRunStatus(run.stateDir ?? join(stateRoot, run.run));
152
175
  const pid = Number(status.pid || 0);
153
176
  if (pid > 0)
154
177
  pids.set(String(pid), run.run);
@@ -167,9 +190,8 @@ function summarizeRunsWithoutSubagents(stateRoot, ownerId) {
167
190
  runs: [],
168
191
  total: 0,
169
192
  };
170
- const runs = readdirSync(stateRoot, { withFileTypes: true })
171
- .filter((entry) => entry.isDirectory())
172
- .map((entry) => observeRun(join(stateRoot, entry.name)))
193
+ const runs = listRunStateDirs(stateRoot)
194
+ .map((stateDir) => observeRun(stateDir))
173
195
  .filter((run) => Boolean(run))
174
196
  .filter((run) => ownerId === undefined || run.ownerId === ownerId)
175
197
  .sort((a, b) => (b.updatedAt ?? "").localeCompare(a.updatedAt ?? ""));
@@ -260,23 +282,86 @@ export function renderSubagentStatus(count, frame = 0) {
260
282
  export function renderRunStatus(summary, frame = 0) {
261
283
  return renderSubagentStatus(summary.runningSubagents, frame);
262
284
  }
285
+ function isNestedStateDir(parent, child) {
286
+ const path = relative(parent, child);
287
+ return Boolean(path) && !path.startsWith("..") && !isAbsolute(path);
288
+ }
263
289
  export function findRunRetirementCandidates(summary) {
264
290
  return summary.runs
265
- .filter((run) => {
291
+ .map((run) => {
266
292
  const activeSubagents = Math.max(0, Math.floor(run.activeSubagents ?? 0));
267
293
  const descendantSubagents = Math.max(0, Math.floor(run.descendantSubagents ?? 0));
268
- return (run.status === "running" &&
269
- run.retireWhen === "children_terminal" &&
270
- run.stateDir &&
271
- activeSubagents + descendantSubagents <= 0);
294
+ const childRuns = run.stateDir
295
+ ? summary.runs.filter((child) => child.stateDir !== undefined &&
296
+ child.stateDir !== run.stateDir &&
297
+ isNestedStateDir(run.stateDir, child.stateDir))
298
+ : [];
299
+ const runningChildRuns = childRuns.filter((child) => child.status === "running").length;
300
+ return {
301
+ activeSubagents,
302
+ childRuns: childRuns.length,
303
+ descendantSubagents,
304
+ ready: run.status === "running" &&
305
+ run.retireWhen === "children_terminal" &&
306
+ run.stateDir !== undefined &&
307
+ !run.terminalHandled &&
308
+ activeSubagents + descendantSubagents + runningChildRuns <= 0,
309
+ run,
310
+ terminalChildRuns: childRuns.filter((child) => TERMINAL.has(child.status)).length,
311
+ };
272
312
  })
273
- .map((run) => ({
274
- activeSubagents: Math.max(0, Math.floor(run.activeSubagents ?? 0)),
275
- descendantSubagents: Math.max(0, Math.floor(run.descendantSubagents ?? 0)),
276
- run: run.run,
277
- stateDir: run.stateDir,
313
+ .filter((item) => item.ready)
314
+ .map((item) => ({
315
+ activeSubagents: item.activeSubagents,
316
+ childRuns: item.childRuns,
317
+ descendantSubagents: item.descendantSubagents,
318
+ run: item.run.run,
319
+ stateDir: item.run.stateDir,
320
+ terminalChildRuns: item.terminalChildRuns,
278
321
  }));
279
322
  }
323
+ export async function executeRunRetirements(summary, options) {
324
+ const results = [];
325
+ for (const candidate of findRunRetirementCandidates(summary)) {
326
+ if (options.attempted?.has(candidate.stateDir)) {
327
+ results.push({ action: "skip", run: candidate.run, stateDir: candidate.stateDir });
328
+ continue;
329
+ }
330
+ options.attempted?.add(candidate.stateDir);
331
+ try {
332
+ await options.sendStop(candidate);
333
+ options.notify?.(`Retiring actor ${candidate.run} after child runs reached terminal state`, "info");
334
+ results.push({ action: "stop", run: candidate.run, stateDir: candidate.stateDir });
335
+ continue;
336
+ }
337
+ catch (error) {
338
+ try {
339
+ const cancelResult = options.cancelRun(candidate);
340
+ const cancelled = Boolean(cancelResult.cancelled);
341
+ options.notify?.(cancelled
342
+ ? `Retiring actor ${candidate.run} by cancellation after graceful stop failed`
343
+ : `Actor retirement skipped for ${candidate.run}: ${error instanceof Error ? error.message : String(error)}`, cancelled ? "warning" : "error");
344
+ results.push({
345
+ action: cancelled ? "cancel" : "skip",
346
+ ...(cancelled ? {} : { error: error instanceof Error ? error.message : String(error) }),
347
+ run: candidate.run,
348
+ stateDir: candidate.stateDir,
349
+ });
350
+ }
351
+ catch (cancelError) {
352
+ const message = cancelError instanceof Error ? cancelError.message : String(cancelError);
353
+ options.notify?.(`Actor retirement failed for ${candidate.run}: ${message}`, "error");
354
+ results.push({
355
+ action: "failed",
356
+ error: message,
357
+ run: candidate.run,
358
+ stateDir: candidate.stateDir,
359
+ });
360
+ }
361
+ }
362
+ }
363
+ return results;
364
+ }
280
365
  export function detectRunTransitions(previous, summary) {
281
366
  const transitions = [];
282
367
  for (const run of summary.runs) {
@@ -36,15 +36,18 @@ function listRecipeFiles(root) {
36
36
  return [];
37
37
  return readdirSync(root, { withFileTypes: true })
38
38
  .filter((entry) => entry.isFile() &&
39
- entry.name.endsWith(".json") &&
39
+ (entry.name.endsWith(".json") || entry.name.endsWith(".md")) &&
40
40
  entry.name !== "legacy-tool-registry-migration-report.json")
41
41
  .map((entry) => join(root, entry.name))
42
- .sort();
42
+ .sort((a, b) => a.replace(/\.md$/, ".json").localeCompare(b.replace(/\.md$/, ".json")) || (a.endsWith(".json") ? -1 : 1));
43
43
  }
44
44
  function getRecipeConfigDiagnostics(file, config) {
45
45
  if (!config)
46
46
  return [`Invalid recipe: ${file}`];
47
- return CommandTemplates.getCommandTemplateWarnings(typeof config.template === "string" ? config.template : { template: config.template }).map((warning) => `Recipe ${file}: ${warning}`);
47
+ const commandTemplateConfig = typeof config.template === "object" && config.template !== null
48
+ ? config.template
49
+ : config;
50
+ return CommandTemplates.getCommandTemplateWarnings(commandTemplateConfig).map((warning) => `Recipe ${file}: ${warning}`);
48
51
  }
49
52
  function readDiscoveredRecipe(root, file, priority, defaultTool = false, mutableUsage = false) {
50
53
  const id = RecipeReferences.getRecipeIdFromPath(file);
@@ -135,13 +138,18 @@ export function discoverRecipeSources(sources) {
135
138
  const active = new Map();
136
139
  const diagnostics = getRecipeRootDiagnostics(sources);
137
140
  for (const [id, bucket] of byId) {
138
- bucket.sort((a, b) => a.priority - b.priority || a.path.localeCompare(b.path));
141
+ bucket.sort((a, b) => a.priority - b.priority ||
142
+ a.path.replace(/\.md$/, ".json").localeCompare(b.path.replace(/\.md$/, ".json")) ||
143
+ (a.path.endsWith(".json") ? -1 : 1));
139
144
  const winner = bucket[0];
140
145
  winner.active = true;
141
146
  winner.shadows = bucket.slice(1).map((entry) => entry.path);
142
147
  active.set(id, winner);
143
- for (const shadow of bucket.slice(1))
148
+ for (const shadow of bucket.slice(1)) {
144
149
  shadow.shadowed = true;
150
+ if (winner.path.endsWith(".json") && shadow.path.endsWith(".md"))
151
+ shadow.diagnostics.push(`Markdown recipe ${shadow.path} is shadowed by JSON recipe ${winner.path}`);
152
+ }
145
153
  if (winner.invalid)
146
154
  diagnostics.push(`Recipe ${id} is invalid and blocks lower-priority recipes`);
147
155
  if (winner.disabled)
@@ -5,7 +5,7 @@
5
5
  */
6
6
  import { existsSync, readFileSync, statSync } from "node:fs";
7
7
  import { homedir } from "node:os";
8
- import { basename, dirname, resolve } from "node:path";
8
+ import { basename, dirname, extname, resolve } from "node:path";
9
9
  import * as CommandTemplates from "./command-templates.js";
10
10
  import * as Paths from "./paths.js";
11
11
  const MAX_RECIPE_FILE_BYTES = 1024 * 1024;
@@ -23,26 +23,31 @@ export function resolveRecipePath(value, recipeRoot = Paths.getRecipeRoot()) {
23
23
  return resolve(homedir(), expanded.slice(2));
24
24
  if (expanded.includes("/"))
25
25
  return resolve(expanded);
26
- return resolve(recipeRoot, expanded.endsWith(".json") ? expanded : `${expanded}.json`);
26
+ return resolve(recipeRoot, expanded.endsWith(".json") || expanded.endsWith(".md")
27
+ ? expanded
28
+ : `${expanded}.json`);
27
29
  }
28
30
  function isBareRecipeName(value) {
29
31
  const trimmed = value.trim();
30
32
  return Boolean(trimmed) && !trimmed.includes("/") && !trimmed.startsWith("~") && !trimmed.includes("{");
31
33
  }
32
- function recipeNameFile(value) {
34
+ function recipeNameFiles(value) {
33
35
  const trimmed = value.trim();
34
- return trimmed.endsWith(".json") ? trimmed : `${trimmed}.json`;
36
+ if (trimmed.endsWith(".json") || trimmed.endsWith(".md"))
37
+ return [trimmed];
38
+ return [`${trimmed}.json`, `${trimmed}.md`];
35
39
  }
36
40
  function resolveRecipeImportPath(value, currentRecipeRoot) {
37
41
  if (!isBareRecipeName(value))
38
42
  return resolveRecipePath(value, currentRecipeRoot);
39
- const file = recipeNameFile(value);
40
43
  const roots = [
41
44
  Paths.getRecipeRoot(),
42
45
  currentRecipeRoot,
43
46
  Paths.getPackagedRecipeRoot(),
44
47
  ];
45
- const candidates = [...new Set(roots.map((root) => resolve(root, file)))];
48
+ const candidates = [
49
+ ...new Set(roots.flatMap((root) => recipeNameFiles(value).map((file) => resolve(root, file)))),
50
+ ];
46
51
  return candidates.find((candidate) => existsSync(candidate)) ?? candidates[0];
47
52
  }
48
53
  export function getRecipePath(value, recipeRoot = Paths.getRecipeRoot()) {
@@ -51,13 +56,15 @@ export function getRecipePath(value, recipeRoot = Paths.getRecipeRoot()) {
51
56
  const trimmed = value.trim();
52
57
  if (!trimmed || hasWhitespace(trimmed))
53
58
  return undefined;
54
- if (trimmed.endsWith(".json"))
59
+ if (trimmed.endsWith(".json") || trimmed.endsWith(".md"))
55
60
  return resolveRecipePath(trimmed, recipeRoot);
56
- const path = resolveRecipePath(trimmed, recipeRoot);
61
+ const jsonPath = resolveRecipePath(trimmed, recipeRoot);
62
+ const mdPath = resolveRecipePath(`${trimmed}.md`, recipeRoot);
63
+ const path = existsSync(jsonPath) ? jsonPath : mdPath;
57
64
  if (!existsSync(path))
58
65
  return undefined;
59
66
  try {
60
- const raw = JSON.parse(readFileSync(path, "utf8"));
67
+ const raw = readRawRecipeConfig(path);
61
68
  return raw && typeof raw === "object" && Object.hasOwn(raw, "template")
62
69
  ? path
63
70
  : undefined;
@@ -137,6 +144,122 @@ function getRecipeCommandTemplate(raw) {
137
144
  }
138
145
  return normalizeRecipeTemplate({ ...envelope, template });
139
146
  }
147
+ function parseMarkdownScalar(value) {
148
+ const trimmed = value.trim();
149
+ if (!trimmed)
150
+ return "";
151
+ if ((trimmed.startsWith("{") && trimmed.endsWith("}")) ||
152
+ (trimmed.startsWith("[") && trimmed.endsWith("]"))) {
153
+ try {
154
+ return JSON.parse(trimmed);
155
+ }
156
+ catch {
157
+ return trimmed;
158
+ }
159
+ }
160
+ const quoted = trimmed.match(/^(?:"([^"]*)"|'([^']*)')$/);
161
+ if (quoted)
162
+ return quoted[1] ?? quoted[2] ?? "";
163
+ if (trimmed === "true")
164
+ return true;
165
+ if (trimmed === "false")
166
+ return false;
167
+ if (trimmed === "null")
168
+ return null;
169
+ if (/^-?\d+(?:\.\d+)?$/.test(trimmed))
170
+ return Number(trimmed);
171
+ return trimmed;
172
+ }
173
+ function parseMarkdownFrontmatterObject(lines) {
174
+ if (lines.every((line) => /^\s*-\s+/.test(line))) {
175
+ return lines.map((line) => parseMarkdownScalar(line.replace(/^\s*-\s+/, "")));
176
+ }
177
+ const result = {};
178
+ for (let index = 0; index < lines.length; index += 1) {
179
+ const match = lines[index].match(/^\s{2}([A-Za-z_][A-Za-z0-9_.-]*):\s*(.*)$/);
180
+ if (!match)
181
+ continue;
182
+ if (match[2]) {
183
+ result[match[1]] = parseMarkdownScalar(match[2]);
184
+ continue;
185
+ }
186
+ const nested = [];
187
+ while (index + 1 < lines.length && /^\s{4}/.test(lines[index + 1])) {
188
+ index += 1;
189
+ nested.push(lines[index].slice(2));
190
+ }
191
+ result[match[1]] = parseMarkdownFrontmatterObject(nested);
192
+ }
193
+ return result;
194
+ }
195
+ function parseMarkdownFrontmatter(value) {
196
+ const result = {};
197
+ const lines = value.split(/\r?\n/);
198
+ for (let index = 0; index < lines.length; index += 1) {
199
+ const line = lines[index];
200
+ if (!line.trim() || line.trim().startsWith("#"))
201
+ continue;
202
+ const match = line.match(/^([A-Za-z_][A-Za-z0-9_.-]*):\s*(.*)$/);
203
+ if (!match)
204
+ continue;
205
+ if (match[2]) {
206
+ result[match[1]] = parseMarkdownScalar(match[2]);
207
+ continue;
208
+ }
209
+ const nested = [];
210
+ while (index + 1 < lines.length && /^\s+/.test(lines[index + 1])) {
211
+ index += 1;
212
+ nested.push(lines[index]);
213
+ }
214
+ result[match[1]] = parseMarkdownFrontmatterObject(nested);
215
+ }
216
+ return result;
217
+ }
218
+ function findMarkdownRecipeFence(body) {
219
+ const pattern = /```([^\n`]*)\n([\s\S]*?)```/g;
220
+ for (const match of body.matchAll(pattern)) {
221
+ const info = match[1].trim().toLowerCase();
222
+ if (info.includes("recipe") ||
223
+ info.includes("template") ||
224
+ info.includes("command") ||
225
+ info.includes("json")) {
226
+ return { info, body: match[2].trim() };
227
+ }
228
+ }
229
+ return undefined;
230
+ }
231
+ function parseMarkdownRecipeConfig(content) {
232
+ const lines = content.split(/\r?\n/);
233
+ if (lines[0]?.trim() !== "---")
234
+ return undefined;
235
+ const end = lines.findIndex((line, index) => index > 0 && line.trim() === "---");
236
+ if (end === -1)
237
+ return undefined;
238
+ const frontmatter = parseMarkdownFrontmatter(lines.slice(1, end).join("\n"));
239
+ const fence = findMarkdownRecipeFence(lines.slice(end + 1).join("\n"));
240
+ if (!fence)
241
+ return Object.hasOwn(frontmatter, "template") ? frontmatter : undefined;
242
+ const text = fence.body.trim();
243
+ if (!text)
244
+ return undefined;
245
+ if (fence.info.includes("json") ||
246
+ fence.info.includes("recipe") ||
247
+ text.startsWith("{") ||
248
+ text.startsWith("[") ||
249
+ text.startsWith('"')) {
250
+ try {
251
+ const parsed = JSON.parse(text);
252
+ if (isRecord(parsed) && Object.hasOwn(parsed, "template"))
253
+ return { ...frontmatter, ...parsed };
254
+ return { ...frontmatter, template: parsed };
255
+ }
256
+ catch {
257
+ if (fence.info.includes("json") || fence.info.includes("recipe"))
258
+ return undefined;
259
+ }
260
+ }
261
+ return { ...frontmatter, template: text };
262
+ }
140
263
  export function readRawRecipeConfig(path) {
141
264
  if (!existsSync(path))
142
265
  return undefined;
@@ -145,7 +268,10 @@ export function readRawRecipeConfig(path) {
145
268
  throw new Error(`Recipe file exceeds size limit ${MAX_RECIPE_FILE_BYTES} bytes: ${path}`);
146
269
  }
147
270
  try {
148
- const raw = JSON.parse(readFileSync(path, "utf8"));
271
+ const content = readFileSync(path, "utf8");
272
+ if (path.endsWith(".md"))
273
+ return parseMarkdownRecipeConfig(content);
274
+ const raw = JSON.parse(content);
149
275
  return raw && typeof raw === "object" ? raw : undefined;
150
276
  }
151
277
  catch {
@@ -153,7 +279,7 @@ export function readRawRecipeConfig(path) {
153
279
  }
154
280
  }
155
281
  export function getRecipeIdFromPath(file) {
156
- return basename(file, ".json");
282
+ return basename(file, extname(file));
157
283
  }
158
284
  function readRecipeConfig(value) {
159
285
  const path = getRecipePath(value);
package/dist/lib/tools.js CHANGED
@@ -790,7 +790,7 @@ export function createActorMessageToolDefinition(deps = {}) {
790
790
  result = AsyncRuns.killRun(address.value);
791
791
  }
792
792
  else {
793
- result = AsyncRuns.sendRunMessage(address.value, messageBodyToRunLine(message));
793
+ result = await AsyncRuns.sendRunMessage(address.value, messageBodyToRunLine(message));
794
794
  }
795
795
  }
796
796
  else if (address.kind === "branch" && address.value) {
@@ -820,7 +820,7 @@ export function createActorMessageToolDefinition(deps = {}) {
820
820
  ActorRooms.writeCommunicationSnapshot(stateDir, runId);
821
821
  ActorRooms.appendBranchInboxMessage(stateDir, runId, message.to, message);
822
822
  }
823
- result = AsyncRuns.sendRunMessage(address.value, JSON.stringify(message));
823
+ result = await AsyncRuns.sendRunMessage(address.value, JSON.stringify(message));
824
824
  }
825
825
  else if (address.kind === "room" && address.value && address.room) {
826
826
  const runId = address.value;
@@ -831,11 +831,11 @@ export function createActorMessageToolDefinition(deps = {}) {
831
831
  throw new Error(`${message.to} has no run state directory.`);
832
832
  const recipients = getRoomMulticastRecipients(message, runId);
833
833
  const roomResult = ActorRooms.appendRoomMessage(stateDir, address.room, message);
834
- const multicast = recipients.map((recipient) => AsyncRuns.sendRunMessage(runId, JSON.stringify({ ...message, to: recipient })));
834
+ await Promise.all(recipients.map((recipient) => AsyncRuns.sendRunMessage(runId, JSON.stringify({ ...message, to: recipient }))));
835
835
  result = {
836
836
  ...roomResult,
837
- ...(multicast.length > 0
838
- ? { multicast: recipients, multicast_count: multicast.length }
837
+ ...(recipients.length > 0
838
+ ? { multicast: recipients, multicast_count: recipients.length }
839
839
  : {}),
840
840
  };
841
841
  }
package/docs/README.md CHANGED
@@ -5,7 +5,7 @@ Living index of all documentation in the `/docs` directory.
5
5
  ## Documents
6
6
 
7
7
  - [command-templates.md](./command-templates.md) — Portable synchronous command execution standard
8
- - [template-recipes.md](./template-recipes.md) — Saved JSON recipe standard, imports, and reusable command-template graph composition
8
+ - [template-recipes.md](./template-recipes.md) — Saved JSON/Markdown recipe standard, imports, and reusable command-template graph composition
9
9
  - [async-runs.md](./async-runs.md) — Detached run lifecycle, state files, actor messages, cancellation, and ambient indicators
10
10
  - [actor-messages.md](./actor-messages.md) — Actor/message protocol for symmetric communication primitives
11
11
  - [tool-registry.md](./tool-registry.md) — Local `pi-actors` registry storage and `register_tool` adaptation
@@ -44,6 +44,8 @@ An alternate implementation shape is a dedicated non-LLM communication actor: a
44
44
 
45
45
  That actor-backed shape can also reduce direct file storage. Instead of every protocol feature owning JSON files as primary state, a helper actor can keep live room/roster structures in memory or another local structure and write files only as snapshots, audit logs, artifacts, or recovery checkpoints. The decision boundary is practical: keep files when durability and inspectability are the main value; prefer actor-owned structures when live coordination, subscriptions, fanout, unread state, or mutation consistency becomes the main value.
46
46
 
47
+ Current backend decision: keep the file-backed adapter for now. The covered workload is append-heavy room coordination plus direct branch inbox queueing/claiming, where durable local files are still the useful source of truth for recovery and `inspect`. A communication helper should be introduced only when a real workflow needs long-lived subscriptions, live fanout policy, or shared mutable room state beyond the current lock/debounce/compaction safeguards.
48
+
47
49
  Package-specific endpoints may still exist, but the envelope stays the same.
48
50
 
49
51
  ## Message Envelope
@@ -94,7 +96,7 @@ Transports differ, but the public contract does not:
94
96
 
95
97
  - `to: run:<id>` routes through the run-local control channel selected by that recipe or runtime adapter.
96
98
  - `to: coordinator` routes to the runtime attention path when `from` names a run actor. `to: session:<id>` uses the same actor-message path only when the sender run is owned by that session, making explicit session-directed checkpoints possible without exposing runtime delivery knobs. Generic async-runner `command.done` messages and explicit coordinator/session-bound messages include the actor envelope fields alongside runtime metadata.
97
- - `to: branch:<run>/<branch>` currently routes through the parent run mailbox with the full envelope preserved so the run or recipe-specific worker protocol can dispatch branch-local control. It also persists a queued branch-local copy under `branches/<branch>/inbox.jsonl`, inspectable with `inspect branch:<run>/<branch> view=mailbox`; compact inspection includes the inbox message `id`, status, route, type, and timestamps so worker protocols can correlate claims/retries. Branch-local inbox append and status rewrites are guarded by a small lock so direct delivery and coordinator claims do not overwrite each other during bursts. Coordinator claim handling also assigns an ID to older/manual queued records that do not have one so they can still transition to `handled` or `failed` instead of repeating forever. It is not a broadcast room and it does not make an arbitrary prompt process consume the message automatically. Target direction: direct branch messages should become initiating inbox work for long-lived branch runners, delivered into the recipient's next prompt/context as soon as the runner can accept work.
99
+ - `to: branch:<run>/<branch>` currently routes through the parent run mailbox with the full envelope preserved so the run or recipe-specific worker protocol can dispatch branch-local control. It also persists a queued branch-local copy under `branches/<branch>/inbox.jsonl`, inspectable with `inspect branch:<run>/<branch> view=mailbox`; compact inspection includes the inbox message `id`, status, route, type, and timestamps so worker protocols can correlate claims/retries. Branch-local inbox append and status rewrites are guarded by a small lock so direct delivery and coordinator claims do not overwrite each other during bursts. Status transitions preserve active queued/claimed records and compact older handled/failed terminal records with bounded retention, so persistent runners do not accumulate unbounded completed inbox history. Coordinator claim handling also assigns an ID to older/manual queued records that do not have one so they can still transition to `handled` or `failed` instead of repeating forever. It is not a broadcast room and it does not make an arbitrary prompt process consume the message automatically. Target direction: direct branch messages should become initiating inbox work for long-lived branch runners, delivered into the recipient's next prompt/context as soon as the runner can accept work.
98
100
  - `to: room:<run>` appends the full envelope to the room timeline, updates room state for room-control types such as `actor.join` and `actor.leave`, and can route selected-recipient multicast when `metadata.recipients` contains same-run `branch:<run>/<branch>` addresses.
99
101
  - `to: tool:<name>` invokes an executable pi tool by name. Object bodies become tool parameters; primitive bodies are passed as `{ "input": body }`.
100
102
 
@@ -205,6 +207,7 @@ Runtime operations use the actor/message vocabulary:
205
207
  create detached work -> spawn
206
208
  run-local control -> message to run:<id>
207
209
  run stop/kill -> message type control.stop/control.kill
210
+ platform control -> internal adapter selected from run state
208
211
  coordinator signal -> message to coordinator/session
209
212
  tool execution -> message to tool:<name>
210
213
  intentional observe -> inspect
@@ -158,7 +158,7 @@ The actor-level surface is:
158
158
  - `message`: send one typed envelope to `run:<id>`, `branch:<run>/<branch>`, `room:<run>`, `tool:<name>`, `coordinator`, or `session:<id>`.
159
159
  - `inspect`: intentionally read owned `run:<id>` status, tail, messages, artifacts, files, mailbox metadata, or communication snapshot; read `room:<run>` status, messages, previews, roster, or contacts; read current `coordinator` run inventory only when a coordinator session is known; read `session:<id>` or `session:all` run inventory with optional status filtering when the session is explicit; read `tool:<name>` status or schema for registered tool actors.
160
160
 
161
- Opt-in supervisor retirement uses `retire_when: "children_terminal"` as lifecycle metadata. Candidate detection is conservative: a supervisor is not retirement-ready while command-template progress or descendant `pi -p` worker processes are still active; future retirement execution must also verify child async-run state and flushed outputs before stopping the supervisor.
161
+ Opt-in supervisor retirement uses `retire_when: "children_terminal"` as lifecycle metadata. Run summaries discover nested child run state dirs under the visible state root so bounded supervisor trees are observable. Candidate detection is conservative: a supervisor is not retirement-ready while command-template progress, descendant `pi -p` worker processes, or nested child async runs under the supervisor state dir are still active. Candidate metadata includes observed child-run counts. When the session watcher observes a ready candidate, it sends a graceful `stop` control message once; if the run has no ready control endpoint, it falls back to owned-run cancellation and records the terminal action through normal run events. Persistent or non-opt-in runs are not retirement candidates.
162
162
 
163
163
  Low-level async actions map into the actor surface instead of forming a second public model:
164
164
 
@@ -185,7 +185,7 @@ Some recipes expose a run-local control channel. When present, a caller can send
185
185
 
186
186
  For `run:<id>`, `message` adapts the body to the recipe's run-local control channel. For `branch:<run>/<branch>`, it sends the full envelope through the parent run mailbox and records a queued branch-local inbox entry at `branches/<branch>/inbox.jsonl` so the run can dispatch branch-local control. Current consumers are recipe-specific worker protocols that read the parent run mailbox or branch inbox; independent one-shot prompt processes do not automatically consume branch inbox entries. For `tool:<name>`, object bodies become the target tool parameters and primitive bodies are passed as `{ "input": body }`. The generic runtime records control messages but does not interpret arbitrary run mailbox content. For example, a music player may accept `play`, `pause`, `next`, and `stop`, while a collaborative agent recipe may accept `continue`, `revise:<note>`, `approve`, or `abort`. Recipes may treat terminal control messages such as `stop` as synchronously handled so the later process exit does not generate a duplicate async follow-up.
187
187
 
188
- The standard run-local transport is Unix-oriented. Use WSL/Linux/macOS for packaged message-controlled recipes, or let a Windows-specific recipe expose its own transport such as a Windows named pipe or localhost socket.
188
+ Run-local control uses a platform adapter under the same `message` API. Unix recipes may keep the existing FIFO endpoint, and native Windows recipes can expose a named-pipe endpoint in run state. Recipe authors should document message vocabulary through `mailbox.accepts`, not through transport arguments. Packaged scripts that still create Unix-only endpoints remain WSL/Linux/macOS-only until migrated.
189
189
 
190
190
  ## Coordinator Notifications
191
191
 
@@ -219,7 +219,7 @@ Use coordinator/session-bound messages for completion and decision points, not f
219
219
 
220
220
  An async run belongs to the current user, cwd, and launching agent session at start time. Send, cancellation, and force-kill target only the recorded runner pid when command line and cwd still match the recorded owner data. Stale pid reuse must fail closed.
221
221
 
222
- On Unix-like systems, cancel and kill signal the runner process group when available, then fall back to the runner pid. The runner starts command-template children in that process group, so long-running descendants such as audio players stop with the run instead of becoming orphaned background processes. After the process exits, status reflects the operator action as `cancelled` or `killed` instead of a generic `exited`.
222
+ On Unix-like systems, cancel and kill signal the runner process group when available, then fall back to the runner pid. On native Windows, cancel and kill use Windows process-tree termination through the platform adapter. The runner starts command-template children in the owned process tree, so long-running descendants such as audio players should stop with the run instead of becoming orphaned background processes. After the process exits, status reflects the operator action as `cancelled` or `killed` instead of a generic `exited`.
223
223
 
224
224
  State is append-only where practical. Final result writes should be atomic. Recipe-local control endpoints and actor-message logs may live in the state dir. pi-actors core owns the generic run-local message adapter and runtime attention policy; command and message vocabularies belong to the recipe/script.
225
225
 
@@ -70,7 +70,7 @@ inspect target=run:docs_review view=tail
70
70
 
71
71
  Pipeline recipes demonstrate second-order composition:
72
72
 
73
- - `recipes/coordinator-locker.json`: Long-lived coordinator cell with queue, acquire/renew/release lease locks, journal, and actor messages for worker coordination.
73
+ - `recipes/coordinator-locker.json`: Long-lived coordinator cell with queue, acquire/renew/release lease locks, journal, actor messages for worker coordination, and platform-adapted control metadata.
74
74
  - `recipes/subagent-review-coordinator.json`: Lens reviewers → verifier → merger → judge → normalizer.
75
75
  - `recipes/pipeline-release-readiness.json`: Task-first release cell: changelog section → package summary → packaged skill summary → validation → release review → artifact report.
76
76
  - `recipes/pipeline-release-summary.json`: Evidence-only release summary cell: changelog section → package summary → packaged skill summary → validation → release summary / risks / PR body draft artifact. It does not commit, open a PR, merge, tag, publish, or perform external release side effects.
@@ -84,7 +84,7 @@ Pipeline recipes demonstrate second-order composition:
84
84
  - `recipes/pipeline-development-tasking.json`: Plan → task card → critique → integrator handoff.
85
85
  - `recipes/pipeline-docs-maintenance.json`: Docs index → documentation review → maintenance plan → artifact report.
86
86
  - `recipes/pipeline-media-library.json`: Playlist build → media-library artifact report.
87
- - `recipes/pipeline-room-swarm.json`: Room participants join `room:<run>`, coordinate over repeated room-visible rounds, leave cleanly, and synthesize the room transcript into a caller-provided artifact path. Keep model/thinking/mission policy caller-owned. Custom roles can be supplied with `roles_path` as a JSON array of `{ "name", "persona" }` objects; `name` stays ASCII-safe for `branch:<run>/<name>` addresses and debugger output remains plain and name-driven. The packaged swarm uses contacts for peer awareness but does not rely on direct branch delivery unless a caller-specific worker protocol consumes branch envelopes. Set `locker=true` to compose a local `coordinator-locker` cell under `{state_dir}/locker` for artifact ownership, resource lease locks, and a decision journal without merging locker policy into the room-participant script.
87
+ - `recipes/pipeline-room-swarm.json`: Room participants join `room:<run>`, coordinate over repeated room-visible rounds, leave cleanly, and synthesize the room transcript into a caller-provided artifact path. Supported coordinator modes are `consensus`, `pipeline`, `fanout`, and `pool`; unknown modes fail closed instead of silently running consensus. Keep model/thinking/mission policy caller-owned. Custom roles can be supplied with `roles_path` as a JSON array of `{ "name", "persona" }` objects; `name` stays ASCII-safe for `branch:<run>/<name>` addresses and debugger output remains plain and name-driven. The packaged swarm uses contacts for peer awareness but does not rely on direct branch delivery unless a caller-specific worker protocol consumes branch envelopes. Set `locker=true` to compose a local `coordinator-locker` cell under `{state_dir}/locker` for artifact ownership, resource lease locks, and a decision journal without merging locker policy into the room-participant script.
88
88
  - `recipes/pipeline-artifact-report.json`: Normalize → artifact-shaped output → actor-message-shaped record. This pipeline prepares a candidate artifact and emits `artifact.prepared`/`artifact.blocked`; the `artifact_path` is a target path, not a guarantee that the file was written.
89
89
  - `recipes/pipeline-artifact-write.json`: Normalize → artifact-shaped output → deterministic artifact write → actor-message-shaped record. Use only when the caller explicitly wants filesystem writes; `write_mode` is `create`, `overwrite`, or `append`.
90
90
  - `recipes/pipeline-artifact-bundle.json`: Optional validation → deterministic artifact write → machine-readable manifest generation → deterministic manifest write → actor-message-shaped record. Use when the caller explicitly wants a filesystem handoff bundle with both artifact and manifest paths.
@@ -97,7 +97,7 @@ Utility recipes cover local operator workflows that do not need subagents:
97
97
 
98
98
  - `recipes/utility-markdown-index.json`: List Markdown files in a directory as input for README/docs index maintenance.
99
99
  - `recipes/utility-jsonl-tail.json`: Tail a JSONL message/log file with a configurable line count.
100
- - `recipes/utility-validation-wrapper.json`: Run a caller-supplied validation command in a scoped directory with a bounded timeout.
100
+ - `recipes/utility-validation-wrapper.json`: Run a caller-supplied validation command in a scoped directory with a bounded timeout. This intentionally crosses a trusted shell boundary; discovery surfaces it as a diagnostic, and callers should pass explicit validation commands only.
101
101
  - `recipes/utility-git-status.json`: Read concise branch/worktree state for a repo.
102
102
  - `recipes/utility-git-log.json`: Read recent decorated commit history for a repo.
103
103
  - `recipes/utility-run-state-files.json`: List run-state files such as `run.json` under an async run state root.
@@ -115,7 +115,17 @@ Utility recipes cover local operator workflows that do not need subagents:
115
115
  - `recipes/utility-skill-summary.json`: Use `scripts/recipe-utils.mjs` to summarize packaged skill frontmatter, body shape, formatter-safe scalar lines, and package-version alignment.
116
116
  - `recipes/utility-validate-recipe.json`: Use `scripts/validate-recipe.mjs` to validate one template recipe file, or all packaged recipes in a directory with `all: true`.
117
117
 
118
- These recipes are intentionally small. Register them only for trusted local commands and prefer narrow scopes. The helper-backed utilities share `scripts/recipe-utils.mjs` so repeated parsing/listing logic stays out of recipe strings.
118
+ These recipes are intentionally small. Register them only for trusted local commands and prefer narrow scopes. Discovery diagnostics flag obvious trust-boundary shapes such as shell/eval/destructive commands; those warnings are operator review aids, not a sandbox. The helper-backed utilities share `scripts/recipe-utils.mjs` so repeated parsing/listing logic stays out of recipe strings.
119
+
120
+ ## Actor OS Smoke Matrix
121
+
122
+ The repeatable smoke surface is the normal validation suite:
123
+
124
+ ```text
125
+ npm test
126
+ ```
127
+
128
+ The scenario coverage is intentionally local-first and bounded: shared room coordination and roster snapshots (`actor-rooms` / `tools` tests), direct branch delivery and claim/handle transitions (`tools` and coordinator tests), inspector navigation (`actor-inspector-tui` tests), recipe context injection (`actor-recipe-context` / async-run tests), recipe persistence suggestions (`observability` tests), and opt-in retirement candidate/execution smoke (`observability` / async-run tests). These scenarios exercise public `spawn` / `message` / `inspect` behavior or the packaged script surfaces rather than relying on manual swarm demos.
119
129
 
120
130
  ## Music Player
121
131