No detached run mode: --local-agent binds the agent worker to the invoking terminal

Merged
AG
2mo ago
+1174-26

Detach local run and resume processes from the invoking terminal

Closing a terminal currently kills --local-agent and authored TypeScript flow
bodies. flows run/resume --detach moves the whole CLI execution into its own
session, returns the admitted run ID, and provides log/receipt paths and a
flows status command. Both agent workers and authored bodies can continue
after the invoking terminal closes.

  • Preflight runs once in the child. Atomic receipts return startup refusals
    with their original diagnostics and exit code; replayed resume history is
    never treated as admission.
  • Observer URLs use an explicit callback and a bounded wait. Late URLs and
    terminal outcomes remain in the receipt/log. Cloud mirroring stays opt-in.
  • Local worker remedies explain terminal lifetime and --detach. Help and
    docs/SURFACE.md describe exit semantics, credentials, logs, and inspection.
  • Startup is bounded and reports early child failure with a log tail.
    FLOWS_LOCAL_AGENT_ENV_FD is refused rather than silently dropped.

Scope: implements rung 1 of reviewed-plan.md §7, its explicitly recommended
standalone release. worker.release, flows worker start/stop/list, and
redrive-on-attach remain follow-ups. No kernel behavior or workflow files change.
This is process detachment, not automatic restart supervision. The parent's
exit 0 confirms admission; the child may subsequently fail, park, or suspend.

Follow-up fix: --detach did not work in the standalone binary

The first cut of --detach only worked when the CLI was invoked as
node dist/cli.js. The shipped single-file flows executable broke on both
halves of the handshake:

  • The child was spawned as <flows-binary> /$bunfs/root/cli.js run .... That
    path exists only inside the running process' virtual filesystem, so the
    binary read it as the flow argument and refused the invocation. --detach
    returned detached_start_failed and exit 1; no run was ever admitted.
  • cli.ts takes the detach receipt only under isDirectInvocation, which a
    compiled binary never satisfies, and the generated standalone entry did not
    take it either. So even with the argv fixed, the child would run to
    completion while the parent waited out its full 60 s startup budget and then
    sent the child SIGTERM.

The fix (commit below the original head; see the PR conversation) passes an explicit
selfCommand: [process.execPath] from the standalone entry, so the compiled binary
re-execs itself with the arguments alone, and the standalone entry takes the
receipt before any authored source is installed.

Covered by tests/authored-node-runtime.test.ts > "detaches through the compiled
executable and the child publishes its receipt", which builds and drives the real
compiled binary (Bun 1.4.0), and tests/detached-start.test.ts > "re-execs a
compiled executable through selfCommand". Both fail on the original head.

Validation: 244 tests passed across nine selected suites, including real-kernel
YAML, authored-body, and resume cases that kill the invoking terminal's process
group mid-agent, then assert completion through flows status --json.
The tests also cover preflight refusal, human-influenced resume consent,
park receipts/remedies, timeout/early-exit handling, observer publication,
argument forwarding, and the CLI declaration. Provider execution uses a
controlled wrapper fixture, not paid provider calls.

Literal commands and complete captured output:

The full SDK suite and kernel test suite were not run. No mutation-verification
claim is made. Live-test temporary directories are removed by fixture cleanup;
the captured outputs above are retained.

Checks

The checks fail on the base commit too, so these failures were not introduced by this change: they come from the repository itself or from the environment the checks ran in. This pull request is a draft until someone looks.

