@deftai/directive-content 0.55.2 → 0.56.0

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.
Files changed (217) hide show
  1. package/.githooks/pre-commit +143 -0
  2. package/.githooks/pre-push +121 -0
  3. package/QUICK-START.md +2 -2
  4. package/Taskfile.yml +934 -0
  5. package/UPGRADING.md +47 -1
  6. package/events/README.md +3 -3
  7. package/package.json +5 -4
  8. package/scripts/_agents_md.py +494 -0
  9. package/scripts/_cache_fetch.py +635 -0
  10. package/scripts/_cache_quota.py +529 -0
  11. package/scripts/_cache_refresh.py +163 -0
  12. package/scripts/_cache_validate.py +209 -0
  13. package/scripts/_content_root.py +42 -0
  14. package/scripts/_doctor_state.py +277 -0
  15. package/scripts/_event_detect.py +305 -0
  16. package/scripts/_events.py +514 -0
  17. package/scripts/_lifecycle_hygiene.py +568 -0
  18. package/scripts/_pathspec.py +91 -0
  19. package/scripts/_policy_show_cli.py +266 -0
  20. package/scripts/_precutover.py +92 -0
  21. package/scripts/_project_context.py +224 -0
  22. package/scripts/_project_definition_io.py +164 -0
  23. package/scripts/_relocate_snapshot.py +209 -0
  24. package/scripts/_relocate_states.py +343 -0
  25. package/scripts/_resolve_preflight_path.py +152 -0
  26. package/scripts/_safe_subprocess.py +167 -0
  27. package/scripts/_session_start_hook.py +205 -0
  28. package/scripts/_sor_gate_diff.py +365 -0
  29. package/scripts/_stdio_utf8.py +59 -0
  30. package/scripts/_triage_bootstrap_gitignore.py +904 -0
  31. package/scripts/_triage_classify_cli.py +122 -0
  32. package/scripts/_triage_queue_cli.py +625 -0
  33. package/scripts/_triage_scope_cli.py +343 -0
  34. package/scripts/_triage_scope_drift_cli.py +121 -0
  35. package/scripts/_triage_scope_ignores.py +286 -0
  36. package/scripts/_triage_scope_milestone.py +432 -0
  37. package/scripts/_triage_scope_mutations.py +337 -0
  38. package/scripts/_triage_scope_renderers.py +207 -0
  39. package/scripts/_triage_smoketest_stages.py +674 -0
  40. package/scripts/_triage_subscribe_cli.py +140 -0
  41. package/scripts/_triage_welcome_cli.py +421 -0
  42. package/scripts/_vbrief_build.py +239 -0
  43. package/scripts/_vbrief_fidelity.py +479 -0
  44. package/scripts/_vbrief_legacy.py +589 -0
  45. package/scripts/_vbrief_reconciliation.py +883 -0
  46. package/scripts/_vbrief_routing.py +277 -0
  47. package/scripts/_vbrief_safety.py +778 -0
  48. package/scripts/_vbrief_sources.py +312 -0
  49. package/scripts/_vbrief_speckit.py +262 -0
  50. package/scripts/_vbrief_story_quality.py +353 -0
  51. package/scripts/_vbrief_validation.py +299 -0
  52. package/scripts/build_dist.py +412 -0
  53. package/scripts/cache.py +1078 -0
  54. package/scripts/cache_scanner.py +745 -0
  55. package/scripts/candidates_log.py +432 -0
  56. package/scripts/capacity_backfill.py +680 -0
  57. package/scripts/capacity_show.py +653 -0
  58. package/scripts/ci_local.py +689 -0
  59. package/scripts/code_structure_validate.py +765 -0
  60. package/scripts/codebase_default_extractor.py +495 -0
  61. package/scripts/codebase_map.py +304 -0
  62. package/scripts/codebase_map_fresh.py +104 -0
  63. package/scripts/codebase_projection_registry.py +94 -0
  64. package/scripts/codebase_provider.py +582 -0
  65. package/scripts/doctor.py +2257 -0
  66. package/scripts/framework_commands.py +505 -0
  67. package/scripts/gh_rest.py +882 -0
  68. package/scripts/github_auth_modes.py +437 -0
  69. package/scripts/github_body.py +292 -0
  70. package/scripts/ip_risk.py +531 -0
  71. package/scripts/issue_emit.py +670 -0
  72. package/scripts/issue_ingest.py +1064 -0
  73. package/scripts/migrate_preflight.py +418 -0
  74. package/scripts/migrate_vbrief.py +2677 -0
  75. package/scripts/monitor_pr.py +401 -0
  76. package/scripts/pack_migrate_lessons.py +336 -0
  77. package/scripts/pack_migrate_patterns.py +254 -0
  78. package/scripts/pack_migrate_rules.py +350 -0
  79. package/scripts/pack_migrate_skills.py +423 -0
  80. package/scripts/pack_migrate_strategies.py +311 -0
  81. package/scripts/pack_migrate_swarm_spec.py +250 -0
  82. package/scripts/pack_render.py +434 -0
  83. package/scripts/packs_slice.py +712 -0
  84. package/scripts/platform_capabilities.py +336 -0
  85. package/scripts/policy.py +2826 -0
  86. package/scripts/policy_set.py +324 -0
  87. package/scripts/pr_check_closing_keywords.py +524 -0
  88. package/scripts/pr_check_protected_issues.py +267 -0
  89. package/scripts/pr_merge_readiness.py +1004 -0
  90. package/scripts/pr_wait_mergeable.py +669 -0
  91. package/scripts/prd_render.py +159 -0
  92. package/scripts/preflight_architecture_sor.py +974 -0
  93. package/scripts/preflight_branch.py +289 -0
  94. package/scripts/preflight_cache.py +974 -0
  95. package/scripts/preflight_gh.py +721 -0
  96. package/scripts/preflight_implementation.py +272 -0
  97. package/scripts/preflight_story_start.py +838 -0
  98. package/scripts/preflight_wip_cap.py +149 -0
  99. package/scripts/probe_session.py +545 -0
  100. package/scripts/project_render.py +293 -0
  101. package/scripts/quarantine_ext.py +237 -0
  102. package/scripts/reconcile_issues.py +1442 -0
  103. package/scripts/refresh-path.ps1 +107 -0
  104. package/scripts/release.py +2030 -0
  105. package/scripts/release_e2e.py +1011 -0
  106. package/scripts/release_publish.py +486 -0
  107. package/scripts/release_rollback.py +980 -0
  108. package/scripts/relocate.py +1034 -0
  109. package/scripts/resolve_changelog_unreleased.py +667 -0
  110. package/scripts/resolve_version.py +490 -0
  111. package/scripts/resume_conditions.py +706 -0
  112. package/scripts/ritual_sentinel.py +609 -0
  113. package/scripts/roadmap_render.py +635 -0
  114. package/scripts/rule_ownership_lint.py +325 -0
  115. package/scripts/scm.py +591 -0
  116. package/scripts/scope_audit_log.py +387 -0
  117. package/scripts/scope_decompose.py +654 -0
  118. package/scripts/scope_demote.py +509 -0
  119. package/scripts/scope_lifecycle.py +1126 -0
  120. package/scripts/scope_undo.py +772 -0
  121. package/scripts/session_start.py +406 -0
  122. package/scripts/setup_ghx.py +339 -0
  123. package/scripts/setup_windows.ps1 +220 -0
  124. package/scripts/slice_audit.py +585 -0
  125. package/scripts/slice_record.py +530 -0
  126. package/scripts/slice_record_existing.py +692 -0
  127. package/scripts/slug_normalize.py +178 -0
  128. package/scripts/spec_render.py +477 -0
  129. package/scripts/spec_validate.py +238 -0
  130. package/scripts/subagent_monitor.py +658 -0
  131. package/scripts/swarm_complete_cohort.py +644 -0
  132. package/scripts/swarm_launch.py +1206 -0
  133. package/scripts/swarm_readiness.py +554 -0
  134. package/scripts/swarm_verify_review_clean.py +438 -0
  135. package/scripts/swarm_worktrees.py +497 -0
  136. package/scripts/toolchain-check.py +52 -0
  137. package/scripts/triage_actions.py +871 -0
  138. package/scripts/triage_bootstrap.py +1153 -0
  139. package/scripts/triage_bulk.py +630 -0
  140. package/scripts/triage_classify.py +932 -0
  141. package/scripts/triage_help.py +1685 -0
  142. package/scripts/triage_queue.py +1944 -0
  143. package/scripts/triage_reconcile.py +581 -0
  144. package/scripts/triage_refresh.py +643 -0
  145. package/scripts/triage_scope.py +999 -0
  146. package/scripts/triage_scope_drift.py +575 -0
  147. package/scripts/triage_smoketest.py +396 -0
  148. package/scripts/triage_subscribe.py +399 -0
  149. package/scripts/triage_summary.py +1011 -0
  150. package/scripts/triage_welcome.py +1178 -0
  151. package/scripts/ts_check_lane.py +86 -0
  152. package/scripts/validate-links.py +64 -0
  153. package/scripts/validate_strategy_output.py +212 -0
  154. package/scripts/vbrief_activate.py +228 -0
  155. package/scripts/vbrief_migrate_conformance.py +368 -0
  156. package/scripts/vbrief_reconcile_graph.py +306 -0
  157. package/scripts/vbrief_reconcile_labels.py +460 -0
  158. package/scripts/vbrief_reconcile_umbrellas.py +741 -0
  159. package/scripts/vbrief_validate.py +1195 -0
  160. package/scripts/verify-stubs.py +61 -0
  161. package/scripts/verify_capacity.py +160 -0
  162. package/scripts/verify_encoding.py +699 -0
  163. package/scripts/verify_hooks_installed.py +206 -0
  164. package/scripts/verify_investigation.py +360 -0
  165. package/scripts/verify_judgment_gates.py +827 -0
  166. package/scripts/verify_no_task_runtime.py +171 -0
  167. package/scripts/verify_scm_boundary.py +509 -0
  168. package/scripts/verify_session_ritual.py +389 -0
  169. package/scripts/verify_tools.py +426 -0
  170. package/scripts/verify_vbrief_conformance.py +478 -0
  171. package/tasks/architecture.yml +13 -0
  172. package/tasks/cache.yml +69 -0
  173. package/tasks/capacity.yml +38 -0
  174. package/tasks/change.yml +46 -0
  175. package/tasks/changelog.yml +24 -0
  176. package/tasks/ci.yml +49 -0
  177. package/tasks/codebase.yml +47 -0
  178. package/tasks/commit.yml +30 -0
  179. package/tasks/core.yml +126 -0
  180. package/tasks/deployments.yml +54 -0
  181. package/tasks/framework.yml +74 -0
  182. package/tasks/install.yml +60 -0
  183. package/tasks/issue.yml +50 -0
  184. package/tasks/migrate.yml +73 -0
  185. package/tasks/packs.yml +92 -0
  186. package/tasks/policy.yml +75 -0
  187. package/tasks/pr.yml +89 -0
  188. package/tasks/prd.yml +39 -0
  189. package/tasks/project.yml +27 -0
  190. package/tasks/reconcile.yml +32 -0
  191. package/tasks/relocate.yml +56 -0
  192. package/tasks/roadmap.yml +28 -0
  193. package/tasks/scm.yml +126 -0
  194. package/tasks/scope-undo.yml +36 -0
  195. package/tasks/scope.yml +141 -0
  196. package/tasks/session.yml +19 -0
  197. package/tasks/setup.yml +37 -0
  198. package/tasks/slice.yml +69 -0
  199. package/tasks/spec.yml +41 -0
  200. package/tasks/swarm.yml +85 -0
  201. package/tasks/toolchain.yml +13 -0
  202. package/tasks/triage-actions.yml +94 -0
  203. package/tasks/triage-bootstrap.yml +43 -0
  204. package/tasks/triage-bulk.yml +75 -0
  205. package/tasks/triage-classify.yml +30 -0
  206. package/tasks/triage-queue.yml +50 -0
  207. package/tasks/triage-reconcile.yml +29 -0
  208. package/tasks/triage-scope-drift.yml +29 -0
  209. package/tasks/triage-scope.yml +31 -0
  210. package/tasks/triage-smoketest.yml +33 -0
  211. package/tasks/triage-subscribe.yml +36 -0
  212. package/tasks/triage-summary.yml +29 -0
  213. package/tasks/triage-welcome.yml +32 -0
  214. package/tasks/ts.yml +328 -0
  215. package/tasks/vbrief.yml +206 -0
  216. package/tasks/verify.yml +292 -0
  217. package/templates/agents-entry.md +1 -1
