mandrel 1.57.0 → 1.59.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 (131) hide show
  1. package/.agents/README.md +89 -87
  2. package/.agents/docs/SDLC.md +11 -7
  3. package/.agents/docs/workflows.md +2 -1
  4. package/.agents/schemas/audit-rules.json +20 -0
  5. package/.agents/scripts/acceptance-eval.js +20 -3
  6. package/.agents/scripts/assert-branch.js +1 -3
  7. package/.agents/scripts/bootstrap.js +1 -1
  8. package/.agents/scripts/check-arch-cycles.js +360 -0
  9. package/.agents/scripts/coverage-capture.js +24 -3
  10. package/.agents/scripts/epic-deliver-preflight.js +5 -3
  11. package/.agents/scripts/epic-deliver-prepare.js +12 -4
  12. package/.agents/scripts/epic-execute-record-wave.js +1 -1
  13. package/.agents/scripts/evidence-gate.js +1 -1
  14. package/.agents/scripts/git-rebase-and-resolve.js +1 -1
  15. package/.agents/scripts/hierarchy-gate.js +34 -14
  16. package/.agents/scripts/lib/baselines/kinds/coverage.js +33 -149
  17. package/.agents/scripts/lib/baselines/kinds/duplication.js +27 -116
  18. package/.agents/scripts/lib/baselines/kinds/kind-factory.js +192 -0
  19. package/.agents/scripts/lib/baselines/kinds/lighthouse.js +34 -133
  20. package/.agents/scripts/lib/baselines/kinds/maintainability.js +31 -124
  21. package/.agents/scripts/lib/baselines/kinds/mutation.js +25 -111
  22. package/.agents/scripts/lib/baselines/maintainability-baseline-io.js +59 -0
  23. package/.agents/scripts/lib/baselines/maintainability-baseline-save.js +37 -0
  24. package/.agents/scripts/lib/baselines/writer.js +1 -1
  25. package/.agents/scripts/lib/close-validation/commands.js +188 -0
  26. package/.agents/scripts/lib/close-validation/gates.js +235 -0
  27. package/.agents/scripts/lib/close-validation/process.js +101 -0
  28. package/.agents/scripts/lib/close-validation/projections/maintainability.js +1 -1
  29. package/.agents/scripts/lib/close-validation/runner.js +325 -0
  30. package/.agents/scripts/lib/close-validation/telemetry.js +70 -0
  31. package/.agents/scripts/lib/config/quality.js +6 -6
  32. package/.agents/scripts/lib/config-resolver.js +2 -5
  33. package/.agents/scripts/lib/coverage-capture.js +147 -4
  34. package/.agents/scripts/lib/cpu-pool.js +14 -0
  35. package/.agents/scripts/lib/crap-utils.js +6 -11
  36. package/.agents/scripts/lib/dynamic-workflow/documentation-report-contract.js +87 -0
  37. package/.agents/scripts/lib/git-utils.js +24 -22
  38. package/.agents/scripts/lib/maintainability-engine.js +1 -1
  39. package/.agents/scripts/lib/maintainability-utils.js +4 -187
  40. package/.agents/scripts/lib/observability/perf-report-readers.js +32 -23
  41. package/.agents/scripts/lib/orchestration/acceptance-eval-decision.js +80 -6
  42. package/.agents/scripts/lib/orchestration/code-review.js +90 -77
  43. package/.agents/scripts/lib/orchestration/dispatch-pipeline.js +5 -12
  44. package/.agents/scripts/lib/orchestration/epic-deliver-lease-guard.js +14 -14
  45. package/.agents/scripts/lib/orchestration/epic-plan-decompose/phases/planning-artifacts.js +2 -2
  46. package/.agents/scripts/lib/orchestration/epic-plan-lease-guard.js +184 -49
  47. package/.agents/scripts/lib/orchestration/epic-plan-spec/phases/drain.js +1 -1
  48. package/.agents/scripts/lib/orchestration/epic-plan-spec/phases/plan-epic.js +26 -2
  49. package/.agents/scripts/lib/orchestration/epic-plan-spec/phases/run-spec-phase.js +26 -6
  50. package/.agents/scripts/lib/orchestration/epic-runner/phases/build-wave-dag.js +7 -20
  51. package/.agents/scripts/lib/orchestration/epic-runner/progress-reporter/composition.js +1 -2
  52. package/.agents/scripts/lib/orchestration/epic-runner/progress-reporter/signals.js +0 -6
  53. package/.agents/scripts/lib/orchestration/epic-runner/progress-signals/component-drift.js +103 -0
  54. package/.agents/scripts/lib/orchestration/epic-runner/progress-signals/crap-drift.js +22 -64
  55. package/.agents/scripts/lib/orchestration/epic-runner/progress-signals/maintainability-drift.js +38 -76
  56. package/.agents/scripts/lib/orchestration/epic-runner/story-run-progress-writer.js +2 -2
  57. package/.agents/scripts/lib/orchestration/epic-runner/sub-agent-return.js +4 -16
  58. package/.agents/scripts/lib/orchestration/file-assumptions.js +4 -3
  59. package/.agents/scripts/lib/orchestration/lease-guard-shared.js +144 -0
  60. package/.agents/scripts/lib/orchestration/lifecycle/emit-story-heartbeat.js +2 -2
  61. package/.agents/scripts/lib/orchestration/lifecycle/listeners/watcher.js +7 -7
  62. package/.agents/scripts/lib/orchestration/post-merge/phases/notification.js +3 -3
  63. package/.agents/scripts/lib/orchestration/post-merge/phases/worktree-reap.js +7 -7
  64. package/.agents/scripts/lib/orchestration/preflight-cache.js +35 -12
  65. package/.agents/scripts/lib/orchestration/review-providers/codex.js +5 -60
  66. package/.agents/scripts/lib/orchestration/review-providers/native.js +7 -6
  67. package/.agents/scripts/lib/orchestration/review-providers/parse-findings.js +105 -0
  68. package/.agents/scripts/lib/orchestration/review-providers/security-review.js +7 -59
  69. package/.agents/scripts/lib/orchestration/single-story-close/phases/close-validation.js +2 -4
  70. package/.agents/scripts/lib/orchestration/single-story-close/phases/normalize-pr-title.js +241 -0
  71. package/.agents/scripts/lib/orchestration/single-story-close/phases/options.js +1 -1
  72. package/.agents/scripts/lib/orchestration/single-story-close/phases/pull-request.js +16 -3
  73. package/.agents/scripts/lib/orchestration/single-story-close/runner.js +2 -4
  74. package/.agents/scripts/lib/orchestration/single-story-lease-guard.js +32 -35
  75. package/.agents/scripts/lib/orchestration/skill-capsule-loader.js +1 -2
  76. package/.agents/scripts/lib/orchestration/story-close/auto-refresh-runner.js +451 -503
  77. package/.agents/scripts/lib/orchestration/story-close/baseline-attribution/phases/pre-merge-attribution.js +8 -2
  78. package/.agents/scripts/lib/orchestration/story-close/baseline-attribution/phases/refresh-commit.js +47 -2
  79. package/.agents/scripts/lib/orchestration/story-close/baseline-attribution/phases/regression-projection.js +2 -2
  80. package/.agents/scripts/lib/orchestration/story-close/format-autofix.js +358 -54
  81. package/.agents/scripts/lib/orchestration/story-close/phases/close.js +1 -1
  82. package/.agents/scripts/lib/orchestration/story-close/phases/gates.js +3 -2
  83. package/.agents/scripts/lib/orchestration/story-close/phases/locked-pipeline.js +30 -3
  84. package/.agents/scripts/lib/orchestration/story-close/post-merge-close.js +5 -18
  85. package/.agents/scripts/lib/orchestration/story-close/pre-merge-validation.js +3 -3
  86. package/.agents/scripts/lib/orchestration/story-close-recovery.js +33 -16
  87. package/.agents/scripts/lib/orchestration/story-reachability.js +47 -0
  88. package/.agents/scripts/lib/orchestration/ticket-validator-conflicts.js +2 -33
  89. package/.agents/scripts/lib/orchestration/ticketing/bulk.js +42 -64
  90. package/.agents/scripts/lib/orchestration/ticketing/reads.js +9 -0
  91. package/.agents/scripts/lib/orchestration/ticketing/state.js +50 -436
  92. package/.agents/scripts/lib/orchestration/ticketing/transition.js +471 -0
  93. package/.agents/scripts/lib/orchestration/ticketing.js +0 -1
  94. package/.agents/scripts/lib/orchestration/wave-record-notifications.js +1 -1
  95. package/.agents/scripts/lib/orchestration/wave-record-projection.js +1 -7
  96. package/.agents/scripts/lib/project-root.js +17 -0
  97. package/.agents/scripts/lib/story-adjacency.js +76 -0
  98. package/.agents/scripts/lib/story-lifecycle.js +1 -1
  99. package/.agents/scripts/lib/transpile.js +93 -0
  100. package/.agents/scripts/lib/wave-runner/tick.js +4 -153
  101. package/.agents/scripts/lib/workers/crap-worker.js +1 -1
  102. package/.agents/scripts/lib/workers/maintainability-report-worker.js +1 -1
  103. package/.agents/scripts/lib/worktree/lifecycle/creation.js +20 -2
  104. package/.agents/scripts/lib/worktree/lifecycle/force-drain.js +90 -0
  105. package/.agents/scripts/lib/worktree/lifecycle/reap.js +26 -8
  106. package/.agents/scripts/lib/worktree/node-modules-strategy.js +74 -0
  107. package/.agents/scripts/providers/github/tickets.js +110 -6
  108. package/.agents/scripts/run-lint.js +9 -0
  109. package/.agents/scripts/run-tests.js +24 -4
  110. package/.agents/scripts/stories-wave-tick.js +8 -5
  111. package/.agents/scripts/story-init.js +149 -10
  112. package/.agents/scripts/sync-branch-from-base.js +1 -1
  113. package/.agents/skills/stack/qa/lighthouse-baseline/SKILL.md +1 -1
  114. package/.agents/workflows/audit-documentation.md +226 -0
  115. package/.agents/workflows/epic-deliver.md +16 -23
  116. package/.agents/workflows/epic-plan.md +1 -1
  117. package/.agents/workflows/helpers/epic-deliver-story.md +17 -28
  118. package/.agents/workflows/helpers/single-story-deliver.md +2 -1
  119. package/.agents/workflows/onboard.md +4 -3
  120. package/.agents/workflows/story-deliver.md +1 -1
  121. package/README.md +21 -8
  122. package/lib/cli/init.js +336 -0
  123. package/package.json +2 -1
  124. package/.agents/scripts/lib/auto-refresh-baselines.js +0 -308
  125. package/.agents/scripts/lib/close-validation.js +0 -897
  126. package/.agents/scripts/lib/orchestration/cascade-grouping.js +0 -275
  127. package/.agents/scripts/lib/orchestration/epic-runner/progress-reporter.js +0 -69
  128. package/.agents/scripts/lib/orchestration/story-close/format-autofix-scoped.js +0 -221
  129. package/.agents/scripts/lib/orchestration/story-close/format-autofix-shared.js +0 -123
  130. package/.agents/scripts/lib/task-utils.js +0 -26
  131. package/.agents/scripts/story-deliver-prepare.js +0 -267
