@deftai/directive-content 0.55.2 → 0.56.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/.githooks/pre-commit +143 -0
  2. package/.githooks/pre-push +121 -0
  3. package/QUICK-START.md +2 -2
  4. package/Taskfile.yml +934 -0
  5. package/UPGRADING.md +47 -1
  6. package/events/README.md +3 -3
  7. package/package.json +5 -4
  8. package/scripts/_agents_md.py +494 -0
  9. package/scripts/_cache_fetch.py +635 -0
  10. package/scripts/_cache_quota.py +529 -0
  11. package/scripts/_cache_refresh.py +163 -0
  12. package/scripts/_cache_validate.py +209 -0
  13. package/scripts/_content_root.py +42 -0
  14. package/scripts/_doctor_state.py +277 -0
  15. package/scripts/_event_detect.py +305 -0
  16. package/scripts/_events.py +514 -0
  17. package/scripts/_lifecycle_hygiene.py +568 -0
  18. package/scripts/_pathspec.py +91 -0
  19. package/scripts/_policy_show_cli.py +266 -0
  20. package/scripts/_precutover.py +92 -0
  21. package/scripts/_project_context.py +224 -0
  22. package/scripts/_project_definition_io.py +164 -0
  23. package/scripts/_relocate_snapshot.py +209 -0
  24. package/scripts/_relocate_states.py +343 -0
  25. package/scripts/_resolve_preflight_path.py +152 -0
  26. package/scripts/_safe_subprocess.py +167 -0
  27. package/scripts/_session_start_hook.py +205 -0
  28. package/scripts/_sor_gate_diff.py +365 -0
  29. package/scripts/_stdio_utf8.py +59 -0
  30. package/scripts/_triage_bootstrap_gitignore.py +904 -0
  31. package/scripts/_triage_classify_cli.py +122 -0
  32. package/scripts/_triage_queue_cli.py +625 -0
  33. package/scripts/_triage_scope_cli.py +343 -0
  34. package/scripts/_triage_scope_drift_cli.py +121 -0
  35. package/scripts/_triage_scope_ignores.py +286 -0
  36. package/scripts/_triage_scope_milestone.py +432 -0
  37. package/scripts/_triage_scope_mutations.py +337 -0
  38. package/scripts/_triage_scope_renderers.py +207 -0
  39. package/scripts/_triage_smoketest_stages.py +674 -0
  40. package/scripts/_triage_subscribe_cli.py +140 -0
  41. package/scripts/_triage_welcome_cli.py +421 -0
  42. package/scripts/_vbrief_build.py +239 -0
  43. package/scripts/_vbrief_fidelity.py +479 -0
  44. package/scripts/_vbrief_legacy.py +589 -0
  45. package/scripts/_vbrief_reconciliation.py +883 -0
  46. package/scripts/_vbrief_routing.py +277 -0
  47. package/scripts/_vbrief_safety.py +778 -0
  48. package/scripts/_vbrief_sources.py +312 -0
  49. package/scripts/_vbrief_speckit.py +262 -0
  50. package/scripts/_vbrief_story_quality.py +353 -0
  51. package/scripts/_vbrief_validation.py +299 -0
  52. package/scripts/build_dist.py +412 -0
  53. package/scripts/cache.py +1078 -0
  54. package/scripts/cache_scanner.py +745 -0
  55. package/scripts/candidates_log.py +432 -0
  56. package/scripts/capacity_backfill.py +680 -0
  57. package/scripts/capacity_show.py +653 -0
  58. package/scripts/ci_local.py +689 -0
  59. package/scripts/code_structure_validate.py +765 -0
  60. package/scripts/codebase_default_extractor.py +495 -0
  61. package/scripts/codebase_map.py +304 -0
  62. package/scripts/codebase_map_fresh.py +104 -0
  63. package/scripts/codebase_projection_registry.py +94 -0
  64. package/scripts/codebase_provider.py +582 -0
  65. package/scripts/doctor.py +2257 -0
  66. package/scripts/framework_commands.py +505 -0
  67. package/scripts/gh_rest.py +882 -0
  68. package/scripts/github_auth_modes.py +437 -0
  69. package/scripts/github_body.py +292 -0
  70. package/scripts/ip_risk.py +531 -0
  71. package/scripts/issue_emit.py +670 -0
  72. package/scripts/issue_ingest.py +1064 -0
  73. package/scripts/migrate_preflight.py +418 -0
  74. package/scripts/migrate_vbrief.py +2677 -0
  75. package/scripts/monitor_pr.py +401 -0
  76. package/scripts/pack_migrate_lessons.py +336 -0
  77. package/scripts/pack_migrate_patterns.py +254 -0
  78. package/scripts/pack_migrate_rules.py +350 -0
  79. package/scripts/pack_migrate_skills.py +423 -0
  80. package/scripts/pack_migrate_strategies.py +311 -0
  81. package/scripts/pack_migrate_swarm_spec.py +250 -0
  82. package/scripts/pack_render.py +434 -0
  83. package/scripts/packs_slice.py +712 -0
  84. package/scripts/platform_capabilities.py +336 -0
  85. package/scripts/policy.py +2826 -0
  86. package/scripts/policy_set.py +324 -0
  87. package/scripts/pr_check_closing_keywords.py +524 -0
  88. package/scripts/pr_check_protected_issues.py +267 -0
  89. package/scripts/pr_merge_readiness.py +1004 -0
  90. package/scripts/pr_wait_mergeable.py +669 -0
  91. package/scripts/prd_render.py +159 -0
  92. package/scripts/preflight_architecture_sor.py +974 -0
  93. package/scripts/preflight_branch.py +289 -0
  94. package/scripts/preflight_cache.py +974 -0
  95. package/scripts/preflight_gh.py +721 -0
  96. package/scripts/preflight_implementation.py +272 -0
  97. package/scripts/preflight_story_start.py +838 -0
  98. package/scripts/preflight_wip_cap.py +149 -0
  99. package/scripts/probe_session.py +545 -0
  100. package/scripts/project_render.py +293 -0
  101. package/scripts/quarantine_ext.py +237 -0
  102. package/scripts/reconcile_issues.py +1442 -0
  103. package/scripts/refresh-path.ps1 +107 -0
  104. package/scripts/release.py +2030 -0
  105. package/scripts/release_e2e.py +1011 -0
  106. package/scripts/release_publish.py +486 -0
  107. package/scripts/release_rollback.py +980 -0
  108. package/scripts/relocate.py +1034 -0
  109. package/scripts/resolve_changelog_unreleased.py +667 -0
  110. package/scripts/resolve_version.py +490 -0
  111. package/scripts/resume_conditions.py +706 -0
  112. package/scripts/ritual_sentinel.py +609 -0
  113. package/scripts/roadmap_render.py +635 -0
  114. package/scripts/rule_ownership_lint.py +325 -0
  115. package/scripts/scm.py +591 -0
  116. package/scripts/scope_audit_log.py +387 -0
  117. package/scripts/scope_decompose.py +654 -0
  118. package/scripts/scope_demote.py +509 -0
  119. package/scripts/scope_lifecycle.py +1126 -0
  120. package/scripts/scope_undo.py +772 -0
  121. package/scripts/session_start.py +406 -0
  122. package/scripts/setup_ghx.py +339 -0
  123. package/scripts/setup_windows.ps1 +220 -0
  124. package/scripts/slice_audit.py +585 -0
  125. package/scripts/slice_record.py +530 -0
  126. package/scripts/slice_record_existing.py +692 -0
  127. package/scripts/slug_normalize.py +178 -0
  128. package/scripts/spec_render.py +477 -0
  129. package/scripts/spec_validate.py +238 -0
  130. package/scripts/subagent_monitor.py +658 -0
  131. package/scripts/swarm_complete_cohort.py +644 -0
  132. package/scripts/swarm_launch.py +1206 -0
  133. package/scripts/swarm_readiness.py +554 -0
  134. package/scripts/swarm_verify_review_clean.py +438 -0
  135. package/scripts/swarm_worktrees.py +497 -0
  136. package/scripts/toolchain-check.py +52 -0
  137. package/scripts/triage_actions.py +871 -0
  138. package/scripts/triage_bootstrap.py +1153 -0
  139. package/scripts/triage_bulk.py +630 -0
  140. package/scripts/triage_classify.py +932 -0
  141. package/scripts/triage_help.py +1685 -0
  142. package/scripts/triage_queue.py +1944 -0
  143. package/scripts/triage_reconcile.py +581 -0
  144. package/scripts/triage_refresh.py +643 -0
  145. package/scripts/triage_scope.py +999 -0
  146. package/scripts/triage_scope_drift.py +575 -0
  147. package/scripts/triage_smoketest.py +396 -0
  148. package/scripts/triage_subscribe.py +399 -0
  149. package/scripts/triage_summary.py +1011 -0
  150. package/scripts/triage_welcome.py +1178 -0
  151. package/scripts/ts_check_lane.py +86 -0
  152. package/scripts/validate-links.py +64 -0
  153. package/scripts/validate_strategy_output.py +212 -0
  154. package/scripts/vbrief_activate.py +228 -0
  155. package/scripts/vbrief_migrate_conformance.py +368 -0
  156. package/scripts/vbrief_reconcile_graph.py +306 -0
  157. package/scripts/vbrief_reconcile_labels.py +460 -0
  158. package/scripts/vbrief_reconcile_umbrellas.py +741 -0
  159. package/scripts/vbrief_validate.py +1195 -0
  160. package/scripts/verify-stubs.py +61 -0
  161. package/scripts/verify_capacity.py +160 -0
  162. package/scripts/verify_encoding.py +699 -0
  163. package/scripts/verify_hooks_installed.py +206 -0
  164. package/scripts/verify_investigation.py +360 -0
  165. package/scripts/verify_judgment_gates.py +827 -0
  166. package/scripts/verify_no_task_runtime.py +171 -0
  167. package/scripts/verify_scm_boundary.py +509 -0
  168. package/scripts/verify_session_ritual.py +389 -0
  169. package/scripts/verify_tools.py +426 -0
  170. package/scripts/verify_vbrief_conformance.py +478 -0
  171. package/tasks/architecture.yml +13 -0
  172. package/tasks/cache.yml +69 -0
  173. package/tasks/capacity.yml +38 -0
  174. package/tasks/change.yml +46 -0
  175. package/tasks/changelog.yml +24 -0
  176. package/tasks/ci.yml +49 -0
  177. package/tasks/codebase.yml +47 -0
  178. package/tasks/commit.yml +30 -0
  179. package/tasks/core.yml +126 -0
  180. package/tasks/deployments.yml +54 -0
  181. package/tasks/framework.yml +74 -0
  182. package/tasks/install.yml +60 -0
  183. package/tasks/issue.yml +50 -0
  184. package/tasks/migrate.yml +73 -0
  185. package/tasks/packs.yml +92 -0
  186. package/tasks/policy.yml +75 -0
  187. package/tasks/pr.yml +89 -0
  188. package/tasks/prd.yml +39 -0
  189. package/tasks/project.yml +27 -0
  190. package/tasks/reconcile.yml +32 -0
  191. package/tasks/relocate.yml +56 -0
  192. package/tasks/roadmap.yml +28 -0
  193. package/tasks/scm.yml +126 -0
  194. package/tasks/scope-undo.yml +36 -0
  195. package/tasks/scope.yml +141 -0
  196. package/tasks/session.yml +19 -0
  197. package/tasks/setup.yml +37 -0
  198. package/tasks/slice.yml +69 -0
  199. package/tasks/spec.yml +41 -0
  200. package/tasks/swarm.yml +85 -0
  201. package/tasks/toolchain.yml +13 -0
  202. package/tasks/triage-actions.yml +94 -0
  203. package/tasks/triage-bootstrap.yml +43 -0
  204. package/tasks/triage-bulk.yml +75 -0
  205. package/tasks/triage-classify.yml +30 -0
  206. package/tasks/triage-queue.yml +50 -0
  207. package/tasks/triage-reconcile.yml +29 -0
  208. package/tasks/triage-scope-drift.yml +29 -0
  209. package/tasks/triage-scope.yml +31 -0
  210. package/tasks/triage-smoketest.yml +33 -0
  211. package/tasks/triage-subscribe.yml +36 -0
  212. package/tasks/triage-summary.yml +29 -0
  213. package/tasks/triage-welcome.yml +32 -0
  214. package/tasks/ts.yml +328 -0
  215. package/tasks/vbrief.yml +206 -0
  216. package/tasks/verify.yml +292 -0
  217. package/templates/agents-entry.md +1 -1
