| 1 | from pathlib import Path |
| 2 | import argparse |
| 3 | import re |
| 4 | import tomllib |
| 5 | |
| 6 | ROOT = Path(__file__).resolve().parent.parent |
| 7 | GROUP = "crates/tui/src/commands/groups/core" |
| 8 | ROOTS = (f"{GROUP}/navigation.rs",) |
| 9 | PROOF = "tests/portable-core-navigation/src/lib.rs" |
| 10 | MANIFEST = "tests/portable-core-navigation/Cargo.toml" |
| 11 | HOST = f"{GROUP}/mod.rs" |
| 12 | PAIRS = ( |
| 13 | ("anchor", "AnchorCmd"), ("help", "HelpCmd"), ("copy", "CopyCmd"), ("hotbar", "HotbarCmd"), |
| 14 | ("links", "LinksCmd"), ("transcript", "TranscriptCmd"), ("turn", "TurnCmd"), |
| 15 | ("home", "HomeCmd"), ("home", "OverviewCmd"), ("workspace", "WorkspaceCmd"), |
| 16 | ("translate", "TranslateCmd"), ("voice", "VoiceCmd"), |
| 17 | ("voice", "VoiceSendCmd"), ("voice", "VoiceControlCmd"), |
| 18 | ("dashboard", "DashboardCmd"), ("workspace", "WorktreeCmd"), |
| 19 | ) |
| 20 | NAMES = { |
| 21 | "anchor": ("anchor",), "help": ("help",), "copy": ("copy",), "hotbar": ("hotbar",), |
| 22 | "links": ("links",), "transcript": ("transcript",), "turn": ("turn",), |
| 23 | "home": ("home", "overview"), "workspace": ("workspace", "worktree"), |
| 24 | "translate": ("translate",), "voice": ("voice", "voicesend", "voicecontrol"), |
| 25 | "dashboard": ("dashboard",), |
| 26 | } |
| 27 | REQUIRED = {ROOTS[0]} | {f"{GROUP}/{name}.rs" for name in NAMES} |
| 28 | ALLOWED_NORMAL = { |
| 29 | "codewhale-portable-core-navigation", "codewhale-command-contract", "codewhale-protocol", "codewhale-app-server-protocol", |
| 30 | "chrono", "iana-time-zone", "android_system_properties", "libc", "core-foundation-sys", |
| 31 | "iana-time-zone-haiku", "js-sys", "cfg-if", "futures-util", "futures-core", "futures-task", |
| 32 | "pin-project-lite", "slab", "wasm-bindgen", "once_cell", "wasm-bindgen-macro", "quote", |
| 33 | "proc-macro2", "unicode-ident", "wasm-bindgen-macro-support", "bumpalo", "syn", |
| 34 | "wasm-bindgen-shared", "log", "windows-core", "windows-implement", "windows-interface", |
| 35 | "windows-link", "windows-result", "windows-strings", "num-traits", "serde", "serde_core", |
| 36 | "serde_derive", "serde_json", "indexmap", "equivalent", "hashbrown", "foldhash", "itoa", "memchr", |
| 37 | "zmij", "uuid", "getrandom", "r-efi", |
| 38 | } |
| 39 | HOST_IMPORT = re.compile( |
| 40 | r"\b(?:App|AppAction|codewhale_(?:tui|core|runtime|config|execpolicy|secrets|state|localization))\b" |
| 41 | r"|\bcrate::(?:tui|core|config|commands)\b|\bsuper::core\b|\bBox\s*<" |
| 42 | r"|\bstd::(?:fs|net|process|env)\b|\binclude(?:_str|_bytes)?\s*!" |
| 43 | ) |
| 44 | MODULE = re.compile(r'(?:#\[path\s*=\s*"([^"]+)"\]\s*)?(?:pub(?:\([^)]*\))?\s+)?mod\s+(\w+)\s*;') |
| 45 | PAIR = re.compile(r'\(\s*(\w+)::(\w+)::info\(\),\s*\1::\2::handler\(\),?\s*\)') |
| 46 | |
| 47 | |
| 48 | def source_text(path): |
| 49 | return "\n".join(line.split("//")[0] for line in path.read_text().splitlines()) |
| 50 | |
| 51 | |
| 52 | def conditional_source(text): |
| 53 | without_test_module = re.sub(r'#\[cfg\(test\)\]\s*mod\s+tests\s*\{', 'mod tests {', text) |
| 54 | return bool(re.search(r'#\s*\[\s*cfg(?:_attr)?\b', without_test_module)) |
| 55 | |
| 56 | |
| 57 | def graph_violations(graph): |
| 58 | names = {line.split()[0] for line in graph.splitlines() if line.strip()} |
| 59 | errors = [f"unapproved normal dependency: {name}" for name in sorted(names - ALLOWED_NORMAL)] |
| 60 | for required in ("codewhale-portable-core-navigation", "codewhale-command-contract", "codewhale-protocol"): |
| 61 | if required not in names: |
| 62 | errors.append(f"missing actual normal graph package: {required}") |
| 63 | return errors |
| 64 | |
| 65 | |
| 66 | def source_closure(root): |
| 67 | pending = [root / name for name in ROOTS] |
| 68 | seen, errors = set(), [] |
| 69 | while pending: |
| 70 | path = pending.pop().resolve() |
| 71 | if path in seen: |
| 72 | continue |
| 73 | if not path.is_relative_to(root) or not path.is_file(): |
| 74 | errors.append(f"missing or out-of-tree navigation source: {path}") |
| 75 | continue |
| 76 | seen.add(path) |
| 77 | text = source_text(path) |
| 78 | if HOST_IMPORT.search(text): |
| 79 | errors.append(f"host/service in portable navigation source: {path.relative_to(root)}") |
| 80 | if conditional_source(text): |
| 81 | errors.append(f"conditional production source is not a normal proof: {path.relative_to(root)}") |
| 82 | for explicit, name in MODULE.findall(text): |
| 83 | if explicit: |
| 84 | child = path.parent / explicit |
| 85 | else: |
| 86 | base = path.parent if path.name in {"mod.rs", "lib.rs"} else path.with_suffix("") |
| 87 | child = base / f"{name}.rs" |
| 88 | if not child.is_file(): |
| 89 | child = base / name / "mod.rs" |
| 90 | pending.append(child) |
| 91 | return {str(path.relative_to(root)) for path in seen}, errors |
| 92 | |
| 93 | |
| 94 | def violations(root): |
| 95 | root = root.resolve() |
| 96 | errors = [] |
| 97 | wrapper = root / PROOF |
| 98 | mount = re.fullmatch(r'\s*#\[path\s*=\s*"([^"]+)"\]\s*pub mod navigation;\s*', wrapper.read_text()) |
| 99 | if not mount or (wrapper.parent / mount.group(1)).resolve() != (root / ROOTS[0]).resolve(): |
| 100 | errors.append("proof must mount only the actual complete production navigation root normally") |
| 101 | manifest = tomllib.loads((root / MANIFEST).read_text()) |
| 102 | if manifest.get("package", {}).get("publish") is not False: |
| 103 | errors.append("proof must remain unpublished") |
| 104 | deps = manifest.get("dependencies", {}) |
| 105 | contract = deps.get("codewhale-command-contract", {}) |
| 106 | if set(deps) != {"codewhale-command-contract"} or not isinstance(contract, dict) or (root / MANIFEST).parent.joinpath(contract.get("path", "")).resolve() != (root / "crates/command-contract").resolve(): |
| 107 | errors.append("actual shared contract must be the sole normal direct dependency") |
| 108 | if manifest.get("dev-dependencies") or manifest.get("build-dependencies") or manifest.get("target"): |
| 109 | errors.append("proof cannot hide a host or substitute behind test/build/target dependencies") |
| 110 | closure, source_errors = source_closure(root) |
| 111 | errors.extend(source_errors) |
| 112 | for name in sorted(REQUIRED - closure): |
| 113 | errors.append(f"incomplete navigation source closure: {name}") |
| 114 | for name in sorted(REQUIRED): |
| 115 | path = root / name |
| 116 | if path.is_file(): |
| 117 | text = source_text(path) |
| 118 | if HOST_IMPORT.search(text): |
| 119 | errors.append(f"host/service in declared movable source: {name}") |
| 120 | if conditional_source(text): |
| 121 | errors.append(f"conditional declared movable source: {name}") |
| 122 | inventory = source_text(root / ROOTS[0]) |
| 123 | if tuple(PAIR.findall(inventory)) != PAIRS or not re.search(rf';\s*{len(PAIRS)}\s*\]', inventory): |
| 124 | errors.append(f"production inventory must contain all {len(PAIRS)} real matching registrations in original filtered order") |
| 125 | for module, expected in NAMES.items(): |
| 126 | path = root / GROUP / f"{module}.rs" |
| 127 | if path.is_file() and tuple(re.findall(r'\bname:\s*"([^"]+)"', source_text(path))) != expected: |
| 128 | errors.append(f"canonical metadata mismatch: {module}") |
| 129 | actual = [int(index) for index in re.findall( |
| 130 | r'core_navigation_host::registration::<(\d+)>\(\)', source_text(root / HOST) |
| 131 | )] |
| 132 | if actual != list(range(len(PAIRS))): |
| 133 | errors.append(f"real host core registry must contain all {len(PAIRS)} inventory entries exactly once in relative order") |
| 134 | return errors |
| 135 | |
| 136 | |
| 137 | def main(): |
| 138 | parser = argparse.ArgumentParser(description="Verify the real complete core-navigation slice and its all-target normal graph") |
| 139 | parser.add_argument("--graph", type=Path, required=True) |
| 140 | args = parser.parse_args() |
| 141 | errors = violations(ROOT) + graph_violations(args.graph.read_text()) |
| 142 | for error in errors: |
| 143 | print(f"[core-navigation-proof] FAIL: {error}") |
| 144 | if not errors: |
| 145 | print(f"[core-navigation-proof] PASS: all {len(PAIRS)} actual registrations, complete source closure, host parity and approved normal graph") |
| 146 | return bool(errors) |
| 147 | |
| 148 | |
| 149 | if __name__ == "__main__": |
| 150 | raise SystemExit(main()) |
| 151 |