@deftai/directive-content 0.55.1 → 0.56.0

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