Skip to content

Report in worker telemetry whether each call's container was restored from a memory snapshot - #1124

Merged
anth-volk merged 10 commits into
mainfrom
claude/restore-telemetry-9cc3a6
Nov 26, 2026
Merged

anth-volk merged 10 commits into
mainfrom
claude/restore-telemetry-9cc3a6

Conversation

@anth-volk

@anth-volk anth-volk commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1123

Same-Repository Draft PR Check

  • This PR is a draft.
  • This PR is from a branch in PolicyEngine/policyengine-sim-api, not a personal fork.

Changelog

  • This PR adds a changelog fragment, changelog.d/container-start-telemetry.added.md, whose type is added.

Summary

Since #1118, the simulation workers run in containers restored from Modal memory snapshots. Until now, their telemetry couldn't show which calls ran in such a container. This PR adds two attributes to the operation of each run_simulation, run_simulation_segment and run_budget_window_batch call. The attributes reach the call's operation.completed log record and its span.

Attribute Values
container_start restored_from_snapshot, took_snapshot (Modal took the snapshot from this container) or no_snapshot (plain functions, and worker class containers Modal started without snapshots)
container_call_number 1 for a container's first call, 2 for its second, and so on

How a call tells them apart (Modal 1.6.4)

  • Snapshot hook: Modal runs it in every container that isn't restored. Before the models load, the hook now records two things:
    • the container's MODAL_TASK_ID;
    • whether MODAL_ENABLE_SNAP_RESTORE is 1. Modal takes a snapshot only when it is (maybe_snapshot in modal/_runtime/user_code_imports.py).
  • Restore: a restored container starts with that record in memory. Modal's memory_restore sets its MODAL_TASK_ID to the restored container's own ID.
  • Each call: _set_modal_call_attributes compares the recorded ID with the current one:
    • they differ: restored_from_snapshot;
    • they match: took_snapshot;
    • the flag wasn't 1, or the hook never ran: no_snapshot.
  • The recorded ID is never reported: it names the snapshotting container, not the one serving the call.

What the change keeps

  • Observability stays per call: nothing about it is configured in the hook.
  • Failures can't change a call: setting each attribute is suppressed separately, and so is the hook's record. A failure therefore leaves the call's result, its function_call_id and the model preload unchanged.
  • Every call counts: a call takes its number before its container start is read.
  • No call before the snapshot: so every container's first call is number 1.
  • No metrics: neither attribute reaches them. The package's operation metrics carry only the service identity and the operation's name, kind and outcome.

Setup times

No new span. The restore and the snapshot hook both run outside any call, so no span measures them. With these attributes, the existing spans of a restored container's first call show the setup left after the restore, such as the time before and inside country_module_load. docs/engineering/skills/observability.md has a new section on both attributes and on how to read setup times with them.

Environment variables

Variable Provider When it is read
MODAL_TASK_ID Modal's container runtime; no secret or image env. On restore, Modal 1.6.4's memory_restore rewrites it from the restore state. In the snapshot hook, before the snapshot; and in each call, after any restore
MODAL_ENABLE_SNAP_RESTORE Modal's container runtime. Modal's comment in _container_entrypoint.py: set to 1 for snapshot and restore tasks, unset otherwise. In the snapshot hook only, where Modal reads it to decide whether to snapshot

Production records already carry service.instance.id built from MODAL_TASK_ID, so it is set at call time.

Checked on Modal: on 2026-11-25, a measurement on Modal testing with Modal 1.6.4 stored MODAL_TASK_ID in the snapshot hook, as this PR does. Every restored container then reported a different MODAL_TASK_ID from the one its hook recorded, and the hook saw a set ID.

Not verified until this deploys: that took_snapshot and no_snapshot match Modal's own logs. In production, Modal logs "Restoring Function from memory snapshot." for a restored container and "Creating CPU memory snapshot for Function." for a snapshotting one; modal app logs <app> --search "memory snapshot" --show-container-id lists them. Neither variable's behaviour is documented by Modal, so check again after a Modal upgrade. Modal's TaskGetInfo can't be used for this: its snapshot_behavior was UNSPECIFIED for every production container on 2026-11-25, restored ones included.

Tests

The new file is tests/test_modal_container_start.py. Its tests were committed failing, before each change.

  • Each container_start value, including a worker class container started without snapshots.
  • The call counter.
  • Both attributes reaching the operation record and span, and staying out of metrics.
  • The attributes being set before the job runs, and carried by failed and preempted calls.
  • Attribute and hook-record failures leaving the result and the preload unchanged.

Run by node ID:

  • the 26 new tests;
  • tests/test_modal_bundle_image.py::test_both_simulation_workers_load_the_country_models_before_the_snapshot.

