From 96b327fe1222d297275f340c35e4a428393add2b Mon Sep 17 00:00:00 2001 From: zengchuang Date: Thu, 19 Nov 2026 23:41:43 +0800 Subject: [PATCH] test(apps): verify fixed views over a real CPU record Signed-off-by: zengchuang Agent-assisted-by: Codex (author) Signed-off-by: zengchuang --- docs/module-layout.md | 4 +- tests/test_apps_fixed_cpu_views.py | 129 +++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 tests/test_apps_fixed_cpu_views.py diff --git a/docs/module-layout.md b/docs/module-layout.md index 2b77230c..d84c22a4 100644 --- a/docs/module-layout.md +++ b/docs/module-layout.md @@ -111,8 +111,8 @@ Both project adapters resolve their sibling worker in the canonical directory. The registered launchers use those files with the approved interpreter and the existing `-I -B` flags. Each worker loads without InferRSI installed or a package context, and its top-level imports are standard-library-only. Legacy worker -paths retain Python import compatibility; external interpreters are launched -against the canonical files. Grant schemas, read-only access, limits, revocation +files are importable compatibility shims, not runnable scripts. External +interpreters are launched against the canonical files. Grant schemas, read-only access, limits, revocation and receipts are unchanged. MCP still starts through `inferrsi mindie serve`. ### Core and technical packages diff --git a/tests/test_apps_fixed_cpu_views.py b/tests/test_apps_fixed_cpu_views.py new file mode 100644 index 00000000..7d2e7540 --- /dev/null +++ b/tests/test_apps_fixed_cpu_views.py @@ -0,0 +1,129 @@ +"""Keep real CPU facts fixed across the Apps read-only views.""" + +import json +import subprocess +import sys +from pathlib import Path +from threading import Thread +from unittest.mock import patch +from urllib.request import urlopen + +from inferrsi.apps import api as apps_api +from inferrsi.apps.cli import main +from inferrsi.apps.views.dashboard_live import DashboardServer +from inferrsi.contracts.json_v1 import encode +from inferrsi.harness.api import Harness +from inferrsi.harness.generation import prepare_generation +from inferrsi.harness.run_store import Claim, RunStore +from tests import test_harness_generation_seal as seal_tests +from tests.test_apps_generation_plan import generation as generation +from tests.test_harness_generation_seal import cpu_generation as cpu_generation + + +def test_real_cpu_record_is_shared_by_all_read_only_surfaces(cpu_generation, tmp_path, capsys): + store, target, harness, _, inputs = cpu_generation + spec = prepare_generation(store, target, **inputs) + record = harness.execute(spec) + assert record["exit_code"] == 0 + record_ref = record["record_ref"] + before = store.committed(record["attempt_id"]) + archive_root = tmp_path / "archive" + archive_root.mkdir() + config = { + "schema_version": 1, + "archive_namespace": "fixtures", + "harness_namespace": "generation", + "mounts": [ + {"kind": "directory", "namespace": "fixtures", "prefix": "fixtures", + "root": str(archive_root)}, + {"kind": "harness_sqlite", "namespace": "generation", "prefix": "stores/generation", + "root": str(store.root)}, + ], + } + flags = [] + for name, value in (("record-ref", record_ref), ("storage-config", config)): + path = tmp_path / (name + ".json") + path.write_bytes(encode(value)) + flags.extend(("--" + name, str(path))) + + def forbidden(*args, **kwargs): + raise AssertionError("a read-only surface attempted execution, assessment or a write") + + with ( + patch.object(Harness, "execute", forbidden), + patch.object(Harness, "allocate", forbidden), + patch.object(RunStore, "claim", forbidden), + patch.object(Claim, "commit", forbidden), + patch.object(apps_api, "assess", forbidden), + patch.object(subprocess, "Popen", forbidden), + ): + expected = apps_api.read_attempt_view(record_ref, storage_config=config) + assert expected["assessment"] is None + assert expected["assessment_ref"] is None + assert expected["execution"]["attempt_id"] == record["attempt_id"] + assert expected["execution"]["exit_code"] == 0 + + assert main(["status", *flags]) == 0 + assert json.loads(capsys.readouterr().out) == expected + assert main(["trace", *flags]) == 0 + traced = json.loads(capsys.readouterr().out) + assert traced == apps_api.read_attempt_view( + record_ref, storage_config=config, include_trace=True, + ) + assert {key: traced[key] for key in expected} == expected + assert traced["trace"]["steps"] + + documents = {} + for command in ("report", "dashboard"): + output = tmp_path / (command + ".html") + assert main([command, *flags, "--output", str(output)]) == 0 + assert json.loads(capsys.readouterr().out) == {command: str(output.resolve())} + document = output.read_text(encoding="utf-8") + assert record_ref["artifact"]["sha256"] in document + assert record["attempt_id"] in document + documents[command] = document + with DashboardServer(None, None, 0, fixed_attempt=traced) as server: + thread = Thread(target=server.serve_forever, daemon=True) + thread.start() + try: + address = f"http://127.0.0.1:{server.server_port}" + with urlopen(address + "/api/state", timeout=5) as response: + assert json.load(response)["data"]["fixed_attempt"] == traced + with urlopen(address + "/", timeout=5) as response: + assert response.read().decode("utf-8") == documents["dashboard"] + finally: + server.shutdown() + thread.join(timeout=5) + assert not thread.is_alive() + + child = """ +import json, sys +sys.path.insert(0, sys.argv[1]) +from pathlib import Path +from unittest.mock import patch +from inferrsi.apps import api +from inferrsi.harness.api import Harness +from inferrsi.harness.run_store import Claim, RunStore +def forbidden(*args, **kwargs): + raise AssertionError('read-only child attempted execution, assessment or a write') +with patch.object(Harness, 'execute', forbidden), patch.object(Harness, 'allocate', forbidden), \ + patch.object(RunStore, 'claim', forbidden), \ + patch.object(Claim, 'commit', forbidden), patch.object(api, 'assess', forbidden): + view = api.read_attempt_view( + json.loads(Path(sys.argv[2]).read_text()), + storage_config=json.loads(Path(sys.argv[3]).read_text()), + include_trace=True, + ) +print(json.dumps(view)) +""" + source = str(Path(seal_tests.__file__).resolve().parents[1] / "src") + result = subprocess.run( + [sys.executable, "-I", "-c", child, source, + str(tmp_path / "record-ref.json"), str(tmp_path / "storage-config.json")], + cwd=tmp_path, capture_output=True, text=True, timeout=30, + ) + assert result.returncode == 0, result.stderr + assert json.loads(result.stdout) == traced + after = store.committed(record["attempt_id"]) + assert after.record_ref == before.record_ref == record_ref + assert after.payload_bytes == before.payload_bytes