What ran (.relayflow/check.sh)
#!/bin/sh
# Fresh-machine equivalent of this repository's CI checks.
#
# Mirrors, in order:
#   .github/workflows/cloud-runtime-artifact.yml  (job: linux-x64-artifact)
#   .github/workflows/schema-publish.yml          (job: validate, PR half only)
#   .github/workflows/surface-package.yml         (job: scripts/surface-package-gate.sh)
#
# Deliberately NOT run here, because this machine has neither the credentials
# nor the surfaces these need:
#   * .github/workflows/review-swarm.yml — needs the CLOUD_API_KEY and
#     RELAY_WORKSPACE_KEY secrets and a live Agent Relay cloud workspace to
#     launch the review swarm into.
#   * .github/workflows/review-swarm-wrapper-guard.yml — a pull_request_target
#     guard that reads the PR base via `gh` and a GitHub token; its two parity
#     checks are repo-local, but the guard itself is a PR-event gate, not a
#     tree check.
#   * .github/workflows/publish.yml and the publish/pages jobs of
#     schema-publish.yml — `npm publish` with NPM_TOKEN and a gh-pages
#     deployment.
#   * the macos-14 darwin-arm64 runtime jobs of publish.yml — need a macOS
#     runner; this is linux-x64.
set -e

repo_root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd -P)
cd "$repo_root"

step() {
  printf '\n=== %s ===\n' "$1"
}

# ---------------------------------------------------------------- toolchains

step 'Toolchain: node'
# setup-node@v4 node-version 22 in CI. Nothing here installs node: on a fresh
# machine it is the prerequisite that brings its own package manager (npm), so
# fail loudly rather than guess at a distro package.
if ! command -v node >/dev/null 2>&1; then
  echo "node is required (CI uses node 22); install it and re-run" >&2
  exit 127
fi
node --version
npm --version

step 'Toolchain: bun'
# setup-bun@v2 bun-version 1.4.0 in CI. packages/surface, packages/schema and
# the standalone CLI build are all bun-driven.
#
# The version is PINNED, not merely present: tests/authored-node-runtime.test.ts
# asserts the standalone build ran on exactly 1.4.0, so an older bun already on
# PATH (this machine ships 1.3.6 under nvm) fails the suite with
# "expected '1.3.6' to be '1.4.0'". Install the pinned version beside it and put
# it first, the way setup-bun@v2 does.
BUN_PIN=1.4.0
if [ "$(bun --version 2>/dev/null)" != "$BUN_PIN" ]; then
  if [ "$("$HOME/.bun/bin/bun" --version 2>/dev/null)" != "$BUN_PIN" ]; then
    BUN_INSTALL="$HOME/.bun" curl -fsSL https://bun.sh/install | bash -s "bun-v$BUN_PIN"
  fi
  PATH="$HOME/.bun/bin:$PATH"
  export PATH
fi
bun --version
[ "$(bun --version)" = "$BUN_PIN" ] || { echo "bun $BUN_PIN is required; got $(bun --version)" >&2; exit 127; }

step 'Toolchain: rust'
# dtolnay/rust-toolchain@stable in CI.
#
# Plain cargo, NOT ops/cargo.sh — the same reason the workflow gives: that
# wrapper redirects RUSTUP_HOME to $HOME/.relayflows-toolchain/rustup, which is
# empty on a fresh machine, so the rustup shim has no default toolchain to
# choose and dies with "rustup could not choose a version of cargo to run".
if ! command -v cargo >/dev/null 2>&1; then
  if [ ! -x "$HOME/.cargo/bin/cargo" ]; then
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
      | sh -s -- -y --default-toolchain stable --profile minimal --no-modify-path
  fi
  if [ ! -x "$HOME/.cargo/bin/cargo" ]; then
    echo "rustup install did not produce $HOME/.cargo/bin/cargo" >&2
    exit 127
  fi
  PATH="$HOME/.cargo/bin:$PATH"
  export PATH
fi
cargo --version

step 'Toolchain: bubblewrap (hosted extension sandbox)'
# CI provisions bwrap so the SDK suite can exercise the exact production
# user/PID/network/mount namespace command. Without it those cases cannot run.
if ! command -v bwrap >/dev/null 2>&1; then
  if command -v sudo >/dev/null 2>&1 && command -v apt-get >/dev/null 2>&1; then
    sudo apt-get update
    sudo apt-get install --yes --no-install-recommends bubblewrap
  else
    echo "bwrap missing and no sudo/apt-get here; sandbox cases will fail" >&2
  fi
