its-magic 0.1.2-32 → 0.1.2-33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,415 @@
1
+ """
2
+ Documentation profile resolution and surface helpers (DEC-0059).
3
+
4
+ Shared by installer (optional surface sync) and scripts/validate_doc_profile.py.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import os
10
+ from typing import Dict, List, Optional, Set, Tuple
11
+
12
+ DOC_AUDIENCE_ALLOWED = frozenset({"user", "developer", "both"})
13
+ DOC_DETAIL_ALLOWED = frozenset({"concise", "balanced", "technical-deep"})
14
+
15
+ USER_KEY_TO_H2: Dict[str, str] = {
16
+ "USER_PURPOSE": "Purpose",
17
+ "USER_QUICKSTART": "Quickstart",
18
+ "USER_EXAMPLES": "Examples",
19
+ "USER_TROUBLESHOOTING": "Troubleshooting",
20
+ "USER_LIMITATIONS": "Limitations",
21
+ "USER_RELATED_DOCS": "Related documentation",
22
+ }
23
+
24
+ DEV_KEY_TO_H2: Dict[str, str] = {
25
+ "DEV_PREREQS": "Prerequisites",
26
+ "DEV_WORKFLOW": "Workflow",
27
+ "DEV_QUALITY_GATES": "Quality gates",
28
+ "DEV_ARCHITECTURE": "Architecture notes",
29
+ "DEV_CONTRACTS": "Contracts and interfaces",
30
+ "DEV_DECISIONS": "Engineering decisions",
31
+ }
32
+
33
+ DEV_H2_TITLES = frozenset(DEV_KEY_TO_H2.values())
34
+ USER_H2_TITLES = frozenset(USER_KEY_TO_H2.values())
35
+
36
+ POINTER_H2 = "Contributing"
37
+
38
+ # Root README H2 budget (user-channel headings + optional Contributing pointer only).
39
+ ROOT_BUDGET: Dict[Tuple[str, str], int] = {}
40
+ for aud in ("user", "developer", "both"):
41
+ for det in ("concise", "balanced", "technical-deep"):
42
+ if aud == "user":
43
+ ROOT_BUDGET[(aud, det)] = {"concise": 5, "balanced": 7, "technical-deep": 9}[det]
44
+ elif aud == "developer":
45
+ ROOT_BUDGET[(aud, det)] = {"concise": 4, "balanced": 6, "technical-deep": 8}[det]
46
+ else:
47
+ ROOT_BUDGET[(aud, det)] = {"concise": 6, "balanced": 8, "technical-deep": 6}[det]
48
+
49
+
50
+ def resolve_doc_profile(merged: Dict[str, str]) -> Tuple[Optional[str], Optional[str], List[str]]:
51
+ """Return (audience, detail, errors). Empty profile keys default per DEC-0059 §6."""
52
+ raw_a = (merged.get("DOC_AUDIENCE_PROFILE") or "").strip().lower()
53
+ raw_d = (merged.get("DOC_DETAIL_LEVEL") or "").strip().lower()
54
+ errors: List[str] = []
55
+ if raw_a and raw_a not in DOC_AUDIENCE_ALLOWED:
56
+ errors.append(
57
+ "[DOC_PROFILE_INVALID] DOC_AUDIENCE_PROFILE must be one of: "
58
+ f"user, developer, both (got={raw_a!r})."
59
+ )
60
+ if raw_d and raw_d not in DOC_DETAIL_ALLOWED:
61
+ errors.append(
62
+ "[DOC_PROFILE_INVALID] DOC_DETAIL_LEVEL must be one of: "
63
+ f"concise, balanced, technical-deep (got={raw_d!r})."
64
+ )
65
+ if errors:
66
+ return None, None, errors
67
+ audience = raw_a or "both"
68
+ detail = raw_d or "balanced"
69
+ return audience, detail, []
70
+
71
+
72
+ def required_user_keys(audience: str, detail: str) -> Set[str]:
73
+ if audience not in ("user", "both"):
74
+ return set()
75
+ if detail == "concise":
76
+ return {"USER_PURPOSE", "USER_QUICKSTART", "USER_LIMITATIONS"}
77
+ if detail == "balanced":
78
+ return {
79
+ "USER_PURPOSE",
80
+ "USER_QUICKSTART",
81
+ "USER_LIMITATIONS",
82
+ "USER_EXAMPLES",
83
+ "USER_RELATED_DOCS",
84
+ }
85
+ return {
86
+ "USER_PURPOSE",
87
+ "USER_QUICKSTART",
88
+ "USER_LIMITATIONS",
89
+ "USER_EXAMPLES",
90
+ "USER_RELATED_DOCS",
91
+ "USER_TROUBLESHOOTING",
92
+ }
93
+
94
+
95
+ def required_dev_keys(audience: str, detail: str) -> Set[str]:
96
+ if audience not in ("developer", "both"):
97
+ return set()
98
+ if detail == "concise":
99
+ return {"DEV_PREREQS", "DEV_WORKFLOW"}
100
+ if detail == "balanced":
101
+ return {"DEV_PREREQS", "DEV_WORKFLOW", "DEV_QUALITY_GATES", "DEV_ARCHITECTURE"}
102
+ return {
103
+ "DEV_PREREQS",
104
+ "DEV_WORKFLOW",
105
+ "DEV_QUALITY_GATES",
106
+ "DEV_ARCHITECTURE",
107
+ "DEV_CONTRACTS",
108
+ "DEV_DECISIONS",
109
+ }
110
+
111
+
112
+ def extract_h2_titles(markdown: str) -> List[str]:
113
+ titles: List[str] = []
114
+ for line in markdown.splitlines():
115
+ if line.startswith("## ") and not line.startswith("###"):
116
+ titles.append(line[3:].strip())
117
+ return titles
118
+
119
+
120
+ def has_exact_h2(markdown: str, title: str) -> bool:
121
+ for line in markdown.splitlines():
122
+ if line.startswith("## ") and not line.startswith("###"):
123
+ if line[3:].strip() == title:
124
+ return True
125
+ return False
126
+
127
+
128
+ def count_profile_root_h2s(
129
+ markdown: str,
130
+ audience: str,
131
+ detail: str,
132
+ required_user_keys_set: Set[str],
133
+ ) -> int:
134
+ """
135
+ Count H2 lines in budget scope: required USER_* titles only for user/both;
136
+ Contributing pointer alone for developer-only (DEC-0059 / R-0054 user H2 budgets).
137
+ """
138
+ titles = extract_h2_titles(markdown)
139
+ want: Set[str] = set()
140
+ for key in required_user_keys_set:
141
+ want.add(USER_KEY_TO_H2[key])
142
+ if audience == "developer":
143
+ return sum(1 for t in titles if t == POINTER_H2)
144
+ n = 0
145
+ for t in titles:
146
+ if t in want:
147
+ n += 1
148
+ return n
149
+
150
+
151
+ def dev_h2_forbidden_in_root(markdown: str) -> List[str]:
152
+ """Return DEV_* H2 titles present in root (split layout forbids these in README)."""
153
+ found: List[str] = []
154
+ for t in extract_h2_titles(markdown):
155
+ if t in DEV_H2_TITLES:
156
+ found.append(t)
157
+ return found
158
+
159
+
160
+ def validate_optional_modes(merged: Dict[str, str], readme_text: str) -> List[str]:
161
+ """
162
+ US-0031 / US-0032 compatibility: never require optional artifacts when modes are off.
163
+ When on, ensure profile surfaces mention cross-links (additive, lightweight).
164
+ """
165
+ out: List[str] = []
166
+ sp = (merged.get("SPEC_PACK_MODE") or "0").strip()
167
+ ug = (merged.get("USER_GUIDE_MODE") or "0").strip()
168
+ if sp != "1" and ug != "1":
169
+ return out
170
+ if sp == "1":
171
+ if "docs/engineering" not in readme_text and "spec" not in readme_text.lower():
172
+ out.append(
173
+ "[DOC_OPTIONAL_CROSSLINK_WEAK] SPEC_PACK_MODE=1: root README should mention "
174
+ "engineering docs or spec-pack paths in a user channel section (see Related documentation)."
175
+ )
176
+ if ug == "1":
177
+ if "user-guides" not in readme_text and "user guide" not in readme_text.lower():
178
+ out.append(
179
+ "[DOC_OPTIONAL_CROSSLINK_WEAK] USER_GUIDE_MODE=1: root README should mention "
180
+ "docs/user-guides in a user channel section."
181
+ )
182
+ return out
183
+
184
+
185
+ def ensure_section(path: str, h2_title: str, body: str) -> Tuple[bool, str]:
186
+ """
187
+ Append ## h2_title + body if missing. Non-destructive.
188
+ Returns (changed, message).
189
+ """
190
+ ensure_parent_dir(path)
191
+ if os.path.isfile(path):
192
+ text = _read_utf8(path)
193
+ else:
194
+ text = ""
195
+
196
+ if has_exact_h2(text, h2_title):
197
+ return False, f"[DOC_PROFILE_SYNC] skip existing: ## {h2_title} ({path})"
198
+
199
+ block = f"\n\n## {h2_title}\n\n{body.strip()}\n"
200
+ if not text:
201
+ base = os.path.basename(path)
202
+ if base.lower() == "readme.md":
203
+ text = f"# Documentation\n"
204
+ else:
205
+ text = f"# {base.replace('.md', '').replace('_', ' ')}\n"
206
+ new_text = text.rstrip() + block
207
+ _write_utf8(path, new_text)
208
+ return True, f"[DOC_PROFILE_SYNC] appended: ## {h2_title} ({path})"
209
+
210
+
211
+ def ensure_doc_surfaces_merged(
212
+ merged: Dict[str, str],
213
+ target_root: str,
214
+ print_ok: bool = True,
215
+ ) -> List[str]:
216
+ audience, detail, errors = resolve_doc_profile(merged)
217
+ if errors:
218
+ return list(errors)
219
+ assert audience is not None and detail is not None
220
+ messages: List[str] = []
221
+ uk = required_user_keys(audience, detail)
222
+ dk = required_dev_keys(audience, detail)
223
+ readme = os.path.join(target_root, "README.md")
224
+ dev_readme = os.path.join(target_root, "docs", "developer", "README.md")
225
+
226
+ stubs_user = {
227
+ "USER_PURPOSE": (
228
+ "Describe what this repository is for in plain language. "
229
+ "Replace this placeholder with your product outcome."
230
+ ),
231
+ "USER_QUICKSTART": (
232
+ "Link to your fastest path to success. For its-magic, see [Setup](#setup) above."
233
+ ),
234
+ "USER_EXAMPLES": "Add short, copy-paste friendly examples for common tasks.",
235
+ "USER_TROUBLESHOOTING": (
236
+ "List frequent issues, what to check, and where logs or docs live."
237
+ ),
238
+ "USER_LIMITATIONS": "Call out known limits, unsupported environments, or sharp edges.",
239
+ "USER_RELATED_DOCS": (
240
+ "Link runbooks, architecture notes, and deeper guides. "
241
+ "Operator commands live in `docs/engineering/runbook.md`."
242
+ ),
243
+ }
244
+ stubs_dev = {
245
+ "DEV_PREREQS": "Toolchain, repo layout, and local prerequisites for contributors.",
246
+ "DEV_WORKFLOW": "Branching, phases, and day-to-day contributor workflow.",
247
+ "DEV_QUALITY_GATES": "Tests, lint, typecheck, and review expectations before merge.",
248
+ "DEV_ARCHITECTURE": "High-level modules, boundaries, and extension points.",
249
+ "DEV_CONTRACTS": "Public interfaces, file formats, and compatibility promises.",
250
+ "DEV_DECISIONS": "Pointers to `decisions/` and architecture sections that matter.",
251
+ }
252
+
253
+ for key in sorted(uk):
254
+ h2 = USER_KEY_TO_H2[key]
255
+ changed, msg = ensure_section(readme, h2, stubs_user[key])
256
+ if print_ok or changed:
257
+ messages.append(msg)
258
+
259
+ if dk:
260
+ for key in sorted(dk):
261
+ h2 = DEV_KEY_TO_H2[key]
262
+ changed, msg = ensure_section(dev_readme, h2, stubs_dev[key])
263
+ if print_ok or changed:
264
+ messages.append(msg)
265
+
266
+ if audience in ("developer", "both"):
267
+ contrib_body = (
268
+ "Contributor-focused workflow and guardrails live in "
269
+ "[`docs/developer/README.md`](docs/developer/README.md)."
270
+ )
271
+ changed, msg = ensure_section(readme, POINTER_H2, contrib_body)
272
+ if print_ok or changed:
273
+ messages.append(msg)
274
+
275
+ return messages
276
+
277
+
278
+ def optional_mode_warnings(merged: Dict[str, str], readme_text: str) -> List[str]:
279
+ """Non-blocking hints when optional doc modes are enabled (US-0031 / US-0032)."""
280
+ return validate_optional_modes(merged, readme_text)
281
+
282
+
283
+ def ensure_parent_dir(path: str) -> None:
284
+ parent = os.path.dirname(path)
285
+ if parent and not os.path.isdir(parent):
286
+ os.makedirs(parent, exist_ok=True)
287
+
288
+
289
+ def _read_utf8(path: str) -> str:
290
+ with open(path, "r", encoding="utf-8") as f:
291
+ return f.read()
292
+
293
+
294
+ def _write_utf8(path: str, text: str) -> None:
295
+ ensure_parent_dir(path)
296
+ with open(path, "w", encoding="utf-8", newline="\n") as f:
297
+ f.write(text)
298
+
299
+
300
+ def validate_repo_doc_profile(
301
+ target_root: str,
302
+ merged: Dict[str, str],
303
+ template_root: Optional[str],
304
+ ) -> List[str]:
305
+ """
306
+ Full validation for active target_root; optional template_root for parity.
307
+ Returns error strings (empty => pass).
308
+ """
309
+ errors: List[str] = []
310
+ audience, detail, err = resolve_doc_profile(merged)
311
+ errors.extend(err)
312
+ if errors:
313
+ return errors
314
+ assert audience is not None and detail is not None
315
+
316
+ uk = required_user_keys(audience, detail)
317
+ dk = required_dev_keys(audience, detail)
318
+
319
+ readme_path = os.path.join(target_root, "README.md")
320
+ dev_path = os.path.join(target_root, "docs", "developer", "README.md")
321
+
322
+ if not os.path.isfile(readme_path):
323
+ errors.append("[DOC_PROFILE_MERGE_ERROR] README.md missing (required for user channel checks).")
324
+ return errors
325
+
326
+ readme_text = _read_utf8(readme_path)
327
+
328
+ if audience in ("developer", "both") and dk:
329
+ if not os.path.isfile(dev_path):
330
+ errors.append(
331
+ "[DOC_SECTION_MISSING:shard] docs/developer/README.md missing but profile requires developer sections."
332
+ )
333
+
334
+ if audience in ("developer", "both"):
335
+ if not has_exact_h2(readme_text, POINTER_H2):
336
+ errors.append(
337
+ f"[DOC_SECTION_MISSING:DEV_SHARD_POINTER] Missing H2 ## {POINTER_H2} in README.md "
338
+ "(required pointer to docs/developer/README.md per DEC-0059)."
339
+ )
340
+ bad = dev_h2_forbidden_in_root(readme_text)
341
+ if bad:
342
+ errors.append(
343
+ "[DOC_PROFILE_INVALID] DEV sections must not use root README for split layout; "
344
+ f"found H2 titles {bad!r}. Move them to docs/developer/README.md."
345
+ )
346
+
347
+ for key in sorted(uk):
348
+ h2 = USER_KEY_TO_H2[key]
349
+ if not has_exact_h2(readme_text, h2):
350
+ errors.append(f"[DOC_SECTION_MISSING:{key}] Missing H2 ## {h2} in README.md.")
351
+
352
+ dev_text = ""
353
+ if os.path.isfile(dev_path):
354
+ dev_text = _read_utf8(dev_path)
355
+
356
+ for key in sorted(dk):
357
+ h2 = DEV_KEY_TO_H2[key]
358
+ if not has_exact_h2(dev_text, h2):
359
+ errors.append(f"[DOC_SECTION_MISSING:{key}] Missing H2 ## {h2} in docs/developer/README.md.")
360
+
361
+ budget = ROOT_BUDGET.get((audience, detail), 8)
362
+ counted = count_profile_root_h2s(readme_text, audience, detail, uk)
363
+ if counted > budget:
364
+ errors.append(
365
+ f"[DOC_SECTION_BUDGET_EXCEEDED] Root README profile-scoped H2 count={counted} "
366
+ f"exceeds budget={budget} for audience={audience!r} detail={detail!r}."
367
+ )
368
+
369
+ if template_root:
370
+ tr = os.path.join(template_root, "README.md")
371
+ td = os.path.join(template_root, "docs", "developer", "README.md")
372
+ if not os.path.isfile(tr):
373
+ errors.append("[DOC_TEMPLATE_PARITY_FAIL] template/README.md missing.")
374
+ else:
375
+ tt = _read_utf8(tr)
376
+ for key in sorted(uk):
377
+ h2 = USER_KEY_TO_H2[key]
378
+ if has_exact_h2(readme_text, h2) != has_exact_h2(tt, h2):
379
+ errors.append(
380
+ f"[DOC_TEMPLATE_PARITY_FAIL] USER H2 ## {h2} presence differs active vs template README."
381
+ )
382
+ if audience in ("developer", "both"):
383
+ if has_exact_h2(readme_text, POINTER_H2) != has_exact_h2(tt, POINTER_H2):
384
+ errors.append(
385
+ "[DOC_TEMPLATE_PARITY_FAIL] ## Contributing pointer presence differs active vs template README."
386
+ )
387
+ if dk:
388
+ if not os.path.isfile(td) or not os.path.isfile(dev_path):
389
+ errors.append(
390
+ "[DOC_TEMPLATE_PARITY_FAIL] developer README missing in active or template."
391
+ )
392
+ else:
393
+ tdev = _read_utf8(td)
394
+ for key in sorted(dk):
395
+ h2 = DEV_KEY_TO_H2[key]
396
+ if has_exact_h2(dev_text, h2) != has_exact_h2(tdev, h2):
397
+ errors.append(
398
+ f"[DOC_TEMPLATE_PARITY_FAIL] DEV H2 ## {h2} presence differs active vs template."
399
+ )
400
+
401
+ return errors
402
+
403
+
404
+ def self_test_resolver() -> None:
405
+ """Tier B: assert matrix key sets."""
406
+ assert required_user_keys("user", "concise") == {
407
+ "USER_PURPOSE",
408
+ "USER_QUICKSTART",
409
+ "USER_LIMITATIONS",
410
+ }
411
+ assert required_dev_keys("developer", "technical-deep") == set(DEV_KEY_TO_H2.keys())
412
+ a, d, e = resolve_doc_profile({"DOC_AUDIENCE_PROFILE": "", "DOC_DETAIL_LEVEL": ""})
413
+ assert not e and a == "both" and d == "balanced"
414
+ _, _, e2 = resolve_doc_profile({"DOC_AUDIENCE_PROFILE": "nope"})
415
+ assert e2 and "DOC_PROFILE_INVALID" in e2[0]
@@ -0,0 +1,198 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Shared sync / push eligibility evaluation for validate-and-push (merged scratchpad).
4
+
5
+ Imports installer.merge_scratchpad_layers / validate_merged_scratchpad only — no
6
+ duplicate DEC-0055 precedence logic. Emits machine-readable JSON on stdout; reason
7
+ codes only (no planning-shaped tokens) on stderr for errors.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import argparse
13
+ import fnmatch
14
+ import json
15
+ import os
16
+ import re
17
+ import sys
18
+ from pathlib import Path
19
+ from typing import List, Tuple
20
+
21
+ # Repo root (parent of scripts/)
22
+ _ROOT = Path(__file__).resolve().parent.parent
23
+ if str(_ROOT) not in sys.path:
24
+ sys.path.insert(0, str(_ROOT))
25
+
26
+ import installer # noqa: E402
27
+
28
+ _VALID_MODES = frozenset(
29
+ {"disabled", "manual", "by_phase", "by_milestone", "custom_phase_list"}
30
+ )
31
+
32
+ def _parse_csv(val: str) -> List[str]:
33
+ if not val or not val.strip():
34
+ return []
35
+ return [p.strip() for p in val.split(",") if p.strip()]
36
+
37
+
38
+ def _merge_and_validate(root: str) -> Tuple[dict, List[str]]:
39
+ ok, diags = installer.validate_merged_scratchpad(root)
40
+ if not ok:
41
+ return {}, diags
42
+ merged, _paths = installer.merge_scratchpad_layers(root)
43
+ return merged, []
44
+
45
+
46
+ def eval_policy(root: str, _branch: str) -> Tuple[bool, str, List[str]]:
47
+ """
48
+ Pre-test gate: merged scratchpad validation + SYNC_POLICY_MODE + ALLOW_AUTO_PUSH
49
+ + custom_phase_list boundary (SYNC_PHASE_BOUNDARY env).
50
+ Returns (ok, reason_code, scratchpad_diagnostics).
51
+ """
52
+ merged, diags = _merge_and_validate(root)
53
+ if diags:
54
+ return False, "SCRATCHPAD_MERGE_ERROR", diags
55
+
56
+ mode = (merged.get("SYNC_POLICY_MODE") or "").strip().lower()
57
+ if not mode or mode not in _VALID_MODES:
58
+ mode = "manual"
59
+ if mode == "disabled":
60
+ return False, "SYNC_DISABLED", []
61
+ if mode == "manual":
62
+ return False, "MANUAL_MODE_NO_AUTO", []
63
+
64
+ allow = (merged.get("ALLOW_AUTO_PUSH") or "").strip()
65
+ if allow != "1":
66
+ return False, "AUTO_PUSH_NOT_ENABLED", []
67
+
68
+ if mode == "custom_phase_list":
69
+ phases = [p.strip().lower() for p in _parse_csv(merged.get("SYNC_CUSTOM_PHASES", ""))]
70
+ boundary = (os.environ.get("SYNC_PHASE_BOUNDARY") or "").strip().lower()
71
+ if not boundary:
72
+ return False, "SYNC_TRIGGER_NOT_ELIGIBLE", []
73
+ if not phases or boundary not in phases:
74
+ return False, "SYNC_TRIGGER_NOT_ELIGIBLE", []
75
+
76
+ # by_phase / by_milestone: invocation counts as eligible boundary (DEC-0058 §4).
77
+ return True, "", []
78
+
79
+
80
+ def _branch_allowed(branch: str, allowlist_csv: str) -> bool:
81
+ patterns = _parse_csv(allowlist_csv)
82
+ if not patterns:
83
+ return False
84
+ for pat in patterns:
85
+ if fnmatch.fnmatchcase(branch, pat):
86
+ return True
87
+ if branch == pat:
88
+ return True
89
+ return False
90
+
91
+
92
+ def _scan_qa_findings_blocking(repo: Path) -> bool:
93
+ """True if blocking markers found (DEC-0058 §6)."""
94
+ sprints = repo / "sprints"
95
+ if not sprints.is_dir():
96
+ return False
97
+ for p in sorted(sprints.iterdir()):
98
+ if not p.is_dir() or len(p.name) != 5 or not p.name.startswith("S"):
99
+ continue
100
+ if not p.name[1:].isdigit():
101
+ continue
102
+ qf = p / "qa-findings.md"
103
+ if not qf.is_file():
104
+ continue
105
+ text = qf.read_text(encoding="utf-8", errors="replace")
106
+ if "BLOCKING_QA_FINDINGS" in text:
107
+ return True
108
+ in_blocking = False
109
+ for line in text.splitlines():
110
+ if re.match(r"^##\s+blocking\s*$", line, re.IGNORECASE):
111
+ in_blocking = True
112
+ continue
113
+ if re.match(r"^##\s+\S", line):
114
+ in_blocking = False
115
+ if in_blocking and re.search(r"^-\s+\[\s+\]", line):
116
+ return True
117
+ if re.search(r"^-\s+\[\s+\]", line) and re.search(
118
+ r"BLOCKING|FAIL", line, re.IGNORECASE
119
+ ):
120
+ return True
121
+ return False
122
+
123
+
124
+ def _count_sprint_qa_findings(repo: Path) -> int:
125
+ n = 0
126
+ sprints = repo / "sprints"
127
+ if not sprints.is_dir():
128
+ return 0
129
+ for p in sprints.iterdir():
130
+ if not p.is_dir() or len(p.name) != 5 or not p.name.startswith("S"):
131
+ continue
132
+ if not p.name[1:].isdigit():
133
+ continue
134
+ if (p / "qa-findings.md").is_file():
135
+ n += 1
136
+ return n
137
+
138
+
139
+ def eval_post_test(root: str, branch: str) -> Tuple[bool, str, List[str]]:
140
+ """
141
+ After tests: branch allowlist + PRE_QA + BLOCKING_QA_FINDINGS scan.
142
+ """
143
+ merged, diags = _merge_and_validate(root)
144
+ if diags:
145
+ return False, "SCRATCHPAD_MERGE_ERROR", diags
146
+
147
+ allow_csv = merged.get("AUTO_PUSH_BRANCH_ALLOWLIST") or ""
148
+ if not _branch_allowed(branch, allow_csv):
149
+ return False, "BRANCH_NOT_ALLOWLISTED", []
150
+
151
+ b = branch.strip()
152
+ if b not in ("main", "master"):
153
+ if _count_sprint_qa_findings(Path(root)) == 0:
154
+ return False, "PRE_QA_AUTOPUSH_FORBIDDEN", []
155
+
156
+ if _scan_qa_findings_blocking(Path(root)):
157
+ return False, "BLOCKING_QA_FINDINGS", []
158
+
159
+ return True, "", []
160
+
161
+
162
+ def main() -> int:
163
+ parser = argparse.ArgumentParser(description="Sync/push gate evaluation for validate-and-push.")
164
+ sub = parser.add_subparsers(dest="cmd", required=True)
165
+
166
+ p_pol = sub.add_parser("policy", help="Pre-test merged scratchpad + sync policy gate.")
167
+ p_pol.add_argument("--root", required=True)
168
+ p_pol.add_argument("--branch", required=True)
169
+
170
+ p_post = sub.add_parser("post", help="Post-test allowlist + QA scan gate.")
171
+ p_post.add_argument("--root", required=True)
172
+ p_post.add_argument("--branch", required=True)
173
+
174
+ args = parser.parse_args()
175
+ root = os.path.abspath(args.root)
176
+
177
+ if args.cmd == "policy":
178
+ ok, reason, diags = eval_policy(root, args.branch)
179
+ if diags:
180
+ for d in diags:
181
+ print(d, file=sys.stderr)
182
+ print(json.dumps({"ok": False, "reason_code": "SCRATCHPAD_MERGE_ERROR"}))
183
+ return 2
184
+ print(json.dumps({"ok": ok, "reason_code": reason or None}))
185
+ return 0 if ok else 2
186
+
187
+ ok, reason, pdiags = eval_post_test(root, args.branch)
188
+ if pdiags:
189
+ for d in pdiags:
190
+ print(d, file=sys.stderr)
191
+ print(json.dumps({"ok": False, "reason_code": "SCRATCHPAD_MERGE_ERROR"}))
192
+ return 2
193
+ print(json.dumps({"ok": ok, "reason_code": reason or None}))
194
+ return 0 if ok else 2
195
+
196
+
197
+ if __name__ == "__main__":
198
+ raise SystemExit(main())