"""Bounded offline input coverage for the packaged mainline/commission corpus. Source mechanics describe original configurations, not quick-model blockers. No game client, account service, GUI, APK, or network is needed. Private snapshots stay in memory; only counts and task-level model results are reported. """ from __future__ import annotations import argparse from collections import Counter from collections.abc import Mapping from contextlib import redirect_stderr, redirect_stdout from dataclasses import dataclass import gzip import hashlib import importlib import io import json import math from pathlib import Path import re import subprocess import sys import threading import time import types ROOT = Path(__file__).resolve().parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) PRIORITY_TASK = "1002013" DEFAULT_DAMAGE_PROFILE = "client_v4_x10" DAMAGE_PROFILES = (DEFAULT_DAMAGE_PROFILE, "original_1x") MAX_SNAPSHOT_BYTES = 16 * 1024 * 1024 MAX_RUNS = 5 TOP = 10 @dataclass(frozen=True) class Case: task: str task_type: int step: int step_type: str battle: Mapping def identity(self): return {"task": self.task, "type": self.task_type, "step": self.step, "step_type": self.step_type} def collect_cases(data=None): """Resolve actual inline battles and one-based passStage references.""" if data is None: from ranch_assistant.game_data import load_progression_data data = load_progression_data() stages = {} cases = [] for task_id, task in sorted(data.tasks.items()): if task.get("type") not in (1, 4): continue for number, step in enumerate(task.get("steps") or (), 1): kind = step.get("statusType") if kind not in ("battle", "passStage"): continue if kind == "passStage": value = step.get("statusValue") or {} key = (str(value["stage"]), str(value.get("level", 1))) if key not in stages: stages[key] = data.task_battle(task_id, number) battle = stages[key] else: battle = data.task_battle(task_id, number) cases.append(Case(str(task_id), task["type"], number, kind, battle)) return cases def _token(value): value = str(value) return value if re.fullmatch(r"[A-Za-z0-9_]{1,40}", value) else "unknown" def mechanics(battle): """Count source occurrences; callers count affected configurations once.""" result = Counter() if not isinstance(battle, Mapping): result["missing_battle"] += 1 return result for entry in battle.get("controller") or (): result["controller:" + _token(entry.get("type"))] += 1 for entry in battle.get("triggers") or (): result["trigger:" + _token(entry.get("type"))] += 1 for effect in entry.get("effects") or (): result["trigger_effect:" + _token(effect.get("type"))] += 1 for team in battle.get("teams") or (): for unit in team.get("mons") or (): if not isinstance(unit, Mapping): continue for entry in unit.get("controller") or (): result["unit_controller:" + _token(entry.get("type"))] += 1 for name in ("protectTarget", "destroyTarget"): if unit.get(name): result["objective:" + name] += 1 for kind in ("winType", "loseType"): for value in battle.get(kind) or (): if str(value) not in (("1",) if kind == "winType" else ("1", "2")): result["objective:" + kind + ":" + _token(value)] += 1 return result def _rank(counts, limit=None): rows = [{"reason": key, "count": count} for key, count in sorted(counts.items(), key=lambda item: (-item[1], item[0]))] return rows if limit is None else rows[:limit] def source_scan(cases): configurations, occurrences, kinds = Counter(), Counter(), Counter() for case in cases: features = mechanics(case.battle) configurations.update(features.keys()) occurrences.update(features) kinds[(case.task_type, case.step_type)] += 1 return {"count": len(cases), "counts": [{"type": task_type, "step_type": kind, "count": count} for (task_type, kind), count in sorted(kinds.items())], "mechanics": _rank(configurations), "occurrences": _rank(occurrences)} def fixture_roster(tables=None): """Same ordinary level-20 roster recipe as test_battle_simulator.request. Only the roster is synthetic. Battle configurations are never modified. """ if tables is None: directory = ROOT / "ranch_assistant" / "data" manifest = json.loads((directory / "combat_manifest.json").read_text(encoding="utf-8")) artifact = (directory / "combat_tables.json.gz").read_bytes() meta = manifest["artifacts"]["combat_tables.json.gz"] if hashlib.sha256(artifact).hexdigest() != meta["sha256"]: raise ValueError("combat_data_integrity") decoded = gzip.decompress(artifact) if hashlib.sha256(decoded).hexdigest() != meta["decoded_sha256"]: raise ValueError("combat_data_integrity") tables = json.loads(decoded) row = tables["monster_info"]["1001010"] monster = {"id": 101, "typeId": 1001010, "level": 20, "mode": 2, "tpLevel": 0, "feel": 0, "upStar": 0, "pAtk": 0, "pDef": 0, "pHp": 0, "pDex": 0} for name in ("Atk", "Def", "Hp", "Dex"): stat = name.lower() monster["g" + name] = (row[stat + "_grow_min"] + row[stat + "_grow_max"]) / 2 return {"master": {"level": 43}, "monsters": [monster], "teams": [{"teamName": "teamA", "monsters": [101]}], "monsterAttributes": []} def load_snapshot(path): """Read a raw snapshot (or {raw: snapshot}) without echoing input/errors.""" try: with Path(path).open("rb") as stream: content = stream.read(MAX_SNAPSHOT_BYTES + 1) if len(content) > MAX_SNAPSHOT_BYTES: raise ValueError raw = json.loads(content.decode("utf-8-sig")) if isinstance(raw, dict) and isinstance(raw.get("raw"), dict): raw = raw["raw"] if not isinstance(raw, dict) or not all(k in raw for k in ("master", "monsters", "teams")): raise ValueError if not isinstance(raw["master"], dict): raise ValueError # Drop credentials/account-envelope fields before any model call. result = {k: raw[k] for k in ("monsters", "teams", "monsterAttributes") if k in raw} result["master"] = {k: v for k, v in raw["master"].items() if k in ("level", "globalAtk", "globalDef", "globalHp", "globalDex")} return result except (OSError, UnicodeError, ValueError, TypeError, RecursionError): raise ValueError("snapshot_load_failed") from None def make_request(case, roster, *, team="teamA", seed=0, damage_profile=DEFAULT_DAMAGE_PROFILE): if damage_profile not in DAMAGE_PROFILES: raise ValueError("invalid_damage_profile") return {"context": "task_pass_stage" if case.step_type == "passStage" else "task_battle", "original_data": {"battle": case.battle}, "roster": roster, "team": team, "battle_position": {"battleType": "task", "taskTypeId": case.task, "taskStep": case.step - 1}, "seed": seed, "trace_limit": 0, "damage_profile": damage_profile} def representative_cases(cases, limit=3, tasks=()): """Priority task, protection objective, then both commission input routes.""" if not 0 <= limit <= MAX_RUNS: raise ValueError("run_limit") selected = [] def take(predicate): case = next((case for case in cases if predicate(case) and case not in selected), None) if case is not None: selected.append(case) for task in dict.fromkeys((PRIORITY_TASK, *tasks)): take(lambda case: case.task == task) take(lambda case: "objective:protectTarget" in mechanics(case.battle)) for kind in ("battle", "passStage"): take(lambda case: case.task_type == 4 and case.step_type == kind) take(lambda case: case.task_type == 1 and case.step_type == "battle") return selected[:limit] def _safe_counts(report): counts = Counter() for anomaly in report.get("anomalies") or (): code = anomaly.get("code") if isinstance(anomaly, Mapping) else None key = code if isinstance(code, str) and re.fullmatch(r"\+\d{1,3}over[a-z]{1,16}", code) else "detected" counts["anomaly:" + key] += 1 return _rank(counts) def _elapsed(value): if isinstance(value, (float, int)) and not isinstance(value, bool) and math.isfinite(value) and value >= 0: return round(value, 3) return None def result_summary(report, *, public_reasons=()): """Strict report boundary: no units, IDs, stats, traces, or arbitrary text.""" allowed = set(public_reasons) reasons = sorted({reason if isinstance(reason, str) and reason in allowed else "input_or_model_failure" for reason in report.get("reasons") or ()}) outcome = report.get("outcome") if outcome not in ("win", "loss", "timeout", "cancelled", "unsupported", "error", None): outcome = "error" terminal_reason = report.get("terminal_reason") if outcome in ("unsupported", "error", "timeout", "cancelled") and terminal_reason in ( "invalid_numeric_battle_data", "model_execution_failure", "resource_limit", "wall_clock", "cancelled"): reasons = sorted(set(reasons) | {terminal_reason}) return {"support": report.get("supported") is True, "reason": reasons, "outcome": outcome, "counts": _safe_counts(report), "elapsed_ms": _elapsed(report.get("elapsed_ms"))} def inspect_inputs(cases, runtime, *, cancel_event=None, deadline=None, damage_profile=DEFAULT_DAMAGE_PROFILE): """Inspect all source inputs with the fixture; never run all battles.""" started = time.monotonic() roster = fixture_roster() rows, counts, reasons = [], Counter(), Counter() for case in cases: if cancel_event is not None and cancel_event.is_set(): return {"rows": rows, "reason": ["cancelled"], "count": len(rows)} if deadline is not None and time.monotonic() >= deadline: return {"rows": rows, "reason": ["budget_exhausted"], "count": len(rows)} report = runtime.inspect(make_request(case, roster, damage_profile=damage_profile)) # These messages depend only on pinned public data and the test fixture. public = [s[:240] for s in report.get("reasons") or () if isinstance(s, str)][:32] row = dict(case.identity(), **result_summary(report, public_reasons=public)) rows.append(row) counts[(case.task_type, case.step_type, row["support"])] += 1 reasons.update(set(row["reason"])) return {"count": len(rows), "reason": [], "rows": rows, "priority": [row for row in rows if row["task"] == PRIORITY_TASK], "counts": [{"type": typ, "step_type": kind, "support": support, "count": count} for (typ, kind, support), count in sorted(counts.items())], "unsupported_reasons": _rank(reasons), "wall_ms": round((time.monotonic() - started) * 1000, 3)} def replay_cases(cases, runtime, roster, *, team="teamA", timeout=1.0, cancel_event=None, deadline=None, public_reasons=(), damage_profile=DEFAULT_DAMAGE_PROFILE): """Run a bounded handful through the existing quick runtime, unchanged.""" if len(cases) > MAX_RUNS or not math.isfinite(timeout) or not 0 < timeout <= 2: raise ValueError("invalid_resource_bounds") rows = [] for case in cases: if cancel_event is not None and cancel_event.is_set(): break remaining = timeout if deadline is None else min(timeout, deadline - time.monotonic()) if remaining <= 0: break started = time.monotonic() try: # Never forward library output, even if a future model prints input. with redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): report = runtime.run(make_request(case, roster, team=team, damage_profile=damage_profile), timeout=remaining, cancel_event=cancel_event) row = result_summary(report, public_reasons=public_reasons) except KeyboardInterrupt: row = {"support": None, "outcome": "cancelled", "reason": ["cancelled"], "counts": []} except Exception: row = {"support": None, "outcome": "error", "reason": ["model_execution_failed"], "counts": []} rows.append(dict(case.identity(), **row, wall_ms=round((time.monotonic() - started) * 1000, 3))) if row["outcome"] == "cancelled": break return rows def quick_runtime(): """Use Fermat's public quick API exclusively, including for inspection.""" quick = importlib.import_module("ranch_assistant.battle_quick") return types.SimpleNamespace(inspect=lambda request: quick.prepare(request)[1], run=quick.simulate_quick) def head_runtime(): """Inspect committed HEAD model source in memory over the same public corpus. No checkout, archive, worktree, private path copy, or Git mutation occurs. HEAD inspection is historical information; it never gates the quick model. """ source = subprocess.run(["git", "show", "HEAD:ranch_assistant/battle_simulator.py"], cwd=ROOT, capture_output=True, check=True, timeout=3).stdout module = types.ModuleType("ranch_assistant._progression_support_head") module.__file__ = str(ROOT / "ranch_assistant" / "battle_simulator.py") sys.modules[module.__name__] = module try: exec(compile(source, module.__file__, "exec"), module.__dict__) return module.ExperimentalBattleRuntime() except Exception: sys.modules.pop(module.__name__, None) raise ValueError("head_model_unavailable") from None def _offline_audit(event, args): if event.startswith("socket.") or event == "urllib.Request": raise PermissionError("offline_only") def main(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--structure-only", action="store_true", help="Source scan only; no model import or simulation") parser.add_argument("--snapshot", type=Path, help="Private local raw snapshot; contents/path are never reported") parser.add_argument("--team", default="teamA") parser.add_argument("--damage-profile", choices=DAMAGE_PROFILES, default=DEFAULT_DAMAGE_PROFILE, help="Default matches Web automation; original_1x is an explicit comparison") parser.add_argument("--task", action="append", default=[]) parser.add_argument("--runs", type=int, default=3) parser.add_argument("--timeout", type=float, default=1.0) parser.add_argument("--budget", type=float, default=15.0) parser.add_argument("--compare-head", action="store_true", help="Historical HEAD inspection only; no baseline simulations") parser.add_argument("--output", type=Path, help="Full JSON report under ignored test-artifacts/") args = parser.parse_args(argv) started = time.monotonic() report = {"reason": [], "damage_profile": args.damage_profile} output = None try: if not 0 <= args.runs <= MAX_RUNS or not math.isfinite(args.timeout) or not 0 < args.timeout <= 2: raise ValueError("invalid_resource_bounds") if not math.isfinite(args.budget) or not 0 < args.budget <= 30: raise ValueError("invalid_resource_bounds") if args.output is not None: destination = args.output.resolve() if not destination.is_relative_to(ROOT / "test-artifacts") or args.snapshot and destination == args.snapshot.resolve(): raise ValueError("invalid_report_destination") output = destination cases = collect_cases() report["source"] = source_scan(cases) if not args.structure_only: deadline = started + args.budget stop = threading.Event() runtime = quick_runtime() report["quick_inputs"] = inspect_inputs(cases, runtime, deadline=deadline, cancel_event=stop, damage_profile=args.damage_profile) report["reason"].extend(report["quick_inputs"]["reason"]) if args.compare_head and not report["reason"]: report["historical_head"] = inspect_inputs(cases, head_runtime(), deadline=deadline, damage_profile=args.damage_profile) report["reason"].extend(report["historical_head"]["reason"]) if not report["reason"] and args.runs: if any(task not in {case.task for case in cases} for task in args.task): raise ValueError("task_not_found") selected = representative_cases(cases, args.runs, args.task) roster = load_snapshot(args.snapshot) if args.snapshot else fixture_roster() public = [reason for row in report["quick_inputs"]["rows"] for reason in row["reason"]] report["replays"] = replay_cases(selected, runtime, roster, team=args.team, timeout=args.timeout, deadline=deadline, cancel_event=stop, public_reasons=public, damage_profile=args.damage_profile) if len(report["replays"]) < len(selected): report["reason"].append("budget_exhausted") if any(row["outcome"] in ("timeout", "error", "cancelled") for row in report["replays"]): report["reason"].append("replay_incomplete") except KeyboardInterrupt: report["reason"] = ["cancelled"] except Exception as exc: safe = {"invalid_resource_bounds", "invalid_report_destination", "snapshot_load_failed", "task_not_found"} report["reason"] = [str(exc) if isinstance(exc, ValueError) and str(exc) in safe else "offline_check_failed"] report["wall_ms"] = round((time.monotonic() - started) * 1000, 3) if output is not None: try: output.parent.mkdir(parents=True, exist_ok=True) output.write_text(json.dumps(report, ensure_ascii=True, indent=2) + "\n", encoding="utf-8") except OSError: report["reason"] = ["report_write_failed"] summary = {key: ({k: v[:TOP] if isinstance(v, list) else v for k, v in value.items() if k != "rows"} if isinstance(value, dict) else value) for key, value in report.items()} print(json.dumps(summary, ensure_ascii=True, separators=(",", ":"))) return 1 if report["reason"] else 0 if __name__ == "__main__": sys.addaudithook(_offline_audit) raise SystemExit(main())