Skip to content
1 change: 1 addition & 0 deletions changelog.d/v2-runs-missing-parameters.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`POST /v2/reports` and `POST /v2/simulations` refuse a policy that sets parameters the running catalog version no longer has, as `POST /v2/household-calculations` does: they answer 422 with code `missing_parameters`, naming those parameters, and the simulation service is not asked, so no run starts. A report checks its baseline's policy, then its reform's. A catalog that cannot be read during the check answers 503.
12 changes: 10 additions & 2 deletions policyengine_api/fastapi_routes/resource_service_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ def create_report(self, report_input: EconomyReportCreationInput) -> ReportRead:

Raise ``EconomyCalculationsUnsupportedError`` for a country without
economy reports, ``PolicyNotFoundError`` for a policy the country
does not store, and the simulation service's errors.
does not store, ``PolicyParametersMissingError`` for a policy that
sets parameters the running catalog version lacks,
``SubmissionStorageError`` when the database or its catalog cannot be
read for those checks, the simulation service's errors, and
``CreatedReportUnreadableError`` when the report it created cannot be
read back.
"""
...

Expand All @@ -231,7 +236,10 @@ def create_simulation(
) -> SimulationRead:
"""Create the simulation through the simulation service; return it as stored.

Raise as ``V2StoredReportResourceService.create_report`` does.
Raise the checks' and the simulation service's errors as
``V2StoredReportResourceService.create_report`` does, for the
simulation's one policy, and ``CreatedSimulationUnreadableError`` when
the simulation it created cannot be read back.
"""
...

Expand Down
30 changes: 20 additions & 10 deletions policyengine_api/fastapi_routes/v2/reports/response_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

from pydantic import BaseModel, ConfigDict, JsonValue

from policyengine_api.fastapi_routes.v2.errors_support import V2ErrorResponse
from policyengine_api.fastapi_routes.v2.errors_support import (
V2ErrorResponse,
V2MissingParametersErrorResponse,
)
from policyengine_api.fastapi_routes.v2.simulation_resources_support import (
error_response,
)
Expand Down Expand Up @@ -184,14 +187,20 @@ class UnavailableAfterReportCreatedResponse(V2ErrorResponse):
400: error_response("The body and query name different countries."),
404: error_response("A policy the report names is not stored in the country."),
413: error_response("The request body exceeds 16 KiB."),
422: error_response(
"The request does not match the schema, the country has no economy "
"reports (such as a household-only country), or the simulation service "
"cannot run the report: its 422, or its 400 for a region, dataset or "
"dynamic it does not have. The message says which; for the simulation "
"service's refusal, it gives the service's code, when it gave one, and "
"its reason, but not its status."
),
422: {
"model": V2MissingParametersErrorResponse | V2ErrorResponse,
"description": (
"The request does not match the schema; the country has no economy "
"reports (such as a household-only country); a policy the report "
"names sets parameters the running catalog version does not have "
"(code missing_parameters, which names them; the baseline's policy is "
"checked first); or the simulation service cannot run the report: its "
"422, or its 400 for a region, dataset or dynamic it does not have. The "
"message says which; for the simulation service's refusal, it gives "
"the service's code, when it gave one, and its reason, but not its "
"status."
),
},
502: {
"model": V2ErrorResponse | CreatedReportUnreadableResponse,
"description": (
Expand Down Expand Up @@ -221,7 +230,8 @@ class UnavailableAfterReportCreatedResponse(V2ErrorResponse):
"release deploys), and the message gives its code and reason; it "
"answered 400 or 422 in a shape the Public API cannot read; or it answered "
"401, 403, 404, 405, 413 or 429, which are sent before its route runs. Or "
"the v2 database is unavailable. Or the service created the report, then "
"the v2 database, or the catalog the policies are checked against, cannot "
"be read. Or the service created the report, then "
"Modal refused the run's call and the service failed the run: its 503 "
"named the report, with its status and code, and the answer gives its "
"report_id."
Expand Down
4 changes: 3 additions & 1 deletion policyengine_api/fastapi_routes/v2/reports/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ def build_v2_report_router(dependencies: NativeRouteDependencies) -> APIRouter:
summary="Create an economy report",
description=(
"Checks that the country has economy reports and stores each named "
"policy, then has the simulation service create the report, its two "
"policy, and that the running catalog version has every parameter "
"those policies set, then has the simulation service create the "
"report, its two "
"simulations and their runs on its default worker's PolicyEngine.py "
"version, and start the run. Answers the report as stored, with its "
"pending run; read it again to follow the run. A created report "
Expand Down
16 changes: 12 additions & 4 deletions policyengine_api/fastapi_routes/v2/simulation_resources_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
V2ErrorMapping,
V2ErrorResponse,
run_v2_operation,
v2_missing_parameters_response,
)
from policyengine_api.services.v2.policies.validators import (
PolicyNotFoundError,
PolicyParametersMissingError,
)
from policyengine_api.services.v2.policies.validators import PolicyNotFoundError
from policyengine_api.services.v2.simulation_service.types import (
SimulationServiceAnswerUnreadableError,
SimulationServiceInternalError,
Expand Down Expand Up @@ -136,13 +140,17 @@ def simulation_resource_operation(
) -> OperationT | JSONResponse:
"""Run one service call and answer its failure with a typed v2 error.