@@ -0,0 +1,765 @@
1
+ #!/usr/bin/env python3
2
+ """code_structure_validate.py -- validate #1595 codeStructure metadata.
3
+
4
+ The PR2 profile keeps authored codebase-structure intent at
5
+ ``PROJECT-DEFINITION.plan.architecture.codeStructure`` while generated maps,
6
+ indexes, and headers remain projections. This validator is intentionally small
7
+ and deterministic: it validates the shape and cross-references of the authored
8
+ ``codeStructure`` record without attempting extraction or MAP generation.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import argparse
14
+ import json
15
+ import re
16
+ import sys
17
+ from dataclasses import dataclass, field
18
+ from pathlib import Path, PurePosixPath
19
+ from typing import Any
20
+
21
+ STABLE_ID_RE = re.compile(r"^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$")
22
+ CODE_STRUCTURE_VERSION = "0.1"
23
+ DIRECTIVE_HOME = "x-directive/architecture.codeStructure"
24
+ PLAN_HOME = "plan.architecture.codeStructure"
25
+ PROJECT_DEFINITION_PATH = Path("vbrief/PROJECT-DEFINITION.vbrief.json")
26
+ GENERATED_PROJECTION_MARKERS = ("generated", "do not edit", "source of truth")
27
+ DERIVED_FACT_KEYS = {
28
+ "callgraph",
29
+ "classes",
30
+ "coupling",
31
+ "dependencies",
32
+ "dependencygraph",
33
+ "entrypoints",
34
+ "exports",
35
+ "filecount",
36
+ "files",
37
+ "functions",
38
+ "imports",
39
+ "language",
40
+ "languages",
41
+ "loc",
42
+ "symbols",
43
+ }
44
+
45
+
46
+ class CodeStructureConfigError(RuntimeError):
47
+ """Raised when a file cannot be loaded as a JSON object."""
48
+
49
+
50
+ @dataclass(frozen=True)
51
+ class Finding:
52
+ """One deterministic validation finding."""
53
+
54
+ code: str
55
+ message: str
56
+ location: str
57
+
58
+
59
+ @dataclass(frozen=True)
60
+ class ValidationResult:
61
+ """Validation result for one codeStructure record."""
62
+
63
+ errors: list[Finding]
64
+ warnings: list[Finding] = field(default_factory=list)
65
+
66
+ @property
67
+ def ok(self) -> bool:
68
+ return not self.errors
69
+
70
+
71
+ @dataclass(frozen=True)
72
+ class ExtractedCodeStructure:
73
+ """A codeStructure record plus the home it was read from."""
74
+
75
+ record: dict[str, Any]
76
+ home: str
77
+
78
+
79
+ def _finding(code: str, message: str, location: str) -> Finding:
80
+ return Finding(code=code, message=message, location=location)
81
+
82
+
83
+ def _is_stable_id(value: object) -> bool:
84
+ return isinstance(value, str) and bool(STABLE_ID_RE.fullmatch(value))
85
+
86
+
87
+ def _non_empty_string(value: object) -> bool:
88
+ return isinstance(value, str) and bool(value.strip())
89
+
90
+
91
+ def _as_list(value: object) -> list[Any]:
92
+ return value if isinstance(value, list) else []
93
+
94
+
95
+ def _safe_relative_path(value: object) -> bool:
96
+ if not isinstance(value, str):
97
+ return False
98
+ text = value.strip()
99
+ if not text or "\\" in text or text.startswith(("~", "$")):
100
+ return False
101
+ # Reject POSIX absolute paths and Windows drive-ish paths while keeping
102
+ # repository-relative dot directories such as .planning/.
103
+ if PurePosixPath(text).is_absolute() or re.match(r"^[A-Za-z]:", text):
104
+ return False
105
+ parts = PurePosixPath(text).parts
106
+ return ".." not in parts
107
+
108
+
109
+ def _normal_key(value: str) -> str:
110
+ return re.sub(r"[^a-z0-9]", "", value.lower())
111
+
112
+
113
+ def _project_relative(path: Path, project_root: Path) -> str:
114
+ try:
115
+ return path.resolve().relative_to(project_root.resolve()).as_posix()
116
+ except ValueError:
117
+ return str(path)
118
+
119
+
120
+ def extract_code_structure_homes(data: dict[str, Any]) -> list[ExtractedCodeStructure]:
121
+ """Return every recognized codeStructure home in deterministic priority order."""
122
+ homes: list[ExtractedCodeStructure] = []
123
+ plan = data.get("plan")
124
+ if isinstance(plan, dict):
125
+ architecture = plan.get("architecture")
126
+ if isinstance(architecture, dict):
127
+ record = architecture.get("codeStructure")
128
+ if isinstance(record, dict):
129
+ homes.append(ExtractedCodeStructure(record=record, home=PLAN_HOME))
130
+
131
+ extension = data.get("x-directive/architecture")
132
+ if isinstance(extension, dict):
133
+ record = extension.get("codeStructure")
134
+ if isinstance(record, dict):
135
+ homes.append(ExtractedCodeStructure(record=record, home=DIRECTIVE_HOME))
136
+ return homes
137
+
138
+
139
+ def extract_code_structure(data: dict[str, Any]) -> ExtractedCodeStructure | None:
140
+ """Return a codeStructure record from the canonical home or consumer fallback."""
141
+ homes = extract_code_structure_homes(data)
142
+ return homes[0] if homes else None
143
+
144
+
145
+ def _scan_for_derived_fact_keys(value: object, errors: list[Finding], location: str) -> None:
146
+ if isinstance(value, dict):
147
+ for key, nested in value.items():
148
+ key_location = f"{location}.{key}" if location else str(key)
149
+ if _normal_key(str(key)) in DERIVED_FACT_KEYS:
150
+ errors.append(
151
+ _finding(
152
+ "CS-DERIVED-FACT",
153
+ f"codeStructure must not author derived fact key {key!r}",
154
+ key_location,
155
+ )
156
+ )
157
+ _scan_for_derived_fact_keys(nested, errors, key_location)
158
+ return
159
+ if isinstance(value, list):
160
+ for index, nested in enumerate(value):
161
+ _scan_for_derived_fact_keys(nested, errors, f"{location}[{index}]")
162
+
163
+
164
+ def _validate_required_arrays(record: dict[str, Any], errors: list[Finding], source: str) -> None:
165
+ if record.get("version") != CODE_STRUCTURE_VERSION:
166
+ errors.append(
167
+ _finding(
168
+ "CS-VERSION",
169
+ f"codeStructure.version must be {CODE_STRUCTURE_VERSION!r}",
170
+ source,
171
+ )
172
+ )
173
+ for key in ("modules", "pathOwnership", "allowedPatterns", "projectionManifest"):
174
+ if not isinstance(record.get(key), list):
175
+ errors.append(_finding("CS-SHAPE", f"codeStructure.{key} must be an array", source))
176
+ if isinstance(record.get("modules"), list) and not record["modules"]:
177
+ errors.append(
178
+ _finding("CS-MODULES", "codeStructure.modules must contain at least one module", source)
179
+ )
180
+
181
+
182
+ def _validate_module(
183
+ module: object,
184
+ index: int,
185
+ errors: list[Finding],
186
+ glob_owner: dict[str, str],
187
+ ) -> str | None:
188
+ location = f"modules[{index}]"
189
+ if not isinstance(module, dict):
190
+ errors.append(_finding("CS-MODULE", "module entry must be an object", location))
191
+ return None
192
+
193
+ module_id = module.get("id")
194
+ if not _is_stable_id(module_id):
195
+ errors.append(
196
+ _finding(
197
+ "CS-MODULE-ID",
198
+ "module id must be a stable lowercase kebab-case id",
199
+ f"{location}.id",
200
+ )
201
+ )
202
+ return None
203
+
204
+ for key in ("name", "purpose"):
205
+ if not _non_empty_string(module.get(key)):
206
+ errors.append(
207
+ _finding("CS-MODULE", f"module {module_id!r} needs non-empty {key}", location)
208
+ )
209
+
210
+ globs = module.get("pathGlobs")
211
+ if not isinstance(globs, list) or not globs:
212
+ errors.append(
213
+ _finding(
214
+ "CS-GLOB",
215
+ f"module {module_id!r} needs at least one pathGlob",
216
+ f"{location}.pathGlobs",
217
+ )
218
+ )
219
+ return str(module_id)
220
+
221
+ for glob_index, glob_value in enumerate(globs):
222
+ glob_location = f"{location}.pathGlobs[{glob_index}]"
223
+ if not _safe_relative_path(glob_value):
224
+ errors.append(
225
+ _finding(
226
+ "CS-GLOB",
227
+ f"module {module_id!r} pathGlob must be repository-relative",
228
+ glob_location,
229
+ )
230
+ )
231
+ continue
232
+ prior = glob_owner.get(str(glob_value))
233
+ if prior is not None and prior != module_id:
234
+ errors.append(
235
+ _finding(
236
+ "CS-GLOB-CONFLICT",
237
+ f"pathGlob {glob_value!r} is assigned to both {prior!r} and {module_id!r}",
238
+ glob_location,
239
+ )
240
+ )
241
+ else:
242
+ glob_owner[str(glob_value)] = str(module_id)
243
+
244
+ return str(module_id)
245
+
246
+
247
+ def _validate_module_ref(
248
+ module_id: object,
249
+ module_ids: set[str],
250
+ location: str,
251
+ errors: list[Finding],
252
+ ) -> None:
253
+ if not isinstance(module_id, str) or module_id not in module_ids:
254
+ errors.append(
255
+ _finding(
256
+ "CS-MODULE-REF",
257
+ f"module reference {module_id!r} does not match a declared module id",
258
+ location,
259
+ )
260
+ )
261
+
262
+
263
+ def _validate_path_ownership(
264
+ entries: list[Any],
265
+ module_ids: set[str],
266
+ errors: list[Finding],
267
+ ) -> None:
268
+ ownership: dict[str, str] = {}
269
+ for index, entry in enumerate(entries):
270
+ location = f"pathOwnership[{index}]"
271
+ if not isinstance(entry, dict):
272
+ errors.append(
273
+ _finding("CS-OWNERSHIP", "pathOwnership entry must be an object", location)
274
+ )
275
+ continue
276
+ glob_value = entry.get("pathGlob")
277
+ if not _safe_relative_path(glob_value):
278
+ errors.append(
279
+ _finding("CS-GLOB", "pathOwnership.pathGlob must be repository-relative", location)
280
+ )
281
+ module_id = entry.get("module")
282
+ _validate_module_ref(module_id, module_ids, f"{location}.module", errors)
283
+ if isinstance(glob_value, str) and isinstance(module_id, str):
284
+ prior = ownership.get(glob_value)
285
+ if prior is not None and prior != module_id:
286
+ errors.append(
287
+ _finding(
288
+ "CS-OWNERSHIP-CONFLICT",
289
+ f"pathOwnership {glob_value!r} points at both {prior!r} and {module_id!r}",
290
+ location,
291
+ )
292
+ )
293
+ else:
294
+ ownership[glob_value] = module_id
295
+
296
+
297
+ def _validate_allowed_patterns(
298
+ entries: list[Any],
299
+ module_ids: set[str],
300
+ errors: list[Finding],
301
+ ) -> None:
302
+ seen_ids: set[str] = set()
303
+ for index, entry in enumerate(entries):
304
+ location = f"allowedPatterns[{index}]"
305
+ if not isinstance(entry, dict):
306
+ errors.append(
307
+ _finding("CS-PATTERN", "allowedPatterns entry must be an object", location)
308
+ )
309
+ continue
310
+ pattern_id = entry.get("id")
311
+ if not _is_stable_id(pattern_id):
312
+ errors.append(
313
+ _finding("CS-PATTERN-ID", "allowed pattern id must be stable kebab-case", location)
314
+ )
315
+ elif pattern_id in seen_ids:
316
+ errors.append(
317
+ _finding("CS-PATTERN-ID", f"duplicate allowed pattern id {pattern_id!r}", location)
318
+ )
319
+ else:
320
+ seen_ids.add(str(pattern_id))
321
+ _validate_module_ref(entry.get("module"), module_ids, f"{location}.module", errors)
322
+ for key in ("name", "description"):
323
+ if not _non_empty_string(entry.get(key)):
324
+ errors.append(_finding("CS-PATTERN", f"allowed pattern needs {key}", location))
325
+ applies_to = entry.get("appliesTo")
326
+ if applies_to is None:
327
+ continue
328
+ if not isinstance(applies_to, list):
329
+ errors.append(
330
+ _finding("CS-PATTERN", "allowed pattern appliesTo must be an array", location)
331
+ )
332
+ continue
333
+ for path_index, path_value in enumerate(applies_to):
334
+ if not _safe_relative_path(path_value):
335
+ errors.append(
336
+ _finding(
337
+ "CS-PATH",
338
+ "allowed pattern appliesTo path must be repository-relative",
339
+ f"{location}.appliesTo[{path_index}]",
340
+ )
341
+ )
342
+
343
+
344
+ def _projection_has_generated_banner(path: Path) -> bool:
345
+ try:
346
+ text = path.read_text(encoding="utf-8", errors="replace")[:2048].lower()
347
+ except OSError:
348
+ return False
349
+ return all(marker in text for marker in GENERATED_PROJECTION_MARKERS)
350
+
351
+
352
+ def _validate_projection_manifest(
353
+ entries: list[Any], errors: list[Finding], project_root: Path | None
354
+ ) -> None:
355
+ seen_paths: set[str] = set()
356
+ for index, entry in enumerate(entries):
357
+ location = f"projectionManifest[{index}]"
358
+ if not isinstance(entry, dict):
359
+ errors.append(
360
+ _finding("CS-PROJECTION", "projectionManifest entry must be an object", location)
361
+ )
362
+ continue
363
+ path_value = entry.get("path")
364
+ if not _safe_relative_path(path_value):
365
+ errors.append(
366
+ _finding("CS-PATH", "projection path must be repository-relative", location)
367
+ )
368
+ elif str(path_value) in seen_paths:
369
+ errors.append(
370
+ _finding("CS-PROJECTION", f"duplicate projection path {path_value!r}", location)
371
+ )
372
+ else:
373
+ seen_paths.add(str(path_value))
374
+ if not _is_stable_id(entry.get("kind")):
375
+ errors.append(
376
+ _finding("CS-PROJECTION", "projection kind must be stable kebab-case", location)
377
+ )
378
+ if not _non_empty_string(entry.get("source")):
379
+ errors.append(
380
+ _finding("CS-PROJECTION", "projection source must be non-empty", location)
381
+ )
382
+ elif entry.get("source") not in {PLAN_HOME, DIRECTIVE_HOME}:
383
+ errors.append(
384
+ _finding(
385
+ "CS-PROJECTION-SOURCE",
386
+ f"projection source must be {PLAN_HOME!r} or {DIRECTIVE_HOME!r}",
387
+ f"{location}.source",
388
+ )
389
+ )
390
+ generated = entry.get("generated")
391
+ if not isinstance(generated, bool):
392
+ errors.append(
393
+ _finding("CS-PROJECTION", "projection generated must be boolean", location)
394
+ )
395
+ elif generated is not True:
396
+ errors.append(
397
+ _finding(
398
+ "CS-PROJECTION",
399
+ "projectionManifest entries must declare generated=true",
400
+ location,
401
+ )
402
+ )
403
+ for command_key in ("task", "freshnessTask"):
404
+ if command_key in entry:
405
+ errors.append(
406
+ _finding(
407
+ "CS-PROJECTION-COMMAND",
408
+ f"projectionManifest must not store runner-specific {command_key}",
409
+ f"{location}.{command_key}",
410
+ )
411
+ )
412
+ if (
413
+ project_root is not None
414
+ and isinstance(path_value, str)
415
+ and _safe_relative_path(path_value)
416
+ ):
417
+ projection_path = project_root / path_value
418
+ if projection_path.exists() and not _projection_has_generated_banner(projection_path):
419
+ errors.append(
420
+ _finding(
421
+ "CS-PROJECTION-BANNER",
422
+ "existing projection path must carry a generated banner and source pointer",
423
+ f"{location}.path",
424
+ )
425
+ )
426
+
427
+
428
+ def _validate_file_purpose_overrides(
429
+ entries: object,
430
+ module_ids: set[str],
431
+ errors: list[Finding],
432
+ ) -> None:
433
+ if entries is None:
434
+ return
435
+ if not isinstance(entries, list):
436
+ errors.append(
437
+ _finding(
438
+ "CS-FILE-OVERRIDE", "filePurposeOverrides must be an array", "filePurposeOverrides"
439
+ )
440
+ )
441
+ return
442
+ seen_paths: set[str] = set()
443
+ for index, entry in enumerate(entries):
444
+ location = f"filePurposeOverrides[{index}]"
445
+ if not isinstance(entry, dict):
446
+ errors.append(_finding("CS-FILE-OVERRIDE", "file override must be an object", location))
447
+ continue
448
+ path_value = entry.get("path")
449
+ if not _safe_relative_path(path_value):
450
+ errors.append(
451
+ _finding("CS-PATH", "file override path must be repository-relative", location)
452
+ )
453
+ elif str(path_value) in seen_paths:
454
+ errors.append(
455
+ _finding("CS-FILE-OVERRIDE", f"duplicate override path {path_value!r}", location)
456
+ )
457
+ else:
458
+ seen_paths.add(str(path_value))
459
+ if not _non_empty_string(entry.get("purpose")):
460
+ errors.append(_finding("CS-FILE-OVERRIDE", "file override needs purpose", location))
461
+ if "module" in entry:
462
+ _validate_module_ref(entry.get("module"), module_ids, f"{location}.module", errors)
463
+
464
+
465
+ def _validate_glossary_refs(
466
+ entries: object, errors: list[Finding], project_root: Path | None
467
+ ) -> None:
468
+ if entries is None:
469
+ return
470
+ if not isinstance(entries, list):
471
+ errors.append(_finding("CS-GLOSSARY", "glossaryRefs must be an array", "glossaryRefs"))
472
+ return
473
+ for index, entry in enumerate(entries):
474
+ location = f"glossaryRefs[{index}]"
475
+ if not isinstance(entry, dict):
476
+ errors.append(_finding("CS-GLOSSARY", "glossary ref must be an object", location))
477
+ continue
478
+ if not _non_empty_string(entry.get("term")):
479
+ errors.append(_finding("CS-GLOSSARY", "glossary ref needs term", location))
480
+ uri = entry.get("uri")
481
+ if "uri" in entry and not _safe_relative_path(uri):
482
+ errors.append(
483
+ _finding("CS-PATH", "glossary ref uri must be repository-relative", location)
484
+ )
485
+ elif project_root is not None and isinstance(uri, str):
486
+ target = project_root / uri
487
+ if not target.exists():
488
+ errors.append(
489
+ _finding(
490
+ "CS-GLOSSARY-URI",
491
+ f"glossary ref uri does not exist: {uri!r}",
492
+ f"{location}.uri",
493
+ )
494
+ )
495
+
496
+
497
+ def _validate_boundedness(record: dict[str, Any], warnings: list[Finding]) -> None:
498
+ modules = _as_list(record.get("modules"))
499
+ overrides = _as_list(record.get("filePurposeOverrides"))
500
+ if overrides and len(overrides) > max(10, len(modules) * 2):
501
+ warnings.append(
502
+ _finding(
503
+ "CS-BOUNDEDNESS",
504
+ (
505
+ "filePurposeOverrides should stay bounded to human overrides, "
506
+ "not become a per-file registry"
507
+ ),
508
+ "filePurposeOverrides",
509
+ )
510
+ )
511
+
512
+ ownership = _as_list(record.get("pathOwnership"))
513
+ if ownership and len(ownership) > max(12, len(modules) * 3):
514
+ warnings.append(
515
+ _finding(
516
+ "CS-BOUNDEDNESS",
517
+ (
518
+ "pathOwnership is large relative to module count; "
519
+ "prefer module globs where possible"
520
+ ),
521
+ "pathOwnership",
522
+ )
523
+ )
524
+
525
+ for index, module in enumerate(modules):
526
+ if not isinstance(module, dict):
527
+ continue
528
+ globs = module.get("pathGlobs")
529
+ if not isinstance(globs, list) or len(globs) != 1 or not isinstance(globs[0], str):
530
+ continue
531
+ glob_value = globs[0]
532
+ if not _has_glob_magic(glob_value := str(glob_value)):
533
+ warnings.append(
534
+ _finding(
535
+ "CS-SINGLE-FILE-MODULE",
536
+ (
537
+ "module has a single non-glob path; ensure this is intentional "
538
+ "and not per-file metadata"
539
+ ),
540
+ f"modules[{index}].pathGlobs[0]",
541
+ )
542
+ )
543
+
544
+
545
+ def _has_glob_magic(value: str) -> bool:
546
+ return any(char in value for char in "*?[")
547
+
548
+
549
+ def validate_code_structure(
550
+ record: dict[str, Any], source: str = "<memory>", project_root: Path | None = None
551
+ ) -> ValidationResult:
552
+ """Validate one codeStructure record."""
553
+ errors: list[Finding] = []
554
+ warnings: list[Finding] = []
555
+ _validate_required_arrays(record, errors, source)
556
+ _scan_for_derived_fact_keys(record, errors, "codeStructure")
557
+
558
+ glob_owner: dict[str, str] = {}
559
+ module_ids: set[str] = set()
560
+ for index, module in enumerate(_as_list(record.get("modules"))):
561
+ module_id = _validate_module(module, index, errors, glob_owner)
562
+ if module_id is None:
563
+ continue
564
+ if module_id in module_ids:
565
+ errors.append(
566
+ _finding(
567
+ "CS-MODULE-ID", f"duplicate module id {module_id!r}", f"modules[{index}].id"
568
+ )
569
+ )
570
+ module_ids.add(module_id)
571
+
572
+ _validate_path_ownership(_as_list(record.get("pathOwnership")), module_ids, errors)
573
+ _validate_allowed_patterns(_as_list(record.get("allowedPatterns")), module_ids, errors)
574
+ _validate_projection_manifest(_as_list(record.get("projectionManifest")), errors, project_root)
575
+ _validate_file_purpose_overrides(record.get("filePurposeOverrides"), module_ids, errors)
576
+ _validate_glossary_refs(record.get("glossaryRefs"), errors, project_root)
577
+ _validate_boundedness(record, warnings)
578
+ return ValidationResult(errors=errors, warnings=warnings)
579
+
580
+
581
+ def load_json_file(path: Path) -> dict[str, Any]:
582
+ """Load a JSON object from *path*."""
583
+ try:
584
+ data = json.loads(path.read_text(encoding="utf-8"))
585
+ except FileNotFoundError as exc:
586
+ raise CodeStructureConfigError(f"codeStructure file not found: {path}") from exc
587
+ except json.JSONDecodeError as exc:
588
+ raise CodeStructureConfigError(
589
+ f"{path} is not valid JSON: {exc.msg} (line {exc.lineno})"
590
+ ) from exc
591
+ if not isinstance(data, dict):
592
+ raise CodeStructureConfigError(f"{path} top-level value must be an object")
593
+ return data
594
+
595
+
596
+ def validate_file(
597
+ path: Path, *, project_root: Path | None = None, allow_standalone: bool = True
598
+ ) -> ValidationResult:
599
+ """Load and validate the codeStructure record in *path*."""
600
+ data = load_json_file(path)
601
+ homes = extract_code_structure_homes(data)
602
+ errors: list[Finding] = []
603
+ if len(homes) > 1:
604
+ errors.append(
605
+ _finding(
606
+ "CS-HOME-CONFLICT",
607
+ (
608
+ "only one codeStructure home is allowed; found "
609
+ f"{', '.join(home.home for home in homes)}"
610
+ ),
611
+ str(path),
612
+ )
613
+ )
614
+ if project_root is not None and not allow_standalone:
615
+ rel_path = _project_relative(path, project_root)
616
+ if rel_path != PROJECT_DEFINITION_PATH.as_posix() and homes:
617
+ errors.append(
618
+ _finding(
619
+ "CS-HOME",
620
+ (
621
+ "canonical codeStructure metadata must live in "
622
+ "vbrief/PROJECT-DEFINITION.vbrief.json; sibling files "
623
+ "must be generated projections"
624
+ ),
625
+ str(path),
626
+ )
627
+ )
628
+
629
+ if not homes:
630
+ return ValidationResult(
631
+ errors=[
632
+ _finding(
633
+ "CS-MISSING",
634
+ f"no {PLAN_HOME} or {DIRECTIVE_HOME} record found",
635
+ str(path),
636
+ )
637
+ ]
638
+ )
639
+
640
+ extracted = homes[0]
641
+ result = validate_code_structure(
642
+ extracted.record,
643
+ source=f"{path}:{extracted.home}",
644
+ project_root=project_root,
645
+ )
646
+ return ValidationResult(errors=errors + result.errors, warnings=result.warnings)
647
+
648
+
649
+ def discover_code_structure_paths(project_root: Path) -> list[Path]:
650
+ """Discover codeStructure-bearing vBRIEFs for a project root."""
651
+ paths: dict[str, Path] = {}
652
+ project_def = project_root / "vbrief" / "PROJECT-DEFINITION.vbrief.json"
653
+ if project_def.exists():
654
+ try:
655
+ data = load_json_file(project_def)
656
+ except CodeStructureConfigError:
657
+ paths[project_def.as_posix()] = project_def
658
+ else:
659
+ if extract_code_structure(data) is not None:
660
+ paths[project_def.as_posix()] = project_def
661
+
662
+ vbrief_root = project_root / "vbrief"
663
+ if vbrief_root.exists():
664
+ for vbrief_path in sorted(vbrief_root.rglob("*.vbrief.json")):
665
+ if vbrief_path == project_def:
666
+ continue
667
+ try:
668
+ data = load_json_file(vbrief_path)
669
+ except CodeStructureConfigError:
670
+ continue
671
+ if extract_code_structure(data) is not None:
672
+ paths[vbrief_path.as_posix()] = vbrief_path
673
+ return [paths[key] for key in sorted(paths)]
674
+
675
+
676
+ def _result_to_dict(path: Path, result: ValidationResult) -> dict[str, Any]:
677
+ return {
678
+ "path": str(path),
679
+ "ok": result.ok,
680
+ "errors": [
681
+ {"code": finding.code, "message": finding.message, "location": finding.location}
682
+ for finding in result.errors
683
+ ],
684
+ "warnings": [
685
+ {"code": finding.code, "message": finding.message, "location": finding.location}
686
+ for finding in result.warnings
687
+ ],
688
+ }
689
+
690
+
691
+ def _config_error_to_dict(path: Path, error: CodeStructureConfigError) -> dict[str, Any]:
692
+ return {
693
+ "path": str(path),
694
+ "ok": False,
695
+ "errors": [{"code": "CS-CONFIG", "message": str(error), "location": str(path)}],
696
+ "warnings": [],
697
+ }
698
+
699
+
700
+ def main(argv: list[str] | None = None) -> int:
701
+ """CLI entry point."""
702
+ parser = argparse.ArgumentParser(description="Validate codeStructure metadata.")
703
+ parser.add_argument("--project-root", default=".", help="Project root for default discovery.")
704
+ parser.add_argument("--path", action="append", help="Explicit codeStructure vBRIEF path.")
705
+ parser.add_argument("--json", action="store_true", help="Emit machine-readable JSON summary.")
706
+ parser.add_argument("--strict", action="store_true", help="Treat warnings as failures.")
707
+ args = parser.parse_args(argv)
708
+
709
+ project_root = Path(args.project_root)
710
+ explicit_paths = bool(args.path)
711
+ paths = (
712
+ [Path(p) for p in args.path]
713
+ if explicit_paths
714
+ else discover_code_structure_paths(project_root)
715
+ )
716
+
717
+ if not paths:
718
+ if args.json:
719
+ print(json.dumps({"ok": True, "validated": []}, indent=2))
720
+ else:
721
+ print("OK: no codeStructure metadata found")
722
+ return 0
723
+
724
+ summaries: list[dict[str, Any]] = []
725
+ exit_code = 0
726
+ for path in paths:
727
+ try:
728
+ result = validate_file(
729
+ path,
730
+ project_root=None if explicit_paths else project_root,
731
+ allow_standalone=explicit_paths,
732
+ )
733
+ except CodeStructureConfigError as exc:
734
+ summaries.append(_config_error_to_dict(path, exc))
735
+ exit_code = 2
736
+ continue
737
+ summaries.append(_result_to_dict(path, result))
738
+ if exit_code == 0 and (not result.ok or (args.strict and result.warnings)):
739
+ exit_code = 1
740
+
741
+ if args.json:
742
+ print(json.dumps({"ok": exit_code == 0, "validated": summaries}, indent=2))
743
+ else:
744
+ for summary in summaries:
745
+ path = summary["path"]
746
+ for finding in summary["errors"]:
747
+ prefix = "ERROR" if finding["code"] == "CS-CONFIG" else "FAIL"
748
+ output = sys.stderr if prefix == "ERROR" else sys.stdout
749
+ print(
750
+ f"{prefix}: {path}: {finding['code']}: "
751
+ f"{finding['location']}: {finding['message']}",
752
+ file=output,
753
+ )
754
+ for finding in summary["warnings"]:
755
+ print(
756
+ f"WARN: {path}: {finding['code']}: "
757
+ f"{finding['location']}: {finding['message']}"
758
+ )
759
+ if summary["ok"] and (not args.strict or not summary["warnings"]):
760
+ print(f"OK: {path}")
761
+ return exit_code
762
+
763
+
764
+ if __name__ == "__main__":
765
+ sys.exit(main())