@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.
- package/BACKLOG.md +0 -85
- package/CHANGELOG.md +17 -0
- package/README.md +7 -1
- package/dist/index.js +11 -0
- package/dist/lib/actor-rooms.d.ts +1 -0
- package/dist/lib/actor-rooms.js +15 -1
- package/dist/lib/async-runs.d.ts +17 -1
- package/dist/lib/async-runs.js +127 -36
- package/dist/lib/command-templates.js +8 -1
- package/dist/lib/observability.d.ts +15 -0
- package/dist/lib/observability.js +103 -18
- package/dist/lib/recipe-discovery.js +13 -5
- package/dist/lib/recipe-references.js +137 -11
- package/dist/lib/tools.js +5 -5
- package/docs/README.md +1 -1
- package/docs/actor-messages.md +4 -1
- package/docs/async-runs.md +3 -3
- package/docs/recipe-library.md +14 -4
- package/docs/template-recipes.md +37 -7
- package/docs/tool-registry.md +4 -3
- package/index.ts +14 -0
- package/lib/actor-rooms.ts +23 -1
- package/lib/async-runs.ts +186 -48
- package/lib/command-templates.ts +8 -1
- package/lib/observability.ts +133 -20
- package/lib/recipe-discovery.ts +21 -6
- package/lib/recipe-references.ts +141 -17
- package/lib/tools.ts +10 -8
- package/package.json +1 -1
- package/recipes/pipeline-room-swarm.json +1 -1
- package/scripts/coordinator.mjs +60 -32
- package/scripts/locker.mjs +61 -23
- package/scripts/validate-recipe.mjs +2 -2
- package/skills/actors/SKILL.md +10 -9
- package/skills/swarm/SKILL.md +1 -1
package/BACKLOG.md
CHANGED
|
@@ -2,79 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
## Open Work
|
|
4
4
|
|
|
5
|
-
### Branch Inbox Retention and Transition Scaling
|
|
6
|
-
|
|
7
|
-
- Priority: Medium.
|
|
8
|
-
- Goal: Keep direct branch message queues reliable for long-lived interactive branch runners without unbounded rewrite amplification.
|
|
9
|
-
- Direction:
|
|
10
|
-
- Evaluate current whole-file branch inbox status rewrites under realistic long-lived direct-message workloads.
|
|
11
|
-
- Consider bounded retention, compaction, or append-only transition logs for `queued` / `claimed` / `handled` / `failed` state changes while preserving stable message IDs and exact-once claim semantics.
|
|
12
|
-
- Keep branch-local inbox append/status mutations lock-guarded and preserve inspector visibility for unread/current-branch filters.
|
|
13
|
-
- Exit:
|
|
14
|
-
- A documented decision or implementation explains how branch inboxes scale for persistent runners and proves existing direct-message semantics remain compatible.
|
|
15
|
-
|
|
16
|
-
### Installed Recipe Trust Boundary Hardening
|
|
17
|
-
|
|
18
|
-
- Priority: Medium.
|
|
19
|
-
- Goal: Keep recipe-library growth local-first without letting operator muscle memory become an accidental sandbox bypass.
|
|
20
|
-
- Direction:
|
|
21
|
-
- Review packaged recipes, examples, and docs for destructive or external side effects and ensure they require explicit paths, typed args, narrow helper scripts, and clear operator gates.
|
|
22
|
-
- Keep warnings framed as diagnostics, not a security boundary.
|
|
23
|
-
- Prefer small audited helper scripts over broad shell templates when recipes touch files, processes, networks, or external services.
|
|
24
|
-
- Exit:
|
|
25
|
-
- A trust-boundary review confirms packaged recipes and docs preserve the current local-first/not-sandbox-first contract, with any needed hardening captured in tests or docs.
|
|
26
|
-
|
|
27
|
-
### Direct Branch Message Consumption Semantics
|
|
28
|
-
|
|
29
|
-
- Priority: Medium.
|
|
30
|
-
- Goal: Make it impossible to misunderstand branch inbox delivery as universal active delivery without a consuming coordinator or runner protocol.
|
|
31
|
-
- Direction:
|
|
32
|
-
- Audit README, actor-message docs, async-run docs, actors skill, and recipe guidance for branch inbox wording.
|
|
33
|
-
- Clarify that direct branch messages are queued and become active work only when the relevant coordinator/runner claims, injects, handles, or fails them.
|
|
34
|
-
- Add or update a bounded smoke scenario that demonstrates queued direct messages, claim/handle transitions, and inspector visibility.
|
|
35
|
-
- Exit:
|
|
36
|
-
- Public docs and tests show both halves of direct branch delivery: durable branch-local queueing and explicit worker consumption semantics.
|
|
37
|
-
|
|
38
|
-
### Actor Rooms, Roster, and Cross-Branch Messaging
|
|
39
|
-
|
|
40
|
-
- Priority: High.
|
|
41
|
-
- Goal: Continue evolving actor communication without adding a second public messaging model.
|
|
42
|
-
- Direction:
|
|
43
|
-
- Evaluate whether room storage/routing should remain built into the tool adapter or move behind a dedicated non-LLM communication actor recipe/script, possibly singleton-scoped. Preserve the same public `room:<run>` address and envelope either way.
|
|
44
|
-
- Treat the next backend decision as an evidence-backed experiment, not a rewrite: stress a real room/direct-message workload, compare the current file-backed adapter with a thin communication actor/helper, and record the decision.
|
|
45
|
-
- Consider reducing direct file-backed state where it improves coherence: model room/roster state as actor-owned data structures served by helper scripts/actors, with files retained only for durable snapshots, recovery, artifacts, or audit logs.
|
|
46
|
-
- Further storage changes should preserve the current burst/read/concurrency safeguards: branch communication snapshot writes are debounced, root snapshots stay current, roster files are not rewritten during bursts when only `last_seen` changes, room status inspection does not parse full timelines, branch-local inbox append/status rewrites are lock-guarded, and legacy no-ID branch inbox records can be claimed exactly once.
|
|
47
|
-
- Prevent monolith drift: `actor-rooms.ts` may remain a thin adapter, but growing routing policy, subscription loops, fanout policy, or long-lived state ownership should move behind a focused communication helper/actor rather than accumulating in the tool adapter.
|
|
48
|
-
- Exit:
|
|
49
|
-
- Any backend/storage change preserves existing `spawn` / `message` / `inspect` semantics and room address compatibility.
|
|
50
|
-
- A short decision note or changelog entry explains why the room backend stayed file-backed or moved behind a communication actor/helper.
|
|
51
|
-
|
|
52
|
-
### Graceful Actor Retirement
|
|
53
|
-
|
|
54
|
-
- Priority: Medium.
|
|
55
|
-
- Goal: Automatically retire coordinator/helper actors that were launched only to supervise a bounded worker tree once their dependent workers have finished.
|
|
56
|
-
- Direction:
|
|
57
|
-
- Build on the existing `retire_when: "children_terminal"` recipe/run metadata contract and observability retirement-candidate detection for ephemeral supervisors.
|
|
58
|
-
- Treat auto-retirement as opt-in only; never infer it for arbitrary long-lived services, user tools, or persistent backlog implementers.
|
|
59
|
-
- Extend candidate detection beyond current active command/proc-descendant gating to full observed child async-run state rather than log text: the supervisor may retire only when all launched child async runs are terminal and required artifacts/outbox events have been flushed.
|
|
60
|
-
- Prefer graceful stop (`control.stop` / actor message) before process termination; escalate only after a bounded timeout and record the retirement event in run state.
|
|
61
|
-
- Preserve manual `cancel` / `kill` semantics and make retirement visible through `inspect` / ambient observability.
|
|
62
|
-
- Exit:
|
|
63
|
-
- A packaged coordinator recipe can launch worker actors, complete its coordination duties, and shut itself down automatically after the worker tree reaches terminal state.
|
|
64
|
-
- Persistent services and implementer actors remain alive unless their recipe explicitly opts into retirement.
|
|
65
|
-
|
|
66
|
-
### Coordinator Strategy Boundary
|
|
67
|
-
|
|
68
|
-
- Priority: Medium.
|
|
69
|
-
- Goal: Keep the generic coordinator from becoming a second overloaded monolith as room/direct-message workflows mature.
|
|
70
|
-
- Direction:
|
|
71
|
-
- Split only at real pressure points: branch inbox claim/finalize helpers, participant execution, room transcript synthesis, and mode strategies are likely seams, but avoid cosmetic module churn.
|
|
72
|
-
- Preserve the current principle that the locker stays generic/thin and all orchestration policy stays in coordinator strategy code or recipe composition.
|
|
73
|
-
- Prefer reusable helper modules or small scripts only when at least two packaged workflows need the same behavior.
|
|
74
|
-
- Exit:
|
|
75
|
-
- Adding a new coordinator mode or packaged multi-agent workflow does not require editing unrelated mode logic.
|
|
76
|
-
- Existing room-swarm, locker, and direct-branch-message tests still cover the extracted seams.
|
|
77
|
-
|
|
78
5
|
### Consensus-First Build Recipe
|
|
79
6
|
|
|
80
7
|
- Priority: Medium.
|
|
@@ -90,18 +17,6 @@
|
|
|
90
17
|
- A packaged recipe can reproduce the interactive-music-instrument workflow shape for another single-artifact task without copying the demo script.
|
|
91
18
|
- Docs and skills point agents to the packaged recipe and explain when to choose it over a free-form room swarm.
|
|
92
19
|
|
|
93
|
-
### Actor OS Scenario Smoke Matrix
|
|
94
|
-
|
|
95
|
-
- Priority: Medium.
|
|
96
|
-
- Goal: Convert the 0.19.x actor-communication hardening into repeatable end-to-end scenario checks instead of relying on ad hoc demos.
|
|
97
|
-
- Direction:
|
|
98
|
-
- Cover one scenario each for shared room coordination, direct branch work delivery, branch inbox claim/handle/fail transitions, inspector navigation, recipe context injection, recipe persistence suggestion, and opt-in retirement candidate detection.
|
|
99
|
-
- Keep scenarios local-first and bounded: fake `pi`/models where possible, no external services, no long sleeps, no broad golden transcripts.
|
|
100
|
-
- Prefer packaged recipes and public `spawn` / `message` / `inspect` calls so the smoke matrix exercises the same surface agents use.
|
|
101
|
-
- Exit:
|
|
102
|
-
- A single validation command or documented test group verifies the actor OS behaviors that made 0.19.x production-useful.
|
|
103
|
-
- The smoke matrix catches regressions in actor communication, recipe memory, and observability without requiring a manual swarm demo.
|
|
104
|
-
|
|
105
20
|
### Persistent Backlog Implementer Workflow
|
|
106
21
|
|
|
107
22
|
- Priority: Medium.
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
## 0.21.0: Native Windows Actor Control and Literate Recipes
|
|
6
|
+
|
|
7
|
+
- `[Async Runs]` Added a platform-adapted run-control path: Unix FIFO behavior remains backward-compatible, native Windows can target named-pipe run-control endpoints recorded in run state, and run message receipts still update events and inbox state through the same actor-message path.
|
|
8
|
+
- `[Async Runs]` Added Windows process-tree termination planning for cancel/kill through `taskkill`, while preserving Unix process-group signaling semantics.
|
|
9
|
+
- `[Scripts]` Migrated `locker.mjs` and coordinator locker calls to platform-adapted control metadata: Unix still uses `control.fifo`, while native Windows can use a deterministic named-pipe endpoint with the same message protocol.
|
|
10
|
+
- `[Branch Messages]` Added bounded branch-inbox terminal retention during status transitions, preserving active queued/claimed work while compacting older handled/failed records for long-lived branch runners.
|
|
11
|
+
- `[Recipe Discovery]` Tightened trust-boundary diagnostics so combined short shell/eval flags such as `bash -lc` and nested recipe command-template objects are surfaced, including the packaged validation wrapper's trusted shell boundary.
|
|
12
|
+
- `[Rooms]` Recorded the backend decision to keep the current file-backed room adapter until real workflows need live subscriptions/fanout or shared mutable state, backed by a mixed room/direct-branch workload regression.
|
|
13
|
+
- `[Recipes]` Added Markdown-authored recipe loading for `.md` files with frontmatter metadata and fenced executable recipe/template blocks, with same-id JSON shadowing Markdown in the same priority layer.
|
|
14
|
+
- `[Retirement]` Extended run summaries to discover nested child async-run state dirs, blocks opt-in retirement while nested children are still running, surfaces child/terminal child counts on candidates, and has the session watcher retire ready candidates with one graceful stop attempt plus owned cancellation fallback. Added an integration smoke where an idle supervisor stops after its nested child is terminal while a non-opt-in service remains running.
|
|
15
|
+
- `[Coordinator]` Consolidated direct branch inbox claim/finalize rewrites behind one locked mutation helper and moved room-swarm mode dispatch behind an explicit mode registry. Unknown coordinator modes now fail closed, and `pipeline-room-swarm` exposes the supported mode enum.
|
|
16
|
+
- `[Docs]` Documented the local Actor OS smoke matrix covered by `npm test`, spanning room coordination, direct branch delivery, inbox claim/handle transitions, inspector navigation, recipe context injection, persistence suggestions, and opt-in retirement smoke.
|
|
17
|
+
- `[Docs/Tests]` Documented native Windows support scope and added regression coverage for Windows endpoint metadata, mocked named-pipe sends, Windows process-control planning, unchanged Unix FIFO behavior, locker control metadata, branch inbox compaction, mixed room/direct workloads, Markdown recipe loading/discovery/validation, nested child-run retirement gating, and packaged recipe trust diagnostics.
|
|
18
|
+
- `[Package]` Bumped package metadata, lockfile metadata, and packaged skill metadata to `0.21.0` for the minor release.
|
|
19
|
+
|
|
3
20
|
## 0.20.2: Installed Extension Entrypoint Hotfix
|
|
4
21
|
|
|
5
22
|
- `[Packaging]` Added a JavaScript extension entrypoint wrapper and changed package metadata to load `./index.js`, so npm-installed packages import compiled `dist/index.js` instead of asking Node to strip `index.ts` under `node_modules`. Source checkouts still fall back to `index.ts` before a local build exists.
|
package/README.md
CHANGED
|
@@ -169,6 +169,7 @@ The persistent tool surface is file-discovered:
|
|
|
169
169
|
|
|
170
170
|
```text
|
|
171
171
|
~/.pi/agent/recipes/*.json
|
|
172
|
+
~/.pi/agent/recipes/*.md
|
|
172
173
|
```
|
|
173
174
|
|
|
174
175
|
That directory is operator-managed executable memory.
|
|
@@ -178,6 +179,7 @@ Rules:
|
|
|
178
179
|
- User recipes in `~/.pi/agent/recipes/` are tools by location;
|
|
179
180
|
- Recipe filenames define tool ids;
|
|
180
181
|
- User recipes override same-name lower-priority recipes;
|
|
182
|
+
- Same-id JSON recipes shadow Markdown recipes in the same priority layer;
|
|
181
183
|
- Packaged recipes are standard-library components, not automatically installed operator policy;
|
|
182
184
|
- `register_tool` creates, updates, lists, or deletes user recipe files through the normal agent interface.
|
|
183
185
|
|
|
@@ -220,7 +222,7 @@ Templates support:
|
|
|
220
222
|
- Retries, recovery, failure policy, delays, and guarded execution;
|
|
221
223
|
- Async run values such as `{run_id}`, `{state_dir}`, `{actor_address}`, `{default_room}`, and `{communication_file}`.
|
|
222
224
|
|
|
223
|
-
The template owns execution shape. The recipe owns saved metadata, defaults, imports, mailbox, and artifacts. The run actor owns detached lifecycle, state, messages, cancellation, and inspection. File-backed async recipes also provide child `pi -p` actors with a bounded JSONL recipe context bundle by default, including raw entry/import recipe records and a `"you_are_here": true` marker for the recipe node that launched the child. Set `"actor_context": false` or `"off"` in a recipe to suppress that context for minimal prompts.
|
|
225
|
+
The template owns execution shape. The recipe owns saved metadata, defaults, imports, mailbox, and artifacts. JSON is the canonical precise recipe format; Markdown recipes use frontmatter plus fenced `template`/`json recipe` blocks for literate authoring and compile into the same model. The run actor owns detached lifecycle, state, messages, cancellation, and inspection. File-backed async recipes also provide child `pi -p` actors with a bounded JSONL recipe context bundle by default, including raw entry/import recipe records and a `"you_are_here": true` marker for the recipe node that launched the child. Set `"actor_context": false` or `"off"` in a recipe to suppress that context for minimal prompts.
|
|
224
226
|
|
|
225
227
|
## Recipe Library
|
|
226
228
|
|
|
@@ -249,6 +251,10 @@ Use artifacts when outputs should survive context compression.
|
|
|
249
251
|
|
|
250
252
|
Use mailbox declarations when an actor has a stable conversational surface.
|
|
251
253
|
|
|
254
|
+
## Platform Support
|
|
255
|
+
|
|
256
|
+
Core actor state, inspection, foreground tools, and basic async runs are portable Node.js behavior. Run-local messaging and stop/kill use a platform adapter under the same `message` API: Unix-compatible recipes can use their existing local control endpoint, while native Windows recipes can expose a Windows-native endpoint in run state. Some packaged scripts still depend on Unix tools and are WSL/Linux/macOS-only until migrated; their public recipe surface should stay `spawn` / `message` / `inspect` either way.
|
|
257
|
+
|
|
252
258
|
## Safety Boundary
|
|
253
259
|
|
|
254
260
|
`pi-actors` is local-first, not sandbox-first.
|
package/dist/index.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { existsSync, readdirSync, watch } from "node:fs";
|
|
8
8
|
import * as ActorInspectorTui from "./lib/actor-inspector-tui.js";
|
|
9
|
+
import * as AsyncRuns from "./lib/async-runs.js";
|
|
9
10
|
import * as CommandTemplates from "./lib/command-templates.js";
|
|
10
11
|
import * as Observability from "./lib/observability.js";
|
|
11
12
|
import * as Paths from "./lib/paths.js";
|
|
@@ -38,6 +39,7 @@ export default function toolRegistryExtension(pi) {
|
|
|
38
39
|
const runDirWatchers = new Map();
|
|
39
40
|
const observedRuns = new Map();
|
|
40
41
|
const observedRunEventLines = new Map();
|
|
42
|
+
const retirementAttempts = new Set();
|
|
41
43
|
let runStatusFrame = 0;
|
|
42
44
|
let communicationWidgetVisible = false;
|
|
43
45
|
let actorInspectorRows = 12;
|
|
@@ -49,6 +51,14 @@ export default function toolRegistryExtension(pi) {
|
|
|
49
51
|
let selectedInspectorSequence;
|
|
50
52
|
let recipeWatcherFailureNotified = false;
|
|
51
53
|
const getRunOwnerId = (ctx) => ctx.sessionManager.getSessionId();
|
|
54
|
+
const retireCandidateRuns = (ctx, summary) => {
|
|
55
|
+
void Observability.executeRunRetirements(summary, {
|
|
56
|
+
attempted: retirementAttempts,
|
|
57
|
+
cancelRun: (candidate) => AsyncRuns.cancelRun(candidate.stateDir),
|
|
58
|
+
notify: (message, level) => ctx.ui.notify(message, level),
|
|
59
|
+
sendStop: (candidate) => AsyncRuns.sendRunMessage(candidate.stateDir, "stop"),
|
|
60
|
+
});
|
|
61
|
+
};
|
|
52
62
|
const updateRunUi = (ctx, notify = false) => {
|
|
53
63
|
const ownerId = getRunOwnerId(ctx);
|
|
54
64
|
const summary = Observability.summarizeRuns(undefined, ownerId);
|
|
@@ -93,6 +103,7 @@ export default function toolRegistryExtension(pi) {
|
|
|
93
103
|
const outboxEvents = Observability.detectRunOutboxEvents(observedRunEventLines, summary);
|
|
94
104
|
if (!notify)
|
|
95
105
|
return;
|
|
106
|
+
retireCandidateRuns(ctx, summary);
|
|
96
107
|
for (const transition of transitions) {
|
|
97
108
|
if (!Observability.shouldNotifyRunTransition(transition))
|
|
98
109
|
continue;
|
|
@@ -67,6 +67,7 @@ export declare function readBranchInboxMessages(stateDir: string, run: string, a
|
|
|
67
67
|
queued_at?: string;
|
|
68
68
|
status?: string;
|
|
69
69
|
}>;
|
|
70
|
+
export declare function getBranchInboxTerminalRetainLimit(): number;
|
|
70
71
|
export declare function appendBranchInboxMessage(stateDir: string, run: string, address: string, message: ActorMessage): void;
|
|
71
72
|
export declare function updateBranchInboxMessageStatus(stateDir: string, run: string, address: string, id: string, status: "claimed" | "handled" | "failed", metadata?: Record<string, unknown>): boolean;
|
|
72
73
|
export declare function appendRoomMessage(stateDir: string, room: string, message: ActorMessage): RoomAppendResult;
|
package/dist/lib/actor-rooms.js
CHANGED
|
@@ -9,6 +9,7 @@ import * as path from "node:path";
|
|
|
9
9
|
const STATE_LOCK_MAX_AGE_MS = 5 * 60 * 1000;
|
|
10
10
|
const STATE_LOCK_TIMEOUT_MS = 5000;
|
|
11
11
|
const DEFAULT_ROOM_MAX_MESSAGES = 10000;
|
|
12
|
+
const DEFAULT_BRANCH_INBOX_TERMINAL_RETAINED = 2000;
|
|
12
13
|
const DEFAULT_SNAPSHOT_MIN_INTERVAL_MS = 250;
|
|
13
14
|
function roomDir(stateDir, room) {
|
|
14
15
|
return path.join(stateDir, "rooms", room);
|
|
@@ -275,6 +276,18 @@ export function readBranchInboxMessages(stateDir, run, address, limit = 40) {
|
|
|
275
276
|
throw error;
|
|
276
277
|
}
|
|
277
278
|
}
|
|
279
|
+
export function getBranchInboxTerminalRetainLimit() {
|
|
280
|
+
const value = Number(process.env.PI_ACTORS_BRANCH_INBOX_TERMINAL_RETAINED ?? "");
|
|
281
|
+
return Number.isInteger(value) && value >= 0
|
|
282
|
+
? value
|
|
283
|
+
: DEFAULT_BRANCH_INBOX_TERMINAL_RETAINED;
|
|
284
|
+
}
|
|
285
|
+
function compactBranchInboxMessages(messages) {
|
|
286
|
+
const retainTerminal = getBranchInboxTerminalRetainLimit();
|
|
287
|
+
const active = messages.filter((message) => message.status !== "handled" && message.status !== "failed");
|
|
288
|
+
const terminal = messages.filter((message) => message.status === "handled" || message.status === "failed");
|
|
289
|
+
return [...terminal.slice(-retainTerminal), ...active];
|
|
290
|
+
}
|
|
278
291
|
export function appendBranchInboxMessage(stateDir, run, address, message) {
|
|
279
292
|
const branch = branchIdFromAddress(address, run);
|
|
280
293
|
if (!branch)
|
|
@@ -305,7 +318,8 @@ export function updateBranchInboxMessageStatus(stateDir, run, address, id, statu
|
|
|
305
318
|
});
|
|
306
319
|
if (!changed)
|
|
307
320
|
return false;
|
|
308
|
-
|
|
321
|
+
const compacted = compactBranchInboxMessages(updated);
|
|
322
|
+
fs.writeFileSync(file, `${compacted.map((message) => JSON.stringify(message)).join("\n")}\n`);
|
|
309
323
|
return true;
|
|
310
324
|
}
|
|
311
325
|
finally {
|
package/dist/lib/async-runs.d.ts
CHANGED
|
@@ -6,8 +6,13 @@
|
|
|
6
6
|
import type { CommandTemplateFailureScope, CommandTemplateValue } from "./command-templates.ts";
|
|
7
7
|
import * as RecipeReferences from "./recipe-references.ts";
|
|
8
8
|
export type AsyncRunLaunchSource = "spawn" | "tool";
|
|
9
|
+
export interface AsyncRunControlEndpoint {
|
|
10
|
+
path: string;
|
|
11
|
+
type: "fifo" | "named-pipe";
|
|
12
|
+
}
|
|
9
13
|
export interface AsyncRunStartParams {
|
|
10
14
|
async?: boolean;
|
|
15
|
+
control?: AsyncRunControlEndpoint;
|
|
11
16
|
file?: string;
|
|
12
17
|
launch_source?: AsyncRunLaunchSource;
|
|
13
18
|
name?: string;
|
|
@@ -72,6 +77,7 @@ export interface AsyncRunMeta {
|
|
|
72
77
|
template: CommandTemplateValue;
|
|
73
78
|
values: Record<string, unknown>;
|
|
74
79
|
artifacts?: Record<string, string>;
|
|
80
|
+
control?: AsyncRunControlEndpoint;
|
|
75
81
|
mailbox?: RecipeReferences.TemplateRecipeMailbox;
|
|
76
82
|
recipe_context_records?: RecipeReferences.TemplateRecipeContextRecord[];
|
|
77
83
|
retire_when?: "children_terminal";
|
|
@@ -96,6 +102,16 @@ export declare function appendRunOutboxEvent(runOrDir: string, event: {
|
|
|
96
102
|
to?: string;
|
|
97
103
|
type?: string;
|
|
98
104
|
}): Record<string, unknown>;
|
|
99
|
-
export
|
|
105
|
+
export interface SendRunMessageOptions {
|
|
106
|
+
namedPipeSend?: (path: string, payload: string) => Promise<number>;
|
|
107
|
+
platform?: NodeJS.Platform;
|
|
108
|
+
}
|
|
109
|
+
export declare function sendRunMessage(runOrDir: string, message: string, options?: SendRunMessageOptions): Promise<Record<string, unknown>>;
|
|
110
|
+
export interface RunProcessSignalPlan {
|
|
111
|
+
args?: string[];
|
|
112
|
+
command?: string;
|
|
113
|
+
signalTarget: "processGroup" | "process" | "processTree";
|
|
114
|
+
}
|
|
115
|
+
export declare function getRunProcessSignalPlan(pid: number, signal: NodeJS.Signals, runtimePlatform?: NodeJS.Platform): RunProcessSignalPlan;
|
|
100
116
|
export declare function cancelRun(runOrDir: string): Record<string, unknown>;
|
|
101
117
|
export declare function killRun(runOrDir: string): Record<string, unknown>;
|
package/dist/lib/async-runs.js
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
* Zones: async runtime, lifecycle, state files
|
|
4
4
|
* Owns detached run state, observation, log tailing, listing, and cancellation safety
|
|
5
5
|
*/
|
|
6
|
-
import { spawn } from "node:child_process";
|
|
6
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
7
7
|
import { closeSync, constants, existsSync, mkdirSync, openSync, readdirSync, readFileSync, readlinkSync, rmSync, statSync, writeFileSync, writeSync, } from "node:fs";
|
|
8
|
+
import { createConnection } from "node:net";
|
|
8
9
|
import { platform } from "node:os";
|
|
9
10
|
import { basename, dirname, extname, join, resolve } from "node:path";
|
|
10
11
|
import { fileURLToPath } from "node:url";
|
|
@@ -89,7 +90,8 @@ function assertNoActiveRunState(stateDir) {
|
|
|
89
90
|
throw new Error(`Run state already has an active owned process: ${String(meta.run ?? stateDir)}. Stop it before reusing the same run_id or state_dir.`);
|
|
90
91
|
}
|
|
91
92
|
function resolveRecipeFile(file) {
|
|
92
|
-
return RecipeReferences.
|
|
93
|
+
return (RecipeReferences.getRecipePath(file, DEFAULT_RECIPE_ROOT) ??
|
|
94
|
+
RecipeReferences.resolveRecipePath(file, DEFAULT_RECIPE_ROOT));
|
|
93
95
|
}
|
|
94
96
|
function isMutableUsageRecipeFile(file) {
|
|
95
97
|
const userRoot = resolve(DEFAULT_RECIPE_ROOT);
|
|
@@ -300,6 +302,7 @@ export function startRun(params, cwd) {
|
|
|
300
302
|
template: resolved.template,
|
|
301
303
|
values,
|
|
302
304
|
...(artifacts ? { artifacts } : {}),
|
|
305
|
+
...(startParams.control ? { control: startParams.control } : {}),
|
|
303
306
|
...(startParams.mailbox ? { mailbox: startParams.mailbox } : {}),
|
|
304
307
|
...(recipeContextRecords && recipeContextRecords.length > 0
|
|
305
308
|
? { recipe_context_records: recipeContextRecords }
|
|
@@ -507,10 +510,84 @@ export function appendRunOutboxEvent(runOrDir, event) {
|
|
|
507
510
|
state_dir: stateDir,
|
|
508
511
|
};
|
|
509
512
|
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
+
function getRunControlEndpoint(status, stateDir) {
|
|
514
|
+
const control = status.control;
|
|
515
|
+
if (control && typeof control === "object" && !Array.isArray(control)) {
|
|
516
|
+
const record = control;
|
|
517
|
+
if ((record.type === "fifo" || record.type === "named-pipe") &&
|
|
518
|
+
typeof record.path === "string" &&
|
|
519
|
+
record.path.trim()) {
|
|
520
|
+
return { path: record.path, type: record.type };
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
return { path: join(stateDir, "control.fifo"), type: "fifo" };
|
|
524
|
+
}
|
|
525
|
+
function writeRunMessageReceipt(stateDir, message, bytes) {
|
|
526
|
+
const trimmedMessage = message.trim().toLowerCase();
|
|
527
|
+
const terminalMessage = ["stop", "cancel", "quit", "exit"].includes(trimmedMessage);
|
|
528
|
+
const ts = new Date().toISOString();
|
|
529
|
+
writeFileSync(join(stateDir, "events.jsonl"), `${JSON.stringify({ bytes, event: "run.message", terminal: terminalMessage || undefined, ts })}\n`, { flag: "a" });
|
|
530
|
+
try {
|
|
531
|
+
const envelope = JSON.parse(message);
|
|
532
|
+
writeFileSync(join(stateDir, "inbox.jsonl"), `${JSON.stringify({ ...envelope, received_at: ts })}\n`, { flag: "a" });
|
|
533
|
+
}
|
|
534
|
+
catch {
|
|
535
|
+
// Plain control lines are already represented in events.jsonl.
|
|
536
|
+
}
|
|
537
|
+
if (terminalMessage) {
|
|
538
|
+
markTerminalHandled(stateDir, {
|
|
539
|
+
event: "run.message",
|
|
540
|
+
message: trimmedMessage,
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
function sendRunMessageToFifo(endpoint, payload) {
|
|
545
|
+
if (!existsSync(endpoint.path))
|
|
546
|
+
throw new Error(`Run control FIFO not found: ${endpoint.path}`);
|
|
547
|
+
const stat = statSync(endpoint.path);
|
|
548
|
+
if ((stat.mode & constants.S_IFMT) !== constants.S_IFIFO) {
|
|
549
|
+
throw new Error(`Run control endpoint is not a FIFO: ${endpoint.path}`);
|
|
513
550
|
}
|
|
551
|
+
let fd;
|
|
552
|
+
try {
|
|
553
|
+
fd = openSync(endpoint.path, constants.O_WRONLY | constants.O_NONBLOCK);
|
|
554
|
+
return writeSync(fd, payload);
|
|
555
|
+
}
|
|
556
|
+
finally {
|
|
557
|
+
if (fd !== undefined)
|
|
558
|
+
closeSync(fd);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
function sendRunMessageToNamedPipe(endpoint, payload, send) {
|
|
562
|
+
if (send)
|
|
563
|
+
return send(endpoint.path, payload);
|
|
564
|
+
return new Promise((resolve, reject) => {
|
|
565
|
+
const socket = createConnection(endpoint.path);
|
|
566
|
+
let settled = false;
|
|
567
|
+
const timeout = setTimeout(() => {
|
|
568
|
+
if (settled)
|
|
569
|
+
return;
|
|
570
|
+
settled = true;
|
|
571
|
+
socket.destroy();
|
|
572
|
+
reject(new Error("named pipe connection timed out"));
|
|
573
|
+
}, 5000);
|
|
574
|
+
const finish = (error) => {
|
|
575
|
+
if (settled)
|
|
576
|
+
return;
|
|
577
|
+
settled = true;
|
|
578
|
+
clearTimeout(timeout);
|
|
579
|
+
if (error)
|
|
580
|
+
reject(error);
|
|
581
|
+
else
|
|
582
|
+
resolve(Buffer.byteLength(payload));
|
|
583
|
+
};
|
|
584
|
+
socket.on("error", finish);
|
|
585
|
+
socket.on("connect", () => {
|
|
586
|
+
socket.end(payload, () => finish());
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
export async function sendRunMessage(runOrDir, message, options = {}) {
|
|
514
591
|
const status = getRunStatus(runOrDir);
|
|
515
592
|
const stateDir = String(status.state_dir);
|
|
516
593
|
const run = String(status.run ?? runOrDir);
|
|
@@ -521,52 +598,66 @@ export function sendRunMessage(runOrDir, message) {
|
|
|
521
598
|
throw new Error(`Run pid is not alive: ${run}`);
|
|
522
599
|
if (!pidMatchesRun(pid, String(status.cwd), stateDir))
|
|
523
600
|
throw new Error(`Run pid owner mismatch: ${run}`);
|
|
524
|
-
const
|
|
525
|
-
if (!existsSync(controlPath))
|
|
526
|
-
throw new Error(`Run control FIFO not found: ${controlPath}`);
|
|
527
|
-
const stat = statSync(controlPath);
|
|
528
|
-
if ((stat.mode & constants.S_IFMT) !== constants.S_IFIFO) {
|
|
529
|
-
throw new Error(`Run control endpoint is not a FIFO: ${controlPath}`);
|
|
530
|
-
}
|
|
601
|
+
const endpoint = getRunControlEndpoint(status, stateDir);
|
|
531
602
|
const payload = message.endsWith("\n") ? message : `${message}\n`;
|
|
532
|
-
|
|
603
|
+
const runtimePlatform = options.platform ?? process.platform;
|
|
533
604
|
try {
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
event: "run.message",
|
|
550
|
-
message: trimmedMessage,
|
|
551
|
-
});
|
|
605
|
+
if (endpoint.type === "fifo") {
|
|
606
|
+
if (runtimePlatform === "win32") {
|
|
607
|
+
throw new Error("run actor messages on native Windows require a named-pipe control endpoint; this recipe still exposes Unix FIFO control.");
|
|
608
|
+
}
|
|
609
|
+
const bytes = sendRunMessageToFifo(endpoint, payload);
|
|
610
|
+
writeRunMessageReceipt(stateDir, message, bytes);
|
|
611
|
+
return {
|
|
612
|
+
bytes,
|
|
613
|
+
control: "control.fifo",
|
|
614
|
+
control_path: endpoint.path,
|
|
615
|
+
control_type: endpoint.type,
|
|
616
|
+
run,
|
|
617
|
+
sent: true,
|
|
618
|
+
state_dir: stateDir,
|
|
619
|
+
};
|
|
552
620
|
}
|
|
621
|
+
const bytes = await sendRunMessageToNamedPipe(endpoint, payload, options.namedPipeSend);
|
|
622
|
+
writeRunMessageReceipt(stateDir, message, bytes);
|
|
553
623
|
return {
|
|
554
624
|
bytes,
|
|
555
|
-
control:
|
|
625
|
+
control: endpoint.path,
|
|
626
|
+
control_path: endpoint.path,
|
|
627
|
+
control_type: endpoint.type,
|
|
556
628
|
run,
|
|
557
629
|
sent: true,
|
|
558
630
|
state_dir: stateDir,
|
|
559
631
|
};
|
|
560
632
|
}
|
|
561
633
|
catch (error) {
|
|
562
|
-
throw new Error(`Run control
|
|
634
|
+
throw new Error(`Run control endpoint is not ready: ${endpoint.path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
563
635
|
}
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
636
|
+
}
|
|
637
|
+
export function getRunProcessSignalPlan(pid, signal, runtimePlatform = process.platform) {
|
|
638
|
+
if (runtimePlatform === "win32") {
|
|
639
|
+
return {
|
|
640
|
+
args: [
|
|
641
|
+
"/PID",
|
|
642
|
+
String(pid),
|
|
643
|
+
"/T",
|
|
644
|
+
...(signal === "SIGKILL" ? ["/F"] : []),
|
|
645
|
+
],
|
|
646
|
+
command: "taskkill",
|
|
647
|
+
signalTarget: "processTree",
|
|
648
|
+
};
|
|
567
649
|
}
|
|
650
|
+
return { signalTarget: "processGroup" };
|
|
568
651
|
}
|
|
569
652
|
function signalOwnedRunProcess(pid, signal) {
|
|
653
|
+
const plan = getRunProcessSignalPlan(pid, signal);
|
|
654
|
+
if (plan.command && plan.args) {
|
|
655
|
+
const result = spawnSync(plan.command, plan.args, { encoding: "utf8" });
|
|
656
|
+
if (result.status !== 0) {
|
|
657
|
+
throw new Error(result.stderr?.trim() || result.stdout?.trim() || `${plan.command} failed`);
|
|
658
|
+
}
|
|
659
|
+
return plan;
|
|
660
|
+
}
|
|
570
661
|
try {
|
|
571
662
|
process.kill(-pid, signal);
|
|
572
663
|
return { signalTarget: "processGroup" };
|
|
@@ -74,8 +74,15 @@ function getExecutableName(command) {
|
|
|
74
74
|
return "";
|
|
75
75
|
return command.split(/[\\/]/).pop()?.toLowerCase() ?? "";
|
|
76
76
|
}
|
|
77
|
+
function matchesFlag(arg, flag) {
|
|
78
|
+
if (arg === flag)
|
|
79
|
+
return true;
|
|
80
|
+
if (/^-[A-Za-z]$/.test(flag) && /^-[A-Za-z]+$/.test(arg))
|
|
81
|
+
return arg.slice(1).includes(flag.slice(1));
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
77
84
|
function hasAnyFlag(args, flags) {
|
|
78
|
-
return args.some((arg) => flags.
|
|
85
|
+
return args.some((arg) => flags.some((flag) => matchesFlag(arg, flag)));
|
|
79
86
|
}
|
|
80
87
|
function hasRiskyPathArg(args) {
|
|
81
88
|
return args.some((arg) => arg === "/" ||
|
|
@@ -37,9 +37,23 @@ export interface RunSummary {
|
|
|
37
37
|
}
|
|
38
38
|
export interface RunRetirementCandidate {
|
|
39
39
|
activeSubagents: number;
|
|
40
|
+
childRuns: number;
|
|
40
41
|
descendantSubagents: number;
|
|
41
42
|
run: string;
|
|
42
43
|
stateDir: string;
|
|
44
|
+
terminalChildRuns: number;
|
|
45
|
+
}
|
|
46
|
+
export interface RunRetirementExecution {
|
|
47
|
+
action: "stop" | "cancel" | "skip" | "failed";
|
|
48
|
+
error?: string;
|
|
49
|
+
run: string;
|
|
50
|
+
stateDir: string;
|
|
51
|
+
}
|
|
52
|
+
export interface RunRetirementExecutorOptions {
|
|
53
|
+
attempted?: Set<string>;
|
|
54
|
+
cancelRun: (candidate: RunRetirementCandidate) => Record<string, unknown>;
|
|
55
|
+
notify?: (message: string, level: "info" | "warning" | "error") => void;
|
|
56
|
+
sendStop: (candidate: RunRetirementCandidate) => Promise<unknown>;
|
|
43
57
|
}
|
|
44
58
|
export interface RunTransition {
|
|
45
59
|
from: RunObservedStatus;
|
|
@@ -72,6 +86,7 @@ export declare function countRunningSubagents(stateRoot?: string, ownerId?: stri
|
|
|
72
86
|
export declare function renderSubagentStatus(count: number, frame?: number): string | undefined;
|
|
73
87
|
export declare function renderRunStatus(summary: RunSummary, frame?: number): string | undefined;
|
|
74
88
|
export declare function findRunRetirementCandidates(summary: RunSummary): RunRetirementCandidate[];
|
|
89
|
+
export declare function executeRunRetirements(summary: RunSummary, options: RunRetirementExecutorOptions): Promise<RunRetirementExecution[]>;
|
|
75
90
|
export declare function detectRunTransitions(previous: Map<string, RunObservedStatus>, summary: RunSummary): RunTransition[];
|
|
76
91
|
export declare function pruneRunObservationState(previousStatuses: Map<string, RunObservedStatus>, previousLineCounts: Map<string, number>, summary: RunSummary, terminalRuns?: Iterable<string>): void;
|
|
77
92
|
export declare function detectRunOutboxEvents(previousLineCounts: Map<string, number>, summary: RunSummary): RunOutboxEvent[];
|