@deftai/directive-content 0.55.1 → 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 (220) hide show
  1. package/.githooks/pre-commit +143 -0
  2. package/.githooks/pre-push +121 -0
  3. package/QUICK-START.md +13 -3
  4. package/Taskfile.yml +934 -0
  5. package/UPGRADING.md +82 -11
  6. package/events/README.md +3 -3
  7. package/package.json +5 -4
  8. package/packs/skills/skills-pack-0.1.json +22 -22
  9. package/scripts/_agents_md.py +494 -0
  10. package/scripts/_cache_fetch.py +635 -0
  11. package/scripts/_cache_quota.py +529 -0
  12. package/scripts/_cache_refresh.py +163 -0
  13. package/scripts/_cache_validate.py +209 -0
  14. package/scripts/_content_root.py +42 -0
  15. package/scripts/_doctor_state.py +277 -0
  16. package/scripts/_event_detect.py +305 -0
  17. package/scripts/_events.py +514 -0
  18. package/scripts/_lifecycle_hygiene.py +568 -0
  19. package/scripts/_pathspec.py +91 -0
  20. package/scripts/_policy_show_cli.py +266 -0
  21. package/scripts/_precutover.py +92 -0
  22. package/scripts/_project_context.py +224 -0
  23. package/scripts/_project_definition_io.py +164 -0
  24. package/scripts/_relocate_snapshot.py +209 -0
  25. package/scripts/_relocate_states.py +343 -0
  26. package/scripts/_resolve_preflight_path.py +152 -0
  27. package/scripts/_safe_subprocess.py +167 -0
  28. package/scripts/_session_start_hook.py +205 -0
  29. package/scripts/_sor_gate_diff.py +365 -0
  30. package/scripts/_stdio_utf8.py +59 -0
  31. package/scripts/_triage_bootstrap_gitignore.py +904 -0
  32. package/scripts/_triage_classify_cli.py +122 -0
  33. package/scripts/_triage_queue_cli.py +625 -0
  34. package/scripts/_triage_scope_cli.py +343 -0
  35. package/scripts/_triage_scope_drift_cli.py +121 -0
  36. package/scripts/_triage_scope_ignores.py +286 -0
  37. package/scripts/_triage_scope_milestone.py +432 -0
  38. package/scripts/_triage_scope_mutations.py +337 -0
  39. package/scripts/_triage_scope_renderers.py +207 -0
  40. package/scripts/_triage_smoketest_stages.py +674 -0
  41. package/scripts/_triage_subscribe_cli.py +140 -0
  42. package/scripts/_triage_welcome_cli.py +421 -0
  43. package/scripts/_vbrief_build.py +239 -0
  44. package/scripts/_vbrief_fidelity.py +479 -0
  45. package/scripts/_vbrief_legacy.py +589 -0
  46. package/scripts/_vbrief_reconciliation.py +883 -0
  47. package/scripts/_vbrief_routing.py +277 -0
  48. package/scripts/_vbrief_safety.py +778 -0
  49. package/scripts/_vbrief_sources.py +312 -0
  50. package/scripts/_vbrief_speckit.py +262 -0
  51. package/scripts/_vbrief_story_quality.py +353 -0
  52. package/scripts/_vbrief_validation.py +299 -0
  53. package/scripts/build_dist.py +412 -0
  54. package/scripts/cache.py +1078 -0
  55. package/scripts/cache_scanner.py +745 -0
  56. package/scripts/candidates_log.py +432 -0
  57. package/scripts/capacity_backfill.py +680 -0
  58. package/scripts/capacity_show.py +653 -0
  59. package/scripts/ci_local.py +689 -0
  60. package/scripts/code_structure_validate.py +765 -0
  61. package/scripts/codebase_default_extractor.py +495 -0
  62. package/scripts/codebase_map.py +304 -0
  63. package/scripts/codebase_map_fresh.py +104 -0
  64. package/scripts/codebase_projection_registry.py +94 -0
  65. package/scripts/codebase_provider.py +582 -0
  66. package/scripts/doctor.py +2257 -0
  67. package/scripts/framework_commands.py +505 -0
  68. package/scripts/gh_rest.py +882 -0
  69. package/scripts/github_auth_modes.py +437 -0
  70. package/scripts/github_body.py +292 -0
  71. package/scripts/ip_risk.py +531 -0
  72. package/scripts/issue_emit.py +670 -0
  73. package/scripts/issue_ingest.py +1064 -0
  74. package/scripts/migrate_preflight.py +418 -0
  75. package/scripts/migrate_vbrief.py +2677 -0
  76. package/scripts/monitor_pr.py +401 -0
  77. package/scripts/pack_migrate_lessons.py +336 -0
  78. package/scripts/pack_migrate_patterns.py +254 -0
  79. package/scripts/pack_migrate_rules.py +350 -0
  80. package/scripts/pack_migrate_skills.py +423 -0
  81. package/scripts/pack_migrate_strategies.py +311 -0
  82. package/scripts/pack_migrate_swarm_spec.py +250 -0
  83. package/scripts/pack_render.py +434 -0
  84. package/scripts/packs_slice.py +712 -0
  85. package/scripts/platform_capabilities.py +336 -0
  86. package/scripts/policy.py +2826 -0
  87. package/scripts/policy_set.py +324 -0
  88. package/scripts/pr_check_closing_keywords.py +524 -0
  89. package/scripts/pr_check_protected_issues.py +267 -0
  90. package/scripts/pr_merge_readiness.py +1004 -0
  91. package/scripts/pr_wait_mergeable.py +669 -0
  92. package/scripts/prd_render.py +159 -0
  93. package/scripts/preflight_architecture_sor.py +974 -0
  94. package/scripts/preflight_branch.py +289 -0
  95. package/scripts/preflight_cache.py +974 -0
  96. package/scripts/preflight_gh.py +721 -0
  97. package/scripts/preflight_implementation.py +272 -0
  98. package/scripts/preflight_story_start.py +838 -0
  99. package/scripts/preflight_wip_cap.py +149 -0
  100. package/scripts/probe_session.py +545 -0
  101. package/scripts/project_render.py +293 -0
  102. package/scripts/quarantine_ext.py +237 -0
  103. package/scripts/reconcile_issues.py +1442 -0
  104. package/scripts/refresh-path.ps1 +107 -0
  105. package/scripts/release.py +2030 -0
  106. package/scripts/release_e2e.py +1011 -0
  107. package/scripts/release_publish.py +486 -0
  108. package/scripts/release_rollback.py +980 -0
  109. package/scripts/relocate.py +1034 -0
  110. package/scripts/resolve_changelog_unreleased.py +667 -0
  111. package/scripts/resolve_version.py +490 -0
  112. package/scripts/resume_conditions.py +706 -0
  113. package/scripts/ritual_sentinel.py +609 -0
  114. package/scripts/roadmap_render.py +635 -0
  115. package/scripts/rule_ownership_lint.py +325 -0
  116. package/scripts/scm.py +591 -0
  117. package/scripts/scope_audit_log.py +387 -0
  118. package/scripts/scope_decompose.py +654 -0
  119. package/scripts/scope_demote.py +509 -0
  120. package/scripts/scope_lifecycle.py +1126 -0
  121. package/scripts/scope_undo.py +772 -0
  122. package/scripts/session_start.py +406 -0
  123. package/scripts/setup_ghx.py +339 -0
  124. package/scripts/setup_windows.ps1 +220 -0
  125. package/scripts/slice_audit.py +585 -0
  126. package/scripts/slice_record.py +530 -0
  127. package/scripts/slice_record_existing.py +692 -0
  128. package/scripts/slug_normalize.py +178 -0
  129. package/scripts/spec_render.py +477 -0
  130. package/scripts/spec_validate.py +238 -0
  131. package/scripts/subagent_monitor.py +658 -0
  132. package/scripts/swarm_complete_cohort.py +644 -0
  133. package/scripts/swarm_launch.py +1206 -0
  134. package/scripts/swarm_readiness.py +554 -0
  135. package/scripts/swarm_verify_review_clean.py +438 -0
  136. package/scripts/swarm_worktrees.py +497 -0
  137. package/scripts/toolchain-check.py +52 -0
  138. package/scripts/triage_actions.py +871 -0
  139. package/scripts/triage_bootstrap.py +1153 -0
  140. package/scripts/triage_bulk.py +630 -0
  141. package/scripts/triage_classify.py +932 -0
  142. package/scripts/triage_help.py +1685 -0
  143. package/scripts/triage_queue.py +1944 -0
  144. package/scripts/triage_reconcile.py +581 -0
  145. package/scripts/triage_refresh.py +643 -0
  146. package/scripts/triage_scope.py +999 -0
  147. package/scripts/triage_scope_drift.py +575 -0
  148. package/scripts/triage_smoketest.py +396 -0
  149. package/scripts/triage_subscribe.py +399 -0
  150. package/scripts/triage_summary.py +1011 -0
  151. package/scripts/triage_welcome.py +1178 -0
  152. package/scripts/ts_check_lane.py +86 -0
  153. package/scripts/validate-links.py +64 -0
  154. package/scripts/validate_strategy_output.py +212 -0
  155. package/scripts/vbrief_activate.py +228 -0
  156. package/scripts/vbrief_migrate_conformance.py +368 -0
  157. package/scripts/vbrief_reconcile_graph.py +306 -0
  158. package/scripts/vbrief_reconcile_labels.py +460 -0
  159. package/scripts/vbrief_reconcile_umbrellas.py +741 -0
  160. package/scripts/vbrief_validate.py +1195 -0
  161. package/scripts/verify-stubs.py +61 -0
  162. package/scripts/verify_capacity.py +160 -0
  163. package/scripts/verify_encoding.py +699 -0
  164. package/scripts/verify_hooks_installed.py +206 -0
  165. package/scripts/verify_investigation.py +360 -0
  166. package/scripts/verify_judgment_gates.py +827 -0
  167. package/scripts/verify_no_task_runtime.py +171 -0
  168. package/scripts/verify_scm_boundary.py +509 -0
  169. package/scripts/verify_session_ritual.py +389 -0
  170. package/scripts/verify_tools.py +426 -0
  171. package/scripts/verify_vbrief_conformance.py +478 -0
  172. package/skills/deft-directive-swarm/SKILL.md +7 -26
  173. package/skills/deft-directive-sync/SKILL.md +1 -1
  174. package/tasks/architecture.yml +13 -0
  175. package/tasks/cache.yml +69 -0
  176. package/tasks/capacity.yml +38 -0
  177. package/tasks/change.yml +46 -0
  178. package/tasks/changelog.yml +24 -0
  179. package/tasks/ci.yml +49 -0
  180. package/tasks/codebase.yml +47 -0
  181. package/tasks/commit.yml +30 -0
  182. package/tasks/core.yml +126 -0
  183. package/tasks/deployments.yml +54 -0
  184. package/tasks/framework.yml +74 -0
  185. package/tasks/install.yml +60 -0
  186. package/tasks/issue.yml +50 -0
  187. package/tasks/migrate.yml +73 -0
  188. package/tasks/packs.yml +92 -0
  189. package/tasks/policy.yml +75 -0
  190. package/tasks/pr.yml +89 -0
  191. package/tasks/prd.yml +39 -0
  192. package/tasks/project.yml +27 -0
  193. package/tasks/reconcile.yml +32 -0
  194. package/tasks/relocate.yml +56 -0
  195. package/tasks/roadmap.yml +28 -0
  196. package/tasks/scm.yml +126 -0
  197. package/tasks/scope-undo.yml +36 -0
  198. package/tasks/scope.yml +141 -0
  199. package/tasks/session.yml +19 -0
  200. package/tasks/setup.yml +37 -0
  201. package/tasks/slice.yml +69 -0
  202. package/tasks/spec.yml +41 -0
  203. package/tasks/swarm.yml +85 -0
  204. package/tasks/toolchain.yml +13 -0
  205. package/tasks/triage-actions.yml +94 -0
  206. package/tasks/triage-bootstrap.yml +43 -0
  207. package/tasks/triage-bulk.yml +75 -0
  208. package/tasks/triage-classify.yml +30 -0
  209. package/tasks/triage-queue.yml +50 -0
  210. package/tasks/triage-reconcile.yml +29 -0
  211. package/tasks/triage-scope-drift.yml +29 -0
  212. package/tasks/triage-scope.yml +31 -0
  213. package/tasks/triage-smoketest.yml +33 -0
  214. package/tasks/triage-subscribe.yml +36 -0
  215. package/tasks/triage-summary.yml +29 -0
  216. package/tasks/triage-welcome.yml +32 -0
  217. package/tasks/ts.yml +328 -0
  218. package/tasks/vbrief.yml +206 -0
  219. package/tasks/verify.yml +292 -0
  220. package/templates/agents-entry.md +2 -2
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env python3
2
+ """Node-toolchain-aware TypeScript lane for `task check` (#1530, #1790).
3
+
4
+ `task check` -> `check:framework-source` historically ran only the Python
5
+ suite + gates; the TypeScript engine (biome lint, tsc build, vitest) ran only
6
+ in the dedicated CI job. That split let a TS lint/format/test failure pass a
7
+ contributor's local `task check` and redden CI after push (PR #1780: a worker's
8
+ local gate was green while CI biome failed on unformatted files).
9
+
10
+ This helper closes the gap WITHOUT regressing the documented invariant that
11
+ `check:framework-source` must not hard-require a Node toolchain in Node-less
12
+ environments (the vendored-consumer guard pattern from #1474). When `pnpm` is on
13
+ PATH it runs `pnpm run lint`, `pnpm run build`, and `pnpm run test` in order,
14
+ failing fast on the first non-zero exit. When `pnpm` is absent it prints a clear
15
+ notice and exits 0 -- the TS lane stays validated by the CI job in that case.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import argparse
21
+ import shutil
22
+ import subprocess # noqa: S404 -- fixed, non-shell pnpm invocations only
23
+ import sys
24
+ from collections.abc import Callable, Sequence
25
+ from pathlib import Path
26
+ from typing import Any
27
+
28
+ # Run order is deliberate: lint (cheapest, catches the PR #1780 biome class
29
+ # first), then build, then the test suite.
30
+ LANE_COMMANDS: tuple[tuple[str, ...], ...] = (
31
+ ("run", "lint"),
32
+ ("run", "build"),
33
+ ("run", "test"),
34
+ )
35
+
36
+ SKIP_NOTICE = (
37
+ "[ts:check-lane] pnpm not found on PATH -- skipping the TypeScript lane "
38
+ "(build/lint/test). The TS engine stays validated by the dedicated CI job. "
39
+ "Install the Node toolchain (pnpm) to run the TS lane locally."
40
+ )
41
+
42
+
43
+ def _resolve_pnpm() -> str | None:
44
+ """Return the pnpm executable path, or None when it is not installed."""
45
+ return shutil.which("pnpm")
46
+
47
+
48
+ def run_ts_lane(
49
+ project_root: Path,
50
+ *,
51
+ pnpm: str | None,
52
+ runner: Callable[..., Any] = subprocess.run,
53
+ out: Callable[[str], Any] = print,
54
+ ) -> int:
55
+ """Run the TS lane when pnpm is available; skip (exit 0) when it is not.
56
+
57
+ `pnpm`, `runner`, and `out` are injected so the guard logic is unit-testable
58
+ without a real Node toolchain or real subprocess execution.
59
+ """
60
+ if not pnpm:
61
+ out(SKIP_NOTICE)
62
+ return 0
63
+
64
+ for command in LANE_COMMANDS:
65
+ argv: Sequence[str] = (pnpm, *command)
66
+ result = runner(argv, cwd=str(project_root))
67
+ code = getattr(result, "returncode", 0)
68
+ if code != 0:
69
+ out(f"[ts:check-lane] `pnpm {' '.join(command)}` failed (exit {code}).")
70
+ return code
71
+ return 0
72
+
73
+
74
+ def main(argv: Sequence[str] | None = None) -> int:
75
+ parser = argparse.ArgumentParser(description=__doc__)
76
+ parser.add_argument(
77
+ "--project-root",
78
+ default=".",
79
+ help="Repo root that owns the pnpm workspace (default: cwd).",
80
+ )
81
+ args = parser.parse_args(argv)
82
+ return run_ts_lane(Path(args.project_root), pnpm=_resolve_pnpm())
83
+
84
+
85
+ if __name__ == "__main__":
86
+ sys.exit(main())
@@ -0,0 +1,64 @@
1
+ """Validate internal links in markdown files."""
2
+
3
+ import os
4
+ import re
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ EXCLUDE_DIRS = {
9
+ ".git", "backup", "node_modules", ".venv", "__pycache__", "dist",
10
+ ".planning", "specs", # planning docs and test fixtures
11
+ }
12
+ LINK_RE = re.compile(r"\[([^\]]*)\]\(([^)]+)\)")
13
+
14
+ # Skip template variables, reference markers, and example-only links
15
+ SKIP_PATTERNS = re.compile(r"[{}@]|^\[|^\./relative-|^path$")
16
+
17
+
18
+ def main() -> int:
19
+ broken = []
20
+
21
+ for md in sorted(Path(".").rglob("*.md")):
22
+ if any(p in md.parts for p in EXCLUDE_DIRS):
23
+ continue
24
+ # Skip history/archive/ files
25
+ if "history" in md.parts and "archive" in md.parts:
26
+ continue
27
+ try:
28
+ text = md.read_text("utf-8", errors="replace")
29
+ for i, line in enumerate(text.splitlines(), 1):
30
+ for m in LINK_RE.finditer(line):
31
+ target = m.group(2)
32
+ # Skip external URLs, anchors, and mailto
33
+ if target.startswith(("http://", "https://", "mailto:", "#")):
34
+ continue
35
+ # Skip template variables and example links
36
+ if SKIP_PATTERNS.search(target):
37
+ continue
38
+ # Strip anchor and query params
39
+ clean = target.split("#")[0].split("?")[0]
40
+ if not clean:
41
+ continue
42
+ resolved = (md.parent / clean).resolve()
43
+ if not resolved.exists():
44
+ broken.append((str(md), i, target))
45
+ except Exception:
46
+ pass
47
+
48
+ strict = os.environ.get("LINK_CHECK_STRICT", "") == "1" or "--strict" in sys.argv
49
+
50
+ if broken:
51
+ mode = "errors" if strict else "warnings"
52
+ print(f"Found {len(broken)} broken internal link(s) ({mode}):")
53
+ for fp, ln, target in broken[:50]:
54
+ print(f" {fp}:{ln} -> {target}")
55
+ if len(broken) > 50:
56
+ print(f" ... and {len(broken) - 50} more")
57
+ return 1 if strict else 0
58
+
59
+ print("All internal markdown links valid")
60
+ return 0
61
+
62
+
63
+ if __name__ == "__main__":
64
+ sys.exit(main())
@@ -0,0 +1,212 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ validate_strategy_output.py -- Deterministic validation gate for
4
+ strategy output shape (v0.20 contract per #1166 s2).
5
+
6
+ Enforces that any vbrief/ tree produced by a spec-generating strategy (yolo, interview,
7
+ speckit, rapid, enterprise, ...) conforms to the canonical v0.20 output contract.
8
+
9
+ See: strategies/v0-20-contract.md (when present) and the parent epic #1166.
10
+
11
+ Rules enforced (hard fail):
12
+ - All scope vBRIEFs under vbrief/proposed/ (and other lifecycle dirs if present) MUST
13
+ use the date-prefixed filename convention: YYYY-MM-DD-<slug>.vbrief.json
14
+ (catches interview-style bare names like "scaffold.vbrief.json").
15
+ - vbrief/PROJECT-DEFINITION.vbrief.json MUST exist (full project identity).
16
+ - vbrief/specification.vbrief.json MUST NOT be present as a strategy-produced artifact
17
+ (legacy dual-write). The framework's own canonical source-of-truth copy is tolerated,
18
+ as is a post-cutover full-spec consumer tree where specification.vbrief.json is the
19
+ canonical source rendered to SPECIFICATION.md and all lifecycle folders exist.
20
+ - If vbrief/ exists, the five standard lifecycle subfolders should be present or the
21
+ strategy must have created them (proposed/ at minimum for emission).
22
+
23
+ Exit codes:
24
+ 0 -- conformant (or framework self with tolerated legacy spec.vbrief)
25
+ 1 -- non-conformant output shape (prints actionable errors citing the contract)
26
+ 2 -- usage / invocation error
27
+
28
+ Usage:
29
+ uv run python scripts/validate_strategy_output.py [--project-root <path>] [--strict]
30
+
31
+ Wired into:
32
+ - `task check` (via root Taskfile.yml)
33
+ - skills/deft-directive-build/SKILL.md Pre-Cutover Detection Guard (generalized)
34
+ - CI matrix (via task check)
35
+
36
+ Story: s2-deterministic-gate under #1166
37
+ """
38
+
39
+ from __future__ import annotations
40
+
41
+ import argparse
42
+ import re
43
+ import sys
44
+ from pathlib import Path
45
+
46
+ # Filename pattern for v0.20-conformant scope vBRIEFs (date-prefixed).
47
+ # Matches the convention in vbrief/vbrief.md and conventions/vbrief-filenames.md
48
+ DATE_PREFIXED_RE = re.compile(r"^\d{4}-\d{2}-\d{2}-[a-z0-9]+(?:-[a-z0-9]+)*\.vbrief\.json$")
49
+ GENERATED_SPEC_PURPOSE = "<!-- Purpose: rendered specification -->"
50
+ GENERATED_SPEC_SOURCE = "<!-- Source of truth: vbrief/specification.vbrief.json -->"
51
+ LIFECYCLE_DIRS = ("proposed", "pending", "active", "completed", "cancelled")
52
+
53
+
54
+ def _is_deft_framework_root(project_root: Path) -> bool:
55
+ """Heuristic: is this the deft framework source itself?
56
+ (tolerate its specification.vbrief.json as canonical source, not strategy output)
57
+ """
58
+ return (
59
+ (project_root / "AGENTS.md").exists()
60
+ and (project_root / "Taskfile.yml").exists()
61
+ and (project_root / "strategies").is_dir()
62
+ )
63
+
64
+
65
+ def _read_text_safe(path: Path) -> str:
66
+ try:
67
+ return path.read_text(encoding="utf-8", errors="replace")
68
+ except OSError:
69
+ return ""
70
+
71
+
72
+ def _has_complete_lifecycle(vbrief_dir: Path) -> bool:
73
+ return all((vbrief_dir / folder).is_dir() for folder in LIFECYCLE_DIRS)
74
+
75
+
76
+ def _is_post_cutover_full_spec_state(project_root: Path) -> bool:
77
+ """Return True for canonical consumer full-spec state.
78
+
79
+ This is deliberately stricter than "specification.vbrief.json exists" so
80
+ the gate still catches strategy-generated legacy dual-writes. A consumer
81
+ may keep ``vbrief/specification.vbrief.json`` as the source of truth only
82
+ after the vBRIEF-centric shape is complete and the root SPECIFICATION.md is
83
+ a rendered export from that source.
84
+ """
85
+ vbrief_dir = project_root / "vbrief"
86
+ spec_md = _read_text_safe(project_root / "SPECIFICATION.md")
87
+ return (
88
+ # Caller already confirmed specification.vbrief.json exists; the
89
+ # remaining conditions separate canonical state from a legacy dual-write.
90
+ (vbrief_dir / "PROJECT-DEFINITION.vbrief.json").is_file()
91
+ and _has_complete_lifecycle(vbrief_dir)
92
+ and GENERATED_SPEC_PURPOSE in spec_md
93
+ and GENERATED_SPEC_SOURCE in spec_md
94
+ )
95
+
96
+
97
+ def validate_strategy_output(project_root: Path, strict: bool = False) -> list[str]:
98
+ """
99
+ Return list of error strings (empty == pass).
100
+ """
101
+ errors: list[str] = []
102
+ vbrief_dir = project_root / "vbrief"
103
+
104
+ if not vbrief_dir.exists():
105
+ # No vbrief/ produced at all -- some legacy strategies may still do this,
106
+ # but v0.20 contract requires the lifecycle layout. Flag only in strict mode
107
+ # or when other signals present; for now soft (strategies are converging).
108
+ if strict:
109
+ errors.append(
110
+ "vbrief/ directory missing entirely. v0.20 strategies must emit at least "
111
+ "vbrief/proposed/ (with date-prefixed files) + PROJECT-DEFINITION.vbrief.json."
112
+ )
113
+ return errors
114
+
115
+ # 1. PROJECT-DEFINITION.vbrief.json must exist at vbrief/ root.
116
+ proj_def = vbrief_dir / "PROJECT-DEFINITION.vbrief.json"
117
+ if not proj_def.exists():
118
+ errors.append(
119
+ "Missing vbrief/PROJECT-DEFINITION.vbrief.json. "
120
+ "All v0.20-conformant strategy output must include a complete project definition "
121
+ "(see v0-20-contract.md and task project:render)."
122
+ )
123
+
124
+ # 2. Forbid legacy specification.vbrief.json in generated user projects.
125
+ # Tolerate canonical source copies only in the framework tree or in
126
+ # complete post-cutover consumer full-spec state.
127
+ spec_legacy = vbrief_dir / "specification.vbrief.json"
128
+ if (
129
+ spec_legacy.exists()
130
+ and not _is_deft_framework_root(project_root)
131
+ and not _is_post_cutover_full_spec_state(project_root)
132
+ ):
133
+ errors.append(
134
+ "Legacy artifact vbrief/specification.vbrief.json present. "
135
+ "v0.20 strategies MUST NOT dual-write the old specification.vbrief.json "
136
+ "alongside scope vBRIEFs in the lifecycle folders. "
137
+ "See strategies/v0-20-contract.md (contract) and issue #1166."
138
+ )
139
+ # Framework source / post-cutover full-spec state tolerated (canonical spec,
140
+ # not strategy output).
141
+
142
+ # 3. Every .vbrief.json under the lifecycle folders (proposed/ primarily, but all)
143
+ # must be date-prefixed. This is the key shape invariant for s2.
144
+ for dname in LIFECYCLE_DIRS:
145
+ dpath = vbrief_dir / dname
146
+ if dpath.exists() and dpath.is_dir():
147
+ for f in sorted(dpath.glob("*.vbrief.json")):
148
+ if not DATE_PREFIXED_RE.match(f.name):
149
+ errors.append(
150
+ f"Non-conformant filename in vbrief/{dname}/: {f.name}. "
151
+ "v0.20 requires strict YYYY-MM-DD-<slug>.vbrief.json "
152
+ "(date prefix from creation). Bare names (e.g. scaffold.vbrief.json) "
153
+ "are pre-v0.20. See strategies/v0-20-contract.md and "
154
+ "vbrief/vbrief.md filename convention."
155
+ )
156
+
157
+ # 4. If proposed/ exists it must not be empty for a strategy that claims to have emitted scope.
158
+ # (light check; real emptiness is often valid for trivial specs)
159
+ # We rely primarily on the filename rule above.
160
+
161
+ return errors
162
+
163
+
164
+ def main(argv: list[str] | None = None) -> int:
165
+ parser = argparse.ArgumentParser(
166
+ description="Deterministic validation gate for v0.20 strategy output shape."
167
+ )
168
+ parser.add_argument(
169
+ "--project-root",
170
+ type=Path,
171
+ default=Path("."),
172
+ help="Root of the project whose vbrief/ tree to validate (default: cwd)",
173
+ )
174
+ parser.add_argument(
175
+ "--strict",
176
+ action="store_true",
177
+ help="Treat missing vbrief/ as error (useful in CI for generated projects)",
178
+ )
179
+ parser.add_argument(
180
+ "--quiet",
181
+ action="store_true",
182
+ help="Suppress success message on clean exit",
183
+ )
184
+ args = parser.parse_args(argv)
185
+
186
+ project_root = args.project_root.resolve()
187
+ errors = validate_strategy_output(project_root, strict=args.strict)
188
+
189
+ if errors:
190
+ print("❌ Strategy output shape validation FAILED (v0.20 contract gate)", file=sys.stderr)
191
+ for err in errors:
192
+ print(f" • {err}", file=sys.stderr)
193
+ print(
194
+ "\nReference: strategies/v0-20-contract.md (once landed) + "
195
+ "https://github.com/deftai/directive/issues/1166 (s2-deterministic-gate)",
196
+ file=sys.stderr,
197
+ )
198
+ print(
199
+ "Fix: re-run the emitting strategy after the contract migration "
200
+ "stories land, or run `task migrate:vbrief` + `task project:render` "
201
+ "+ `task scope:promote` as appropriate.",
202
+ file=sys.stderr,
203
+ )
204
+ return 1
205
+
206
+ if not args.quiet:
207
+ print("✓ Strategy output shape conforms to v0.20 contract")
208
+ return 0
209
+
210
+
211
+ if __name__ == "__main__":
212
+ sys.exit(main())
@@ -0,0 +1,228 @@
1
+ #!/usr/bin/env python3
2
+ """vbrief_activate.py -- structural lifecycle move pending/ -> active/ (#810).
3
+
4
+ Companion to ``scripts/preflight_implementation.py``. The preflight gate
5
+ asserts a vBRIEF is eligible for implementation; this helper is the
6
+ ONLY supported way to satisfy it. Behavior:
7
+
8
+ - Already-active vBRIEFs (folder == ``active`` AND status ==
9
+ ``running``): print a no-op message and exit 0. Idempotent.
10
+ - Pending vBRIEFs (folder == ``pending``): flip ``plan.status`` from
11
+ ``pending`` / ``approved`` to ``running``, stamp ``vBRIEFInfo.updated``
12
+ to current ISO 8601 UTC, atomically move to ``vbrief/active/``.
13
+ - Any other source folder (``proposed``, ``completed``, ``cancelled``,
14
+ ``active`` with non-running status, foreign folder): reject with an
15
+ actionable message. Exit 1.
16
+ - Malformed JSON, missing ``plan``, unreadable file: reject. Exit 1.
17
+
18
+ The atomic move uses :func:`pathlib.Path.replace` (POSIX rename
19
+ semantics on Linux/macOS, MoveFileEx on Windows) so concurrent reads
20
+ never see a half-written destination.
21
+
22
+ Mirrors the shape of ``scripts/scope_lifecycle.py`` (the existing
23
+ lifecycle tooling) and ``scripts/preflight_implementation.py`` (the
24
+ preflight companion). Pure stdlib.
25
+ """
26
+
27
+ from __future__ import annotations
28
+
29
+ import argparse
30
+ import contextlib
31
+ import json
32
+ import sys
33
+ from datetime import UTC, datetime
34
+ from pathlib import Path
35
+ from typing import Any
36
+
37
+ #: Folders the lifecycle move flows BETWEEN. Source-folder allow-list
38
+ #: defends against silent data loss from accidentally activating a
39
+ #: ``completed/`` or ``cancelled/`` vBRIEF.
40
+ SOURCE_FOLDERS = frozenset({"pending"})
41
+ ACTIVE_FOLDER = "active"
42
+ ELIGIBLE_STATUSES_FOR_FLIP = frozenset({"pending", "approved"})
43
+ TARGET_STATUS = "running"
44
+
45
+
46
+ def _utc_now_iso() -> str:
47
+ """Return an ISO 8601 UTC timestamp with ``Z`` suffix.
48
+
49
+ Matches the existing ``vBRIEFInfo.updated`` format used elsewhere
50
+ in the framework (see ``vbrief/schemas/vbrief-core.schema.json``
51
+ examples and ``scripts/scope_lifecycle.py``).
52
+ """
53
+ return datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
54
+
55
+
56
+ def _load_vbrief(path: Path) -> tuple[dict[str, Any] | None, str | None]:
57
+ """Load and validate the vBRIEF payload.
58
+
59
+ Returns ``(payload, None)`` on success or ``(None, error_msg)`` on
60
+ failure. Never raises -- malformed input is reported via the
61
+ structured error message.
62
+ """
63
+ try:
64
+ raw = path.read_text(encoding="utf-8")
65
+ except (OSError, UnicodeDecodeError) as exc:
66
+ return None, f"Could not read vBRIEF at {path}: {exc}."
67
+ try:
68
+ payload = json.loads(raw)
69
+ except json.JSONDecodeError as exc:
70
+ return None, f"vBRIEF at {path} is not valid JSON: {exc.msg} (line {exc.lineno})."
71
+ if not isinstance(payload, dict):
72
+ return None, f"vBRIEF at {path} top-level value is not a JSON object."
73
+ return payload, None
74
+
75
+
76
+ def activate(vbrief_path: Path) -> tuple[int, str]:
77
+ """Pure activator -- returns ``(exit_code, human_message)``.
78
+
79
+ Performs the lifecycle move + status flip + timestamp stamp
80
+ atomically (load -> validate -> mutate in memory -> write to
81
+ target -> remove source). Idempotent on already-active inputs.
82
+ """
83
+ if not vbrief_path.exists():
84
+ return 1, f"vBRIEF not found at {vbrief_path}."
85
+ if not vbrief_path.is_file():
86
+ return 1, f"vBRIEF path {vbrief_path} is not a regular file."
87
+
88
+ payload, err = _load_vbrief(vbrief_path)
89
+ if err is not None or payload is None:
90
+ return 1, err or "vBRIEF could not be loaded."
91
+
92
+ plan = payload.get("plan")
93
+ if not isinstance(plan, dict):
94
+ return 1, f"vBRIEF at {vbrief_path} lacks a `plan` object -- malformed."
95
+
96
+ status = plan.get("status")
97
+ if not isinstance(status, str) or not status:
98
+ return (
99
+ 1,
100
+ f"vBRIEF at {vbrief_path} lacks `plan.status` -- malformed.",
101
+ )
102
+
103
+ folder = vbrief_path.parent.name
104
+
105
+ # Idempotent no-op: already in the eligible state.
106
+ if folder == ACTIVE_FOLDER and status == TARGET_STATUS:
107
+ return 0, f"No-op: {vbrief_path} already active."
108
+
109
+ # Reject any other ``active/`` state -- e.g. status ``blocked`` or
110
+ # ``completed``. These are NOT activations; the operator should use
111
+ # ``task scope:unblock`` / ``task scope:complete`` etc.
112
+ if folder == ACTIVE_FOLDER:
113
+ return (
114
+ 1,
115
+ f"vBRIEF is already in active/ but plan.status is '{status}', "
116
+ f"not '{TARGET_STATUS}'. Use the appropriate task (e.g. "
117
+ f"`task scope:unblock`) instead of `task vbrief:activate`.",
118
+ )
119
+
120
+ # Reject sources outside the allow-list. ``proposed/`` must promote
121
+ # to ``pending/`` first via ``task scope:promote``; ``completed/``
122
+ # and ``cancelled/`` are terminal.
123
+ if folder not in SOURCE_FOLDERS:
124
+ return (
125
+ 1,
126
+ f"vBRIEF is in {folder}/ -- only pending/ vBRIEFs can be activated. "
127
+ f"Use the lifecycle tasks (`task scope:promote`, etc.) to move it "
128
+ f"into pending/ first.",
129
+ )
130
+
131
+ # Status sanity-check on the source. The schema's enum allows
132
+ # several pre-implementation states; only those documented as
133
+ # eligible for the flip are honored here.
134
+ if status not in ELIGIBLE_STATUSES_FOR_FLIP:
135
+ return (
136
+ 1,
137
+ f"plan.status is '{status}' -- only "
138
+ f"{sorted(ELIGIBLE_STATUSES_FOR_FLIP)} can be flipped to "
139
+ f"'{TARGET_STATUS}'.",
140
+ )
141
+
142
+ # --- Mutate in memory --------------------------------------------------
143
+ plan["status"] = TARGET_STATUS
144
+ info = payload.setdefault("vBRIEFInfo", {})
145
+ if not isinstance(info, dict):
146
+ return (
147
+ 1,
148
+ f"vBRIEF at {vbrief_path} has a non-object `vBRIEFInfo` -- malformed.",
149
+ )
150
+ info["updated"] = _utc_now_iso()
151
+
152
+ # --- Resolve destination ----------------------------------------------
153
+ # Walk up two levels from <root>/vbrief/pending/<file>.json to find
154
+ # the ``vbrief/`` parent, then descend into ``active/``.
155
+ vbrief_dir = vbrief_path.parent.parent
156
+ active_dir = vbrief_dir / ACTIVE_FOLDER
157
+ try:
158
+ active_dir.mkdir(parents=True, exist_ok=True)
159
+ except OSError as exc:
160
+ return 1, f"Could not create {active_dir}: {exc}."
161
+
162
+ dest = active_dir / vbrief_path.name
163
+ if dest.exists():
164
+ return (
165
+ 1,
166
+ f"Refusing to overwrite existing destination {dest}. Resolve the "
167
+ f"collision manually before re-running `task vbrief:activate`.",
168
+ )
169
+
170
+ # --- Atomic write + source removal ------------------------------------
171
+ # Write to a sibling temp file in the destination directory so
172
+ # ``Path.replace`` is a same-filesystem rename (atomic on POSIX +
173
+ # Windows). The source file is removed only after the destination
174
+ # is durable, so a mid-flight crash leaves the original in place.
175
+ tmp = dest.with_suffix(dest.suffix + ".tmp")
176
+ try:
177
+ tmp.write_text(
178
+ json.dumps(payload, indent=2, ensure_ascii=False) + "\n",
179
+ encoding="utf-8",
180
+ )
181
+ tmp.replace(dest)
182
+ except OSError as exc:
183
+ # Best-effort cleanup of the partial temp file.
184
+ with contextlib.suppress(OSError):
185
+ tmp.unlink(missing_ok=True)
186
+ return 1, f"Could not write {dest}: {exc}."
187
+
188
+ try:
189
+ vbrief_path.unlink()
190
+ except OSError as exc:
191
+ return (
192
+ 1,
193
+ f"Wrote {dest} but could not remove source {vbrief_path}: {exc}. "
194
+ f"Manual cleanup required.",
195
+ )
196
+
197
+ return 0, f"Activated {vbrief_path.name}: pending/ -> active/ (status: {TARGET_STATUS})."
198
+
199
+
200
+ def _build_parser() -> argparse.ArgumentParser:
201
+ parser = argparse.ArgumentParser(
202
+ prog="vbrief_activate.py",
203
+ description=(
204
+ "Activate a pending vBRIEF: flip plan.status to 'running', "
205
+ "stamp vBRIEFInfo.updated, atomically move to vbrief/active/. "
206
+ "Idempotent on already-active inputs (#810)."
207
+ ),
208
+ )
209
+ parser.add_argument(
210
+ "vbrief_path",
211
+ help="Path to the candidate vBRIEF JSON file (in vbrief/pending/).",
212
+ )
213
+ return parser
214
+
215
+
216
+ def main(argv: list[str] | None = None) -> int:
217
+ parser = _build_parser()
218
+ args = parser.parse_args(argv)
219
+ code, message = activate(Path(args.vbrief_path))
220
+ if code == 0:
221
+ print(message)
222
+ else:
223
+ print(message, file=sys.stderr)
224
+ return code
225
+
226
+
227
+ if __name__ == "__main__":
228
+ sys.exit(main())