diff --git a/benchmarks/valid/run.py b/benchmarks/valid/run.py index c29f1032..4727bf75 100644 --- a/benchmarks/valid/run.py +++ b/benchmarks/valid/run.py @@ -13,7 +13,7 @@ from __future__ import annotations import json from collections.abc import Callable, Mapping -from dataclasses import dataclass +from dataclasses import dataclass, replace from enum import StrEnum from typing import TypeVar, cast @@ -306,6 +306,13 @@ class _Interrupted(Exception): self.phase = phase +class _InvalidProduct(Exception): + """Internal marker carrying the phase whose stage returned no product of its declared type.""" + + def __init__(self, phase: WorkflowPhase) -> None: + self.phase = phase + + def benchmark_run_command(context: BenchmarkOperatorContextV1, request: BenchmarkRunRequestV1) -> OperatorCommandResultV1: """Preflight, then execute, spend, replay, and publish with durable heartbeats.""" _validated_context(context) @@ -314,16 +321,18 @@ def benchmark_run_command(context: BenchmarkOperatorContextV1, request: Benchmar if preflight.insufficient_outcome is not None: return _terminal_result(context, OperatorCommandKind.RUN, preflight.insufficient_outcome) try: - execution = _phase(context, request, WorkflowPhase.ARM_EXECUTION, request.stages.execution, preflight) + execution = _phase(context, request, WorkflowPhase.ARM_EXECUTION, request.stages.execution, preflight, AdmissionExecutionV1) if execution.impaired_outcome is not None: return _terminal_result(context, OperatorCommandKind.RUN, execution.impaired_outcome) - release = _phase(context, request, WorkflowPhase.CUSTODY_SPEND, request.stages.spend, execution) + release = _phase(context, request, WorkflowPhase.CUSTODY_SPEND, request.stages.spend, execution, AdmissionSpendReleaseV1) if release.refusal_reason is not None: return _terminal_result(context, OperatorCommandKind.RUN, _spend_refused(request, release)) - replay = _phase(context, request, WorkflowPhase.SCORING, request.stages.replay, release) - publication = _phase(context, request, WorkflowPhase.REPORTING, request.stages.publication, replay) + replay = _phase(context, request, WorkflowPhase.SCORING, request.stages.replay, release, EvaluationReplayV3) + publication = _phase(context, request, WorkflowPhase.REPORTING, request.stages.publication, replay, ScopedReplayPublicationV1) except _Interrupted as interrupted: return _terminal_result(context, OperatorCommandKind.RUN, _interrupted(context, request, interrupted.phase)) + except _InvalidProduct as invalid: + return _terminal_result(context, OperatorCommandKind.RUN, _instrument_failure(context, request, invalid.phase)) return _completed_result(context, publication, replay) @@ -374,6 +383,7 @@ def _phase( phase: WorkflowPhase, stage: Callable[..., _T], prior: object, + product_type: type[_T], ) -> _T: try: product = stage(prior) @@ -382,6 +392,8 @@ def _phase( except Exception: raise _Interrupted(phase) from None _heartbeat(context, request, phase) + if type(product) is not product_type: + raise _InvalidProduct(phase) return product @@ -427,6 +439,17 @@ def _interrupted(context: BenchmarkOperatorContextV1, request: BenchmarkRunReque ) +def _instrument_failure( + context: BenchmarkOperatorContextV1, request: BenchmarkRunRequestV1, phase: WorkflowPhase +) -> BenchmarkWorkflowOutcomeV1: + """A stage returned no product of its declared type: a measurement failure; the spent flag stays durable.""" + return replace( + _interrupted(context, request, phase), + reason_codes=(WorkflowReasonCode.INSTRUMENT_FAILURE,), + outcome_digest="", + ) + + def _completed_result( context: BenchmarkOperatorContextV1, publication: ScopedReplayPublicationV1, diff --git a/tests/test_valid_benchmark_run.py b/tests/test_valid_benchmark_run.py index e69fa9c7..a0a8cf7a 100644 --- a/tests/test_valid_benchmark_run.py +++ b/tests/test_valid_benchmark_run.py @@ -725,3 +725,40 @@ def test_command_refusals_stay_closed_without_caller_content() -> None: run.benchmark_resume_command(crashed.context(), crashed.resume_request(workflow_digest=_digest("missing-workflow"))) assert unknown.value.reason is run.OperatorCommandReason.WORKFLOW_UNKNOWN assert str(unknown.value) == "operator_command_workflow_unknown" + + +_STAGE_PHASES = ( + ("execution", WorkflowPhase.ARM_EXECUTION), + ("spend", WorkflowPhase.CUSTODY_SPEND), + ("replay", WorkflowPhase.SCORING), + ("publication", WorkflowPhase.REPORTING), +) +_WRONG_PRODUCT = "not-a-stage-product" + + +@pytest.mark.parametrize("product", [None, _WRONG_PRODUCT], ids=["none", "wrong-type"]) +@pytest.mark.parametrize(("stage_name", "phase"), _STAGE_PHASES) +def test_run_command_closes_a_missing_or_wrong_typed_stage_product(monkeypatch, stage_name, phase, product) -> None: + """A stage product of the wrong type is a measurement failure, never a raw AttributeError.""" + environment = _CommandEnv() + stages = replace(environment.stages(replay=_scoped_replay(monkeypatch)[1]), **{stage_name: lambda _prior: product}) + + result = run.benchmark_run_command(environment.context(), environment.run_request(stages)) + + assert result.command is run.OperatorCommandKind.RUN + assert result.outcome.status is WorkflowStatus.IMPAIRED + assert result.outcome.phase is phase + assert result.outcome.reason_codes == (WorkflowReasonCode.INSTRUMENT_FAILURE,) + assert result.outcome.evaluation_replay_ref is None + spent = phase in (WorkflowPhase.SCORING, WorkflowPhase.REPORTING) + assert result.outcome.labels_spent is spent + assert _custody_status(environment) is (PopulationStatus.SPENT if spent else PopulationStatus.AVAILABLE) + assert environment.loader.reads == int(spent) + assert not any(operation.startswith("put:replays/") for operation in environment.durable.operations) + assert result.outcome_reference is not None + status = run.benchmark_status_command(environment.context(), environment.status_request(result.outcome_reference)) + assert status.outcome == result.outcome + assert status.report_bytes == result.report_bytes + assert result.report_text.isascii() + assert _WRONG_PRODUCT not in result.report_text + assert "Traceback" not in result.report_text