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=70409a5052984325dccfdc5f6520818568a81f39
RUN git clone https://github.com/boa-dev/boa . \
 && 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 apt-get update && \
    apt-get install -y --no-install-recommends pkg-config libssl-dev ca-certificates && \
    rm -rf /var/lib/apt/lists/*

RUN if [ -f /app/Cargo.toml ]; then cargo fetch --manifest-path /app/Cargo.toml; fi

# v1.1 node-id scoring: cargo-nextest (pinned prebuilt binary) emits JUnit XML.
# Reporter config lives OUTSIDE the repo (--config-file) so the model can't
# hijack it via .config/nextest.toml.
ARG NEXTEST_VERSION=0.9.97
RUN curl -LsSf "https://get.nexte.st/${NEXTEST_VERSION}/linux" | tar zxf - -C /usr/local/bin \
 && cargo nextest --version

RUN mkdir -p /opt/nextest \
 && printf '[profile.junit]\nfail-fast = false\n\n[profile.junit.junit]\npath = "junit.xml"\n' > /opt/nextest/nextest.toml

# Warm the build cache so verifier runs only recompile the boa_engine crate delta.
RUN cargo nextest run -p boa_engine --tests --lib --no-run --config-file /opt/nextest/nextest.toml --profile junit

# Official ctrf-io converter (github.com/ctrf-io/junit-to-ctrf), pinned. Installed
# globally (out-of-tree; /app stays porcelain-clean). mars-base already ships
# node v24 + npm; the node --version guard fails the build loudly if the base
# ever drops it.
RUN node --version && npm install -g junit-to-ctrf@0.0.14 --ignore-scripts && junit-to-ctrf --version
# Fallback ONLY if a future base image lacks node (not needed today):
# RUN curl -fsSL https://nodejs.org/dist/v22.17.0/node-v22.17.0-linux-x64.tar.xz | tar -xJ -C /opt && ln -s /opt/node-v22.17.0-linux-x64/bin/node /usr/local/bin/node && ln -s /opt/node-v22.17.0-linux-x64/bin/npm /usr/local/bin/npm

# 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"]
