@deftai/directive-content 0.59.0 → 0.60.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 (184) hide show
  1. package/.githooks/pre-push +10 -9
  2. package/Taskfile.yml +48 -58
  3. package/UPGRADING.md +1 -1
  4. package/docs/assets/directive-lifecycle-diagram.png +0 -0
  5. package/docs/directive-lifecycle.md +73 -0
  6. package/docs/getting-started.md +5 -1
  7. package/package.json +3 -3
  8. package/packs/skills/skills-pack-0.1.json +22 -22
  9. package/scm/github.md +20 -2
  10. package/tasks/change.yml +16 -31
  11. package/tasks/ci.yml +8 -0
  12. package/tasks/commit.yml +12 -19
  13. package/tasks/core.yml +10 -0
  14. package/tasks/engine.yml +42 -0
  15. package/tasks/framework.yml +3 -0
  16. package/tasks/install.yml +20 -19
  17. package/tasks/migrate.yml +26 -15
  18. package/tasks/project.yml +16 -0
  19. package/tasks/toolchain.yml +15 -5
  20. package/tasks/vbrief.yml +4 -3
  21. package/tasks/verify.yml +12 -14
  22. package/scripts/_agents_md.py +0 -494
  23. package/scripts/_cache_fetch.py +0 -635
  24. package/scripts/_cache_quota.py +0 -529
  25. package/scripts/_cache_refresh.py +0 -163
  26. package/scripts/_cache_validate.py +0 -209
  27. package/scripts/_content_root.py +0 -42
  28. package/scripts/_doctor_state.py +0 -277
  29. package/scripts/_event_detect.py +0 -305
  30. package/scripts/_events.py +0 -514
  31. package/scripts/_lifecycle_hygiene.py +0 -568
  32. package/scripts/_pathspec.py +0 -91
  33. package/scripts/_policy_show_cli.py +0 -266
  34. package/scripts/_precutover.py +0 -92
  35. package/scripts/_project_context.py +0 -224
  36. package/scripts/_project_definition_io.py +0 -164
  37. package/scripts/_relocate_snapshot.py +0 -209
  38. package/scripts/_relocate_states.py +0 -343
  39. package/scripts/_resolve_preflight_path.py +0 -152
  40. package/scripts/_safe_subprocess.py +0 -167
  41. package/scripts/_session_start_hook.py +0 -205
  42. package/scripts/_sor_gate_diff.py +0 -365
  43. package/scripts/_stdio_utf8.py +0 -59
  44. package/scripts/_triage_bootstrap_gitignore.py +0 -904
  45. package/scripts/_triage_classify_cli.py +0 -122
  46. package/scripts/_triage_queue_cli.py +0 -625
  47. package/scripts/_triage_scope_cli.py +0 -343
  48. package/scripts/_triage_scope_drift_cli.py +0 -121
  49. package/scripts/_triage_scope_ignores.py +0 -286
  50. package/scripts/_triage_scope_milestone.py +0 -432
  51. package/scripts/_triage_scope_mutations.py +0 -337
  52. package/scripts/_triage_scope_renderers.py +0 -207
  53. package/scripts/_triage_smoketest_stages.py +0 -674
  54. package/scripts/_triage_subscribe_cli.py +0 -140
  55. package/scripts/_triage_welcome_cli.py +0 -421
  56. package/scripts/_vbrief_build.py +0 -239
  57. package/scripts/_vbrief_fidelity.py +0 -479
  58. package/scripts/_vbrief_legacy.py +0 -589
  59. package/scripts/_vbrief_reconciliation.py +0 -883
  60. package/scripts/_vbrief_routing.py +0 -277
  61. package/scripts/_vbrief_safety.py +0 -778
  62. package/scripts/_vbrief_sources.py +0 -312
  63. package/scripts/_vbrief_speckit.py +0 -262
  64. package/scripts/_vbrief_story_quality.py +0 -353
  65. package/scripts/_vbrief_validation.py +0 -299
  66. package/scripts/build_dist.py +0 -412
  67. package/scripts/cache.py +0 -1078
  68. package/scripts/cache_scanner.py +0 -745
  69. package/scripts/candidates_log.py +0 -432
  70. package/scripts/capacity_backfill.py +0 -680
  71. package/scripts/capacity_show.py +0 -653
  72. package/scripts/ci_local.py +0 -689
  73. package/scripts/code_structure_validate.py +0 -765
  74. package/scripts/codebase_default_extractor.py +0 -495
  75. package/scripts/codebase_map.py +0 -304
  76. package/scripts/codebase_map_fresh.py +0 -104
  77. package/scripts/codebase_projection_registry.py +0 -94
  78. package/scripts/codebase_provider.py +0 -582
  79. package/scripts/doctor.py +0 -2552
  80. package/scripts/framework_commands.py +0 -505
  81. package/scripts/gh_rest.py +0 -882
  82. package/scripts/github_auth_modes.py +0 -437
  83. package/scripts/github_body.py +0 -292
  84. package/scripts/ip_risk.py +0 -531
  85. package/scripts/issue_emit.py +0 -670
  86. package/scripts/issue_ingest.py +0 -1064
  87. package/scripts/migrate_preflight.py +0 -418
  88. package/scripts/migrate_vbrief.py +0 -2677
  89. package/scripts/monitor_pr.py +0 -401
  90. package/scripts/pack_migrate_lessons.py +0 -336
  91. package/scripts/pack_migrate_patterns.py +0 -254
  92. package/scripts/pack_migrate_rules.py +0 -350
  93. package/scripts/pack_migrate_skills.py +0 -423
  94. package/scripts/pack_migrate_strategies.py +0 -311
  95. package/scripts/pack_migrate_swarm_spec.py +0 -250
  96. package/scripts/pack_render.py +0 -434
  97. package/scripts/packs_slice.py +0 -712
  98. package/scripts/platform_capabilities.py +0 -336
  99. package/scripts/policy.py +0 -2826
  100. package/scripts/policy_set.py +0 -324
  101. package/scripts/pr_check_closing_keywords.py +0 -524
  102. package/scripts/pr_check_protected_issues.py +0 -267
  103. package/scripts/pr_merge_readiness.py +0 -1004
  104. package/scripts/pr_wait_mergeable.py +0 -669
  105. package/scripts/prd_render.py +0 -159
  106. package/scripts/preflight_architecture_sor.py +0 -974
  107. package/scripts/preflight_branch.py +0 -289
  108. package/scripts/preflight_cache.py +0 -974
  109. package/scripts/preflight_gh.py +0 -721
  110. package/scripts/preflight_implementation.py +0 -272
  111. package/scripts/preflight_story_start.py +0 -838
  112. package/scripts/preflight_wip_cap.py +0 -149
  113. package/scripts/probe_session.py +0 -545
  114. package/scripts/project_render.py +0 -293
  115. package/scripts/quarantine_ext.py +0 -237
  116. package/scripts/reconcile_issues.py +0 -1442
  117. package/scripts/refresh-path.ps1 +0 -107
  118. package/scripts/release.py +0 -2030
  119. package/scripts/release_e2e.py +0 -1011
  120. package/scripts/release_publish.py +0 -486
  121. package/scripts/release_rollback.py +0 -980
  122. package/scripts/relocate.py +0 -1034
  123. package/scripts/resolve_changelog_unreleased.py +0 -667
  124. package/scripts/resolve_version.py +0 -490
  125. package/scripts/resume_conditions.py +0 -706
  126. package/scripts/ritual_sentinel.py +0 -609
  127. package/scripts/roadmap_render.py +0 -635
  128. package/scripts/rule_ownership_lint.py +0 -325
  129. package/scripts/scm.py +0 -591
  130. package/scripts/scope_audit_log.py +0 -387
  131. package/scripts/scope_decompose.py +0 -654
  132. package/scripts/scope_demote.py +0 -509
  133. package/scripts/scope_lifecycle.py +0 -1126
  134. package/scripts/scope_undo.py +0 -772
  135. package/scripts/session_start.py +0 -406
  136. package/scripts/setup_ghx.py +0 -339
  137. package/scripts/setup_windows.ps1 +0 -220
  138. package/scripts/slice_audit.py +0 -585
  139. package/scripts/slice_record.py +0 -530
  140. package/scripts/slice_record_existing.py +0 -692
  141. package/scripts/slug_normalize.py +0 -178
  142. package/scripts/spec_render.py +0 -477
  143. package/scripts/spec_validate.py +0 -238
  144. package/scripts/subagent_monitor.py +0 -658
  145. package/scripts/swarm_complete_cohort.py +0 -644
  146. package/scripts/swarm_launch.py +0 -1206
  147. package/scripts/swarm_readiness.py +0 -554
  148. package/scripts/swarm_verify_review_clean.py +0 -438
  149. package/scripts/swarm_worktrees.py +0 -497
  150. package/scripts/toolchain-check.py +0 -52
  151. package/scripts/triage_actions.py +0 -871
  152. package/scripts/triage_bootstrap.py +0 -1153
  153. package/scripts/triage_bulk.py +0 -630
  154. package/scripts/triage_classify.py +0 -932
  155. package/scripts/triage_help.py +0 -1685
  156. package/scripts/triage_queue.py +0 -1944
  157. package/scripts/triage_reconcile.py +0 -581
  158. package/scripts/triage_refresh.py +0 -643
  159. package/scripts/triage_scope.py +0 -999
  160. package/scripts/triage_scope_drift.py +0 -575
  161. package/scripts/triage_smoketest.py +0 -396
  162. package/scripts/triage_subscribe.py +0 -399
  163. package/scripts/triage_summary.py +0 -1011
  164. package/scripts/triage_welcome.py +0 -1178
  165. package/scripts/ts_check_lane.py +0 -86
  166. package/scripts/validate-links.py +0 -64
  167. package/scripts/validate_strategy_output.py +0 -212
  168. package/scripts/vbrief_activate.py +0 -228
  169. package/scripts/vbrief_migrate_conformance.py +0 -368
  170. package/scripts/vbrief_reconcile_graph.py +0 -306
  171. package/scripts/vbrief_reconcile_labels.py +0 -460
  172. package/scripts/vbrief_reconcile_umbrellas.py +0 -741
  173. package/scripts/vbrief_validate.py +0 -1144
  174. package/scripts/verify-stubs.py +0 -61
  175. package/scripts/verify_capacity.py +0 -160
  176. package/scripts/verify_encoding.py +0 -699
  177. package/scripts/verify_hooks_installed.py +0 -206
  178. package/scripts/verify_investigation.py +0 -360
  179. package/scripts/verify_judgment_gates.py +0 -827
  180. package/scripts/verify_no_task_runtime.py +0 -171
  181. package/scripts/verify_scm_boundary.py +0 -509
  182. package/scripts/verify_session_ritual.py +0 -389
  183. package/scripts/verify_tools.py +0 -426
  184. package/scripts/verify_vbrief_conformance.py +0 -478
