@izkac/forgekit 0.3.12 → 0.3.14

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 (120) hide show
  1. package/bin/forge.mjs +119 -107
  2. package/bin/forgekit.mjs +83 -83
  3. package/bin/review.mjs +81 -81
  4. package/package.json +2 -1
  5. package/scripts/prepack.mjs +78 -78
  6. package/scripts/run-tests.mjs +50 -50
  7. package/src/adr.mjs +236 -236
  8. package/src/adr.test.mjs +170 -170
  9. package/src/change.mjs +327 -234
  10. package/src/change.test.mjs +145 -83
  11. package/src/cleanup-sessions.mjs +84 -84
  12. package/src/config.mjs +103 -103
  13. package/src/defer.mjs +75 -75
  14. package/src/doctor.mjs +350 -341
  15. package/src/doctor.test.mjs +114 -114
  16. package/src/fleet.test.mjs +50 -0
  17. package/src/init.mjs +680 -621
  18. package/src/install.mjs +815 -815
  19. package/src/install.test.mjs +180 -180
  20. package/src/integrity-check.mjs +60 -60
  21. package/src/integrity.mjs +688 -682
  22. package/src/integrity.test.mjs +612 -566
  23. package/src/lib/fleet.mjs +61 -4
  24. package/src/lib.mjs +160 -143
  25. package/src/lib.test.mjs +128 -0
  26. package/src/models.defaults.json +41 -41
  27. package/src/new-session.mjs +99 -99
  28. package/src/openspec-overlays/README.md +19 -19
  29. package/src/openspec-overlays/openspec-apply-change-footer.md +14 -14
  30. package/src/openspec-overlays/opsx-apply-completion-step.md +1 -1
  31. package/src/openspec-overlays/opsx-apply-implement-step.md +11 -11
  32. package/src/paths.mjs +92 -92
  33. package/src/plan-engine.mjs +321 -278
  34. package/src/plan-engine.test.mjs +447 -283
  35. package/src/preferences.defaults.json +78 -78
  36. package/src/preferences.mjs +438 -438
  37. package/src/preferences.test.mjs +174 -174
  38. package/src/record-evidence.mjs +204 -204
  39. package/src/repo-root.mjs +33 -0
  40. package/src/resolve-model.mjs +312 -312
  41. package/src/resolve-model.test.mjs +194 -194
  42. package/src/review/cli.test.mjs +117 -117
  43. package/src/review/export.mjs +172 -172
  44. package/src/review/export.test.mjs +197 -197
  45. package/src/review/fixtures/valid-review.json +42 -42
  46. package/src/review/lib.mjs +894 -894
  47. package/src/review/lib.test.mjs +266 -266
  48. package/src/review/schema.json +196 -196
  49. package/src/review/signals.test.mjs +62 -62
  50. package/src/score-cli.mjs +68 -68
  51. package/src/score.mjs +568 -568
  52. package/src/score.test.mjs +366 -366
  53. package/src/session-reminder.mjs +207 -207
  54. package/src/session-status.mjs +70 -70
  55. package/src/set-models.mjs +186 -186
  56. package/src/set-phase.mjs +205 -205
  57. package/src/set-prefs.mjs +294 -294
  58. package/src/specs-sync.mjs +232 -0
  59. package/src/specs-sync.test.mjs +114 -0
  60. package/src/spine.mjs +93 -93
  61. package/src/triage-prompt.mjs +175 -175
  62. package/src/triage-prompt.test.mjs +50 -50
  63. package/src/vendor-openspec-overlays.mjs +176 -176
  64. package/src/vendor-openspec-overlays.test.mjs +62 -62
  65. package/vendor/skills/archive-to-adr/SKILL.md +149 -149
  66. package/vendor/skills/forge/SKILL.md +136 -136
  67. package/vendor/skills/forge/docs/forge.md +650 -647
  68. package/vendor/skills/forge/phases/brainstorm.md +23 -23
  69. package/vendor/skills/forge/phases/finish.md +90 -87
  70. package/vendor/skills/forge/phases/implement.md +77 -77
  71. package/vendor/skills/forge/phases/plan-openspec.md +60 -60
  72. package/vendor/skills/forge/phases/plan-specs.md +163 -117
  73. package/vendor/skills/forge/phases/review.md +25 -25
  74. package/vendor/skills/forge/phases/verify.md +124 -124
  75. package/vendor/skills/forge/references/forge-layout.md +85 -85
  76. package/vendor/skills/forge/references/pace.md +115 -115
  77. package/vendor/skills/forge/references/plan-routing.md +52 -51
  78. package/vendor/skills/forge/references/runtime-integrity.md +232 -232
  79. package/vendor/skills/forge/references/substantial-work.md +37 -37
  80. package/vendor/skills/forge/references/test-evidence.md +30 -30
  81. package/vendor/skills/forge/references/test-strategy.md +68 -68
  82. package/vendor/skills/forge/skills/subagent-driven-development/SKILL.md +87 -87
  83. package/vendor/skills/forge/subagents/final-reviewer-prompt.md +56 -56
  84. package/vendor/skills/forge/subagents/implementer-prompt.md +38 -38
  85. package/vendor/skills/git-resolve-adr-conflict/SKILL.md +132 -132
  86. package/vendor/skills/thorough-code-review/SKILL.md +290 -290
  87. package/vendor/skills/thorough-code-review/examples.md +133 -133
  88. package/vendor/skills/thorough-code-review/reference/accepted-risks.md +26 -26
  89. package/vendor/skills/thorough-code-review/reference/lenses.md +96 -96
  90. package/vendor/skills/thorough-code-review/reference/phase1-scout.md +62 -62
  91. package/vendor/skills/thorough-code-review/reference/phase2-skeptic.md +105 -105
  92. package/vendor/skills/thorough-code-review/reference/report-schema.json +222 -222
  93. package/vendor/skills/thorough-code-review/reference/report-template.md +115 -115
  94. package/vendor/skills/thorough-code-review/reference/severity-rubric.md +49 -49
  95. package/vendor/skills/thorough-code-review/reference/signals-preflight.md +55 -55
  96. package/vendor/templates/adr/README.md +7 -7
  97. package/vendor/templates/adr/decisions.md +141 -141
  98. package/vendor/templates/adr/hooks/check-pending-adrs.mjs +74 -74
  99. package/vendor/templates/adr/hooks/check-pending-adrs.sh +3 -3
  100. package/vendor/templates/adr/hooks/openspec-archive-agent-message.mjs +52 -52
  101. package/vendor/templates/adr/hooks/openspec-archive-agent-message.sh +3 -3
  102. package/vendor/templates/project/claude/commands/forge-apply.md +75 -75
  103. package/vendor/templates/project/claude/commands/forge-brainstorm.md +7 -7
  104. package/vendor/templates/project/claude/commands/forge-build.md +17 -17
  105. package/vendor/templates/project/claude/commands/forge-plan.md +12 -12
  106. package/vendor/templates/project/claude/commands/forge-skip.md +14 -14
  107. package/vendor/templates/project/claude/commands/forge-status.md +16 -16
  108. package/vendor/templates/project/claude/commands/forge.md +16 -16
  109. package/vendor/templates/project/claude/hooks/forge-prompt-hook.mjs +73 -73
  110. package/vendor/templates/project/claude/hooks/forge-session-start.mjs +19 -19
  111. package/vendor/templates/project/claude/hooks/forge-triage-hook.mjs +77 -77
  112. package/vendor/templates/project/cursor/commands/forge-apply.md +75 -75
  113. package/vendor/templates/project/cursor/commands/forge-brainstorm.md +10 -10
  114. package/vendor/templates/project/cursor/commands/forge-build.md +17 -17
  115. package/vendor/templates/project/cursor/commands/forge-plan.md +15 -15
  116. package/vendor/templates/project/cursor/commands/forge-skip.md +14 -14
  117. package/vendor/templates/project/cursor/commands/forge-status.md +16 -16
  118. package/vendor/templates/project/cursor/commands/forge.md +16 -16
  119. package/vendor/templates/project/cursor/hooks/forge-session-start.mjs +30 -30
  120. package/vendor/templates/project/cursor/hooks/forge-session-start.sh +3 -3
