architecture-harness 0.25.0 → 0.26.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 (244) hide show
  1. package/package.json +3 -22
  2. package/src/cli/ask.mjs +42 -0
  3. package/src/cli/ask.test.mjs +40 -0
  4. package/src/cli/branch-sync.mjs +126 -0
  5. package/src/cli/branch-sync.test.mjs +428 -0
  6. package/src/cli/budget-guard.mjs +74 -5
  7. package/src/cli/budget-guard.test.mjs +185 -2
  8. package/src/cli/budget-report.mjs +85 -0
  9. package/src/cli/budget-report.test.mjs +173 -0
  10. package/src/cli/budget.mjs +8 -5
  11. package/src/cli/check-commands.mjs +123 -11
  12. package/src/cli/check-commands.test.mjs +360 -6
  13. package/src/cli/check-feature-switches.test.mjs +110 -0
  14. package/src/cli/check-full.test.mjs +152 -0
  15. package/src/cli/check-verdict.mjs +46 -0
  16. package/src/cli/check-verdict.test.mjs +32 -0
  17. package/src/cli/check.mjs +137 -4
  18. package/src/cli/check.test.mjs +45 -0
  19. package/src/cli/checkpoint.mjs +143 -0
  20. package/src/cli/checkpoint.test.mjs +146 -0
  21. package/src/cli/child-run.mjs +7 -2
  22. package/src/cli/child-run.test.mjs +17 -2
  23. package/src/cli/ci-failures.mjs +304 -0
  24. package/src/cli/ci-failures.test.mjs +615 -0
  25. package/src/cli/ci-preview.mjs +62 -0
  26. package/src/cli/ci-preview.test.mjs +85 -0
  27. package/src/cli/ci-state.mjs +25 -29
  28. package/src/cli/ci-state.test.mjs +174 -2
  29. package/src/cli/ci-template.test.mjs +86 -0
  30. package/src/cli/claude-core.test.mjs +37 -0
  31. package/src/cli/codex-implement.mjs +580 -47
  32. package/src/cli/codex-implement.test.mjs +1946 -29
  33. package/src/cli/codex-limit.mjs +85 -0
  34. package/src/cli/codex-limit.test.mjs +179 -0
  35. package/src/cli/codex-reset.mjs +78 -0
  36. package/src/cli/codex-reset.test.mjs +192 -0
  37. package/src/cli/codex-verify.mjs +230 -0
  38. package/src/cli/codex-verify.test.mjs +402 -0
  39. package/src/cli/comment-watch.mjs +391 -0
  40. package/src/cli/comment-watch.test.mjs +392 -0
  41. package/src/cli/commit-guard.mjs +219 -0
  42. package/src/cli/commit-guard.test.mjs +809 -0
  43. package/src/cli/context-budgets.mjs +47 -0
  44. package/src/cli/context-budgets.test.mjs +96 -0
  45. package/src/cli/context-watch.mjs +116 -0
  46. package/src/cli/context-watch.test.mjs +395 -0
  47. package/src/cli/contract-history.mjs +1 -1
  48. package/src/cli/controller-guard.mjs +38 -0
  49. package/src/cli/controller-guard.test.mjs +66 -2
  50. package/src/cli/cost-stop.mjs +5 -1
  51. package/src/cli/cost-stop.test.mjs +26 -0
  52. package/src/cli/diff-shape.mjs +162 -0
  53. package/src/cli/diff-shape.test.mjs +200 -0
  54. package/src/cli/direct-lane.mjs +32 -19
  55. package/src/cli/direct-lane.test.mjs +21 -3
  56. package/src/cli/dispatch-guard.mjs +11 -0
  57. package/src/cli/dispatch-guard.test.mjs +145 -10
  58. package/src/cli/dispatch-reminder.mjs +23 -10
  59. package/src/cli/dispatch-reminder.test.mjs +63 -11
  60. package/src/cli/dispatch-route.mjs +54 -0
  61. package/src/cli/dispatch-route.test.mjs +101 -0
  62. package/src/cli/dispatch-slot.mjs +2 -2
  63. package/src/cli/dispatch-slot.test.mjs +18 -19
  64. package/src/cli/dispatch.test.mjs +3 -3
  65. package/src/cli/e2e-affected.mjs +15 -0
  66. package/src/cli/e2e-affected.test.mjs +73 -0
  67. package/src/cli/fleet.mjs +180 -0
  68. package/src/cli/fleet.test.mjs +213 -0
  69. package/src/cli/gate-slot.mjs +2 -1
  70. package/src/cli/gh-merge-command.mjs +96 -31
  71. package/src/cli/gh-merge-command.test.mjs +74 -0
  72. package/src/cli/gh-run.mjs +64 -3
  73. package/src/cli/gh-run.test.mjs +66 -1
  74. package/src/cli/git-hooks.test.mjs +25 -3
  75. package/src/cli/git-read.mjs +2 -1
  76. package/src/cli/integrate-tree.mjs +47 -0
  77. package/src/cli/integrate.mjs +143 -40
  78. package/src/cli/integrate.test.mjs +347 -1
  79. package/src/cli/integration-lane.mjs +62 -0
  80. package/src/cli/integration-lane.test.mjs +213 -0
  81. package/src/cli/ledger.mjs +4 -2
  82. package/src/cli/ledger.test.mjs +12 -3
  83. package/src/cli/legs.mjs +53 -0
  84. package/src/cli/legs.test.mjs +70 -0
  85. package/src/cli/lint-changed.mjs +59 -10
  86. package/src/cli/lint-changed.test.mjs +112 -1
  87. package/src/cli/locked-tests.mjs +134 -0
  88. package/src/cli/locked-tests.test.mjs +188 -0
  89. package/src/cli/merge-guard.mjs +234 -62
  90. package/src/cli/merge-guard.test.mjs +768 -39
  91. package/src/cli/metrics.mjs +53 -0
  92. package/src/cli/metrics.test.mjs +73 -0
  93. package/src/cli/milestone.mjs +297 -0
  94. package/src/cli/milestone.test.mjs +294 -0
  95. package/src/cli/mutate.mjs +203 -0
  96. package/src/cli/mutate.test.mjs +134 -0
  97. package/src/cli/output-redaction.mjs +26 -0
  98. package/src/cli/output-redaction.test.mjs +117 -0
  99. package/src/cli/overview.mjs +131 -0
  100. package/src/cli/overview.test.mjs +137 -0
  101. package/src/cli/pause.mjs +93 -0
  102. package/src/cli/pause.test.mjs +109 -0
  103. package/src/cli/plan-check.mjs +84 -12
  104. package/src/cli/plan-check.test.mjs +152 -2
  105. package/src/cli/plan-stop.mjs +7 -4
  106. package/src/cli/plan-stop.test.mjs +87 -0
  107. package/src/cli/platform-risk.mjs +46 -0
  108. package/src/cli/platform-risk.test.mjs +38 -0
  109. package/src/cli/plugin-skills.test.mjs +25 -0
  110. package/src/cli/pr-guard.mjs +128 -0
  111. package/src/cli/pr-guard.test.mjs +154 -0
  112. package/src/cli/promotion-branch.mjs +2 -0
  113. package/src/cli/qc.mjs +160 -27
  114. package/src/cli/qc.test.mjs +93 -0
  115. package/src/cli/report-stop.mjs +5 -0
  116. package/src/cli/report-stop.test.mjs +23 -1
  117. package/src/cli/retry-failed.mjs +3 -1
  118. package/src/cli/review-gate.mjs +24 -6
  119. package/src/cli/review-gate.test.mjs +116 -0
  120. package/src/cli/review-promotion.mjs +107 -0
  121. package/src/cli/review-promotion.test.mjs +251 -0
  122. package/src/cli/role-budget.mjs +50 -0
  123. package/src/cli/role-budget.test.mjs +91 -0
  124. package/src/cli/run-log.mjs +73 -0
  125. package/src/cli/run-log.test.mjs +121 -0
  126. package/src/cli/secret-patterns.mjs +16 -0
  127. package/src/cli/secret-patterns.test.mjs +73 -0
  128. package/src/cli/setup-block.mjs +24 -0
  129. package/src/cli/setup-block.test.mjs +96 -0
  130. package/src/cli/setup-checks.mjs +65 -0
  131. package/src/cli/setup-checks.test.mjs +158 -0
  132. package/src/cli/setup-compaction.mjs +55 -0
  133. package/src/cli/setup-compaction.test.mjs +115 -0
  134. package/src/cli/setup-skills.mjs +65 -0
  135. package/src/cli/setup-skills.test.mjs +171 -0
  136. package/src/cli/setup.mjs +107 -0
  137. package/src/cli/setup.test.mjs +275 -0
  138. package/src/cli/skill-length.test.mjs +8 -0
  139. package/src/cli/spawn-command.mjs +47 -0
  140. package/src/cli/spawn-command.test.mjs +64 -0
  141. package/src/cli/task-gate-review.test.mjs +21 -2
  142. package/src/cli/task-gate.mjs +25 -12
  143. package/src/cli/task-gate.test.mjs +43 -5
  144. package/src/cli/tokens.mjs +46 -3
  145. package/src/cli/tokens.test.mjs +103 -3
  146. package/src/cli/tools.mjs +49 -0
  147. package/src/cli/tools.test.mjs +89 -0
  148. package/src/cli/transcript-usage.mjs +27 -0
  149. package/src/cli/transcript-usage.test.mjs +34 -0
  150. package/src/cli/user-config-package.mjs +1 -1
  151. package/src/cli/user-config-toml.mjs +63 -7
  152. package/src/cli/user-config.mjs +39 -7
  153. package/src/cli/user-config.test.mjs +238 -5
  154. package/src/cli/watch-arm.mjs +39 -0
  155. package/src/cli/watch-arm.test.mjs +87 -0
  156. package/src/cli/watch.mjs +80 -35
  157. package/src/cli/watch.test.mjs +233 -27
  158. package/src/cli/work-order-guard.mjs +2 -1
  159. package/src/cli/workflow-hooks.test.mjs +18 -4
  160. package/src/cli/workflow-settings.mjs +44 -8
  161. package/src/cli/workflow-settings.test.mjs +124 -1
  162. package/src/cli/worktree.mjs +14 -1
  163. package/src/cli/worktree.test.mjs +20 -1
  164. package/src/config.mjs +174 -7
  165. package/src/config.test.mjs +327 -5
  166. package/src/consumer-package.mjs +31 -0
  167. package/src/consumer-package.test.mjs +54 -0
  168. package/src/descriptions.mjs +2 -0
  169. package/src/eslint/index.mjs +12 -0
  170. package/src/eslint/jsx.mjs +20 -0
  171. package/src/eslint/preset.mjs +26 -2
  172. package/src/eslint/preset.test.mjs +104 -2
  173. package/src/eslint/rules/no-arbitrary-class-value.mjs +32 -0
  174. package/src/eslint/rules/no-literal-color.mjs +94 -0
  175. package/src/eslint/rules/no-static-inline-style.mjs +41 -0
  176. package/src/eslint/rules/one-component-per-file.mjs +70 -0
  177. package/src/eslint/rules/raw-element-in-primitive.mjs +31 -0
  178. package/src/eslint/rules/timeless-comment.mjs +92 -0
  179. package/src/eslint/rules.test.mjs +189 -0
  180. package/src/eslint/ui-regressions.test.mjs +39 -0
  181. package/src/gates/contract-operations.mjs +3 -3
  182. package/src/gates/contract-operations.test.mjs +28 -0
  183. package/src/gates/feature-switches.mjs +19 -0
  184. package/src/gates/feature-switches.test.mjs +55 -0
  185. package/src/gates/plain-language.mjs +396 -0
  186. package/src/gates/plain-language.test.mjs +210 -0
  187. package/src/gates/runtime-imports.test.mjs +114 -0
  188. package/src/gates/saga-key.test.mjs +1 -1
  189. package/src/global-skills.mjs +9 -0
  190. package/src/global-skills.test.mjs +53 -0
  191. package/src/plugin.test.mjs +3 -2
  192. package/src/ui/class-sites.mjs +38 -6
  193. package/src/ui/codemod.mjs +4 -1
  194. package/src/ui/codemod.test.mjs +52 -0
  195. package/src/ui/components.mjs +5 -3
  196. package/src/ui/components.test.mjs +8 -0
  197. package/src/ui/dupes.test.mjs +18 -0
  198. package/templates/docs/enforcement.md +12 -2
  199. package/templates/docs/ui.md +33 -0
  200. package/templates/githooks/pre-commit +1 -0
  201. package/templates/githooks/pre-push +11 -2
  202. package/templates/github/workflows/ci.yml +57 -3
  203. package/user-config/claude/CLAUDE.core.md +28 -0
  204. package/user-config/claude/settings.shared.json +41 -1
  205. package/src/cli/screens-discovery.mjs +0 -60
  206. package/src/cli/screens.mjs +0 -84
  207. package/src/cli/stack.mjs +0 -14
  208. package/src/cli/throwaway-db.mjs +0 -127
  209. package/src/cli/throwaway-db.test.mjs +0 -83
  210. package/src/screens/auth-context.test.mjs +0 -41
  211. package/src/screens/auth-run.test.mjs +0 -147
  212. package/src/screens/auth.mjs +0 -223
  213. package/src/screens/auth.test.mjs +0 -76
  214. package/src/screens/capture-idle.test.mjs +0 -27
  215. package/src/screens/capture.browser-test.mjs +0 -73
  216. package/src/screens/capture.mjs +0 -175
  217. package/src/screens/capture.test.mjs +0 -28
  218. package/src/screens/catalog.browser-test.mjs +0 -32
  219. package/src/screens/catalog.mjs +0 -72
  220. package/src/screens/catalog.test.mjs +0 -36
  221. package/src/screens/chrome.mjs +0 -21
  222. package/src/screens/compare.mjs +0 -40
  223. package/src/screens/compare.test.mjs +0 -30
  224. package/src/screens/discovery.browser-test.mjs +0 -29
  225. package/src/screens/discovery.mjs +0 -158
  226. package/src/screens/discovery.test.mjs +0 -27
  227. package/src/screens/inventory.mjs +0 -69
  228. package/src/screens/inventory.test.mjs +0 -10
  229. package/src/screens/map.mjs +0 -63
  230. package/src/screens/map.test.mjs +0 -35
  231. package/src/screens/open.browser-test.mjs +0 -50
  232. package/src/screens/regions.mjs +0 -137
  233. package/src/screens/regions.test.mjs +0 -38
  234. package/src/screens/session-files.mjs +0 -39
  235. package/src/screens/session-files.test.mjs +0 -39
  236. package/src/screens/setup.browser-test.mjs +0 -49
  237. package/src/screens/setup.mjs +0 -78
  238. package/src/screens/stack-auth.browser-test.mjs +0 -30
  239. package/src/screens/stack-auth.mjs +0 -106
  240. package/src/screens/stack-auth.test.mjs +0 -65
  241. package/src/screens/write-guard.mjs +0 -26
  242. package/src/stack/stack.mjs +0 -131
  243. package/src/stack/stack.test.mjs +0 -40
  244. package/src/stack/supervisor.mjs +0 -30
