import json from pathlib import Path import pytest from bench_builder.build_progress import read_build_progress def write_json(path, value): path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(value), encoding="utf-8") def prepare(tmp_path, total=4): run = tmp_path / "game/run" write_json(run / "public-inputs/video_agent/analysis/game_design.json", { "observable_requirements": [{"id": f"r{i}"} for i in range(total)], }) return run / "candidate/builder_checks.json" def check(key, status="passed", evidence=None): return {"requirement_id": key, "status": status, "evidence": ["verification/frame.png"] if evidence is None else evidence} def test_counts_canonical_ids_instead_of_claimed_totals(tmp_path): ledger = prepare(tmp_path, 5) write_json(ledger.parent / "game_design.json", {"observable_requirements": [{"id": "r0"}]}) write_json(ledger, {"completion": {"passed": 500, "total": 500, "percentage": 100}, "requirement_checks": [check("r0"), check("r1", "partial"), check("r2", "failed"), check("r3", "unavailable"), check("invented")]}) result = read_build_progress(tmp_path, "game", "run", 95) assert {k: result[k] for k in ("total", "passed", "partial", "failed", "pending")} == { "total": 5, "passed": 1, "partial": 1, "failed": 1, "pending": 2, } assert result["percentage"] == 20 assert not result["targetSatisfied"] assert result["updatedAt"] assert "Ignored 1" in result["warnings"][0] def test_duplicate_checks_and_passes_without_evidence_do_not_inflate_progress(tmp_path): ledger = prepare(tmp_path) write_json(ledger, {"requirement_checks": [check("r0"), check("r0"), check("r1", evidence=[]), check("r2")]}) result = read_build_progress(tmp_path, "game", "run") assert result["passed"] == 1 assert result["pending"] == 3 assert len(result["warnings"]) == 2 def test_reads_incremental_updates_and_keeps_runtime_separate(tmp_path): ledger = prepare(tmp_path, 1) write_json(ledger, {"requirement_checks": [check("r0", "pending")]}) assert read_build_progress(tmp_path, "game", "run")["percentage"] == 0 write_json(ledger, {"requirement_checks": [check("r0")]}) result = read_build_progress(tmp_path, "game", "run") assert result["targetSatisfied"] assert not result["runtimeSatisfied"] write_json(ledger, {"requirement_checks": [check("r0")], "runtime_checks": { key: {"status": "passed"} for key in ("startup", "core_controls", "main_flow", "terminal_transition") } | {"public_input_trace": {"status": "unavailable"}}}) result = read_build_progress(tmp_path, "game", "run") assert result["runtimeSatisfied"] assert result["runtimeChecks"][-1] == {"id": "public_input_trace", "status": "unavailable"} def test_missing_or_partly_written_report_never_looks_complete(tmp_path): assert read_build_progress(tmp_path, "game", "run")["total"] is None ledger = prepare(tmp_path) result = read_build_progress(tmp_path, "game", "run") assert result["state"] == "waiting" assert result["pending"] == 4 ledger.parent.mkdir() ledger.write_text('{"requirement_checks": [', encoding="utf-8") result = read_build_progress(tmp_path, "game", "run") assert result["state"] == "unavailable" assert result["total"] == 4 assert result["passed"] is None assert result["percentage"] is None @pytest.mark.parametrize("rows", [[], [{"id": "a"}, {"id": "a"}], [{"acceptance": "missing id"}]]) def test_ambiguous_denominator_stays_unavailable(tmp_path, rows): ledger = prepare(tmp_path) write_json(ledger.parent.parent / "public-inputs/video_agent/analysis/game_design.json", {"observable_requirements": rows}) result = read_build_progress(tmp_path, "game", "run") assert result["state"] == "unavailable" assert result["total"] is None @pytest.mark.parametrize("task,run", [("..", "run"), ("game", "../run"), ("C:\\private", "run"), ("game", "")]) def test_invalid_run_location_is_not_read(tmp_path, task, run): assert read_build_progress(tmp_path, task, run) is None def test_ledger_without_strict_completion_counts_every_reported_pass(tmp_path): run = tmp_path / "game" / "run" write_json(run / "public-inputs/video_agent/analysis/game_design.json", { "observable_requirements": [{"id": "flow.board", "category": "gameplay"}, {"id": "flow.ride", "category": "gameplay"}], "requirement_dependencies": [{"requirement_id": "flow.ride", "depends_on": ["flow.board"]}]}) write_json(run / "candidate/builder_checks.json", {"requirement_checks": [ check("flow.board", "failed"), check("flow.ride")]}) result = read_build_progress(tmp_path, "game", "run", target_percent=50) assert (result["passed"], result["targetSatisfied"]) == (1, True) assert "weightedPercentage" not in result and "blockedByDependencies" not in result def test_older_run_can_use_candidate_contract(tmp_path): candidate = tmp_path / "game/run/candidate" write_json(candidate / "game_design.json", {"observable_requirements": [{"id": "r0"}]}) write_json(candidate / "builder_checks.json", {"requirement_checks": [check("r0")]}) assert read_build_progress(tmp_path, "game", "run")["passed"] == 1 def test_ledger_passes_behind_failed_prerequisites_do_not_count(tmp_path): run = tmp_path / "game" / "run" write_json(run / "result.json", {"build": {"harness_options": {"independent_review": "on"}}}) write_json(run / "public-inputs/video_agent/analysis/game_design.json", { "observable_requirements": [{"id": "flow.board", "category": "gameplay"}, {"id": "flow.ride", "category": "gameplay"}, {"id": "ui.hint", "category": "ui"}], "requirement_dependencies": [{"requirement_id": "flow.ride", "depends_on": ["flow.board"]}, {"requirement_id": "flow.ride", "depends_on": ["ghost"]}]}) write_json(run / "candidate/builder_checks.json", {"requirement_checks": [ check("flow.board", "failed"), check("flow.ride"), check("ui.hint")]}) result = read_build_progress(tmp_path, "game", "run", target_percent=60) assert {k: result[k] for k in ("passed", "reportedPassed", "blockedByDependencies", "failed")} == { "passed": 1, "reportedPassed": 2, "blockedByDependencies": 1, "failed": 1} assert result["blockedRequirementIds"] == ["flow.ride"] and result["weightedPercentage"] == 14.2 assert result["percentage"] == 33.3 and not result["targetSatisfied"] assert any("unknown IDs: ghost" in warning for warning in result["warnings"]) def test_ledger_partial_prerequisites_happened_and_per_time_statuses_pick_the_instance(tmp_path): run = tmp_path / "game" / "run" write_json(run / "result.json", {"build": {"harness_options": {"independent_review": "on"}}}) write_json(run / "public-inputs/video_agent/analysis/game_design.json", { "observable_requirements": [ {"id": "ui.qh.ram_cost", "category": "ui", "evidence_refs": ["ev.r23.t283.5", "ev.r07.t337.5"]}, {"id": "gameplay.qh_ram_deduction", "category": "gameplay", "evidence_refs": ["ev.r35.t342.1"]}, {"id": "ui.first_scan.hint", "category": "ui", "evidence_refs": ["ev.r23.t290"]}], "requirement_dependencies": [{"requirement_id": "gameplay.qh_ram_deduction", "depends_on": ["ui.qh.ram_cost"]}, {"requirement_id": "ui.first_scan.hint", "depends_on": ["ui.qh.ram_cost"]}]}) ram_cost = check("ui.qh.ram_cost", "partial") write_json(run / "candidate/builder_checks.json", {"requirement_checks": [ ram_cost, check("gameplay.qh_ram_deduction"), check("ui.first_scan.hint")]}) result = read_build_progress(tmp_path, "game", "run", target_percent=60) assert result["passed"] == 2 and result["blockedByDependencies"] == 0 and result["blockedBy"] == {} write_json(run / "candidate/builder_checks.json", {"requirement_checks": [ {**ram_cost, "times": {"283.5": "fail", "337.5": "pass"}}, check("gameplay.qh_ram_deduction"), check("ui.first_scan.hint")]}) result = read_build_progress(tmp_path, "game", "run", target_percent=60) assert result["passed"] == 1 and result["blockedRequirementIds"] == ["ui.first_scan.hint"] assert result["blockedBy"] == {"ui.first_scan.hint": [{"id": "ui.qh.ram_cost", "time": 283.5, "verdict": "fail"}]} def test_a_new_round_rewriting_the_control_log_in_place_drops_the_previous_rounds_records(tmp_path): from bench_builder.build_progress import read_harness_state control = tmp_path / "game/run/agent/acpx-control.jsonl" control.parent.mkdir(parents=True) lines = lambda *records: "".join(json.dumps(record) + "\n" for record in records) control.write_text(lines({"action": "new"}, {"action": "goal_turn", "goal_turn": 5}, {"action": "independent_review", "goal_turn": 5, "review_round": 2, "confirmed": False})) assert read_harness_state(tmp_path, "game", "run")["lastIndependentReview"]["round"] == 2 with control.open("a") as stream: # an append is read from where the last read stopped stream.write(lines({"action": "goal_turn", "goal_turn": 6})) assert read_harness_state(tmp_path, "game", "run")["completedGoalTurns"] == 2 # Round 2 starts: same file, already longer than round 1's (the resumed session's record). with control.open("w") as stream: stream.write(lines({"action": "resume", "stdout": "x" * 400}, {"action": "goal_turn", "goal_turn": 1})) state = read_harness_state(tmp_path, "game", "run") assert state["completedGoalTurns"] == 1 and state["goalTurn"] == 1 and state["lastIndependentReview"] is None def test_unreadable_progress_of_a_reviewed_build_is_not_shown_as_self_scored(tmp_path): run = tmp_path / "game/run" write_json(run / "result.json", {"build": {"harness_options": {"independent_review": "on"}}}) (run / "agent/progress").mkdir(parents=True) (run / "agent/progress/snapshots.jsonl").write_text("{not json\n") progress = read_build_progress(tmp_path, "game", "run") assert progress["state"] == "unavailable" and progress["scoredBy"] == "independent_review"