# syntax=docker/dockerfile:1.7
ARG HERMES_IMAGE=nousresearch/hermes-agent@sha256:bc79a80fc9cb97521f4380c5d5b2ad88e2469e50fc1d5027339bbb42cc206854
FROM ${HERMES_IMAGE}

USER root

ENV PLAYWRIGHT_BROWSERS_PATH=/opt/hermes/tools

ARG MCP_PACKAGE=@sellable/mcp@0.1.1105-wip.fastonboard2.20261204.1
ARG MCP_INTEGRITY=sha512-kEl1X7g7VFDAEM9B+d7pieiOrUFbojPL4tvms4uIG6OK3coyDcSaEQ9LYvUfD2psy6hsjs3HobUIDRsn3CQDEg==
ARG INSTALLER_PACKAGE=@sellable/install@0.1.994-wip.fastonboard2.20261204.1
ARG ADMIN_MCP_PACKAGE=@sellable/admin-mcp@0.1.204
ARG ADMIN_MCP_INTEGRITY=sha512-8/5lo54crSCBxckndq6ZVqNUw/3rv0YR0oq3L5XG0n1vIqxx3P1rZPzk14GeauapaegC6pviqdAm8scdK6hj7g==
ARG TUS_JS_CLIENT_VERSION=4.3.1
ARG YAML_VERSION=2.9.0
ARG WS_VERSION=8.18.3
ARG HERMES_VERSION=0.23.5
ARG SLACK_SDK_VERSION=3.45.0
ARG DDGS_VERSION=9.16.1
ARG BROWSER_HARNESS_VERSION=0.1.13
ARG CHROMIUM_BROWSER_REVISION=1248
ARG DEFAULT_PROFILE_BUNDLE_FIXTURE_VERSION=1
ARG DEFAULT_PROFILE_BUNDLE_VERSION=4
ARG DEFAULT_PROFILE_BUNDLE_DIGEST=8699bf24c974b92294b30227dd730424397e0f295e8505826ee247472d7f3798

# Claude Code's own program, unmodified: Opus writes a campaign's plan and message
# on this machine (write_with_claude). Its key is never in the image.
ARG CLAUDE_CODE_PACKAGE=@anthropic-ai/claude-code-linux-x64@2.1.333
ARG CLAUDE_CODE_INTEGRITY=sha512-DX5OhLe/2KSF8RhNE+qHfgksGAT4Cn8r9dpq1PNxNcQIg/6XPoOivqOTm3qjHOp0DDJPA01S9xeqSqvD5rV7ig==
RUN set -eux; \
    work="$(mktemp -d)"; \
    cd "${work}"; \
    npm pack --silent "${CLAUDE_CODE_PACKAGE}" > name; \
    test "sha512-$(openssl dgst -sha512 -binary "$(cat name)" | base64 -w0)" = "${CLAUDE_CODE_INTEGRITY}"; \
    tar -xzf "$(cat name)" package/claude; \
    install -d -o root -g root -m 0755 /usr/local/lib/sellable-agent/claude-code; \
    install -o root -g root -m 0555 package/claude /usr/local/lib/sellable-agent/claude-code/claude; \
    cd /; rm -rf "${work}"; \
    HOME=/tmp /usr/local/lib/sellable-agent/claude-code/claude --version | grep -F "${CLAUDE_CODE_PACKAGE##*@}"; \
    npm cache clean --force

