@deftai/directive-content 0.59.0 → 0.61.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 (190) hide show
  1. package/.githooks/pre-commit +10 -128
  2. package/.githooks/pre-push +8 -108
  3. package/Taskfile.yml +48 -58
  4. package/UPGRADING.md +19 -3
  5. package/docs/assets/directive-lifecycle-diagram.png +0 -0
  6. package/docs/directive-lifecycle.md +73 -0
  7. package/docs/getting-started.md +5 -1
  8. package/package.json +3 -3
  9. package/packs/skills/skills-pack-0.1.json +1 -1
  10. package/packs/strategies/strategies-pack-0.1.json +19 -19
  11. package/scm/github.md +37 -6
  12. package/skills/deft-directive-setup/SKILL.md +24 -15
  13. package/strategies/speckit.md +14 -14
  14. package/strategies/v0-20-contract.md +12 -1
  15. package/tasks/change.yml +16 -31
  16. package/tasks/ci.yml +8 -0
  17. package/tasks/commit.yml +12 -19
  18. package/tasks/core.yml +10 -0
  19. package/tasks/engine.yml +42 -0
  20. package/tasks/framework.yml +3 -0
  21. package/tasks/install.yml +20 -19
  22. package/tasks/migrate.yml +26 -15
  23. package/tasks/project.yml +26 -0
  24. package/tasks/toolchain.yml +15 -5
  25. package/tasks/vbrief.yml +4 -3
  26. package/tasks/verify.yml +12 -14
  27. package/templates/agents-entry.md +1 -1
  28. package/scripts/_agents_md.py +0 -494
  29. package/scripts/_cache_fetch.py +0 -635
  30. package/scripts/_cache_quota.py +0 -529
  31. package/scripts/_cache_refresh.py +0 -163
  32. package/scripts/_cache_validate.py +0 -209
  33. package/scripts/_content_root.py +0 -42
  34. package/scripts/_doctor_state.py +0 -277
  35. package/scripts/_event_detect.py +0 -305
  36. package/scripts/_events.py +0 -514
  37. package/scripts/_lifecycle_hygiene.py +0 -568
  38. package/scripts/_pathspec.py +0 -91
  39. package/scripts/_policy_show_cli.py +0 -266
  40. package/scripts/_precutover.py +0 -92
  41. package/scripts/_project_context.py +0 -224
  42. package/scripts/_project_definition_io.py +0 -164
  43. package/scripts/_relocate_snapshot.py +0 -209
  44. package/scripts/_relocate_states.py +0 -343
  45. package/scripts/_resolve_preflight_path.py +0 -152
  46. package/scripts/_safe_subprocess.py +0 -167
  47. package/scripts/_session_start_hook.py +0 -205
  48. package/scripts/_sor_gate_diff.py +0 -365
  49. package/scripts/_stdio_utf8.py +0 -59
  50. package/scripts/_triage_bootstrap_gitignore.py +0 -904
  51. package/scripts/_triage_classify_cli.py +0 -122
  52. package/scripts/_triage_queue_cli.py +0 -625
  53. package/scripts/_triage_scope_cli.py +0 -343
  54. package/scripts/_triage_scope_drift_cli.py +0 -121
  55. package/scripts/_triage_scope_ignores.py +0 -286
  56. package/scripts/_triage_scope_milestone.py +0 -432
  57. package/scripts/_triage_scope_mutations.py +0 -337
  58. package/scripts/_triage_scope_renderers.py +0 -207
  59. package/scripts/_triage_smoketest_stages.py +0 -674
  60. package/scripts/_triage_subscribe_cli.py +0 -140
  61. package/scripts/_triage_welcome_cli.py +0 -421
  62. package/scripts/_vbrief_build.py +0 -239
  63. package/scripts/_vbrief_fidelity.py +0 -479
  64. package/scripts/_vbrief_legacy.py +0 -589
  65. package/scripts/_vbrief_reconciliation.py +0 -883
  66. package/scripts/_vbrief_routing.py +0 -277
  67. package/scripts/_vbrief_safety.py +0 -778
  68. package/scripts/_vbrief_sources.py +0 -312
  69. package/scripts/_vbrief_speckit.py +0 -262
  70. package/scripts/_vbrief_story_quality.py +0 -353
  71. package/scripts/_vbrief_validation.py +0 -299
  72. package/scripts/build_dist.py +0 -412
  73. package/scripts/cache.py +0 -1078
  74. package/scripts/cache_scanner.py +0 -745
  75. package/scripts/candidates_log.py +0 -432
  76. package/scripts/capacity_backfill.py +0 -680
  77. package/scripts/capacity_show.py +0 -653
  78. package/scripts/ci_local.py +0 -689
  79. package/scripts/code_structure_validate.py +0 -765
  80. package/scripts/codebase_default_extractor.py +0 -495
  81. package/scripts/codebase_map.py +0 -304
  82. package/scripts/codebase_map_fresh.py +0 -104
  83. package/scripts/codebase_projection_registry.py +0 -94
  84. package/scripts/codebase_provider.py +0 -582
  85. package/scripts/doctor.py +0 -2552
  86. package/scripts/framework_commands.py +0 -505
  87. package/scripts/gh_rest.py +0 -882
  88. package/scripts/github_auth_modes.py +0 -437
  89. package/scripts/github_body.py +0 -292
  90. package/scripts/ip_risk.py +0 -531
  91. package/scripts/issue_emit.py +0 -670
  92. package/scripts/issue_ingest.py +0 -1064
  93. package/scripts/migrate_preflight.py +0 -418
  94. package/scripts/migrate_vbrief.py +0 -2677
  95. package/scripts/monitor_pr.py +0 -401
  96. package/scripts/pack_migrate_lessons.py +0 -336
  97. package/scripts/pack_migrate_patterns.py +0 -254
  98. package/scripts/pack_migrate_rules.py +0 -350
  99. package/scripts/pack_migrate_skills.py +0 -423
  100. package/scripts/pack_migrate_strategies.py +0 -311
  101. package/scripts/pack_migrate_swarm_spec.py +0 -250
  102. package/scripts/pack_render.py +0 -434
  103. package/scripts/packs_slice.py +0 -712
  104. package/scripts/platform_capabilities.py +0 -336
  105. package/scripts/policy.py +0 -2826
  106. package/scripts/policy_set.py +0 -324
  107. package/scripts/pr_check_closing_keywords.py +0 -524
  108. package/scripts/pr_check_protected_issues.py +0 -267
  109. package/scripts/pr_merge_readiness.py +0 -1004
  110. package/scripts/pr_wait_mergeable.py +0 -669
  111. package/scripts/prd_render.py +0 -159
  112. package/scripts/preflight_architecture_sor.py +0 -974
  113. package/scripts/preflight_branch.py +0 -289
  114. package/scripts/preflight_cache.py +0 -974
  115. package/scripts/preflight_gh.py +0 -721
  116. package/scripts/preflight_implementation.py +0 -272
  117. package/scripts/preflight_story_start.py +0 -838
  118. package/scripts/preflight_wip_cap.py +0 -149
  119. package/scripts/probe_session.py +0 -545
  120. package/scripts/project_render.py +0 -293
  121. package/scripts/quarantine_ext.py +0 -237
  122. package/scripts/reconcile_issues.py +0 -1442
  123. package/scripts/refresh-path.ps1 +0 -107
  124. package/scripts/release.py +0 -2030
  125. package/scripts/release_e2e.py +0 -1011
  126. package/scripts/release_publish.py +0 -486
  127. package/scripts/release_rollback.py +0 -980
  128. package/scripts/relocate.py +0 -1034
  129. package/scripts/resolve_changelog_unreleased.py +0 -667
  130. package/scripts/resolve_version.py +0 -490
  131. package/scripts/resume_conditions.py +0 -706
  132. package/scripts/ritual_sentinel.py +0 -609
  133. package/scripts/roadmap_render.py +0 -635
  134. package/scripts/rule_ownership_lint.py +0 -325
  135. package/scripts/scm.py +0 -591
  136. package/scripts/scope_audit_log.py +0 -387
  137. package/scripts/scope_decompose.py +0 -654
  138. package/scripts/scope_demote.py +0 -509
  139. package/scripts/scope_lifecycle.py +0 -1126
  140. package/scripts/scope_undo.py +0 -772
  141. package/scripts/session_start.py +0 -406
  142. package/scripts/setup_ghx.py +0 -339
  143. package/scripts/setup_windows.ps1 +0 -220
  144. package/scripts/slice_audit.py +0 -585
  145. package/scripts/slice_record.py +0 -530
  146. package/scripts/slice_record_existing.py +0 -692
  147. package/scripts/slug_normalize.py +0 -178
  148. package/scripts/spec_render.py +0 -477
  149. package/scripts/spec_validate.py +0 -238
  150. package/scripts/subagent_monitor.py +0 -658
  151. package/scripts/swarm_complete_cohort.py +0 -644
  152. package/scripts/swarm_launch.py +0 -1206
  153. package/scripts/swarm_readiness.py +0 -554
  154. package/scripts/swarm_verify_review_clean.py +0 -438
  155. package/scripts/swarm_worktrees.py +0 -497
  156. package/scripts/toolchain-check.py +0 -52
  157. package/scripts/triage_actions.py +0 -871
  158. package/scripts/triage_bootstrap.py +0 -1153
  159. package/scripts/triage_bulk.py +0 -630
  160. package/scripts/triage_classify.py +0 -932
  161. package/scripts/triage_help.py +0 -1685
  162. package/scripts/triage_queue.py +0 -1944
  163. package/scripts/triage_reconcile.py +0 -581
  164. package/scripts/triage_refresh.py +0 -643
  165. package/scripts/triage_scope.py +0 -999
  166. package/scripts/triage_scope_drift.py +0 -575
  167. package/scripts/triage_smoketest.py +0 -396
  168. package/scripts/triage_subscribe.py +0 -399
  169. package/scripts/triage_summary.py +0 -1011
  170. package/scripts/triage_welcome.py +0 -1178
  171. package/scripts/ts_check_lane.py +0 -86
  172. package/scripts/validate-links.py +0 -64
  173. package/scripts/validate_strategy_output.py +0 -212
  174. package/scripts/vbrief_activate.py +0 -228
  175. package/scripts/vbrief_migrate_conformance.py +0 -368
  176. package/scripts/vbrief_reconcile_graph.py +0 -306
  177. package/scripts/vbrief_reconcile_labels.py +0 -460
  178. package/scripts/vbrief_reconcile_umbrellas.py +0 -741
  179. package/scripts/vbrief_validate.py +0 -1144
  180. package/scripts/verify-stubs.py +0 -61
  181. package/scripts/verify_capacity.py +0 -160
  182. package/scripts/verify_encoding.py +0 -699
  183. package/scripts/verify_hooks_installed.py +0 -206
  184. package/scripts/verify_investigation.py +0 -360
  185. package/scripts/verify_judgment_gates.py +0 -827
  186. package/scripts/verify_no_task_runtime.py +0 -171
  187. package/scripts/verify_scm_boundary.py +0 -509
  188. package/scripts/verify_session_ritual.py +0 -389
  189. package/scripts/verify_tools.py +0 -426
  190. package/scripts/verify_vbrief_conformance.py +0 -478
@@ -1,765 +0,0 @@
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())