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
1 change: 1 addition & 0 deletions changelog.d/1498-modal-copied-back-staging.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`tools/stage_uk_rowwise_candidate.py --modal-receipt <receipt>` stages a UK run directory copied back from a Modal run with `modal volume get`, for when a UK build is registered with the Modal runner. Before it reads any output, it checks that the receipt is of a stage that finished (`COMPLETED`, not stopped at its budget) and that the directory is inside the run's copied-back state directory (`state/` beside the receipt's `receipts/` directory, or `--modal-state-root`, both resolved). The receipt must list the build manifest and every output it registers; an output the stage never copied to the runs volume (`outputs_not_mirrored`) is named as such. Every output must have the receipt's SHA-256 and size. So must the build manifest unless its `staged_dataset` record is `local_and_remote`, as an attempt to hand the directory over from the workstation leaves it whenever it records its outcome in the manifest; the command reads that mode from the manifest, so a manifest edited to say `local_and_remote` is not compared either, while its outputs still are. Every refusal comes before the command starts an emitter. A directory that passes is handed over with the re-stage command's existing code, so the records, retries and landed-bundle handling are those of a directory built on the workstation. With `--local-only` and `--modal-receipt`, the command hands nothing over: it checks the directory and writes the staged manifest and the checksum file, but leaves the build manifest as it is, so a manifest that still holds the build's record is still compared by later runs with the receipt. On a copied-back directory, pass the receipt with `--local-only`: without it, `--local-only` writes a `local_only` record into the build manifest, which can make every later run with the receipt refuse the directory. The command now reports a build manifest that is not valid JSON or not a JSON object, and an output that does not match its manifest digest, as an error message instead of a traceback.
65 changes: 64 additions & 1 deletion docs/uk-staging-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,70 @@ chained to the next row). So the dashboard keeps
showing such a run without its bundle. An identical remote bundle the manifest
does not know about is recorded as `already_staged` with the bundle's own
commit; a different bundle
under the same run id is refused (`REMOTE_DIFFERS`). Fetch a bundle for a scorecard, an evaluation leg or the
under the same run id is refused (`REMOTE_DIFFERS`).

