# Keep this default aligned with infra.lib.config.DEFAULT_BASE_PYTHON_IMAGE.
ARG PYTHON_BASE_IMAGE=dhi.io/python:3.13-dev@sha256:cacda18986eefebc42b0c6c03565ec2d65a4507b6adff559e4daf0f1e2bfe338
# Keep this default aligned with infra.lib.config.DEFAULT_MEMORY_HIGH_AGENT_BASE_IMAGE.
ARG MEMORY_HIGH_AGENT_BASE_IMAGE=dhi.io/python:3.13@sha256:953d20534e4db354421e84f2b83967031b0b224202c719ffbe7485851e9cef60

FROM amazon/aws-cli:2.39.3@sha256:d557b3524475d70dd3104cf44d96568ee3188897b73a6ece2b37aa50e1af362f AS aws-cli
FROM ghcr.io/astral-sh/uv:0.13.7@sha256:96f40d3904aad8d0bd010539001d36c8e2188b981e1334471a90c4a1d93bb490 AS uv
FROM ${PYTHON_BASE_IMAGE} AS python

FROM --platform=$BUILDPLATFORM alpine:3.24@sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6 AS helm
ARG HELM_VERSION=3.21.3
# TARGETARCH, not `uname -m`: with --platform=$BUILDPLATFORM above this stage
# runs natively on the builder, so `uname -m` would report the *builder's* arch
# and fetch the wrong helm when cross-building. TARGETARCH is already spelled
# the way get.helm.sh names its tarballs (amd64/arm64), so it needs no mapping.
ARG TARGETARCH
# From the release's .sha256sum files; bump together with HELM_VERSION.
ARG HELM_SHA256_AMD64=15e041a93a590dce8100f39385cd98c84a765c9e36aeeb9e2dc6ff9e4769e2e0
ARG HELM_SHA256_ARM64=67f58155079ff9ffab98ba5c88daff0ed9b542f3a4732f5dd426dde7dd0f5244
RUN case "${TARGETARCH}" in \
      amd64) sha256=${HELM_SHA256_AMD64} ;; \
      arm64) sha256=${HELM_SHA256_ARM64} ;; \
      *) echo "no helm checksum for ${TARGETARCH}" >&2; exit 1 ;; \
    esac \
 && apk add --no-cache curl \
 && curl -fsSLo helm.tar.gz https://get.helm.sh/helm-v${HELM_VERSION}-linux-${TARGETARCH}.tar.gz \
 && echo "${sha256}  helm.tar.gz" | sha256sum -c - \
 && tar -zxvf helm.tar.gz \
 && mv linux-${TARGETARCH}/helm /helm

####################
##### BASE #####
####################
FROM python AS base

USER root

RUN --mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
    --mount=type=cache,target=/var/cache/apt,sharing=locked \
    apt-get update \
 && apt-get install -y --no-install-recommends \
        curl \
        git \
        libjemalloc2 \
        passwd

ARG USER_ID=65532
ARG GROUP_ID=65532
RUN (getent group nonroot > /dev/null 2>&1 && groupmod -g ${GROUP_ID} nonroot || groupadd -g ${GROUP_ID} nonroot) \
 && (id nonroot > /dev/null 2>&1 && usermod -u ${USER_ID} -g ${GROUP_ID} nonroot || useradd -u ${USER_ID} -g ${GROUP_ID} -m -d /home/nonroot nonroot) \
 && chown -R ${USER_ID}:${GROUP_ID} /home/nonroot

COPY --from=uv /uv /uvx /usr/local/bin/

ARG UV_PROJECT_ENVIRONMENT=/opt/python
ENV PATH=${UV_PROJECT_ENVIRONMENT}/bin:$PATH
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV UV_COMPILE_BYTECODE=1
# Inspect AI relies on the metadata to determine installation status:
ENV UV_NO_INSTALLER_METADATA=0
ENV UV_LINK_MODE=copy

####################
##### BUILDERS #####
####################
FROM base AS builder-base
WORKDIR /source
COPY pyproject.toml uv.lock ./
# `uv sync --locked` only reads the path dependencies' pyproject.toml (they are
# in dev-only groups, never installed here); copying their sources would rerun
# every venv install on each lambda .py change.
COPY services/modules/eval_log_importer/pyproject.toml services/modules/eval_log_importer/
COPY services/modules/eval_log_reader/pyproject.toml services/modules/eval_log_reader/
COPY services/modules/job_status_updated/pyproject.toml services/modules/job_status_updated/
COPY services/modules/sample_editor/pyproject.toml services/modules/sample_editor/