@@ -16,7 +16,7 @@
16
16
  * chain are unchanged.
17
17
  *
18
18
  * Public API:
19
- * - `runCodeReview({ epicId, provider, logger, bus, ... })` →
19
+ * - `runCodeReview({ scope, ticketId, provider, logger, bus, ... })` →
20
20
  * `{ status, severity, posted, report, halted, blockerReason }`.
21
21
  *
22
22
  * Behaviour:
@@ -302,88 +302,104 @@ function buildCodeReviewEndPayload({ epicId, result, durationMs }) {
302
302
  }
303
303
 
304
304
  /**
305
- * Resolve the scope envelope from the (legacy `epicId` + optional
306
- * `baseBranch`) shape OR the (new `scope`/`ticketId`/`headRef`/
307
- * `commentTargetId`) shape into a single normalized record. Extracted to
308
- * keep `runCodeReview` body below the CRAP-cyclomatic ceiling.
305
+ * Resolve the project base branch fallback used when a caller omits
306
+ * `baseRef`.
307
+ */
308
+ function resolveConfigBase(config) {
309
+ return (
310
+ config?.project?.baseBranch ?? config?.agentSettings?.baseBranch ?? 'main'
311
+ );
312
+ }
313
+
314
+ /** Positive-integer override, else the supplied default. */
315
+ function resolveCommentTargetId(commentTargetId, fallback) {
316
+ return Number.isInteger(commentTargetId) && commentTargetId > 0
317
+ ? commentTargetId
318
+ : fallback;
319
+ }
320
+
321
+ /**
322
+ * Resolve the Story-scope envelope from the parameterized
323
+ * `{ scope: 'story', ticketId, baseRef, headRef, commentTargetId }` shape.
309
324
  *
310
- * @param {{
311
- * epicId?: number,
312
- * scope?: 'epic'|'story',
313
- * ticketId?: number,
314
- * baseBranch?: string|null,
315
- * baseRef?: string|null,
316
- * headRef?: string|null,
317
- * commentTargetId?: number|null,
318
- * }} opts
319
- * @param {object} config
320
325
  * @returns {{
321
- * scope: 'epic'|'story',
326
+ * scope: 'story',
322
327
  * ticketId: number,
323
328
  * baseRef: string,
324
329
  * headRef: string,
325
330
  * commentTargetId: number,
326
- * epicIdForLedger: number|null,
331
+ * epicIdForLedger: null,
327
332
  * }}
328
333
  */
