# syntax=docker/dockerfile:1.7
# One image for all services; the first argument picks the role: `web` (default), `worker` or
# `sandbox` (runs the quality eval's coding answers, see compose.yaml).

FROM node:24-trixie-slim AS node

# --- build -------------------------------------------------------------------
FROM node AS build
ENV NEXT_TELEMETRY_DISABLED=1
WORKDIR /app
COPY package.json package-lock.json ./
# npm's download cache and Turbopack's build cache (.next/cache) are cache mounts: kept between
# builds on the same machine, never part of an image layer.
RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
# Standalone output: the web server plus only the node_modules files it uses. The worker, migrate
# and sandbox scripts are bundled (esbuild) and only need @libsql/client from those.
RUN --mount=type=cache,target=/app/.next/cache \
    NEXT_OUTPUT=standalone npm run build \
 && npm run build:worker

# --- downloads ---------------------------------------------------------------
# Pinned release archives, in their own stage: only what is installed from them reaches the image.
FROM python:3.13-slim-trixie AS downloads
# BetterBench (Apache-2.0), the benchmark run against endpoints. Not on PyPI: the release
# archive is pinned by checksum; flags used by src/worker/betterbench.ts are verified per version.
ARG BETTERBENCH_VERSION=0.6.1
ADD --checksum=sha256:f0a9d7fcc411c809ff87a0a22e2d76f6662b426431d2ae9fa273bc2644464db7 \
    https://codeload.github.com/GGZ14/BetterBench/tar.gz/refs/tags/v${BETTERBENCH_VERSION} /downloads/betterbench.tar.gz
# L80 CLI (launch80.com): publishes a BetterBench run as a shareable page. Release archives pinned
# by checksum (from the release's checksums.txt); the per-architecture one is installed.
ARG L80_VERSION=0.4.0
ARG TARGETARCH
ADD --checksum=sha256:cde6b67dfb67b0ffddd56dfd384b8b24610573199287c59597359145310d9d23 \
    https://github.com/launch80/L80-Skills/releases/download/v${L80_VERSION}/L80_linux_amd64.tar.gz /downloads/l80/amd64.tar.gz
ADD --checksum=sha256:bbd7c6cf35bcba2bf9a651f348581b691b36d5f7cdca9c8a11e8a1ac2fc3f5bc \
    https://github.com/launch80/L80-Skills/releases/download/v${L80_VERSION}/L80_linux_arm64.tar.gz /downloads/l80/arm64.tar.gz
RUN mkdir -p /opt/l80 \
 && tar -xzf "/downloads/l80/${TARGETARCH:-amd64}.tar.gz" -C /opt/l80 L80 \
 && chmod 0755 /opt/l80/L80

# --- runtime -----------------------------------------------------------------
# Python base (provides the official `hf` CLI) plus the Node binary. The Python image already
# has CA certificates and libstdc++, so no apt.
FROM python:3.13-slim-trixie AS runtime
COPY --from=node /usr/local/bin/node /usr/local/bin/node
COPY --from=downloads /opt/l80/L80 /opt/l80/L80

# Pinned: the worker relies on the CLI's on-disk layout (see src/worker/hf-layout.ts);
# re-verify that before bumping.
ARG HF_HUB_VERSION=2.1.1
RUN --mount=type=bind,from=downloads,source=/downloads/betterbench.tar.gz,target=/tmp/betterbench.tar.gz \
    python -m venv /opt/hf \
 && /opt/hf/bin/pip install --no-cache-dir "huggingface_hub[hf_xet]==${HF_HUB_VERSION}" \
 && python -m venv /opt/betterbench \
 && /opt/betterbench/bin/pip install --no-cache-dir /tmp/betterbench.tar.gz \
 && rm -rf /root/.cache \
 && useradd --uid 1000 --create-home --shell /usr/sbin/nologin app \
 && mkdir -p /data /models /sandbox \
 && chown app:app /data /models \
 && chmod 0777 /sandbox

ARG APP_VERSION=dev
ENV PATH=/opt/hf/bin:$PATH \
    APP_VERSION=$APP_VERSION \
    NODE_ENV=production \
    NEXT_TELEMETRY_DISABLED=1 \
    PORT=3000 \
    HOSTNAME=0.0.0.0 \
    DATABASE_URL=file:/data/app.db \
    MODELS_DIR=/models \
    HF_HOME=/data/hf-home \
    BETTERBENCH_BIN=/opt/betterbench/bin/betterbench \
    L80_BIN=/opt/l80/L80

WORKDIR /app
# server.js, package.json, the traced node_modules (also what dist/ needs: @libsql/client) and .next.
COPY --from=build --chown=app:app /app/.next/standalone ./
COPY --from=build --chown=app:app /app/.next/static ./.next/static
COPY --from=build --chown=app:app /app/dist ./dist
COPY --from=build --chown=app:app /app/drizzle ./drizzle
COPY --chmod=755 docker-entrypoint.sh docker-healthcheck.sh /usr/local/bin/
# Next.js writes its cache at runtime, as whatever PUID the container runs as.
RUN mkdir -p .next/cache && chmod 0777 .next/cache

# Starts as root: docker-entrypoint.sh prepares /data (and an empty models folder) for PUID:PGID
# (default 1000:1000), then drops to that user. /sandbox is the exchange folder between the worker
# (PUID) and the sandbox (nobody); a fresh named volume copies its 0777.
EXPOSE 3000
HEALTHCHECK --interval=15s --timeout=5s --start-period=30s --retries=3 CMD ["docker-healthcheck.sh"]
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["web"]
