pi-spine 2.2.0 → 2.3.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/bin/spine-cli/verify.mjs +2 -19
- package/package.json +1 -1
- package/src/batch/attached-runner-promote.mjs +370 -0
- package/src/batch/attached-runner-reconcile.mjs +291 -0
- package/src/batch/attached-runner.mjs +24 -645
- package/src/batch/batch-guards.mjs +69 -0
- package/src/batch/contract-exec.mjs +483 -0
- package/src/batch/contract-parse.mjs +243 -0
- package/src/batch/contract-verify.mjs +32 -707
- package/src/batch/detached-diagnostics.mjs +255 -0
- package/src/batch/detached-run.mjs +326 -0
- package/src/batch/detached-start.mjs +18 -894
- package/src/batch/detached-wait.mjs +346 -0
- package/src/batch/engine.mjs +8 -67
- package/src/batch/integrate-git.mjs +21 -0
- package/src/batch/integrate.mjs +5 -15
- package/src/batch/journal-rebuild-structural.mjs +352 -0
- package/src/batch/journal-rebuild.mjs +16 -348
- package/src/batch/lane-dirty-check-commit.mjs +361 -0
- package/src/batch/lane-dirty-check-git.mjs +298 -0
- package/src/batch/lane-dirty-check.mjs +31 -297
- package/src/batch/reconcile-batch.mjs +483 -0
- package/src/batch/reconcile-classify.mjs +433 -0
- package/src/batch/reconcile-diagnosis.mjs +459 -0
- package/src/batch/reconcile-light-cache.mjs +61 -0
- package/src/batch/reconcile-orphan.mjs +198 -0
- package/src/batch/reconcile.mjs +19 -1693
- package/src/batch/resume-multi-lanes.mjs +6 -148
- package/src/batch/resume-multi-queue.mjs +159 -0
- package/src/batch/resume-single-validate.mjs +25 -0
- package/src/batch/resume.mjs +3 -20
- package/src/batch/review-artifacts.mjs +419 -0
- package/src/batch/review-step-run.mjs +383 -0
- package/src/batch/review-step.mjs +451 -0
- package/src/batch/review.mjs +32 -1199
- package/src/batch/salvage-batch-list.mjs +291 -0
- package/src/batch/salvage-batch.mjs +10 -284
- package/src/batch/sequence-plan.mjs +234 -0
- package/src/batch/sequence-run.mjs +418 -0
- package/src/batch/sequence-wait.mjs +167 -0
- package/src/batch/sequence.mjs +22 -783
- package/src/batch/state-guards.mjs +284 -0
- package/src/batch/state-io.mjs +135 -0
- package/src/batch/state.mjs +27 -396
- package/src/batch/worker-heartbeat.mjs +375 -0
- package/src/batch/worker-host.mjs +50 -541
- package/src/batch/worker-spawn.mjs +315 -0
|
@@ -1,714 +1,39 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
2
|
/**
|
|
3
3
|
* Machine contract verification in lane worktree (handoff §4.5, SP-154/SP-155).
|
|
4
|
+
* Re-export shim — parse in contract-parse.mjs, exec in contract-exec.mjs (SP-585/SP-603).
|
|
4
5
|
*/
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import { parseAggregateLineCoverage } from "../../scripts/coverage-parse.mjs";
|
|
11
|
-
import { resolveContractMode } from "../tasks/packet/validate-contract.mjs";
|
|
12
|
-
import {
|
|
13
|
-
isBaseFileScopeSatisfied,
|
|
14
|
-
isBaseScopeSatisfied,
|
|
15
|
-
isPrelandedFileScopeSatisfied,
|
|
16
|
-
isResumeBaselineFileScopeSatisfied,
|
|
17
|
-
isStubPrelandedFileScopeSatisfied,
|
|
7
|
+
export {
|
|
8
|
+
resolvePromptRelPath,
|
|
9
|
+
isFileScopePatternPrelanded,
|
|
10
|
+
hasSpineTaskDeliveryChanges,
|
|
18
11
|
} from "./contract-prelanded.mjs";
|
|
19
|
-
import { appendJournalEvent } from "./journal.mjs";
|
|
20
|
-
import {
|
|
21
|
-
sanitizeFlutterBuildBeforeAnalyze,
|
|
22
|
-
shouldCleanFlutterBuildBeforeAnalyze,
|
|
23
|
-
} from "./lane-dirty-check.mjs";
|
|
24
|
-
import {
|
|
25
|
-
NPM_TEST_DASH_DASH_RE,
|
|
26
|
-
TEST_COMMAND_NPM_TEST_DASH_DASH_FIX_HINT,
|
|
27
|
-
} from "../tasks/validate-contract-warn.mjs";
|
|
28
12
|
|
|
29
|
-
export {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
export
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const env = { ...sourceEnv };
|
|
57
|
-
for (const key of CONTRACT_TEST_WORKER_ENV_KEYS) {
|
|
58
|
-
delete env[key];
|
|
59
|
-
}
|
|
60
|
-
if (env.SPINE_BATCH_ID && !env.SPINE_PARENT_BATCH_ID) {
|
|
61
|
-
env.SPINE_PARENT_BATCH_ID = env.SPINE_BATCH_ID;
|
|
62
|
-
}
|
|
63
|
-
delete env.SPINE_BATCH_ID;
|
|
64
|
-
return env;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @param {string} taskId
|
|
69
|
-
* @param {ReturnType<import("../tasks/packet/parse-prompt.mjs").parseContract>} parsedContract
|
|
70
|
-
* @param {object} [config]
|
|
71
|
-
*/
|
|
72
|
-
export function shouldRunContractVerify(taskId, parsedContract, config = {}) {
|
|
73
|
-
const mode = resolveContractMode({
|
|
74
|
-
mode: config.contract?.mode,
|
|
75
|
-
taskId,
|
|
76
|
-
legacyTaskIdPrefixes: config.contract?.legacyTaskIdPrefixes,
|
|
77
|
-
});
|
|
78
|
-
if (mode === "legacy") return false;
|
|
79
|
-
if (!parsedContract?.hasSection) return false;
|
|
80
|
-
|
|
81
|
-
return Boolean(
|
|
82
|
-
parsedContract.testCommand ||
|
|
83
|
-
(parsedContract.fileScopeMustChange?.length ?? 0) > 0 ||
|
|
84
|
-
(parsedContract.fileScopeMustNotChange?.length ?? 0) > 0 ||
|
|
85
|
-
parsedContract.minLineCoverage != null ||
|
|
86
|
-
(parsedContract.artifactsMustExist?.length ?? 0) > 0,
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Exit-verification tasks (§8 phase exit) must not stub-complete without contract pass.
|
|
92
|
-
*
|
|
93
|
-
* @param {string} promptMarkdown
|
|
94
|
-
*/
|
|
95
|
-
export function isExitVerificationTask(promptMarkdown) {
|
|
96
|
-
const text = String(promptMarkdown ?? "");
|
|
97
|
-
if (/^#\s*Task:.*exit verification/im.test(text)) {
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
if (/^##\s*Exit verification/im.test(text)) {
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
if (/Phase\s+\d+\s+exit verification/i.test(text)) {
|
|
104
|
-
return true;
|
|
105
|
-
}
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* @param {string} promptMarkdown
|
|
111
|
-
* @param {ReturnType<import("../tasks/packet/parse-prompt.mjs").parseContract>} parsedContract
|
|
112
|
-
* @param {object} [config]
|
|
113
|
-
*/
|
|
114
|
-
export function shouldRunContractVerifyForWorker(promptMarkdown, parsedContract, config = {}) {
|
|
115
|
-
if (!shouldRunContractVerify(null, parsedContract, config)) {
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
const stubMode =
|
|
119
|
-
process.env.SPINE_WORKER_STUB === "1" || process.env.SPINE_WORKER_STUB === "true";
|
|
120
|
-
if (!stubMode) {
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
return isExitVerificationTask(promptMarkdown);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* @returns {boolean}
|
|
128
|
-
*/
|
|
129
|
-
export function isStubWorkerMode() {
|
|
130
|
-
return process.env.SPINE_WORKER_STUB === "1" || process.env.SPINE_WORKER_STUB === "true";
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Legacy stub workers write "Task: stub" in `.DONE` body.
|
|
135
|
-
*
|
|
136
|
-
* @param {string} doneContent
|
|
137
|
-
*/
|
|
138
|
-
export function isLegacyStubDoneMarker(doneContent) {
|
|
139
|
-
return /Task:\s*stub/i.test(String(doneContent ?? ""));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* @param {string} donePath
|
|
144
|
-
*/
|
|
145
|
-
export function isLegacyStubDoneFile(donePath) {
|
|
146
|
-
if (!fs.existsSync(donePath)) {
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
149
|
-
return isLegacyStubDoneMarker(fs.readFileSync(donePath, "utf-8"));
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Lane commit enforces `fileScopeMustChange` when stub mode is active or `.DONE` is legacy stub.
|
|
154
|
-
*
|
|
155
|
-
* @param {string} donePath
|
|
156
|
-
*/
|
|
157
|
-
export function shouldEnforceStubContractAtLaneCommit(donePath) {
|
|
158
|
-
if (isStubWorkerMode()) {
|
|
159
|
-
return true;
|
|
160
|
-
}
|
|
161
|
-
return isLegacyStubDoneFile(donePath);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* @param {ReturnType<import("../tasks/packet/parse-prompt.mjs").parseContract>} parsedContract
|
|
166
|
-
*/
|
|
167
|
-
export function hasReleaseCriticalContract(parsedContract) {
|
|
168
|
-
return (parsedContract?.fileScopeMustChange?.length ?? 0) > 0;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* @param {string} worktreePath
|
|
173
|
-
*/
|
|
174
|
-
function listUntrackedFiles(worktreePath) {
|
|
175
|
-
try {
|
|
176
|
-
const output = execFileSync("git", ["ls-files", "-o", "--exclude-standard"], {
|
|
177
|
-
cwd: worktreePath,
|
|
178
|
-
encoding: "utf-8",
|
|
179
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
180
|
-
timeout: 30_000,
|
|
181
|
-
});
|
|
182
|
-
return output
|
|
183
|
-
.split(/\r?\n/)
|
|
184
|
-
.map((line) => line.trim())
|
|
185
|
-
.filter(Boolean);
|
|
186
|
-
} catch {
|
|
187
|
-
return [];
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* @param {string} worktreePath
|
|
193
|
-
* @param {string} baseBranch
|
|
194
|
-
* @param {string[]} [pendingPaths]
|
|
195
|
-
*/
|
|
196
|
-
export function listEffectiveChangedFiles(worktreePath, baseBranch = "main", pendingPaths = []) {
|
|
197
|
-
return [
|
|
198
|
-
...new Set([
|
|
199
|
-
...listChangedFiles(worktreePath, baseBranch),
|
|
200
|
-
...pendingPaths,
|
|
201
|
-
...listUntrackedFiles(worktreePath),
|
|
202
|
-
]),
|
|
203
|
-
];
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* @param {string} worktreePath
|
|
208
|
-
* @param {ReturnType<import("../tasks/packet/parse-prompt.mjs").parseContract>} parsedContract
|
|
209
|
-
* @param {string} [baseBranch]
|
|
210
|
-
* @param {string[]} [pendingPaths] Uncommitted paths about to be lane-committed
|
|
211
|
-
* @param {object} [config]
|
|
212
|
-
* @returns {{ ok: boolean, failures: string[] }}
|
|
213
|
-
*/
|
|
214
|
-
export function verifyStubFileScopeMustChange(
|
|
215
|
-
worktreePath,
|
|
216
|
-
parsedContract,
|
|
217
|
-
baseBranch = "main",
|
|
218
|
-
pendingPaths = [],
|
|
219
|
-
config = {},
|
|
220
|
-
) {
|
|
221
|
-
const patterns = parsedContract?.fileScopeMustChange ?? [];
|
|
222
|
-
if (patterns.length === 0) {
|
|
223
|
-
return { ok: true, failures: [] };
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const changedFiles = listEffectiveChangedFiles(worktreePath, baseBranch, pendingPaths);
|
|
227
|
-
/** @type {string[]} */
|
|
228
|
-
const failures = [];
|
|
229
|
-
for (const pattern of patterns) {
|
|
230
|
-
const matched = changedFiles.some((file) =>
|
|
231
|
-
pattern.endsWith("/") ? file.startsWith(pattern) : matchesContractPattern(file, pattern),
|
|
232
|
-
);
|
|
233
|
-
if (matched) {
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
if (isStubPrelandedFileScopeSatisfied(worktreePath, pattern, changedFiles, config, baseBranch)) {
|
|
237
|
-
continue;
|
|
238
|
-
}
|
|
239
|
-
if (isBaseScopeSatisfied(worktreePath, pattern, changedFiles, baseBranch, config)) {
|
|
240
|
-
continue;
|
|
241
|
-
}
|
|
242
|
-
failures.push(`Contract fileScopeMustChange: no matching changes for ${pattern}`);
|
|
243
|
-
}
|
|
244
|
-
return { ok: failures.length === 0, failures };
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* @param {string} worktreePath
|
|
249
|
-
* @param {string} [baseBranch]
|
|
250
|
-
* @param {string} [sinceCommit] When set, diff `sinceCommit..HEAD` instead of `baseBranch...HEAD`.
|
|
251
|
-
*/
|
|
252
|
-
export function listChangedFiles(worktreePath, baseBranch = "main", sinceCommit = undefined) {
|
|
253
|
-
const scopedSince = String(sinceCommit ?? "").trim();
|
|
254
|
-
const diffRange =
|
|
255
|
-
scopedSince.length > 0 ? `${scopedSince}..HEAD` : `${baseBranch}...HEAD`;
|
|
256
|
-
|
|
257
|
-
try {
|
|
258
|
-
const output = execFileSync("git", ["diff", "--name-only", diffRange], {
|
|
259
|
-
cwd: worktreePath,
|
|
260
|
-
encoding: "utf-8",
|
|
261
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
262
|
-
timeout: 30_000,
|
|
263
|
-
});
|
|
264
|
-
return output
|
|
265
|
-
.split(/\r?\n/)
|
|
266
|
-
.map((line) => line.trim())
|
|
267
|
-
.filter(Boolean);
|
|
268
|
-
} catch {
|
|
269
|
-
if (scopedSince.length > 0) {
|
|
270
|
-
return [];
|
|
271
|
-
}
|
|
272
|
-
try {
|
|
273
|
-
const output = execFileSync("git", ["diff", "--name-only", "HEAD"], {
|
|
274
|
-
cwd: worktreePath,
|
|
275
|
-
encoding: "utf-8",
|
|
276
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
277
|
-
timeout: 30_000,
|
|
278
|
-
});
|
|
279
|
-
return output
|
|
280
|
-
.split(/\r?\n/)
|
|
281
|
-
.map((line) => line.trim())
|
|
282
|
-
.filter(Boolean);
|
|
283
|
-
} catch {
|
|
284
|
-
return [];
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* @param {string} file
|
|
291
|
-
* @param {string} pattern
|
|
292
|
-
*/
|
|
293
|
-
export function matchesContractPattern(file, pattern) {
|
|
294
|
-
return micromatch.isMatch(file, pattern, { dot: true });
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* @param {number} byteCount
|
|
299
|
-
*/
|
|
300
|
-
function formatMaxBufferLabel(byteCount) {
|
|
301
|
-
const mib = byteCount / (1024 * 1024);
|
|
302
|
-
if (mib >= 1 && Number.isInteger(mib)) {
|
|
303
|
-
return `${mib}MB`;
|
|
304
|
-
}
|
|
305
|
-
if (byteCount >= 1024) {
|
|
306
|
-
return `${Math.round(byteCount / 1024)}KB`;
|
|
307
|
-
}
|
|
308
|
-
return `${byteCount}B`;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Block the current thread for the given duration without busy-waiting.
|
|
313
|
-
* Safe for CLI/batch tools; not suitable for servers.
|
|
314
|
-
*
|
|
315
|
-
* @param {number} ms
|
|
316
|
-
*/
|
|
317
|
-
function sleepSync(ms) {
|
|
318
|
-
if (ms <= 0) return;
|
|
319
|
-
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* Write a failure log for a contract testCommand attempt to the task's .reviews/ directory.
|
|
324
|
-
* Returns the written path, or null if taskFolder is not provided.
|
|
325
|
-
*
|
|
326
|
-
* @param {string | undefined} taskFolder
|
|
327
|
-
* @param {string} command
|
|
328
|
-
* @param {{ exitCode: number, output: string, bufferOverflow?: boolean }} result
|
|
329
|
-
* @param {number} attempt
|
|
330
|
-
* @param {number} totalAttempts
|
|
331
|
-
* @returns {string | null}
|
|
332
|
-
*/
|
|
333
|
-
export function writeContractFailureLog(taskFolder, command, result, attempt, totalAttempts) {
|
|
334
|
-
if (!taskFolder) return null;
|
|
335
|
-
const reviewsDir = path.join(taskFolder, ".reviews");
|
|
336
|
-
fs.mkdirSync(reviewsDir, { recursive: true });
|
|
337
|
-
const ts = new Date().toISOString().replace(/[:.]/g, "-");
|
|
338
|
-
const logPath = path.join(reviewsDir, `contract-fail-${ts}.log`);
|
|
339
|
-
const header = [
|
|
340
|
-
"Contract testCommand failure log",
|
|
341
|
-
`Command: ${command}`,
|
|
342
|
-
`Exit code: ${result.exitCode}`,
|
|
343
|
-
`Attempt: ${attempt} of ${totalAttempts}`,
|
|
344
|
-
`Timestamp: ${new Date().toISOString()}`,
|
|
345
|
-
`Buffer overflow: ${result.bufferOverflow ? "yes" : "no"}`,
|
|
346
|
-
"---",
|
|
347
|
-
"",
|
|
348
|
-
].join("\n");
|
|
349
|
-
fs.writeFileSync(logPath, header + (result.output ?? ""), "utf-8");
|
|
350
|
-
return logPath;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Defense in depth: block `npm test -- <path>` before subprocess spawn (issue #187, SP-541).
|
|
355
|
-
*
|
|
356
|
-
* @param {string} command
|
|
357
|
-
* @returns {boolean}
|
|
358
|
-
*/
|
|
359
|
-
export function isRefusedNpmTestDashDashCommand(command) {
|
|
360
|
-
const trimmed = String(command ?? "").trim();
|
|
361
|
-
if (!trimmed || trimmed === "true") {
|
|
362
|
-
return false;
|
|
363
|
-
}
|
|
364
|
-
return NPM_TEST_DASH_DASH_RE.test(trimmed);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* @param {string} command
|
|
369
|
-
* @returns {string}
|
|
370
|
-
*/
|
|
371
|
-
function formatRefusedNpmTestDashDashMessage(command) {
|
|
372
|
-
return `Contract testCommand refused before spawn: npm test -- <path> is blocked at runtime (command: ${command}). ${TEST_COMMAND_NPM_TEST_DASH_DASH_FIX_HINT}`;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* @param {string} worktreePath
|
|
377
|
-
* @param {string} command
|
|
378
|
-
* @param {{ maxBuffer?: number }} [options]
|
|
379
|
-
*/
|
|
380
|
-
export function runContractTestCommand(worktreePath, command, options = {}) {
|
|
381
|
-
const trimmed = String(command ?? "").trim();
|
|
382
|
-
if (!trimmed || trimmed === "true") {
|
|
383
|
-
return { ok: true, exitCode: 0, output: "" };
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
if (isRefusedNpmTestDashDashCommand(trimmed)) {
|
|
387
|
-
const summary = formatRefusedNpmTestDashDashMessage(trimmed);
|
|
388
|
-
return {
|
|
389
|
-
ok: false,
|
|
390
|
-
exitCode: 1,
|
|
391
|
-
output: summary,
|
|
392
|
-
summary,
|
|
393
|
-
refusedBeforeSpawn: true,
|
|
394
|
-
};
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
const maxBuffer = options.maxBuffer ?? CONTRACT_TEST_COMMAND_MAX_BUFFER;
|
|
398
|
-
const shell = process.env.SHELL || (process.platform === "win32" ? "cmd.exe" : "/bin/sh");
|
|
399
|
-
const shellFlag = process.platform === "win32" ? "/c" : "-c";
|
|
400
|
-
const result = spawnSync(shell, [shellFlag, trimmed], {
|
|
401
|
-
cwd: worktreePath,
|
|
402
|
-
env: buildContractTestEnv(),
|
|
403
|
-
encoding: "utf-8",
|
|
404
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
405
|
-
timeout: 10 * 60 * 1000,
|
|
406
|
-
maxBuffer,
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
const stdout = String(result.stdout ?? "");
|
|
410
|
-
const stderr = String(result.stderr ?? "");
|
|
411
|
-
const output = `${stdout}\n${stderr}`;
|
|
412
|
-
|
|
413
|
-
if (result.error?.code === "ENOBUFS") {
|
|
414
|
-
const limitLabel = formatMaxBufferLabel(maxBuffer);
|
|
415
|
-
return {
|
|
416
|
-
ok: false,
|
|
417
|
-
exitCode: Number(result.status ?? 255),
|
|
418
|
-
output,
|
|
419
|
-
bufferOverflow: true,
|
|
420
|
-
summary: `testCommand output exceeded maxBuffer (${limitLabel}); use a scoped testCommand instead of full-suite commands. Command: ${trimmed}`,
|
|
421
|
-
};
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
const exitCode = Number(result.status ?? 1);
|
|
425
|
-
if (exitCode === 0) {
|
|
426
|
-
return { ok: true, exitCode: 0, output };
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
const summary = [stdout.trim(), stderr.trim()].filter(Boolean).join("\n").slice(0, 500);
|
|
430
|
-
return { ok: false, exitCode, output, summary };
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
/**
|
|
434
|
-
* @param {string} worktreePath
|
|
435
|
-
* @param {object} config
|
|
436
|
-
*/
|
|
437
|
-
function resolveCoverageCommand(config) {
|
|
438
|
-
return (
|
|
439
|
-
config?.testing?.testWithCoverage ??
|
|
440
|
-
config?.testing?.test ??
|
|
441
|
-
"npm run coverage:check"
|
|
442
|
-
);
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
/**
|
|
446
|
-
* @param {string} worktreePath
|
|
447
|
-
* @param {ReturnType<import("../tasks/packet/parse-prompt.mjs").parseContract>} parsedContract
|
|
448
|
-
* @param {object} config
|
|
449
|
-
* @param {string} [testCommandOutput]
|
|
450
|
-
*/
|
|
451
|
-
function resolveLineCoverage(worktreePath, parsedContract, config, testCommandOutput = "") {
|
|
452
|
-
let output = testCommandOutput;
|
|
453
|
-
if (parsedContract.minLineCoverage != null) {
|
|
454
|
-
const parsedFromTest = parseAggregateLineCoverage(output);
|
|
455
|
-
if (parsedFromTest != null) {
|
|
456
|
-
return parsedFromTest;
|
|
457
|
-
}
|
|
458
|
-
const coverageCommand = resolveCoverageCommand(config);
|
|
459
|
-
const coverageResult = runContractTestCommand(worktreePath, coverageCommand);
|
|
460
|
-
output = coverageResult.output;
|
|
461
|
-
}
|
|
462
|
-
return parseAggregateLineCoverage(output);
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* @param {string} worktreePath
|
|
467
|
-
* @param {string} artifactPattern
|
|
468
|
-
* @returns {{ ok: boolean, matchedPath?: string }}
|
|
469
|
-
*/
|
|
470
|
-
function findArtifactMatch(worktreePath, artifactPattern) {
|
|
471
|
-
const normalized = String(artifactPattern ?? "").replace(/\\/g, "/");
|
|
472
|
-
if (!normalized) {
|
|
473
|
-
return { ok: false };
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
if (!/[*?[\]{}]/.test(normalized)) {
|
|
477
|
-
const fullPath = path.join(worktreePath, normalized);
|
|
478
|
-
return fs.existsSync(fullPath) ? { ok: true, matchedPath: normalized } : { ok: false };
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
const baseDir = path.dirname(normalized);
|
|
482
|
-
const searchRoot = path.join(worktreePath, baseDir);
|
|
483
|
-
if (!fs.existsSync(searchRoot)) {
|
|
484
|
-
return { ok: false };
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
/** @type {Array<{ abs: string, rel: string }>} */
|
|
488
|
-
const stack = [{ abs: searchRoot, rel: baseDir === "." ? "" : baseDir }];
|
|
489
|
-
while (stack.length > 0) {
|
|
490
|
-
const { abs, rel } = stack.pop();
|
|
491
|
-
for (const entry of fs.readdirSync(abs, { withFileTypes: true })) {
|
|
492
|
-
const entryRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
493
|
-
const entryAbs = path.join(abs, entry.name);
|
|
494
|
-
if (entry.isDirectory()) {
|
|
495
|
-
stack.push({ abs: entryAbs, rel: entryRel });
|
|
496
|
-
continue;
|
|
497
|
-
}
|
|
498
|
-
if (micromatch.isMatch(entryRel, normalized)) {
|
|
499
|
-
return { ok: true, matchedPath: entryRel };
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
return { ok: false };
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
/**
|
|
508
|
-
* Prepare lane worktree before contract testCommand (Flutter analyzer hygiene #78).
|
|
509
|
-
*
|
|
510
|
-
* @param {string} worktreePath
|
|
511
|
-
* @param {ReturnType<import("../tasks/packet/parse-prompt.mjs").parseContract>} parsedContract
|
|
512
|
-
* @param {object} [config]
|
|
513
|
-
* @returns {{ hygieneApplied: boolean, cleaned?: boolean, buildDir?: string, reason?: string }}
|
|
514
|
-
*/
|
|
515
|
-
export function prepareContractVerifyEnvironment(worktreePath, parsedContract, config = {}) {
|
|
516
|
-
const testCommand = parsedContract?.testCommand;
|
|
517
|
-
if (!testCommand || !shouldCleanFlutterBuildBeforeAnalyze(testCommand, config)) {
|
|
518
|
-
return { hygieneApplied: false };
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
const result = sanitizeFlutterBuildBeforeAnalyze(worktreePath);
|
|
522
|
-
return { hygieneApplied: true, ...result };
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
/**
|
|
526
|
-
* @param {string} worktreePath
|
|
527
|
-
* @param {ReturnType<import("../tasks/packet/parse-prompt.mjs").parseContract>} parsedContract
|
|
528
|
-
* @param {object} [config]
|
|
529
|
-
* @param {string} [config.baseBranch]
|
|
530
|
-
* @param {string} [config.sinceCommit] When set, scope file-scope checks to `sinceCommit..HEAD` (serialized lanes).
|
|
531
|
-
* @param {string} [config.taskStartCommit] Alias for `sinceCommit` (SP-415 journal resolution; anchor stable across retry/resume per SP-478).
|
|
532
|
-
* @param {string} [config.projectRoot] Repo root for journal events (optional).
|
|
533
|
-
* @param {string} [config.batchId] Batch ID for journal events (optional).
|
|
534
|
-
* @param {string} [config.taskId] Task ID for journal events (optional).
|
|
535
|
-
* @param {string} [config.taskFolder] Task folder path for writing failure logs to .reviews/ (optional).
|
|
536
|
-
* @returns {{ ok: boolean, checks: Array<{ field: string, ok: boolean, message: string }>, retries?: number }}
|
|
537
|
-
*/
|
|
538
|
-
export function verifyContract(worktreePath, parsedContract, config = {}) {
|
|
539
|
-
/** @type {Array<{ field: string, ok: boolean, message: string }>} */
|
|
540
|
-
const checks = [];
|
|
541
|
-
const baseBranch = config?.baseBranch ?? "main";
|
|
542
|
-
const sinceCommit = config?.sinceCommit ?? config?.taskStartCommit ?? undefined;
|
|
543
|
-
const changedFiles = listChangedFiles(worktreePath, baseBranch, sinceCommit);
|
|
544
|
-
|
|
545
|
-
prepareContractVerifyEnvironment(worktreePath, parsedContract, config);
|
|
546
|
-
|
|
547
|
-
let testCommandOutput = "";
|
|
548
|
-
let testCommandOk = true;
|
|
549
|
-
let retriedCount = 0;
|
|
550
|
-
if (parsedContract.testCommand) {
|
|
551
|
-
const maxRetries = config?.contract?.testRetries ?? CONTRACT_TEST_DEFAULT_RETRIES;
|
|
552
|
-
const retryDelayMs = config?.contract?.testRetryDelayMs ?? CONTRACT_TEST_RETRY_DELAY_MS;
|
|
553
|
-
const totalAttempts = maxRetries + 1;
|
|
554
|
-
let lastResult = null;
|
|
555
|
-
let successAttempt = 0;
|
|
556
|
-
|
|
557
|
-
for (let attempt = 1; attempt <= totalAttempts; attempt++) {
|
|
558
|
-
lastResult = runContractTestCommand(worktreePath, parsedContract.testCommand, {
|
|
559
|
-
maxBuffer: config?.contractTestMaxBuffer,
|
|
560
|
-
});
|
|
561
|
-
|
|
562
|
-
if (lastResult.ok) {
|
|
563
|
-
successAttempt = attempt;
|
|
564
|
-
break;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
writeContractFailureLog(
|
|
568
|
-
config?.taskFolder,
|
|
569
|
-
parsedContract.testCommand,
|
|
570
|
-
lastResult,
|
|
571
|
-
attempt,
|
|
572
|
-
totalAttempts,
|
|
573
|
-
);
|
|
574
|
-
|
|
575
|
-
if (attempt < totalAttempts) {
|
|
576
|
-
retriedCount++;
|
|
577
|
-
if (config?.projectRoot && config?.batchId) {
|
|
578
|
-
appendJournalEvent(config.projectRoot, config.batchId, "contract.test_retry", {
|
|
579
|
-
taskId: config?.taskId,
|
|
580
|
-
attempt,
|
|
581
|
-
exitCode: lastResult.exitCode,
|
|
582
|
-
totalAttempts,
|
|
583
|
-
});
|
|
584
|
-
}
|
|
585
|
-
sleepSync(retryDelayMs);
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
testCommandOutput = lastResult.output;
|
|
590
|
-
testCommandOk = lastResult.ok;
|
|
591
|
-
const attemptLabel = successAttempt > 1
|
|
592
|
-
? ` (passed on attempt ${successAttempt} of ${totalAttempts})`
|
|
593
|
-
: "";
|
|
594
|
-
checks.push({
|
|
595
|
-
field: "testCommand",
|
|
596
|
-
ok: lastResult.ok,
|
|
597
|
-
message: lastResult.ok
|
|
598
|
-
? `testCommand passed${attemptLabel}`
|
|
599
|
-
: lastResult.bufferOverflow
|
|
600
|
-
? `Contract ${lastResult.summary}`
|
|
601
|
-
: `Contract testCommand failed after ${totalAttempts} attempt(s) (exit ${lastResult.exitCode}): ${lastResult.summary || "(no output)"}`,
|
|
602
|
-
});
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
for (const pattern of parsedContract.fileScopeMustChange ?? []) {
|
|
606
|
-
const matched = changedFiles.some((file) =>
|
|
607
|
-
pattern.endsWith("/") ? file.startsWith(pattern) : matchesContractPattern(file, pattern),
|
|
608
|
-
);
|
|
609
|
-
const prelanded = !matched &&
|
|
610
|
-
isPrelandedFileScopeSatisfied(
|
|
611
|
-
worktreePath,
|
|
612
|
-
pattern,
|
|
613
|
-
changedFiles,
|
|
614
|
-
parsedContract,
|
|
615
|
-
config,
|
|
616
|
-
baseBranch,
|
|
617
|
-
{ testCommandOk },
|
|
618
|
-
(worktree, artifactPath) => findArtifactMatch(worktree, artifactPath).ok,
|
|
619
|
-
);
|
|
620
|
-
const resumeBaseline = !matched && !prelanded && sinceCommit &&
|
|
621
|
-
isResumeBaselineFileScopeSatisfied(
|
|
622
|
-
worktreePath,
|
|
623
|
-
pattern,
|
|
624
|
-
changedFiles,
|
|
625
|
-
sinceCommit,
|
|
626
|
-
baseBranch,
|
|
627
|
-
parsedContract,
|
|
628
|
-
{ testCommandOk },
|
|
629
|
-
(worktree, artifactPath) => findArtifactMatch(worktree, artifactPath).ok,
|
|
630
|
-
);
|
|
631
|
-
const baseSatisfied = !matched && !prelanded && !resumeBaseline &&
|
|
632
|
-
isBaseFileScopeSatisfied(
|
|
633
|
-
worktreePath,
|
|
634
|
-
pattern,
|
|
635
|
-
changedFiles,
|
|
636
|
-
parsedContract,
|
|
637
|
-
baseBranch,
|
|
638
|
-
{ testCommandOk },
|
|
639
|
-
(worktree, artifactPath) => findArtifactMatch(worktree, artifactPath).ok,
|
|
640
|
-
config,
|
|
641
|
-
);
|
|
642
|
-
const ok = matched || prelanded || resumeBaseline || baseSatisfied;
|
|
643
|
-
checks.push({
|
|
644
|
-
field: "fileScopeMustChange",
|
|
645
|
-
ok,
|
|
646
|
-
message: matched
|
|
647
|
-
? `fileScopeMustChange matched: ${pattern}`
|
|
648
|
-
: prelanded
|
|
649
|
-
? `fileScopeMustChange pre-landed on ${baseBranch}: ${pattern}`
|
|
650
|
-
: resumeBaseline
|
|
651
|
-
? `fileScopeMustChange pre-landed at resume baseline: ${pattern}`
|
|
652
|
-
: baseSatisfied
|
|
653
|
-
? `fileScopeMustChange satisfied on ${baseBranch}: ${pattern}`
|
|
654
|
-
: `Contract fileScopeMustChange: no matching changes for ${pattern}`,
|
|
655
|
-
});
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
for (const pattern of parsedContract.fileScopeMustNotChange ?? []) {
|
|
659
|
-
const forbidden = changedFiles.filter((file) => matchesContractPattern(file, pattern));
|
|
660
|
-
if (forbidden.length === 0) {
|
|
661
|
-
checks.push({
|
|
662
|
-
field: "fileScopeMustNotChange",
|
|
663
|
-
ok: true,
|
|
664
|
-
message: `fileScopeMustNotChange respected: ${pattern}`,
|
|
665
|
-
});
|
|
666
|
-
} else {
|
|
667
|
-
for (const file of forbidden) {
|
|
668
|
-
checks.push({
|
|
669
|
-
field: "fileScopeMustNotChange",
|
|
670
|
-
ok: false,
|
|
671
|
-
message: `Contract fileScopeMustNotChange: forbidden change ${file}`,
|
|
672
|
-
});
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
if (parsedContract.minLineCoverage != null) {
|
|
678
|
-
const actual = resolveLineCoverage(worktreePath, parsedContract, config, testCommandOutput);
|
|
679
|
-
const required = parsedContract.minLineCoverage;
|
|
680
|
-
if (actual == null) {
|
|
681
|
-
checks.push({
|
|
682
|
-
field: "minLineCoverage",
|
|
683
|
-
ok: false,
|
|
684
|
-
message: `Contract minLineCoverage: could not parse coverage output (required ${required}%)`,
|
|
685
|
-
});
|
|
686
|
-
} else {
|
|
687
|
-
const ok = actual >= required;
|
|
688
|
-
checks.push({
|
|
689
|
-
field: "minLineCoverage",
|
|
690
|
-
ok,
|
|
691
|
-
message: ok
|
|
692
|
-
? `minLineCoverage met: ${actual.toFixed(2)}% >= ${required}%`
|
|
693
|
-
: `Contract minLineCoverage: ${actual.toFixed(2)}% < ${required}%`,
|
|
694
|
-
});
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
for (const artifactPath of parsedContract.artifactsMustExist ?? []) {
|
|
699
|
-
const match = findArtifactMatch(worktreePath, artifactPath);
|
|
700
|
-
checks.push({
|
|
701
|
-
field: "artifactsMustExist",
|
|
702
|
-
ok: match.ok,
|
|
703
|
-
message: match.ok
|
|
704
|
-
? `artifact exists: ${match.matchedPath ?? artifactPath}`
|
|
705
|
-
: `Contract artifactsMustExist: missing ${artifactPath}`,
|
|
706
|
-
});
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
return {
|
|
710
|
-
ok: checks.length === 0 || checks.every((check) => check.ok),
|
|
711
|
-
checks,
|
|
712
|
-
...(retriedCount > 0 ? { retries: retriedCount } : {}),
|
|
713
|
-
};
|
|
714
|
-
}
|
|
13
|
+
export {
|
|
14
|
+
hasReleaseCriticalContract,
|
|
15
|
+
isExitVerificationTask,
|
|
16
|
+
isLegacyStubDoneFile,
|
|
17
|
+
isLegacyStubDoneMarker,
|
|
18
|
+
isStubWorkerMode,
|
|
19
|
+
listChangedFiles,
|
|
20
|
+
listEffectiveChangedFiles,
|
|
21
|
+
matchesContractPattern,
|
|
22
|
+
shouldEnforceStubContractAtLaneCommit,
|
|
23
|
+
shouldRunContractVerify,
|
|
24
|
+
shouldRunContractVerifyForWorker,
|
|
25
|
+
verifyStubFileScopeMustChange,
|
|
26
|
+
} from "./contract-parse.mjs";
|
|
27
|
+
|
|
28
|
+
export {
|
|
29
|
+
CONTRACT_TEST_COMMAND_MAX_BUFFER,
|
|
30
|
+
CONTRACT_TEST_DEFAULT_RETRIES,
|
|
31
|
+
CONTRACT_TEST_RETRY_DELAY_MS,
|
|
32
|
+
CONTRACT_TEST_WORKER_ENV_KEYS,
|
|
33
|
+
buildContractTestEnv,
|
|
34
|
+
isRefusedNpmTestDashDashCommand,
|
|
35
|
+
prepareContractVerifyEnvironment,
|
|
36
|
+
runContractTestCommand,
|
|
37
|
+
verifyContract,
|
|
38
|
+
writeContractFailureLog,
|
|
39
|
+
} from "./contract-exec.mjs";
|