mandrel 2.86.0 → 2.88.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 (165) hide show
  1. package/.agents/README.md +29 -0
  2. package/.agents/agents/auditor.md +4 -3
  3. package/.agents/docs/agentrc-reference.json +2 -1
  4. package/.agents/docs/configuration.md +44 -20
  5. package/.agents/docs/workflows.md +1 -1
  6. package/.agents/schemas/agentrc.schema.json +18 -12
  7. package/.agents/schemas/story-deliver-terminal.schema.json +1 -1
  8. package/.agents/schemas/story-progress.schema.json +5 -0
  9. package/.agents/scripts/audit-to-stories.js +3 -2
  10. package/.agents/scripts/boot-sweep.js +3 -2
  11. package/.agents/scripts/check-arch-cycles.js +19 -41
  12. package/.agents/scripts/check-context-budget.js +16 -44
  13. package/.agents/scripts/check-cyclomatic.js +13 -37
  14. package/.agents/scripts/check-dead-exports.js +16 -46
  15. package/.agents/scripts/clean-worktrees.js +1 -6
  16. package/.agents/scripts/coverage-capture.js +23 -3
  17. package/.agents/scripts/deliver-run.js +11 -0
  18. package/.agents/scripts/diagnose-friction.js +3 -3
  19. package/.agents/scripts/lib/baselines/bundle-size-scorer.js +115 -0
  20. package/.agents/scripts/lib/baselines/coverage-updater-cli.js +122 -29
  21. package/.agents/scripts/lib/baselines/crap-updater-cli.js +45 -14
  22. package/.agents/scripts/lib/baselines/duplication-scanner.js +45 -2
  23. package/.agents/scripts/lib/baselines/env-overrides.js +39 -34
  24. package/.agents/scripts/lib/baselines/kernel.js +3 -2
  25. package/.agents/scripts/lib/baselines/kinds/crap.js +14 -38
  26. package/.agents/scripts/lib/baselines/preview-gates.js +4 -1
  27. package/.agents/scripts/lib/baselines/refresh-service.js +101 -22
  28. package/.agents/scripts/lib/baselines/writer.js +5 -1
  29. package/.agents/scripts/lib/bdd-runner-detect.js +7 -10
  30. package/.agents/scripts/lib/child-exec.js +14 -1
  31. package/.agents/scripts/lib/child-stream.js +122 -0
  32. package/.agents/scripts/lib/clean-temp.js +19 -13
  33. package/.agents/scripts/lib/cli-args.js +49 -0
  34. package/.agents/scripts/lib/codex/skills-projection.js +1 -12
  35. package/.agents/scripts/lib/config/gates/coverage.schema.js +1 -1
  36. package/.agents/scripts/lib/config/gates/mutation.schema.js +2 -2
  37. package/.agents/scripts/lib/config/gates/shared.js +1 -1
  38. package/.agents/scripts/lib/config/gh-config-dir.js +63 -0
  39. package/.agents/scripts/lib/config/github.js +2 -0
  40. package/.agents/scripts/lib/config/paths.js +14 -3
  41. package/.agents/scripts/lib/config/quality.js +6 -2
  42. package/.agents/scripts/lib/config-resolver.js +78 -64
  43. package/.agents/scripts/lib/config-settings-schema-delivery.js +4 -3
  44. package/.agents/scripts/lib/config-settings-schema.js +7 -0
  45. package/.agents/scripts/lib/coverage-baseline.js +7 -6
  46. package/.agents/scripts/lib/coverage-capture.js +59 -0
  47. package/.agents/scripts/lib/crap-utils.js +59 -47
  48. package/.agents/scripts/lib/full-suite-lock.js +1 -10
  49. package/.agents/scripts/lib/gates/baseline-store.js +10 -0
  50. package/.agents/scripts/lib/generated/agentrc-validator.js +1 -1
  51. package/.agents/scripts/lib/gh-exec.js +45 -2
  52. package/.agents/scripts/lib/gh-transport.js +27 -0
  53. package/.agents/scripts/lib/git-utils.js +16 -9
  54. package/.agents/scripts/lib/maintainability-engine.js +27 -6
  55. package/.agents/scripts/lib/maintainability-utils.js +14 -1
  56. package/.agents/scripts/lib/observability/source-classifier.js +1 -0
  57. package/.agents/scripts/lib/orchestration/check-baselines/phases/evaluate.js +33 -3
  58. package/.agents/scripts/lib/orchestration/evidence-stream.js +186 -0
  59. package/.agents/scripts/lib/orchestration/file-assumptions.js +2 -19
  60. package/.agents/scripts/lib/orchestration/git-cleanup/phases/branches-reap.js +3 -7
  61. package/.agents/scripts/lib/orchestration/git-cleanup/phases/git-probes-ff.js +34 -40
  62. package/.agents/scripts/lib/orchestration/git-cleanup/phases/git-probes.js +4 -3
  63. package/.agents/scripts/lib/orchestration/plan-persist/persist-helpers.js +2 -19
  64. package/.agents/scripts/lib/orchestration/pr-watch.js +1 -5
  65. package/.agents/scripts/lib/orchestration/project-meta-cache.js +17 -4
  66. package/.agents/scripts/lib/orchestration/reassert-status-column.js +1 -8
  67. package/.agents/scripts/lib/orchestration/resolve-stories.js +1 -0
  68. package/.agents/scripts/lib/orchestration/review-providers/native.js +2 -2
  69. package/.agents/scripts/lib/orchestration/single-story-close/phase-timer.js +43 -0
  70. package/.agents/scripts/lib/orchestration/single-story-close/phases/confirm-merge.js +37 -13
  71. package/.agents/scripts/lib/orchestration/single-story-close/phases/finish.js +264 -0
  72. package/.agents/scripts/lib/orchestration/single-story-close/phases/open-pr.js +68 -0
  73. package/.agents/scripts/lib/orchestration/single-story-close/phases/pre-push.js +106 -0
  74. package/.agents/scripts/lib/orchestration/single-story-close/phases/reconcile-done.js +43 -35
  75. package/.agents/scripts/lib/orchestration/single-story-close/phases/review-halt.js +52 -0
  76. package/.agents/scripts/lib/orchestration/single-story-close/result.js +273 -0
  77. package/.agents/scripts/lib/orchestration/single-story-close/runner.js +68 -776
  78. package/.agents/scripts/lib/orchestration/story-close/context-budget-writeback.js +20 -9
  79. package/.agents/scripts/lib/orchestration/story-close/duplication-close-rescore.js +2 -2
  80. package/.agents/scripts/lib/orchestration/story-close/ratchet-shrink-writeback.js +20 -12
  81. package/.agents/scripts/lib/orchestration/story-handoff.js +50 -21
  82. package/.agents/scripts/lib/orchestration/story-progress.js +32 -12
  83. package/.agents/scripts/lib/orchestration/ticket-validator.js +3 -13
  84. package/.agents/scripts/lib/qa/resolve-selection.js +1 -8
  85. package/.agents/scripts/lib/single-story/confirm-and-land.js +33 -0
  86. package/.agents/scripts/lib/single-story-sweep/reconcile-closing.js +43 -11
  87. package/.agents/scripts/lib/single-story-sweep/sweep-lock.js +130 -30
  88. package/.agents/scripts/lib/skills/parse-skill.js +1 -4
  89. package/.agents/scripts/lib/story-body/story-body.js +12 -1
  90. package/.agents/scripts/lib/suite-admission.js +3 -11
  91. package/.agents/scripts/lib/temp-retention.js +25 -11
  92. package/.agents/scripts/lib/test-run-credit.js +2 -5
  93. package/.agents/scripts/lib/transpile.js +193 -31
  94. package/.agents/scripts/lib/util/paths.js +31 -0
  95. package/.agents/scripts/lib/util/sleep.js +15 -0
  96. package/.agents/scripts/lib/validation-evidence.js +29 -3
  97. package/.agents/scripts/lib/wave-runner/live-probe.js +24 -0
  98. package/.agents/scripts/lib/workers/crap-worker.js +23 -12
  99. package/.agents/scripts/lib/workers/maintainability-report-worker.js +18 -12
  100. package/.agents/scripts/lib/workflow-closure.js +1 -8
  101. package/.agents/scripts/notify.js +83 -90
  102. package/.agents/scripts/plan-context.js +8 -4
  103. package/.agents/scripts/providers/github/auth.js +14 -30
  104. package/.agents/scripts/providers/github/blocked-by-add.js +11 -27
  105. package/.agents/scripts/providers/github/compose.js +1 -0
  106. package/.agents/scripts/providers/github/gh-token-diagnostics.js +130 -0
  107. package/.agents/scripts/providers/github/identity.js +158 -0
  108. package/.agents/scripts/providers/github/issues.js +2 -2
  109. package/.agents/scripts/providers/github/mappers.js +16 -19
  110. package/.agents/scripts/providers/github/projects-v2-graphql.js +4 -36
  111. package/.agents/scripts/providers/github/request-helpers.js +44 -0
  112. package/.agents/scripts/providers/github/sub-issue-add.js +11 -28
  113. package/.agents/scripts/providers/github.js +10 -2
  114. package/.agents/scripts/quality-preview.js +6 -19
  115. package/.agents/scripts/single-story-confirm-merge.js +8 -20
  116. package/.agents/scripts/single-story-init.js +0 -1
  117. package/.agents/scripts/stories-wave-tick.js +60 -46
  118. package/.agents/scripts/story-handoff.js +2 -23
  119. package/.agents/scripts/story-review-compute.js +3 -22
  120. package/.agents/scripts/sync-claude-agents.js +1 -12
  121. package/.agents/scripts/sync-claude-commands.js +1 -12
  122. package/.agents/scripts/update-bundle-size-baseline.js +56 -0
  123. package/.agents/scripts/update-coverage-baseline.js +16 -41
  124. package/.agents/scripts/update-crap-baseline.js +10 -19
  125. package/.agents/scripts/update-duplication-baseline.js +13 -3
  126. package/.agents/scripts/update-ticket-state.js +44 -45
  127. package/.agents/templates/docs/audit-sweep-runbook.md +1 -1
  128. package/.agents/workflows/audit-accessibility.md +1 -1
  129. package/.agents/workflows/audit-adrs.md +1 -1
  130. package/.agents/workflows/audit-architecture.md +1 -1
  131. package/.agents/workflows/audit-baselines.md +2 -2
  132. package/.agents/workflows/audit-clean-code.md +1 -1
  133. package/.agents/workflows/audit-data-model.md +1 -1
  134. package/.agents/workflows/audit-dependencies.md +1 -1
  135. package/.agents/workflows/audit-devops.md +1 -1
  136. package/.agents/workflows/audit-documentation.md +1 -1
  137. package/.agents/workflows/audit-exceptions.md +2 -2
  138. package/.agents/workflows/audit-mobile.md +1 -1
  139. package/.agents/workflows/audit-navigability.md +1 -1
  140. package/.agents/workflows/audit-performance.md +1 -1
  141. package/.agents/workflows/audit-privacy.md +1 -1
  142. package/.agents/workflows/audit-quality.md +1 -1
  143. package/.agents/workflows/audit-security.md +1 -1
  144. package/.agents/workflows/audit-seo.md +1 -1
  145. package/.agents/workflows/audit-sre.md +1 -1
  146. package/.agents/workflows/audit-to-stories.md +9 -7
  147. package/.agents/workflows/audit-ux-ui.md +1 -1
  148. package/.agents/workflows/helpers/audit-lens-core.md +8 -5
  149. package/.agents/workflows/helpers/deliver-digest.md +4 -5
  150. package/.agents/workflows/helpers/deliver-story-reference.md +4 -4
  151. package/.agents/workflows/mandrel-deliver.md +3 -1
  152. package/README.md +6 -3
  153. package/docs/CHANGELOG.md +36 -0
  154. package/lib/cli/doctor-checks/codex.js +240 -0
  155. package/lib/cli/doctor-checks/merge-driver.js +79 -0
  156. package/lib/cli/doctor-checks/payload.js +201 -0
  157. package/lib/cli/doctor-checks/read-json.js +16 -0
  158. package/lib/cli/doctor-checks/runtime-deps.js +146 -0
  159. package/lib/cli/doctor-checks/spawn.js +18 -0
  160. package/lib/cli/doctor-checks/sync-parity.js +205 -0
  161. package/lib/cli/doctor-checks/test-credit-path.js +270 -0
  162. package/lib/cli/doctor-checks/toolchain.js +137 -0
  163. package/lib/cli/doctor-checks/version-current.js +82 -0
  164. package/lib/cli/registry.js +40 -1330
  165. package/package.json +7 -1
