Skip to content
1 change: 1 addition & 0 deletions changelog.d/4244.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The annual and budget-window economy routes now answer 422, naming each parameter and period that simulations can't read, for a stored policy saved before new policies' period keys and values were checked, instead of starting a job that fails or replaying its failure as a 502. Nothing is submitted and the refusal is logged at WARNING, not ERROR. Policies that pass the checks are answered as before.
19 changes: 19 additions & 0 deletions docs/canonical-spm.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,25 @@ after a deploy that changes the versions or worker app a request resolves
to, the `DELETE` finds nothing to remove (404) and the next GET starts a new
run anyway.

Before either economy route starts a job or replays a stored failure, it
checks the stored reform and baseline policies
(`policyengine_api/services/economy_policy_values.py`). Every value must pass
the value check a new policy gets (`policyengine_api/utils/policy_data_validation.py`):
a finite number, a boolean, null, a list, or `"Infinity"` or `"-Infinity"`,
also for a parameter given one value without periods. Every period key must be
one the simulation service reads: one date or a range of two, each read with
`strptime` and `%Y-%m-%d` (so unpadded months and days pass, unlike for new
policies), with the end no earlier than the start. Some policies saved before
the checks for new policies hold values no simulation can read, such as
amounts stored as text, and every job for them fails. For such a policy the route answers
HTTP 422 with `errors` listing each refused period key or value, with its
parameter (`name`), its `path` in the policy's data and the `policy_id`,
starts no job, stores nothing and logs the refusal at WARNING. A stored failure
of such a policy is answered with the same 422 instead of being replayed, and
a job already running for it is still polled until it fails. If the policies
can't be read before a replay, as during a database outage, the stored failure
is replayed as before and the skipped check is logged at WARNING.

`DELETE /<country_id>/economy/<policy_id>/over/<baseline_policy_id>` takes
the GET's query parameters, parsed by the same model, and acts on the stored
record the GET would answer from. It covers every failed job: national,
Expand Down
42 changes: 39 additions & 3 deletions policyengine_api/routes/economy_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
FailedImpactDeletion,
FailedImpactDeletionOutcome,
)
from policyengine_api.services.economy_policy_values import UnreadablePolicies
from policyengine_api.services.economy_service import (
BudgetWindowEconomicImpactResult,
EconomicImpactResult,
Expand Down Expand Up @@ -108,6 +109,27 @@ def _policy_not_found_response(error: PolicyNotFoundError) -> Response:
)


def _refused_policies(result: object) -> UnreadablePolicies | None:
"""Return the stored policies a result was refused for, if it was refused."""
refused = getattr(result, "unreadable_policies", None)
return refused if isinstance(refused, UnreadablePolicies) else None


def _unreadable_policies_response(unreadable_policies: UnreadablePolicies) -> Response:
"""Return 422 naming each stored policy entry that simulations can't read.

``errors`` lists each refused period key or value with its parameter, its
path in the policy's data and the policy's ID. No job was started and no
stored failure was replayed.
"""
return _make_error_response(
unreadable_policies.message,
HTTPStatus.UNPROCESSABLE_ENTITY,
result=None,
errors=unreadable_policies.error_entries,
)


