# syntax=docker/dockerfile:1
# agent-api (v0.12) — the unit control plane's HTTP front door: POST /invocations (the dispatcher
# sink → a runtime.v1 agent spawn), POST /api/chat (SSE), /api/sessions, /health. The unit WORKER
# itself is a separate runtime.v1 `agent` workload (claude-in-container) — this image is the control
# plane only (no claude here).
#
# Build context: the v0.12 carve root (compose `context: ../..`) — wider than its own dir because
# agent_api validates AT THE SEAM (P8): contracts.py loads the sealed transcript.v1 / workspace.v1 /
# invoke.v1 / unit.v1 schemas BY PATH at import (walking up to find `meetings/contracts/...` and
# `agent/contracts/...`), so those frozen files must be vendored into the image alongside src/.
# python:3.12-slim (Debian 13), pinned by digest; uv past its advisories (fixed in 0.11.15).
FROM python:3.12-slim@sha256:05cda9777409a9c3ffddd94a4c476b79f0769a0b4857f0c7ed9226b6800b0d6f

RUN pip install --no-cache-dir uv==0.11.33

ENV UV_LINK_MODE=copy \
    UV_PROJECT_ENVIRONMENT=/opt/venv \
    PYTHONUNBUFFERED=1 \
    PYTHONPATH=/app \
    PATH=/opt/venv/bin:$PATH

WORKDIR /app

COPY core/agent/pyproject.toml core/agent/uv.lock ./
# Control-plane image installs ONLY base + the `control-plane` dep group (fastapi/uvicorn/multipart);
# NOT the worker deps, NOT dev/test (D1 — real per-image trimming).
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-install-project --no-default-groups --group control-plane

