FROM public.ecr.aws/x8v8d7g8/mars-base:latest

WORKDIR /app

# Git time-travel: clone, then make the repo's default branch point AT the base
# commit with no future history — a real branch checkout (not a detached HEAD),
# future commits/tags gc'd away so the reference solution can't leak from history.
ARG BASE_SHA=89bede495dc2c85e1c57ba627a18526f71d57396
RUN git clone https://github.com/KaTeX/KaTeX . \
 && DEFAULT="$(git remote show origin | sed -n 's/.*HEAD branch: //p')" \
 && git checkout -B "$DEFAULT" "$BASE_SHA" \
 && git remote remove origin \
 && for b in $(git for-each-ref --format='%(refname:short)' refs/heads | grep -vx "$DEFAULT"); do git branch -D "$b" || true; done \
 && for t in $(git tag); do git merge-base --is-ancestor "$t" HEAD 2>/dev/null || git tag -d "$t"; done \
 && git reflog expire --expire=now --all \
 && git gc --prune=now \
 && (git submodule update --init --recursive || true)

RUN npm install --legacy-peer-deps --include=dev

# KaTeX is a yarn repo (committed yarn.lock, no package-lock.json). npm install
# rewrites yarn.lock and generates an untracked package-lock.json; restore the
# committed yarn.lock and drop the generated lockfile so the image worktree
# stays pristine (node_modules is untracked and unaffected). Required:
# model.patch capture diffs against base, and lockfiles are HARD tripwire paths.
RUN git checkout -- yarn.lock && rm -f package-lock.json

# v1.1 node-id scoring (CTRF route): official CTRF reporter for jest
# (github.com/ctrf-io/jest-ctrf-json-reporter, ctrf-io org), installed
# OUT-OF-TREE under /opt/jest-ctrf so /app stays byte-identical — package.json
# and the lockfiles are HARD tripwire paths and the model.patch baseline must
# stay clean (this also sidesteps the repo's --legacy-peer-deps mess).
# jest-environment-node MUST be co-installed and pinned to the task's jest
# version: 0.0.11's index.js loads dist/environment.js which hard-requires
# jest-environment-node at module load. The repo's jest meta-package is 30.4.2,
# whose resolved jest-environment-node is 30.4.1 (no 30.4.2 of that package
# exists on npm) — pin to the exact version /app/node_modules already resolves.
# The require checks make the build fail loudly if the reporter is not
# loadable; the git-status check enforces a pristine /app worktree.
RUN mkdir -p /opt/jest-ctrf \
 && cd /opt/jest-ctrf \
 && npm install --no-audit --no-fund jest-ctrf-json-reporter@0.0.11 jest-environment-node@30.4.1 \
 && node -e "require('/opt/jest-ctrf/node_modules/jest-ctrf-json-reporter')" \
 && node -e "require.resolve('/opt/jest-ctrf/node_modules/jest-ctrf-json-reporter/dist/index.js')" \
 && cd /app && git status --porcelain | (! grep -q .)

# Disable git commit hooks (husky etc.): dev-workflow tooling, not task content.
# Broken hook environments otherwise block the agent's (and oracle's) commits.
RUN cd /app && git config core.hooksPath /dev/null

CMD ["/bin/bash"]