Also run:

  • Ruff at CI's version (0.15.22);
  • python -m scripts.rules pyright for the executor;
  • scripts/run_quality_guards.py.

The rest of the executor's unit tests weren't run locally; CI runs them.

Review

Three rounds of adversarial review by separate reviewers.

  • Round 1 found the bug fixed in cc849328: a worker class container that Modal starts without snapshots runs the hook too, and reported took_snapshot.
  • Rounds 2 and 3 found no correctness bugs. Their smaller findings, on the docs, test coverage and counting order, are fixed.

Not in this PR

  • Restore duration: not measured, since it happens before any worker code runs. Modal's container records give the times each container was queued and became ready.

Docstrings corrected

app.py and src/modal/country_model_preload.py said Modal runs the snapshot hook "once, in the container that takes the snapshot". Both now say it runs in every container that isn't restored.

🤖 Generated with Claude Code

anth-volk and others added 9 commits November 25, 2026 18:29
Each run_simulation and run_simulation_segment call's operation should carry
container_start (restored_from_snapshot, took_snapshot or no_snapshot) and
container_call_number. These tests fail until the workers set both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The workers' snapshot hook records the loading container's MODAL_TASK_ID
before Modal takes the snapshot. Each call compares it with the current
MODAL_TASK_ID, which Modal 1.6.4 sets to the restored container's own task
ID, and sets container_start (restored_from_snapshot, took_snapshot or
no_snapshot) and container_call_number on its operation. Each attribute set
is suppressed on its own, so a failure leaves the call's result and its
function_call_id unchanged.

The failure test now checks only the attributes it is about: with a Modal
call ID, the annual worker also sets its job stage attributes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Modal 1.6.4 runs the snapshot hook in every worker container that doesn't
restore, and takes a snapshot only when its worker sets
MODAL_ENABLE_SNAP_RESTORE to 1. A container started without snapshots should
report no_snapshot, not took_snapshot; that test fails until the hook records
the flag.

Also test that both attributes are set before the job runs, and that a failed
or preempted call's operation record still carries them. Those pass already:
they cover a gap in the tests, not a bug.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Modal 1.6.4 runs the snapshot hook in every worker container that isn't
restored, but takes a snapshot only when its worker sets
MODAL_ENABLE_SNAP_RESTORE to 1. The hook now records that flag with the task
ID, so a container started without snapshots reports no_snapshot instead of
took_snapshot.

The observability skill now says which spans hold the setup left in a call:
a took_snapshot container's hook also loads the models before its first call,
and only the operation's span carries the two attributes. The changelog no
longer says the attributes measure setup time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A call whose container start can't be read should still use up its call
number, and a failure to record the snapshot hook should still load the
models and leave the container reporting no_snapshot. Both tests fail until
the counter advances first and the hook's record is suppressed.

Also test that the plain functions, run_budget_window_batch included, set
both attributes before their job runs, and check the attributes the operation
hands to its metrics before the package filters them. Those pass already.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A call now takes its number before its container start is read, so it
counts even if that read fails, and a failure to record the snapshot hook no
longer stops the preload: the container then reports no_snapshot. The worker
base class's docstring now says that Modal runs the snapshot hook in every
container that isn't restored.

The observability skill no longer says Modal's container records hold the
restore's duration (they give queued and ready times, which include
scheduling), and it names the plain functions that load models in a call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tests' restore now also runs the snap=False hook, as Modal does after
each restore, so a change that reset the container record there would fail.
The observability skill says which calls the call number counts, names the
Modal call that holds container times, and notes that a segmented national
run's parent call opens no country_module_load span.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The preload module's docstring said Modal runs the snap=True hook once, in
the container that takes the snapshot. Modal 1.6.4 runs it in every container
that isn't restored, and takes a snapshot only from a container it starts with
snapshots enabled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1.5 · 3 found that Modal's TaskGetInfo returned snapshot_behavior
UNSPECIFIED for every production container on 2026-11-25, restored ones
included, while each restore and snapshot showed in the container's log
lines. The skill now points to those log lines, and records that restored
containers on Modal testing reported their own MODAL_TASK_ID.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review November 26, 2026 12:58
@anth-volk
anth-volk merged commit 8f48379 into main Nov 26, 2026
30 checks passed
@anth-volk
anth-volk deleted the claude/restore-telemetry-9cc3a6 branch November 26, 2026 12:58
anth-volk added a commit that referenced this pull request Nov 26, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk added a commit that referenced this pull request Nov 26, 2026
#1124 tested that the plain run_simulation and run_simulation_segment
functions report no_snapshot, and its observability skill section named
them. With both functions removed, those four test cases fail with
AttributeError. The two tests now check run_budget_window_batch, the
app's only plain function, and the skill names the worker class methods
instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Report in worker telemetry whether each call's container was restored from a memory snapshot

1 participant