# git + openssh-client. The control plane reads workspace git state via workspace_reader, and LOADS an
# existing repo into a workspace over git's SSH transport: `ssh` is the binary git shells out to for an
# ssh:// remote, and `ssh-keygen` mints the per-workspace deploy key (control_plane/deploy_keys.py) that
# makes loading a private repo possible without anybody pasting a token. NO claude-code / node here:
# agents never run in the control plane — the claude turn runs in the separate `vexa-agent` WORKER image
# (core/agent/worker/Dockerfile). This is the payoff of the control-plane ⊥ worker split (a lean image).
RUN apt-get update && apt-get install -y --no-install-recommends git openssh-client ca-certificates && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# The control-plane packages (NO worker): control_plane + shared + contracts, copied to /app so
# `PYTHONPATH=/app` resolves them as top-level imports.
COPY core/agent/shared ./shared
COPY core/agent/control_plane ./control_plane
COPY core/agent/contracts ./contracts
# ADR-0037 / PRD decision 40.5 -- this domain's MCP tool manifest, served BY PATH at import
# from routers/health.py (parents[2] from routers/ lands on core/agent/). Not under shared/,
# control_plane/, or contracts/ above, so it needs its own COPY line (F221: omitting this line
# 503s GET /.well-known/mcp-tools.json -- "this build carries no tool manifest" -- and the
# assembled MCP edge serves 0 agent tools). test_agent_api_image_manifest.py asserts every
# by-path read control_plane/*.py makes is COPYd somewhere in this Dockerfile.
COPY core/agent/mcp.tools.v1.json ./mcp.tools.v1.json
# The domain's routes.v1 manifest: its `verbs` rows say which verbs need a person in the loop, read
# BY PATH at import by control_plane/route_policy.py (parents[1] lands on core/agent/). A build
# without it refuses to start rather than serve those verbs to a worker with nobody watching.
COPY core/agent/routes.v1.json ./routes.v1.json
# The portable workspace primitives (workspace_id/workspace_paths/entities/links), moved out of
# core/agent/shared into their own domain (PRD decision 47, step 2). control_plane imports them as
# `workspaces.shared.<module>` — a plain __init__.py (no FastAPI/pydantic) is enough to make
# `workspaces` a package under PYTHONPATH=/app.
COPY core/workspaces/__init__.py ./workspaces/__init__.py
COPY core/workspaces/shared ./workspaces/shared
# WS-S3: the s3 workspace backend (agent-api: the store; worker: the stdlib client)
COPY core/workspaces/store ./workspaces/store
# The registry of workspace templates (the control plane validates/seeds from one — Phase 6).
# workspace-seeds/<name>/ — one validated seed per subdir (today: default, finos).
COPY behavior/workspaces ./workspace-seeds
# THE PRESET LIBRARY (`_global/asks/`). Before this line the image carried no `behavior/` at all,
# so nothing a deploy did could put a new preset in front of a running instance: `_global/asks/`
# was populated by hand and blank-instance.sh keeps it on purpose (the admin owns those files). On
# 2026-09-05 a first-time visitor met an empty desk because `first-visit.md` was added to the repo
# one day after that instance's library was copied across. `control_plane/preset_library.py` tops
# `_global/asks/` up from here ADDITIVELY at start and at global-ready, and `scaffolds.read_preset`
# falls back to here when the store's copy is merely behind. The destination is pinned to
# `preset_library.DEFAULT_IMAGE_ASKS_DIR` by test_preset_library.py.
COPY behavior/asks ./behavior/asks
# THE ORGANISATION TIER'S SEED (`_global` itself). Same rule as the library above, one directory up:
# `control_plane/global_seed.py` copies these in ADDITIVELY at start and at global-ready, so a fresh
# instance's `_global` arrives scaffolded — the five layer files with their unwritten regions,
# `POLICIES.md`, and the flow pages generated from the flows' own code — and the setup conversation
# fills blanks instead of composing structure. `behavior/mail` lands at `_global/mail/` by the same
# copy: that directory's README already calls the repo copy the SOURCE and
# `/workspaces/_global/mail/<name>.md` the live copy, and until now the only thing that carried one
# to the other was somebody remembering to. Both destinations are pinned by test_global_seed.py.
COPY behavior/global ./behavior/global
COPY behavior/mail ./behavior/mail
# Workspace templates (event organization, client project, …): seeds for `_global/workspace-templates/`,
# copied in additively by global_seed and read by workspace_templates.
COPY behavior/workspace-templates ./behavior/workspace-templates

# The sealed contracts loaded BY PATH at import. In-image layout placed under /app/ so the module
# walk-up (Path(__file__).resolve().parents → /app/contracts/loader.py → /app) resolves
# `meetings/contracts/...` and `agent/contracts/...` (repo_root == /app).
COPY core/meetings/contracts/transcript.v1/transcript.schema.json  ./meetings/contracts/transcript.v1/transcript.schema.json
COPY core/workspaces/contracts/workspace.v1/workspace.schema.json  ./workspaces/contracts/workspace.v1/workspace.schema.json
COPY core/agent/contracts/invoke.v1/invoke.schema.json             ./agent/contracts/invoke.v1/invoke.schema.json
COPY core/agent/contracts/unit.v1/unit.schema.json                 ./agent/contracts/unit.v1/unit.schema.json
COPY core/agent/contracts/routine.v1/routine.schema.json           ./agent/contracts/routine.v1/routine.schema.json
COPY core/agent/contracts/event.v1/event.schema.json               ./agent/contracts/event.v1/event.schema.json
COPY core/agent/contracts/tool.v1/tool.schema.json                 ./agent/contracts/tool.v1/tool.schema.json
COPY core/agent/contracts/models.v1/models.schema.json             ./agent/contracts/models.v1/models.schema.json

EXPOSE 8100
# uvicorn resolves the lazy PEP-562 `app` (control_plane.api:app), which wires the real RuntimeHttpClient
# + Dispatcher at startup.
CMD ["uvicorn", "control_plane.api:app", "--host", "0.0.0.0", "--port", "8100"]
