返回 last30days-skill
skill_contract.py
根目录 / tests / skill_contract.py
1 """Read runtime instructions only through their required root routing gates."""
2
3 from pathlib import Path
4 import re
5
6
7 SKILL_ROOT = Path(__file__).resolve().parents[1] / "skills" / "last30days"
8 ROUTES = {
9 "Before any engine command": ("runtime",),
10 "Library search, feed, or queue intent": ("library-queue",),
11 "First-run setup is required": ("setup-wizard",),
12 "Source repair requested or indicated by doctor": ("setup-wizard",),
13 "Reddit session offer: first-run X answer, or the gate printed REDDIT_OFFER": ("reddit-session",),
14 "Trending/discovery intent": ("discovery",),
15 "Ordinary topic research or source-health diagnosis": ("research-runbook",),
16 "Before synthesizing ordinary or comparison research": ("synthesis",),
17 "Explicit comparison intent": ("comparison",),
18 "--competitors mode": ("competitors", "comparison"),
19 "Hiring intent or an engine ## Hiring Signals, ## Jobs Digest or ## Patent Digest block": (
20 "hiring-signals", "patent-signals",
21 ),
22 "--agent mode or explicit machine-readable JSON": ("agent-mode",),
23 "Recommendation intent": ("recommendations",),
24 "Identifiable product requires category peers": ("category-peers",),
25 "Grok Bot X lane": ("grok-bot-x",),
26 "OpenCode browse lane": ("opencode-browse-lane",),
27 "Follow-up on existing research": ("followups",),
28 "HTML/export intent": ("save-html-brief",),
29 }
30 STANDARD_CONDITIONS = (
31 "Before any engine command",
32 "Ordinary topic research or source-health diagnosis",
33 "Before synthesizing ordinary or comparison research",
34 )
35
36
37 def root_text(skill_root=SKILL_ROOT):
38 return (skill_root / "SKILL.md").read_text(encoding="utf-8")
39
40
41 def routing_table(skill_root=SKILL_ROOT):
42 text = root_text(skill_root)
43 assert "## Reference routing\n" in text, "missing root reference routing gate"
44 section = text.split("## Reference routing\n", 1)[1].split("\n## ", 1)[0]
45 routes = {}
46 for line in section.splitlines():
47 if not line.startswith("| "):
48 continue
49 cells = [cell.strip() for cell in line.strip("|").split("|")]
50 if cells[0] == "Condition" or cells[0].startswith("---"):
51 continue
52 assert len(cells) == 3, f"malformed reference route: {line}"
53 condition, instruction, return_point = cells
54 condition = condition.replace("`", "")
55 links = re.findall(r"\[([^]]+)\]\(([^)]+)\)", instruction)
56 assert links, f"route {condition!r} has no load link"
57 assert instruction.startswith("Read ") and instruction.endswith("in full."), (
58 f"route {condition!r} must require reading its reference in full"
59 )
60 references = []
61 for label, target in links:
62 assert label == target, f"reference label/target mismatch: {label} -> {target}"
63 assert re.fullmatch(r"references/[a-z-]+\.md", target), target
64 references.append(Path(target).stem)
65 assert condition not in routes, f"duplicate routing condition: {condition}"
66 assert return_point, f"route {condition!r} has no return point"
67 routes[condition] = tuple(references)
68 return routes
69
70
71 def reference_text(name, skill_root=SKILL_ROOT):
72 expected = {condition: names for condition, names in ROUTES.items() if name in names}
73 assert expected, f"unclassified reference: {name}"
74 actual = routing_table(skill_root)
75 for condition, names in expected.items():
76 assert actual.get(condition) == names, f"missing or incorrect {condition!r} load gate"
77 path = skill_root / "references" / f"{name}.md"
78 assert path.is_file(), f"required reference is missing: {path}"
79 return path.read_text(encoding="utf-8")
80
81
82 def contract_documents(skill_root=SKILL_ROOT):
83 """Global prohibitions also apply in every reachable optional instruction."""
84 return {
85 "SKILL.md": root_text(skill_root),
86 **{name: reference_text(name, skill_root) for name in dict.fromkeys(
87 name for names in ROUTES.values() for name in names
88 )},
89 }
90
90 lines PYTHON