# --- runway tier-2 layer (varlock + 1Password) ---
# Spliced in by `runway init --tier=2` immediately before the
# final `ENTRYPOINT ["sleep", "infinity"]` line.

# Install varlock via npm. The official `ghcr.io/dmno-dev/varlock`
# image is musl/Alpine — copying its binary into a glibc base
# (node:24-bookworm) produces an ELF that the loader can't resolve
# ("not found" on exec). npm install gets the right binary for the
# image's libc.
USER root
RUN npm install -g varlock

# Install the 1Password CLI so varlock can resolve `op://` references.
RUN apt-get update && apt-get install -y --no-install-recommends \
      ca-certificates curl gnupg \
  && curl -sS https://downloads.1password.com/linux/keys/1password.asc \
      | gpg --dearmor -o /usr/share/keyrings/1password-archive-keyring.gpg \
  && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/1password-archive-keyring.gpg] https://downloads.1password.com/linux/debian/$(dpkg --print-architecture) stable main" \
      > /etc/apt/sources.list.d/1password.list \
  && apt-get update && apt-get install -y 1password-cli \
  && rm -rf /var/lib/apt/lists/*

# Shim the `claude` binary so every invocation runs through varlock.
# The real binary moves to claude.real; the shim (vendored at
# .sandcastle/runway-claude-shim by `runway init --tier=2`) resolves
# secrets from 1Password (using OP_SERVICE_ACCOUNT_TOKEN passed in by
# runway) and execs the real claude with secrets in its process env.
# Secrets exist only inside the lifetime of one claude invocation —
# never in any image layer, never on the container filesystem.
#
# VA-413: when .env.schema declares the runway agent signing env vars
# (RUNWAY_SIGNING_SSH_PRIVKEY, _PUBKEY, _NAME, _EMAIL), the shim also
# materialises the SSH signing key to a 0600 file under $HOME and
# configures git to use it. The bootstrap is a no-op when any of the
# four are unset, so unsigned-commit scaffolds keep working.
#
# COPY path is relative to the .sandcastle build context (see
# `buildAgentImage` in scaffolder-image.ts: `docker build .sandcastle`),
# so the source is `runway-claude-shim`, not `.sandcastle/runway-claude-shim`.
RUN mv /home/agent/.local/bin/claude /home/agent/.local/bin/claude.real
COPY runway-claude-shim /home/agent/.local/bin/claude
RUN chmod +x /home/agent/.local/bin/claude \
  && chown ${AGENT_UID}:${AGENT_GID} /home/agent/.local/bin/claude

USER ${AGENT_UID}:${AGENT_GID}

# Final ENTRYPOINT remains:
#   ENTRYPOINT ["sleep", "infinity"]
# Sandcastle will `docker exec <container> claude ...` and our shim
# transparently wraps each invocation.
