mandrel 1.88.0 → 1.90.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 (145) hide show
  1. package/.agents/README.md +18 -13
  2. package/.agents/audit-checklists/architecture.md +24 -0
  3. package/.agents/audit-checklists/clean-code.md +24 -0
  4. package/.agents/audit-checklists/dependencies.md +14 -0
  5. package/.agents/audit-checklists/devops.md +17 -0
  6. package/.agents/audit-checklists/documentation.md +22 -0
  7. package/.agents/audit-checklists/lighthouse.md +15 -0
  8. package/.agents/audit-checklists/navigability.md +14 -0
  9. package/.agents/audit-checklists/performance.md +22 -0
  10. package/.agents/audit-checklists/privacy.md +21 -0
  11. package/.agents/audit-checklists/quality.md +18 -0
  12. package/.agents/audit-checklists/security.md +22 -0
  13. package/.agents/audit-checklists/seo.md +16 -0
  14. package/.agents/audit-checklists/sre.md +24 -0
  15. package/.agents/audit-checklists/ux-ui.md +21 -0
  16. package/.agents/docs/SDLC.md +62 -27
  17. package/.agents/docs/configuration.md +5 -4
  18. package/.agents/instructions.md +51 -21
  19. package/.agents/personas/architect.md +10 -7
  20. package/.agents/personas/engineer.md +4 -3
  21. package/.agents/personas/project-manager.md +5 -2
  22. package/.agents/personas/refactorer.md +5 -3
  23. package/.agents/rules/git-conventions.md +77 -0
  24. package/.agents/schemas/agentrc.schema.json +10 -6
  25. package/.agents/schemas/audit-rules.json +16 -2
  26. package/.agents/schemas/audit-rules.schema.json +7 -6
  27. package/.agents/schemas/lifecycle/epic.blocked.schema.json +1 -1
  28. package/.agents/schemas/lifecycle/merge.unlanded.schema.json +39 -0
  29. package/.agents/schemas/signal-event.schema.json +28 -13
  30. package/.agents/scripts/acceptance-spec-reconciler.js +6 -4
  31. package/.agents/scripts/check-context-budget.js +320 -0
  32. package/.agents/scripts/coverage-capture.js +17 -0
  33. package/.agents/scripts/diagnose-friction.js +4 -4
  34. package/.agents/scripts/epic-audit-prepare.js +30 -2
  35. package/.agents/scripts/epic-audit-recheck.js +46 -13
  36. package/.agents/scripts/epic-deliver-prepare.js +80 -8
  37. package/.agents/scripts/epic-plan-spec.js +4 -8
  38. package/.agents/scripts/generate-lens-checklists.js +180 -0
  39. package/.agents/scripts/lib/audit-suite/checklist-threading.js +300 -0
  40. package/.agents/scripts/lib/audit-suite/findings.js +27 -0
  41. package/.agents/scripts/lib/audit-suite/index.js +9 -0
  42. package/.agents/scripts/lib/audit-suite/lens-checklist.js +212 -0
  43. package/.agents/scripts/lib/audit-suite/selector.js +136 -5
  44. package/.agents/scripts/lib/checks/loop-health.js +340 -0
  45. package/.agents/scripts/lib/cli-args.js +8 -0
  46. package/.agents/scripts/lib/close-validation/gates.js +64 -24
  47. package/.agents/scripts/lib/config/ci.js +12 -1
  48. package/.agents/scripts/lib/config/runners.js +13 -5
  49. package/.agents/scripts/lib/config/temp-paths.js +24 -0
  50. package/.agents/scripts/lib/config-settings-schema-delivery.js +28 -8
  51. package/.agents/scripts/lib/doc-tiers.js +291 -0
  52. package/.agents/scripts/lib/epic-body-sections.js +5 -2
  53. package/.agents/scripts/lib/epic-merge-lock.js +83 -0
  54. package/.agents/scripts/lib/epic-plan-clarity.js +3 -1
  55. package/.agents/scripts/lib/feedback-loop/audit-results-graduator.js +47 -15
  56. package/.agents/scripts/lib/feedback-loop/graduator-core.js +395 -86
  57. package/.agents/scripts/lib/feedback-loop/memory-freshness.js +299 -72
  58. package/.agents/scripts/lib/feedback-loop/retro-proposals-graduator.js +438 -0
  59. package/.agents/scripts/lib/gates/friction.js +15 -5
  60. package/.agents/scripts/lib/npm-scripts.js +55 -0
  61. package/.agents/scripts/lib/observability/perf-aggregator.js +30 -104
  62. package/.agents/scripts/lib/observability/perf-report-readers.js +1 -1
  63. package/.agents/scripts/lib/observability/signal-validator.js +204 -0
  64. package/.agents/scripts/lib/observability/signals-writer.js +157 -54
  65. package/.agents/scripts/lib/observability/tool-trace-hook.js +42 -4
  66. package/.agents/scripts/lib/orchestration/acceptance-eval-decision.js +1 -1
  67. package/.agents/scripts/lib/orchestration/code-review.js +74 -4
  68. package/.agents/scripts/lib/orchestration/consolidation-precondition.js +213 -0
  69. package/.agents/scripts/lib/orchestration/doc-reader.js +4 -96
  70. package/.agents/scripts/lib/orchestration/docs-digest.js +34 -0
  71. package/.agents/scripts/lib/orchestration/epic-plan-spec/phases/authoring-context.js +56 -19
  72. package/.agents/scripts/lib/orchestration/epic-plan-spec/phases/run-spec-phase.js +22 -0
  73. package/.agents/scripts/lib/orchestration/lifecycle/emit-merge-unlanded.js +193 -0
  74. package/.agents/scripts/lib/orchestration/lifecycle/listeners/README.md +6 -0
  75. package/.agents/scripts/lib/orchestration/lifecycle/listeners/automerge-armer.js +248 -13
  76. package/.agents/scripts/lib/orchestration/lifecycle/listeners/automerge-predicate.js +109 -12
  77. package/.agents/scripts/lib/orchestration/lifecycle/listeners/finalizer.js +47 -61
  78. package/.agents/scripts/lib/orchestration/lifecycle/listeners/index.js +46 -4
  79. package/.agents/scripts/lib/orchestration/lifecycle/listeners/label-transitioner.js +144 -0
  80. package/.agents/scripts/lib/orchestration/lifecycle/listeners/merge-watcher.js +258 -14
  81. package/.agents/scripts/lib/orchestration/lifecycle/listeners/notify-dispatcher.js +6 -0
  82. package/.agents/scripts/lib/orchestration/merge-block-class.js +246 -0
  83. package/.agents/scripts/lib/orchestration/plan-review-routing.js +1 -1
  84. package/.agents/scripts/lib/orchestration/post-merge/phases/worktree-reap.js +3 -3
  85. package/.agents/scripts/lib/orchestration/retro/phases/compose-body.js +63 -34
  86. package/.agents/scripts/lib/orchestration/retro/phases/gather-signals.js +167 -52
  87. package/.agents/scripts/lib/orchestration/retro/phases/post-and-mirror.js +49 -2
  88. package/.agents/scripts/lib/orchestration/retro-proposals.js +12 -55
  89. package/.agents/scripts/lib/orchestration/retro-runner.js +9 -0
  90. package/.agents/scripts/lib/orchestration/single-story-close/phases/close-validation.js +5 -1
  91. package/.agents/scripts/lib/orchestration/single-story-close/phases/code-review.js +8 -0
  92. package/.agents/scripts/lib/orchestration/single-story-close/phases/confirm-merge.js +419 -0
  93. package/.agents/scripts/lib/orchestration/single-story-close/phases/options.js +35 -2
  94. package/.agents/scripts/lib/orchestration/single-story-close/phases/wrong-tree-guard.js +353 -69
  95. package/.agents/scripts/lib/orchestration/single-story-close/runner.js +66 -4
  96. package/.agents/scripts/lib/orchestration/spec-section-validator.js +60 -9
  97. package/.agents/scripts/lib/orchestration/story-close/auto-refresh-runner.js +7 -5
  98. package/.agents/scripts/lib/orchestration/story-close/merge-runner.js +24 -2
  99. package/.agents/scripts/lib/orchestration/story-close/phases/code-review.js +167 -8
  100. package/.agents/scripts/lib/orchestration/story-close/pre-merge-validation.js +8 -1
  101. package/.agents/scripts/lib/orchestration/story-close/shared-checkout-guard.js +163 -0
  102. package/.agents/scripts/lib/orchestration/ticketing/reads.js +20 -9
  103. package/.agents/scripts/lib/planning-corpus.js +306 -0
  104. package/.agents/scripts/lib/signals/detectors/common.js +10 -10
  105. package/.agents/scripts/lib/signals/detectors/index.js +4 -4
  106. package/.agents/scripts/lib/signals/detectors/retry.js +19 -18
  107. package/.agents/scripts/lib/signals/detectors/rework.js +1 -1
  108. package/.agents/scripts/lib/signals/schema.js +56 -81
  109. package/.agents/scripts/lib/signals/span-tree.js +6 -5
  110. package/.agents/scripts/lib/story-plan.js +3 -0
  111. package/.agents/scripts/lib/wave-runner/tick.js +10 -2
  112. package/.agents/scripts/lifecycle-emit.js +39 -8
  113. package/.agents/scripts/providers/github/issues.js +12 -1
  114. package/.agents/scripts/resolve-doc-tiers.js +83 -0
  115. package/.agents/scripts/retro-run.js +51 -0
  116. package/.agents/scripts/signals-view.js +1 -1
  117. package/.agents/scripts/single-story-close.js +20 -1
  118. package/.agents/scripts/standalone-feedback-rollup.js +188 -0
  119. package/.agents/scripts/story-close.js +48 -0
  120. package/.agents/scripts/story-plan.js +51 -12
  121. package/.agents/scripts/validate-docs-freshness.js +69 -15
  122. package/.agents/skills/core/documentation-and-adrs/SKILL.md +58 -0
  123. package/.agents/skills/core/epic-plan-decompose-author/SKILL.md +5 -3
  124. package/.agents/skills/core/epic-plan-spec-author/SKILL.md +20 -7
  125. package/.agents/skills/core/scope-triage/SKILL.md +61 -0
  126. package/.agents/skills/skills.index.json +3 -3
  127. package/.agents/workflows/audit-documentation.md +82 -2
  128. package/.agents/workflows/helpers/code-review.md +116 -43
  129. package/.agents/workflows/helpers/deliver-epic.md +123 -54
  130. package/.agents/workflows/helpers/deliver-stories.md +26 -0
  131. package/.agents/workflows/helpers/epic-audit.md +116 -366
  132. package/.agents/workflows/helpers/epic-deliver-story.md +14 -0
  133. package/.agents/workflows/helpers/epic-plan-decompose.md +18 -200
  134. package/.agents/workflows/helpers/epic-plan-spec.md +18 -180
  135. package/.agents/workflows/helpers/plan-epic.md +141 -105
  136. package/.agents/workflows/helpers/plan-story.md +32 -0
  137. package/.agents/workflows/helpers/single-story-deliver.md +43 -0
  138. package/.agents/workflows/loops/nightly-audit.md +9 -7
  139. package/docs/CHANGELOG.md +29 -0
  140. package/lib/cli/doctor.js +44 -0
  141. package/package.json +4 -3
  142. package/.agents/scripts/epic-plan-spec-validate.js +0 -111
  143. package/.agents/scripts/lib/feedback-loop/code-review-graduator.js +0 -224
  144. package/.agents/scripts/lib/orchestration/epic-plan-spec/phases/prompts.js +0 -58
  145. package/.agents/scripts/lib/signals/detectors/hotspot.js +0 -292
