Skip to content

Remove the plain worker functions and the gateway's fallback to them, and refuse rollbacks to apps without the worker class - #1131

Merged
anth-volk merged 20 commits into
mainfrom
claude/remove-plain-worker-functions-c67982
Nov 26, 2026
Merged

anth-volk merged 20 commits into
mainfrom
claude/remove-plain-worker-functions-c67982

Conversation

@anth-volk

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

Copy link
Copy Markdown
Contributor

Fixes #1130

What changes

#1118 made the simulation workers the Modal classes SimulationWorker and SimulationSegmentWorker, restored from memory snapshots. It also kept plain run_simulation and run_simulation_segment functions, with no snapshots and running the same code, for callers still on the code from before the classes. Those callers were the old gateway, while a deploy replaced the routed app in place, and runs started before the deploy. The gateway also fell back to the plain run_simulation function of apps deployed before #1118.

This PR removes all of that.

Versioned app (projects/policyengine-simulation-executor/src/modal/app.py):

  • the two plain functions, and their mentions in the _run_simulation and _run_simulation_segment docstrings;
  • the tests that exist only for them: the plain-function assertions in test_modal_bundle_image.py, and the test of the plain run_simulation's job records in test_job_progress.py, which a class test already repeats;
  • from Report in worker telemetry whether each call's container was restored from a memory snapshot #1124's container-start telemetry, the run_simulation and run_simulation_segment cases of the two plain-function tests in test_modal_container_start.py, and the three passages of docs/engineering/skills/observability.md that named the plain functions. The tests now check run_budget_window_batch, the app's only plain function.

The plain run_simulation_segment's test was the only test that a segment call returns its result and records a successful operation. That code still runs as SimulationSegmentWorker.run_simulation_segment, so test_a_segment_returns_its_result_and_records_success now calls the class method.

Gateway:

  • _spawn_annual_simulation in job_creation_routes.py spawns only through the SimulationWorker class. It no longer hydrates the class first or falls back to an app's plain run_simulation function.
  • FUNCTION_FORM_SIMULATION_WORKER is removed from the contract library's simulation_workers.py.
  • The gateway test fixture's function_form_apps is now apps_without_worker_class. A method of a missing class raises NotFoundError when spawned, where Modal resolves the lazy handle. The fixture's spawn_error option and its hydrate method, used only by the fallback and its tests, are removed.
  • The fallback test becomes test__given_an_app_with_no_worker_class__then_no_function_is_spawned.