def _annual_impact_request(
country_id: str,
policy_id: int,
Expand Down Expand Up @@ -147,8 +169,11 @@ def get_economic_impact(
fields the Simulation Entrypoint reports. Invalid query parameters give 400,
as does a ``time_period`` outside the country's ``ECONOMY_YEARS``, with
the code ``DATASET_YEAR_UNAVAILABLE`` in ``errors``, even when a result for
it is stored. A policy ID the country does not have gives 404, and a
failed calculation, or a request to the simulation service that gets no
it is stored. A policy ID the country does not have gives 404. A stored
reform or baseline policy with period keys or values simulations can't
read gives 422 naming each one in ``errors``, before any job starts or
any stored failure is replayed. A failed calculation, or a request to the
simulation service that gets no
response (after one retry if its connection broke), gives 502. One
request is an exception: for a finished stored impact, the route asks the
simulation service which worker app the request resolves to, to check
Expand Down Expand Up @@ -180,6 +205,10 @@ def get_economic_impact(
except SimulationEntrypointConnectionError as error:
return _simulation_unreachable_response(error, deletable=True)

refused_policies = _refused_policies(economic_impact_result)
if refused_policies is not None:
return _unreadable_policies_response(refused_policies)

result_dict: dict[str, str | dict | int | None] = economic_impact_result.to_dict()

if result_dict["status"] == "error":
Expand Down Expand Up @@ -269,7 +298,10 @@ def get_budget_window_economic_impact(
outside the country's ``ECONOMY_YEARS``, with the code
``DATASET_YEAR_UNAVAILABLE`` in ``errors``; a window that starts in those
years may end after them. A policy ID the country does
not have gives 404, and a request to the simulation service that gets no
not have gives 404, and a stored policy with period keys or values
simulations can't read gives 422 naming each one in ``errors``, before
any batch starts or any stored failure is replayed. A request to the
simulation service that gets no
response (after one retry if its connection broke) gives 502; a failed
calculation is reported in the body with status 200.
"""
Expand Down Expand Up @@ -304,6 +336,10 @@ def get_budget_window_economic_impact(
except SimulationEntrypointConnectionError as error:
return _simulation_unreachable_response(error, deletable=False)

refused_policies = _refused_policies(economic_impact_result)
if refused_policies is not None:
return _unreadable_policies_response(refused_policies)

result_dict = economic_impact_result.to_dict()

response = _json_response(
Expand Down
11 changes: 9 additions & 2 deletions policyengine_api/services/economy_budget_window_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ def _budget_window_result_from_state(
) -> BudgetWindowEconomicImpactResult | None:
"""Answer from this report's stored state, or None if it gives no answer.

A failure is replayed, a completed result is certified and returned, a
submitted batch is polled, and a starting claim reports every year queued.
A failure is replayed, unless a stored policy holds period keys or
values simulations can't read, which refuses the request instead; a
completed result is certified and returned, a submitted batch is
polled, and a starting claim reports every year queued.
"""
if cached_state.status == "failed":
unreadable_policies = self._unreadable_stored_policies(setup_options)
if unreadable_policies is not None:
return BudgetWindowEconomicImpactResult.refused_for_unreadable_policies(
unreadable_policies
)
return self._replay_budget_window_failure(cached_state)

if cached_state.status == "completed":
Expand Down
13 changes: 10 additions & 3 deletions policyengine_api/services/economy_budget_window_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,22 @@ def _submit_budget_window(
"""Submit the batch job if this request wins the claim, and report it queued.

Both policies load first, so a request naming a policy its country does
not have raises PolicyNotFoundError before it claims the submission. A
request that loses the claim reports the window queued without
submitting; a submission the gateway rejects as invalid is a failed result.
not have raises PolicyNotFoundError, and one whose stored policies hold
period keys or values simulations can't read is refused, before it
claims the submission. A request that loses the claim reports the
window queued without submitting; a submission the gateway rejects as
invalid is a failed result.
"""
policies = self._get_policy_jsons(
setup_options.country_id,
setup_options.baseline_policy_id,
setup_options.reform_policy_id,
)
unreadable_policies = self._unreadable_loaded_policies(setup_options, policies)
if unreadable_policies is not None:
return BudgetWindowEconomicImpactResult.refused_for_unreadable_policies(
unreadable_policies
)
years = budget_window_setup.years
cache_status = "starting-claim-hit"
observability_id_candidate = resolve_observability_id(
Expand Down
82 changes: 81 additions & 1 deletion policyengine_api/services/economy_impact_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,48 @@
from policyengine_api.gcp_logging import logger
from policyengine_api.libs.simulation_entrypoint import SimulationRunProgress
from policyengine_api.services.economic_impact_request import LocalAreaBreakdown
from policyengine_api.services.economy_policy_values import UnreadablePolicies
from policyengine_api.spm import SPMSelection
from policyengine_api.utils import budget_window as budget_window_utils

UNREADABLE_POLICY_LOG_MESSAGE = (
"Refused economic impact request for a stored policy that simulations can't read"
)
"""Logged at WARNING when a request is refused for an unreadable stored policy.

The daily telemetry error report groups records at ERROR or above. The routes
answer such a request with 422, a client error, so it forms no error group.
"""


def log_skipped_policy_check(error: Exception) -> None:
"""Log at WARNING that a stored failure is replayed without checking its policies.

The stored policies couldn't be read, as during a database outage. The
failure is replayed as before the check existed.
"""
logger.log_struct(
{
"message": (
"Could not check the stored policies before replaying a stored "
"economic impact failure"
),
"error_type": type(error).__name__,
},
severity="WARNING",
)


def _log_unreadable_policies(unreadable_policies: UnreadablePolicies) -> None:
logger.log_struct(
{
"message": UNREADABLE_POLICY_LOG_MESSAGE,
"policy_ids": ", ".join(map(str, unreadable_policies.policy_ids)),
"invalid_entries": unreadable_policies.invalid_entry_count,
},
severity="WARNING",
)


class ImpactAction(Enum):
"""What to do with an economic impact request, given its stored calculation."""
Expand Down Expand Up @@ -180,13 +219,17 @@ class EconomicImpactResult(BaseModel):
would answer from a newer job, or the failure's lifetime cannot be read.
``delete_after_seconds`` is how long until a DELETE may remove the stored
failure, 0 once it may; it is None whenever ``retry_after_seconds`` is.
``unreadable_policies`` is set when the request was refused, with no job
started and no stored failure replayed, because a stored policy holds
period keys or values simulations can't read; the route answers 422.
"""

status: ImpactStatus
data: dict | None = None
message: str | None = None
retry_after_seconds: int | None = None
delete_after_seconds: int | None = None
unreadable_policies: UnreadablePolicies | None = None
# Stage fields relayed from the Simulation Entrypoint while computing.
# Kept out of to_dict(); the route adds them only when present.
run_progress: SimulationRunProgress | None = None
Expand Down Expand Up @@ -233,6 +276,21 @@ def error(
delete_after_seconds=delete_after_seconds,
)

@classmethod
def refused_for_unreadable_policies(
cls, unreadable_policies: UnreadablePolicies
) -> "EconomicImpactResult":
"""Refuse a request whose stored policies simulations can't read.

Logged at WARNING, not ERROR: the route answers 422, a client error.
"""
_log_unreadable_policies(unreadable_policies)
return cls(
status=ImpactStatus.ERROR,
message=unreadable_policies.message,
unreadable_policies=unreadable_policies,
)


class FailedImpactDeletionOutcome(Enum):
"""What a DELETE on the economy URL did with the stored record it selected."""
Expand Down Expand Up @@ -305,11 +363,17 @@ class BudgetWindowEconomicImpactResult(BaseModel):
message: str | None = None
error: str | None = None
cache_status: str | None = None
# Set when the request was refused for an unreadable stored policy; the
# route answers 422 (see EconomicImpactResult).
unreadable_policies: UnreadablePolicies | None = None

model_config = {"frozen": True}

def to_dict(self) -> dict[str, Any]:
"""Return the response body; ``cache_status`` is internal and is left out."""
"""Return the response body, without the internal fields.

``cache_status`` and ``unreadable_policies`` are left out.
"""
return {
"status": self.status.value,
"data": self.data,
Expand Down Expand Up @@ -376,3 +440,19 @@ def failed(
error=message,
cache_status=cache_status,
)

@classmethod
def refused_for_unreadable_policies(
cls, unreadable_policies: UnreadablePolicies
) -> "BudgetWindowEconomicImpactResult":
"""Refuse a request whose stored policies simulations can't read.

Logged at WARNING, not ERROR: the route answers 422, a client error.
"""
_log_unreadable_policies(unreadable_policies)
return cls(
status=ImpactStatus.ERROR,
message=unreadable_policies.message,
error=unreadable_policies.message,
unreadable_policies=unreadable_policies,
)
Loading
Loading