Files
opencode-manager/Dockerfile.sandbox
T
Chris Scott 4862d9c5cb fix(sandbox): wait for guest agent, verify exec user provisioning, pin rebuilt image (#343)
* fix(sandbox): wait for the guest agent before provisioning the exec user

A freshly created microVM does not accept msb exec until its guest agent is
up, so provisioning raced the boot and died on three 15s timeouts, which then
failed the whole enforcement-enable path. Gate provisioning on msb ping
bounded by SANDBOX_START_TIMEOUT_MS, give the exec a 30s budget, and downgrade
a provisioning failure to a warning so a missing sudo entry never blocks
sandboxed commands.

* fix(sandbox): align canonical shell spec with msb and pin the rebuilt guest image

- pass --shell /bin/sh explicitly at create and derive the canonical
  spec from the create args so msb 0.6.15 recordings match attestation
- bump the SANDBOX_IMAGE pin to the rebuilt guest image digest
- verify the provisioned exec user inside the guest and give the
  provisioning exec the full start timeout, bounded by a deadline

* docs(sandbox): sync the image digest pin into the docker and environment docs

* fix(sandbox): verify the guest image as an unknown uid and pin the rebuild

The build verification ran as uid 1000, which is the base image's node user,
so it exercised the known-user path and would have passed even if the numeric
exec uid the runtime actually uses were broken. Verify the toolchain as uid
4242 instead and keep the sudo check on the known user, since sudo refuses
unknown uids by design. Also only write the shadow entry when the passwd entry
is created, so a uid that already exists leaves no orphan record.

* fix(sandbox): close spawned command stdin so msb exec completes

msb exec reads its stdin during session setup and waits for EOF, so
every child spawned through executeCommand with an open stdin pipe hung
until its timeout while identical CLI execs finished instantly. Spawn
children with stdin ignored.

Also verify the provisioned exec user from a fresh guest exec after
each provisioning attempt and move failed provisioning to a logged
background retry bounded by five attempts so a slow guest no longer
blocks enforcement or leaves sudo silently missing.

* test(sandbox): derive expected inspect spec from canonical builders

* fix(sandbox): cancel provisioning retries on stop and serialize attempts

Advance the provisioning retry generation and drop the pending retry at the
start of every managed-sandbox stop, so a retry scheduled before a toggle-off
no longer pings and provisions a stopped sandbox. Clearing the promise as well
keeps later retries schedulable, since the generation guard would otherwise
strand it and short-circuit scheduling.

Route every provisioning caller through a single in-flight promise so the
boot-path attempt and the background retry cannot run the check-then-append
passwd and shadow script concurrently and append duplicate account entries.
2026-08-28 19:23:40 -04:00

91 lines
5.4 KiB
Docker

# Guest image for the agent sandbox microVM (SANDBOX_IMAGE).
#
# node:24 is buildpack-deps based, so gcc/g++/make/ld/pkg-config, glib-2.0,
# git, ssh, python3 and curl are already present. This image adds the rest of
# the Manager image toolchain (pnpm, bun, uv, jq) and the GitHub CLI and a
# Playwright-managed Chromium.
FROM node:24
ARG PLAYWRIGHT_VERSION=1.56.0
# Bump TOOLS_CACHEBUST (e.g. via --build-arg) to force a fresh tool install
# without invalidating the rest of the build cache.
ARG TOOLS_CACHEBUST=0
# Shared browser location so the sandbox exec user (SANDBOX_EXEC_USER, default
# "node") can read browsers installed at build time by root.
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
# Lets agent code `require("playwright")` from any working directory without a
# project-local install. Only a resolution fallback; local node_modules wins.
ENV NODE_PATH=/usr/local/lib/node_modules
# msb exec runs commands as a numeric host uid with no /etc/passwd entry, so
# the guest defaults to HOME=/ and every tool that writes per-user state (uv
# and pip caches, git config, gh config, the pnpm store) fails with EACCES.
# Point HOME at a sticky world-writable directory and give corepack a shared
# prewarmed cache so the preinstalled pnpm resolves without network or root.
ENV HOME=/home/ocm-agent
ENV COREPACK_HOME=/usr/local/share/corepack
ENV UV_TOOL_BIN_DIR=/opt/agent-tools/bin
ENV PNPM_HOME=/opt/agent-tools
ENV PATH=/opt/agent-tools/bin:$PATH
RUN echo "Installing gh and CLI tooling (cachebust=${TOOLS_CACHEBUST})" && \
apt-get update && \
apt-get install -y --no-install-recommends \
ca-certificates curl gh jq less ripgrep sudo tree file procps \
python3-pip python3-venv && \
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg \
-o /usr/share/keyrings/githubcli-archive-keyring.gpg && \
chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg && \
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \
> /etc/apt/sources.list.d/github-cli.list && \
apt-get update && \
apt-get install -y --no-install-recommends gh && \
rm -rf /var/lib/apt/lists/* && \
rm -f /usr/lib/python3*/EXTERNALLY-MANAGED && \
printf 'Defaults env_keep += "npm_config_prefix"\nALL ALL=(ALL:ALL) NOPASSWD:ALL\n' \
> /etc/sudoers.d/ocm-guest && \
chmod 440 /etc/sudoers.d/ocm-guest && \
visudo -c && \
gh --version
RUN echo "Installing pnpm, bun and uv (cachebust=${TOOLS_CACHEBUST})" && \
mkdir -p /home/ocm-agent /opt/agent-tools/bin /usr/local/share/corepack && \
chmod 1777 /home/ocm-agent /opt/agent-tools /opt/agent-tools/bin /usr/local/share/corepack && \
corepack enable && corepack prepare pnpm@latest --activate && \
curl -fsSL https://bun.sh/install | BUN_INSTALL=/opt/bun bash && \
chmod -R a+rX /opt/bun && \
ln -sf /opt/bun/bin/bun /opt/bun/bin/bunx && \
ln -s /opt/bun/bin/bun /usr/local/bin/bun && \
ln -s /opt/bun/bin/bun /usr/local/bin/bunx && \
curl -LsSf https://astral.sh/uv/install.sh | UV_INSTALL_DIR=/usr/local/bin UV_NO_MODIFY_PATH=1 sh && \
rm -rf /root/.npm /root/.cache/uv /home/ocm-agent/.cache && \
pnpm --version && bun --version && uv --version
RUN echo "Installing playwright=${PLAYWRIGHT_VERSION} chromium (cachebust=${TOOLS_CACHEBUST})" && \
npm install -g "playwright@${PLAYWRIGHT_VERSION}" && \
npx --yes "playwright@${PLAYWRIGHT_VERSION}" install --with-deps chromium && \
chmod -R a+rX "${PLAYWRIGHT_BROWSERS_PATH}" && \
rm -rf /var/lib/apt/lists/* /root/.npm && \
npx --yes "playwright@${PLAYWRIGHT_VERSION}" --version
# Declared after the Playwright layer on purpose: npm_config_prefix must not
# affect the root-run `npm install -g playwright`, which has to land in
# /usr/local/lib/node_modules for the NODE_PATH fallback above to work.
ENV npm_config_prefix=/opt/agent-tools
# The runtime exec user is a numeric host uid with no /etc/passwd entry, so the
# toolchain is verified as uid 4242, which is absent from this image's passwd
# database. uid 1000 would not do: it is the base image's `node` user, so it
# would exercise the known-user path and pass even if the real one were broken.
# sudo is checked separately as that known user, because sudo refuses unknown
# uids by design and the Manager provisions their passwd entry at runtime.
RUN echo "Verifying guest toolchain as an unknown uid (cachebust=${TOOLS_CACHEBUST})" && \
rm -rf /home/ocm-agent && \
mkdir -p /home/ocm-agent && \
chmod 1777 /home/ocm-agent && \
setpriv --reuid=4242 --regid=4242 --clear-groups sh -c "set -e; pnpm --version; bun --version; bunx --version; uv --version; uvx --version; playwright --version; gh --version; jq --version; rg --version; npm install -g --silent cowsay && cowsay -t verify-ok | head -1; git config --global user.email verify@ocm.local; uv venv /tmp/uv-check >/dev/null; uv pip install --python /tmp/uv-check/bin/python idna >/dev/null; pip3 install --user --force-reinstall --no-deps --quiet idna; rm -rf /tmp/uv-check" && \
setpriv --reuid=1000 --regid=1000 --clear-groups sh -c "set -e; sudo -n apt-get update -qq; sudo -n apt-get install -y -qq --no-install-recommends bc; bc --version | head -1" && \
rm -rf /home/ocm-agent /opt/agent-tools /var/lib/apt/lists/* && \
mkdir -p /home/ocm-agent /opt/agent-tools/bin && \
chmod 1777 /home/ocm-agent /opt/agent-tools /opt/agent-tools/bin