@botbuddy/cli 1.89.0 → 1.90.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/daemon/bin/darwin-arm64/botbuddy-daemon +0 -0
- package/daemon/bin/linux-x64/botbuddy-daemon +0 -0
- package/daemon-release/darwin-arm64.json +4 -4
- package/daemon-release/linux-x64.json +4 -4
- package/daemon-release/manifest.json +5 -5
- package/package.json +1 -1
- package/src/commands.mjs +2 -2
- package/src/host-child.mjs +218 -0
- package/src/mcp-key.mjs +4 -1
- package/src/stack.mjs +113 -5
- package/src/test-child.mjs +18 -7
- package/src/wait-core.mjs +9 -0
|
Binary file
|
|
Binary file
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"release_version": "1.
|
|
3
|
-
"source_commit": "
|
|
4
|
-
"source_sha256": "
|
|
2
|
+
"release_version": "1.90.1",
|
|
3
|
+
"source_commit": "7fbc2e7eabaa462d3ca8713b30642bb675df6342",
|
|
4
|
+
"source_sha256": "eb13d455da4e123456d80d9ec009441e0f3f8fd09cc572f41afab929332358d5",
|
|
5
5
|
"target": "darwin-arm64",
|
|
6
|
-
"sha256": "
|
|
6
|
+
"sha256": "a4677dd03ba8f7b3d8c32d3ccfb1f3dc682b3a26ea93d9942cdb5049645174c3"
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"release_version": "1.
|
|
3
|
-
"source_commit": "
|
|
4
|
-
"source_sha256": "
|
|
2
|
+
"release_version": "1.90.1",
|
|
3
|
+
"source_commit": "7fbc2e7eabaa462d3ca8713b30642bb675df6342",
|
|
4
|
+
"source_sha256": "eb13d455da4e123456d80d9ec009441e0f3f8fd09cc572f41afab929332358d5",
|
|
5
5
|
"target": "linux-x64",
|
|
6
|
-
"sha256": "
|
|
6
|
+
"sha256": "b3ef42831050890ed19a0db719104cb8bb7434b776dabcca346c8ae21f2ca8f3"
|
|
7
7
|
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schema_version": 1,
|
|
3
|
-
"release_version": "1.
|
|
4
|
-
"source_commit": "
|
|
5
|
-
"source_sha256": "
|
|
3
|
+
"release_version": "1.90.1",
|
|
4
|
+
"source_commit": "7fbc2e7eabaa462d3ca8713b30642bb675df6342",
|
|
5
|
+
"source_sha256": "eb13d455da4e123456d80d9ec009441e0f3f8fd09cc572f41afab929332358d5",
|
|
6
6
|
"targets": {
|
|
7
7
|
"darwin-arm64": {
|
|
8
8
|
"goos": "darwin",
|
|
9
9
|
"goarch": "arm64",
|
|
10
10
|
"binary": "daemon/bin/darwin-arm64/botbuddy-daemon",
|
|
11
|
-
"sha256": "
|
|
11
|
+
"sha256": "a4677dd03ba8f7b3d8c32d3ccfb1f3dc682b3a26ea93d9942cdb5049645174c3",
|
|
12
12
|
"mode": "0755"
|
|
13
13
|
},
|
|
14
14
|
"linux-x64": {
|
|
15
15
|
"goos": "linux",
|
|
16
16
|
"goarch": "amd64",
|
|
17
17
|
"binary": "daemon/bin/linux-x64/botbuddy-daemon",
|
|
18
|
-
"sha256": "
|
|
18
|
+
"sha256": "b3ef42831050890ed19a0db719104cb8bb7434b776dabcca346c8ae21f2ca8f3",
|
|
19
19
|
"mode": "0755"
|
|
20
20
|
}
|
|
21
21
|
}
|
package/package.json
CHANGED
package/src/commands.mjs
CHANGED
|
@@ -1566,8 +1566,8 @@ export function mcpHelp(log = console.log) {
|
|
|
1566
1566
|
A bb_mcp_ key is tenant-sealed: on a machine serving more than
|
|
1567
1567
|
one tenant, mint each tenant under a distinct --env <NAME> var
|
|
1568
1568
|
and commit that repo's matching .botbuddy-agent.json mcp_env.
|
|
1569
|
-
Refuses an occupied Keychain slot unless --force
|
|
1570
|
-
|
|
1569
|
+
Refuses an occupied Keychain slot unless --force, which
|
|
1570
|
+
replaces the key in that slot and revokes the key it replaces.
|
|
1571
1571
|
revoke <agent_id> [--tenant <slug>] [--env <NAME>]
|
|
1572
1572
|
Revoke ONLY that MCP key (returned by setup). Your login and
|
|
1573
1573
|
live agent sessions keep working. Multi-tenant owners must
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// BOT-3030 — `bb stack run --test-child` when the control plane places the child on the
|
|
2
|
+
// lease's serving host (child-on-host).
|
|
3
|
+
//
|
|
4
|
+
// The requester keeps the lease, the test_child slot (renewed by the run as before) and the
|
|
5
|
+
// reap. It ships its head as a `git archive` bundle, queues one host run, and then waits on
|
|
6
|
+
// the platform signal for that run (`event:type=test_child_run,subject=<run id>`), never a
|
|
7
|
+
// poll. The host's daemon runs the child as its own systemd unit with a per-run DynamicUser,
|
|
8
|
+
// against the host-local connection, and measures it with the unit's cgroup. The child gets
|
|
9
|
+
// no BotBuddy credential: only names passed with --child-env are forwarded, and the server
|
|
10
|
+
// refuses credential-shaped ones.
|
|
11
|
+
//
|
|
12
|
+
// startHostChild returns a child-shaped handle (an EventEmitter that emits `exit`) so the run
|
|
13
|
+
// loop keeps its heartbeat, slot renewal, signals and receipt unchanged.
|
|
14
|
+
|
|
15
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
16
|
+
import { createHash } from "node:crypto";
|
|
17
|
+
import { EventEmitter } from "node:events";
|
|
18
|
+
|
|
19
|
+
export const HOST_CHILD_BUNDLE_MAX_BYTES = 200 * 1024 * 1024;
|
|
20
|
+
/** A host run that never reports terminal is given up this long after its own runtime limit. */
|
|
21
|
+
export const HOST_CHILD_WAIT_GRACE_MS = 10 * 60_000;
|
|
22
|
+
export const HOST_CHILD_DEFAULT_MAX_RUNTIME_S = 3600;
|
|
23
|
+
const ENV_NAME = /^[A-Z][A-Z0-9_]{0,63}$/;
|
|
24
|
+
|
|
25
|
+
/** The four requester tool calls, over the run's retrying caller. */
|
|
26
|
+
export function hostChildApi(rpc) {
|
|
27
|
+
return {
|
|
28
|
+
create: (args) => rpc.call("create_host_test_child_run", args),
|
|
29
|
+
queue: (runId) => rpc.call("queue_host_test_child_run", { run_id: runId }),
|
|
30
|
+
get: (runId) => rpc.call("get_host_test_child_run", { run_id: runId }),
|
|
31
|
+
cancel: (runId) => rpc.call("cancel_host_test_child_run", { run_id: runId }),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* `git archive --format=tar.gz HEAD` of the worktree: the requester's committed head. An
|
|
37
|
+
* uncommitted change is not shipped, and the caller is told so.
|
|
38
|
+
*/
|
|
39
|
+
export function buildHeadBundle(worktreeRoot, { run = spawnSync, maxBytes = HOST_CHILD_BUNDLE_MAX_BYTES } = {}) {
|
|
40
|
+
// nosemgrep: javascript.lang.security.detect-child-process.detect-child-process -- fixed argv, no shell.
|
|
41
|
+
const head = run("git", ["-C", worktreeRoot, "rev-parse", "HEAD"], { encoding: "utf8" });
|
|
42
|
+
if (head.status !== 0) return { ok: false, error: "the worktree has no committed HEAD to ship" };
|
|
43
|
+
// nosemgrep: javascript.lang.security.detect-child-process.detect-child-process -- fixed argv, no shell.
|
|
44
|
+
const dirty = run("git", ["-C", worktreeRoot, "status", "--porcelain", "--untracked-files=no"], { encoding: "utf8" });
|
|
45
|
+
// nosemgrep: javascript.lang.security.detect-child-process.detect-child-process -- fixed argv, no shell.
|
|
46
|
+
const archive = run("git", ["-C", worktreeRoot, "archive", "--format=tar.gz", "HEAD"], { maxBuffer: maxBytes + 1 });
|
|
47
|
+
if (archive.status !== 0 || !archive.stdout) return { ok: false, error: `git archive failed: ${String(archive.stderr ?? "").trim().slice(0, 200) || "no output"}` };
|
|
48
|
+
const bytes = Buffer.isBuffer(archive.stdout) ? archive.stdout : Buffer.from(archive.stdout);
|
|
49
|
+
if (bytes.length > maxBytes) return { ok: false, error: `the head bundle is ${bytes.length} bytes, above the ${maxBytes}-byte limit` };
|
|
50
|
+
return {
|
|
51
|
+
ok: true, bytes, sha256: createHash("sha256").update(bytes).digest("hex"), sizeBytes: bytes.length,
|
|
52
|
+
head: String(head.stdout).trim(), dirty: dirty.status === 0 && String(dirty.stdout).trim() !== "",
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** `--child-env NAME` takes NAME's value from this shell; the server refuses credential names. */
|
|
57
|
+
export function childEnvFrom(names, env = process.env) {
|
|
58
|
+
const out = {};
|
|
59
|
+
for (const name of names ?? []) {
|
|
60
|
+
if (!ENV_NAME.test(name)) throw new Error(`--child-env ${name}: names are upper-case identifiers`);
|
|
61
|
+
if (env[name] !== undefined) out[name] = String(env[name]);
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** How long a stop that the control plane has not yet accepted waits before asking again. */
|
|
67
|
+
export const HOST_CHILD_CANCEL_RETRY_MS = 30_000;
|
|
68
|
+
|
|
69
|
+
export function hostChildWaitArgv({ runId, deadline, since = null }) {
|
|
70
|
+
const argv = ["wait", `event:type=test_child_run,subject=${runId}`, `timer:deadline=${deadline}`];
|
|
71
|
+
if (Number.isSafeInteger(since) && since >= 0) argv.push("--since", String(since));
|
|
72
|
+
return argv;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function defaultHostChildWait(target, { bin = process.execPath, cli = process.argv[1], run = spawn } = {}) {
|
|
76
|
+
return new Promise((resolve) => {
|
|
77
|
+
// nosemgrep: javascript.lang.security.detect-child-process.detect-child-process -- fixed argv, no shell.
|
|
78
|
+
const child = run(bin, [cli, ...hostChildWaitArgv(target)], { stdio: ["ignore", "ignore", "inherit"] });
|
|
79
|
+
const onAbort = () => { try { child.kill("SIGTERM"); } catch { /* gone */ } };
|
|
80
|
+
if (target.signal?.aborted) onAbort();
|
|
81
|
+
else target.signal?.addEventListener?.("abort", onAbort, { once: true });
|
|
82
|
+
child.on("exit", (code) => { target.signal?.removeEventListener?.("abort", onAbort); resolve(code === 0); });
|
|
83
|
+
child.on("error", () => resolve(false));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Terminal host run → the run loop's exit code. */
|
|
88
|
+
export function hostRunExitCode(run) {
|
|
89
|
+
if (run?.state === "exited" && Number.isInteger(run.exit_code)) return run.exit_code;
|
|
90
|
+
if (run?.state === "canceled") return 130;
|
|
91
|
+
return Number.isInteger(run?.exit_code) && run.exit_code !== 0 ? run.exit_code : 1;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Create, upload and queue the host run, then wait for it in the background.
|
|
96
|
+
* Returns `{ ok: true, child, runId }` or `{ ok: false, code, error }` (nothing started).
|
|
97
|
+
*/
|
|
98
|
+
export async function startHostChild({
|
|
99
|
+
api, upload, slotId, argv, env = {}, worktreeRoot, maxRuntimeSeconds = HOST_CHILD_DEFAULT_MAX_RUNTIME_S,
|
|
100
|
+
bundle: prebuilt = null, wait = defaultHostChildWait, log = () => {}, writeOutput = (s) => process.stdout.write(s), now = Date.now,
|
|
101
|
+
signal = undefined,
|
|
102
|
+
}) {
|
|
103
|
+
const bundle = prebuilt ?? buildHeadBundle(worktreeRoot);
|
|
104
|
+
if (!bundle.ok) return { ok: false, code: "HOST_CHILD_BUNDLE_FAILED", error: bundle.error };
|
|
105
|
+
if (bundle.dirty) log(`stack: host child ships HEAD ${bundle.head?.slice(0, 12) ?? ""}; uncommitted changes in the worktree are not shipped`);
|
|
106
|
+
let created;
|
|
107
|
+
try {
|
|
108
|
+
created = await api.create({ slot_id: slotId, argv, cwd: "", env, bundle_sha256: bundle.sha256, bundle_size_bytes: bundle.sizeBytes, max_runtime_seconds: maxRuntimeSeconds });
|
|
109
|
+
} catch (error) { created = { ok: false, error: String(error?.message ?? error) }; }
|
|
110
|
+
const c = created?.data;
|
|
111
|
+
if (!created?.ok || !c?.success) {
|
|
112
|
+
return { ok: false, code: c?.code ?? "HOST_CHILD_UNAVAILABLE", error: `the host run was not created (${c?.message ?? c?.code ?? created?.error ?? "no answer"})` };
|
|
113
|
+
}
|
|
114
|
+
const runId = c.run_id;
|
|
115
|
+
const abandon = async (code, error) => { try { await api.cancel(runId); } catch { /* the slot TTL ends it */ } return { ok: false, code, runId, error }; };
|
|
116
|
+
// Codex #1782 r5: an interrupt aborts a stalled upload, and an interrupted run is never queued.
|
|
117
|
+
const uploaded = await upload(c.bundle_upload_url, bundle, signal ? { signal } : undefined);
|
|
118
|
+
if (signal?.aborted) return abandon("HOST_CHILD_INTERRUPTED", "the run was interrupted while its head bundle uploaded; the host run was canceled");
|
|
119
|
+
if (!uploaded?.ok) return abandon("HOST_CHILD_BUNDLE_UPLOAD_FAILED", uploaded?.error ?? "the head bundle upload failed");
|
|
120
|
+
let queued;
|
|
121
|
+
try { queued = await api.queue(runId); } catch (error) { queued = { ok: false, error: String(error?.message ?? error) }; }
|
|
122
|
+
if (!queued?.ok || !queued.data?.success) return abandon(queued?.data?.code ?? "HOST_CHILD_QUEUE_FAILED", `the host run was not queued (${queued?.data?.message ?? queued?.data?.code ?? queued?.error ?? "no answer"})`);
|
|
123
|
+
log(`stack: test child queued on ${c.host_key} as host run ${runId} (bundle ${bundle.sizeBytes} bytes, sha256 ${bundle.sha256.slice(0, 12)})`);
|
|
124
|
+
|
|
125
|
+
const child = new EventEmitter();
|
|
126
|
+
child.hostRun = true;
|
|
127
|
+
child.runId = runId;
|
|
128
|
+
child.hostKey = c.host_key;
|
|
129
|
+
child.final = null;
|
|
130
|
+
const abort = new AbortController();
|
|
131
|
+
// Codex #1782 r4: a stop is retried until the control plane accepts the cancel; an
|
|
132
|
+
// unanswered one wakes the wait so the next pass asks again (bounded by the run's limit).
|
|
133
|
+
let stopWanted = false;
|
|
134
|
+
let cancelState = "none"; // none → pending → accepted
|
|
135
|
+
let waitAbort = null;
|
|
136
|
+
// Codex #1782 r10: only a long park (armed before the stop) is cut short by a failed cancel;
|
|
137
|
+
// a short retry park is left to run out, so an outage backs off instead of looping.
|
|
138
|
+
let waitIsLong = false;
|
|
139
|
+
const requestCancel = () => {
|
|
140
|
+
if (cancelState !== "none" || child.final) return;
|
|
141
|
+
cancelState = "pending";
|
|
142
|
+
Promise.resolve().then(() => api.cancel(runId)).then((r) => {
|
|
143
|
+
cancelState = r?.ok && r.data?.success ? "accepted" : "none";
|
|
144
|
+
}, () => { cancelState = "none"; }).then(() => {
|
|
145
|
+
if (cancelState === "none" && waitIsLong) waitAbort?.abort("cancel_retry");
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
child.stop = () => {
|
|
149
|
+
if (child.final) return;
|
|
150
|
+
if (!stopWanted) log(`stack: stopping host run ${runId}`);
|
|
151
|
+
stopWanted = true;
|
|
152
|
+
requestCancel();
|
|
153
|
+
};
|
|
154
|
+
// The caller gave the run up before using it (its slot was lost): stop waiting for it.
|
|
155
|
+
child.detach = () => abort.abort("detached");
|
|
156
|
+
const giveUpAt = now() + (maxRuntimeSeconds * 1000) + HOST_CHILD_WAIT_GRACE_MS;
|
|
157
|
+
const finish = (run, code) => {
|
|
158
|
+
if (child.final) return;
|
|
159
|
+
child.final = run;
|
|
160
|
+
if (typeof run?.output_tail === "string" && run.output_tail) writeOutput(run.output_tail.endsWith("\n") ? run.output_tail : `${run.output_tail}\n`);
|
|
161
|
+
if (run?.state !== "exited") log(`stack: host run ${runId} ${run?.state ?? "lost"}${run?.failure_code ? ` (${run.failure_code})` : ""}${run?.failure_message ? `: ${run.failure_message}` : ""}`);
|
|
162
|
+
abort.abort("done");
|
|
163
|
+
child.finalCode = code;
|
|
164
|
+
// Emitted on a later turn: the run loop attaches its listener after startHostChild returns.
|
|
165
|
+
setImmediate(() => child.emit("exit", code, null));
|
|
166
|
+
};
|
|
167
|
+
(async () => {
|
|
168
|
+
let since = Number.isSafeInteger(Number(queued.data.signal_cursor)) ? Number(queued.data.signal_cursor) : null;
|
|
169
|
+
let announcedStart = false;
|
|
170
|
+
for (;;) {
|
|
171
|
+
if (abort.signal.aborted) return;
|
|
172
|
+
let read;
|
|
173
|
+
try { read = await api.get(runId); } catch { read = null; }
|
|
174
|
+
const run = read?.ok && read.data?.success ? read.data : null;
|
|
175
|
+
if (run?.terminal) return finish(run, hostRunExitCode(run));
|
|
176
|
+
if (run?.state === "running" && !announcedStart) { announcedStart = true; log(`stack: host run ${runId} started on ${run.host_key} as ${run.unit_name}`); }
|
|
177
|
+
// The cursor is read with the state, so a change after this read replays into the wait.
|
|
178
|
+
if (run && Number.isSafeInteger(Number(run.signal_cursor))) since = Number(run.signal_cursor);
|
|
179
|
+
if (stopWanted) requestCancel();
|
|
180
|
+
if (now() >= giveUpAt) {
|
|
181
|
+
child.stop();
|
|
182
|
+
return finish({ ...(run ?? {}), state: "failed", failure_code: "HOST_CHILD_UNREPORTED", failure_message: "the host never reported the run's end" }, 1);
|
|
183
|
+
}
|
|
184
|
+
// While a stop is not yet accepted, wake soon to ask again.
|
|
185
|
+
const span = stopWanted && cancelState !== "accepted" ? HOST_CHILD_CANCEL_RETRY_MS : 30 * 60_000;
|
|
186
|
+
waitIsLong = span > HOST_CHILD_CANCEL_RETRY_MS;
|
|
187
|
+
const deadline = new Date(Math.min(giveUpAt, now() + span)).toISOString();
|
|
188
|
+
waitAbort = new AbortController();
|
|
189
|
+
const onAbort = () => waitAbort.abort("done");
|
|
190
|
+
abort.signal.addEventListener("abort", onAbort, { once: true });
|
|
191
|
+
const parked = await wait({ runId, deadline, since, signal: waitAbort.signal }).catch(() => false);
|
|
192
|
+
abort.signal.removeEventListener("abort", onAbort);
|
|
193
|
+
const retry = waitAbort.signal.reason === "cancel_retry";
|
|
194
|
+
waitAbort = null;
|
|
195
|
+
if (abort.signal.aborted) return;
|
|
196
|
+
if (retry) continue;
|
|
197
|
+
if (!parked) {
|
|
198
|
+
// The wait could not park (refused or unreachable): end, rather than turn into a poll.
|
|
199
|
+
child.stop();
|
|
200
|
+
return finish({ ...(run ?? {}), state: "failed", failure_code: "HOST_CHILD_WAIT_FAILED", failure_message: "the wait for the host run failed" }, 1);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
})();
|
|
204
|
+
return { ok: true, child, runId };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Receipt fields for a host run. */
|
|
208
|
+
export function hostChildReceiptFields(child) {
|
|
209
|
+
if (!child?.hostRun) return {};
|
|
210
|
+
const run = child.final ?? {};
|
|
211
|
+
return {
|
|
212
|
+
host_child: {
|
|
213
|
+
run_id: child.runId, host: child.hostKey, state: run.state ?? null, unit: run.unit_name ?? null,
|
|
214
|
+
exit_code: run.exit_code ?? null, failure_code: run.failure_code ?? null,
|
|
215
|
+
started_at: run.started_at ?? null, finished_at: run.finished_at ?? null,
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
package/src/mcp-key.mjs
CHANGED
|
@@ -465,9 +465,12 @@ export async function revokeMcpKey({
|
|
|
465
465
|
// and macOS offers no per-service cross-process lock. So we always hand back the
|
|
466
466
|
// exact, side-effect-free deletion command instead of deleting anything.
|
|
467
467
|
const slot = envVar || "<NAME>";
|
|
468
|
+
// BOT-3337 AC-2: a key revoked earlier (e.g. by `mcp setup --force`) is still
|
|
469
|
+
// dead; say so instead of reporting a second revoke.
|
|
470
|
+
const already = response.data?.already_revoked === true;
|
|
468
471
|
return {
|
|
469
472
|
schema_version: 1,
|
|
470
|
-
outcome: "revoked",
|
|
473
|
+
outcome: already ? "already_revoked" : "revoked",
|
|
471
474
|
agent_id: id,
|
|
472
475
|
...(envVar ? { env_var: envVar } : {}),
|
|
473
476
|
next_step:
|
package/src/stack.mjs
CHANGED
|
@@ -62,6 +62,7 @@ import { bold, dim, yellow } from "./utils.mjs";
|
|
|
62
62
|
import { STACK_RELEASE_INTENT_ENV, classifyStackRunOutcome, observeStackRunSignal, ownerReleaseHeld, reportStackRunOutcome } from "./stack-run-outcome.mjs";
|
|
63
63
|
import { REQUESTER_NETWORK_CANDIDATE_CODES, REQUESTER_NETWORK_LOST_CODE, createRequesterNetworkWatch, requesterNetworkLostMessage } from "./requester-network.mjs";
|
|
64
64
|
import { provisionFailureFields, provisionFailureLine } from "./stack-provision-failure.mjs";
|
|
65
|
+
import { hostChildApi, hostChildReceiptFields, startHostChild, childEnvFrom, HOST_CHILD_DEFAULT_MAX_RUNTIME_S } from "./host-child.mjs";
|
|
65
66
|
import { acquireTestChildSlot, defaultTestChildWait, readPs, requesterSample, startChildSampler, testChildApi, testChildReceiptFields } from "./test-child.mjs";
|
|
66
67
|
|
|
67
68
|
export const STACK_SCHEMA_VERSION = 1;
|
|
@@ -294,6 +295,17 @@ ${bold("run OPTIONS")}
|
|
|
294
295
|
renewed while the child runs and released at exit; the receipt adds
|
|
295
296
|
child_placement {host, slot, placement_reason}, child_resources {peak RSS,
|
|
296
297
|
CPU-seconds} and requester_sample.
|
|
298
|
+
BOT-3030: when placement answers the serving host (default-off flag), the
|
|
299
|
+
committed HEAD ships as a git-archive bundle and the child runs THERE, as
|
|
300
|
+
its own systemd unit with a per-run user, on the host's own ports; its
|
|
301
|
+
output is printed when it ends, and child_resources is its cgroup peak
|
|
302
|
+
memory and CPU. The requester keeps the lease, the slot and the reap.
|
|
303
|
+
--child-requires LIST BOT-3030: what the child needs on a host (comma list: node>=24,curl,psql,
|
|
304
|
+
playwright-chromium-libs). A host lacking one is not placed on; the
|
|
305
|
+
child runs here and the run names what was missing.
|
|
306
|
+
--child-env NAME BOT-3030: pass NAME (this shell's value) to a host child (repeatable).
|
|
307
|
+
Nothing else is passed; credential-shaped names are refused.
|
|
308
|
+
--child-max-runtime S BOT-3030: the host stops the child after S seconds (default 3600).
|
|
297
309
|
--local-only Require a co-located host (execution "local"); no bundle is built.
|
|
298
310
|
Default (neither flag): execution "auto" — co-located if it admits now,
|
|
299
311
|
else a remote host with capacity; the bundle digest is sent up front.
|
|
@@ -532,7 +544,7 @@ export function parseStackArgs(argv) {
|
|
|
532
544
|
connectionFile: null, noWait: false, claimOnly: false, localExec: false, keepVolumes: false, waitCapacity: null, holdOnTimeout: null,
|
|
533
545
|
dockerContext: null, dockerEndpoint: null,
|
|
534
546
|
// BOT-2142: execution placement (null = command default), remote-lease callback + bundle cap.
|
|
535
|
-
execution: null, remote: false, localOnly: false, testChild: false, executionRing: "stable", callbackPort: null, bundleMaxBytes: null,
|
|
547
|
+
execution: null, remote: false, localOnly: false, testChild: false, childRequires: [], childEnv: [], childMaxRuntime: null, executionRing: "stable", callbackPort: null, bundleMaxBytes: null,
|
|
536
548
|
// BOT-2269: exact stack-relative files shipped in the remote-lease bundle despite the
|
|
537
549
|
// default exclusions (`--bundle-include`, repeatable).
|
|
538
550
|
bundleInclude: [],
|
|
@@ -633,6 +645,31 @@ export function parseStackArgs(argv) {
|
|
|
633
645
|
case "--remote": opts.remote = true; break;
|
|
634
646
|
case "--local-only": opts.localOnly = true; break;
|
|
635
647
|
case "--test-child": opts.testChild = true; break;
|
|
648
|
+
// BOT-3030: what a host-placed child needs, and the only variables it is given.
|
|
649
|
+
case "--child-requires": {
|
|
650
|
+
if (need(a, rest[i + 1])) {
|
|
651
|
+
const list = rest[++i].split(",").map((x) => x.trim()).filter(Boolean);
|
|
652
|
+
if (!list.length || list.some((x) => !/^(node>=[0-9]{1,3}|[a-z][a-z0-9-]{0,47})$/.test(x))) errors.push("--child-requires takes a comma list of node>=<major> or capability names (curl, psql, playwright-chromium-libs)");
|
|
653
|
+
else opts.childRequires.push(...list.filter((x) => !opts.childRequires.includes(x)));
|
|
654
|
+
}
|
|
655
|
+
break;
|
|
656
|
+
}
|
|
657
|
+
case "--child-env": {
|
|
658
|
+
if (need(a, rest[i + 1])) {
|
|
659
|
+
const name = rest[++i];
|
|
660
|
+
if (!/^[A-Z][A-Z0-9_]{0,63}$/.test(name)) errors.push("--child-env takes an upper-case variable name");
|
|
661
|
+
else opts.childEnv.push(name);
|
|
662
|
+
}
|
|
663
|
+
break;
|
|
664
|
+
}
|
|
665
|
+
case "--child-max-runtime": {
|
|
666
|
+
if (need(a, rest[i + 1])) {
|
|
667
|
+
const n = Number(rest[++i]);
|
|
668
|
+
if (!Number.isInteger(n) || n < 60 || n > 21600) errors.push("--child-max-runtime takes 60 to 21600 seconds");
|
|
669
|
+
else opts.childMaxRuntime = n;
|
|
670
|
+
}
|
|
671
|
+
break;
|
|
672
|
+
}
|
|
636
673
|
case "--execution-ring": {
|
|
637
674
|
if (need(a, rest[i + 1])) {
|
|
638
675
|
const ring = rest[++i];
|
|
@@ -711,6 +748,9 @@ export function parseStackArgs(argv) {
|
|
|
711
748
|
if ((opts.callbackPort != null || opts.bundleMaxBytes != null) && command !== "run") {
|
|
712
749
|
errors.push("--callback-port and --bundle-max-mb are valid only with `stack run`");
|
|
713
750
|
}
|
|
751
|
+
if ((opts.childRequires.length || opts.childEnv.length || opts.childMaxRuntime != null) && !opts.testChild) {
|
|
752
|
+
errors.push("--child-requires, --child-env and --child-max-runtime need --test-child");
|
|
753
|
+
}
|
|
714
754
|
if (opts.testChild && command !== "run") {
|
|
715
755
|
errors.push("--test-child is valid only with `stack run` (only run starts a child)");
|
|
716
756
|
}
|
|
@@ -3885,6 +3925,8 @@ function childGroupSignal(child, signal) {
|
|
|
3885
3925
|
}
|
|
3886
3926
|
|
|
3887
3927
|
function waitForChild(child) {
|
|
3928
|
+
// BOT-3030: a host child that already ended (its end arrived before this wait) resolves now.
|
|
3929
|
+
if (child?.hostRun && child.finalCode != null) return Promise.resolve({ code: child.finalCode, signal: null });
|
|
3888
3930
|
return new Promise((resolveChild, rejectChild) => {
|
|
3889
3931
|
child.once("error", rejectChild);
|
|
3890
3932
|
child.once("exit", (code, signal) => resolveChild({ code: code ?? (signal ? SIGNAL_EXIT[signal] ?? 1 : 1), signal }));
|
|
@@ -4428,6 +4470,13 @@ async function runStackLifecycleOnce(opts, childArgv, adapters, incidentBox) {
|
|
|
4428
4470
|
// BOT-3240 (Codex #1691 r4): a loss before the child exists ends a pending slot wait now.
|
|
4429
4471
|
testChildWaitAbort?.abort("stopped");
|
|
4430
4472
|
if (!child || childResult) return;
|
|
4473
|
+
if (child.hostRun) {
|
|
4474
|
+
// BOT-3030: a host child is stopped by its host's daemon; its end arrives as the run's exit.
|
|
4475
|
+
if (forcedCode != null && forcedStopCode == null) incidentBox.lossAtMs ??= Date.now();
|
|
4476
|
+
forcedStopCode ??= forcedCode;
|
|
4477
|
+
child.stop();
|
|
4478
|
+
return;
|
|
4479
|
+
}
|
|
4431
4480
|
// BOT-3109: when a loss stopped the child, so the requester probe weighs the network then, not after cleanup.
|
|
4432
4481
|
if (forcedCode != null && forcedStopCode == null) incidentBox.lossAtMs ??= Date.now();
|
|
4433
4482
|
forcedStopCode ??= forcedCode;
|
|
@@ -5413,7 +5462,7 @@ async function runStackLifecycleOnce(opts, childArgv, adapters, incidentBox) {
|
|
|
5413
5462
|
try {
|
|
5414
5463
|
placed = await acquireTestChildSlot({
|
|
5415
5464
|
signal: testChildWaitAbort.signal,
|
|
5416
|
-
api: testChildCalls, leaseId, wait: adapters.testChildWait ?? defaultTestChildWait, log: stderrLine,
|
|
5465
|
+
api: testChildCalls, leaseId, wait: adapters.testChildWait ?? defaultTestChildWait, log: stderrLine, prerequisites: opts.childRequires,
|
|
5417
5466
|
isStopped: () => Boolean(receivedSignal || tunnelLost || callbackLost || originLost),
|
|
5418
5467
|
onPark: touchWhileWaiting,
|
|
5419
5468
|
});
|
|
@@ -5455,9 +5504,63 @@ async function runStackLifecycleOnce(opts, childArgv, adapters, incidentBox) {
|
|
|
5455
5504
|
return { exitCode, outcome: receivedSignal ? "interrupted" : "refused", code: placed.code, leaseId, cleanup, error: placed.error };
|
|
5456
5505
|
}
|
|
5457
5506
|
}
|
|
5458
|
-
|
|
5507
|
+
if (testChild?.placement?.host_kind === "serving_host") {
|
|
5508
|
+
// BOT-3030: the child runs on the serving host; this run keeps the lease and the slot.
|
|
5509
|
+
let hostEnv;
|
|
5510
|
+
try { hostEnv = childEnvFrom(opts.childEnv); } catch (error) { hostEnv = { error: error.message }; }
|
|
5511
|
+
// Codex #1782: building and uploading the head bundle can outlast the slot TTL, so the
|
|
5512
|
+
// slot is renewed through preparation too; a slot lost meanwhile stops the host run.
|
|
5513
|
+
let prepLost = null;
|
|
5514
|
+
const prepRenew = hostEnv.error ? null : clock.setInterval(async () => {
|
|
5515
|
+
if (!testChild.slotId || prepLost) return;
|
|
5516
|
+
let r;
|
|
5517
|
+
try { r = await testChildCalls.renew(testChild.slotId); } catch { r = null; }
|
|
5518
|
+
if (r?.ok && r.data?.success) testChild.confirmedUntilMs = Date.parse(r.data.expires_at ?? "") || testChild.confirmedUntilMs;
|
|
5519
|
+
else if (r?.ok && r.data?.success === false) prepLost = r.data.code ?? "TEST_CHILD_SLOT_LOST";
|
|
5520
|
+
}, Math.max(10_000, Math.floor((testChild.ttlSeconds ?? 180) * 1000 / 3)));
|
|
5521
|
+
prepRenew?.unref?.();
|
|
5522
|
+
// Codex #1782 r10: the lease heartbeat starts only once the child does, so the lease is
|
|
5523
|
+
// touched on its own cadence through preparation too, or a slow upload loses the stack.
|
|
5524
|
+
const prepTouch = hostEnv.error ? null : clock.setInterval(() => {
|
|
5525
|
+
void Promise.resolve().then(() => api.touch(leaseId, undefined)).catch(() => null);
|
|
5526
|
+
}, Math.max(1_000, Math.floor((current.data.idle_ttl_secs ?? opts.idleTtl ?? 1800) * 1000 / 3)));
|
|
5527
|
+
prepTouch?.unref?.();
|
|
5528
|
+
let started;
|
|
5529
|
+
activeWaitAbort = new AbortController();
|
|
5530
|
+
try {
|
|
5531
|
+
started = hostEnv.error ? { ok: false, code: "TEST_CHILD_ENV_REFUSED", error: hostEnv.error } : await startHostChild({
|
|
5532
|
+
api: adapters.hostChild ?? hostChildApi(rpc), upload: adapters.uploadBundle ?? uploadStackBundle,
|
|
5533
|
+
slotId: testChild.slotId, argv: childArgv, env: hostEnv, worktreeRoot: execution.worktreeRoot,
|
|
5534
|
+
maxRuntimeSeconds: opts.childMaxRuntime ?? HOST_CHILD_DEFAULT_MAX_RUNTIME_S,
|
|
5535
|
+
...(adapters.hostChildBundle ? { bundle: adapters.hostChildBundle } : {}),
|
|
5536
|
+
...(adapters.hostChildWait ? { wait: adapters.hostChildWait } : {}),
|
|
5537
|
+
log: stderrLine, ...(adapters.hostChildOutput ? { writeOutput: adapters.hostChildOutput } : {}),
|
|
5538
|
+
signal: activeWaitAbort.signal,
|
|
5539
|
+
});
|
|
5540
|
+
} finally {
|
|
5541
|
+
activeWaitAbort = null;
|
|
5542
|
+
if (prepRenew) clock.clearInterval(prepRenew);
|
|
5543
|
+
if (prepTouch) clock.clearInterval(prepTouch);
|
|
5544
|
+
}
|
|
5545
|
+
if (started.ok && prepLost) {
|
|
5546
|
+
started.child.stop();
|
|
5547
|
+
started.child.detach();
|
|
5548
|
+
started = { ok: false, code: prepLost, error: `the test_child slot was lost while the head bundle was prepared (${prepLost}); the host run was canceled` };
|
|
5549
|
+
}
|
|
5550
|
+
if (!started.ok) {
|
|
5551
|
+
testChild.released = await releaseTestChild(null);
|
|
5552
|
+
await closeTunnel();
|
|
5553
|
+
const cleanup = await requestCleanup();
|
|
5554
|
+
return { exitCode: receivedSignal ? SIGNAL_EXIT[receivedSignal] : cleanup.ok ? EXIT.BACKEND : EXIT.CLEANUP_FAILED,
|
|
5555
|
+
outcome: receivedSignal ? "interrupted" : "refused", code: started.code, leaseId, cleanup, error: started.error, testChild };
|
|
5556
|
+
}
|
|
5557
|
+
child = started.child;
|
|
5558
|
+
} else {
|
|
5559
|
+
child = startChild(childArgv, childEnv, childCwd);
|
|
5560
|
+
}
|
|
5459
5561
|
if (testChild) {
|
|
5460
|
-
|
|
5562
|
+
// A host child is measured by its own cgroup on the host, not sampled here.
|
|
5563
|
+
if (!child.hostRun) testChild.sampler = (adapters.testChildSampler ?? startChildSampler)(child.pid);
|
|
5461
5564
|
// Renew at a third of the slot TTL, independent of the lease touch cadence.
|
|
5462
5565
|
const renewMs = Math.max(10_000, Math.floor((testChild.ttlSeconds ?? 180) * 1000 / 3));
|
|
5463
5566
|
// Codex #1691 P1: a child whose slot is lost is stopped — a definitive TEST_CHILD_SLOT_LOST at
|
|
@@ -5570,7 +5673,12 @@ async function runStackLifecycleOnce(opts, childArgv, adapters, incidentBox) {
|
|
|
5570
5673
|
hardTimer?.unref?.();
|
|
5571
5674
|
childResult = await waitForChild(child);
|
|
5572
5675
|
if (testChildRenew) { clock.clearInterval(testChildRenew); testChildRenew = null; }
|
|
5573
|
-
if (testChild
|
|
5676
|
+
if (testChild && child.hostRun) {
|
|
5677
|
+
// The host already recorded the cgroup cost on the lease; release without overwriting it.
|
|
5678
|
+
testChild.resources = child.final?.child_resources ?? null;
|
|
5679
|
+
testChild.hostChild = hostChildReceiptFields(child).host_child;
|
|
5680
|
+
testChild.released = await releaseTestChild(null);
|
|
5681
|
+
} else if (testChild?.sampler) {
|
|
5574
5682
|
testChild.resources = testChild.sampler.stop();
|
|
5575
5683
|
testChild.sampler = null;
|
|
5576
5684
|
try { testChild.requesterSample = (adapters.testChildRequesterSample ?? (() => requesterSample({ rows: readPs() })))(); } catch { testChild.requesterSample = null; }
|
package/src/test-child.mjs
CHANGED
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
// memory and CPU-seconds; at exit the slot is released and the cost recorded on the lease.
|
|
8
8
|
//
|
|
9
9
|
// The kill switch and the default-off flag are read by the server on every decision.
|
|
10
|
-
//
|
|
11
|
-
//
|
|
10
|
+
// BOT-3030: a placement on the serving host (child-on-host) runs there (host-child.mjs); a
|
|
11
|
+
// placement kind this CLI does not know is refused (CHILD_PLACEMENT_UNSUPPORTED), never run
|
|
12
|
+
// elsewhere.
|
|
12
13
|
|
|
13
14
|
import { spawn, spawnSync } from "node:child_process";
|
|
14
15
|
import { readFileSync } from "node:fs";
|
|
@@ -25,7 +26,10 @@ export const SAMPLE_METHOD = "ps_tree_sample";
|
|
|
25
26
|
/** The three tool calls, over the run's retrying caller. */
|
|
26
27
|
export function testChildApi(rpc) {
|
|
27
28
|
return {
|
|
28
|
-
place: (leaseId, requesterSample) => rpc.call("place_test_child", {
|
|
29
|
+
place: (leaseId, requesterSample, prerequisites = null) => rpc.call("place_test_child", {
|
|
30
|
+
lease_id: leaseId, ...(requesterSample ? { requester_sample: requesterSample } : {}),
|
|
31
|
+
...(prerequisites?.length ? { prerequisites } : {}),
|
|
32
|
+
}),
|
|
29
33
|
renew: (slotId) => rpc.call("renew_test_child_slot", { slot_id: slotId }),
|
|
30
34
|
release: (slotId, childResources) => rpc.call("release_test_child_slot", { slot_id: slotId, ...(childResources ? { child_resources: childResources } : {}) }),
|
|
31
35
|
};
|
|
@@ -88,20 +92,26 @@ export function defaultTestChildWait(target, { bin = process.execPath, cli = pro
|
|
|
88
92
|
* Returns `{ ok: true, slotId, ttlSeconds, placement, waits }` or
|
|
89
93
|
* `{ ok: false, code, error, placement?, waits }`. Never throws.
|
|
90
94
|
*/
|
|
91
|
-
export
|
|
95
|
+
export const RUNNABLE_HOST_KINDS = Object.freeze(["requester", "serving_host"]);
|
|
96
|
+
|
|
97
|
+
export async function acquireTestChildSlot({ api, leaseId, wait = defaultTestChildWait, log = () => {}, isStopped = () => false, maxWaits = Infinity, onPark = async () => {}, signal = null, sample = placementSample, prerequisites = null }) {
|
|
92
98
|
const waits = [];
|
|
93
99
|
for (;;) {
|
|
94
100
|
// A reading that fails never blocks the run: the server then falls back to the host's cap.
|
|
95
101
|
let reading;
|
|
96
102
|
try { reading = sample(); } catch { reading = undefined; }
|
|
97
103
|
let answer;
|
|
98
|
-
try { answer = await api.place(leaseId, reading); } catch (error) { answer = { ok: false, error: String(error?.message ?? error) }; }
|
|
104
|
+
try { answer = await api.place(leaseId, reading, prerequisites); } catch (error) { answer = { ok: false, error: String(error?.message ?? error) }; }
|
|
99
105
|
const data = answer?.data;
|
|
100
106
|
if (answer?.ok && data?.success) {
|
|
101
107
|
const placement = data.child_placement ?? null;
|
|
102
|
-
if (placement?.host_kind
|
|
108
|
+
if (!RUNNABLE_HOST_KINDS.includes(placement?.host_kind)) {
|
|
103
109
|
return { ok: false, code: CHILD_PLACEMENT_UNSUPPORTED, placement, waits, slotId: data.slot_id,
|
|
104
|
-
error: `the control plane placed this child on ${placement?.host ?? "another host"} (${placement?.placement_reason ?? "unknown"}),
|
|
110
|
+
error: `the control plane placed this child on ${placement?.host ?? "another host"} as ${placement?.host_kind ?? "an unknown kind"} (${placement?.placement_reason ?? "unknown"}), which this CLI cannot run` };
|
|
111
|
+
}
|
|
112
|
+
// BOT-3030 (AC-5): say why a declared child stayed off its serving host.
|
|
113
|
+
if (Array.isArray(placement.missing_prerequisites) && placement.missing_prerequisites.length) {
|
|
114
|
+
log(`stack: the child runs on the requester: ${placement.serving_host ?? "the serving host"} lacks ${placement.missing_prerequisites.join(", ")} (${placement.placement_reason})`);
|
|
105
115
|
}
|
|
106
116
|
return { ok: true, slotId: data.slot_id, ttlSeconds: data.ttl_seconds ?? 180, expiresAt: data.expires_at ?? null, placement, waits };
|
|
107
117
|
}
|
|
@@ -284,5 +294,6 @@ export function testChildReceiptFields(tc) {
|
|
|
284
294
|
...(tc.requesterSample ? { requester_sample: tc.requesterSample } : {}),
|
|
285
295
|
...(tc.waits?.length ? { test_child_waits: tc.waits.length, test_child_first_wait_at: tc.waits[0].at } : {}),
|
|
286
296
|
...(tc.released != null ? { test_child_slot_released: tc.released } : {}),
|
|
297
|
+
...(tc.hostChild ? { host_child: tc.hostChild } : {}),
|
|
287
298
|
};
|
|
288
299
|
}
|
package/src/wait-core.mjs
CHANGED
|
@@ -139,6 +139,9 @@ export const SIGNAL_TYPES = Object.freeze([
|
|
|
139
139
|
// BOT-3240: a test_child slot in the caller's tenant was released (subject = the pool's
|
|
140
140
|
// run host). `bb stack run --test-child` parks on it while its pool is full.
|
|
141
141
|
"test_child_slot",
|
|
142
|
+
// BOT-3030: a host-placed test child started or ended (subject = the run's UUID).
|
|
143
|
+
// `bb stack run --test-child` waits on it while its child runs on the serving host.
|
|
144
|
+
"test_child_run",
|
|
142
145
|
]);
|
|
143
146
|
|
|
144
147
|
const LOCK_SUBTYPES = Object.freeze(["playwright_lane", "vite_port", "backend_port", "supabase_local"]);
|
|
@@ -572,6 +575,12 @@ const VALIDATORS = {
|
|
|
572
575
|
if (p.type === "test_child_slot" && !subject) {
|
|
573
576
|
throw new Error("event:type=test_child_slot needs subject=<run host>");
|
|
574
577
|
}
|
|
578
|
+
// BOT-3030: a test_child_run wait names its run, as the emitter writes it.
|
|
579
|
+
if (p.type === "test_child_run") {
|
|
580
|
+
const id = subject ? subject.toLowerCase() : "";
|
|
581
|
+
if (!CANONICAL_UUID_RE.test(id)) throw new Error("event:type=test_child_run needs subject=<run UUID>");
|
|
582
|
+
return { type: p.type, subject: id };
|
|
583
|
+
}
|
|
575
584
|
return { type: p.type, subject };
|
|
576
585
|
},
|
|
577
586
|
};
|