@deftai/directive-content 0.59.0 → 0.61.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/.githooks/pre-commit +10 -128
  2. package/.githooks/pre-push +8 -108
  3. package/Taskfile.yml +48 -58
  4. package/UPGRADING.md +19 -3
  5. package/docs/assets/directive-lifecycle-diagram.png +0 -0
  6. package/docs/directive-lifecycle.md +73 -0
  7. package/docs/getting-started.md +5 -1
  8. package/package.json +3 -3
  9. package/packs/skills/skills-pack-0.1.json +1 -1
  10. package/packs/strategies/strategies-pack-0.1.json +19 -19
  11. package/scm/github.md +37 -6
  12. package/skills/deft-directive-setup/SKILL.md +24 -15
  13. package/strategies/speckit.md +14 -14
  14. package/strategies/v0-20-contract.md +12 -1
  15. package/tasks/change.yml +16 -31
  16. package/tasks/ci.yml +8 -0
  17. package/tasks/commit.yml +12 -19
  18. package/tasks/core.yml +10 -0
  19. package/tasks/engine.yml +42 -0
  20. package/tasks/framework.yml +3 -0
  21. package/tasks/install.yml +20 -19
  22. package/tasks/migrate.yml +26 -15
  23. package/tasks/project.yml +26 -0
  24. package/tasks/toolchain.yml +15 -5
  25. package/tasks/vbrief.yml +4 -3
  26. package/tasks/verify.yml +12 -14
  27. package/templates/agents-entry.md +1 -1
  28. package/scripts/_agents_md.py +0 -494
  29. package/scripts/_cache_fetch.py +0 -635
  30. package/scripts/_cache_quota.py +0 -529
  31. package/scripts/_cache_refresh.py +0 -163
  32. package/scripts/_cache_validate.py +0 -209
  33. package/scripts/_content_root.py +0 -42
  34. package/scripts/_doctor_state.py +0 -277
  35. package/scripts/_event_detect.py +0 -305
  36. package/scripts/_events.py +0 -514
  37. package/scripts/_lifecycle_hygiene.py +0 -568
  38. package/scripts/_pathspec.py +0 -91
  39. package/scripts/_policy_show_cli.py +0 -266
  40. package/scripts/_precutover.py +0 -92
  41. package/scripts/_project_context.py +0 -224
  42. package/scripts/_project_definition_io.py +0 -164
  43. package/scripts/_relocate_snapshot.py +0 -209
  44. package/scripts/_relocate_states.py +0 -343
  45. package/scripts/_resolve_preflight_path.py +0 -152
  46. package/scripts/_safe_subprocess.py +0 -167
  47. package/scripts/_session_start_hook.py +0 -205
  48. package/scripts/_sor_gate_diff.py +0 -365
  49. package/scripts/_stdio_utf8.py +0 -59
  50. package/scripts/_triage_bootstrap_gitignore.py +0 -904
  51. package/scripts/_triage_classify_cli.py +0 -122
  52. package/scripts/_triage_queue_cli.py +0 -625
  53. package/scripts/_triage_scope_cli.py +0 -343
  54. package/scripts/_triage_scope_drift_cli.py +0 -121
  55. package/scripts/_triage_scope_ignores.py +0 -286
  56. package/scripts/_triage_scope_milestone.py +0 -432
  57. package/scripts/_triage_scope_mutations.py +0 -337
  58. package/scripts/_triage_scope_renderers.py +0 -207
  59. package/scripts/_triage_smoketest_stages.py +0 -674
  60. package/scripts/_triage_subscribe_cli.py +0 -140
  61. package/scripts/_triage_welcome_cli.py +0 -421
  62. package/scripts/_vbrief_build.py +0 -239
  63. package/scripts/_vbrief_fidelity.py +0 -479
  64. package/scripts/_vbrief_legacy.py +0 -589
  65. package/scripts/_vbrief_reconciliation.py +0 -883
  66. package/scripts/_vbrief_routing.py +0 -277
  67. package/scripts/_vbrief_safety.py +0 -778
  68. package/scripts/_vbrief_sources.py +0 -312
  69. package/scripts/_vbrief_speckit.py +0 -262
  70. package/scripts/_vbrief_story_quality.py +0 -353
  71. package/scripts/_vbrief_validation.py +0 -299
  72. package/scripts/build_dist.py +0 -412
  73. package/scripts/cache.py +0 -1078
  74. package/scripts/cache_scanner.py +0 -745
  75. package/scripts/candidates_log.py +0 -432
  76. package/scripts/capacity_backfill.py +0 -680
  77. package/scripts/capacity_show.py +0 -653
  78. package/scripts/ci_local.py +0 -689
  79. package/scripts/code_structure_validate.py +0 -765
  80. package/scripts/codebase_default_extractor.py +0 -495
  81. package/scripts/codebase_map.py +0 -304
  82. package/scripts/codebase_map_fresh.py +0 -104
  83. package/scripts/codebase_projection_registry.py +0 -94
  84. package/scripts/codebase_provider.py +0 -582
  85. package/scripts/doctor.py +0 -2552
  86. package/scripts/framework_commands.py +0 -505
  87. package/scripts/gh_rest.py +0 -882
  88. package/scripts/github_auth_modes.py +0 -437
  89. package/scripts/github_body.py +0 -292
  90. package/scripts/ip_risk.py +0 -531
  91. package/scripts/issue_emit.py +0 -670
  92. package/scripts/issue_ingest.py +0 -1064
  93. package/scripts/migrate_preflight.py +0 -418
  94. package/scripts/migrate_vbrief.py +0 -2677
  95. package/scripts/monitor_pr.py +0 -401
  96. package/scripts/pack_migrate_lessons.py +0 -336
  97. package/scripts/pack_migrate_patterns.py +0 -254
  98. package/scripts/pack_migrate_rules.py +0 -350
  99. package/scripts/pack_migrate_skills.py +0 -423
  100. package/scripts/pack_migrate_strategies.py +0 -311
  101. package/scripts/pack_migrate_swarm_spec.py +0 -250
  102. package/scripts/pack_render.py +0 -434
  103. package/scripts/packs_slice.py +0 -712
  104. package/scripts/platform_capabilities.py +0 -336
  105. package/scripts/policy.py +0 -2826
  106. package/scripts/policy_set.py +0 -324
  107. package/scripts/pr_check_closing_keywords.py +0 -524
  108. package/scripts/pr_check_protected_issues.py +0 -267
  109. package/scripts/pr_merge_readiness.py +0 -1004
  110. package/scripts/pr_wait_mergeable.py +0 -669
  111. package/scripts/prd_render.py +0 -159
  112. package/scripts/preflight_architecture_sor.py +0 -974
  113. package/scripts/preflight_branch.py +0 -289
  114. package/scripts/preflight_cache.py +0 -974
  115. package/scripts/preflight_gh.py +0 -721
  116. package/scripts/preflight_implementation.py +0 -272
  117. package/scripts/preflight_story_start.py +0 -838
  118. package/scripts/preflight_wip_cap.py +0 -149
  119. package/scripts/probe_session.py +0 -545
  120. package/scripts/project_render.py +0 -293
  121. package/scripts/quarantine_ext.py +0 -237
  122. package/scripts/reconcile_issues.py +0 -1442
  123. package/scripts/refresh-path.ps1 +0 -107
  124. package/scripts/release.py +0 -2030
  125. package/scripts/release_e2e.py +0 -1011
  126. package/scripts/release_publish.py +0 -486
  127. package/scripts/release_rollback.py +0 -980
  128. package/scripts/relocate.py +0 -1034
  129. package/scripts/resolve_changelog_unreleased.py +0 -667
  130. package/scripts/resolve_version.py +0 -490
  131. package/scripts/resume_conditions.py +0 -706
  132. package/scripts/ritual_sentinel.py +0 -609
  133. package/scripts/roadmap_render.py +0 -635
  134. package/scripts/rule_ownership_lint.py +0 -325
  135. package/scripts/scm.py +0 -591
  136. package/scripts/scope_audit_log.py +0 -387
  137. package/scripts/scope_decompose.py +0 -654
  138. package/scripts/scope_demote.py +0 -509
  139. package/scripts/scope_lifecycle.py +0 -1126
  140. package/scripts/scope_undo.py +0 -772
  141. package/scripts/session_start.py +0 -406
  142. package/scripts/setup_ghx.py +0 -339
  143. package/scripts/setup_windows.ps1 +0 -220
  144. package/scripts/slice_audit.py +0 -585
  145. package/scripts/slice_record.py +0 -530
  146. package/scripts/slice_record_existing.py +0 -692
  147. package/scripts/slug_normalize.py +0 -178
  148. package/scripts/spec_render.py +0 -477
  149. package/scripts/spec_validate.py +0 -238
  150. package/scripts/subagent_monitor.py +0 -658
  151. package/scripts/swarm_complete_cohort.py +0 -644
  152. package/scripts/swarm_launch.py +0 -1206
  153. package/scripts/swarm_readiness.py +0 -554
  154. package/scripts/swarm_verify_review_clean.py +0 -438
  155. package/scripts/swarm_worktrees.py +0 -497
  156. package/scripts/toolchain-check.py +0 -52
  157. package/scripts/triage_actions.py +0 -871
  158. package/scripts/triage_bootstrap.py +0 -1153
  159. package/scripts/triage_bulk.py +0 -630
  160. package/scripts/triage_classify.py +0 -932
  161. package/scripts/triage_help.py +0 -1685
  162. package/scripts/triage_queue.py +0 -1944
  163. package/scripts/triage_reconcile.py +0 -581
  164. package/scripts/triage_refresh.py +0 -643
  165. package/scripts/triage_scope.py +0 -999
  166. package/scripts/triage_scope_drift.py +0 -575
  167. package/scripts/triage_smoketest.py +0 -396
  168. package/scripts/triage_subscribe.py +0 -399
  169. package/scripts/triage_summary.py +0 -1011
  170. package/scripts/triage_welcome.py +0 -1178
  171. package/scripts/ts_check_lane.py +0 -86
  172. package/scripts/validate-links.py +0 -64
  173. package/scripts/validate_strategy_output.py +0 -212
  174. package/scripts/vbrief_activate.py +0 -228
  175. package/scripts/vbrief_migrate_conformance.py +0 -368
  176. package/scripts/vbrief_reconcile_graph.py +0 -306
  177. package/scripts/vbrief_reconcile_labels.py +0 -460
  178. package/scripts/vbrief_reconcile_umbrellas.py +0 -741
  179. package/scripts/vbrief_validate.py +0 -1144
  180. package/scripts/verify-stubs.py +0 -61
  181. package/scripts/verify_capacity.py +0 -160
  182. package/scripts/verify_encoding.py +0 -699
  183. package/scripts/verify_hooks_installed.py +0 -206
  184. package/scripts/verify_investigation.py +0 -360
  185. package/scripts/verify_judgment_gates.py +0 -827
  186. package/scripts/verify_no_task_runtime.py +0 -171
  187. package/scripts/verify_scm_boundary.py +0 -509
  188. package/scripts/verify_session_ritual.py +0 -389
  189. package/scripts/verify_tools.py +0 -426
  190. package/scripts/verify_vbrief_conformance.py +0 -478