329
- function resolveScopeEnvelope(opts, config) {
330
- const explicitScope = opts.scope;
331
- const epicIdLegacy = opts.epicId;
332
- const configBase =
333
- config?.project?.baseBranch ?? config?.agentSettings?.baseBranch ?? 'main';
334
-
335
- if (explicitScope === 'story') {
336
- if (!Number.isInteger(opts.ticketId) || opts.ticketId <= 0) {
337
- throw new TypeError(
338
- 'runCodeReview: ticketId is required (positive integer) when scope="story".',
339
- );
340
- }
341
- if (typeof opts.headRef !== 'string' || opts.headRef.length === 0) {
342
- throw new TypeError(
343
- 'runCodeReview: headRef is required (non-empty string) when scope="story".',
344
- );
345
- }
346
- const baseRef = opts.baseRef ?? opts.baseBranch ?? configBase;
347
- const commentTargetId =
348
- Number.isInteger(opts.commentTargetId) && opts.commentTargetId > 0
349
- ? opts.commentTargetId
350
- : opts.ticketId;
351
- return {
352
- scope: 'story',
353
- ticketId: opts.ticketId,
354
- baseRef,
355
- headRef: opts.headRef,
356
- commentTargetId,
357
- epicIdForLedger: null,
358
- };
334
+ function resolveStoryScope(opts, config) {
335
+ if (!Number.isInteger(opts.ticketId) || opts.ticketId <= 0) {
336
+ throw new TypeError(
337
+ 'runCodeReview: ticketId is required (positive integer) when scope="story".',
338
+ );
339
+ }
340
+ if (typeof opts.headRef !== 'string' || opts.headRef.length === 0) {
341
+ throw new TypeError(
342
+ 'runCodeReview: headRef is required (non-empty string) when scope="story".',
343
+ );
359
344
  }
345
+ return {
346
+ scope: 'story',
347
+ ticketId: opts.ticketId,
348
+ baseRef: opts.baseRef ?? resolveConfigBase(config),
349
+ headRef: opts.headRef,
350
+ commentTargetId: resolveCommentTargetId(
351
+ opts.commentTargetId,
352
+ opts.ticketId,
353
+ ),
354
+ epicIdForLedger: null,
355
+ };
356
+ }
360
357
 
361
- // Epic scope (default + legacy `epicId` callers).
362
- const effectiveEpicId =
363
- Number.isInteger(opts.ticketId) && opts.ticketId > 0
364
- ? opts.ticketId
365
- : epicIdLegacy;
366
- if (!Number.isInteger(effectiveEpicId) || effectiveEpicId <= 0) {
358
+ /**
359
+ * Resolve the Epic-scope envelope from the parameterized
360
+ * `{ scope: 'epic', ticketId, baseRef, headRef, commentTargetId }` shape.
361
+ * `headRef` defaults to `epic/<ticketId>` and `baseRef` to the project
362
+ * base branch.
363
+ *
364
+ * @returns {{
365
+ * scope: 'epic',
366
+ * ticketId: number,
367
+ * baseRef: string,
368
+ * headRef: string,
369
+ * commentTargetId: number,
370
+ * epicIdForLedger: number,
371
+ * }}
372
+ */
373
+ function resolveEpicScope(opts, config) {
374
+ if (!Number.isInteger(opts.ticketId) || opts.ticketId <= 0) {
367
375
  throw new TypeError(
368
- 'runCodeReview: epicId is required (positive integer).',
376
+ 'runCodeReview: ticketId is required (positive integer) when scope="epic".',
369
377
  );
370
378
  }
371
- const baseRef = opts.baseRef ?? opts.baseBranch ?? configBase;
372
- const headRef = opts.headRef ?? `epic/${effectiveEpicId}`;
373
- const commentTargetId =
374
- Number.isInteger(opts.commentTargetId) && opts.commentTargetId > 0
375
- ? opts.commentTargetId
376
- : effectiveEpicId;
377
379
  return {
378
380
  scope: 'epic',
379
- ticketId: effectiveEpicId,
380
- baseRef,
381
- headRef,
382
- commentTargetId,
383
- epicIdForLedger: effectiveEpicId,
381
+ ticketId: opts.ticketId,
382
+ baseRef: opts.baseRef ?? resolveConfigBase(config),
383
+ headRef: opts.headRef ?? `epic/${opts.ticketId}`,
384
+ commentTargetId: resolveCommentTargetId(
385
+ opts.commentTargetId,
386
+ opts.ticketId,
387
+ ),
388
+ epicIdForLedger: opts.ticketId,
384
389
  };
385
390
  }
386
391
 
392
+ /**
393
+ * Dispatch the parameterized scope envelope
394
+ * (`{ scope, ticketId, baseRef, headRef, commentTargetId }`) to the
395
+ * matching pure resolver. `scope` defaults to `'epic'`.
396
+ */
397
+ function resolveScopeEnvelope(opts, config) {
398
+ return opts.scope === 'story'
399
+ ? resolveStoryScope(opts, config)
400
+ : resolveEpicScope(opts, config);
401
+ }
402
+
387
403
  /**
388
404
  * In-process wrapper that the `/epic-deliver` runner and the
389
405
  * `/single-story-deliver` close path consume.
@@ -407,26 +423,23 @@ function resolveScopeEnvelope(opts, config) {
407
423
  * `scope: 'epic'` because the `code-review.end` schema requires
408
424
  * `epicId` and the ledger only spans Epic lifecycles.
409
425
  *
410
- * Argument shapes:
411
- * - Legacy (Epic):
412
- * `{ epicId, provider, bus, [baseBranch] }`
413
- * - Parameterized (Epic or Story):
414
- * `{ scope, ticketId, baseRef, headRef, [commentTargetId],
415
- * provider, bus }`
416
- * For `scope === 'story'`, `commentTargetId` overrides the post
417
- * target (e.g. PR number) while `ticketId` continues to label the
418
- * rendered header ("Story #N").
426
+ * Argument shape (parameterized, Epic or Story):
427
+ * `{ scope, ticketId, baseRef, headRef, [commentTargetId],
428
+ * provider, bus }`
429
+ * `scope` defaults to `'epic'`; `baseRef` defaults to the project base
430
+ * branch and (Epic scope only) `headRef` defaults to `epic/<ticketId>`.
431
+ * For `scope === 'story'`, `commentTargetId` overrides the post
432
+ * target (e.g. PR number) while `ticketId` continues to label the
433
+ * rendered header ("Story #N").
419
434
  *
420
435
  * @param {{
421
- * epicId?: number,
422
436
  * scope?: 'epic'|'story',
423
- * ticketId?: number,
437
+ * ticketId: number,
424
438
  * baseRef?: string|null,
425
439
  * headRef?: string|null,
426
440
  * commentTargetId?: number|null,
427
441
  * provider: object,
428
442
  * logger?: { info?: Function, warn?: Function, error?: Function, fatal?: Function, createProgress?: Function },
429
- * baseBranch?: string|null,
430
443
  * planningRisk?: { overallLevel?: ('low'|'medium'|'high'), axes?: Array<{ axis?: string, level?: string }> }|null,
431
444
  * changedFileCount?: number|null,
432
445
  * storyId?: number|null,
@@ -7,11 +7,11 @@
7
7
  */
8
8
 
9
9
  import { PROJECT_ROOT, resolveConfig } from '../config-resolver.js';
10
- import { parseBlockedBy } from '../dependency-parser.js';
11
10
  import { getEpicBranch } from '../git-utils.js';
12
11
  import { Logger } from '../Logger.js';
13
12
  import { TYPE_LABELS } from '../label-constants.js';
14
13
  import { createProvider } from '../provider-factory.js';
14
+ import { buildStoryAdjacency } from '../story-adjacency.js';
15
15
  import { WorktreeManager } from '../worktree-manager.js';
16
16
  import { computeStoryWaves } from './dependency-analyzer.js';
17
17
  import { reconcileHierarchy } from './reconciler.js';
@@ -161,17 +161,10 @@ export function buildStoryDispatchGraph(allTickets) {
161
161
  );
162
162
  const storyMap = new Map(stories.map((s) => [s.id, s]));
163
163
 
164
- const explicitDeps = new Map();
165
- for (const story of stories) {
166
- const fromBody = parseBlockedBy(story.body ?? '');
167
- const fromField = Array.isArray(story.dependencies)
168
- ? story.dependencies.map(Number)
169
- : [];
170
- const merged = [...new Set([...fromBody, ...fromField])].filter(
171
- (id) => Number.isInteger(id) && id !== story.id && storyMap.has(id),
172
- );
173
- if (merged.length > 0) explicitDeps.set(story.id, merged);
174
- }
164
+ // Shared story-level adjacency builder (lib/story-adjacency.js) owns the
165
+ // dependency-source ordering contract: body `blocked by` references via
166
+ // `parseBlockedBy`, then explicit `dependencies[]`, foreign edges dropped.
167
+ const explicitDeps = buildStoryAdjacency(stories);
175
168
 
176
169
  // computeStoryWaves expects a Map<storyId, { tasks: [] }>; with no Tasks
177
170
  // present, only explicitDeps + focus-area rollup (no-op for empty
@@ -29,7 +29,10 @@
29
29
  * `process.exit(1)` by the `runAsCli` boundary), never `Logger.fatal`.
30
30
  */
31
31
 
32
- import { acquireLease, normalizeOperatorHandle } from './ticket-lease.js';
32
+ import {
33
+ acquireLeaseFailClosed,
34
+ resolveOperatorFromCandidates,
35
+ } from './lease-guard-shared.js';
33
36
 
34
37
  /**
35
38
  * Resolve the acquiring operator's identity. Precedence (Tech Spec #3476):
@@ -37,8 +40,11 @@ import { acquireLease, normalizeOperatorHandle } from './ticket-lease.js';
37
40
  * 2. `github.operatorHandle` in `.agentrc.json`.
38
41
  * 3. The local `git config user.email`.
39
42
  *
40
- * The `@`-prefix some operators carry on `operatorHandle` is stripped so the
41
- * value matches the bare login written to a ticket's `assignees`.
43
+ * The `@`-prefix some operators carry on `operatorHandle` is stripped (via
44
+ * the shared lease-guard kernel) so the value matches the bare login written
45
+ * to a ticket's `assignees`. This surface's missing-handle policy is `'null'`
46
+ * — `runPrepareGuards` fails closed on a null operator with deliver-specific
47
+ * wording after the (cheap, local) checkout-safety guard has run.
42
48
  *
43
49
  * @param {object} args
44
50
  * @param {string} [args.asFlag] Explicit `--as` value.
@@ -48,12 +54,9 @@ import { acquireLease, normalizeOperatorHandle } from './ticket-lease.js';
48
54
  * be determined.
49
55
  */
50
56
  export function resolveOperator({ asFlag, config, gitUserEmail } = {}) {
51
- const candidates = [asFlag, config?.github?.operatorHandle, gitUserEmail];
52
- for (const raw of candidates) {
53
- const normalized = normalizeOperatorHandle(raw);
54
- if (normalized !== null) return normalized;
55
- }
56
- return null;
57
+ return resolveOperatorFromCandidates({
58
+ candidates: [asFlag, config?.github?.operatorHandle, gitUserEmail],
59
+ });
57
60
  }
58
61
 
59
62
  /**
@@ -208,7 +211,7 @@ export async function acquireEpicLease({
208
211
  config,
209
212
  now,
210
213
  }) {
211
- const result = await acquireLease({
214
+ return acquireLeaseFailClosed({
212
215
  provider,
213
216
  ticketId: epicId,
214
217
  operator,
@@ -216,11 +219,8 @@ export async function acquireEpicLease({
216
219
  steal,
217
220
  config,
218
221
  now,
222
+ renderRefusal: renderLeaseRefusal,
219
223
  });
220
- if (!result.acquired) {
221
- throw new Error(renderLeaseRefusal(result, epicId));
222
- }
223
- return result;
224
224
  }
225
225
 
226
226
  /**
@@ -11,7 +11,7 @@
11
11
  */
12
12
 
13
13
  import { resolveListValue } from '../../../config/shared.js';
14
- import { _internal as conflictInternal } from '../../ticket-validator-conflicts.js';
14
+ import { DEFAULT_REGISTRY_PATTERNS } from '../../ticket-validator-conflicts.js';
15
15
 
16
16
  /**
17
17
  * Ensure the supplied Epic body carries a `## Planning Artifacts` section.
@@ -67,7 +67,7 @@ export function resolveConflictPolicy(cfg) {
67
67
  }
68
68
  if (planning?.crossCuttingRegistries !== undefined) {
69
69
  policy.registries = resolveListValue(
70
- conflictInternal.DEFAULT_REGISTRY_PATTERNS,
70
+ DEFAULT_REGISTRY_PATTERNS,
71
71
  planning.crossCuttingRegistries,
72
72
  );
73
73
  }
@@ -8,19 +8,18 @@
8
8
  * silently duplicate the Feature/Story tree:
9
9
  *
10
10
  * - `acquireEpicPlanLease` — claim the Epic before Phase 7 (spec). Refuses
11
- * (throws, exit non-zero) when a foreign claim
12
- * already holds the Epic, naming the current
13
- * owner. **Fail-closed (audit #3513):**
14
- * `/epic-plan` emits no `story.heartbeat` during
15
- * its run (heartbeats are a delivery-time
16
- * signal), so there is no live-heartbeat source
17
- * to judge a concurrent plan's liveness from.
18
- * Defaulting liveness to "stale" made every
19
- * foreign claim look reclaimable, leaving the
20
- * guard inert. We therefore treat ANY foreign
21
- * assignee as a live claim and refuse the take
22
- * unless `--steal` forcibly transfers it. An
23
- * unassigned or self-held Epic still proceeds.
11
+ * (throws, exit non-zero) when a live foreign
12
+ * claim already holds the Epic, naming the
13
+ * current owner. **Claim-time liveness
14
+ * (Story #4019):** `/epic-plan` emits no
15
+ * `story.heartbeat`, so the lease records its
16
+ * own claim-time in a `plan-lease` structured
17
+ * comment on the Epic at acquire time. A
18
+ * foreign claim fresher than the lease TTL
19
+ * refuses (unless `--steal`); a stale or
20
+ * record-less claim is reclaimed
21
+ * automatically. An unassigned or self-held
22
+ * Epic still proceeds.
24
23
  * - `releaseEpicPlanLease` — release the claim after Phase 8 (decompose).
25
24
  * Best-effort and self-scoped: a no-op once the
26
25
  * Epic was reassigned elsewhere.
@@ -35,13 +34,15 @@
35
34
  */
36
35
 
37
36
  import { getGitHub } from '../config/github.js';
37
+ import { resolveLeaseTtlMs } from '../config/limits.js';
38
38
  import { Logger } from '../Logger.js';
39
39
  import { TYPE_LABELS } from '../label-constants.js';
40
40
  import {
41
- acquireLease,
42
- normalizeOperatorHandle,
43
- releaseLease,
44
- } from './ticket-lease.js';
41
+ acquireLeaseFailClosed,
42
+ resolveOperatorFromCandidates,
43
+ } from './lease-guard-shared.js';
44
+ import { currentOwner, releaseLease } from './ticket-lease.js';
45
+ import { findStructuredComment, upsertStructuredComment } from './ticketing.js';
45
46
 
46
47
  /**
47
48
  * Resolve the operator handle that owns this `/epic-plan` run from
@@ -52,32 +53,115 @@ import {
52
53
  * (`acquireEpicPlanLease`) then fails closed by throwing rather than running an
53
54
  * ownerless, unguarded plan.
54
55
  *
55
- * The `@`-prefix some operators carry on `operatorHandle` is stripped so the
56
- * value matches the bare login GitHub writes to (and returns from) a ticket's
57
- * `assignees` otherwise the assignee PATCH is rejected (HTTP 422, invalid
58
- * assignee) and the self-held-claim comparison (`owner === operator`) never
59
- * matches. This mirrors the sibling lease guards
60
- * (`single-story-lease-guard.js`, `epic-deliver-lease-guard.js`).
56
+ * The `@`-prefix some operators carry on `operatorHandle` is stripped (via
57
+ * the shared lease-guard kernel) so the value matches the bare login GitHub
58
+ * writes to (and returns from) a ticket's `assignees` otherwise the
59
+ * assignee PATCH is rejected (HTTP 422, invalid assignee) and the
60
+ * self-held-claim comparison (`owner === operator`) never matches.
61
+ *
62
+ * The plan surface's missing-handle policy is `'null'` (intentional
63
+ * divergence from the standalone path's `'throw'`): `releaseEpicPlanLease`
64
+ * is best-effort and must degrade to a `no-operator` no-op rather than
65
+ * throw, so the throw-on-missing decision lives in `acquireEpicPlanLease`.
61
66
  *
62
67
  * @param {object} config Resolved config bag.
63
68
  * @returns {string|null}
64
69
  */
65
70
  export function resolveOperator(config) {
66
- return normalizeOperatorHandle(getGitHub(config).operatorHandle);
71
+ return resolveOperatorFromCandidates({
72
+ candidates: [getGitHub(config).operatorHandle],
73
+ });
74
+ }
75
+
76
+ /**
77
+ * Structured-comment type carrying the plan-lease claim-time record.
78
+ * Registered in `ticketing/reads.js` `STRUCTURED_COMMENT_TYPES`.
79
+ */
80
+ export const PLAN_LEASE_COMMENT_TYPE = 'plan-lease';
81
+
82
+ /**
83
+ * Render the `plan-lease` structured-comment body: a one-line human
84
+ * summary plus the canonical fenced-JSON record `parsePlanLeaseClaim`
85
+ * reads back.
86
+ *
87
+ * @param {{ epicId: number, owner: string, claimedAt: string }} input
88
+ * `claimedAt` is an ISO-8601 timestamp.
89
+ * @returns {string}
90
+ */
91
+ export function buildPlanLeaseCommentBody({ epicId, owner, claimedAt }) {
92
+ const record = {
93
+ kind: PLAN_LEASE_COMMENT_TYPE,
94
+ epicId,
95
+ owner,
96
+ claimedAt,
97
+ };
98
+ return [
99
+ `### 🔒 Plan Lease — claimed by \`${owner}\``,
100
+ '',
101
+ `This Epic is being planned by \`${owner}\` (claimed ${claimedAt}). A`,
102
+ 'concurrent `/epic-plan` run refuses while this claim is fresher than the',
103
+ 'lease TTL, and reclaims automatically once it goes stale.',
104
+ '',
105
+ '```json',
106
+ JSON.stringify(record, null, 2),
107
+ '```',
108
+ ].join('\n');
109
+ }
110
+
111
+ /**
112
+ * Parse the claim record out of a `plan-lease` comment body. Returns
113
+ * `{ owner, claimedAtMs }` or `null` when the body carries no readable
114
+ * record — which callers treat as "no claim-time recorded" (stale,
115
+ * reclaimable).
116
+ *
117
+ * @param {string|undefined|null} body
118
+ * @returns {{ owner: string, claimedAtMs: number } | null}
119
+ */
120
+ export function parsePlanLeaseClaim(body) {
121
+ if (typeof body !== 'string') return null;
122
+ const match = body.match(/```json\s*\n([\s\S]*?)\n\s*```/);
123
+ if (!match) return null;
124
+ let record;
125
+ try {
126
+ record = JSON.parse(match[1]);
127
+ } catch (_err) {
128
+ return null;
129
+ }
130
+ if (!record || record.kind !== PLAN_LEASE_COMMENT_TYPE) return null;
131
+ const owner =
132
+ typeof record.owner === 'string' && record.owner.length > 0
133
+ ? record.owner
134
+ : null;
135
+ const claimedAtMs = Date.parse(record.claimedAt ?? '');
136
+ if (owner === null || !Number.isFinite(claimedAtMs)) return null;
137
+ return { owner, claimedAtMs };
67
138
  }
68
139
 
69
140
  /**
70
141
  * Acquire the Epic-lease before Phase 7.
71
142
  *
72
- * **Fail-closed (audit #3513).** `/epic-plan` emits no `story.heartbeat`
73
- * during its run, so there is no live-heartbeat source to judge a concurrent
74
- * plan's liveness from. Rather than default liveness to "stale" (which made
75
- * every foreign claim look reclaimable and left this guard inert), we anchor
76
- * `heartbeatAt` to the same `now` the lease primitive evaluates against. That
77
- * makes `isClaimLive` return true for ANY foreign owner, so `acquireLease`
78
- * refuses a foreign assignee unless `steal` is set naming the current owner.
79
- * An unassigned Epic (`unclaimed`) or a self-held claim (`already-held`) still
80
- * proceeds without a write. This mirrors `single-story-lease-guard.js`.
143
+ * **Claim-time liveness (Story #4019, superseding the audit-#3513
144
+ * fail-closed anchor).** `/epic-plan` emits no `story.heartbeat`, so the
145
+ * old guard treated EVERY foreign assignee as live which made the
146
+ * documented "`--steal` once you have confirmed the other run is dead"
147
+ * contract undecidable (there was no in-band liveness signal to confirm
148
+ * against). The lease now records its own claim-time: on every successful
149
+ * acquire the guard upserts a `plan-lease` structured comment on the Epic
150
+ * carrying `{ owner, claimedAt }`. A subsequent run judges a foreign
151
+ * claim's liveness from that claim-time against the lease TTL
152
+ * (`resolveLeaseTtlMs`):
153
+ *
154
+ * - **Fresh foreign claim** (claim-time within TTL) → refuse, naming the
155
+ * owner and the claim age; `--steal` force-transfers.
156
+ * - **Stale foreign claim** (claim-time older than TTL) → reclaim
157
+ * automatically.
158
+ * - **No claim-time record** (foreign assignee but no readable
159
+ * `plan-lease` comment, or the comment names a different owner) →
160
+ * treated as stale and reclaimed — the assignee predates this
161
+ * mechanism or was set out-of-band, so there is nothing to wait on.
162
+ *
163
+ * An unassigned Epic (`unclaimed`) or a self-held claim (`already-held`)
164
+ * proceeds; both refresh the claim-time record.
81
165
  *
82
166
  * A refused claim throws (caught at the CLI boundary → exit non-zero).
83
167
  *
@@ -85,7 +169,7 @@ export function resolveOperator(config) {
85
169
  * @param {import('../ITicketingProvider.js').ITicketingProvider} args.provider
86
170
  * @param {number} args.epicId
87
171
  * @param {object} [args.config]
88
- * @param {boolean} [args.steal=false] Force-transfer a foreign claim.
172
+ * @param {boolean} [args.steal=false] Force-transfer a live foreign claim.
89
173
  * @param {number} [args.now] Injectable clock (epoch ms; tests).
90
174
  * @returns {Promise<{ acquired: boolean, owner: string|null, previousOwner: string|null, reason: string }>}
91
175
  */
@@ -108,31 +192,82 @@ export async function acquireEpicPlanLease({
108
192
  );
109
193
  }
110
194
 
111
- // Fail closed: with no live-heartbeat source on the plan path, treat any
112
- // foreign assignee as a live claim by anchoring `heartbeatAt` to the same
113
- // `now` the primitive evaluates against (`isClaimLive` → true for any owner).
114
- // `acquireLease` then refuses a foreign claim unless `steal` is set; an
115
- // unassigned or self-held Epic proceeds without a write.
116
195
  const resolvedNow =
117
196
  typeof now === 'number' && Number.isFinite(now) ? now : Date.now();
118
- const result = await acquireLease({
197
+ const ttlMs = resolveLeaseTtlMs(config);
198
+
199
+ // Resolve the current assignee and the recorded claim-time. The
200
+ // claim-time only counts when the `plan-lease` record names the same
201
+ // owner as the assignee — a mismatched or missing record means the claim
202
+ // has no liveness signal and is treated as stale (reclaimable).
203
+ const ticket = await provider.getTicket(epicId);
204
+ const owner = currentOwner(ticket?.assignees);
205
+ let heartbeatAt = null;
206
+ if (owner !== null && owner !== operator) {
207
+ let claim = null;
208
+ try {
209
+ const comment = await findStructuredComment(
210
+ provider,
211
+ epicId,
212
+ PLAN_LEASE_COMMENT_TYPE,
213
+ );
214
+ claim = comment ? parsePlanLeaseClaim(comment.body) : null;
215
+ } catch (err) {
216
+ Logger.warn(
217
+ `[epic-plan] Could not read plan-lease claim record on #${epicId} ` +
218
+ `(treating foreign claim as stale): ${err.message}`,
219
+ );
220
+ }
221
+ if (claim && claim.owner === owner) {
222
+ heartbeatAt = claim.claimedAtMs;
223
+ }
224
+ }
225
+
226
+ const result = await acquireLeaseFailClosed({
119
227
  provider,
120
228
  ticketId: epicId,
121
229
  operator,
122
- heartbeatAt: resolvedNow,
230
+ heartbeatAt,
123
231
  steal,
124
232
  config,
125
233
  now: resolvedNow,
234
+ renderRefusal: (refused) => {
235
+ const ageMinutes =
236
+ heartbeatAt !== null
237
+ ? Math.round((resolvedNow - heartbeatAt) / 60000)
238
+ : null;
239
+ const ageNote =
240
+ ageMinutes !== null
241
+ ? `Its plan-lease claim is ~${ageMinutes} minute(s) old (TTL ${Math.round(ttlMs / 60000)} minute(s)), so the run is presumed live. `
242
+ : '';
243
+ return (
244
+ `[epic-plan] Epic #${epicId} is currently claimed by '${refused.owner}'. ` +
245
+ `Refusing to plan concurrently — another /epic-plan run owns this Epic. ` +
246
+ `${ageNote}Wait for that run to finish (the claim auto-expires at the ` +
247
+ `lease TTL), or re-run with --steal to forcibly transfer the claim.`
248
+ );
249
+ },
126
250
  });
127
251
 
128
- if (!result.acquired) {
129
- throw new Error(
130
- `[epic-plan] Epic #${epicId} is currently claimed by '${result.owner}'. ` +
131
- `Refusing to plan concurrently — another /epic-plan run owns this Epic ` +
132
- `(the plan path has no heartbeat ledger, so a foreign assignee always ` +
133
- `blocks unless stolen). Wait for that run to finish, or re-run with ` +
134
- `--steal to forcibly transfer the claim once you have confirmed the ` +
135
- `other run is dead.`,
252
+ // Record (or refresh) the claim-time so the next run can judge this
253
+ // claim's liveness. Best-effort: a comment failure degrades to a
254
+ // record-less claim (which a later run treats as stale) — it never
255
+ // fails the plan.
256
+ try {
257
+ await upsertStructuredComment(
258
+ provider,
259
+ epicId,
260
+ PLAN_LEASE_COMMENT_TYPE,
261
+ buildPlanLeaseCommentBody({
262
+ epicId,
263
+ owner: operator,
264
+ claimedAt: new Date(resolvedNow).toISOString(),
265
+ }),
266
+ );
267
+ } catch (err) {
268
+ Logger.warn(
269
+ `[epic-plan] Failed to record plan-lease claim-time on #${epicId} ` +
270
+ `(non-fatal; a later run will treat this claim as stale): ${err.message}`,
136
271
  );
137
272
  }
138
273
 
@@ -8,8 +8,8 @@
8
8
  */
9
9
 
10
10
  import path from 'node:path';
11
- import { PROJECT_ROOT } from '../../../config-resolver.js';
12
11
  import * as gitUtils from '../../../git-utils.js';
12
+ import { PROJECT_ROOT } from '../../../project-root.js';
13
13
  import { forceDrainPendingCleanup } from '../../../worktree/lifecycle/force-drain.js';
14
14
  import { readManifest } from '../../../worktree/lifecycle/pending-cleanup.js';
15
15
  import { sweepStaleStoryWorktrees } from '../../plan-runner/worktree-sweep.js';