RUN --mount=type=secret,id=mcp_trust_root,required=true \
    set -eux; \
    test -s /run/secrets/mcp_trust_root \
    && mkdir -p /usr/local/lib/sellable-agent/external-runtime/mcp \
                /usr/local/share/sellable-agent \
    && test "$(npm view "${MCP_PACKAGE}" dist.integrity)" = "${MCP_INTEGRITY}" \
    && npm install --prefix /usr/local/lib/sellable-agent/external-runtime/mcp \
         --omit=dev --ignore-scripts --no-audit --no-fund "${MCP_PACKAGE}" \
    && test "$(node -p "require('/usr/local/lib/sellable-agent/external-runtime/mcp/node_modules/@sellable/mcp/package.json').version")" = "${MCP_PACKAGE##*@}" \
    && test "$(npm view "${ADMIN_MCP_PACKAGE}" dist.integrity)" = "${ADMIN_MCP_INTEGRITY}" \
    && npm install --prefix /usr/local/lib/sellable-agent/internal-admin-mcp \
         --omit=dev --ignore-scripts --no-audit --no-fund "${ADMIN_MCP_PACKAGE}" \
    && test "$(node -p "require('/usr/local/lib/sellable-agent/internal-admin-mcp/node_modules/@sellable/admin-mcp/package.json').version")" = "${ADMIN_MCP_PACKAGE##*@}" \
    && test -x /usr/local/lib/sellable-agent/internal-admin-mcp/node_modules/.bin/sellable-admin-mcp \
    && npm install --prefix /usr/local/lib/sellable-agent \
         --omit=dev --ignore-scripts --no-audit --no-fund \
         "tus-js-client@${TUS_JS_CLIENT_VERSION}" "yaml@${YAML_VERSION}" \
         "ws@${WS_VERSION}" \
    && test "$(node -p "require('/usr/local/lib/sellable-agent/node_modules/tus-js-client/package.json').version")" = "${TUS_JS_CLIENT_VERSION}" \
    && test "$(node -p "require('/usr/local/lib/sellable-agent/node_modules/yaml/package.json').version")" = "${YAML_VERSION}" \
    && test "$(node -p "require('/usr/local/lib/sellable-agent/node_modules/ws/package.json').version")" = "${WS_VERSION}" \
    && mkdir -p /usr/local/lib/sellable-agent/external-runtime/mcp/bin \
    && ln -s ../node_modules/.bin/sellable-mcp \
         /usr/local/lib/sellable-agent/external-runtime/mcp/bin/sellable-mcp \
    && test -x /usr/local/lib/sellable-agent/external-runtime/mcp/bin/sellable-mcp \
    && install -o root -g root -m 0444 /run/secrets/mcp_trust_root \
         /usr/local/share/sellable-agent/mcp-turn-trust-root.pem \
    && test -x "/opt/hermes/tools/chromium-${CHROMIUM_BROWSER_REVISION}/chrome-linux64/chrome" \
    && test "$(cat /etc/hermes/chromium-executable-path)" = "/opt/hermes/tools/chromium-${CHROMIUM_BROWSER_REVISION}/chrome-linux64/chrome" \
    && test "$(/opt/hermes/.venv/bin/python -c "import importlib.metadata; print(importlib.metadata.version('browser-harness'))")" = "${BROWSER_HARNESS_VERSION}" \
    && /opt/hermes/.venv/bin/python -m ensurepip --upgrade --default-pip \
    && test -x /opt/hermes/.venv/bin/pip \
    && /opt/hermes/.venv/bin/python -m pip --version \
    && HERMES_UV="$(find /opt/hermes/tools -maxdepth 2 -name uv -type f | head -n 1)" \
    && test -x "${HERMES_UV}" \
    && test "$(/opt/hermes/.venv/bin/python -c "import importlib.metadata; print(importlib.metadata.version('slack-sdk'))")" = "${SLACK_SDK_VERSION}" \
    && "${HERMES_UV}" pip install --python /opt/hermes/.venv/bin/python --no-cache edge-tts==7.2.7 \
    && test "$(/opt/hermes/.venv/bin/python -c "import importlib.metadata; print(importlib.metadata.version('edge-tts'))")" = "7.2.7" \
    && "${HERMES_UV}" pip install --python /opt/hermes/.venv/bin/python --no-cache "ddgs==${DDGS_VERSION}" \
    && test "$(/opt/hermes/.venv/bin/python -c "import importlib.metadata; print(importlib.metadata.version('ddgs'))")" = "${DDGS_VERSION}" \
    && /opt/hermes/.venv/bin/python -c "from ddgs import DDGS" \
    && rm -f /etc/s6-overlay/s6-rc.d/user/contents.d/dashboard \
             /etc/s6-overlay/s6-rc.d/user/contents.d/main-hermes \
             /etc/s6-overlay/s6-rc.d/user/contents.d/profile-gateways \
             /etc/cont-init.d/01-hermes-setup \
             /etc/cont-init.d/015-supervise-perms \
             /etc/cont-init.d/02-reconcile-profiles \
    && npm cache clean --force

