# A Python app, for a server rather than a laptop.
#
# Two stages so the runtime carries no build tooling: `uv` resolves and
# installs into /app/.venv, and the second stage copies the venv and the source
# next to it. The install is deliberately editable, which matters because the
# app reads files from beside its package (templates, `app/static`, `seed/`):
# that path is true in a checkout and true here, and false in site-packages.
# Ours: roledesk has no `frontend/` and no Node stage, since the server renders
# Jinja templates and serves `app/static` as it is.
#
# Already in place at deploy/Dockerfile, with its ignore file beside it.
# 2-build-image.ps1 builds with it from the `git archive` of the production
# commit that step 1 exports, so the recipe is versioned with the code it builds.
#
# The package is `app` and stays `app` (the domain noun is `role`). Edit the
# two `uv sync` lines when you add extras.
#
# Each base image is pinned by tag and digest: the tag says which release, the
# digest makes a rebuild next month install the same bytes. The digest is the
# multi-platform index's. To move on, look up the new tag's digest on its
# registry (Docker Hub's tag API for python, ghcr.io for uv) and
# replace both halves.

ARG PYTHON=3.15-slim@sha256:2da676046630d7bf14c6764ae1143dc7fa0377a53f94bf5a642f0b214aca5bce
ARG UV=0.13.1@sha256:4a1f3666816dafadd20ceb71eac9a7d56d7ef30279f7d64048519ab901ada336

FROM ghcr.io/astral-sh/uv:${UV} AS uv

# ------------------------------------------------------------------ build

FROM python:${PYTHON} AS build
COPY --from=uv /uv /usr/local/bin/uv

ENV UV_LINK_MODE=copy \
    UV_COMPILE_BYTECODE=1 \
    UV_PYTHON_DOWNLOADS=never

WORKDIR /app

# Dependencies first and on their own layer, keyed on the lock file alone, so
# editing a Python file rebuilds in seconds rather than re-resolving the tree.
# The extras are the same on both `uv sync` lines: `passkeys`, without which
# the passkey routes answer 501, `embeddings`, the default ranking backend
# (torch comes from the CPU index named in pyproject.toml, [tool.uv.sources],
# not the CUDA build; the model itself is downloaded by the first `embed` job
# into HOME, which is the /data volume), and `llm`, the SDKs the compose file
# passes keys for.
COPY pyproject.toml uv.lock README.md ./
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev --no-install-project \
        --extra passkeys --extra embeddings --extra llm

COPY app app
# The module folders this repo ships beside the app, here and again in the
# runtime stage below. `pyproject.toml` lists `pipeline*`, `auth*`, `llm*` and
# `desk*` in its packages as well, or the install leaves them out.
COPY pipeline pipeline
# The door. A project that does not want one drops this line *and* the
# `auth.connect` call in `app/main.py` together: dropping the folder and
# leaving the call is a container that fails at startup, and dropping the call
# and leaving the folder is a container that starts with the door open.
COPY auth auth
# The model call. Nothing imports it until an app adds its SDKs, and then the
# folder has to be here already, so that is not a Dockerfile edit.
COPY llm llm
# Ours: the source contract and the generic desk modules, the plugin folder the
# config's `plugin_paths` names, and the data a first run imports (`seed/`).
COPY desk desk
COPY plugins plugins
COPY seed seed
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev \
        --extra passkeys --extra embeddings --extra llm

# ---------------------------------------------------------------- runtime

FROM python:${PYTHON}

# Nothing is built here, so nothing but the interpreter's own runtime is
# needed. `tini` is the one addition: a web server's shutdown and a worker
# pool's stop both hang off SIGTERM, and PID 1 without an init reaps no
# children and forwards no signals, so a `docker compose down` would wait the
# full ten seconds and then kill the workers mid-job.
RUN apt-get update \
 && apt-get install -y --no-install-recommends tini \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY --from=build /app/.venv /app/.venv
COPY app app
COPY pipeline pipeline
# The login page and the two scripts are files inside this folder, served from
# routes; see the build stage above.
COPY auth auth
COPY llm llm
COPY desk desk
COPY plugins plugins
COPY seed seed

ENV PATH=/app/.venv/bin:$PATH \
    PYTHONUNBUFFERED=1 \
    APP_CONFIG=/data/config.yaml \
    HOME=/data/home

# In the volume, not the image: whatever uid the compose file runs as owns
# /data, and a CLI the app shells out to — the `claude` binary, for one —
# wants somewhere to write. An image-baked /home/app would be right for uid
# 1000 and unwritable for anyone else.
#
# The image runs as 1000:1000, the compose file's default PUID:PGID and the
# owner docs/Install Server.md gives the data folder, so a `docker run` without a
# `user:` does not write root-owned files into it. /data is made here, before
# VOLUME, so an anonymous volume starts out owned by that uid too.
RUN install -d -o 1000 -g 1000 /data /data/home
USER 1000:1000
VOLUME /data

EXPOSE 8787

# A session-free probe that checks the application can still read its store.
# `docker compose up --wait` in deploy.sh waits on exactly this, so a probe
# that only proves the port is open buys a deployment that reports success
# while the database is unreachable.
HEALTHCHECK --interval=60s --timeout=5s --start-period=20s --retries=3 \
    CMD python -c "import urllib.request as u; u.urlopen('http://127.0.0.1:8787/health', timeout=4)"

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["app", "serve", "--host", "0.0.0.0", "--port", "8787"]
