diff --git a/.github/scripts/deploy-cloud-run-simulation-entry.sh b/.github/scripts/deploy-cloud-run-simulation-entry.sh index 320bfd236..cbe60559e 100755 --- a/.github/scripts/deploy-cloud-run-simulation-entry.sh +++ b/.github/scripts/deploy-cloud-run-simulation-entry.sh @@ -102,7 +102,6 @@ required_variables=( SIMULATION_SERVICE_DATABASE_URL_SECRET_NAME MODAL_TOKEN_ID_SECRET_NAME MODAL_TOKEN_SECRET_SECRET_NAME - V2_SIMULATION_OUTPUT_BUCKET_VALUE OBSERVABILITY_SERVICE_NAMESPACE OBSERVABILITY_TRACE_PROJECT_ID OTEL_EXPORTER_OTLP_ENDPOINT @@ -129,12 +128,6 @@ esac || fail "TAG is invalid" [[ "${ENTRYPOINT_RUNTIME_SERVICE_ACCOUNT}" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.gserviceaccount\.com$ ]] \ || fail "ENTRYPOINT_RUNTIME_SERVICE_ACCOUNT is invalid" -# The bucket that keeps v2 simulation output files for good. The service -# reads it into its settings and uses it for nothing (a report joins its -# sides' simulation runs by version set); it never reads the bucket. The -# setting and this check go with the run-reuse search. -[[ "${V2_SIMULATION_OUTPUT_BUCKET_VALUE}" =~ ^[a-z0-9][a-z0-9._-]{1,61}[a-z0-9]$ ]] \ - || fail "V2_SIMULATION_OUTPUT_BUCKET_VALUE is invalid" [[ "${ENTRYPOINT_MIN_INSTANCES}" =~ ^[0-9]+$ ]] \ || fail "ENTRYPOINT_MIN_INSTANCES must be a non-negative integer" [[ "${ENTRYPOINT_MAX_INSTANCES}" =~ ^[0-9]+$ ]] \ @@ -202,7 +195,6 @@ jq -n ' POLICYENGINE_OTEL_GOOGLE_AUDIENCE: env.POLICYENGINE_OTEL_GOOGLE_AUDIENCE, SIMULATION_V2_MODAL_ENVIRONMENT: env.MODAL_ENVIRONMENT, SIMULATION_V2_MANIFEST_NAME: "simulation-api-v2-version-manifest", - V2_SIMULATION_OUTPUT_BUCKET: env.V2_SIMULATION_OUTPUT_BUCKET_VALUE, STAGE12_ENABLED: ( if env.DEPLOY_STAGE12_V2 == "true" then env.STAGE12_ENABLED_VALUE else "0" end ) diff --git a/.github/workflows/simulation-deploy.reusable.yml b/.github/workflows/simulation-deploy.reusable.yml index 49f939272..eaa526412 100644 --- a/.github/workflows/simulation-deploy.reusable.yml +++ b/.github/workflows/simulation-deploy.reusable.yml @@ -247,7 +247,6 @@ jobs: STAGE12_DATABASE_URL_SECRET_NAME: ${{ vars.STAGE12_DATABASE_URL_SECRET_NAME }} MODAL_TOKEN_ID_SECRET_NAME: ${{ vars.MODAL_TOKEN_ID_SECRET_NAME }} MODAL_TOKEN_SECRET_SECRET_NAME: ${{ vars.MODAL_TOKEN_SECRET_SECRET_NAME }} - V2_SIMULATION_OUTPUT_BUCKET_VALUE: ${{ vars.V2_SIMULATION_OUTPUT_BUCKET }} run: .github/scripts/deploy-cloud-run-simulation-entry.sh - name: Resolve exact candidate metadata diff --git a/changelog.d/1081.removed.md b/changelog.d/1081.removed.md new file mode 100644 index 000000000..27063045c --- /dev/null +++ b/changelog.d/1081.removed.md @@ -0,0 +1 @@ +The Simulation Entrypoint's deployment no longer sets `V2_SIMULATION_OUTPUT_BUCKET` on its Cloud Run service or requires the `beta` and `prod` GitHub variable of that name for the entrypoint's deploy step; the service never used it. The Stage 12 deployment steps still pass it to the Modal coordinator and workers, which write simulation output files to that bucket. The simulation persistence library drops the create functions that use no definition keys, the search for an earlier succeeded simulation run to reuse, and the `reuse` parameters of the economy store's input reads, none of which the service has called since reports, simulations and runs are found or created by key. Requests and answers are unchanged. diff --git a/docs/migration/stage-12-modal-worker-foundation.md b/docs/migration/stage-12-modal-worker-foundation.md index 1b90bd3f0..4c08b260f 100644 --- a/docs/migration/stage-12-modal-worker-foundation.md +++ b/docs/migration/stage-12-modal-worker-foundation.md @@ -288,12 +288,15 @@ creates or claims a `stage12_evaluation_*` record. Only comparison-path calls, which pass an evaluation record, write those, until the comparison path is removed. The household functions work the same way on `household_jobs`. -**A report run.** The simulation service creates, in one transaction, the -`simulations` and `reports` rows, a `simulation_runs` row for the baseline and -one for the reform (pending, with no `modal_call_id`; or a run that already -succeeded, to reuse it), and the `report_runs` row naming both (pending, with no -`modal_call_id`). It then starts the manifest's `report_coordinator_callable` -(`coordinate_report`) with two arguments and no evaluation record: +**A report run.** The simulation service finds or creates by definition key, +in one transaction, the `simulations` and `reports` rows, a `simulation_runs` +row for the baseline and one for the reform (pending, with no `modal_call_id`; +or a run of the same simulation on the same versions that it joins, which may +have succeeded already), and the `report_runs` row naming both (pending, with +no `modal_call_id`, or a run on the same versions that it joins). For a report +run it created, it then starts the manifest's `report_coordinator_callable` +(`coordinate_report`) with two arguments and no evaluation record; a joined +report run gets no call: - the report input, a `ReportExecutionInput` whose `evaluation_id` is the report run's ID and whose baseline and reform `simulation_execution_id`s are their @@ -313,7 +316,7 @@ run row naming other simulation runs, fails the report run with `report_run_input_mismatch`. If a worker it starts finds its run held by a worker an earlier attempt started, which Modal never cancelled, the coordinator waits for that worker instead. It uses the stored output of each simulation run -that already succeeded (see the reuse of succeeded runs above), fails the +that already succeeded (see the joined runs above), fails the report run with the run's own recorded failure if a run it must calculate already failed (`simulation_run_failed` if it recorded none), and starts a worker for each other one with a context naming only that @@ -322,8 +325,10 @@ the report run with its result rows. It returns `{"report_run_id", "status"}`, or the same marked `deduplicated` without running when another call holds the report run or it has finished. -**A simulation run on its own.** The service creates the `simulations` row and -its `simulation_runs` row (pending, no call), starts the country's +**A simulation run on its own.** The service finds or creates by definition +key the `simulations` row and its `simulation_runs` row (pending, no call, or +a run on the same versions that it joins, which gets no call). For a run it +created, it starts the country's `single_simulation_callable` with a `SimulationExecutionInput` (role `standalone`, no output plan, `evaluation_id` and `simulation_execution_id` both the run's ID) and a context naming the run in `simulation_run_id`, with @@ -394,9 +399,11 @@ window of 1 to 75 consecutive years. It is an economy report whose the year, with its own simulation runs of that year, so a year can reuse a single-year report's runs, and the reverse. -- **Creating and starting.** The simulation service creates every year's runs - in one transaction, all pending with no call, then starts the earliest - years within one ten-second wait for Modal, counted from the first start; +- **Creating and starting.** The simulation service finds or creates the + window and its window run by key, in one transaction; a window run it + creates names every year's report run, each on its year's simulation runs, + found or created, and every run it creates is pending with no call. For a + window run it created, it then starts the earliest years within one ten-second wait for Modal, counted from the first start; a later year not confirmed within it is deferred (`YearStartDeferred`) and waits for a started year's coordinator. The contract's `year_calculation` derives each year's coordinator diff --git a/libs/policyengine-simulation-persistence/README.md b/libs/policyengine-simulation-persistence/README.md index 738ec2cde..46106b720 100644 --- a/libs/policyengine-simulation-persistence/README.md +++ b/libs/policyengine-simulation-persistence/README.md @@ -83,32 +83,16 @@ runs too long and the limit never stays on a pooled connection. ## Economy simulations and reports -`economy_run_records.py` holds the simulation service's reads and writes for -an economy report, or a standalone economy simulation. It reads the -country's model (`read_model_id`) and a catalog version's region by code -(`read_region`) and dataset by name or ID (`read_dataset`, -`read_dataset_by_id`). `insert_economy_report` creates, in the caller's -transaction, each side's new simulation and first run, the report naming -both simulations and the reform's policy with its options in `inputs`, and -its first report run, trigger `initial`; every run is `pending` with no -Modal call ID. A side may instead name a run that already succeeded -(`ReportSimulation.reused`), for which nothing is created. -`insert_economy_simulation` creates a standalone simulation and its first -run. Both create unkeyed rows, as `insert_budget_window_report` and -`insert_household_report` do; the Simulation Entrypoint calls none of them -now, creating every record by key ("Shared simulations, reports and runs" -below), and tests use them to store records created before keys. A row -naming one that does not exist is refused by its foreign key with -SQLSTATE `23503`; `EconomyRecordReferenceError` names the table and column. - -`find_reusable_run` returns the run a new report's side may reuse: the -newest, by completion, of the same economy simulation (policy, dynamic, -catalog version, dataset, region and year) that succeeded on the same -country package and .py versions, whose output file is under the output -bucket's `gs:///`, that a succeeded report run with the same options -used, and that no report run refused to reuse (an error message starting -`reused_simulation_`). So a submission after a refused reuse calculates the -simulation again. +`economy_run_records.py` holds the simulation service's catalog reads and +run writes for an economy report, or a standalone economy simulation. It +reads the country's model (`read_model_id`) and a catalog version's region +by code (`read_region`) and dataset by name or ID (`read_dataset`, +`read_dataset_by_id`), and builds a simulation run `pending` with no Modal +call ID (`simulation_run_row`, `insert_simulation_run`). The Simulation +Entrypoint creates every simulation, report and run by key ("Shared +simulations, reports and runs" below). A row naming one that does not exist +is refused by its foreign key with SQLSTATE `23503`; +`EconomyRecordReferenceError` names the table and column. `record_report_run_call` and `record_simulation_run_call` record a Modal call's ID, and `fail_unstarted_report_run` and @@ -119,9 +103,9 @@ with its newest run's state. `EconomyRunStore` runs them as the Simulation Entrypoint does, each step in a transaction of its own: `read_report_inputs` reads the catalog version, -model, region and dataset, each side's policy and dynamic values, and each -side's reusable run together, never giving both sides the same run; -`read_simulation_inputs` does the same for one simulation; the writes and +model, region and dataset, and each side's policy and dynamic values +together; `read_simulation_inputs` does the same for one simulation, and +`read_budget_window_inputs` for a budget window's two sides; the writes and reads each take their own. A missing region or dataset raises `UnknownRegionError` or `UnknownDatasetError`, and a report side's refused policy or dynamic raises `ReportSideError`, naming the side. A create whose @@ -157,7 +141,7 @@ report and run rows (C1 · 24). These modules do it: weren't forced, outside budget windows), then a read of the row that won, retried when that run fails in between (`SharedRowNotSettledError` after three tries). A forced run is inserted directly and never looked - up, joined or offered for reuse. + up or joined. - `keyed_economy_records.py` runs a report's find-or-create in one transaction: each side's simulation by key, in key order; the report by key; the report's joinable run on the current versions, joined with the @@ -190,23 +174,23 @@ report and run rows (C1 · 24). These modules do it: ## Household reports -`household_report_records.py` holds the simulation service's writes for a -household report: one stored household, for one year, under a baseline and, -optionally, a reform. `insert_household_report` creates, in the caller's -transaction, a household simulation (no dataset or region) and its first -run for each side, never reusing one; the report, of type `household`, -naming the household, both simulations and the reform's policy, with its -options in `inputs`; and its first report run, trigger `initial`, naming no -reform run for a report of the baseline alone. Every run is `pending` with -no Modal call ID, and a row naming one that does not exist is refused as -`EconomyRecordReferenceError`. +A household report calculates one stored household, for one year, under a +baseline and, optionally, a reform. Its report has type `household` +(`HOUSEHOLD_REPORT_TYPE`) and names the household, and each side is a +household simulation (no dataset or region) naming the side's policy. +`keyed_household_records.py` finds or creates the report, its simulations +and its run by key, as an economy report's are (C1 · 24's answer 8); +`household_report_records.py` builds each side's run, `pending` with no +Modal call ID (`household_simulation_run_row`, +`insert_household_simulation_run`). A row naming one that does not exist +is refused as `EconomyRecordReferenceError`. `HouseholdReportStore` runs the steps in transactions of their own: `read_inputs` reads the catalog version for the .py version, the country's model, the stored household and each side's policy values together, raising `UnknownCatalogVersionError`, `UnknownHouseholdError` or `ReportSideError` for the side whose policy is missing or sets parameters -the catalog version lacks; `create_report` raises +the catalog version lacks; `find_or_create_report` raises `EconomyRecordOutcomeUnknownError` when the commit fails after the rows were inserted; and `record_report_call` and `fail_unstarted_report` use the economy report run writes. @@ -223,11 +207,6 @@ version, dataset and region it calculates on. record calculates: its kind (`economy`, `budget_window`, `household` or `unsupported`), country, year, its region's code and dataset's name, and each simulation's policy and dynamic. -- `find_reusable_run_of` searches one simulation's runs, by its ID, under - `find_reusable_run`'s conditions on a run (versions, the run's own catalog - version, dataset, region and year, the output file, a succeeded report run - with the same options, no refused reuse, not forced); it doesn't compare the - simulation row, which keeps the catalog rows it was created on. - `insert_report_rerun` joins the report's run on the re-run's versions that hasn't failed and wasn't forced (pending, running or succeeded), creating nothing, or else creates the report's new run, naming no @@ -239,8 +218,8 @@ version, dataset and region it calculates on. a transaction-level advisory lock on the report's ID, so concurrent re-runs of one report wait for each other and end on one run. No re-run is refused while a run is under way. A forced one (`NewReportRerun.forced`) takes no lock, always - creates a new run of each side, marked `forced`, and joins none. No side - of a report's re-run names a reused run. `insert_simulation_rerun` joins or creates the + creates a new run of each side, marked `forced`, and joins none. + `insert_simulation_rerun` joins or creates the simulation's run on the re-run's versions, catalog version and year through `shared_rows`, with no lock. Each returns the run, its status and whether it created it. When the database refuses the new run's key to the @@ -274,10 +253,8 @@ in a transaction of its own and returns the run; the store's `read_inputs` reads the re-run's household and policies as it does a new report's. `EconomyRunStore.read_report_rerun_inputs` reads a re-run's catalog rows by -the stored region code and dataset name, and each side's values, with, given -`reuse`, its reusable run of its own simulation, never giving both sides the -same run; the service passes none, as its re-runs join or force their runs. -`read_window_rerun_inputs` does the same for each year of a window. +the stored region code and dataset name, and each side's values. +`read_window_rerun_inputs` does the same for a window's two sides. `create_report_rerun`, `create_window_rerun` and `create_simulation_rerun` run the inserts in a transaction of their own. diff --git a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/__init__.py b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/__init__.py index 68f8e2076..8a688c3c8 100644 --- a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/__init__.py +++ b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/__init__.py @@ -7,11 +7,8 @@ read_missing_table_privileges, ) from policyengine_simulation_persistence.budget_window_records import ( - BudgetWindowYear, - NewBudgetWindowReport, WindowSide, WindowYearRuns, - insert_budget_window_report, ) from policyengine_simulation_persistence.definition_keys import ( KEY_VERSION, @@ -57,18 +54,10 @@ CatalogRegion, EconomyPopulation, EconomyRecordReferenceError, - NewEconomyReport, - NewEconomySimulation, - ReportSimulation, - ReusableRun, RunVersions, SimulationChoice, fail_unstarted_report_run, fail_unstarted_simulation_run, - find_reusable_run, - find_reusable_run_of, - insert_economy_report, - insert_economy_simulation, read_dataset, read_dataset_by_id, read_model_id, @@ -86,7 +75,6 @@ EconomySimulationInputs, ReportSide, ReportSideError, - RunReuse, SideRefusal, SimulationInputs, UnknownDatasetError, @@ -117,9 +105,6 @@ ) from policyengine_simulation_persistence.household_report_records import ( HOUSEHOLD_REPORT_TYPE, - HouseholdReportSimulation, - NewHouseholdReport, - insert_household_report, ) from policyengine_simulation_persistence.household_report_store import ( HouseholdReportInputs, @@ -197,7 +182,6 @@ "SERVICE_TABLE_PRIVILEGES", "SERVICE_UPDATABLE_COLUMNS", "BudgetWindowInputs", - "BudgetWindowYear", "CatalogDataset", "CatalogRegion", "EconomyCatalogRows", @@ -229,7 +213,6 @@ "HouseholdJobStore", "HouseholdReportDefinition", "HouseholdReportInputs", - "HouseholdReportSimulation", "HouseholdReportStore", "HouseholdRerunSide", "HouseholdSimulationDefinition", @@ -239,11 +222,7 @@ "KeyedHouseholdReport", "KeyedWindowYear", "MissingPolicyParametersError", - "NewBudgetWindowReport", - "NewEconomyReport", - "NewEconomySimulation", "NewHouseholdJob", - "NewHouseholdReport", "NewHouseholdReportRerun", "NewReportRerun", "NewSimulationRerun", @@ -258,11 +237,8 @@ "ReportRunTrigger", "ReportSide", "ReportSideError", - "ReportSimulation", "RerunRecordDeletedError", "RerunSide", - "ReusableRun", - "RunReuse", "RunVersions", "SharedRowNotSettledError", "SideDefinition", @@ -303,15 +279,9 @@ "find_or_insert_simulation", "find_or_insert_simulation_run", "find_or_insert_simulation_runs", - "find_reusable_run", - "find_reusable_run_of", "household_jobs", "input_metadata", - "insert_budget_window_report", - "insert_economy_report", - "insert_economy_simulation", "insert_household_job", - "insert_household_report", "insert_household_report_rerun", "insert_report_rerun", "insert_simulation_rerun", diff --git a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/budget_window_records.py b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/budget_window_records.py index 0b5be3fe0..9bcfd1cd5 100644 --- a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/budget_window_records.py +++ b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/budget_window_records.py @@ -1,53 +1,33 @@ -"""The rows of a budget-window report: one report run per year of its window. +"""What every budget-window record shares: its years and each year's runs. A budget-window report compares the same baseline and reform in each year of a window of 1 to 75 consecutive years. It is stored as an economy report whose ``budget_window_size`` is the window's length, with one -``budget_window_runs`` row for its calculation and, for each year, a report +``budget_window_runs`` row for each calculation and, for each year, a report run naming that row and the year. Each year has its own baseline and reform -simulations, of that year; a window created before keys could instead name, -on a year's side, a run that already succeeded (``ReportSimulation.reused``). -The report names its first year's simulations. - -``insert_budget_window_report`` creates such a window without keys, as -windows were created before C1 · 27; the simulation service now finds or -creates windows by key (``keyed_window_records``), and tests use this to -store a window created before keys. Every row is pending with no Modal -call; the service then starts the first years, and each year's coordinator -starts the next ones (see ``policyengine_stage12_persistence.BudgetWindowStore``). +simulations, of that year. The report names its first year's simulations. + +The simulation service finds or creates a window by key +(``keyed_window_records``) and re-runs a stored one +(``economy_rerun_records``). ``check_window_years`` refuses years that +aren't such a window's. ``WindowYearRuns`` is a year's report run and each +side's simulation run, as a found window's years and a re-run's years both +hold them. """ from __future__ import annotations -from dataclasses import dataclass, replace -from typing import TYPE_CHECKING, Any, Protocol - -from sqlalchemy import insert +from typing import TYPE_CHECKING, Protocol -from policyengine_simulation_persistence.economy_run_records import ( - ECONOMY_REPORT_TYPE, - EconomyPopulation, - ReportSimulation, - RunVersions, - insert_simulation_with_run, - references_checked, -) from policyengine_simulation_persistence.tables import ( LATEST_YEAR, MAX_BUDGET_WINDOW_YEARS, - ReportRunStatus, - ReportRunTrigger, - budget_window_runs, - report_runs, - reports, ) if TYPE_CHECKING: from collections.abc import Sequence from uuid import UUID - from sqlalchemy import Connection - class WindowSide(Protocol): """A window year's side: its simulation and the run the year names.""" @@ -62,17 +42,12 @@ def simulation_run_id(self) -> UUID: """The run of it the year names.""" ... - @property - def reused(self) -> bool: - """Whether the run already succeeded, rather than being created.""" - ... - class WindowYearRuns(Protocol): """A year of a window run: its year, its report run and each side. - A new window's ``BudgetWindowYear`` and a re-run's ``WindowRerunYear`` - are both one. + A found or created window's ``FoundWindowYear`` and a re-run's + ``WindowRerunYear`` are both one. """ @property @@ -96,51 +71,6 @@ def reform(self) -> WindowSide: ... -@dataclass(frozen=True) -class BudgetWindowYear: - """One year of a window: its report run and its two sides' runs.""" - - year: int - report_run_id: UUID - baseline: ReportSimulation - reform: ReportSimulation - - -@dataclass(frozen=True) -class NewBudgetWindowReport: - """A budget-window report to create, with its first calculation's runs. - - ``population`` is the first year's; each year's simulations use the same - catalog version, region and dataset in their own year. ``inputs`` holds - the report's options as the Public API reads them. ``years`` runs from - the first year, one year at a time, earliest first. - """ - - id: UUID - window_run_id: UUID - country: str - label: str - description: str | None - tax_benefit_model_id: UUID - population: EconomyPopulation - versions: RunVersions - inputs: dict[str, Any] - years: tuple[BudgetWindowYear, ...] - - def __post_init__(self) -> None: - """Refuse years that aren't 1 to 75 consecutive ones from the first.""" - check_window_years(self.population.year, [year.year for year in self.years]) - - @property - def first_year(self) -> BudgetWindowYear: - """The window's first year, whose simulations the report names.""" - return self.years[0] - - def population_in(self, year: int) -> EconomyPopulation: - """Return what the window's simulations of ``year`` calculate on.""" - return replace(self.population, year=year) - - def check_window_years(first_year: int, years: Sequence[int]) -> None: """Raise ValueError unless ``years`` are 1 to 75 in a row from ``first_year``. @@ -159,101 +89,3 @@ def check_window_years(first_year: int, years: Sequence[int]) -> None: f"ending by {LATEST_YEAR}" ) raise ValueError(msg) - - -def insert_budget_window_report( - connection: Connection, report: NewBudgetWindowReport -) -> None: - """Create the report, its budget-window run, and each year's runs, all pending. - - Each year's sides that reuse no run get a new simulation and run of that - year. Raise EconomyRecordReferenceError when the database refuses a row - it names. The refusal ends the caller's transaction, or its savepoint. - """ - with references_checked(): - for window_year in report.years: - population = report.population_in(window_year.year) - for side in (window_year.baseline, window_year.reform): - if not side.reused: - insert_simulation_with_run( - connection, - simulation_id=side.simulation_id, - run_id=side.simulation_run_id, - choice=side.choice, - population=population, - versions=report.versions, - ) - first_year = report.first_year - population = report.population - connection.execute( - insert(reports).values( - id=report.id, - label=report.label, - description=report.description, - country=report.country, - type=ECONOMY_REPORT_TYPE, - user_id=None, - tax_benefit_model_id=report.tax_benefit_model_id, - policy_id=first_year.reform.choice.policy_id, - baseline_simulation_id=first_year.baseline.simulation_id, - reform_simulation_id=first_year.reform.simulation_id, - household_id=None, - dataset_id=population.dataset_id, - region_id=population.region_id, - year=population.year, - budget_window_size=len(report.years), - inputs=report.inputs, - ) - ) - connection.execute( - insert(budget_window_runs).values( - id=report.window_run_id, - report_id=report.id, - tax_benefit_model_version_id=population.tax_benefit_model_version_id, - trigger=ReportRunTrigger.INITIAL, - recalculate=False, - status=ReportRunStatus.PENDING, - ) - ) - connection.execute( - insert(report_runs), - [ - _year_run_row( - window_year, - report_id=report.id, - window_run_id=report.window_run_id, - versions=report.versions, - ) - for window_year in report.years - ], - ) - - -def _year_run_row( - window_year: BudgetWindowYear, - *, - report_id: UUID, - window_run_id: UUID, - versions: RunVersions, -) -> dict[str, Any]: - """Return a year's report run in a window's first window run, pending, unforced. - - As windows were created before keys: trigger ``initial``. - - Keyed windows' year runs, which name no trigger, are - ``keyed_window_records.window_year_run_row``'s. - """ - return { - "id": window_year.report_run_id, - "report_id": report_id, - "baseline_simulation_run_id": window_year.baseline.simulation_run_id, - "reform_simulation_run_id": window_year.reform.simulation_run_id, - "budget_window_run_id": window_run_id, - "year": window_year.year, - "country_package_version": versions.country_package_version, - "policyengine_version": versions.policyengine_version, - "status": ReportRunStatus.PENDING, - "trigger": ReportRunTrigger.INITIAL, - "modal_call_id": None, - "forced": False, - } diff --git a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_inputs.py b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_inputs.py index 1f6c4402f..4d0ad395e 100644 --- a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_inputs.py +++ b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_inputs.py @@ -4,24 +4,20 @@ a year, for the .py version of the default worker, and each side's stored policy and dynamic. ``read_catalog_rows`` reads the catalog version's model, region and dataset; ``read_report_sides`` and ``read_window_sides`` read -each side's values on that catalog version, with the runs a side may reuse -when a search is given. ``EconomyRunStore`` runs these in its transactions. +each side's values on that catalog version. ``EconomyRunStore`` runs these +in its transactions. """ from __future__ import annotations -from dataclasses import dataclass, replace -from typing import TYPE_CHECKING, Any, Literal, get_args +from dataclasses import dataclass +from typing import TYPE_CHECKING, Literal, get_args from policyengine_simulation_persistence.economy_run_records import ( CatalogDataset, CatalogRegion, EconomyPopulation, - ReusableRun, - RunVersions, SimulationChoice, - find_reusable_run, - find_reusable_run_of, read_dataset, read_dataset_by_id, read_model_id, @@ -44,7 +40,6 @@ ) if TYPE_CHECKING: - from collections.abc import Callable, Iterable from uuid import UUID from sqlalchemy import Connection @@ -53,11 +48,6 @@ # Which of a report's simulations a value belongs to. type ReportSide = Literal["baseline", "reform"] -# Finds a report side's reusable run, given the side, its policy and dynamic, -# and the run the other side reuses, which it must not. -type ReusableRunSearch = Callable[ - [ReportSide, SimulationChoice, UUID | None], ReusableRun | None -] # The errors a simulation's own policy or dynamic can raise. type SideRefusal = ( UnknownPolicyError @@ -108,20 +98,6 @@ class EconomyRequest: year: int -@dataclass(frozen=True) -class RunReuse: - """How a report looks for simulation runs to reuse (``find_reusable_run``). - - ``versions`` are those the new runs would calculate on, ``inputs`` the - report's options, and ``output_uri_prefix`` the output bucket's - ``gs:///``. - """ - - versions: RunVersions - inputs: dict[str, Any] - output_uri_prefix: str - - @dataclass(frozen=True) class EconomyCatalogRows: """The catalog rows a request is calculated on.""" @@ -143,15 +119,10 @@ def population(self, year: int) -> EconomyPopulation: @dataclass(frozen=True) class SimulationInputs: - """One simulation's policy and dynamic, and the values they set together. - - ``reusable_run`` names a succeeded run the simulation may use instead of - a new one, when the request looked for one and one exists. - """ + """One simulation's policy and dynamic, and the values they set together.""" choice: SimulationChoice reform: PolicyReform - reusable_run: ReusableRun | None = None @dataclass(frozen=True) @@ -173,17 +144,15 @@ class EconomyReportInputs: @dataclass(frozen=True) class BudgetWindowInputs: - """What a budget-window report is read from, and each year's reusable runs. + """What a budget-window report is read from: the catalog rows and both sides. ``baseline`` and ``reform`` hold each side's values, the same in every - year. ``reusable_runs`` gives, by year, the baseline's and the reform's - reusable run, each None when there is none. + year. """ catalog: EconomyCatalogRows baseline: SimulationInputs reform: SimulationInputs - reusable_runs: dict[int, tuple[ReusableRun | None, ReusableRun | None]] def read_catalog_rows( @@ -289,112 +258,33 @@ def read_side_values( raise ReportSideError(side, error) from error -def read_report_sides( # noqa: PLR0913 # the request's rows, both sides and the search +def read_report_sides( connection: Connection, request: EconomyRequest, catalog: EconomyCatalogRows, *, baseline: SimulationChoice, reform: SimulationChoice, - search: ReusableRunSearch | None, ) -> EconomyReportInputs: - """Read both sides' values and, with ``search``, each side's reusable run. - - The reform never gets the run the baseline reuses. - """ - sides: list[SimulationInputs] = [] - excluded_run_id: UUID | None = None - requested: tuple[tuple[ReportSide, SimulationChoice], ...] = ( - ("baseline", baseline), - ("reform", reform), + """Read both sides' values, the baseline's first.""" + return EconomyReportInputs( + catalog=catalog, + baseline=read_side_values(connection, request, catalog, "baseline", baseline), + reform=read_side_values(connection, request, catalog, "reform", reform), ) - for side, choice in requested: - inputs = read_side_values(connection, request, catalog, side, choice) - reusable_run = None if search is None else search(side, choice, excluded_run_id) - if reusable_run is not None: - excluded_run_id = reusable_run.simulation_run_id - inputs = replace(inputs, reusable_run=reusable_run) - sides.append(inputs) - return EconomyReportInputs(catalog=catalog, baseline=sides[0], reform=sides[1]) -def read_window_sides( # noqa: PLR0913 # the request's rows, both sides and each year's search +def read_window_sides( connection: Connection, request: EconomyRequest, catalog: EconomyCatalogRows, *, baseline: SimulationChoice, reform: SimulationChoice, - searches: Iterable[tuple[int, ReusableRunSearch | None]], ) -> BudgetWindowInputs: - """Read both sides' values, and each year's reusable runs with its search. - - A year without a search reuses nothing; in each year the reform never - gets the run the baseline reuses. - """ - baseline_inputs = read_side_values( - connection, request, catalog, "baseline", baseline - ) - reform_inputs = read_side_values(connection, request, catalog, "reform", reform) - reusable_runs: dict[int, tuple[ReusableRun | None, ReusableRun | None]] = {} - for year, search in searches: - baseline_run = reform_run = None - if search is not None: - baseline_run = search("baseline", baseline, None) - reform_run = search( - "reform", - reform, - None if baseline_run is None else baseline_run.simulation_run_id, - ) - reusable_runs[year] = (baseline_run, reform_run) + """Read both sides' values, the baseline's first, for every year of a window.""" return BudgetWindowInputs( catalog=catalog, - baseline=baseline_inputs, - reform=reform_inputs, - reusable_runs=reusable_runs, + baseline=read_side_values(connection, request, catalog, "baseline", baseline), + reform=read_side_values(connection, request, catalog, "reform", reform), ) - - -def search_by_definition( - connection: Connection, population: EconomyPopulation, reuse: RunReuse -) -> ReusableRunSearch: - """Search any simulation with the side's definition (a new report's sides).""" - - def search( - _side: ReportSide, choice: SimulationChoice, excluded_run_id: UUID | None - ) -> ReusableRun | None: - return find_reusable_run( - connection, - choice=choice, - population=population, - versions=reuse.versions, - inputs=reuse.inputs, - output_uri_prefix=reuse.output_uri_prefix, - excluded_run_id=excluded_run_id, - ) - - return search - - -def search_own_runs( - connection: Connection, - population: EconomyPopulation, - reuse: RunReuse, - simulation_ids: dict[ReportSide, UUID], -) -> ReusableRunSearch: - """Search only each side's own simulation's runs (a stored report's re-run).""" - - def search( - side: ReportSide, _choice: SimulationChoice, excluded_run_id: UUID | None - ) -> ReusableRun | None: - return find_reusable_run_of( - connection, - simulation_id=simulation_ids[side], - population=population, - versions=reuse.versions, - inputs=reuse.inputs, - output_uri_prefix=reuse.output_uri_prefix, - excluded_run_id=excluded_run_id, - ) - - return search diff --git a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_rerun_records.py b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_rerun_records.py index c88e13238..b1bdee38f 100644 --- a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_rerun_records.py +++ b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_rerun_records.py @@ -115,16 +115,13 @@ class RerunSide: """The simulation run one side of a report's or a window year's re-run names. - The simulation is always the report's own. With ``reused`` (an unforced - budget window's year only), the run is one of its runs that succeeded - on the current versions, and nothing is created for the side; otherwise - ``simulation_run_id`` is the run created, pending, or the ID an economy - report's side proposes, used only if it inserts the run. + The simulation is always the report's own. ``simulation_run_id`` is the + run a forced re-run creates, pending, or the ID an unforced re-run's + side proposes, used only if it inserts the run. """ simulation_id: UUID simulation_run_id: UUID - reused: bool @dataclass(frozen=True) @@ -135,7 +132,7 @@ class NewReportRerun: that hasn't failed and wasn't forced, or inserts one; a side's ``simulation_run_id`` is the ID it proposes, used only if it inserts the run. ``forced`` makes it a forced re-run's extra run, which inserts a - new run of each side, marked forced. No side names a reused run. + new run of each side, marked forced. """ report_id: UUID @@ -146,12 +143,6 @@ class NewReportRerun: reform: RerunSide forced: bool = False - def __post_init__(self) -> None: - """Refuse a side naming a reused run: each side joins or creates its run.""" - if self.baseline.reused or self.reform.reused: - msg = "a report re-run's sides join or create their runs, reusing none" - raise ValueError(msg) - @property def new_simulation_run_ids(self) -> tuple[UUID, ...]: """Return the IDs of the simulation runs a forced re-run creates. @@ -182,7 +173,7 @@ class NewWindowRerun: extra window run on new runs of every year. Unforced, a keyed window's re-run joins its window run on the versions, or each year's sides join their simulations' runs; a side's ``simulation_run_id`` is the ID it - proposes. No side names a reused run. + proposes. """ report_id: UUID @@ -193,13 +184,8 @@ class NewWindowRerun: years: tuple[WindowRerunYear, ...] def __post_init__(self) -> None: - """Refuse years that aren't 1 to 75 from the first, or a reused side.""" + """Refuse years that aren't 1 to 75 in a row from the first.""" check_window_years(self.population.year, [year.year for year in self.years]) - if any( - side.reused for year in self.years for side in (year.baseline, year.reform) - ): - msg = "a window re-run's sides join or create their runs, reusing none" - raise ValueError(msg) def population_in(self, year: int) -> EconomyPopulation: """Return what the window's simulations of ``year`` calculate on.""" diff --git a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_run_records.py b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_run_records.py index daac997e2..01059a24e 100644 --- a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_run_records.py +++ b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_run_records.py @@ -1,22 +1,14 @@ -"""The simulation service's reads and writes for economy simulations and reports. +"""The simulation service's catalog reads and run writes for economy records. An economy report compares a baseline and a reform simulation of one -region's data for one year. The service reads the catalog rows the request -names from the Public API's tables, then creates in one transaction: - -- for each side, a new ``simulations`` row and its first ``simulation_runs`` - row, ``pending`` with no Modal call ID; or, when a run of the same - simulation already succeeded and may be reused (``find_reusable_run``), - nothing: the report names that run and its simulation instead; -- the ``reports`` row, naming both simulations and the reform's policy, with - the report's options in ``inputs``; -- its first ``report_runs`` row, ``pending`` with no Modal call ID and - trigger ``initial``, naming both simulation runs. - -A standalone economy simulation is one ``simulations`` row and its first -run, created the same way. - -The service then starts the Modal call that calculates the run it created, +region's data for one year. The service reads the catalog rows a request +names from the Public API's tables (``read_model_id``, ``read_region``, +``read_dataset``), then finds or creates, by definition key, each +simulation, the report and their runs (``keyed_economy_records``). A run +it creates is ``pending`` with no Modal call ID; this module builds and +inserts such runs (``simulation_run_row``, ``insert_simulation_run``). + +The service then starts the Modal call that calculates a run it created, and records the call's ID on that run, or fails it at once when Modal refuses the call. Both updates apply only while the run is still ``pending`` with no call ID: the coordinator or worker may already have @@ -28,7 +20,8 @@ Every function takes a connection, so the caller chooses the transactions. Each update sets only the columns the service's role may update, with ``updated_at``, whose automatic update the Public API's models make on their -own side only. +own side only. ``references_checked`` turns the database's refusal of a row +naming one that does not exist into ``EconomyRecordReferenceError``. """ from __future__ import annotations @@ -37,16 +30,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Any, Final -from sqlalchemy import ( - cast, - func, - insert, - literal, - or_, - select, - update, -) -from sqlalchemy.dialects.postgresql import JSONB +from sqlalchemy import func, insert, select, update from sqlalchemy.exc import IntegrityError from policyengine_simulation_persistence.input_tables import ( @@ -57,9 +41,7 @@ ) from policyengine_simulation_persistence.tables import ( ReportRunStatus, - ReportRunTrigger, SimulationStatus, - SimulationType, budget_window_runs, report_runs, reports, @@ -71,15 +53,12 @@ from collections.abc import Iterator, Sequence from uuid import UUID - from sqlalchemy import ColumnElement, Connection, FromClause, Table + from sqlalchemy import ColumnElement, Connection, Table # PostgreSQL's SQLSTATE for a row naming one that does not exist. FOREIGN_KEY_VIOLATION: Final = "23503" # A report's type, as the Public API reads it. ECONOMY_REPORT_TYPE: Final = "economy" -# The start of the error message of a report run whose coordinator refused a -# reused simulation run's output (``reused_simulation_: ``). -REFUSED_REUSE_PREFIX: Final = "reused_simulation_" # Both run tables' statuses use these values. _PENDING: Final = SimulationStatus.PENDING.value _FAILED: Final = SimulationStatus.FAILED.value @@ -145,68 +124,6 @@ class SimulationChoice: dynamic_id: UUID | None -@dataclass(frozen=True) -class ReusableRun: - """A succeeded simulation run a new report may name, and its simulation.""" - - simulation_id: UUID - simulation_run_id: UUID - - -@dataclass(frozen=True) -class ReportSimulation: - """The simulation and run one side of a report names. - - With ``reused``, both exist already and the run has succeeded, so nothing - is created for the side. Otherwise both are created, the run pending. - """ - - simulation_id: UUID - simulation_run_id: UUID - choice: SimulationChoice - reused: bool - - -@dataclass(frozen=True) -class NewEconomyReport: - """An economy report to create, with its first run. - - ``inputs`` holds the report's options as the Public API reads them. - """ - - id: UUID - run_id: UUID - country: str - label: str - description: str | None - tax_benefit_model_id: UUID - population: EconomyPopulation - versions: RunVersions - inputs: dict[str, Any] - baseline: ReportSimulation - reform: ReportSimulation - - @property - def new_simulation_run_ids(self) -> tuple[UUID, ...]: - """Return the IDs of the simulation runs created for the report.""" - return tuple( - side.simulation_run_id - for side in (self.baseline, self.reform) - if not side.reused - ) - - -@dataclass(frozen=True) -class NewEconomySimulation: - """A standalone economy simulation to create, with its first run.""" - - id: UUID - run_id: UUID - choice: SimulationChoice - population: EconomyPopulation - versions: RunVersions - - class EconomyRecordReferenceError(LookupError): """The database refused a row naming one that does not exist. @@ -276,283 +193,6 @@ def _dataset( return None if row is None else CatalogDataset(id=row.id, name=row.name) -def find_reusable_run( # noqa: PLR0913 # each keyword is one part of the match - connection: Connection, - *, - choice: SimulationChoice, - population: EconomyPopulation, - versions: RunVersions, - inputs: dict[str, Any], - output_uri_prefix: str, - excluded_run_id: UUID | None = None, -) -> ReusableRun | None: - """Return the succeeded run a new report's simulation may name, or None. - - The run is the newest, by completion, of a run: - - - of an economy simulation with the same policy, dynamic, catalog - version, dataset, region and year; - - that succeeded on the same country package and .py versions, for that - catalog version, year, dataset and region; - - whose output file is under ``output_uri_prefix``, the output bucket, - which never deletes a file; - - that a succeeded report run used, in a report with the same ``inputs`` - (options), so its file holds the outputs a report with those options - planned; - - that no report run has refused to reuse (an error message starting - ``reused_simulation_``), so a submission after a refused reuse - calculates the simulation again; - - that wasn't forced: a forced re-run's run is never named by another - report run (C1 · 24's answer 3); and - - that is not ``excluded_run_id``, the other side's run. - - The coordinator may still refuse it, when the new report plans outputs - that report did not, as for a reform that turns on labour supply - responses: the refusal fails that report run, and the run is not offered - again. - """ - return _newest_reusable_run( - connection, - ( - simulations.c.simulation_type == SimulationType.ECONOMY, - simulations.c.policy_id.is_not_distinct_from(choice.policy_id), - simulations.c.dynamic_id.is_not_distinct_from(choice.dynamic_id), - simulations.c.tax_benefit_model_version_id - == population.tax_benefit_model_version_id, - simulations.c.dataset_id == population.dataset_id, - simulations.c.region_id == population.region_id, - simulations.c.year == population.year, - simulations.c.filter_field.is_(None), - ), - _WantedRun( - population=population, - versions=versions, - inputs=inputs, - output_uri_prefix=output_uri_prefix, - excluded_run_id=excluded_run_id, - ), - ) - - -def find_reusable_run_of( # noqa: PLR0913 # the simulation, then each part of the match - connection: Connection, - *, - simulation_id: UUID, - population: EconomyPopulation, - versions: RunVersions, - inputs: dict[str, Any], - output_uri_prefix: str, - excluded_run_id: UUID | None = None, -) -> ReusableRun | None: - """Return the succeeded run of one simulation a re-run of its report may name. - - The run is the newest, by completion, of the simulation's runs that meets - every condition ``find_reusable_run`` sets on a run, or None. Its catalog - version, dataset and region are compared on the run itself: a re-run on - a later .py version calculates the simulation on that version's catalog - rows, while the simulation keeps the ones it was created on. - """ - return _newest_reusable_run( - connection, - (simulation_runs.c.simulation_id == simulation_id,), - _WantedRun( - population=population, - versions=versions, - inputs=inputs, - output_uri_prefix=output_uri_prefix, - excluded_run_id=excluded_run_id, - ), - ) - - -@dataclass(frozen=True) -class _WantedRun: - """What a reusable run must have calculated, and the run it must not be.""" - - population: EconomyPopulation - versions: RunVersions - inputs: dict[str, Any] - output_uri_prefix: str - excluded_run_id: UUID | None - - -def _newest_reusable_run( - connection: Connection, - simulation_conditions: tuple[ColumnElement[bool], ...], - wanted: _WantedRun, -) -> ReusableRun | None: - """Return the newest run meeting the conditions and every run condition.""" - used_by = report_runs.alias("used_by_report_run") - refused_by = report_runs.alias("refused_by_report_run") - used_with_same_options = ( - select(used_by.c.id) - .join(reports, reports.c.id == used_by.c.report_id) - .where( - used_by.c.status == ReportRunStatus.SUCCEEDED, - _names_run(used_by), - cast(reports.c.inputs, JSONB) == literal(wanted.inputs, JSONB), - ) - .exists() - ) - refused = ( - select(refused_by.c.id) - .where( - refused_by.c.status == ReportRunStatus.FAILED, - _names_run(refused_by), - refused_by.c.error_message.startswith( - REFUSED_REUSE_PREFIX, autoescape=True - ), - ) - .exists() - ) - population = wanted.population - versions = wanted.versions - conditions = [ - *simulation_conditions, - simulation_runs.c.status == SimulationStatus.SUCCEEDED, - simulation_runs.c.forced.is_(False), - simulation_runs.c.year == population.year, - simulation_runs.c.tax_benefit_model_version_id - == population.tax_benefit_model_version_id, - simulation_runs.c.country_package_version == versions.country_package_version, - simulation_runs.c.policyengine_version == versions.policyengine_version, - simulation_runs.c.dataset_id == population.dataset_id, - simulation_runs.c.region_id == population.region_id, - simulation_runs.c.output_uri.startswith( - wanted.output_uri_prefix, autoescape=True - ), - simulation_runs.c.output_sha256.is_not(None), - used_with_same_options, - ~refused, - ] - if wanted.excluded_run_id is not None: - conditions.append(simulation_runs.c.id != wanted.excluded_run_id) - row = connection.execute( - select(simulation_runs.c.id, simulation_runs.c.simulation_id) - .join(simulations, simulations.c.id == simulation_runs.c.simulation_id) - .where(*conditions) - .order_by(simulation_runs.c.completed_at.desc(), simulation_runs.c.id.desc()) - .limit(1) - ).one_or_none() - if row is None: - return None - return ReusableRun(simulation_id=row.simulation_id, simulation_run_id=row.id) - - -def _names_run(report_run: FromClause) -> ColumnElement[bool]: - """Return whether ``report_run`` names the outer query's simulation run.""" - return or_( - report_run.c.baseline_simulation_run_id == simulation_runs.c.id, - report_run.c.reform_simulation_run_id == simulation_runs.c.id, - ) - - -def insert_economy_report(connection: Connection, report: NewEconomyReport) -> None: - """Create the report, its first run and each side's new simulation and run. - - Raise EconomyRecordReferenceError when the database refuses a row it - names. The refusal ends the caller's transaction, or its savepoint. - """ - with references_checked(): - for side in (report.baseline, report.reform): - if not side.reused: - insert_simulation_with_run( - connection, - simulation_id=side.simulation_id, - run_id=side.simulation_run_id, - choice=side.choice, - population=report.population, - versions=report.versions, - ) - population = report.population - connection.execute( - insert(reports).values( - id=report.id, - label=report.label, - description=report.description, - country=report.country, - type=ECONOMY_REPORT_TYPE, - user_id=None, - tax_benefit_model_id=report.tax_benefit_model_id, - policy_id=report.reform.choice.policy_id, - baseline_simulation_id=report.baseline.simulation_id, - reform_simulation_id=report.reform.simulation_id, - household_id=None, - dataset_id=population.dataset_id, - region_id=population.region_id, - year=population.year, - budget_window_size=None, - inputs=report.inputs, - ) - ) - connection.execute( - insert(report_runs).values( - id=report.run_id, - report_id=report.id, - baseline_simulation_run_id=report.baseline.simulation_run_id, - reform_simulation_run_id=report.reform.simulation_run_id, - budget_window_run_id=None, - year=None, - country_package_version=report.versions.country_package_version, - policyengine_version=report.versions.policyengine_version, - status=ReportRunStatus.PENDING, - trigger=ReportRunTrigger.INITIAL, - modal_call_id=None, - ) - ) - - -def insert_economy_simulation( - connection: Connection, simulation: NewEconomySimulation -) -> None: - """Create the standalone simulation and its first run, pending. - - Raise EconomyRecordReferenceError when the database refuses a row it - names. The refusal ends the caller's transaction, or its savepoint. - """ - with references_checked(): - insert_simulation_with_run( - connection, - simulation_id=simulation.id, - run_id=simulation.run_id, - choice=simulation.choice, - population=simulation.population, - versions=simulation.versions, - ) - - -def insert_simulation_with_run( # noqa: PLR0913 # the simulation's and its run's parts - connection: Connection, - *, - simulation_id: UUID, - run_id: UUID, - choice: SimulationChoice, - population: EconomyPopulation, - versions: RunVersions, -) -> None: - """Create an economy simulation and its first run, pending with no call.""" - connection.execute( - insert(simulations).values( - id=simulation_id, - simulation_type=SimulationType.ECONOMY, - dataset_id=population.dataset_id, - household_id=None, - policy_id=choice.policy_id, - dynamic_id=choice.dynamic_id, - tax_benefit_model_version_id=population.tax_benefit_model_version_id, - region_id=population.region_id, - year=population.year, - ) - ) - insert_simulation_run( - connection, - run_id=run_id, - simulation_id=simulation_id, - population=population, - versions=versions, - ) - - def insert_simulation_run( # noqa: PLR0913 # the run, what it calculates on, and its marker connection: Connection, *, diff --git a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_run_store.py b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_run_store.py index 23b7e43ba..79cbb2782 100644 --- a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_run_store.py +++ b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/economy_run_store.py @@ -2,9 +2,12 @@ The Simulation Entrypoint creates an economy report, or a standalone economy simulation, in three steps, each its own transaction, with the Modal call -started between them: it reads the request's inputs, creates the rows, then -records the call or fails the run it started. ``EconomyRunStore`` runs the -reads and writes of ``economy_run_records`` and ``policy_reform`` that way. +started between them: it reads the request's inputs, finds or creates the +rows by key, then records the call on a run it created or fails that run. +A stored record's re-run takes the same steps, joining or creating its new +runs instead. ``EconomyRunStore`` runs the reads and writes of +``economy_inputs``, ``keyed_economy_records``, ``keyed_window_records``, +``economy_rerun_records`` and ``economy_run_records`` that way. """ from __future__ import annotations @@ -13,9 +16,6 @@ from sqlalchemy.exc import SQLAlchemyError -from policyengine_simulation_persistence.budget_window_records import ( - insert_budget_window_report, -) from policyengine_simulation_persistence.economy_inputs import ( BudgetWindowInputs, EconomyCatalogRows, @@ -24,7 +24,6 @@ EconomySimulationInputs, ReportSide, ReportSideError, - RunReuse, SideRefusal, SimulationInputs, UnknownDatasetError, @@ -33,8 +32,6 @@ read_report_sides, read_simulation_values, read_window_sides, - search_by_definition, - search_own_runs, ) from policyengine_simulation_persistence.economy_rerun_records import ( insert_report_rerun, @@ -51,8 +48,6 @@ SimulationChoice, fail_unstarted_report_run, fail_unstarted_simulation_run, - insert_economy_report, - insert_economy_simulation, record_report_run_call, record_simulation_run_call, ) @@ -72,9 +67,6 @@ from sqlalchemy import Connection, Engine - from policyengine_simulation_persistence.budget_window_records import ( - NewBudgetWindowReport, - ) from policyengine_simulation_persistence.economy_rerun_records import ( NewReportRerun, NewSimulationRerun, @@ -82,16 +74,11 @@ ReportDefinition, SideDefinition, SimulationDefinition, - WindowYearDefinition, ) from policyengine_simulation_persistence.economy_run_reads import ( EconomyReportState, EconomySimulationState, ) - from policyengine_simulation_persistence.economy_run_records import ( - NewEconomyReport, - NewEconomySimulation, - ) from policyengine_simulation_persistence.keyed_economy_records import ( FoundEconomyReport, FoundEconomySimulation, @@ -165,31 +152,17 @@ def read_report_inputs( *, baseline: SimulationChoice, reform: SimulationChoice, - reuse: RunReuse | None, ) -> EconomyReportInputs: - """Read a report's catalog rows, both sides' values and reusable runs. + """Read a report's catalog rows and both sides' values together. - With ``reuse``, each side gets the run ``find_reusable_run`` finds, - and the reform never gets the baseline's. Raise - UnknownCatalogVersionError, UnknownRegionError or UnknownDatasetError, - or ReportSideError naming the side whose policy or dynamic is refused. + Raise UnknownCatalogVersionError, UnknownRegionError or + UnknownDatasetError, or ReportSideError naming the side whose policy + or dynamic is refused. """ with self._transaction() as connection: catalog = read_catalog_rows(connection, request) - search = ( - None - if reuse is None - else search_by_definition( - connection, catalog.population(request.year), reuse - ) - ) return read_report_sides( - connection, - request, - catalog, - baseline=baseline, - reform=reform, - search=search, + connection, request, catalog, baseline=baseline, reform=reform ) def read_report_definition(self, report_id: UUID) -> ReportDefinition | None: @@ -210,38 +183,21 @@ def read_report_rerun_inputs( *, baseline: SideDefinition, reform: SideDefinition, - reuse: RunReuse | None, ) -> EconomyReportInputs: - """Read a report re-run's catalog rows, both sides' values and reusable runs. + """Read a report re-run's catalog rows and both sides' values. ``request`` names the stored report's region code and dataset name, - found in the catalog version of the current .py version. With - ``reuse``, each side gets the run ``find_reusable_run_of`` finds among - its own simulation's runs, and the reform never gets the baseline's. - Raise the errors ``read_report_inputs`` raises. + found in the catalog version of the current .py version. Raise the + errors ``read_report_inputs`` raises. """ with self._transaction() as connection: catalog = read_catalog_rows(connection, request) - search = ( - None - if reuse is None - else search_own_runs( - connection, - catalog.population(request.year), - reuse, - { - "baseline": baseline.simulation_id, - "reform": reform.simulation_id, - }, - ) - ) return read_report_sides( connection, request, catalog, baseline=baseline.choice, reform=reform.choice, - search=search, ) def read_window_rerun_inputs( @@ -250,47 +206,24 @@ def read_window_rerun_inputs( *, baseline: SimulationChoice, reform: SimulationChoice, - years: Sequence[WindowYearDefinition], - reuse: RunReuse | None, ) -> BudgetWindowInputs: - """Read a window re-run's catalog rows, both sides' values and each year's runs. + """Read a window re-run's catalog rows and both sides' values. - ``request.year`` is the window's first year. With ``reuse``, each - year's sides get the runs ``find_reusable_run_of`` finds among that - year's own simulations' runs, and the reform never gets the - baseline's. Raise the errors ``read_report_inputs`` raises. + ``request.year`` is the window's first year, and ``request`` names + the stored report's region code and dataset name, found in the + catalog version of the current .py version. Raise the errors + ``read_report_inputs`` raises. """ with self._transaction() as connection: catalog = read_catalog_rows(connection, request) return read_window_sides( - connection, - request, - catalog, - baseline=baseline, - reform=reform, - searches=( - ( - window_year.year, - None - if reuse is None - else search_own_runs( - connection, - catalog.population(window_year.year), - reuse, - { - "baseline": window_year.baseline_simulation_id, - "reform": window_year.reform_simulation_id, - }, - ), - ) - for window_year in years - ), + connection, request, catalog, baseline=baseline, reform=reform ) def create_window_rerun(self, rerun: NewWindowRerun) -> FoundBudgetWindow: """Join or create the window's run and its years; see ``insert_window_rerun``. - Raise EconomyRecordOutcomeUnknownError as ``create_report`` does. + Raise EconomyRecordOutcomeUnknownError as ``find_or_create_report`` does. """ return self._create(lambda connection: insert_window_rerun(connection, rerun)) @@ -298,14 +231,14 @@ def create_report_rerun(self, rerun: NewReportRerun) -> FoundReportRun: """Join or create the report's run; see ``insert_report_rerun``. Raise ``insert_report_rerun``'s errors, and - EconomyRecordOutcomeUnknownError as ``create_report`` does. + EconomyRecordOutcomeUnknownError as ``find_or_create_report`` does. """ return self._create(lambda connection: insert_report_rerun(connection, rerun)) def create_simulation_rerun(self, rerun: NewSimulationRerun) -> FoundSimulationRun: """Join or create the simulation's run; see ``insert_simulation_rerun``. - Raise EconomyRecordOutcomeUnknownError as ``create_report`` does. + Raise EconomyRecordOutcomeUnknownError as ``find_or_create_report`` does. """ return self._create( lambda connection: insert_simulation_rerun(connection, rerun) @@ -317,46 +250,18 @@ def read_budget_window_inputs( *, baseline: SimulationChoice, reform: SimulationChoice, - reuse: RunReuse | None, - years: Sequence[int], ) -> BudgetWindowInputs: - """Read a window's catalog rows and both sides' values, then each year's runs. + """Read a window's catalog rows and both sides' values together. - ``request.year`` is the window's first year. With ``reuse``, each - year's sides get the runs ``find_reusable_run`` finds for that year, - and the reform never gets the baseline's. Raise the errors + ``request.year`` is the window's first year. Raise the errors ``read_report_inputs`` raises. """ with self._transaction() as connection: catalog = read_catalog_rows(connection, request) return read_window_sides( - connection, - request, - catalog, - baseline=baseline, - reform=reform, - searches=( - ( - year, - None - if reuse is None - else search_by_definition( - connection, catalog.population(year), reuse - ), - ) - for year in years - ), + connection, request, catalog, baseline=baseline, reform=reform ) - def create_budget_window_report(self, report: NewBudgetWindowReport) -> None: - """Create the window's report, its budget-window run and each year's runs. - - Raise ``insert_budget_window_report``'s errors, and - EconomyRecordOutcomeUnknownError when the commit fails after the - rows were inserted. - """ - self._create(lambda connection: insert_budget_window_report(connection, report)) - def find_or_create_report(self, report: KeyedEconomyReport) -> FoundEconomyReport: """Find the report by key, with its run, or create them. @@ -391,24 +296,6 @@ def find_or_create_simulation( lambda connection: find_or_create_economy_simulation(connection, simulation) ) - def create_report(self, report: NewEconomyReport) -> None: - """Create the report, its new simulations and its runs. - - Raise ``insert_economy_report``'s errors, and - EconomyRecordOutcomeUnknownError when the commit fails after the - rows were inserted. - """ - self._create(lambda connection: insert_economy_report(connection, report)) - - def create_simulation(self, simulation: NewEconomySimulation) -> None: - """Create the simulation and its run; see ``insert_economy_simulation``. - - Raise EconomyRecordOutcomeUnknownError as ``create_report`` does. - """ - self._create( - lambda connection: insert_economy_simulation(connection, simulation) - ) - def _create[Created](self, insert: Callable[[Connection], Created]) -> Created: """Run ``insert`` in a transaction, telling a failed commit apart. @@ -496,7 +383,6 @@ def read_simulation(self, simulation_id: UUID) -> EconomySimulationState | None: "EconomySimulationInputs", "ReportSide", "ReportSideError", - "RunReuse", "SideRefusal", "SimulationInputs", "TransactionFactory", diff --git a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/household_report_records.py b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/household_report_records.py index 083c1a77c..c4df16170 100644 --- a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/household_report_records.py +++ b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/household_report_records.py @@ -1,47 +1,31 @@ -"""The simulation service's writes for household reports. +"""The simulation service's household report type and household run rows. A household report calculates one stored household, for one year, under a -baseline policy and, optionally, a reform. The service creates in one -transaction: - -- for each side, a new household ``simulations`` row naming the household - and the side's policy, and its first ``simulation_runs`` row, ``pending`` - with no Modal call ID. Household runs are never reused: each report gets - its own; -- the ``reports`` row, of type ``household``, naming the household, both - simulations and the reform's policy, with the report's options in - ``inputs``; -- its first ``report_runs`` row, ``pending`` with no Modal call ID and - trigger ``initial``, naming both simulation runs, or the baseline's - alone. +baseline policy and, optionally, a reform. Its ``reports`` row has type +``household`` (``HOUSEHOLD_REPORT_TYPE``) and names the household; each side +is a household ``simulations`` row naming the household and the side's +policy. The service finds or creates the report, its simulations and its +first run by definition key (``keyed_household_records``), and re-runs a +stored one (``economy_rerun_records``). Either creates each side's run +``pending`` with no Modal call ID, from ``household_simulation_run_row``; +a forced re-run inserts it with ``insert_household_simulation_run``. The service then starts one household function call for the report run and records the call's ID on it, or fails it and its simulation runs at once, with ``economy_run_records``' report run writes. The household function writes the rest: each run's ``household_result``, and every status after -``pending``. A re-run (``economy_rerun_records``) adds a run to each of a -stored report's household simulations with ``insert_household_simulation_run``. +``pending``. """ from __future__ import annotations -from dataclasses import dataclass from typing import TYPE_CHECKING, Any, Final from sqlalchemy import insert -from policyengine_simulation_persistence.economy_run_records import ( - references_checked, -) from policyengine_simulation_persistence.tables import ( - ReportRunStatus, - ReportRunTrigger, SimulationStatus, - SimulationType, - report_runs, - reports, simulation_runs, - simulations, ) if TYPE_CHECKING: @@ -55,127 +39,6 @@ HOUSEHOLD_REPORT_TYPE: Final = "household" -@dataclass(frozen=True) -class HouseholdReportSimulation: - """The new household simulation and run one side of a report creates. - - ``policy_id`` is the side's stored policy, or None for current law. - """ - - simulation_id: UUID - simulation_run_id: UUID - policy_id: UUID | None - - -@dataclass(frozen=True) -class NewHouseholdReport: - """A household report to create, with its first run. - - ``reform`` is None for a report of the baseline alone. ``inputs`` holds - the report's options as the Public API reads them. - """ - - id: UUID - run_id: UUID - country: str - label: str - description: str | None - tax_benefit_model_id: UUID - tax_benefit_model_version_id: UUID - household_id: UUID - year: int - versions: RunVersions - inputs: dict[str, Any] - baseline: HouseholdReportSimulation - reform: HouseholdReportSimulation | None - - @property - def sides(self) -> tuple[HouseholdReportSimulation, ...]: - """Return each side the report calculates, the baseline's first.""" - return (self.baseline,) if self.reform is None else (self.baseline, self.reform) - - @property - def simulation_run_ids(self) -> tuple[UUID, ...]: - """Return the IDs of the simulation runs created for the report.""" - return tuple(side.simulation_run_id for side in self.sides) - - -def insert_household_report(connection: Connection, report: NewHouseholdReport) -> None: - """Create the report, its first run and each side's simulation and run. - - Raise EconomyRecordReferenceError when the database refuses a row it - names. The refusal ends the caller's transaction, or its savepoint. - """ - with references_checked(): - for side in report.sides: - _insert_household_simulation(connection, report, side) - reform = report.reform - connection.execute( - insert(reports).values( - id=report.id, - label=report.label, - description=report.description, - country=report.country, - type=HOUSEHOLD_REPORT_TYPE, - user_id=None, - tax_benefit_model_id=report.tax_benefit_model_id, - policy_id=None if reform is None else reform.policy_id, - baseline_simulation_id=report.baseline.simulation_id, - reform_simulation_id=None if reform is None else reform.simulation_id, - household_id=report.household_id, - dataset_id=None, - region_id=None, - year=report.year, - budget_window_size=None, - inputs=report.inputs, - ) - ) - connection.execute( - insert(report_runs).values( - id=report.run_id, - report_id=report.id, - baseline_simulation_run_id=report.baseline.simulation_run_id, - reform_simulation_run_id=( - None if reform is None else reform.simulation_run_id - ), - budget_window_run_id=None, - year=None, - country_package_version=report.versions.country_package_version, - policyengine_version=report.versions.policyengine_version, - status=ReportRunStatus.PENDING, - trigger=ReportRunTrigger.INITIAL, - modal_call_id=None, - ) - ) - - -def _insert_household_simulation( - connection: Connection, report: NewHouseholdReport, side: HouseholdReportSimulation -) -> None: - """Create one side's household simulation and its first run, pending.""" - connection.execute( - insert(simulations).values( - id=side.simulation_id, - simulation_type=SimulationType.HOUSEHOLD, - dataset_id=None, - household_id=report.household_id, - policy_id=side.policy_id, - dynamic_id=None, - tax_benefit_model_version_id=report.tax_benefit_model_version_id, - region_id=None, - year=report.year, - ) - ) - insert_household_simulation_run( - connection, - run_id=side.simulation_run_id, - simulation_id=side.simulation_id, - year=report.year, - tax_benefit_model_version_id=report.tax_benefit_model_version_id, - versions=report.versions, - ) - - def insert_household_simulation_run( # noqa: PLR0913 # the run and what it calculates on connection: Connection, *, diff --git a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/household_report_store.py b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/household_report_store.py index 0ddd4209e..e33d29231 100644 --- a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/household_report_store.py +++ b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/household_report_store.py @@ -2,13 +2,13 @@ The Simulation Entrypoint creates a household report in three steps, each its own transaction, with the household function's call started between -them: it reads the report's inputs, creates the rows, then records the call -on the report run or fails the run. A stored report's re-run takes the same -steps, creating the report's new runs instead. ``HouseholdReportStore`` runs -the reads and writes of ``household_report_records``, -``household_job_records``, ``policy_reform``, ``economy_rerun_records``' -household report re-run and ``economy_run_records``' report run writes that -way. +them: it reads the report's inputs, finds or creates the rows by key, then +records the call on a report run it created or fails the run. A stored +report's re-run takes the same steps, joining or creating the report's new +runs instead. ``HouseholdReportStore`` runs the reads and writes of +``keyed_household_records``, ``household_job_records``, ``policy_reform``, +``economy_rerun_records``' household report re-run and +``economy_run_records``' report run writes that way. """ from __future__ import annotations @@ -39,9 +39,6 @@ UnknownCatalogVersionError, UnknownHouseholdError, ) -from policyengine_simulation_persistence.household_report_records import ( - insert_household_report, -) from policyengine_simulation_persistence.keyed_household_records import ( find_or_create_household_report, ) @@ -65,9 +62,6 @@ from policyengine_simulation_persistence.household_job_records import ( StoredHousehold, ) - from policyengine_simulation_persistence.household_report_records import ( - NewHouseholdReport, - ) from policyengine_simulation_persistence.keyed_household_records import ( FoundHouseholdReport, KeyedHouseholdReport, @@ -197,20 +191,11 @@ def find_or_create_report( lambda connection: find_or_create_household_report(connection, report) ) - def create_report(self, report: NewHouseholdReport) -> None: - """Create the report, its simulations and its runs. - - Raise ``insert_household_report``'s errors, and - EconomyRecordOutcomeUnknownError when the commit fails after the - rows were inserted: they may exist. - """ - self._create(lambda connection: insert_household_report(connection, report)) - def create_report_rerun(self, rerun: NewHouseholdReportRerun) -> FoundReportRun: """Join a stored report's run on the re-run's versions, or create it. See ``insert_household_report_rerun``; raise its errors, and - EconomyRecordOutcomeUnknownError as ``create_report`` does. + EconomyRecordOutcomeUnknownError as ``find_or_create_report`` does. """ return self._create( lambda connection: insert_household_report_rerun(connection, rerun) diff --git a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/rerun_definitions.py b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/rerun_definitions.py index e1561ba75..af0f332c4 100644 --- a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/rerun_definitions.py +++ b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/rerun_definitions.py @@ -61,8 +61,7 @@ class WindowYearDefinition: """One year of a budget window's newest calculation, and its sides' simulations. They are the simulations of the runs that year's report run names: a - year that reused a run names that run's simulation, which may be another - report's. + year that joined another report's run names that run's simulation. """ year: int diff --git a/libs/policyengine-simulation-persistence/tests/economy_rows.py b/libs/policyengine-simulation-persistence/tests/economy_rows.py index 0b90d3a24..517487ebf 100644 --- a/libs/policyengine-simulation-persistence/tests/economy_rows.py +++ b/libs/policyengine-simulation-persistence/tests/economy_rows.py @@ -1,9 +1,10 @@ """Rows for the economy simulation and report tests. The catalog rows a request names are the Public API's, inserted as the -superuser. A run that already succeeded, and the report run that used it, are -written as the superuser too, through the service tables' models: the -service's role can neither mark a run succeeded nor record its output. +superuser. A stored simulation, report and their runs are written as the +superuser too, through the service tables' models: the service's role can +neither mark a run succeeded nor record its output. None of them has a +definition key, as a record the service created before keys has none. """ from __future__ import annotations @@ -146,15 +147,13 @@ class StoredRun: class RunShape: """What a stored economy simulation and its run hold. - A succeeded run records an output file in the output bucket unless - ``output_uri`` names another. ``forced`` marks a forced re-run's run. + A succeeded run records an output file in the output bucket. ``forced`` + marks a forced re-run's run. """ choice: SimulationChoice = CURRENT_LAW status: SimulationStatus = SimulationStatus.SUCCEEDED versions: RunVersions = VERSIONS - output_uri: str | None = None - completed_at: datetime = COMPLETED_AT forced: bool = False @@ -206,11 +205,9 @@ def insert_run_of( dataset_id=population.dataset_id, region_id=population.region_id, status=shape.status, - completed_at=shape.completed_at if finished else None, + completed_at=COMPLETED_AT if finished else None, output_uri=( - (shape.output_uri or f"{OUTPUT_BUCKET_PREFIX}outputs/{run_id}.parquet") - if succeeded - else None + f"{OUTPUT_BUCKET_PREFIX}outputs/{run_id}.parquet" if succeeded else None ), output_sha256="d" * 64 if succeeded else None, forced=shape.forced, @@ -313,23 +310,43 @@ def insert_report_run_of( return run_id -def insert_reusable_run( - connection: Connection, - economy: EconomyCatalog, - shape: RunShape | None = None, - *, - inputs: dict[str, Any] | None = None, -) -> StoredRun: - """Insert a run shaped ``shape`` that a succeeded report run used, with ``inputs``. +@dataclass(frozen=True) +class PendingReport: + """A stored report whose first run, and both its new simulation runs, wait. - The report run's other simulation run failed, so it is never reusable. + Every run is ``pending`` with no Modal call ID, as the service left a + report it had just created. """ + + report_id: UUID + report_run_id: UUID + baseline: StoredRun + reform: StoredRun + + @property + def simulation_run_ids(self) -> tuple[UUID, UUID]: + """Return the baseline's simulation run, then the reform's.""" + return (self.baseline.simulation_run_id, self.reform.simulation_run_id) + + +def insert_pending_report( + connection: Connection, economy: EconomyCatalog +) -> PendingReport: + """Insert a current-law report and its first run, all runs pending.""" population = economy.population() - reused = insert_simulation_run(connection, population, shape) - other_side = insert_simulation_run( - connection, population, RunShape(status=SimulationStatus.FAILED) + pending = RunShape(status=SimulationStatus.PENDING) + baseline = insert_simulation_run(connection, population, pending) + reform = insert_simulation_run(connection, population, pending) + report_id = insert_report_of(connection, economy, (baseline, reform)) + report_run_id = insert_report_run_of( + connection, + report_id, + (baseline, reform), + ReportRunOutcome(status=ReportRunStatus.PENDING), ) - insert_report_run_naming( - connection, economy, (reused, other_side), ReportRunOutcome(inputs=inputs) + return PendingReport( + report_id=report_id, + report_run_id=report_run_id, + baseline=baseline, + reform=reform, ) - return reused diff --git a/libs/policyengine-simulation-persistence/tests/household_report_rows.py b/libs/policyengine-simulation-persistence/tests/household_report_rows.py new file mode 100644 index 000000000..1ceb62eb5 --- /dev/null +++ b/libs/policyengine-simulation-persistence/tests/household_report_rows.py @@ -0,0 +1,189 @@ +"""Household reports stored as the simulation service created them before keys. + +The service now finds or creates a household report by definition key +(``keyed_household_records``). A report it created before then has no key: +each side is a household simulation of its own with one run, the report +names the household, both simulations and the reform's policy, and its +first run has trigger ``initial``. Every run is ``pending`` with no Modal +call ID. ``insert_household_report_rows`` inserts such a report directly, +as whatever role the connection acts as. +""" + +from __future__ import annotations + +from dataclasses import dataclass +from typing import TYPE_CHECKING, Any +from uuid import uuid4 + +from policyengine_simulation_persistence import ( + HOUSEHOLD_REPORT_TYPE, + ReportRunStatus, + ReportRunTrigger, + SimulationStatus, + SimulationType, + report_runs, + reports, + simulation_runs, + simulations, +) +from sqlalchemy import insert + +if TYPE_CHECKING: + from uuid import UUID + + from policyengine_simulation_persistence import RunVersions + from public_api_rows import CatalogVersion + from sqlalchemy import Connection + + +@dataclass(frozen=True) +class StoredHouseholdSide: + """One side of a stored household report: its simulation, run and policy. + + ``policy_id`` is the side's stored policy, or None for current law. + """ + + simulation_id: UUID + simulation_run_id: UUID + policy_id: UUID | None + + +@dataclass(frozen=True) +class StoredHouseholdReport: + """A stored household report and its first run. + + ``reform`` is None for a report of the baseline alone. + """ + + id: UUID + run_id: UUID + household_id: UUID + year: int + baseline: StoredHouseholdSide + reform: StoredHouseholdSide | None + + @property + def sides(self) -> tuple[StoredHouseholdSide, ...]: + """Return each side the report calculates, the baseline's first.""" + return (self.baseline,) if self.reform is None else (self.baseline, self.reform) + + @property + def simulation_run_ids(self) -> tuple[UUID, ...]: + """Return the IDs of the report's first simulation runs.""" + return tuple(side.simulation_run_id for side in self.sides) + + +def insert_household_report_rows( # noqa: PLR0913 # the report's catalog, household and sides + connection: Connection, + catalog: CatalogVersion, + *, + household_id: UUID, + year: int, + versions: RunVersions, + reform_policy_id: UUID | None = None, + with_reform: bool = True, +) -> StoredHouseholdReport: + """Insert a household report with no key, its simulations and its runs. + + The baseline is current law; with ``with_reform``, the reform has + ``reform_policy_id``, current law when it is None. + """ + report = StoredHouseholdReport( + id=uuid4(), + run_id=uuid4(), + household_id=household_id, + year=year, + baseline=StoredHouseholdSide( + simulation_id=uuid4(), simulation_run_id=uuid4(), policy_id=None + ), + reform=( + StoredHouseholdSide( + simulation_id=uuid4(), + simulation_run_id=uuid4(), + policy_id=reform_policy_id, + ) + if with_reform + else None + ), + ) + for side in report.sides: + connection.execute( + insert(simulations).values( + id=side.simulation_id, + simulation_type=SimulationType.HOUSEHOLD, + dataset_id=None, + household_id=household_id, + policy_id=side.policy_id, + dynamic_id=None, + tax_benefit_model_version_id=catalog.version_id, + region_id=None, + year=year, + ) + ) + connection.execute( + insert(simulation_runs).values( + _pending_run(side, catalog=catalog, year=year, versions=versions) + ) + ) + reform = report.reform + connection.execute( + insert(reports).values( + id=report.id, + label="A larger child tax credit", + description="For one family", + country=catalog.country, + type=HOUSEHOLD_REPORT_TYPE, + user_id=None, + tax_benefit_model_id=catalog.model_id, + policy_id=None if reform is None else reform.policy_id, + baseline_simulation_id=report.baseline.simulation_id, + reform_simulation_id=None if reform is None else reform.simulation_id, + household_id=household_id, + dataset_id=None, + region_id=None, + year=year, + budget_window_size=None, + inputs={}, + ) + ) + connection.execute( + insert(report_runs).values( + id=report.run_id, + report_id=report.id, + baseline_simulation_run_id=report.baseline.simulation_run_id, + reform_simulation_run_id=( + None if reform is None else reform.simulation_run_id + ), + budget_window_run_id=None, + year=None, + country_package_version=versions.country_package_version, + policyengine_version=versions.policyengine_version, + status=ReportRunStatus.PENDING, + trigger=ReportRunTrigger.INITIAL, + modal_call_id=None, + ) + ) + return report + + +def _pending_run( + side: StoredHouseholdSide, + *, + catalog: CatalogVersion, + year: int, + versions: RunVersions, +) -> dict[str, Any]: + """Return a side's household simulation run, pending with no Modal call ID.""" + return { + "id": side.simulation_run_id, + "simulation_id": side.simulation_id, + "year": year, + "tax_benefit_model_version_id": catalog.version_id, + "country_package_version": versions.country_package_version, + "policyengine_version": versions.policyengine_version, + "dataset_id": None, + "region_id": None, + "status": SimulationStatus.PENDING, + "modal_call_id": None, + "forced": False, + } diff --git a/libs/policyengine-simulation-persistence/tests/test_budget_window_records.py b/libs/policyengine-simulation-persistence/tests/test_budget_window_records.py index a08d4766f..43f912a5a 100644 --- a/libs/policyengine-simulation-persistence/tests/test_budget_window_records.py +++ b/libs/policyengine-simulation-persistence/tests/test_budget_window_records.py @@ -1,9 +1,8 @@ -"""A budget-window report's inputs and rows: one report run per year. +"""A budget-window report's inputs: its catalog rows and both sides' values. A budget window compares the same baseline and reform in each year of its -window. Each year has its own simulations, with that year, so a year can -reuse a single-year report's run, and the reverse. These run against a -database the Public API's own v2 migration chain built +window, so both sides' values are read once for every year. These run +against a database the Public API's own v2 migration chain built (``PUBLIC_API_SCHEMA_DATABASE_URL``), acting as the simulation service's role in a transaction the test rolls back. """ @@ -11,44 +10,22 @@ from __future__ import annotations from contextlib import contextmanager -from dataclasses import replace from datetime import UTC, datetime from typing import TYPE_CHECKING -from uuid import uuid4 import pytest from conftest import act_as_the_service, rolled_back_transaction -from economy_rows import ( - CURRENT_LAW, - OPTIONS, - OUTPUT_BUCKET_PREFIX, - POLICYENGINE_VERSION, - VERSIONS, - RunShape, - insert_economy_catalog, - insert_report_run_naming, - insert_simulation_run, -) +from economy_rows import CURRENT_LAW, POLICYENGINE_VERSION, insert_economy_catalog from policyengine_simulation_persistence import ( - BudgetWindowYear, - EconomyPopulation, - EconomyRecordReferenceError, EconomyRequest, EconomyRunStore, - NewBudgetWindowReport, - ReportSimulation, - ReusableRun, - RunReuse, SimulationChoice, - SimulationStatus, ) from public_api_rows import StoredValue, insert_policy -from sqlalchemy import text if TYPE_CHECKING: from collections.abc import Iterator - from economy_rows import EconomyCatalog from sqlalchemy import Connection pytestmark = pytest.mark.public_api_schema @@ -62,10 +39,6 @@ dataset_name=None, year=2026, ) -REUSE = RunReuse( - versions=VERSIONS, inputs=OPTIONS, output_uri_prefix=OUTPUT_BUCKET_PREFIX -) -WINDOW_YEARS = (2026, 2027, 2028) def _store_on(connection: Connection) -> EconomyRunStore: @@ -79,283 +52,22 @@ def savepoint() -> Iterator[Connection]: return EconomyRunStore(savepoint) -def _reusable_run_in(connection: Connection, economy: EconomyCatalog, year: int): - """Insert a succeeded current-law run of ``year`` that a report run used.""" - population = economy.population(year=year) - reused = insert_simulation_run(connection, population) - other_side = insert_simulation_run( - connection, population, RunShape(status=SimulationStatus.FAILED) - ) - insert_report_run_naming(connection, economy, (reused, other_side)) - return reused - - -def test_a_window_reads_each_year_s_reusable_runs(public_api_database_url) -> None: +def test_a_windows_catalog_rows_and_both_sides_values_are_read( + public_api_database_url, +) -> None: with rolled_back_transaction(public_api_database_url) as connection: economy = insert_economy_catalog(connection, parameter_names=(CTC_AMOUNT,)) policy_id = insert_policy( connection, economy.catalog, [StoredValue(CTC_AMOUNT, 3_000, START)] ) - reused_in_2027 = _reusable_run_in(connection, economy, 2027) act_as_the_service(connection) reform_choice = SimulationChoice(policy_id=policy_id, dynamic_id=None) inputs = _store_on(connection).read_budget_window_inputs( - REQUEST, - baseline=CURRENT_LAW, - reform=reform_choice, - reuse=REUSE, - years=WINDOW_YEARS, + REQUEST, baseline=CURRENT_LAW, reform=reform_choice ) assert inputs.catalog.catalog_version_id == economy.catalog.version_id assert (inputs.baseline.choice, inputs.baseline.reform) == (CURRENT_LAW, {}) assert inputs.reform.choice == reform_choice - assert inputs.reusable_runs == { - 2026: (None, None), - 2027: ( - ReusableRun( - simulation_id=reused_in_2027.simulation_id, - simulation_run_id=reused_in_2027.simulation_run_id, - ), - None, - ), - 2028: (None, None), - } - - -def test_a_window_reads_no_reusable_run_without_reuse(public_api_database_url) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - _reusable_run_in(connection, economy, 2027) - act_as_the_service(connection) - - inputs = _store_on(connection).read_budget_window_inputs( - REQUEST, - baseline=CURRENT_LAW, - reform=CURRENT_LAW, - reuse=None, - years=WINDOW_YEARS, - ) - - assert set(inputs.reusable_runs.values()) == {(None, None)} - - -def _new_side(choice: SimulationChoice = CURRENT_LAW) -> ReportSimulation: - return ReportSimulation( - simulation_id=uuid4(), simulation_run_id=uuid4(), choice=choice, reused=False - ) - - -def _window(economy: EconomyCatalog, reused_in_2027) -> NewBudgetWindowReport: - years = [] - for year in WINDOW_YEARS: - baseline = ( - ReportSimulation( - simulation_id=reused_in_2027.simulation_id, - simulation_run_id=reused_in_2027.simulation_run_id, - choice=CURRENT_LAW, - reused=True, - ) - if year == 2027 - else _new_side() - ) - years.append( - BudgetWindowYear( - year=year, - report_run_id=uuid4(), - baseline=baseline, - reform=_new_side(), - ) - ) - return NewBudgetWindowReport( - id=uuid4(), - window_run_id=uuid4(), - country="us", - label="Ten-year budget window", - description=None, - tax_benefit_model_id=economy.catalog.model_id, - population=economy.population(year=2026), - versions=VERSIONS, - inputs=OPTIONS, - years=tuple(years), - ) - - -def _rows(connection: Connection, table_name: str, **filters): - rows = connection.execute( - text( - f"SELECT * FROM {table_name} WHERE " # noqa: S608 # test-only names - + " AND ".join(f"{name} = :{name}" for name in filters) - ), - filters, - ) - return [dict(row._mapping) for row in rows] - - -def test_a_window_is_created_with_a_report_run_per_year( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - reused_in_2027 = _reusable_run_in(connection, economy, 2027) - window = _window(economy, reused_in_2027) - act_as_the_service(connection) - - _store_on(connection).create_budget_window_report(window) - - connection.execute(text("RESET ROLE")) - (report,) = _rows(connection, "reports", id=window.id) - (window_run,) = _rows(connection, "budget_window_runs", id=window.window_run_id) - year_runs = { - row["year"]: row - for row in _rows( - connection, "report_runs", budget_window_run_id=window.window_run_id - ) - } - simulations = { - run_id: ( - _rows(connection, "simulation_runs", id=run_id)[0], - _rows( - connection, - "simulations", - id=_rows(connection, "simulation_runs", id=run_id)[0][ - "simulation_id" - ], - )[0], - ) - for year in window.years - for run_id in ( - year.baseline.simulation_run_id, - year.reform.simulation_run_id, - ) - } - - first_year = window.years[0] - assert ( - report["type"], - report["year"], - report["budget_window_size"], - report["inputs"], - report["baseline_simulation_id"], - report["reform_simulation_id"], - report["household_id"], - ) == ( - "economy", - 2026, - 3, - OPTIONS, - first_year.baseline.simulation_id, - first_year.reform.simulation_id, - None, - ) - assert ( - window_run["report_id"], - window_run["status"], - window_run["trigger"], - window_run["recalculate"], - window_run["tax_benefit_model_version_id"], - ) == (window.id, "pending", "initial", False, economy.catalog.version_id) - for year in window.years: - year_run = year_runs[year.year] - assert ( - year_run["id"], - year_run["report_id"], - year_run["status"], - year_run["trigger"], - year_run["modal_call_id"], - year_run["baseline_simulation_run_id"], - year_run["reform_simulation_run_id"], - year_run["country_package_version"], - year_run["policyengine_version"], - ) == ( - year.report_run_id, - window.id, - "pending", - "initial", - None, - year.baseline.simulation_run_id, - year.reform.simulation_run_id, - VERSIONS.country_package_version, - VERSIONS.policyengine_version, - ) - for side in (year.baseline, year.reform): - simulation_run, simulation = simulations[side.simulation_run_id] - # Each year has its own simulations, of that year. - assert (simulation["year"], simulation_run["year"]) == ( - year.year, - year.year, - ) - assert simulation_run["status"] == ( - "succeeded" if side.reused else "pending" - ) - - -def test_a_window_naming_a_missing_policy_is_refused_and_writes_nothing( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - reused_in_2027 = _reusable_run_in(connection, economy, 2027) - window = _window(economy, reused_in_2027) - missing_policy = SimulationChoice(policy_id=uuid4(), dynamic_id=None) - last_year = window.years[-1] - window = replace( - window, - years=( - *window.years[:-1], - replace( - last_year, reform=replace(last_year.reform, choice=missing_policy) - ), - ), - ) - act_as_the_service(connection) - - with pytest.raises(EconomyRecordReferenceError) as refused: - _store_on(connection).create_budget_window_report(window) - - connection.execute(text("RESET ROLE")) - written = connection.execute( - text("SELECT count(*) FROM reports WHERE id = :id"), {"id": window.id} - ).scalar_one() - - assert (refused.value.table_name, refused.value.column_name) == ( - "simulations", - "policy_id", - ) - assert written == 0 - - -@pytest.mark.parametrize( - "years", - [(), (2026, 2028), tuple(range(2026, 2102))], - ids=["no-years", "a-gap", "76-years"], -) -def test_a_window_s_years_must_be_consecutive_from_its_first(years) -> None: - population = EconomyPopulation( - tax_benefit_model_version_id=uuid4(), - region_id=uuid4(), - dataset_id=uuid4(), - year=years[0] if years else 2026, - ) - with pytest.raises(ValueError, match="budget window"): - NewBudgetWindowReport( - id=uuid4(), - window_run_id=uuid4(), - country="us", - label="Window", - description=None, - tax_benefit_model_id=uuid4(), - population=population, - versions=VERSIONS, - inputs=OPTIONS, - years=tuple( - BudgetWindowYear( - year=year, - report_run_id=uuid4(), - baseline=_new_side(), - reform=_new_side(), - ) - for year in years - ), - ) + assert inputs.reform.reform == {CTC_AMOUNT: {"2026-01-01.2200-12-31": 3_000}} diff --git a/libs/policyengine-simulation-persistence/tests/test_economy_reruns.py b/libs/policyengine-simulation-persistence/tests/test_economy_reruns.py index 73e3fbfc6..83214bea3 100644 --- a/libs/policyengine-simulation-persistence/tests/test_economy_reruns.py +++ b/libs/policyengine-simulation-persistence/tests/test_economy_reruns.py @@ -1,9 +1,9 @@ """Re-running a stored economy report or simulation on a later catalog version. -A re-run reads what the stored report or simulation calculates, creates a new -run of it on the current versions, and keeps every earlier run. A report's -side may reuse a run of its own simulation that succeeded on those versions; -nothing is created while the record has a run pending or running. +A re-run reads what the stored report or simulation calculates, joins or +creates its run on the current versions, and keeps every earlier run. A +report's side joins its own simulation's run on those versions, or creates +one; a forced re-run creates an extra run beside any run under way. These run against a database the Public API's own v2 migration chain built (``PUBLIC_API_SCHEMA_DATABASE_URL``), as the simulation service's role, each @@ -25,7 +25,6 @@ LATER_POLICYENGINE_VERSION, LATER_VERSIONS, OPTIONS, - OUTPUT_BUCKET_PREFIX, VERSIONS, EconomyCatalog, ReportRunOutcome, @@ -39,7 +38,6 @@ insert_simulation_run, ) from policyengine_simulation_persistence import ( - EconomyPopulation, EconomyRecordReferenceError, EconomyRequest, EconomyRunStore, @@ -53,8 +51,6 @@ ReportSideError, RerunRecordDeletedError, RerunSide, - ReusableRun, - RunReuse, SideDefinition, SimulationChoice, SimulationDefinition, @@ -62,8 +58,6 @@ SimulationType, UnknownDatasetError, UnknownRegionError, - find_reusable_run, - find_reusable_run_of, insert_report_rerun, insert_simulation_rerun, lock_reruns_of, @@ -100,9 +94,6 @@ dataset_name="populace_us_2024", year=2026, ) -LATER_REUSE = RunReuse( - versions=LATER_VERSIONS, inputs=OPTIONS, output_uri_prefix=OUTPUT_BUCKET_PREFIX -) @dataclass(frozen=True) @@ -254,24 +245,6 @@ def test_a_stored_economy_simulations_definition_is_read( ) -def _find_later_run_of( - connection: Connection, - simulation_id: UUID, - later: EconomyCatalog, - *, - excluded_run_id: UUID | None = None, -) -> ReusableRun | None: - return find_reusable_run_of( - connection, - simulation_id=simulation_id, - population=later.population(), - versions=LATER_VERSIONS, - inputs=OPTIONS, - output_uri_prefix=OUTPUT_BUCKET_PREFIX, - excluded_run_id=excluded_run_id, - ) - - def _used_on_later_catalog( connection: Connection, stored: StoredReport, @@ -281,7 +254,7 @@ def _used_on_later_catalog( """Insert a succeeded later run of the baseline that another report used. The other report, created on ``economy`` beside the stored one, compares - the stored report's baseline simulation (reusing its run) with a reform + the stored report's baseline simulation (naming its run) with a reform simulation of its own; its initial run succeeded, and its manual re-run on the later catalog created both sides' later runs and succeeded. The stored report has no run on the later versions, so a re-run of it has @@ -306,57 +279,6 @@ def _used_on_later_catalog( return later_baseline -def test_a_simulations_own_run_on_a_later_catalog_version_is_reused( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - later = insert_later_catalog(connection, economy) - stored = _stored_report(connection, economy) - later_baseline = _used_on_later_catalog(connection, stored, economy, later) - act_as_the_service(connection) - - found = _find_later_run_of(connection, stored.baseline.simulation_id, later) - excluded = _find_later_run_of( - connection, - stored.baseline.simulation_id, - later, - excluded_run_id=later_baseline.simulation_run_id, - ) - # The run on the earlier catalog version is not on the current versions. - earlier = find_reusable_run_of( - connection, - simulation_id=stored.baseline.simulation_id, - population=economy.population(), - versions=LATER_VERSIONS, - inputs=OPTIONS, - output_uri_prefix=OUTPUT_BUCKET_PREFIX, - ) - - assert found == ReusableRun( - simulation_id=stored.baseline.simulation_id, - simulation_run_id=later_baseline.simulation_run_id, - ) - assert excluded is None - assert earlier is None - - -def test_another_simulations_run_is_not_reused_for_a_simulations_rerun( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - later = insert_later_catalog(connection, economy) - stored = _stored_report(connection, economy) - _used_on_later_catalog(connection, stored, economy, later) - other = _stored_report(connection, economy) - act_as_the_service(connection) - - found = _find_later_run_of(connection, other.baseline.simulation_id, later) - - assert found is None - - def _rerun_of(stored: StoredReport, later: EconomyCatalog) -> NewReportRerun: """Return an unforced re-run of ``stored`` on the later catalog's versions.""" return NewReportRerun( @@ -367,12 +289,10 @@ def _rerun_of(stored: StoredReport, later: EconomyCatalog) -> NewReportRerun: baseline=RerunSide( simulation_id=stored.baseline.simulation_id, simulation_run_id=uuid4(), - reused=False, ), reform=RerunSide( simulation_id=stored.reform.simulation_id, simulation_run_id=uuid4(), - reused=False, ), ) @@ -655,53 +575,21 @@ def test_a_report_reruns_inputs_are_read_on_the_current_catalog_version( ) reform_choice = SimulationChoice(policy_id=policy_id, dynamic_id=None) stored = _stored_report(connection, economy, reform_choice=reform_choice) - reused = _used_on_later_catalog(connection, stored, economy, later) act_as_the_service(connection) - store = _store_on(connection) - sides = { - "baseline": SideDefinition(stored.baseline.simulation_id, CURRENT_LAW), - "reform": SideDefinition(stored.reform.simulation_id, reform_choice), - } - inputs = store.read_report_rerun_inputs( - LATER_REQUEST, **sides, reuse=LATER_REUSE + inputs = _store_on(connection).read_report_rerun_inputs( + LATER_REQUEST, + baseline=SideDefinition(stored.baseline.simulation_id, CURRENT_LAW), + reform=SideDefinition(stored.reform.simulation_id, reform_choice), ) - forced = store.read_report_rerun_inputs(LATER_REQUEST, **sides, reuse=None) assert (inputs.catalog.catalog_version_id, inputs.catalog.region.id) == ( later.catalog.version_id, later.region_id, ) assert inputs.catalog.dataset.id == later.dataset_id - assert inputs.baseline.reusable_run == ReusableRun( - simulation_id=stored.baseline.simulation_id, - simulation_run_id=reused.simulation_run_id, - ) - assert inputs.reform.reusable_run is None + assert inputs.baseline.reform == {} assert inputs.reform.reform == {CTC_AMOUNT: {"2026-01-01.2200-12-31": 3_000}} - assert forced.baseline.reusable_run is None - - -def test_both_sides_of_one_simulation_never_reuse_the_same_run( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - later = insert_later_catalog(connection, economy) - stored = _stored_report(connection, economy) - reused = _used_on_later_catalog(connection, stored, economy, later) - act_as_the_service(connection) - side = SideDefinition(stored.baseline.simulation_id, CURRENT_LAW) - - inputs = _store_on(connection).read_report_rerun_inputs( - LATER_REQUEST, baseline=side, reform=side, reuse=LATER_REUSE - ) - - assert inputs.baseline.reusable_run == ReusableRun( - simulation_id=stored.baseline.simulation_id, - simulation_run_id=reused.simulation_run_id, - ) - assert inputs.reform.reusable_run is None def test_a_rerun_names_the_side_whose_policy_the_current_catalog_cannot_set( @@ -722,7 +610,6 @@ def test_a_rerun_names_the_side_whose_policy_the_current_catalog_cannot_set( LATER_REQUEST, baseline=SideDefinition(stored.baseline.simulation_id, CURRENT_LAW), reform=SideDefinition(stored.reform.simulation_id, reform_choice), - reuse=None, ) assert refused.value.side == "reform" @@ -761,7 +648,6 @@ def test_a_rerun_is_refused_when_the_current_catalog_lacks_its_region_or_dataset request, baseline=SideDefinition(stored.baseline.simulation_id, CURRENT_LAW), reform=SideDefinition(stored.reform.simulation_id, CURRENT_LAW), - reuse=None, ) @@ -822,47 +708,6 @@ def test_a_rerun_and_its_runs_are_created_through_the_store( assert simulation.id == simulation_rerun.simulation_id -def test_a_run_on_another_catalog_version_is_not_reused_by_a_new_report( - public_api_database_url, -) -> None: - """Only the run's own catalog version differs from the request's. - - It is also the newest run, so it would be found without that condition. - """ - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - later = insert_later_catalog(connection, economy) - stored = _stored_report(connection, economy) - connection.execute( - text( - "UPDATE public.simulation_runs " - "SET tax_benefit_model_version_id = :later, " - "completed_at = completed_at + interval '1 day' WHERE id = :id" - ), - { - "later": later.catalog.version_id, - "id": stored.baseline.simulation_run_id, - }, - ) - act_as_the_service(connection) - - found = find_reusable_run( - connection, - choice=CURRENT_LAW, - population=economy.population(), - versions=VERSIONS, - inputs=OPTIONS, - output_uri_prefix=OUTPUT_BUCKET_PREFIX, - ) - - # The reform's run, the same simulation definition on the right catalog - # version, is the only one left to reuse. - assert found == ReusableRun( - simulation_id=stored.reform.simulation_id, - simulation_run_id=stored.reform.simulation_run_id, - ) - - def test_a_household_or_filtered_simulation_and_another_model_are_read_as_such( public_api_database_url, ) -> None: @@ -979,9 +824,7 @@ def test_a_rerun_of_a_deleted_report_or_simulation_says_the_record_is_gone( # A report whose side simulation is gone names that simulation instead. gone_side = replace( _rerun_of(stored, later), - reform=RerunSide( - simulation_id=uuid4(), simulation_run_id=uuid4(), reused=False - ), + reform=RerunSide(simulation_id=uuid4(), simulation_run_id=uuid4()), ) with ( pytest.raises(EconomyRecordReferenceError) as refused, @@ -1005,12 +848,10 @@ def _forced_rerun_of(stored: StoredReport, economy: EconomyCatalog) -> NewReport baseline=RerunSide( simulation_id=stored.baseline.simulation_id, simulation_run_id=uuid4(), - reused=False, ), reform=RerunSide( simulation_id=stored.reform.simulation_id, simulation_run_id=uuid4(), - reused=False, ), forced=True, ) @@ -1050,29 +891,6 @@ def test_a_forced_report_rerun_is_an_extra_run_beside_any_run_on_its_versions( assert earlier_run["forced"] is False -@pytest.mark.parametrize("forced", [True, False]) -def test_a_report_rerun_names_no_reused_run(forced: bool) -> None: # noqa: FBT001 # the parameter - """Each side joins or creates its run; a forced one creates it (answer 3).""" - reused = RerunSide(simulation_id=uuid4(), simulation_run_id=uuid4(), reused=True) - created = RerunSide(simulation_id=uuid4(), simulation_run_id=uuid4(), reused=False) - - with pytest.raises(ValueError, match="reusing none"): - NewReportRerun( - report_id=uuid4(), - run_id=uuid4(), - population=EconomyPopulation( - tax_benefit_model_version_id=uuid4(), - region_id=uuid4(), - dataset_id=uuid4(), - year=2026, - ), - versions=VERSIONS, - baseline=reused, - reform=created, - forced=forced, - ) - - def test_a_report_rerun_losing_to_a_run_created_after_its_lookup_joins_it( committed_engine, committed_catalog ) -> None: diff --git a/libs/policyengine-simulation-persistence/tests/test_economy_run_records.py b/libs/policyengine-simulation-persistence/tests/test_economy_run_records.py index 1ddf50262..886a590f4 100644 --- a/libs/policyengine-simulation-persistence/tests/test_economy_run_records.py +++ b/libs/policyengine-simulation-persistence/tests/test_economy_run_records.py @@ -1,38 +1,31 @@ -"""The simulation service's reads and writes for economy simulations and reports. +"""The simulation service's catalog reads and run writes for economy records. These run against a database the Public API's own v2 migration chain built (``PUBLIC_API_SCHEMA_DATABASE_URL``), as the simulation service's role, each -in a transaction rolled back afterwards. The Public API's rows a request names -are inserted first, as the superuser. +in a transaction rolled back afterwards. The Public API's rows a request +names, and any stored report or simulation, are inserted first, as the +superuser. """ from __future__ import annotations -from dataclasses import replace from typing import TYPE_CHECKING from uuid import UUID, uuid4 import pytest from conftest import act_as_the_service, rolled_back_transaction from economy_rows import ( - CURRENT_LAW, - VERSIONS, - EconomyCatalog, + PendingReport, + RunShape, insert_economy_catalog, - insert_reusable_run, + insert_pending_report, + insert_simulation_run, ) from policyengine_simulation_persistence import ( - EconomyRecordReferenceError, - NewEconomyReport, - NewEconomySimulation, ReportRunStatus, - ReportSimulation, - SimulationChoice, SimulationStatus, fail_unstarted_report_run, fail_unstarted_simulation_run, - insert_economy_report, - insert_economy_simulation, read_dataset, read_dataset_by_id, read_model_id, @@ -42,7 +35,7 @@ report_runs, simulation_runs, ) -from public_api_rows import insert_dataset, insert_policy, insert_region +from public_api_rows import insert_dataset, insert_region from sqlalchemy import select, text, update if TYPE_CHECKING: @@ -50,38 +43,6 @@ pytestmark = pytest.mark.public_api_schema -REPORT_OPTIONS = {"include_cliffs": True, "spm": {"geography_kind": "national"}} - - -def _new_side(choice: SimulationChoice = CURRENT_LAW) -> ReportSimulation: - return ReportSimulation( - simulation_id=uuid4(), - simulation_run_id=uuid4(), - choice=choice, - reused=False, - ) - - -def _new_report( - economy: EconomyCatalog, - *, - baseline: ReportSimulation | None = None, - reform: ReportSimulation | None = None, -) -> NewEconomyReport: - return NewEconomyReport( - id=uuid4(), - run_id=uuid4(), - country=economy.catalog.country, - label="Child tax credit", - description="A larger credit", - tax_benefit_model_id=economy.catalog.model_id, - population=economy.population(), - versions=VERSIONS, - inputs=REPORT_OPTIONS, - baseline=baseline or _new_side(), - reform=reform or _new_side(), - ) - def _row(connection: Connection, table_name: str, row_id: UUID) -> dict: row = connection.execute( @@ -91,12 +52,6 @@ def _row(connection: Connection, table_name: str, row_id: UUID) -> dict: return dict(row.one()) -def _count(connection: Connection, table_name: str) -> int: - return connection.execute( - text(f"SELECT count(*) FROM public.{table_name}") # noqa: S608 # fixed table names - ).scalar_one() - - def test_the_countrys_model_is_found_by_its_name(public_api_database_url) -> None: with rolled_back_transaction(public_api_database_url) as connection: economy = insert_economy_catalog(connection) @@ -179,260 +134,10 @@ def test_a_dataset_is_found_by_name_or_id_in_its_catalog_version_only( assert (by_id.id, by_id.name) == (economy.dataset_id, "populace_us_2024") -def test_a_report_its_simulations_and_runs_are_created_pending( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - policy_id = insert_policy(connection, economy.catalog) - report = _new_report( - economy, reform=_new_side(SimulationChoice(policy_id, dynamic_id=None)) - ) - act_as_the_service(connection) - - insert_economy_report(connection, report) - stored_report = _row(connection, "reports", report.id) - stored_run = _row(connection, "report_runs", report.run_id) - baseline = _row(connection, "simulations", report.baseline.simulation_id) - reform = _row(connection, "simulations", report.reform.simulation_id) - baseline_run = _row( - connection, "simulation_runs", report.baseline.simulation_run_id - ) - reform_run = _row( - connection, "simulation_runs", report.reform.simulation_run_id - ) - - population = economy.population() - assert { - key: stored_report[key] - for key in ( - "label", - "description", - "country", - "type", - "user_id", - "tax_benefit_model_id", - "policy_id", - "baseline_simulation_id", - "reform_simulation_id", - "household_id", - "dataset_id", - "region_id", - "year", - "inputs", - "budget_window_size", - ) - } == { - "label": "Child tax credit", - "description": "A larger credit", - "country": "us", - "type": "economy", - "user_id": None, - "tax_benefit_model_id": economy.catalog.model_id, - "policy_id": policy_id, - "baseline_simulation_id": report.baseline.simulation_id, - "reform_simulation_id": report.reform.simulation_id, - "household_id": None, - "dataset_id": economy.dataset_id, - "region_id": economy.region_id, - "year": 2026, - "inputs": REPORT_OPTIONS, - "budget_window_size": None, - } - assert { - key: stored_run[key] - for key in ( - "report_id", - "baseline_simulation_run_id", - "reform_simulation_run_id", - "budget_window_run_id", - "year", - "country_package_version", - "policyengine_version", - "status", - "trigger", - "modal_call_id", - "started_at", - "completed_at", - ) - } == { - "report_id": report.id, - "baseline_simulation_run_id": report.baseline.simulation_run_id, - "reform_simulation_run_id": report.reform.simulation_run_id, - "budget_window_run_id": None, - "year": None, - "country_package_version": VERSIONS.country_package_version, - "policyengine_version": VERSIONS.policyengine_version, - "status": "pending", - "trigger": "initial", - "modal_call_id": None, - "started_at": None, - "completed_at": None, - } - for simulation, run, expected_policy in ( - (baseline, baseline_run, None), - (reform, reform_run, policy_id), - ): - assert ( - simulation["simulation_type"], - simulation["policy_id"], - simulation["dynamic_id"], - simulation["household_id"], - simulation["dataset_id"], - simulation["region_id"], - simulation["tax_benefit_model_version_id"], - simulation["year"], - simulation["filter_field"], - ) == ( - "economy", - expected_policy, - None, - None, - population.dataset_id, - population.region_id, - population.tax_benefit_model_version_id, - 2026, - None, - ) - assert ( - run["simulation_id"], - run["year"], - run["tax_benefit_model_version_id"], - run["country_package_version"], - run["policyengine_version"], - run["dataset_id"], - run["region_id"], - run["status"], - run["modal_call_id"], - run["output_uri"], - ) == ( - simulation["id"], - 2026, - population.tax_benefit_model_version_id, - VERSIONS.country_package_version, - VERSIONS.policyengine_version, - population.dataset_id, - population.region_id, - "pending", - None, - None, - ) - - -def test_a_side_reusing_a_succeeded_run_creates_nothing_for_that_side( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - reused = insert_reusable_run(connection, economy) - report = _new_report( - economy, - baseline=ReportSimulation( - simulation_id=reused.simulation_id, - simulation_run_id=reused.simulation_run_id, - choice=CURRENT_LAW, - reused=True, - ), - ) - simulations_before = _count(connection, "simulations") - runs_before = _count(connection, "simulation_runs") - act_as_the_service(connection) - - insert_economy_report(connection, report) - stored_report = _row(connection, "reports", report.id) - stored_run = _row(connection, "report_runs", report.run_id) - simulations_after = _count(connection, "simulations") - runs_after = _count(connection, "simulation_runs") - - assert stored_report["baseline_simulation_id"] == reused.simulation_id - assert stored_run["baseline_simulation_run_id"] == reused.simulation_run_id - assert stored_run["reform_simulation_run_id"] == report.reform.simulation_run_id - assert (simulations_after, runs_after) == ( - simulations_before + 1, - runs_before + 1, - ) - - -@pytest.mark.parametrize( - ("missing_column", "table_name"), - [("region_id", "simulations"), ("dataset_id", "simulations")], -) -def test_a_report_naming_a_missing_row_names_the_column_that_refused_it( - public_api_database_url, missing_column: str, table_name: str -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - report = _new_report(economy) - population = replace(report.population, **{missing_column: uuid4()}) - act_as_the_service(connection) - - with pytest.raises(EconomyRecordReferenceError) as refusal: - insert_economy_report(connection, replace(report, population=population)) - - assert (refusal.value.table_name, refusal.value.column_name) == ( - table_name, - missing_column, - ) - - -def test_a_report_naming_a_missing_dynamic_names_its_column( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - report = _new_report( - economy, reform=_new_side(SimulationChoice(None, dynamic_id=uuid4())) - ) - act_as_the_service(connection) - - with pytest.raises(EconomyRecordReferenceError) as refusal: - insert_economy_report(connection, report) - - assert (refusal.value.table_name, refusal.value.column_name) == ( - "simulations", - "dynamic_id", - ) - - -def test_a_simulation_and_its_run_are_created_pending(public_api_database_url) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - policy_id = insert_policy(connection, economy.catalog) - simulation = NewEconomySimulation( - id=uuid4(), - run_id=uuid4(), - choice=SimulationChoice(policy_id, dynamic_id=None), - population=economy.population(year=2027), - versions=VERSIONS, - ) - act_as_the_service(connection) - - insert_economy_simulation(connection, simulation) - stored = _row(connection, "simulations", simulation.id) - run = _row(connection, "simulation_runs", simulation.run_id) - - assert ( - stored["simulation_type"], - stored["policy_id"], - stored["dynamic_id"], - stored["dataset_id"], - stored["region_id"], - stored["year"], - ) == ("economy", policy_id, None, economy.dataset_id, economy.region_id, 2027) - assert ( - run["simulation_id"], - run["year"], - run["status"], - run["modal_call_id"], - run["policyengine_version"], - ) == (simulation.id, 2027, "pending", None, VERSIONS.policyengine_version) - - -def _created_report(connection: Connection) -> NewEconomyReport: - economy = insert_economy_catalog(connection) - report = _new_report(economy) +def _created_report(connection: Connection) -> PendingReport: + """Insert a pending report and its runs, then act as the service.""" + report = insert_pending_report(connection, insert_economy_catalog(connection)) act_as_the_service(connection) - insert_economy_report(connection, report) return report @@ -443,15 +148,15 @@ def test_a_report_runs_call_is_recorded_once_while_it_is_pending_without_one( report = _created_report(connection) first = record_report_run_call( - connection, report_run_id=report.run_id, modal_call_id="fc-first" + connection, report_run_id=report.report_run_id, modal_call_id="fc-first" ) second = record_report_run_call( - connection, report_run_id=report.run_id, modal_call_id="fc-second" + connection, report_run_id=report.report_run_id, modal_call_id="fc-second" ) unknown = record_report_run_call( connection, report_run_id=uuid4(), modal_call_id="fc-other" ) - call_id = _row(connection, "report_runs", report.run_id)["modal_call_id"] + call_id = _row(connection, "report_runs", report.report_run_id)["modal_call_id"] assert (first, second, unknown) == ( ReportRunStatus.PENDING, @@ -467,21 +172,21 @@ def test_a_claimed_report_run_keeps_its_claim(public_api_database_url) -> None: connection.execute(text("RESET ROLE")) connection.execute( update(report_runs) - .where(report_runs.c.id == report.run_id) + .where(report_runs.c.id == report.report_run_id) .values(status=ReportRunStatus.RUNNING, modal_call_id="fc-coordinator") ) act_as_the_service(connection) status = record_report_run_call( - connection, report_run_id=report.run_id, modal_call_id="fc-late" + connection, report_run_id=report.report_run_id, modal_call_id="fc-late" ) failed = fail_unstarted_report_run( connection, - report_run_id=report.run_id, + report_run_id=report.report_run_id, simulation_run_ids=(report.baseline.simulation_run_id,), error_message="modal_call_refused: Modal refused the call.", ) - stored = _row(connection, "report_runs", report.run_id) + stored = _row(connection, "report_runs", report.report_run_id) baseline = _row( connection, "simulation_runs", report.baseline.simulation_run_id ) @@ -501,7 +206,7 @@ def test_an_unstarted_report_run_and_its_new_simulation_runs_fail_at_once( failed = fail_unstarted_report_run( connection, - report_run_id=report.run_id, + report_run_id=report.report_run_id, simulation_run_ids=( report.baseline.simulation_run_id, report.reform.simulation_run_id, @@ -510,11 +215,11 @@ def test_an_unstarted_report_run_and_its_new_simulation_runs_fail_at_once( ) again = fail_unstarted_report_run( connection, - report_run_id=report.run_id, + report_run_id=report.report_run_id, simulation_run_ids=(), error_message="a second failure", ) - stored = _row(connection, "report_runs", report.run_id) + stored = _row(connection, "report_runs", report.report_run_id) stored_simulation_runs = connection.execute( select( simulation_runs.c.status, @@ -544,45 +249,38 @@ def test_a_simulation_runs_call_is_recorded_or_the_run_failed_while_unstarted( ) -> None: with rolled_back_transaction(public_api_database_url) as connection: economy = insert_economy_catalog(connection) - recorded = NewEconomySimulation( - id=uuid4(), - run_id=uuid4(), - choice=CURRENT_LAW, - population=economy.population(), - versions=VERSIONS, - ) - refused = NewEconomySimulation( - id=uuid4(), - run_id=uuid4(), - choice=CURRENT_LAW, - population=economy.population(), - versions=VERSIONS, + pending = RunShape(status=SimulationStatus.PENDING) + recorded, refused = ( + insert_simulation_run(connection, economy.population(), pending) + for _ in range(2) ) act_as_the_service(connection) - insert_economy_simulation(connection, recorded) - insert_economy_simulation(connection, refused) status = record_simulation_run_call( - connection, simulation_run_id=recorded.run_id, modal_call_id="fc-worker" + connection, + simulation_run_id=recorded.simulation_run_id, + modal_call_id="fc-worker", ) not_failed = fail_unstarted_simulation_run( connection, - simulation_run_id=recorded.run_id, + simulation_run_id=recorded.simulation_run_id, error_message="modal_call_refused: refused", ) failed = fail_unstarted_simulation_run( connection, - simulation_run_id=refused.run_id, + simulation_run_id=refused.simulation_run_id, error_message="modal_call_refused: refused", ) not_recorded = record_simulation_run_call( - connection, simulation_run_id=refused.run_id, modal_call_id="fc-late" + connection, + simulation_run_id=refused.simulation_run_id, + modal_call_id="fc-late", ) unknown = record_simulation_run_call( connection, simulation_run_id=uuid4(), modal_call_id="fc-other" ) - recorded_run = _row(connection, "simulation_runs", recorded.run_id) - refused_run = _row(connection, "simulation_runs", refused.run_id) + recorded_run = _row(connection, "simulation_runs", recorded.simulation_run_id) + refused_run = _row(connection, "simulation_runs", refused.simulation_run_id) assert (status, not_failed, failed, not_recorded, unknown) == ( SimulationStatus.PENDING, diff --git a/libs/policyengine-simulation-persistence/tests/test_economy_run_reuse.py b/libs/policyengine-simulation-persistence/tests/test_economy_run_reuse.py deleted file mode 100644 index 6049c1f5a..000000000 --- a/libs/policyengine-simulation-persistence/tests/test_economy_run_reuse.py +++ /dev/null @@ -1,329 +0,0 @@ -"""Which succeeded simulation run a new report reuses, if any. - -A report reuses the newest run of a simulation with the same policy, dynamic, -catalog version, dataset, region and year, that succeeded on the same country -package and .py versions, whose output file is in the output bucket, and that -a succeeded report run with the same options used. A run any report run -refused to reuse is never offered again. - -These run against a database the Public API's own v2 migration chain built -(``PUBLIC_API_SCHEMA_DATABASE_URL``), as the simulation service's role, each -in a transaction rolled back afterwards. -""" - -from __future__ import annotations - -from dataclasses import dataclass, field, replace -from datetime import timedelta -from typing import TYPE_CHECKING, Any -from uuid import UUID, uuid4 - -import pytest -from conftest import act_as_the_service, rolled_back_transaction -from economy_rows import ( - COMPLETED_AT, - CURRENT_LAW, - OPTIONS, - OUTPUT_BUCKET_PREFIX, - VERSIONS, - EconomyCatalog, - ReportRunOutcome, - RunShape, - StoredRun, - insert_economy_catalog, - insert_report_run_naming, - insert_reusable_run, - insert_simulation_run, -) -from policyengine_simulation_persistence import ( - ReportRunStatus, - ReusableRun, - RunVersions, - SimulationChoice, - SimulationStatus, - find_reusable_run, - find_reusable_run_of, -) -from public_api_rows import insert_dataset, insert_dynamic, insert_policy, insert_region - -if TYPE_CHECKING: - from collections.abc import Callable - - from sqlalchemy import Connection - -pytestmark = pytest.mark.public_api_schema - - -@dataclass(frozen=True) -class Request: - """What a new report's simulation asks for, as the reuse search sees it.""" - - choice: SimulationChoice = CURRENT_LAW - year: int = 2026 - versions: RunVersions = VERSIONS - inputs: dict[str, Any] = field(default_factory=lambda: dict(OPTIONS)) - excluded_run_id: UUID | None = None - - -def _find( - connection: Connection, economy: EconomyCatalog, request: Request | None = None -) -> ReusableRun | None: - request = request or Request() - return find_reusable_run( - connection, - choice=request.choice, - population=economy.population(year=request.year), - versions=request.versions, - inputs=request.inputs, - output_uri_prefix=OUTPUT_BUCKET_PREFIX, - excluded_run_id=request.excluded_run_id, - ) - - -def _reusable(run: StoredRun) -> ReusableRun: - return ReusableRun( - simulation_id=run.simulation_id, simulation_run_id=run.simulation_run_id - ) - - -def test_a_succeeded_run_a_report_used_with_the_same_options_is_reused( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - policy_id = insert_policy(connection, economy.catalog) - dynamic_id = insert_dynamic(connection, economy.catalog) - choice = SimulationChoice(policy_id=policy_id, dynamic_id=dynamic_id) - reused = insert_reusable_run(connection, economy, RunShape(choice=choice)) - act_as_the_service(connection) - - found = _find(connection, economy, Request(choice=choice)) - - assert found == _reusable(reused) - - -def _another_policy(connection: Connection, economy: EconomyCatalog) -> dict: - return { - "choice": SimulationChoice(insert_policy(connection, economy.catalog), None) - } - - -def _another_dynamic(connection: Connection, economy: EconomyCatalog) -> dict: - return { - "choice": SimulationChoice(None, insert_dynamic(connection, economy.catalog)) - } - - -# Each makes the request differ from the stored run in one way. -DIFFERENCES: dict[str, Callable[[Connection, EconomyCatalog], dict[str, Any]]] = { - "another policy": _another_policy, - "another dynamic": _another_dynamic, - "another year": lambda _connection, _economy: {"year": 2027}, - "another .py version": lambda _connection, _economy: { - "versions": replace(VERSIONS, policyengine_version="6.18.0") - }, - "another country package version": lambda _connection, _economy: { - "versions": replace(VERSIONS, country_package_version="1.765.0") - }, - "other options": lambda _connection, _economy: {"inputs": {"include_cliffs": True}}, -} - - -@pytest.mark.parametrize("difference", sorted(DIFFERENCES)) -def test_a_run_differing_from_the_request_is_not_reused( - public_api_database_url, difference: str -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - insert_reusable_run(connection, economy) - request = DIFFERENCES[difference](connection, economy) - act_as_the_service(connection) - - assert _find(connection, economy, Request(**request)) is None - - -def test_a_run_of_another_region_or_dataset_is_not_reused( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - insert_reusable_run(connection, economy) - other_dataset_id = insert_dataset(connection, economy.catalog, "acs_2024") - other_region_id = insert_region( - connection, - economy.catalog, - "state/ca", - default_dataset_id=economy.dataset_id, - region_type="state", - ) - act_as_the_service(connection) - - for other in ( - replace(economy, dataset_id=other_dataset_id), - replace(economy, region_id=other_region_id), - ): - assert _find(connection, other) is None - - -@pytest.mark.parametrize( - "status", - [SimulationStatus.PENDING, SimulationStatus.RUNNING, SimulationStatus.FAILED], -) -def test_an_unfinished_or_failed_run_is_not_reused( - public_api_database_url, status: SimulationStatus -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - insert_reusable_run(connection, economy, RunShape(status=status)) - act_as_the_service(connection) - - assert _find(connection, economy) is None - - -def test_a_forced_run_is_never_reused(public_api_database_url) -> None: - """Answer 3: a forced re-run's run is never named by another report run.""" - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - forced = insert_reusable_run(connection, economy, RunShape(forced=True)) - act_as_the_service(connection) - - by_definition = _find(connection, economy) - by_own_simulation = find_reusable_run_of( - connection, - simulation_id=forced.simulation_id, - population=economy.population(), - versions=VERSIONS, - inputs=OPTIONS, - output_uri_prefix=OUTPUT_BUCKET_PREFIX, - ) - - assert by_definition is None - assert by_own_simulation is None - - -def test_a_run_whose_file_is_outside_the_output_bucket_is_not_reused( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - insert_reusable_run( - connection, - economy, - RunShape( - output_uri="gs://policyengine-stage12-staging-artifacts/runs/output.parquet" - ), - ) - # A bucket whose name starts with the output bucket's is another bucket. - insert_reusable_run( - connection, - economy, - RunShape( - output_uri=OUTPUT_BUCKET_PREFIX.rstrip("/") + "-old/output.parquet" - ), - ) - act_as_the_service(connection) - - assert _find(connection, economy) is None - - -@pytest.mark.parametrize( - "report_run_status", - [ReportRunStatus.PENDING, ReportRunStatus.RUNNING, ReportRunStatus.FAILED], -) -def test_a_run_no_succeeded_report_run_used_is_not_reused( - public_api_database_url, report_run_status: ReportRunStatus -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - population = economy.population() - stored = insert_simulation_run(connection, population) - other_side = insert_simulation_run( - connection, population, RunShape(status=SimulationStatus.FAILED) - ) - insert_report_run_naming( - connection, - economy, - (stored, other_side), - ReportRunOutcome(status=report_run_status), - ) - # A run no report run names at all, such as a simulation's own. - insert_simulation_run(connection, population) - act_as_the_service(connection) - - assert _find(connection, economy) is None - - -@pytest.mark.parametrize("refused_as", ["baseline", "reform"]) -def test_a_run_a_report_run_refused_to_reuse_is_not_reused_again( - public_api_database_url, refused_as: str -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - reused = insert_reusable_run(connection, economy) - other_side = insert_simulation_run( - connection, economy.population(), RunShape(status=SimulationStatus.FAILED) - ) - baseline, reform = ( - (reused, other_side) if refused_as == "baseline" else (other_side, reused) - ) - insert_report_run_naming( - connection, - economy, - (baseline, reform), - ReportRunOutcome( - status=ReportRunStatus.FAILED, - error_message=( - "reused_simulation_output_incompatible: a planned column is missing" - ), - ), - ) - act_as_the_service(connection) - - assert _find(connection, economy) is None - - -def test_a_run_whose_report_run_failed_for_another_reason_is_still_reused( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - reused = insert_reusable_run(connection, economy) - other_side = insert_simulation_run( - connection, economy.population(), RunShape(status=SimulationStatus.FAILED) - ) - insert_report_run_naming( - connection, - economy, - (reused, other_side), - ReportRunOutcome( - status=ReportRunStatus.FAILED, - error_message=( - "simulation_failed: the reform's worker ran out of memory" - ), - ), - ) - act_as_the_service(connection) - - assert _find(connection, economy) == _reusable(reused) - - -def test_the_newest_matching_run_is_reused_unless_excluded( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - older = insert_reusable_run(connection, economy) - newer = insert_reusable_run( - connection, economy, RunShape(completed_at=COMPLETED_AT + timedelta(days=1)) - ) - act_as_the_service(connection) - - newest = _find(connection, economy) - without_newest = _find( - connection, economy, Request(excluded_run_id=newer.simulation_run_id) - ) - without_any = _find(connection, economy, Request(excluded_run_id=uuid4())) - - assert newest == _reusable(newer) - assert without_newest == _reusable(older) - assert without_any == _reusable(newer) diff --git a/libs/policyengine-simulation-persistence/tests/test_economy_run_store.py b/libs/policyengine-simulation-persistence/tests/test_economy_run_store.py index 3c0156ebb..8e61562f5 100644 --- a/libs/policyengine-simulation-persistence/tests/test_economy_run_store.py +++ b/libs/policyengine-simulation-persistence/tests/test_economy_run_store.py @@ -18,12 +18,12 @@ from conftest import act_as_the_service, rolled_back_transaction from economy_rows import ( CURRENT_LAW, - OPTIONS, - OUTPUT_BUCKET_PREFIX, POLICYENGINE_VERSION, VERSIONS, + RunShape, insert_economy_catalog, - insert_reusable_run, + insert_pending_report, + insert_simulation_run, ) from policyengine_simulation_persistence import ( CatalogDataset, @@ -34,14 +34,11 @@ EconomyRequest, EconomyRunStore, MissingPolicyParametersError, - NewEconomyReport, - NewEconomySimulation, + NewReportRerun, OverlappingReformError, ReportRunStatus, ReportSideError, - ReportSimulation, - ReusableRun, - RunReuse, + RerunSide, SideRefusal, SimulationChoice, SimulationStatus, @@ -74,9 +71,6 @@ dataset_name=None, year=2026, ) -REUSE = RunReuse( - versions=VERSIONS, inputs=OPTIONS, output_uri_prefix=OUTPUT_BUCKET_PREFIX -) def _store_on(connection: Connection) -> EconomyRunStore: @@ -101,16 +95,11 @@ def test_a_reports_inputs_are_read_together(public_api_database_url) -> None: dynamic_id = insert_dynamic( connection, economy.catalog, [StoredValue(ELASTICITY, -0.05, START)] ) - reused = insert_reusable_run(connection, economy) act_as_the_service(connection) - store = _store_on(connection) reform_choice = SimulationChoice(policy_id=policy_id, dynamic_id=dynamic_id) - inputs = store.read_report_inputs( - REQUEST, baseline=CURRENT_LAW, reform=reform_choice, reuse=REUSE - ) - without_reuse = store.read_report_inputs( - REQUEST, baseline=CURRENT_LAW, reform=reform_choice, reuse=None + inputs = _store_on(connection).read_report_inputs( + REQUEST, baseline=CURRENT_LAW, reform=reform_choice ) catalog = inputs.catalog @@ -134,27 +123,6 @@ def test_a_reports_inputs_are_read_together(public_api_database_url) -> None: CTC_AMOUNT: {"2026-01-01.2200-12-31": 3_000}, ELASTICITY: {"2026-01-01.2200-12-31": -0.05}, } - assert inputs.baseline.reusable_run == ReusableRun( - simulation_id=reused.simulation_id, simulation_run_id=reused.simulation_run_id - ) - assert inputs.reform.reusable_run is None - assert without_reuse.baseline.reusable_run is None - - -def test_the_two_sides_never_reuse_the_same_run(public_api_database_url) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - only = insert_reusable_run(connection, economy) - act_as_the_service(connection) - - inputs = _store_on(connection).read_report_inputs( - REQUEST, baseline=CURRENT_LAW, reform=CURRENT_LAW, reuse=REUSE - ) - - assert inputs.baseline.reusable_run == ReusableRun( - simulation_id=only.simulation_id, simulation_run_id=only.simulation_run_id - ) - assert inputs.reform.reusable_run is None def test_a_named_dataset_is_read_instead_of_the_regions_default( @@ -175,7 +143,6 @@ def test_a_named_dataset_is_read_instead_of_the_regions_default( "acs_local_2024", ) assert (inputs.simulation.choice, inputs.simulation.reform) == (CURRENT_LAW, {}) - assert inputs.simulation.reusable_run is None @pytest.mark.parametrize( @@ -209,9 +176,7 @@ def test_each_missing_row_a_request_names_is_refused( # A report names the side whose policy or dynamic is missing. side_refused = missing in {"policy", "dynamic"} with pytest.raises(ReportSideError if side_refused else error_type) as raised: - store.read_report_inputs( - request, baseline=CURRENT_LAW, reform=choice, reuse=REUSE - ) + store.read_report_inputs(request, baseline=CURRENT_LAW, reform=choice) with pytest.raises(error_type): store.read_simulation_inputs(request, choice) @@ -265,39 +230,25 @@ def _status(connection: Connection, table_name: str, run_id) -> tuple: ) -def test_a_report_is_created_then_its_call_recorded_or_its_run_failed( +def test_a_reports_call_is_recorded_or_its_run_failed( public_api_database_url, ) -> None: with rolled_back_transaction(public_api_database_url) as connection: economy = insert_economy_catalog(connection) + recorded, refused = ( + insert_pending_report(connection, economy) for _ in range(2) + ) act_as_the_service(connection) store = _store_on(connection) - reports = [ - NewEconomyReport( - id=uuid4(), - run_id=uuid4(), - country="us", - label=f"Report {number}", - description=None, - tax_benefit_model_id=economy.catalog.model_id, - population=economy.population(), - versions=VERSIONS, - inputs=OPTIONS, - baseline=ReportSimulation(uuid4(), uuid4(), CURRENT_LAW, reused=False), - reform=ReportSimulation(uuid4(), uuid4(), CURRENT_LAW, reused=False), - ) - for number in (1, 2) - ] - for report in reports: - store.create_report(report) - recorded, refused = reports - status = store.record_report_call(recorded.run_id, "fc-coordinator") + status = store.record_report_call(recorded.report_run_id, "fc-coordinator") failed = store.fail_unstarted_report( - refused.run_id, refused.new_simulation_run_ids, "modal_call_refused: no" + refused.report_run_id, + refused.simulation_run_ids, + "modal_call_refused: no", ) - recorded_state = _status(connection, "report_runs", recorded.run_id) - refused_state = _status(connection, "report_runs", refused.run_id) + recorded_state = _status(connection, "report_runs", recorded.report_run_id) + refused_state = _status(connection, "report_runs", refused.report_run_id) refused_baseline = _status( connection, "simulation_runs", refused.baseline.simulation_run_id ) @@ -308,33 +259,29 @@ def test_a_report_is_created_then_its_call_recorded_or_its_run_failed( assert refused_baseline == ("failed", None) -def test_a_simulation_is_created_then_its_call_recorded_or_its_run_failed( +def test_a_simulations_call_is_recorded_or_its_run_failed( public_api_database_url, ) -> None: with rolled_back_transaction(public_api_database_url) as connection: economy = insert_economy_catalog(connection) + pending = RunShape(status=SimulationStatus.PENDING) + recorded, refused = ( + insert_simulation_run(connection, economy.population(), pending) + for _ in range(2) + ) act_as_the_service(connection) store = _store_on(connection) - simulations = [ - NewEconomySimulation( - id=uuid4(), - run_id=uuid4(), - choice=CURRENT_LAW, - population=economy.population(), - versions=VERSIONS, - ) - for _ in range(2) - ] - for simulation in simulations: - store.create_simulation(simulation) - recorded, refused = simulations - status = store.record_simulation_call(recorded.run_id, "fc-worker") + status = store.record_simulation_call(recorded.simulation_run_id, "fc-worker") failed = store.fail_unstarted_simulation( - refused.run_id, "modal_call_refused: no" + refused.simulation_run_id, "modal_call_refused: no" + ) + recorded_state = _status( + connection, "simulation_runs", recorded.simulation_run_id + ) + refused_state = _status( + connection, "simulation_runs", refused.simulation_run_id ) - recorded_state = _status(connection, "simulation_runs", recorded.run_id) - refused_state = _status(connection, "simulation_runs", refused.run_id) assert (status, failed) == (SimulationStatus.PENDING, True) assert recorded_state == ("pending", "fc-worker") @@ -345,26 +292,12 @@ def test_a_report_and_a_simulation_are_read_as_they_stand( public_api_database_url, ) -> None: with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) + report = insert_pending_report(connection, insert_economy_catalog(connection)) act_as_the_service(connection) store = _store_on(connection) - report = NewEconomyReport( - id=uuid4(), - run_id=uuid4(), - country="us", - label="Read back", - description=None, - tax_benefit_model_id=economy.catalog.model_id, - population=economy.population(), - versions=VERSIONS, - inputs=OPTIONS, - baseline=ReportSimulation(uuid4(), uuid4(), CURRENT_LAW, reused=False), - reform=ReportSimulation(uuid4(), uuid4(), CURRENT_LAW, reused=False), - ) - store.create_report(report) - store.record_report_call(report.run_id, "fc-coordinator") + store.record_report_call(report.report_run_id, "fc-coordinator") - stored_report = store.read_report(report.id) + stored_report = store.read_report(report.report_id) stored_simulation = store.read_simulation(report.baseline.simulation_id) missing = (store.read_report(uuid4()), store.read_simulation(uuid4())) @@ -372,7 +305,7 @@ def test_a_report_and_a_simulation_are_read_as_they_stand( assert stored_report is not None assert stored_report.run is not None assert (stored_report.run.id, stored_report.run.status) == ( - report.run_id, + report.report_run_id, ReportRunStatus.PENDING, ) assert stored_simulation is not None @@ -422,7 +355,6 @@ def test_a_reports_refused_values_name_the_side_they_belong_to( REQUEST, baseline=choices["baseline"], reform=choices["reform"], - reuse=None, ) assert raised.value.side == side @@ -452,14 +384,11 @@ def execute(self, statement, *arguments): self.statements.append(statement) -def _new_report() -> NewEconomyReport: - return NewEconomyReport( - id=uuid4(), +def _forced_rerun() -> NewReportRerun: + """Return a forced report re-run, whose create only inserts rows.""" + return NewReportRerun( + report_id=uuid4(), run_id=uuid4(), - country="us", - label="Lost commit", - description=None, - tax_benefit_model_id=uuid4(), population=EconomyPopulation( tax_benefit_model_version_id=uuid4(), region_id=uuid4(), @@ -467,29 +396,18 @@ def _new_report() -> NewEconomyReport: year=2026, ), versions=VERSIONS, - inputs=OPTIONS, - baseline=ReportSimulation(uuid4(), uuid4(), CURRENT_LAW, reused=False), - reform=ReportSimulation(uuid4(), uuid4(), CURRENT_LAW, reused=False), + baseline=RerunSide(simulation_id=uuid4(), simulation_run_id=uuid4()), + reform=RerunSide(simulation_id=uuid4(), simulation_run_id=uuid4()), + forced=True, ) def test_a_create_whose_commit_fails_may_have_been_written() -> None: connection = _RecordingConnection() store = EconomyRunStore(lambda: _CommitFails(connection)) - report = _new_report() with pytest.raises(EconomyRecordOutcomeUnknownError) as raised: - store.create_report(report) - with pytest.raises(EconomyRecordOutcomeUnknownError): - store.create_simulation( - NewEconomySimulation( - id=uuid4(), - run_id=uuid4(), - choice=CURRENT_LAW, - population=report.population, - versions=VERSIONS, - ) - ) + store.create_report_rerun(_forced_rerun()) assert isinstance(raised.value.__cause__, OperationalError) assert connection.statements @@ -511,7 +429,7 @@ def test_a_create_whose_statement_fails_is_not_written() -> None: store = EconomyRunStore(_StatementFails) with pytest.raises(OperationalError): - store.create_report(_new_report()) + store.create_report_rerun(_forced_rerun()) def test_every_side_refusal_is_raised_with_its_side(monkeypatch) -> None: @@ -544,7 +462,5 @@ def refuse(*_, refusal=refusal): monkeypatch.setattr(economy_inputs, "read_simulation_values", refuse) with pytest.raises(ReportSideError) as raised: - store.read_report_inputs( - REQUEST, baseline=CURRENT_LAW, reform=CURRENT_LAW, reuse=None - ) + store.read_report_inputs(REQUEST, baseline=CURRENT_LAW, reform=CURRENT_LAW) assert (raised.value.side, raised.value.error) == ("baseline", refusal) diff --git a/libs/policyengine-simulation-persistence/tests/test_economy_window_reruns.py b/libs/policyengine-simulation-persistence/tests/test_economy_window_reruns.py index 2cb13f189..6519323f2 100644 --- a/libs/policyengine-simulation-persistence/tests/test_economy_window_reruns.py +++ b/libs/policyengine-simulation-persistence/tests/test_economy_window_reruns.py @@ -3,9 +3,8 @@ A budget window's re-run is a new budget-window run of the report, with a report run per year. Each year's sides are the simulations that year's report run of the report's newest window run names, which may belong to -another report when that year reused a run. A side may reuse a run of its -own simulation that succeeded on the current versions. These run against a -database the Public API's own v2 migration chain built +another report when that year joined another report's run. These run +against a database the Public API's own v2 migration chain built (``PUBLIC_API_SCHEMA_DATABASE_URL``), as the simulation service's role, in a transaction rolled back afterwards. """ @@ -23,18 +22,16 @@ CURRENT_LAW, LATER_POLICYENGINE_VERSION, LATER_VERSIONS, - OPTIONS, - OUTPUT_BUCKET_PREFIX, VERSIONS, EconomyCatalog, RunShape, + StoredRun, insert_economy_catalog, insert_later_catalog, - insert_report_run_of, - insert_run_of, + insert_report_of, + insert_simulation_run, ) from policyengine_simulation_persistence import ( - BudgetWindowYear, EconomyPopulation, EconomyRecordReferenceError, EconomyReportDefinition, @@ -43,24 +40,22 @@ FoundBudgetWindow, KeyedBudgetWindow, KeyedWindowYear, - NewBudgetWindowReport, NewWindowRerun, ProposedSide, ReportRunStatus, - ReportSimulation, + ReportRunTrigger, RerunRecordDeletedError, RerunSide, - ReusableRun, - RunReuse, SimulationStatus, WindowRerunYear, WindowYearDefinition, + budget_window_runs, find_or_create_budget_window, - insert_budget_window_report, insert_window_rerun, read_report_definition, + report_runs, ) -from sqlalchemy import text +from sqlalchemy import insert, text if TYPE_CHECKING: from collections.abc import Iterator @@ -77,9 +72,16 @@ dataset_name="populace_us_2024", year=2026, ) -LATER_REUSE = RunReuse( - versions=LATER_VERSIONS, inputs=OPTIONS, output_uri_prefix=OUTPUT_BUCKET_PREFIX -) + + +@dataclass(frozen=True) +class StoredWindowYear: + """A year of a stored window: its report run and each side's simulation run.""" + + year: int + report_run_id: UUID + baseline: StoredRun + reform: StoredRun @dataclass(frozen=True) @@ -88,16 +90,71 @@ class StoredWindow: report_id: UUID window_run_id: UUID - years: tuple[BudgetWindowYear, ...] + years: tuple[StoredWindowYear, ...] -def _new_side() -> ReportSimulation: - return ReportSimulation( - simulation_id=uuid4(), - simulation_run_id=uuid4(), - choice=CURRENT_LAW, - reused=False, +def _insert_window_rows( + connection: Connection, economy: EconomyCatalog +) -> StoredWindow: + """Insert a window with no key, as the service created one before keys. + + Each year has its own current-law simulations, with a pending run each; + the report names its first year's; its window run and each year's report + run are pending, with trigger ``initial``. + """ + pending = RunShape(status=SimulationStatus.PENDING) + years = tuple( + StoredWindowYear( + year=year, + report_run_id=uuid4(), + baseline=insert_simulation_run( + connection, economy.population(year=year), pending + ), + reform=insert_simulation_run( + connection, economy.population(year=year), pending + ), + ) + for year in WINDOW_YEARS + ) + first_year = years[0] + report_id = insert_report_of( + connection, + economy, + (first_year.baseline, first_year.reform), + budget_window_size=len(years), ) + window_run_id = uuid4() + connection.execute( + insert(budget_window_runs).values( + id=window_run_id, + report_id=report_id, + tax_benefit_model_version_id=economy.catalog.version_id, + trigger=ReportRunTrigger.INITIAL, + recalculate=False, + status=ReportRunStatus.PENDING, + ) + ) + connection.execute( + insert(report_runs), + [ + { + "id": window_year.report_run_id, + "report_id": report_id, + "baseline_simulation_run_id": window_year.baseline.simulation_run_id, + "reform_simulation_run_id": window_year.reform.simulation_run_id, + "budget_window_run_id": window_run_id, + "year": window_year.year, + "country_package_version": VERSIONS.country_package_version, + "policyengine_version": VERSIONS.policyengine_version, + "status": ReportRunStatus.PENDING, + "trigger": ReportRunTrigger.INITIAL, + "modal_call_id": None, + "forced": False, + } + for window_year in years + ], + ) + return StoredWindow(report_id=report_id, window_run_id=window_run_id, years=years) def _stored_window( @@ -106,29 +163,8 @@ def _stored_window( *, window_status: ReportRunStatus = ReportRunStatus.SUCCEEDED, ) -> StoredWindow: - """Insert a budget-window report as the service would, then end its runs.""" - years = tuple( - BudgetWindowYear( - year=year, - report_run_id=uuid4(), - baseline=_new_side(), - reform=_new_side(), - ) - for year in WINDOW_YEARS - ) - report = NewBudgetWindowReport( - id=uuid4(), - window_run_id=uuid4(), - country="us", - label="Ten-year cost", - description=None, - tax_benefit_model_id=economy.catalog.model_id, - population=economy.population(year=WINDOW_YEARS[0]), - versions=VERSIONS, - inputs=OPTIONS, - years=years, - ) - insert_budget_window_report(connection, report) + """Insert a window as the service created one before keys, then end its runs.""" + report = _insert_window_rows(connection, economy) # Earlier than anything else the test's transaction creates, whose rows # all get the transaction's time. for table, condition in ( @@ -167,9 +203,7 @@ def _stored_window( "window_run_id": report.window_run_id, }, ) - return StoredWindow( - report_id=report.id, window_run_id=report.window_run_id, years=years - ) + return report def _store_on(connection: Connection) -> EconomyRunStore: @@ -207,12 +241,10 @@ def _window_rerun_of( baseline=RerunSide( simulation_id=window_year.baseline.simulation_id, simulation_run_id=uuid4(), - reused=False, ), reform=RerunSide( simulation_id=window_year.reform.simulation_id, simulation_run_id=uuid4(), - reused=False, ), ) for window_year in stored.years @@ -262,7 +294,6 @@ def test_a_definitions_years_come_from_the_newest_window_run( baseline=RerunSide( simulation_id=other_2027.baseline.simulation_id, simulation_run_id=uuid4(), - reused=False, ), ) act_as_the_service(connection) @@ -414,12 +445,10 @@ def _rerun_of_keyed( baseline=RerunSide( simulation_id=year.baseline.simulation_id, simulation_run_id=uuid4(), - reused=False, ), reform=RerunSide( simulation_id=year.reform.simulation_id, simulation_run_id=uuid4(), - reused=False, ), ) for year in found.years @@ -479,72 +508,25 @@ def test_an_unforced_window_rerun_after_a_failed_run_names_its_years_runs( assert again.baseline.run_inserted is False -def test_a_window_reruns_inputs_name_each_years_own_reusable_runs( +def test_a_window_reruns_inputs_are_read_on_the_current_catalog_version( public_api_database_url, ) -> None: with rolled_back_transaction(public_api_database_url) as connection: economy = insert_economy_catalog(connection) later = insert_later_catalog(connection, economy) - stored = _stored_window(connection, economy) - later_shape = RunShape(versions=LATER_VERSIONS) - reused_2027 = insert_run_of( - connection, - stored.years[1].baseline.simulation_id, - later.population(year=2027), - later_shape, - ) - failed_reform_2027 = insert_run_of( - connection, - stored.years[1].reform.simulation_id, - later.population(year=2027), - replace(later_shape, status=SimulationStatus.FAILED), - ) - insert_report_run_of( - connection, stored.report_id, (reused_2027, failed_reform_2027) - ) + _stored_window(connection, economy) act_as_the_service(connection) - years = _definition_years(stored) inputs = _store_on(connection).read_window_rerun_inputs( - LATER_REQUEST, - baseline=CURRENT_LAW, - reform=CURRENT_LAW, - years=years, - reuse=LATER_REUSE, - ) - forced = _store_on(connection).read_window_rerun_inputs( - LATER_REQUEST, - baseline=CURRENT_LAW, - reform=CURRENT_LAW, - years=years, - reuse=None, + LATER_REQUEST, baseline=CURRENT_LAW, reform=CURRENT_LAW ) - assert inputs.catalog.catalog_version_id == later.catalog.version_id - assert inputs.reusable_runs == { - 2026: (None, None), - 2027: ( - ReusableRun( - simulation_id=stored.years[1].baseline.simulation_id, - simulation_run_id=reused_2027.simulation_run_id, - ), - None, - ), - 2028: (None, None), - } - assert forced.reusable_runs == dict.fromkeys(WINDOW_YEARS, (None, None)) - - -def _definition_years(stored: StoredWindow) -> tuple[WindowYearDefinition, ...]: - """Return the stored window's years as its definition names them.""" - return tuple( - WindowYearDefinition( - year=window_year.year, - baseline_simulation_id=window_year.baseline.simulation_id, - reform_simulation_id=window_year.reform.simulation_id, - ) - for window_year in stored.years + assert (inputs.catalog.catalog_version_id, inputs.catalog.region.id) == ( + later.catalog.version_id, + later.region_id, ) + assert inputs.catalog.dataset.id == later.dataset_id + assert (inputs.baseline.choice, inputs.reform.choice) == (CURRENT_LAW, CURRENT_LAW) def test_a_window_rerun_is_created_through_the_store(public_api_database_url) -> None: @@ -566,45 +548,13 @@ def test_a_window_rerun_is_created_through_the_store(public_api_database_url) -> assert joined.window_run.id == rerun.window_run_id -def test_a_window_rerun_names_no_reused_side() -> None: - """Each year's sides join or create their runs, as a report re-run's do.""" - reused = RerunSide(simulation_id=uuid4(), simulation_run_id=uuid4(), reused=True) - created = RerunSide(simulation_id=uuid4(), simulation_run_id=uuid4(), reused=False) - - with pytest.raises(ValueError, match="reusing none"): - NewWindowRerun( - report_id=uuid4(), - window_run_id=uuid4(), - population=EconomyPopulation( - tax_benefit_model_version_id=uuid4(), - region_id=uuid4(), - dataset_id=uuid4(), - year=WINDOW_YEARS[0], - ), - versions=VERSIONS, - recalculate=False, - years=( - WindowRerunYear( - year=WINDOW_YEARS[0], - report_run_id=uuid4(), - baseline=reused, - reform=created, - ), - ), - ) - - def test_a_window_rerun_refuses_years_that_are_not_its_window() -> None: """Its years run one at a time from its first year, at most 75 of them.""" year = WindowRerunYear( year=2026, report_run_id=uuid4(), - baseline=RerunSide( - simulation_id=uuid4(), simulation_run_id=uuid4(), reused=False - ), - reform=RerunSide( - simulation_id=uuid4(), simulation_run_id=uuid4(), reused=False - ), + baseline=RerunSide(simulation_id=uuid4(), simulation_run_id=uuid4()), + reform=RerunSide(simulation_id=uuid4(), simulation_run_id=uuid4()), ) rerun = NewWindowRerun( report_id=uuid4(), @@ -662,9 +612,7 @@ def test_a_window_rerun_of_a_deleted_report_or_year_simulation_is_refused( rerun = _window_rerun_of(stored, later) gone_year = replace( rerun.years[2], - reform=RerunSide( - simulation_id=uuid4(), simulation_run_id=uuid4(), reused=False - ), + reform=RerunSide(simulation_id=uuid4(), simulation_run_id=uuid4()), ) with pytest.raises(RerunRecordDeletedError), connection.begin_nested(): diff --git a/libs/policyengine-simulation-persistence/tests/test_household_report_reruns.py b/libs/policyengine-simulation-persistence/tests/test_household_report_reruns.py index 03548be41..9a33414c0 100644 --- a/libs/policyengine-simulation-persistence/tests/test_household_report_reruns.py +++ b/libs/policyengine-simulation-persistence/tests/test_household_report_reruns.py @@ -17,13 +17,12 @@ import pytest from conftest import act_as_the_service, rolled_back_transaction +from household_report_rows import insert_household_report_rows from policyengine_simulation_persistence import ( EconomyRecordOutcomeUnknownError, EconomyRecordReferenceError, - HouseholdReportSimulation, HouseholdReportStore, HouseholdRerunSide, - NewHouseholdReport, NewHouseholdReportRerun, ReportRunStatus, RerunRecordDeletedError, @@ -32,7 +31,6 @@ SimulationChoice, SimulationStatus, fail_unstarted_report_run, - insert_household_report, read_report_definition, ) from public_api_rows import ( @@ -48,6 +46,7 @@ from collections.abc import Iterator from uuid import UUID + from household_report_rows import StoredHouseholdReport from public_api_rows import CatalogVersion from sqlalchemy import Connection @@ -108,41 +107,23 @@ def _stored_report( *, with_reform: bool = True, first_run_ended: bool = True, -) -> NewHouseholdReport: +) -> StoredHouseholdReport: """Insert a household report and its first runs, as the superuser. With ``first_run_ended``, its first run failed before its call started, so the report has no run under way. """ - household_id = insert_household( - connection, country="us", default_year=2026, document=DOCUMENT - ) - report = NewHouseholdReport( - id=uuid4(), - run_id=uuid4(), - country="us", - label="A larger child tax credit", - description="For one family", - tax_benefit_model_id=catalog.model_id, - tax_benefit_model_version_id=catalog.version_id, - household_id=household_id, + report = insert_household_report_rows( + connection, + catalog, + household_id=insert_household( + connection, country="us", default_year=2026, document=DOCUMENT + ), year=REPORT_YEAR, versions=VERSIONS, - inputs={}, - baseline=HouseholdReportSimulation( - simulation_id=uuid4(), simulation_run_id=uuid4(), policy_id=None - ), - reform=( - HouseholdReportSimulation( - simulation_id=uuid4(), - simulation_run_id=uuid4(), - policy_id=insert_policy(connection, catalog), - ) - if with_reform - else None - ), + reform_policy_id=insert_policy(connection, catalog) if with_reform else None, + with_reform=with_reform, ) - insert_household_report(connection, report) if first_run_ended: fail_unstarted_report_run( connection, @@ -153,7 +134,7 @@ def _stored_report( return report -def _succeed_first_runs(connection: Connection, report: NewHouseholdReport) -> None: +def _succeed_first_runs(connection: Connection, report: StoredHouseholdReport) -> None: """Mark the report's first run and its side runs succeeded, as the superuser.""" for table_name, run_ids in ( ("report_runs", [report.run_id]), @@ -169,7 +150,7 @@ def _succeed_first_runs(connection: Connection, report: NewHouseholdReport) -> N def _rerun_of( - report: NewHouseholdReport, catalog: CatalogVersion + report: StoredHouseholdReport, catalog: CatalogVersion ) -> NewHouseholdReportRerun: """Return the report's re-run on ``catalog``: a new run of each side's own. diff --git a/libs/policyengine-simulation-persistence/tests/test_household_report_store.py b/libs/policyengine-simulation-persistence/tests/test_household_report_store.py index b7a138465..1b34bda4c 100644 --- a/libs/policyengine-simulation-persistence/tests/test_household_report_store.py +++ b/libs/policyengine-simulation-persistence/tests/test_household_report_store.py @@ -16,13 +16,10 @@ import pytest from conftest import act_as_the_service, rolled_back_transaction +from household_report_rows import insert_household_report_rows from policyengine_simulation_persistence import ( - EconomyRecordOutcomeUnknownError, - EconomyRecordReferenceError, - HouseholdReportSimulation, HouseholdReportStore, MissingPolicyParametersError, - NewHouseholdReport, ReportRunStatus, ReportSideError, RunVersions, @@ -38,7 +35,6 @@ insert_policy, ) from sqlalchemy import text -from sqlalchemy.exc import OperationalError if TYPE_CHECKING: from collections.abc import Iterator @@ -65,8 +61,6 @@ ], "household": [{"id": "your household", "values": {}}], } -# A household report has no options: it always adds missing variables. -OPTIONS: dict[str, object] = {} def _store_on(connection: Connection) -> HouseholdReportStore: @@ -96,40 +90,6 @@ def _household(connection: Connection, *, default_year: int | None = 2026) -> UU ) -def _new_report( - catalog: CatalogVersion, - household_id: UUID, - *, - reform_policy_id: UUID | None = None, - with_reform: bool = True, -) -> NewHouseholdReport: - return NewHouseholdReport( - id=uuid4(), - run_id=uuid4(), - country="us", - label="A larger child tax credit", - description="For one family", - tax_benefit_model_id=catalog.model_id, - tax_benefit_model_version_id=catalog.version_id, - household_id=household_id, - year=2026, - versions=VERSIONS, - inputs=OPTIONS, - baseline=HouseholdReportSimulation( - simulation_id=uuid4(), simulation_run_id=uuid4(), policy_id=None - ), - reform=( - HouseholdReportSimulation( - simulation_id=uuid4(), - simulation_run_id=uuid4(), - policy_id=reform_policy_id, - ) - if with_reform - else None - ), - ) - - def _row(connection: Connection, table_name: str, row_id: UUID) -> dict: return dict( connection.execute( @@ -236,127 +196,24 @@ def test_a_policy_setting_a_parameter_the_version_lacks_is_refused_on_its_side( assert refused.value.error.parameter_names == ("gov.removed.parameter",) -def test_a_household_report_is_created_with_its_simulations_and_pending_runs( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - catalog = _catalog(connection) - household_id = _household(connection) - policy_id = insert_policy(connection, catalog) - act_as_the_service(connection) - report = _new_report(catalog, household_id, reform_policy_id=policy_id) - assert report.reform is not None - - _store_on(connection).create_report(report) - stored_report = _row(connection, "reports", report.id) - stored_run = _row(connection, "report_runs", report.run_id) - simulations = [ - _row(connection, "simulations", side.simulation_id) - for side in (report.baseline, report.reform) - ] - simulation_runs = [ - _row(connection, "simulation_runs", side.simulation_run_id) - for side in (report.baseline, report.reform) - ] - - assert report.simulation_run_ids == ( - report.baseline.simulation_run_id, - report.reform.simulation_run_id, - ) - assert { - key: stored_report[key] - for key in ( - "label", - "description", - "country", - "type", - "tax_benefit_model_id", - "policy_id", - "baseline_simulation_id", - "reform_simulation_id", - "household_id", - "dataset_id", - "region_id", - "year", - "budget_window_size", - "inputs", - ) - } == { - "label": "A larger child tax credit", - "description": "For one family", - "country": "us", - "type": "household", - "tax_benefit_model_id": catalog.model_id, - "policy_id": policy_id, - "baseline_simulation_id": report.baseline.simulation_id, - "reform_simulation_id": report.reform.simulation_id, - "household_id": household_id, - "dataset_id": None, - "region_id": None, - "year": 2026, - "budget_window_size": None, - "inputs": OPTIONS, - } - assert (stored_run["status"], stored_run["trigger"]) == ("pending", "initial") - assert stored_run["modal_call_id"] is None - assert ( - stored_run["baseline_simulation_run_id"], - stored_run["reform_simulation_run_id"], - ) == report.simulation_run_ids - assert ( - stored_run["country_package_version"], - stored_run["policyengine_version"], - ) == ("1.764.6", POLICYENGINE_VERSION) - assert [simulation["simulation_type"] for simulation in simulations] == [ - "household", - "household", - ] - assert [simulation["policy_id"] for simulation in simulations] == [ - None, - policy_id, - ] - for simulation in simulations: - assert simulation["household_id"] == household_id - assert simulation["tax_benefit_model_version_id"] == catalog.version_id - assert (simulation["dataset_id"], simulation["region_id"]) == (None, None) - assert (simulation["dynamic_id"], simulation["year"]) == (None, 2026) - for run in simulation_runs: - assert (run["status"], run["modal_call_id"]) == ("pending", None) - assert (run["dataset_id"], run["region_id"], run["year"]) == (None, None, 2026) - assert run["household_result"] is None - assert run["policyengine_version"] == POLICYENGINE_VERSION - - -def test_a_report_of_the_baseline_alone_names_no_reform( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - catalog = _catalog(connection) - household_id = _household(connection) - act_as_the_service(connection) - report = _new_report(catalog, household_id, with_reform=False) - - _store_on(connection).create_report(report) - stored_report = _row(connection, "reports", report.id) - stored_run = _row(connection, "report_runs", report.run_id) - - assert report.simulation_run_ids == (report.baseline.simulation_run_id,) - assert stored_report["reform_simulation_id"] is None - assert stored_report["policy_id"] is None - assert stored_run["reform_simulation_run_id"] is None - - def test_a_report_run_records_its_call_or_is_failed_with_its_runs( public_api_database_url, ) -> None: with rolled_back_transaction(public_api_database_url) as connection: catalog = _catalog(connection) household_id = _household(connection) + recorded, refused = ( + insert_household_report_rows( + connection, + catalog, + household_id=household_id, + year=2026, + versions=VERSIONS, + ) + for _ in range(2) + ) act_as_the_service(connection) store = _store_on(connection) - recorded, refused = (_new_report(catalog, household_id) for _ in range(2)) - store.create_report(recorded) - store.create_report(refused) status = store.record_report_call(recorded.run_id, "fc-household") recorded_not_failed = store.fail_unstarted_report( @@ -386,67 +243,3 @@ def test_a_report_run_records_its_call_or_is_failed_with_its_runs( "modal_call_refused: no", ) assert [run["status"] for run in refused_simulation_runs] == ["failed", "failed"] - - -def test_a_report_naming_a_missing_household_is_refused_by_its_key( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - catalog = _catalog(connection) - act_as_the_service(connection) - - with pytest.raises(EconomyRecordReferenceError) as refused: - _store_on(connection).create_report(_new_report(catalog, uuid4())) - - assert refused.value.column_name == "household_id" - - -class _CommitFails: - """A transaction whose block runs, and whose commit then fails.""" - - def __init__(self, connection) -> None: - self.connection = connection - - def __enter__(self): - return self.connection - - def __exit__(self, error_type, error, traceback) -> None: - if error is None: - statement = "COMMIT" - raise OperationalError(statement, {}, Exception("connection lost")) - - -class _RecordingConnection: - def __init__(self) -> None: - self.statements: list[object] = [] - - def execute(self, statement, *arguments): - self.statements.append(statement) - - -def test_a_create_whose_commit_fails_may_have_been_written() -> None: - connection = _RecordingConnection() - store = HouseholdReportStore(lambda: _CommitFails(connection)) - catalog_rows = {"model_id": uuid4(), "version_id": uuid4()} - - with pytest.raises(EconomyRecordOutcomeUnknownError) as raised: - store.create_report( - NewHouseholdReport( - id=uuid4(), - run_id=uuid4(), - country="us", - label="Lost commit", - description=None, - tax_benefit_model_id=catalog_rows["model_id"], - tax_benefit_model_version_id=catalog_rows["version_id"], - household_id=uuid4(), - year=2026, - versions=VERSIONS, - inputs=OPTIONS, - baseline=HouseholdReportSimulation(uuid4(), uuid4(), None), - reform=None, - ) - ) - - assert isinstance(raised.value.__cause__, OperationalError) - assert connection.statements diff --git a/libs/policyengine-simulation-persistence/tests/test_keyed_economy_records.py b/libs/policyengine-simulation-persistence/tests/test_keyed_economy_records.py index ebe33047b..592b06423 100644 --- a/libs/policyengine-simulation-persistence/tests/test_keyed_economy_records.py +++ b/libs/policyengine-simulation-persistence/tests/test_keyed_economy_records.py @@ -32,6 +32,7 @@ ) from policyengine_simulation_persistence import ( KEY_VERSION, + EconomyRecordReferenceError, EconomyReportDefinition, EconomyRunStore, EconomySimulationDefinition, @@ -602,8 +603,8 @@ def test_a_keyed_reports_rerun_joins_each_sides_run_on_its_versions( run_id=uuid4(), population=economy.population(), versions=VERSIONS, - baseline=RerunSide(created.baseline.simulation_id, uuid4(), reused=False), - reform=RerunSide(created.reform.simulation_id, uuid4(), reused=False), + baseline=RerunSide(created.baseline.simulation_id, uuid4()), + reform=RerunSide(created.reform.simulation_id, uuid4()), ) found = insert_report_rerun(connection, rerun) @@ -635,8 +636,8 @@ def test_a_keyed_rerun_with_one_simulation_on_both_sides_inserts_one_run( run_id=uuid4(), population=economy.population(), versions=VERSIONS, - baseline=RerunSide(simulation_id, uuid4(), reused=False), - reform=RerunSide(simulation_id, uuid4(), reused=False), + baseline=RerunSide(simulation_id, uuid4()), + reform=RerunSide(simulation_id, uuid4()), ) found = insert_report_rerun(connection, rerun) @@ -653,3 +654,41 @@ def test_a_keyed_rerun_with_one_simulation_on_both_sides_inserts_one_run( == rerun.baseline.simulation_run_id ) assert runs == 2 + + +@pytest.mark.parametrize( + ("missing_row", "column_name"), + [ + ("region", "region_id"), + ("dataset", "dataset_id"), + ("dynamic", "dynamic_id"), + ("policy", "policy_id"), + ], +) +def test_a_report_naming_a_missing_row_names_the_column_that_refused_it( + public_api_database_url, missing_row: str, column_name: str +) -> None: + """The service answers each such column with its own refusal code.""" + with rolled_back_transaction(public_api_database_url) as connection: + economy = insert_economy_catalog(connection) + report = _keyed_report( + economy, + reform={ + "dynamic": SimulationChoice(policy_id=None, dynamic_id=uuid4()), + "policy": SimulationChoice(policy_id=uuid4(), dynamic_id=None), + }.get(missing_row, CURRENT_LAW), + ) + if missing_row in {"region", "dataset"}: + report = replace( + report, + population=replace(report.population, **{column_name: uuid4()}), + ) + act_as_the_service(connection) + + with pytest.raises(EconomyRecordReferenceError) as refused: + find_or_create_economy_report(connection, report) + + assert (refused.value.table_name, refused.value.column_name) == ( + "simulations", + column_name, + ) diff --git a/libs/policyengine-simulation-persistence/tests/test_keyed_household_records.py b/libs/policyengine-simulation-persistence/tests/test_keyed_household_records.py index f277830ed..120d20a70 100644 --- a/libs/policyengine-simulation-persistence/tests/test_keyed_household_records.py +++ b/libs/policyengine-simulation-persistence/tests/test_keyed_household_records.py @@ -18,6 +18,7 @@ from conftest import act_as_the_service, rolled_back_transaction from policyengine_simulation_persistence import ( KEY_VERSION, + EconomyRecordReferenceError, FoundHouseholdReport, FoundSide, HouseholdReportDefinition, @@ -508,3 +509,20 @@ def test_a_forced_keyed_household_rerun_is_an_extra_run_beside_the_one_under_way assert (created.id, created.inserted) == (rerun.run_id, True) assert report_run["forced"] is True assert [side_run["forced"] for side_run in side_runs] == [True, True] + + +def test_a_household_report_naming_a_missing_household_is_refused_by_its_key( + public_api_database_url, +) -> None: + with rolled_back_transaction(public_api_database_url) as connection: + stored = _stored_inputs(connection) + report = _keyed_report(stored, reform=None) + missing_household = replace( + report, definition=replace(report.definition, household_id=uuid4()) + ) + act_as_the_service(connection) + + with pytest.raises(EconomyRecordReferenceError) as refused: + find_or_create_household_report(connection, missing_household) + + assert refused.value.column_name == "household_id" diff --git a/libs/policyengine-simulation-persistence/tests/test_keyed_window_records.py b/libs/policyengine-simulation-persistence/tests/test_keyed_window_records.py index 9d3ce4fed..746f4355b 100644 --- a/libs/policyengine-simulation-persistence/tests/test_keyed_window_records.py +++ b/libs/policyengine-simulation-persistence/tests/test_keyed_window_records.py @@ -32,6 +32,7 @@ from policyengine_simulation_persistence import ( KEY_VERSION, EconomyPopulation, + EconomyRecordReferenceError, EconomyReportDefinition, EconomyRunStore, FoundBudgetWindow, @@ -451,3 +452,28 @@ def second_request() -> None: year.report_run_id for year in first.years ] assert started_windows == [first.window_run.id] + + +def test_a_window_naming_a_missing_policy_is_refused_and_writes_nothing( + public_api_database_url, +) -> None: + with rolled_back_transaction(public_api_database_url) as connection: + economy = insert_economy_catalog(connection) + missing_policy = SimulationChoice(policy_id=uuid4(), dynamic_id=None) + act_as_the_service(connection) + before = _counts(connection) + + with ( + pytest.raises(EconomyRecordReferenceError) as refused, + connection.begin_nested(), + ): + find_or_create_budget_window( + connection, _keyed_window(economy, missing_policy) + ) + after = _counts(connection) + + assert (refused.value.table_name, refused.value.column_name) == ( + "simulations", + "policy_id", + ) + assert after == before diff --git a/projects/policyengine-simulation-entry/DEPLOYMENT.md b/projects/policyengine-simulation-entry/DEPLOYMENT.md index 2a759270f..fa001a6cc 100644 --- a/projects/policyengine-simulation-entry/DEPLOYMENT.md +++ b/projects/policyengine-simulation-entry/DEPLOYMENT.md @@ -119,17 +119,12 @@ fails the check. ## Economy reports and simulations `POST /reports` and `POST /simulations`, and their reads, use the same -database, Modal token and v2 manifest settings as the household routes. The -deploy also sets `V2_SIMULATION_OUTPUT_BUCKET`, the `beta` or `prod` variable -of that name: the bucket that keeps v2 simulation output files for good. A -missing or invalid bucket name stops the deploy. The service reads it into -its settings but uses it for nothing: a report joins its sides' simulation -runs by version set (C1 · 24's answers 4 and 5), not by their files. The -setting and its deploy check are removed with the run-reuse search, in a -later change. The service never reads the bucket itself, so it needs no -storage permission. -The coordinator and workers get the same bucket from the Modal secret the -Stage 12 secret sync writes. +database, Modal token and v2 manifest settings as the household routes. A +report joins its sides' simulation runs by version set (C1 · 24's answers 4 +and 5), not by their output files, so the service needs no simulation +output bucket setting and no storage permission. The coordinator and workers +get the bucket they write those files to, `V2_SIMULATION_OUTPUT_BUCKET`, from +the Modal secret the Stage 12 secret sync writes. ### The staging economy check diff --git a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/app.py b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/app.py index 7f1e43a68..ba75fa1d8 100644 --- a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/app.py +++ b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/app.py @@ -599,7 +599,6 @@ def economy_submissions_for( settings=ServiceSettings( environment=settings.environment, modal_environment=settings.v2_modal_environment, - output_bucket=settings.v2_simulation_output_bucket or None, ), ) diff --git a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/budget_window_submissions.py b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/budget_window_submissions.py index 77b639ce6..ba8d84d64 100644 --- a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/budget_window_submissions.py +++ b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/budget_window_submissions.py @@ -68,13 +68,13 @@ ) if TYPE_CHECKING: - from collections.abc import Callable, Sequence + from collections.abc import Callable from policyengine_simulation_persistence import ( BudgetWindowInputs, EconomyRequest, FoundBudgetWindow, - RunReuse, + FoundWindowYear, WindowYearRuns, ) from policyengine_stage12_persistence import ( @@ -107,10 +107,8 @@ def read_budget_window_inputs( *, baseline: SimulationChoice, reform: SimulationChoice, - reuse: RunReuse | None, - years: Sequence[int], ) -> BudgetWindowInputs: - """Read the window's catalog rows, both sides' values and each year's runs.""" + """Read the window's catalog rows and both sides' values.""" ... def find_or_create_budget_window( @@ -188,8 +186,6 @@ async def submit_budget_window( economy_request, baseline=simulation_choice(request.baseline), reform=simulation_choice(request.reform), - reuse=None, - years=request.years, ), request=economy_request, ) @@ -435,7 +431,7 @@ def year_run_ids(window_year: WindowYearRuns) -> ReportRunIds: ) -def year_created(window_year: WindowYearRuns) -> BudgetWindowYearCreated: +def year_created(window_year: FoundWindowYear) -> BudgetWindowYearCreated: """Return a year of the window run a request ended on, as the answer lists it. A side is reused when its run isn't one this request inserted. diff --git a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/config.py b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/config.py index 58814e7db..58c935321 100644 --- a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/config.py +++ b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/config.py @@ -102,11 +102,6 @@ class Settings: # manifest, and the manifest's name. Empty outside deployed environments. v2_modal_environment: str = "" v2_manifest_name: str = V2_VERSION_MANIFEST_NAME - # The bucket that keeps v2 simulation output files for good. The service - # uses it for nothing: a report joins its sides' simulation runs by - # version set, not by their files; it goes with the run-reuse search. - # Empty outside deployed environments. - v2_simulation_output_bucket: str = "" @classmethod def from_env(cls) -> Settings: @@ -162,7 +157,6 @@ def from_env(cls) -> Settings: v2_manifest_name=os.getenv( "SIMULATION_V2_MANIFEST_NAME", V2_VERSION_MANIFEST_NAME ), - v2_simulation_output_bucket=os.getenv("V2_SIMULATION_OUTPUT_BUCKET", ""), ) @property diff --git a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_reruns.py b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_reruns.py index fc2cec1ad..c95772da3 100644 --- a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_reruns.py +++ b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_reruns.py @@ -98,7 +98,7 @@ ) if TYPE_CHECKING: - from collections.abc import Callable, Sequence + from collections.abc import Callable from uuid import UUID from policyengine_simulation_persistence import ( @@ -108,7 +108,6 @@ FoundReportRun, FoundSimulationRun, ReportDefinition, - RunReuse, SideDefinition, SimulationChoice, SimulationDefinition, @@ -164,9 +163,8 @@ def read_report_rerun_inputs( *, baseline: SideDefinition, reform: SideDefinition, - reuse: RunReuse | None, ) -> EconomyReportInputs: - """Read a re-run's catalog rows, both sides' values and reusable runs.""" + """Read a re-run's catalog rows and both sides' values.""" ... def read_window_rerun_inputs( @@ -175,10 +173,8 @@ def read_window_rerun_inputs( *, baseline: SimulationChoice, reform: SimulationChoice, - years: Sequence[WindowYearDefinition], - reuse: RunReuse | None, ) -> BudgetWindowInputs: - """Read a window re-run's catalog rows, both sides' values and years' runs.""" + """Read a window re-run's catalog rows and both sides' values.""" ... def create_window_rerun(self, rerun: NewWindowRerun) -> FoundBudgetWindow: @@ -294,8 +290,6 @@ async def rerun_report( economy_request, baseline=stored.baseline, reform=stored.reform, - # A re-run reuses no run: its sides join or are forced. - reuse=None, ), request=economy_request, source=STORED_REPORT, @@ -463,8 +457,6 @@ async def _rerun_window( window.economy_request, baseline=stored.baseline.choice, reform=stored.reform.choice, - years=window.years, - reuse=None, ), request=window.economy_request, source=STORED_REPORT, @@ -544,7 +536,6 @@ def _new_side(self, simulation_id: UUID) -> RerunSide: return RerunSide( simulation_id=simulation_id, simulation_run_id=self._new_id(), - reused=False, ) async def _create_rerun[Created]( diff --git a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_submissions.py b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_submissions.py index aabdc079b..644b99d41 100644 --- a/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_submissions.py +++ b/projects/policyengine-simulation-entry/src/policyengine_simulation_entry/economy_submissions.py @@ -108,7 +108,6 @@ EconomySimulationState, FoundEconomyReport, FoundEconomySimulation, - RunReuse, ) from policyengine_simulation_entry.economy_api_models import ( @@ -141,9 +140,8 @@ def read_report_inputs( *, baseline: SimulationChoice, reform: SimulationChoice, - reuse: RunReuse | None, ) -> EconomyReportInputs: - """Read a report's catalog rows, both sides' values and reusable runs.""" + """Read a report's catalog rows and both sides' values.""" ... def read_simulation_inputs( @@ -202,17 +200,12 @@ def read_simulation(self, simulation_id: UUID) -> EconomySimulationState | None: class ServiceSettings: """The deployment the service runs in. - ``environment`` labels the calls' artifacts, ``modal_environment`` holds - the v2 workers, and ``output_bucket`` is the bucket that keeps - simulation output files for good. The service keeps it here and uses - it for nothing: a report joins its sides' runs by version set - (C1 · 24's answers 4 and 5), not by their files; the setting goes with - the reuse search. + ``environment`` labels the calls' artifacts, and ``modal_environment`` + holds the v2 workers. """ environment: str modal_environment: str - output_bucket: str | None = None def _database_unavailable() -> EconomySubmissionUnavailableError: @@ -281,7 +274,6 @@ async def submit_report( economy_request, baseline=simulation_choice(request.baseline), reform=simulation_choice(request.reform), - reuse=None, ), request=economy_request, ) diff --git a/projects/policyengine-simulation-entry/tests/economy_rerun_fakes.py b/projects/policyengine-simulation-entry/tests/economy_rerun_fakes.py index 966393973..3ffa4822a 100644 --- a/projects/policyengine-simulation-entry/tests/economy_rerun_fakes.py +++ b/projects/policyengine-simulation-entry/tests/economy_rerun_fakes.py @@ -13,7 +13,6 @@ from economy_service_fakes import ( CREATED_AT, DYNAMIC_ID, - OUTPUT_BUCKET, POLICY_ID, REFORM, FakeCaller, @@ -44,8 +43,6 @@ from test_budget_window_submissions import FakeAdvancer, FakeWindowStore if TYPE_CHECKING: - from collections.abc import Sequence - from policyengine_simulation_entry.household_report_reruns import ( HouseholdReportReruns, ) @@ -54,7 +51,6 @@ NewReportRerun, NewSimulationRerun, NewWindowRerun, - RunReuse, ) REPORT_ID = UUID("00000000-0000-4000-8000-0000000000a1") @@ -127,8 +123,8 @@ class FakeRerunStore(FakeWindowStore): """Records the re-run reads and writes, and answers as told. - A budget window's re-run is read with ``reusable_by_year``, as a new - window is, and recorded in ``windows``, where the fake advancer finds it. + A budget window's re-run is recorded in ``windows``, as a new window is, + where the fake advancer finds it. ``inputs_failure`` is raised by a re-run's read of its inputs, and ``rerun_failure`` by its create instead of recording it. A simulation @@ -176,26 +172,19 @@ def read_report_rerun_inputs( *, baseline: SideDefinition, reform: SideDefinition, - reuse: RunReuse | None, ) -> EconomyReportInputs: - self.rerun_reads.append((request, (baseline, reform, reuse))) + self.rerun_reads.append((request, (baseline, reform))) if self.inputs_failure is not None: raise self.inputs_failure - baseline_reusable, reform_reusable = self.reusable return EconomyReportInputs( catalog=self.catalog, - baseline=SimulationInputs( - choice=baseline.choice, - reform={}, - reusable_run=None if reuse is None else baseline_reusable, - ), + baseline=SimulationInputs(choice=baseline.choice, reform={}), # Current law sets no values, on either side. reform=SimulationInputs( choice=reform.choice, reform=REFORM if reform.choice.policy_id or reform.choice.dynamic_id else {}, - reusable_run=None if reuse is None else reform_reusable, ), ) @@ -213,22 +202,14 @@ def read_window_rerun_inputs( *, baseline: SimulationChoice, reform: SimulationChoice, - years: Sequence[WindowYearDefinition], - reuse: RunReuse | None, ) -> BudgetWindowInputs: - self.rerun_reads.append((request, (baseline, reform, tuple(years), reuse))) + self.rerun_reads.append((request, (baseline, reform))) if self.inputs_failure is not None: raise self.inputs_failure return BudgetWindowInputs( catalog=self.catalog, baseline=SimulationInputs(choice=baseline, reform={}), reform=SimulationInputs(choice=reform, reform=REFORM), - reusable_runs={ - year.year: (None, None) - if reuse is None - else self.reusable_by_year.get(year.year, (None, None)) - for year in years - }, ) def create_window_rerun(self, rerun: NewWindowRerun) -> FoundBudgetWindow: @@ -304,12 +285,11 @@ def advance_window(self, window_run_id: UUID, **_: Any) -> NoReturn: raise AssertionError(msg) -def rerun_service( # noqa: PLR0913 # the store, then each seam a test may replace +def rerun_service( # the store, then each seam a test may replace store: FakeRerunStore, *, workers: FakeWorkers | None = None, caller: FakeCaller | None = None, - output_bucket: str | None = OUTPUT_BUCKET, advancer: FakeAdvancer | None = None, household_reports: HouseholdReportReruns | None = None, ) -> EconomyRerunService: @@ -322,7 +302,6 @@ def rerun_service( # noqa: PLR0913 # the store, then each seam a test may repl settings=ServiceSettings( environment="staging", modal_environment="staging", - output_bucket=output_bucket, ), call_start_timeout_seconds=0.05, new_id=numbered_ids(), diff --git a/projects/policyengine-simulation-entry/tests/economy_service_fakes.py b/projects/policyengine-simulation-entry/tests/economy_service_fakes.py index 40bc933a0..7978f2b7c 100644 --- a/projects/policyengine-simulation-entry/tests/economy_service_fakes.py +++ b/projects/policyengine-simulation-entry/tests/economy_service_fakes.py @@ -40,7 +40,6 @@ FoundSide, FoundSimulationRun, ReportRunStatus, - ReusableRun, SimulationInputs, SimulationStatus, ) @@ -52,7 +51,6 @@ EconomyRequest, KeyedEconomyReport, KeyedEconomySimulation, - RunReuse, SimulationChoice, ) @@ -62,12 +60,13 @@ DATASET_ID = UUID("00000000-0000-4000-8000-0000000000c4") POLICY_ID = UUID("00000000-0000-4000-8000-0000000000c5") DYNAMIC_ID = UUID("00000000-0000-4000-8000-0000000000c6") -REUSED = ReusableRun( +# A side whose simulation run another request inserted, which a report joins. +REUSED = FoundSide( simulation_id=UUID("00000000-0000-4000-8000-0000000000d1"), simulation_run_id=UUID("00000000-0000-4000-8000-0000000000d2"), + run_inserted=False, ) CREATED_AT = datetime(2026, 11, 12, 15, tzinfo=UTC) -OUTPUT_BUCKET = "policyengine-v2-simulation-outputs-staging" CTC_AMOUNT = "gov.irs.credits.ctc.amount.base[0].amount" REFORM = {CTC_AMOUNT: {"2026-01-01.2200-12-31": 3_000}} CATALOG = EconomyCatalogRows( @@ -174,7 +173,6 @@ class FakeStore: """ catalog: EconomyCatalogRows = CATALOG - reusable: tuple[ReusableRun | None, ReusableRun | None] = (None, None) read_failure: Exception | None = None create_failure: Exception | None = None record_failure: Exception | None = None @@ -197,22 +195,17 @@ def read_report_inputs( *, baseline: SimulationChoice, reform: SimulationChoice, - reuse: RunReuse | None, ) -> EconomyReportInputs: - self.report_reads.append((request, (baseline, reform, reuse))) + self.report_reads.append((request, (baseline, reform))) if self.read_failure is not None: raise self.read_failure - baseline_reusable, reform_reusable = self.reusable return EconomyReportInputs( catalog=self.catalog, - baseline=SimulationInputs( - choice=baseline, reform={}, reusable_run=baseline_reusable - ), + baseline=SimulationInputs(choice=baseline, reform={}), # Current law sets no values, on either side. reform=SimulationInputs( choice=reform, reform=REFORM if reform.policy_id or reform.dynamic_id else {}, - reusable_run=reform_reusable, ), ) @@ -369,7 +362,6 @@ def service( *, workers: FakeWorkers | None = None, caller: FakeCaller | None = None, - output_bucket: str | None = OUTPUT_BUCKET, ) -> EconomySubmissionService: return EconomySubmissionService( store=store, @@ -378,7 +370,6 @@ def service( settings=ServiceSettings( environment="staging", modal_environment="staging", - output_bucket=output_bucket, ), call_start_timeout_seconds=0.05, new_id=numbered_ids(), diff --git a/projects/policyengine-simulation-entry/tests/test_budget_window_submissions.py b/projects/policyengine-simulation-entry/tests/test_budget_window_submissions.py index ef2cca099..1717f11d8 100644 --- a/projects/policyengine-simulation-entry/tests/test_budget_window_submissions.py +++ b/projects/policyengine-simulation-entry/tests/test_budget_window_submissions.py @@ -21,7 +21,6 @@ from economy_service_fakes import ( CATALOG, CREATED_AT, - OUTPUT_BUCKET, REFORM, FakeCaller, FakeStore, @@ -53,7 +52,6 @@ FoundWindowRun, FoundWindowYear, ReportRunStatus, - ReusableRun, SimulationInputs, ) from policyengine_stage12_persistence import ( @@ -67,10 +65,8 @@ from test_budget_window_requests import WINDOW_BODY OBSERVED = {"observability_id": "observed"} -REUSED_IN_2027 = ReusableRun( - simulation_id=UUID("00000000-0000-4000-8000-0000000000d1"), - simulation_run_id=UUID("00000000-0000-4000-8000-0000000000d2"), -) +# A 2027 simulation run another report inserted, which a window year joins. +JOINED_RUN_IN_2027 = UUID("00000000-0000-4000-8000-0000000000d2") @dataclass @@ -83,26 +79,20 @@ class FakeWindowStore(FakeStore): names instead of its own. """ - reusable_by_year: dict[int, tuple[ReusableRun | None, ReusableRun | None]] = field( - default_factory=dict - ) joined_window: FoundBudgetWindow | None = None joined_year_runs: dict[tuple[int, str], UUID] = field(default_factory=dict) window_reads: list[tuple[Any, ...]] = field(default_factory=list) windows: list[Any] = field(default_factory=list) found_windows: list[FoundBudgetWindow] = field(default_factory=list) - def read_budget_window_inputs(self, request, *, baseline, reform, reuse, years): - self.window_reads.append((request, baseline, reform, reuse, tuple(years))) + def read_budget_window_inputs(self, request, *, baseline, reform): + self.window_reads.append((request, baseline, reform)) if self.read_failure is not None: raise self.read_failure return BudgetWindowInputs( catalog=self.catalog, baseline=SimulationInputs(choice=baseline, reform={}), reform=SimulationInputs(choice=reform, reform=REFORM), - reusable_runs={ - year: self.reusable_by_year.get(year, (None, None)) for year in years - }, ) def find_or_create_budget_window(self, window) -> FoundBudgetWindow: @@ -205,7 +195,6 @@ def _service(store, *, caller=None, advancer=None): settings=ServiceSettings( environment="staging", modal_environment="staging", - output_bucket=OUTPUT_BUCKET, ), call_start_timeout_seconds=0.05, new_id=numbered_ids(), @@ -231,14 +220,12 @@ def test_a_window_is_created_with_a_run_per_year_and_its_first_five_started() -> _service(store, caller=caller, advancer=advancer), budget_window_size=7 ) - ((request, baseline, reform, reuse, years),) = store.window_reads + ((request, baseline, reform),) = store.window_reads assert (baseline.policy_id, str(reform.policy_id)) == ( None, WINDOW_BODY["reform"]["policy_id"], ) - assert (request.year, years) == (2026, tuple(range(2026, 2033))) - # Answer 8: found or created by key; no run is searched for to reuse. - assert reuse is None + assert request.year == 2026 (report,) = store.windows assert report.definition == EconomyReportDefinition( country="us", @@ -313,9 +300,7 @@ def test_each_later_year_is_derived_from_the_first() -> None: def test_a_year_names_its_own_years_run_another_report_created() -> None: """Answer 8: a window year and a single-year report share runs.""" - store = FakeWindowStore( - joined_year_runs={(2027, "baseline"): REUSED_IN_2027.simulation_run_id} - ) + store = FakeWindowStore(joined_year_runs={(2027, "baseline"): JOINED_RUN_IN_2027}) caller = FakeCaller() advancer = FakeAdvancer(store) @@ -324,13 +309,9 @@ def test_a_year_names_its_own_years_run_another_report_created() -> None: ) assert [year.baseline_reused for year in created.years] == [False, True, False] - assert created.years[1].baseline_simulation_run_id == ( - REUSED_IN_2027.simulation_run_id - ) + assert created.years[1].baseline_simulation_run_id == (JOINED_RUN_IN_2027) year_2027 = ReportExecutionInput.model_validate(caller.report_runs[1][1]) - assert year_2027.baseline.simulation_execution_id == ( - REUSED_IN_2027.simulation_run_id - ) + assert year_2027.baseline.simulation_execution_id == (JOINED_RUN_IN_2027) def test_an_identical_window_request_joins_its_window_run_and_starts_no_year() -> None: diff --git a/projects/policyengine-simulation-entry/tests/test_config.py b/projects/policyengine-simulation-entry/tests/test_config.py index d3b72af5a..3d6612c99 100644 --- a/projects/policyengine-simulation-entry/tests/test_config.py +++ b/projects/policyengine-simulation-entry/tests/test_config.py @@ -412,18 +412,6 @@ def test_the_v2_manifest_settings_configure_no_stage12_resource(): assert not settings.stage12_resources_configured -def test_the_v2_output_bucket_is_read_from_the_environment(monkeypatch): - from policyengine_simulation_entry.config import Settings - - monkeypatch.setenv("V2_SIMULATION_OUTPUT_BUCKET", "policyengine-v2-outputs-staging") - - assert Settings.from_env().v2_simulation_output_bucket == ( - "policyengine-v2-outputs-staging" - ) - monkeypatch.delenv("V2_SIMULATION_OUTPUT_BUCKET") - assert Settings.from_env().v2_simulation_output_bucket == "" - - def test_economy_runs_need_the_database_and_the_v2_modal_environment(): assert not make_settings().economy_runs_configured assert not make_settings( diff --git a/projects/policyengine-simulation-entry/tests/test_deployment_assets_support.py b/projects/policyengine-simulation-entry/tests/test_deployment_assets_support.py index 5d8dc1eb0..a0c8ada56 100644 --- a/projects/policyengine-simulation-entry/tests/test_deployment_assets_support.py +++ b/projects/policyengine-simulation-entry/tests/test_deployment_assets_support.py @@ -644,5 +644,4 @@ def cloud_run_deploy_environment(tmp_path: Path) -> dict[str, str]: "SIMULATION_SERVICE_DATABASE_URL_SECRET_NAME": ( "simulation-staging-service-database-url" ), - "V2_SIMULATION_OUTPUT_BUCKET_VALUE": "policyengine-v2-outputs-staging", } diff --git a/projects/policyengine-simulation-entry/tests/test_economy_deployment.py b/projects/policyengine-simulation-entry/tests/test_economy_deployment.py deleted file mode 100644 index 0686a8ef7..000000000 --- a/projects/policyengine-simulation-entry/tests/test_economy_deployment.py +++ /dev/null @@ -1,85 +0,0 @@ -"""Deployment wiring for the economy report and simulation routes. - -Every deploy gives the service the bucket that keeps v2 simulation output -files for good (the environment variable ``V2_SIMULATION_OUTPUT_BUCKET``) -and refuses a missing or invalid one. The service reads it into its -settings and uses it for nothing, since a report joins its sides' -simulation runs by version set; it never reads the bucket. -""" - -from __future__ import annotations - -import json -import subprocess -from pathlib import Path - -import pytest -from test_deployment_assets_support import ( - CLOUD_RUN_DEPLOY_SCRIPT, - REUSABLE_DEPLOY_WORKFLOW, - cloud_run_deploy_environment, - workflow_jobs, -) - - -def _run_deploy(tmp_path, **overrides): - environment = cloud_run_deploy_environment(tmp_path) | overrides - result = subprocess.run( - [CLOUD_RUN_DEPLOY_SCRIPT], - check=False, - capture_output=True, - text=True, - env=environment, - ) - return result, environment - - -@pytest.mark.parametrize("deploy_stage12", ["true", "false"]) -def test_every_deploy_names_the_v2_output_bucket(tmp_path, deploy_stage12): - result, environment = _run_deploy( - tmp_path, - DEPLOY_STAGE12_V2=deploy_stage12, - V2_SIMULATION_OUTPUT_BUCKET_VALUE="policyengine-v2-outputs-staging", - ) - - assert result.returncode == 0, result.stderr - runtime_environment = json.loads( - Path(environment["GCLOUD_ENV_FILE_CAPTURE"]).read_text(encoding="utf-8") - ) - assert runtime_environment["V2_SIMULATION_OUTPUT_BUCKET"] == ( - "policyengine-v2-outputs-staging" - ) - - -@pytest.mark.parametrize( - ("bucket", "message"), - [ - ("", "V2_SIMULATION_OUTPUT_BUCKET_VALUE must be set"), - ( - "gs://policyengine-v2-outputs", - "V2_SIMULATION_OUTPUT_BUCKET_VALUE is invalid", - ), - ("Policyengine", "V2_SIMULATION_OUTPUT_BUCKET_VALUE is invalid"), - ], -) -def test_a_deploy_without_a_valid_output_bucket_stops(tmp_path, bucket, message): - result, environment = _run_deploy( - tmp_path, V2_SIMULATION_OUTPUT_BUCKET_VALUE=bucket - ) - - assert result.returncode != 0 - assert message in result.stderr - assert not Path(environment["GCLOUD_ARGUMENTS_CAPTURE"]).exists() - - -def test_the_deploy_step_passes_the_environments_output_bucket(): - deploy_entrypoint = workflow_jobs(REUSABLE_DEPLOY_WORKFLOW)["deploy_entrypoint"] - deploy_step = next( - step - for step in deploy_entrypoint["steps"] - if step.get("name") == "Deploy Cloud Run entrypoint candidate" - ) - - assert deploy_step["env"]["V2_SIMULATION_OUTPUT_BUCKET_VALUE"] == ( - "${{ vars.V2_SIMULATION_OUTPUT_BUCKET }}" - ) diff --git a/projects/policyengine-simulation-entry/tests/test_economy_reruns.py b/projects/policyengine-simulation-entry/tests/test_economy_reruns.py index 8e10cb48f..0eb96f1da 100644 --- a/projects/policyengine-simulation-entry/tests/test_economy_reruns.py +++ b/projects/policyengine-simulation-entry/tests/test_economy_reruns.py @@ -57,7 +57,6 @@ ReportSideError, RerunRecordDeletedError, RerunSide, - ReusableRun, RunVersions, SharedRowNotSettledError, SimulationStatus, @@ -71,14 +70,6 @@ country_package_version="2.2.1", policyengine_version=POLICYENGINE_VERSION ) OBSERVED = {"observability_id": "observed"} -REUSED_2027 = ReusableRun( - simulation_id=UUID("00000000-0000-4000-8000-000000002027"), - simulation_run_id=UUID("00000000-0000-4000-8000-0000000000b2"), -) -REUSED_BASELINE = ReusableRun( - simulation_id=BASELINE_SIMULATION_ID, - simulation_run_id=UUID("00000000-0000-4000-8000-0000000000b1"), -) def _rerun_report(service, request: ReportRerunRequest | None = None): @@ -104,12 +95,12 @@ def test_a_report_with_no_keys_is_rerun_as_forced_runs_on_the_current_versions() None ): """Interim answer B: its simulations' runs may have report-dependent plans.""" - store = FakeRerunStore(reusable=(REUSED_BASELINE, None)) + store = FakeRerunStore() caller = FakeCaller() rerun = _rerun_report(rerun_service(store, caller=caller)) - ((request, (baseline, reform, reuse)),) = store.rerun_reads + ((request, (baseline, reform)),) = store.rerun_reads assert (request.country, request.policyengine_version, request.year) == ( "us", POLICYENGINE_VERSION, @@ -117,7 +108,6 @@ def test_a_report_with_no_keys_is_rerun_as_forced_runs_on_the_current_versions() ) assert (request.region_code, request.dataset_name) == ("us", "populace_us_2024") assert (baseline, reform) == (STORED_REPORT.baseline, STORED_REPORT.reform) - assert reuse is None # Extra runs, marked forced, which nothing joins and no index refuses. assert store.report_reruns == [ NewReportRerun( @@ -128,12 +118,10 @@ def test_a_report_with_no_keys_is_rerun_as_forced_runs_on_the_current_versions() baseline=RerunSide( simulation_id=BASELINE_SIMULATION_ID, simulation_run_id=UUID(int=2), - reused=False, ), reform=RerunSide( simulation_id=REFORM_SIMULATION_ID, simulation_run_id=UUID(int=3), - reused=False, ), forced=True, ) @@ -168,15 +156,13 @@ def test_a_report_with_no_keys_is_rerun_as_forced_runs_on_the_current_versions() def test_a_forced_rerun_recalculates_both_simulations() -> None: - store = FakeRerunStore(reusable=(REUSED_BASELINE, None)) + store = FakeRerunStore() rerun = _rerun_report(rerun_service(store), ReportRerunRequest(force=True)) - assert store.rerun_reads[0][1][2] is None (created,) = store.report_reruns # An extra run, marked forced, which nothing joins (answer 3). assert created.forced is True - assert (created.baseline.reused, created.reform.reused) == (False, False) assert created.new_simulation_run_ids == (UUID(int=2), UUID(int=3)) assert created.baseline.simulation_id == BASELINE_SIMULATION_ID assert (rerun.baseline_reused, rerun.reform_reused) == (False, False) @@ -235,10 +221,8 @@ def test_a_keyed_reports_rerun_has_each_side_join_its_run_on_the_versions() -> N rerun = _rerun_report(rerun_service(store, caller=caller)) - assert store.rerun_reads[0][1][2] is None (created,) = store.report_reruns assert created.forced is False - assert (created.baseline.reused, created.reform.reused) == (False, False) payload = ReportExecutionInput.model_validate(caller.report_runs[0][1]) assert payload.baseline.simulation_execution_id == joined_baseline_run assert payload.reform.simulation_execution_id == created.reform.simulation_run_id @@ -359,7 +343,7 @@ def test_a_report_missing_a_row_it_calculates_on_is_not_rerun(missing, named) -> def test_a_rerun_modal_refuses_fails_only_its_new_runs_and_names_them() -> None: - store = FakeRerunStore(reusable=(REUSED_BASELINE, None)) + store = FakeRerunStore() with pytest.raises(EconomySubmissionUnavailableError) as unavailable: _rerun_report( @@ -664,23 +648,19 @@ def test_a_row_deleted_after_a_reruns_reads_is_refused_naming_the_report( def test_a_budget_window_is_rerun_as_a_new_window_run_and_its_first_years_started(): """Interim answer B: a window has no keys yet, so every year is recalculated.""" - store = FakeRerunStore( - report_definition=STORED_WINDOW, reusable_by_year={2027: (REUSED_2027, None)} - ) + store = FakeRerunStore(report_definition=STORED_WINDOW) caller = FakeCaller() advancer = FakeAdvancer(store) rerun = _rerun_report(rerun_service(store, caller=caller, advancer=advancer)) - ((request, (baseline, reform, years, reuse)),) = store.rerun_reads + ((request, (baseline, reform)),) = store.rerun_reads assert (request.policyengine_version, request.year) == (POLICYENGINE_VERSION, 2026) assert (request.region_code, request.dataset_name) == ("us", "populace_us_2024") - assert (baseline, reform, years) == ( + assert (baseline, reform) == ( STORED_WINDOW.baseline.choice, STORED_WINDOW.reform.choice, - STORED_WINDOW.window_years, ) - assert reuse is None (window,) = store.windows assert (window.report_id, window.window_run_id, window.recalculate) == ( REPORT_ID, @@ -693,7 +673,6 @@ def test_a_budget_window_is_rerun_as_a_new_window_run_and_its_first_years_starte baseline_id, reform_id = WINDOW_SIMULATION_IDS[window_year.year] assert window_year.baseline.simulation_id == baseline_id assert window_year.reform.simulation_id == reform_id - assert not any(window_year.baseline.reused for window_year in window.years) assert advancer.advances == [window.window_run_id] assert len(caller.report_runs) == 3 for (year_to_start, _), (_, payload, context, observed) in zip( @@ -718,7 +697,7 @@ def test_a_budget_window_is_rerun_as_a_new_window_run_and_its_first_years_starte "report_run_id": window_year.report_run_id, "baseline_simulation_id": window_year.baseline.simulation_id, "baseline_simulation_run_id": window_year.baseline.simulation_run_id, - "baseline_reused": window_year.baseline.reused, + "baseline_reused": False, "reform_simulation_id": window_year.reform.simulation_id, "reform_simulation_run_id": window_year.reform.simulation_run_id, "reform_reused": False, @@ -729,19 +708,15 @@ def test_a_budget_window_is_rerun_as_a_new_window_run_and_its_first_years_starte def test_a_forced_window_rerun_recalculates_every_year() -> None: - store = FakeRerunStore( - report_definition=STORED_WINDOW, reusable_by_year={2027: (REUSED_2027, None)} - ) + store = FakeRerunStore(report_definition=STORED_WINDOW) rerun = _rerun_report( rerun_service(store, advancer=FakeAdvancer(store)), ReportRerunRequest(force=True), ) - assert store.rerun_reads[0][1][3] is None (window,) = store.windows assert window.recalculate is True - assert not any(window_year.baseline.reused for window_year in window.years) assert not any(year.baseline_reused for year in rerun.years) diff --git a/projects/policyengine-simulation-entry/tests/test_economy_routes.py b/projects/policyengine-simulation-entry/tests/test_economy_routes.py index 30a1bcffc..6f88a5054 100644 --- a/projects/policyengine-simulation-entry/tests/test_economy_routes.py +++ b/projects/policyengine-simulation-entry/tests/test_economy_routes.py @@ -370,7 +370,6 @@ def test_a_configured_service_builds_economy_submissions_without_connecting(): make_settings( simulation_database_url=DATABASE_URL, v2_modal_environment="staging", - v2_simulation_output_bucket="policyengine-v2-outputs-staging", ), database, ) diff --git a/projects/policyengine-simulation-entry/tests/test_economy_submissions.py b/projects/policyengine-simulation-entry/tests/test_economy_submissions.py index e7c5804ef..47b9f3137 100644 --- a/projects/policyengine-simulation-entry/tests/test_economy_submissions.py +++ b/projects/policyengine-simulation-entry/tests/test_economy_submissions.py @@ -106,7 +106,7 @@ def test_a_report_is_created_by_key_and_its_coordinator_started() -> None: created = _submit_report(service(store, caller=caller)) - (request, (baseline, reform, reuse)) = store.report_reads[0] + (request, (baseline, reform)) = store.report_reads[0] assert (request.country, request.policyengine_version, request.region_code) == ( "us", POLICYENGINE_VERSION, @@ -115,8 +115,6 @@ def test_a_report_is_created_by_key_and_its_coordinator_started() -> None: assert (request.dataset_name, request.year) == (None, 2026) assert baseline == SimulationChoice(policy_id=None, dynamic_id=None) assert reform == SimulationChoice(policy_id=POLICY_ID, dynamic_id=DYNAMIC_ID) - # Runs are found by key, not searched for (C1 · 24). - assert reuse is None (report,) = store.reports spm = {"geography_kind": "national"} assert report.definition == EconomyReportDefinition(