# The runner dependency graph depends on the lock, not Hawk's source. Keep that
# graph above the source boundary so application edits preserve the Git installs,
# virtualenvs, Restic cache, hardlink pass, and full bytecode compile. Nothing
# in the lock-only stages may read /source/hawk.
FROM builder-base AS builder-runner-lock
COPY README.md .python-version ./

# Install only third-party dependencies into each environment. Hawk is added in
# the final image as a small overlay; --no-install-project is the cache boundary
# that keeps these environments reusable across Hawk source edits.
FROM builder-runner-lock AS builder-runner-bootstrap-deps
ENV UV_PROJECT_ENVIRONMENT=/opt/hawk-bootstrap
ENV PATH=${UV_PROJECT_ENVIRONMENT}/bin:$PATH
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync \
        --extra=runner \
        --extra=inspect \
        --locked \
        --no-dev \
        --no-install-project

FROM builder-runner-lock AS builder-runner-runtime-deps
ENV UV_PROJECT_ENVIRONMENT=/opt/hawk-runtime
ENV PATH=${UV_PROJECT_ENVIRONMENT}/bin:$PATH
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync \
        --extra=runner \
        --extra=inspect \
        --extra=inspect-scout \
        --locked \
        --no-dev \
        --no-install-project

# Only needs the inspect CLI, which the lock pins, so this stays off the source
# path and its download survives a source edit.
FROM builder-runner-runtime-deps AS restic-cache
# Checkpoint injection only supports Linux sandboxes. Keep both Linux binaries
# because a sandbox may be scheduled on a different architecture from its
# runner, but do not ship binaries that a Kubernetes job can never select.
# Build the destination tree with fixed ownership and mtimes so the runner's
# COPY --link layer is byte-stable across otherwise unrelated image builds.
RUN HOME=/out/home/nonroot inspect download restic \
 && find /out/home/nonroot/.cache/inspect_ai/bin \
        -type f \
        -name 'restic_*' \
        ! -name 'restic_*_linux_amd64' \
        ! -name 'restic_*_linux_arm64' \
        -delete \
 && test -n "$(find /out/home/nonroot/.cache/inspect_ai/bin \
        -type f -name 'restic_*_linux_amd64' -print -quit)" \
 && test -n "$(find /out/home/nonroot/.cache/inspect_ai/bin \
        -type f -name 'restic_*_linux_arm64' -print -quit)" \
 && test -z "$(find /out/home/nonroot/.cache/inspect_ai/bin \
        -type f -name 'restic_*' \
        ! -name 'restic_*_linux_amd64' \
        ! -name 'restic_*_linux_arm64' -print -quit)" \
 && chown -R ${USER_ID}:${GROUP_ID} /out/home/nonroot \
 && chown root:root /out /out/home \
 && find /out -exec touch -h -t 202001010000.00 {} +

# Bootstrap and runtime remain separate: bootstrap must survive a failed user
# package mutation so it can report the error. Their third-party files are
# nevertheless mostly identical, so deduplicate those stable files before Hawk
# is installed. Shared inodes stay root-owned and immutable, while every runtime
# directory remains job-user-owned so uv can unlink and replace files safely.
FROM base AS builder-runner-dependency-environments
# With --prioritize-trees, the later tree supplies the retained inode. Keep the
# immutable bootstrap last; the assertions below also make this fail closed if
# hardlink's selection semantics ever change. Shared sources may have been
# installed seconds apart, so recompile after the retained inode changes their
# runtime mtime; otherwise Python discards timestamp bytecode on every import.
# Embedding bootstrap's source paths makes that bytecode byte-identical to
# bootstrap's, so a second pass shares it too; at import, Python rewrites each
# code object's filename to the runtime path it actually loaded. hardlink only
# joins files with equal modes, so bootstrap becomes read-only afterwards.
RUN --mount=type=bind,from=builder-runner-bootstrap-deps,source=/opt/hawk-bootstrap,target=/mnt/hawk-bootstrap,ro \
    --mount=type=bind,from=builder-runner-runtime-deps,source=/opt/hawk-runtime,target=/mnt/hawk-runtime,ro \
    cp -a /mnt/hawk-bootstrap /opt/hawk-bootstrap \
 && cp -a /mnt/hawk-runtime /opt/hawk-runtime \
 && chown -R nonroot:nonroot /opt/hawk-runtime \
 && hardlink --ignore-owner --ignore-time --respect-xattrs --prioritize-trees \
        /opt/hawk-runtime /opt/hawk-bootstrap \
 && setpriv --reuid=${USER_ID} --regid=${GROUP_ID} --clear-groups \
        /opt/hawk-runtime/bin/python -m compileall -q -f -j 0 \
        -s /opt/hawk-runtime -p /opt/hawk-bootstrap /opt/hawk-runtime \
 && hardlink --ignore-owner --ignore-time --respect-xattrs --prioritize-trees \
        /opt/hawk-runtime /opt/hawk-bootstrap \
 && chmod -R a-w /opt/hawk-bootstrap \
 && test -n "$(find /opt/hawk-runtime -type f -user root -links +1 -print -quit)" \
 && test -z "$(find /opt/hawk-bootstrap ! -type l -perm /022 -print -quit)" \
 && test -z "$(find /opt/hawk-runtime -type f -user root \
        \( -links 1 -o -perm /022 \) -print -quit)" \
 && test -z "$(find /opt/hawk-runtime -type f \
        ! -user root ! -user nonroot -print -quit)" \
 && test -z "$(find /opt/hawk-runtime \
        \( -type d -o -type l \) \
        ! -user nonroot -print -quit)"