@@ -1,630 +0,0 @@
1
- #!/usr/bin/env python3
2
- """triage_bulk.py -- Story 4 bulk triage ops over the unified cache (#883 Story 3).
3
-
4
- Public surface:
5
-
6
- - :func:`bulk_action(action_key, repo, ...)` -- programmatic entrypoint.
7
- - :func:`main(argv)` -- CLI dispatcher invoked by ``tasks/triage-bulk.yml``.
8
-
9
- The four CLI sub-actions exposed via ``argparse``:
10
-
11
- - ``bulk-accept`` -> ``triage_actions.accept(N, repo)``
12
- - ``bulk-reject`` -> ``triage_actions.reject(N, repo, reason=...)``
13
- - ``bulk-defer`` -> ``triage_actions.defer(N, repo)``
14
- - ``bulk-needs-ac`` -> ``triage_actions.needs_ac(N, repo)``
15
-
16
- Filter flags (combinable, AND semantics):
17
-
18
- - ``--label <name>`` match a label by name on the issue.
19
- - ``--author <login>`` match the GitHub author login.
20
- - ``--age-days <N>`` match issues older than ``now - N days``.
21
- - ``--cluster <slug>`` match a ``cluster:<slug>`` (or bare ``<slug>``) label.
22
-
23
- Cache contract (#883 Story 3 rebind onto cache:*)
24
- -------------------------------------------------
25
-
26
- The candidate universe is read via the unified cache: for each issue
27
- cached under ``.deft-cache/github-issue/<owner>/<repo>/<N>/`` we call
28
- :func:`scripts.cache.cache_get` (which validates ``meta.json`` against
29
- the schema) and reload the matching ``raw.json`` for the per-issue
30
- payload (number / labels / author / createdAt / ...). Live
31
- ``gh issue list`` calls are forbidden in this module -- the cache is
32
- the read surface for the triage workflow.
33
-
34
- When the per-repo cache is missing or empty, :func:`bulk_action` raises
35
- :class:`CacheEmptyError` and :func:`main` exits with status ``2`` and
36
- the canonical message::
37
-
38
- triage_bulk: cache is empty for {repo}; run `task triage:bootstrap` first.
39
-
40
- Audit-log short-circuit (preserves #915 fix invariants)
41
- -------------------------------------------------------
42
-
43
- Before applying the chosen action, the cached candidate set is
44
- intersected with Story 2's append-only audit log
45
- (:mod:`candidates_log`). For each candidate, the LATEST recorded
46
- decision (by ``timestamp``) determines whether the candidate is
47
- skipped:
48
-
49
- - **Terminal decisions** (``accept``, ``reject``, ``mark-duplicate``)
50
- are ALWAYS skipped.
51
- - **In-progress decisions** (``defer``, ``needs-ac``) are skipped
52
- UNLESS the operator passes ``--re-action`` (CLI) /
53
- ``re_action=True`` (Python).
54
- - ``reset`` is non-skipping by design.
55
-
56
- Zero-match exits cleanly with status 0 and a single stdout line.
57
- """
58
-
59
- from __future__ import annotations
60
-
61
- import argparse
62
- import contextlib
63
- import importlib
64
- import json
65
- import re
66
- import sys
67
- from collections.abc import Callable, Iterable
68
- from datetime import UTC, datetime, timedelta
69
- from pathlib import Path
70
- from typing import Any
71
-
72
- # Surface sibling ``scripts`` modules so the cache walk and audit-log
73
- # read resolve when this file is invoked via
74
- # ``python scripts/triage_bulk.py`` from a Taskfile dispatch.
75
- sys.path.insert(0, str(Path(__file__).resolve().parent))
76
-
77
- # Mapping from CLI sub-action keyword to the ``triage_actions`` module
78
- # attribute resolved at runtime.
79
- ACTION_FN_NAMES: dict[str, str] = {
80
- "accept": "accept",
81
- "reject": "reject",
82
- "defer": "defer",
83
- "needs-ac": "needs_ac",
84
- }
85
-
86
- #: Audit-log decisions that ALWAYS short-circuit a bulk action.
87
- TERMINAL_DECISIONS: frozenset[str] = frozenset({"accept", "reject", "mark-duplicate"})
88
-
89
- #: Audit-log decisions that short-circuit unless the operator opts in via
90
- #: ``--re-action``.
91
- IN_PROGRESS_DECISIONS: frozenset[str] = frozenset({"defer", "needs-ac"})
92
-
93
- #: ``owner/repo`` parser used to derive cache-layout segments.
94
- _REPO_RE: re.Pattern[str] = re.compile(
95
- r"^([A-Za-z0-9][A-Za-z0-9._-]*)/([A-Za-z0-9][A-Za-z0-9._-]*)$"
96
- )
97
-
98
- #: Cache source consumed by triage v1 (only github-issue is supported).
99
- _CACHE_SOURCE: str = "github-issue"
100
-
101
-
102
- class CacheEmptyError(RuntimeError):
103
- """Raised by :func:`bulk_action` when the per-repo cache is missing/empty."""
104
-
105
-
106
- def _parse_repo(repo: str) -> tuple[str, str]:
107
- """Validate ``owner/repo`` and return ``(owner, name)``."""
108
-
109
- if not isinstance(repo, str) or not repo:
110
- raise ValueError(
111
- f"repo must be a non-empty 'owner/name' string (got {repo!r})"
112
- )
113
- m = _REPO_RE.match(repo.strip())
114
- if not m:
115
- raise ValueError(
116
- f"invalid repo {repo!r}: expected 'owner/name' "
117
- "(alphanumerics, '.', '_', '-' only)"
118
- )
119
- return m.group(1), m.group(2)
120
-
121
-
122
- def _load_triage_actions() -> Any:
123
- """Lazy-import the Story 3 actions module."""
124
-
125
- for candidate in ("triage_actions", "scripts.triage_actions"):
126
- try:
127
- return importlib.import_module(candidate)
128
- except ModuleNotFoundError:
129
- continue
130
- raise RuntimeError(
131
- "triage_actions module not available -- Story 3 has not landed in "
132
- "this checkout. Install the cache+actions cohort or stub triage_actions "
133
- "in sys.modules before invoking bulk ops."
134
- )
135
-
136
-
137
- def _load_candidates_log() -> Any:
138
- """Lazy-import Story 2's :mod:`candidates_log` (for ``read_all``)."""
139
-
140
- for candidate in ("candidates_log", "scripts.candidates_log"):
141
- try:
142
- return importlib.import_module(candidate)
143
- except ModuleNotFoundError:
144
- continue
145
- raise RuntimeError(
146
- "candidates_log module not available -- cannot intersect the cached "
147
- "candidate set with the audit log."
148
- )
149
-
150
-
151
- def _load_cache_module() -> Any:
152
- """Lazy-import the unified cache module (#883 Story 2)."""
153
-
154
- for candidate in ("cache", "scripts.cache"):
155
- try:
156
- return importlib.import_module(candidate)
157
- except ModuleNotFoundError:
158
- continue
159
- raise RuntimeError(
160
- "cache module not available -- #883 Story 2 has not landed in this "
161
- "checkout. Cannot read the unified content cache without it."
162
- )
163
-
164
-
165
- def _cache_root(cache_root: Path | None) -> Path:
166
- return Path(cache_root) if cache_root is not None else Path(".deft-cache")
167
-
168
-
169
- def _iter_cache_keys(repo: str, *, cache_root: Path | None = None) -> list[str]:
170
- """Walk the cache layout and return canonical ``owner/repo/N`` keys.
171
-
172
- The unified layout is ``.deft-cache/github-issue/<owner>/<repo>/<N>/``;
173
- only directories whose name parses as a positive integer are surfaced
174
- so ad-hoc artefacts do not poison the candidate walk.
175
- """
176
-
177
- owner, name = _parse_repo(repo)
178
- base = _cache_root(cache_root) / _CACHE_SOURCE / owner / name
179
- if not base.is_dir():
180
- return []
181
- keys: list[str] = []
182
- for entry in sorted(base.iterdir(), key=lambda p: p.name):
183
- if not entry.is_dir():
184
- continue
185
- if not entry.name.isdigit():
186
- continue
187
- keys.append(f"{owner}/{name}/{entry.name}")
188
- return keys
189
-
190
-
191
- def list_cached_candidates(
192
- repo: str,
193
- *,
194
- cache_root: Path | None = None,
195
- cache_module: Any | None = None,
196
- out: Any | None = None,
197
- ) -> list[dict[str, Any]]:
198
- """Return parsed issue payloads sourced through ``cache:get``.
199
-
200
- For every key under the unified ``github-issue`` cache layout, we call
201
- :func:`scripts.cache.cache_get` (which validates ``meta.json`` against
202
- the schema) and re-load the per-entry ``raw.json`` to recover the
203
- original issue payload. Malformed / unreadable files are logged on
204
- ``out`` and skipped -- the bulk operation never aborts mid-walk on a
205
- single bad cache entry. Missing cache directory yields ``[]``.
206
- """
207
-
208
- sink = out if out is not None else sys.stderr
209
- cache_mod = cache_module if cache_module is not None else _load_cache_module()
210
- root = _cache_root(cache_root)
211
- keys = _iter_cache_keys(repo, cache_root=root)
212
-
213
- candidates: list[dict[str, Any]] = []
214
- not_found_exc = getattr(cache_mod, "CacheNotFoundError", LookupError)
215
- cache_error_exc = getattr(cache_mod, "CacheError", RuntimeError)
216
- validation_exc = getattr(cache_mod, "CacheValidationError", ValueError)
217
-
218
- for key in keys:
219
- try:
220
- result = cache_mod.cache_get(
221
- _CACHE_SOURCE, key, cache_root=root, allow_stale=True
222
- )
223
- except not_found_exc as exc: # type: ignore[misc]
224
- print(f"[triage:bulk] WARN: cache miss for {key}: {exc}", file=sink)
225
- continue
226
- except validation_exc as exc: # type: ignore[misc]
227
- print(
228
- f"[triage:bulk] WARN: invalid meta.json for {key}: {exc}",
229
- file=sink,
230
- )
231
- continue
232
- except cache_error_exc as exc: # type: ignore[misc]
233
- print(f"[triage:bulk] WARN: cache error for {key}: {exc}", file=sink)
234
- continue
235
-
236
- raw_path = Path(result.entry_dir) / "raw.json"
237
- try:
238
- raw_text = raw_path.read_text(encoding="utf-8")
239
- except (OSError, UnicodeDecodeError) as exc:
240
- print(
241
- f"[triage:bulk] WARN: skipping unreadable raw.json for {key}: "
242
- f"{type(exc).__name__}: {exc}",
243
- file=sink,
244
- )
245
- continue
246
- try:
247
- payload = json.loads(raw_text)
248
- except json.JSONDecodeError as exc:
249
- print(
250
- f"[triage:bulk] WARN: skipping malformed raw.json for {key}: {exc}",
251
- file=sink,
252
- )
253
- continue
254
- if not isinstance(payload, dict):
255
- print(
256
- f"[triage:bulk] WARN: skipping non-object raw.json for {key} "
257
- f"(got {type(payload).__name__})",
258
- file=sink,
259
- )
260
- continue
261
- candidates.append(payload)
262
- return candidates
263
-
264
-
265
- def _filter_issues(
266
- issues: Iterable[dict[str, Any]],
267
- *,
268
- label: str | None = None,
269
- author: str | None = None,
270
- age_days: int | None = None,
271
- cluster: str | None = None,
272
- now: datetime | None = None,
273
- ) -> list[dict[str, Any]]:
274
- """Apply combinable filters with AND semantics."""
275
-
276
- now = now or datetime.now(UTC)
277
- cutoff: datetime | None = None
278
- if age_days is not None:
279
- cutoff = now - timedelta(days=age_days)
280
-
281
- matched: list[dict[str, Any]] = []
282
- for issue in issues:
283
- labels = [
284
- entry.get("name")
285
- for entry in issue.get("labels", []) or []
286
- if isinstance(entry, dict)
287
- ]
288
-
289
- if label is not None and label not in labels:
290
- continue
291
-
292
- if author is not None:
293
- actor = issue.get("author") or {}
294
- login = actor.get("login") if isinstance(actor, dict) else None
295
- if login != author:
296
- continue
297
-
298
- if cutoff is not None:
299
- created_raw = issue.get("createdAt")
300
- if not created_raw:
301
- continue
302
- try:
303
- created_at = datetime.fromisoformat(
304
- str(created_raw).replace("Z", "+00:00")
305
- )
306
- except ValueError:
307
- continue
308
- if created_at > cutoff:
309
- continue
310
-
311
- if cluster is not None:
312
- cluster_label = f"cluster:{cluster}"
313
- if not any(name in (cluster_label, cluster) for name in labels):
314
- continue
315
-
316
- matched.append(issue)
317
- return matched
318
-
319
-
320
- def _build_skip_set(re_action: bool) -> frozenset[str]:
321
- """Return the set of latest-decision values that disqualify a candidate."""
322
-
323
- if re_action:
324
- return TERMINAL_DECISIONS
325
- return TERMINAL_DECISIONS | IN_PROGRESS_DECISIONS
326
-
327
-
328
- def _latest_decision_by_issue(
329
- repo: str, *, candidates_log_module: Any | None = None
330
- ) -> dict[int, dict[str, Any]]:
331
- """Return ``{issue_number: latest-entry-dict}`` for ``repo``."""
332
-
333
- module = (
334
- candidates_log_module
335
- if candidates_log_module is not None
336
- else _load_candidates_log()
337
- )
338
- read_all = getattr(module, "read_all", None)
339
- if not callable(read_all):
340
- raise RuntimeError(
341
- "candidates_log.read_all not callable (Story 2 contract violated)"
342
- )
343
-
344
- latest: dict[int, dict[str, Any]] = {}
345
- for entry in read_all(repo=repo):
346
- if not isinstance(entry, dict):
347
- continue
348
- n = entry.get("issue_number")
349
- if not isinstance(n, int) or isinstance(n, bool):
350
- continue
351
- ts = str(entry.get("timestamp", ""))
352
- prior = latest.get(n)
353
- if prior is None or ts > str(prior.get("timestamp", "")):
354
- latest[n] = entry
355
- return latest
356
-
357
-
358
- def _exclude_logged(
359
- candidates: Iterable[dict[str, Any]],
360
- *,
361
- repo: str,
362
- re_action: bool,
363
- candidates_log_module: Any | None = None,
364
- out: Any | None = None,
365
- ) -> list[dict[str, Any]]:
366
- """Drop candidates whose latest audit decision is in the skip set."""
367
-
368
- skip_set = _build_skip_set(re_action)
369
- latest = _latest_decision_by_issue(
370
- repo, candidates_log_module=candidates_log_module
371
- )
372
-
373
- kept: list[dict[str, Any]] = []
374
- skipped = 0
375
- for issue in candidates:
376
- try:
377
- n = int(issue["number"])
378
- except (KeyError, TypeError, ValueError):
379
- kept.append(issue)
380
- continue
381
- prior = latest.get(n)
382
- if prior is None:
383
- kept.append(issue)
384
- continue
385
- if str(prior.get("decision", "")) in skip_set:
386
- skipped += 1
387
- continue
388
- kept.append(issue)
389
-
390
- if skipped:
391
- msg = (
392
- f"[triage:bulk] skipped {skipped} candidate(s) with prior "
393
- "audit-log records"
394
- )
395
- if not re_action:
396
- msg += " (pass --re-action to override defer/needs-ac records)"
397
- sink = out if out is not None else sys.stderr
398
- print(msg, file=sink)
399
- return kept
400
-
401
-
402
- def _resolve_action(actions_module: Any, action_key: str) -> Callable[..., Any]:
403
- fn_name = ACTION_FN_NAMES[action_key]
404
- fn = getattr(actions_module, fn_name, None)
405
- if not callable(fn):
406
- raise RuntimeError(
407
- f"triage_actions.{fn_name} not found (Story 3 contract violated)"
408
- )
409
- return fn # type: ignore[no-any-return]
410
-
411
-
412
- _SIGNATURE_TYPEERROR_TOKENS = (
413
- "unexpected keyword argument",
414
- "got multiple values for",
415
- "missing 1 required positional argument",
416
- "takes 2 positional arguments",
417
- "takes 3 positional arguments",
418
- )
419
-
420
-
421
- def _is_signature_mismatch(exc: TypeError) -> bool:
422
- """True if a ``TypeError`` looks like it came from the *call site*."""
423
-
424
- msg = str(exc)
425
- return any(token in msg for token in _SIGNATURE_TYPEERROR_TOKENS)
426
-
427
-
428
- def _invoke_action(
429
- fn: Callable[..., Any],
430
- issue_number: int,
431
- repo: str,
432
- *,
433
- action_key: str,
434
- reason: str | None,
435
- ) -> None:
436
- """Call a Story 3 single-issue action with kwargs, falling back to positional."""
437
-
438
- kwargs: dict[str, Any] = {}
439
- if action_key == "reject" and reason is not None:
440
- kwargs["reason"] = reason
441
- try:
442
- fn(issue_number, repo, **kwargs)
443
- except TypeError as exc:
444
- if not _is_signature_mismatch(exc):
445
- raise
446
- if action_key == "reject" and reason is not None:
447
- fn(issue_number, repo, reason)
448
- else:
449
- fn(issue_number, repo)
450
-
451
-
452
- def bulk_action(
453
- action_key: str,
454
- repo: str,
455
- *,
456
- label: str | None = None,
457
- author: str | None = None,
458
- age_days: int | None = None,
459
- cluster: str | None = None,
460
- reason: str | None = None,
461
- re_action: bool = False,
462
- cache_root: Path | None = None,
463
- actions_module: Any | None = None,
464
- cache_module: Any | None = None,
465
- candidates_log_module: Any | None = None,
466
- issues_provider: Callable[[str], list[dict[str, Any]]] | None = None,
467
- now: datetime | None = None,
468
- out: Any | None = None,
469
- ) -> int:
470
- """Execute ``action_key`` over the filtered candidate set."""
471
-
472
- if action_key not in ACTION_FN_NAMES:
473
- raise ValueError(f"Unknown bulk action: {action_key!r}")
474
-
475
- sink = out or sys.stdout
476
- if issues_provider is not None:
477
- candidates = issues_provider(repo)
478
- else:
479
- candidates = list_cached_candidates(
480
- repo,
481
- cache_root=cache_root,
482
- cache_module=cache_module,
483
- out=sink,
484
- )
485
-
486
- if not candidates:
487
- raise CacheEmptyError(
488
- f"triage_bulk: cache is empty for {repo}; "
489
- "run `task triage:bootstrap` first."
490
- )
491
-
492
- matched = _filter_issues(
493
- candidates,
494
- label=label,
495
- author=author,
496
- age_days=age_days,
497
- cluster=cluster,
498
- now=now,
499
- )
500
-
501
- matched = _exclude_logged(
502
- matched,
503
- repo=repo,
504
- re_action=re_action,
505
- candidates_log_module=candidates_log_module,
506
- out=sink,
507
- )
508
-
509
- if not matched:
510
- print(
511
- f"[triage:bulk-{action_key}] zero matches for given filters",
512
- file=sink,
513
- )
514
- return 0
515
-
516
- module = actions_module if actions_module is not None else _load_triage_actions()
517
- fn = _resolve_action(module, action_key)
518
-
519
- actioned = 0
520
- for issue in matched:
521
- try:
522
- issue_number = int(issue["number"])
523
- except (KeyError, TypeError, ValueError):
524
- print(
525
- f"[triage:bulk-{action_key}] skipping malformed issue entry: "
526
- f"{issue!r}",
527
- file=sink,
528
- )
529
- continue
530
- _invoke_action(fn, issue_number, repo, action_key=action_key, reason=reason)
531
- actioned += 1
532
- print(f"[triage:bulk-{action_key}] #{issue_number} actioned", file=sink)
533
-
534
- print(f"[triage:bulk-{action_key}] total: {actioned}", file=sink)
535
- return actioned
536
-
537
-
538
- def _build_parser() -> argparse.ArgumentParser:
539
- parser = argparse.ArgumentParser(
540
- prog="triage_bulk",
541
- description=(
542
- "Bulk triage operations over the unified cache (#845 Story 4 "
543
- "/ #883 Story 3 rebind)"
544
- ),
545
- )
546
- parser.add_argument(
547
- "action",
548
- choices=list(ACTION_FN_NAMES.keys()),
549
- help="bulk action to apply (accept|reject|defer|needs-ac)",
550
- )
551
- parser.add_argument("--repo", required=True, help="GitHub repo, owner/name")
552
- parser.add_argument(
553
- "--label", default=None, help="filter: only issues carrying this label"
554
- )
555
- parser.add_argument(
556
- "--author",
557
- default=None,
558
- help="filter: only issues authored by this GitHub login",
559
- )
560
- parser.add_argument(
561
- "--age-days",
562
- type=int,
563
- default=None,
564
- help="filter: only issues older than N days (createdAt threshold)",
565
- )
566
- parser.add_argument(
567
- "--cluster",
568
- default=None,
569
- help="filter: only issues tagged with cluster:<slug> or bare <slug> label",
570
- )
571
- parser.add_argument(
572
- "--reason",
573
- default=None,
574
- help="reject only: reason recorded in audit log + upstream issue close comment",
575
- )
576
- parser.add_argument(
577
- "--re-action",
578
- action="store_true",
579
- dest="re_action",
580
- help=(
581
- "Re-action candidates whose LATEST audit-log record is `defer` or "
582
- "`needs-ac` (#915). Without this flag, in-progress records "
583
- "short-circuit the bulk run; terminal records "
584
- "(accept|reject|mark-duplicate) ALWAYS short-circuit regardless."
585
- ),
586
- )
587
- return parser
588
-
589
-
590
- def _reconfigure_utf8() -> None:
591
- """Best-effort UTF-8 stdout/stderr on Windows hosts (mirrors #814)."""
592
-
593
- if sys.platform != "win32":
594
- return
595
- for stream_name in ("stdout", "stderr"):
596
- stream = getattr(sys, stream_name, None)
597
- reconfigure = getattr(stream, "reconfigure", None)
598
- if callable(reconfigure):
599
- with contextlib.suppress(Exception):
600
- reconfigure(encoding="utf-8")
601
-
602
-
603
- def main(argv: list[str] | None = None) -> int:
604
- _reconfigure_utf8()
605
- # N10 (#1150): structured --help via scripts/triage_help.REGISTRY.
606
- from triage_help import intercept_help
607
-
608
- rc = intercept_help("triage_bulk", argv)
609
- if rc is not None:
610
- return rc
611
- args = _build_parser().parse_args(argv)
612
- try:
613
- bulk_action(
614
- args.action,
615
- args.repo,
616
- label=args.label,
617
- author=args.author,
618
- age_days=args.age_days,
619
- cluster=args.cluster,
620
- reason=args.reason,
621
- re_action=args.re_action,
622
- )
623
- except CacheEmptyError as exc:
624
- print(str(exc), file=sys.stderr)
625
- return 2
626
- return 0
627
-
628
-
629
- if __name__ == "__main__":
630
- sys.exit(main())