# opencode-ornith15-122b-tf-gb10: opencode 1.18.23 driving Ornith-1.5-122B served by TensorFold in the same container.
# The engine stages are the ornith15-122b-tensorfold-gb10 capsule's, line for line (MiaAI-Lab's DGX Spark recipe);
# the harness lines are opencode-inkling2mini-tf-v2-gb10's.
# ornith15-122b-tensorfold-gb10: TensorFold's Zig engine (tensorfold-native) with EXL3 support for the Qwen3.5-MoE CUDA
# engine, built for the DGX Spark's GB10 (linux-aarch64, sm_121) exactly as MiaAI-Lab's recipe does it:
#   github.com/MiaAI-Lab/Ornith-1.5-122B-TensorFold @ 0c8fd6a0d1e2ece7557978a7d8c3d96d137eb075
# - TensorFold (Apache-2.0) from github.com/ashhart/TensorFold at d1018b31, plus the recipe's patches/0001-0026
#   (EXL3 loader and kernels, DFlash2 drafter, prompt-pass speedups), taken from the recipe's tarball at that commit
#   (SHA-256 checked).
# - Zig 0.17.0 (checked by SHA-256). Base: NVIDIA's PyTorch 26.07 container (CUDA 13, nvcc with sm_121a), pinned by
#   digest. The kernels are embedded in the binary (fatbins).
# One change from the recipe: its final stage also pip-installs huggingface_hub for downloads; m64 fetches the weights.

FROM nvcr.io/nvidia/pytorch@sha256:2140e699b3beaf7f96a0081fd9c9406bc3832b435cdb60dfa2d261f7d2f34a1c AS build
ARG ZIG_VERSION=0.17.0
ARG ZIG_SHA256=9e8d11661d4ae3bd57702a3832781e23ad151dde5798e16a5ccd503f65234ff8
RUN curl -fsSL -o /tmp/zig.tar.xz "https://ziglang.org/download/${ZIG_VERSION}/zig-aarch64-linux-${ZIG_VERSION}.tar.xz" && \
    echo "${ZIG_SHA256}  /tmp/zig.tar.xz" | sha256sum -c - && \
    mkdir -p /opt/zig && tar -xJf /tmp/zig.tar.xz -C /opt/zig --strip-components=1 && rm /tmp/zig.tar.xz && \
    /opt/zig/zig version
ARG TF_REPO=https://github.com/ashhart/TensorFold.git
ARG TF_REF=d1018b318fb51981c4a18f12e40269a4d3ee9713
RUN git init -q /tensorfold && cd /tensorfold && git remote add origin "${TF_REPO}" && \
    git fetch -q --depth 1 origin "${TF_REF}" && git checkout -q --detach FETCH_HEAD && \
    test "$(git rev-parse HEAD)" = "${TF_REF}"
ARG RECIPE_COMMIT=0c8fd6a0d1e2ece7557978a7d8c3d96d137eb075
ARG RECIPE_SHA256=aa60e40eac0dc8dd5a1141337ee3a2add01fb5d3a7b91706087be7a8afd18e0d
RUN curl -fsSL -o /tmp/recipe.tgz "https://github.com/MiaAI-Lab/Ornith-1.5-122B-TensorFold/archive/${RECIPE_COMMIT}.tar.gz" && \
    echo "${RECIPE_SHA256}  /tmp/recipe.tgz" | sha256sum -c - && \
    mkdir -p /opt/recipe && tar -xzf /tmp/recipe.tgz -C /opt/recipe --strip-components=1 && rm /tmp/recipe.tgz && \
    mkdir -p /opt/tensorfold/share/doc/recipe && \
    for f in LICENSE NOTICE CREDITS.md; do cp "/opt/recipe/$f" /opt/tensorfold/share/doc/recipe/ || exit 1; done