####################################################################
##### Below this line, stages may depend on Hawk's source.     #####
####################################################################
FROM builder-base AS builder-api
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync \
        --extra=api \
        --locked \
        --no-dev \
        --no-install-project

################
##### PROD #####
################
FROM builder-runner-dependency-environments AS runner

# Reuse the environment baked into the official runner image by default.
# Operators can select fresh as an immediate rollback/debugging path.
ENV HAWK_RUNNER_ENV_STRATEGY=prebuilt
# Pre-exec subprocesses stay on the immutable bootstrap toolchain, so a package
# a job installs into the runtime can't shadow uv or git while setup runs.
# exec_python prepends the selected runtime's bin directory at execution.
ENV PATH=/opt/hawk-bootstrap/bin:$PATH

COPY --from=helm /helm /usr/local/bin/helm
# --link: the layer is built from the restic-cache stage alone, not this stage's
# snapshot, so its digest is identical on every build.
COPY --link --from=restic-cache /out/ /

WORKDIR /home/nonroot/app
# The base stage already contains both environments in one lock-dependent,
# hardlink-preserving layer. Keep every source-dependent overlay below it.
COPY pyproject.toml uv.lock README.md .python-version ./
COPY hawk ./hawk
COPY hawk_flags ./hawk_flags

# Only Hawk's package files vary with source edits. Install it directly from the
# retained root-owned source so the fresh rollback keeps its original local
# source spec; the lock-only stages above already installed both complete
# third-party graphs. uv rebuilds a local source tree on every install, so build
# both copies concurrently, the runtime's as the job user.
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=cache,target=/home/nonroot/.cache/uv,uid=${USER_ID},gid=${GROUP_ID} \
    uv pip install \
        --no-deps \
        --python=/opt/hawk-bootstrap/bin/python \
        /home/nonroot/app \
    & bootstrap_install=$! \
 && HOME=/home/nonroot setpriv \
        --reuid=${USER_ID} --regid=${GROUP_ID} --clear-groups \
        uv pip install \
        --no-deps \
        --python=/opt/hawk-runtime/bin/python \
        /home/nonroot/app \
 && wait "$bootstrap_install" \
 && uv pip check --python=/opt/hawk-bootstrap/bin/python \
 && uv pip check --python=/opt/hawk-runtime/bin/python \
 && test -z "$(find /opt/hawk-bootstrap ! -user root -print -quit)" \
 && test -z "$(find /opt/hawk-bootstrap ! -type l -perm /022 -print -quit)" \
 && test -n "$(find /opt/hawk-runtime -type f -user root -links +1 -print -quit)" \
 && test -z "$(find /opt/hawk-runtime -type f -user root \
        \( -links 1 -o -perm /022 \) -print -quit)" \
 && test -z "$(find /opt/hawk-runtime -type f \
        ! -user root ! -user nonroot -print -quit)" \
 && test -z "$(find /opt/hawk-runtime \
        \( -type d -o -type l \) \
        ! -user nonroot -print -quit)"

