返回 CodeWhale
check-command-config-proof.py
根目录 / scripts / check-command-config-proof.py
1 #!/usr/bin/env python3
2 """Require the complete production config group and a service-free normal graph.
3
4 The adjacent normal library is the Rust authority: no fake outcomes, selected
5 leaf or cfg(test)-only harness. Follow local module includes (including tests),
6 then audit all-target transitive normal Cargo edges supplied by the runner.
7
8 FEAT-028 completes the whole config group, so this proof covers both the
9 portable main handlers (`config.rs`) and the policy slice (`policy.rs`).
10 """
11 from pathlib import Path
12 import argparse
13 import re
14 import tomllib
15
16 ROOT = Path(__file__).resolve().parent.parent
17 GROUP_DIR = "crates/tui/src/commands/groups/config"
18 ROOTS = ("crates/tui/src/commands/groups/config/mod.rs",)
19 PROOF = "tests/portable-config/src/lib.rs"
20 MANIFEST = "tests/portable-config/Cargo.toml"
21 REQUIRED = {
22 ROOTS[0],
23 "crates/tui/src/commands/groups/config/config.rs",
24 "crates/tui/src/commands/groups/config/import_claude.rs",
25 "crates/tui/src/commands/groups/config/permissions.rs",
26 "crates/tui/src/commands/groups/config/policy.rs",
27 "crates/tui/src/commands/groups/config/status.rs",
28 "crates/tui/src/commands/groups/config/policy_messages.rs",
29 "crates/tui/src/commands/groups/config/policy_tests.rs",
30 "crates/command-contract/src/money.rs",
31 }
32 ALLOWED_WORKSPACE = {
33 "codewhale-portable-config",
34 "codewhale-command-contract",
35 "codewhale-protocol", "codewhale-app-server-protocol",
36 "codewhale-sanitize",
37 }
38 FORBIDDEN_SERVICES = {
39 "tokio", "reqwest", "hyper", "rusqlite", "sqlx", "keyring", "dbus", "zbus",
40 "secret-service", "ratatui", "crossterm", "ureq", "mio", "native-tls", "openssl",
41 }
42 HOST_IMPORT = re.compile(
43 r"\b(?:App|AppAction|codewhale_(?:tui|core|runtime|config|execpolicy|secrets|state|localization))\b"
44 r"|\bcrate::(?:tui|core|config|commands::(?:traits|contract|CommandResult))\b"
45 r"|\bstd::(?:fs|net|process|env)\b"
46 )
47 MODULE = re.compile(r'(?:#\[path\s*=\s*"([^"]+)"\]\s*)?(?:pub(?:\([^)]*\))?\s+)?mod\s+(\w+)\s*;')
48
49
50 def graph_violations(graph):
51 names = {line.split()[0] for line in graph.splitlines() if line.strip()}
52 return [f"unapproved normal dependency: {name}" for name in sorted(names)
53 if (name.startswith("codewhale-") and name not in ALLOWED_WORKSPACE)
54 or name in FORBIDDEN_SERVICES]
55
56
57 def source_closure(root, roots):
58 pending = [root / path for path in roots]
59 seen = set()
60 errors = []
61 while pending:
62 path = pending.pop().resolve()
63 if path in seen:
64 continue
65 if not path.is_relative_to(root.resolve()) or not path.is_file():
66 errors.append(f"missing or out-of-tree config module: {path}")
67 continue
68 seen.add(path)
69 source = "\n".join(line.split("//")[0] for line in path.read_text().splitlines())
70 if HOST_IMPORT.search(source):
71 errors.append(f"host service in config source: {path.relative_to(root)}")
72 if "use codewhale_command_contract::money;" in source:
73 pending.append(root / "crates/command-contract/src/money.rs")
74 for explicit, name in MODULE.findall(source):
75 if explicit:
76 child = path.parent / explicit
77 else:
78 base = path.parent if path.name in {"mod.rs", "lib.rs"} else path.with_suffix("")
79 child = base / f"{name}.rs"
80 if not child.is_file():
81 child = base / name / "mod.rs"
82 pending.append(child)
83 return {str(path.relative_to(root)) for path in seen}, errors
84
85
86 def directory_violations(root):
87 """Reject a host import in ANY file of the movable group directory.
88
89 The mounted-closure scan only sees files reachable from the proof roots; the
90 group root (`mod.rs`) and any future sibling would escape it. This scans the
91 whole directory so no file can be omitted, which is the FEAT-026 lesson.
92 """
93 errors = []
94 for path in sorted((root / GROUP_DIR).glob("*.rs")):
95 source = "\n".join(line.split("//")[0] for line in path.read_text().splitlines())
96 if HOST_IMPORT.search(source):
97 errors.append(f"host service in movable group file: {path.relative_to(root)}")
98 return errors
99
100
101 def violations(root):
102 root = root.resolve()
103 errors = []
104 wrapper = root / PROOF
105 text = wrapper.read_text()
106 includes = re.findall(r'#\[path\s*=\s*"([^"]+)"\]\s*pub mod (\w+);', text)
107 resolved = {(wrapper.parent / inc).resolve() for inc, _ in includes}
108 if "#[cfg" in text or resolved != {(root / path).resolve() for path in ROOTS}:
109 errors.append("proof must include the actual config and policy roots as a normal library")
110 manifest = tomllib.loads((root / MANIFEST).read_text())
111 if manifest.get("package", {}).get("publish") is not False:
112 errors.append("proof must remain unpublished")
113 if not {"codewhale-command-contract", "codewhale-protocol"} <= set(manifest.get("dependencies", {})):
114 errors.append("shared shapes must be normal dependencies, not test-only substitutes")
115 closure, source_errors = source_closure(root, ROOTS)
116 errors.extend(source_errors)
117 errors.extend(directory_violations(root))
118 for missing in sorted(REQUIRED - closure):
119 errors.append(f"incomplete config source closure: {missing}")
120 return errors
121
122
123 def main():
124 parser = argparse.ArgumentParser(description=__doc__)
125 parser.add_argument("--graph", type=Path, required=True)
126 args = parser.parse_args()
127 errors = violations(ROOT) + graph_violations(args.graph.read_text())
128 for error in errors:
129 print(f"[config-proof] FAIL: {error}")
130 if not errors:
131 print("[config-proof] PASS: actual complete group and approved normal dependency graph")
132 return bool(errors)
133
134
135 if __name__ == "__main__":
136 raise SystemExit(main())
137
137 lines PYTHON