COPY mcp-context-proxy.mjs mcp-runtime-binding.mjs fly-customer-model.mjs fly-customer-worker.mjs profile-materializer.mjs hermes-bridge.mjs hermes-memory-snapshot.mjs fly-runtime-identity.mjs fly-soul-bridge.mjs fly-soul-bridge-exec.mjs fly-exec-staging.mjs fly-skills-bridge.mjs fly-skills-bridge-exec.mjs fly-capabilities-bridge.mjs fly-capabilities-bridge-exec.mjs soul-artifact-validator.mjs default-profile-bundle.mjs default-profile-reconciler.mjs fly-cron-proof-exec.mjs model-auth-reconciler.mjs model-auth-canary-reseed.mjs model-health-reporter.mjs realtime-webhook-bridge.mjs \
  /usr/local/lib/sellable-agent/
COPY abandoned-runtime-scratch.mjs /usr/local/lib/sellable-agent/abandoned-runtime-scratch.mjs
COPY hermes-first-welcome.mjs /usr/local/lib/sellable-agent/hermes-first-welcome.mjs
COPY default-profile-native-proof.mjs /usr/local/lib/sellable-agent/default-profile-native-proof.mjs
COPY managed-auth-client.mjs managed-auth-supervisor.py /usr/local/lib/sellable-agent/
COPY hermes-web-runtime-check.py /usr/local/lib/sellable-agent/hermes-web-runtime-check.py
COPY public-skills.mjs public-skills.json /usr/local/lib/sellable-agent/
COPY hermes-plugins/delegation/advanced-delegation/ /opt/hermes/plugins/delegation/advanced-delegation/
COPY hermes-plugins/sellable-curator-approval/ /opt/hermes/plugins/sellable-curator-approval/
COPY default-profile-bundles/shared/ /usr/local/lib/sellable-agent/default-profile-bundles/shared/
COPY default-profile-bundles/customer/ /usr/local/lib/sellable-agent/default-profile-bundles/customer/
COPY default-profile-bundles/fixtures/ /usr/local/lib/sellable-agent/default-profile-bundles/fixtures/
COPY hermes-memory-dirty.mjs /opt/sellable/bin/hermes-memory-dirty
COPY hermes-memory-reconcile.sh /opt/sellable/share/hermes-memory-reconcile.sh
COPY hermes-endpoint-cron.mjs /opt/sellable/bin/hermes-endpoint-cron
COPY fly-customer-image/model-auth-store.mjs /usr/local/lib/sellable-agent/fly-customer-image/model-auth-store.mjs
COPY fly-customer-image/hermes-runs-client.mjs /usr/local/lib/sellable-agent/fly-customer-image/hermes-runs-client.mjs
COPY fly-customer-image/endpoint-model-selection.mjs fly-customer-image/runs-api-connections.mjs fly-customer-image/openai-runs-client.mjs fly-customer-image/runs-api-health-check.mjs fly-customer-image/public-endpoint-guard.mjs fly-customer-image/workspace-connection-client.mjs fly-customer-image/slack-stand-in.mjs /usr/local/lib/sellable-agent/fly-customer-image/
COPY fly-customer-image/opener-stage-execution.mjs /usr/local/lib/sellable-agent/fly-customer-image/opener-stage-execution.mjs
COPY fly-customer-image/endpoint-uat-completion-gate.mjs /usr/local/lib/sellable-agent/fly-customer-image/endpoint-uat-completion-gate.mjs
COPY fly-customer-image/customer-runtime-phase-telemetry.mjs /usr/local/lib/sellable-agent/fly-customer-image/customer-runtime-phase-telemetry.mjs
COPY fly-customer-image/customer-runtime.mjs /usr/local/lib/sellable-agent/fly-customer-image/customer-runtime.mjs
COPY model-signins.mjs /usr/local/lib/sellable-agent/model-signins.mjs
COPY sellable-model-signin /usr/local/bin/sellable-model-signin
COPY install-model-provider-plugins.sh /tmp/install-model-provider-plugins.sh
# Two small add-ons, outside Hermes' own folder: a machine staff did not sign in
# never loads them.
RUN sh /tmp/install-model-provider-plugins.sh /usr/local/lib/sellable-agent/model-providers \
    && rm -f /tmp/install-model-provider-plugins.sh \
    && chmod 0555 /usr/local/bin/sellable-model-signin
