From f2093a53804ee132efce16e2610f63a77fd49b84 Mon Sep 17 00:00:00 2001 From: Marius Ciepluch <11855163+norandom@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:05:20 +0100 Subject: [PATCH] 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). --- .../test_valid_benchmark_scale_validation.py | 30 +++++++++++++++++-- tests/valid_benchmark_scale_support.py | 6 ++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/tests/test_valid_benchmark_scale_validation.py b/tests/test_valid_benchmark_scale_validation.py index 3ac735ac..7ecaba71 100644 --- a/tests/test_valid_benchmark_scale_validation.py +++ b/tests/test_valid_benchmark_scale_validation.py @@ -16,6 +16,7 @@ import json import sys import tracemalloc from pathlib import Path +from types import SimpleNamespace import pytest @@ -34,6 +35,7 @@ from valid_benchmark_scale_support import ( # noqa: E402 SCALE_REQUIRED_PER_POSITIVE, ScaleLifecycle, StreamFault, + TimingAdapterProxy, case_objects_factory, component_arm_ids, 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 full.byte_count > small.byte_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) 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 "arm-combined" not in lane_arms for _arm, runtime_ms in chain.arm_runtimes: - assert runtime_ms >= 0 + assert runtime_ms > 0 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: projection = chain.projection inputs = chain.population.manifest.input_digests diff --git a/tests/valid_benchmark_scale_support.py b/tests/valid_benchmark_scale_support.py index 44b8e6db..f0892858 100644 --- a/tests/valid_benchmark_scale_support.py +++ b/tests/valid_benchmark_scale_support.py @@ -346,7 +346,7 @@ def run_streaming_validation( reasons.append(WorkflowReasonCode.INTERRUPTED) elif consumed != declared_total and not reasons: 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 return StreamRunReceipt( status, @@ -397,7 +397,7 @@ class TimingAdapterProxy: started = time.perf_counter() result = self._inner.execute(bundle) 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 @@ -500,7 +500,7 @@ def stream_with_lane( consumed += len(batch) total_bytes += sum(len(item.raw) for item in batch) 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 receipt = StreamRunReceipt( WorkflowStatus.IMPAIRED if impaired else WorkflowStatus.COMPLETED,