# Disposable runner image for real-agent campaigns (Stage 5 containment).
#
# Build context is the benchmarks repo root (wrangler.jsonc image_build_context).
# One container instance runs one campaign job, then is destroyed. Inside it:
#   - the harness, journal and oracle run as root under /work/<job> (mode 700);
#   - the agent CLI runs as the unprivileged `agent` user via
#     /usr/local/bin/claude-contained, which can write only its trial repo and
#     fresh HOME, and cannot read the journal, plan, other trials or /proc
#     environments of root processes;
#   - containment_check.py proves that before any trial is admitted.
# Independent Git instrument for the trusted oracle: a separately built,
# pinned Git (not the Git under test), so Git rows get the same
# independent_committed_tree evidence class as Oak rows read by the
# independent SQLite reader. Without it the campaign report refuses to pair
# Git and Oak trials.
FROM debian:bookworm-slim AS git-instrument
ARG GIT_INSTRUMENT_VERSION=2.47.1
ARG GIT_INSTRUMENT_SHA256=f3d8f9bb23ae392374e91cd9d395970dabc5b9c5ee72f39884613cd84a6ed310
RUN apt-get update \
 && apt-get install -y --no-install-recommends build-essential ca-certificates curl xz-utils zlib1g-dev \
 && rm -rf /var/lib/apt/lists/*
RUN curl -fsSL -o /tmp/git.tar.xz "https://www.kernel.org/pub/software/scm/git/git-${GIT_INSTRUMENT_VERSION}.tar.xz" \
 && echo "${GIT_INSTRUMENT_SHA256}  /tmp/git.tar.xz" | sha256sum -c - \
 && tar -xJf /tmp/git.tar.xz -C /tmp \
 && make -C "/tmp/git-${GIT_INSTRUMENT_VERSION}" -j4 prefix=/opt/git-instrument \
      NO_CURL=1 NO_EXPAT=1 NO_GETTEXT=1 NO_TCLTK=1 NO_PERL=1 NO_PYTHON=1 NO_OPENSSL=1 install \
 && /opt/git-instrument/bin/git --version

FROM node:22-bookworm-slim

ARG OAK_VERSION=0.112.0
ARG OAK_SHA256=d01f0994284694897c85398ba0b8c23fdc66bc838192b5bf30abb9235384b174
ARG CLAUDE_CODE_VERSION=2.1.321

RUN apt-get update \
 && apt-get install -y --no-install-recommends git python3 ca-certificates curl procps util-linux zsh \
 && rm -rf /var/lib/apt/lists/*

# Oak: the published linux-x86_64 release, pinned by digest.
RUN curl -fsSL -o /usr/local/bin/oak "https://oak.space/api/releases/v${OAK_VERSION}/linux-x86_64" \
 && echo "${OAK_SHA256}  /usr/local/bin/oak" | sha256sum -c - \
 && chmod 755 /usr/local/bin/oak

RUN npm install -g "@anthropic-ai/claude-code@${CLAUDE_CODE_VERSION}" \
 && npm cache clean --force \
 && ln -s "$(command -v claude)" /usr/local/bin/claude-real

COPY --from=git-instrument /opt/git-instrument /opt/git-instrument

RUN useradd --create-home --uid 1001 --shell /bin/sh agent

# Harness (read-only to the agent: root-owned, no group/other write).
COPY scripts /bench/scripts
COPY config /bench/config
COPY scenarios /bench/scenarios
COPY docs /bench/docs
COPY cloudflare/agent-runner/agents.toml cloudflare/agent-runner/server.py cloudflare/agent-runner/containment_check.py /opt/runner/
COPY cloudflare/agent-runner/claude-contained /usr/local/bin/claude-contained
RUN chmod -R go-w /bench /opt/runner \
 && chmod 755 /usr/local/bin/claude-contained \
 && mkdir -m 700 /work

# OAK_BENCH_READER_DEADLINE_SCALE: the independent Oak reader's safety
# deadline was calibrated on an M4; this runner's vCPU is several times slower.
ENV OAK_BENCH_LOCK_PATH=/work/.measurement.lock \
    OAK_BENCH_GIT_INSTRUMENT=/opt/git-instrument/bin/git \
    OAK_BENCH_READER_DEADLINE_SCALE=4 \
    OAK_NO_UPDATE_CHECK=1 \
    PYTHONDONTWRITEBYTECODE=1
EXPOSE 8080
CMD ["python3", "-I", "/opt/runner/server.py"]