@@ -22,27 +22,22 @@ export function isWorktreeLockFailure(stderr) {
22
22
  return WORKTREE_LOCK_RE.test(stderr ?? '');
23
23
  }
24
24
 
25
- /* node:coverage ignore next */
26
25
  export function isWorkingTreeClean(cwd) {
27
26
  return defaultFfProbes.isClean(cwd);
28
27
  }
29
28
 
30
- /* node:coverage ignore next */
31
29
  export function fetchRef(cwd, remoteName, ref) {
32
30
  return defaultFfProbes.fetch(cwd, remoteName, ref);
33
31
  }
34
32
 
35
- /* node:coverage ignore next */
36
33
  export function canFastForward(cwd, baseBranch, remoteName) {
37
34
  return defaultFfProbes.canFastForward(cwd, baseBranch, remoteName);
38
35
  }
39
36
 
40
- /* node:coverage ignore next */
41
37
  export function checkoutBranch(cwd, branch) {
42
38
  return defaultFfProbes.checkout(cwd, branch);
43
39
  }
44
40
 
45
- /* node:coverage ignore next */
46
41
  export function mergeFastForward(cwd, ref) {
47
42
  return defaultFfProbes.merge(cwd, ref);
48
43
  }
@@ -116,42 +111,41 @@ export function makeFfProbes(spawn = gitSpawn) {
116
111
 
117
112
  const defaultFfProbes = makeFfProbes(gitSpawn);
118
113
 
119
- /* node:coverage ignore next */
120
- export function removeWorktree(worktreePath, cwd) {
121
- const plain = gitSpawn(cwd, 'worktree', 'remove', worktreePath);
122
- if (plain.status === 0) return { ok: true, dirty: false };
123
- const forced = gitSpawn(cwd, 'worktree', 'remove', '--force', worktreePath);
124
- if (forced.status === 0) {
125
- return { ok: true, dirty: true, stderr: plain.stderr };
126
- }
127
- const stderr = forced.stderr || plain.stderr;
128
- return {
129
- ok: false,
130
- dirty: true,
131
- lockClass: isWorktreeLockFailure(stderr),
132
- stderr,
133
- };
134
- }
135
-
136
114
  /**
137
- * Never `--quiet`: it suppresses the `[deleted]` lines that are the only
138
- * record of what was pruned, making real work read as "nothing to do".
115
+ * The destructive worktree / prune / stash wrappers over an injected spawn,
116
+ * like {@link makeFfProbes}.
139
117
  *
140
- * @param {string} cwd
141
- * @param {string} remoteName
142
- * @param {(output: string, remoteName: string) => string[]} parsePruneFn
143
- * @returns {{ ok: boolean, pruned: string[], stderr?: string }}
118
+ * @param {(cwd: string, ...args: string[]) => { status: number, stdout: string, stderr: string }} [spawn]
144
119
  */
145
- /* node:coverage ignore next */
146
- export function pruneRemoteTracking(cwd, remoteName, parsePruneFn) {
147
- const res = gitSpawn(cwd, 'fetch', '--prune', remoteName);
148
- if (res.status !== 0) return { ok: false, pruned: [], stderr: res.stderr };
149
- return { ok: true, pruned: parsePruneFn(res.stderr, remoteName) };
150
- }
151
-
152
- /* node:coverage ignore next */
153
- export function dropStash(ref, cwd) {
154
- const res = gitSpawn(cwd, 'stash', 'drop', ref);
155
- if (res.status !== 0) return { ok: false, stderr: res.stderr };
156
- return { ok: true };
120
+ export function makeWorktreeOps(spawn = gitSpawn) {
121
+ return {
122
+ removeWorktree: (worktreePath, cwd) => {
123
+ const plain = spawn(cwd, 'worktree', 'remove', worktreePath);
124
+ if (plain.status === 0) return { ok: true, dirty: false };
125
+ const forced = spawn(cwd, 'worktree', 'remove', '--force', worktreePath);
126
+ if (forced.status === 0) {
127
+ return { ok: true, dirty: true, stderr: plain.stderr };
128
+ }
129
+ const stderr = forced.stderr || plain.stderr;
130
+ return {
131
+ ok: false,
132
+ dirty: true,
133
+ lockClass: isWorktreeLockFailure(stderr),
134
+ stderr,
135
+ };
136
+ },
137
+ // Never `--quiet`: it hides the `[deleted]` lines the parser reads.
138
+ pruneRemoteTracking: (cwd, remoteName, parsePruneFn) => {
139
+ const res = spawn(cwd, 'fetch', '--prune', remoteName);
140
+ if (res.status !== 0) {
141
+ return { ok: false, pruned: [], stderr: res.stderr };
142
+ }
143
+ return { ok: true, pruned: parsePruneFn(res.stderr, remoteName) };
144
+ },
145
+ dropStash: (ref, cwd) => {
146
+ const res = spawn(cwd, 'stash', 'drop', ref);
147
+ if (res.status !== 0) return { ok: false, stderr: res.stderr };
148
+ return { ok: true };
149
+ },
150
+ };
157
151
  }
@@ -9,19 +9,20 @@ import { execFileSync } from 'node:child_process';
9
9
 
10
10
  import { gitSpawn } from '../../../git-utils.js';
11
11
  import { parseWorktreePorcelain } from '../../../worktree-manager.js';
12
+ import { makeWorktreeOps } from './git-probes-ff.js';
12
13
  import { resolveMergedTip } from './merged-tip.js';
13
14
 
14
15
  export {
15
16
  canFastForward,
16
17
  checkoutBranch,
17
- dropStash,
18
18
  fetchRef,
19
19
  isWorkingTreeClean,
20
20
  mergeFastForward,
21
- pruneRemoteTracking,
22
- removeWorktree,
23
21
  } from './git-probes-ff.js';
24
22
 
23
+ export const { removeWorktree, pruneRemoteTracking, dropStash } =
24
+ makeWorktreeOps(gitSpawn);
25
+
25
26
  /* node:coverage ignore next */
26
27
  export function listLocalBranches(cwd) {
27
28
  const res = gitSpawn(
@@ -4,7 +4,7 @@
4
4
  * @module lib/orchestration/plan-persist/persist-helpers
5
5
  */
6
6
 
7
- import { gitSpawn } from '../../git-utils.js';
7
+ import { gitSpawn, pathExistsAtBase } from '../../git-utils.js';
8
8
  import { validateTaskBodies } from '../task-body-validator.js';
9
9
  import { validateAndNormalizeTickets } from '../ticket-validator.js';
10
10
  import { normalizeAcceptanceHandles } from './acceptance-handle-repair.js';
@@ -47,23 +47,6 @@ function resolveProbeRef({ baseBranch, cwd }) {
47
47
  return candidates.find((ref) => refResolves(ref, repo)) ?? null;
48
48
  }
49
49
 
50
- /**
51
- * The validator gates' own probe, so repair and gates agree on what is at
52
- * base.
53
- *
54
- * @param {{ baseBranchRef: string, path: string, cwd?: string }} opts
55
- * @returns {boolean}
56
- */
57
- function defaultGitRunner({ baseBranchRef, path, cwd }) {
58
- const result = gitSpawn(
59
- cwd ?? process.cwd(),
60
- 'cat-file',
61
- '-e',
62
- `${baseBranchRef}:${path}`,
63
- );
64
- return result.status === 0;
65
- }
66
-
67
50
  /**
68
51
  * With no base to read, a bare bullet is taken as an in-place edit.
69
52
  *
@@ -72,7 +55,7 @@ function defaultGitRunner({ baseBranchRef, path, cwd }) {
72
55
  */
73
56
  function makeExistsAtBase({ baseBranchRef, cwd, gitRunner }) {
74
57
  if (baseBranchRef === null) return () => true;
75
- const runner = gitRunner ?? defaultGitRunner;
58
+ const runner = gitRunner ?? pathExistsAtBase;
76
59
  return (path) => Boolean(runner({ baseBranchRef, path, cwd }));
77
60
  }
78
61
 
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  import { spawnSync } from 'node:child_process';
9
-
9
+ import { defaultSleep } from '../util/sleep.js';
10
10
  import { applyBehindUpdate } from './behind-recovery.js';
11
11
  import { createCancelledRunDiscount } from './cancelled-run-discount.js';
12
12
  import { checkVerdict } from './check-state.js';
@@ -88,10 +88,6 @@ function allGreen(outcomes) {
88
88
  return values.every((v) => checkVerdict(v) === 'pass');
89
89
  }
90
90
 
91
- function defaultSleep(ms) {
92
- return new Promise((resolve) => setTimeout(resolve, ms));
93
- }
94
-
95
91
  /**
96
92
  * Never poll an empty name set: `allTerminal({})` is vacuously true and would
97
93
  * report a red verdict with no failing check. `gh` overloads non-zero for
@@ -56,6 +56,21 @@ function readCacheFile(filePath) {
56
56
  }
57
57
  }
58
58
 
59
+ /** Tmp + rename: a reader never sees a partial write. Throws; reaps the tmp. */
60
+ function writeStoreAtomic(filePath, store) {
61
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
62
+ const tmp = `${filePath}.${process.pid}.tmp`;
63
+ try {
64
+ fs.writeFileSync(tmp, `${JSON.stringify(store, null, 2)}\n`, 'utf8');
65
+ fs.renameSync(tmp, filePath);
66
+ } catch (err) {
67
+ try {
68
+ fs.unlinkSync(tmp);
69
+ } catch {}
70
+ throw err;
71
+ }
72
+ }
73
+
59
74
  /**
60
75
  * A fresh entry with `options` re-hydrated to a Map, else `null`. Never throws.
61
76
  *
@@ -137,8 +152,7 @@ export function writeProjectMetaCache(opts = {}) {
137
152
  fieldId: meta.fieldId,
138
153
  options,
139
154
  };
140
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
141
- fs.writeFileSync(filePath, `${JSON.stringify(store, null, 2)}\n`, 'utf8');
155
+ writeStoreAtomic(filePath, store);
142
156
  return true;
143
157
  } catch {
144
158
  return false;
@@ -164,8 +178,7 @@ export function invalidateProjectMetaCache(opts = {}) {
164
178
  const store = readCacheFile(filePath);
165
179
  if (!(key in store)) return false;
166
180
  delete store[key];
167
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
168
- fs.writeFileSync(filePath, `${JSON.stringify(store, null, 2)}\n`, 'utf8');
181
+ writeStoreAtomic(filePath, store);
169
182
  return true;
170
183
  } catch {
171
184
  return false;
@@ -5,6 +5,7 @@
5
5
  * win that race; a one-shot mutation routinely lost it.
6
6
  */
7
7
 
8
+ import { defaultSleep } from '../util/sleep.js';
8
9
  import { ColumnSync, columnForLabels } from './column-sync.js';
9
10
 
10
11
  /** Four attempts at 5 s cover a ~15 s window past typical bot timing. */
@@ -12,14 +13,6 @@ const DEFAULT_POLL_ATTEMPTS = 4;
12
13
 
13
14
  const DEFAULT_POLL_DELAY_MS = 5000;
14
15
 
15
- /**
16
- * @param {number} ms
17
- * @returns {Promise<void>}
18
- */
19
- function defaultSleep(ms) {
20
- return new Promise((resolve) => setTimeout(resolve, ms));
21
- }
22
-
23
16
  function requirePositiveInt(value, name) {
24
17
  if (!Number.isInteger(value) || value <= 0) {
25
18
  throw new TypeError(
@@ -74,6 +74,7 @@ export function toStoryRecord(issue, requestedId, { allowUnlabelled } = {}) {
74
74
  url: issue?.html_url ?? issue?.url ?? null,
75
75
  labels,
76
76
  state: String(issue?.state ?? 'open').toLowerCase(),
77
+ stateReason: issue?.stateReason ?? issue?.state_reason ?? null,
77
78
  // The sole assignee is the Story lease holder; the live probe reads it to
78
79
  // withhold a Story another operator holds.
79
80
  assignees: Array.isArray(issue?.assignees)
@@ -17,7 +17,7 @@ import {
17
17
  classifyReport,
18
18
  } from '../../maintainability-engine.js';
19
19
  import { PROJECT_ROOT } from '../../project-root.js';
20
- import { transpileIfNeeded } from '../../transpile.js';
20
+ import { transpileForReport } from '../../transpile.js';
21
21
  import {
22
22
  resolveMaintainabilityIgnoreGlobs,
23
23
  scopeMaintainabilityFiles,
@@ -57,7 +57,7 @@ export function readHeadSource(relPath, headRef, gitSpawnFn = gitSpawn) {
57
57
  * @returns {ReturnType<typeof calculateReport>}
58
58
  */
59
59
  export function scoreSourceReport(source, relPath) {
60
- const prepared = transpileIfNeeded(relPath, source);
60
+ const prepared = transpileForReport(relPath, source);
61
61
  if (prepared === null) {
62
62
  return {
63
63
  moduleScore: 0,
@@ -0,0 +1,43 @@
1
+ import { Logger } from '../../Logger.js';
2
+
3
+ const progress = Logger.createProgress('single-story-close', { stderr: true });
4
+
5
+ /**
6
+ * Wall-clock seconds per named phase; each transition logs the phase it ends.
7
+ * Overlapping work `pause`s the clock and `record`s its own time.
8
+ *
9
+ * @param {() => number} [nowMs]
10
+ */
11
+ export function createPhaseTimer(nowMs = Date.now) {
12
+ const durations = {};
13
+ let current = null;
14
+ let since = 0;
15
+ const record = (phase, ms) => {
16
+ const seconds = Math.round(Math.max(0, ms) / 100) / 10;
17
+ durations[phase] = (durations[phase] ?? 0) + seconds;
18
+ progress('TIMING', `⏱ ${phase}: ${seconds}s`);
19
+ };
20
+ const end = () => {
21
+ if (current === null) return;
22
+ record(current, nowMs() - since);
23
+ current = null;
24
+ };
25
+ return {
26
+ enter(phase) {
27
+ end();
28
+ if (phase === 'init') return;
29
+ current = phase;
30
+ since = nowMs();
31
+ },
32
+ record,
33
+ pause: end,
34
+ finish() {
35
+ end();
36
+ return Object.keys(durations).length > 0 ? { ...durations } : null;
37
+ },
38
+ stamp(terminal) {
39
+ const phaseDurations = this.finish();
40
+ if (phaseDurations) terminal.phaseDurations = phaseDurations;
41
+ },
42
+ };
43
+ }
@@ -5,8 +5,11 @@
5
5
  * Two timing domains: `maxWaitSeconds` bounds THIS invocation (it must fit
6
6
  * the host's ~10-min tool ceiling after the close gates) and expires to a
7
7
  * resumable `pending` with no mutation; `maxBudgetSeconds` bounds the
8
- * cumulative wait, anchored at the PR's `createdAt` so resumes don't restart
9
- * it, and exhausting it is the real give-up (classify, emit, block). The
8
+ * cumulative wait, anchored at the latest auto-merge arm
9
+ * (`autoMergeRequest.enabledAt`, else the PR's `createdAt`): every close
10
+ * re-arms, so a re-close gets a fresh budget, while a `--wait` resume does
11
+ * not re-arm and keeps the clock. Exhausting it is the real give-up
12
+ * (classify, emit, block). The
10
13
  * poll is stateless and re-entrant. Async mode probes once (see
11
14
  * ASYNC_PROBE_WINDOW_SECONDS). Checks are probed every iteration so a red required
12
15
  * check fails fast instead of burning the budget.
@@ -25,6 +28,7 @@ import {
25
28
  readPrMergeState as defaultReadPrMergeState,
26
29
  } from '../../../single-story/confirm-merge.js';
27
30
  import { pollUntil } from '../../../util/poll-loop.js';
31
+ import { defaultSleep } from '../../../util/sleep.js';
28
32
  import { applyBehindUpdate } from '../../behind-recovery.js';
29
33
  import { isRerunPermitted } from '../../check-state.js';
30
34
  import { recordRequiredRed as defaultRecordRequiredRed } from '../../ci-red-handling.js';
@@ -78,7 +82,7 @@ export const DEFAULT_UPDATE_ATTEMPTS = 3;
78
82
 
79
83
  /**
80
84
  * Minimum polls before the cumulative budget may block: the clock is anchored
81
- * at `createdAt`, so a long-open PR is over budget on its first probe and
85
+ * at the latest arm, so a long-armed PR is over budget on its first probe and
82
86
  * would otherwise block without ever having waited.
83
87
  */
84
88
  export const MIN_POLLS_BEFORE_BUDGET_BLOCK = 2;
@@ -88,10 +92,6 @@ const CLOSED_UNMERGED_BLOCK_CLASS = 'api-race-other';
88
92
  const CLOSED_UNMERGED_REASON =
89
93
  'PR probe error: PR closed without merging (state=CLOSED)';
90
94
 
91
- function defaultSleep(ms) {
92
- return new Promise((resolve) => setTimeout(resolve, ms));
93
- }
94
-
95
95
  /** Spawn-level timeout kills a wedged `gh` child; injected `gh`s are bounded by withGhTimeout. */
96
96
  const defaultGh = createGh(undefined, { timeoutMs: MERGE_WAIT_GH_TIMEOUT_MS });
97
97
 
@@ -139,6 +139,17 @@ function readAutoMergeArmed(view) {
139
139
  return view.autoMergeRequest != null;
140
140
  }
141
141
 
142
+ /**
143
+ * When auto-merge was last armed — the budget anchor; `null` when disarmed
144
+ * or unreadable, so the anchor falls back to `createdAt`.
145
+ *
146
+ * @param {object|null|undefined} view
147
+ * @returns {string|null}
148
+ */
149
+ function readAutoMergeEnabledAt(view) {
150
+ return readString(view?.autoMergeRequest?.enabledAt, null);
151
+ }
152
+
142
153
  /**
143
154
  * `inMergeQueue` for a green open PR, the only state GitHub enqueues from;
144
155
  * `undefined` otherwise, so the common poll pays no extra call. An enqueued
@@ -197,6 +208,7 @@ export async function readPrWaitProbe({
197
208
  state: readString(view?.state, null),
198
209
  mergedAt: readString(view?.mergedAt, null),
199
210
  createdAt: readString(view?.createdAt, null),
211
+ autoMergeEnabledAt: readAutoMergeEnabledAt(view),
200
212
  mergeStateStatus: readString(view?.mergeStateStatus),
201
213
  reviewDecision: readString(view?.reviewDecision),
202
214
  checksStatus,
@@ -279,16 +291,27 @@ export function resolveMergeWaitConfig(
279
291
  };
280
292
  }
281
293
 
294
+ function parseTimestampMs(value) {
295
+ if (typeof value !== 'string' || !value) return null;
296
+ const parsed = Date.parse(value);
297
+ return Number.isFinite(parsed) ? parsed : null;
298
+ }
299
+
282
300
  /**
283
- * Budget anchor; without `createdAt` it falls back to this invocation's start
284
- * (worst case a fresh budget, never a premature block).
301
+ * Budget anchor: the later of `createdAt` and the auto-merge arm time
302
+ * (`enabledAt`), so a re-close that re-arms starts a fresh budget. With
303
+ * neither readable it falls back to this invocation's start (worst case a
304
+ * fresh budget, never a premature block).
285
305
  *
306
+ * @param {{ createdAt?: string|null, enabledAt?: string|null, fallbackMs: number }} args
286
307
  * @returns {number} epoch ms
287
308
  */
288
- export function resolveBudgetAnchorMs({ createdAt, fallbackMs }) {
289
- if (typeof createdAt !== 'string' || !createdAt) return fallbackMs;
290
- const parsed = Date.parse(createdAt);
291
- return Number.isFinite(parsed) ? parsed : fallbackMs;
309
+ export function resolveBudgetAnchorMs({ createdAt, enabledAt, fallbackMs }) {
310
+ const anchors = [
311
+ parseTimestampMs(createdAt),
312
+ parseTimestampMs(enabledAt),
313
+ ].filter((ms) => ms !== null);
314
+ return anchors.length > 0 ? Math.max(...anchors) : fallbackMs;
292
315
  }
293
316
 
294
317
  /**
@@ -1081,6 +1104,7 @@ export function decideMergeWaitPoll({
1081
1104
  }) {
1082
1105
  const anchorMs = resolveBudgetAnchorMs({
1083
1106
  createdAt: probe.createdAt,
1107
+ enabledAt: probe.autoMergeEnabledAt,
1084
1108
  fallbackMs: state.startedAtMs,
1085
1109
  });
1086
1110
  const waitedMs = nowMs - state.startedAtMs;