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

WORKDIR /app

ENV NODE_ENV=development

# 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=f66fc061fde4f764b113ededa09be63dae564159
RUN git clone https://github.com/gvergnaud/ts-pattern . \
 && 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 ci --include=dev --no-audit --no-fund

# v1.1 node-id scoring: 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 and the
# anti-cheat tripwire on package.json/package-lock.json stays valid; the
# verifier loads it by absolute path via --reporters.
# jest-ctrf-json-reporter@0.0.11 hard-requires jest-environment-node at module
# load (dist/environment.js), so it MUST be co-installed, pinned to this
# repo's jest toolchain version (30.1.2, the in-tree transitive pin).
# The require() smoke-test below fails the build if that co-install breaks.
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.1.2 \
 && 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"]