# Use jemalloc instead of glibc malloc to reduce long-run fragmentation.
# glibc per-thread arenas accumulate freed pages without returning them to the
# kernel; jemalloc's decay-based release is built for long-lived processes.
ENV LD_PRELOAD=libjemalloc.so.2
ENV MALLOC_CONF=background_thread:true,dirty_decay_ms:30000,muzzy_decay_ms:30000,metadata_thp:auto

# Commit-aware build version from Pulumi (hawk.core.version_stamp), reported in
# X-Hawk-Server-Build. Last, so a new version reuses every layer above; empty
# falls back to the package version.
ARG HAWK_BUILD_VERSION=""
ENV HAWK_BUILD_VERSION=${HAWK_BUILD_VERSION}

USER nonroot
STOPSIGNAL SIGINT

ENTRYPOINT ["/opt/hawk-bootstrap/bin/python", "-m", "hawk.runner.entrypoint"]

FROM base AS api
# Install graphviz. x11-common's postinst needs update-rc.d from init-system-helpers
RUN apt-get update \
 && apt-get install -y --no-install-recommends init-system-helpers graphviz \
 && rm -rf /var/lib/apt/lists/*

COPY --from=aws-cli /usr/local/aws-cli/v2/current /usr/local
COPY --from=helm /helm /usr/local/bin/helm

WORKDIR /home/nonroot/app
COPY --from=builder-api ${UV_PROJECT_ENVIRONMENT} ${UV_PROJECT_ENVIRONMENT}
COPY --chown=nonroot:nonroot pyproject.toml uv.lock README.md .python-version ./
COPY --chown=nonroot:nonroot hawk ./hawk
COPY --chown=nonroot:nonroot hawk_flags ./hawk_flags
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=source=services/modules,target=services/modules \
    uv sync \
        --extra=api \
        --locked \
        --no-dev

RUN mkdir -p /home/nonroot/.aws /home/nonroot/.kube /home/nonroot/.minikube \
 && chown -R nonroot:nonroot /home/nonroot/.aws /home/nonroot/.kube /home/nonroot/.minikube

# See the runner stage.
ARG HAWK_BUILD_VERSION=""
ENV HAWK_BUILD_VERSION=${HAWK_BUILD_VERSION}

USER nonroot
ENTRYPOINT [ "uvicorn", "hawk.api.server:app" ]
# --no-access-log: uvicorn's plaintext access log is replaced by the
# structured `hawk.api.access` middleware in hawk.api.server.
CMD [ "--host=0.0.0.0", "--port=8080", "--no-access-log" ]

#################
##### AUDIT #####
#################
# The model-group audit's Batch job. Batch overrides CMD but not ENTRYPOINT,
# so the API environment gets the audit as its entrypoint.
FROM api AS audit
ENTRYPOINT [ "python", "-m", "hawk.tools.audit_model_groups" ]
CMD [ "--help" ]

####################
##### JANITOR #####
####################
FROM builder-base AS builder-janitor
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync \
        --extra=janitor \
        --locked \
        --no-dev \
        --no-install-project

FROM base AS janitor
COPY --from=helm /helm /usr/local/bin/helm

WORKDIR /home/nonroot/app
COPY --from=builder-janitor ${UV_PROJECT_ENVIRONMENT} ${UV_PROJECT_ENVIRONMENT}
COPY --chown=nonroot:nonroot pyproject.toml uv.lock README.md ./
COPY --chown=nonroot:nonroot hawk ./hawk
COPY --chown=nonroot:nonroot hawk_flags ./hawk_flags
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=source=services/modules,target=services/modules \
    uv sync \
        --extra=janitor \
        --locked \
        --no-dev

# See the runner stage.
ARG HAWK_BUILD_VERSION=""
ENV HAWK_BUILD_VERSION=${HAWK_BUILD_VERSION}

USER nonroot
ENTRYPOINT ["python", "-m", "hawk.janitor"]

#############################
##### MEMORY-HIGH AGENT #####
#############################
# Standard library only, so no venv: the image holds just the files it imports.
FROM ${MEMORY_HIGH_AGENT_BASE_IMAGE} AS memory-high-agent
WORKDIR /app
COPY hawk/__init__.py ./hawk/__init__.py
COPY hawk/core/__init__.py hawk/core/k8s_labels.py ./hawk/core/
COPY hawk/memory_high_agent/__init__.py hawk/memory_high_agent/__main__.py hawk/memory_high_agent/agent.py ./hawk/memory_high_agent/
ENV PYTHONPATH=/app PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1
ENTRYPOINT ["python", "-m", "hawk.memory_high_agent"]