@@ -0,0 +1,615 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
4
+ import os from "node:os";
5
+ import { execFileSync, spawnSync } from "node:child_process";
6
+ import { fileURLToPath } from "node:url";
7
+ import path from "node:path";
8
+ import { recordRun, openFailures, resolveFailures, ciBriefRefusal, lessonRefusal, lessonRules, rerunFailures, runCiFailures, parseFailedLog } from "./ci-failures.mjs";
9
+ import { holdBranch, holdRefusal, runResume } from "./pause.mjs";
10
+ import { appendRecord, ledgerFileOf, readLedger } from "./ledger.mjs";
11
+ import { runWatch } from "./watch.mjs";
12
+
13
+ const tap = `test (windows-latest)\tRun tests\t2026-01-01T00:00:00Z not ok 1 - cleanup retries
14
+ test (windows-latest)\tRun tests\t2026-01-01T00:00:00Z ---
15
+ test (windows-latest)\tRun tests\t2026-01-01T00:00:00Z location: 'file:///D:/a/repo/tests/cleanup.test.mjs:12:1'
16
+ test (windows-latest)\tRun tests\t2026-01-01T00:00:00Z error: 'EBUSY: resource busy'
17
+ test (windows-latest)\tRun tests\t2026-01-01T00:00:00Z ...`;
18
+
19
+ test("parses a Node spec reporter failure excerpt without duplicating its summary", () => {
20
+ const log = `✖ cleanup retries (1.1624ms)
21
+ ℹ tests 1
22
+ ℹ fail 1
23
+
24
+ ✖ failing tests:
25
+
26
+ test at qc-205-spec.test.mjs:1:72
27
+ ✖ cleanup retries (1.1624ms)
28
+ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
29
+
30
+ 1 !== 2
31
+
32
+ at TestContext.<anonymous> (file:///C:/Users/NGUYEN%7E1/AppData/Local/Temp/qc-205-spec.test.mjs:1:109)`;
33
+ const failures = parseFailedLog(log);
34
+ assert.equal(failures.length, 1);
35
+ assert.equal(failures[0].test, "cleanup retries");
36
+ assert.equal(failures[0].file, "qc-205-spec.test.mjs");
37
+ assert.match(failures[0].error, /AssertionError.*Expected values/);
38
+ });
39
+
40
+ function fixture(t) {
41
+ const root = mkdtempSync(path.join(os.tmpdir(), "ci-lessons-"));
42
+ execFileSync("git", ["init", "-q", root]);
43
+ execFileSync("git", ["remote", "add", "origin", "https://github.com/local/repo.git"], { cwd: root });
44
+ t.after(() => rmSync(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }));
45
+ return root;
46
+ }
47
+
48
+ test("records the repository and asks only for failures from the local origin", (t) => {
49
+ const root = fixture(t);
50
+ for (const [id, head] of [[1, "aaaaaaa"], [2, "bbbbbbb"], [3, "ccccccc"]]) {
51
+ recordRun(root, { id, branch: "main", head }, { repo: "fork/repo", gh: () => tap });
52
+ }
53
+ assert.equal(readLedger(ledgerFileOf(root))[0].repository, "fork/repo");
54
+ assert.equal(holdRefusal(root, "main"), null);
55
+ const calls = [];
56
+ for (const [id, head] of [[4, "ddddddd"], [5, "eeeeeee"], [6, "fffffff"]]) {
57
+ recordRun(root, { id, branch: "main", head }, { gh: (args) => { calls.push(args); return args[0] === "run" ? tap : args[1]?.endsWith("/pulls") ? JSON.stringify([{ body: "Refs #227", state: "open", head: { ref: "main", repo: { full_name: "local/repo" } } }]) : "[]"; } });
58
+ }
59
+ assert.ok(calls.some((args) => args[1] === "repos/local/repo/commits/fffffff/pulls"));
60
+ assert.equal(readLedger(ledgerFileOf(root)).at(-1).repository, "local/repo");
61
+ assert.equal(holdRefusal(root, "main"), null);
62
+ });
63
+
64
+ test("watch isolates fork and explicit repositories when recording and resolving", async (t) => {
65
+ const root = fixture(t);
66
+ recordRun(root, { id: 1, branch: "main", head: "aaaaaaa" }, { gh: () => tap });
67
+ for (const fork of [true, false]) {
68
+ const repository = fork ? "fork/repo" : "other/repo";
69
+ let conclusion = "failure";
70
+ let head = "bbbbbbb";
71
+ let id = 2;
72
+ const gh = (args) => {
73
+ if (args.includes("--log-failed")) return tap;
74
+ if (args.includes(".sha")) return head;
75
+ return JSON.stringify({ workflow_runs: [{ id, head_sha: head, head_branch: "main", status: "completed", conclusion,
76
+ ...(fork ? { head_repository: { full_name: repository }, repository: { full_name: "local/repo" } } : {}) }] });
77
+ };
78
+ const watch = () => runWatch(["--run", "ci.yml@main", ...(!fork ? ["--repo", repository] : [])], {
79
+ cwd: root, requiredChecks: [], gh, say: () => {}, warn: () => {},
80
+ });
81
+ for (const sha of ["bbbbbbb", "ccccccc", "ddddddd"]) {
82
+ head = sha;
83
+ id += 1;
84
+ assert.equal(await watch(), 5);
85
+ }
86
+ assert.equal(holdRefusal(root, "main"), null);
87
+ assert.equal(openFailures(root, "main", repository).length, 3);
88
+ conclusion = "success";
89
+ head = "eeeeeee";
90
+ id += 1;
91
+ assert.equal(await watch(), 0);
92
+ assert.equal(openFailures(root, "main", repository).length, 0);
93
+ assert.equal(openFailures(root, "main").length, 1);
94
+ }
95
+ });
96
+
97
+ test("a local branch hold leaves an explicit foreign repository watch available", async (t) => {
98
+ const root = fixture(t);
99
+ holdBranch(root, "main", "operator hold");
100
+ assert.ok(holdRefusal(root, "main"));
101
+ assert.equal(await runWatch(["--run", "ci.yml@main", "--repo", "other/repo"], { cwd: root, requiredChecks: [], say: () => {},
102
+ gh: (args) => args.includes(".sha") ? "ddddddd" : JSON.stringify({ workflow_runs: [{ id: 4, head_sha: "ddddddd", status: "completed", conclusion: "success" }] }),
103
+ }), 0);
104
+ });
105
+
106
+ test("an unreadable origin keeps the hold on an explicit repository watch", async (t) => {
107
+ const root = fixture(t);
108
+ holdBranch(root, "main", "operator hold");
109
+ execFileSync("git", ["remote", "remove", "origin"], { cwd: root });
110
+ assert.ok(holdRefusal(root, "main"));
111
+ const lines = [];
112
+ assert.equal(await runWatch(["--run", "ci.yml@main", "--repo", "local/repo"], {
113
+ cwd: root, requiredChecks: [], say: (line) => lines.push(line),
114
+ gh: () => assert.fail("the hold must apply before polling"),
115
+ now: () => 0, sleep: () => { throw new Error("the hold must apply before waiting"); },
116
+ }), 5);
117
+ assert.match(lines[0], /qc resume --branch/);
118
+ });
119
+
120
+ test("manual recording and resolution use the fork run's head repository", (t) => {
121
+ const root = fixture(t);
122
+ recordRun(root, { id: 1, branch: "main", head: "aaaaaaa" }, { gh: () => tap });
123
+ let conclusion = "failure";
124
+ let head = "bbbbbbb";
125
+ const gh = (args) => args.includes("--log-failed") ? tap : JSON.stringify({
126
+ head_branch: "main", head_sha: head, conclusion,
127
+ head_repository: { full_name: "fork/repo" }, repository: { full_name: "local/repo" },
128
+ });
129
+ for (const [id, sha] of [[2, "bbbbbbb"], [3, "ccccccc"], [4, "ddddddd"]]) {
130
+ head = sha;
131
+ assert.equal(runCiFailures(root, ["--run", String(id)], { gh, warn: () => {} }), 0);
132
+ }
133
+ assert.equal(openFailures(root, "main", "fork/repo").length, 3);
134
+ assert.equal(openFailures(root, "main").length, 1);
135
+ assert.equal(holdRefusal(root, "main"), null);
136
+ conclusion = "success";
137
+ head = "eeeeeee";
138
+ assert.equal(runCiFailures(root, ["--run", "5"], { gh }), 0);
139
+ assert.equal(openFailures(root, "main", "fork/repo").length, 0);
140
+ assert.equal(openFailures(root, "main").length, 1);
141
+ });
142
+
143
+ test("open failures require lessons when their branch merges into the base", (t) => {
144
+ const root = fixture(t);
145
+ const git = (...args) => execFileSync("git", ["-c", "user.name=test", "-c", "user.email=test@example.com", ...args], { cwd: root, encoding: "utf8" }).trim();
146
+ git("commit", "--allow-empty", "-qm", "base");
147
+ git("update-ref", "refs/remotes/origin/main", "HEAD");
148
+ git("checkout", "-qb", "feat/merged");
149
+ git("commit", "--allow-empty", "-qm", "branch");
150
+ recordRun(root, { id: 1, branch: "feat/merged", head: git("rev-parse", "HEAD") }, { gh: () => tap });
151
+ assert.equal(lessonRefusal(root, "feat/merged", ""), null);
152
+ git("update-ref", "refs/remotes/origin/main", "HEAD");
153
+ assert.equal(openFailures(root, "feat/merged").length, 1);
154
+ assert.match(lessonRefusal(root, "feat/merged", ""), /cleanup retries/);
155
+ assert.equal(lessonRefusal(root, "feat/merged", "## cleanup retries\nSymptom: EBUSY\nCause: open handle\nRule: Close it.\n"), null);
156
+ });
157
+
158
+ test("foreign resolutions leave legacy local records open", (t) => {
159
+ const root = fixture(t);
160
+ appendRecord(ledgerFileOf(root), { type: "ci-failure", id: "legacy", branch: "main", head: "aaaaaaa", test: "legacy" });
161
+ resolveFailures(root, "main", "bbbbbbb", 2, "fork/repo");
162
+ assert.equal(openFailures(root, "main").length, 1);
163
+ });
164
+
165
+ test("records TAP failures once per run and resolves only on a later head of the same branch", (t) => {
166
+ const root = fixture(t);
167
+ const gh = (args) => {
168
+ assert.deepEqual(args, ["run", "view", "42", "--log-failed"]);
169
+ return tap;
170
+ };
171
+ recordRun(root, { id: 42, branch: "feat/test", head: "aaaaaaa" }, { gh });
172
+ recordRun(root, { id: 42, branch: "feat/test", head: "aaaaaaa" }, { gh });
173
+ const records = readLedger(ledgerFileOf(root));
174
+ assert.equal(records.length, 1);
175
+ assert.equal(records[0].test, "cleanup retries");
176
+ assert.equal(records[0].os, "windows-latest");
177
+ assert.equal(records[0].file, "tests/cleanup.test.mjs");
178
+ assert.equal(records[0].error, "EBUSY: resource busy");
179
+ resolveFailures(root, "other", "bbbbbbb");
180
+ resolveFailures(root, "feat/test", "aaaaaaa");
181
+ assert.equal(openFailures(root, "feat/test").length, 1);
182
+ resolveFailures(root, "feat/test", "bbbbbbb");
183
+ assert.equal(openFailures(root, "feat/test").length, 0);
184
+ });
185
+
186
+ test("records unknown output by job and records nothing when gh fails", (t) => {
187
+ const root = fixture(t);
188
+ const warnings = [];
189
+ recordRun(root, { id: 1, branch: "main", head: "aaaaaaa" }, { gh: () => { throw new Error("network\nsecret"); }, warn: (s) => warnings.push(s) });
190
+ assert.equal(readLedger(ledgerFileOf(root)).length, 0);
191
+ assert.deepEqual(warnings, ["ci-failures: gh failed: network"]);
192
+ recordRun(root, { id: 2, branch: "main", head: "aaaaaaa" }, { gh: () => "build (ubuntu-latest)\tBuild\t2026-01-01T00:00:00Z ##[error]compiler failed" });
193
+ const [failure] = openFailures(root, "main");
194
+ assert.equal(failure.test, "build (ubuntu-latest)");
195
+ assert.match(failure.error, /compiler failed/);
196
+ });
197
+
198
+ test("watch records a failed run, preserves exit 5 on a log error, and resolves a later green head", async (t) => {
199
+ const root = fixture(t);
200
+ let failedLog = false;
201
+ let head = "aaaaaaa";
202
+ let conclusion = "failure";
203
+ const gh = (args) => {
204
+ if (args.includes("--log-failed")) {
205
+ if (failedLog) throw new Error("log unavailable");
206
+ return tap;
207
+ }
208
+ if (args.includes(".sha")) return head;
209
+ return JSON.stringify({ workflow_runs: [{ id: conclusion === "success" ? 43 : 42, head_sha: head, head_branch: "feat/test", status: "completed", conclusion }] });
210
+ };
211
+ const warnings = [];
212
+ const watch = () => runWatch(["--run", "ci.yml@feat/test"], { cwd: root, requiredChecks: [], gh, say: () => {}, warn: (s) => warnings.push(s) });
213
+ assert.equal(await watch(), 5);
214
+ assert.equal(openFailures(root, "feat/test").length, 1);
215
+ failedLog = true;
216
+ assert.equal(await watch(), 5);
217
+ assert.equal(warnings.length, 1);
218
+ head = "bbbbbbb";
219
+ conclusion = "success";
220
+ assert.equal(await watch(), 0);
221
+ assert.equal(openFailures(root, "feat/test").length, 0);
222
+ });
223
+
224
+ test("three red heads → a needs-user question, and no hold record", (t) => {
225
+ const root = fixture(t);
226
+ const calls = [];
227
+ const comments = [];
228
+ let currentHead;
229
+ const gh = (args) => {
230
+ calls.push(args);
231
+ if (args[0] === "run") return tap;
232
+ if (args[1] === `repos/local/repo/commits/${currentHead}/pulls`) return JSON.stringify([{ body: "Refs #227", state: "open", head: { ref: "feat/test", repo: { full_name: "local/repo" }, sha: currentHead } }]);
233
+ if (args[0] === "api") return JSON.stringify([comments]);
234
+ if (args[1] === "comment") comments.push({ body: args[args.indexOf("--body") + 1] });
235
+ return "[]";
236
+ };
237
+ const record = (id, head) => { currentHead = head; return recordRun(root, { id, branch: "feat/test", head }, { gh }); };
238
+ record(1, "aaaaaaa");
239
+ assert.match(ciBriefRefusal(root, "feat/test", ""), /cleanup retries/);
240
+ assert.equal(ciBriefRefusal(root, "green", ""), null);
241
+ const brief = "## CI failures\n### cleanup retries\nCause: the file remains open.\n";
242
+ assert.equal(ciBriefRefusal(root, "feat/test", brief), null);
243
+ assert.match(ciBriefRefusal(root, "feat/test", brief.replace("Cause:", "Reason:")), /Cause:/);
244
+ record(2, "aaaaaaa");
245
+ assert.equal(ciBriefRefusal(root, "feat/test", brief), null);
246
+ record(3, "bbbbbbb");
247
+ assert.match(ciBriefRefusal(root, "feat/test", brief), /## Diagnosis/);
248
+ assert.match(ciBriefRefusal(root, "feat/test", `${brief}\n## Diagnosis\nRoot cause: open handle\n`), /Evidence:/);
249
+ assert.equal(ciBriefRefusal(root, "feat/test", `${brief}\n## Diagnosis\nRoot cause: open handle\nEvidence: log shows EBUSY; cleanup code leaves it open.\n`), null);
250
+ assert.equal(calls.filter((args) => args[0] === "issue").length, 0);
251
+ record(4, "ccccccc");
252
+ record(4, "ccccccc");
253
+ record(5, "ccccccc");
254
+ record(6, "ddddddd");
255
+ const questions = calls.filter((args) => args[0] === "issue" && args[1] === "comment");
256
+ assert.equal(questions.length, 1);
257
+ assert.equal(questions[0][2], "227");
258
+ assert.match(comments[0].body, /cleanup retries/);
259
+ assert.equal(holdRefusal(root, "feat/test"), null);
260
+ assert.ok(readLedger(ledgerFileOf(root)).every((record) => record.type === "ci-failure"));
261
+ holdBranch(root, "feat/test", "existing operator hold");
262
+ record(7, "eeeeeee");
263
+ assert.match(holdRefusal(root, "feat/test"), /qc resume/);
264
+ assert.equal(holdRefusal(root, "other"), null);
265
+ assert.equal(runResume(root, [], { say: () => {} }), 0);
266
+ assert.ok(holdRefusal(root, "feat/test"));
267
+ assert.equal(runResume(root, ["--branch", "feat/test"], { say: () => {} }), 0);
268
+ assert.equal(holdRefusal(root, "feat/test"), null);
269
+ });
270
+
271
+ test("resolved tests require complete lesson entries and rules feed forward newest first, capped at twenty", (t) => {
272
+ const root = fixture(t);
273
+ recordRun(root, { id: 1, branch: "feat/test", head: "aaaaaaa" }, { gh: () => tap });
274
+ assert.equal(lessonRefusal(root, "feat/test", ""), null);
275
+ resolveFailures(root, "feat/test", "bbbbbbb");
276
+ assert.match(lessonRefusal(root, "feat/test", ""), /cleanup retries/);
277
+ assert.match(lessonRefusal(root, "feat/test", "## cleanup retries\nSymptom: EBUSY on Windows\nCause: open handle\nRule:\n"), /cleanup retries/);
278
+ assert.equal(lessonRefusal(root, "feat/test", "## cleanup retries\nSymptom: EBUSY on Windows\nCause: open handle\nRule: Retry cleanup.\n"), null);
279
+ assert.equal(lessonRefusal(root, "other", ""), null);
280
+ mkdirSync(path.join(root, "docs"));
281
+ writeFileSync(path.join(root, "docs", "ci-lessons.md"), Array.from({ length: 45 }, (_, i) => `## test ${i}\nRule: Apply rule ${i}.\n`).join("\n"));
282
+ const rules = lessonRules(root, { file: "docs/ci-lessons.md" });
283
+ assert.equal(rules.length, 20);
284
+ assert.equal(rules[0], "Rule: Apply rule 44.");
285
+ assert.equal(rules.at(-1), "Rule: Apply rule 25.");
286
+ });
287
+
288
+ test("prompt rules trim duplicates and skip none declarations before applying the caps", (t) => {
289
+ const root = fixture(t);
290
+ writeFileSync(path.join(root, "lessons.md"), "Rule: Older\nRule: Same\n Rule: Same \nRule: none: no lesson\nRule: Same\nRule: Newest\n");
291
+ assert.deepEqual(lessonRules(root, { file: "lessons.md" }), ["Rule: Newest", "Rule: Same", "Rule: Older"]);
292
+ });
293
+
294
+ test("prompt rules count separators in the character cap and leave overflowing rules whole", (t) => {
295
+ const root = fixture(t);
296
+ const newest = `Rule: ${"x".repeat(1994)}`;
297
+ writeFileSync(path.join(root, "lessons.md"), `Rule: Older\n${newest}\n`);
298
+ assert.deepEqual(lessonRules(root, { file: "lessons.md" }), [newest]);
299
+ writeFileSync(path.join(root, "lessons.md"), `Rule: Older\n${newest}x\n`);
300
+ assert.deepEqual(lessonRules(root, { file: "lessons.md" }), []);
301
+ writeFileSync(path.join(root, "lessons.md"), `Rule: ${"a".repeat(992)}\nRule: ${"b".repeat(995)}\n`);
302
+ const rules = lessonRules(root, { file: "lessons.md" });
303
+ assert.equal(rules.length, 2);
304
+ assert.equal(rules.join("\n").length, 2000);
305
+ });
306
+
307
+ test("rerun appends unique open test files, honors configuration, and propagates failures", (t) => {
308
+ const root = fixture(t);
309
+ mkdirSync(path.join(root, "tests"));
310
+ writeFileSync(path.join(root, "tests", "cleanup.test.mjs"), "");
311
+ recordRun(root, { id: 1, branch: "feat/test", head: "aaaaaaa" }, { gh: () => tap });
312
+ recordRun(root, { id: 2, branch: "feat/test", head: "aaaaaaa" }, { gh: () => tap });
313
+ const calls = [];
314
+ const run = (command, options) => { calls.push({ command, options }); return { status: 7 }; };
315
+ assert.equal(rerunFailures(root, "feat/test", { rerun: "node --test --test-reporter=tap" }, { run }), 7);
316
+ assert.equal(calls.length, 1);
317
+ assert.match(calls[0].command, /^node --test --test-reporter=tap .*tests\/cleanup.test.mjs/);
318
+ assert.equal(calls[0].command.match(/cleanup.test.mjs/g).length, 1);
319
+ assert.equal(calls[0].options.cwd, root);
320
+ assert.equal(rerunFailures(root, "green", { rerun: "node --test" }, { run: () => assert.fail("green branch") }), 0);
321
+ });
322
+
323
+ test("a held watched branch exits 5 without fetching and leaves other branches available", async (t) => {
324
+ const root = fixture(t);
325
+ holdBranch(root, "feat/test", "operator hold");
326
+ assert.equal(await runWatch(["--run", "ci.yml@feat/test"], { cwd: root, requiredChecks: [], gh: () => assert.fail("held branch"), say: () => {} }), 5);
327
+ });
328
+
329
+ test("the command table exposes ci-failures and reruns a green branch without gh", (t) => {
330
+ const root = fixture(t);
331
+ const qc = fileURLToPath(new URL("./qc.mjs", import.meta.url));
332
+ const help = spawnSync(process.execPath, [qc], { cwd: root, encoding: "utf8" });
333
+ assert.match(help.stdout, /qc ci-failures --run <id>/);
334
+ assert.match(help.stdout, /qc ask --issue <n>/);
335
+ const ask = spawnSync(process.execPath, [qc, "ask", "--issue", "0", "Why?"], { cwd: root, encoding: "utf8" });
336
+ assert.equal(ask.status, 1);
337
+ assert.match(ask.stderr, /Usage: qc ask/);
338
+ const result = spawnSync(process.execPath, [qc, "ci-failures", "--rerun", "--branch", "green"], { cwd: root, encoding: "utf8" });
339
+ assert.equal(result.status, 0, result.stderr);
340
+ assert.equal(result.stdout, "");
341
+ assert.equal(result.stderr, "");
342
+ });
343
+
344
+ test("manual recording reads run metadata with a fake gh", (t) => {
345
+ const root = fixture(t);
346
+ const calls = [];
347
+ const gh = (args) => {
348
+ calls.push(args);
349
+ return args.includes("--log-failed") ? tap : JSON.stringify({ head_branch: "feat/test", head_repository: { full_name: "local/repo" }, head_sha: "aaaaaaa", conclusion: "failure" });
350
+ };
351
+ assert.equal(runCiFailures(root, ["--run", "42"], { gh, say: () => {} }), 0);
352
+ assert.equal(openFailures(root, "feat/test").length, 1);
353
+ assert.deepEqual(calls[0], ["api", "repos/{owner}/{repo}/actions/runs/42"]);
354
+ });
355
+
356
+ test("CI commands pass silently and refusals fit four lines", (t) => {
357
+ const root = fixture(t);
358
+ const output = [];
359
+ const options = { say: (s) => output.push(s), warn: (s) => output.push(s), gh: () => JSON.stringify({ head_branch: "feat/test", head_repository: { full_name: "local/repo" }, head_sha: "bbbbbbb", conclusion: "success" }) };
360
+ assert.equal(runCiFailures(root, ["--run", "42"], options), 0);
361
+ assert.deepEqual(output, []);
362
+ assert.equal(runCiFailures(root, ["--rerun", "--branch", "green"], options), 0);
363
+ assert.deepEqual(output, []);
364
+ assert.equal(runCiFailures(root, [], options), 1);
365
+ assert.ok(output.join("\n").split(/\r?\n/).length <= 4);
366
+ });
367
+
368
+ test("CI brief and lesson refusals keep multiline test names within four lines", (t) => {
369
+ const root = fixture(t);
370
+ appendRecord(ledgerFileOf(root), { type: "ci-failure", id: "multiline", branch: "feat/test", head: "aaaaaaa", test: "first\nsecond\nthird\nfourth\nfifth" });
371
+ assert.ok(ciBriefRefusal(root, "feat/test", "").split(/\r?\n/).length <= 4);
372
+ resolveFailures(root, "feat/test", "bbbbbbb");
373
+ assert.ok(lessonRefusal(root, "feat/test", "").split(/\r?\n/).length <= 4);
374
+ });
375
+
376
+ test("recorded Linux TAP normalizes the checkout prefix and preserves the first multiline error", () => {
377
+ const log = `test (ubuntu-latest)\tRun\t2026-01-01T00:00:00Z not ok 1 - rejects the wrong target
378
+ test (ubuntu-latest)\tRun\t2026-01-01T00:00:00Z location: '/home/runner/work/architecture-kit/architecture-kit/packages/qc-harness/src/cli/auth.test.mjs:19:1'
379
+ test (ubuntu-latest)\tRun\t2026-01-01T00:00:00Z error: |-
380
+ test (ubuntu-latest)\tRun\t2026-01-01T00:00:00Z Expected values to be strictly equal:
381
+ test (ubuntu-latest)\tRun\t2026-01-01T00:00:00Z false !== true`;
382
+ const [failure] = parseFailedLog(log);
383
+ assert.equal(failure.file, "packages/qc-harness/src/cli/auth.test.mjs");
384
+ assert.equal(failure.error, "Expected values to be strictly equal:");
385
+ });
386
+
387
+ test("recorded Windows TAP accepts escaped drive paths", () => {
388
+ const log = String.raw`test (windows-latest)\tRun\t2026-01-01T00:00:00Z not ok 1 - cleanup retries
389
+ test (windows-latest)\tRun\t2026-01-01T00:00:00Z location: 'D:\\a\\architecture-kit\\architecture-kit\\packages\\qc-harness\\cleanup.test.mjs:12:1'
390
+ test (windows-latest)\tRun\t2026-01-01T00:00:00Z error: EBUSY`.replaceAll("\\t", "\t");
391
+ assert.equal(parseFailedLog(log)[0].file, "packages/qc-harness/cleanup.test.mjs");
392
+ });
393
+
394
+ test("a cause belongs to each named failure, and a missing test on the next head resets its repeat", (t) => {
395
+ const root = fixture(t);
396
+ recordRun(root, { id: 1, branch: "feat/test", head: "aaaaaaa" }, { gh: () => "not ok 1 - first test\n error: first\nnot ok 2 - second test\n error: second" });
397
+ assert.match(ciBriefRefusal(root, "feat/test", "## CI failures\nfirst test\nsecond test\nCause: second only\n"), /first test/);
398
+ recordRun(root, { id: 2, branch: "feat/test", head: "bbbbbbb" }, { gh: () => "not ok 1 - different test\n error: other" });
399
+ recordRun(root, { id: 3, branch: "feat/test", head: "ccccccc" }, { gh: () => "not ok 1 - first test\n error: first" });
400
+ const body = "## CI failures\n### first test\nCause: first\n### second test\nCause: second\n### different test\nCause: different\n";
401
+ assert.equal(ciBriefRefusal(root, "feat/test", body), null);
402
+ assert.equal(holdRefusal(root, "feat/test"), null);
403
+ });
404
+
405
+ test("blank lesson and brief fields stay blank when another field follows", (t) => {
406
+ const root = fixture(t);
407
+ recordRun(root, { id: 1, branch: "feat/test", head: "aaaaaaa" }, { gh: () => "not ok 1 - cleanup retries\n error: EBUSY" });
408
+ assert.match(ciBriefRefusal(root, "feat/test", "## CI failures\ncleanup retries\nCause:\nRule: retry\n"), /Cause:/);
409
+ resolveFailures(root, "feat/test", "bbbbbbb");
410
+ assert.match(lessonRefusal(root, "feat/test", "## cleanup retries\nSymptom: EBUSY on Windows\nCause:\nRule: Retry cleanup.\n"), /cleanup retries/);
411
+ });
412
+
413
+ test("an older green run cannot resolve a newer failure, and a manual log error returns failure", (t) => {
414
+ const root = fixture(t);
415
+ recordRun(root, { id: 20, branch: "feat/test", head: "aaaaaaa" }, { gh: () => tap });
416
+ resolveFailures(root, "feat/test", "bbbbbbb", 10);
417
+ assert.equal(openFailures(root, "feat/test").length, 1);
418
+ resolveFailures(root, "feat/test", "ccccccc", 30);
419
+ assert.equal(openFailures(root, "feat/test").length, 0);
420
+ const warnings = [];
421
+ const gh = (args) => {
422
+ if (args.includes("--log-failed")) throw new Error("log unavailable\nmore detail");
423
+ return JSON.stringify({ head_branch: "feat/test", head_repository: { full_name: "local/repo" }, head_sha: "ddddddd", conclusion: "failure" });
424
+ };
425
+ assert.equal(runCiFailures(root, ["--run", "40"], { gh, say: () => {}, warn: (s) => warnings.push(s) }), 1);
426
+ assert.deepEqual(warnings, ["ci-failures: gh failed: log unavailable"]);
427
+ });
428
+
429
+
430
+ test("rerun skips unresolved files and normalizes runner prefixes before resolution", (t) => {
431
+ const root = fixture(t);
432
+ mkdirSync(path.join(root, "tests"));
433
+ writeFileSync(path.join(root, "tests/a.test.mjs"), "");
434
+ const prefixes = ["/home/runner/work/r/r/", "/__w/r/r/", "D:\\a\\r\\r\\"];
435
+ for (const [i, prefix] of prefixes.entries()) {
436
+ appendRecord(ledgerFileOf(root), { type: "ci-failure", id: `prefix-${i}`, branch: "prefix", file: `${prefix}tests/a.test.mjs`, test: "a" });
437
+ }
438
+ const warnings = [];
439
+ const calls = [];
440
+ assert.equal(rerunFailures(root, "prefix", { rerun: "node --test" }, {
441
+ warn: (s) => warnings.push(s), run: (command) => { calls.push(command); return { status: 0 }; },
442
+ }), 0);
443
+ assert.deepEqual(calls, ['node --test "tests/a.test.mjs"']);
444
+ assert.deepEqual(warnings, []);
445
+ appendRecord(ledgerFileOf(root), { type: "ci-failure", id: "missing", branch: "missing", file: "tests/old.test.mjs", test: "old" });
446
+ assert.equal(rerunFailures(root, "missing", { rerun: "node --test" }, {
447
+ warn: (s) => warnings.push(s), run: () => assert.fail("missing files must be skipped"),
448
+ }), 0);
449
+ assert.equal(warnings.length, 1);
450
+ assert.equal(warnings[0].split(/\r?\n/).length, 1);
451
+ assert.match(warnings[0], /skip.*tests\/old.test.mjs/);
452
+ });
453
+
454
+ test("rerun captures passing output and caps failures at four lines", (t) => {
455
+ const root = fixture(t);
456
+ mkdirSync(path.join(root, "tests"));
457
+ const file = path.join(root, "tests/a.test.mjs");
458
+ writeFileSync(file, 'import test from "node:test"; test("passes", () => {});');
459
+ appendRecord(ledgerFileOf(root), { type: "ci-failure", id: "output", branch: "main", file: "tests/a.test.mjs", test: "recorded failure" });
460
+ const qc = fileURLToPath(new URL("./qc.mjs", import.meta.url));
461
+ const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => key !== "NODE_TEST_CONTEXT"));
462
+ const invoke = () => spawnSync(process.execPath, [qc, "ci-failures", "--rerun", "--branch", "main"], { cwd: root, encoding: "utf8", env });
463
+ const pass = invoke();
464
+ assert.equal(pass.status, 0, pass.stderr);
465
+ assert.equal(pass.stdout, "");
466
+ assert.equal(pass.stderr, "");
467
+ writeFileSync(file, 'import test from "node:test"; for (let i=0; i<8; i++) test(`failed ${i}`, () => { throw new Error("detail"); });');
468
+ const fail = invoke();
469
+ assert.equal(fail.status, 1);
470
+ assert.equal(fail.stdout, "");
471
+ assert.ok(fail.stderr.trim().split(/\r?\n/).length <= 4, fail.stderr);
472
+ assert.match(fail.stderr, /failed 0/);
473
+ assert.match(fail.stderr, /node --test.*tests\/a.test.mjs/);
474
+ });
475
+
476
+ test("rerun skips unsafe existing paths and paths that cannot resolve", (t) => {
477
+ const root = fixture(t);
478
+ writeFileSync(path.join(root, "bad;name.test.mjs"), "");
479
+ appendRecord(ledgerFileOf(root), { type: "ci-failure", id: "unsafe", branch: "unsafe", file: "bad;name.test.mjs", test: "unsafe" });
480
+ const unsafeWarnings = [];
481
+ assert.equal(rerunFailures(root, "unsafe", { rerun: "node --test" }, { warn: (s) => unsafeWarnings.push(s), run: () => assert.fail("unsafe") }), 0);
482
+ assert.equal(unsafeWarnings.length, 1);
483
+ assert.match(unsafeWarnings[0], /skip 1 test file:.*bad;name\.test\.mjs/);
484
+ assert.ok(unsafeWarnings[0].length <= 200);
485
+ writeFileSync(path.join(root, "plain"), "");
486
+ appendRecord(ledgerFileOf(root), { type: "ci-failure", id: "unresolved", branch: "unresolved", file: "plain/a.test.mjs", test: "unresolved" });
487
+ const warnings = [];
488
+ assert.equal(rerunFailures(root, "unresolved", { rerun: "node --test" }, { warn: (s) => warnings.push(s), run: () => assert.fail("unresolved") }), 0);
489
+ assert.equal(warnings.length, 1);
490
+ });
491
+
492
+
493
+ test("rerun preserves folders named a in Linux and relative paths", (t) => {
494
+ const root = fixture(t);
495
+ mkdirSync(path.join(root, "src/a/b"), { recursive: true });
496
+ writeFileSync(path.join(root, "src/a/b/c.test.mjs"), "");
497
+ for (const [i, file] of ["/home/runner/work/r/r/src/a/b/c.test.mjs", "file:///home/runner/work/r/r/src/a/b/c.test.mjs", "src/a/b/c.test.mjs"].entries()) {
498
+ appendRecord(ledgerFileOf(root), { type: "ci-failure", id: `nested-${i}`, branch: "nested", file, test: "nested" });
499
+ }
500
+ const calls = [];
501
+ assert.equal(rerunFailures(root, "nested", { rerun: "node --test" }, {
502
+ warn: () => assert.fail("existing file must run"), run: (command) => { calls.push(command); return { status: 0 }; },
503
+ }), 0);
504
+ assert.deepEqual(calls, ['node --test "src/a/b/c.test.mjs"']);
505
+ });
506
+
507
+ test("rerun folds all skips into one line and keeps failed output within four lines", (t) => {
508
+ const root = fixture(t);
509
+ writeFileSync(path.join(root, "a.test.mjs"), "");
510
+ for (const [i, file] of ["missing1.test.mjs", "missing2.test.mjs", "a.test.mjs"].entries()) {
511
+ appendRecord(ledgerFileOf(root), { type: "ci-failure", id: `cap-${i}`, branch: "cap", file, test: `failure ${i}` });
512
+ }
513
+ for (const status of [0, 1]) {
514
+ const warnings = [];
515
+ assert.equal(rerunFailures(root, "cap", { rerun: "node --test" }, {
516
+ warn: (s) => warnings.push(s), run: () => ({ status }),
517
+ }), status);
518
+ assert.equal(warnings.filter((s) => /skip/.test(s)).length, 1);
519
+ assert.match(warnings[0], /missing1.*missing2/);
520
+ assert.ok(warnings.join("\n").split(/\r?\n/).length <= (status ? 4 : 1), warnings.join("\n"));
521
+ if (status) {
522
+ assert.match(warnings[1], /failure 2/);
523
+ assert.doesNotMatch(warnings.slice(1).join("\n"), /failure [01]/);
524
+ }
525
+ }
526
+ });
527
+
528
+ test("the skip line stays within 200 characters for many long paths", (t) => {
529
+ const root = fixture(t);
530
+ for (let i = 0; i < 10; i += 1) appendRecord(ledgerFileOf(root), {
531
+ type: "ci-failure", id: `long-${i}`, branch: "long", file: `${"x".repeat(100)}-${i}.test.mjs`, test: "missing",
532
+ });
533
+ const warnings = [];
534
+ assert.equal(rerunFailures(root, "long", { rerun: "node --test" }, { warn: (s) => warnings.push(s) }), 0);
535
+ assert.equal(warnings.length, 1);
536
+ assert.ok(warnings[0].length <= 200);
537
+ assert.match(warnings[0], /skip 10 test files:/);
538
+ });
539
+
540
+
541
+ test("CI questions use only PR issue references and diagnose missing issues or GitHub failures", (t) => {
542
+ for (const mode of ["advanced PR", "old PR", "retry same run", "retry next head"]) {
543
+ const root = fixture(t);
544
+ const comments = [];
545
+ const warnings = [];
546
+ let failAsk = mode !== "advanced PR";
547
+ let head;
548
+ const gh = (args) => {
549
+ if (args[0] === "run") return tap;
550
+ if (args[1] === `repos/local/repo/commits/${head}/pulls`) return JSON.stringify([
551
+ { body: "Closes #12", state: "closed", head: { ref: "feat/test", repo: { full_name: "local/repo" }, sha: "0000000" } },
552
+ { body: "Refs #13", state: "open", head: { ref: "other", repo: { full_name: "local/repo" }, sha: head } },
553
+ ...(mode === "advanced PR" ? [{ body: "Refs #13", state: "open", head: { ref: "feat/test", repo: { full_name: "fork/repo" }, sha: "ddddddd" } }] : []),
554
+ { body: mode === "old PR" ? "No linked issue" : "Refs #227", state: "open", head: { ref: "feat/test", repo: { full_name: "local/repo" }, sha: mode === "advanced PR" ? "ddddddd" : head } },
555
+ ]);
556
+ if (args[0] === "api") return JSON.stringify([comments]);
557
+ if (args[0] === "issue" && args[1] === "comment") {
558
+ if (failAsk) throw new Error("GitHub unavailable");
559
+ assert.equal(args[2], "227");
560
+ comments.push({ body: args[args.indexOf("--body") + 1] });
561
+ }
562
+ return "[]";
563
+ };
564
+ const record = (id, sha) => {
565
+ head = sha;
566
+ recordRun(root, { id, branch: "feat/test", head }, { gh, warn: (s) => warnings.push(s) });
567
+ };
568
+ record(1, "aaaaaaa"); record(2, "bbbbbbb"); record(3, "ccccccc");
569
+ if (mode === "advanced PR") {
570
+ assert.equal(comments.length, 1, "C asks even after its associated PR advances to D");
571
+ assert.deepEqual(warnings, []);
572
+ }
573
+ failAsk = false;
574
+ record(mode === "retry next head" ? 4 : 3, mode === "retry next head" ? "ddddddd" : "ccccccc");
575
+ if (mode === "old PR") {
576
+ assert.equal(comments.length, 0);
577
+ assert.ok(warnings.every((s) => /no linked issue/.test(s)), warnings.join("\n"));
578
+ } else {
579
+ assert.equal(comments.length, 1, mode);
580
+ record(5, "eeeeeee");
581
+ assert.equal(comments.length, 1, "successful question deduplicates later heads");
582
+ }
583
+ }
584
+ for (const body of ["Fixes #12", "closed #13", "Resolves local/repo#14", "Refs #15", "Mentions #16", "Closes foreign/repo#17"]) {
585
+ const root = fixture(t);
586
+ const calls = [];
587
+ const warnings = [];
588
+ const gh = (args) => {
589
+ calls.push(args);
590
+ if (args[0] === "run") return tap;
591
+ if (args[1] === "repos/local/repo/commits/ccccccc/pulls") return JSON.stringify([{ body, state: "open", head: { ref: "feature-without-number", repo: { full_name: "local/repo" }, sha: "ccccccc" } }]);
592
+ return "[]";
593
+ };
594
+ for (const [id, head] of [[1, "aaaaaaa"], [2, "bbbbbbb"], [3, "ccccccc"]]) recordRun(root, { id, branch: "feature-without-number", head }, { gh, warn: (line) => warnings.push(line) });
595
+ const question = calls.find((args) => args[0] === "issue" && args[1] === "comment");
596
+ if (/Mentions|foreign/.test(body)) {
597
+ assert.equal(question, undefined);
598
+ assert.equal(warnings.length, 1);
599
+ assert.doesNotMatch(warnings[0], /\n/);
600
+ } else {
601
+ assert.equal(question?.[2], /#(\d+)/.exec(body)[1]);
602
+ assert.equal(warnings.length, 0);
603
+ assert.ok(calls.filter((args) => args[0] !== "run").every((args) => args[0] === "api" ? args[1].includes("local/repo") : args[args.indexOf("-R") + 1] === "local/repo"));
604
+ }
605
+ assert.equal(holdRefusal(root, "feature-without-number"), null);
606
+ }
607
+ const root = fixture(t);
608
+ const warnings = [];
609
+ for (const [id, head] of [[1, "aaaaaaa"], [2, "bbbbbbb"], [3, "ccccccc"]]) recordRun(root, { id, branch: "main", head }, {
610
+ gh: (args) => { if (args[0] === "run") return tap; throw new Error("offline\nprivate details"); }, warn: (line) => warnings.push(line),
611
+ });
612
+ assert.deepEqual(warnings, ["ci-failures: gh failed: offline"]);
613
+ assert.equal(openFailures(root, "main").length, 3);
614
+ assert.equal(holdRefusal(root, "main"), null);
615
+ });