Repository navigation
Description
Context
Observed. Ready-candidate run 41534661678, unit (3) job 125583899161, head 926fcd5b0 (PR #3320), failed packages/ui-sdk/tests/governed-worker.test.ts:244, "launch refuses a folder identity swapped after planning before payload start (host restored: true)", in 5016 ms:
expect(existsSync(join(brain, "payload-started.md")), "UNQUALIFIED_FOLDER_PAYLOAD_NEVER_STARTS").toBe(false);
error: UNQUALIFIED_FOLDER_PAYLOAD_NEVER_STARTS
Expected: false
Received: true
The refusal assertions at lines 274-275 passed. The launch was refused with "retained folder mount points are unavailable" after the 5 s qualification deadline, and the payload still ran. This is a fail-open in the governed launch, not only a flaky test. A payload ran whose protected-folder mounts were never qualified.
Mechanism (source at main 0dab179b1bc393b44f6749b74cb1ca01804c1666).
- The non-helper live launch gates the payload with bubblewrap
--block-fd 4(workerBootstrap,packages/ui-sdk/src/server/worker-launcher.ts:969-976). fd 4 is the read end of a server-held FIFO (createWorkerStatusPipeSteps,packages/ui-sdk/src/server/worker-status.ts:32). - bubblewrap reads one byte from
--block-fdand proceeds whateverreadreturns, EOF included. Measured on bubblewrap here: a gate whose only writer closes without writing still runs the payload. - On any setup refusal, the catch in
launchAgentWorkerSteps(packages/ui-sdk/src/server/worker-launcher.ts:1550-1557) doeschild?.kill("SIGKILL"), which signals the outer bwrap process only, and then immediatelystatus?.close(). That callscloseGate()and closes the only gate writer (packages/ui-sdk/src/server/worker-status.ts:65). kill()returns before the outer process has died. The gated sandbox child is killed only later, through--die-with-parentwhen the outer actually exits. Closing the writer first delivers EOF, so the still-live sandbox child passes the gate and execs the payload. The race is lost whenever the outer bwrap is not scheduled to die before the payload's first write.- The separate-UID helper path is not affected: its
terminate()kills the tree and waits for the child before closinggate_write(packages/ui-sdk/src/server/worker-identity-program.ts:84-89, then thefinallyclose).
Evidence.
- bubblewrap EOF semantics: a FIFO gate with no writer bytes, closed after 0.5 s, still runs the payload.
- A standalone ordering probe with the same shape (shell
exec 4<…; exec bwrap --die-with-parent --unshare-user --unshare-pid … --block-fd 4). SIGKILL the outer process, then close the gate: 196/200 payloads ran. SIGKILL the gated child PID reported by--json-status-fdbefore closing: 0/200. - The real launcher, deterministically. This copy of the
host restored: truecase defers only the outerchild.killby 200 ms, standing in for a descheduled bwrap. It fails on exactlyUNQUALIFIED_FOLDER_PAYLOAD_NEVER_STARTS(Received: true). A local experiment that SIGKILLs the status-reported namespace PID beforestatus?.close()makes the same harness pass, with the refusal unchanged. The source was reverted; nothing is committed. - Intermittent in CI. Both variants passed in the four most recent green
mainCI runs checked (41532349775, 41519548963, 41515009247, 41504814872; the restore variant at ~5010 ms each). They also passed on PR chore(deps): drop the unused claude-agent-sdk-research-292 alias #3320's own earlier headddff51c2a(run 41494121721,unit (3)), which already contains its lockfile change. Only the one occurrence above was found among the 18 failedunitjobs of the latest 200 CI runs. - Not caused by chore(deps): drop the unused claude-agent-sdk-research-292 alias #3320. That PR changes only
bun.lock,package.json, an experiment probe, a native-diagram pin and docs. It touches nothing underpackages/ui-sdk. The test's payload is/bin/sh, not the SDK binary.
Diagnostic harness (copy of the restore-host case with a deferred outer kill)
import { expect, spyOn, test } from "bun:test";
import { mkdtempSync, mkdirSync, writeFileSync, renameSync, existsSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { prepareWorkerSandbox, launchAgentWorker } from "../src/server/worker-launcher.js";
test("refused restore-host launch with a delayed outer kill", async () => {
const brain = mkdtempSync(join(tmpdir(), "gate-race-"));
mkdirSync(join(brain, "context/policies"), { recursive: true });
writeFileSync(join(brain, "context/policies/governance.md"), "Athena sets the policy.\n");
mkdirSync(join(brain, "group/private"), { recursive: true });
const sandbox = prepareWorkerSandbox(brain, { profile: "governed", areas: [], folderPaths: ["group/private"] });
const spawn = Bun.spawn; let swapped = false;
const hook = spyOn(Bun, "spawn").mockImplementation((...args: any[]) => {
const launch = !swapped && args[1]?.env?.BRAIN_WORKER_LAUNCH;
if (launch) { swapped = true; renameSync(join(brain, "group"), join(brain, "retired-group")); mkdirSync(join(brain, "group/private"), { recursive: true }); }
const child = (spawn as any)(...args);
if (launch) { const kill = child.kill.bind(child); child.kill = (s?: any) => { setTimeout(() => kill(s), 200); }; }
return child;
});
let refusal: unknown;
try { launchAgentWorker({ brainPath: brain, sandbox, env: {}, stateBytes: 1024 * 1024, populateState: () => {
renameSync(join(brain, "group"), join(brain, "replacement-group")); renameSync(join(brain, "retired-group"), join(brain, "group")); },
command: ["/bin/sh", "-c", "printf Odysseus > payload-started.md"] }); } catch (error) { refusal = error; } finally { hook.mockRestore(); }
await Bun.sleep(600);
expect(String(refusal)).toContain("retained folder mount points are unavailable");
expect(existsSync(join(brain, "payload-started.md")), "UNQUALIFIED_FOLDER_PAYLOAD_NEVER_STARTS").toBe(false);
}, 20000);Run with Bun 1.4.4 on a 16-CPU host with permitted user namespaces.
Scope
- On every refusal path of a launch gated by the status pipe, the gated sandbox process must be dead before the gate can deliver EOF. Either SIGKILL the authenticated, status-reported namespace PID (re-checking its start time) and confirm it exited before
status.close(), or keep the gate writer open until the outer bwrap and its sandbox child have been reaped. Choose whichever keeps the PID-identity guarantees ofverifiedWorkerPid(packages/ui-sdk/src/server/worker-status.ts). - Audit every other closer of a status-pipe gate for the same ordering. That covers the
child.exitedcleanup that closes the status channel (packages/ui-sdk/src/server/worker-launcher.ts:1428-1429) and any synchronous or probe path that creates a status pipe. Record for each whether an early writer close can release a live gated child. - Record whether a server crash between spawn and release also closes the writer and releases a live gated child. This needs scoping: file it separately if it holds and is not fixed by item 1.
Out of scope
- The separate-UID identity helper path, which already waits before closing its gate.
- Changing the qualification deadline, the folder-identity checks or the refusal messages.
- The mount-record refusals tracked in ui-sdk: attribute intermittent invalid worker mount-record refusals #2701.
Acceptance criteria
- A deterministic regression test in
packages/ui-sdk/tests/fails on the current source withUNQUALIFIED_FOLDER_PAYLOAD_NEVER_STARTS(received true). It does so by deferring the outer-process kill, as in the harness above, or by an equivalent scheduling-independent mechanism. It passes after the fix. Record both runs and a mutation receipt in the PR. - The existing
governed-worker.test.ts:244case passes unchanged, with no assertion, deadline or guard relaxed. - Each gate closer from Scope item 2 is listed in the PR with its disposition.
Verification
bun run test packages/ui-sdk/tests/governed-worker.test.ts, the new regression test, and bun run test packages/ui-sdk packages/ui-server on a host that permits user namespaces. Record Bun version and CPU count.
Contract impact
None.
Blocks merge qualification of #2509 (PR #3320), whose ready-candidate run hit this failure.
Activity
schlessera commentedon Nov 2, 2026
Claiming this issue (token
claim-3387-b8550c9e). Branch:fix/3387-gate-kill-order. Collision report: path overlap with #3342 onpackages/ui-sdk/tests/governed-worker.test.tsonly; the regression test goes in a new file to avoid it. Other reports are closure-only.schlessera commentedon Nov 2, 2026
Status (claim token
claim-3387-b8550c9e): the fix is in draft PR #3396 (head99fd7e42e, milestone 0.45.0,Closes #3387). It is ready to confirm, but was not confirmed here because the integration lease is held by another candidate.--die-with-parent, and runs the payload once the gate closes.child.exited.finally(...)) could also release a living gated child when bubblewrap died before release. It is fixed in the same PR.origin/mainon the payload-marker assertions and pass after the fix, and mutation receipts for each layer are in the PR. These runs used a rootless, network-less container, because the working host blocks unprivileged user namespaces. Hosted CI remains the authoritative full proof.To resume:
bun scripts/integrate.ts confirm 3396 --issues schlessera/brain-kit#3387once the lease is free and main is green.fix(ui-sdk): keep a gated payload out when the gate holder dies befor…
fix(ui-sdk): keep a gated payload out when the gate holder dies befor…