package/src/integrity.mjs CHANGED
@@ -1,682 +1,688 @@
1
- /**
2
- * Forge runtime-integrity mechanics: spine matrix, deferral registry,
3
- * executable E2E acceptance, and the integrity checks that gate
4
- * `forge phase done|finish`.
5
- *
6
- * Spine matrix — `spine.json` in the change dir (or session dir when the
7
- * session has no tracked change). One row per capability/REQ cluster:
8
- * library → runtime owner → writes → reads → UI consumer → evidence.
9
- * Library-only rows (missing runtime owner / writes / evidence) fail
10
- * validation, so "wire later" cannot be checkboxed past `forge phase done`.
11
- *
12
- * E2E acceptance — `e2e.json` next to the spine: the closed product loop as
13
- * an executable step list. `forge e2e run` executes it and records
14
- * `e2e-results.json` (session dir) with a hash of the steps, so results go
15
- * stale when steps change. When the spine has real rows, the gate requires a
16
- * green, current run — prose in verify-evidence.md no longer satisfies it.
17
- *
18
- * Deferral registry — `deferrals.json` in the session dir. Reviewers may only
19
- * accept "wiring deferred" when a registered deferral names the open task;
20
- * unresolved deferrals block done/finish.
21
- */
22
-
23
- import crypto from 'node:crypto';
24
- import fs from 'node:fs';
25
- import path from 'node:path';
26
- import { spawnSync } from 'node:child_process';
27
- import { readJson, writeJson } from './lib.mjs';
28
- import { loadProjectConfig } from './config.mjs';
29
- import { DEFAULT_SPECS_DIR, resolveProjectPlanEngine } from './plan-engine.mjs';
30
-
31
- /** Signals that a change involves jobs/workers and therefore needs a spine. */
32
- export const JOBS_SIGNAL_RE =
33
- /\b(worker|workers|job|jobs|queue|queues|pipeline|pipelines|etl|orchestration|handler|handlers|cron|scheduler|daemon|ingest|dispatch)\b/i;
34
-
35
- /** Row fields that must be filled (reads/uiConsumer accept "N/A"). */
36
- export const SPINE_ROW_REQUIRED = Object.freeze([
37
- 'capability',
38
- 'library',
39
- 'runtimeOwner',
40
- 'writes',
41
- 'reads',
42
- 'uiConsumer',
43
- 'evidence',
44
- ]);
45
-
46
- const SPINE_FILE = 'spine.json';
47
- const DEFERRALS_FILE = 'deferrals.json';
48
-
49
- /**
50
- * @param {unknown} value
51
- */
52
- function isNonEmptyString(value) {
53
- return typeof value === 'string' && value.trim().length > 0;
54
- }
55
-
56
- /**
57
- * Resolve the change directory for a session (openspec or specs engine).
58
- * Returns null when the session has no tracked change.
59
- *
60
- * @param {{ cwd?: string, session?: Record<string, unknown> | null }} opts
61
- */
62
- /**
63
- * The archived copy of a change, when the live dir is gone. Archiving moves
64
- * `changes/<change>/` to `changes/archive/<YYYY-MM-DD>-<change>/`, so the
65
- * done-gate integrity check (which runs *after* archive in the finish flow)
66
- * would otherwise never find spine.json/e2e.json. Returns null if no match.
67
- *
68
- * @param {string} changesDir absolute path to `<root>/changes`
69
- * @param {string} change change name
70
- */
71
- function findArchivedChangeDir(changesDir, change) {
72
- const archiveDir = path.join(changesDir, 'archive');
73
- if (!fs.existsSync(archiveDir)) return null;
74
- let entries;
75
- try {
76
- entries = fs.readdirSync(archiveDir, { withFileTypes: true });
77
- } catch {
78
- return null;
79
- }
80
- const matches = entries
81
- .filter((e) => e.isDirectory())
82
- .map((e) => e.name)
83
- // CLI + documented manual archive both name dirs `YYYY-MM-DD-<change>`;
84
- // slice(11) drops the `YYYY-MM-DD-` prefix so the suffix must equal the
85
- // change exactly (no false match on `…-other-<change>`).
86
- .filter((name) => name === change || (/^\d{4}-\d{2}-\d{2}-/.test(name) && name.slice(11) === change))
87
- .sort();
88
- // Date-prefixed names sort lexically by date — newest archive wins.
89
- return matches.length ? path.join(archiveDir, matches[matches.length - 1]) : null;
90
- }
91
-
92
- /** Live change dir if present, else its archived copy, else the live path. */
93
- function liveOrArchived(changesDir, change) {
94
- const liveDir = path.join(changesDir, change);
95
- if (fs.existsSync(liveDir)) return liveDir;
96
- return findArchivedChangeDir(changesDir, change) ?? liveDir;
97
- }
98
-
99
- export function resolveChangeDir(opts = {}) {
100
- const cwd = opts.cwd ?? process.cwd();
101
- const session = opts.session ?? null;
102
- // Write callers (spine/e2e init) must always target the LIVE change dir —
103
- // scaffolding into an archived record would corrupt frozen history. Only
104
- // read callers (checks, gates) fall back to the archive.
105
- const forWrite = opts.forWrite === true;
106
- const change = session && isNonEmptyString(session.openspecChange) ? session.openspecChange : null;
107
- if (!change) return null;
108
-
109
- const openspecChanges = path.join(cwd, 'openspec', 'changes');
110
- const openspecDir = path.join(openspecChanges, change);
111
- if (session.planType === 'openspec') {
112
- return forWrite ? openspecDir : liveOrArchived(openspecChanges, change);
113
- }
114
-
115
- let specsRoot = DEFAULT_SPECS_DIR;
116
- try {
117
- specsRoot = resolveProjectPlanEngine(cwd, { useUserDefault: false }).dir;
118
- } catch {
119
- // keep default
120
- }
121
- const specsChanges = path.join(cwd, specsRoot, 'changes');
122
- const specsDir = path.join(specsChanges, change);
123
- if (session.planType === 'specs') {
124
- return forWrite ? specsDir : liveOrArchived(specsChanges, change);
125
- }
126
-
127
- // planType unknown — prefer whichever exists (live first, then archived)
128
- if (fs.existsSync(openspecDir)) return openspecDir;
129
- if (fs.existsSync(specsDir)) return specsDir;
130
- if (forWrite) return openspecDir;
131
- return (
132
- findArchivedChangeDir(openspecChanges, change) ??
133
- findArchivedChangeDir(specsChanges, change) ??
134
- openspecDir
135
- );
136
- }
137
-
138
- /**
139
- * Path to spine.json: change dir when available, else session dir.
140
- *
141
- * @param {{ cwd?: string, session?: Record<string, unknown> | null, sessionDir?: string }} opts
142
- */
143
- export function spinePath(opts = {}) {
144
- const changeDir = resolveChangeDir(opts);
145
- if (changeDir) return path.join(changeDir, SPINE_FILE);
146
- if (opts.sessionDir) return path.join(opts.sessionDir, SPINE_FILE);
147
- throw new Error('Cannot resolve spine.json location: no change and no session dir');
148
- }
149
-
150
- /**
151
- * @param {{ change?: string | null }} [opts]
152
- */
153
- export function spineTemplate(opts = {}) {
154
- return {
155
- change: opts.change ?? null,
156
- notApplicable: null,
157
- rows: [
158
- {
159
- capability: '<REQ id or capability cluster, e.g. REQ-GOV-01 matching>',
160
- library: '<module path, e.g. services/etl-core/matcher.py>',
161
- runtimeOwner: '<production caller, e.g. worker job analyze_study>',
162
- writes: '<artifact/collection, e.g. study_proposals>',
163
- reads: '<consumed inputs, or N/A>',
164
- uiConsumer: '<UI/API surface reading the writes, or N/A>',
165
- evidence: '<tier-2/E2E evidence path proving the wired path>',
166
- },
167
- ],
168
- };
169
- }
170
-
171
- /**
172
- * Scaffold spine.json (refuses to overwrite unless force).
173
- *
174
- * @param {{ file: string, change?: string | null, force?: boolean }} opts
175
- */
176
- export function initSpine(opts) {
177
- if (fs.existsSync(opts.file) && !opts.force) {
178
- throw new Error(`spine.json already exists: ${opts.file} (use --force to overwrite)`);
179
- }
180
- writeJson(opts.file, spineTemplate({ change: opts.change }));
181
- return opts.file;
182
- }
183
-
184
- /**
185
- * Validate a spine document.
186
- *
187
- * Valid when either:
188
- * - `notApplicable` is a non-empty string (honest opt-out, e.g. docs-only), or
189
- * - `rows` is a non-empty array where every required cell is filled and no
190
- * cell still contains scaffold placeholders (`<...>`).
191
- *
192
- * @param {unknown} doc
193
- * @returns {{ ok: boolean, problems: string[] }}
194
- */
195
- export function validateSpine(doc) {
196
- /** @type {string[]} */
197
- const problems = [];
198
- if (!doc || typeof doc !== 'object' || Array.isArray(doc)) {
199
- return { ok: false, problems: ['spine.json is not an object'] };
200
- }
201
- const spine = /** @type {Record<string, unknown>} */ (doc);
202
-
203
- if (isNonEmptyString(spine.notApplicable)) {
204
- return { ok: true, problems: [] };
205
- }
206
-
207
- const rows = spine.rows;
208
- if (!Array.isArray(rows) || rows.length === 0) {
209
- return {
210
- ok: false,
211
- problems: ['spine.rows is empty — add one row per capability, or set notApplicable with a reason'],
212
- };
213
- }
214
-
215
- rows.forEach((row, i) => {
216
- if (!row || typeof row !== 'object' || Array.isArray(row)) {
217
- problems.push(`row ${i + 1}: not an object`);
218
- return;
219
- }
220
- const r = /** @type {Record<string, unknown>} */ (row);
221
- for (const field of SPINE_ROW_REQUIRED) {
222
- const value = r[field];
223
- if (!isNonEmptyString(value)) {
224
- problems.push(`row ${i + 1} (${r.capability ?? '?'}): missing ${field}`);
225
- } else if (/^<.*>$/.test(value.trim())) {
226
- problems.push(`row ${i + 1} (${r.capability ?? '?'}): ${field} still has scaffold placeholder`);
227
- }
228
- }
229
- });
230
-
231
- return { ok: problems.length === 0, problems };
232
- }
233
-
234
- /* ------------------------------------------------------------------ */
235
- /* E2E acceptance — executable product loop */
236
- /* ------------------------------------------------------------------ */
237
-
238
- const E2E_FILE = 'e2e.json';
239
- const E2E_RESULTS_FILE = 'e2e-results.json';
240
- export const E2E_DEFAULT_TIMEOUT_MS = 300_000;
241
-
242
- /**
243
- * Path to e2e.json: change dir when available, else session dir.
244
- *
245
- * @param {{ cwd?: string, session?: Record<string, unknown> | null, sessionDir?: string }} opts
246
- */
247
- export function e2ePath(opts = {}) {
248
- const changeDir = resolveChangeDir(opts);
249
- if (changeDir) return path.join(changeDir, E2E_FILE);
250
- if (opts.sessionDir) return path.join(opts.sessionDir, E2E_FILE);
251
- throw new Error('Cannot resolve e2e.json location: no change and no session dir');
252
- }
253
-
254
- /**
255
- * @param {{ change?: string | null }} [opts]
256
- */
257
- export function e2eTemplate(opts = {}) {
258
- return {
259
- change: opts.change ?? null,
260
- notApplicable: null,
261
- steps: [
262
- {
263
- name: '<boot>',
264
- cmd: '<command that starts the system, e.g. docker compose up -d api worker>',
265
- },
266
- {
267
- name: '<produce>',
268
- cmd: '<command that drives the real production entry point, e.g. node scripts/e2e/enqueue-analyze.mjs>',
269
- expect: '<regex the combined output must match — delete this field if exit code 0 is enough>',
270
- },
271
- {
272
- name: '<consume-assert>',
273
- cmd: '<command that proves the domain side effects exist, e.g. node scripts/e2e/assert-ratified.mjs>',
274
- },
275
- ],
276
- };
277
- }
278
-
279
- /**
280
- * Scaffold e2e.json (refuses to overwrite unless force).
281
- *
282
- * @param {{ file: string, change?: string | null, force?: boolean }} opts
283
- */
284
- export function initE2e(opts) {
285
- if (fs.existsSync(opts.file) && !opts.force) {
286
- throw new Error(`e2e.json already exists: ${opts.file} (use --force to overwrite)`);
287
- }
288
- writeJson(opts.file, e2eTemplate({ change: opts.change }));
289
- return opts.file;
290
- }
291
-
292
- /**
293
- * Validate an e2e document.
294
- *
295
- * Valid when either:
296
- * - `notApplicable` is a non-empty string (loop cannot be driven by any
297
- * command — reviewers police the reason), or
298
- * - `steps` is a non-empty array where every step has a filled `name` and
299
- * `cmd` (no scaffold placeholders), `expect` (optional) is a valid regex,
300
- * and `timeoutMs` (optional) is a positive number.
301
- *
302
- * @param {unknown} doc
303
- * @returns {{ ok: boolean, problems: string[] }}
304
- */
305
- export function validateE2e(doc) {
306
- /** @type {string[]} */
307
- const problems = [];
308
- if (!doc || typeof doc !== 'object' || Array.isArray(doc)) {
309
- return { ok: false, problems: ['e2e.json is not an object'] };
310
- }
311
- const e2e = /** @type {Record<string, unknown>} */ (doc);
312
-
313
- if (isNonEmptyString(e2e.notApplicable)) {
314
- return { ok: true, problems: [] };
315
- }
316
-
317
- const steps = e2e.steps;
318
- if (!Array.isArray(steps) || steps.length === 0) {
319
- return {
320
- ok: false,
321
- problems: [
322
- 'e2e.steps is empty — add executable product-loop steps, or set notApplicable with a reason',
323
- ],
324
- };
325
- }
326
-
327
- steps.forEach((step, i) => {
328
- if (!step || typeof step !== 'object' || Array.isArray(step)) {
329
- problems.push(`step ${i + 1}: not an object`);
330
- return;
331
- }
332
- const s = /** @type {Record<string, unknown>} */ (step);
333
- for (const field of ['name', 'cmd']) {
334
- const value = s[field];
335
- if (!isNonEmptyString(value)) {
336
- problems.push(`step ${i + 1} (${s.name ?? '?'}): missing ${field}`);
337
- } else if (/^<.*>$/.test(value.trim())) {
338
- problems.push(`step ${i + 1} (${s.name ?? '?'}): ${field} still has scaffold placeholder`);
339
- }
340
- }
341
- if (s.expect !== undefined && s.expect !== null) {
342
- if (!isNonEmptyString(s.expect)) {
343
- problems.push(`step ${i + 1} (${s.name ?? '?'}): expect must be a non-empty regex string`);
344
- } else if (/^<.*>$/.test(s.expect.trim())) {
345
- problems.push(`step ${i + 1} (${s.name ?? '?'}): expect still has scaffold placeholder`);
346
- } else {
347
- try {
348
- new RegExp(s.expect);
349
- } catch {
350
- problems.push(`step ${i + 1} (${s.name ?? '?'}): expect is not a valid regex`);
351
- }
352
- }
353
- }
354
- if (s.timeoutMs !== undefined && (typeof s.timeoutMs !== 'number' || s.timeoutMs <= 0)) {
355
- problems.push(`step ${i + 1} (${s.name ?? '?'}): timeoutMs must be a positive number`);
356
- }
357
- });
358
-
359
- return { ok: problems.length === 0, problems };
360
- }
361
-
362
- /**
363
- * Hash of the step list — recorded in results so editing e2e.json after a
364
- * green run invalidates the results.
365
- *
366
- * @param {unknown[]} steps
367
- */
368
- export function e2eStepsHash(steps) {
369
- return crypto.createHash('sha256').update(JSON.stringify(steps ?? [])).digest('hex');
370
- }
371
-
372
- /**
373
- * @param {string} text
374
- */
375
- function outputTail(text, lines = 30) {
376
- if (!text) return '';
377
- return text.split(/\r?\n/).slice(-lines).join('\n').trim();
378
- }
379
-
380
- /**
381
- * Execute e2e steps sequentially (shell). Stops at the first failure —
382
- * later steps depend on earlier ones. Exit code must be 0 and `expect`
383
- * (when present) must match combined stdout+stderr.
384
- *
385
- * @param {{ steps?: unknown[] }} doc — a validated e2e document with steps
386
- * @param {{ cwd?: string }} [opts]
387
- */
388
- export function runE2eSteps(doc, opts = {}) {
389
- const cwd = opts.cwd ?? process.cwd();
390
- const steps = Array.isArray(doc?.steps) ? doc.steps : [];
391
- /** @type {Array<Record<string, unknown>>} */
392
- const results = [];
393
- let ok = true;
394
-
395
- for (const step of steps) {
396
- const s = /** @type {Record<string, any>} */ (step);
397
- if (!ok) {
398
- results.push({ name: s.name, cmd: s.cmd, skipped: true });
399
- continue;
400
- }
401
- const started = Date.now();
402
- const r = spawnSync(s.cmd, {
403
- shell: true,
404
- cwd,
405
- encoding: 'utf8',
406
- timeout: typeof s.timeoutMs === 'number' ? s.timeoutMs : E2E_DEFAULT_TIMEOUT_MS,
407
- maxBuffer: 10 * 1024 * 1024,
408
- });
409
- const output = `${r.stdout ?? ''}${r.stderr ?? ''}`;
410
- const exitCode = typeof r.status === 'number' ? r.status : null;
411
- let expectMatched = null;
412
- let stepOk = exitCode === 0;
413
- if (stepOk && isNonEmptyString(s.expect)) {
414
- expectMatched = new RegExp(s.expect).test(output);
415
- stepOk = expectMatched;
416
- }
417
- results.push({
418
- name: s.name,
419
- cmd: s.cmd,
420
- exitCode,
421
- expectMatched,
422
- ok: stepOk,
423
- durationMs: Date.now() - started,
424
- outputTail: outputTail(output),
425
- error: r.error ? String(r.error.message ?? r.error) : null,
426
- });
427
- if (!stepOk) ok = false;
428
- }
429
-
430
- return {
431
- ok,
432
- ranAt: new Date().toISOString(),
433
- stepsHash: e2eStepsHash(steps),
434
- steps: results,
435
- };
436
- }
437
-
438
- /**
439
- * @param {string} sessionDir
440
- */
441
- export function e2eResultsPath(sessionDir) {
442
- return path.join(sessionDir, E2E_RESULTS_FILE);
443
- }
444
-
445
- /**
446
- * @param {string} sessionDir
447
- * @param {ReturnType<typeof runE2eSteps>} results
448
- */
449
- export function writeE2eResults(sessionDir, results) {
450
- writeJson(e2eResultsPath(sessionDir), results);
451
- return e2eResultsPath(sessionDir);
452
- }
453
-
454
- /**
455
- * @param {string} sessionDir
456
- * @returns {Record<string, any> | null}
457
- */
458
- export function loadE2eResults(sessionDir) {
459
- const file = e2eResultsPath(sessionDir);
460
- if (!fs.existsSync(file)) return null;
461
- try {
462
- return readJson(file);
463
- } catch {
464
- return null;
465
- }
466
- }
467
-
468
- /**
469
- * Gate check for the executable E2E acceptance. Returns the problems that
470
- * block `forge phase done` — empty when the loop was executed green (and the
471
- * results are current), or when e2e.json honestly opts out via notApplicable.
472
- *
473
- * @param {{ e2eFile: string, sessionDir: string }} opts
474
- * @returns {{ problems: string[], notApplicable: boolean }}
475
- */
476
- export function checkE2eGate(opts) {
477
- /** @type {string[]} */
478
- const problems = [];
479
-
480
- if (!fs.existsSync(opts.e2eFile)) {
481
- problems.push(
482
- `e2e.json required at ${opts.e2eFile} — run forge e2e init, author the product-loop steps, then forge e2e run. Spine rows mean an async loop exists; it must be executed, not described.`,
483
- );
484
- return { problems, notApplicable: false };
485
- }
486
-
487
- let doc;
488
- try {
489
- doc = readJson(opts.e2eFile);
490
- } catch (err) {
491
- problems.push(`e2e.json unreadable: ${err instanceof Error ? err.message : err}`);
492
- return { problems, notApplicable: false };
493
- }
494
-
495
- const valid = validateE2e(doc);
496
- if (!valid.ok) {
497
- problems.push(...valid.problems.map((p) => `e2e: ${p}`));
498
- return { problems, notApplicable: false };
499
- }
500
-
501
- if (isNonEmptyString(doc.notApplicable)) {
502
- return { problems: [], notApplicable: true };
503
- }
504
-
505
- const results = loadE2eResults(opts.sessionDir);
506
- if (!results) {
507
- problems.push('e2e-results.json missing — run forge e2e run (a green run is required before done)');
508
- } else if (results.stepsHash !== e2eStepsHash(doc.steps)) {
509
- problems.push('e2e-results.json is stale — e2e.json changed since the last run; re-run forge e2e run');
510
- } else if (!results.ok) {
511
- const failed = Array.isArray(results.steps) ? results.steps.find((s) => s?.ok === false) : null;
512
- problems.push(
513
- `e2e run failed${failed ? ` at step "${failed.name}"` : ''} fix and re-run forge e2e run`,
514
- );
515
- }
516
-
517
- return { problems, notApplicable: false };
518
- }
519
-
520
- /**
521
- * @param {string} sessionDir
522
- */
523
- export function deferralsPath(sessionDir) {
524
- return path.join(sessionDir, DEFERRALS_FILE);
525
- }
526
-
527
- /**
528
- * @param {string} sessionDir
529
- * @returns {{ deferrals: Array<{ task: string, reason: string, createdAt: string, resolvedAt: string | null }> }}
530
- */
531
- export function loadDeferrals(sessionDir) {
532
- const file = deferralsPath(sessionDir);
533
- if (!fs.existsSync(file)) return { deferrals: [] };
534
- const doc = readJson(file);
535
- return { deferrals: Array.isArray(doc?.deferrals) ? doc.deferrals : [] };
536
- }
537
-
538
- /**
539
- * @param {string} sessionDir
540
- * @param {{ task: string, reason: string }} entry
541
- */
542
- export function addDeferral(sessionDir, entry) {
543
- if (!isNonEmptyString(entry.task)) throw new Error('Deferral requires --task <id>');
544
- if (!isNonEmptyString(entry.reason)) throw new Error('Deferral requires --reason "<why>"');
545
- const doc = loadDeferrals(sessionDir);
546
- if (doc.deferrals.some((d) => d.task === entry.task && !d.resolvedAt)) {
547
- throw new Error(`Deferral for task ${entry.task} already open`);
548
- }
549
- doc.deferrals.push({
550
- task: entry.task,
551
- reason: entry.reason,
552
- createdAt: new Date().toISOString(),
553
- resolvedAt: null,
554
- });
555
- writeJson(deferralsPath(sessionDir), doc);
556
- return doc;
557
- }
558
-
559
- /**
560
- * @param {string} sessionDir
561
- * @param {string} task
562
- */
563
- export function resolveDeferral(sessionDir, task) {
564
- const doc = loadDeferrals(sessionDir);
565
- const open = doc.deferrals.find((d) => d.task === task && !d.resolvedAt);
566
- if (!open) throw new Error(`No open deferral for task ${task}`);
567
- open.resolvedAt = new Date().toISOString();
568
- writeJson(deferralsPath(sessionDir), doc);
569
- return doc;
570
- }
571
-
572
- /**
573
- * @param {string} sessionDir
574
- */
575
- export function openDeferrals(sessionDir) {
576
- return loadDeferrals(sessionDir).deferrals.filter((d) => !d.resolvedAt);
577
- }
578
-
579
- /**
580
- * @param {Record<string, unknown> | null | undefined} session
581
- */
582
- export function sessionJobsSignalText(session) {
583
- return [session?.paceSignal, session?.slug, session?.openspecChange]
584
- .filter((v) => isNonEmptyString(v))
585
- .join(' ');
586
- }
587
-
588
- /**
589
- * Run the mechanical integrity checks for a session.
590
- *
591
- * Checks:
592
- * 1. No unresolved deferrals.
593
- * 2. spine.json — **always required** (filled rows, or `notApplicable` with a
594
- * reason). Keyword sniffing is not enough to decide; missing spine is how
595
- * library-only platforms checkbox past gaps.
596
- * 3. E2E acceptance — when a spine has real rows (not notApplicable):
597
- * e2e.json must exist with filled steps (or its own notApplicable reason)
598
- * and e2e-results.json must record a green, current run (steps hash must
599
- * match). Prose in verify-evidence.md does not satisfy this; an explicit
600
- * BLOCKED marker there still means the change cannot be done. Sync-only
601
- * work should prefer spine `notApplicable` over inventing a fake loop.
602
- *
603
- * @param {{ cwd?: string, sessionDir: string, session: Record<string, unknown> }} opts
604
- * @returns {{ ok: boolean, problems: string[], spineFile: string, spineExists: boolean, e2eFile: string | null }}
605
- */
606
- /**
607
- * Project-level e2e off switch: `.forge/config.json` `e2e.disabled` set to a
608
- * non-empty reason string. Operator-set via `forge e2e disable "<reason>"` —
609
- * agents must never set it themselves. When set, the integrity gate stops
610
- * demanding an executed green e2e run (the most time-consuming part of a
611
- * session); spine, deferrals, evidence, and BLOCKED checks still apply.
612
- *
613
- * @param {string} [cwd]
614
- * @returns {string | null} the reason, or null when e2e is enabled
615
- */
616
- export function e2eDisabledReason(cwd = process.cwd()) {
617
- try {
618
- const reason = loadProjectConfig(cwd)?.e2e?.disabled;
619
- return isNonEmptyString(reason) ? reason : null;
620
- } catch {
621
- return null;
622
- }
623
- }
624
-
625
- export function runIntegrityChecks(opts) {
626
- /** @type {string[]} */
627
- const problems = [];
628
- const { sessionDir, session } = opts;
629
- const cwd = opts.cwd ?? process.cwd();
630
-
631
- const open = openDeferrals(sessionDir);
632
- if (open.length > 0) {
633
- problems.push(
634
- `unresolved deferrals: ${open.map((d) => `${d.task} (${d.reason})`).join('; ')} — resolve via forge defer resolve --task <id>`,
635
- );
636
- }
637
-
638
- const spineFile = spinePath({ cwd, session, sessionDir });
639
- const spineExists = fs.existsSync(spineFile);
640
-
641
- /** @type {ReturnType<typeof validateSpine> | null} */
642
- let spineResult = null;
643
- let spineHasRows = false;
644
- if (!spineExists) {
645
- problems.push(
646
- `spine.json required at ${spineFile} — run forge spine init, then fill rows (or set notApplicable with a reason). Spine is mandatory for every change so capability→runtime wiring cannot be skipped by accident.`,
647
- );
648
- } else {
649
- try {
650
- const doc = readJson(spineFile);
651
- spineResult = validateSpine(doc);
652
- spineHasRows =
653
- Array.isArray(doc?.rows) &&
654
- doc.rows.length > 0 &&
655
- !isNonEmptyString(doc?.notApplicable);
656
- } catch (err) {
657
- spineResult = {
658
- ok: false,
659
- problems: [`spine.json unreadable: ${err instanceof Error ? err.message : err}`],
660
- };
661
- }
662
- if (!spineResult.ok) {
663
- problems.push(...spineResult.problems.map((p) => `spine: ${p}`));
664
- }
665
- }
666
-
667
- let e2eFile = null;
668
- const e2eDisabled = e2eDisabledReason(cwd);
669
- if (spineExists && spineHasRows) {
670
- if (!e2eDisabled) {
671
- e2eFile = e2ePath({ cwd, session, sessionDir });
672
- problems.push(...checkE2eGate({ e2eFile, sessionDir }).problems);
673
- }
674
-
675
- const evidenceFile = path.join(sessionDir, 'verify-evidence.md');
676
- if (fs.existsSync(evidenceFile) && /\bBLOCKED\b/.test(fs.readFileSync(evidenceFile, 'utf8'))) {
677
- problems.push('verify-evidence.md contains BLOCKED change cannot be marked done while E2E is blocked');
678
- }
679
- }
680
-
681
- return { ok: problems.length === 0, problems, spineFile, spineExists, e2eFile, e2eDisabled };
682
- }
1
+ /**
2
+ * Forge runtime-integrity mechanics: spine matrix, deferral registry,
3
+ * executable E2E acceptance, and the integrity checks that gate
4
+ * `forge phase done|finish`.
5
+ *
6
+ * Spine matrix — `spine.json` in the change dir (or session dir when the
7
+ * session has no tracked change). One row per capability/REQ cluster:
8
+ * library → runtime owner → writes → reads → UI consumer → evidence.
9
+ * Library-only rows (missing runtime owner / writes / evidence) fail
10
+ * validation, so "wire later" cannot be checkboxed past `forge phase done`.
11
+ *
12
+ * E2E acceptance — `e2e.json` next to the spine: the closed product loop as
13
+ * an executable step list. `forge e2e run` executes it and records
14
+ * `e2e-results.json` (session dir) with a hash of the steps, so results go
15
+ * stale when steps change. When the spine has real rows, the gate requires a
16
+ * green, current run — prose in verify-evidence.md no longer satisfies it.
17
+ *
18
+ * Deferral registry — `deferrals.json` in the session dir. Reviewers may only
19
+ * accept "wiring deferred" when a registered deferral names the open task;
20
+ * unresolved deferrals block done/finish.
21
+ */
22
+
23
+ import crypto from 'node:crypto';
24
+ import fs from 'node:fs';
25
+ import path from 'node:path';
26
+ import { spawnSync } from 'node:child_process';
27
+ import { readJson, writeJson } from './lib.mjs';
28
+ import { loadProjectConfig } from './config.mjs';
29
+ import { DEFAULT_SPECS_DIR, resolveProjectPlanEngine } from './plan-engine.mjs';
30
+
31
+ /** Signals that a change involves jobs/workers and therefore needs a spine. */
32
+ export const JOBS_SIGNAL_RE =
33
+ /\b(worker|workers|job|jobs|queue|queues|pipeline|pipelines|etl|orchestration|handler|handlers|cron|scheduler|daemon|ingest|dispatch)\b/i;
34
+
35
+ /** Row fields that must be filled (reads/uiConsumer accept "N/A"). */
36
+ export const SPINE_ROW_REQUIRED = Object.freeze([
37
+ 'capability',
38
+ 'library',
39
+ 'runtimeOwner',
40
+ 'writes',
41
+ 'reads',
42
+ 'uiConsumer',
43
+ 'evidence',
44
+ ]);
45
+
46
+ const SPINE_FILE = 'spine.json';
47
+ const DEFERRALS_FILE = 'deferrals.json';
48
+
49
+ /**
50
+ * @param {unknown} value
51
+ */
52
+ function isNonEmptyString(value) {
53
+ return typeof value === 'string' && value.trim().length > 0;
54
+ }
55
+
56
+ /**
57
+ * Resolve the change directory for a session (openspec or specs engine).
58
+ * Returns null when the session has no tracked change.
59
+ *
60
+ * @param {{ cwd?: string, session?: Record<string, unknown> | null }} opts
61
+ */
62
+ /**
63
+ * The archived copy of a change, when the live dir is gone. Archiving moves
64
+ * `changes/<change>/` to `changes/archive/<YYYY-MM-DD>-<change>/`, so the
65
+ * done-gate integrity check (which runs *after* archive in the finish flow)
66
+ * would otherwise never find spine.json/e2e.json. Returns null if no match.
67
+ *
68
+ * @param {string} changesDir absolute path to `<root>/changes`
69
+ * @param {string} change change name
70
+ */
71
+ function findArchivedChangeDir(changesDir, change) {
72
+ const archiveDir = path.join(changesDir, 'archive');
73
+ if (!fs.existsSync(archiveDir)) return null;
74
+ let entries;
75
+ try {
76
+ entries = fs.readdirSync(archiveDir, { withFileTypes: true });
77
+ } catch {
78
+ return null;
79
+ }
80
+ const matches = entries
81
+ .filter((e) => e.isDirectory())
82
+ .map((e) => e.name)
83
+ // CLI + documented manual archive both name dirs `YYYY-MM-DD-<change>`;
84
+ // slice(11) drops the `YYYY-MM-DD-` prefix so the suffix must equal the
85
+ // change exactly (no false match on `…-other-<change>`).
86
+ .filter((name) => name === change || (/^\d{4}-\d{2}-\d{2}-/.test(name) && name.slice(11) === change))
87
+ .sort();
88
+ // Date-prefixed names sort lexically by date — newest archive wins.
89
+ return matches.length ? path.join(archiveDir, matches[matches.length - 1]) : null;
90
+ }
91
+
92
+ /** Live change dir if present, else its archived copy, else the live path. */
93
+ function liveOrArchived(changesDir, change) {
94
+ const liveDir = path.join(changesDir, change);
95
+ if (fs.existsSync(liveDir)) return liveDir;
96
+ return findArchivedChangeDir(changesDir, change) ?? liveDir;
97
+ }
98
+
99
+ export function resolveChangeDir(opts = {}) {
100
+ const cwd = opts.cwd ?? process.cwd();
101
+ const session = opts.session ?? null;
102
+ // Write callers (spine/e2e init) must always target the LIVE change dir —
103
+ // scaffolding into an archived record would corrupt frozen history. Only
104
+ // read callers (checks, gates) fall back to the archive.
105
+ const forWrite = opts.forWrite === true;
106
+ const change = session && isNonEmptyString(session.openspecChange) ? session.openspecChange : null;
107
+ if (!change) return null;
108
+
109
+ const openspecChanges = path.join(cwd, 'openspec', 'changes');
110
+ const openspecDir = path.join(openspecChanges, change);
111
+ if (session.planType === 'openspec') {
112
+ return forWrite ? openspecDir : liveOrArchived(openspecChanges, change);
113
+ }
114
+
115
+ // Only a *specs* engine resolution can name the specs dir. The engine
116
+ // resolver's last resort is {engine:'openspec', dir:'openspec'}, so taking
117
+ // `.dir` unconditionally pointed specs sessions at `openspec/changes/<name>`
118
+ // in every project whose .forge/config.json has no `plan` block — the change
119
+ // dir, its brief.html, spine.json and e2e.json all silently missing.
120
+ let specsRoot = DEFAULT_SPECS_DIR;
121
+ try {
122
+ const engine = resolveProjectPlanEngine(cwd, { useUserDefault: false });
123
+ if (engine.engine === 'specs') specsRoot = engine.dir;
124
+ } catch {
125
+ // keep default
126
+ }
127
+ const specsChanges = path.join(cwd, specsRoot, 'changes');
128
+ const specsDir = path.join(specsChanges, change);
129
+ if (session.planType === 'specs') {
130
+ return forWrite ? specsDir : liveOrArchived(specsChanges, change);
131
+ }
132
+
133
+ // planType unknown — prefer whichever exists (live first, then archived)
134
+ if (fs.existsSync(openspecDir)) return openspecDir;
135
+ if (fs.existsSync(specsDir)) return specsDir;
136
+ if (forWrite) return openspecDir;
137
+ return (
138
+ findArchivedChangeDir(openspecChanges, change) ??
139
+ findArchivedChangeDir(specsChanges, change) ??
140
+ openspecDir
141
+ );
142
+ }
143
+
144
+ /**
145
+ * Path to spine.json: change dir when available, else session dir.
146
+ *
147
+ * @param {{ cwd?: string, session?: Record<string, unknown> | null, sessionDir?: string }} opts
148
+ */
149
+ export function spinePath(opts = {}) {
150
+ const changeDir = resolveChangeDir(opts);
151
+ if (changeDir) return path.join(changeDir, SPINE_FILE);
152
+ if (opts.sessionDir) return path.join(opts.sessionDir, SPINE_FILE);
153
+ throw new Error('Cannot resolve spine.json location: no change and no session dir');
154
+ }
155
+
156
+ /**
157
+ * @param {{ change?: string | null }} [opts]
158
+ */
159
+ export function spineTemplate(opts = {}) {
160
+ return {
161
+ change: opts.change ?? null,
162
+ notApplicable: null,
163
+ rows: [
164
+ {
165
+ capability: '<REQ id or capability cluster, e.g. REQ-GOV-01 matching>',
166
+ library: '<module path, e.g. services/etl-core/matcher.py>',
167
+ runtimeOwner: '<production caller, e.g. worker job analyze_study>',
168
+ writes: '<artifact/collection, e.g. study_proposals>',
169
+ reads: '<consumed inputs, or N/A>',
170
+ uiConsumer: '<UI/API surface reading the writes, or N/A>',
171
+ evidence: '<tier-2/E2E evidence path proving the wired path>',
172
+ },
173
+ ],
174
+ };
175
+ }
176
+
177
+ /**
178
+ * Scaffold spine.json (refuses to overwrite unless force).
179
+ *
180
+ * @param {{ file: string, change?: string | null, force?: boolean }} opts
181
+ */
182
+ export function initSpine(opts) {
183
+ if (fs.existsSync(opts.file) && !opts.force) {
184
+ throw new Error(`spine.json already exists: ${opts.file} (use --force to overwrite)`);
185
+ }
186
+ writeJson(opts.file, spineTemplate({ change: opts.change }));
187
+ return opts.file;
188
+ }
189
+
190
+ /**
191
+ * Validate a spine document.
192
+ *
193
+ * Valid when either:
194
+ * - `notApplicable` is a non-empty string (honest opt-out, e.g. docs-only), or
195
+ * - `rows` is a non-empty array where every required cell is filled and no
196
+ * cell still contains scaffold placeholders (`<...>`).
197
+ *
198
+ * @param {unknown} doc
199
+ * @returns {{ ok: boolean, problems: string[] }}
200
+ */
201
+ export function validateSpine(doc) {
202
+ /** @type {string[]} */
203
+ const problems = [];
204
+ if (!doc || typeof doc !== 'object' || Array.isArray(doc)) {
205
+ return { ok: false, problems: ['spine.json is not an object'] };
206
+ }
207
+ const spine = /** @type {Record<string, unknown>} */ (doc);
208
+
209
+ if (isNonEmptyString(spine.notApplicable)) {
210
+ return { ok: true, problems: [] };
211
+ }
212
+
213
+ const rows = spine.rows;
214
+ if (!Array.isArray(rows) || rows.length === 0) {
215
+ return {
216
+ ok: false,
217
+ problems: ['spine.rows is empty — add one row per capability, or set notApplicable with a reason'],
218
+ };
219
+ }
220
+
221
+ rows.forEach((row, i) => {
222
+ if (!row || typeof row !== 'object' || Array.isArray(row)) {
223
+ problems.push(`row ${i + 1}: not an object`);
224
+ return;
225
+ }
226
+ const r = /** @type {Record<string, unknown>} */ (row);
227
+ for (const field of SPINE_ROW_REQUIRED) {
228
+ const value = r[field];
229
+ if (!isNonEmptyString(value)) {
230
+ problems.push(`row ${i + 1} (${r.capability ?? '?'}): missing ${field}`);
231
+ } else if (/^<.*>$/.test(value.trim())) {
232
+ problems.push(`row ${i + 1} (${r.capability ?? '?'}): ${field} still has scaffold placeholder`);
233
+ }
234
+ }
235
+ });
236
+
237
+ return { ok: problems.length === 0, problems };
238
+ }
239
+
240
+ /* ------------------------------------------------------------------ */
241
+ /* E2E acceptance — executable product loop */
242
+ /* ------------------------------------------------------------------ */
243
+
244
+ const E2E_FILE = 'e2e.json';
245
+ const E2E_RESULTS_FILE = 'e2e-results.json';
246
+ export const E2E_DEFAULT_TIMEOUT_MS = 300_000;
247
+
248
+ /**
249
+ * Path to e2e.json: change dir when available, else session dir.
250
+ *
251
+ * @param {{ cwd?: string, session?: Record<string, unknown> | null, sessionDir?: string }} opts
252
+ */
253
+ export function e2ePath(opts = {}) {
254
+ const changeDir = resolveChangeDir(opts);
255
+ if (changeDir) return path.join(changeDir, E2E_FILE);
256
+ if (opts.sessionDir) return path.join(opts.sessionDir, E2E_FILE);
257
+ throw new Error('Cannot resolve e2e.json location: no change and no session dir');
258
+ }
259
+
260
+ /**
261
+ * @param {{ change?: string | null }} [opts]
262
+ */
263
+ export function e2eTemplate(opts = {}) {
264
+ return {
265
+ change: opts.change ?? null,
266
+ notApplicable: null,
267
+ steps: [
268
+ {
269
+ name: '<boot>',
270
+ cmd: '<command that starts the system, e.g. docker compose up -d api worker>',
271
+ },
272
+ {
273
+ name: '<produce>',
274
+ cmd: '<command that drives the real production entry point, e.g. node scripts/e2e/enqueue-analyze.mjs>',
275
+ expect: '<regex the combined output must match — delete this field if exit code 0 is enough>',
276
+ },
277
+ {
278
+ name: '<consume-assert>',
279
+ cmd: '<command that proves the domain side effects exist, e.g. node scripts/e2e/assert-ratified.mjs>',
280
+ },
281
+ ],
282
+ };
283
+ }
284
+
285
+ /**
286
+ * Scaffold e2e.json (refuses to overwrite unless force).
287
+ *
288
+ * @param {{ file: string, change?: string | null, force?: boolean }} opts
289
+ */
290
+ export function initE2e(opts) {
291
+ if (fs.existsSync(opts.file) && !opts.force) {
292
+ throw new Error(`e2e.json already exists: ${opts.file} (use --force to overwrite)`);
293
+ }
294
+ writeJson(opts.file, e2eTemplate({ change: opts.change }));
295
+ return opts.file;
296
+ }
297
+
298
+ /**
299
+ * Validate an e2e document.
300
+ *
301
+ * Valid when either:
302
+ * - `notApplicable` is a non-empty string (loop cannot be driven by any
303
+ * command reviewers police the reason), or
304
+ * - `steps` is a non-empty array where every step has a filled `name` and
305
+ * `cmd` (no scaffold placeholders), `expect` (optional) is a valid regex,
306
+ * and `timeoutMs` (optional) is a positive number.
307
+ *
308
+ * @param {unknown} doc
309
+ * @returns {{ ok: boolean, problems: string[] }}
310
+ */
311
+ export function validateE2e(doc) {
312
+ /** @type {string[]} */
313
+ const problems = [];
314
+ if (!doc || typeof doc !== 'object' || Array.isArray(doc)) {
315
+ return { ok: false, problems: ['e2e.json is not an object'] };
316
+ }
317
+ const e2e = /** @type {Record<string, unknown>} */ (doc);
318
+
319
+ if (isNonEmptyString(e2e.notApplicable)) {
320
+ return { ok: true, problems: [] };
321
+ }
322
+
323
+ const steps = e2e.steps;
324
+ if (!Array.isArray(steps) || steps.length === 0) {
325
+ return {
326
+ ok: false,
327
+ problems: [
328
+ 'e2e.steps is empty add executable product-loop steps, or set notApplicable with a reason',
329
+ ],
330
+ };
331
+ }
332
+
333
+ steps.forEach((step, i) => {
334
+ if (!step || typeof step !== 'object' || Array.isArray(step)) {
335
+ problems.push(`step ${i + 1}: not an object`);
336
+ return;
337
+ }
338
+ const s = /** @type {Record<string, unknown>} */ (step);
339
+ for (const field of ['name', 'cmd']) {
340
+ const value = s[field];
341
+ if (!isNonEmptyString(value)) {
342
+ problems.push(`step ${i + 1} (${s.name ?? '?'}): missing ${field}`);
343
+ } else if (/^<.*>$/.test(value.trim())) {
344
+ problems.push(`step ${i + 1} (${s.name ?? '?'}): ${field} still has scaffold placeholder`);
345
+ }
346
+ }
347
+ if (s.expect !== undefined && s.expect !== null) {
348
+ if (!isNonEmptyString(s.expect)) {
349
+ problems.push(`step ${i + 1} (${s.name ?? '?'}): expect must be a non-empty regex string`);
350
+ } else if (/^<.*>$/.test(s.expect.trim())) {
351
+ problems.push(`step ${i + 1} (${s.name ?? '?'}): expect still has scaffold placeholder`);
352
+ } else {
353
+ try {
354
+ new RegExp(s.expect);
355
+ } catch {
356
+ problems.push(`step ${i + 1} (${s.name ?? '?'}): expect is not a valid regex`);
357
+ }
358
+ }
359
+ }
360
+ if (s.timeoutMs !== undefined && (typeof s.timeoutMs !== 'number' || s.timeoutMs <= 0)) {
361
+ problems.push(`step ${i + 1} (${s.name ?? '?'}): timeoutMs must be a positive number`);
362
+ }
363
+ });
364
+
365
+ return { ok: problems.length === 0, problems };
366
+ }
367
+
368
+ /**
369
+ * Hash of the step list — recorded in results so editing e2e.json after a
370
+ * green run invalidates the results.
371
+ *
372
+ * @param {unknown[]} steps
373
+ */
374
+ export function e2eStepsHash(steps) {
375
+ return crypto.createHash('sha256').update(JSON.stringify(steps ?? [])).digest('hex');
376
+ }
377
+
378
+ /**
379
+ * @param {string} text
380
+ */
381
+ function outputTail(text, lines = 30) {
382
+ if (!text) return '';
383
+ return text.split(/\r?\n/).slice(-lines).join('\n').trim();
384
+ }
385
+
386
+ /**
387
+ * Execute e2e steps sequentially (shell). Stops at the first failure —
388
+ * later steps depend on earlier ones. Exit code must be 0 and `expect`
389
+ * (when present) must match combined stdout+stderr.
390
+ *
391
+ * @param {{ steps?: unknown[] }} doc — a validated e2e document with steps
392
+ * @param {{ cwd?: string }} [opts]
393
+ */
394
+ export function runE2eSteps(doc, opts = {}) {
395
+ const cwd = opts.cwd ?? process.cwd();
396
+ const steps = Array.isArray(doc?.steps) ? doc.steps : [];
397
+ /** @type {Array<Record<string, unknown>>} */
398
+ const results = [];
399
+ let ok = true;
400
+
401
+ for (const step of steps) {
402
+ const s = /** @type {Record<string, any>} */ (step);
403
+ if (!ok) {
404
+ results.push({ name: s.name, cmd: s.cmd, skipped: true });
405
+ continue;
406
+ }
407
+ const started = Date.now();
408
+ const r = spawnSync(s.cmd, {
409
+ shell: true,
410
+ cwd,
411
+ encoding: 'utf8',
412
+ timeout: typeof s.timeoutMs === 'number' ? s.timeoutMs : E2E_DEFAULT_TIMEOUT_MS,
413
+ maxBuffer: 10 * 1024 * 1024,
414
+ });
415
+ const output = `${r.stdout ?? ''}${r.stderr ?? ''}`;
416
+ const exitCode = typeof r.status === 'number' ? r.status : null;
417
+ let expectMatched = null;
418
+ let stepOk = exitCode === 0;
419
+ if (stepOk && isNonEmptyString(s.expect)) {
420
+ expectMatched = new RegExp(s.expect).test(output);
421
+ stepOk = expectMatched;
422
+ }
423
+ results.push({
424
+ name: s.name,
425
+ cmd: s.cmd,
426
+ exitCode,
427
+ expectMatched,
428
+ ok: stepOk,
429
+ durationMs: Date.now() - started,
430
+ outputTail: outputTail(output),
431
+ error: r.error ? String(r.error.message ?? r.error) : null,
432
+ });
433
+ if (!stepOk) ok = false;
434
+ }
435
+
436
+ return {
437
+ ok,
438
+ ranAt: new Date().toISOString(),
439
+ stepsHash: e2eStepsHash(steps),
440
+ steps: results,
441
+ };
442
+ }
443
+
444
+ /**
445
+ * @param {string} sessionDir
446
+ */
447
+ export function e2eResultsPath(sessionDir) {
448
+ return path.join(sessionDir, E2E_RESULTS_FILE);
449
+ }
450
+
451
+ /**
452
+ * @param {string} sessionDir
453
+ * @param {ReturnType<typeof runE2eSteps>} results
454
+ */
455
+ export function writeE2eResults(sessionDir, results) {
456
+ writeJson(e2eResultsPath(sessionDir), results);
457
+ return e2eResultsPath(sessionDir);
458
+ }
459
+
460
+ /**
461
+ * @param {string} sessionDir
462
+ * @returns {Record<string, any> | null}
463
+ */
464
+ export function loadE2eResults(sessionDir) {
465
+ const file = e2eResultsPath(sessionDir);
466
+ if (!fs.existsSync(file)) return null;
467
+ try {
468
+ return readJson(file);
469
+ } catch {
470
+ return null;
471
+ }
472
+ }
473
+
474
+ /**
475
+ * Gate check for the executable E2E acceptance. Returns the problems that
476
+ * block `forge phase done` — empty when the loop was executed green (and the
477
+ * results are current), or when e2e.json honestly opts out via notApplicable.
478
+ *
479
+ * @param {{ e2eFile: string, sessionDir: string }} opts
480
+ * @returns {{ problems: string[], notApplicable: boolean }}
481
+ */
482
+ export function checkE2eGate(opts) {
483
+ /** @type {string[]} */
484
+ const problems = [];
485
+
486
+ if (!fs.existsSync(opts.e2eFile)) {
487
+ problems.push(
488
+ `e2e.json required at ${opts.e2eFile} — run forge e2e init, author the product-loop steps, then forge e2e run. Spine rows mean an async loop exists; it must be executed, not described.`,
489
+ );
490
+ return { problems, notApplicable: false };
491
+ }
492
+
493
+ let doc;
494
+ try {
495
+ doc = readJson(opts.e2eFile);
496
+ } catch (err) {
497
+ problems.push(`e2e.json unreadable: ${err instanceof Error ? err.message : err}`);
498
+ return { problems, notApplicable: false };
499
+ }
500
+
501
+ const valid = validateE2e(doc);
502
+ if (!valid.ok) {
503
+ problems.push(...valid.problems.map((p) => `e2e: ${p}`));
504
+ return { problems, notApplicable: false };
505
+ }
506
+
507
+ if (isNonEmptyString(doc.notApplicable)) {
508
+ return { problems: [], notApplicable: true };
509
+ }
510
+
511
+ const results = loadE2eResults(opts.sessionDir);
512
+ if (!results) {
513
+ problems.push('e2e-results.json missing run forge e2e run (a green run is required before done)');
514
+ } else if (results.stepsHash !== e2eStepsHash(doc.steps)) {
515
+ problems.push('e2e-results.json is stale — e2e.json changed since the last run; re-run forge e2e run');
516
+ } else if (!results.ok) {
517
+ const failed = Array.isArray(results.steps) ? results.steps.find((s) => s?.ok === false) : null;
518
+ problems.push(
519
+ `e2e run failed${failed ? ` at step "${failed.name}"` : ''} — fix and re-run forge e2e run`,
520
+ );
521
+ }
522
+
523
+ return { problems, notApplicable: false };
524
+ }
525
+
526
+ /**
527
+ * @param {string} sessionDir
528
+ */
529
+ export function deferralsPath(sessionDir) {
530
+ return path.join(sessionDir, DEFERRALS_FILE);
531
+ }
532
+
533
+ /**
534
+ * @param {string} sessionDir
535
+ * @returns {{ deferrals: Array<{ task: string, reason: string, createdAt: string, resolvedAt: string | null }> }}
536
+ */
537
+ export function loadDeferrals(sessionDir) {
538
+ const file = deferralsPath(sessionDir);
539
+ if (!fs.existsSync(file)) return { deferrals: [] };
540
+ const doc = readJson(file);
541
+ return { deferrals: Array.isArray(doc?.deferrals) ? doc.deferrals : [] };
542
+ }
543
+
544
+ /**
545
+ * @param {string} sessionDir
546
+ * @param {{ task: string, reason: string }} entry
547
+ */
548
+ export function addDeferral(sessionDir, entry) {
549
+ if (!isNonEmptyString(entry.task)) throw new Error('Deferral requires --task <id>');
550
+ if (!isNonEmptyString(entry.reason)) throw new Error('Deferral requires --reason "<why>"');
551
+ const doc = loadDeferrals(sessionDir);
552
+ if (doc.deferrals.some((d) => d.task === entry.task && !d.resolvedAt)) {
553
+ throw new Error(`Deferral for task ${entry.task} already open`);
554
+ }
555
+ doc.deferrals.push({
556
+ task: entry.task,
557
+ reason: entry.reason,
558
+ createdAt: new Date().toISOString(),
559
+ resolvedAt: null,
560
+ });
561
+ writeJson(deferralsPath(sessionDir), doc);
562
+ return doc;
563
+ }
564
+
565
+ /**
566
+ * @param {string} sessionDir
567
+ * @param {string} task
568
+ */
569
+ export function resolveDeferral(sessionDir, task) {
570
+ const doc = loadDeferrals(sessionDir);
571
+ const open = doc.deferrals.find((d) => d.task === task && !d.resolvedAt);
572
+ if (!open) throw new Error(`No open deferral for task ${task}`);
573
+ open.resolvedAt = new Date().toISOString();
574
+ writeJson(deferralsPath(sessionDir), doc);
575
+ return doc;
576
+ }
577
+
578
+ /**
579
+ * @param {string} sessionDir
580
+ */
581
+ export function openDeferrals(sessionDir) {
582
+ return loadDeferrals(sessionDir).deferrals.filter((d) => !d.resolvedAt);
583
+ }
584
+
585
+ /**
586
+ * @param {Record<string, unknown> | null | undefined} session
587
+ */
588
+ export function sessionJobsSignalText(session) {
589
+ return [session?.paceSignal, session?.slug, session?.openspecChange]
590
+ .filter((v) => isNonEmptyString(v))
591
+ .join(' ');
592
+ }
593
+
594
+ /**
595
+ * Run the mechanical integrity checks for a session.
596
+ *
597
+ * Checks:
598
+ * 1. No unresolved deferrals.
599
+ * 2. spine.json **always required** (filled rows, or `notApplicable` with a
600
+ * reason). Keyword sniffing is not enough to decide; missing spine is how
601
+ * library-only platforms checkbox past gaps.
602
+ * 3. E2E acceptance — when a spine has real rows (not notApplicable):
603
+ * e2e.json must exist with filled steps (or its own notApplicable reason)
604
+ * and e2e-results.json must record a green, current run (steps hash must
605
+ * match). Prose in verify-evidence.md does not satisfy this; an explicit
606
+ * BLOCKED marker there still means the change cannot be done. Sync-only
607
+ * work should prefer spine `notApplicable` over inventing a fake loop.
608
+ *
609
+ * @param {{ cwd?: string, sessionDir: string, session: Record<string, unknown> }} opts
610
+ * @returns {{ ok: boolean, problems: string[], spineFile: string, spineExists: boolean, e2eFile: string | null }}
611
+ */
612
+ /**
613
+ * Project-level e2e off switch: `.forge/config.json` → `e2e.disabled` set to a
614
+ * non-empty reason string. Operator-set via `forge e2e disable "<reason>"`
615
+ * agents must never set it themselves. When set, the integrity gate stops
616
+ * demanding an executed green e2e run (the most time-consuming part of a
617
+ * session); spine, deferrals, evidence, and BLOCKED checks still apply.
618
+ *
619
+ * @param {string} [cwd]
620
+ * @returns {string | null} the reason, or null when e2e is enabled
621
+ */
622
+ export function e2eDisabledReason(cwd = process.cwd()) {
623
+ try {
624
+ const reason = loadProjectConfig(cwd)?.e2e?.disabled;
625
+ return isNonEmptyString(reason) ? reason : null;
626
+ } catch {
627
+ return null;
628
+ }
629
+ }
630
+
631
+ export function runIntegrityChecks(opts) {
632
+ /** @type {string[]} */
633
+ const problems = [];
634
+ const { sessionDir, session } = opts;
635
+ const cwd = opts.cwd ?? process.cwd();
636
+
637
+ const open = openDeferrals(sessionDir);
638
+ if (open.length > 0) {
639
+ problems.push(
640
+ `unresolved deferrals: ${open.map((d) => `${d.task} (${d.reason})`).join('; ')} — resolve via forge defer resolve --task <id>`,
641
+ );
642
+ }
643
+
644
+ const spineFile = spinePath({ cwd, session, sessionDir });
645
+ const spineExists = fs.existsSync(spineFile);
646
+
647
+ /** @type {ReturnType<typeof validateSpine> | null} */
648
+ let spineResult = null;
649
+ let spineHasRows = false;
650
+ if (!spineExists) {
651
+ problems.push(
652
+ `spine.json required at ${spineFile} — run forge spine init, then fill rows (or set notApplicable with a reason). Spine is mandatory for every change so capability→runtime wiring cannot be skipped by accident.`,
653
+ );
654
+ } else {
655
+ try {
656
+ const doc = readJson(spineFile);
657
+ spineResult = validateSpine(doc);
658
+ spineHasRows =
659
+ Array.isArray(doc?.rows) &&
660
+ doc.rows.length > 0 &&
661
+ !isNonEmptyString(doc?.notApplicable);
662
+ } catch (err) {
663
+ spineResult = {
664
+ ok: false,
665
+ problems: [`spine.json unreadable: ${err instanceof Error ? err.message : err}`],
666
+ };
667
+ }
668
+ if (!spineResult.ok) {
669
+ problems.push(...spineResult.problems.map((p) => `spine: ${p}`));
670
+ }
671
+ }
672
+
673
+ let e2eFile = null;
674
+ const e2eDisabled = e2eDisabledReason(cwd);
675
+ if (spineExists && spineHasRows) {
676
+ if (!e2eDisabled) {
677
+ e2eFile = e2ePath({ cwd, session, sessionDir });
678
+ problems.push(...checkE2eGate({ e2eFile, sessionDir }).problems);
679
+ }
680
+
681
+ const evidenceFile = path.join(sessionDir, 'verify-evidence.md');
682
+ if (fs.existsSync(evidenceFile) && /\bBLOCKED\b/.test(fs.readFileSync(evidenceFile, 'utf8'))) {
683
+ problems.push('verify-evidence.md contains BLOCKED — change cannot be marked done while E2E is blocked');
684
+ }
685
+ }
686
+
687
+ return { ok: problems.length === 0, problems, spineFile, spineExists, e2eFile, e2eDisabled };
688
+ }