diff --git a/SHA256SUMS b/SHA256SUMS index d1190f4..eccb21e 100644 --- a/SHA256SUMS +++ b/SHA256SUMS @@ -1,14 +1,14 @@ effebd70742b26c7000ead248418c3d560d93062ccfe1ff68ef5e456a2a4da77 cpp/frontier_iii14_iii20_prover.cpp -307e856631aad36762d7cceb6255e3314185ed33aa3700a63fb93bee10f4c872 cpp/soko_engine.cpp +4da54c90582c69327f29105b426df475dd613668486e5bdfabc3c697835aa699 cpp/soko_engine.cpp 78f661f4f89d8cd4a28364a4386742420837367f7f752f8f514273be6e939b24 soko_mcp/__init__.py 1e27da781dc8bd819a0653df324608a15c919e4b21f5042a7814a3f33eae8848 soko_mcp/__main__.py 4c7660e4c53f7b47c4b36fe2334dc52e46e01679b7a5e77ba43c562162642e1e soko_mcp/analyzer.py 43d8b8aaf29c3e9fb857ea803a40edf5cc6e230e8e81e2c8eb002d41364f75ed soko_mcp/catalog.py -336da844367ed6808c60cbb2b7f9af065f728f04cb168d8efd13ffe6770b3328 soko_mcp/checker_auditor.py +24fdc7a595cae79cc48ca059980c36aec95be916817fb4380e650331540b4259 soko_mcp/checker_auditor.py c8c5b01f4e6ad49c6586439082778d3f806c6891d07d1a76912c261533c247ac soko_mcp/data/catalog.json -22d12d869a96f59fb21f0e03cbb378570feddb36e4c9d523a3ff7c265c26b82e soko_mcp/engine_bridge.py -2bad704a7d39ee3a63505918e5917a097a0ba6da27a3e6b0bf246edc9dc76d7c soko_mcp/evidence.py -447b6c006dfe36347a96bd519b5ff23b76ed40352c0e5ccb32d42f496f402951 soko_mcp/flat_oracle.py +c84eef014119ff2e0c1ba815c3c696700fff82713981edd78eabcde9d017b450 soko_mcp/engine_bridge.py +84797aa46359316ecf75c48483865ceeee21441a690ef3896000ee76da115ab3 soko_mcp/evidence.py +9e8540bfda9cb467bc842711296948172bca50ea35a70b8387d7e7de2ad110e7 soko_mcp/flat_oracle.py 0a3557fa10865270258570aed9f310a4860870a4469e583d61bb3693a3234e49 soko_mcp/formal_kernel.py b3ab54723b90072e92fd91affe560b2a07c957f42b3137ed3870478be200f2b2 soko_mcp/integrity.py 6adb9f952fd12f0385444be24f4ba45b3470873168fc43663f37026177c92d40 soko_mcp/invariant_synth.py @@ -16,6 +16,6 @@ b3ab54723b90072e92fd91affe560b2a07c957f42b3137ed3870478be200f2b2 soko_mcp/integ 8145f311051a3f6bfa94467da7a6d9a30535f9edf1bb00662982da70e9cc9853 soko_mcp/oracle.py e9b288556c51363e1edbb161c2c119c540ad8aa6e5ae31e23739a6284f2800a6 soko_mcp/original.py fe92ee760e3282c941641bebb47f7b19adfc2011279f1c0fde10f926ed14e99a soko_mcp/play.py -e8602e188cbcd8bffc997fe2d38404f4f7646d2259b4fc06aad28cde9490f0ca soko_mcp/server.py -01c6dc6046cca9771b64b6a51ac980f589e6623beb53e64ad99561c619416776 tracks/iii-14/verify.py -e2e855c4501c479db8f3c2f34f9e2f858099d8d99a6a3a79a3dd12187ba4ecf3 tracks/iii-20/verify.py +e37d920de311e77208627487ac01c9fda674241488d7ad862a9856150f48e228 soko_mcp/server.py +201bd6a5026cc666ba9faa09f276cad6d08cfef798fdbbd8ffaa7ca7dc6bfa69 tracks/iii-14/verify.py +e4c7451233f2e018d6489fc2c226ac951a9e5835e5c3611384bca7800d72174b tracks/iii-20/verify.py diff --git a/cpp/soko_engine.cpp b/cpp/soko_engine.cpp index 910254e..b60c6b0 100644 --- a/cpp/soko_engine.cpp +++ b/cpp/soko_engine.cpp @@ -13,10 +13,31 @@ * - Collision-safe bucket chaining in `exact_window_shortcut`. */ -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include using namespace std; static constexpr int MAX_CELLS = 512; +static constexpr uint8_t OBJ_CORPSE = 21; static const int DX[4] = {0, 0, -1, 1}; static const int DY[4] = {-1, 1, 0, 0}; static const char DCH[5] = {'U', 'D', 'L', 'R', 'A'}; @@ -145,6 +166,7 @@ bool run_late(const LevelMeta& M, DynState& s) { } } } + bool corpse = false; if (M.part >= 2) { for (int y = 0; y < M.h; ++y) for (int x = 0; x < M.w; ++x) @@ -169,7 +191,7 @@ bool run_late(const LevelMeta& M, DynState& s) { for (int i = 0; i < M.n; ++i) { if (dying[i]) { if (is_monster(M.part, s.obj[i])) s.obj[i] = 0; - else if (is_hero(s.obj[i])) return true; + else if (is_hero(s.obj[i])) { s.obj[i] = OBJ_CORPSE; corpse = true; } } } } @@ -181,7 +203,7 @@ bool run_late(const LevelMeta& M, DynState& s) { for (int idx : M.y_doors_init) if (s.obj[idx] == 18) s.obj[idx] = 0; } else if (any_u && s.y_open) { s.y_open = 0; - for (int idx : M.y_doors_init) { if (is_hero(s.obj[idx])) return true; s.obj[idx] = 18; } + for (int idx : M.y_doors_init) { if (is_hero(s.obj[idx])) corpse = true; s.obj[idx] = 18; } } } if (!M.p_switches.empty()) { @@ -192,7 +214,7 @@ bool run_late(const LevelMeta& M, DynState& s) { for (int idx : M.p_doors_init) if (s.obj[idx] == 19) s.obj[idx] = 0; } else if (any_u && s.p_open) { s.p_open = 0; - for (int idx : M.p_doors_init) { if (is_hero(s.obj[idx])) return true; s.obj[idx] = 19; } + for (int idx : M.p_doors_init) { if (is_hero(s.obj[idx])) corpse = true; s.obj[idx] = 19; } } } // The monster gate opens after the door phase: a closing colored door may @@ -205,7 +227,7 @@ bool run_late(const LevelMeta& M, DynState& s) { if (M.has_bard) { for (int i = 0; i < M.n; ++i) if (s.has_note(i) && !is_moveable(M.part, s.obj[i])) s.notes[i >> 6] &= ~(1ULL << (i & 63)); } - return false; + return corpse; } inline bool check_win(const LevelMeta& M, const DynState& s) { @@ -535,6 +557,7 @@ string render_ascii(const LevelMeta& M, const DynState& s) { else if (o == 15) ch = 'O'; else if (o == 16) ch = 'L'; else if (o == 17) ch = 'R'; else if (o == 18) ch = '['; else if (o == 19) ch = ']'; else if (o == 20) ch = 'M'; + else if (o == OBJ_CORPSE) ch = 'X'; else if (M.floor_grid[i] == 1) ch = 'G'; else if (M.floor_grid[i] == 2) ch = '{'; else if (M.floor_grid[i] == 3) ch = '}'; @@ -590,7 +613,8 @@ inline int compact_bucket16(const LevelMeta& M, const DynState& s) { } string tube_beam_refine(const LevelMeta& M, const DynState& start, const vector& target, - int max_d, int beam_w, int cap_bkt, bool role_agnostic, double budget_s) { + int max_d, int beam_w, int cap_bkt, bool role_agnostic, double budget_s, + size_t max_states = 13000000, uint64_t* states_accum = nullptr) { auto t0 = chrono::steady_clock::now(); int N = (int)target.size() - 1; auto norm_v = [&](uint8_t v) -> uint8_t { @@ -612,7 +636,7 @@ string tube_beam_refine(const LevelMeta& M, const DynState& start, const vector< string best_win; for (int d = 1; d <= max_d; ++d) { - if (chrono::duration(chrono::steady_clock::now() - t0).count() > budget_s || seen.cnt > 13000000) break; + if ((budget_s > 0.0 && chrono::duration(chrono::steady_clock::now() - t0).count() > budget_s) || seen.cnt > max_states) break; vector c_states; vector c_precs; vector c_bkt; @@ -676,6 +700,7 @@ string tube_beam_refine(const LevelMeta& M, const DynState& start, const vector< } cur.swap(nxt); } + if (states_accum) *states_accum += seen.cnt; return best_win; } @@ -1354,14 +1379,18 @@ int main() { ios::sync_with_stdio(false); cin.tie(nullptr); + string header_line; + if (!getline(cin, header_line)) return 1; + istringstream hs(header_line); string mode; int part, w, h, p1, p2, p3; - if (!(cin >> mode >> part >> w >> h >> p1 >> p2 >> p3)) return 1; + long long p4 = 0; + if (!(hs >> mode >> part >> w >> h >> p1 >> p2 >> p3)) return 1; + hs >> p4; if (w * h > MAX_CELLS) { cout << "{\"error\":\"grid exceeds MAX_CELLS (512)\"}\n"; return 1; } - string dummy; getline(cin, dummy); vector rows(h); for (int y = 0; y < h; ++y) getline(cin, rows[y]); string seed_route; @@ -1433,13 +1462,15 @@ int main() { int beam_w = max(2000, p1); int win_len = max(8, p2); double total_budget = max(0.0, (double)p3); + size_t stage_state_cap = p4 > 0 ? (size_t)p4 : (size_t)13000000; string best = seed_route; vector log_entries; + uint64_t opt_states_seen = 0; auto opt_t0 = chrono::steady_clock::now(); - if (total_budget <= 0.0) { + if (total_budget <= 0.0 && p4 <= 0) { cout << "{\"initial_len\":" << seed_route.size() << ",\"best_len\":" << seed_route.size() << ",\"improved\":false,\"best_route\":\"" << seed_route << "\",\"stages\":[]" - << ",\"timed_out\":true}\n"; + << ",\"states_seen\":0,\"timed_out\":true}\n"; return 0; } @@ -1464,7 +1495,7 @@ int main() { vector> axes; for (int sm : {1, 2, 3}) axes.push_back({sym_diff(sm), sm}); sort(axes.begin(), axes.end()); - double per_stage = total_budget / 2.2; + double per_stage = total_budget > 0.0 ? (total_budget / 2.2) : 0.0; if (axes[0].first <= M.n / 5) { int best_sym = axes[0].second; @@ -1472,7 +1503,7 @@ int main() { DynState cur = s0; mir_traj.push_back(transform_state(M, cur, best_sym)); for (char c : best) { step_state(M, cur, d_idx(c), false); mir_traj.push_back(transform_state(M, cur, best_sym)); } - string m_hit = tube_beam_refine(M, s0, mir_traj, (int)best.size() - 1, beam_w, 240, true, per_stage); + string m_hit = tube_beam_refine(M, s0, mir_traj, (int)best.size() - 1, beam_w, 240, true, per_stage, stage_state_cap, &opt_states_seen); if (!m_hit.empty() && m_hit.size() < best.size()) { log_entries.push_back("chiral_sym_" + to_string(best_sym) + ":" + to_string(best.size()) + "->" + to_string(m_hit.size())); best = m_hit; @@ -1482,7 +1513,7 @@ int main() { for (int iter = 0; iter < 2; ++iter) { vector ref_traj = {s0}; for (char c : best) { DynState nx = ref_traj.back(); step_state(M, nx, d_idx(c), false); ref_traj.push_back(nx); } - string r_hit = tube_beam_refine(M, s0, ref_traj, (int)best.size() - 1, max(32000, (beam_w * 4) / 5), 240, true, per_stage); + string r_hit = tube_beam_refine(M, s0, ref_traj, (int)best.size() - 1, max(32000, (beam_w * 4) / 5), 240, true, per_stage, stage_state_cap, &opt_states_seen); if (!r_hit.empty() && r_hit.size() < best.size()) { log_entries.push_back("iter_tube_" + to_string(iter + 1) + ":" + to_string(best.size()) + "->" + to_string(r_hit.size())); best = r_hit; @@ -1500,12 +1531,14 @@ int main() { if (i) log_str += ","; log_str += "\"" + log_entries[i] + "\""; } + bool timed_out_flag = (total_budget > 0.0) && (chrono::duration(chrono::steady_clock::now() - opt_t0).count() > total_budget); cout << "{\"initial_len\":" << seed_route.size() << ",\"best_len\":" << best.size() << ",\"improved\":" << (best.size() < seed_route.size() ? "true" : "false") << ",\"best_route\":\"" << best << "\"" << ",\"stages\":[" << log_str << "]" - << ",\"timed_out\":" << (chrono::duration(chrono::steady_clock::now() - opt_t0).count() > total_budget ? "true" : "false") + << ",\"states_seen\":" << opt_states_seen + << ",\"timed_out\":" << (timed_out_flag ? "true" : "false") << "}\n"; return 0; } diff --git a/soko_mcp/checker_auditor.py b/soko_mcp/checker_auditor.py index 0c02933..bab046c 100644 --- a/soko_mcp/checker_auditor.py +++ b/soko_mcp/checker_auditor.py @@ -191,7 +191,7 @@ def audit_dual_checkers( if impl_source is None or not impl_source.strip(): verdict = "PASS" if s_rep["clean"] else "FAIL" return { - "epistemic_status": "VERIFIED" if verdict == "PASS" else "REJECTED_CHECKER_HAZARD", + "epistemic_status": "LINT_PASS" if verdict == "PASS" else "REJECTED_CHECKER_HAZARD", "mode": "single_checker", "verdict": verdict, "spec_audit": s_rep, @@ -203,7 +203,7 @@ def audit_dual_checkers( distinct = s_skel != i_skel verdict = "PASS" if (distinct and s_rep["clean"] and i_rep["clean"]) else "FAIL" return { - "epistemic_status": "VERIFIED" if verdict == "PASS" else "REJECTED_CHECKER_HAZARD", + "epistemic_status": "LINT_PASS" if verdict == "PASS" else "REJECTED_CHECKER_HAZARD", "mode": "dual_checker", "verdict": verdict, "distinct_alpha_skeleton": distinct, diff --git a/soko_mcp/engine_bridge.py b/soko_mcp/engine_bridge.py index 9124948..b91fdfc 100644 --- a/soko_mcp/engine_bridge.py +++ b/soko_mcp/engine_bridge.py @@ -26,7 +26,7 @@ from typing import Any, Dict, List, Optional, Tuple from .catalog import get_room, load_catalog -from .evidence import digest, normalize_route, problem_identity, validate_room +from .evidence import digest, must_fail, normalize_route, problem_identity, validate_room, verify_certificate_gate from .flat_oracle import replay_flat_oracle from .oracle import FRONTIER_ROOMS, PuzzleScriptOracle, replay_oracle @@ -228,11 +228,14 @@ def ensure_cpp_engine() -> Optional[Path]: return None src_sha = _file_sha256(CPP_SRC) stamp = _cpp_bin_stamp() - try: - if CPP_BIN.is_file() and stamp.read_text(encoding="ascii").strip() == src_sha: - return CPP_BIN - except OSError: - pass + cached_sha: Optional[str] = None + if CPP_BIN.is_file() and stamp.is_file(): + try: + cached_sha = stamp.read_text(encoding="ascii").strip() + except OSError: + cached_sha = None + if cached_sha is not None and cached_sha == src_sha: + return CPP_BIN compiler = shutil.which("g++") or shutil.which("clang++") if not compiler: return None @@ -250,20 +253,30 @@ def ensure_cpp_engine() -> Optional[Path]: return CPP_BIN -def _invoke_cpp(mode: str, part: int, grid: List[str], p1: int, p2: int, p3: int, route: str = "-") -> Optional[Dict[str, Any]]: +def _invoke_cpp( + mode: str, + part: int, + grid: List[str], + p1: int, + p2: int, + p3: int, + route: str = "-", + p4: int = 0, +) -> Optional[Dict[str, Any]]: exe = ensure_cpp_engine() if exe is None: return None h, w = len(grid), len(grid[0]) if w * h > 512: return None - payload = f"{mode} {part} {w} {h} {p1} {p2} {p3}\n" + "\n".join(grid) + f"\n{route or '-'}\n" + header = f"{mode} {part} {w} {h} {p1} {p2} {p3}" + (f" {p4}" if p4 > 0 else "") + payload = header + "\n" + "\n".join(grid) + f"\n{route or '-'}\n" proc = subprocess.run( [str(exe)], input=payload, capture_output=True, text=True, - timeout=max(30, p3 + 30), + timeout=max(120 if p4 > 0 else 30, p3 + 30), ) if proc.returncode != 0 or not proc.stdout.strip(): return None @@ -344,6 +357,47 @@ def verify_route_dual( else: epistemic = "VERIFIED" if is_verified else "REJECTED" + controls_log: List[str] = [] + must_fail( + "rejected_route_promoted_to_verified", + bool( + verify_certificate_gate( + { + "epistemic_status": "VERIFIED", + "valid_winning_route": False, + "must_fail_controls": ["probe"], + } + )["passed"] + ), + controls_log, + ) + if py_res["exact_win"]: + if len(clean) > 0: + trunc_prefix = clean[:-1] + must_fail( + "truncated_prefix_rejected", + bool( + replay_oracle(part, grid, trunc_prefix)["exact_win"] + or ( + not _skip_flat_for_test + and replay_flat_oracle(part, grid, trunc_prefix)["exact_win"] + ) + ), + controls_log, + ) + extra_route = clean + "U" + must_fail( + "post_win_extra_key_rejected", + bool( + replay_oracle(part, grid, extra_route)["exact_win"] + or ( + not _skip_flat_for_test + and replay_flat_oracle(part, grid, extra_route)["exact_win"] + ) + ), + controls_log, + ) + result = { "epistemic_status": epistemic, "evidence_level": "differential_replay", @@ -366,6 +420,7 @@ def verify_route_dual( "dual_engine_checked": dual_or_triple_checked, "triple_oracle_checked": len(engines_executed) == 3, "differential_match": differential_match, + "must_fail_controls": controls_log, "checker_pins": pins, "checker_pins_ok": pins_ok, "final_ascii": py_res["final_ascii"], @@ -418,6 +473,7 @@ def optimize_route_verified( beam_width: int = 40000, window_len: int = 14, budget_sec: int = 35, + max_states: int = 0, ) -> Dict[str, Any]: clean_seed = seed_route.strip().upper() seed_check = verify_route_dual(part, grid, clean_seed) @@ -431,10 +487,12 @@ def optimize_route_verified( f"differential_match={seed_check['differential_match']}" ) - cpp_out = _invoke_cpp("optimize", part, grid, beam_width, window_len, budget_sec, clean_seed) + cpp_out = _invoke_cpp("optimize", part, grid, beam_width, window_len, budget_sec, clean_seed, p4=max_states) + states_seen = 0 if cpp_out is not None and "best_route" in cpp_out: cand_route = cpp_out["best_route"] stages = cpp_out.get("stages", []) + states_seen = int(cpp_out.get("states_seen", 0) or 0) backend = "cpp20_zero_alloc_engine" else: cand_route = _python_window_shortcut(part, grid, clean_seed, min(10, window_len)) @@ -445,7 +503,19 @@ def optimize_route_verified( if not cand_check["valid_winning_route"]: raise RuntimeError("Optimizer produced a candidate route that failed multi-oracle differential verification!") - return { + controls_log = list(cand_check["must_fail_controls"]) + if cand_check["route_len"] < len(clean_seed): + seed_prefix = clean_seed[: cand_check["route_len"]] + must_fail( + "seed_already_at_improved_len", + bool( + replay_oracle(part, grid, seed_prefix)["exact_win"] + or replay_flat_oracle(part, grid, seed_prefix)["exact_win"] + ), + controls_log, + ) + + out = { "epistemic_status": "VERIFIED", "backend": backend, "initial_len": len(clean_seed), @@ -456,11 +526,16 @@ def optimize_route_verified( "best_route_sha256_12": cand_check["route_sha256_12"], "traj_fnv64": cand_check["traj_fnv64"], "stages": stages, + "states_seen": states_seen, + "max_states": max_states, "dual_oracle_verified": True, "triple_oracle_checked": cand_check["triple_oracle_checked"], "differential_match": cand_check["differential_match"], + "must_fail_controls": controls_log, "checker_pins": cand_check["checker_pins"], } + out["certificate_sha256"] = digest(out) + return out def solve_room_verified( @@ -497,7 +572,7 @@ def solve_room_verified( v = verify_route_dual(part, grid, route) if not v["valid_winning_route"]: raise RuntimeError("Solver candidate failed multi-oracle verification!") - return { + out_solved = { "epistemic_status": "VERIFIED", "solved": True, "route_len": v["route_len"], @@ -507,8 +582,11 @@ def solve_room_verified( "states_explored": cpp_out.get("explored", 0), "dual_oracle_verified": True, "triple_oracle_checked": v["triple_oracle_checked"], + "must_fail_controls": list(v["must_fail_controls"]), "checker_pins": v["checker_pins"], } + out_solved["certificate_sha256"] = digest(out_solved) + return out_solved return { "epistemic_status": "UNSOLVED_WITHIN_BUDGET", "solved": False, @@ -523,11 +601,12 @@ def _shortest_known_route_len(part: int, grid: List[str]) -> Optional[int]: if meta.get("part") == part and list(meta.get("grid", [])) == list(grid) and meta.get("reference_route"): candidates.add(str(meta["reference_route"])) try: - for meta in load_catalog().values(): - if meta.get("part") == part and list(meta.get("grid", [])) == list(grid) and meta.get("reference_route"): - candidates.add(str(meta["reference_route"])) - except Exception: # catalog data is optional for this cross-check - pass + catalog_rooms = load_catalog() + except (OSError, ValueError, KeyError): # catalog data is optional for this cross-check + catalog_rooms = {} + for meta in catalog_rooms.values(): + if meta.get("part") == part and list(meta.get("grid", [])) == list(grid) and meta.get("reference_route"): + candidates.add(str(meta["reference_route"])) best: Optional[int] = None for route in candidates: py_res = replay_oracle(part, grid, route) @@ -698,12 +777,57 @@ def _rejected(status: str, **extra: Any) -> Dict[str, Any]: # Otherwise an inexact or unwitnessed floor is reported as an unwitnessed lower bound. status = "VERIFIED" if (exact_opt and witness_verified is not None) else "LOWER_BOUND_UNWITNESSED" + floor_controls: List[str] = [] + must_fail( + "unwitnessed_bound_promoted_to_verified", + bool( + verify_certificate_gate( + { + "epistemic_status": "VERIFIED", + "proven_floor": max(1, claimed_floor), + "exact_optimum_reached": False, + "optimality_proven": False, + "winning_witness": None, + "must_fail_controls": ["probe"], + } + )["passed"] + ), + floor_controls, + ) + must_fail( + "empirical_floor_promoted_to_proven", + bool( + verify_certificate_gate( + { + "epistemic_status": "PROVEN", + "proven_floor": max(1, claimed_floor), + "exact_optimum_reached": True, + "optimality_proven": True, + "formal_proof_checked": False, + "winning_witness": {"route": "R", "route_len": max(1, claimed_floor)}, + "must_fail_controls": ["probe"], + } + )["passed"] + ), + floor_controls, + ) + if witness_verified is not None and claimed_floor > 0: + w_trunc = witness[: claimed_floor - 1] + must_fail( + "truncated_witness_prefix_rejected", + bool( + replay_oracle(part, grid, w_trunc)["exact_win"] + or replay_flat_oracle(part, grid, w_trunc)["exact_win"] + ), + floor_controls, + ) edges = _count("edge_consistency_checks") - return { + out_floor = { "epistemic_status": status, "optimality_proven": bool(exact_opt and witness_verified is not None), "proven_floor": claimed_floor, "exact_optimum_reached": exact_opt, + "formal_proof_checked": False, "search_strategy": search_strategy, "quotient_pdb_enabled": bool(cpp_out.get("quotient_pdb_enabled", False)), "pair_atom_pdb_enabled": bool(cpp_out.get("pair_atom_pdb_enabled", False)), @@ -715,6 +839,7 @@ def _rejected(status: str, **extra: Any) -> Dict[str, Any]: "edge_consistency_checks": edges, **search_fields, "winning_witness": witness_verified, + "must_fail_controls": floor_controls, "checker_pins": get_soko_checker_pins(), "verification_note": ( None if status == "VERIFIED" else @@ -728,3 +853,5 @@ def _rejected(status: str, **extra: Any) -> Dict[str, Any]: f"h(s) <= 1 + h(s') covered the {edges} transitions it expanded, not pruned branches." ), } + out_floor["certificate_sha256"] = digest(out_floor) + return out_floor diff --git a/soko_mcp/evidence.py b/soko_mcp/evidence.py index 97375b6..0933745 100644 --- a/soko_mcp/evidence.py +++ b/soko_mcp/evidence.py @@ -51,3 +51,107 @@ def problem_identity(part: int, grid: list[str]) -> dict: "termination": "alive and first win on final key", "cost": "number of UDLRA keys", } return {"problem": problem, "instance_sha256": digest(problem)} + + +def must_fail(name: str, predicate_holds: bool, log: list[str]) -> None: + """Record a must-fail negative control (modeled on Foundation-0 certlib.must_fail). + + `predicate_holds` is the checker's acceptance verdict on a deliberately broken + or tautological input. The control passes only when `predicate_holds` is False. + """ + if not isinstance(name, str) or not name.strip(): + raise ValueError("must_fail control name must be a non-empty string") + if bool(predicate_holds): + raise ValueError(f"must_fail control '{name}' was wrongly accepted") + log.append(name.strip()) + + +def verify_certificate_gate(cert: dict) -> dict: + """Fail-closed certificate gate enforcing `must_fail_controls`, hash integrity, and anti-tier-swap rules.""" + if not isinstance(cert, dict): + return {"passed": False, "epistemic_status": "REJECTED_MALFORMED_CERTIFICATE", "reason": "certificate must be a dict"} + + if "certificate_sha256" in cert: + expected_hash = digest({k: v for k, v in cert.items() if k != "certificate_sha256"}) + if cert.get("certificate_sha256") != expected_hash: + return { + "passed": False, + "epistemic_status": "REJECTED_CERTIFICATE_HASH_MISMATCH", + "reason": "certificate_sha256 does not match canonical payload digest", + } + + status = str(cert.get("epistemic_status", "")).strip() + if not status: + return {"passed": False, "epistemic_status": "REJECTED_MISSING_STATUS", "reason": "missing epistemic_status"} + + controls = cert.get("must_fail_controls") + if status in ("PROVEN", "VERIFIED", "LOWER_BOUND", "LOWER_BOUND_UNWITNESSED"): + if ( + not isinstance(controls, list) + or len(controls) == 0 + or any(not isinstance(c, str) or not c.strip() for c in controls) + or len(set(controls)) != len(controls) + ): + return { + "passed": False, + "epistemic_status": "REJECTED_MISSING_MUST_FAIL_CONTROLS", + "reason": "certificate must carry a non-empty list of distinct must_fail_controls names", + } + + # Anti-tier-swap checks (LOWER_BOUND / LINT_PASS / bounded-subgraph promoted to VERIFIED or PROVEN) + kind = str(cert.get("certificate_kind", "")) + claim_kind = str(cert.get("claim_kind", "")).lower() + if status == "VERIFIED": + if cert.get("mode") in ("single_checker", "dual_checker"): + return { + "passed": False, + "epistemic_status": "REJECTED_TIER_SWAP", + "reason": "static AST lint report cannot claim VERIFIED (must be LINT_PASS)", + } + if claim_kind in ("lower_bound", "bound") and not cert.get("exact_optimum_reached", False): + return { + "passed": False, + "epistemic_status": "REJECTED_TIER_SWAP", + "reason": "lower-bound claim without exact optimal witness cannot be promoted to VERIFIED", + } + if "proven_floor" in cert and "exact_optimum_reached" in cert: + witness = cert.get("winning_witness") + if ( + not bool(cert.get("exact_optimum_reached")) + or cert.get("optimality_proven") is False + or (int(cert.get("proven_floor") or 0) > 0 and not isinstance(witness, dict)) + or (isinstance(witness, dict) and witness.get("route_len") != cert.get("proven_floor")) + ): + return { + "passed": False, + "epistemic_status": "REJECTED_TIER_SWAP", + "reason": "unwitnessed or non-exact floor bound cannot be promoted to VERIFIED", + } + if kind.endswith("_LOWER_BOUND") and not cert.get("winning_witness"): + return { + "passed": False, + "epistemic_status": "REJECTED_TIER_SWAP", + "reason": f"lower-bound certificate ({kind}) without matching primal witness cannot claim VERIFIED", + } + if "valid_winning_route" in cert and cert.get("valid_winning_route") is not True: + return { + "passed": False, + "epistemic_status": "REJECTED_TIER_SWAP", + "reason": "rejected route certificate cannot be promoted to VERIFIED", + } + + if status == "PROVEN": + if cert.get("complete_domain_closed") is False: + return { + "passed": False, + "epistemic_status": "REJECTED_TIER_SWAP", + "reason": "bounded-subgraph check (complete_domain_closed=False) cannot be promoted to PROVEN", + } + if "proven_floor" in cert and not cert.get("formal_proof_checked", False): + return { + "passed": False, + "epistemic_status": "REJECTED_TIER_SWAP", + "reason": "empirical search floor without formal proof cannot be promoted to PROVEN", + } + + return {"passed": True, "epistemic_status": status, "must_fail_controls": list(controls or [])} diff --git a/soko_mcp/flat_oracle.py b/soko_mcp/flat_oracle.py index d6f88f3..b4fa41d 100644 --- a/soko_mcp/flat_oracle.py +++ b/soko_mcp/flat_oracle.py @@ -12,7 +12,7 @@ import hashlib from typing import Any, Dict, List, Tuple -# Canonical cell object integer codes (0..20), matching C++20 `DynState` byte encoding +# Canonical cell object integer codes (0..21), matching C++20 `DynState` byte encoding _EMPTY = 0 _ACT_F, _ACT_T, _ACT_W, _ACT_P, _ACT_B, _ACT_D = 1, 2, 3, 4, 5, 6 _INA_1, _INA_2, _INA_3, _INA_4, _INA_5, _INA_6 = 7, 8, 9, 10, 11, 12 @@ -24,6 +24,7 @@ _YDOOR = 18 _PDOOR = 19 _MGATE = 20 +_CORPSE = 21 _CHAR_TO_OBJ: Dict[str, int] = { "F": _ACT_F, @@ -49,6 +50,7 @@ } _OBJ_TO_CHAR: Dict[int, str] = {v: k for k, v in _CHAR_TO_OBJ.items()} +_OBJ_TO_CHAR[_CORPSE] = "X" # not an input tile; produced only by fatal Late rules _STEP_DELTAS: Dict[str, Tuple[int, int]] = { "U": (0, -1), @@ -245,6 +247,7 @@ def _apply_late_phase(self) -> bool: if self._is_monster_code(tc): self.cells[idx] = _EMPTY elif self._is_hero_code(tc): + self.cells[idx] = _CORPSE hero_died = True # Yellow switches & doors diff --git a/soko_mcp/server.py b/soko_mcp/server.py index e713631..faf5bda 100644 --- a/soko_mcp/server.py +++ b/soko_mcp/server.py @@ -146,9 +146,16 @@ "budget_sec": { "type": "integer", "default": 35, - "minimum": 4, + "minimum": 0, "maximum": 180, - "description": "Total compute budget in seconds across optimization stages.", + "description": "Compute time budget in seconds (pass 0 with max_states > 0 for deterministic wall-clock-free search).", + }, + "max_states": { + "type": "integer", + "default": 0, + "minimum": 0, + "maximum": 50000000, + "description": "Deterministic per-stage state cap for Chiral-Mirror and Iterative Tube Beam.", }, }, "additionalProperties": False, @@ -846,9 +853,13 @@ def dispatch_tool_call(name: str, args: Dict[str, Any]) -> Dict[str, Any]: beam_width=int(args.get("beam_width", 40000)), window_len=int(args.get("window_len", 14)), budget_sec=int(args.get("budget_sec", 35)), + max_states=int(args.get("max_states", 0)), ) if rid: res["room_id"] = rid + if "certificate_sha256" in res: + res.pop("certificate_sha256", None) + res["certificate_sha256"] = digest(res) return res if name == "soko_solve_room": @@ -862,6 +873,9 @@ def dispatch_tool_call(name: str, args: Dict[str, Any]) -> Dict[str, Any]: ) if rid: res["room_id"] = rid + if "certificate_sha256" in res: + res.pop("certificate_sha256", None) + res["certificate_sha256"] = digest(res) return res if name == "soko_prove_floor": @@ -874,6 +888,9 @@ def dispatch_tool_call(name: str, args: Dict[str, Any]) -> Dict[str, Any]: ) if rid: res["room_id"] = rid + if "certificate_sha256" in res: + res.pop("certificate_sha256", None) + res["certificate_sha256"] = digest(res) return res if name == "soko_run_planted_bug_audit": diff --git a/tests/test_certificate_corruptions.py b/tests/test_certificate_corruptions.py new file mode 100644 index 0000000..4ca2e72 --- /dev/null +++ b/tests/test_certificate_corruptions.py @@ -0,0 +1,192 @@ +"""Certificate `must_fail` control and tier-swap corruption regression tests (#53 A6, A7). + +Modeled on `Foundation-0/incentre-lattices` `certlib.must_fail` and +`Foundation-0/f0` `tests/run_gate_corruptions.py`: + 1. Every issued route, optimization, solver, floor, and track certificate carries a + non-empty list of distinct `must_fail_controls` names and passes `verify_certificate_gate`. + 2. Stripping, emptying, or duplicating `must_fail_controls` fails closed. + 3. Promoting a `LOWER_BOUND` / `LOWER_BOUND_UNWITNESSED` claim (or static `LINT_PASS` + checker audit, or bounded-subgraph result) to `VERIFIED` or `PROVEN` fails closed + (`REJECTED_TIER_SWAP`), even if `certificate_sha256` is recomputed. + 4. A tautological negative control (such as seeding an improvement control with an + already-passing target) raises `ValueError` immediately in `must_fail`. +""" +from __future__ import annotations + +import copy +import importlib.util +from pathlib import Path +import unittest + +from soko_mcp.checker_auditor import audit_builtin_soko_oracles, audit_dual_checkers +from soko_mcp.engine_bridge import ( + ensure_cpp_engine, + optimize_route_verified, + prove_floor_verified, + solve_room_verified, + verify_route_dual, +) +from soko_mcp.evidence import digest, must_fail, verify_certificate_gate +from soko_mcp.oracle import FRONTIER_ROOMS + +REPO_ROOT = Path(__file__).resolve().parent.parent + + +def _resign(cert: dict) -> dict: + mutated = copy.deepcopy(cert) + mutated.pop("certificate_sha256", None) + mutated["certificate_sha256"] = digest(mutated) + return mutated + + +def _load_track_verifier(track_slug: str): + path = REPO_ROOT / "tracks" / track_slug / "verify.py" + spec = importlib.util.spec_from_file_location(f"track_{track_slug.replace('-', '_')}", path) + if spec is None or spec.loader is None: + raise RuntimeError(f"cannot load {path}") + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + + +class MustFailAndTierSwapCorruptionTests(unittest.TestCase): + def test_must_fail_helper_rejects_passing_or_unnamed_controls(self) -> None: + log: list[str] = [] + must_fail("truncated_route", False, log) + self.assertEqual(log, ["truncated_route"]) + + # Simulates seeding an 87 -> 84 control with 84 so the broken predicate holds (True): + with self.assertRaisesRegex(ValueError, "wrongly accepted"): + must_fail("seed_already_at_improved_len", True, log) + + for bad_name in ("", " ", 123): # type: ignore[list-item] + with self.assertRaises(ValueError): + must_fail(bad_name, False, log) # type: ignore[arg-type] + + def test_builtin_and_dual_checker_auditor_return_lint_pass_not_verified(self) -> None: + builtin = audit_builtin_soko_oracles() + self.assertEqual(builtin["epistemic_status"], "LINT_PASS") + self.assertEqual(builtin["verdict"], "PASS") + + single = audit_dual_checkers("def check(x: int) -> bool:\n return x == 3\n") + self.assertEqual(single["epistemic_status"], "LINT_PASS") + self.assertEqual(single["verdict"], "PASS") + + # Promoting a static lint report to VERIFIED must fail closed as a tier swap: + promoted_lint = _resign({**builtin, "epistemic_status": "VERIFIED", "must_fail_controls": ["dummy_ctrl"]}) + gate = verify_certificate_gate(promoted_lint) + self.assertFalse(gate["passed"]) + self.assertEqual(gate["epistemic_status"], "REJECTED_TIER_SWAP") + + def test_all_live_certificates_carry_must_fail_controls_and_pass_gate(self) -> None: + i1 = FRONTIER_ROOMS["I-1"] + route_cert = verify_route_dual(i1["part"], i1["grid"], i1["reference_route"]) + self.assertEqual(route_cert["epistemic_status"], "VERIFIED") + self.assertIn("rejected_route_promoted_to_verified", route_cert["must_fail_controls"]) + self.assertIn("truncated_prefix_rejected", route_cert["must_fail_controls"]) + self.assertIn("post_win_extra_key_rejected", route_cert["must_fail_controls"]) + self.assertTrue(verify_certificate_gate(route_cert)["passed"]) + + for slug in ("iii-14", "iii-20"): + tmod = _load_track_verifier(slug) + tcert = tmod.verify_candidate() + self.assertEqual(tcert["epistemic_status"], "VERIFIED") + self.assertIn("truncated_reference_prefix", tcert["must_fail_controls"]) + self.assertIn("extra_key_after_win", tcert["must_fail_controls"]) + self.assertTrue(verify_certificate_gate(tcert)["passed"]) + + if ensure_cpp_engine() is not None: + exact_floor = prove_floor_verified(i1["part"], i1["grid"], max_bound=12, budget_sec=30) + self.assertEqual(exact_floor["epistemic_status"], "VERIFIED") + self.assertIn("unwitnessed_bound_promoted_to_verified", exact_floor["must_fail_controls"]) + self.assertIn("empirical_floor_promoted_to_proven", exact_floor["must_fail_controls"]) + self.assertIn("truncated_witness_prefix_rejected", exact_floor["must_fail_controls"]) + self.assertTrue(verify_certificate_gate(exact_floor)["passed"]) + + # Detour of length 11 on I-1 shortened back to 9 via deterministic window shortcut: + opt_cert = optimize_route_verified(i1["part"], i1["grid"], "DRRRRRRRRRU", beam_width=2000, window_len=12, budget_sec=0, max_states=50000) + self.assertEqual(opt_cert["epistemic_status"], "VERIFIED") + self.assertEqual(opt_cert["best_len"], 9) + self.assertIn("seed_already_at_improved_len", opt_cert["must_fail_controls"]) + self.assertTrue(verify_certificate_gate(opt_cert)["passed"]) + + solved_cert = solve_room_verified(i1["part"], i1["grid"], max_depth=15, beam_width=2000, budget_sec=30) + self.assertEqual(solved_cert["epistemic_status"], "VERIFIED") + self.assertTrue(verify_certificate_gate(solved_cert)["passed"]) + + def test_gate_corruptions_tier_swap_missing_controls_and_hash_tamper(self) -> None: + i1 = FRONTIER_ROOMS["I-1"] + base_route = verify_route_dual(i1["part"], i1["grid"], i1["reference_route"]) + self.assertTrue(verify_certificate_gate(base_route)["passed"]) + + # Corruption 1: Tampered payload without updating certificate_sha256 + tampered_hash = copy.deepcopy(base_route) + tampered_hash["route_len"] = 8 + g_hash = verify_certificate_gate(tampered_hash) + self.assertFalse(g_hash["passed"]) + self.assertEqual(g_hash["epistemic_status"], "REJECTED_CERTIFICATE_HASH_MISMATCH") + + # Corruption 2: Empty, missing, or duplicate must_fail_controls + for bad_controls in ([], None, [""], ["dup", "dup"]): + corrupted = copy.deepcopy(base_route) + if bad_controls is None: + corrupted.pop("must_fail_controls", None) + else: + corrupted["must_fail_controls"] = bad_controls + corrupted = _resign(corrupted) + g_ctrl = verify_certificate_gate(corrupted) + self.assertFalse(g_ctrl["passed"]) + self.assertEqual(g_ctrl["epistemic_status"], "REJECTED_MISSING_MUST_FAIL_CONTROLS") + + # Corruption 3: Tier swap — promoting LOWER_BOUND_UNWITNESSED / LOWER_BOUND to VERIFIED + if ensure_cpp_engine() is not None: + r20 = FRONTIER_ROOMS["III-20"] + lb_cert = prove_floor_verified(3, r20["grid"], max_bound=12, budget_sec=30) + self.assertEqual(lb_cert["epistemic_status"], "LOWER_BOUND_UNWITNESSED") + self.assertTrue(verify_certificate_gate(lb_cert)["passed"]) + + tier_swapped_floor = _resign({**lb_cert, "epistemic_status": "VERIFIED"}) + g_swap = verify_certificate_gate(tier_swapped_floor) + self.assertFalse(g_swap["passed"]) + self.assertEqual(g_swap["epistemic_status"], "REJECTED_TIER_SWAP") + + # Synthetic LOWER_BOUND record promoted to VERIFIED (even when resigned): + synthetic_lb = _resign({ + "epistemic_status": "VERIFIED", + "claim_kind": "lower_bound", + "proven_floor": 44, + "exact_optimum_reached": False, + "optimality_proven": False, + "winning_witness": None, + "must_fail_controls": ["unwitnessed_bound_promoted_to_verified"], + }) + g_syn = verify_certificate_gate(synthetic_lb) + self.assertFalse(g_syn["passed"]) + self.assertEqual(g_syn["epistemic_status"], "REJECTED_TIER_SWAP") + + # Formal lower-bound certificate promoted from PROVEN/LOWER_BOUND to VERIFIED without primal witness: + formal_lb_swap = _resign({ + "epistemic_status": "VERIFIED", + "certificate_kind": "QUOTIENT_HOMOMORPHISM_LOWER_BOUND", + "certified_lower_bound": 14, + "complete_domain_closed": True, + "must_fail_controls": ["incomplete_concrete_graph_rejected"], + }) + g_flb = verify_certificate_gate(formal_lb_swap) + self.assertFalse(g_flb["passed"]) + self.assertEqual(g_flb["epistemic_status"], "REJECTED_TIER_SWAP") + + # Bounded subgraph certificate promoted to PROVEN: + bounded_to_proven = _resign({ + "epistemic_status": "PROVEN", + "certificate_kind": "HOUDINI_INDUCTIVE_SAFETY_INVARIANT", + "complete_domain_closed": False, + "must_fail_controls": ["non_inductive_conjuncts_pruned"], + }) + g_b2p = verify_certificate_gate(bounded_to_proven) + self.assertFalse(g_b2p["passed"]) + self.assertEqual(g_b2p["epistemic_status"], "REJECTED_TIER_SWAP") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_engine_differential.py b/tests/test_engine_differential.py index 469a1a6..e3ac9b1 100644 --- a/tests/test_engine_differential.py +++ b/tests/test_engine_differential.py @@ -45,10 +45,75 @@ def test_native_trajectory_matches_readable_oracle_on_catalog(self): self.assertEqual(native["traj_fnv64"], readable["traj_fnv64"]) for key in ("alive", "exact_win", "premature_win", "steps_executed"): self.assertEqual(native[key], readable[key], key) - # After a death the readable oracle renders the corpse frame while - # the native engine keeps the last live frame; neither can win. - if readable["alive"]: - self.assertEqual(native["final_ascii"], readable["final_ascii"]) + self.assertEqual(native["final_ascii"], readable["final_ascii"]) + + def test_post_death_late_phase_finishes_kills_doors_and_monster_gate(self): + from soko_mcp.engine_bridge import _invoke_cpp + + # 1. Hero killed by one goblin while adjacent to a second goblin: remaining + # goblin-goblin mutual kills and monster-gate opening must still complete. + board_simul_gate = [ + "#########", + "#F.O.OGM#", + "#..O.2..#", + "#########", + ] + for route in ("R", "RR"): + readable = replay_oracle(2, board_simul_gate, route) + native = _invoke_cpp("verify", 2, board_simul_gate, 0, 0, 0, route) + self.assertIsNotNone(native) + self.assertFalse(readable["alive"]) + self.assertEqual(native["alive"], readable["alive"]) + self.assertEqual(native["final_ascii"], readable["final_ascii"]) + self.assertEqual( + native["final_ascii"].replace("X", "."), + readable["final_ascii"].replace("X", "."), + ) + + # 2. Hero steps onto a switch next to a goblin while another hero previously + # opened the yellow door: dying hero turns into a corpse (not weighing), + # so the yellow door closes on the same late phase. + board_switch_death = [ + "##########", + "#*{.[..GG#", + "#.F.O..2.#", + "##########", + ] + # Step 1 ('U' blocked or 'R' next to O): test hero killed on switch `{` + board_hero_on_sw = [ + "##########", + "#..[...GG#", + "#.F{.O.2.#", + "##########", + ] + for route in ("R", "RR"): + readable = replay_oracle(2, board_hero_on_sw, route) + native = _invoke_cpp("verify", 2, board_hero_on_sw, 0, 0, 0, route) + self.assertIsNotNone(native) + self.assertEqual(native["alive"], readable["alive"]) + self.assertEqual(native["final_ascii"], readable["final_ascii"]) + + # 3. Closing yellow door crushes inactive hero in doorway AND crushes last monster + # in second doorway, opening the monster gate 'M' in the same late phase. + board_door_crush = [ + "###########", + "#F*..[.[MG#", + "#..{...O2G#", + "###########", + ] + # Push crate onto '{' to open both '[' doors, move '2' and 'O' or test direct crush: + board_door_crush2 = [ + "###########", + "#F{..[.[MG#", + "#....2..OG#", + "###########", + ] + for route in ("R", "RAUU", "RAAUL"): + readable = replay_oracle(2, board_door_crush2, route) + native = _invoke_cpp("verify", 2, board_door_crush2, 0, 0, 0, route) + self.assertIsNotNone(native) + self.assertEqual(native["alive"], readable["alive"]) + self.assertEqual(native["final_ascii"], readable["final_ascii"]) if __name__ == "__main__": diff --git a/tests/test_flat_oracle_corpse.py b/tests/test_flat_oracle_corpse.py new file mode 100644 index 0000000..48ea9fb --- /dev/null +++ b/tests/test_flat_oracle_corpse.py @@ -0,0 +1,87 @@ +"""Regression tests for FlatBitmaskOracle corpse semantics (NEON-D3). + +All checks use unittest assertions so they execute under `python3 -O`. +""" +import random +import unittest + +from soko_mcp.catalog import get_room, load_catalog +from soko_mcp.engine_bridge import verify_route_dual +from soko_mcp.flat_oracle import FlatBitmaskOracle, replay_flat_oracle +from soko_mcp.oracle import PuzzleScriptOracle, replay_oracle + + +class FlatOracleCorpseTests(unittest.TestCase): + def test_ii4_rrr_agrees_with_puzzlescript_and_renders_corpse(self) -> None: + room = get_room("II-4") + ps = replay_oracle(room["part"], room["grid"], "RRR") + fb = replay_flat_oracle(room["part"], room["grid"], "RRR") + self.assertFalse(ps["alive"]) + self.assertFalse(fb["alive"]) + self.assertEqual(ps["steps_executed"], fb["steps_executed"]) + self.assertEqual(ps["final_ascii"], fb["final_ascii"]) + self.assertIn("X", fb["final_ascii"]) + self.assertEqual(ps["traj_fnv64"], fb["traj_fnv64"]) + + res = verify_route_dual(room["part"], room["grid"], "RRR") + self.assertTrue(res["differential_match"]) + self.assertTrue(res["trajectory_digest_match"]) + self.assertFalse(res["alive"]) + + def test_dead_hero_does_not_weigh_yellow_switch(self) -> None: + grid = ["#######", "#F{O.G#", "#[....#", "#######"] + ps = replay_oracle(2, grid, "R") + fb = replay_flat_oracle(2, grid, "R") + self.assertFalse(ps["alive"]) + self.assertFalse(fb["alive"]) + self.assertEqual(ps["final_ascii"], fb["final_ascii"]) + self.assertIn("X", fb["final_ascii"]) + self.assertEqual(fb["final_ascii"].splitlines()[2], "#[....#") + + def test_dead_hero_on_goal_is_not_a_win(self) -> None: + grid = ["######", "#FGO.#", "######"] + ps_ret = PuzzleScriptOracle(2, grid).step("R") + fb_ret = FlatBitmaskOracle(2, grid).step("R") + self.assertEqual(ps_ret, (False, False)) + self.assertEqual(fb_ret, (False, False)) + + def test_dragon_fatal_step_agrees_in_dual_verify(self) -> None: + grid = ["######", "#R..G#", "#..F.#", "######"] + ps = replay_oracle(2, grid, "U") + fb = replay_flat_oracle(2, grid, "U") + self.assertFalse(ps["alive"]) + self.assertFalse(fb["alive"]) + self.assertIn("X", fb["final_ascii"]) + self.assertEqual(ps["final_ascii"], fb["final_ascii"]) + self.assertEqual(ps["traj_fnv64"], fb["traj_fnv64"]) + + res = verify_route_dual(2, grid, "U") + self.assertTrue(res["differential_match"]) + self.assertTrue(res["trajectory_digest_match"]) + self.assertFalse(res["alive"]) + + def test_528_catalog_seeded_routes_and_66_references_agree(self) -> None: + rng = random.Random(20261114) + checked = 0 + for room_id, room in load_catalog().items(): + ref = room["reference_route"] + routes = [ref] + for case in range(8): + if case % 2: + routes.append("".join(rng.choice("UDLRA") for _ in range(rng.randint(1, 60)))) + else: + keys = list(ref[: rng.randint(1, len(ref))]) + for _ in range(rng.randint(0, 4)): + keys.insert(rng.randint(0, len(keys)), rng.choice("UDLRA")) + routes.append("".join(keys)) + for route in routes: + checked += 1 + ps = replay_oracle(room["part"], room["grid"], route) + fb = replay_flat_oracle(room["part"], room["grid"], route) + for field in ("alive", "exact_win", "premature_win", "steps_executed", "traj_fnv64", "final_ascii"): + self.assertEqual(fb[field], ps[field], f"{room_id} route={route!r} field={field}") + self.assertEqual(checked, 66 * 9) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_mcp_server.py b/tests/test_mcp_server.py index 81f1daf..648d548 100644 --- a/tests/test_mcp_server.py +++ b/tests/test_mcp_server.py @@ -29,7 +29,8 @@ def request(self, method: str, params: dict | None = None) -> dict: msg = {"jsonrpc": "2.0", "id": req_id, "method": method} if params is not None: msg["params"] = params - assert self.proc.stdin is not None and self.proc.stdout is not None + if self.proc.stdin is None or self.proc.stdout is None: + raise RuntimeError("MCP server stdio pipes are not initialized") self.proc.stdin.write((json.dumps(msg) + "\n").encode("utf-8")) self.proc.stdin.flush() line = self.proc.stdout.readline() @@ -42,7 +43,8 @@ def notify(self, method: str, params: dict | None = None) -> None: msg = {"jsonrpc": "2.0", "method": method} if params is not None: msg["params"] = params - assert self.proc.stdin is not None + if self.proc.stdin is None: + raise RuntimeError("MCP server stdin pipe is not initialized") self.proc.stdin.write((json.dumps(msg) + "\n").encode("utf-8")) self.proc.stdin.flush() @@ -233,7 +235,7 @@ def test_06_solve_from_scratch_and_quotient_pdb_prove_floor_f04(self) -> None: "tools/call", { "name": "soko_solve_room", - "arguments": {"part": 1, "grid": custom_grid, "max_depth": 25, "beam_width": 4000, "budget_sec": 5}, + "arguments": {"part": 1, "grid": custom_grid, "max_depth": 25, "beam_width": 4000, "budget_sec": 30}, }, )["result"]["structuredContent"] self.assertTrue(solved["solved"]) @@ -241,7 +243,7 @@ def test_06_solve_from_scratch_and_quotient_pdb_prove_floor_f04(self) -> None: flr22 = self.client.request( "tools/call", - {"name": "soko_prove_floor", "arguments": {"room_id": "III-22", "max_bound": 12, "budget_sec": 6}}, + {"name": "soko_prove_floor", "arguments": {"room_id": "III-22", "max_bound": 12, "budget_sec": 30}}, )["result"]["structuredContent"] self.assertEqual(flr22["proven_floor"], 9) self.assertTrue(flr22["exact_optimum_reached"]) @@ -270,13 +272,15 @@ def test_06_solve_from_scratch_and_quotient_pdb_prove_floor_f04(self) -> None: # v0.3.0 WizardRayGoalBound fixes F-04b trivial floor=1 on Wizard rooms (III-15 & III-20): flr20 = self.client.request( "tools/call", - {"name": "soko_prove_floor", "arguments": {"room_id": "III-20", "max_bound": 12, "budget_sec": 3}}, + {"name": "soko_prove_floor", "arguments": {"room_id": "III-20", "max_bound": 12, "budget_sec": 30}}, )["result"]["structuredContent"] self.assertEqual(flr20["epistemic_status"], "LOWER_BOUND_UNWITNESSED") self.assertTrue(flr20["wizard_ray_bound_enabled"]) self.assertGreaterEqual(flr20["proven_floor"], 8) def test_07_generic_chiral_optimizer_reproduces_87_to_84_world_record(self) -> None: + # Driven purely by deterministic per-stage state cap (`max_states=6_000_000`, `budget_sec=0`), + # eliminating wall-clock dependence (#53 A3). opt = self.client.request( "tools/call", { @@ -285,13 +289,17 @@ def test_07_generic_chiral_optimizer_reproduces_87_to_84_world_record(self) -> N "room_id": "III-20", "beam_width": 40000, "window_len": 14, - "budget_sec": 35, + "budget_sec": 0, + "max_states": 6000000, }, }, )["result"]["structuredContent"] self.assertEqual(opt["epistemic_status"], "VERIFIED") self.assertEqual(opt["initial_len"], 87) self.assertLessEqual(opt["best_len"], 84) + self.assertEqual(opt["max_states"], 6000000) + self.assertGreater(opt["states_seen"], 0) + self.assertIn("seed_already_at_improved_len", opt["must_fail_controls"]) self.assertTrue(opt["dual_oracle_verified"]) self.assertTrue(opt["triple_oracle_checked"]) self.assertTrue(opt["differential_match"]) diff --git a/tests/test_v04_foundation0_synthesis.py b/tests/test_v04_foundation0_synthesis.py index 872af77..81ce661 100644 --- a/tests/test_v04_foundation0_synthesis.py +++ b/tests/test_v04_foundation0_synthesis.py @@ -137,7 +137,8 @@ def setUpClass(cls) -> None: ], }, ) - assert reg["epistemic_status"] == "VERIFIED" and reg["tcb_locked"], reg + if reg["epistemic_status"] != "VERIFIED" or not reg["tcb_locked"]: + raise RuntimeError(f"formal_define_system failed: {reg}") def test_01_capabilities_and_17_tools_v040(self) -> None: caps = get_capabilities() @@ -283,9 +284,9 @@ def dual_y(): self.assertEqual(trunc_res["epistemic_status"], "REJECTED_INCOMPLETE_CONCRETE_GRAPH_FOR_QUOTIENT") def test_04_formal_audit_checkers_self_audit_and_hazard_detection(self) -> None: - # 1. Built-in self-audit of oracle.py vs flat_oracle.py + # 1. Built-in self-audit of oracle.py vs flat_oracle.py (static AST lint -> LINT_PASS, not VERIFIED) builtin_rep = dispatch_tool_call("formal_audit_checkers", {"builtin_soko_oracles": True}) - self.assertEqual(builtin_rep["epistemic_status"], "VERIFIED") + self.assertEqual(builtin_rep["epistemic_status"], "LINT_PASS") self.assertEqual(builtin_rep["verdict"], "PASS") self.assertTrue(builtin_rep["distinct_alpha_skeleton"]) diff --git a/tracks/iii-14/verify.py b/tracks/iii-14/verify.py index e1f9e66..95df9ec 100644 --- a/tracks/iii-14/verify.py +++ b/tracks/iii-14/verify.py @@ -16,6 +16,7 @@ REPO_ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(REPO_ROOT)) +from soko_mcp.evidence import digest, must_fail from soko_mcp.flat_oracle import replay_flat_oracle from soko_mcp.oracle import FRONTIER_ROOMS, replay_oracle @@ -27,9 +28,11 @@ def verify_candidate(route: str = REFERENCE_ROUTE) -> dict: meta = FRONTIER_ROOMS[ROOM_ID] + part = int(meta["part"]) + grid = list(meta["grid"]) clean = route.strip().upper() - spec_res = replay_oracle(int(meta["part"]), list(meta["grid"]), clean) - flat_res = replay_flat_oracle(int(meta["part"]), list(meta["grid"]), clean) + spec_res = replay_oracle(part, grid, clean) + flat_res = replay_flat_oracle(part, grid, clean) parity = ( spec_res["alive"] == flat_res["alive"] and spec_res["exact_win"] == flat_res["exact_win"] @@ -38,7 +41,10 @@ def verify_candidate(route: str = REFERENCE_ROUTE) -> dict: and spec_res["final_ascii"] == flat_res["final_ascii"] ) valid = bool(parity and spec_res["exact_win"] and not spec_res["premature_win"]) - return { + controls: list[str] = [] + must_fail("truncated_reference_prefix", replay_oracle(part, grid, REFERENCE_ROUTE[:-1])["exact_win"], controls) + must_fail("extra_key_after_win", replay_oracle(part, grid, REFERENCE_ROUTE + "U")["exact_win"], controls) + out = { "epistemic_status": "VERIFIED" if valid else "REJECTED", "room_id": ROOM_ID, "valid_winning_route": valid, @@ -46,9 +52,12 @@ def verify_candidate(route: str = REFERENCE_ROUTE) -> dict: "route_len": len(clean), "beats_reference_best": valid and len(clean) < REFERENCE_BEST_LEN, "published_frontier_floor": PUBLISHED_FRONTIER_FLOOR, + "must_fail_controls": controls, "route_sha256_12": hashlib.sha256(clean.encode("utf-8")).hexdigest()[:12], "traj_fnv64": spec_res["traj_fnv64"], } + out["certificate_sha256"] = digest(out) + return out if __name__ == "__main__": diff --git a/tracks/iii-20/verify.py b/tracks/iii-20/verify.py index d77de63..1964e55 100644 --- a/tracks/iii-20/verify.py +++ b/tracks/iii-20/verify.py @@ -16,6 +16,7 @@ REPO_ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(REPO_ROOT)) +from soko_mcp.evidence import digest, must_fail from soko_mcp.flat_oracle import replay_flat_oracle from soko_mcp.oracle import FRONTIER_ROOMS, replay_oracle @@ -27,9 +28,11 @@ def verify_candidate(route: str = REFERENCE_ROUTE) -> dict: meta = FRONTIER_ROOMS[ROOM_ID] + part = int(meta["part"]) + grid = list(meta["grid"]) clean = route.strip().upper() - spec_res = replay_oracle(int(meta["part"]), list(meta["grid"]), clean) - flat_res = replay_flat_oracle(int(meta["part"]), list(meta["grid"]), clean) + spec_res = replay_oracle(part, grid, clean) + flat_res = replay_flat_oracle(part, grid, clean) parity = ( spec_res["alive"] == flat_res["alive"] and spec_res["exact_win"] == flat_res["exact_win"] @@ -38,7 +41,10 @@ def verify_candidate(route: str = REFERENCE_ROUTE) -> dict: and spec_res["final_ascii"] == flat_res["final_ascii"] ) valid = bool(parity and spec_res["exact_win"] and not spec_res["premature_win"]) - return { + controls: list[str] = [] + must_fail("truncated_reference_prefix", replay_oracle(part, grid, REFERENCE_ROUTE[:-1])["exact_win"], controls) + must_fail("extra_key_after_win", replay_oracle(part, grid, REFERENCE_ROUTE + "U")["exact_win"], controls) + out = { "epistemic_status": "VERIFIED" if valid else "REJECTED", "room_id": ROOM_ID, "valid_winning_route": valid, @@ -46,9 +52,12 @@ def verify_candidate(route: str = REFERENCE_ROUTE) -> dict: "route_len": len(clean), "beats_reference_best": valid and len(clean) < REFERENCE_BEST_LEN, "published_frontier_floor": PUBLISHED_FRONTIER_FLOOR, + "must_fail_controls": controls, "route_sha256_12": hashlib.sha256(clean.encode("utf-8")).hexdigest()[:12], "traj_fnv64": spec_res["traj_fnv64"], } + out["certificate_sha256"] = digest(out) + return out if __name__ == "__main__":