pi-durable-subagents 1.5.1 → 1.6.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.
- package/CHANGELOG.md +41 -0
- package/README.md +69 -14
- package/dist/agent/capabilities.js +19 -11
- package/dist/agent/child/live.js +10 -2
- package/dist/agent/child.js +1 -1
- package/dist/agent/exact/capture.js +22 -2
- package/dist/agent/exact/child.js +8 -1
- package/dist/agent/exact/session.js +4 -2
- package/dist/agent/extension.js +13 -0
- package/dist/agent/main/snapshots.js +10 -9
- package/dist/agent/main.js +2 -1
- package/dist/cli/chaos/stack.js +53 -21
- package/dist/cli/control.js +4 -3
- package/dist/cli/doctor.js +3 -1
- package/dist/cli/drill/index.js +35 -17
- package/dist/cli/hold.js +221 -16
- package/dist/cli/main.js +7 -2
- package/dist/cli/restart.js +14 -2
- package/dist/cli/service.js +77 -2
- package/dist/cli/smoke.js +10 -6
- package/dist/compat/agents.js +47 -13
- package/dist/events/log.js +33 -11
- package/dist/kernel/cache.js +3 -2
- package/dist/kernel/fsx.js +203 -0
- package/dist/kernel/ids.js +11 -0
- package/dist/kernel/journal.js +42 -25
- package/dist/kernel/mailbox.js +7 -4
- package/dist/orchestrator/engine.js +49 -46
- package/dist/orchestrator/evaluator-client.js +11 -3
- package/dist/orchestrator/executor/command.js +21 -6
- package/dist/orchestrator/executor/digest.js +2 -2
- package/dist/orchestrator/executor/effects/gate.js +291 -56
- package/dist/orchestrator/executor/effects/index.js +3 -4
- package/dist/orchestrator/executor/effects/output.js +9 -5
- package/dist/orchestrator/executor/effects/prepare.js +34 -32
- package/dist/orchestrator/executor/effects/worktree-path.js +102 -0
- package/dist/orchestrator/executor/ended.js +7 -3
- package/dist/orchestrator/executor/index.js +216 -62
- package/dist/orchestrator/executor/memory.js +15 -0
- package/dist/orchestrator/executor/observe.js +6 -1
- package/dist/orchestrator/executor/session.js +10 -4
- package/dist/orchestrator/executor/sweep.js +34 -1
- package/dist/orchestrator/executor/usage.js +19 -5
- package/dist/orchestrator/executor/worktree.js +1 -1
- package/dist/orchestrator/main.js +4 -2
- package/dist/orchestrator/reader-ledger.js +3 -2
- package/dist/orchestrator/settled.js +2 -1
- package/dist/orchestrator/snapshot.js +24 -19
- package/dist/orchestrator/stats.js +2 -1
- package/dist/orchestrator/store.js +3 -2
- package/dist/platform/cmdline.js +3 -0
- package/dist/platform/command.js +184 -20
- package/dist/platform/containment.js +204 -40
- package/dist/platform/job.js +1404 -0
- package/dist/platform/launch.js +262 -29
- package/dist/platform/lease.js +351 -32
- package/dist/platform/lock.js +464 -16
- package/dist/platform/proctable.js +31 -2
- package/dist/platform/windows.js +642 -0
- package/dist/requests.js +1 -1
- package/dist/types.js +1 -0
- package/dist/ui/data.js +7 -6
- package/dist/ui/session.js +6 -5
- package/package.json +3 -3
package/dist/platform/launch.js
CHANGED
|
@@ -7,14 +7,26 @@
|
|
|
7
7
|
// end it. Without lingering the user manager stops its scopes at logout, while a plain detached process survives it
|
|
8
8
|
// (KillUserProcesses=no, the usual default): it starts as a plain detached process then.
|
|
9
9
|
import { spawn } from "node:child_process";
|
|
10
|
-
import { accessSync, constants, existsSync, statSync } from "node:fs";
|
|
10
|
+
import { accessSync, closeSync, constants, existsSync, mkdirSync, openSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
11
11
|
import { userInfo } from "node:os";
|
|
12
|
-
import { delimiter, isAbsolute, join } from "node:path";
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
import { delimiter, dirname, extname, isAbsolute, join, normalize, resolve, win32 } from "node:path";
|
|
13
|
+
import { ENV } from "../types.js";
|
|
14
|
+
import { comspec, envValue, IS_WINDOWS, winTagArg } from "./windows.js";
|
|
15
|
+
const WINDOWS_EXECUTABLE = new Set([".exe", ".com", ".cmd", ".bat"]);
|
|
16
|
+
/** Whether `path` is a regular file this process may execute. On Windows only a PE image or a batch file is directly
|
|
17
|
+
* executable; the extensionless `pi` (a POSIX shell script an npm install also writes) is not. `platform` names the
|
|
18
|
+
* rules that apply (default: this process's; tests exercise the Windows rules on another host). */
|
|
19
|
+
export function executableFile(path, platform = process.platform) {
|
|
15
20
|
try {
|
|
16
21
|
if (!statSync(path).isFile())
|
|
17
22
|
return false;
|
|
23
|
+
if (platform === "win32") {
|
|
24
|
+
const ext = extname(path).toLowerCase();
|
|
25
|
+
if (!WINDOWS_EXECUTABLE.has(ext))
|
|
26
|
+
return false;
|
|
27
|
+
accessSync(path, constants.F_OK);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
18
30
|
accessSync(path, constants.X_OK);
|
|
19
31
|
return true;
|
|
20
32
|
}
|
|
@@ -22,28 +34,202 @@ export function executableFile(path) {
|
|
|
22
34
|
return false;
|
|
23
35
|
}
|
|
24
36
|
}
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
/** An absolute path under the given platform's rules: a Windows drive or UNC root is absolute only to `win32`. */
|
|
38
|
+
const absolute = (path, platform) => platform === "win32" ? isAbsolute(path) || win32.isAbsolute(path) : isAbsolute(path);
|
|
39
|
+
/** PATH directories. Windows separators are `;`, but POSIX-style test setups prepend `dir:...`; split those too, without
|
|
40
|
+
* cutting a `C:` drive prefix (a colon at index 1 after a letter). */
|
|
41
|
+
function pathDirs(PATH, platform) {
|
|
42
|
+
if (platform !== "win32")
|
|
43
|
+
return (PATH ?? "").split(delimiter);
|
|
44
|
+
const out = [];
|
|
45
|
+
for (const part of (PATH ?? "").split(";")) {
|
|
46
|
+
let start = 0;
|
|
47
|
+
for (let i = 0; i < part.length; i++)
|
|
48
|
+
if (part[i] === ":" && !(i === 1 && /[A-Za-z]/.test(part[0]))) {
|
|
49
|
+
out.push(part.slice(start, i));
|
|
50
|
+
start = i + 1;
|
|
51
|
+
}
|
|
52
|
+
out.push(part.slice(start));
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
/** The first executable `name` in the directories of `PATH`, unresolved (a package-manager shim stays a shim). Windows
|
|
57
|
+
* looks through PATHEXT: `pi` resolves to `pi.cmd`/`pi.exe`, never the extensionless POSIX shim beside them. */
|
|
58
|
+
export function findOnPath(name, PATH, options = {}) {
|
|
59
|
+
const platform = options.platform ?? process.platform;
|
|
60
|
+
const exts = platform === "win32" && !extname(name)
|
|
61
|
+
? (options.pathext ?? envValue(process.env, "PATHEXT") ?? ".COM;.EXE;.BAT;.CMD").split(";").map(ext => ext.toLowerCase()).filter(ext => WINDOWS_EXECUTABLE.has(ext))
|
|
62
|
+
: [""];
|
|
63
|
+
for (const dir of pathDirs(PATH, platform)) {
|
|
64
|
+
if (!dir || !absolute(dir, platform))
|
|
29
65
|
continue;
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
66
|
+
for (const ext of exts) {
|
|
67
|
+
const file = join(dir, name + ext);
|
|
68
|
+
if (executableFile(file, platform))
|
|
69
|
+
return file;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
/** What a spawn actually runs. An npm `.cmd` shim is not a PE image and Node cannot start it directly; the shim names
|
|
75
|
+
* the node script it runs, so that script runs under this Node instead and argv stays an argv (no shell quoting).
|
|
76
|
+
* A bare name is looked up on the command's own PATH (`options.env`), not this process's: the command's PATH may have
|
|
77
|
+
* the shim the caller's has not (a call's prepended `$DSA_HOME/bin`). */
|
|
78
|
+
/** Quote one argument for `cmd.exe /d /s /c ""path" "arg""` (passed with `windowsVerbatimArguments: true`).
|
|
79
|
+
* Wrapping every token in double quotes ensures cmd.exe treats spaces and metacharacters (`&`, `|`, `<`, `>`, `(`, `)`,
|
|
80
|
+
* `^`) literally both on the outer cmd.exe command line and when a batch shim forwards `%*` to Node. Embedded CR, LF or
|
|
81
|
+
* double quotes cannot be safely escaped for cmd.exe batch parsing (CVE-2024-27980 / BatBadBut) and are refused. */
|
|
82
|
+
function quoteCmdBatchArg(value) {
|
|
83
|
+
if (/[\r\n"]/.test(value))
|
|
84
|
+
throw new Error("Windows .cmd/.bat arguments cannot contain CR, LF or double quotes");
|
|
85
|
+
return `"${value}"`;
|
|
86
|
+
}
|
|
87
|
+
export function execTarget(command, args, options = {}) {
|
|
88
|
+
const platform = options.platform ?? process.platform, env = options.env ?? process.env;
|
|
89
|
+
if (platform !== "win32")
|
|
90
|
+
return { command, args: [...args] };
|
|
91
|
+
// A bare name is looked up through PATHEXT (`pi` → `pi.cmd`), the way the command's own shell would find it.
|
|
92
|
+
const resolved = !isAbsolute(command) && !/[\\/]/.test(command)
|
|
93
|
+
? findOnPath(command, envValue(env, "PATH"), { platform, pathext: envValue(env, "PATHEXT") }) ?? command
|
|
94
|
+
: command;
|
|
95
|
+
if (!/\.(cmd|bat)$/i.test(resolved))
|
|
96
|
+
return { command: resolved, args: [...args] };
|
|
97
|
+
const script = shimScript(resolved);
|
|
98
|
+
if (script) {
|
|
99
|
+
const node = resolveNode(env, { platform, peOnly: true }).path;
|
|
100
|
+
if (node)
|
|
101
|
+
return { command: node, args: [script, ...args] };
|
|
102
|
+
}
|
|
103
|
+
// Outer quotes satisfy `cmd.exe /s /c`, which unconditionally strips the first and last `"` from the command string,
|
|
104
|
+
// leaving `"path" "arg1" ...` intact for cmd.exe and batch `%*` forwarding. Callers pass `windowsVerbatimArguments: true`.
|
|
105
|
+
const inner = [quoteCmdBatchArg(resolved), ...args.map(quoteCmdBatchArg)].join(" ");
|
|
106
|
+
return { command: comspec(env), args: ["/d", "/s", "/c", `"${inner}"`], verbatim: true };
|
|
107
|
+
}
|
|
108
|
+
/** The node script a generated `.cmd`/`.bat` shim runs, when it names one (`%dp0%` is the shim's directory).
|
|
109
|
+
* Shims that customize NODE_PATH or NODE_OPTIONS (e.g. pnpm global shims) are not unwrapped so their environment
|
|
110
|
+
* setup runs through cmd.exe instead of being silently dropped. */
|
|
111
|
+
export function shimScript(file) {
|
|
112
|
+
let text;
|
|
113
|
+
try {
|
|
114
|
+
text = readFileSync(file, "utf8");
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
if (/^\s*@?\s*set\s+"?NODE_(?:PATH|OPTIONS)=/im.test(text))
|
|
120
|
+
return undefined;
|
|
121
|
+
const dir = dirname(file);
|
|
122
|
+
for (const match of text.matchAll(/"([^"\r\n]+\.(?:js|mjs|cjs))"|'([^'\r\n]+\.(?:js|mjs|cjs))'/gi)) {
|
|
123
|
+
const raw = match[1] ?? match[2];
|
|
124
|
+
const rel = raw.replace(/^%~?dp0%?[\\/]*/i, "");
|
|
125
|
+
const resolved = rel !== raw
|
|
126
|
+
? resolve(dir, process.platform === "win32" ? rel : rel.replace(/\\/g, "/"))
|
|
127
|
+
: normalize(raw.replace(/%~?dp0%?/gi, dir));
|
|
128
|
+
if (isAbsolute(resolved) && existsSync(resolved))
|
|
129
|
+
return resolved;
|
|
33
130
|
}
|
|
34
131
|
return undefined;
|
|
35
132
|
}
|
|
133
|
+
/** The command processor a string command/gate runs through on this platform. */
|
|
134
|
+
export function shellTarget(command, exec, platform = process.platform) {
|
|
135
|
+
// A command line is readable across processes on Windows (the environment is not): the versioned ASCII marker lets
|
|
136
|
+
// recovery find a gate whose identity was never journaled, without mutating env.DSA_EXEC or breaking on `&`/quotes/Unicode.
|
|
137
|
+
return platform === "win32"
|
|
138
|
+
? { command: exec ? `(if 0==1 echo ${winTagArg("DSA_EXEC", exec)} ) & ${command}` : command, args: [], shell: true }
|
|
139
|
+
: { command: "/bin/sh", args: ["-c", command] };
|
|
140
|
+
}
|
|
141
|
+
const NODE_BASENAME = /^node(?:js)?(?:[-._]?v?\d[\d._-]*)?(?:\.exe)?$/i;
|
|
142
|
+
const NODE_SHIM_BASENAME = /^node(?:js)?(?:[-._]?v?\d[\d._-]*)?(?:\.(?:exe|com|cmd|bat))?$/i;
|
|
143
|
+
const SHIM_DISPATCHERS = new Set(["mise", "mise.exe", "volta", "volta.exe", "volta-shim", "volta-shim.exe", "asdf", "asdf.exe", "proto-shim", "proto-shim.exe", "rtx", "rtx.exe", "anyenv", "nodenv", "fnm", "fnm.exe"]);
|
|
144
|
+
const basenameOf = (filePath) => filePath.replace(/^.*[\\/]/, "");
|
|
145
|
+
const realPathOf = (filePath) => { try {
|
|
146
|
+
return (realpathSync.native ?? realpathSync)(filePath);
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return filePath;
|
|
150
|
+
} };
|
|
151
|
+
const normFile = (filePath, platform) => platform === "win32" ? win32.normalize(filePath).toLowerCase() : filePath;
|
|
152
|
+
/** Whether this process itself is a standalone Node.js runtime (not a compiled Bun or SEA pi binary where
|
|
153
|
+
* process.execPath points to pi and running `execPath main.js` would open a recursive pi session, #11). */
|
|
154
|
+
export function isNodeProcess(options = {}) {
|
|
155
|
+
const versions = options.versions ?? process.versions;
|
|
156
|
+
if (versions.bun)
|
|
157
|
+
return false;
|
|
158
|
+
const releaseName = options.releaseName ?? process.release?.name;
|
|
159
|
+
if (releaseName !== "node")
|
|
160
|
+
return false;
|
|
161
|
+
const execPath = options.execPath ?? process.execPath;
|
|
162
|
+
return NODE_BASENAME.test(basenameOf(execPath));
|
|
163
|
+
}
|
|
164
|
+
/** Whether `nodePath` and `piPath` invoke the same program. Multi-call version-manager shims (mise, volta, asdf, ...)
|
|
165
|
+
* share one realpath dispatcher binary across `shims/node` and `shims/pi` and dispatch on argv[0]; allow distinct
|
|
166
|
+
* paths only when the shared target is a known shim dispatcher, so a symlink to a versioned `pi-real` binary is
|
|
167
|
+
* still refused. */
|
|
168
|
+
export function isSameProgram(nodePath, piPath, platform = process.platform) {
|
|
169
|
+
if (!piPath)
|
|
170
|
+
return false;
|
|
171
|
+
if (normFile(nodePath, platform) === normFile(piPath, platform))
|
|
172
|
+
return true;
|
|
173
|
+
const realNode = realPathOf(nodePath), realPi = realPathOf(piPath);
|
|
174
|
+
if (normFile(realNode, platform) !== normFile(realPi, platform))
|
|
175
|
+
return false;
|
|
176
|
+
const nodeName = basenameOf(nodePath), piName = basenameOf(piPath), targetName = basenameOf(realNode).toLowerCase();
|
|
177
|
+
if (NODE_SHIM_BASENAME.test(nodeName) && !NODE_SHIM_BASENAME.test(piName) && SHIM_DISPATCHERS.has(targetName))
|
|
178
|
+
return false;
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
/** Resolve a Node.js executable for launching the orchestrator and JS helpers (#11):
|
|
182
|
+
* 1. DSA_NODE_BIN when set (must be an absolute path to an executable file, and not pi itself);
|
|
183
|
+
* 2. process.execPath when this process is a real Node.js runtime (not a compiled Bun/standalone pi binary);
|
|
184
|
+
* 3. `node` (or `nodejs` on POSIX) on PATH, preferring PE binaries (.exe/.com) over .cmd/.bat shims on Windows. */
|
|
185
|
+
export function resolveNode(env, options = {}) {
|
|
186
|
+
const platform = options.platform ?? process.platform;
|
|
187
|
+
const isPe = (p) => /\.(?:exe|com)$/i.test(p);
|
|
188
|
+
const given = env.DSA_NODE_BIN;
|
|
189
|
+
if (given) {
|
|
190
|
+
if (!absolute(given, platform) || !executableFile(given, platform))
|
|
191
|
+
return { problem: `DSA_NODE_BIN=${given} is not an absolute path to an executable file` };
|
|
192
|
+
if (platform === "win32" && options.peOnly && !isPe(given))
|
|
193
|
+
return { problem: `DSA_NODE_BIN=${given} is not a Windows .exe/.com Node executable` };
|
|
194
|
+
if (isSameProgram(given, options.piPath, platform))
|
|
195
|
+
return { problem: `DSA_NODE_BIN=${given} resolves to the pi executable (${options.piPath})` };
|
|
196
|
+
return { path: given, source: "DSA_NODE_BIN" };
|
|
197
|
+
}
|
|
198
|
+
const execPath = options.execPath ?? process.execPath;
|
|
199
|
+
if (isNodeProcess({ execPath, versions: options.versions, releaseName: options.releaseName }) && !isSameProgram(execPath, options.piPath, platform)) {
|
|
200
|
+
return { path: execPath, source: "execPath" };
|
|
201
|
+
}
|
|
202
|
+
const PATH = platform === "win32" ? envValue(env, "PATH") : env.PATH;
|
|
203
|
+
const pathext = platform === "win32" ? envValue(env, "PATHEXT") : undefined;
|
|
204
|
+
const found = platform === "win32"
|
|
205
|
+
? findOnPath("node", PATH, { platform, pathext: ".EXE;.COM" }) ?? (options.peOnly ? undefined : findOnPath("node", PATH, { platform, pathext }))
|
|
206
|
+
: findOnPath("node", PATH, { platform }) ?? findOnPath("nodejs", PATH, { platform });
|
|
207
|
+
if (found) {
|
|
208
|
+
if (isSameProgram(found, options.piPath, platform))
|
|
209
|
+
return { problem: `node on PATH (${found}) resolves to the pi executable (${options.piPath})` };
|
|
210
|
+
return { path: found, source: "PATH" };
|
|
211
|
+
}
|
|
212
|
+
return { problem: `node was not found on PATH (${PATH || "unset"}) and DSA_NODE_BIN is not set` };
|
|
213
|
+
}
|
|
214
|
+
/** The starter cannot start an orchestrator without a Node.js runtime (#11). */
|
|
215
|
+
export class NodeNotFound extends Error {
|
|
216
|
+
constructor(problem) {
|
|
217
|
+
super(`cannot start the orchestrator: ${problem}. Set DSA_NODE_BIN to the absolute path of Node.js 22.19+, or put node on PATH`);
|
|
218
|
+
this.name = "NodeNotFound";
|
|
219
|
+
}
|
|
220
|
+
}
|
|
36
221
|
/** The `pi` the orchestrator launches subagents with: DSA_PI_BIN when set (it must be an executable file), else the
|
|
37
222
|
* first `pi` on PATH. `problem` says why there is none. */
|
|
38
|
-
export function resolvePi(env) {
|
|
223
|
+
export function resolvePi(env, platform = process.platform) {
|
|
39
224
|
const given = env.DSA_PI_BIN;
|
|
40
225
|
if (given) {
|
|
41
|
-
if (
|
|
226
|
+
if (absolute(given, platform) && executableFile(given, platform))
|
|
42
227
|
return { path: given, source: "DSA_PI_BIN" };
|
|
43
228
|
return { problem: `DSA_PI_BIN=${given} is not an absolute path to an executable file` };
|
|
44
229
|
}
|
|
45
|
-
const
|
|
46
|
-
|
|
230
|
+
const PATH = platform === "win32" ? envValue(env, "PATH") : env.PATH;
|
|
231
|
+
const found = findOnPath("pi", PATH, { platform, pathext: platform === "win32" ? envValue(env, "PATHEXT") : undefined });
|
|
232
|
+
return found ? { path: found, source: "PATH" } : { problem: `pi was not found on PATH (${PATH || "unset"}) and DSA_PI_BIN is not set` };
|
|
47
233
|
}
|
|
48
234
|
/** The starter cannot start an orchestrator that could launch no subagent. */
|
|
49
235
|
export class PiNotFound extends Error {
|
|
@@ -100,10 +286,27 @@ export async function scopeRunner(env, options = {}) {
|
|
|
100
286
|
function started(child) {
|
|
101
287
|
return new Promise((resolve, reject) => { child.once("spawn", resolve); child.once("error", reject); });
|
|
102
288
|
}
|
|
289
|
+
/** What the detached orchestrator's stdio is: discarded, unless DSA_ORCHESTRATOR_LOG names a file to append its stdout
|
|
290
|
+
* and stderr to. The orchestrator has no terminal, so without the log a start that fails before it records anything
|
|
291
|
+
* (`orchestrator` entry, `orchestrator-exit` reason) leaves no trace at all; the diagnostics of a failover drill,
|
|
292
|
+
* chaos scenario or a CI e2e case need it. An unusable path never fails the start: it falls back to "discarded". */
|
|
293
|
+
function orchestratorStdio(env) {
|
|
294
|
+
const log = env.DSA_ORCHESTRATOR_LOG;
|
|
295
|
+
if (!log)
|
|
296
|
+
return { stdio: "ignore", fds: [] };
|
|
297
|
+
try {
|
|
298
|
+
mkdirSync(dirname(log), { recursive: true });
|
|
299
|
+
const fd = openSync(log, "a", 0o600);
|
|
300
|
+
return { stdio: ["ignore", fd, fd], fds: [fd] };
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
return { stdio: "ignore", fds: [] };
|
|
304
|
+
}
|
|
305
|
+
}
|
|
103
306
|
/** The variables the executor sets for one call's processes (a subagent, a command call, a gate). An orchestrator
|
|
104
307
|
* started from inside a call (its CLI, a command that runs `start`) never carries them: with the call's DSA_EXEC it
|
|
105
308
|
* would be one of that call's processes, and the call's fence (its end, a stop) would kill it. */
|
|
106
|
-
export const PER_CALL_ENV = ["DSA_SESSION", "DSA_EXEC", "DSA_CALL", "DSA_INBOX", "DSA_JOURNAL", "DSA_SCHEMA", "DSA_BUDGET", "DSA_MODEL", "DSA_RESULT", "DSA_OUTPUT", "DSA_PROBE"];
|
|
309
|
+
export const PER_CALL_ENV = ["DSA_SESSION", "DSA_EXEC", "DSA_CALL", "DSA_INBOX", "DSA_JOURNAL", "DSA_SCHEMA", "DSA_BUDGET", "DSA_MODEL", "DSA_RESULT", "DSA_OUTPUT", "DSA_PROBE", ENV.orchestratorLaunch];
|
|
107
310
|
/** `env` without PER_CALL_ENV. */
|
|
108
311
|
export function orchestratorEnv(env) {
|
|
109
312
|
const rest = { ...env };
|
|
@@ -115,23 +318,53 @@ export function orchestratorEnv(env) {
|
|
|
115
318
|
* session) and every other per-call variable (PER_CALL_ENV) plus DSA_HOME and the resolved DSA_PI_BIN. In its own systemd scope when one is available (the scope execs
|
|
116
319
|
* the command: same environment, and the pid is the orchestrator's), else a plain detached process. */
|
|
117
320
|
export async function launchOrchestrator(entry, env, options) {
|
|
118
|
-
const
|
|
321
|
+
const platform = options.platform ?? process.platform;
|
|
322
|
+
const pi = resolvePi(env, platform);
|
|
119
323
|
if (!pi.path && options.requirePi)
|
|
120
324
|
throw new PiNotFound(pi.problem);
|
|
121
|
-
const
|
|
122
|
-
|
|
325
|
+
const piPath = pi.path ?? (isNodeProcess() ? undefined : process.execPath);
|
|
326
|
+
let node;
|
|
327
|
+
if (options.node !== undefined) {
|
|
328
|
+
if (isSameProgram(options.node, piPath, platform))
|
|
329
|
+
throw new NodeNotFound(`orchestrator runtime ${options.node} resolves to the pi executable (${piPath})`);
|
|
330
|
+
node = options.node;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
const resolved = resolveNode(env, { platform, piPath });
|
|
334
|
+
if (!resolved.path)
|
|
335
|
+
throw new NodeNotFound(resolved.problem);
|
|
336
|
+
node = resolved.path;
|
|
337
|
+
}
|
|
338
|
+
const childEnv = { ...orchestratorEnv(env), DSA_HOME: options.home, [ENV.orchestratorLaunch]: "1", ...(pi.path ? { DSA_PI_BIN: pi.path } : {}) };
|
|
339
|
+
const run = options.spawn ?? spawn;
|
|
123
340
|
const runner = await scopeRunner(env, options);
|
|
124
|
-
|
|
341
|
+
const diagnostic = orchestratorStdio(childEnv);
|
|
342
|
+
// The child holds its own copies of the descriptors; ours are closed once it has started (a failed start closes them).
|
|
343
|
+
const close = () => { for (const fd of diagnostic.fds) {
|
|
125
344
|
try {
|
|
126
|
-
|
|
127
|
-
await started(child);
|
|
128
|
-
child.unref();
|
|
129
|
-
return { pid: child.pid, scope: true, ...(pi.path ? { pi: pi.path } : {}) };
|
|
345
|
+
closeSync(fd);
|
|
130
346
|
}
|
|
131
|
-
catch { /*
|
|
347
|
+
catch { /* already closed */ }
|
|
348
|
+
} };
|
|
349
|
+
try {
|
|
350
|
+
if (runner) {
|
|
351
|
+
try {
|
|
352
|
+
const child = run(runner, [...SCOPE_ARGS, "--description", "pi-durable-subagents orchestrator", "--", node, entry], { detached: true, stdio: diagnostic.stdio, env: childEnv });
|
|
353
|
+
await started(child);
|
|
354
|
+
child.unref();
|
|
355
|
+
return { pid: child.pid, scope: true, ...(pi.path ? { pi: pi.path } : {}) };
|
|
356
|
+
}
|
|
357
|
+
catch { /* fall back to a plain detached process */ }
|
|
358
|
+
}
|
|
359
|
+
const target = platform === "win32" && /\.(?:cmd|bat)$/i.test(node)
|
|
360
|
+
? execTarget(node, [entry], { env: childEnv, platform })
|
|
361
|
+
: { command: node, args: [entry] };
|
|
362
|
+
const child = run(target.command, target.args, { detached: true, stdio: diagnostic.stdio, env: childEnv, ...(target.verbatim ? { windowsVerbatimArguments: true } : {}) });
|
|
363
|
+
await started(child);
|
|
364
|
+
child.unref();
|
|
365
|
+
return { pid: child.pid, scope: false, ...(pi.path ? { pi: pi.path } : {}) };
|
|
366
|
+
}
|
|
367
|
+
finally {
|
|
368
|
+
close();
|
|
132
369
|
}
|
|
133
|
-
const child = run(node, [entry], { detached: true, stdio: "ignore", env: childEnv });
|
|
134
|
-
await started(child);
|
|
135
|
-
child.unref();
|
|
136
|
-
return { pid: child.pid, scope: false, ...(pi.path ? { pi: pi.path } : {}) };
|
|
137
370
|
}
|