diff --git a/.github/workflows/runloop-blueprint.yml b/.github/workflows/runloop-blueprint.yml new file mode 100644 index 0000000..5e1323e --- /dev/null +++ b/.github/workflows/runloop-blueprint.yml @@ -0,0 +1,42 @@ +# Manual paid Runloop Blueprint build for C3B. +# workflow_dispatch only. Burns trial credits. Do not add to PR CI. +# Equivalent to: bash blueprints/runloop-interactive/build.sh +# RUNLOOP_API_KEY stays in GitHub secrets; never in git, guest env, or logs. +name: runloop-blueprint + +on: + workflow_dispatch: + +concurrency: + group: runloop-blueprint + cancel-in-progress: false + +permissions: + contents: read + +jobs: + build-interactive-blueprint: + name: Build interactive Blueprint + runs-on: ubuntu-24.04 + timeout-minutes: 30 + env: + RUNLOOP_API_KEY: ${{ secrets.RUNLOOP_API_KEY }} + FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT: flok-runloop-interactive + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 + with: + node-version: "22.16" + cache: npm + - run: npm ci + - name: Verify Runloop configuration + run: | + if [ -z "$RUNLOOP_API_KEY" ]; then + echo "RUNLOOP_API_KEY is missing (must FAIL, not skip)" + exit 1 + fi + echo "Blueprint name flok-runloop-interactive" + - name: Build interactive Blueprint + run: bash blueprints/runloop-interactive/build.sh diff --git a/.github/workflows/runloop-c3.yml b/.github/workflows/runloop-c3.yml index 128564d..ffd0367 100644 --- a/.github/workflows/runloop-c3.yml +++ b/.github/workflows/runloop-c3.yml @@ -1,14 +1,29 @@ -# Manual Runloop Devbox live tests (C3A). -# Not on pull_request. Not on push. Burns trial credits. +# Manual Runloop paid jobs. workflow_dispatch only. Burns trial credits. +# Not on pull_request. Not on push. # Do NOT use runloopai/deploy-agent — FLOKS tests Devboxes, not Runloop Agents. # RUNLOOP_API_KEY stays in GitHub secrets; never in git, guest env, or logs. +# +# phase: +# c3a — original C3A live Devbox isolation (default) +# c3b-blueprint — bash blueprints/runloop-interactive/build.sh +# c3b-live — C3B interactive live test (only after build_complete) name: runloop-c3 on: workflow_dispatch: + inputs: + phase: + description: "Which paid Runloop job to run" + required: true + type: choice + default: c3a + options: + - c3a + - c3b-blueprint + - c3b-live concurrency: - group: runloop-c3 + group: runloop-c3-${{ github.event.inputs.phase || 'c3a' }} cancel-in-progress: false permissions: @@ -17,11 +32,12 @@ permissions: jobs: runloop-live: name: Runloop Devbox live test + if: ${{ github.event.inputs.phase == 'c3a' }} runs-on: ubuntu-24.04 timeout-minutes: 20 env: RUNLOOP_API_KEY: ${{ secrets.RUNLOOP_API_KEY }} - FLOK_RUNLOOP_BLUEPRINT: ${{ vars.FLOK_RUNLOOP_BLUEPRINT }} + FLOK_RUNLOOP_BLUEPRINT: ${{ vars.FLOK_RUNLOOP_BLUEPRINT || secrets.FLOK_RUNLOOP_BLUEPRINT }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: @@ -33,11 +49,81 @@ jobs: - run: npm ci - name: Verify Runloop configuration run: | - test -n "$RUNLOOP_API_KEY" - test -n "$FLOK_RUNLOOP_BLUEPRINT" - echo "Runloop blueprint: $FLOK_RUNLOOP_BLUEPRINT" - echo "RUNLOOP_API_KEY length=${#RUNLOOP_API_KEY}" + if [ -z "$RUNLOOP_API_KEY" ]; then + echo "RUNLOOP_API_KEY is missing (must FAIL, not skip)" + exit 1 + fi + if [ -z "$FLOK_RUNLOOP_BLUEPRINT" ]; then + FLOK_RUNLOOP_BLUEPRINT="runloop/universal-ubuntu-24.04-x86_64-dnd" + echo "FLOK_RUNLOOP_BLUEPRINT unset; using documented default" + echo "FLOK_RUNLOOP_BLUEPRINT=$FLOK_RUNLOOP_BLUEPRINT" >> "$GITHUB_ENV" + fi + echo "Runloop blueprint configured" - name: Run Runloop live tests env: FLOK_LIVE_RUNLOOP_TEST: "1" run: npm run test:live:runloop + + build-interactive-blueprint: + name: Build interactive Blueprint + if: ${{ github.event.inputs.phase == 'c3b-blueprint' }} + runs-on: ubuntu-24.04 + timeout-minutes: 30 + env: + RUNLOOP_API_KEY: ${{ secrets.RUNLOOP_API_KEY }} + FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT: flok-runloop-interactive + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 + with: + node-version: "22.16" + cache: npm + - run: npm ci + - name: Verify Runloop configuration + run: | + if [ -z "$RUNLOOP_API_KEY" ]; then + echo "RUNLOOP_API_KEY is missing (must FAIL, not skip)" + exit 1 + fi + echo "Blueprint name flok-runloop-interactive" + - name: Build interactive Blueprint + run: bash blueprints/runloop-interactive/build.sh + + runloop-c3b-live: + name: Runloop interactive live test + if: ${{ github.event.inputs.phase == 'c3b-live' }} + runs-on: ubuntu-24.04 + timeout-minutes: 25 + env: + RUNLOOP_API_KEY: ${{ secrets.RUNLOOP_API_KEY }} + FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT: ${{ vars.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT || secrets.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT }} + FLOK_RUNLOOP_BLUEPRINT: ${{ vars.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT || secrets.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 + with: + node-version: "22.16" + cache: npm + - run: npm ci + - name: Verify Runloop configuration + run: | + if [ -z "$RUNLOOP_API_KEY" ]; then + echo "RUNLOOP_API_KEY is missing (must FAIL, not skip)" + exit 1 + fi + if [ -z "$FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT" ] && [ -z "$FLOK_RUNLOOP_BLUEPRINT" ]; then + FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT="flok-runloop-interactive" + FLOK_RUNLOOP_BLUEPRINT="flok-runloop-interactive" + echo "FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT unset; using documented default" + echo "FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT=$FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT" >> "$GITHUB_ENV" + echo "FLOK_RUNLOOP_BLUEPRINT=$FLOK_RUNLOOP_BLUEPRINT" >> "$GITHUB_ENV" + fi + echo "C3B blueprint configured" + - name: Run C3B live tests + env: + FLOK_LIVE_RUNLOOP_C3B_TEST: "1" + run: npm run test:live:runloop-c3b diff --git a/.github/workflows/runloop-c3b.yml b/.github/workflows/runloop-c3b.yml new file mode 100644 index 0000000..11048c5 --- /dev/null +++ b/.github/workflows/runloop-c3b.yml @@ -0,0 +1,50 @@ +# Manual Runloop C3B interactive live tests. +# workflow_dispatch only. Burns trial credits. Do not add to PR CI. +name: runloop-c3b + +on: + workflow_dispatch: + +concurrency: + group: runloop-c3b + cancel-in-progress: false + +permissions: + contents: read + +jobs: + runloop-c3b-live: + name: Runloop interactive live test + runs-on: ubuntu-24.04 + timeout-minutes: 25 + env: + RUNLOOP_API_KEY: ${{ secrets.RUNLOOP_API_KEY }} + FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT: ${{ vars.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT || secrets.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT }} + FLOK_RUNLOOP_BLUEPRINT: ${{ vars.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT || secrets.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 + with: + node-version: "22.16" + cache: npm + - run: npm ci + - name: Verify Runloop configuration + run: | + if [ -z "$RUNLOOP_API_KEY" ]; then + echo "RUNLOOP_API_KEY is missing (must FAIL, not skip)" + exit 1 + fi + if [ -z "$FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT" ] && [ -z "$FLOK_RUNLOOP_BLUEPRINT" ]; then + FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT="flok-runloop-interactive" + FLOK_RUNLOOP_BLUEPRINT="flok-runloop-interactive" + echo "FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT unset; using documented default" + echo "FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT=$FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT" >> "$GITHUB_ENV" + echo "FLOK_RUNLOOP_BLUEPRINT=$FLOK_RUNLOOP_BLUEPRINT" >> "$GITHUB_ENV" + fi + echo "C3B blueprint configured" + - name: Run C3B live tests + env: + FLOK_LIVE_RUNLOOP_C3B_TEST: "1" + run: npm run test:live:runloop-c3b diff --git a/PHASES.md b/PHASES.md index 898ebff..d0cd14a 100644 --- a/PHASES.md +++ b/PHASES.md @@ -90,40 +90,98 @@ Work only on the currently open phase. Nexus-IQ / AEON / Graphiti are **forbidde --- -## Phase 3 — Runloop production provider ← CURRENT - -**Goal:** Runloop Devboxes as production-v1 ComputerProvider. C3A is the compute substrate. C3B (interactive computer / browser / noVNC) is deferred until C3A is green. - -### Implement (C3A) -- `providers/runloop.ts` — RunloopProvider (`name: "runloop"`) -- Official `@runloop/api-client` **1.28.0** via `RunloopSDK` (not the legacy client, not `runloopai/deploy-agent`) -- Default blueprint `FLOK_RUNLOOP_BLUEPRINT=runloop/universal-ubuntu-24.04-x86_64-dnd`, arch `x86_64` -- Workspace jail `/home/user/flok` -- `RUNLOOP_API_KEY` stays in the control plane; never in Devbox env, workspace, exec env, MCP, or audit -- argv[] exec (shell mode rejected) via JSON/base64 → Python `os.execvp` wrapper -- `pause` → `suspend` + `awaitSuspended` (disk preserved, **not** RAM) -- `wake` → `resume` + `awaitRunning` -- `stop` / `destroy` → idempotent `shutdown` -- `checkpoint` → `snapshotDisk`; `restore` creates a new Devbox from the snapshot (forks allowed) -- Non-live tests with an injected control-plane fake (zero network) -- Live tests opt-in: `FLOK_LIVE_RUNLOOP_TEST=1` — missing key/blueprint **FAILS**, never silent skip - -### C3B (do not implement until C3A is green) -Chromium + display + noVNC / Browserbase-or-Kernel. Authenticated Runloop tunnels later. Do not add those dependencies now. +## Phase 3 — Runloop production provider -### Non-goals (do not invent) -- Provider factory, MCP gateway, pairing/capability wiring, worker, Kysely, Nexus/AEON/Graphiti -- Daytona (removed from the active C3 path) +**Goal:** Runloop Devboxes as production-v1 ComputerProvider. C3A is the compute substrate. C3B is the interactive computer / browser layer. -### Gate C3A +### Gate C3A — Runloop compute substrate Two live Devboxes prove different provider IDs, isolated filesystems, distinct `boot_id`, independent lifecycle, suspend/resume disk persist, snapshot → forked Devbox with independent mutation, and shutdown of every paid Devbox. Live tests are **opt-in only** (`FLOK_LIVE_RUNLOOP_TEST=1`, manual `runloop-c3` workflow). Not part of required PR CI. -**Status:** OPEN +**Status:** CLOSED / PASSED (2026-08-22) + +**Evidence** +- Merge: [Adaptive-Liquidity/floks-pc#4](https://github.com/Adaptive-Liquidity/floks-pc/pull/4) → `292d4ff5c1a3dd569d581e4c5ac13c4562f7d454` +- Integrated provider head: `bf9e6c1252236a5b5a2ff5a75321aed469d7284d` +- Live workflow: `runloop-c3` run `32554469979` on `ubuntu-24.04` / Node 22.16 + - URL: https://github.com/Adaptive-Liquidity/floks-pc/actions/runs/32554469979 + - Job `Runloop Devbox live test` (`96986381570`): **success** + - SHA under test: `6cad86630621b287400993b48403fec2dbccccc8` (workflow-only follow-up; provider code identical to `bf9e6c1`) + - `FLOK_LIVE_RUNLOOP_TEST=1 npm run test:live:runloop` + - 6 pass / 0 fail / 0 skip + - Live case `two live Devboxes prove isolation, suspend, snapshot fork, then shutdown` **21.2s, not skipped** +- Live proofs encoded in `tests/providers/runloop.isolation.test.ts` (all asserted; suite would fail otherwise): + - Devbox A + B created; provider IDs distinct + - A writes `/home/user/flok/A.txt`; B cannot read it; B writes its own file + - distinct `/proc/sys/kernel/random/boot_id` + - suspend A; B remains running + - resume A; A disk file survives + - snapshot A; Devbox C restored/forked; C inherits `A.txt`; mutate C does not change A + - `finally` destroy C, B, A (shutdown) +- Isolation: zero writes under Floks-main +- Nexus / graph flags remain false -### Gate C3B -Deferred. Interactive computer layer after C3A PASS. +**Known limitations (not C3A blockers)** +- Independent Runloop inventory of leftover Devboxes was not queried from this session (no control-plane key here). Cleanup is the live test `finally` destroy path, which ran because the test passed. +- Runloop `optimistic_timeout` first-wait hint remains capped at 25s; `timedOut` is reported when exitCode is null. +- `vars.FLOK_RUNLOOP_BLUEPRINT` was empty; the passing live run resolved the blueprint from a repository **secret** of the same name. + +### Gate C3B — interactive computer + +**Goal:** Browser + private display + screenshot + bounded input + persistent profile inside each Runloop Devbox. Grok remains the intelligence. Runloop remains the computer. + +### Implement +- Reproducible interactive Blueprint under `blueprints/runloop-interactive/` based on `FROM runloop:runloop/universal-ubuntu-24.04-x86_64-dnd` +- Graphical stack as non-root `flok-ui`; Chrome without `--no-sandbox` +- Persistent profile `/home/user/flok/.browser/profile` +- `ensureInteractiveStack()` after provision, restore, and resume (disk survives suspend; RAM/processes do not) +- `observe()` screenshot from `:99` +- Bounded `act()`: click_coordinates, type, key, scroll, open_url, wait; `click_element` fail-closed +- Local noVNC on localhost only; `takeover()` remains fail-closed; `vnc: false` +- `computerUse: true` after paid C3B live gate; `accessibility: false`, `vnc: false`, `pauseMemory: false` +- Manual `runloop-c3` phase `c3b-live` (GitHub only lists workflows that exist on main; `runloop-c3b.yml` is not dispatchable) + +### Non-goals (do not invent) +- Browserbase, Kernel, Runloop Agents, MCP, pairing, Grok Bot connection, Nexus/AEON/Graphiti +- Public VNC URL +- `mode: "shell"` +- Chromium `--no-sandbox` unless a verified Runloop incompatibility is documented + +**Status:** CLOSED / PASSED (2026-08-22) + +**Evidence** +- Interactive Blueprint: `bpt_34BQTBwmrCLxEQkEMjQKm` / `flok-runloop-interactive` `build_complete` (~106s). Workflow run `32557645663`. +- Prior FAIL (not closeable): `runloop-c3` phase `c3b-live` run [`32557742597`](https://github.com/Adaptive-Liquidity/floks-pc/actions/runs/32557742597) job `96994511350` SHA `3efa97f` — empty Chrome profile 2.5s after `open_url` Popen. Unpaid fix `b892978` added bounded `pollUntilChromeReady` (~20s / 500ms) with classified failures. +- Live: `runloop-c3` phase `c3b-live` run [`32559415086`](https://github.com/Adaptive-Liquidity/floks-pc/actions/runs/32559415086) job `96998605345` SHA `b892978a575b273631e32018d61467884ef04124` + - URL: https://github.com/Adaptive-Liquidity/floks-pc/actions/runs/32559415086 + - Job `Runloop interactive live test`: **success** + - `FLOK_LIVE_RUNLOOP_C3B_TEST=1 npm run test:live:runloop-c3b` + - 1 pass / 0 fail / 0 skip + - Live case `one Devbox: stack, fixture, observe, input, profile, suspend/resume, local noVNC, cleanup` **64.1s, not skipped** + - Chrome 151.0.7922.173; blueprint default `flok-runloop-interactive` +- Live proofs encoded in `tests/live/runloop.c3b.live.test.ts` (all asserted; suite would fail otherwise): + - Chrome readiness / profile initialization (`pollUntilChromeReady`, `requireProfile: true`; `filesystem.list` of `/home/user/flok/.browser/profile` non-empty) + - Real screenshot (`observe` 1440×900 PNG IHDR; no accessibility fabrications) + - `click_coordinates` / `type` / `key` / `scroll` all `success: true` + - Localhost noVNC (`http://127.0.0.1:6080/`) + - No public VNC (ports 5900 and 6080 listen on loopback only) + - Suspend / resume: profile marker `c3b-marker` survived disk suspend + - Graphical stack recovery: Xvfb `:99` + Openbox + Chrome-ready after resume; screenshot after resume + - Profile persistence: Chrome `--user-data-dir=/home/user/flok/.browser/profile` without `--no-sandbox` + - Devbox cleanup: `finally` `destroy` (no `destroy failed` log) +- Isolation: zero writes under Floks-main +- Nexus / graph flags remain false +- Capabilities after this close: `computerUse: true`, `accessibility: false`, `vnc: false`, `pauseMemory: false` +- C4 is **not started**. Do not begin pairing / MCP / Grok Bot work until explicit approval. + +**Known limitations (not C3B close criteria)** +- Chrome `.deb` is unpinned stable (151.0.7922.173 recorded live) +- Horizontal scroll not implemented +- WM chrome can offset clicks +- Memory-plane screenshots stub a 1×1 PNG advertised as 1440×900 +- `/run/user/1500` is tmpfs and is recreated after resume +- If user namespaces are disabled **and** `chrome-sandbox` sits on a `nosuid` mount, live Chrome will fail closed. That evidence is required before considering `--no-sandbox`, which remains forbidden. --- @@ -131,6 +189,8 @@ Deferred. Interactive computer layer after C3A PASS. **Goal:** Pair codes + capability tokens. MCP auth alone cannot identify a Bot. +**Status:** NOT STARTED. Do not begin C4 until explicit approval. + ### Gate C4 Valid NOEMA capability cannot access Code’s machine even when both Bots share the same account-level MCP connection. diff --git a/README.md b/README.md index 0f5c759..dd4302f 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,9 @@ All computer domain logic, providers, migrations, worker, and MCP surface live h | **C0** — scaffold, authority, domain types, ComputerProvider interface | **CLOSED / PASSED** | | **C1** — domain logic, FakeProvider, ComputerService, unit tests | **CLOSED / PASSED** | | **C2** — DockerDevProvider + isolation/persistence | **CLOSED / PASSED** | -| **C3** — Runloop Devbox provider (C3A compute substrate) | CURRENT | -| C4+ | Not started (Nexus-IQ remains hard-locked until G0) | +| **C3A** — Runloop Devbox compute substrate | **CLOSED / PASSED** | +| **C3B** — interactive computer (display, browser, screenshot, bounded input) | **CLOSED / PASSED** | +| C4+ | Not started (do not begin C4; Nexus-IQ remains hard-locked until G0) | ## Authority files (read in this order) @@ -60,23 +61,26 @@ FLOK_LIVE_RUNLOOP_TEST=1 npm run test:live:runloop When the flag is set, missing credentials **fail** the suite. Live Runloop is **not** part of `npm run verify` or required PR CI. +C3B interactive live tests are a separate manual workflow (`runloop-c3` phase `c3b-live`; `runloop-c3b.yml` is not registered on main). They need the custom interactive Blueprint (built from the DnD base; not bare Ubuntu) and `FLOK_LIVE_RUNLOOP_C3B_TEST=1`. Paid live gate **passed** on run `32559415086` (SHA `b892978`). `computerUse` is **true**. `accessibility` / `vnc` / `pauseMemory` stay **false**. + Store production credentials in GitHub Actions only (never in git, `.env` committed to the repo, or a Node VM / Devbox): | Name | Where | Purpose | |------|--------|---------| | `RUNLOOP_API_KEY` | Repository **secret** | Runloop control-plane API key | -| `FLOK_RUNLOOP_BLUEPRINT` | Repository **variable** | Blueprint name (e.g. `runloop/universal-ubuntu-24.04-x86_64-dnd`) | +| `FLOK_RUNLOOP_BLUEPRINT` | Repository **variable or secret** | C3A generic Ubuntu Blueprint name | +| `FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT` | Repository **variable or secret** | C3B interactive Blueprint name (after `bash blueprints/runloop-interactive/build.sh`) | + +GitHub **Variables** and **Secrets** are different stores. A value saved only as a Secret is not visible as `vars.FLOK_*`. Workflows accept either. ```text PR ├─ verify ← every PR (free) ├─ docker-c2 ← every PR (GitHub-hosted Docker) -└─ runloop-live ← manual workflow_dispatch only - ├─ secrets.RUNLOOP_API_KEY - └─ vars.FLOK_RUNLOOP_BLUEPRINT +└─ runloop-c3 ← manual workflow_dispatch only (phase c3a / c3b-blueprint / c3b-live) ``` -Run it from Actions → **runloop-c3** → Run workflow. Do not use `runloopai/deploy-agent`. Do not make it a required status check. +Run C3A from Actions → **runloop-c3** → Run workflow. Do not use `runloopai/deploy-agent`. Do not make paid jobs required status checks. Never log the API key or its length. Do **not** enable Nexus, AEON, or Graphiti until Gate G0 is marked PASSED in `PHASES.md`. diff --git a/blueprints/runloop-interactive/Dockerfile b/blueprints/runloop-interactive/Dockerfile new file mode 100644 index 0000000..f782c06 --- /dev/null +++ b/blueprints/runloop-interactive/Dockerfile @@ -0,0 +1,65 @@ +# FLOKS C3B interactive computer image. +# Built as a Runloop Blueprint (paid). Do not build in ordinary CI. +# +# Extends the production-v1 DnD workstation (Docker, Node, Python, Git, …) +# so the graphical layer does not strip compute tooling. +# Runloop DnD profile is root; Chrome/Xvfb/Openbox/x11vnc/noVNC run as +# non-root user `flok-ui` (uid 1500). No --no-sandbox. +# +# Chrome .deb version is recorded at image-build time in /etc/flok-chrome-version +# (not a source pin of a specific .deb; Google's stable channel moves). +FROM runloop:runloop/universal-ubuntu-24.04-x86_64-dnd + +ENV DEBIAN_FRONTEND=noninteractive \ + FLOK_DISPLAY=:99 \ + FLOK_DISPLAY_WIDTH=1440 \ + FLOK_DISPLAY_HEIGHT=900 \ + FLOK_DISPLAY_DEPTH=24 \ + FLOK_UI_USER=flok-ui \ + FLOK_UI_HOME=/home/flok-ui \ + FLOK_UI_UID=1500 + +# Graphical stack only. Do not replace Node/Python/Git/Docker from the DnD base. +RUN apt-get update && apt-get install -y --no-install-recommends \ + ca-certificates curl gnupg procps util-linux \ + xvfb openbox x11vnc novnc websockify \ + xdotool imagemagick dbus-x11 x11-utils \ + fonts-dejavu-core fonts-liberation \ + && mkdir -p /usr/share/keyrings \ + && curl -fsSL https://dl.google.com/linux/linux_signing_key.pub \ + | gpg --dearmor -o /usr/share/keyrings/google-linux.gpg \ + && echo "deb [arch=amd64 signed-by=/usr/share/keyrings/google-linux.gpg] http://dl.google.com/linux/chrome/deb/ stable main" \ + > /etc/apt/sources.list.d/google-chrome.list \ + && apt-get update \ + && apt-get install -y --no-install-recommends google-chrome-stable \ + && ln -sf /usr/bin/google-chrome-stable /usr/local/bin/chromium \ + && ln -sf /usr/bin/google-chrome-stable /usr/local/bin/google-chrome \ + && rm -rf /var/lib/apt/lists/* + +# Dedicated graphical user. Do NOT `USER flok-ui` — DnD stays root for Docker. +# Chrome's setuid sandbox helper is required because we refuse --no-sandbox. +RUN set -euo pipefail \ + && if ! getent group flok-ui >/dev/null; then groupadd -g 1500 flok-ui; fi \ + && if ! id -u flok-ui >/dev/null 2>&1; then \ + useradd -m -u 1500 -g 1500 -d /home/flok-ui -s /bin/bash flok-ui; \ + fi \ + && mkdir -p /home/user/flok/.browser/profile /home/user/flok/.flok /run/user/1500 \ + && chown root:flok-ui /home/user/flok \ + && chmod 775 /home/user/flok \ + && chown -R flok-ui:flok-ui /home/flok-ui /home/user/flok/.browser /run/user/1500 \ + && chown root:root /home/user/flok/.flok \ + && chmod 755 /home/user/flok/.flok \ + && chmod 700 /home/user/flok/.browser /home/user/flok/.browser/profile /run/user/1500 \ + && SANDBOX="$(find /opt/google -name chrome-sandbox -type f 2>/dev/null | head -1 || true)" \ + && test -n "$SANDBOX" \ + && chmod 4755 "$SANDBOX" \ + && ls -l "$SANDBOX" \ + && echo "flok-ui uid=$(id -u flok-ui) home=/home/flok-ui" > /etc/flok-ui-user \ + && google-chrome-stable --version > /etc/flok-chrome-version \ + && command -v docker >/dev/null \ + && command -v git >/dev/null \ + && command -v python3 >/dev/null \ + && command -v node >/dev/null + +# Stay root. Graphical processes drop to flok-ui at runtime via runuser. +WORKDIR /home/user/flok diff --git a/blueprints/runloop-interactive/README.md b/blueprints/runloop-interactive/README.md new file mode 100644 index 0000000..3741e38 --- /dev/null +++ b/blueprints/runloop-interactive/README.md @@ -0,0 +1,81 @@ +# FLOKS interactive Runloop Blueprint (C3B) + +Canonical personal-computer image for a FLOKS Node. The browser runs **inside** +the Devbox. Browserbase and Kernel are not used. + +## Base + +``` +FROM runloop:runloop/universal-ubuntu-24.04-x86_64-dnd +``` + +This keeps the production workstation (Docker-in-Docker, Node, Python, Git). +Runloop's DnD profile is **root**. The graphical stack does **not** run as root. + +| Identity | Role | +|----------|------| +| `root` | DnD / control-plane default | +| `flok-ui` (uid 1500, home `/home/flok-ui`) | Xvfb, Openbox, Chrome, x11vnc, websockify | + +Workspace stays `/home/user/flok` (group `flok-ui`, mode 775). Browser profile +`/home/user/flok/.browser/profile` is `flok-ui:flok-ui` mode 700. + +## Stack + +| Component | Role | +|-----------|------| +| Xvfb `:99` | 1440×900×24 private display (as `flok-ui`) | +| Openbox | minimal window manager (as `flok-ui`) | +| x11vnc | VNC **localhost only** (`-nopw` is acceptable only with `-localhost`) | +| noVNC + websockify | HTTP/WebSocket on `127.0.0.1:6080` | +| xdotool | bounded input via `runuser -u flok-ui` (argv, not shell-concatenated) | +| ImageMagick `import` | screenshots; temp PNG deleted after collection | +| Google Chrome stable | browser as `flok-ui` (sandbox preserved; **no `--no-sandbox`**) | + +Chrome's `.deb` is the distro `stable` channel. The image records +`/etc/flok-chrome-version` at build time; that is evidence, not a source pin. +`chrome-sandbox` is installed setuid (`chmod 4755`). + +## Build (paid, manual) + +```bash +export RUNLOOP_API_KEY=... # never commit +bash blueprints/runloop-interactive/build.sh +``` + +The script **creates**, then **polls** status: + +- continue: `queued`, `provisioning`, `building` +- success only: `build_complete` (prints id / name / status) +- fail: `failed` / `build_failed` (prints build logs) +- fail: timeout (default 1500s; `FLOK_BLUEPRINT_BUILD_TIMEOUT_SEC`) + +Do **not** dispatch `runloop-c3b` while the Blueprint is still queued or building. + +Then set GitHub **secret or variable**: + +``` +FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT=flok-runloop-interactive +``` + +Ordinary `npm test` / `verify` / PR CI must **not** build this Blueprint. + +If Chromium's sandbox is proven incompatible with Runloop Devboxes, document the +failure (exec stderr + kernel/user-ns evidence) before adding `--no-sandbox`. +No such evidence exists yet, so the flag is not used. + +## Suspend/resume + +Runloop suspend keeps **disk**, not RAM. After resume, FLOKS calls +`ensureInteractiveStack()` again (stale Chrome `SingletonLock` is removed when +the `flok-ui` process is gone). Chromium is relaunched as `flok-ui` against the +same profile directory. In-memory tabs are not preserved. + +On the generic C3A Ubuntu Blueprint, `ensureInteractiveStack()` is a no-op +(`ok missing-xvfb`) so compute-only provision still works. + +## Takeover + +Local noVNC is a private endpoint, not a public URL. `takeover()` stays +fail-closed and `vnc` capability stays `false` until authenticated Runloop +tunnels are wired later. Do not use `auth_mode=open`. diff --git a/blueprints/runloop-interactive/build.sh b/blueprints/runloop-interactive/build.sh new file mode 100755 index 0000000..a3fa54a --- /dev/null +++ b/blueprints/runloop-interactive/build.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# Manually create/update the paid Runloop Blueprint. Do NOT run from ordinary CI. +# Requires RUNLOOP_API_KEY. Polls until build_complete, else dumps logs and fails. +# Do not dispatch runloop-c3b while this is still queued/building. +set -euo pipefail +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +cd "$ROOT" +if [ -z "${RUNLOOP_API_KEY:-}" ]; then + echo "RUNLOOP_API_KEY is required" >&2 + exit 1 +fi +export FLOK_BP_NAME="${FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT:-flok-runloop-interactive}" +export FLOK_BLUEPRINT_BUILD_TIMEOUT_SEC="${FLOK_BLUEPRINT_BUILD_TIMEOUT_SEC:-1500}" +export FLOK_BLUEPRINT_POLL_MS="${FLOK_BLUEPRINT_POLL_MS:-5000}" +exec node --experimental-vm-modules node_modules/tsx/dist/cli.mjs \ + blueprints/runloop-interactive/build.ts diff --git a/blueprints/runloop-interactive/build.ts b/blueprints/runloop-interactive/build.ts new file mode 100644 index 0000000..a3cabb4 --- /dev/null +++ b/blueprints/runloop-interactive/build.ts @@ -0,0 +1,91 @@ +/** + * Paid Blueprint builder. Invoked only by build.sh (never npm test / verify / PR CI). + * Creates the Blueprint, then polls until a terminal status. + */ +import { readFileSync } from "node:fs"; +import { RunloopSDK } from "@runloop/api-client"; +import { classifyBlueprintBuildStatus } from "../../src/lib/computers/providers/runloop-interactive.js"; + +const name = process.env.FLOK_BP_NAME; +if (!name) { + throw new Error("FLOK_BP_NAME missing"); +} +if (!process.env.RUNLOOP_API_KEY) { + throw new Error("RUNLOOP_API_KEY is required"); +} + +const timeoutSec = Number(process.env.FLOK_BLUEPRINT_BUILD_TIMEOUT_SEC ?? "1500"); +const pollMs = Number(process.env.FLOK_BLUEPRINT_POLL_MS ?? "5000"); +if (!Number.isFinite(timeoutSec) || timeoutSec < 30) { + throw new Error("FLOK_BLUEPRINT_BUILD_TIMEOUT_SEC must be >= 30"); +} +if (!Number.isFinite(pollMs) || pollMs < 500) { + throw new Error("FLOK_BLUEPRINT_POLL_MS must be >= 500"); +} + +const dockerfile = readFileSync("blueprints/runloop-interactive/Dockerfile", "utf8"); +const sdk = new RunloopSDK({ bearerToken: process.env.RUNLOOP_API_KEY }); + +console.log("creating blueprint", name); +const created = await sdk.api.blueprints.create({ + name, + dockerfile, + metadata: { + "flok.purpose": "c3b-interactive", + "flok.provider": "runloop", + "flok.base": "runloop/universal-ubuntu-24.04-x86_64-dnd", + "flok.ui_user": "flok-ui", + }, + launch_parameters: { architecture: "x86_64" }, +}); + +const id = created.id; +console.log("blueprint id", id); +console.log("name", created.name); +console.log("status", created.status); + +async function dumpLogs(reason: string): Promise { + console.error(`blueprint ${reason}; dumping build logs`); + try { + const logs = await sdk.api.blueprints.logs(id); + for (const line of logs.logs ?? []) { + console.error(`${line.timestamp_ms} ${line.level} ${line.message}`); + } + } catch (e) { + console.error("failed to fetch blueprint logs", e instanceof Error ? e.message : e); + } +} + +const deadline = Date.now() + timeoutSec * 1000; +let info = created; + +while (true) { + const phase = classifyBlueprintBuildStatus(info.status); + if (phase === "success") { + console.log("blueprint id", info.id); + console.log("name", info.name); + console.log("status", info.status); + if (info.failure_reason) console.log("failure_reason", info.failure_reason); + process.exit(0); + } + if (phase === "failure") { + console.error("status", info.status); + if (info.failure_reason) console.error("failure_reason", info.failure_reason); + await dumpLogs("failed"); + process.exit(1); + } + if (Date.now() >= deadline) { + console.error("status", info.status); + await dumpLogs(`timed out after ${timeoutSec}s (still ${info.status})`); + process.exit(1); + } + console.log("waiting", info.status); + await new Promise((r) => setTimeout(r, pollMs)); + try { + info = await sdk.api.blueprints.retrieve(id); + } catch (e) { + console.error("blueprint retrieve failed", e instanceof Error ? e.message : e); + await dumpLogs("retrieve failed"); + process.exit(1); + } +} diff --git a/blueprints/runloop-interactive/ensure-interactive.sh b/blueprints/runloop-interactive/ensure-interactive.sh new file mode 100755 index 0000000..5edb81a --- /dev/null +++ b/blueprints/runloop-interactive/ensure-interactive.sh @@ -0,0 +1,131 @@ +#!/bin/bash +# Idempotent graphical stack for a FLOKS Node Devbox. +# Display :99, Openbox, localhost x11vnc, localhost noVNC — all as flok-ui. +# Does not survive Runloop suspend — call again after resume. +# If Xvfb is not installed (generic C3A Ubuntu Blueprint), exit 0 so +# compute-only provision/resume still works. +set -euo pipefail +export DISPLAY="${FLOK_DISPLAY:-:99}" +WIDTH="${FLOK_DISPLAY_WIDTH:-1440}" +HEIGHT="${FLOK_DISPLAY_HEIGHT:-900}" +DEPTH="${FLOK_DISPLAY_DEPTH:-24}" +PROFILE="${FLOK_BROWSER_PROFILE:-/home/user/flok/.browser/profile}" +RUNDIR="/tmp/flok-interactive" +NOVNC_PORT="${FLOK_NOVNC_PORT:-6080}" +UI_USER="${FLOK_UI_USER:-flok-ui}" +UI_HOME="${FLOK_UI_HOME:-/home/flok-ui}" +UI_UID="${FLOK_UI_UID:-1500}" +XDG_RUNTIME_DIR="/run/user/${UI_UID}" + +mkdir -p "$RUNDIR" "$PROFILE" /home/user/flok/.flok /home/user/flok/.browser + +if ! command -v Xvfb >/dev/null 2>&1; then + echo "ok missing-xvfb profile=$PROFILE" + exit 0 +fi + +if ! id -u "$UI_USER" >/dev/null 2>&1; then + echo "flok-ui user missing; refuse to start Chrome as root" >&2 + exit 1 +fi +if ! command -v runuser >/dev/null 2>&1; then + echo "runuser missing; refuse to start graphical stack as root" >&2 + exit 1 +fi + +mkdir -p "$XDG_RUNTIME_DIR" /tmp/.X11-unix +chown "$UI_USER:$UI_USER" "$XDG_RUNTIME_DIR" +chmod 700 "$XDG_RUNTIME_DIR" +chmod 1777 /tmp/.X11-unix || true +chown "$UI_USER:$UI_USER" "$RUNDIR" || true +# Root-executed helpers live in .flok; never hand that directory to flok-ui. +chown root:root /home/user/flok/.flok +chmod 755 /home/user/flok/.flok +if [ -f /home/user/flok/.flok/execvp.py ]; then + chown root:root /home/user/flok/.flok/execvp.py + chmod 755 /home/user/flok/.flok/execvp.py +fi +if [ -f /home/user/flok/.flok/ensure-interactive.sh ]; then + chown root:root /home/user/flok/.flok/ensure-interactive.sh + chmod 755 /home/user/flok/.flok/ensure-interactive.sh +fi +if [ -f /home/user/flok/.flok/fixture.html ]; then + chown root:root /home/user/flok/.flok/fixture.html + chmod 644 /home/user/flok/.flok/fixture.html +fi +chown -R "$UI_USER:$UI_USER" /home/user/flok/.browser +chmod 700 /home/user/flok/.browser +chmod 700 "$PROFILE" || true +chmod 775 /home/user/flok || true +if [ -L /tmp/flok-chrome.log ] || { [ -e /tmp/flok-chrome.log ] && [ ! -f /tmp/flok-chrome.log ]; }; then + echo "refusing to use /tmp/flok-chrome.log: not a regular file" >&2 + ls -ld /tmp/flok-chrome.log >&2 + exit 1 +fi +touch /tmp/flok-chrome.log +if [ -L /tmp/flok-chrome.log ]; then + echo "refusing to use /tmp/flok-chrome.log: not a regular file" >&2 + exit 1 +fi +chown --no-dereference "$UI_USER:$UI_USER" /tmp/flok-chrome.log +chmod 640 /tmp/flok-chrome.log +if ! runuser -u "$UI_USER" -- test -w "$PROFILE"; then + echo "profile not writable by $UI_USER: $PROFILE" >&2 + ls -ld "$PROFILE" /home/user/flok/.browser /home/user/flok >&2 + exit 1 +fi + +alive() { + local pf="$1" + if [ -f "$pf" ]; then + local pid + pid="$(cat "$pf")" + if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then + return 0 + fi + fi + return 1 +} + +start_ui() { + local name="$1" + shift + local pidfile="$RUNDIR/${name}.pid" + local logfile="/tmp/flok-${name}.log" + local pid + pid="$( + runuser -u "$UI_USER" -- env \ + DISPLAY="$DISPLAY" \ + HOME="$UI_HOME" \ + XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR" \ + sh -c 'log="$1"; shift; nohup "$@" >>"$log" 2>&1 & echo $!' sh "$logfile" "$@" + )" + echo "$pid" > "$pidfile" +} + +# Suspend kills Chrome but leaves SingletonLock on disk. +if ! pgrep -u "$UI_USER" -f -- "--user-data-dir=${PROFILE}" >/dev/null 2>&1; then + rm -f "$PROFILE/SingletonLock" "$PROFILE/SingletonSocket" "$PROFILE/SingletonCookie" +fi + +if ! alive "$RUNDIR/xvfb.pid"; then + rm -f /tmp/.X11-unix/X99 /tmp/.X99-lock + start_ui xvfb Xvfb "$DISPLAY" -screen 0 "${WIDTH}x${HEIGHT}x${DEPTH}" -nolisten tcp + sleep 0.4 +fi +if ! alive "$RUNDIR/openbox.pid"; then + start_ui openbox openbox +fi +if command -v x11vnc >/dev/null 2>&1 && ! alive "$RUNDIR/x11vnc.pid"; then + start_ui x11vnc x11vnc -display "$DISPLAY" -localhost -nopw -forever -shared -rfbport 5900 +fi +if command -v websockify >/dev/null 2>&1 && ! alive "$RUNDIR/novnc.pid"; then + WEB="" + for d in /usr/share/novnc /usr/share/novnc/utils; do + if [ -d "$d" ]; then WEB="$d"; break; fi + done + if [ -n "$WEB" ]; then + start_ui novnc websockify --web "$WEB" "127.0.0.1:${NOVNC_PORT}" 127.0.0.1:5900 + fi +fi +echo "ok display=$DISPLAY profile=$PROFILE ui=$UI_USER" diff --git a/blueprints/runloop-interactive/fixture.html b/blueprints/runloop-interactive/fixture.html new file mode 100644 index 0000000..074d375 --- /dev/null +++ b/blueprints/runloop-interactive/fixture.html @@ -0,0 +1,24 @@ + + + + + FLOKS C3B fixture + + + +
click-me
+
idle
+ + + diff --git a/docs/computers/ARCHITECTURE.md b/docs/computers/ARCHITECTURE.md index bbbf480..e70c180 100644 --- a/docs/computers/ARCHITECTURE.md +++ b/docs/computers/ARCHITECTURE.md @@ -16,6 +16,7 @@ Flok Node Runtime (this package) │ ├─ FakeProvider (unit / contract tests) │ ├─ DockerDevProvider (local integration only) │ ├─ RunloopProvider (production v1 — Devboxes) + │ │ filesystem, terminal, private display, in-Devbox browser │ └─ KataProvider (self-host, Phase 13) ├─ MCP Gateway (eight tools) ├─ Checkpoints → S3-compatible object storage @@ -30,7 +31,7 @@ This runtime owns the isolated-computer substrate. After Gate G0 it can be impor ## Control plane vs compute plane - **Control plane** (this package): pairing, capabilities, jobs, policies, audit, MCP endpoint. -- **Compute plane**: one VM (or later microVM) per Node. Filesystem, processes, browser, VNC live only there. +- **Compute plane**: one VM (or later microVM) per Node. Filesystem, processes, browser, and the private display live only there. C3B extends `runloop/universal-ubuntu-24.04-x86_64-dnd` so Docker/Node/Python/Git stay available; Chrome and the X stack run as `flok-ui`, not root. Grok remains the intelligence; Runloop is not the agent runtime. No route or MCP tool may call a concrete provider. Everything goes through `ComputerService` → `ComputerProvider`. diff --git a/docs/computers/PROVIDER_CONTRACT.md b/docs/computers/PROVIDER_CONTRACT.md index ea9c32e..e0a0095 100644 --- a/docs/computers/PROVIDER_CONTRACT.md +++ b/docs/computers/PROVIDER_CONTRACT.md @@ -72,18 +72,23 @@ interface ProviderCapabilities { | Runloop | Production v1 | Devbox (C3A) | Disk only | Yes | Official `@runloop/api-client` 1.28.0 | | Kata | Self-host (Phase 13) | Kata + Firecracker | Yes | Yes | High density | -## Runloop notes (Phase 3 / C3A) +## Runloop notes (Phase 3) - Package: `@runloop/api-client@1.28.0` via **RunloopSDK** (not the legacy client). -- Default blueprint: `runloop/universal-ubuntu-24.04-x86_64-dnd`, architecture `x86_64`. +- C3A default blueprint: `runloop/universal-ubuntu-24.04-x86_64-dnd`, architecture `x86_64`. +- C3B interactive blueprint: source under `blueprints/runloop-interactive/`. Base `FROM runloop:runloop/universal-ubuntu-24.04-x86_64-dnd` so Docker/Node/Python/Git remain. Graphical stack (Xvfb `:99` 1440×900×24, Openbox, Chrome, xdotool, ImageMagick `import`, localhost x11vnc/noVNC) runs as non-root `flok-ui` (uid 1500). Browserbase and Kernel are not used. - Workspace jail: `/home/user/flok`. -- `pause` is `suspend` — **disk** is preserved, in-memory process state is not (`pauseMemory: false`). +- `pause` is `suspend` — **disk** is preserved, in-memory process state is not (`pauseMemory: false`). Graphical daemons and Chromium must be restarted after provision, restore, and resume via `ensureInteractiveStack()`. - `checkpoint` is `snapshotDisk`; restore creates a **new** Devbox from that snapshot (forks supported). - argv exec: serialize `{argv,cwd,env}` JSON → base64 → Python `os.execvp`. `mode: "shell"` rejected. -- Native Computer Use / VNC / accessibility are **C3B**, not claimed on C3A. +- C3B `observe()` screenshots the private display (PNG, temp file deleted). Accessibility is not implemented (`accessibility: false`). +- C3B `act()` supports `click_coordinates`, `type`, `key`, `scroll`, `open_url`, `wait`, and allowlisted `launch_application` (browser). `click_element` is fail-closed. +- `takeover()` stays fail-closed. Local noVNC is bound to `127.0.0.1:6080` only. Do not advertise `vnc: true` until authenticated tunnels exist. Never use `auth_mode=open`. +- Capabilities: `computerUse: true` after paid C3B live gate `32559415086`; `accessibility: false`, `vnc: false`, `pauseMemory: false`. - Auth: `RUNLOOP_API_KEY`. Never place the key inside a Devbox, exec env, log, or MCP response. - Do **not** use `runloopai/deploy-agent`. C3 tests Devboxes, not Runloop Agents. - Live lifetime: `keep_alive_time_seconds=900`. Do not combine with `lifecycle.after_idle`. +- Chromium sandbox is preserved (no `--no-sandbox`) unless a verified Runloop incompatibility is documented. ## MCP notes (for Phase 5) diff --git a/docs/computers/SECURITY.md b/docs/computers/SECURITY.md index 9ef83b1..1b79d1f 100644 --- a/docs/computers/SECURITY.md +++ b/docs/computers/SECURITY.md @@ -5,8 +5,9 @@ 1. **Private by default, explicit sharing by design.** 2. **Capability-based access.** Account-level MCP authentication cannot identify which Grok Bot is calling (xAI team deployments share MCP auth). Pairing + capability tokens are mandatory. 3. **Fail closed.** Illegal state transitions, expired codes, revoked capabilities, path escapes, and cross-Node access all reject. -4. **No secrets in the guest.** Provider API keys, long-lived credentials, and Flok capability tokens never enter a Node VM or appear in MCP responses / audit content. -5. **Metadata-only audit.** Terminal output, screenshots, cookies, and page contents are not persisted by default. +4. **No secrets in the guest.** Provider API keys (`RUNLOOP_API_KEY`), long-lived credentials, and Flok capability tokens never enter a Node VM or appear in MCP responses / audit content. +5. **Metadata-only audit.** Terminal output, screenshots, cookies, and page contents are not persisted by default. C3B temporary screenshot files are deleted after collection. +6. **Browser profiles are Node-private.** `/home/user/flok/.browser/profile` is guest state in the workspace jail. Never copy it between Nodes. Never put control-plane secrets in it. ## Pairing @@ -52,13 +53,25 @@ Never automatically transferred: browser profiles, cookies, SSH keys, `.env`, cr ## Takeover (VNC) -- Single-use, short-lived signed URL. -- Never exposes provider credentials. -- Expires quickly. +C3B installs localhost-only x11vnc + noVNC (`127.0.0.1:6080`). That is **not** a public takeover URL. + +- `takeover()` stays fail-closed until an authenticated Runloop tunnel exists. +- `vnc` capability stays `false` until that contract is actually satisfied. +- Never use `auth_mode=open` as the production takeover mechanism. +- Tunnel URLs/credentials are sensitive; do not log them. + +C3B Chrome runs as dedicated non-root user `flok-ui` (uid 1500). The DnD +Devbox remains root so Docker-in-Docker still works. `--no-sandbox` is not +used. Browser profile `/home/user/flok/.browser/profile` is `700` and owned by +`flok-ui`. x11vnc/noVNC bind `127.0.0.1` only. + +Later (C7): single-use, short-lived signed URL. Never exposes provider credentials. ## Provider secrets -`DAYTONA_API_KEY` (and future Kata/Firecracker credentials) live only in the control-plane environment. +`RUNLOOP_API_KEY` (and future Kata/Firecracker credentials) live only in the control-plane environment. +Never print the key or metadata about it (including length) in CI logs. + Secret scanning (Gate C10) must prove they never appear in: - Node environment diff --git a/package.json b/package.json index 11d255e..5d96dca 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "test:live": "node --experimental-vm-modules node_modules/tsx/dist/cli.mjs --test tests/live/**/*.test.ts", "test:live:docker": "node --experimental-vm-modules node_modules/tsx/dist/cli.mjs --test tests/providers/docker-dev.isolation.test.ts", "test:live:runloop": "node --experimental-vm-modules node_modules/tsx/dist/cli.mjs --test tests/providers/runloop.isolation.test.ts tests/live/runloop.live.test.ts", + "test:live:runloop-c3b": "node --experimental-vm-modules node_modules/tsx/dist/cli.mjs --test tests/live/runloop.c3b.live.test.ts", "build": "tsc", "verify": "npm run typecheck && npm test && npm run build" }, diff --git a/src/lib/computers/providers/runloop-client.ts b/src/lib/computers/providers/runloop-client.ts index 18a2392..560c5da 100644 --- a/src/lib/computers/providers/runloop-client.ts +++ b/src/lib/computers/providers/runloop-client.ts @@ -6,6 +6,8 @@ * RUNLOOP_API_KEY must never appear in create env, exec env, or guest files. */ +import type { Action } from "../types.js"; + export const RUNLOOP_WORKSPACE_ROOT = "/home/user/flok"; export const RUNLOOP_PROVIDER_NAME = "runloop" as const; export const DEFAULT_RUNLOOP_BLUEPRINT = @@ -88,6 +90,12 @@ export interface RunloopDevboxSession { fsCopy(from: string, to: string): Promise; snapshotDisk(name: string): Promise; + + /** C3B: start or no-op restart of display/WM/VNC. Idempotent. */ + ensureInteractiveStack(): Promise; + screenshot(): Promise<{ width: number; height: number; png: Buffer; activeWindow?: string }>; + novncLocalOk(): Promise; + uiAction(action: Action): Promise; } export interface RunloopControlPlane { diff --git a/src/lib/computers/providers/runloop-interactive.ts b/src/lib/computers/providers/runloop-interactive.ts new file mode 100644 index 0000000..370884e --- /dev/null +++ b/src/lib/computers/providers/runloop-interactive.ts @@ -0,0 +1,780 @@ +/** + * C3B interactive-computer helpers. + * Pure validation + constants. No network. + * + * Suspend preserves disk, not RAM. Graphical daemons and Chromium must be + * restarted after provision, restore, and resume via ensureInteractiveStack(). + */ + +import { randomUUID } from "node:crypto"; +import { posix as pathPosix } from "node:path"; +import type { Action } from "../types.js"; +import { RUNLOOP_WORKSPACE_ROOT } from "./runloop-client.js"; + +export const FLOK_DISPLAY = ":99"; +export const DISPLAY_WIDTH = 1440; +export const DISPLAY_HEIGHT = 900; +export const DISPLAY_DEPTH = 24; +export const BROWSER_PROFILE_DIR = `${RUNLOOP_WORKSPACE_ROOT}/.browser/profile`; +export const INTERACTIVE_DIR = `${RUNLOOP_WORKSPACE_ROOT}/.flok`; +export const FIXTURE_PATH = `${INTERACTIVE_DIR}/fixture.html`; +/** Unique PNG path under the flok-ui-writable browser dir (not root-locked .flok). */ +export const OBS_SHOT_DIR = `${RUNLOOP_WORKSPACE_ROOT}/.browser`; +export function uniqueObsShotPath(): string { + return `${OBS_SHOT_DIR}/obs-${randomUUID()}.png`; +} +export const ENSURE_SCRIPT_PATH = `${INTERACTIVE_DIR}/ensure-interactive.sh`; +export const LOCAL_NOVNC_URL = "http://127.0.0.1:6080/"; +export const MAX_TYPE_CHARS = 2000; +export const MAX_WAIT_MS = 10_000; +export const DEFAULT_INTERACTIVE_BLUEPRINT = "flok-runloop-interactive"; +/** Non-root user that owns Xvfb/Openbox/Chrome/x11vnc/noVNC. DnD stays root. */ +export const FLOK_UI_USER = "flok-ui"; +export const FLOK_UI_HOME = "/home/flok-ui"; +export const FLOK_UI_UID = 1500; +export const FLOK_UI_XDG_RUNTIME = `/run/user/${FLOK_UI_UID}`; + +export const ALLOWED_APPS = new Set(["browser", "chromium", "chrome", "google-chrome"]); + +export const ALLOWED_KEYS = new Set([ + "Return", + "Tab", + "Escape", + "BackSpace", + "Delete", + "space", + "Up", + "Down", + "Left", + "Right", + "Home", + "End", + "F5", + "ctrl+l", + "ctrl+t", + "ctrl+w", + "ctrl+r", + "ctrl+a", + "ctrl+c", + "ctrl+v", +]); + +const KEY_RE = /^[A-Za-z0-9]$/; + +export function isAllowedKey(key: string): boolean { + return ALLOWED_KEYS.has(key) || KEY_RE.test(key); +} + +export function validateOpenUrl(url: string): string | null { + let parsed: URL; + try { + parsed = new URL(url); + } catch { + return "malformed URL"; + } + if (parsed.protocol === "http:" || parsed.protocol === "https:") return null; + if (parsed.protocol === "file:") { + const path = pathPosix.normalize(decodeURIComponent(parsed.pathname)); + if (path === RUNLOOP_WORKSPACE_ROOT || path.startsWith(`${RUNLOOP_WORKSPACE_ROOT}/`)) { + return null; + } + return "file URL escapes workspace jail"; + } + return `unsupported URL scheme: ${parsed.protocol}`; +} + +/** Returns an error string or null if the action is acceptable. */ +export function validateAction(action: Action): string | null { + switch (action.type) { + case "click_element": + return "click_element unsupported until accessibility addressing exists"; + case "click_coordinates": { + if (typeof action.x !== "number" || typeof action.y !== "number") { + return "click_coordinates requires x and y"; + } + if (!Number.isInteger(action.x) || !Number.isInteger(action.y)) { + return "coordinates must be integers"; + } + if (action.x < 0 || action.y < 0 || action.x >= DISPLAY_WIDTH || action.y >= DISPLAY_HEIGHT) { + return `coordinates out of bounds (0..${DISPLAY_WIDTH - 1}, 0..${DISPLAY_HEIGHT - 1})`; + } + return null; + } + case "type": { + if (typeof action.text !== "string" || action.text.length === 0) { + return "type requires text"; + } + if (action.text.length > MAX_TYPE_CHARS) { + return `type text exceeds ${MAX_TYPE_CHARS} characters`; + } + if (action.text.includes("\0")) return "type text contains NUL"; + return null; + } + case "key": { + if (typeof action.key !== "string" || action.key.length === 0) return "key required"; + if (!isAllowedKey(action.key)) return `key not allowed: ${action.key}`; + return null; + } + case "scroll": { + if (action.x !== undefined && action.x !== 0) { + return "horizontal scroll unsupported"; + } + if (!Number.isInteger(action.y) || action.y === 0) { + return "scroll requires non-zero integer y delta"; + } + return null; + } + case "open_url": { + if (typeof action.url !== "string" || action.url.length === 0) return "open_url requires url"; + return validateOpenUrl(action.url); + } + case "launch_application": { + const app = action.application ?? ""; + if (!ALLOWED_APPS.has(app)) return `application not in allowlist: ${app || "(empty)"}`; + return null; + } + case "wait": { + const ms = action.durationMs ?? 0; + if (!Number.isInteger(ms) || ms <= 0) return "wait requires positive durationMs"; + if (ms > MAX_WAIT_MS) return `wait exceeds ${MAX_WAIT_MS}ms`; + return null; + } + default: + return "unsupported action"; + } +} + +/** Read IHDR width/height from a PNG buffer. Null if not a PNG. */ +export function pngDimensions(png: Buffer): { width: number; height: number } | null { + if (png.length < 24) return null; + if (png[0] !== 0x89 || png[1] !== 0x50 || png[2] !== 0x4e || png[3] !== 0x47) return null; + return { width: png.readUInt32BE(16), height: png.readUInt32BE(20) }; +} + +export type BlueprintBuildPhase = "pending" | "success" | "failure"; + +/** + * Runloop Blueprint `status` is queued | provisioning | building | failed | build_complete. + * Docs also mention build_failed as a failure_reason (and sometimes as a status alias). + */ +export function classifyBlueprintBuildStatus(status: string): BlueprintBuildPhase { + switch (status) { + case "queued": + case "provisioning": + case "building": + return "pending"; + case "build_complete": + return "success"; + case "failed": + case "build_failed": + return "failure"; + default: + return "failure"; + } +} + +/** Prefix argv so a Devbox-root exec drops to flok-ui. No shell concatenation. */ +export function argvAsUiUser(argv: string[]): string[] { + return [ + "runuser", + "-u", + FLOK_UI_USER, + "--", + "env", + `DISPLAY=${FLOK_DISPLAY}`, + `HOME=${FLOK_UI_HOME}`, + `XDG_RUNTIME_DIR=${FLOK_UI_XDG_RUNTIME}`, + ...argv, + ]; +} + +/** Chrome argv as flok-ui. Sandbox preserved: never add --no-sandbox. */ +export function chromeLaunchArgv(url: string): string[] { + return argvAsUiUser([ + "google-chrome-stable", + `--user-data-dir=${BROWSER_PROFILE_DIR}`, + `--window-size=${DISPLAY_WIDTH},${DISPLAY_HEIGHT}`, + "--window-position=0,0", + `--app=${url}`, + "--no-first-run", + "--disable-sync", + ]); +} + +/** Guest-local Chrome startup log. Not an audit artifact; truncated in diagnostics. */ +export const CHROME_LOG_PATH = "/tmp/flok-chrome.log"; +export const CHROME_HOME_FALLBACK_DIR = `${FLOK_UI_HOME}/.config/google-chrome`; +export const CHROME_READY_TIMEOUT_MS = 20_000; +export const CHROME_READY_POLL_MS = 500; +const CHROME_LOG_TAIL_CHARS = 4096; +const PROFILE_TEST_MARKERS = new Set(["c3b-marker", "last-url", "launched"]); + +export type ChromeReadyClass = + | "ready" + | "pending" + | "never_started" + | "started_then_exited" + | "sandbox_disabled" + | "permissions_failure" + | "sandbox_failure" + | "display_failure" + | "profile_redirected" + | "readiness_timeout"; + +export interface ChromeReadyEvidence { + chromeCmdlines: string[]; + profileEntries: string[]; + profileEntriesUi: string[]; + profileUid: number | null; + profileGid: number | null; + profileMode: string | null; + profileWritableByUi: boolean; + browserDirMode: string | null; + workspaceMode: string | null; + sandboxPath: string | null; + sandboxMode: string | null; + sandboxNosuid: boolean | null; + xvfbAlive: boolean; + openboxAlive: boolean; + display: string; + visibleWindows: string[]; + homeFallbackEntries: string[]; + chromeLogTail: string; + logHasSandboxError: boolean; + logHasChromeOutput: boolean; + unprivilegedUserns: string | null; +} + +export interface ChromeReadyResult { + status: ChromeReadyClass; + ready: boolean; + /** Stop polling: success or a classified failure. */ + terminal: boolean; + message: string; +} + +export function chromeHasUserDataDir(cmdline: string): boolean { + return cmdline.includes(`--user-data-dir=${BROWSER_PROFILE_DIR}`); +} + +/** True if the cmdline disables Chrome's sandbox. Never treat this as ready. */ +export function chromeHasNoSandbox(cmdline: string): boolean { + return /(?:^|\s)--no-sandbox(?:\s|$)/.test(cmdline); +} + +export function chromeHasDisableSetuidSandbox(cmdline: string): boolean { + return /(?:^|\s)--disable-setuid-sandbox(?:\s|$)/.test(cmdline); +} + +export function chromeSandboxDisabled(cmdline: string): boolean { + return chromeHasNoSandbox(cmdline) || chromeHasDisableSetuidSandbox(cmdline); +} + +export function chromeProfileHasBrowserState(entries: string[]): boolean { + return entries.some((name) => !PROFILE_TEST_MARKERS.has(name) && !name.startsWith(".")); +} + +export function sanitizeChromeLog(raw: string): string { + const filtered = raw + .split("\n") + .filter( + (line) => + !/api[_-]?key|bearer\s+\S+|set-cookie|cookie:|runloop_api|authorization:/i.test(line), + ) + .join("\n"); + return filtered.length <= CHROME_LOG_TAIL_CHARS + ? filtered + : filtered.slice(filtered.length - CHROME_LOG_TAIL_CHARS); +} + +function ourChromeCmdlines(cmdlines: string[]): string[] { + return cmdlines.filter( + (c) => /google-chrome/.test(c) || chromeHasUserDataDir(c), + ); +} + +export function classifyChromeReadiness( + evidence: ChromeReadyEvidence, + opts?: { timedOut?: boolean; requireProfile?: boolean }, +): ChromeReadyResult { + const timedOut = opts?.timedOut === true; + const requireProfile = opts?.requireProfile === true; + const ours = ourChromeCmdlines(evidence.chromeCmdlines); + const alive = ours.length > 0; + const noSandbox = ours.some(chromeSandboxDisabled); + const userData = ours.some(chromeHasUserDataDir); + const profileState = + chromeProfileHasBrowserState(evidence.profileEntries) || + chromeProfileHasBrowserState(evidence.profileEntriesUi); + const windowUp = evidence.visibleWindows.length > 0; + const fallback = evidence.homeFallbackEntries.length > 0; + + if (noSandbox) { + return { + status: "sandbox_disabled", + ready: false, + terminal: true, + message: "Chrome cmdline contains --no-sandbox or --disable-setuid-sandbox; refuse to continue", + }; + } + if (alive && !evidence.profileWritableByUi) { + return { + status: "permissions_failure", + ready: false, + terminal: true, + message: `profile not writable by ${FLOK_UI_USER} (uid ${FLOK_UI_UID} mode=${evidence.profileMode} uid=${evidence.profileUid})`, + }; + } + if (alive && !evidence.xvfbAlive) { + return { + status: "display_failure", + ready: false, + terminal: true, + message: `Xvfb not running on ${evidence.display}`, + }; + } + if (alive && fallback && !profileState && timedOut) { + return { + status: "profile_redirected", + ready: false, + terminal: true, + message: `Chrome wrote ${CHROME_HOME_FALLBACK_DIR} instead of ${BROWSER_PROFILE_DIR}`, + }; + } + if (alive && userData && profileState) { + return { + status: "ready", + ready: true, + terminal: true, + message: "Chrome alive with initialized profile", + }; + } + if (alive && userData && windowUp && !requireProfile) { + return { + status: "ready", + ready: true, + terminal: true, + message: "Chrome alive with a visible window", + }; + } + if (alive && evidence.logHasSandboxError && timedOut) { + return { + status: "sandbox_failure", + ready: false, + terminal: true, + message: "Chrome sandbox/namespace error in startup log; profile never initialized", + }; + } + if (!alive && timedOut) { + if (evidence.logHasChromeOutput) { + return { + status: "started_then_exited", + ready: false, + terminal: true, + message: "Chrome started then exited before becoming ready", + }; + } + return { + status: "never_started", + ready: false, + terminal: true, + message: "no flok-ui Chrome process and no Chrome log output", + }; + } + if (timedOut) { + return { + status: "readiness_timeout", + ready: false, + terminal: true, + message: `Chrome still not ready after ${CHROME_READY_TIMEOUT_MS}ms (alive=${alive} userDataDir=${userData} profileState=${profileState} windows=${windowUp})`, + }; + } + return { + status: "pending", + ready: false, + terminal: false, + message: alive + ? profileState + ? "Chrome alive; waiting for window" + : windowUp || requireProfile + ? "Chrome alive; waiting for profile" + : "Chrome alive; waiting for profile or window" + : "waiting for Chrome process", + }; +} + +export async function pollUntilChromeReady( + probe: () => Promise, + opts?: { + timeoutMs?: number; + intervalMs?: number; + requireProfile?: boolean; + now?: () => number; + sleep?: (ms: number) => Promise; + }, +): Promise<{ result: ChromeReadyResult; evidence: ChromeReadyEvidence }> { + const timeoutMs = opts?.timeoutMs ?? CHROME_READY_TIMEOUT_MS; + const intervalMs = opts?.intervalMs ?? CHROME_READY_POLL_MS; + const requireProfile = opts?.requireProfile === true; + const now = opts?.now ?? Date.now; + const sleep = + opts?.sleep ?? ((ms: number) => new Promise((resolve) => setTimeout(resolve, ms))); + const deadline = now() + timeoutMs; + let evidence = await probe(); + for (;;) { + const timedOut = now() >= deadline; + const result = classifyChromeReadiness(evidence, { timedOut, requireProfile }); + if (result.ready || result.terminal) return { result, evidence }; + await sleep(intervalMs); + evidence = await probe(); + } +} + +function asStringArray(value: unknown): string[] { + if (!Array.isArray(value)) return []; + return value.filter((item): item is string => typeof item === "string"); +} + +function asNullableNumber(value: unknown): number | null { + return typeof value === "number" && Number.isFinite(value) ? value : null; +} + +function asNullableString(value: unknown): string | null { + return typeof value === "string" ? value : null; +} + +function asBoolean(value: unknown, fallback = false): boolean { + return typeof value === "boolean" ? value : fallback; +} + +export function parseChromeReadyEvidence(raw: string): ChromeReadyEvidence { + const parsed: unknown = JSON.parse(raw); + if (typeof parsed !== "object" || parsed === null) { + throw new Error("chrome ready probe did not return an object"); + } + const o = parsed as Record; + return { + chromeCmdlines: asStringArray(o.chromeCmdlines), + profileEntries: asStringArray(o.profileEntries), + profileEntriesUi: asStringArray(o.profileEntriesUi), + profileUid: asNullableNumber(o.profileUid), + profileGid: asNullableNumber(o.profileGid), + profileMode: asNullableString(o.profileMode), + profileWritableByUi: asBoolean(o.profileWritableByUi), + browserDirMode: asNullableString(o.browserDirMode), + workspaceMode: asNullableString(o.workspaceMode), + sandboxPath: asNullableString(o.sandboxPath), + sandboxMode: asNullableString(o.sandboxMode), + sandboxNosuid: typeof o.sandboxNosuid === "boolean" ? o.sandboxNosuid : null, + xvfbAlive: asBoolean(o.xvfbAlive), + openboxAlive: asBoolean(o.openboxAlive), + display: typeof o.display === "string" ? o.display : FLOK_DISPLAY, + visibleWindows: asStringArray(o.visibleWindows), + homeFallbackEntries: asStringArray(o.homeFallbackEntries), + chromeLogTail: sanitizeChromeLog(typeof o.chromeLogTail === "string" ? o.chromeLogTail : ""), + logHasSandboxError: asBoolean(o.logHasSandboxError), + logHasChromeOutput: asBoolean(o.logHasChromeOutput), + unprivilegedUserns: asNullableString(o.unprivilegedUserns), + }; +} + +export function formatChromeReadyFailure( + result: ChromeReadyResult, + evidence: ChromeReadyEvidence, +): string { + const lines = [ + `chrome readiness: ${result.status}`, + result.message, + `display=${evidence.display} xvfb=${evidence.xvfbAlive} openbox=${evidence.openboxAlive}`, + `profile mode=${evidence.profileMode} uid=${evidence.profileUid} gid=${evidence.profileGid} writableByUi=${evidence.profileWritableByUi}`, + `workspace mode=${evidence.workspaceMode} browserDir mode=${evidence.browserDirMode}`, + `sandbox path=${evidence.sandboxPath} mode=${evidence.sandboxMode} nosuid=${evidence.sandboxNosuid} userns_clone=${evidence.unprivilegedUserns}`, + `profile entries: ${evidence.profileEntries.join(",") || "(none)"}`, + `profile entries (flok-ui): ${evidence.profileEntriesUi.join(",") || "(none)"}`, + `home fallback entries: ${evidence.homeFallbackEntries.join(",") || "(none)"}`, + `windows: ${evidence.visibleWindows.join(" | ") || "(none)"}`, + "chrome cmdlines:", + evidence.chromeCmdlines.length ? evidence.chromeCmdlines.map((c) => ` ${c}`).join("\n") : " (none)", + "chrome log tail:", + evidence.chromeLogTail.trim() ? evidence.chromeLogTail.trim() : " (empty)", + ]; + return lines.join("\n"); +} + +/** + * Guest probe: one argv python3 -c. No secrets. Prints JSON on stdout. + * Lists the profile as root and as flok-ui so a mount-namespace split is visible. + */ +export const CHROME_READY_PROBE_PY = [ + "import json,os,subprocess,pathlib", + "UID=1500", + "USER='flok-ui'", + "PROFILE='/home/user/flok/.browser/profile'", + "BROWSER='/home/user/flok/.browser'", + "WS='/home/user/flok'", + "LOG='/tmp/flok-chrome.log'", + "FALLBACK='/home/flok-ui/.config/google-chrome'", + "DISPLAY=':99'", + "def listdir(p):", + " try: return sorted(os.listdir(p))", + " except Exception: return []", + "def mode_of(p):", + " try:", + " st=os.stat(p)", + " return st.st_uid, st.st_gid, format(st.st_mode & 0o7777, 'o')", + " except Exception:", + " return None, None, None", + "def pgrep(pat):", + " try:", + " r=subprocess.run(['pgrep','-u',USER,'-af',pat],capture_output=True,text=True)", + " return [ln for ln in (r.stdout or '').splitlines() if ln.strip() and 'pgrep' not in ln]", + " except Exception:", + " return []", + "def alive(pat):", + " try:", + " r=subprocess.run(['pgrep','-u',USER,'-f',pat],capture_output=True)", + " return r.returncode==0", + " except Exception:", + " return False", + "def runuser_ls(p):", + " try:", + " r=subprocess.run(['runuser','-u',USER,'--','ls','-1',p],capture_output=True,text=True)", + " if r.returncode!=0: return []", + " return [ln for ln in (r.stdout or '').splitlines() if ln.strip()]", + " except Exception:", + " return []", + "def writable(p):", + " try:", + " r=subprocess.run(['runuser','-u',USER,'--','test','-w',p])", + " return r.returncode==0", + " except Exception:", + " return False", + "def windows():", + " out=[]", + " cmds=[", + " ['runuser','-u',USER,'--','env',f'DISPLAY={DISPLAY}','xlsclients','-display',DISPLAY],", + " ['runuser','-u',USER,'--','env',f'DISPLAY={DISPLAY}','xdotool','search','--onlyvisible','--class','Google-chrome'],", + " ]", + " for argv in cmds:", + " try:", + " r=subprocess.run(argv,capture_output=True,text=True,timeout=2)", + " except Exception:", + " continue", + " for ln in (r.stdout or '').splitlines():", + " s=ln.strip()", + " if not s: continue", + " low=s.lower()", + " if 'xlsclients' in argv and ('chrome' not in low and 'chromium' not in low): continue", + " if s not in out: out.append(s)", + " return out", + "def sandbox():", + " path=None", + " for cand in ('/opt/google/chrome/chrome-sandbox','/opt/google/chrome-sandbox'):", + " if os.path.isfile(cand): path=cand; break", + " if not path:", + " for root,dirs,files in os.walk('/opt/google'):", + " if 'chrome-sandbox' in files:", + " path=os.path.join(root,'chrome-sandbox'); break", + " if not path: return None,None,None", + " st=os.stat(path)", + " nosuid=None", + " try:", + " m=subprocess.run(['findmnt','-no','OPTIONS','-T',path],capture_output=True,text=True,timeout=2)", + " nosuid='nosuid' in (m.stdout or '')", + " except Exception:", + " pass", + " return path, format(st.st_mode & 0o7777, 'o'), nosuid", + "def userns():", + " try: return pathlib.Path('/proc/sys/kernel/unprivileged_userns_clone').read_text().strip()", + " except Exception: return None", + "cmd=pgrep('google-chrome')+pgrep('--user-data-dir=/home/user/flok/.browser/profile')", + "seen=set(); cmdlines=[]", + "for ln in cmd:", + " if ln not in seen: seen.add(ln); cmdlines.append(ln)", + "pu,pg,pm=mode_of(PROFILE)", + "_,_,bm=mode_of(BROWSER)", + "_,_,wm=mode_of(WS)", + "sp,sm,sn=sandbox()", + "try: log=pathlib.Path(LOG).read_bytes()[-4096:].decode('utf-8','replace')", + "except Exception: log=''", + "low=log.lower()", + "sand_err=any(s in low for s in ['suid sandbox','chrome-sandbox','new namespace','operation not permitted','zygote_host','no_sandbox','namespace sandbox'])", + "out={", + " 'chromeCmdlines':cmdlines,", + " 'profileEntries':listdir(PROFILE),", + " 'profileEntriesUi':runuser_ls(PROFILE),", + " 'profileUid':pu,'profileGid':pg,'profileMode':pm,", + " 'profileWritableByUi':writable(PROFILE),", + " 'browserDirMode':bm,'workspaceMode':wm,", + " 'sandboxPath':sp,'sandboxMode':sm,'sandboxNosuid':sn,", + " 'xvfbAlive':alive('Xvfb'),'openboxAlive':alive('openbox'),", + " 'display':DISPLAY,", + " 'visibleWindows':windows(),", + " 'homeFallbackEntries':listdir(FALLBACK),", + " 'chromeLogTail':log,", + " 'logHasSandboxError':sand_err,", + " 'logHasChromeOutput':bool(log.strip()),", + " 'unprivilegedUserns':userns(),", + "}", + "print(json.dumps(out))", +].join("\n"); + +/** + * Guest-side stack starter. Written onto the Devbox at ensure time. + * If Xvfb is absent (generic C3A Ubuntu Blueprint) this exits 0 so compute + * provision/resume still succeed. observe/act then fail on screenshot/input. + * Graphical processes run as flok-ui; refuse to start Chrome as root. + */ +export const ENSURE_INTERACTIVE_SH = `#!/bin/bash +set -euo pipefail +export DISPLAY="\${FLOK_DISPLAY:-:99}" +WIDTH="\${FLOK_DISPLAY_WIDTH:-1440}" +HEIGHT="\${FLOK_DISPLAY_HEIGHT:-900}" +DEPTH="\${FLOK_DISPLAY_DEPTH:-24}" +PROFILE="\${FLOK_BROWSER_PROFILE:-/home/user/flok/.browser/profile}" +RUNDIR="/tmp/flok-interactive" +NOVNC_PORT="\${FLOK_NOVNC_PORT:-6080}" +UI_USER="\${FLOK_UI_USER:-${FLOK_UI_USER}}" +UI_HOME="\${FLOK_UI_HOME:-${FLOK_UI_HOME}}" +UI_UID="\${FLOK_UI_UID:-${FLOK_UI_UID}}" +XDG_RUNTIME_DIR="/run/user/\${UI_UID}" + +mkdir -p "$RUNDIR" "$PROFILE" /home/user/flok/.flok /home/user/flok/.browser + +if ! command -v Xvfb >/dev/null 2>&1; then + echo "ok missing-xvfb profile=$PROFILE" + exit 0 +fi + +if ! id -u "$UI_USER" >/dev/null 2>&1; then + echo "flok-ui user missing; refuse to start Chrome as root" >&2 + exit 1 +fi +if ! command -v runuser >/dev/null 2>&1; then + echo "runuser missing; refuse to start graphical stack as root" >&2 + exit 1 +fi + +mkdir -p "$XDG_RUNTIME_DIR" /tmp/.X11-unix +chown "$UI_USER:$UI_USER" "$XDG_RUNTIME_DIR" +chmod 700 "$XDG_RUNTIME_DIR" +chmod 1777 /tmp/.X11-unix || true +chown "$UI_USER:$UI_USER" "$RUNDIR" || true +# Root-executed helpers live in .flok; never hand that directory to flok-ui. +chown root:root /home/user/flok/.flok +chmod 755 /home/user/flok/.flok +if [ -f /home/user/flok/.flok/execvp.py ]; then + chown root:root /home/user/flok/.flok/execvp.py + chmod 755 /home/user/flok/.flok/execvp.py +fi +if [ -f /home/user/flok/.flok/ensure-interactive.sh ]; then + chown root:root /home/user/flok/.flok/ensure-interactive.sh + chmod 755 /home/user/flok/.flok/ensure-interactive.sh +fi +if [ -f /home/user/flok/.flok/fixture.html ]; then + chown root:root /home/user/flok/.flok/fixture.html + chmod 644 /home/user/flok/.flok/fixture.html +fi +chown -R "$UI_USER:$UI_USER" /home/user/flok/.browser +chmod 700 /home/user/flok/.browser +chmod 700 "$PROFILE" || true +chmod 775 /home/user/flok || true +if [ -L /tmp/flok-chrome.log ] || { [ -e /tmp/flok-chrome.log ] && [ ! -f /tmp/flok-chrome.log ]; }; then + echo "refusing to use /tmp/flok-chrome.log: not a regular file" >&2 + ls -ld /tmp/flok-chrome.log >&2 + exit 1 +fi +touch /tmp/flok-chrome.log +if [ -L /tmp/flok-chrome.log ]; then + echo "refusing to use /tmp/flok-chrome.log: not a regular file" >&2 + exit 1 +fi +chown --no-dereference "$UI_USER:$UI_USER" /tmp/flok-chrome.log +chmod 640 /tmp/flok-chrome.log +if ! runuser -u "$UI_USER" -- test -w "$PROFILE"; then + echo "profile not writable by $UI_USER: $PROFILE" >&2 + ls -ld "$PROFILE" /home/user/flok/.browser /home/user/flok >&2 + exit 1 +fi + +alive() { + local pf="$1" + if [ -f "$pf" ]; then + local pid + pid="$(cat "$pf")" + if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then + return 0 + fi + fi + return 1 +} + +start_ui() { + local name="$1" + shift + local pidfile="$RUNDIR/\${name}.pid" + local logfile="/tmp/flok-\${name}.log" + local pid + pid="\$( + runuser -u "$UI_USER" -- env \\ + DISPLAY="$DISPLAY" \\ + HOME="$UI_HOME" \\ + XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR" \\ + sh -c 'log="$1"; shift; nohup "$@" >>"$log" 2>&1 & echo $!' sh "$logfile" "$@" + )" + echo "$pid" > "$pidfile" +} + +# Suspend kills Chrome but leaves SingletonLock on disk. +if ! pgrep -u "$UI_USER" -f -- "--user-data-dir=\${PROFILE}" >/dev/null 2>&1; then + rm -f "$PROFILE/SingletonLock" "$PROFILE/SingletonSocket" "$PROFILE/SingletonCookie" +fi + +if ! alive "$RUNDIR/xvfb.pid"; then + rm -f /tmp/.X11-unix/X99 /tmp/.X99-lock + start_ui xvfb Xvfb "$DISPLAY" -screen 0 "\${WIDTH}x\${HEIGHT}x\${DEPTH}" -nolisten tcp + sleep 0.4 +fi +if ! alive "$RUNDIR/openbox.pid"; then + start_ui openbox openbox +fi +if command -v x11vnc >/dev/null 2>&1 && ! alive "$RUNDIR/x11vnc.pid"; then + start_ui x11vnc x11vnc -display "$DISPLAY" -localhost -nopw -forever -shared -rfbport 5900 +fi +if command -v websockify >/dev/null 2>&1 && ! alive "$RUNDIR/novnc.pid"; then + WEB="" + for d in /usr/share/novnc /usr/share/novnc/utils; do + if [ -d "$d" ]; then WEB="$d"; break; fi + done + if [ -n "$WEB" ]; then + start_ui novnc websockify --web "$WEB" "127.0.0.1:\${NOVNC_PORT}" 127.0.0.1:5900 + fi +fi +echo "ok display=$DISPLAY profile=$PROFILE ui=$UI_USER" +`; + +export const FIXTURE_HTML = ` + + + + FLOKS C3B fixture + + + +
click-me
+
idle
+ + + +`; diff --git a/src/lib/computers/providers/runloop-memory.ts b/src/lib/computers/providers/runloop-memory.ts index 6acbd10..71aa4fe 100644 --- a/src/lib/computers/providers/runloop-memory.ts +++ b/src/lib/computers/providers/runloop-memory.ts @@ -7,6 +7,13 @@ import { randomBytes } from "node:crypto"; import { posix as pathPosix } from "node:path"; import { PathEscape } from "../errors.js"; +import type { Action } from "../types.js"; +import { + BROWSER_PROFILE_DIR, + DISPLAY_HEIGHT, + DISPLAY_WIDTH, + INTERACTIVE_DIR, +} from "./runloop-interactive.js"; import { assertNoControlPlaneSecrets, RUNLOOP_WORKSPACE_ROOT, @@ -30,11 +37,19 @@ function newId(prefix: string): string { export class MemoryRunloopControlPlane implements RunloopControlPlane { private readonly sessions = new Map(); private readonly snapshots = new Map>(); + /** Test hook: next create/restore session fails its first ensureInteractiveStack. */ + failNextEnsure = false; + lastCreatedId: string | null = null; async create(params: RunloopCreateParams): Promise { assertNoControlPlaneSecrets(params.envVars); const id = newId("rlbox"); const session = new MemoryRunloopDevbox(id, params, this.snapshots); + if (this.failNextEnsure) { + session.failEnsureOnce = true; + this.failNextEnsure = false; + } + this.lastCreatedId = id; this.sessions.set(id, session); return session; } @@ -66,6 +81,10 @@ class MemoryRunloopDevbox implements RunloopDevboxSession { readonly flockId: string; readonly bootId: string; destroyed = false; + /** How many times ensureInteractiveStack actually (re)started. */ + stackStarts = 0; + failEnsureOnce = false; + private stackUp = false; private current: RunloopDevboxState = "running"; private fs: Map; private readonly snapshots: Map>; @@ -95,6 +114,7 @@ class MemoryRunloopDevbox implements RunloopDevboxSession { async suspend(): Promise { this.assertAlive(); this.current = "paused"; + this.stackUp = false; } async resume(): Promise { @@ -246,6 +266,68 @@ class MemoryRunloopDevbox implements RunloopDevboxSession { return name; } + async ensureInteractiveStack(): Promise { + this.assertAlive(); + if (this.failEnsureOnce) { + this.failEnsureOnce = false; + throw new Error("ensureInteractiveStack failed"); + } + if (this.current !== "running") { + throw new Error(`runloop devbox ${this.id} is ${this.current}`); + } + if (!this.stackUp) { + this.stackStarts += 1; + this.stackUp = true; + } + await this.fsMkdir(BROWSER_PROFILE_DIR); + await this.fsMkdir(INTERACTIVE_DIR); + } + + async screenshot(): Promise<{ + width: number; + height: number; + png: Buffer; + activeWindow?: string; + }> { + this.assertRunning(); + if (!this.stackUp) await this.ensureInteractiveStack(); + return { + width: DISPLAY_WIDTH, + height: DISPLAY_HEIGHT, + png: MIN_PNG, + activeWindow: `flok-${this.birdId}`, + }; + } + + async novncLocalOk(): Promise { + this.assertRunning(); + return this.stackUp; + } + + async uiAction(action: Action): Promise { + this.assertRunning(); + if (!this.stackUp) await this.ensureInteractiveStack(); + if (action.type === "open_url" && action.url) { + await this.fsMkdir(BROWSER_PROFILE_DIR); + await this.fsWrite( + `${BROWSER_PROFILE_DIR}/last-url`, + Buffer.from(action.url, "utf8"), + ); + } + if (action.type === "launch_application") { + await this.fsMkdir(BROWSER_PROFILE_DIR); + await this.fsWrite( + `${BROWSER_PROFILE_DIR}/launched`, + Buffer.from("1", "utf8"), + ); + } + } + + /** Test helper: simulate suspend discarding RAM daemons. */ + markStackDown(): void { + this.stackUp = false; + } + private assertAlive(): void { if (this.destroyed || this.current === "deleted") { throw new Error(`runloop devbox ${this.id} destroyed`); @@ -273,3 +355,10 @@ function resolveArgPath(userPath: string | undefined, cwd: string): string { if (userPath.startsWith("/")) return pathPosix.normalize(userPath); return pathPosix.normalize(pathPosix.join(cwd, userPath)); } + +/** 1×1 PNG. Memory-plane screenshot stub — not a real display capture. */ +const MIN_PNG = Buffer.from( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==", + "base64", +); + diff --git a/src/lib/computers/providers/runloop-sdk.ts b/src/lib/computers/providers/runloop-sdk.ts index c264de5..22a2c13 100644 --- a/src/lib/computers/providers/runloop-sdk.ts +++ b/src/lib/computers/providers/runloop-sdk.ts @@ -7,6 +7,22 @@ import { RunloopSDK } from "@runloop/api-client"; import { posix as pathPosix } from "node:path"; import { ProviderUnavailable } from "../errors.js"; import { assertInsideRoot } from "../path.js"; +import type { Action } from "../types.js"; +import { + BROWSER_PROFILE_DIR, + ENSURE_INTERACTIVE_SH, + ENSURE_SCRIPT_PATH, + FIXTURE_HTML, + FIXTURE_PATH, + FLOK_DISPLAY, + FLOK_UI_USER, + INTERACTIVE_DIR, + argvAsUiUser, + chromeLaunchArgv, + pngDimensions, + uniqueObsShotPath, + CHROME_LOG_PATH, +} from "./runloop-interactive.js"; import { assertNoControlPlaneSecrets, DEFAULT_RUNLOOP_ARCH, @@ -140,6 +156,8 @@ class SdkRunloopDevbox implements RunloopDevboxSession { readonly birdId: string; readonly flockId: string; bootId = ""; + private interactiveStackUp = false; + private graphicalStack = false; constructor( private readonly box: SdkDevbox, @@ -154,6 +172,7 @@ class SdkRunloopDevbox implements RunloopDevboxSession { async ensureWorkspace(): Promise { await this.box.cmd.exec(`mkdir -p ${shellSingle(RUNLOOP_WORKSPACE_ROOT + "/.flok")}`); await this.box.file.write({ file_path: EXECVP_PATH, contents: EXECVP_PY }); + await this.lockRootExecutedAssets(); const boot = await this.box.cmd.exec("cat /proc/sys/kernel/random/boot_id"); this.bootId = ((await boot.stdout()) ?? "").trim(); } @@ -164,11 +183,13 @@ class SdkRunloopDevbox implements RunloopDevboxSession { } async suspend(): Promise { + this.interactiveStackUp = false; await this.box.suspend(); await this.box.awaitSuspended(); } async resume(): Promise { + this.interactiveStackUp = false; await this.box.resume(); await this.box.awaitRunning(); } @@ -327,6 +348,228 @@ class SdkRunloopDevbox implements RunloopDevboxSession { return snap.id; } + async ensureInteractiveStack(): Promise { + if (this.interactiveStackUp) { + if (!this.graphicalStack || (await this.xvfbAlive())) return; + this.interactiveStackUp = false; + } + this.requireFs( + await this.fsMkdir(pathPosix.dirname(ENSURE_SCRIPT_PATH)), + "ensureInteractiveStack mkdir", + ); + // Take .flok away from flok-ui before writing helpers root will execute. + await this.lockRootExecutedAssets(); + this.requireFs( + await this.fsWrite(ENSURE_SCRIPT_PATH, Buffer.from(ENSURE_INTERACTIVE_SH, "utf8")), + "ensureInteractiveStack write script", + ); + this.requireFs( + await this.fsWrite(FIXTURE_PATH, Buffer.from(FIXTURE_HTML, "utf8")), + "ensureInteractiveStack write fixture", + ); + this.requireFs(await this.fsMkdir(BROWSER_PROFILE_DIR), "ensureInteractiveStack mkdir profile"); + await this.lockRootExecutedAssets(); + const r = await this.exec({ + argv: ["bash", ENSURE_SCRIPT_PATH], + cwd: RUNLOOP_WORKSPACE_ROOT, + timeoutMs: 30_000, + }); + if (r.exitCode !== 0) { + throw new ProviderUnavailable( + "runloop", + `ensureInteractiveStack failed: ${r.stderr || r.stdout}`, + ); + } + this.graphicalStack = !r.stdout.includes("missing-xvfb"); + this.interactiveStackUp = true; + } + + async screenshot(): Promise<{ + width: number; + height: number; + png: Buffer; + activeWindow?: string; + }> { + const shotPath = uniqueObsShotPath(); + this.requireFs(await this.fsMkdir(pathPosix.dirname(shotPath)), "screenshot dir"); + const shot = await this.exec({ + argv: argvAsUiUser(["import", "-display", FLOK_DISPLAY, "-window", "root", shotPath]), + cwd: RUNLOOP_WORKSPACE_ROOT, + env: { DISPLAY: FLOK_DISPLAY }, + timeoutMs: 15_000, + }); + if (shot.exitCode !== 0) { + await this.fsDelete(shotPath).catch(() => undefined); + throw new ProviderUnavailable("runloop", `screenshot failed: ${shot.stderr}`); + } + try { + const file = await this.fsRead(shotPath); + if (!file.ok || !file.data) { + throw new ProviderUnavailable("runloop", "screenshot read failed"); + } + const dims = pngDimensions(file.data); + if (!dims) { + throw new ProviderUnavailable("runloop", "screenshot is not a valid PNG"); + } + let activeWindow: string | undefined; + const win = await this.exec({ + argv: argvAsUiUser(["xdotool", "getactivewindow", "getwindowname"]), + cwd: RUNLOOP_WORKSPACE_ROOT, + env: { DISPLAY: FLOK_DISPLAY }, + timeoutMs: 5_000, + }); + if (win.exitCode === 0 && win.stdout.trim()) activeWindow = win.stdout.trim(); + const out: { width: number; height: number; png: Buffer; activeWindow?: string } = { + width: dims.width, + height: dims.height, + png: file.data, + }; + if (activeWindow) out.activeWindow = activeWindow; + return out; + } finally { + await this.fsDelete(shotPath).catch(() => undefined); + } + } + + async novncLocalOk(): Promise { + const r = await this.exec({ + argv: [ + "python3", + "-c", + "import urllib.request; urllib.request.urlopen('http://127.0.0.1:6080/', timeout=2); print('ok')", + ], + cwd: RUNLOOP_WORKSPACE_ROOT, + timeoutMs: 5_000, + }); + return r.exitCode === 0 && r.stdout.includes("ok"); + } + + async uiAction(action: Action): Promise { + const env = { DISPLAY: FLOK_DISPLAY }; + let argv: string[]; + switch (action.type) { + case "click_coordinates": + argv = argvAsUiUser([ + "xdotool", + "mousemove", + String(action.x), + String(action.y), + "click", + "1", + ]); + break; + case "type": + argv = argvAsUiUser(["xdotool", "type", "--", action.text ?? ""]); + break; + case "key": + argv = argvAsUiUser(["xdotool", "key", "--", action.key ?? ""]); + break; + case "scroll": { + if (typeof action.x === "number" && action.x !== 0) { + throw new Error("horizontal scroll unsupported"); + } + const dy = action.y ?? 0; + if (!Number.isInteger(dy) || dy === 0) { + throw new Error("scroll requires non-zero integer y delta"); + } + const button = dy < 0 ? "4" : "5"; + const n = Math.min(20, Math.abs(dy)); + argv = argvAsUiUser(["xdotool", "click", "--repeat", String(n), button]); + break; + } + case "wait": + argv = ["sleep", String((action.durationMs ?? 100) / 1000)]; + break; + case "open_url": + case "launch_application": { + const url = + action.type === "open_url" + ? (action.url ?? `file://${FIXTURE_PATH}`) + : `file://${FIXTURE_PATH}`; + // Detach so exec returning does not SIGHUP Chrome. No --no-sandbox. + // Launch accepted ≠ Chrome ready; live gate polls readiness separately. + // runuser drops to flok-ui; python launcher stays the Devbox user (root on DnD). + // Startup stderr goes to a guest tmp log (not FLOKS audit). + const chromeCode = [ + "import subprocess,sys", + `log=open(${JSON.stringify(CHROME_LOG_PATH)},"ab",buffering=0)`, + "subprocess.Popen(sys.argv[1:], start_new_session=True, stdout=log, stderr=subprocess.STDOUT)", + "print('launched')", + "", + ].join("\n"); + argv = ["python3", "-c", chromeCode, ...chromeLaunchArgv(url)]; + break; + } + default: + throw new Error(`unsupported action ${action.type}`); + } + const r = await this.exec({ + argv, + cwd: RUNLOOP_WORKSPACE_ROOT, + env, + timeoutMs: 20_000, + }); + if (action.type === "open_url" || action.type === "launch_application") { + // Python Popen returns immediately; non-zero means spawn failed (missing binary, etc.). + if (r.exitCode !== 0) { + throw new ProviderUnavailable( + "runloop", + r.stderr || r.stdout || "chrome launch failed", + ); + } + return; + } + if (r.exitCode !== 0 && !r.timedOut) { + throw new ProviderUnavailable("runloop", r.stderr || `uiAction ${action.type} failed`); + } + } + + private requireFs(r: RunloopFsResult, what: string): void { + if (!r.ok) { + throw new ProviderUnavailable("runloop", `${what} failed: ${r.errorCode}`); + } + } + + /** Cheap Xvfb liveness probe. Full ensure only re-runs if this fails. */ + private async xvfbAlive(): Promise { + try { + const r = await this.exec({ + argv: ["pgrep", "-u", FLOK_UI_USER, "-f", `Xvfb ${FLOK_DISPLAY}`], + cwd: RUNLOOP_WORKSPACE_ROOT, + timeoutMs: 5_000, + }); + return r.exitCode === 0; + } catch { + return false; + } + } + + /** + * Lock root-executed guest helpers via Devbox-root cmd.exec (not execvp.py). + * flok-ui must not be able to replace anything root later runs. + */ + private async lockRootExecutedAssets(): Promise { + const dir = shellSingle(INTERACTIVE_DIR); + const execvp = shellSingle(EXECVP_PATH); + const script = shellSingle(ENSURE_SCRIPT_PATH); + const fixture = shellSingle(FIXTURE_PATH); + const lock = await this.box.cmd.exec( + [ + `chown root:root ${dir}`, + `chmod 755 ${dir}`, + `if [ -f ${execvp} ]; then chown root:root ${execvp} && chmod 755 ${execvp}; fi`, + `if [ -f ${script} ]; then chown root:root ${script} && chmod 755 ${script}; fi`, + `if [ -f ${fixture} ]; then chown root:root ${fixture} && chmod 644 ${fixture}; fi`, + ].join(" && "), + ); + if ((lock.exitCode ?? 1) !== 0) { + throw new ProviderUnavailable( + "runloop", + `lock root-executed assets failed: ${await lock.stderr()}`, + ); + } + } + private async enforceResolved(path: string): Promise { try { assertInsideRoot(path, RUNLOOP_WORKSPACE_ROOT); diff --git a/src/lib/computers/providers/runloop.ts b/src/lib/computers/providers/runloop.ts index 75e02c2..3ff0530 100644 --- a/src/lib/computers/providers/runloop.ts +++ b/src/lib/computers/providers/runloop.ts @@ -6,7 +6,8 @@ * Shell mode is rejected. Filesystem is jailed to /home/user/flok. * * Suspend preserves disk, not RAM (pauseMemory: false). - * Native Computer Use / VNC are C3B — not claimed here. + * C3B: screenshot + bounded input on a private display (as flok-ui). + * takeover/vnc stay fail-closed. computerUse is true after paid C3B live. */ import { posix as pathPosix } from "node:path"; @@ -30,6 +31,7 @@ import type { } from "../types.js"; import { ComputerError, PathEscape, ProviderUnavailable } from "../errors.js"; import { assertInsideRoot } from "../path.js"; +import { validateAction } from "./runloop-interactive.js"; import { assertNoControlPlaneSecrets, DEFAULT_RUNLOOP_ARCH, @@ -54,11 +56,10 @@ export class RunloopBlueprintRequired extends ComputerError { } export class ComputerUseNotAvailable extends ComputerError { - constructor() { - super( - "C3B_NOT_IMPLEMENTED", - "Interactive computer layer is C3B; Runloop C3A has no native Computer Use/VNC", - ); + constructor( + detail = "Secure human takeover is not enabled; local noVNC stays on 127.0.0.1", + ) { + super("C3B_TAKEOVER_UNAVAILABLE", detail); this.name = "ComputerUseNotAvailable"; } } @@ -119,7 +120,7 @@ export class RunloopProvider implements ComputerProvider { return { linuxVm: true, windowsVm: false, - computerUse: false, + computerUse: true, accessibility: false, vnc: false, pauseMemory: false, @@ -137,7 +138,13 @@ export class RunloopProvider implements ComputerProvider { const params = this.createParams(spec); const session = await this.plane.create(params); this.sessions.set(session.id, session); - await session.fsMkdir(RUNLOOP_WORKSPACE_ROOT).catch(() => undefined); + try { + await session.fsMkdir(RUNLOOP_WORKSPACE_ROOT).catch(() => undefined); + await session.ensureInteractiveStack(); + } catch (e) { + await this.abandonSession(session.id); + throw e; + } return { providerRef: session.id, status: "ready" }; } @@ -157,6 +164,7 @@ export class RunloopProvider implements ComputerProvider { async wake(ref: string): Promise { const s = await this.requireSession(ref); await s.resume(); + await s.ensureInteractiveStack(); } async pause(ref: string): Promise { @@ -335,12 +343,57 @@ export class RunloopProvider implements ComputerProvider { } } - async observe(_ref: string, _request: ObserveRequest): Promise { - throw new ComputerUseNotAvailable(); + async observe(ref: string, request: ObserveRequest): Promise { + const s = await this.requireSession(ref); + await s.ensureInteractiveStack(); + if (request.includeAccessibility) { + throw new ComputerUseNotAvailable("accessibility addressing is not implemented"); + } + const shot = await s.screenshot(); + const obs: Observation = { + screenWidth: shot.width, + screenHeight: shot.height, + }; + if (shot.activeWindow) obs.activeWindow = shot.activeWindow; + if (request.includeScreenshot !== false) { + obs.screenshotBase64 = shot.png.toString("base64"); + } + return obs; } - async act(_ref: string, _request: ActionBatch): Promise { - throw new ComputerUseNotAvailable(); + async act(ref: string, request: ActionBatch): Promise { + const s = await this.requireSession(ref); + await s.ensureInteractiveStack(); + const results: ActionResult["results"] = []; + let ok = true; + for (let i = 0; i < request.actions.length; i++) { + const action = request.actions[i]!; + const err = validateAction(action); + if (err) { + ok = false; + results.push({ action, success: false, error: err }); + for (const rest of request.actions.slice(i + 1)) { + results.push({ action: rest, success: false, error: "not executed" }); + } + break; + } + try { + await s.uiAction(action); + results.push({ action, success: true }); + } catch (e) { + ok = false; + results.push({ + action, + success: false, + error: e instanceof Error ? e.message : "action failed", + }); + for (const rest of request.actions.slice(i + 1)) { + results.push({ action: rest, success: false, error: "not executed" }); + } + break; + } + } + return { ok, results }; } async takeover(_ref: string): Promise { @@ -375,6 +428,12 @@ export class RunloopProvider implements ComputerProvider { }; const session = await this.plane.restore(request.providerSnapshotRef, params); this.sessions.set(session.id, session); + try { + await session.ensureInteractiveStack(); + } catch (e) { + await this.abandonSession(session.id); + throw e; + } return { providerRef: session.id, status: "ready" }; } @@ -411,6 +470,15 @@ export class RunloopProvider implements ComputerProvider { ); } } + + /** Unregister and shut down a session the caller never received a providerRef for. */ + private async abandonSession(id: string): Promise { + const session = this.sessions.get(id); + this.sessions.delete(id); + if (session) { + await session.shutdown().catch(() => undefined); + } + } } export function runloopJoin(rel: string): string { diff --git a/tests/live/runloop.c3b.live.test.ts b/tests/live/runloop.c3b.live.test.ts new file mode 100644 index 0000000..5204425 --- /dev/null +++ b/tests/live/runloop.c3b.live.test.ts @@ -0,0 +1,317 @@ +/** + * Opt-in C3B live tests. Skipped unless FLOK_LIVE_RUNLOOP_C3B_TEST=1. + * When the flag is set, missing credentials FAIL (never silent-skip). + * Always destroy the paid Devbox in finally. + * + * open_url is launch-accepted (Popen), not Chrome-ready. This suite polls + * pollUntilChromeReady after every launch before continuing. + * + * Do not run from ordinary verify / PR CI. + */ + +import { describe, it, before } from "node:test"; +import assert from "node:assert/strict"; +import { RunloopProvider } from "../../src/lib/computers/providers/index.js"; +import type { ExecResult } from "../../src/lib/computers/types.js"; +import { + BROWSER_PROFILE_DIR, + CHROME_READY_PROBE_PY, + CHROME_READY_TIMEOUT_MS, + DISPLAY_HEIGHT, + DISPLAY_WIDTH, + FIXTURE_PATH, + chromeHasNoSandbox, + chromeHasUserDataDir, + chromeProfileHasBrowserState, + chromeSandboxDisabled, + formatChromeReadyFailure, + parseChromeReadyEvidence, + pngDimensions, + pollUntilChromeReady, + type ChromeReadyEvidence, +} from "../../src/lib/computers/providers/runloop-interactive.js"; + +const LIVE = process.env.FLOK_LIVE_RUNLOOP_C3B_TEST === "1"; + +const LISTEN_CHECK = [ + "import pathlib,sys", + "ports={5900,6080}", + "v4_loop,v4_any={'0100007F'},{'00000000'}", + "v6_loop,v6_any={'00000000000000000000000001000000'},{'00000000000000000000000000000000'}", + "found={p:[] for p in ports}", + "for path,loop,any_ in (('/proc/net/tcp',v4_loop,v4_any),('/proc/net/tcp6',v6_loop,v6_any)):", + " try: lines=pathlib.Path(path).read_text().splitlines()[1:]", + " except FileNotFoundError: continue", + " for line in lines:", + " parts=line.split()", + " if parts[3]!='0A': continue", + " ip,hp=parts[1].split(':')", + " port=int(hp,16)", + " if port in ports: found[port].append((path,ip,ip in loop))", + "bad=[]", + "for port,addrs in found.items():", + " if not addrs: bad.append(f'{port} not listening')", + " for path,ip,is_loop in addrs:", + " if not is_loop: bad.append(f'{port} bound {path}:{ip}')", + "print('LISTEN',found)", + "print('BAD',bad)", + "sys.exit(1 if bad else 0)", +].join("\n"); + +async function mustExec( + p: RunloopProvider, + ref: string, + argv: string[], + stage: string, +): Promise { + const r = await p.exec(ref, { argv, timeoutMs: 20_000 }); + assert.equal(r.exitCode, 0, `${stage}: ${argv.join(" ")}\nstdout=${r.stdout}\nstderr=${r.stderr}`); + return r; +} + +function evidenceFromProbeStdout(stdout: string, stderr: string, stage: string): ChromeReadyEvidence { + const trimmed = stdout.trim(); + const start = trimmed.indexOf("{"); + const end = trimmed.lastIndexOf("}"); + assert.ok( + start >= 0 && end > start, + `${stage}: chrome probe did not print JSON\nstdout=${stdout}\nstderr=${stderr}`, + ); + try { + return parseChromeReadyEvidence(trimmed.slice(start, end + 1)); + } catch (err) { + const detail = err instanceof Error ? err.message : String(err); + throw new Error( + `${stage}: chrome probe JSON parse failed: ${detail}\nstdout=${stdout}\nstderr=${stderr}`, + ); + } +} + +async function awaitChromeReady( + p: RunloopProvider, + ref: string, + stage: string, +): Promise { + const { result, evidence } = await pollUntilChromeReady( + async () => { + const r = await p.exec(ref, { + argv: ["python3", "-c", CHROME_READY_PROBE_PY], + timeoutMs: 15_000, + }); + assert.equal( + r.exitCode, + 0, + `${stage}: chrome readiness probe failed\nstdout=${r.stdout}\nstderr=${r.stderr}`, + ); + return evidenceFromProbeStdout(r.stdout, r.stderr, stage); + }, + { requireProfile: true }, + ); + if (!result.ready) { + assert.fail(`${stage}: ${formatChromeReadyFailure(result, evidence)}`); + } + const ours = evidence.chromeCmdlines.filter( + (c) => /google-chrome/.test(c) || chromeHasUserDataDir(c), + ); + assert.ok( + ours.some(chromeHasUserDataDir), + `${stage}: Chrome ready but --user-data-dir missing\n${formatChromeReadyFailure(result, evidence)}`, + ); + assert.equal( + ours.some(chromeSandboxDisabled), + false, + `${stage}: Chrome sandbox disabled\n${formatChromeReadyFailure(result, evidence)}`, + ); + assert.equal( + ours.some(chromeHasNoSandbox), + false, + `${stage}: Chrome cmdline contains --no-sandbox\n${formatChromeReadyFailure(result, evidence)}`, + ); + assert.ok( + chromeProfileHasBrowserState(evidence.profileEntries) || + chromeProfileHasBrowserState(evidence.profileEntriesUi), + `${stage}: Chrome process/window up but profile not initialized\n${formatChromeReadyFailure(result, evidence)}`, + ); + return evidence; +} + +describe("Runloop C3B live interactive Devbox", { skip: !LIVE }, () => { + before(() => { + if (!process.env.RUNLOOP_API_KEY) { + throw new Error("FLOK_LIVE_RUNLOOP_C3B_TEST=1 but RUNLOOP_API_KEY missing"); + } + if (!process.env.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT && !process.env.FLOK_RUNLOOP_BLUEPRINT) { + throw new Error( + "FLOK_LIVE_RUNLOOP_C3B_TEST=1 but interactive blueprint is missing (must FAIL, not skip)", + ); + } + }); + + it("one Devbox: stack, fixture, observe, input, profile, suspend/resume, local noVNC, cleanup", async () => { + if (process.env.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT) { + process.env.FLOK_RUNLOOP_BLUEPRINT = process.env.FLOK_RUNLOOP_INTERACTIVE_BLUEPRINT; + } + const p = await RunloopProvider.fromEnv(); + const caps = p.capabilities(); + assert.equal(caps.computerUse, true, "computerUse is true after the paid C3B live gate"); + assert.equal(caps.vnc, false); + assert.equal(caps.accessibility, false); + assert.equal(caps.pauseMemory, false); + + const refs: string[] = []; + try { + const a = await p.provision({ birdId: "c3b-live", flockId: "flock-live" }); + refs.push(a.providerRef); + assert.ok(a.providerRef, "provision: missing providerRef"); + + const tools = await mustExec( + p, + a.providerRef, + ["bash", "-lc", "command -v docker && command -v git && command -v python3 && command -v node"], + "tools present", + ); + assert.match(tools.stdout, /docker/, "tools present: docker missing"); + assert.match(tools.stdout, /git/, "tools present: git missing"); + assert.match(tools.stdout, /python3/, "tools present: python3 missing"); + assert.match(tools.stdout, /node/, "tools present: node missing"); + + const ui = await mustExec(p, a.providerRef, ["id", "-u", "flok-ui"], "flok-ui uid"); + assert.equal(ui.stdout.trim(), "1500", "flok-ui uid: expected 1500"); + + const chromeVer = await mustExec( + p, + a.providerRef, + ["cat", "/etc/flok-chrome-version"], + "chrome version", + ); + assert.match(chromeVer.stdout, /Google Chrome/i, "chrome version: unexpected output"); + console.log("C3B chrome version", chromeVer.stdout.trim()); + console.log("C3B chrome ready timeout ms", CHROME_READY_TIMEOUT_MS); + + const xvfb = await mustExec(p, a.providerRef, ["pgrep", "-u", "flok-ui", "-a", "Xvfb"], "Xvfb :99"); + assert.match(xvfb.stdout, /:99/, "Xvfb :99: display missing from cmdline"); + const openbox = await mustExec( + p, + a.providerRef, + ["pgrep", "-u", "flok-ui", "-a", "openbox"], + "Openbox", + ); + assert.match(openbox.stdout, /openbox/i, "Openbox: process missing"); + + const opened = await p.act(a.providerRef, { + actions: [{ type: "open_url", url: `file://${FIXTURE_PATH}` }], + }); + assert.equal(opened.ok, true, `open_url launch accepted: ${JSON.stringify(opened.results)}`); + + await awaitChromeReady(p, a.providerRef, "chrome ready after open_url"); + + const profileList = await p.filesystem(a.providerRef, { + operation: "list", + path: BROWSER_PROFILE_DIR, + }); + assert.equal(profileList.ok, true, "profile initialized (filesystem.list): list failed"); + assert.ok( + Array.isArray(profileList.data) && profileList.data.length > 0, + `profile initialized (filesystem.list): empty array after readiness probe data=${JSON.stringify(profileList.data)}`, + ); + + const obs = await p.observe(a.providerRef, { includeScreenshot: true }); + assert.equal(obs.screenWidth, DISPLAY_WIDTH, "screenshot: width"); + assert.equal(obs.screenHeight, DISPLAY_HEIGHT, "screenshot: height"); + assert.ok(obs.screenshotBase64 && obs.screenshotBase64.length > 100, "screenshot: missing png"); + assert.equal(obs.accessibilitySummary, undefined, "screenshot: accessibility must stay unset"); + const dims = pngDimensions(Buffer.from(obs.screenshotBase64, "base64")); + assert.deepEqual(dims, { width: DISPLAY_WIDTH, height: DISPLAY_HEIGHT }, "screenshot: png IHDR"); + + const clicks = await p.act(a.providerRef, { + actions: [ + { type: "click_coordinates", x: 220, y: 180 }, + { type: "type", text: "flok" }, + { type: "key", key: "Return" }, + { type: "scroll", y: 3 }, + ], + }); + assert.equal(clicks.ok, true, `click/type/key/scroll: ${JSON.stringify(clicks.results)}`); + assert.equal(clicks.results.length, 4, "click/type/key/scroll: expected 4 results"); + for (const r of clicks.results) { + assert.equal(r.success, true, `click/type/key/scroll: ${JSON.stringify(r)}`); + } + + const marker = await p.filesystem(a.providerRef, { + operation: "write", + path: `${BROWSER_PROFILE_DIR}/c3b-marker`, + content: "profile-disk", + }); + assert.equal(marker.ok, true, "persistence marker write: failed"); + + const novnc = await mustExec( + p, + a.providerRef, + [ + "python3", + "-c", + "import urllib.request; urllib.request.urlopen('http://127.0.0.1:6080/', timeout=3); print('ok')", + ], + "noVNC localhost", + ); + assert.match(novnc.stdout, /ok/, "noVNC localhost: unexpected body"); + + const listen = await p.exec(a.providerRef, { + argv: ["python3", "-c", LISTEN_CHECK], + timeoutMs: 10_000, + }); + assert.equal( + listen.exitCode, + 0, + `no public VNC bind: ${listen.stdout}\n${listen.stderr}`, + ); + + await p.pause(a.providerRef); + await p.wake(a.providerRef); + + const kept = await p.filesystem(a.providerRef, { + operation: "read", + path: `${BROWSER_PROFILE_DIR}/c3b-marker`, + }); + assert.equal(kept.ok, true, "suspend/resume marker survived: read failed"); + assert.equal(kept.data, "profile-disk", "suspend/resume marker survived: content mismatch"); + + const xvfb2 = await mustExec( + p, + a.providerRef, + ["pgrep", "-u", "flok-ui", "-a", "Xvfb"], + "graphical stack after resume (Xvfb)", + ); + assert.match(xvfb2.stdout, /:99/, "graphical stack after resume: Xvfb :99 missing"); + const openbox2 = await mustExec( + p, + a.providerRef, + ["pgrep", "-u", "flok-ui", "-a", "openbox"], + "graphical stack after resume (Openbox)", + ); + assert.match(openbox2.stdout, /openbox/i, "graphical stack after resume: Openbox missing"); + + const relaunch = await p.act(a.providerRef, { + actions: [{ type: "open_url", url: `file://${FIXTURE_PATH}` }], + }); + assert.equal(relaunch.ok, true, `open_url relaunch accepted: ${JSON.stringify(relaunch.results)}`); + + await awaitChromeReady(p, a.providerRef, "chrome ready after resume"); + + const after = await p.observe(a.providerRef, { includeScreenshot: true }); + assert.ok(after.screenshotBase64, "screenshot after resume: missing png"); + const dims2 = pngDimensions(Buffer.from(after.screenshotBase64, "base64")); + assert.deepEqual( + dims2, + { width: DISPLAY_WIDTH, height: DISPLAY_HEIGHT }, + "screenshot after resume: png IHDR", + ); + } finally { + for (const ref of refs) { + await p.destroy(ref).catch((err: unknown) => { + console.error("destroy failed", err); + }); + } + } + }); +}); diff --git a/tests/providers/runloop.c3b.test.ts b/tests/providers/runloop.c3b.test.ts new file mode 100644 index 0000000..eda9d93 --- /dev/null +++ b/tests/providers/runloop.c3b.test.ts @@ -0,0 +1,705 @@ +/** + * C3B unpaid tests. Zero Runloop network. + */ + +import { describe, it } from "node:test"; +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { + ComputerUseNotAvailable, + MemoryRunloopControlPlane, + RunloopProvider, + FLAGS, + assertNexusDisabled, +} from "../../src/lib/computers/index.js"; +import { + validateAction, + pngDimensions, + classifyBlueprintBuildStatus, + chromeLaunchArgv, + argvAsUiUser, + ENSURE_INTERACTIVE_SH, + DISPLAY_WIDTH, + DISPLAY_HEIGHT, + BROWSER_PROFILE_DIR, + OBS_SHOT_DIR, + uniqueObsShotPath, + FLOK_UI_USER, + FLOK_UI_UID, + CHROME_LOG_PATH, + CHROME_HOME_FALLBACK_DIR, + CHROME_READY_TIMEOUT_MS, + CHROME_READY_POLL_MS, + CHROME_READY_PROBE_PY, + chromeHasNoSandbox, + chromeHasDisableSetuidSandbox, + chromeHasUserDataDir, + chromeProfileHasBrowserState, + chromeSandboxDisabled, + classifyChromeReadiness, + formatChromeReadyFailure, + parseChromeReadyEvidence, + pollUntilChromeReady, + sanitizeChromeLog, + type ChromeReadyEvidence, +} from "../../src/lib/computers/providers/runloop-interactive.js"; + +function provider(): RunloopProvider { + return new RunloopProvider({ + client: new MemoryRunloopControlPlane(), + blueprint: "memory-linux-vm", + }); +} + +describe("C3B action validation", () => { + it("rejects click_element", () => { + const err = validateAction({ type: "click_element", elementId: "x" }); + assert.ok(err); + assert.match(err, /unsupported/); + }); + + it("rejects out-of-bounds and non-integer coordinates", () => { + assert.ok(validateAction({ type: "click_coordinates", x: -1, y: 0 })); + assert.ok(validateAction({ type: "click_coordinates", x: DISPLAY_WIDTH, y: 0 })); + assert.ok(validateAction({ type: "click_coordinates", x: 1.5, y: 2 })); + assert.equal(validateAction({ type: "click_coordinates", x: 200, y: 200 }), null); + }); + + it("rejects malformed, non-http, and jail-escaping file URLs", () => { + assert.ok(validateAction({ type: "open_url", url: "not a url" })); + assert.ok(validateAction({ type: "open_url", url: "javascript:alert(1)" })); + assert.ok(validateAction({ type: "open_url", url: "file:///etc/passwd" })); + assert.ok(validateAction({ type: "open_url", url: "file:///home/user/flok/../etc/passwd" })); + assert.equal(validateAction({ type: "open_url", url: "https://example.com" }), null); + assert.equal( + validateAction({ type: "open_url", url: "file:///home/user/flok/.flok/fixture.html" }), + null, + ); + }); + + it("rejects applications outside the allowlist", () => { + assert.ok(validateAction({ type: "launch_application", application: "bash" })); + assert.equal(validateAction({ type: "launch_application", application: "browser" }), null); + }); + + it("rejects disallowed keys and bounds type/wait", () => { + assert.ok(validateAction({ type: "key", key: "; rm -rf /" })); + assert.ok(validateAction({ type: "key", key: "$(reboot)" })); + assert.ok(validateAction({ type: "type", text: "x".repeat(2001) })); + assert.ok(validateAction({ type: "wait", durationMs: 99_000 })); + assert.ok(validateAction({ type: "wait", durationMs: 0 })); + assert.ok(validateAction({ type: "scroll" })); + assert.ok(validateAction({ type: "scroll", x: 5 })); + assert.ok(validateAction({ type: "scroll", y: 0 })); + assert.ok(validateAction({ type: "scroll", x: 1, y: 3 })); + assert.match(validateAction({ type: "scroll", x: 5 }) ?? "", /horizontal/); + assert.equal(validateAction({ type: "scroll", y: 3 }), null); + assert.equal(validateAction({ type: "scroll", y: -2 }), null); + assert.equal(validateAction({ type: "scroll", x: 0, y: 3 }), null); + assert.equal(validateAction({ type: "type", text: "hello" }), null); + assert.equal(validateAction({ type: "key", key: "Return" }), null); + }); +}); + +describe("C3B pngDimensions", () => { + it("reads IHDR from a real 1x1 PNG and rejects garbage", () => { + const png = Buffer.from( + "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==", + "base64", + ); + assert.deepEqual(pngDimensions(png), { width: 1, height: 1 }); + assert.equal(pngDimensions(Buffer.from("not-png")), null); + }); +}); + +describe("C3B blueprint build status", () => { + it("polls queued/provisioning/building and terminals on complete or failed", () => { + assert.equal(classifyBlueprintBuildStatus("queued"), "pending"); + assert.equal(classifyBlueprintBuildStatus("provisioning"), "pending"); + assert.equal(classifyBlueprintBuildStatus("building"), "pending"); + assert.equal(classifyBlueprintBuildStatus("build_complete"), "success"); + assert.equal(classifyBlueprintBuildStatus("failed"), "failure"); + assert.equal(classifyBlueprintBuildStatus("build_failed"), "failure"); + assert.equal(classifyBlueprintBuildStatus("mystery"), "failure"); + }); +}); + +describe("C3B graphical user isolation", () => { + it("drops Chrome and input to flok-ui without --no-sandbox", () => { + assert.equal(FLOK_UI_USER, "flok-ui"); + assert.equal(FLOK_UI_UID, 1500); + const chrome = chromeLaunchArgv("https://example.com/"); + assert.equal(chrome[0], "runuser"); + assert.equal(chrome[2], "flok-ui"); + assert.equal(chrome.includes("--no-sandbox"), false); + assert.equal(chrome.includes("--disable-setuid-sandbox"), false); + assert.ok(chrome.some((a) => a.startsWith("--user-data-dir="))); + const click = argvAsUiUser(["xdotool", "click", "1"]); + assert.deepEqual(click.slice(0, 3), ["runuser", "-u", "flok-ui"]); + }); +}); + +describe("C3B Dockerfile and ensure contract", () => { + const here = dirname(fileURLToPath(import.meta.url)); + const root = join(here, "../.."); + const dockerfile = readFileSync( + join(root, "blueprints/runloop-interactive/Dockerfile"), + "utf8", + ); + const ensureFile = readFileSync( + join(root, "blueprints/runloop-interactive/ensure-interactive.sh"), + "utf8", + ); + + it("extends the documented DnD Ubuntu base and stays root", () => { + assert.match(dockerfile, /^FROM runloop:runloop\/universal-ubuntu-24.04-x86_64-dnd$/m); + assert.equal(/^USER\s/m.test(dockerfile), false); + assert.match(dockerfile, /useradd -m -u 1500/); + assert.match(dockerfile, /command -v docker/); + assert.match(dockerfile, /command -v git/); + assert.match(dockerfile, /command -v python3/); + assert.match(dockerfile, /command -v node/); + assert.match(dockerfile, /chmod 4755/); + assert.match( + dockerfile, + /chmod 700 \/home\/user\/flok\/\.browser \/home\/user\/flok\/\.browser\/profile/, + ); + assert.equal(/^(RUN|CMD|ENTRYPOINT).*(--no-sandbox)/m.test(dockerfile), false); + assert.match(dockerfile, /chown root:root \/home\/user\/flok\/\.flok/); + assert.doesNotMatch(dockerfile, /chown -R flok-ui:flok-ui[^\n]*\.flok/); + }); + + it("ensure script is localhost-only, flok-ui only, and restarts X after resume", () => { + for (const src of [ENSURE_INTERACTIVE_SH, ensureFile]) { + assert.match(src, /refuse to start Chrome as root/); + assert.match(src, /x11vnc .* -localhost /); + assert.match(src, /127\.0\.0\.1:\$\{NOVNC_PORT\}/); + assert.match(src, /rm -f \/tmp\/\.X11-unix\/X99 \/tmp\/\.X99-lock/); + assert.match(src, /chmod 700 "\$PROFILE"/); + assert.match(src, /\/tmp\/flok-chrome\.log/); + assert.match(src, /not a regular file/); + assert.match(src, /chmod 640 \/tmp\/flok-chrome\.log/); + assert.match(src, /chown --no-dereference/); + assert.doesNotMatch(src, /chmod 644 \/tmp\/flok-chrome\.log/); + assert.match(src, /test -w "\$PROFILE"/); + assert.match(src, /chown root:root \/home\/user\/flok\/\.flok/); + assert.match(src, /chown -R "\$UI_USER:\$UI_USER" \/home\/user\/flok\/\.browser/); + assert.doesNotMatch(src, /chown -R .* \/home\/user\/flok\/\.browser \/home\/user\/flok\/\.flok/); + assert.doesNotMatch(src, /--no-sandbox/); + assert.doesNotMatch(src, /chmod 777/); + assert.doesNotMatch(src, /--disable-setuid-sandbox/); + } + }); + + it("ENSURE_INTERACTIVE_SH matches ensure-interactive.sh from set -euo pipefail", () => { + const body = (src: string): string => { + const i = src.indexOf("set -euo pipefail"); + assert.ok(i >= 0, "missing set -euo pipefail"); + return src.slice(i).replace(/\s+$/, ""); + }; + assert.equal(body(ENSURE_INTERACTIVE_SH), body(ensureFile)); + }); +}); + +describe("C3B RunloopProvider (memory)", () => { + it("hard-locks Nexus", () => { + assert.equal(FLAGS.FLOK_NEXUS_IQ_ENABLED, false); + assertNexusDisabled(); + }); + + it("advertises computerUse true after the paid C3B live gate", () => { + const caps = provider().capabilities(); + assert.equal(caps.computerUse, true); + assert.equal(caps.vnc, false); + assert.equal(caps.accessibility, false); + assert.equal(caps.pauseMemory, false); + }); + + it("ensureInteractiveStack is idempotent", async () => { + const plane = new MemoryRunloopControlPlane(); + const p = new RunloopProvider({ client: plane, blueprint: "memory" }); + const a = await p.provision({ birdId: "idemp", flockId: "f" }); + const session = await plane.get(a.providerRef); + const first = (session as unknown as { stackStarts: number }).stackStarts; + await session.ensureInteractiveStack(); + await session.ensureInteractiveStack(); + const second = (session as unknown as { stackStarts: number }).stackStarts; + assert.equal(first, 1); + assert.equal(second, 1); + }); + + it("resume recreates the graphical stack", async () => { + const plane = new MemoryRunloopControlPlane(); + const p = new RunloopProvider({ client: plane, blueprint: "memory" }); + const a = await p.provision({ birdId: "resume-ui", flockId: "f" }); + const session = await plane.get(a.providerRef); + assert.equal((session as unknown as { stackStarts: number }).stackStarts, 1); + await p.pause(a.providerRef); + await p.wake(a.providerRef); + assert.equal((session as unknown as { stackStarts: number }).stackStarts, 2); + }); + + it("re-ensures after in-session stack death without suspend", async () => { + const plane = new MemoryRunloopControlPlane(); + const p = new RunloopProvider({ client: plane, blueprint: "memory" }); + const a = await p.provision({ birdId: "stack-death", flockId: "f" }); + const session = await plane.get(a.providerRef); + assert.equal((session as unknown as { stackStarts: number }).stackStarts, 1); + (session as unknown as { markStackDown: () => void }).markStackDown(); + await p.observe(a.providerRef, { includeScreenshot: true }); + assert.equal((session as unknown as { stackStarts: number }).stackStarts, 2); + }); + + it("destroys the Devbox if ensure fails during provision", async () => { + const plane = new MemoryRunloopControlPlane(); + plane.failNextEnsure = true; + const p = new RunloopProvider({ client: plane, blueprint: "memory" }); + await assert.rejects(() => p.provision({ birdId: "leak-prov", flockId: "f" })); + assert.ok(plane.lastCreatedId); + await assert.rejects(() => plane.get(plane.lastCreatedId as string)); + }); + + it("destroys the restored Devbox if ensure fails", async () => { + const plane = new MemoryRunloopControlPlane(); + const p = new RunloopProvider({ client: plane, blueprint: "memory" }); + const a = await p.provision({ birdId: "leak-restore", flockId: "f" }); + const ck = await p.checkpoint(a.providerRef); + plane.failNextEnsure = true; + await assert.rejects(() => + p.restore({ + computerId: a.providerRef, + checkpointId: ck.providerSnapshotRef, + providerSnapshotRef: ck.providerSnapshotRef, + }), + ); + assert.ok(plane.lastCreatedId); + assert.notEqual(plane.lastCreatedId, a.providerRef); + await assert.rejects(() => plane.get(plane.lastCreatedId as string)); + const original = await plane.get(a.providerRef); + assert.equal((original as unknown as { destroyed: boolean }).destroyed, false); + }); + + it("observe screenshot shape without fabricating accessibility", async () => { + const p = provider(); + const a = await p.provision({ birdId: "obs", flockId: "f" }); + const obs = await p.observe(a.providerRef, { includeScreenshot: true }); + assert.equal(obs.screenWidth, DISPLAY_WIDTH); + assert.equal(obs.screenHeight, DISPLAY_HEIGHT); + assert.ok(obs.screenshotBase64); + assert.equal(obs.accessibilitySummary, undefined); + await assert.rejects( + () => p.observe(a.providerRef, { includeAccessibility: true }), + (err: unknown) => err instanceof ComputerUseNotAvailable, + ); + }); + + it("act applies bounded actions and fails closed on click_element", async () => { + const p = provider(); + const a = await p.provision({ birdId: "act", flockId: "f" }); + const good = await p.act(a.providerRef, { + actions: [ + { type: "open_url", url: "https://example.com/" }, + { type: "click_coordinates", x: 200, y: 200 }, + { type: "type", text: "hi" }, + { type: "key", key: "Return" }, + { type: "scroll", y: 3 }, + { type: "wait", durationMs: 50 }, + ], + }); + assert.equal(good.ok, true); + assert.equal(good.results.length, 6); + const bad = await p.act(a.providerRef, { + actions: [{ type: "click_element", elementId: "nope" }], + }); + assert.equal(bad.ok, false); + assert.match(bad.results[0]?.error ?? "", /unsupported/); + }); + + it("does not shell-inject via type text", async () => { + const text = "$(reboot); rm -rf /"; + const argv = argvAsUiUser(["xdotool", "type", "--", text]); + assert.equal(argv[argv.length - 1], text); + assert.equal(argv.includes("--"), true); + assert.equal(argv.filter((a) => a === text).length, 1); + assert.equal(argv.includes("sh"), false); + assert.equal(argv.includes("-c"), false); + assert.equal(argv.includes(`/bin/sh -c ${text}`), false); + const p = provider(); + const a = await p.provision({ birdId: "inj", flockId: "f" }); + const r = await p.act(a.providerRef, { + actions: [{ type: "type", text }], + }); + assert.equal(r.ok, true); + }); + + it("stops the act batch after the first failure and does not send later input", async () => { + const p = provider(); + const a = await p.provision({ birdId: "batch-stop", flockId: "f" }); + const r = await p.act(a.providerRef, { + actions: [ + { type: "click_coordinates", x: 10, y: 10 }, + { type: "click_element", elementId: "nope" }, + { type: "type", text: "should-not-run" }, + { type: "key", key: "Return" }, + ], + }); + assert.equal(r.ok, false); + assert.equal(r.results.length, 4); + assert.equal(r.results[0]?.success, true); + assert.equal(r.results[1]?.success, false); + assert.match(r.results[1]?.error ?? "", /unsupported/); + assert.equal(r.results[2]?.success, false); + assert.equal(r.results[2]?.error, "not executed"); + assert.equal(r.results[3]?.success, false); + assert.equal(r.results[3]?.error, "not executed"); + }); + + it("uniqueObsShotPath is under .browser and unique per call", () => { + const a = uniqueObsShotPath(); + const b = uniqueObsShotPath(); + assert.notEqual(a, b); + assert.equal(a.startsWith(`${OBS_SHOT_DIR}/obs-`), true); + assert.equal(b.startsWith(`${OBS_SHOT_DIR}/obs-`), true); + assert.equal(a.endsWith(".png"), true); + assert.equal(a.includes("/.flok/"), false); + }); + + it("browser profile lives under the workspace jail", async () => { + const p = provider(); + const a = await p.provision({ birdId: "prof", flockId: "f" }); + await p.act(a.providerRef, { + actions: [{ type: "open_url", url: "https://example.com/" }], + }); + const marker = await p.filesystem(a.providerRef, { + operation: "read", + path: `${BROWSER_PROFILE_DIR}/last-url`, + }); + assert.equal(marker.ok, true); + assert.equal(marker.data, "https://example.com/"); + }); + + it("two Devboxes do not share browser profiles", async () => { + const p = provider(); + const a = await p.provision({ birdId: "pa", flockId: "f" }); + const b = await p.provision({ birdId: "pb", flockId: "f" }); + await p.act(a.providerRef, { + actions: [{ type: "open_url", url: "https://a.example/" }], + }); + const fromB = await p.filesystem(b.providerRef, { + operation: "read", + path: `${BROWSER_PROFILE_DIR}/last-url`, + }); + assert.equal(fromB.ok, false); + }); + + it("profile survives suspend/resume (disk, not process)", async () => { + const p = provider(); + const a = await p.provision({ birdId: "persist", flockId: "f" }); + await p.act(a.providerRef, { + actions: [{ type: "open_url", url: "https://keep.example/" }], + }); + await p.pause(a.providerRef); + await p.wake(a.providerRef); + const kept = await p.filesystem(a.providerRef, { + operation: "read", + path: `${BROWSER_PROFILE_DIR}/last-url`, + }); + assert.equal(kept.ok, true); + assert.equal(kept.data, "https://keep.example/"); + }); + + it("takeover remains fail-closed; vnc capability false", async () => { + const p = provider(); + assert.equal(p.capabilities().vnc, false); + const a = await p.provision({ birdId: "vnc", flockId: "f" }); + await assert.rejects(() => p.takeover(a.providerRef)); + }); + + it("memory-plane last-url/launched markers are not a real Chrome profile", async () => { + const p = provider(); + const a = await p.provision({ birdId: "fake-profile", flockId: "f" }); + await p.act(a.providerRef, { + actions: [ + { type: "open_url", url: "https://example.com/" }, + { type: "launch_application", application: "browser" }, + ], + }); + const listed = await p.filesystem(a.providerRef, { + operation: "list", + path: BROWSER_PROFILE_DIR, + }); + assert.equal(listed.ok, true); + assert.ok(Array.isArray(listed.data)); + assert.equal( + chromeProfileHasBrowserState(listed.data as string[]), + false, + "memory-plane last-url/launched must not count as Chrome browser state", + ); + }); +}); + +const CHROME_CMD = + "1500 google-chrome-stable --user-data-dir=/home/user/flok/.browser/profile --window-size=1440,900 --app=file:///home/user/flok/.flok/fixture.html"; + +function evidence(over: Partial = {}): ChromeReadyEvidence { + return { + chromeCmdlines: [], + profileEntries: [], + profileEntriesUi: [], + profileUid: 1500, + profileGid: 1500, + profileMode: "700", + profileWritableByUi: true, + browserDirMode: "700", + workspaceMode: "775", + sandboxPath: "/opt/google/chrome/chrome-sandbox", + sandboxMode: "4755", + sandboxNosuid: false, + xvfbAlive: true, + openboxAlive: true, + display: ":99", + visibleWindows: [], + homeFallbackEntries: [], + chromeLogTail: "", + logHasSandboxError: false, + logHasChromeOutput: false, + unprivilegedUserns: "1", + ...over, + }; +} + +describe("C3B Chrome readiness classification", () => { + it("is ready when flok-ui Chrome has user-data-dir and real profile state", () => { + const result = classifyChromeReadiness( + evidence({ + chromeCmdlines: [CHROME_CMD], + profileEntries: ["Default", "Local State"], + }), + ); + assert.equal(result.status, "ready"); + assert.equal(result.ready, true); + assert.equal(result.terminal, true); + }); + + it("is ready when a Chrome window is visible even before profile flush", () => { + const result = classifyChromeReadiness( + evidence({ + chromeCmdlines: [CHROME_CMD], + visibleWindows: ["chrome"], + }), + ); + assert.equal(result.status, "ready"); + assert.equal(result.ready, true); + const live = classifyChromeReadiness( + evidence({ + chromeCmdlines: [CHROME_CMD], + visibleWindows: ["chrome"], + }), + { requireProfile: true }, + ); + assert.equal(live.status, "pending"); + assert.equal(live.ready, false); + }); + + it("treats an empty profile as not ready", () => { + const pending = classifyChromeReadiness(evidence({ chromeCmdlines: [CHROME_CMD] })); + assert.equal(pending.status, "pending"); + assert.equal(pending.ready, false); + assert.equal(pending.terminal, false); + const timed = classifyChromeReadiness(evidence({ chromeCmdlines: [CHROME_CMD] }), { + timedOut: true, + }); + assert.equal(timed.status, "readiness_timeout"); + assert.equal(timed.ready, false); + assert.equal(timed.terminal, true); + }); + + it("does not treat memory-plane test markers as Chrome browser state", () => { + assert.equal(chromeProfileHasBrowserState(["last-url"]), false); + assert.equal(chromeProfileHasBrowserState(["launched"]), false); + assert.equal(chromeProfileHasBrowserState(["c3b-marker"]), false); + assert.equal(chromeProfileHasBrowserState([".pki"]), false); + assert.equal(chromeProfileHasBrowserState(["Default"]), true); + assert.equal(chromeProfileHasBrowserState(["Local State"]), true); + const result = classifyChromeReadiness( + evidence({ + chromeCmdlines: [CHROME_CMD], + profileEntries: ["last-url", "c3b-marker", "launched"], + }), + ); + assert.equal(result.status, "pending"); + assert.equal(result.ready, false); + }); + + it("classifies never_started and started_then_exited on timeout", () => { + const never = classifyChromeReadiness(evidence(), { timedOut: true }); + assert.equal(never.status, "never_started"); + const exited = classifyChromeReadiness( + evidence({ logHasChromeOutput: true, chromeLogTail: "chrome failed" }), + { timedOut: true }, + ); + assert.equal(exited.status, "started_then_exited"); + }); + + it("refuses --no-sandbox and --disable-setuid-sandbox immediately", () => { + const noSandbox = classifyChromeReadiness( + evidence({ + chromeCmdlines: [`${CHROME_CMD} --no-sandbox`], + profileEntries: ["Default"], + }), + ); + assert.equal(noSandbox.status, "sandbox_disabled"); + assert.equal(noSandbox.ready, false); + assert.equal(noSandbox.terminal, true); + const disableSetuid = classifyChromeReadiness( + evidence({ chromeCmdlines: [`${CHROME_CMD} --disable-setuid-sandbox`] }), + ); + assert.equal(disableSetuid.status, "sandbox_disabled"); + assert.equal(chromeHasNoSandbox(CHROME_CMD), false); + assert.equal(chromeHasNoSandbox(`${CHROME_CMD} --no-sandbox`), true); + assert.equal(chromeHasDisableSetuidSandbox(`${CHROME_CMD} --disable-setuid-sandbox`), true); + assert.equal(chromeSandboxDisabled(`${CHROME_CMD} --no-sandbox`), true); + }); + + it("classifies permissions_failure when the profile is not writable by flok-ui", () => { + const result = classifyChromeReadiness( + evidence({ + chromeCmdlines: [CHROME_CMD], + profileWritableByUi: false, + profileUid: 0, + profileMode: "755", + }), + ); + assert.equal(result.status, "permissions_failure"); + assert.equal(result.terminal, true); + assert.match(result.message, /not writable/); + }); + + it("classifies display_failure when Chrome is alive without Xvfb", () => { + const result = classifyChromeReadiness( + evidence({ chromeCmdlines: [CHROME_CMD], xvfbAlive: false }), + ); + assert.equal(result.status, "display_failure"); + assert.equal(result.terminal, true); + }); + + it("waits on HOME fallback during startup, then classifies profile_redirected", () => { + const ev = evidence({ + chromeCmdlines: [CHROME_CMD], + homeFallbackEntries: ["Default"], + }); + const pending = classifyChromeReadiness(ev); + assert.equal(pending.status, "pending"); + assert.equal(pending.terminal, false); + const redirected = classifyChromeReadiness(ev, { timedOut: true }); + assert.equal(redirected.status, "profile_redirected"); + assert.match(redirected.message, new RegExp(CHROME_HOME_FALLBACK_DIR)); + }); + + it("classifies sandbox_failure from startup log only after timeout", () => { + const ev = evidence({ + chromeCmdlines: [CHROME_CMD], + logHasSandboxError: true, + logHasChromeOutput: true, + chromeLogTail: "The SUID sandbox helper binary was found, but is not configured correctly", + }); + const pending = classifyChromeReadiness(ev); + assert.equal(pending.status, "pending"); + const failed = classifyChromeReadiness(ev, { timedOut: true }); + assert.equal(failed.status, "sandbox_failure"); + }); + + it("becomes ready on the third probe via pollUntilChromeReady", async () => { + let n = 0; + let now = 0; + const { result, evidence: ev } = await pollUntilChromeReady( + async () => { + n += 1; + if (n < 3) { + return evidence({ chromeCmdlines: [CHROME_CMD], visibleWindows: ["chrome"] }); + } + return evidence({ + chromeCmdlines: [CHROME_CMD], + profileEntries: ["Default"], + }); + }, + { + timeoutMs: 10_000, + intervalMs: 100, + requireProfile: true, + now: () => now, + sleep: async (ms) => { + now += ms; + }, + }, + ); + assert.equal(result.status, "ready"); + assert.equal(n, 3); + assert.deepEqual(ev.profileEntries, ["Default"]); + }); + + it("pollUntilChromeReady terminals on classified failure without waiting out the clock", async () => { + let n = 0; + const { result } = await pollUntilChromeReady( + async () => { + n += 1; + return evidence({ + chromeCmdlines: [`${CHROME_CMD} --no-sandbox`], + }); + }, + { + timeoutMs: 20_000, + intervalMs: 500, + now: () => 0, + sleep: async () => { + throw new Error("should not sleep after sandbox_disabled"); + }, + }, + ); + assert.equal(result.status, "sandbox_disabled"); + assert.equal(n, 1); + }); + + it("sanitizes Chrome logs and formats classified failures", () => { + const raw = sanitizeChromeLog("ok\nAuthorization: Bearer secret\napi_key=abc\nchrome started"); + assert.doesNotMatch(raw, /Bearer secret/); + assert.doesNotMatch(raw, /api_key=abc/); + assert.match(raw, /chrome started/); + const failure = formatChromeReadyFailure( + { + status: "permissions_failure", + ready: false, + terminal: true, + message: "profile not writable by flok-ui", + }, + evidence({ profileMode: "755", profileUid: 0, profileWritableByUi: false }), + ); + assert.match(failure, /permissions_failure/); + assert.match(failure, /profile mode=755 uid=0/); + assert.doesNotMatch(failure, /RUNLOOP_API_KEY/); + }); + + it("parses probe JSON and keeps CHROME_READY_PROBE_PY guest-local", () => { + const parsed = parseChromeReadyEvidence( + JSON.stringify( + evidence({ + chromeCmdlines: [CHROME_CMD], + profileEntries: ["Default"], + chromeLogTail: "api_key=should-strip\nready", + }), + ), + ); + assert.equal(parsed.profileEntries[0], "Default"); + assert.equal(chromeHasUserDataDir(parsed.chromeCmdlines[0] ?? ""), true); + assert.doesNotMatch(parsed.chromeLogTail, /api_key/); + assert.match(CHROME_READY_PROBE_PY, /\/tmp\/flok-chrome\.log/); + assert.match(CHROME_READY_PROBE_PY, /\/home\/user\/flok\/\.browser\/profile/); + assert.match(CHROME_READY_PROBE_PY, /flok-ui/); + assert.match(CHROME_READY_PROBE_PY, /unprivileged_userns_clone/); + assert.equal(CHROME_LOG_PATH, "/tmp/flok-chrome.log"); + assert.equal(CHROME_READY_TIMEOUT_MS, 20_000); + assert.equal(CHROME_READY_POLL_MS, 500); + }); +}); diff --git a/tests/providers/runloop.test.ts b/tests/providers/runloop.test.ts index 5f0f4ad..9b3f263 100644 --- a/tests/providers/runloop.test.ts +++ b/tests/providers/runloop.test.ts @@ -20,6 +20,10 @@ import { CONTROL_PLANE_SECRET_ENV_KEYS, RUNLOOP_WORKSPACE_ROOT, } from "../../src/lib/computers/providers/index.js"; +import { + DISPLAY_HEIGHT, + DISPLAY_WIDTH, +} from "../../src/lib/computers/providers/runloop-interactive.js"; function provider(): RunloopProvider { return new RunloopProvider({ @@ -33,7 +37,7 @@ describe("RunloopProvider (no network)", () => { assert.equal(provider().name, "runloop"); }); - it("advertises honest C3A capabilities", () => { + it("advertises honest C3B capabilities", () => { const caps = provider().capabilities(); assert.equal(caps.linuxVm, true); assert.equal(caps.windowsVm, false); @@ -42,7 +46,7 @@ describe("RunloopProvider (no network)", () => { assert.equal(caps.forks, true); assert.equal(caps.customImages, true); assert.equal(caps.networkPolicy, true); - assert.equal(caps.computerUse, false); + assert.equal(caps.computerUse, true); assert.equal(caps.vnc, false); assert.equal(caps.accessibility, false); }); @@ -177,17 +181,13 @@ describe("RunloopProvider (no network)", () => { assert.equal(r.data, "hello-runloop"); }); - it("observe / act / takeover are C3B not implemented", async () => { + it("observe returns screenshot; takeover stays fail-closed", async () => { const p = provider(); const a = await p.provision({ birdId: "c3b", flockId: "f" }); - await assert.rejects( - () => p.observe(a.providerRef, { includeScreenshot: true }), - (err: unknown) => err instanceof ComputerUseNotAvailable, - ); - await assert.rejects( - () => p.act(a.providerRef, { actions: [] }), - (err: unknown) => err instanceof ComputerUseNotAvailable, - ); + const obs = await p.observe(a.providerRef, { includeScreenshot: true }); + assert.equal(obs.screenWidth, DISPLAY_WIDTH); + assert.equal(obs.screenHeight, DISPLAY_HEIGHT); + assert.ok(obs.screenshotBase64 && obs.screenshotBase64.length > 10); await assert.rejects( () => p.takeover(a.providerRef), (err: unknown) => err instanceof ComputerUseNotAvailable,