fi
if command -v bwrap >/dev/null 2>&1; then
  bwrap --version
  # Ubuntu 24.04 restricts unprivileged user namespaces through AppArmor.
  # Best-effort, unlike CI's hard `sudo sysctl -w`: inside a container the knob
  # is often absent or read-only, and that is not a reason to fail the run.
  if command -v sudo >/dev/null 2>&1 \
    && sudo sysctl kernel.apparmor_restrict_unprivileged_userns >/dev/null 2>&1; then
    sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 || true
  fi
  bwrap --unshare-all --die-with-parent --new-session --ro-bind / / /bin/true \
    || echo "bwrap smoke failed; unprivileged user namespaces look unavailable" >&2
fi

step 'Isolate the tree from an ancestor package.json'
# CI checks out into a directory with no package.json above it, so Node sees
# "type unknown" for the extension-less agent-CLI fixtures under
# testdata/preflight/ and its module-syntax detection loads them as ESM.
#
# This machine has $HOME/package.json with "type": "commonjs", and the repo
# root has no package.json of its own, so that stray manifest is the nearest
# one for the whole checkout. Detection is then off, every `*-cli` fixture is
# loaded as CommonJS, and each exits 0 having written nothing -- which the
# worker reports as
#   CLI "..." exited before completing the relayflows-agent-cli-v1 same-process
#   handshake
# and seven live-kernel cases fail on a null step output.
#
# Restore CI's "type unknown" by shadowing it with an empty manifest in the
# checkout's PARENT, not inside the repo: no "type" key means detection is on
# again, and nothing in the tree gains an untracked file.
repo_parent=$(dirname -- "$repo_root")
if [ ! -e "$repo_root/package.json" ] && [ ! -e "$repo_parent/package.json" ]; then
  node -e '
    const { dirname, join } = require("node:path");
    const { existsSync } = require("node:fs");
    let dir = dirname(process.argv[1]);
    for (;;) {
      const candidate = join(dir, "package.json");
      if (existsSync(candidate)) { console.log(candidate); break; }
      const next = dirname(dir);
      if (next === dir) break;
      dir = next;
    }
  ' "$repo_parent" > /tmp/ancestor-manifest.txt
  ancestor=$(cat /tmp/ancestor-manifest.txt)
  if [ -n "$ancestor" ]; then
    echo "shadowing ancestor manifest $ancestor with $repo_parent/package.json"
    printf '{}\n' > "$repo_parent/package.json"
  fi
fi
node -e '
  const { spawnSync } = require("node:child_process");
  const fixture = process.argv[1];
  const probe = spawnSync(process.execPath, [fixture, "--relayflows-adapter-v1"], {
    input: "", encoding: "utf8",
  });
  if (!probe.stdout.startsWith("relayflows-agent-cli-v1")) {
    throw new Error(`agent CLI fixtures do not load as ESM here; got ${JSON.stringify(probe.stdout)} ${JSON.stringify(probe.stderr)}`);
  }
  console.log("agent CLI fixtures load as ESM");
' testdata/preflight/analyze-story-stub-cli

# ------------------------------------- cloud-runtime-artifact: linux-x64-artifact

step 'Test artifact contract'
node --test scripts/cloud-artifact.test.mjs

step 'Build relayflowd'
( cd kernel && cargo build --locked --release -p relayflowd )

step 'Test kernel'
( cd kernel && cargo test --workspace )

step 'Reclaim the kernel debug target'
# CI runs the three mirrored workflows as SEPARATE jobs, each with its own
# runner disk. Here they share one 10 GB filesystem, and `cargo test
# --workspace` above leaves a ~3.6 GB debug target that nothing below needs:
# every later step reaches relayflowd through RELAYFLOWD_BIN, which points at
# the release build. Keeping it starves the SDK suite, whose authored-node
# fixtures each dereference a ~145 MB @relayflows/surface copy into $TMPDIR --
# seventeen of them, all retained until the file's afterAll -- and the suite
# dies with "ENOSPC: no space left on device" instead of a test result.
rm -rf kernel/target/debug
df -h "$repo_root" | tail -1

