Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ added `microcosm-emitter status`, which the US Modal stage runner runs after
each stage to record how many telemetry events the stage's emitter left
undelivered. The release is
written once, in `packages/microcosm-build/src/microcosm/build/minimum_emitter_version.json`,
which builds read as package data and the Modal image's install step reads
which builds read as package data and the install steps of the Modal images
(the US stage runner's and the UK launcher's, `tools/modal_uk_cut.py`) read
from the launching checkout. Releases before 0.10.0 send telemetry to the
collector's Cloud Run address, which changes when the collector moves to
another Google Cloud project; from 0.10.0 on, releases send to the
Expand Down
1 change: 1 addition & 0 deletions changelog.d/1757-uk-modal-launcher-emitter.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`tools/modal_uk_cut.py`'s image installs the separately installed `microcosm-emitter` (microcosm#1757), so a UK build run on Modal no longer stops at `run_uk_cut.py`'s preflight, which starts the emitter to check write access (#1460). As in the US Modal image, the install step asks for `microcosm-emitter>=` the release `packages/microcosm-build/src/microcosm/build/minimum_emitter_version.json` names in the launching checkout, installs it into `/usr/local/bin` outside the environment the container syncs, and runs its `version` so a command that cannot run fails the image build; `MICROCOSM_EMITTER_COMMAND` points the build at it. Before it downloads anything, the image now also drops the compromised eMudhra (emSign) certificate authorities as the US image does (`emudhra-ca-trust`): it upgrades `ca-certificates`, replaces the base's pip 25.3 with the hash-pinned pip 26.2.2, and installs uv 0.13.4 instead of 0.11.7 and certifi 2026.10.19 or later for the image's own Python; it also sets `UV_PYTHON_DOWNLOADS=never`. The launcher refuses a minimum file it cannot read, its plan names the emitter requirement, and the plan refuses a commit whose tree it cannot read, or whose `minimum_emitter_version.json` names a higher release than the launching checkout's, which the build's preflight would otherwise refuse in the paid container. The emitter's event queue files stay in the container's cache directory, as in the US runner, so hand-overs still queued when the container exits are lost with it. The launcher has still never run on Modal.
33 changes: 21 additions & 12 deletions docs/orrery-publication.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,27 +146,36 @@ Hugging Face.
(`>=0.16.0,<1`), the dependency-free package of typed calls, which `uv.lock`
records with its hashes. The emitter itself, `microcosm-emitter`, is not a
workspace dependency: each machine installs it on its own with
`uv tool install microcosm-emitter`, and the Modal image installs the newest
release when it is built for a plan commit. A running build keeps the
`uv tool install microcosm-emitter`. The US Modal stage runner's image
installs the newest release when it is built for a plan commit; the UK
launcher's image (`tools/modal_uk_cut.py`), which clones its commit only at
run time, installs the newest release when Modal first builds that step and
keeps it until the minimum below rises or Modal rebuilds the layer, as it
does when an earlier step of the image or Modal's image builder version
changes. A running build keeps the
emitter it started; the next build uses whichever release is installed then.
So an emitter release needs no Microcosm change. The socket protocol carries
its own version (`PROTOCOL_VERSION`), every emitter release accepts the
versions interfaces still send, and an emitter that lacks a module the build
asks for starts without it.

A build does not start an emitter older than `MINIMUM_EMITTER_VERSION` in
`microcosm.build.telemetry_emitter`, now 0.17.0, and the Modal image installs
at least the same release (`image_emitter_requirement()` in
`tools/modal_us_stage_plan.py`, `microcosm-emitter>=0.17.0`). The release is
`microcosm.build.telemetry_emitter`, now 0.17.0, and both Modal images install
at least the same release (`microcosm-emitter>=0.17.0`): the US stage
runner's through `image_emitter_requirement()` in
`tools/modal_us_stage_plan.py`, the UK launcher's through
`emitter_requirement()` in `tools/modal_uk_cut.py`. The release is
written once, in microcosm-build's package data,
`packages/microcosm-build/src/microcosm/build/minimum_emitter_version.json`:
the adapter reads it as package data, and the runner, which imports nothing
from the workspace, reads it by its path in the checkout that launches the
app. The image adds that checkout's file beside the runner's copy of the
plan module, so the check, which runs in the container, reads the same
file. At a Modal launch the install step therefore comes from the launching
checkout and the minimum from the commit the plan pins (see the
[US Modal stage runbook](us-modal-stage-runbook.md)). Releases
the adapter reads it as package data, and both launchers, which import
nothing from the workspace, read it by its path in the checkout that
launches the app. At a Modal launch the install step therefore comes from
the launching checkout and the minimum from the commit the build runs. The
US runner's image adds the launching checkout's file beside its copy of the
plan module, so its check, which runs in the container, reads the same file
(see the [US Modal stage runbook](us-modal-stage-runbook.md)). The UK
launcher's plan reads the commit's file with `git show` and refuses a
commit whose minimum is higher than the launching checkout's. Releases
before 0.10.0 send telemetry to the collector's Cloud Run address, which
changes when the collector moves to another Google Cloud project. From 0.10.0
on, the emitter's default collector address is the collector's hostname,
Expand Down
8 changes: 8 additions & 0 deletions docs/uk-cut-anyone.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ the Mac is busy, short of disk or asleep. The container:

- clones a pushed commit on main;
- syncs that commit's own lock with the `uk` extra;
- runs the `microcosm-emitter` its image installs, at or above the release
`minimum_emitter_version.json` names in the checkout you launch from, as
the US Modal image does. The build checks the minimum of the commit it
clones, so the plan refuses a commit whose minimum is higher: launch from
a checkout at or after that commit. Like the US image, the image first
drops the compromised eMudhra certificate authorities (an upgraded CA
store, a hash-pinned pip, uv 0.13.4 and a certifi floor) before it
downloads anything;
- keeps the work directory, with its inputs and every step's outputs, on the
`microcosm-uk-cuts` volume, so a rerun resumes.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@
STARTUP_TIMEOUT_SECONDS = 15.0
#: The package data file that holds the oldest emitter release a build
#: starts, ``MINIMUM_EMITTER_VERSION``. It is the one place the release is
#: written: the Modal stage runner (``tools/modal_us_stage_plan.py``), which
#: imports nothing from the workspace, reads the same file by its path in the
#: checkout that launches it, and installs at least that release in its image.
#: written: the US Modal stage runner (``tools/modal_us_stage_plan.py``) and
#: the UK Modal launcher (``tools/modal_uk_cut.py``), which import nothing
#: from the workspace, read the same file by its path in the checkout that
#: launches them, and install at least that release in their images.
MINIMUM_EMITTER_VERSION_RESOURCE = "minimum_emitter_version.json"
_RELEASE_NUMBER = re.compile(r"(\d+)\.(\d+)\.(\d+)")

Expand Down
267 changes: 258 additions & 9 deletions packages/microcosm-build/tests/engine_free/uk/test_modal_uk_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from __future__ import annotations

import importlib.util
import json
import re
import shutil
import sys
from pathlib import Path
Expand Down Expand Up @@ -59,31 +61,280 @@ def test_without_the_opt_in_no_credential_is_attached(monkeypatch):
module.main("a" * 40, "t", run=True)


def _git_answering(module, commit_minimum, *, commit_readable=True):
"""``subprocess.run`` for ``plan``: the commit is on main, and its
``minimum_emitter_version.json`` names ``commit_minimum`` (``None``: the
commit has no such file, as before microcosm#1522). Without
``commit_readable``, git cannot read the commit's tree, and answers
``ls-tree`` and ``show`` as it does then."""

def run(argv, **kwargs):
unreadable = module.subprocess.CompletedProcess(
argv, 128, stdout="", stderr="fatal: not a tree object"
)
if argv[:2] == ["git", "ls-tree"]:
if not commit_readable:
return unreadable
listed = "" if commit_minimum is None else f"{argv[-1]}\n"
return module.subprocess.CompletedProcess(argv, 0, stdout=listed, stderr="")
if argv[:2] == ["git", "show"]:
if not commit_readable or commit_minimum is None:
return unreadable
return module.subprocess.CompletedProcess(
argv,
0,
stdout=json.dumps({"minimum_emitter_version": commit_minimum}),
stderr="",
)
return module.subprocess.CompletedProcess(argv, 0)

return run


def _launching_minimum():
from microcosm.build.telemetry_emitter import MINIMUM_EMITTER_VERSION

return MINIMUM_EMITTER_VERSION


def test_planning_contacts_no_modal_service(monkeypatch, capsys):
monkeypatch.setenv("HF_TOKEN", "hf-test")
monkeypatch.delenv(UK_GATE_SIGNING_KEY_ENV, raising=False)
module, stub = _load_app(monkeypatch)
monkeypatch.setattr(
module.subprocess,
"run",
lambda argv, **kw: module.subprocess.CompletedProcess(argv, 0),
module.subprocess, "run", _git_answering(module, _launching_minimum())
)
stub.reset_mock()
assert module.plan("a" * 40, "t") == [UK_GATE_SIGNING_KEY_ENV]
assert stub.mock_calls == []
assert "missing credentials" in capsys.readouterr().out


def _image_steps(stub, step):
"""The calls named ``step`` the module made on the stub, in order."""

return [entry for entry in stub.mock_calls if entry[0].split(".")[-1] == step]


def _image_chain(stub):
"""Each image-definition call as (step name, arguments), in order."""

return [
(name.rsplit(".", 1)[-1], args)
for name, args, _ in stub.mock_calls
if name.startswith("Image.debian_slim")
]


def _emitter_installs(stub):
return [
step.args
for step in _image_steps(stub, "run_commands")
if any("microcosm-emitter" in command for command in step.args)
]


def _load_us_plan(monkeypatch):
"""The US Modal stage runner's plan module, loaded by path like the tools."""

spec = importlib.util.spec_from_file_location(
"modal_us_stage_plan_beside_uk_launcher",
ROOT / "tools" / "modal_us_stage_plan.py",
)
module = importlib.util.module_from_spec(spec)
assert spec.loader is not None
# dataclasses resolve string annotations through sys.modules.
monkeypatch.setitem(sys.modules, spec.name, module)
spec.loader.exec_module(module)
return module


def test_the_image_carries_the_registry_for_the_container(monkeypatch):
module, stub = _load_app(monkeypatch)
built = stub.Image.debian_slim.return_value.apt_install.return_value
added = built.pip_install.return_value.env.return_value.add_local_file
added.assert_called_once_with(str(module.REGISTRY_SOURCE), module.REGISTRY_REMOTE)
# The registry's copy is the last step, called on the image the emitter's
# install returns: Modal refuses build steps after a local file.
added = [
entry
for entry in stub.mock_calls
if entry[0]
== (
"Image.debian_slim().apt_install().run_commands().pip_install()"
".env().run_commands().add_local_file"
)
]
assert [entry.args for entry in added] == [
(str(module.REGISTRY_SOURCE), module.REGISTRY_REMOTE)
]
assert _image_chain(stub)[-1][0] == "add_local_file"
assert module.REGISTRY_SOURCE.is_file()
# Modal places the app module in /root, beside the registry's copy.
assert Path(module.REGISTRY_REMOTE).parent == Path("/root")


def test_the_image_trusts_no_compromised_certificate_authority(monkeypatch):
"""As the US Modal image does, before anything is downloaded.

The compromised eMudhra (emSign) roots: uv before 0.13.4 compiles them in,
the cached base's pip 25.3 bundles them, and the base's CA store, which
git and the standard library read, holds them until it is upgraded.
"""

us_plan = _load_us_plan(monkeypatch)
_, stub = _load_app(monkeypatch)
chain = _image_chain(stub)
assert [name for name, _ in chain] == [
"debian_slim",
"apt_install",
"run_commands",
"pip_install",
"env",
"run_commands",
"add_local_file",
]
assert "ca-certificates" in chain[1][1]
assert chain[2][1] == (us_plan.image_pip_bootstrap_command(),)
# certifi for the image's own Python, which Modal's client uses.
assert chain[3][1] == (
f"uv=={us_plan.IMAGE_UV_VERSION}",
f"certifi>={us_plan.RUNNER_CERTIFI_MINIMUM_VERSION}",
)


def test_the_image_installs_the_emitter_at_the_builds_minimum_release(monkeypatch):
"""The pre-flight starts the separately installed emitter (#1410, #1460)."""

_, stub = _load_app(monkeypatch)
assert _emitter_installs(stub) == [
(
"UV_TOOL_BIN_DIR=/usr/local/bin uv tool install "
f"'microcosm-emitter>={_launching_minimum()}'",
"/usr/local/bin/microcosm-emitter version",
)
]


def test_the_build_is_pointed_at_the_installed_emitter(monkeypatch):
_, stub = _load_app(monkeypatch)
(environment,) = _image_steps(stub, "env")
assert environment.args[0].get("MICROCOSM_EMITTER_COMMAND") == (
"/usr/local/bin/microcosm-emitter"
)


def test_uv_never_downloads_a_python_in_the_image(monkeypatch):
"""The image's 3.13 meets the lock and the emitter, as CI tests the lock."""

_, stub = _load_app(monkeypatch)
(environment,) = _image_steps(stub, "env")
assert environment.args[0].get("UV_PYTHON_DOWNLOADS") == "never"


def test_the_launchers_agree_on_the_emitter(monkeypatch):
us_plan = _load_us_plan(monkeypatch)
module, _ = _load_app(monkeypatch)
assert module.emitter_requirement() == us_plan.image_emitter_requirement()
assert module.EMITTER_COMMAND == us_plan.IMAGE_EMITTER_COMMAND
assert module.EMITTER_COMMAND_ENV == us_plan.EMITTER_COMMAND_ENV


@pytest.mark.parametrize(
"content",
[
None,
"not json",
"[]",
'{"minimum_emitter_version": 17}',
'{"minimum_emitter_version": "0.17"}',
'{"minimum_emitter_version": "0.17.0 "}',
],
)
def test_an_unreadable_minimum_release_stops_the_image_definition(
monkeypatch, tmp_path, content
):
module, _ = _load_app(monkeypatch)
version_file = tmp_path / "minimum_emitter_version.json"
if content is not None:
version_file.write_text(content, encoding="utf-8")
monkeypatch.setattr(
module, "MINIMUM_EMITTER_VERSION_FILE", version_file, raising=False
)
with pytest.raises(SystemExit, match=re.escape(str(version_file))):
module._image()


def test_the_container_defines_the_image_without_the_checkout(monkeypatch, tmp_path):
"""The container imports the module at /root, with no checkout around it."""

module, stub = _load_app(monkeypatch)
monkeypatch.setattr(
module,
"MINIMUM_EMITTER_VERSION_FILE",
tmp_path / "missing" / "minimum_emitter_version.json",
raising=False,
)
stub.is_local.return_value = False
stub.reset_mock()
module._image()
assert _emitter_installs(stub) == []
assert _image_steps(stub, "add_local_file") == []


def test_the_plan_names_the_emitter_the_image_installs(monkeypatch, capsys):
module, _ = _load_app(monkeypatch)
monkeypatch.setattr(
module.subprocess, "run", _git_answering(module, _launching_minimum())
)
module.plan("a" * 40, "t")
assert f"emitter microcosm-emitter>={_launching_minimum()}" in (
capsys.readouterr().out
)


def test_the_plan_refuses_a_commit_that_needs_a_newer_emitter(monkeypatch):
"""Preflight in the paid container would refuse the image's emitter."""

module, _ = _load_app(monkeypatch)
monkeypatch.setattr(module.subprocess, "run", _git_answering(module, "99.0.0"))
with pytest.raises(SystemExit, match=r"99\.0\.0.*checkout at or after"):
module.plan("a" * 40, "t")


def test_the_plan_refuses_a_commit_whose_tree_it_cannot_read(monkeypatch):
"""git answers a missing commit and a missing path alike (exit 128)."""

module, _ = _load_app(monkeypatch)
monkeypatch.setattr(
module.subprocess,
"run",
_git_answering(module, _launching_minimum(), commit_readable=False),
)
with pytest.raises(SystemExit, match="aaaaaaaaa"):
module.plan("a" * 40, "t")


def test_the_plan_refuses_a_commit_whose_minimum_it_cannot_read(monkeypatch):
module, _ = _load_app(monkeypatch)
monkeypatch.setattr(module.subprocess, "run", _git_answering(module, "0.17"))
with pytest.raises(SystemExit, match="aaaaaaaaa"):
module.plan("a" * 40, "t")


@pytest.mark.parametrize("commit_minimum", ["launching", "0.13.0", None])
def test_the_plan_accepts_a_commit_the_image_emitter_satisfies(
monkeypatch, commit_minimum
):
"""The same minimum, an older one, or none: commits before microcosm#1522
kept an older minimum in code."""

module, _ = _load_app(monkeypatch)
if commit_minimum == "launching":
commit_minimum = _launching_minimum()
monkeypatch.setattr(
module.subprocess, "run", _git_answering(module, commit_minimum)
)
module.plan("a" * 40, "t")


def test_the_registry_loads_from_a_bare_copy(monkeypatch, tmp_path):
"""In the container only the copied file exists, with no microcosm."""

Expand All @@ -109,9 +360,7 @@ def test_missing_credentials_are_asked_for_one_command(monkeypatch):
monkeypatch.delenv(UK_GATE_SIGNING_KEY_ENV, raising=False)
module, _ = _load_app(monkeypatch)
monkeypatch.setattr(
module.subprocess,
"run",
lambda argv, **kw: module.subprocess.CompletedProcess(argv, 0),
module.subprocess, "run", _git_answering(module, _launching_minimum())
)
with pytest.raises(SystemExit, match="to this command") as refusal:
module.main("a" * 40, "t", run=True)
Expand Down
Loading
Loading