No UK program is registered with the Modal runner yet; the runner's
programs are all US builds today (`tools/modal_us_stage_plan.py`), and none of
them writes a staged-dataset bundle. A UK build run there would stage nothing
from its container: the runner sets `HF_HUB_OFFLINE=1` for every tool, so the
build would record its bundle as `local_only` (see above). After `modal volume
get` has copied such a run back
([the Modal runbook](us-modal-stage-runbook.md#commands), step 5), you would
stage it from the workstation, with your own emitter and Hub credential, by
naming the stage's receipt:

```bash
uv run python tools/stage_uk_rowwise_candidate.py \
--run-dir ./modal-runs/<run_id>/state/<output directory> \
--modal-receipt ./modal-runs/<run_id>/receipts/<stage>-<utc>.json
```

Before it reads any output, the command checks that the receipt is of a
stage that finished (`COMPLETED`, and not stopped at its budget) and that the
run directory, wherever a link or `..` in its path leads, is inside the run's
state directory: `state/` beside the receipt's `receipts/` directory, or the
directory `--modal-state-root` names, either resolved the same way. The
receipt must list the build manifest and every output it registers, at their
paths under the state directory. An output the stage hashed but never copied
to the runs volume (the receipt's `outputs_not_mirrored`) is named as such.
Unless the build manifest's `staged_dataset` record is `local_and_remote`,
the manifest must have the receipt's SHA-256 and size, so a manifest edited to
register another file the stage wrote, rename a role or change any other field
is refused. An attempt to hand the directory over from the workstation that
records its outcome in the manifest writes a `local_and_remote` record,
whether the bundle landed, failed or was refused, so after such an attempt the
manifest is not compared. A bundle the emitter queued without copying the
manifest leaves the manifest as handed over, with the receipt's bytes and the
build's record, and it is then still compared. The command reads that mode from the manifest
itself, so it cannot tell a record a hand-over wrote from one edited to say
`local_and_remote`: such an edit also ends the manifest's comparison, and the
edits above are then not caught. Every output must have the receipt's SHA-256
and size whatever the record says, so an output whose bytes differ from the
receipt's is always refused. Every refusal comes before an emitter starts.
`staged_manifest.json` and `sha256sums.txt` are not compared, because staging
writes them again. Then the command stages the directory as above, under the
build's own run id: the hand-over, the records, a re-stage after a failed
attempt and a second run on a landed bundle work as they do for a directory
built on the workstation. Unset `HF_HUB_OFFLINE` first if your shell sets it:
the command refuses to stage while it is set. With `--local-only` it checks
the directory against the receipt and writes `staged_manifest.json` and
`sha256sums.txt`, but leaves the build manifest as it is, so a manifest that
still holds the build's record is still compared by later runs with the
receipt.

> **Warning:** on a copied-back directory, always pass `--modal-receipt` with
> `--local-only`. Without the receipt, `--local-only` writes a `local_only`
> record into the build manifest. Whenever that record differs from the
> build's own (a build that disabled its staged dataset, another `--run-id`, a
> national build, or a hand-over that did not land), every later run with the
> receipt refuses the directory as "not the build manifest the Modal stage
> wrote". If that has happened, fetch the run again with `modal volume get`.

Nothing here submits the bundle to the collector: the command's emitter runs its Hugging Face module only, and builds do not submit
artifacts to the collector's artifact intake yet
(PolicyEngine/microcosm-collector#12).

Fetch a bundle for a scorecard, an evaluation leg or the
dashboard's local-directory mode with:

```bash
Expand Down
19 changes: 19 additions & 0 deletions docs/us-modal-stage-runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,17 @@ python3 tools/modal_us_stage_plan.py verify-receipt \
--state-root ./modal-runs/<run_id>/state
```

No program registered here writes a staged-dataset bundle yet; see "What
this does not cover yet". A UK rowwise build's run directory, whose
`rowwise_candidate_manifest.json` registers one, would be staged from the
workstation after step 5, with the operator's own emitter and Hub credential:
`tools/stage_uk_rowwise_candidate.py --modal-receipt` checks the outputs the
build manifest registers against the receipt, and the manifest itself until
its record says a hand-over happened, then hands the bundle over as for a
local build. It reads that record from the manifest itself, so an edit that
says a hand-over happened is not caught; the outputs are still compared (see
[Staged datasets](uk-staging-operations.md#staged-datasets)).

To prove the run path on a new commit or workspace for well under a cent,
run `docs/us-modal-stage-smoke-plan.json` (tool `runner-smoke`). It is an
inline script, so it needs no file in the pinned tree. It imports the synced
Expand Down Expand Up @@ -1622,6 +1633,14 @@ hold digests, sizes and paths, never file contents.
placement avoids preemption (see "Preemption and restarts"). The heavy
class sets a memory request but no hard limit. What Modal does with a
container killed for memory has not been observed here.
- **Staging a US build's datasets.** Only UK builds write a staged-dataset
bundle (their manifest's `outputs`, staged under `staged/<run_id>/` on a
private repository), and the copied-back staging command reads only their
manifest. The `package` stage writes a release directory with its own
`release_manifest.json` (an `artifacts` mapping) and `sha256sums.txt`, which is
also the name of the bundle's checksum file. A US bundle needs a private
repository, a manifest that registers its outputs, and a command that reads
it; none exists yet.
- **Certification.** A receipt proves which bytes a stage produced. It does
not certify a release. Preflight (`tools/preflight_us_release_gates.py`)
and certification still run on the output as before.
Original file line number Diff line number Diff line change
Expand Up @@ -1295,23 +1295,6 @@ def _drive_epochs(args, telemetry, epochs: int = 2) -> None:
)


def _remote_hub(monkeypatch, client_options=None, **kwargs):
"""One fake Hub for the run files and the bundle the build's emitter commits."""
from test_support.microcosm_build.telemetry import share_fake_hub
from test_support.microcosm_build.uk_rowwise_candidate import _FakeHub

hub = _FakeHub(**kwargs)
share_fake_hub(monkeypatch, hub, **(client_options or {}))
return hub


def _bundle_emitter_for(hub):
"""The re-stage tool's own short-lived emitter, committing to ``hub``."""
from test_support.microcosm_build.emitter import FakeEmitterClient

return lambda: FakeEmitterClient(hub_files=hub.files, hub_api=hub)


def test_no_staging_records_both_opt_outs(tmp_path, monkeypatch):
pytest.importorskip("tables")
status, out = run_dense_main(tmp_path, monkeypatch, staging="--no-staging")
Expand Down Expand Up @@ -1948,25 +1931,6 @@ def _landed_dense_build(tmp_path, monkeypatch):
return hub, out


def _edit_an_output(out):
"""Change a CSV output after landing, keeping the manifest consistent."""
manifest_path = out / cli.MANIFEST_FILENAME
manifest = json.loads(manifest_path.read_text())
key, entry = next(
(key, entry)
for key, entry in manifest["outputs"].items()
if entry and str(entry["path"]).endswith(".csv")
)
path = out / Path(entry["path"]).name
path.write_bytes(path.read_bytes() + b"edited,after,landing\n")
manifest["outputs"][key] = {
**entry,
"sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
"bytes": path.stat().st_size,
}
manifest_path.write_text(json.dumps(manifest, indent=2, sort_keys=True))


@pytest.mark.parametrize(
("case", "expected_status"),
[
Expand Down
Loading
Loading