step 'Build authoring surface'
( cd packages/surface && bun install --frozen-lockfile --ignore-scripts && bun run build )

# --ignore-scripts because the surface is already built above; without it npm
# runs the file: dependency's prepare before its own devDependencies exist.
step 'Install SDK dependencies'
npm ci --prefix packages/sdk --ignore-scripts

# `npm ci` installs @relayflows/surface from the REGISTRY, not the local build
# above. Override it with the local directory; --no-save keeps package.json and
# package-lock.json unchanged. The leading ./ is load-bearing — without it npm
# reads the string as a GitHub org/repo shorthand.
step 'Override registry surface with local build'
npm install ./packages/surface --prefix packages/sdk --no-save --ignore-scripts

# workflows/*.flow.ts import @relayflows/surface from the repo root, where
# nothing is installed; link the same local surface there.
step 'Link the local surface at the repo root'
mkdir -p node_modules/@relayflows
ln -sfn ../../packages/sdk/node_modules/@relayflows/surface \
  node_modules/@relayflows/surface
node -e "console.log(require.resolve('@relayflows/surface'))"

step 'Test SDK and type-level authoring contracts'
# This is `npm test` minus test:prep, which shells out to ops/cargo.sh (see the
# rust note above) only to produce a relayflowd binary that
# tests/live-kernel.test.ts execs — the release binary built above is pointed
# at via RELAYFLOWD_BIN instead of compiling a second debug copy.
#
# test:prep's other half is kept: re-assert the executable bit on the preflight
# CLI fixtures, so a preflight test cannot fail with an opaque EACCES.
[ ! -d testdata/preflight ] \
  || find testdata/preflight -name '*-cli' -type f -exec chmod +x {} +
# RELAYFLOWS_ALLOW_ANALYZER_SKIP, exactly as CI sets it: live-kernel.test.ts
# runs one case against the real Claude analyzer and fails by default when it
# cannot. There is no `claude` binary and no model access here, so that one
# case is skipped and says so in its own output — meaning this run is NOT
# gate-2 acceptance evidence. Everything else in the suite still gates.
(
  cd packages/sdk
  npm run typecheck
  npm run build
  npm run typecheck:tests
  RELAYFLOWS_ALLOW_ANALYZER_SKIP=1 \
    RELAYFLOWD_BIN="$repo_root/kernel/target/release/relayflowd" \
    ./node_modules/.bin/vitest run
)

step 'Build standalone flows CLI'
mkdir -p dist/cloud-artifact-input
node scripts/build-standalone-cli.mjs bun-linux-x64 dist/cloud-artifact-input/flows

step 'Assemble artifact and smoke verifier path'
# CI stamps the PR head sha; a local checkout has only HEAD.
node scripts/cloud-artifact.mjs build \
  --relayflowd kernel/target/release/relayflowd \
  --flows-executable dist/cloud-artifact-input/flows \
  --output-dir dist/cloud-artifact \
  --source-commit "$(git rev-parse HEAD)"
archive="$(find dist/cloud-artifact -name '*.tar.gz' -type f -print -quit)"
checksum="$(awk '{print $1}' "$archive.sha256")"
node scripts/cloud-artifact.mjs verify --archive "$archive" --sha256 "$checksum"

step 'Smoke exact Linux artifact'
mkdir -p dist/cloud-artifact-smoke
tar -xzf "$archive" -C dist/cloud-artifact-smoke
dist/cloud-artifact-smoke/bin/relayflowd --help
flows_output="$(dist/cloud-artifact-smoke/bin/flows check --json testdata/hello-deterministic.flow.yaml)"
printf '%s\n' "$flows_output"
node -e '
  const report = JSON.parse(process.argv[1]);
  if (report.ok !== true) throw new Error("flows smoke report was not ok");
  if (report.path !== "testdata/hello-deterministic.flow.yaml") {
    throw new Error(`flows smoke reported unexpected path: ${report.path}`);
  }
' "$flows_output"