@@ -0,0 +1,625 @@
1
+ """CLI helpers for ``scripts/triage_queue.py`` (#1128).
2
+
3
+ Extracted from ``scripts/triage_queue.py`` so the parent module stays
4
+ under the 1000-line MUST cap documented in ``coding/coding.md``. The
5
+ public surface lives in ``triage_queue``; this module is the argparse
6
+ shim and command dispatcher only.
7
+
8
+ Repo resolution (#1246)
9
+ -----------------------
10
+ The ``triage:queue`` / ``triage:show`` / ``triage:audit`` CLI surfaces
11
+ resolve ``--repo`` with the precedence: explicit ``--repo`` flag >
12
+ ``$DEFT_TRIAGE_REPO`` env var > auto-detection from
13
+ ``git remote get-url origin`` (run inside ``--project-root``) > error.
14
+ The auto-detection step removes the most-common-path papercut where an
15
+ operator inside an unambiguous clone had to repeat the repo slug on
16
+ every invocation. Cross-repo invocations remain supported via the
17
+ explicit flag (highest precedence).
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import argparse
23
+ import contextlib
24
+ import os
25
+ import re
26
+ import subprocess
27
+ import sys
28
+ from pathlib import Path
29
+ from typing import Any
30
+
31
+ # Make sibling scripts importable when invoked via Taskfile + uv.
32
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
33
+
34
+ # Optional: slice_audit ships in the same wave as this CLI (#1132 / D13).
35
+ # Guarded so an out-of-band import on a slim test checkout does not break.
36
+ try: # pragma: no cover -- exercised once #1132 lands.
37
+ import slice_audit # type: ignore[import-not-found]
38
+ except ImportError: # pragma: no cover
39
+ slice_audit = None # type: ignore[assignment]
40
+
41
+
42
+ def _add_common_args(parser: argparse.ArgumentParser) -> None:
43
+ parser.add_argument(
44
+ "--project-root",
45
+ default=os.environ.get("DEFT_PROJECT_ROOT", "."),
46
+ help=(
47
+ "Path to the consumer project root (default: $DEFT_PROJECT_ROOT or"
48
+ " the current working directory)."
49
+ ),
50
+ )
51
+ parser.add_argument(
52
+ "--repo",
53
+ default=os.environ.get("DEFT_TRIAGE_REPO"),
54
+ help=("Upstream repo slug 'owner/name'. Falls back to $DEFT_TRIAGE_REPO."),
55
+ )
56
+ parser.add_argument(
57
+ "--cache-root",
58
+ default=None,
59
+ help="Override the cache root (default: <project-root>/.deft-cache).",
60
+ )
61
+ parser.add_argument(
62
+ "--audit-log",
63
+ default=None,
64
+ help=(
65
+ "Override the audit log path (default: <project-root>/"
66
+ "vbrief/.eval/candidates.jsonl). Test hook."
67
+ ),
68
+ )
69
+ parser.add_argument(
70
+ "--slices-log",
71
+ default=None,
72
+ help=(
73
+ "Override the slices.jsonl path (default: <project-root>/"
74
+ "vbrief/.eval/slices.jsonl). Test hook for #1132 / D13."
75
+ ),
76
+ )
77
+
78
+
79
+ def build_parser(default_limit: int) -> argparse.ArgumentParser:
80
+ parser = argparse.ArgumentParser(
81
+ prog="triage_queue.py",
82
+ description=("Ranked triage queue + per-item show + audit-log surface (#1128)."),
83
+ )
84
+ sub = parser.add_subparsers(dest="cmd", required=True)
85
+
86
+ p_queue = sub.add_parser("queue", help="Print the ranked triage queue.")
87
+ _add_common_args(p_queue)
88
+ p_queue.add_argument(
89
+ "--limit",
90
+ type=int,
91
+ default=default_limit,
92
+ help=(
93
+ f"Cap the number of rows printed (default: {default_limit}). Pass 0 to disable the cap."
94
+ ),
95
+ )
96
+ p_queue.add_argument(
97
+ "--include-blocked",
98
+ action="store_true",
99
+ dest="include_blocked",
100
+ help=(
101
+ "Re-surface items whose linked vBRIEF is blocked (plan.status:blocked"
102
+ " or an unresolved swarm.depends_on) into their natural group. By"
103
+ " default such items are demoted into the [BLOCKED] group (#1286)."
104
+ ),
105
+ )
106
+
107
+ p_show = sub.add_parser(
108
+ "show",
109
+ help="Print per-issue triage detail (read-only).",
110
+ )
111
+ _add_common_args(p_show)
112
+ p_show.add_argument(
113
+ "number",
114
+ type=int,
115
+ help="Upstream issue number, e.g. 1128.",
116
+ )
117
+
118
+ p_audit = sub.add_parser(
119
+ "audit",
120
+ help="Print the audit-log surface (plain text or --format=json).",
121
+ )
122
+ _add_common_args(p_audit)
123
+ p_audit.add_argument(
124
+ "--format",
125
+ # 'text' is an alias for 'plain' so the documented surface
126
+ # ('--format=text|json' in the #1180 issue body and the D6 skill)
127
+ # matches the implementation surface (D11 shipped 'plain'|'json').
128
+ choices=("plain", "text", "json"),
129
+ default="plain",
130
+ help=(
131
+ "Output format. 'json' emits the stable schema consumed by D2"
132
+ " (#1122) for triage:summary integration. 'text' is an alias"
133
+ " for 'plain'."
134
+ ),
135
+ )
136
+ p_audit.add_argument(
137
+ "--vbrief-staleness",
138
+ action="store_true",
139
+ help=(
140
+ "Filter to audit entries whose latest 'accept' decision lacks an"
141
+ " active-vBRIEF reference. Used by D4 (#1124)."
142
+ ),
143
+ )
144
+ p_audit.add_argument(
145
+ "--evaluate-resume",
146
+ action="store_true",
147
+ dest="evaluate_resume",
148
+ help=(
149
+ "Before rendering, walk every open 'defer' audit entry whose"
150
+ " resume_on field is non-null and append a 'resume-eligible'"
151
+ " entry for each condition that fires (#1123 / D3)."
152
+ " Idempotent."
153
+ ),
154
+ )
155
+ # Date filters (#1180) -- distinct argparse group so the parallel D13
156
+ # 'Slice operations' group on the same subparser does not textually
157
+ # overlap during rebase. Both flags are optional + composable; an
158
+ # unset flag keeps D11's original behaviour (full audit-log dump).
159
+ date_filters = p_audit.add_argument_group(
160
+ "Date filters (#1180)",
161
+ "Read-only filters over the audit log; transform with jq.",
162
+ )
163
+ date_filters.add_argument(
164
+ "--action",
165
+ default=None,
166
+ help=(
167
+ "Filter to audit entries whose `decision` equals <verb> (e.g."
168
+ " --action=demote-meta, --action=accept). v1 accepts a single"
169
+ " verb; pipe through jq for multi-verb queries. Invalid verb"
170
+ " -> exit 2 with explanatory stderr."
171
+ ),
172
+ )
173
+ date_filters.add_argument(
174
+ "--since",
175
+ default=None,
176
+ help=(
177
+ "Filter to entries whose timestamp is at-or-after now - <window>."
178
+ " Accepts the framework duration grammar: Nd / Nh / Nm / Nw / Ns"
179
+ " (e.g. '7d', '24h', '30m') or ISO-8601 PnDTnHnMnS (e.g. 'P7D',"
180
+ " 'PT24H'). Invalid -> exit 2 with explanatory stderr."
181
+ ),
182
+ )
183
+
184
+ # ----- Slice operations (#1132 / D13) -----
185
+ #
186
+ # Each of the three flags below selects a distinct slice-related
187
+ # surface; they are mutually exclusive (the CLI picks the first one
188
+ # set and emits its renderer instead of the default audit dump).
189
+ # Kept as a distinct argparse group so #1180's date-filter flags can
190
+ # land as a separate `Date filters` group without textual overlap.
191
+ slice_group = p_audit.add_argument_group(
192
+ "Slice operations (#1132 / D13)",
193
+ "Read-only surfaces that join slices.jsonl against the cache.",
194
+ )
195
+ slice_group.add_argument(
196
+ "--orphans",
197
+ action="store_true",
198
+ help=(
199
+ "List children whose umbrella issue is closed while they remain"
200
+ " open. Output: one line per orphan with umbrella back-pointer."
201
+ ),
202
+ )
203
+ slice_group.add_argument(
204
+ "--slice-stalled",
205
+ action="store_true",
206
+ dest="slice_stalled",
207
+ help=(
208
+ "List cohorts where >=1 child has merged but >=1 sibling has"
209
+ " not moved in --days days (default 30)."
210
+ ),
211
+ )
212
+ slice_group.add_argument(
213
+ "--slice-coverage",
214
+ action="store_true",
215
+ dest="slice_coverage",
216
+ help=(
217
+ "For each open umbrella in slices.jsonl, print"
218
+ " <umbrella>: <closed>/<total> children merged."
219
+ ),
220
+ )
221
+ slice_group.add_argument(
222
+ "--days",
223
+ type=int,
224
+ default=None,
225
+ help=(
226
+ "Stall window in days for --slice-stalled (default 30)."
227
+ " No effect without --slice-stalled."
228
+ ),
229
+ )
230
+
231
+ return parser
232
+
233
+
234
+ #: subprocess.run timeout for ``git remote get-url origin`` auto-detection
235
+ #: (#1246). Defensive: a stuck ``git`` proxy (corporate VPN re-auth) would
236
+ #: otherwise hang every ``task triage:queue`` invocation indefinitely.
237
+ _GIT_INFER_TIMEOUT_S: int = 10
238
+
239
+
240
+ def _detect_origin_repo(project_root: Path | None) -> str | None:
241
+ """Return ``owner/name`` parsed from ``git remote get-url origin``, or ``None``.
242
+
243
+ Run inside ``project_root`` (or the current working directory when
244
+ ``project_root`` is ``None``). Returns ``None`` on any of:
245
+
246
+ * ``git`` not on PATH.
247
+ * ``git remote get-url origin`` exits non-zero (outside a git working
248
+ tree, or no ``origin`` remote configured).
249
+ * The subprocess hangs past :data:`_GIT_INFER_TIMEOUT_S` seconds --
250
+ defensive against a wedged credential helper / VPN re-auth.
251
+ * The origin URL is not a recognised ``github.com`` form (https /
252
+ ssh / git@ shapes).
253
+
254
+ Delegated to ``scripts/_project_context.py::_detect_repo_from_git``
255
+ where importable so the framework keeps a single origin-detection
256
+ grammar; falls back to an inline implementation only on slim test
257
+ checkouts that have not yet rebased onto that module.
258
+ """
259
+ try:
260
+ from _project_context import _detect_repo_from_git
261
+ except ImportError: # pragma: no cover -- slim-checkout fallback
262
+ return _detect_origin_repo_inline(project_root)
263
+ return _detect_repo_from_git(project_root)
264
+
265
+
266
+ def _detect_origin_repo_inline(project_root: Path | None) -> str | None:
267
+ """Fallback origin-detector used when ``_project_context`` is unimportable.
268
+
269
+ Mirrors the precedence + parsing rules of the canonical helper so
270
+ consumers on a slim test checkout still get the #1246 papercut
271
+ eliminated. Returns ``None`` when detection fails so the caller can
272
+ surface the canonical "--repo required" error.
273
+ """
274
+ cwd = str(project_root) if project_root is not None else None
275
+ try:
276
+ result = subprocess.run( # noqa: S603 -- argv is a literal
277
+ ["git", "remote", "get-url", "origin"],
278
+ capture_output=True,
279
+ text=True,
280
+ check=False,
281
+ cwd=cwd,
282
+ timeout=_GIT_INFER_TIMEOUT_S,
283
+ )
284
+ except (FileNotFoundError, OSError, subprocess.SubprocessError):
285
+ return None
286
+ if result.returncode != 0:
287
+ return None
288
+ url = (result.stdout or "").strip()
289
+ if not url:
290
+ return None
291
+ match = re.search(
292
+ r"github\.com[:/]([A-Za-z0-9][A-Za-z0-9._-]*)/"
293
+ r"([A-Za-z0-9][A-Za-z0-9._-]*?)(?:\.git)?/?\s*$",
294
+ url,
295
+ )
296
+ if not match:
297
+ return None
298
+ return f"{match.group(1)}/{match.group(2)}"
299
+
300
+
301
+ def _resolve_repo(args: argparse.Namespace) -> str | None:
302
+ """Resolve the effective ``--repo`` slug for triage_queue CLI verbs.
303
+
304
+ Delegates to the canonical :func:`triage_queue._resolve_repo` (#1238)
305
+ so the resolution chain lives in the documented module and stays in
306
+ lockstep with ``preflight_cache`` / ``triage_bootstrap``. Precedence,
307
+ highest first:
308
+
309
+ 1. ``args.repo`` -- the explicit ``--repo`` flag, which also picks up
310
+ ``$DEFT_TRIAGE_REPO`` because the argparse default reads the env
311
+ var. Highest precedence; preserved for cross-repo invocations.
312
+ 2. ``$DEFT_TRIAGE_REPO`` -- the canonical helper re-checks the env var
313
+ so the precedence is honoured even when a caller constructs the
314
+ namespace without the argparse env default.
315
+ 3. ``git remote get-url origin`` parsed from inside
316
+ ``--project-root`` (or the current working directory). Removes
317
+ the papercut where an operator inside an unambiguous clone had
318
+ to repeat the repo slug on every ``task triage:queue`` call.
319
+ 4. ``None`` -- the caller emits the canonical
320
+ ``triage:<verb>: --repo OWNER/NAME (or $DEFT_TRIAGE_REPO) is
321
+ required.`` error so the operator sees an actionable next step
322
+ rather than a silent empty-cache walk.
323
+ """
324
+ import triage_queue
325
+
326
+ project_root: Path | None = None
327
+ if getattr(args, "project_root", None):
328
+ with contextlib.suppress(OSError):
329
+ project_root = Path(args.project_root).resolve()
330
+ return triage_queue._resolve_repo(args.repo, project_root=project_root)
331
+
332
+
333
+ def _override_cache_root(project_root: Path, cache_root: Path) -> None:
334
+ """Best-effort symlink so the cache walker finds ``cache_root``.
335
+
336
+ Used only by the ``--cache-root`` test hook. The function is a no-op
337
+ on Windows without admin / dev mode (symlink creation rejected); the
338
+ test path falls through and passes ``--project-root`` at the cache
339
+ root instead.
340
+ """
341
+ target = project_root / ".deft-cache"
342
+ if target.exists():
343
+ with contextlib.suppress(OSError):
344
+ if target.resolve() == cache_root.resolve():
345
+ return
346
+ return
347
+ with contextlib.suppress(OSError):
348
+ target.symlink_to(cache_root, target_is_directory=True)
349
+
350
+
351
+ def _cmd_queue(args: argparse.Namespace, tq: Any) -> int:
352
+ repo = _resolve_repo(args)
353
+ if not repo:
354
+ print(
355
+ "triage:queue: --repo OWNER/NAME (or $DEFT_TRIAGE_REPO) is required.",
356
+ file=sys.stderr,
357
+ )
358
+ return 2
359
+ project_root = Path(args.project_root).resolve()
360
+ if args.cache_root:
361
+ _override_cache_root(project_root, Path(args.cache_root).resolve())
362
+ # Load both open and closed issues so the orphan detection in #1132
363
+ # can see closed umbrellas; the queue itself still filters to open
364
+ # children via the QueueBuildOptions.orphan_issue_numbers set.
365
+ issues_for_queue = tq.load_cached_issues(repo, project_root=project_root)
366
+ issues_with_closed = tq.load_cached_issues(repo, project_root=project_root, include_closed=True)
367
+ issues_by_number = {i["number"]: i for i in issues_with_closed}
368
+ audit_entries = tq.read_audit_entries(repo, audit_path=args.audit_log)
369
+ ranking_labels = tuple(tq.resolve_ranking_labels(project_root))
370
+ active_refs = frozenset(tq._active_referenced_issue_numbers(project_root))
371
+ orphan_numbers: frozenset[int] = frozenset()
372
+ if slice_audit is not None:
373
+ records = slice_audit.load_slice_records(tq.slice_record, path=args.slices_log)
374
+ orphan_numbers = slice_audit.collect_orphan_issue_numbers(records, issues_by_number)
375
+ limit = None if args.limit == 0 else max(0, int(args.limit))
376
+ options = tq.QueueBuildOptions(
377
+ ranking_labels=ranking_labels,
378
+ active_referenced=active_refs,
379
+ orphan_issue_numbers=orphan_numbers,
380
+ include_blocked=getattr(args, "include_blocked", False),
381
+ limit=limit,
382
+ )
383
+ items = tq.build_queue(issues_for_queue, audit_entries, repo=repo, options=options)
384
+ print(
385
+ tq.render_queue(
386
+ items,
387
+ repo=repo,
388
+ limit=limit,
389
+ ranking_labels=ranking_labels,
390
+ )
391
+ )
392
+ return 0
393
+
394
+
395
+ def _cmd_show(args: argparse.Namespace, tq: Any) -> int:
396
+ repo = _resolve_repo(args)
397
+ if not repo:
398
+ print(
399
+ "triage:show: --repo OWNER/NAME (or $DEFT_TRIAGE_REPO) is required.",
400
+ file=sys.stderr,
401
+ )
402
+ return 2
403
+ project_root = Path(args.project_root).resolve()
404
+ if args.cache_root:
405
+ _override_cache_root(project_root, Path(args.cache_root).resolve())
406
+ issues = {
407
+ i["number"]: i
408
+ for i in tq.load_cached_issues(repo, project_root=project_root, include_closed=True)
409
+ }
410
+ issue = issues.get(int(args.number))
411
+ history: list[dict[str, Any]] = []
412
+ if tq.candidates_log is not None:
413
+ history = list(tq.candidates_log.find_by_issue(int(args.number), repo, path=args.audit_log))
414
+ history_sorted = sorted(history, key=lambda r: r.get("timestamp", ""))
415
+ latest = history_sorted[-1] if history_sorted else None
416
+ active_refs = tq._active_referenced_issue_numbers(project_root)
417
+ print(
418
+ tq.render_show(
419
+ issue,
420
+ repo=repo,
421
+ number=int(args.number),
422
+ latest_decision=latest,
423
+ history=history_sorted,
424
+ in_active_vbrief=int(args.number) in active_refs,
425
+ )
426
+ )
427
+ return 0 if issue is not None else 1
428
+
429
+
430
+ def _cmd_audit(args: argparse.Namespace, tq: Any) -> int:
431
+ repo = _resolve_repo(args)
432
+ project_root = Path(args.project_root).resolve()
433
+ if args.cache_root:
434
+ _override_cache_root(project_root, Path(args.cache_root).resolve())
435
+ # #1132 / D13: slice operation flags short-circuit the audit dump.
436
+ # Mutually exclusive: first set flag wins; if more than one is
437
+ # passed the chained calls render only the highest-priority one.
438
+ if getattr(args, "orphans", False):
439
+ return _cmd_slice_orphans(args, tq, repo=repo, project_root=project_root)
440
+ if getattr(args, "slice_stalled", False):
441
+ return _cmd_slice_stalled(args, tq, repo=repo, project_root=project_root)
442
+ if getattr(args, "slice_coverage", False):
443
+ return _cmd_slice_coverage(args, tq, repo=repo, project_root=project_root)
444
+ # #1180: validate --action / --since up front so a typo fails fast
445
+ # (exit 2) instead of silently returning an empty result set. Runs
446
+ # AFTER the D13 slice short-circuit so --orphans/--slice-stalled/
447
+ # --slice-coverage don't waste cycles validating filters that the
448
+ # slice handlers never consume.
449
+ if args.action is not None:
450
+ valid_actions = tq.valid_audit_actions()
451
+ if args.action not in valid_actions:
452
+ print(
453
+ f"triage:audit --action: unknown verb {args.action!r};"
454
+ f" expected one of {sorted(valid_actions)}",
455
+ file=sys.stderr,
456
+ )
457
+ return 2
458
+ since_window = None
459
+ if args.since is not None:
460
+ try:
461
+ since_window = tq.parse_audit_window(args.since)
462
+ except ValueError as exc:
463
+ print(f"triage:audit --since: {exc}", file=sys.stderr)
464
+ return 2
465
+ # #1123 / D3: optional resume-eligibility evaluation pass. Runs
466
+ # BEFORE the audit dump so newly-appended ``resume-eligible`` rows
467
+ # surface in the same call. No-op when the resume_conditions module
468
+ # is not importable (slim test checkout).
469
+ if getattr(args, "evaluate_resume", False) and tq.resume_conditions is not None:
470
+ cache_root = Path(args.cache_root).resolve() if args.cache_root else None
471
+ try:
472
+ tq.resume_conditions.evaluate_resume_eligibility(
473
+ project_root,
474
+ cache_root=cache_root,
475
+ audit_log_path=args.audit_log,
476
+ repo=repo,
477
+ )
478
+ except Exception as exc: # noqa: BLE001 -- best-effort surface
479
+ print(
480
+ f"triage:audit --evaluate-resume: evaluation failed: {exc}",
481
+ file=sys.stderr,
482
+ )
483
+ entries = tq.read_audit_entries(repo, audit_path=args.audit_log)
484
+ # #1180 date / action filters. Apply BEFORE --vbrief-staleness so the
485
+ # staleness reduction sees the filtered set; the operator who asked
486
+ # for `--since=30d --vbrief-staleness` wants "stale acceptances within
487
+ # the last 30 days", not "stale acceptances ever, then filtered to
488
+ # the last 30 days". Order: action -> since -> staleness.
489
+ if args.action is not None:
490
+ entries = tq.filter_by_action(entries, args.action)
491
+ if since_window is not None:
492
+ entries = tq.filter_by_since(entries, since_window)
493
+ if args.vbrief_staleness:
494
+ active_refs = frozenset(tq._active_referenced_issue_numbers(project_root))
495
+ latest = tq.latest_decisions_by_issue(entries)
496
+ entries = [entry for entry in latest.values() if tq.is_stale_acceptance(entry, active_refs)]
497
+ entries.sort(key=lambda r: r.get("timestamp", ""))
498
+ if args.format == "json":
499
+ print(
500
+ tq.render_audit_json(
501
+ entries,
502
+ repo=repo,
503
+ vbrief_staleness=args.vbrief_staleness,
504
+ )
505
+ )
506
+ else:
507
+ # 'plain' and 'text' alias to the same renderer.
508
+ print(
509
+ tq.render_audit_plain(
510
+ entries,
511
+ repo=repo,
512
+ vbrief_staleness=args.vbrief_staleness,
513
+ )
514
+ )
515
+ return 0
516
+
517
+
518
+ def _slice_inputs(
519
+ args: argparse.Namespace,
520
+ tq: Any,
521
+ *,
522
+ repo: str | None,
523
+ project_root: Path,
524
+ ) -> tuple[list[dict[str, Any]], dict[int, dict[str, Any]]] | None:
525
+ """Load (slice_records, issues_by_number) or return ``None`` on missing surface.
526
+
527
+ Prints the canonical informational message to stderr and returns ``None``
528
+ when ``slice_audit`` is not importable -- the issue body's backward-
529
+ compat requirement ("slices.jsonl missing -> flags exit 0 with
530
+ informational stderr"). Repo is required for the cache walk but a
531
+ missing slices.jsonl is silent (read_all returns []).
532
+ """
533
+ if slice_audit is None:
534
+ print(
535
+ "triage:audit: slice operation flags require scripts/slice_audit.py"
536
+ " (#1132 / D13); skipping.",
537
+ file=sys.stderr,
538
+ )
539
+ return None
540
+ if not repo:
541
+ print(
542
+ "triage:audit: --repo OWNER/NAME (or $DEFT_TRIAGE_REPO) is required"
543
+ " for slice operations.",
544
+ file=sys.stderr,
545
+ )
546
+ return None
547
+ records = slice_audit.load_slice_records(tq.slice_record, path=args.slices_log)
548
+ issues = tq.load_cached_issues(repo, project_root=project_root, include_closed=True)
549
+ issues_by_number = {i["number"]: i for i in issues}
550
+ return records, issues_by_number
551
+
552
+
553
+ def _cmd_slice_orphans(
554
+ args: argparse.Namespace,
555
+ tq: Any,
556
+ *,
557
+ repo: str | None,
558
+ project_root: Path,
559
+ ) -> int:
560
+ loaded = _slice_inputs(args, tq, repo=repo, project_root=project_root)
561
+ if loaded is None:
562
+ return 0
563
+ records, issues_by_number = loaded
564
+ rows = slice_audit.compute_orphans(records, issues_by_number)
565
+ if args.format == "json":
566
+ print(slice_audit.render_orphans_json(rows, repo=repo))
567
+ else:
568
+ print(slice_audit.render_orphans_plain(rows, repo=repo))
569
+ return 0
570
+
571
+
572
+ def _cmd_slice_stalled(
573
+ args: argparse.Namespace,
574
+ tq: Any,
575
+ *,
576
+ repo: str | None,
577
+ project_root: Path,
578
+ ) -> int:
579
+ loaded = _slice_inputs(args, tq, repo=repo, project_root=project_root)
580
+ if loaded is None:
581
+ return 0
582
+ records, issues_by_number = loaded
583
+ days = args.days if args.days is not None else tq.DEFAULT_SLICE_STALLED_DAYS
584
+ rows = slice_audit.compute_stalled(records, issues_by_number, days=days)
585
+ if args.format == "json":
586
+ print(slice_audit.render_stalled_json(rows, repo=repo, days=days))
587
+ else:
588
+ print(slice_audit.render_stalled_plain(rows, repo=repo, days=days))
589
+ return 0
590
+
591
+
592
+ def _cmd_slice_coverage(
593
+ args: argparse.Namespace,
594
+ tq: Any,
595
+ *,
596
+ repo: str | None,
597
+ project_root: Path,
598
+ ) -> int:
599
+ loaded = _slice_inputs(args, tq, repo=repo, project_root=project_root)
600
+ if loaded is None:
601
+ return 0
602
+ records, issues_by_number = loaded
603
+ rows = slice_audit.compute_coverage(records, issues_by_number)
604
+ if args.format == "json":
605
+ print(slice_audit.render_coverage_json(rows, repo=repo))
606
+ else:
607
+ print(slice_audit.render_coverage_plain(rows, repo=repo))
608
+ return 0
609
+
610
+
611
+ def run_cli(argv: list[str] | None, tq_module: Any) -> int:
612
+ """Dispatch ``triage_queue`` CLI args using ``tq_module`` as backend."""
613
+ parser = build_parser(tq_module.DEFAULT_QUEUE_LIMIT)
614
+ try:
615
+ args = parser.parse_args(argv)
616
+ except SystemExit as exc:
617
+ return int(exc.code) if isinstance(exc.code, int) else 2
618
+ if args.cmd == "queue":
619
+ return _cmd_queue(args, tq_module)
620
+ if args.cmd == "show":
621
+ return _cmd_show(args, tq_module)
622
+ if args.cmd == "audit":
623
+ return _cmd_audit(args, tq_module)
624
+ parser.print_help()
625
+ return 2