fix(valid-detection-benchmark): floor scale runtimes at one millisecond

The 11.5 review noted that the scale validator accepted runtime_ms >= 0,
so a sub-millisecond or frozen-clock reading could be recorded as 0 ms.
Clamp every recorded runtime (per-arm, streaming, lane) to at least 1 ms
and tighten the validator to > 0. A frozen-clock test proves all three
floors; it fails against the previous source (0 ms receipts).
This commit is contained in:
Marius Ciepluch
2026-11-23 11:05:20 +01:00
parent f30a5d2cb2
commit f2093a5380
2 changed files with 31 additions and 5 deletions
+28 -2
View File
@@ -16,6 +16,7 @@ import json
import sys import sys
import tracemalloc import tracemalloc
from pathlib import Path from pathlib import Path
from types import SimpleNamespace
import pytest import pytest
@@ -34,6 +35,7 @@ from valid_benchmark_scale_support import ( # noqa: E402
SCALE_REQUIRED_PER_POSITIVE, SCALE_REQUIRED_PER_POSITIVE,
ScaleLifecycle, ScaleLifecycle,
StreamFault, StreamFault,
TimingAdapterProxy,
case_objects_factory, case_objects_factory,
component_arm_ids, component_arm_ids,
default_population_shape, default_population_shape,
@@ -126,7 +128,7 @@ def test_streaming_validation_grows_with_supplied_artifacts_per_batch(population
assert small.consumed_objects == 256 and full.consumed_objects == 840 assert small.consumed_objects == 256 and full.consumed_objects == 840
assert full.byte_count > small.byte_count > 0 assert full.byte_count > small.byte_count > 0
assert full.batch_count > small.batch_count > 0 assert full.batch_count > small.batch_count > 0
assert full.runtime_ms >= 0 and small.runtime_ms >= 0 assert full.runtime_ms > 0 and small.runtime_ms > 0
repeat = run_streaming_validation(case_objects_factory(population, 256), 64, 256) repeat = run_streaming_validation(case_objects_factory(population, 256), 64, 256)
assert repeat.chain_digest == small.chain_digest assert repeat.chain_digest == small.chain_digest
@@ -158,10 +160,34 @@ def test_cold_warm_and_per_arm_runtime_receipts_are_retained(population, chain)
assert lane_arms == set(component_arms) assert lane_arms == set(component_arms)
assert "arm-combined" not in lane_arms assert "arm-combined" not in lane_arms
for _arm, runtime_ms in chain.arm_runtimes: for _arm, runtime_ms in chain.arm_runtimes:
assert runtime_ms >= 0 assert runtime_ms > 0
assert len(chain.arm_runtimes) == len(component_arms) assert len(chain.arm_runtimes) == len(component_arms)
class _NoopAdapter:
"""A committed-adapter stand-in: the timing proxy only needs an `execute`."""
def execute(self, bundle: object) -> object: # noqa: ANN201 - test seam type
return bundle
def test_frozen_clock_measurements_are_recorded_as_one_millisecond(monkeypatch) -> None:
"""A zero elapsed reading must never become a 0 ms receipt, which `>= 0` would accept."""
frozen = SimpleNamespace(perf_counter=lambda: 1000.0)
monkeypatch.setattr("valid_benchmark_scale_support.time", frozen)
proxy = TimingAdapterProxy(_NoopAdapter())
proxy.execute(SimpleNamespace(arm=SimpleNamespace(arm_id="probe-arm")))
assert proxy.arm_runtimes == [("probe-arm", 1)]
receipt = run_streaming_validation(probe_objects_factory(8), 4, 8)
assert receipt.status is WorkflowStatus.COMPLETED and receipt.consumed_objects == 8
assert receipt.runtime_ms == 1
from valid_benchmark_scale_support import BoundedExecutionLane # noqa: E402
lane = stream_with_lane(probe_objects_factory(8), 4, 8, BoundedExecutionLane(maxsize=2))
assert lane.receipt.status is WorkflowStatus.COMPLETED and lane.receipt.consumed_objects == 8
assert lane.receipt.runtime_ms == 1
def test_execution_tier_projection_covers_every_input_without_scoring(chain: ScaleLifecycle) -> None: def test_execution_tier_projection_covers_every_input_without_scoring(chain: ScaleLifecycle) -> None:
projection = chain.projection projection = chain.projection
inputs = chain.population.manifest.input_digests inputs = chain.population.manifest.input_digests
+3 -3
View File
@@ -346,7 +346,7 @@ def run_streaming_validation(
reasons.append(WorkflowReasonCode.INTERRUPTED) reasons.append(WorkflowReasonCode.INTERRUPTED)
elif consumed != declared_total and not reasons: elif consumed != declared_total and not reasons:
reasons.append(WorkflowReasonCode.INSTRUMENT_FAILURE) reasons.append(WorkflowReasonCode.INSTRUMENT_FAILURE)
runtime_ms = int((time.perf_counter() - started) * 1000) runtime_ms = max(1, int((time.perf_counter() - started) * 1000))
status = WorkflowStatus.IMPAIRED if reasons or consumed != declared_total else WorkflowStatus.COMPLETED status = WorkflowStatus.IMPAIRED if reasons or consumed != declared_total else WorkflowStatus.COMPLETED
return StreamRunReceipt( return StreamRunReceipt(
status, status,
@@ -397,7 +397,7 @@ class TimingAdapterProxy:
started = time.perf_counter() started = time.perf_counter()
result = self._inner.execute(bundle) result = self._inner.execute(bundle)
arm_id = getattr(getattr(bundle, "arm", None), "arm_id", "unknown-arm") arm_id = getattr(getattr(bundle, "arm", None), "arm_id", "unknown-arm")
self.arm_runtimes.append((arm_id, max(0, int((time.perf_counter() - started) * 1000)))) self.arm_runtimes.append((arm_id, max(1, int((time.perf_counter() - started) * 1000))))
return result return result
@@ -500,7 +500,7 @@ def stream_with_lane(
consumed += len(batch) consumed += len(batch)
total_bytes += sum(len(item.raw) for item in batch) total_bytes += sum(len(item.raw) for item in batch)
chain = _chain_step(chain, _lane_marker(batch)[2]) chain = _chain_step(chain, _lane_marker(batch)[2])
runtime_ms = int((time.perf_counter() - started) * 1000) runtime_ms = max(1, int((time.perf_counter() - started) * 1000))
impaired = bool(refusals) or consumed < declared_total impaired = bool(refusals) or consumed < declared_total
receipt = StreamRunReceipt( receipt = StreamRunReceipt(
WorkflowStatus.IMPAIRED if impaired else WorkflowStatus.COMPLETED, WorkflowStatus.IMPAIRED if impaired else WorkflowStatus.COMPLETED,