Skip to content

Report container start, call number, app and .py version on each v2 worker call - #1140

Draft
anth-volk wants to merge 12 commits into
mainfrom
v2-restore-telemetry
Draft

anth-volk wants to merge 12 commits into
mainfrom
v2-restore-telemetry

Conversation

@anth-volk

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

Copy link
Copy Markdown
Contributor

Fixes #1139

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 changelog fragments, changelog.d/1139.added.md (type added) and changelog.d/1139.changed.md (type changed).

Builds on #1142

#1142 made the v2 simulation workers Modal classes with memory snapshots (SimulationWorkerUS, SimulationWorkerUK, SimulationSegmentWorkerUS) and kept plain functions of the same names. This PR was stacked on it; #1142 has merged, so this PR now targets main, with main merged in.

Summary

Each call of the three v2 simulation workers now reports, as the run_simulation app's workers already do:

Field Where Value
container_start operation attribute: operation.completed record and the operation's span restored_from_snapshot, took_snapshot (Modal took the snapshot from this container) or no_snapshot (plain functions, and class containers Modal started without snapshots)
container_call_number operation attribute 1 for a container's first call, 2 for its second, and so on
modal_app_name operation attribute the v2 app's name, for example policyengine-simulation-v2-py6-20-0
service.version resource attribute on the call's records, spans and metrics the bundle's policyengine.py version, instead of the observability package's fallback 0.1.0

This covers the class methods and the plain functions, since both run the same _run_single_* body. The service names (policyengine-stage12-us-worker, -uk-worker, -us-segment-worker) are unchanged, so existing queries still select these records. The coordinator and household functions are unchanged.

Shared with the run_simulation app

The restore detection and call counter move from src/modal/app.py into src/modal/container_calls.py (ContainerStart, ContainerCalls), which both apps import. Each app module keeps one ContainerCalls, since each Modal container serves one worker. ContainerCalls.set_call_attributes(runtime) sets both attributes on the open operation and never raises. app.py's workers report exactly what they did before.

How a call tells the container starts apart (Modal 1.6.4)

  • Snapshot hook: the v2 workers' @modal.enter(snap=True) hook, load_country_models, now records the container's MODAL_TASK_ID and whether MODAL_ENABLE_SNAP_RESTORE is 1 before it loads the models. Modal takes a snapshot only when that flag is 1 (maybe_snapshot in modal/_runtime/user_code_imports.py).
  • Restore: a restored container starts with that record in memory, and Modal's memory_restore (modal/_runtime/task_lifecycle_manager.py) sets its MODAL_TASK_ID to its own task ID.
  • Each call: as its operation opens, it 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.
  • Failures: the hook's record and each call's attributes are wrapped so a failure raises nothing; the models still load, the call's result is unchanged, and the call still counts.

Environment variables

Variable Provider When it is read
MODAL_TASK_ID Modal's container runtime; no image env or secret sets it. On restore, Modal 1.6.4's memory_restore rewrites it from the restore state's task_id. In the snapshot hook, before the snapshot; and in each call, after any restore. service.instance.id already reads it per call.
MODAL_ENABLE_SNAP_RESTORE Modal's worker process, for snapshot and restore tasks; no image env or secret sets it. In the snapshot hook only, before the snapshot.

service.version reads no environment variable: it is RESOLVED_BUNDLE.bundle.policyengine_version, read when the module is imported, from the installed policyengine.py package's bundle manifest.

Unverified until this deploys: Modal documents neither variable. The run_simulation app's worker classes were checked on Modal testing on 2026-11-25 (every restored container reported its own MODAL_TASK_ID), but the v2 classes haven't been. After deploy, compare container_start with Modal's container logs: "Restoring Function from memory snapshot." and "Creating CPU memory snapshot for Function.", matching a log line's container ID with the task ID at the start of a record's service.instance.id.

Metrics: neither new attribute reaches metric attributes. service.version is a resource attribute, so it now changes with each .py release, as it already does for the run_simulation app. Whether the collector, outside this repository, turns resource attributes into metric labels is unverified.

Size

src/modal/v2_app.py is at 800 lines, the most sim.quality.python-file-length allows. Any further line added to it in this stack needs the file split first.

Testing

Tests were committed failing before each change.

  • New: tests/test_container_calls.py (12), tests/test_v2_container_start.py (36), tests/test_v2_worker_app_and_version.py (9).
  • Changed: tests/test_stage12_modal_app.py::test_stage12_functions_propagate_identifier_and_close_runtime (4), which asserted that the v2 workers set no attributes.
  • These 61 pass locally, by node ID. Ruff 0.15.22 (format --check, check) and Pyright (python -m scripts.rules pyright projects/policyengine-simulation-executor) pass.
  • The first review round also ran tests/test_modal_container_start.py (22, the run_simulation app's workers) and three snapshot warm-up and job progress tests by node ID; they pass.
  • Not run locally: the rest of the executor's tests, including Restore the v2 simulation workers from Modal memory snapshots with their country's models loaded #1142's tests/test_v2_worker_classes.py. They run in CI.

🤖 Generated with Claude Code

anth-volk and others added 11 commits November 27, 2026 23:33
The v2 workers need the restore detection and call counter the
run_simulation app's workers have. These tests cover that logic as a
module both apps can import: each container_start value, the call
counter, and a failure to set the attributes raising nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ContainerCalls holds the restore detection and call counter that
src/modal/app.py's workers use today: the snapshot hook records the
container's MODAL_TASK_ID and whether MODAL_ENABLE_SNAP_RESTORE is 1,
and each call compares the recorded ID with the current one. Its
set_call_attributes sets container_start and container_call_number on
the call's operation and never raises, so the v2 workers can call it
once per call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each call of SimulationWorkerUS, SimulationWorkerUK and
SimulationSegmentWorkerUS, and of the plain functions kept for callers on
older code, should carry container_start and container_call_number on
its operation record and span, as the run_simulation app's workers do.
The tests cover the three values, the call counter, failed and preempted
calls, and failures to read or set the attributes leaving the result
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The v2 workers' snapshot hook records whether Modal snapshots the
container and its MODAL_TASK_ID, and each call of SimulationWorkerUS,
SimulationWorkerUK and SimulationSegmentWorkerUS, and of the plain
functions kept for callers on older code, sets container_start and
container_call_number on its operation as it opens. The run_simulation
app's workers now use the same shared module, with no change in what
they report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The observability skill's section now covers the v2 app's simulation
workers and their plain functions, the services their records come
from, and the shared ContainerCalls module, and says the v2 workers'
attribute hasn't been checked on Modal yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each call of the three v2 simulation workers, through its class method or
its plain function, should configure its telemetry with the .py version
as service version and name the Modal app in its operation's
modal_app_name, as the run_simulation app's workers do, while keeping the
service names existing queries select by.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each call of the three v2 simulation workers configures its telemetry
with the bundle's policyengine.py version as service version, in place
of the observability package's fallback 0.1.0, and its operation's
modal_app_name names the v2 app, as the run_simulation app's workers do.
The service names stay the workers', which existing queries select by.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 2 found the test compared the version with the same
expression the code passes. It now compares it with the installed
policyengine distribution's version. The service.version change gets
its own "changed" changelog fragment, since it alters a value existing
records already carry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk force-pushed the v2-restore-telemetry branch from 2ac87ff to 5eb3cba Compare November 27, 2026 19:34
Base automatically changed from v2-workers-memory-snapshots to main November 27, 2026 19:53

This branch has not been deployed

No deployments
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 the v2 workers' telemetry whether each call ran in a restored container, its call number, and the app and .py version serving it

1 participant