Routing restores (projects/policyengine-simulation-executor/src/modal/routing_publisher/):

  • The check: require_latest_app_defines_worker_class (stores.py) looks up the SimulationWorker class in the app that a restored state's latest.policyengine routes to, in the same Modal environment.
    • It checks only when that app differs from the one in the state the write replaces: the live state for --restore-latest, and the plan's own published state for each plan undone.
    • When Modal raises NotFoundError (for a missing class or a missing app), it raises LatestAppWithoutWorkerClassError, a ValueError subclass, and routing is unchanged. The message says to roll back to a release from .py 6.20.0 on, after undoing any pending publication with resolve_pending_publication.
    • Any other Modal error is raised as it is.
  • Rollbacks are checked:
    • restore_routing_state (--restore-latest, the rollback's policyengine_version way) checks before writing.
    • restore_routing_plans (--restore-plan, the rollback's deploy_run_id way) checks every state it would write back. _restore_live_plans now works out every plan it will undo first, checks, and only then writes, so a refusal never leaves routing half restored. When nothing is refused, it writes the same states in the same order as before.
  • The pending publication's own plan isn't checked: undoing it always puts back what it replaced.
    • This covers resolve_pending_publication, the deploy workflow's restores of its own unfinished publication, and the pending plan among a deploy_run_id's plans.
    • A plan counts as the pending one when its publication ID and both state digests match the pending record.
    • Every other routing write refuses while a publication is pending, so refusing to undo it would leave no way to clear it except editing the Modal Dict by hand.
    • It can put latest back on an app from before 6.20.0, if latest was on one before that deployment.
  • Docs: the rollback workflow's header comment, the executor README and the changelog say so.

Unchanged: the stage names run_simulation and run_simulation_segment and the modal_function_name attribute, which the class methods use; and what #1124 and #1126 added.

What the gateway change gives up: running comparison requests on apps deployed before #1118, which is every .py release before 6.20.0.

  • A comparison request routed to one of them now fails with Modal's NotFoundError, and the client gets a 500. That's how a request routed to an app that isn't deployed fails today.
  • That covers a request pinned to such a release, a rollback of latest to one, and an API v1 deploy of a commit from before 16 Nov, which pins an older .py version.
  • Budget-window requests still run on those apps: the gateway spawns the old app's run_budget_window_batch function, which spawns its own yearly simulations.
  • A policyengine_version or deploy_run_id rollback that would move latest to one of them is now refused before routing changes (see routing restores above). The rollback's own checks (/health, /ready, /versions) wouldn't have caught it.

Why this is safe now

Checked read-only on 2026-11-25 from about 21:30 UTC, and again on 2026-11-26 from about 14:10 UTC:

Modal environment Routed app (latest = .py 6.20.0) Gateway
main policyengine-simulation-py6-20-0 v9, deployed 13:48 UTC on 26 Nov from 44dd610; v8 15:10 UTC on 25 Nov from 544ff62 v137, deployed 15:21 UTC on 25 Nov from 544ff62
staging policyengine-simulation-py6-20-0 v9, deployed 13:02 UTC on 26 Nov from 44dd610; v8 14:24 UTC on 25 Nov from 544ff62 v203, deployed 14:37 UTC on 25 Nov from 544ff62
  • 544ff62 and 44dd610 contain Restore the simulation workers from Modal memory snapshots with the country models loaded #1118's merge. The deploy workflow deploys only to these two environments.
  • Runs in progress: the workers and the budget-window batch time out after 3600 s. On 25 Nov at 21:26 UTC, the only containers of these apps had started at 21:14–21:15 UTC.
  • API v1 has no Modal dependency. Its economy services reach simulations over HTTP, through the gateway or the Cloud Run entrypoint.
    • They pin API v1's installed .py version, 6.20.0 since 16 Nov, so every API v1 request goes to py6-20-0, which has the classes.
    • Each paired .py update, such as chore(deps): update policyengine to 6.20.3 #1116 here and API v1's #4341 for 6.20.3, deploys a new app from the code at the time, which has the classes, and API v1 then pins that version.
  • Older apps get no calls: none of the older apps in main (py6-17-1, py6-16-0, py6-12-1, py6-12-0, py6-11-1, py6-10-0 and py6-7-5) has written a log line in the 7 days before 14:11 UTC on 26 Nov (modal app logs <app> --env main --since 7d; the same query on py6-20-0 returned 5,783 lines). py6-17-1's last log lines are from 16:01 UTC on 16 Nov, about 20 minutes before API v1's pin moved to 6.20.0.
  • Callers that look the workers up by name:
    • The gateway, the segmented national fan-out and the budget-window scheduler use Cls.from_name.
    • The detailed-save API and Stage 12 code only look up v2_app.py functions.
    • The smoke apps, precompute apps, workflows and .github/scripts look up neither name.
    • GitHub code search finds run_simulation_segment only in this repository.
  • Deploy checks: none calls an older release. The release check compares the deployed API with results computed from the same bundle, and the entrypoint smoke test compares Cloud Run revisions.
  • Rollback: simulation-rollback.yml rewrites the routing state and Cloud Run traffic, and never deploys a gateway. A rollback of latest to 6.20.0 still works.
  • Deploy order: the executor deploys before the gateway. During that window, the gateway that is live already spawns through the class, which the new app has.

Environment variables

This change reads and declares none. The deleted functions attached the gcp_secret and hf_secret Modal secrets, which stay attached to both worker classes and run_budget_window_batch.

The routing restores' new class lookup uses the Modal client's credentials, MODAL_TOKEN_ID and MODAL_TOKEN_SECRET (GitHub Actions secrets), and the --environment argument from MODAL_ENVIRONMENT. Every step that reaches it already passes all three, because it already reads the routing Modal Dict:

  • simulation-rollback.yml: "Restore Modal routing" and "Resolve the unfinished routing publication";
  • simulation-deploy.reusable.yml: "Put latest back after a failed step" and "Finalize the routing publication".

Checks run

  • Red-green:
    • tests/test_modal_bundle_image.py::test_the_simulation_workers_are_not_also_plain_functions failed on 8fcb443c and passes after the removal.
    • The four plain-function cases of Report in worker telemetry whether each call's container was restored from a memory snapshot #1124's tests failed with AttributeError after the merge of main. The two tests pass on run_budget_window_batch.
    • The gateway test test__given_an_app_with_no_worker_class__then_no_function_is_spawned failed on 13101fef: the request returned 200 after spawning the old app's function. It passes after fed9421b.
    • Four routing tests failed on 3a76c06d, either with no class lookup made or with no refusal raised, and pass after 91cd1d81:
      • tests/version_registry/test_routing_rollback.py::test_restoring_latest_checks_that_its_app_defines_the_worker_class
      • ::test_restoring_latest_refuses_an_app_without_the_worker_class
      • ::test_undoing_plans_checks_the_app_each_restored_state_routes_latest_to
      • ::test_undoing_plans_writes_nothing_if_any_restored_state_has_no_worker_class
    • Four more failed on 5eda3860, because the first version refused them, and pass after 525c04f8, which narrows the rule:
      • test_routing_rollback.py::test_restoring_latest_to_the_release_it_is_on_needs_no_worker_class
      • ::test_undoing_a_plan_that_leaves_latest_where_it_is_needs_no_worker_class
      • ::test_undoing_an_unfinished_publication_puts_back_what_it_replaced
      • test_routing_finalization.py::test_operator_recovery_puts_back_what_the_publication_replaced_without_the_class, which replaces the first version's test of the opposite
    • test_routing_rollback.py::test_finished_plans_among_kept_plans_are_still_checked failed on 9c7218c6, because the check was skipped for every plan, and passes after 39397277. It was renamed from test_only_the_unfinished_publication_among_kept_plans_skips_the_check in 8203cc3f, which also made it require the refusal to name resolve_pending_publication.
    • test_routing_rollback.py::test_the_pending_plan_among_kept_plans_is_undone_without_the_check failed on 8203cc3f: the routes plan's undo was refused. It passes after 60f0c1ec.
    • test_routing_rollback.py::test_a_failed_worker_class_lookup_stops_the_rollback_and_writes_nothing and ::test_a_failed_worker_class_lookup_stops_undoing_plans_and_writes_nothing pass. They check that a Modal error other than NotFoundError propagates.
    • ::test_undoing_a_plan_checks_the_app_its_starting_state_routes_latest_to was renamed from test_undoing_plans_checks_the_app_each_restored_state_routes_latest_to.
    • The property test test_routing_rollback.py::test_publish_then_restore_returns_exactly_the_previous_state, whose fake Modal gained the class lookup, passes.
  • Tests that pass on the pushed head:
    • executor tests/test_modal_bundle_image.py::test_the_simulation_workers_are_not_also_plain_functions, ::test_simulation_workers_are_classes_restored_from_memory_snapshots and ::test_simulation_workers_use_the_contract_names
    • executor tests/test_job_progress.py::test_a_segment_returns_its_result_and_records_success, which fails if SimulationSegmentWorker.run_simulation_segment drops its result (checked by deleting that return locally, without committing)
    • executor tests/test_modal_container_start.py::test_a_plain_function_call_reports_a_container_started_without_a_snapshot and ::test_a_plain_function_sets_both_attributes_before_its_job_runs
    • executor tests/test_canonical_spm.py::test_gateway_submission_uses_registry_capability_before_spawn
    • gateway tests/test_simulation_submission.py::TestSubmitSimulationEndpoint::test__given_an_app_with_no_worker_class__then_no_function_is_spawned
  • Adversarial review by separate reviewers:
    • The removal of the plain functions had three rounds. Round 1 found the missing segment test above. Rounds 2 and 3 found no code bug; round 3 covered the merge of main with Report in worker telemetry whether each call's container was restored from a memory snapshot #1124 and Request 1 CPU and 10 GiB for the segment workers #1126.
    • The rollback check's first version had one round. It found that refusing to undo an unfinished publication left the publication pending, which blocks every later routing write, and that restores which don't move latest were refused. Both are fixed by the narrower rule above. The narrowed rule's round found that a pending plan among a deploy_run_id's plans exempted every plan from the check; now only the pending plan's own undo is exempt. The last round found that each plan's undo was compared with the live state from before the restore, rather than with the plan's own published state. That refused the usual layout: a completed routes plan, then a pending latest plan. Each undo is now compared with the state it writes over. No further round ran; three is the limit.
    • The removal of the fallback had one round. It found no code bug, and confirmed in Modal 1.5.1's client source that a missing class raises NotFoundError when spawn resolves the handle, before any job metadata is written. Its wording findings are fixed: the changelog and the contract docstring now limit the failure to comparison requests, and the rollback documentation describes it.
  • Tools: Ruff (0.15.22, check and format) on the changed files, Pyright for the gateway, the executor and the contract library, python -m scripts.rules guard and .github/scripts/check-changelog-fragment.sh all pass.
  • Not verified: whether job-status polls still read calls that were made to the plain run_simulation of py6-20-0 before 15:21 UTC on 25 Nov, once the functions are removed. Those calls ended within an hour of being made.

🤖 Generated with Claude Code

anth-volk and others added 4 commits November 26, 2026 01:32
The versioned app still defines plain run_simulation and
run_simulation_segment functions beside the worker classes, so this
test fails until they are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1118 kept them beside the SimulationWorker and SimulationSegmentWorker
classes for callers on the code from before the classes: the old gateway
while a deploy replaced the routed app in place, and runs started before
it. In Modal main and staging, the routed app and the gateway now both
run code from 544ff62, which contains #1118, and no container from
before those deploys is left.

The gateway keeps its fallback to the run_simulation function of apps
deployed before the classes. The tests that called the plain functions go
with them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Removing the plain run_simulation_segment function took with it the
only test that a segment call returns its result and records a
successful operation. That code still runs as
SimulationSegmentWorker.run_simulation_segment, so the test now calls
the class method. It fails when the method drops its result.

Also check only that the two worker names are not plain functions, so
an unrelated new function doesn't fail the test, and say in the
changelog that only apps deployed from this release lose the functions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Apps deployed after the worker classes but before this change define
the plain run_simulation function too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk and others added 7 commits November 26, 2026 18:51
…orker-functions-c67982

# Conflicts:
#	projects/policyengine-simulation-executor/src/modal/app.py
#	projects/policyengine-simulation-executor/tests/test_modal_bundle_image.py
#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>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r class

The gateway falls back to the plain run_simulation function of an app
that has no SimulationWorker class, so this test fails until the
fallback is removed. The test fixture now raises the class lookup error
when a method of a missing class is spawned, where Modal resolves the
lazy handle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The gateway no longer falls back to the plain run_simulation function
of apps deployed before the worker classes. API v1 pins .py 6.20.0,
whose app has the class, and no older app has logged a call since API
v1 moved to it on 16 Nov. A request routed to an older app, pinned or
after a rollback of latest, now fails with Modal's NotFoundError, as one
routed to an app that isn't deployed does.

FUNCTION_FORM_SIMULATION_WORKER, the test fixture's spawn_error option,
and the hydrate methods of the test class handles go with the fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The gateway still spawns a budget-window batch on an app from before
the worker classes, and that app spawns its yearly simulations itself.
The rollback workflow and the executor README now say that a rollback
of latest to a release before 6.20.0 fails every unpinned comparison
request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk anth-volk changed the title Remove the plain run_simulation and run_simulation_segment functions Remove the plain run_simulation and run_simulation_segment functions and the gateway's fallback to them Nov 26, 2026
anth-volk and others added 9 commits November 26, 2026 22:03
…er class

The gateway now runs comparison requests only through an app's
SimulationWorker class, which apps deployed before .py 6.20.0 don't
define. Restoring latest to such an app, undoing a deployment's plans
back to one, or recovering a pending publication onto one would fail
every unpinned comparison request. These tests fail until the routing
publisher checks the restored app first. The routing tests' fake Modal
gains a Cls lookup in which every app defines the class unless a test
says otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The gateway runs comparison requests only through an app's
SimulationWorker class, which apps deployed before .py 6.20.0 don't
define. Restoring latest with --restore-latest, and undoing plans with
--restore-plan or --finalize-publication, now look the class up in the
app each restored state routes latest to, and refuse before writing
anything when Modal finds none. That covers all three ways of the
rollback workflow and the deploy workflow's own restores.

The plan restore now works out every state it would write before
writing the first, so a refusal never leaves routing half restored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he class

Undoing an unfinished publication (the deploy workflow's restore after a
failed check, or resolve_pending_publication) must put back what it
replaced: refusing leaves the publication pending, and every later
routing write refuses while one is. A restore that leaves latest on the
app it is on changes no request's routing, so it needs no class either.
These tests fail until the check is narrowed. A test also checks that a
Modal error other than NotFoundError stops the rollback unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Undoing an unfinished publication now always puts back what it replaced.
Refusing it left the publication pending, and every later routing write,
rollbacks included, refuses while one is. A restore is checked only when
it moves latest.policyengine to a different app: one that leaves latest
where it is changes where no request goes. The refusal message now says
to roll back to a release from .py 6.20.0 on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A deployment-run rollback can pass a run's finished plans together with
its rerun's pending one. Only the pending plan's own undo may skip the
worker class check, so this test fails until the exemption is limited
to it. A test also checks that a failed class lookup stops a plan
restore unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A deployment-run rollback can pass a run's finished plans together with
its rerun's pending one, and the check was skipped for all of them. Now
only the pending plan's own undo skips it, and the finished plans are
checked. Refusing them leaves nothing stuck: resolve_pending_publication
still undoes the pending plan alone. The README, the rollback workflow's
header and the changelog now say which restores are checked, and that
undoing a pending plan can put latest back on an app from before 6.20.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In the usual deployment-run layout, a completed routes plan is followed
by a pending latest plan. Undoing the routes plan leaves latest where
undoing the latest plan put it, so the rollback must not look the class
up; the check compared it with the live state instead, and refused. The
test of finished plans among a pending one now also expects the refusal
to name resolve_pending_publication. Both fail until the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A plan's undo writes its starting state over its published state, so
that is what decides whether it moves latest. Comparing with the live
state from before the restore refused the usual deployment-run
rollback: a finished routes plan, then a pending latest plan whose undo
is exempt, where the routes plan's undo leaves latest where it is. The
refusal now also says to undo a pending publication first with
resolve_pending_publication.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk anth-volk changed the title Remove the plain run_simulation and run_simulation_segment functions and the gateway's fallback to them Remove the plain worker functions and the gateway's fallback to them, and refuse rollbacks to apps without the worker class Nov 26, 2026
@anth-volk
anth-volk marked this pull request as ready for review November 26, 2026 19:51
@anth-volk
anth-volk merged commit 0eca410 into main Nov 26, 2026
30 checks passed
@anth-volk
anth-volk deleted the claude/remove-plain-worker-functions-c67982 branch November 26, 2026 19:51
anth-volk added a commit that referenced this pull request Nov 26, 2026
…anch

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.

Remove the plain worker functions and the gateway's fallback to them, and refuse rollbacks to apps without the worker class

1 participant