Skip to content

ui-sdk: a refused status-gated launch closes the start gate before its sandbox dies, so the payload can still run #3387

Closed

Description

@schlessera

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-fd and proceeds whatever read returns, 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) does child?.kill("SIGKILL"), which signals the outer bwrap process only, and then immediately status?.close(). That calls closeGate() 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-parent when 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 closing gate_write (packages/ui-sdk/src/server/worker-identity-program.ts:84-89, then the finally close).

Evidence.

  1. bubblewrap EOF semantics: a FIFO gate with no writer bytes, closed after 0.5 s, still runs the payload.
  2. 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-fd before closing: 0/200.
  3. The real launcher, deterministically. This copy of the host restored: true case defers only the outer child.kill by 200 ms, standing in for a descheduled bwrap. It fails on exactly UNQUALIFIED_FOLDER_PAYLOAD_NEVER_STARTS (Received: true). A local experiment that SIGKILLs the status-reported namespace PID before status?.close() makes the same harness pass, with the refusal unchanged. The source was reverted; nothing is committed.
  4. Intermittent in CI. Both variants passed in the four most recent green main CI 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 head ddff51c2a (run 41494121721, unit (3)), which already contains its lockfile change. Only the one occurrence above was found among the 18 failed unit jobs of the latest 200 CI runs.
  5. 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 under packages/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

  1. 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 of verifiedWorkerPid (packages/ui-sdk/src/server/worker-status.ts).
  2. Audit every other closer of a status-pipe gate for the same ordering. That covers the child.exited cleanup 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.
  3. 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

Acceptance criteria

  • A deterministic regression test in packages/ui-sdk/tests/ fails on the current source with UNQUALIFIED_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:244 case 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

  1. added
    type: fixSomething is broken and must behave the way it is documented.
    area: ui-sdkWire protocol, runtime schemas, the server and client SDK surfaces.
    priority: p1Scheduled into the current or next milestone.
    agent-readyContext and acceptance criteria are complete enough for an agent to take this unattended.
    securityAuth, isolation, egress, secrets, or anything in the threat model.
    on Nov 2, 2026
  2. added this to the 0.45.0 milestone on Nov 2, 2026
  3. self-assigned this
    on Nov 2, 2026
  4. schlessera commented on Nov 2, 2026

    @schlessera
    OwnerAuthor

    Claiming this issue (token claim-3387-b8550c9e). Branch: fix/3387-gate-kill-order. Collision report: path overlap with #3342 on packages/ui-sdk/tests/governed-worker.test.ts only; the regression test goes in a new file to avoid it. Other reports are closure-only.

  5. schlessera commented on Nov 2, 2026

    @schlessera
    OwnerAuthor

    Status (claim token claim-3387-b8550c9e): the fix is in draft PR #3396 (head 99fd7e42e, 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.

    • Fix. Closing an unreleased start gate now stops bubblewrap, kills its children and the status-verified sandbox process (by PID and start time), kills bubblewrap, and closes the gate only after bubblewrap has exited and every killed process is confirmed dead. A gate whose processes never die stays open.
    • Finding. Waiting for bubblewrap's death alone is not enough. The gated child outlives a killed and reaped bubblewrap, with or without --die-with-parent, and runs the payload once the gate closes.
    • Scope item 2. Every gate closer and its disposition is listed in the PR. The supervisor-exit closer (child.exited.finally(...)) could also release a living gated child when bubblewrap died before release. It is fixed in the same PR.
    • Scope item 3. A server crash between spawn and release releases the payload 20/20, and this fix does not cover it. Filed as ui-sdk: a server crash between spawn and start-gate release lets the gated payload run #3392.
    • Receipts. A new regression test runs three cases. All three fail on origin/main on 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#3387 once the lease is free and main is green.

  6. removed their assignment
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyContext and acceptance criteria are complete enough for an agent to take this unattended.area: ui-sdkWire protocol, runtime schemas, the server and client SDK surfaces.priority: p1Scheduled into the current or next milestone.securityAuth, isolation, egress, secrets, or anything in the threat model.type: fixSomething is broken and must behave the way it is documented.

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions