diff --git a/.github/workflows/offline-climate-regression.yml b/.github/workflows/offline-climate-regression.yml index a79b463..5a343ea 100644 --- a/.github/workflows/offline-climate-regression.yml +++ b/.github/workflows/offline-climate-regression.yml @@ -4,6 +4,8 @@ on: paths: - 'examples/scenario/offline_regression.py' - 'examples/scenario/test_offline_regression.py' + - 'examples/scenario/step_diagnostics.py' + - 'examples/scenario/test_step_diagnostics.py' - 'examples/scenario/editor.py' - 'examples/scenario/test_editor_gui.py' - 'examples/scenario/PORTABLE.md' @@ -24,6 +26,8 @@ on: paths: - 'examples/scenario/offline_regression.py' - 'examples/scenario/test_offline_regression.py' + - 'examples/scenario/step_diagnostics.py' + - 'examples/scenario/test_step_diagnostics.py' - 'examples/scenario/editor.py' - 'examples/scenario/test_editor_gui.py' - 'examples/scenario/PORTABLE.md' @@ -59,7 +63,8 @@ jobs: - run: | python -m pip install --require-hashes --only-binary=:all: -r examples/scenario/requirements-offline.txt - run: python -m pip check - - run: python -m unittest examples.scenario.test_offline_regression examples.scenario.test_editor -v + - run: python -m unittest examples.scenario.test_offline_regression examples.scenario.test_editor examples.scenario.test_step_diagnostics -v + - run: python -m examples.scenario.step_diagnostics --manifest examples/scenario/climate-timed.json - run: python -m examples.scenario.offline_regression - run: python -m examples.scenario.offline_regression --manifest examples/scenario/climate-custom.json - run: python -m examples.scenario.offline_regression --manifest examples/scenario/switch-custom.json diff --git a/examples/scenario/README.md b/examples/scenario/README.md index 84cad5c..fa0f5c5 100644 --- a/examples/scenario/README.md +++ b/examples/scenario/README.md @@ -191,3 +191,18 @@ The runner ignores ambient PYTEST_* variables when invoking pytest for the fixed contract and integration tests. Contract plugin autoload is explicitly disabled; integration retains the pinned harness's plugin discovery. This protects catalog consistency, not execution isolation. + +### Opt-in step diagnostics (source checkout only) + + python -m examples.scenario.step_diagnostics --manifest examples/scenario/climate-timed.json + +This separate experimental command reports 1-based step indices, virtual +elapsed milliseconds, pass/fail/error/not-run states, and fixed reason codes. +A mismatch in the fourth expectation produces `step: 4`, `elapsed_ms: 31000`, +`reason: expectation-mismatch`, and category counts. Later steps are marked +not-run if replay or comparison cannot continue. It checks that the trusted +reader consumes exactly one frame per step. Reports omit raw snapshots, +values, arbitrary exception text and input paths. Exit codes remain 0/1/2. +This is not yet integrated into the graphical editor or Studio viewer and +is not included in the immutable published P2 ZIP. Use the existing +hash-pinned offline dependencies and reviewed synthetic manifests only. diff --git a/examples/scenario/step_diagnostics.py b/examples/scenario/step_diagnostics.py new file mode 100644 index 0000000..3a3361d --- /dev/null +++ b/examples/scenario/step_diagnostics.py @@ -0,0 +1,77 @@ +"""Opt-in local diagnostics for reviewed synthetic scenario manifests.""" +import argparse +import json +from pathlib import Path + +from iot_fixturekit import FixtureError +from iot_fixturekit.cli import read_json +from examples.diff.compare import compare_synthetic +from examples.scenario.offline_regression import ( + REQUEST, SWITCH_REQUEST, OrderedReplay, load_manifest, manifest_device, + read_state, read_switch, +) + +ERRORS = (FixtureError, ValueError, OSError, TypeError, KeyError, RecursionError) + + +def diagnose(document, reader=None): + """Return bounded step indices, elapsed time and categories, never raw values.""" + result = {"schema_version": 1, "status": "error", "stage": "manifest", "steps": []} + try: + device = manifest_device(document) + frames, expected = load_manifest(document) + except ERRORS: + return result + identity, default = (REQUEST, read_state) if device == "climate" else (SWITCH_REQUEST, read_switch) + session = OrderedReplay(frames, identity) + reader = default if reader is None else reader + failed = False + for index, (step, expectation) in enumerate(zip(document["steps"], expected), 1): + session.advance(step.get("advance_ms", 0)) + row = {"step": index, "elapsed_ms": session.monotonic_ms()} + remaining = len(session.frames) + try: + observed = reader(session) + except ERRORS: + result["steps"].append({**row, "status": "error", "reason": "reader-error"}) + result["stage"] = "replay" + break + if len(session.frames) != remaining - 1: + result["steps"].append({**row, "status": "error", "reason": "unexpected-frame-count"}) + result["stage"] = "replay" + break + try: + differences = compare_synthetic(expectation, observed) + except ERRORS: + result["steps"].append({**row, "status": "error", "reason": "comparison-error"}) + result["stage"] = "comparison" + break + if differences: + failed = True + result["steps"].append({**row, "status": "fail", "reason": "expectation-mismatch", + "differences": differences}) + else: + result["steps"].append({**row, "status": "pass"}) + else: + result["status"] = "fail" if failed else "pass" + result["stage"] = "comparison" if failed else "complete" + return result + for index in range(len(result["steps"]) + 1, len(expected) + 1): + result["steps"].append({"step": index, "status": "not-run"}) + return result + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--manifest", required=True, type=Path) + args = parser.parse_args(argv) + try: + report = diagnose(read_json(args.manifest)) + except ERRORS: + report = {"schema_version": 1, "status": "error", "stage": "manifest", "steps": []} + print(json.dumps(report, sort_keys=True)) + return {"pass": 0, "fail": 1, "error": 2}[report["status"]] + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/examples/scenario/test_step_diagnostics.py b/examples/scenario/test_step_diagnostics.py new file mode 100644 index 0000000..2cd1ca5 --- /dev/null +++ b/examples/scenario/test_step_diagnostics.py @@ -0,0 +1,50 @@ +"""Step diagnostics must locate failures without exposing snapshots.""" +import json +from pathlib import Path +import unittest + +from examples.scenario.offline_regression import read_state +from examples.scenario.step_diagnostics import diagnose +from iot_fixturekit import FixtureError + + +class StepDiagnosticsTests(unittest.TestCase): + def document(self): + return json.loads(Path(__file__).with_name("climate-timed.json").read_text()) + + def test_pass_timeline(self): + report = diagnose(self.document()) + self.assertEqual(report["status"], "pass") + self.assertEqual([s["elapsed_ms"] for s in report["steps"]], [0, 1000, 30000, 31000]) + self.assertEqual([s["status"] for s in report["steps"]], ["pass"] * 4) + + def test_mismatch_located_without_values(self): + document = self.document() + document["steps"][-1]["expected"]["temperature"] = 24 + report = diagnose(document) + self.assertEqual(report["status"], "fail") + self.assertEqual(report["steps"][-1], {"step": 4, "elapsed_ms": 31000, + "status": "fail", "reason": "expectation-mismatch", "differences": {"values_changed": 1}}) + self.assertNotIn("temperature", json.dumps(report)) + + def test_reader_failure_marks_remaining_not_run(self): + def reader(session): + if session.monotonic_ms() == 1000: + raise FixtureError("sensitive sentinel") + return read_state(session) + report = diagnose(self.document(), reader) + self.assertEqual(report["status"], "error") + self.assertEqual(report["steps"][1]["reason"], "reader-error") + self.assertEqual(report["steps"][2:], [{"step": 3, "status": "not-run"}, {"step": 4, "status": "not-run"}]) + self.assertNotIn("sentinel", json.dumps(report)) + + def test_missing_consumption(self): + report = diagnose(self.document(), lambda session: {"status": "unavailable"}) + self.assertEqual(report["steps"][0]["reason"], "unexpected-frame-count") + + def test_invalid_manifest(self): + self.assertEqual(diagnose({})["steps"], []) + + def test_switch(self): + document = json.loads(Path(__file__).with_name("switch-custom.json").read_text()) + self.assertEqual(diagnose(document)["status"], "pass")