# ------------------------------------------------- schema-publish: validate

step 'Regenerate and check committed schema'
# Byte-identical to what is committed, and identical across two generations.
node scripts/generate-json-schema.mjs
git diff --exit-code -- packages/schema/flows.schema.json
cp packages/schema/flows.schema.json /tmp/flows.schema.first.json
node scripts/generate-json-schema.mjs
diff -q /tmp/flows.schema.first.json packages/schema/flows.schema.json

step 'Schema parity and smoke'
( cd packages/schema && bun run test )

# ------------------------------------------- surface-package: the whole gate

step 'Test source, regressions, and packed consumers'
# Last, because this gate re-runs `npm ci` under packages/sdk and swaps the
# directory-installed surface for a freshly packed tarball; the SDK suite above
# has already run against the arrangement CI's artifact job uses.
bash scripts/surface-package-gate.sh

printf '\nALL CHECKS PASSED\n'
Output on this branch (last 80 lines)

 ❯ tests/hosted-extension-protocol.test.ts:447:5
    445|   ])('rejects an import-time %s frame with zero adapter calls', async …
    446|     let calls = 0;
    447|     await expect(runVerifiedNativeExtensionSandbox({
       |     ^
    448|       artifact: await artifact(hostileImport([frame, { type: 'error', …
    449|       manifest: validateFlowExtensionManifest(manifest()), dispatch: d…

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[14/24]⎯

 FAIL  tests/hosted-extension-protocol.test.ts > hosted extension hostile protocol > rejects two forged calls after the authoritative first outcome settles
 FAIL  tests/hosted-extension-protocol.test.ts > hosted extension hostile protocol > waits for a pending adapter to reject after a forged child error
 FAIL  tests/hosted-extension-protocol.test.ts > hosted extension hostile protocol > waits for a pending adapter to resolve after a forged child error
 FAIL  tests/hosted-extension-protocol.test.ts > hosted extension hostile protocol > returns a typed adapter rejection even when the hostile child hangs
Error: hostile child did not invoke the adapter
 ❯ Timeout._onTimeout tests/hosted-extension-protocol.test.ts:135:45
    133| async function waitForInvocation(invoked: Promise<void>): Promise<void…
    134|   await new Promise<void>((resolve, reject) => {
    135|     const timeout = setTimeout(() => reject(new Error('hostile child d…
       |                                             ^
    136|     void invoked.then(() => { clearTimeout(timeout); resolve(); }, rej…
    137|   });

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[15/24]⎯

 FAIL  tests/software-garden-babysitter-composition.test.ts > canonical Software Garden + Babysitter composition > propagates capability denial without a retry or fallback
AssertionError: expected Error: Hosted extension sandbox exited wi… { code: '…' } to be Error: live babysit label is absent // Object.is equality

- Expected
+ Received

- [Error: live babysit label is absent]
+ [Error: Hosted extension sandbox exited without a valid completion (exit 1): bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted
+ ]

 ❯ tests/software-garden-babysitter-composition.test.ts:139:7
    137|       const refusal = new Error('live babysit label is absent');
    138|       let calls = 0;
    139|       await expect(runHostedSoftwareGardenBabysitter({
       |       ^
    140|         flowPath: installed.flowPath,
    141|         dispatch: dispatch(),

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[16/24]⎯

⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯
Error: Hosted extension sandbox exited without a valid completion (exit 1): bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted

 ❯ refuse src/hosted-extension-protocol.ts:135:21
    133|       : new PluginError('plugin_unsupported', 'Hosted capability rejec…
    134|     const refuse = (message: string) => {
    135|       const error = new PluginError('plugin_unsupported', message);
       |                     ^
    136|       CHILD_PROCESS_KILL(child, 'SIGKILL');
    137|       if (capabilityState === 'pending') {
 ❯ ChildProcess.<anonymous> src/hosted-extension-protocol.ts:234:21
 ❯ ChildProcess.emit node:events:520:22
 ❯ maybeClose node:internal/child_process:1084:16
 ❯ Process.ChildProcess._handle.onexit node:internal/child_process:304:5

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { code: 'plugin_unsupported' }
This error originated in "tests/hosted-extension-protocol.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
The latest test that might've caused the error is "rejects two forged calls after the authoritative first outcome settles". It might mean one of the following:
- The error was thrown, while Vitest was running this test.
- If the error occurred after the test had been completed, this was the last documented test before it was thrown.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 Test Files  4 failed | 248 passed | 1 skipped (253)
      Tests  24 failed | 3840 passed | 4 skipped (3868)
     Errors  1 error
   Start at  16:33:52
   Duration  621.05s (transform 6.30s, setup 1.07s, collect 69.03s, tests 1703.69s, environment 29ms, prepare 9.55s)

Output on the base commit (last 80 lines)

 ❯ tests/hosted-extension-protocol.test.ts:447:5
    445|   ])('rejects an import-time %s frame with zero adapter calls', async …
    446|     let calls = 0;
    447|     await expect(runVerifiedNativeExtensionSandbox({
       |     ^
    448|       artifact: await artifact(hostileImport([frame, { type: 'error', …
    449|       manifest: validateFlowExtensionManifest(manifest()), dispatch: d…

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[13/24]⎯

 FAIL  tests/hosted-extension-protocol.test.ts > hosted extension hostile protocol > rejects two forged calls after the authoritative first outcome settles
 FAIL  tests/hosted-extension-protocol.test.ts > hosted extension hostile protocol > waits for a pending adapter to reject after a forged child error
 FAIL  tests/hosted-extension-protocol.test.ts > hosted extension hostile protocol > waits for a pending adapter to resolve after a forged child error
 FAIL  tests/hosted-extension-protocol.test.ts > hosted extension hostile protocol > returns a typed adapter rejection even when the hostile child hangs
Error: hostile child did not invoke the adapter
 ❯ Timeout._onTimeout tests/hosted-extension-protocol.test.ts:135:45
    133| async function waitForInvocation(invoked: Promise<void>): Promise<void…
    134|   await new Promise<void>((resolve, reject) => {
    135|     const timeout = setTimeout(() => reject(new Error('hostile child d…
       |                                             ^
    136|     void invoked.then(() => { clearTimeout(timeout); resolve(); }, rej…
    137|   });

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[14/24]⎯

 FAIL  tests/software-garden-babysitter-composition.test.ts > canonical Software Garden + Babysitter composition > propagates capability denial without a retry or fallback
AssertionError: expected Error: Hosted extension sandbox exited wi… { code: '…' } to be Error: live babysit label is absent // Object.is equality

- Expected
+ Received

- [Error: live babysit label is absent]
+ [Error: Hosted extension sandbox exited without a valid completion (exit 1): bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted
+ ]

 ❯ tests/software-garden-babysitter-composition.test.ts:139:7
    137|       const refusal = new Error('live babysit label is absent');
    138|       let calls = 0;
    139|       await expect(runHostedSoftwareGardenBabysitter({
       |       ^
    140|         flowPath: installed.flowPath,
    141|         dispatch: dispatch(),

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[15/24]⎯

⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯
Error: Hosted extension sandbox exited without a valid completion (exit 1): bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted

 ❯ refuse src/hosted-extension-protocol.ts:135:21
    133|       : new PluginError('plugin_unsupported', 'Hosted capability rejec…
    134|     const refuse = (message: string) => {
    135|       const error = new PluginError('plugin_unsupported', message);
       |                     ^
    136|       CHILD_PROCESS_KILL(child, 'SIGKILL');
    137|       if (capabilityState === 'pending') {
 ❯ ChildProcess.<anonymous> src/hosted-extension-protocol.ts:234:21
 ❯ ChildProcess.emit node:events:520:22
 ❯ maybeClose node:internal/child_process:1084:16
 ❯ Process.ChildProcess._handle.onexit node:internal/child_process:304:5

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { code: 'plugin_unsupported' }
This error originated in "tests/hosted-extension-protocol.test.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
The latest test that might've caused the error is "rejects two forged calls after the authoritative first outcome settles". It might mean one of the following:
- The error was thrown, while Vitest was running this test.
- If the error occurred after the test had been completed, this was the last documented test before it was thrown.
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 Test Files  4 failed | 248 passed | 1 skipped (253)
      Tests  24 failed | 3840 passed | 4 skipped (3868)
     Errors  1 error
   Start at  16:46:01
   Duration  598.61s (transform 6.17s, setup 1.09s, collect 69.57s, tests 1641.68s, environment 29ms, prepare 9.50s)

What the repair agent found

Check repair notes

.relayflow/check.sh on this machine. .relayflow/check.log recorded
Test Files 6 failed | 246 passed | 1 skipped (253) and
Tests 31 failed | 3816 passed | 20 skipped (3867).

Two of the 6 failed files are missing setup and are fixed in check.sh below:
authored-node-runtime.test.ts (the whole file, failed in beforeAll) and the
7 failed cases in live-kernel.test.ts. A third setup problem, disk
exhaustion, appeared only once the first of those was unblocked and is fixed
too. The other 24 failed cases, all in the four hosted-extension/sandbox files,
are outside this machine's control and are left as they are.

1. Fixed in check.sh — bun was not pinned to 1.4.0

tests/authored-node-runtime.test.ts:18 asserts the standalone build ran on
exactly bun 1.4.0, which is what setup-bun@v2 installs in CI. This machine
already had bun 1.3.6 on PATH under nvm, and the old check.sh only installed
bun when none was present, so the suite failed with:

AssertionError: expected '1.3.6' to be '1.4.0'

check.sh now installs and prefers bun-v1.4.0 whenever the version on PATH
is not the pin, and fails loudly if it still is not.

2. Fixed in check.sh — a stray ancestor package.json made the agent-CLI fixtures CommonJS

Seven tests/live-kernel.test.ts cases failed with a null step output and

verification: { gate: 'execution', verdict: 'fail' }
CLI ".../testdata/preflight/analyze-story-stub-cli" exited before
completing the relayflows-agent-cli-v1 same-process handshake

The fixtures under testdata/preflight/ are extension-less #!/usr/bin/env node
ESM files. Node decides their module type from the nearest package.json; the
repository root has none, so in CI the type is unknown and Node's
module-syntax detection loads them as ESM. On this machine the nearest one is
/home/daytona/package.json, which declares "type": "commonjs". Detection is
then off, every fixture is loaded as CommonJS and exits 0 having written
nothing — so the worker never sees the handshake token.

Reproduced in isolation (the same fixture under a {"type":"commonjs"}
ancestor, versus no ancestor at all):

$ node testdata/preflight/analyze-story-stub-cli --relayflows-adapter-v1 </dev/null
exit=0                        # no output at all
$ cp analyze-story-stub-cli wrapper-session.mjs /tmp/stubtest/ && \
  node /tmp/stubtest/analyze-story-stub-cli --relayflows-adapter-v1 </dev/null
relayflows-agent-cli-v1
exit=0

check.sh now restores CI's "type unknown" by writing an empty {} manifest in
the checkout's parent directory — outside the repository, so no untracked
file appears in the tree — and then probes one fixture to prove it loads as ESM
before any suite runs. All seven cases pass with that in place.

3. Fixed in check.sh — the three mirrored CI jobs share one 10 GB disk here

Once bun was pinned, tests/authored-node-runtime.test.ts ran for the first
time and died with

Error: ENOSPC: no space left on device, mkdtemp '/tmp/authored-node-runtime-XXXXXX'

Each of its seventeen fixtures dereferences a ~145 MB @relayflows/surface
copy into $TMPDIR and keeps it until the file's afterAll, so the suite needs
~2.5 GB at peak. In CI the artifact, schema and surface workflows are separate
jobs on separate runner disks; check.sh runs them back to back on one 10 GB
filesystem, where cargo test --workspace has already left a ~3.6 GB debug
target. check.sh now drops kernel/target/debug after the kernel tests —
nothing below that point uses it, since every later step reaches relayflowd
through RELAYFLOWD_BIN, which names the release build.

4. NOT fixed — unprivileged user/network namespaces are unavailable here

Twenty-four cases across tests/hosted-extension-isolation.test.ts,
tests/hosted-extension-protocol.test.ts,
tests/babysitter-native-extension.test.ts and
tests/software-garden-babysitter-composition.test.ts fail with:

Error: Hosted extension sandbox exited without a valid completion (exit 1):
bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted

This is the container, not the change. bubblewrap 0.12.0 is installed, but this
process has no effective capabilities and the kernel refuses the uid map:

$ cat /proc/self/status | grep Cap
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 000001ffffffffff
$ unshare -Urn ip link show
unshare: write failed /proc/self/uid_map: Operation not permitted
$ bwrap --unshare-user --unshare-net --ro-bind / / /bin/true
bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted
$ sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
sysctl: permission denied on key "kernel.apparmor_restrict_unprivileged_userns"

check.sh already attempts that sysctl best-effort and already prints
"bwrap smoke failed; unprivileged user namespaces look unavailable" before the
suites run. The sandbox cases exercise the exact production namespace command
and must not be weakened to pass here; they need a host that permits
unprivileged user namespaces. Nothing on this branch touches the sandbox.

Fixes #534


Summary by cubic

Adds --detach to flows run/resume so local agent runs and authored TypeScript flow bodies survive closing the invoking terminal. Previously, closing a terminal killed --local-agent and authored bodies; now the CLI spawns the run into its own session, prints a run handle (run ID, child PID, log and receipt paths, optional observer URL), and exits, with flows status <run-id> available for follow-up. Also fixes --detach in the standalone single-file flows binary, which previously failed the detach handshake by spawning the non-existent /$bunfs/root/cli.js path as an extra positional.

Behavior

  • The parent's exit 0 confirms admission only; the run may still fail, park (exit 3 in the child), or suspend. Logs and receipts persist under <data-dir>/detached/run-*/.
  • Admission is explicit — only a run.started event or the authored-root run receipt counts, so a resume refusal naming the requested run stays a refusal with its original exit code, and resuming an already-completed authored root still detaches.
  • Preflight runs once in the child; startup refusals return their original diagnostics and exit code. Startup is bounded at 60 seconds, and the observer URL wait at 2 seconds.
  • --detach is refused with --cloud, on check, when repeated, and when FLOWS_LOCAL_AGENT_ENV_FD is set (exit 2). This is process detachment only — no automatic restart supervision.

[!NOTE]
Medium Risk
Touches local run/resume admission and child-process lifecycle; mistakes could admit runs incorrectly or strand journals, but scope is CLI-only with no kernel or Cloud path changes.

Overview
Adds flows run / flows resume --detach so local runs with --local-agent (and authored .flow.ts bodies) keep running after the invoking terminal closes. The parent spawns a detached child that re-invokes the CLI without --detach, polls an atomic record.json under <data-dir>/detached/run-*/, and exits 0 only once the daemon admits the run; pre-admission failures surface as the child's normal report and exit code instead of a fake detached handle.

Implementation: New detached-run / detached-record modules handle spawn, 60s startup timeout (SIGTERM on timeout), 2s observer URL grace, and receipt phases (started / finished / refused). The foreground CLI wires onRunReceipt / journal run.spawned into the child receipt; observer minting can publish late URLs via callback. --detach is refused with --cloud, on check, when duplicated, and when FLOWS_LOCAL_AGENT_ENV_FD is set. Park remedies and docs/SURFACE.md document semantics, logs, and follow-up via flows status.

The standalone compiled flows binary is fixed to takeDetachedReceipt before authored code loads and to re-exec via selfCommand: [process.execPath] instead of a virtual Bun entry path.

Reviewed by Cursor Bugbot for commit 7b962d475117d53f93171d6a2d757743843aee1e. Bugbot is set up for automated code reviews on this repo. Configure here.