# Node for the scripts the model runs, without npm or npx; the Grok Build CLI itself is a static binary.
FROM public.ecr.aws/docker/library/node:22-slim@sha256:c3de60bf2f9dd0ac6370e6117950ff62d6e339527e7472301c9c78a017978392 AS node

# The env's clock: the egress proxy, one static Go binary, and agent-env's libfaketime and its TLS shim, taken from
# agent-env's sources as clock/ and egress/ (shared_sources.toml) and built for this image's libc (Debian bookworm) in
# stages of their own, so no toolchain is left in the image.
FROM public.ecr.aws/docker/library/golang:1.26.11-bookworm@sha256:dc40a13b61304647ed7e5d581d246b86413dfc29d63c6d018310feb93cd98fc0 AS egress
COPY egress/ /egress/
RUN cd /egress && go vet ./... && go test -count=1 ./... \
    && CGO_ENABLED=0 GOFLAGS=-mod=readonly go build -trimpath -ldflags=-s -o /out/egress .

FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900 AS clock
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        build-essential patch curl ca-certificates
COPY clock/ /clock/
RUN sh /clock/build.sh /out

# The Grok Build CLI for this machine's platform: a static musl binary its npm platform package carries
# brotli-compressed, checked against the package's published integrity (base64 of its sha512) and decompressed in a
# stage of its own, so neither npm nor the package's postinstall runs, and brotli stays out of the image. install.sh
# brings the same build.
FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900 AS grok
ARG GROK_VERSION=1.0.92
ARG GROK_SHA512_X64=qwls1sZya5yZorl7QcSvICMfANANRUSnCHMLJozAGZrmwwXeuBScJEHijNF5sjrDb3o+ivO0u65vtQNeLPesCA==
ARG GROK_SHA512_ARM64=46QIUxdE7OP98cVzxEcMOZ8+aFUudQaOi/Wt7btE83xjpnHt2NbOGCNRLteAn5cAvIOJ+xF2hBpYdw8kVzB+Ug==
RUN set -eu; \
    apt-get update && apt-get install -y --no-install-recommends brotli ca-certificates curl; \
    case "$(uname -m)" in x86_64) cpu=x64 sha512=$GROK_SHA512_X64 ;; aarch64) cpu=arm64 sha512=$GROK_SHA512_ARM64 ;; \
      *) echo "Grok Build has no build for $(uname -m)" >&2; exit 1 ;; esac; \
    curl -fsSL --retry 3 -o /tmp/grok.tgz \
      "https://registry.npmjs.org/@xai-official/grok-linux-${cpu}/-/grok-linux-${cpu}-${GROK_VERSION}.tgz"; \
    echo "$(printf %s "$sha512" | base64 -d | od -An -vtx1 | tr -d ' \n')  /tmp/grok.tgz" | sha512sum -c -; \
    tar -xzf /tmp/grok.tgz -C /tmp package/bin/grok.br; \
    mkdir /out && brotli -d /tmp/package/bin/grok.br -o /out/grok && chmod 0755 /out/grok

FROM public.ecr.aws/docker/library/python:3.11-slim@sha256:c20888b6acdd1e63e1c433a185bf3ad162c0288fe484616ce062e0d28add2900
# poppler-utils: pdftoppm, for the pages of a PDF a task attaches. ripgrep: the CLI's search tools fall back to the one
# on PATH where the copy the CLI unpacks can't run.
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl poppler-utils ripgrep \
    && rm -rf /var/lib/apt/lists/*
COPY --from=grok /out/grok /usr/local/bin/grok
COPY --from=node /usr/local/bin/node /usr/local/bin/node
WORKDIR /opt/agent
COPY requirements.txt model-requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt -r model-requirements.txt
# install.sh and run.sh are install/v1's: in the image, so they stay in its build context.
COPY a2a_server.py grok_bridge.py grok_build_atif.py grok_date.py grok_kit.py entrypoint.sh install.sh run.sh ./
# Every clocked agent's egress rule, which the bridge imports, the clock page's format it reads, the model relay each
# CLI run calls the endpoint through, and the follower that feeds the atif/1 adapter (shared_sources.toml).
COPY agent_clock/agent_egress.py clock/clockpage.py model_relay/model_relay.py tail_follow/tail_follow.py ./
# The CLI's cwd, /app, and the agent's home, which holds each context's CLI state and the skills, fixed so neither
# follows the uid.
RUN useradd --create-home --uid 10001 agent && mkdir -p /app && chown agent:agent /app && chmod 0755 entrypoint.sh
# The env's clock: the follower and the egress proxy, each as its own user, under root's supervisord (root.conf), and
# agent-env's libfaketime and its TLS shim preloaded into every process, faking only the agent's (clock.conf). The
# follower's config is the agent's user's to write (agentenv_protocol.a2a_agent.clock) and the follower's to read.
RUN pip install --no-cache-dir supervisor==4.2.5 \
    && useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-clock \
    && useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-egress \
    && mkdir -p /usr/local/lib/agentenv-clock /usr/local/lib/agentenv-egress /etc/agentenv \
    && install -d -o agent -g agentenv-clock -m 2750 /etc/agentenv/clock \
    && touch /etc/agentenv/ca.pem \
    && cp /etc/ssl/certs/ca-certificates.crt /etc/agentenv/ca-bundle.pem \
    && cp /etc/ssl/certs/ca-certificates.crt /usr/local/lib/agentenv-egress/roots.pem
COPY --from=clock /out/libfaketimeMT.so.1 /out/libtls_realtime.so /usr/local/lib/agentenv-clock/
COPY --from=egress /out/egress /usr/local/lib/agentenv-egress/egress
COPY clock/clockpage.py clock/clock-follower /usr/local/lib/agentenv-clock/
COPY clock/clock.conf /etc/agentenv/clock.conf
COPY root.conf /etc/agentenv/root.conf
COPY start-agent /usr/local/bin/start-agent
RUN sed -i -e "s/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u agent)/" \
        -e "s/^AGENTENV_CLOCK_PAGE_UID=.*/AGENTENV_CLOCK_PAGE_UID=$(id -u agentenv-clock)/" /etc/agentenv/clock.conf \
    && chmod 0755 /usr/local/lib/agentenv-clock/clock-follower /usr/local/bin/start-agent \
    && printf '%s\n' /usr/local/lib/agentenv-clock/libfaketimeMT.so.1 /usr/local/lib/agentenv-clock/libtls_realtime.so \
        > /etc/ld.so.preload
# The CLI never updates itself or reports home, whichever process starts it.
ENV A2A_PORT=8000 PYTHONPATH=/opt/agent HOME=/home/agent GROK_DISABLE_AUTOUPDATER=1 DISABLE_TELEMETRY=1
WORKDIR /app
EXPOSE 8000
# Starts as root, which start-agent needs for the clock; the A2A server and the CLI run as the agent's user. CMD rather
# than ENTRYPOINT: Modal passes a sandbox's env to the image's CMD only.
CMD ["/usr/local/bin/start-agent"]