@@ -0,0 +1,163 @@
1
+ /**
2
+ * shared-checkout-guard.js — cross-epic contention guard for the merge
3
+ * phase's `git checkout <epicBranch>` in the shared main checkout
4
+ * (Story #4460).
5
+ *
6
+ * `story-close.js`'s merge phase (`runFinalizeMerge` in `merge-runner.js`)
7
+ * runs `git checkout <epicBranch>` directly in the shared main repo
8
+ * checkout — `close-inputs.js` resolves `cwd` to `PROJECT_ROOT`, not an
9
+ * isolated worktree. The only exclusivity guard around that shared
10
+ * checkout is the per-Epic `epic-merge-lock.js` mutex, which only
11
+ * serializes concurrent runs for the SAME epic. Nothing stops a
12
+ * DIFFERENT epic's concurrently-running `story-close.js` from treating
13
+ * the same shared checkout as scratch space at the same time.
14
+ *
15
+ * This was observed live: while delivering Epic #4425 Stories #4427/#4428,
16
+ * the shared checkout repeatedly carried uncommitted stray changes
17
+ * belonging to a concurrently-running Epic #4405 delivery (parked on
18
+ * `epic/4405` with dirty edits), which blocked the `git checkout epic/4425`
19
+ * merge step with a raw `error: Your local changes ... would be
20
+ * overwritten by checkout`.
21
+ *
22
+ * `assertSharedCheckoutAvailable` runs immediately before that checkout
23
+ * and fails fast with an actionable, story-close-specific diagnostic
24
+ * instead of letting the raw git error surface. It COMPOSES with (does
25
+ * not replace) the per-Epic lock: by the time this guard runs, the
26
+ * caller's own epic lock is already held (acquired around the whole
27
+ * close flow in `story-close.js`), so this guard only inspects OTHER
28
+ * epics' lock files plus the tree's overall dirty state — it never
29
+ * contends with same-epic concurrent runs, which continue to serialize
30
+ * solely via `withEpicMergeLock` before this guard ever executes.
31
+ *
32
+ * Breadth trade-off (deliberate): the foreign-lock probe keys on the
33
+ * OTHER epic's per-epic merge lock, which that run holds for its WHOLE
34
+ * close flow — so any overlapping story-close of another epic trips this
35
+ * guard even when that run never actually touches the shared checkout
36
+ * during the overlap. The refusal is deterministic and loud (throws with
37
+ * a diagnostic naming the holder pid), never a deadlock (no waiting),
38
+ * and stale/dead-pid foreign locks are ignored via the pid-liveness
39
+ * probe. Narrowing the window (a dedicated checkout-phase lock) was
40
+ * considered and rejected: the coarse refusal is rare, cheap to retry,
41
+ * and far simpler than a second lock tier.
42
+ */
43
+
44
+ import { findForeignActiveEpicLock as defaultFindForeignActiveEpicLock } from '../../epic-merge-lock.js';
45
+ import { gitSpawn as defaultGitSpawn } from '../../git-utils.js';
46
+
47
+ const MAX_LISTED_DIRTY_FILES = 20;
48
+
49
+ function describeForeignLock(foreign) {
50
+ const acquired = Number.isFinite(foreign.acquiredAt)
51
+ ? new Date(foreign.acquiredAt).toISOString()
52
+ : 'an unknown time';
53
+ return (
54
+ `[story-close] shared-checkout guard: refusing to touch the shared main checkout — ` +
55
+ `it is currently held by epic #${foreign.epicId}'s story-close merge phase ` +
56
+ `(lock ${foreign.filePath}, pid ${foreign.pid}, acquired ${acquired}). ` +
57
+ `Wait for that epic's story-close run to finish before retrying this merge. ` +
58
+ `If you have independently confirmed that process is no longer running, remove ` +
59
+ `the lock file by hand — never force a checkout past a live foreign lock. ` +
60
+ `See .agents/rules/git-conventions.md § Shared-checkout contention (Story #4460).`
61
+ );
62
+ }
63
+
64
+ function listDirtyFiles(porcelainOutput) {
65
+ // `git status --porcelain` lines are `XY <path>` — a fixed 2-char status
66
+ // column, a space, then the path. Strip only a trailing `\r` (Windows)
67
+ // before slicing off that 3-char prefix; trimming the whole line first
68
+ // would shift the slice offset whenever the status column starts with a
69
+ // space (the common "unstaged modification" case), truncating the path.
70
+ const lines = porcelainOutput
71
+ .split('\n')
72
+ .map((line) => line.replace(/\r$/, ''))
73
+ .filter((line) => line.length > 0);
74
+ const shown = lines
75
+ .slice(0, MAX_LISTED_DIRTY_FILES)
76
+ .map((line) => line.slice(3).trim() || line);
77
+ const overflow = lines.length - shown.length;
78
+ return overflow > 0
79
+ ? `${shown.join(', ')}, … (+${overflow} more)`
80
+ : shown.join(', ');
81
+ }
82
+
83
+ function describeDirtyCheckout({ cwd, epicId, currentBranch, dirtyFiles }) {
84
+ return (
85
+ `[story-close] shared-checkout guard: refusing to check out the epic branch for ` +
86
+ `epic #${epicId} — the shared main checkout at ${cwd} is dirty (currently on ` +
87
+ `\`${currentBranch}\`). Dirty files: ${dirtyFiles}. This usually means another ` +
88
+ `epic's story-close run left uncommitted work in the shared checkout, or a prior ` +
89
+ `run crashed mid-merge. Resolve manually (stash/commit/reset in ${cwd}) before ` +
90
+ `retrying. See .agents/rules/git-conventions.md § Shared-checkout contention ` +
91
+ `(Story #4460).`
92
+ );
93
+ }
94
+
95
+ /**
96
+ * Fail fast when the shared main checkout is not safely available for this
97
+ * epic's merge-phase `git checkout <epicBranch>` — either because another
98
+ * epic's story-close merge phase currently holds it (a live foreign lock),
99
+ * or because it is simply dirty (regardless of whose branch is checked
100
+ * out). Silent no-op when the checkout is clean and uncontended.
101
+ *
102
+ * @param {{
103
+ * cwd: string,
104
+ * epicId: number|string,
105
+ * gitSpawn?: typeof defaultGitSpawn,
106
+ * findForeignActiveEpicLock?: typeof defaultFindForeignActiveEpicLock,
107
+ * }} opts
108
+ * @throws {Error} with an actionable, story-close-specific diagnostic.
109
+ */
110
+ /**
111
+ * Foreign-lock-only variant for the RESUME merge path (Story #4460
112
+ * follow-up): a resume legitimately re-enters a shared checkout that is
113
+ * dirty with THIS story's own in-progress merge, so the dirty-tree half
114
+ * of `assertSharedCheckoutAvailable` would false-positive there. The
115
+ * cross-epic hazard — another epic's live merge phase holding the
116
+ * checkout — still applies and is the only probe this variant runs.
117
+ */
118
+ export function assertNoForeignEpicLock({
119
+ cwd,
120
+ epicId,
121
+ findForeignActiveEpicLock = defaultFindForeignActiveEpicLock,
122
+ }) {
123
+ const foreign = findForeignActiveEpicLock(epicId, { repoRoot: cwd });
124
+ if (foreign) {
125
+ throw new Error(describeForeignLock(foreign));
126
+ }
127
+ }
128
+
129
+ export function assertSharedCheckoutAvailable({
130
+ cwd,
131
+ epicId,
132
+ gitSpawn = defaultGitSpawn,
133
+ findForeignActiveEpicLock = defaultFindForeignActiveEpicLock,
134
+ }) {
135
+ const foreign = findForeignActiveEpicLock(epicId, { repoRoot: cwd });
136
+ if (foreign) {
137
+ throw new Error(describeForeignLock(foreign));
138
+ }
139
+
140
+ const statusRes = gitSpawn(cwd, 'status', '--porcelain');
141
+ if (statusRes.status !== 0) {
142
+ // Can't determine dirtiness from here — let the downstream checkout
143
+ // surface whatever git itself reports rather than guessing.
144
+ return;
145
+ }
146
+ const porcelain = statusRes.stdout || '';
147
+ if (porcelain.trim().length === 0) return;
148
+
149
+ const branchRes = gitSpawn(cwd, 'rev-parse', '--abbrev-ref', 'HEAD');
150
+ const currentBranch =
151
+ branchRes.status === 0
152
+ ? (branchRes.stdout || '').trim() || 'unknown'
153
+ : 'unknown';
154
+
155
+ throw new Error(
156
+ describeDirtyCheckout({
157
+ cwd,
158
+ epicId,
159
+ currentBranch,
160
+ dirtyFiles: listDirtyFiles(porcelain),
161
+ }),
162
+ );
163
+ }
@@ -59,7 +59,17 @@ export const STRUCTURED_COMMENT_TYPES = Object.freeze([
59
59
  'friction',
60
60
  'notification',
61
61
  // Extended set (Story #449 — retro follow-ons)
62
- 'code-review',
62
+ // Story #4411 (Epic #4405) — the former `code-review` structured comment
63
+ // is unified with the former `audit-results` comment into the single
64
+ // `verification-results` findings contract. `runCodeReview` (the sole code
65
+ // producer) upserts `verification-results`; the feedback-loop graduators and
66
+ // the auto-merge integration gate read it. Both the `code-review` and (as of
67
+ // Story #4412's slim-Epic-close cutover) the `audit-results` markers are
68
+ // retired here — the Phase 4 standalone lens walk folded into the Phase 5
69
+ // code-review pass, whose single `verification-results` comment now carries
70
+ // the Epic-close lens findings. Hard cutover, no dual-shape reader per
71
+ // `git-conventions.md`.
72
+ 'verification-results',
63
73
  'retro',
64
74
  'retro-partial',
65
75
  'epic-run-state',
@@ -98,14 +108,6 @@ export const STRUCTURED_COMMENT_TYPES = Object.freeze([
98
108
  // operator can correct drift before Phase 8 decomposes from a stale
99
109
  // spec. Advisory: the run continues regardless of the report contents.
100
110
  'spec-freshness',
101
- // Story #2681 — `/deliver` Phase 4 epic-audit helper upserts an
102
- // `audit-results` comment on the Epic listing the per-lens findings
103
- // returned by the change-set audit pass. The marker was prescribed by
104
- // `helpers/epic-audit.md` Step 4 long before it was added to this
105
- // registry; without the entry the helper's `post-structured-comment.js`
106
- // invocation always failed with "Invalid structured-comment type". One
107
- // entry per Epic; re-runs replace prior content.
108
- 'audit-results',
109
111
  // Story #2813 — the per-Task progress writer (since retired under
110
112
  // #3157) upserted a `model-attribution` comment on a Task ticket at
111
113
  // the moment it transitioned to `agent::executing`, recording which
@@ -160,6 +162,15 @@ export const STRUCTURED_COMMENT_TYPES = Object.freeze([
160
162
  // refuses with the claim age. One entry per Epic; re-acquires upsert
161
163
  // in place.
162
164
  'plan-lease',
165
+ // Story #4415 (Epic #4406) — the feedback-loop graduators
166
+ // (`audit-results-graduator.js` / `retro-proposals-graduator.js`) upsert a
167
+ // `cross-repo-deferred` comment on the Epic listing findings that route
168
+ // to a different repository and were therefore not filed here. Replaces
169
+ // the prior log-line-only trace so the deferral survives the finalize
170
+ // run as a durable, operator-visible record. Discriminated by a
171
+ // `graduator="audit-results|code-review"` attr so the two graduators
172
+ // upsert independent comments; re-runs upsert in place.
173
+ 'cross-repo-deferred',
163
174
  ]);
164
175
 
165
176
  export const WAVE_TYPE_PATTERN = WAVE_MARKER_RE;
@@ -0,0 +1,306 @@
1
+ /**
2
+ * planning-corpus.js — corpus-aware context for the standalone-Story
3
+ * planning path (Story #4432).
4
+ *
5
+ * `/plan --idea` previously drafted a standalone Story from a blank
6
+ * slate: the seed, the body template, and a title-only duplicate scan.
7
+ * For a change request that is really a small delta against an
8
+ * already-delivered surface, that blank slate throws away context the
9
+ * project already has — the docs digest and the relevant Tech Spec
10
+ * sections of existing Epics that cover the touched area.
11
+ *
12
+ * This module assembles that inherited context (`corpusContext`) for
13
+ * `story-plan.js`'s `--emit-context` envelope:
14
+ *
15
+ * 1. `docsDigest` — the same per-project docs digest
16
+ * `orchestration/docs-digest.js` builds for `/deliver` Story
17
+ * children, reused here so the standalone path gets the same
18
+ * compact outline instead of re-reading the whole docs set.
19
+ * `null` when `project.docsContextFiles` is not configured.
20
+ * 2. `relevantSections` — a ranked list of existing Epic Tech Spec
21
+ * (or lede, when no Tech Spec region exists) excerpts that overlap
22
+ * with the seed, so the draft can build on prior art instead of
23
+ * re-deriving it.
24
+ *
25
+ * The Epic list surface (`provider.getEpics`) maps every issue through
26
+ * `issueToEpicListItem`, which deliberately omits `body` (a list-scale
27
+ * payload trim). Body content therefore requires an **explicit**,
28
+ * bounded per-candidate fetch via `provider.getEpic(id)` — never a
29
+ * silent assumption that the list response carries prose to score
30
+ * against.
31
+ *
32
+ * Relevance scoring reuses the same `tokenize` / `overlapScore` Jaccard
33
+ * primitives `duplicate-search.js` exports for Epic-dedupe and
34
+ * `story-plan.js` reuses for Story-dedupe — one matcher, three
35
+ * consumers, no forked scoring logic.
36
+ */
37
+
38
+ import { overlapScore, tokenize } from './duplicate-search.js';
39
+ import { extractEpicSection, hasEpicSection } from './epic-body-sections.js';
40
+ import { Logger } from './Logger.js';
41
+ import { buildDocsDigest } from './orchestration/docs-digest.js';
42
+
43
+ /** Top-K Epics kept after the cheap title-only ranking pass. */
44
+ const DEFAULT_CORPUS_MAX_CANDIDATES = 5;
45
+
46
+ /**
47
+ * Bound on the explicit per-candidate body fetch. Keeps corpus-context
48
+ * assembly at a fixed, small number of GitHub reads regardless of how
49
+ * many open Epics the repo carries.
50
+ */
51
+ export const DEFAULT_CORPUS_BODY_FETCH_TOP_K = 3;
52
+
53
+ /** Minimum Jaccard overlap for a section excerpt to be worth surfacing. */
54
+ const DEFAULT_CORPUS_MIN_SCORE = 0.1;
55
+
56
+ /** Max relevant-section excerpts returned in the envelope. */
57
+ const DEFAULT_CORPUS_MAX_SECTIONS = 5;
58
+
59
+ /** Excerpt length cap (chars) so one oversized Tech Spec doesn't blow the envelope. */
60
+ const EXCERPT_MAX_CHARS = 600;
61
+
62
+ /**
63
+ * Page-scan cap passed to `provider.getEpics({ state: 'open', pageCap })`.
64
+ * The corpus lookup only ever ranks the list down to a top-5 shortlist
65
+ * (`DEFAULT_CORPUS_MAX_CANDIDATES`), so there is no need to inherit
66
+ * `paginateRest`'s full default ceiling (50 pages / 5000 items) to build
67
+ * it — a bounded scan keeps this call a fixed, small number of GitHub
68
+ * reads regardless of how many open Epics the repo carries.
69
+ */
70
+ const CORPUS_EPICS_PAGE_CAP = 5;
71
+
72
+ /**
73
+ * Rank open Epics by title-overlap with the seed. This is the cheap
74
+ * first pass over the list surface (title only — `issueToEpicListItem`
75
+ * has no `body`), used solely to pick the bounded top-K candidates
76
+ * worth an explicit body fetch. It is not the final relevance signal;
77
+ * `extractRelevantSections` re-scores against actual section content.
78
+ *
79
+ * @param {{ seed: string, epics: Array<{ id:number, title:string }>, maxResults?: number }} opts
80
+ * @returns {Array<{ id:number, title:string, score:number }>}
81
+ */
82
+ export function rankCandidateEpics({
83
+ seed,
84
+ epics,
85
+ maxResults = DEFAULT_CORPUS_MAX_CANDIDATES,
86
+ }) {
87
+ if (!seed || typeof seed !== 'string') {
88
+ throw new Error('rankCandidateEpics: seed must be a non-empty string');
89
+ }
90
+ if (!Array.isArray(epics)) {
91
+ throw new Error('rankCandidateEpics: epics must be an array');
92
+ }
93
+ const seedTokens = tokenize(seed);
94
+ if (seedTokens.size === 0) return [];
95
+
96
+ const ranked = [];
97
+ for (const epic of epics) {
98
+ if (!epic || typeof epic.title !== 'string') continue;
99
+ const score = overlapScore(seedTokens, tokenize(epic.title));
100
+ ranked.push({
101
+ id: epic.id,
102
+ title: epic.title,
103
+ score: Number(score.toFixed(4)),
104
+ });
105
+ }
106
+ ranked.sort((a, b) => b.score - a.score);
107
+ return ranked.slice(0, maxResults);
108
+ }
109
+
110
+ /**
111
+ * Fetch full bodies for the top-K ranked candidates via the single-issue
112
+ * read (`provider.getEpic`), which — unlike the `getEpics` list mapper —
113
+ * does carry `body`. This is the explicit, bounded fetch the corpus
114
+ * lookup performs instead of assuming the list surface already has
115
+ * prose to score: a candidate never contributes a relevant section
116
+ * without this round-trip resolving its body.
117
+ *
118
+ * A single candidate's fetch failing (deleted issue, transient error) is
119
+ * non-fatal — it is dropped from the result rather than aborting corpus
120
+ * assembly for every other candidate. Failures are logged via
121
+ * `Logger.debug` (stderr) so they are visible under
122
+ * `AGENT_LOG_LEVEL=verbose` triage without violating the friction-
123
+ * telemetry posture in `.agents/instructions.md` §1.H of never silently
124
+ * swallowing an error.
125
+ *
126
+ * The bounded candidate slice is fetched concurrently
127
+ * (`Promise.allSettled`) rather than sequentially — `topK` is a fixed
128
+ * small ceiling (default 3), so this is a bounded fan-out, not an
129
+ * unbounded one, and it removes the serial network-latency stacking a
130
+ * plain `for`-await loop would otherwise incur.
131
+ *
132
+ * @param {{ provider: object, candidates: Array<{ id:number, title:string }>, topK?: number }} opts
133
+ * @returns {Promise<Array<{ id:number, title:string, body:string }>>}
134
+ */
135
+ export async function fetchCandidateBodies({
136
+ provider,
137
+ candidates,
138
+ topK = DEFAULT_CORPUS_BODY_FETCH_TOP_K,
139
+ }) {
140
+ if (!provider || typeof provider.getEpic !== 'function') return [];
141
+ if (!Array.isArray(candidates) || candidates.length === 0) return [];
142
+
143
+ const bounded = candidates.slice(0, topK);
144
+ const settled = await Promise.allSettled(
145
+ bounded.map(async (candidate) => {
146
+ const epic = await provider.getEpic(candidate.id);
147
+ return {
148
+ id: candidate.id,
149
+ title: candidate.title ?? epic?.title ?? '',
150
+ body: epic?.body ?? '',
151
+ };
152
+ }),
153
+ );
154
+
155
+ const results = [];
156
+ for (let i = 0; i < settled.length; i += 1) {
157
+ const outcome = settled[i];
158
+ if (outcome.status === 'fulfilled') {
159
+ results.push(outcome.value);
160
+ continue;
161
+ }
162
+ // Best-effort: one candidate failing to resolve must not abort
163
+ // corpus-context assembly for the rest — but the failure is still
164
+ // surfaced for triage rather than silently swallowed.
165
+ Logger.debug(
166
+ `[planning-corpus] fetchCandidateBodies: candidate #${bounded[i].id} failed to resolve: ${outcome.reason?.message ?? outcome.reason}`,
167
+ );
168
+ }
169
+ return results;
170
+ }
171
+
172
+ /**
173
+ * Extract a scoreable excerpt from an Epic body: the managed Tech Spec
174
+ * region when present (the folded `## Delivery Slicing` section, #4324),
175
+ * otherwise the ideation lede — the prose before the first `##` heading
176
+ * — so a plain-body Epic still contributes something to score.
177
+ *
178
+ * @param {string} body
179
+ * @returns {{ kind:'techSpec'|'lede', content:string }}
180
+ */
181
+ function extractScoreableExcerpt(body) {
182
+ if (hasEpicSection(body, 'techSpec')) {
183
+ return { kind: 'techSpec', content: extractEpicSection(body, 'techSpec') };
184
+ }
185
+ const lede = (body ?? '').split(/^##\s+/m)[0].trim();
186
+ return { kind: 'lede', content: lede };
187
+ }
188
+
189
+ /**
190
+ * Rank existing-Epic body excerpts (Tech Spec section, or lede) by
191
+ * overlap with the seed. Reuses the same `tokenize` / `overlapScore`
192
+ * primitives as the title-ranking pass above and as
193
+ * `duplicate-search.js` — one matcher shared across every corpus /
194
+ * dedupe surface.
195
+ *
196
+ * @param {{ seed:string, epicBodies: Array<{ id:number, title:string, body:string }>, maxResults?: number, minScore?: number }} opts
197
+ * @returns {Array<{ epicId:number, epicTitle:string, section:'techSpec'|'lede', score:number, excerpt:string }>}
198
+ */
199
+ export function extractRelevantSections({
200
+ seed,
201
+ epicBodies,
202
+ maxResults = DEFAULT_CORPUS_MAX_SECTIONS,
203
+ minScore = DEFAULT_CORPUS_MIN_SCORE,
204
+ }) {
205
+ if (!seed || typeof seed !== 'string') {
206
+ throw new Error('extractRelevantSections: seed must be a non-empty string');
207
+ }
208
+ if (!Array.isArray(epicBodies)) {
209
+ throw new Error('extractRelevantSections: epicBodies must be an array');
210
+ }
211
+ const seedTokens = tokenize(seed);
212
+ if (seedTokens.size === 0) return [];
213
+
214
+ const ranked = [];
215
+ for (const epic of epicBodies) {
216
+ if (!epic) continue;
217
+ const { kind, content } = extractScoreableExcerpt(epic.body ?? '');
218
+ if (!content) continue;
219
+ const score = overlapScore(seedTokens, tokenize(content));
220
+ if (score < minScore) continue;
221
+ ranked.push({
222
+ epicId: epic.id,
223
+ epicTitle: epic.title ?? null,
224
+ section: kind,
225
+ score: Number(score.toFixed(4)),
226
+ excerpt: content.slice(0, EXCERPT_MAX_CHARS),
227
+ });
228
+ }
229
+ ranked.sort((a, b) => b.score - a.score);
230
+ return ranked.slice(0, maxResults);
231
+ }
232
+
233
+ /**
234
+ * Assemble the `corpusContext` field of the story-plan context envelope.
235
+ * Pure orchestration over the three helpers above plus `buildDocsDigest`
236
+ * — no I/O beyond what `provider` and the docs-digest reader perform.
237
+ *
238
+ * `relevantSections` is `[]` (not an error) when the provider has no
239
+ * `getEpics` surface, the seed tokenizes to nothing, or no candidate
240
+ * clears `minScore` — the standalone-Story draft path degrades to
241
+ * exactly today's blank-slate behavior in that case.
242
+ *
243
+ * @param {{
244
+ * seed: string,
245
+ * provider?: { getEpics?: Function, getEpic?: Function },
246
+ * docsContextFiles?: string[],
247
+ * docsRoot?: string,
248
+ * maxCandidates?: number,
249
+ * bodyFetchTopK?: number,
250
+ * maxSections?: number,
251
+ * minScore?: number,
252
+ * }} opts
253
+ * @returns {Promise<{ docsDigest: string|null, relevantSections: Array<object> }>}
254
+ */
255
+ export async function buildCorpusContext({
256
+ seed,
257
+ provider,
258
+ docsContextFiles,
259
+ docsRoot,
260
+ maxCandidates = DEFAULT_CORPUS_MAX_CANDIDATES,
261
+ bodyFetchTopK = DEFAULT_CORPUS_BODY_FETCH_TOP_K,
262
+ maxSections = DEFAULT_CORPUS_MAX_SECTIONS,
263
+ minScore = DEFAULT_CORPUS_MIN_SCORE,
264
+ }) {
265
+ const docsDigest = await buildDocsDigest({ docsContextFiles, docsRoot });
266
+
267
+ let relevantSections = [];
268
+ if (provider && typeof provider.getEpics === 'function') {
269
+ // A single candidate-listing call failing (rate limit, transient
270
+ // network error, provider outage) must not abort the whole
271
+ // `--emit-context` envelope build — degrade to an empty candidate
272
+ // list instead of letting the rejection propagate out of the
273
+ // caller's `Promise.all` and take down the rest of the envelope
274
+ // (body template, tech-stack summary, docs digest) with it.
275
+ let epics = [];
276
+ try {
277
+ epics = await provider.getEpics({
278
+ state: 'open',
279
+ pageCap: CORPUS_EPICS_PAGE_CAP,
280
+ });
281
+ } catch (err) {
282
+ Logger.debug(
283
+ `[planning-corpus] buildCorpusContext: provider.getEpics failed, degrading to an empty candidate list: ${err?.message ?? err}`,
284
+ );
285
+ epics = [];
286
+ }
287
+ const ranked = rankCandidateEpics({
288
+ seed,
289
+ epics: Array.isArray(epics) ? epics : [],
290
+ maxResults: maxCandidates,
291
+ });
292
+ const bodies = await fetchCandidateBodies({
293
+ provider,
294
+ candidates: ranked,
295
+ topK: bodyFetchTopK,
296
+ });
297
+ relevantSections = extractRelevantSections({
298
+ seed,
299
+ epicBodies: bodies,
300
+ maxResults: maxSections,
301
+ minScore,
302
+ });
303
+ }
304
+
305
+ return { docsDigest, relevantSections };
306
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * detectors/common.js — shared helpers for the signals layer.
3
3
  *
4
- * Hoisted out of three detector modules (hotspot, retry, rework) plus
4
+ * Hoisted out of the detector modules (retry, rework) plus
5
5
  * `signals/read.js` and `signals/schema.js`, all of which shipped
6
6
  * byte-equivalent copies of these predicates. See Story #2464.
7
7
  */
@@ -19,15 +19,15 @@ export function isPositiveInt(v) {
19
19
 
20
20
  /**
21
21
  * Pull the tool name from a trace record. The hook writes the tool name
22
- * into `source.tool` and (defensively) into `details.tool` — we accept
23
- * either so older traces still classify correctly.
22
+ * into `emitter.tool` (canonical provenance) and, defensively, into
23
+ * `details.tool` — we accept either.
24
24
  *
25
25
  * @param {object} rec
26
26
  * @returns {string|null}
27
27
  */
28
28
  export function extractTool(rec) {
29
- if (typeof rec?.source?.tool === 'string' && rec.source.tool.length > 0) {
30
- return rec.source.tool;
29
+ if (typeof rec?.emitter?.tool === 'string' && rec.emitter.tool.length > 0) {
30
+ return rec.emitter.tool;
31
31
  }
32
32
  if (typeof rec?.details?.tool === 'string' && rec.details.tool.length > 0) {
33
33
  return rec.details.tool;
@@ -38,12 +38,12 @@ export function extractTool(rec) {
38
38
  /**
39
39
  * Validate and normalize the shared detector argument preamble.
40
40
  *
41
- * `detectRework`, `detectRetry`, and `detectHotspot` previously shipped a
42
- * near-identical guard block: the `args` object-shape `TypeError`, the
43
- * `nowFn` function-type `TypeError`, the positive-integer `RangeError`s for
44
- * the id fields, the non-empty-string `tracesPath` check, and the
41
+ * `detectRework` and `detectRetry` previously shipped a near-identical
42
+ * guard block: the `args` object-shape `TypeError`, the `nowFn`
43
+ * function-type `TypeError`, the positive-integer `RangeError`s for the id
44
+ * fields, the non-empty-string `tracesPath` check, and the
45
45
  * non-negative-integer `threshold` check. Story #4077 hoists that preamble
46
- * here so the three detectors share one error-message contract.
46
+ * here so the detectors share one error-message contract.
47
47
  *
48
48
  * Error wording stays per-detector-accurate by prefixing every message with
49
49
  * `fnName` (e.g. `detectRework: …`). Error *types* are preserved exactly:
@@ -2,13 +2,13 @@
2
2
  * Detectors barrel (Epic #1721 / Story #1771 / Task #1774).
3
3
  *
4
4
  * Single import surface for every signal detector. Detector Stories
5
- * (rework in #1771, retry in #1768, hotspot in #1769) re-export from
6
- * here so callers (`lib/observability/perf-aggregator.js`, future
7
- * emission orchestrators) only ever import from one place.
5
+ * (rework in #1771, retry in #1768) re-export from here so callers
6
+ * (`lib/orchestration/detectors-phase.js`) only ever import from one
7
+ * place. The Epic #1769 hotspot detector was retired in the Epic #4406
8
+ * signal-contract cutover (no live emitter, no consumer).
8
9
  *
9
10
  * @module lib/signals/detectors
10
11
  */
11
12
 
12
- export { detectHotspot, nearestRankP95 } from './hotspot.js';
13
13
  export { detectRetry } from './retry.js';
14
14
  export { detectRework } from './rework.js';
@@ -27,11 +27,12 @@
27
27
  * ## Failure rule
28
28
  *
29
29
  * A trace record is treated as **failed** when its `details.exitCode`
30
- * is a number and not `0`. The hook does not itself capture the exit
31
- * code today (the field is set by callers / future hook extensions);
32
- * records without an `exitCode` field are ignored, which matches the
33
- * decision in the parent Epic body that retry only counts non-zero-exit
34
- * commands.
30
+ * is a number and not `0`. As of Epic #4406 / Story #4413 the tool-trace
31
+ * hook captures `details.exitCode` for Bash `PostToolUse` events, so this
32
+ * detector fires on real deliveries; records without an `exitCode` field
33
+ * (non-Bash tools, or tools that report no exit code) are ignored, which
34
+ * matches the decision in the parent Epic body that retry only counts
35
+ * non-zero-exit commands.
35
36
  *
36
37
  * Successful runs after failures **do not** cancel the count — failure-
37
38
  * count is monotonic per identity. This matches the Epic's intent: once
@@ -41,12 +42,11 @@
41
42
  *
42
43
  * ## Tool filter
43
44
  *
44
- * Only trace records whose `source.tool === 'Bash'` participate. Edit /
45
+ * Only trace records whose `emitter.tool === 'Bash'` participate. Edit /
45
46
  * Write / Read / Grep / Glob events are not retries — those belong to
46
- * other detectors (rework for file-edit churn, hotspot at Epic scope).
47
- * The tool name is read from `source.tool` first and falls back to
48
- * `details.tool` to mirror the rework detector's tolerance for legacy
49
- * trace shapes.
47
+ * other detectors (rework for file-edit churn). The tool name is read
48
+ * from `emitter.tool` first and falls back to `details.tool` (see
49
+ * `common.extractTool`).
50
50
  *
51
51
  * ## Privacy contract
52
52
  *
@@ -124,13 +124,14 @@ function resolveIdentity(rec) {
124
124
  }
125
125
 
126
126
  /**
127
- * Decide whether a trace record represents a failed invocation. The
128
- * hook (today) does not capture exit codes; the field is populated by
129
- * future hook extensions and by tests that need to assert behaviour.
130
- * A record counts as failed when `details.exitCode` is a number and
131
- * not zero. Anything else (missing field, null, non-number, zero) is
132
- * NOT a failure and is ignored entirely — the detector only counts
133
- * non-zero-exit commands per the parent Epic.
127
+ * Decide whether a trace record represents a failed invocation. As of
128
+ * Epic #4406 / Story #4413 the tool-trace hook records `details.exitCode`
129
+ * for Bash `PostToolUse` events, so the field is present on real Bash
130
+ * traces (and still set directly by tests). A record counts as failed
131
+ * when `details.exitCode` is a number and not zero. Anything else
132
+ * (missing field, null, non-number, zero) is NOT a failure and is ignored
133
+ * entirely — the detector only counts non-zero-exit commands per the
134
+ * parent Epic.
134
135
  *
135
136
  * @param {object} rec
136
137
  * @returns {boolean}
@@ -238,7 +239,7 @@ export async function detectRetry(args) {
238
239
  return offenders.map(([commandHash, failureCount]) => ({
239
240
  ts,
240
241
  kind: 'retry',
241
- source: { tool: 'retry-detector' },
242
+ emitter: { tool: 'retry-detector' },
242
243
  epicId,
243
244
  storyId,
244
245
  taskId,
@@ -158,7 +158,7 @@ export async function detectRework(args) {
158
158
  return offenders.map(([targetHash, editCount]) => ({
159
159
  ts,
160
160
  kind: 'rework',
161
- source: { tool: 'rework-detector' },
161
+ emitter: { tool: 'rework-detector' },
162
162
  epicId,
163
163
  storyId,
164
164
  taskId,