COPY fly-customer-image/rootfs/ /

RUN set -eux; \
    fixture_args=""; \
    if test -n "${DEFAULT_PROFILE_BUNDLE_FIXTURE_VERSION}"; then \
      fixture_args="--fixture-version ${DEFAULT_PROFILE_BUNDLE_FIXTURE_VERSION}"; \
    fi; \
    node /usr/local/lib/sellable-agent/default-profile-bundle.mjs \
      --kind CUSTOMER ${fixture_args} \
      --output /usr/local/share/sellable-agent/default-profile-bundle \
    && chmod 0555 /usr/local/lib/sellable-agent/default-profile-bundles/shared/scripts/customer-program-due.py \
    && node -e \
      'const fs=require("fs"); const manifest=JSON.parse(fs.readFileSync("/usr/local/share/sellable-agent/default-profile-bundle/manifest.json")); if(String(manifest.bundleVersion)!==process.argv[1] || manifest.bundleDigest!==process.argv[2]) process.exit(1);' \
      "${DEFAULT_PROFILE_BUNDLE_VERSION}" "${DEFAULT_PROFILE_BUNDLE_DIGEST}" \
    && node --input-type=module -e \
      'import { installHermesAgentBridge, HERMES_AGENT_BRIDGE_DEDICATED_V0235_CONTRACT } from "/usr/local/lib/sellable-agent/hermes-bridge.mjs"; installHermesAgentBridge({ sourceRoot: "/opt/hermes", contract: HERMES_AGENT_BRIDGE_DEDICATED_V0235_CONTRACT });' \
    && PYTHONPATH=/opt/hermes /opt/hermes/.venv/bin/python -c 'from plugins.platforms.slack.adapter import _session_status_method, _sellable_agent_normalize_session_status; from slack_sdk.web.async_client import AsyncWebClient; assert _session_status_method(AsyncWebClient()).__name__ == "agents_sessions_setStatus", "Expected Slack Agent Sessions API"; assert _sellable_agent_normalize_session_status("Thinking...") == "processing" and _sellable_agent_normalize_session_status("") == "active"' \
    && PYTHONPATH=/opt/hermes /opt/hermes/.venv/bin/python -c 'import os, tempfile; from hermes_cli.plugins import discover_plugins, get_plugin_manager; import agent.delegation_registry as dr; d=tempfile.mkdtemp(); os.environ["HERMES_HOME"]=d; discover_plugins(); pm=get_plugin_manager(); assert pm._plugins["delegation/advanced-delegation"].enabled and pm._plugins["sellable-curator-approval"].enabled; prov=dr.get_provider("advanced-delegation"); assert prov is not None and prov.name=="advanced-delegation" and "route" in prov.task_parameters({})' \
    && boot_home="$(mktemp -d)" \
    && (cd /opt/hermes && export HERMES_HOME="${boot_home}" HOME="${boot_home}" && /opt/hermes/.venv/bin/hermes --version && /opt/hermes/.venv/bin/python -c 'from agent.credential_pool import load_pool; load_pool("openai-codex")') > /tmp/boot-check.log 2>&1 \
    && ! grep -iE "Lazy-installing|pip install|npm install" /tmp/boot-check.log \
    && rm -rf "${boot_home}" /tmp/boot-check.log \
    && /opt/hermes/.venv/bin/python /usr/local/lib/sellable-agent/hermes-web-runtime-check.py \
      "$(node --input-type=module -e 'import { HERMES_SELLABLE_WEB_POLICY } from "/usr/local/lib/sellable-agent/hermes-memory-snapshot.mjs"; process.stdout.write(JSON.stringify(HERMES_SELLABLE_WEB_POLICY));')" \
      "${BROWSER_HARNESS_VERSION}" "${CHROMIUM_BROWSER_REVISION}" \
    && node --input-type=module -e \
      'await import("/usr/local/lib/sellable-agent/fly-runtime-identity.mjs");' \
    && node --input-type=module -e \
      'await import("/usr/local/lib/sellable-agent/fly-customer-image/customer-runtime.mjs");' \
    && printf '%s\n' \
      '#!/usr/bin/env node' \
      'import("file:///usr/local/lib/sellable-agent/fly-soul-bridge.mjs").then(({ runSoulBridgeCli }) => runSoulBridgeCli());' \
      > /usr/local/bin/sellable-soul-bridge \
    && chmod 0555 /usr/local/bin/sellable-soul-bridge \
    && printf '%s\n' \
      '#!/usr/bin/env node' \
      'import("file:///usr/local/lib/sellable-agent/fly-soul-bridge-exec.mjs").then(({ runFlySoulBridgeExecCli }) => runFlySoulBridgeExecCli());' \
      > /usr/local/bin/sellable-soul-bridge-exec \
    && chmod 0555 /usr/local/bin/sellable-soul-bridge-exec \
    && printf '%s\n' \
      '#!/usr/bin/env node' \
      'import("file:///usr/local/lib/sellable-agent/fly-skills-bridge-exec.mjs").then(({ runFlySkillsBridgeExecCli }) => runFlySkillsBridgeExecCli());' \
      > /usr/local/bin/sellable-skills-bridge-exec \
    && chmod 0555 /usr/local/bin/sellable-skills-bridge-exec \
    && printf '%s\n' \
      '#!/usr/bin/env node' \
      'import("file:///usr/local/lib/sellable-agent/fly-capabilities-bridge-exec.mjs").then(({ runFlyCapabilitiesBridgeExecCli }) => runFlyCapabilitiesBridgeExecCli());' \
      > /usr/local/bin/sellable-capabilities-bridge-exec \
    && chmod 0555 /usr/local/bin/sellable-capabilities-bridge-exec \
    && sha256sum /usr/local/lib/sellable-agent/fly-soul-bridge.mjs \
      > /usr/local/share/sellable-agent/soul-bridge-source.sha256 \
    && sha256sum /usr/local/lib/sellable-agent/fly-skills-bridge.mjs \
      /usr/local/lib/sellable-agent/fly-skills-bridge-exec.mjs \
      /usr/local/lib/sellable-agent/fly-exec-staging.mjs \
      > /usr/local/share/sellable-agent/skills-bridge-source.sha256 \
    && sha256sum /usr/local/lib/sellable-agent/default-profile-reconciler.mjs \
      /usr/local/lib/sellable-agent/fly-cron-proof-exec.mjs \
      > /usr/local/share/sellable-agent/default-profile-reconcile-source.sha256 \
    && sha256sum /usr/local/lib/sellable-agent/fly-capabilities-bridge.mjs \
      /usr/local/lib/sellable-agent/fly-capabilities-bridge-exec.mjs \
      > /usr/local/share/sellable-agent/capabilities-bridge-source.sha256 \
    && chmod 0444 /usr/local/share/sellable-agent/soul-bridge-source.sha256 \
      /usr/local/share/sellable-agent/skills-bridge-source.sha256 \
      /usr/local/share/sellable-agent/default-profile-reconcile-source.sha256 \
      /usr/local/share/sellable-agent/capabilities-bridge-source.sha256 \
    && chmod 0755 /etc \
    && chmod 0555 /usr/local/lib/sellable-agent/*.mjs \
    && chmod 0555 /usr/local/lib/sellable-agent/managed-auth-supervisor.py \
    && chmod 0555 /opt/sellable/bin/hermes-memory-dirty /opt/sellable/bin/hermes-endpoint-cron \
    && chmod 0444 /opt/sellable/share/hermes-memory-reconcile.sh \
    && chmod 0555 /etc/s6-overlay/s6-rc.d/customer-runtime/run \
    && chmod 0444 /etc/s6-overlay/s6-rc.d/customer-runtime/type \
                  /etc/s6-overlay/s6-rc.d/user/contents.d/customer-runtime

# Prepare Hermes' code once, here, after every patch. The agent's user cannot
# save it, so each start prepared it again, on a small machine (2026-12-03).
RUN cd /opt/hermes \
    && .venv/bin/python -m compileall -q -j 0 -x '/(node_modules|tests|\.git)/' \
      acp_adapter agent cron gateway hermes_agent hermes_cli hermes_platform \
      persistence plugins providers tools utils ./*.py .venv/lib \
    && test -n "$(find gateway/__pycache__ -name 'run.cpython-*.pyc')"

LABEL io.sellable.runtime.kind="customer" \
      io.sellable.runtime.product-mcp-readiness="service-authenticated-safe-read/v1" \
      io.sellable.runtime.listener="native-dedicated-slack" \
      io.sellable.runtime.api-server="authenticated-loopback-127.0.0.1:8642" \
      io.sellable.runtime.installers="build-only" \
      io.sellable.runtime.default-profile-bundle-version="${DEFAULT_PROFILE_BUNDLE_VERSION}" \
      io.sellable.runtime.default-profile-bundle-digest="${DEFAULT_PROFILE_BUNDLE_DIGEST}"

ENV HOME=/opt/data \
    HERMES_HOME=/opt/data \
    HERMES_WRITE_SAFE_ROOT=/opt/data \
    NODE_ENV=production \
    SELLABLE_AGENT_RUNTIME=1 \
    SELLABLE_AGENT_HERMES_VERSION=${HERMES_VERSION} \
    SELLABLE_AGENT_INSTALLER_PACKAGE=${INSTALLER_PACKAGE} \
    SELLABLE_AGENT_MCP_PACKAGE=${MCP_PACKAGE} \
    SELLABLE_AGENT_DEFAULT_PROFILE_BUNDLE_VERSION=${DEFAULT_PROFILE_BUNDLE_VERSION} \
    SELLABLE_AGENT_DEFAULT_PROFILE_BUNDLE_DIGEST=${DEFAULT_PROFILE_BUNDLE_DIGEST} \
    SELLABLE_AGENT_DEFAULT_PROFILE_FIXTURE_TEST_ONLY=1 \
    SELLABLE_AGENT_DEFAULT_PROFILE_FIXTURE_WORKSPACE_ID=cmp6h5wo00000l204a6g2jrk1 \
    SELLABLE_AGENT_DEFAULT_PROFILE_FIXTURE_AGENT_ID=agent_6bed1d07c8892cde4a308a4c54d8bd14 \
    SELLABLE_AGENT_BROWSER_HARNESS_VERSION=${BROWSER_HARNESS_VERSION} \
    SELLABLE_AGENT_CHROMIUM_BROWSER_REVISION=${CHROMIUM_BROWSER_REVISION} \
    PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1

ENTRYPOINT ["/init"]