@@ -1,680 +0,0 @@
1
- #!/usr/bin/env python3
2
- """capacity_backfill.py -- one-time capacity-bucket classifier for completed vBRIEFs (#1606).
3
-
4
- The capacity engine (``scripts/capacity_show.py``, #1419 Slice 4) counts a
5
- completed vBRIEF toward a bucket only when it carries an explicit
6
- ``plan.metadata.capacityBucket`` (and a ``plan.metadata.completedAt`` inside
7
- the trailing window). ``task scope:complete`` stamps both fields going
8
- FORWARD -- but only from ``defaultBucket`` -- so a pre-adoption tree
9
- (directive itself included) has completed work that is *classification
10
- cold-start*: the history exists, but every completion is unclassified, so the
11
- ``minSampleSize`` guard pins capacity in advisory mode forever.
12
-
13
- This module is the deferred ``task capacity:backfill`` migration the #1419 RFC
14
- ("Brownfield Backfill") specified: a one-time, dry-run-default, git-reversible
15
- pass that derives the two missing facts onto ``completed/`` vBRIEFs:
16
-
17
- * ``plan.metadata.completedAt`` -- the git landing time of the file (the most
18
- recent commit that touched it), when not already present. Deterministic,
19
- zero human input.
20
- * ``plan.metadata.capacityBucket`` (+ ``plan.metadata.capacityBucketSource``)
21
- -- inferred from the vBRIEF's origin-issue labels (its ``x-vbrief/github-issue``
22
- reference) matched against the declared ``capacityAllocation.buckets[].match.labels``
23
- predicates. A label match yields ``source="match"`` (high confidence); no
24
- match (or no cached issue / no issue reference) falls to ``defaultBucket``
25
- with ``source="default"`` and is surfaced in the low-confidence batch for
26
- human review.
27
-
28
- Guarantees:
29
-
30
- * **Dry-run by default.** Writes only with ``--apply``.
31
- * **Idempotent.** An explicit existing ``capacityBucket`` / ``completedAt`` is
32
- preserved; a re-run is a no-op for already-stamped files.
33
- * **Never mutates ``cost``** -- historical cost actuals are not backfillable
34
- (no telemetry exists for past runs); ``cost`` accrues forward only.
35
- * **Offline.** Reads cached issue labels from ``.deft-cache/github-issue/``;
36
- no ``gh`` / network calls. Git is the only subprocess (landing time).
37
-
38
- Exit codes (three-state, mirrors ``scripts/triage_reconcile.py``):
39
-
40
- * ``0`` -- backfill completed (or was a no-op on a re-run / dry-run).
41
- * ``1`` -- a runtime step failed (e.g. a write raised).
42
- * ``2`` -- config error: ``--project-root`` missing, or
43
- ``plan.policy.capacityAllocation`` is not configured (nothing to classify
44
- against).
45
-
46
- Refs: #1606 (this tool), #1419 (parent RFC -- Brownfield Backfill), #1511
47
- (flip gates advisory -> enforce; backfill is its prerequisite).
48
- """
49
-
50
- from __future__ import annotations
51
-
52
- import argparse
53
- import json
54
- import os
55
- import sys
56
- from dataclasses import dataclass, field
57
- from datetime import UTC, datetime
58
- from pathlib import Path
59
- from typing import Any
60
-
61
- # Make sibling ``scripts`` modules importable when invoked as
62
- # ``python scripts/capacity_backfill.py`` from the project root.
63
- sys.path.insert(0, str(Path(__file__).resolve().parent))
64
-
65
- from _safe_subprocess import run_text # noqa: E402
66
- from _stdio_utf8 import reconfigure_stdio # noqa: E402
67
- from policy import ( # noqa: E402
68
- load_project_definition,
69
- resolve_capacity_allocation,
70
- )
71
-
72
- reconfigure_stdio()
73
-
74
- #: Lifecycle folder the backfill operates on (the backward / completed view).
75
- COMPLETED_FOLDER: str = "completed"
76
-
77
- #: Default location of the github-issue label cache (offline label source).
78
- CACHE_RELPATH: tuple[str, ...] = (".deft-cache", "github-issue")
79
-
80
- #: ``capacityBucketSource`` values this tool records.
81
- SOURCE_MATCH: str = "match" # a bucket match.labels predicate matched
82
- SOURCE_DEFAULT: str = "default" # no match -> defaultBucket (low confidence)
83
-
84
-
85
- # ---------------------------------------------------------------------------
86
- # Data model
87
- # ---------------------------------------------------------------------------
88
-
89
-
90
- @dataclass(frozen=True)
91
- class BucketMatcher:
92
- """A bucket id paired with its ``match.labels.any-of`` label set."""
93
-
94
- bucket_id: str
95
- labels: frozenset[str]
96
-
97
-
98
- @dataclass(frozen=True)
99
- class BackfillItem:
100
- """One completed vBRIEF's resolved backfill facts."""
101
-
102
- rel_path: str
103
- issue_number: int | None
104
- bucket: str
105
- source: str # SOURCE_MATCH | SOURCE_DEFAULT
106
- set_bucket: bool # capacityBucket was absent and will be / was stamped
107
- set_completed_at: bool # completedAt was absent and will be / was stamped
108
-
109
-
110
- @dataclass
111
- class BackfillResult:
112
- """Aggregate result returned by :func:`backfill`."""
113
-
114
- project_root: Path
115
- dry_run: bool
116
- scanned: int = 0
117
- stamped_bucket: int = 0
118
- stamped_completed_at: int = 0
119
- already_classified: int = 0
120
- matched: int = 0
121
- defaulted: int = 0
122
- fetched: int = 0
123
- skipped_out_of_window: int = 0
124
- skipped_unreadable: int = 0
125
- window_only: bool = False
126
- window_days: int = 0
127
- items: list[BackfillItem] = field(default_factory=list)
128
- low_confidence: list[BackfillItem] = field(default_factory=list)
129
- error: str | None = None
130
- exit_code: int = 0
131
-
132
- def summary(self) -> str:
133
- """Render the human-readable recap the operator sees."""
134
- verb = "would stamp" if self.dry_run else "stamped"
135
- mark = "✓" if self.exit_code == 0 else "✗"
136
- lines = ["", "Capacity backfill recap:"]
137
- lines.append(
138
- f" {mark} scanned {self.scanned} completed vBRIEF(s); "
139
- f"{verb} capacityBucket on {self.stamped_bucket} "
140
- f"(matched {self.matched}, defaulted {self.defaulted}); "
141
- f"{verb} completedAt on {self.stamped_completed_at}; "
142
- f"{self.already_classified} already classified"
143
- )
144
- if self.fetched:
145
- lines.append(
146
- f" fetched labels for {self.fetched} uncached issue(s) via REST"
147
- )
148
- if self.window_only:
149
- lines.append(
150
- f" window-only: skipped {self.skipped_out_of_window} "
151
- f"completion(s) outside the trailing {self.window_days}d window"
152
- )
153
- if self.skipped_unreadable:
154
- lines.append(
155
- f" skipped {self.skipped_unreadable} unreadable/malformed "
156
- "completed vBRIEF file(s) (not counted in scanned)"
157
- )
158
- if self.error:
159
- lines.append(f" error: {self.error}")
160
- if self.low_confidence:
161
- lines.append("")
162
- lines.append(
163
- f" Low-confidence batch ({len(self.low_confidence)}) -- "
164
- "no label match, fell to defaultBucket; review + re-bucket as needed:"
165
- )
166
- for item in self.low_confidence:
167
- issue = f"#{item.issue_number}" if item.issue_number else "(no issue ref)"
168
- lines.append(f" {issue} -> {item.bucket} [{item.rel_path}]")
169
- if self.dry_run and self.exit_code == 0:
170
- lines.append("")
171
- lines.append(" Dry-run -- re-run with --apply to write these changes.")
172
- return "\n".join(lines)
173
-
174
-
175
- # ---------------------------------------------------------------------------
176
- # Bucket-matcher resolution (reads the RAW match.labels the policy resolver drops)
177
- # ---------------------------------------------------------------------------
178
-
179
-
180
- def load_bucket_matchers(project_root: Path) -> tuple[list[BucketMatcher], str]:
181
- """Return ``(ordered matchers, default_bucket)`` from PROJECT-DEFINITION.
182
-
183
- ``resolve_capacity_allocation`` intentionally exposes only ``id`` + ``target``
184
- per bucket, so the raw ``match.labels.any-of`` predicate is read directly
185
- here (mirrors ``_lifecycle_hygiene.resolve_epic_thresholds`` reading the raw
186
- block for ``epicStrandedDays``). Matchers preserve declaration order so the
187
- first bucket whose label set intersects wins.
188
- """
189
- data, _err = load_project_definition(project_root)
190
- matchers: list[BucketMatcher] = []
191
- if not isinstance(data, dict):
192
- return matchers, ""
193
- plan = data.get("plan")
194
- policy = plan.get("policy") if isinstance(plan, dict) else None
195
- cap = policy.get("capacityAllocation") if isinstance(policy, dict) else None
196
- if not isinstance(cap, dict):
197
- return matchers, ""
198
- buckets = cap.get("buckets")
199
- if isinstance(buckets, list):
200
- for bucket in buckets:
201
- if not isinstance(bucket, dict):
202
- continue
203
- bucket_id = bucket.get("id")
204
- if not isinstance(bucket_id, str) or not bucket_id.strip():
205
- continue
206
- labels = _match_labels(bucket.get("match"))
207
- matchers.append(
208
- BucketMatcher(bucket_id=bucket_id.strip(), labels=frozenset(labels))
209
- )
210
- default_bucket = cap.get("defaultBucket")
211
- return matchers, default_bucket if isinstance(default_bucket, str) else ""
212
-
213
-
214
- def _match_labels(match: Any) -> set[str]:
215
- """Extract the ``match.labels.any-of`` string set from a bucket block."""
216
- if not isinstance(match, dict):
217
- return set()
218
- labels = match.get("labels")
219
- if not isinstance(labels, dict):
220
- return set()
221
- any_of = labels.get("any-of")
222
- if not isinstance(any_of, list):
223
- return set()
224
- return {x for x in any_of if isinstance(x, str) and x}
225
-
226
-
227
- def classify_bucket(
228
- issue_labels: set[str], matchers: list[BucketMatcher], default_bucket: str
229
- ) -> tuple[str, str]:
230
- """Return ``(bucket_id, source)`` for an issue's label set.
231
-
232
- First matcher (declaration order) whose ``labels`` intersect *issue_labels*
233
- wins with ``source="match"``. No intersection -> ``(default_bucket, "default")``.
234
- """
235
- for matcher in matchers:
236
- if matcher.labels & issue_labels:
237
- return matcher.bucket_id, SOURCE_MATCH
238
- return default_bucket, SOURCE_DEFAULT
239
-
240
-
241
- # ---------------------------------------------------------------------------
242
- # vBRIEF + cache + git helpers
243
- # ---------------------------------------------------------------------------
244
-
245
-
246
- def _parse_iso(value: Any) -> datetime | None:
247
- """Parse an ISO-8601 timestamp (``...Z`` or offset form) to aware UTC."""
248
- if not isinstance(value, str) or not value.strip():
249
- return None
250
- text = value.strip()
251
- if text.endswith("Z"):
252
- text = text[:-1] + "+00:00"
253
- try:
254
- parsed = datetime.fromisoformat(text)
255
- except ValueError:
256
- return None
257
- if parsed.tzinfo is None:
258
- parsed = parsed.replace(tzinfo=UTC)
259
- return parsed.astimezone(UTC)
260
-
261
-
262
- def _to_iso_z(dt: datetime) -> str:
263
- """Render an aware datetime as the canonical ``...Z`` form used on disk."""
264
- return dt.astimezone(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
265
-
266
-
267
- def extract_issue_ref(plan: dict[str, Any]) -> tuple[str | None, int | None]:
268
- """Pull ``(repo, issue_number)`` from a vBRIEF plan's ``x-vbrief/github-issue`` ref."""
269
- refs = plan.get("references")
270
- if not isinstance(refs, list):
271
- return None, None
272
- for ref in refs:
273
- if not isinstance(ref, dict) or ref.get("type") != "x-vbrief/github-issue":
274
- continue
275
- uri = ref.get("uri")
276
- if not isinstance(uri, str):
277
- continue
278
- cleaned = uri.strip().rstrip("/")
279
- parts = [p for p in cleaned.split("://", 1)[-1].split("/") if p]
280
- if len(parts) >= 4 and parts[-2] == "issues" and parts[-1].isdigit():
281
- return f"{parts[-4]}/{parts[-3]}", int(parts[-1])
282
- return None, None
283
-
284
-
285
- def cached_issue_labels(
286
- project_root: Path, repo: str, issue_number: int, *, cache_dir: Path | None = None
287
- ) -> set[str] | None:
288
- """Return the cached label set for ``repo#issue_number`` (offline), or None.
289
-
290
- None means the issue is not in the cache (a label match cannot be attempted);
291
- an empty set means the issue is cached but carries no labels.
292
- """
293
- base = cache_dir or project_root.joinpath(*CACHE_RELPATH)
294
- raw_path = base / repo / str(issue_number) / "raw.json"
295
- if not raw_path.is_file():
296
- return None
297
- try:
298
- data = json.loads(raw_path.read_text(encoding="utf-8"))
299
- except (OSError, ValueError):
300
- return None
301
- labels = data.get("labels") if isinstance(data, dict) else None
302
- if not isinstance(labels, list):
303
- return set()
304
- out: set[str] = set()
305
- for label in labels:
306
- if isinstance(label, str) and label:
307
- out.add(label)
308
- elif isinstance(label, dict):
309
- name = label.get("name")
310
- if isinstance(name, str) and name:
311
- out.add(name)
312
- return out
313
-
314
-
315
- def git_landing_time(repo_rel_path: str, project_root: Path) -> str | None:
316
- """Return the most recent commit timestamp touching *repo_rel_path*, as ``...Z``.
317
-
318
- *repo_rel_path* MUST be relative to the git repository root (e.g.
319
- ``vbrief/completed/<name>``), not the lifecycle folder. Uses
320
- ``git log -1 --format=%cI -- <path>`` (committer date, ISO-8601 strict) as a
321
- deterministic proxy for when the vBRIEF landed in ``completed/``. Returns None
322
- when git is unavailable or the file is untracked.
323
- """
324
- try:
325
- result = run_text(
326
- ["git", "log", "-1", "--format=%cI", "--", repo_rel_path],
327
- cwd=str(project_root),
328
- )
329
- except (OSError, ValueError):
330
- return None
331
- if result.returncode != 0:
332
- return None
333
- parsed = _parse_iso(result.stdout.strip())
334
- return _to_iso_z(parsed) if parsed is not None else None
335
-
336
-
337
- def fetch_issue_labels(repo: str, issue_number: int) -> set[str] | None:
338
- """Fetch an issue's label set via the REST shim (closed-issue-safe), or None.
339
-
340
- Routes through ``scripts/gh_rest.rest_issue_view`` (REST, never GraphQL --
341
- respects the #954 bucket-hygiene rule and the #1145 scm-boundary). Imported
342
- lazily so the offline default path has no ``gh`` dependency and the unit
343
- tests need no network. Any failure (no gh, network error, malformed
344
- response) returns None so the caller falls back to the default bucket.
345
- """
346
- try:
347
- from gh_rest import rest_issue_view # noqa: PLC0415 -- lazy, opt-in only
348
-
349
- issue = rest_issue_view(repo, issue_number)
350
- except Exception: # noqa: BLE001 -- any fetch failure degrades to default
351
- return None
352
- labels = issue.get("labels") if isinstance(issue, dict) else None
353
- if not isinstance(labels, list):
354
- return set()
355
- out: set[str] = set()
356
- for label in labels:
357
- if isinstance(label, str) and label:
358
- out.add(label)
359
- elif isinstance(label, dict):
360
- name = label.get("name")
361
- if isinstance(name, str) and name:
362
- out.add(name)
363
- return out
364
-
365
-
366
- # ---------------------------------------------------------------------------
367
- # Core backfill logic
368
- # ---------------------------------------------------------------------------
369
-
370
-
371
- def backfill(
372
- project_root: Path,
373
- *,
374
- cache_dir: Path | None = None,
375
- dry_run: bool = True,
376
- window_only: bool = False,
377
- fetch: bool = False,
378
- now: datetime | None = None,
379
- ) -> BackfillResult:
380
- """Backfill ``capacityBucket`` / ``completedAt`` on completed vBRIEFs.
381
-
382
- Idempotent: explicit existing values are preserved. ``cost`` is never
383
- touched. When *window_only* is set, completions whose effective
384
- ``completedAt`` falls outside the trailing ``capacityAllocation.window``
385
- are skipped (the activation-critical subset is exactly the in-window one).
386
- When *fetch* is set, origin-issue labels missing from the local cache are
387
- pulled via the REST shim (the one-time online opt-in for brownfield history
388
- whose closed issues are not in the open-issue-scoped triage cache).
389
- """
390
- now_dt = now or datetime.now(UTC)
391
- allocation = resolve_capacity_allocation(project_root)
392
- result = BackfillResult(
393
- project_root=project_root,
394
- dry_run=dry_run,
395
- window_only=window_only,
396
- window_days=allocation.window_days,
397
- )
398
- if not allocation.configured:
399
- result.error = (
400
- "plan.policy.capacityAllocation is not configured -- configure "
401
- "buckets before backfilling (see #1419 / task capacity:show)"
402
- )
403
- result.exit_code = 2
404
- return result
405
-
406
- matchers, default_bucket = load_bucket_matchers(project_root)
407
- if not default_bucket:
408
- # resolve_capacity_allocation validated the block, but a missing
409
- # defaultBucket means unmatched work has nowhere to go -- fail loud.
410
- result.error = (
411
- "capacityAllocation.defaultBucket is required for backfill "
412
- "(unmatched completions must have a fallback bucket)"
413
- )
414
- result.exit_code = 2
415
- return result
416
-
417
- completed_dir = project_root / "vbrief" / COMPLETED_FOLDER
418
- if not completed_dir.is_dir():
419
- return result
420
-
421
- for path in sorted(completed_dir.glob("*.vbrief.json")):
422
- try:
423
- data = json.loads(path.read_text(encoding="utf-8"))
424
- except (OSError, ValueError):
425
- # Corrupted / non-UTF-8 / malformed-JSON files are skipped, but the
426
- # skip is now counted so the summary's ``scanned`` figure is not
427
- # silently lower than the actual file count (#1606 review).
428
- result.skipped_unreadable += 1
429
- continue
430
- plan = data.get("plan") if isinstance(data, dict) else None
431
- if not isinstance(plan, dict):
432
- continue
433
- result.scanned += 1
434
- rel_path = f"{COMPLETED_FOLDER}/{path.name}"
435
- git_rel_path = f"vbrief/{rel_path}"
436
-
437
- metadata = plan.get("metadata")
438
- if not isinstance(metadata, dict):
439
- metadata = {}
440
-
441
- existing_bucket = metadata.get("capacityBucket")
442
- has_bucket = isinstance(existing_bucket, str) and bool(existing_bucket.strip())
443
- existing_completed_at = metadata.get("completedAt")
444
- has_completed_at = (
445
- isinstance(existing_completed_at, str) and bool(existing_completed_at.strip())
446
- )
447
-
448
- # Resolve the effective completedAt (existing, else git landing time)
449
- # so the window filter and the stamp share one value.
450
- effective_completed_at = existing_completed_at if has_completed_at else None
451
- git_completed_at: str | None = None
452
- if not has_completed_at:
453
- git_completed_at = git_landing_time(git_rel_path, project_root)
454
- effective_completed_at = git_completed_at
455
-
456
- if window_only and not _in_window(effective_completed_at, allocation.window_days, now_dt):
457
- result.skipped_out_of_window += 1
458
- continue
459
-
460
- repo, issue_number = extract_issue_ref(plan)
461
- if has_bucket:
462
- result.already_classified += 1
463
- bucket = existing_bucket.strip()
464
- source = "preserved"
465
- else:
466
- labels: set[str] | None = None
467
- if repo and issue_number is not None:
468
- labels = cached_issue_labels(
469
- project_root, repo, issue_number, cache_dir=cache_dir
470
- )
471
- if labels is None and fetch:
472
- labels = fetch_issue_labels(repo, issue_number)
473
- if labels is not None:
474
- result.fetched += 1
475
- bucket, source = classify_bucket(labels or set(), matchers, default_bucket)
476
-
477
- set_bucket = not has_bucket
478
- set_completed_at = not has_completed_at and git_completed_at is not None
479
-
480
- item = BackfillItem(
481
- rel_path=rel_path,
482
- issue_number=issue_number,
483
- bucket=bucket,
484
- source=source,
485
- set_bucket=set_bucket,
486
- set_completed_at=set_completed_at,
487
- )
488
- result.items.append(item)
489
-
490
- # Write FIRST (apply mode), then tally -- so an OSError mid-run leaves
491
- # the summary counting only what actually reached disk, not the failed
492
- # item (#1606 review). Dry-run performs no write and falls straight to
493
- # the tally so it reports what it WOULD stamp.
494
- if not dry_run and (set_bucket or set_completed_at):
495
- try:
496
- _write_metadata(
497
- path,
498
- data,
499
- plan,
500
- metadata,
501
- bucket=bucket if set_bucket else None,
502
- source=source if set_bucket else None,
503
- completed_at=git_completed_at if set_completed_at else None,
504
- )
505
- except OSError as exc:
506
- result.error = f"{type(exc).__name__}: {exc} ({rel_path})"
507
- result.exit_code = 1
508
- return result
509
-
510
- if set_bucket:
511
- result.stamped_bucket += 1
512
- if source == SOURCE_MATCH:
513
- result.matched += 1
514
- else:
515
- result.defaulted += 1
516
- result.low_confidence.append(item)
517
- if set_completed_at:
518
- result.stamped_completed_at += 1
519
-
520
- return result
521
-
522
-
523
- def _in_window(completed_at: str | None, window_days: int, now: datetime) -> bool:
524
- """True when *completed_at* parses and falls within ``[0, window_days]`` of now."""
525
- parsed = _parse_iso(completed_at)
526
- if parsed is None:
527
- return False
528
- age_days = (now - parsed).total_seconds() / 86400.0
529
- return 0 <= age_days <= window_days
530
-
531
-
532
- def _write_metadata(
533
- path: Path,
534
- data: dict[str, Any],
535
- plan: dict[str, Any],
536
- metadata: dict[str, Any],
537
- *,
538
- bucket: str | None,
539
- source: str | None,
540
- completed_at: str | None,
541
- ) -> None:
542
- """Stamp the resolved fields onto *plan.metadata* and write the file.
543
-
544
- ``cost`` is never read or written here. Mirrors the JSON write style of
545
- ``scripts/scope_lifecycle.py`` (2-space indent, ensure_ascii=False, trailing
546
- newline) so the diff stays minimal and encoding-clean.
547
- """
548
- if not isinstance(plan.get("metadata"), dict):
549
- plan["metadata"] = metadata
550
- if completed_at is not None:
551
- metadata["completedAt"] = completed_at
552
- if bucket is not None:
553
- metadata["capacityBucket"] = bucket
554
- if source is not None:
555
- metadata["capacityBucketSource"] = source
556
- path.write_text(
557
- json.dumps(data, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
558
- )
559
-
560
-
561
- # ---------------------------------------------------------------------------
562
- # CLI
563
- # ---------------------------------------------------------------------------
564
-
565
-
566
- def _emit_json(result: BackfillResult) -> str:
567
- payload = {
568
- "project_root": str(result.project_root),
569
- "dry_run": result.dry_run,
570
- "scanned": result.scanned,
571
- "stamped_bucket": result.stamped_bucket,
572
- "stamped_completed_at": result.stamped_completed_at,
573
- "already_classified": result.already_classified,
574
- "matched": result.matched,
575
- "defaulted": result.defaulted,
576
- "fetched": result.fetched,
577
- "skipped_out_of_window": result.skipped_out_of_window,
578
- "skipped_unreadable": result.skipped_unreadable,
579
- "window_only": result.window_only,
580
- "window_days": result.window_days,
581
- "exit_code": result.exit_code,
582
- "error": result.error,
583
- "low_confidence": [
584
- {"issue_number": it.issue_number, "bucket": it.bucket, "rel_path": it.rel_path}
585
- for it in result.low_confidence
586
- ],
587
- }
588
- return json.dumps(payload, sort_keys=True)
589
-
590
-
591
- def _build_parser() -> argparse.ArgumentParser:
592
- parser = argparse.ArgumentParser(
593
- prog="capacity_backfill.py",
594
- description=(
595
- "One-time capacity-bucket classifier for completed vBRIEFs (#1606). "
596
- "Stamps plan.metadata.capacityBucket (inferred from origin-issue "
597
- "labels via the configured bucket match rules) and completedAt "
598
- "(git landing time) onto completed/ vBRIEFs that lack them. "
599
- "Dry-run by default; idempotent; never touches cost."
600
- ),
601
- )
602
- parser.add_argument(
603
- "--project-root",
604
- default=os.environ.get("DEFT_PROJECT_ROOT", "."),
605
- help="Path to the project root (default: $DEFT_PROJECT_ROOT or cwd).",
606
- )
607
- parser.add_argument(
608
- "--apply",
609
- action="store_true",
610
- help="Write the changes. Without this flag the tool is a dry-run.",
611
- )
612
- parser.add_argument(
613
- "--window-only",
614
- action="store_true",
615
- dest="window_only",
616
- help=(
617
- "Only backfill completions whose completedAt falls within the "
618
- "trailing capacityAllocation.window -- the activation-critical "
619
- "subset capacity:show actually counts."
620
- ),
621
- )
622
- parser.add_argument(
623
- "--fetch",
624
- action="store_true",
625
- help=(
626
- "Pull origin-issue labels via the REST shim for issues missing from "
627
- "the local cache (a one-time online opt-in for brownfield history; "
628
- "closed issues are not in the open-issue-scoped triage cache). "
629
- "Without this flag the tool is fully offline."
630
- ),
631
- )
632
- parser.add_argument(
633
- "--cache-dir",
634
- default=None,
635
- help=(
636
- "Override the github-issue label cache directory "
637
- "(default: <project-root>/.deft-cache/github-issue)."
638
- ),
639
- )
640
- parser.add_argument(
641
- "--json",
642
- action="store_true",
643
- dest="emit_json",
644
- help="Emit a structured JSON payload instead of the human recap.",
645
- )
646
- return parser
647
-
648
-
649
- def main(argv: list[str] | None = None) -> int:
650
- parser = _build_parser()
651
- args = parser.parse_args(argv)
652
-
653
- project_root = Path(args.project_root).resolve()
654
- if not project_root.exists() or not project_root.is_dir():
655
- print(
656
- f"❌ capacity:backfill: --project-root {project_root} does not exist "
657
- "or is not a directory.",
658
- file=sys.stderr,
659
- )
660
- return 2
661
-
662
- cache_dir = Path(args.cache_dir).resolve() if args.cache_dir else None
663
- result = backfill(
664
- project_root,
665
- cache_dir=cache_dir,
666
- dry_run=not args.apply,
667
- window_only=args.window_only,
668
- fetch=args.fetch,
669
- )
670
-
671
- if args.emit_json:
672
- print(_emit_json(result))
673
- else:
674
- print(result.summary(), file=sys.stderr if result.exit_code else sys.stdout)
675
-
676
- return result.exit_code
677
-
678
-
679
- if __name__ == "__main__":
680
- raise SystemExit(main())