pi-spine 2.14.1 → 2.16.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.
- package/package.json +2 -2
- package/skills/spine-release-operator/SKILL.md +60 -7
- package/skills/spine-release-operator/references/issue-intake-checklist.md +1 -1
- package/skills/spine-release-operator/references/post-integrate-regression-gate.md +1 -0
- package/src/batch/abort.mjs +82 -68
- package/src/batch/batch-id.mjs +103 -0
- package/src/batch/batch-state-lock.mjs +357 -0
- package/src/batch/contract-exec.mjs +12 -8
- package/src/batch/diagnosis.mjs +12 -7
- package/src/batch/engine-lanes/review-code.mjs +224 -0
- package/src/batch/engine-lanes/review-final.mjs +318 -0
- package/src/batch/engine-lanes/review-plan.mjs +315 -0
- package/src/batch/engine-lanes/review-poll.mjs +427 -0
- package/src/batch/engine-lanes/review-stub.mjs +146 -0
- package/src/batch/engine-lanes/review.mjs +4 -1532
- package/src/batch/engine.mjs +1 -1
- package/src/batch/journal.mjs +9 -32
- package/src/batch/lifecycle.mjs +106 -95
- package/src/batch/metrics.mjs +13 -26
- package/src/batch/reconcile-orphan.mjs +3 -2
- package/src/batch/salvage-batch-list.mjs +12 -1
- package/src/batch/state-guards.mjs +26 -3
- package/src/batch/state-io.mjs +101 -29
- package/src/batch/state.mjs +7 -13
- package/src/batch/worker-output.mjs +5 -16
- package/src/cli/handoff.mjs +10 -27
- package/src/cli/journal-follow.mjs +11 -2
- package/src/cli/lane-logs.mjs +9 -1
- package/src/config/cursor-rules/profile.mjs +5 -1
- package/src/config/defaults.mjs +10 -1
- package/src/config/settings-fields.mjs +14 -0
- package/src/planner/file-scope.mjs +109 -7
- package/src/process/liveness.mjs +188 -1
- package/src/tasks/analyze/index.mjs +14 -27
- package/src/tasks/packet/parse-prompt.mjs +138 -3
- package/src/util/secret-redact.mjs +140 -0
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Global inter-process lock for batch-state writers (SP-722 / GitHub #264).
|
|
4
|
+
*
|
|
5
|
+
* Serializes read-modify-write cycles on `.spine/batch-state.json`,
|
|
6
|
+
* `.spine/batch-history.json`, and abort-signal writes across the engine,
|
|
7
|
+
* supervisor, and CLI processes. Implemented as an exclusive lock file on
|
|
8
|
+
* `.spine/runtime/batch-state.lock` (temp write + `linkSync` publish) with
|
|
9
|
+
* PID-liveness stale breaking — the same primitive as the resume handoff
|
|
10
|
+
* lock, but scoped to state/history writes rather than resume ownership.
|
|
11
|
+
*
|
|
12
|
+
* Lock ordering (deadlock avoidance):
|
|
13
|
+
* 1. The per-batch resume handoff lock (`resume-handoff.lock`) MAY be held
|
|
14
|
+
* while acquiring this global lock (handoff → global is the only legal
|
|
15
|
+
* cross-lock order; see `attached-runner-reconcile.mjs`).
|
|
16
|
+
* 2. A holder of this global lock MUST NOT acquire the resume handoff lock.
|
|
17
|
+
* 3. Within this lock, batch-state writes happen before batch-history writes.
|
|
18
|
+
* 4. No nested acquisition from the same process: `withBatchStateLock` is
|
|
19
|
+
* re-entrant per process — a nested call runs `fn` directly instead of
|
|
20
|
+
* re-acquiring, so wrappers may freely compose (e.g. abort wraps a
|
|
21
|
+
* section that internally calls `appendBatchHistoryEntry`).
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import crypto from "node:crypto";
|
|
25
|
+
import fs from "node:fs";
|
|
26
|
+
import path from "node:path";
|
|
27
|
+
import {
|
|
28
|
+
ENGINE_STARTTIME_TOLERANCE_MS,
|
|
29
|
+
isProcessAlive,
|
|
30
|
+
probeProcessStartTimeMs,
|
|
31
|
+
} from "../process/liveness.mjs";
|
|
32
|
+
|
|
33
|
+
export const BATCH_STATE_LOCK_REL = path.join(".spine", "runtime", "batch-state.lock");
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Default bound on contention wait. Terminal lifecycle sections (complete /
|
|
37
|
+
* dismiss / abort) can hold the lock across worktree cleanup, so this is
|
|
38
|
+
* generous; normal state writes hold it for single-digit milliseconds.
|
|
39
|
+
*/
|
|
40
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
41
|
+
|
|
42
|
+
/** Poll interval while another process holds the lock. */
|
|
43
|
+
const POLL_INTERVAL_MS = 25;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Tolerance when comparing recorded process starttime to a live probe
|
|
47
|
+
* (PID-reuse detection). Reuse ENGINE_STARTTIME_TOLERANCE_MS so `ps lstart`
|
|
48
|
+
* second granularity cannot false-trigger.
|
|
49
|
+
*/
|
|
50
|
+
const STARTTIME_TOLERANCE_MS = ENGINE_STARTTIME_TOLERANCE_MS;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Re-entrancy + ownership tracking keyed by canonical lock path.
|
|
54
|
+
* Value is the ownership token written into the lock file for this hold.
|
|
55
|
+
* Stored on `globalThis` so dynamic `import()` of this module and the static
|
|
56
|
+
* import from `state-io.mjs` always share one Map — under
|
|
57
|
+
* `--experimental-test-coverage`, duplicate module instances otherwise split
|
|
58
|
+
* re-entrancy tracking and `leakedSelf` unlinks the live lock mid-section
|
|
59
|
+
* (release:check coverage flake — concurrent writers 99/100).
|
|
60
|
+
* @type {Map<string, string>}
|
|
61
|
+
*/
|
|
62
|
+
const HELD_BY_PROCESS_KEY = Symbol.for("pi-spine.batchStateLock.heldByThisProcess");
|
|
63
|
+
const heldByThisProcess = (() => {
|
|
64
|
+
const g = globalThis;
|
|
65
|
+
if (!(g[HELD_BY_PROCESS_KEY] instanceof Map)) {
|
|
66
|
+
g[HELD_BY_PROCESS_KEY] = new Map();
|
|
67
|
+
}
|
|
68
|
+
return /** @type {Map<string, string>} */ (g[HELD_BY_PROCESS_KEY]);
|
|
69
|
+
})();
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Resolve a stable absolute lock path. Create `.spine/runtime` first, then
|
|
73
|
+
* `realpath` that directory so `/var` vs `/private/var` (macOS) cannot split
|
|
74
|
+
* the re-entrancy Map across two keys for the same inode. Nested
|
|
75
|
+
* `withBatchStateLock` (e.g. saveSpineBatchState under an outer hold) must
|
|
76
|
+
* see the same key or `leakedSelf` will unlink the live lock mid-section.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} projectRoot
|
|
79
|
+
*/
|
|
80
|
+
export function batchStateLockPath(projectRoot) {
|
|
81
|
+
const runtimeDir = path.resolve(projectRoot, ".spine", "runtime");
|
|
82
|
+
fs.mkdirSync(runtimeDir, { recursive: true });
|
|
83
|
+
let canonicalRuntime = runtimeDir;
|
|
84
|
+
try {
|
|
85
|
+
canonicalRuntime = fs.realpathSync(runtimeDir);
|
|
86
|
+
} catch {
|
|
87
|
+
canonicalRuntime = runtimeDir;
|
|
88
|
+
}
|
|
89
|
+
return path.join(canonicalRuntime, path.basename(BATCH_STATE_LOCK_REL));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Synchronous sleep for lock polling. All batch-state writers are sync, so
|
|
94
|
+
* the wait must block the thread without yielding to the event loop.
|
|
95
|
+
*
|
|
96
|
+
* @param {number} ms
|
|
97
|
+
*/
|
|
98
|
+
function sleepSync(ms) {
|
|
99
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @param {string} lockPath
|
|
104
|
+
* @param {string} reason
|
|
105
|
+
*/
|
|
106
|
+
function logLockBreak(lockPath, reason) {
|
|
107
|
+
if (process.env.SPINE_LOCK_STEAL_LOG !== "1") return;
|
|
108
|
+
try {
|
|
109
|
+
fs.appendFileSync(
|
|
110
|
+
`${lockPath}.steals`,
|
|
111
|
+
`${Date.now()} pid=${process.pid} ${reason}\n`,
|
|
112
|
+
"utf-8",
|
|
113
|
+
);
|
|
114
|
+
} catch {
|
|
115
|
+
/* ignore diagnostic I/O */
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @returns {string}
|
|
121
|
+
*/
|
|
122
|
+
function newOwnershipToken() {
|
|
123
|
+
return crypto.randomBytes(16).toString("hex");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Single exclusive-create attempt.
|
|
128
|
+
*
|
|
129
|
+
* Publish via temp file + `linkSync` so waiters never observe an empty/partial
|
|
130
|
+
* lock from `wx` create-then-write (that race was parsed as "corrupt" and
|
|
131
|
+
* unlinked mid-hold — concurrent RMW lost updates).
|
|
132
|
+
*
|
|
133
|
+
* @param {string} lockPath
|
|
134
|
+
* @param {string} token
|
|
135
|
+
* @returns {boolean} true when the lock file was created by this call
|
|
136
|
+
*/
|
|
137
|
+
function tryCreateLockFile(lockPath, token) {
|
|
138
|
+
/** @type {number|null} */
|
|
139
|
+
let processStartedAt = null;
|
|
140
|
+
try {
|
|
141
|
+
processStartedAt = probeProcessStartTimeMs(process.pid);
|
|
142
|
+
} catch {
|
|
143
|
+
processStartedAt = null;
|
|
144
|
+
}
|
|
145
|
+
// Never fall back to Date.now() for startedAt: wall-clock acquire time is
|
|
146
|
+
// not process starttime. Under load, ESM import can lag spawn by >2s; a
|
|
147
|
+
// later probe of the real starttime would look like PID reuse and steal
|
|
148
|
+
// the live lock (release:check flake — concurrent writers lost updates).
|
|
149
|
+
const payload = JSON.stringify({
|
|
150
|
+
pid: process.pid,
|
|
151
|
+
startedAt: processStartedAt,
|
|
152
|
+
token,
|
|
153
|
+
});
|
|
154
|
+
const tmpPath = `${lockPath}.${process.pid}.${token}.tmp`;
|
|
155
|
+
try {
|
|
156
|
+
fs.writeFileSync(tmpPath, payload, { encoding: "utf-8" });
|
|
157
|
+
try {
|
|
158
|
+
fs.linkSync(tmpPath, lockPath);
|
|
159
|
+
return true;
|
|
160
|
+
} catch (err) {
|
|
161
|
+
if (/** @type {NodeJS.ErrnoException} */ (err).code !== "EEXIST") {
|
|
162
|
+
throw err;
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
} finally {
|
|
167
|
+
try {
|
|
168
|
+
fs.unlinkSync(tmpPath);
|
|
169
|
+
} catch {
|
|
170
|
+
/* tmp already removed or never created */
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Break the lock when the recorded holder cannot still own it: dead PID,
|
|
177
|
+
* abandoned corrupt payload, same-process leak (no active holder), or a live
|
|
178
|
+
* PID whose OS starttime is newer than the recorded start (PID recycled).
|
|
179
|
+
*
|
|
180
|
+
* Never steals from a live holder on age alone — suite-load CPU starvation
|
|
181
|
+
* can keep a critical section alive for minutes without making the lock stale
|
|
182
|
+
* (release-check flake under full-suite load).
|
|
183
|
+
*
|
|
184
|
+
* @param {string} lockPath
|
|
185
|
+
*/
|
|
186
|
+
function breakStaleLock(lockPath) {
|
|
187
|
+
/** @type {{ pid?: number, startedAt?: number, token?: string } | null} */
|
|
188
|
+
let holder = null;
|
|
189
|
+
let corrupt = false;
|
|
190
|
+
try {
|
|
191
|
+
holder = JSON.parse(fs.readFileSync(lockPath, "utf-8"));
|
|
192
|
+
if (!holder || typeof holder !== "object") corrupt = true;
|
|
193
|
+
} catch {
|
|
194
|
+
corrupt = true;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (!fs.existsSync(lockPath)) {
|
|
198
|
+
// Lock vanished between attempts — nothing to break.
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (corrupt) {
|
|
203
|
+
// Do not unlink on the first corrupt read: a concurrent `wx` writer can
|
|
204
|
+
// briefly expose an empty/partial file. Only clear leftovers that have
|
|
205
|
+
// been unreadable for a while (abandoned junk), otherwise waiters poll.
|
|
206
|
+
try {
|
|
207
|
+
const ageMs = Date.now() - fs.statSync(lockPath).mtimeMs;
|
|
208
|
+
if (ageMs < 2_000) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
} catch {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
logLockBreak(lockPath, "corrupt-stale");
|
|
216
|
+
fs.unlinkSync(lockPath);
|
|
217
|
+
} catch {
|
|
218
|
+
/* raced unlink */
|
|
219
|
+
}
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const holderPid = Number(holder?.pid);
|
|
224
|
+
if (!Number.isFinite(holderPid) || holderPid <= 0) {
|
|
225
|
+
try {
|
|
226
|
+
logLockBreak(lockPath, "invalid-pid");
|
|
227
|
+
fs.unlinkSync(lockPath);
|
|
228
|
+
} catch {
|
|
229
|
+
/* raced unlink */
|
|
230
|
+
}
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Same-process "leak" only when we are not in an active hold. Prefer the
|
|
235
|
+
// ownership token over path-string equality so `/var` vs `/private/var`
|
|
236
|
+
// aliases cannot falsely clear a live outer hold during nested acquire.
|
|
237
|
+
if (holderPid === process.pid) {
|
|
238
|
+
const token = String(holder?.token ?? "");
|
|
239
|
+
const stillHeldByToken =
|
|
240
|
+
token.length > 0 && [...heldByThisProcess.values()].includes(token);
|
|
241
|
+
const stillHeldByPath = heldByThisProcess.has(lockPath);
|
|
242
|
+
if (stillHeldByToken || stillHeldByPath) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
logLockBreak(lockPath, `leakedSelf token=${token} mapSize=${heldByThisProcess.size}`);
|
|
247
|
+
fs.unlinkSync(lockPath);
|
|
248
|
+
} catch {
|
|
249
|
+
/* raced unlink */
|
|
250
|
+
}
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Dead foreign PID: break immediately. Do NOT require a null `ps` probe —
|
|
255
|
+
// under suite load the PID is often recycled by an unrelated live process,
|
|
256
|
+
// and refusing to break left waiters stuck until timeout (stale-lock flake).
|
|
257
|
+
if (holderPid !== process.pid && !isProcessAlive(holderPid)) {
|
|
258
|
+
try {
|
|
259
|
+
logLockBreak(lockPath, `deadPid holder=${holderPid}`);
|
|
260
|
+
fs.unlinkSync(lockPath);
|
|
261
|
+
} catch {
|
|
262
|
+
/* raced unlink */
|
|
263
|
+
}
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Live foreign PID: steal only when OS starttime proves the PID was
|
|
268
|
+
// recycled. Require liveStart *newer* than recorded start — wall-clock
|
|
269
|
+
// startedAt (legacy) is newer than real process start and must not steal.
|
|
270
|
+
// startedAt null (probe failed at acquire) → never steal from a live PID.
|
|
271
|
+
if (holderPid === process.pid) return;
|
|
272
|
+
if (isProcessAlive(holderPid)) {
|
|
273
|
+
const expectedStart = Number(holder?.startedAt);
|
|
274
|
+
if (!Number.isFinite(expectedStart) || expectedStart <= 0) return;
|
|
275
|
+
/** @type {number|null} */
|
|
276
|
+
let liveStart = null;
|
|
277
|
+
try {
|
|
278
|
+
liveStart = probeProcessStartTimeMs(holderPid);
|
|
279
|
+
} catch {
|
|
280
|
+
liveStart = null;
|
|
281
|
+
}
|
|
282
|
+
if (liveStart == null || !Number.isFinite(liveStart) || liveStart <= 0) return;
|
|
283
|
+
if (liveStart - expectedStart > STARTTIME_TOLERANCE_MS) {
|
|
284
|
+
try {
|
|
285
|
+
logLockBreak(lockPath, `pidRecycled holder=${holderPid}`);
|
|
286
|
+
fs.unlinkSync(lockPath);
|
|
287
|
+
} catch {
|
|
288
|
+
/* raced unlink */
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Release the lock file only when our ownership token still matches.
|
|
296
|
+
*
|
|
297
|
+
* @param {string} lockPath
|
|
298
|
+
* @param {string} token
|
|
299
|
+
*/
|
|
300
|
+
function releaseLockFile(lockPath, token) {
|
|
301
|
+
try {
|
|
302
|
+
const holder = JSON.parse(fs.readFileSync(lockPath, "utf-8"));
|
|
303
|
+
if (String(holder?.token ?? "") === token) {
|
|
304
|
+
fs.unlinkSync(lockPath);
|
|
305
|
+
}
|
|
306
|
+
} catch {
|
|
307
|
+
// Release races (lock already broken/replaced) are safe to ignore.
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Run `fn` while holding the global batch-state lock.
|
|
313
|
+
*
|
|
314
|
+
* @param {string} projectRoot
|
|
315
|
+
* @param {() => unknown} fn
|
|
316
|
+
* @param {{ timeoutMs?: number }} [options]
|
|
317
|
+
* @returns {unknown} `fn`'s return value
|
|
318
|
+
*/
|
|
319
|
+
export function withBatchStateLock(projectRoot, fn, options = {}) {
|
|
320
|
+
// batchStateLockPath mkdir+realpath so nested calls share one Map key.
|
|
321
|
+
const lockPath = batchStateLockPath(projectRoot);
|
|
322
|
+
|
|
323
|
+
// Re-entrant pass-through: same-process nesting must not self-deadlock.
|
|
324
|
+
if (heldByThisProcess.has(lockPath)) {
|
|
325
|
+
return fn();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const timeoutMs = Number(options.timeoutMs) > 0 ? Number(options.timeoutMs) : DEFAULT_TIMEOUT_MS;
|
|
329
|
+
const deadline = Date.now() + timeoutMs;
|
|
330
|
+
const token = newOwnershipToken();
|
|
331
|
+
|
|
332
|
+
for (;;) {
|
|
333
|
+
if (tryCreateLockFile(lockPath, token)) break;
|
|
334
|
+
breakStaleLock(lockPath);
|
|
335
|
+
if (Date.now() >= deadline) {
|
|
336
|
+
throw new Error(
|
|
337
|
+
`Timed out acquiring batch-state lock (${path.relative(projectRoot, lockPath)}) ` +
|
|
338
|
+
`after ${timeoutMs}ms — another spine process may be holding it`,
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
sleepSync(POLL_INTERVAL_MS);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
heldByThisProcess.set(lockPath, token);
|
|
345
|
+
try {
|
|
346
|
+
return fn();
|
|
347
|
+
} finally {
|
|
348
|
+
// Unlink (token-gated) before clearing the re-entrancy map so a
|
|
349
|
+
// same-process contender cannot treat this file as leakedSelf and
|
|
350
|
+
// steal it while we still intend to release.
|
|
351
|
+
try {
|
|
352
|
+
releaseLockFile(lockPath, token);
|
|
353
|
+
} finally {
|
|
354
|
+
heldByThisProcess.delete(lockPath);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
@@ -26,6 +26,12 @@ import {
|
|
|
26
26
|
NPM_TEST_DASH_DASH_RE,
|
|
27
27
|
TEST_COMMAND_NPM_TEST_DASH_DASH_FIX_HINT,
|
|
28
28
|
} from "../tasks/validate-contract-warn.mjs";
|
|
29
|
+
import { formatRefusedContractMetacharMessage, isRefusedContractMetacharCommand } from "../tasks/packet/parse-prompt.mjs";
|
|
30
|
+
|
|
31
|
+
// Shared pre-spawn refusal envelope for the npm-scope (#187) and metachar (#268) guards.
|
|
32
|
+
function refusedBeforeSpawnResult(summary) {
|
|
33
|
+
return { ok: false, exitCode: 1, output: summary, summary, refusedBeforeSpawn: true };
|
|
34
|
+
}
|
|
29
35
|
|
|
30
36
|
/** Default stdout/stderr capture limit for contract testCommand (issue #86). */
|
|
31
37
|
export const CONTRACT_TEST_COMMAND_MAX_BUFFER = 10 * 1024 * 1024;
|
|
@@ -141,6 +147,8 @@ function formatRefusedNpmTestDashDashMessage(command) {
|
|
|
141
147
|
return `Contract testCommand refused before spawn: npm test -- <path> is blocked at runtime (command: ${command}). ${TEST_COMMAND_NPM_TEST_DASH_DASH_FIX_HINT}`;
|
|
142
148
|
}
|
|
143
149
|
|
|
150
|
+
/**
|
|
151
|
+
|
|
144
152
|
/**
|
|
145
153
|
* @param {string} worktreePath
|
|
146
154
|
* @param {string} command
|
|
@@ -153,14 +161,10 @@ export function runContractTestCommand(worktreePath, command, options = {}) {
|
|
|
153
161
|
}
|
|
154
162
|
|
|
155
163
|
if (isRefusedNpmTestDashDashCommand(trimmed)) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
output: summary,
|
|
161
|
-
summary,
|
|
162
|
-
refusedBeforeSpawn: true,
|
|
163
|
-
};
|
|
164
|
+
return refusedBeforeSpawnResult(formatRefusedNpmTestDashDashMessage(trimmed));
|
|
165
|
+
}
|
|
166
|
+
if (isRefusedContractMetacharCommand(trimmed)) {
|
|
167
|
+
return refusedBeforeSpawnResult(formatRefusedContractMetacharMessage(trimmed));
|
|
164
168
|
}
|
|
165
169
|
|
|
166
170
|
const maxBuffer = options.maxBuffer ?? CONTRACT_TEST_COMMAND_MAX_BUFFER;
|
package/src/batch/diagnosis.mjs
CHANGED
|
@@ -337,14 +337,19 @@ export function buildHeadline(diagnosis, ctx = {}) {
|
|
|
337
337
|
return `${batchLabel} failed at worker launch — repair lane worktree git, then retry`;
|
|
338
338
|
}
|
|
339
339
|
if (REVIEW_SPAWN_FAILURE_EXIT_REASONS.has(ctx.exitReason ?? "")) {
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
340
|
+
const isFinal = ctx.exitReason.startsWith("final_review");
|
|
341
|
+
const isTimeout = ctx.exitReason.endsWith("_timeout");
|
|
342
|
+
const reviewKind = isFinal ? "final review" : "code review";
|
|
343
|
+
|
|
344
|
+
if (isTimeout) {
|
|
345
|
+
return ctx.failedTaskId
|
|
346
|
+
? `${batchLabel} ${reviewKind} timed out for task ${ctx.failedTaskId} — retry or increase SPINE_REVIEW_TIMEOUT_MS`
|
|
347
|
+
: `${batchLabel} reviewer spawn timed out — retry`;
|
|
348
|
+
}
|
|
349
|
+
|
|
345
350
|
return ctx.failedTaskId
|
|
346
|
-
? `${batchLabel} ${reviewKind}
|
|
347
|
-
: `${batchLabel} reviewer spawn
|
|
351
|
+
? `${batchLabel} ${reviewKind} spawn failed for task ${ctx.failedTaskId} — retry`
|
|
352
|
+
: `${batchLabel} reviewer spawn failed — retry`;
|
|
348
353
|
}
|
|
349
354
|
if (ctx.salvageChangedFileCount > 0 && ctx.failedTaskId) {
|
|
350
355
|
return `${batchLabel} failed (${ctx.failedTaskId}): ${ctx.salvageChangedFileCount} uncommitted file(s) in scope`;
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Engine lane code review phase (SP-729 / #262).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { recordLaneTaskMetric } from "./queue.mjs";
|
|
7
|
+
import { REVIEW_DEFAULTS } from "../../config/defaults.mjs";
|
|
8
|
+
import { appendJournalEvent, readJournalEvents } from "../journal.mjs";
|
|
9
|
+
import {
|
|
10
|
+
recomputeTaskCounters,
|
|
11
|
+
saveSpineBatchState,
|
|
12
|
+
updateSegmentForTask,
|
|
13
|
+
} from "../state.mjs";
|
|
14
|
+
import {
|
|
15
|
+
buildReviewArtifactPath,
|
|
16
|
+
shouldRunCodeReview,
|
|
17
|
+
} from "../review-shared.mjs";
|
|
18
|
+
import {
|
|
19
|
+
findCodeReviewStepNumber,
|
|
20
|
+
findCompletedCodeReview,
|
|
21
|
+
readReviewLevel,
|
|
22
|
+
runStepReview,
|
|
23
|
+
} from "../review.mjs";
|
|
24
|
+
import { honorCompletedReview, runReviewPollLoop } from "./review-poll.mjs";
|
|
25
|
+
import {
|
|
26
|
+
createPhaseStubVerdictQueue,
|
|
27
|
+
shouldUseReviewStub,
|
|
28
|
+
writeStubReviewArtifact,
|
|
29
|
+
} from "./review-stub.mjs";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {object} params
|
|
33
|
+
*/
|
|
34
|
+
export async function runEngineCodeReview({
|
|
35
|
+
taskFolder,
|
|
36
|
+
worktreePath,
|
|
37
|
+
config = {},
|
|
38
|
+
attempt = 1,
|
|
39
|
+
journal,
|
|
40
|
+
stubVerdicts = null,
|
|
41
|
+
}) {
|
|
42
|
+
const reviewLevel = readReviewLevel(taskFolder);
|
|
43
|
+
if (!shouldRunCodeReview({ reviewLevel })) {
|
|
44
|
+
return {
|
|
45
|
+
ok: true,
|
|
46
|
+
skipped: true,
|
|
47
|
+
reviewLevel,
|
|
48
|
+
verdict: null,
|
|
49
|
+
feedback: "",
|
|
50
|
+
artifactPath: "",
|
|
51
|
+
spawnFailed: false,
|
|
52
|
+
exitCode: 0,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const stepNumber = findCodeReviewStepNumber(taskFolder);
|
|
57
|
+
const artifactPath = buildReviewArtifactPath(taskFolder, stepNumber);
|
|
58
|
+
const useStub = shouldUseReviewStub();
|
|
59
|
+
|
|
60
|
+
if (useStub) {
|
|
61
|
+
const queue = stubVerdicts ?? createPhaseStubVerdictQueue("code");
|
|
62
|
+
const verdict = queue.next();
|
|
63
|
+
const feedback =
|
|
64
|
+
verdict === "REVISE" ? "Stub reviewer requested changes." : "Stub reviewer approved.";
|
|
65
|
+
if (journal?.projectRoot && journal?.batchId) {
|
|
66
|
+
appendJournalEvent(journal.projectRoot, journal.batchId, "review.started", {
|
|
67
|
+
taskId: journal.taskId,
|
|
68
|
+
laneNumber: journal.laneNumber,
|
|
69
|
+
correlationId: journal.correlationId,
|
|
70
|
+
stepNumber,
|
|
71
|
+
reviewType: "code",
|
|
72
|
+
reviewLevel,
|
|
73
|
+
artifactPath,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
writeStubReviewArtifact({ artifactPath, title: "Code Review", verdict, feedback });
|
|
77
|
+
if (journal?.projectRoot && journal?.batchId) {
|
|
78
|
+
appendJournalEvent(journal.projectRoot, journal.batchId, "review.completed", {
|
|
79
|
+
taskId: journal.taskId,
|
|
80
|
+
laneNumber: journal.laneNumber,
|
|
81
|
+
correlationId: journal.correlationId,
|
|
82
|
+
stepNumber,
|
|
83
|
+
reviewType: "code",
|
|
84
|
+
reviewLevel,
|
|
85
|
+
verdict,
|
|
86
|
+
artifactPath,
|
|
87
|
+
stub: true,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
ok: verdict === "APPROVE",
|
|
92
|
+
skipped: false,
|
|
93
|
+
reviewLevel,
|
|
94
|
+
verdict,
|
|
95
|
+
feedback,
|
|
96
|
+
artifactPath,
|
|
97
|
+
spawnFailed: false,
|
|
98
|
+
exitCode: verdict === "APPROVE" ? 0 : 2,
|
|
99
|
+
attempt,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return await runStepReview({
|
|
104
|
+
taskFolder,
|
|
105
|
+
worktreePath,
|
|
106
|
+
stepNumber,
|
|
107
|
+
reviewType: "code",
|
|
108
|
+
config,
|
|
109
|
+
journal,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @param {object} params
|
|
115
|
+
*/
|
|
116
|
+
function recordCodeReviewTaskFailure({
|
|
117
|
+
projectRoot,
|
|
118
|
+
state,
|
|
119
|
+
batchId,
|
|
120
|
+
task,
|
|
121
|
+
lane,
|
|
122
|
+
laneCorrelationId,
|
|
123
|
+
exitReason,
|
|
124
|
+
verdict,
|
|
125
|
+
codeReviewAttempt,
|
|
126
|
+
config,
|
|
127
|
+
taskFolder,
|
|
128
|
+
}) {
|
|
129
|
+
const taskId = task.taskId;
|
|
130
|
+
const laneNumber = lane.laneNumber;
|
|
131
|
+
task.status = "failed";
|
|
132
|
+
task.endedAt = Date.now();
|
|
133
|
+
task.exitReason = exitReason;
|
|
134
|
+
updateSegmentForTask(state, taskId, "failed");
|
|
135
|
+
recomputeTaskCounters(state);
|
|
136
|
+
saveSpineBatchState(projectRoot, state);
|
|
137
|
+
appendJournalEvent(projectRoot, batchId, "task.failed", {
|
|
138
|
+
taskId,
|
|
139
|
+
laneNumber,
|
|
140
|
+
laneId: lane.laneId,
|
|
141
|
+
correlationId: laneCorrelationId,
|
|
142
|
+
classification: exitReason,
|
|
143
|
+
exitCode: 1,
|
|
144
|
+
codeVerdict: verdict ?? null,
|
|
145
|
+
codeReviewAttempt,
|
|
146
|
+
});
|
|
147
|
+
recordLaneTaskMetric({
|
|
148
|
+
projectRoot,
|
|
149
|
+
batchId,
|
|
150
|
+
task,
|
|
151
|
+
config,
|
|
152
|
+
taskFolder,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {object} params
|
|
158
|
+
*/
|
|
159
|
+
export async function runCodeReviewPhase({
|
|
160
|
+
projectRoot,
|
|
161
|
+
state,
|
|
162
|
+
batchId,
|
|
163
|
+
config,
|
|
164
|
+
task,
|
|
165
|
+
lane,
|
|
166
|
+
taskFolderInWorktree,
|
|
167
|
+
wt,
|
|
168
|
+
taskBranch,
|
|
169
|
+
laneCorrelationId,
|
|
170
|
+
fileScopePaths,
|
|
171
|
+
}) {
|
|
172
|
+
const reviewLevel = readReviewLevel(taskFolderInWorktree);
|
|
173
|
+
if (!shouldRunCodeReview({ reviewLevel })) {
|
|
174
|
+
return { ok: true, skipped: true };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const maxCodeReviewAttempts =
|
|
178
|
+
config?.review?.maxCodeReviewAttempts ??
|
|
179
|
+
config?.review?.maxFinalAttempts ??
|
|
180
|
+
REVIEW_DEFAULTS.maxFinalAttempts;
|
|
181
|
+
|
|
182
|
+
const journalEvents = readJournalEvents(projectRoot, batchId);
|
|
183
|
+
const honoredResult = honorCompletedReview({
|
|
184
|
+
reviewType: "code",
|
|
185
|
+
passVerdict: "APPROVE",
|
|
186
|
+
attemptField: "codeReviewAttempts",
|
|
187
|
+
attemptKey: "codeReviewAttempt",
|
|
188
|
+
findCompletedReview: findCompletedCodeReview,
|
|
189
|
+
projectRoot,
|
|
190
|
+
state,
|
|
191
|
+
batchId,
|
|
192
|
+
task,
|
|
193
|
+
lane,
|
|
194
|
+
laneCorrelationId,
|
|
195
|
+
taskFolder: taskFolderInWorktree,
|
|
196
|
+
journalEvents,
|
|
197
|
+
});
|
|
198
|
+
if (honoredResult) return honoredResult;
|
|
199
|
+
|
|
200
|
+
const stubVerdicts = shouldUseReviewStub() ? createPhaseStubVerdictQueue("code") : null;
|
|
201
|
+
|
|
202
|
+
return await runReviewPollLoop({
|
|
203
|
+
reviewType: "code",
|
|
204
|
+
passVerdict: "APPROVE",
|
|
205
|
+
attemptField: "codeReviewAttempts",
|
|
206
|
+
attemptKey: "codeReviewAttempt",
|
|
207
|
+
maxAttempts: maxCodeReviewAttempts,
|
|
208
|
+
runEngineReview: (params) => runEngineCodeReview({ ...params, stubVerdicts }),
|
|
209
|
+
recordReviewTaskFailure: recordCodeReviewTaskFailure,
|
|
210
|
+
invalidVerdictOutput: "code review artifact missing APPROVE or REVISE verdict",
|
|
211
|
+
journalEvents,
|
|
212
|
+
projectRoot,
|
|
213
|
+
state,
|
|
214
|
+
batchId,
|
|
215
|
+
config,
|
|
216
|
+
task,
|
|
217
|
+
lane,
|
|
218
|
+
taskFolderInWorktree,
|
|
219
|
+
wt,
|
|
220
|
+
taskBranch,
|
|
221
|
+
laneCorrelationId,
|
|
222
|
+
fileScopePaths,
|
|
223
|
+
});
|
|
224
|
+
}
|