A failure of a type in ``refusals`` is the caller's to act on and is not
logged; any other is logged under ``event``.
A policy that sets parameters the running catalog version lacks answers
422 with code ``missing_parameters``, naming them, whatever the route. A
failure of a type in ``refusals`` is the caller's to act on and is not
logged, nor is that 422; any other is logged under ``event``.
"""

def logged() -> OperationT:
def logged() -> OperationT | JSONResponse:
try:
return operation()
except PolicyParametersMissingError as error:
return v2_missing_parameters_response(str(error), error.missing_parameters)
except Exception as error:
if not isinstance(error, refusals):
_log_failure(event, error)
Expand Down
28 changes: 18 additions & 10 deletions policyengine_api/fastapi_routes/v2/simulations/response_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

from pydantic import BaseModel, ConfigDict

from policyengine_api.fastapi_routes.v2.errors_support import V2ErrorResponse
from policyengine_api.fastapi_routes.v2.errors_support import (
V2ErrorResponse,
V2MissingParametersErrorResponse,
)
from policyengine_api.fastapi_routes.v2.reports.response_models import (
PackageVersionsItem,
)
Expand Down Expand Up @@ -126,14 +129,18 @@ class UnavailableAfterSimulationCreatedResponse(V2ErrorResponse):
"The policy the simulation names is not stored in the country."
),
413: error_response("The request body exceeds 16 KiB."),
422: error_response(
"The request does not match the schema, the country has no economy "
"calculations, or the simulation service cannot run the simulation: "
"its 422, or its 400 for a region, dataset or dynamic it does not "
"have. The message says which; for the simulation service's refusal, "
"it gives the service's code, when it gave one, and its reason, but "
"not its status."
),
422: {
"model": V2MissingParametersErrorResponse | V2ErrorResponse,
"description": (
"The request does not match the schema; the country has no economy "
"calculations; the policy sets parameters the running catalog version "
"does not have (code missing_parameters, which names them); or the "
"simulation service cannot run the simulation: its 422, or its 400 "
"for a region, dataset or dynamic it does not have. The message says "
"which; for the simulation service's refusal, it gives the service's "
"code, when it gave one, and its reason, but not its status."
),
},
502: {
"model": V2ErrorResponse | CreatedSimulationUnreadableResponse,
"description": (
Expand Down Expand Up @@ -163,7 +170,8 @@ class UnavailableAfterSimulationCreatedResponse(V2ErrorResponse):
"release deploys), and the message gives its code and reason; it "
"answered 400 or 422 in a shape the Public API cannot read; or it answered "
"401, 403, 404, 405, 413 or 429, which are sent before its route runs. Or "
"the v2 database is unavailable. Or the service created the simulation, "
"the v2 database, or the catalog the policy is checked against, cannot "
"be read. Or the service created the simulation, "
"then Modal refused the run's call and the service failed the run: its 503 "
"named the simulation, with its status and code, and the answer gives its "
"simulation_id."
Expand Down
3 changes: 2 additions & 1 deletion policyengine_api/fastapi_routes/v2/simulations/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ def build_v2_simulation_router(dependencies: NativeRouteDependencies) -> APIRout
summary="Create an economy simulation",
description=(
"Checks that the country has economy calculations and stores the "
"named policy, then has the simulation service create the "
"named policy, and that the running catalog version has every "
"parameter it sets, then has the simulation service create the "
"simulation and its first run on its default worker's "
"PolicyEngine.py version, and start the run. Answers the simulation "
"as stored, with its pending run. A created simulation that cannot "
Expand Down
81 changes: 63 additions & 18 deletions policyengine_api/services/v2/simulation_service/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,35 @@
``POST /reports`` goes through ``SimulationSubmissionService``, so checks
that apply to both live in one place. It checks, in order, that the
country's ``countries`` row allows economy calculations (a household-only
country does not) and that each policy the submission names is stored in the
country, through the same service as ``/v2/policies``; current law names
none. Only then is the simulation service asked. The dynamics a submission
names, its region and its dataset are checked by the simulation service,
which reads them in the catalog version its default worker runs.
country does not), that each policy the submission names is stored in the
country, through the same service as ``/v2/policies`` (current law names
none), and that none of those policies sets a parameter the running catalog
version lacks (``require_runnable_policies``, which re-runs should call too).
Only then is the simulation service asked. The dynamics a submission names,
its region and its dataset are checked by the simulation service, which reads
them in the catalog version its default worker runs.
"""

