mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 18:28:19 +00:00
fix(valid-detection-benchmark): refuse a wrong-typed stage product as an instrument failure
A run stage that returned None or a product of another type escaped the run command as a raw AttributeError, or was passed on to the next stage. The run command now type-checks every stage product against its declared type and persists an IMPAIRED outcome at that phase with reason INSTRUMENT_FAILURE; the spent flag stays what durable custody says. Seam refusals are still re-raised and the INTERRUPTED path is unchanged.
This commit is contained in:
+28
-5
@@ -13,7 +13,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
from collections.abc import Callable, Mapping
|
from collections.abc import Callable, Mapping
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass, replace
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from typing import TypeVar, cast
|
from typing import TypeVar, cast
|
||||||
|
|
||||||
@@ -306,6 +306,13 @@ class _Interrupted(Exception):
|
|||||||
self.phase = phase
|
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:
|
def benchmark_run_command(context: BenchmarkOperatorContextV1, request: BenchmarkRunRequestV1) -> OperatorCommandResultV1:
|
||||||
"""Preflight, then execute, spend, replay, and publish with durable heartbeats."""
|
"""Preflight, then execute, spend, replay, and publish with durable heartbeats."""
|
||||||
_validated_context(context)
|
_validated_context(context)
|
||||||
@@ -314,16 +321,18 @@ def benchmark_run_command(context: BenchmarkOperatorContextV1, request: Benchmar
|
|||||||
if preflight.insufficient_outcome is not None:
|
if preflight.insufficient_outcome is not None:
|
||||||
return _terminal_result(context, OperatorCommandKind.RUN, preflight.insufficient_outcome)
|
return _terminal_result(context, OperatorCommandKind.RUN, preflight.insufficient_outcome)
|
||||||
try:
|
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:
|
if execution.impaired_outcome is not None:
|
||||||
return _terminal_result(context, OperatorCommandKind.RUN, execution.impaired_outcome)
|
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:
|
if release.refusal_reason is not None:
|
||||||
return _terminal_result(context, OperatorCommandKind.RUN, _spend_refused(request, release))
|
return _terminal_result(context, OperatorCommandKind.RUN, _spend_refused(request, release))
|
||||||
replay = _phase(context, request, WorkflowPhase.SCORING, request.stages.replay, release)
|
replay = _phase(context, request, WorkflowPhase.SCORING, request.stages.replay, release, EvaluationReplayV3)
|
||||||
publication = _phase(context, request, WorkflowPhase.REPORTING, request.stages.publication, replay)
|
publication = _phase(context, request, WorkflowPhase.REPORTING, request.stages.publication, replay, ScopedReplayPublicationV1)
|
||||||
except _Interrupted as interrupted:
|
except _Interrupted as interrupted:
|
||||||
return _terminal_result(context, OperatorCommandKind.RUN, _interrupted(context, request, interrupted.phase))
|
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)
|
return _completed_result(context, publication, replay)
|
||||||
|
|
||||||
|
|
||||||
@@ -374,6 +383,7 @@ def _phase(
|
|||||||
phase: WorkflowPhase,
|
phase: WorkflowPhase,
|
||||||
stage: Callable[..., _T],
|
stage: Callable[..., _T],
|
||||||
prior: object,
|
prior: object,
|
||||||
|
product_type: type[_T],
|
||||||
) -> _T:
|
) -> _T:
|
||||||
try:
|
try:
|
||||||
product = stage(prior)
|
product = stage(prior)
|
||||||
@@ -382,6 +392,8 @@ def _phase(
|
|||||||
except Exception:
|
except Exception:
|
||||||
raise _Interrupted(phase) from None
|
raise _Interrupted(phase) from None
|
||||||
_heartbeat(context, request, phase)
|
_heartbeat(context, request, phase)
|
||||||
|
if type(product) is not product_type:
|
||||||
|
raise _InvalidProduct(phase)
|
||||||
return product
|
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(
|
def _completed_result(
|
||||||
context: BenchmarkOperatorContextV1,
|
context: BenchmarkOperatorContextV1,
|
||||||
publication: ScopedReplayPublicationV1,
|
publication: ScopedReplayPublicationV1,
|
||||||
|
|||||||
@@ -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")))
|
run.benchmark_resume_command(crashed.context(), crashed.resume_request(workflow_digest=_digest("missing-workflow")))
|
||||||
assert unknown.value.reason is run.OperatorCommandReason.WORKFLOW_UNKNOWN
|
assert unknown.value.reason is run.OperatorCommandReason.WORKFLOW_UNKNOWN
|
||||||
assert str(unknown.value) == "operator_command_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
|
||||||
|
|||||||
Reference in New Issue
Block a user