# Grader image for a Proof v2 environment package.
#
# Build from the package root (the build context is the whole package):
#   docker build -f environment/Dockerfile -t proof-envpkg:<package_id> .
# Grade a prepared work dir (no network at grade time):
#   docker run --rm --network none -v "$WORK:/work" proof-envpkg:<package_id> <task_id> /work --out /work/result.json
# Or prepare and grade inside the container:
#   docker run --rm --network none --entrypoint /bin/bash proof-envpkg:<package_id> \
#     -c '/pkg/environment/setup.sh <task_id> /work >&2 && /pkg/verifier/grade.sh <task_id> /work'
#
# golang:1.22 is newer than the subject's go directive; the harness may raise it.
# The image contains the whole package, hidden checks and reference patches
# included: it is the grader's image, never the agent's.
FROM golang:1.22

# zstd: base archives are .tar.zst; python3: the scripts read metadata.json with it.
RUN apt-get update \
 && apt-get install -y --no-install-recommends zstd python3 \
 && apt-get clean

ENV GOPROXY=off \
    GOTOOLCHAIN=local \
    GOWORK=off \
    GOFLAGS="-buildvcs=false -count=1" \
    GRADE_TIMEOUT_S=600

COPY . /pkg
WORKDIR /pkg
RUN chmod +x /pkg/environment/setup.sh /pkg/environment/reset.sh /pkg/verifier/grade.sh

# Warm the build cache for every family's base tree so grading needs no
# compilation of the standard library at run time. Failure here only means a
# slower first grade, so it does not fail the build.
RUN set -u; for a in /pkg/repo/base-*.tar.zst; do \
      d="$(mktemp -d)"; \
      if zstd -dc "$a" | tar -x -C "$d" \
         && (cd "$d" && go build ./... && go test -count=1 -run '^$' ./... >/dev/null); then \
        echo "warmed $(basename "$a")"; \
      else \
        echo "warm-up failed for $(basename "$a") (grading will compile on first run)" >&2; \
      fi; \
      find "$d" -delete; \
    done

ENTRYPOINT ["/pkg/verifier/grade.sh"]
