Skip to content

Container image with Lean, Mathlib, the engine and the workbench, plus CI that smoke-tests it and publishes on version tags - #14

Merged
4waan merged 10 commits into
mainfrom
feat/docker-image
Nov 11, 2026
Merged

4waan merged 10 commits into
mainfrom
feat/docker-image

Conversation

@4waan

@4waan 4waan commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Summary

Cryptificate now has a container route alongside npm start. A multi-stage Dockerfile builds one image holding everything cryptificate serve, check, experiment and verify need: the pinned Lean toolchain, the built lean/ package with Mathlib's compiled library, the release engine, the Rust toolchain and C linker that native experiments compile with (every locked crate is already in the image), the service and the workbench. A new docker workflow builds the image with Buildx and the GitHub Actions cache. It then runs smoke tests inside the image and, on v* tags only, pushes the tested image to ghcr.io/4waan/cryptificate.

The image has not been built on this machine. There is no Docker daemon here and not enough free disk. The CI job is the first place the image gets built. Below I list what I checked natively instead.

What is changed (precise)

No Lean theorem, template, certificate or acceptance rule changes. Nothing in lean/, refinement/, lean-toolchain, lake-manifest.json or the lakefiles is touched.

Dockerfile (new)

  • Base images: pinned by tag and by multi-platform index digest. They are debian:trixie-slim, rust:1.97.1-slim-trixie and node:22.23.4-trixie-slim, with digests read from Docker Hub on 2026-11-02. There is no # syntax= line, so no unpinned frontend image is pulled.
  • lean stage:
    • Installs elan 4.2.4 from its GitHub release archive, checked against a pinned SHA-256 for amd64 and for arm64. I checked that the amd64 archive's elan-init is byte-identical to the elan 4.2.4 binary installed here.
    • Installs the toolchain named in lean/lean-toolchain and runs lake exe cache get. Both run before COPY lean/, so editing Lean sources does not repeat the Mathlib download.
    • Runs lake build. These are the same steps as scripts/setup.sh.
  • engine stage: cargo build --release --locked --bin cryptificate-engine, then cargo fetch --locked, so generated native variants later build offline.
  • web stage: npm ci, then builds the schema package, the server and the web app. The schema package is copied before npm ci because npm ci runs its prepare script (tsc). I confirmed that an npm ci with only the package.json files fails with TS5058.
  • runtime stage:
    • Starts from debian:trixie-slim and adds git, gcc, libc6-dev, tini and ca-certificates, plus the node binary. It copies the Rust toolchain and cargo cache, the engine, the elan toolchain and lean/ with its .lake, and the server, schema and web dist outputs.
    • It also copies crates/kernels and crates/native, which are recorded with native results, refinement/extract.sha256, examples/designs and examples/programs.
    • It runs as uid/gid 1000 (cryptificate) under tini, with EXPOSE 4173.
    • The healthcheck is GET /api/capabilities on 127.0.0.1:$PORT, made with node's fetch.
    • It has OCI labels: title, description, source, url, documentation, licenses, version, revision and base.name.
    • Environment: CARGO_NET_OFFLINE=true, CRYPTIFICATE_ROOT, CRYPTIFICATE_HOME=/var/lib/cryptificate, CRYPTIFICATE_ENGINE, CRYPTIFICATE_LEAN_DIR and CRYPTIFICATE_CONTAINER=1, plus CRYPTIFICATE_REVISION from a build argument.
    • The build fails early unless lake env lean --version and cryptificate --version both work as uid 1000.
  • Why git is in the runtime: Lake runs git rev-parse HEAD and git diff --exit-code HEAD in every package before lake env lean. I observed this with a logging git wrapper. Copying between stages changes inodes and ctimes, so the image sets core.checkStat=minimal and core.trustctime=false. Git then compares only mtime and size and does not rehash the packages on every check. This reasoning follows git's documentation; I have not measured it in an image.
  • .dockerignore: excludes **/.lake, **/target, **/node_modules, **/dist, examples/bundles, examples/measurements and root-level *.tar.gz. It also excludes .cryptificate, .git, refinement/* (except extract.sha256), python, docs and schemas. apps/web/public/recorded/*.tar.gz is kept. I checked that no tracked file the build needs matches an exclude pattern.

Server (apps/server)

  • Bind address:
    • serve takes its bind address from --host, else CRYPTIFICATE_HOST, else 127.0.0.1 (serveHost in server.ts).
    • The loopback default is unchanged everywhere, including in the image: binding all interfaces always takes an explicit flag or variable.
    • The startup warning for non-loopback addresses is unchanged.
    • With CRYPTIFICATE_CONTAINER=1, serve adds a note (bindNotes): a loopback bind is not reachable through docker run -p, the port should be published as -p 127.0.0.1:P:P, and the port number should be the same on both sides.
    • Also with CRYPTIFICATE_CONTAINER=1, when --port differs from PORT, serve notes that the image's health check polls PORT, so Docker would report the container unhealthy, and says to use -e PORT=… instead.
  • Origin policy: unchanged. With --host 0.0.0.0, the Host header must still be a loopback name, and writes must still come from the service's own origin. The new tests pin both points.
  • rust.refinement in the image: with CRYPTIFICATE_CONTAINER=1 it is never proved. refinementStatus returns not_established before reading <state>/refinement.json, with a note saying the image does not check the refinement proofs. A refinement.json in a mounted host state directory or a reused volume records a check made elsewhere, so it is ignored. Outside the image the logic is unchanged.
  • rust.refinement on proof-cache hits: a cache hit used to copy every claim of the run it came from. It now rebuilds rust.refinement from the current refinement status (withCurrentRefinement in claims.ts). That status depends on the machine, not on the cached certificate. Every other cached claim is copied as before. Without this, a cache in a reused state directory could carry a proved refinement claim into the image. It also fixes an older gap: a refinement check recorded after a cached run did not show on later hits.
  • Docs: usage text and apps/server/README.md document CRYPTIFICATE_HOST, PORT, CRYPTIFICATE_ROOT and CRYPTIFICATE_CONTAINER. apps/server/README.md and docs/evidence-model.md (the "Cached exact result" badge) now say that the refinement claim is recomputed on a cache hit.

scripts/docker-smoke.py (new, standard library plus python/cryptificate)

Given an image, the script runs these checks inside containers, with the repository's examples/ mounted:

  1. --version.
  2. check examples/designs/aes.json proves matrix.mds, offsets.dispersion, sbox.bijective and activity.bound.
  3. check with aes-wrong-wire.json gives circuit.equivalence and native.correctness refuted (cargo and gcc inside the image) and rust.refinement not_established.
  4. verify examples/bundles/aes-wrong-wire/*.tar.gz passes: ok, unchanged input digest, at least one match and no mismatch.
  5. serve with no flags is healthy inside the container, cannot be reached through the published port, and prints the note.
  6. serve --host 0.0.0.0 with -p 127.0.0.1:4173:4173:
    • capabilities report Lean, Rust and the engine available, with the Lean library built;
    • / serves HTML;
    • an analysis submitted through the Python client proves the four design claims;
    • a cross-origin POST gets 403.
  7. serve --host 0.0.0.0 -e PORT=4175 with -p 127.0.0.1:4175:4175 (port set by SMOKE_OTHER_PORT) is healthy, answers capabilities through 4175 and prints no health-check note. serve --port 4175 without PORT prints the note that the health check polls 4173.

Steps 1 to 4 run with --network none.

.github/workflows/docker.yml (new)

  • Triggers: pushes to main and v* tags, plus pull requests, filtered to the paths the image depends on; also workflow_dispatch. GitHub does not apply path filters to tag pushes.
  • Disk: the job frees runner disk by removing preinstalled SDKs.
  • Build: docker/build-push-action@v6 with load: true, linux/amd64, cache-from/to: type=gha (mode=min), metadata labels, and version and git describe revision build arguments.
  • Test: runs the smoke script.
  • Publish: only on refs/tags/v*. It logs in to ghcr.io with GITHUB_TOKEN (job permissions contents: read, packages: write), then docker tags and docker pushes the exact tested image under the metadata tags: version, major.minor, latest and sha-….

Docs

  • docs/docker.md (new) covers:
    • what is in the image, with its pins;
    • pull and build;
    • serve with the port rules: loopback default, -p 127.0.0.1:…, and the same port on both sides (set with -e PORT=…, which the health check follows; --port bypasses it) or --allow-origin;
    • a state volume;
    • one-shot check on a mounted design;
    • verify of a bundle;
    • experiments and timing;
    • the uid 1000 permission rule;
    • scope and limits;
    • what CI checks.
  • The README gains a "With Docker" subsection under Install and a docs link.
  • docs/architecture.md gains one row for the Dockerfile.

Tests run

Command Result
npm run build passes
npm test schema 14/14, server 44 passed, 5 skipped (integration, skipped by default as before), web 53/53
node --test dist/test/http.test.js 6/6, including the new bind-address, --port/PORT health-check note and all-interfaces origin-policy tests
node --test dist/test/pipeline.test.js 10/10. Covers the container refinement note, not_established in the container even with a valid refinement.json, and a cache hit that recomputes rust.refinement after refinement.json is removed. That last assertion fails if the old copy-all-claims code is restored.
CARGO_TARGET_DIR=<worktree>/target cargo test --workspace -j2 154 passed, 0 failed, 2 ignored (unchanged Rust code; run before the review fixes, which touch no Rust)
python3 -m unittest discover -s python/tests -t python 9 tests OK
python3 -m py_compile scripts/docker-smoke.py OK
lake build in lean/ Build completed successfully (2018 jobs), no Lean changes (run before the review fixes, which touch no Lean)

Native stage checks. I ran each stage's build commands natively: cargo build --release --locked --bin cryptificate-engine, lake build, and npm ci plus the three builds in a directory holding only what the web stage copies.

Emulated runtime layout. I assembled the exact runtime file tree under a scratch directory, from the paths the Dockerfile copies, and ran it with a clean environment (env -i) set to the image's ENV values:

  • check examples/designs/aes.json proves the four design claims (Lean 5.6 s).
  • check … --program aes-wrong-wire.json refutes the circuit in Lean and natively, with CARGO_NET_OFFLINE=true.
  • check … --program aes-known-shared-sum.json proves equivalence, and native correctness passes on 9214 cases. rust.refinement is not_established, with the image note.
  • verify examples/bundles/aes-wrong-wire/aes-wrong-wire.tar.gz exits 0 with all re-checked claims matching.
  • experiment … --profile quick completes in a network namespace with no network (unshare -n), through search, equivalence, native correctness and three benchmark sessions.
  • serve, with the healthcheck command copied from the Dockerfile, returns 0.

Smoke script. I ran scripts/docker-smoke.py end to end against that layout through a stand-in for the docker command, and all steps passed. A deliberately broken stand-in, where the default serve is reachable, makes step 5 fail as intended. The stand-in cannot reproduce Docker networking or port publishing, so step 5's reachability check is fully exercised only in CI.

Review fixes, checked end to end without Docker.

  • I ran cryptificate check examples/designs/aes.json --program examples/programs/aes-wrong-wire.json with real Lean, the release engine and a state directory holding a valid refinement.json.
    • Without CRYPTIFICATE_CONTAINER, it reports rust.refinement proved (live run).
    • With CRYPTIFICATE_CONTAINER=1 and the same state directory, it reports not_established with the image note, on a proof-cache hit of that first run.
  • I wrote a stand-in for docker that runs the built service on the host with the image's ENV and evaluates the Dockerfile's HEALTHCHECK command with the container's PORT.
    • It reproduces the reported problem. With serve --host 0.0.0.0 --port 4175, the service answers 200 on 4175 while the health check reports unhealthy.
    • With -e PORT=4175, the health check reports healthy.
    • Smoke step 7 passes against the stand-in.
    • When the stand-in drops -e options, step 7 fails.

YAML and shell. The workflow parses as YAML, and the tag-push loop passes bash -n and a dry run.

Scope and limits

  • The image has not been built or run here. Expect the first CI run to be the first real build; whatever it finds will need fixing.
  • No Dockerfile linter was run. The Dockerfile was checked by reading every copied path and by the native runs above.
  • Only linux/amd64 is built and tested. The arm64 elan checksum is pinned, but no arm64 build has been attempted.
  • Size and disk. Expect an image over 10 GB unpacked. Measured here on disk: the Lean toolchain is 2.9 GB and the Lean packages are 7.2 GB. Builders need roughly 25 GB free; that is an estimate. The workflow frees runner disk first, but whether a standard runner has enough room is only known after the first run.
  • The image does not check the refinement proofs (refinement/: Lean v4.35.0 and a second Mathlib). In the image, rust.refinement is not_established, and the docs say so.
  • Lean trusts the same things as scripts/setup.sh: the pinned toolchain and Mathlib's compiled library from lake exe cache get.
  • Ownership. The image runs as uid 1000; other uids via --user are not supported, and outputs written to mounts belong to uid 1000.
  • Timings from experiment in a container describe the container on that host, including any Docker CPU limits.
  • Port changes in the image must use -e PORT=…. With serve --port alone, the service works, but the health check polls the old port and Docker reports the container unhealthy. serve prints a note saying so. Smoke step 7 covers both cases. The stand-in run above exercised it, but real Docker networking for step 7 is exercised only in CI.

Generated by Claude Code

serve keeps binding 127.0.0.1 by default; --host or CRYPTIFICATE_HOST is
now the only way to widen it. With CRYPTIFICATE_CONTAINER=1 (set by the
container image) serve says how to reach it through a published port,
and rust.refinement says the image does not check the refinement proofs.
A multi-stage Dockerfile: elan with the pinned Lean toolchain, Mathlib's
build cache and lake build of lean/; the release engine with every locked
crate fetched; the schema package, service and workbench. The Debian slim
runtime runs as uid 1000 under tini, keeps the service on loopback unless
--host or CRYPTIFICATE_HOST widens it, and checks its own health on
/api/capabilities. scripts/docker-smoke.py runs check, verify and serve
inside the image.
… image

The container branch ran only when the state directory held no valid
refinement.json, so a mounted host state directory or a reused volume made
the image report the refinement proofs as checked on this machine. The image
cannot check them; it now returns not_established before reading the record.
A cache hit copied every claim of the run it came from, including
rust.refinement, whose status depends on whether this machine checked the
refinement proofs. A state directory reused from a source checkout could so
carry a proved refinement claim into the container image, and a refinement
check recorded after a cached run did not show on later hits. The claim is
now rebuilt from the current refinement status; the cached Lean-backed
claims are unchanged.
The image's health check polls 127.0.0.1:$PORT, and serve --port does not
change PORT, so the port change docs/docker.md described left Docker
polling 4173 and reporting a working container unhealthy. The docs now give
the port with -e PORT, and serve prints a note inside the image when --port
differs from PORT.
Resolve the overlap with the branches merged before it, as in the
release/v0.2.0 integration.
@4waan
4waan merged commit 773bc27 into main Nov 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants