Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions tests/apps/test_search_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,20 +277,35 @@ def execute(command, cwd, output, deadline, *args, **kwargs):
("assess", 1110.0, 5.0)]


def test_engine_known_timeout_settles_usage_and_releases_domain(tmp_path, monkeypatch):
@pytest.mark.parametrize(
("elapsed_seconds", "blocked"),
[(1, False), (3, True)],
ids=["within-budget", "over-budget"],
)
def test_engine_known_timeout_settles_usage_and_releases_domain(
tmp_path, monkeypatch, elapsed_seconds, blocked
):
evaluator = EngineEvaluator(
"qwen21-denoise-npu", tmp_path / "frozen",
arguments=["--pairs", "1", "--samples", "1", "--warmups", "0"],
)
executions = []

def protocol(root, protocol_sha256, operation, **kwargs):
# Keep protocol startup out of the time available to the real trial.
if operation == "arguments":
return []
assert operation == "assess" and kwargs["execution"]["status"] == "timeout"
raise TaskProtocolError("timeout", "assessment deadline elapsed", cleanup_verified=True)

def timeout(command, cwd, output, deadline, *args, **kwargs):
# Exercise real process cleanup without model/device execution.
result = execute_trial((sys.executable, "-c", "import time; time.sleep(60)"),
cwd, output, deadline, *args, **kwargs)
executions.append(result)
return result

monkeypatch.setattr("inferrsi.apps.workflows.search_benchmarks.run_task_protocol", protocol)
monkeypatch.setattr("inferrsi.apps.workflows.search_benchmarks.execute_trial", timeout)
source = tmp_path / "source"
source.mkdir()
Expand All @@ -299,25 +314,39 @@ def timeout(command, cwd, output, deadline, *args, **kwargs):
{"evaluation": 2000}, evaluator, domain="cpu-timeout-test")
root = adapter.import_root(source, snapshot="test-snapshot", context={})
control = WorkControl(RunStore(tmp_path / "store", "timeout-test"))
control.create("campaign", {}, {"searches": {}}, cap_ms=20000, final_reserve_ms=3000,
deadline=time.time() + 20)
configuration = contract(checks=("evaluation",), objective_check="evaluation")

# Control only the accounting clocks. The child still runs against a real deadline.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 case 的关键是只替换 accounting clock,child 仍走真实 deadline。可以把这段移到名字直接表达该边界的 helper/fixture,再删掉新增的解释性注释;不需要改真实子进程和 cleanup 断言。按当前 review-score,这类新增维护注释会让 Simplicity 记 0。

launch_time = time.time()
monkeypatch.setattr(
"inferrsi.harness.manager.search.work_control.time",
SimpleNamespace(time=lambda: launch_time),
)
adapter_ticks = iter((0, elapsed_seconds))
monkeypatch.setattr(
"inferrsi.harness.manager.search.search_adapter.time",
SimpleNamespace(monotonic=adapter_ticks.__next__),
)
control.create("campaign", {}, {"searches": {}}, cap_ms=20000, final_reserve_ms=3000,
deadline=launch_time + 20)
with pytest.raises(ValueError, match="Root lacks complete"):
qualify_root(control, "campaign", adapter, root, configuration)
assert executions[0]["status"] == "timeout" and executions[0]["cleanup_verified"] is True
assert len(executions) == 1
execution = executions[0]
assert execution["pid"] > 0
assert execution["status"] == "timeout" and execution["cleanup_verified"] is True
reopened = WorkControl(RunStore(control.store.root, control.store.namespace))
doc = reopened.read("campaign")
operation, settled = next(iter(doc["operations"].items()))
assert settled["status"] == "settled" and settled["actual_ms"] > 0
assert settled["status"] == "settled" and settled["actual_ms"] == elapsed_seconds * 1000
assert doc["spent_ms"] == settled["actual_ms"]
assert settled["receipt"]["verdict"] == "INCONCLUSIVE"
assert settled["receipt"]["cleanup_verified"] is True
assert adapter.recover(operation, {"kind": "verify", "binding": settled["binding"]}) == (
settled["receipt"])
with reopened.store._connection() as db:
assert db.execute("SELECT 1 FROM work_domains").fetchone() is None
# Deadline overruns remain blocking; a clean timeout is not a free retry.
assert doc["blocked"]
assert doc["blocked"] is blocked


@pytest.mark.parametrize("stage", ["arguments", "assess"])
Expand Down
24 changes: 22 additions & 2 deletions tests/knowledge/test_knowledge_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import hashlib
import json
import os
import py_compile
import sqlite3
import subprocess
import sys
Expand All @@ -20,6 +21,7 @@
RevisionConflict,
)
from inferrsi.knowledge.knowledge_store import KnowledgeStore
from inferrsi.knowledge.skill_catalog import is_bytecode_cache

REPO = Path(__file__).resolve().parents[2]
ROOT = REPO / "src/inferrsi/knowledge/data/skill_library"
Expand Down Expand Up @@ -408,16 +410,34 @@ def test_real_shared_resource_bundle_keeps_old_dependency_bytes(tmp_path):
)


def _digest(paths):
def _digest(paths, *, base=REPO):
digest = hashlib.sha256()
for root in paths:
files = sorted(p for p in root.rglob("*") if p.is_file()) if root.is_dir() else [root]
for path in files:
digest.update(path.relative_to(REPO).as_posix().encode())
# Parallel tests import evidence validators and can generate bytecode here.
if is_bytecode_cache(path):
continue
digest.update(path.relative_to(base).as_posix().encode())
digest.update(path.read_bytes())
return digest.hexdigest()


def test_source_digest_ignores_import_bytecode(tmp_path):
evidence = tmp_path / "evidence"
evidence.mkdir()
validator = evidence / "validator.py"
validator.write_text("value = 1\n", encoding="utf-8")
before = _digest([evidence], base=tmp_path)

cache = evidence / "__pycache__" / f"validator.{sys.implementation.cache_tag}.pyc"
py_compile.compile(str(validator), cfile=str(cache), doraise=True)
assert _digest([evidence], base=tmp_path) == before

validator.write_text("value = 2\n", encoding="utf-8")
assert _digest([evidence], base=tmp_path) != before


def test_operations_leave_sources_evidence_and_matrices_unchanged(tmp_path):
watched = [
ROOT,
Expand Down
Loading