from __future__ import annotations

from contextlib import contextmanager
from typing import TYPE_CHECKING

from sqlalchemy.exc import SQLAlchemyError

from policyengine_api.data.v2.catalog.catalog_selection_validation import (
MetadataCatalogUnavailableError,
)
from policyengine_api.services.v2.policies.validators import (
require_policy_parameters_present,
)
from policyengine_api.services.v2.simulation_service.validators import (
SubmissionStorageError,
require_economy_country,
)

if TYPE_CHECKING:
from collections.abc import Callable, Iterable
from collections.abc import Callable, Iterable, Iterator
from uuid import UUID

from policyengine_api.services.v2.simulation_service.types import (
Expand All @@ -37,6 +46,44 @@
)


@contextmanager
def _storage_failures() -> Iterator[None]:
"""Raise SubmissionStorageError for a read that failed.

That includes the policy check's catalog query, which the policy service
reports as MetadataCatalogUnavailableError.
"""
try:
yield
except (SQLAlchemyError, MetadataCatalogUnavailableError) as error:
msg = "V2 storage is unavailable to check the submission"
raise SubmissionStorageError(msg) from error


def require_runnable_policies(
policies: PolicySource, *, country_id: str, policy_ids: Iterable[UUID | None]
) -> None:
"""Raise unless each named policy is stored and its parameters all exist.

Every policy is read before any is checked, so PolicyNotFoundError for a
policy the country does not store comes before PolicyParametersMissingError
for one that sets parameters the running catalog version lacks; the
latter names the first such policy in ``policy_ids``. Current law (None)
names no policy, and a policy the running catalog could not check passes.
Raise SubmissionStorageError when the database or its catalog cannot be
read. Anything else that starts a run of stored policies, such as a re-run,
should check them here first.
"""
with _storage_failures():
stored_policies = [
policies.get_policy(country_id=country_id, policy_id=policy_id)
for policy_id in dict.fromkeys(policy_ids)
if policy_id is not None
]
for policy in stored_policies:
require_policy_parameters_present(policy.id, policy.missing_parameters)


class SimulationSubmissionService:
"""Check submissions, then send them to the simulation service."""

Expand All @@ -58,21 +105,17 @@ def __init__(
self._client_factory = client_factory

def _check(self, country_id: str, policy_ids: Iterable[UUID | None]) -> None:
"""Raise unless the country has economy calculations and stores the policies.
"""Raise unless the country has economy calculations and the policies can run.

Raise EconomyCalculationsUnsupportedError, then PolicyNotFoundError,
and SubmissionStorageError when the database cannot be read.
then PolicyParametersMissingError, and SubmissionStorageError when the
database or its catalog cannot be read.
"""
try:
with _storage_failures():
require_economy_country(self._countries.list_countries(), country_id)
for policy_id in dict.fromkeys(policy_ids):
if policy_id is not None:
self._policies.get_policy(
country_id=country_id, policy_id=policy_id
)
except SQLAlchemyError as error:
msg = "V2 storage is unavailable to check the submission"
raise SubmissionStorageError(msg) from error
require_runnable_policies(
self._policies, country_id=country_id, policy_ids=policy_ids
)

def submit_simulation(
self, simulation: EconomySimulationCreationInput
Expand All @@ -87,7 +130,9 @@ def submit_simulation(
def submit_report(self, report: EconomyReportCreationInput) -> SubmittedReport:
"""Check the report, then have the simulation service create and start it.

Raise the checks' errors, then the simulation service's.
Both the baseline's and the reform's policies are checked, the
baseline's first. Raise the checks' errors, then the simulation
service's.
"""
self._check(
report.country_id, (report.baseline.policy_id, report.reform.policy_id)
Expand Down
3 changes: 2 additions & 1 deletion policyengine_api/services/v2/simulation_service/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ class SimulationServiceClient(Protocol):

``SimulationSubmissionService``, which checks a submission before sending
it through a client, also raises EconomyCalculationsUnsupportedError,
PolicyNotFoundError and SubmissionStorageError from its checks.
PolicyNotFoundError, PolicyParametersMissingError and
SubmissionStorageError from its checks.
"""

def submit_simulation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EconomyCalculationsUnsupportedError(ValueError):


class SubmissionStorageError(RuntimeError):
"""The countries or policies a submission is checked against could not be read."""
"""A submission's countries, policies or catalog could not be read."""


def require_economy_country(countries: Iterable[CountryRead], country_id: str) -> None:
Expand Down
Loading
Loading