RUN cd /tensorfold && \
    for p in /opt/recipe/patches/*.patch; do [ -e "$p" ] || continue; echo "applying $p"; git apply --check "$p" && git apply "$p" || exit 1; done
RUN cd /tensorfold && /opt/zig/zig build -Dnvcc=/usr/local/cuda/bin/nvcc -Doptimize=fast --prefix /opt/tensorfold \
      -j"$(nproc)" fatbins install native && \
    test -x /opt/tensorfold/native/bin/tensorfold-native
RUN mkdir -p /opt/tensorfold/share/doc/tensorfold && cd /tensorfold && \
    for f in LICENSE LICENSE.md LICENSES NOTICE THIRD_PARTY_NOTICES.md; do \
      if [ -e "$f" ]; then cp -a "$f" /opt/tensorfold/share/doc/tensorfold/ || exit 1; fi; done

FROM nvcr.io/nvidia/pytorch@sha256:2140e699b3beaf7f96a0081fd9c9406bc3832b435cdb60dfa2d261f7d2f34a1c
COPY --from=build /opt/tensorfold /opt/tensorfold
RUN ln -s /opt/tensorfold/native/bin/tensorfold-native /usr/local/bin/tensorfold-native && \
    test -x /opt/tensorfold/native/bin/tensorfold-native
ENV PYTHONUNBUFFERED=1 TENSORFOLD_CUDA_KERNELS=native

RUN apt-get update -qq && apt-get install -y --no-install-recommends \
      ca-certificates git ripgrep jq curl xz-utils unzip file less procps build-essential python3-venv \
    && rm -rf /var/lib/apt/lists/*

# Node 22.23.2, official arm64 build, checked against nodejs.org's SHASUMS256.txt.
RUN curl -fsSL https://nodejs.org/dist/v22.23.2/node-v22.23.2-linux-arm64.tar.xz -o /tmp/node.tar.xz \
 && echo "fff4078c5def658577f92c88db7db3bc0072924bfb93fe52c1e744a54e94abb8  /tmp/node.tar.xz" | sha256sum -c - \
 && mkdir -p /opt/node && tar xJf /tmp/node.tar.xz -C /opt/node --strip-components=1 && rm /tmp/node.tar.xz \
 && ln -s /opt/node/bin/node /opt/node/bin/npm /opt/node/bin/npx /usr/local/bin/
ENV PATH=/opt/node/bin:$PATH

RUN npm install -g opencode-ai@1.18.23 && npm cache clean --force

# Everything opencode would otherwise fetch at run time (models.dev catalogue, LSP servers,
# self-update) is disabled. The npm packages it installs on a project's first use are
# warmed here, where the build has network (warm_opencode.sh); start.sh copies them into
# the throwaway HOME. Without this, every run would log a blocked registry.npmjs.org.
ENV OPENCODE_DISABLE_AUTOUPDATE=1 \
    OPENCODE_DISABLE_LSP_DOWNLOAD=1 \
    OPENCODE_DISABLE_MODELS_FETCH=1 \
    OPENCODE_DISABLE_SHARE=1
COPY opencode.json /etc/opencode/opencode.json
COPY warm_opencode.sh /tmp/warm_opencode.sh
RUN bash /tmp/warm_opencode.sh && rm /tmp/warm_opencode.sh

# Gateway deps, the same pinned set as the other agent capsules' gateways (the vLLM image had them; this base does not).
RUN python3 -m venv /opt/gateway && /opt/gateway/bin/pip install --no-cache-dir \
      fastapi==0.115.14 uvicorn==0.32.1 httpx==0.28.1 \
      starlette==0.46.2 pydantic==2.14.1 pydantic_core==2.50.1 annotated-types==0.8.0 \
      typing_extensions==4.16.0 typing-inspection==0.4.4 anyio==4.15.1 idna==3.20 \
      certifi==2026.10.23 httpcore==1.0.9 h11==0.16.0 click==8.5.0
WORKDIR /scratch
