# Runs the pi coding agent with these pi-customizations (extensions, agents,
# prompt templates) inside an NVIDIA OpenShell sandbox (Bring Your Own Container).
#
# Build + run via OpenShell:  openshell sandbox create --from . -- pi
# The sandbox supervisor replaces CMD at runtime, so the start command ("pi")
# must be passed explicitly after `--`.
#
# Provider credentials (ANTHROPIC_API_KEY, etc.) are read from the environment
# at runtime and are intentionally NOT baked into the image.

FROM node:22-trixie-slim

# System tools:
# - iproute2 / iptables: OpenShell network-namespace management + bypass detection
# - git / openssh-client: used by the git-checkpoint and ssh-hosts extensions
# - ca-certificates / curl: TLS roots and a basic fetch tool
RUN apt-get update && apt-get install -y --no-install-recommends \
        iproute2 iptables git openssh-client ca-certificates curl \
    && rm -rf /var/lib/apt/lists/*

# Install the pi coding agent globally. Override with --build-arg PI_VERSION=...
ARG PI_VERSION=0.75.3
RUN npm install -g @earendil-works/pi-coding-agent@${PI_VERSION}

# kubectl for the k8s extension. Pinned for reproducible builds; bump with
# --build-arg KUBECTL_VERSION=vX.Y.Z. The client is version-skew tolerant
# within ±1 minor against the target cluster.
ARG KUBECTL_VERSION=v1.29.3
RUN curl -fsSL "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl" \
        -o /usr/local/bin/kubectl && \
    chmod 0755 /usr/local/bin/kubectl

RUN userdel -r node && groupdel node 2>/dev/null; \
    groupadd -g 1000 sandbox && \
    useradd -u 1000 -g 1000 -d /sandbox -s /bin/bash sandbox

# Bake this pi-customizations package into the image at /app/pi-customizations.
# OpenShell's default Landlock policy allowlists /app for read access from the
# sandbox; /opt is not on the allowlist, so installing there causes
# "Permission denied" inside the sandbox even with permissive Unix perms.
COPY --chown=sandbox:sandbox . /app/pi-customizations

# Writable sandbox workdir owned by the sandbox user.
RUN install -d -o sandbox -g sandbox /sandbox
WORKDIR /sandbox

USER sandbox

# Install this package's runtime dependencies into /app/pi-customizations/node_modules.
# pi-provided imports (@earendil-works/*, typebox) resolve from pi's global install,
# but the websearch extension also pulls in linkedom + readability + turndown +
# turndown-plugin-gfm (declared in dependencies); those need to be present locally.
RUN cd /app/pi-customizations && npm ci --omit=dev --no-audit --no-fund

# Register the customizations with pi. A local-path install is recorded in
# ~/.pi/agent/settings.json without copying.
RUN pi install /app/pi-customizations

# Informational only — OpenShell's supervisor overrides this; launch with `-- pi`.
CMD ["pi"]
