pi-spine 1.2.0 → 1.4.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/README.md +4 -4
- package/bin/spine-cli/verify.mjs +6 -0
- package/bin/spine-deps.mjs +0 -2
- package/bin/spine-plan.mjs +49 -1
- package/bin/spine-rules.mjs +0 -2
- package/bin/spine-settings.mjs +0 -2
- package/bin/spine-worker-runner.mjs +136 -4
- package/bin/spine.mjs +2 -5
- package/package.json +2 -2
- package/skills/create-spine-tasks/SKILL.md +7 -2
- package/skills/create-spine-tasks/references/contract-template.md +95 -3
- package/src/batch/attached-engine-handoff.mjs +0 -1
- package/src/batch/attached-runner.mjs +81 -5
- package/src/batch/contract-stub-delivery.mjs +110 -0
- package/src/batch/contract-task-start.mjs +204 -0
- package/src/batch/contract-verify.mjs +160 -16
- package/src/batch/detached-start.mjs +72 -2
- package/src/batch/diagnosis-launch-failure.mjs +114 -0
- package/src/batch/diagnosis-primary-failure.mjs +89 -0
- package/src/batch/diagnosis-retry-limbo.mjs +1 -1
- package/src/batch/diagnosis-tail-state.mjs +57 -0
- package/src/batch/diagnosis.mjs +22 -114
- package/src/batch/engine-lanes/commit.mjs +145 -0
- package/src/batch/engine-lanes/merge.mjs +1 -2
- package/src/batch/engine-lanes/review.mjs +124 -121
- package/src/batch/engine-lanes.mjs +21 -81
- package/src/batch/engine.mjs +46 -1
- package/src/batch/evidence.mjs +1 -2
- package/src/batch/git-exec.mjs +5 -2
- package/src/batch/heartbeat.mjs +2 -2
- package/src/batch/integrate-assert.mjs +69 -0
- package/src/batch/integrate-worktree.mjs +354 -0
- package/src/batch/integrate.mjs +195 -102
- package/src/batch/journal-rebuild.mjs +158 -3
- package/src/batch/journal.mjs +63 -0
- package/src/batch/lane-commit.mjs +6 -5
- package/src/batch/lane-dirty-check.mjs +513 -0
- package/src/batch/limbo-detect.mjs +34 -0
- package/src/batch/macro-phase.mjs +76 -0
- package/src/batch/metrics.mjs +2 -0
- package/src/batch/pause.mjs +18 -7
- package/src/batch/post-merge-limbo.mjs +3 -28
- package/src/batch/reconcile.mjs +79 -10
- package/src/batch/resume-common.mjs +27 -0
- package/src/batch/resume-multi-lanes.mjs +11 -0
- package/src/batch/resume-multi.mjs +3 -1
- package/src/batch/resume.mjs +8 -4
- package/src/batch/retry.mjs +41 -10
- package/src/batch/review-spawn.mjs +1 -1
- package/src/batch/review.mjs +0 -4
- package/src/batch/rules-manifest-drift.mjs +43 -2
- package/src/batch/sequence-state.mjs +290 -0
- package/src/batch/sequence.mjs +113 -51
- package/src/batch/state.mjs +16 -0
- package/src/batch/worker-host.mjs +14 -5
- package/src/cli/deps.mjs +0 -2
- package/src/cli/sequence.mjs +14 -1
- package/src/cli/settings-set.mjs +33 -3
- package/src/config/contract.mjs +10 -0
- package/src/config/model-id.mjs +103 -0
- package/src/config/spine-init-constants.mjs +1 -0
- package/src/config/spine-preflight-lib.mjs +23 -1
- package/src/dashboard/public/dashboard.css +8 -0
- package/src/dashboard/public/dashboard.js +36 -9
- package/src/dashboard/running-tail-state.mjs +31 -0
- package/src/dashboard/server.mjs +4 -0
- package/src/dashboard/snapshot.mjs +260 -29
- package/src/dashboard/view.mjs +112 -2
- package/src/doctor/agent-models.mjs +53 -0
- package/src/doctor/batch-size-guidance.mjs +30 -2
- package/src/doctor/pi-extension-conflict.mjs +169 -0
- package/src/doctor/run-doctor-checks.mjs +6 -0
- package/src/planner/scope.mjs +5 -1
- package/src/tasks/packet/parse-prompt.mjs +31 -1
- package/src/tasks/packet/validate-contract.mjs +108 -0
- package/src/tasks/packet/validate-prompt.mjs +0 -1
|
@@ -9,11 +9,12 @@ import {
|
|
|
9
9
|
isPostMergeLimbo,
|
|
10
10
|
} from "./post-merge-limbo.mjs";
|
|
11
11
|
import { detectPostMergeLimboForResume } from "./resume-multi-validate.mjs";
|
|
12
|
+
import { isProcessAlive } from "../process/liveness.mjs";
|
|
12
13
|
import { terminateStaleDetachedEngine } from "./resume-engine.mjs";
|
|
13
14
|
import { reconcileBatch } from "./reconcile.mjs";
|
|
14
|
-
import {
|
|
15
|
+
import { readJournalEventsCached } from "./journal.mjs";
|
|
15
16
|
import { enforceOperatorPauseOnDisk } from "./pause.mjs";
|
|
16
|
-
import { loadSpineBatchState, saveSpineBatchState } from "./state.mjs";
|
|
17
|
+
import { loadSpineBatchState, readBatchEnginePid, saveSpineBatchState } from "./state.mjs";
|
|
17
18
|
|
|
18
19
|
/** Journal types surfaced on attached stdout during the land loop. */
|
|
19
20
|
export const ATTACHED_LAND_LOOP_MILESTONE_TYPES = new Set([
|
|
@@ -116,7 +117,7 @@ export async function startAttachedMilestoneReporter({ projectRoot, write = (lin
|
|
|
116
117
|
batchId = loaded.raw?.batchId ? String(loaded.raw.batchId) : null;
|
|
117
118
|
}
|
|
118
119
|
if (batchId) {
|
|
119
|
-
const events =
|
|
120
|
+
const events = readJournalEventsCached(projectRoot, batchId);
|
|
120
121
|
for (const event of events) {
|
|
121
122
|
const key = milestoneEventKey(event);
|
|
122
123
|
if (printed.has(key)) continue;
|
|
@@ -136,7 +137,7 @@ export async function startAttachedMilestoneReporter({ projectRoot, write = (lin
|
|
|
136
137
|
stopped = true;
|
|
137
138
|
await loopPromise;
|
|
138
139
|
if (batchId) {
|
|
139
|
-
const events =
|
|
140
|
+
const events = readJournalEventsCached(projectRoot, batchId);
|
|
140
141
|
for (const event of events) {
|
|
141
142
|
const key = milestoneEventKey(event);
|
|
142
143
|
if (printed.has(key)) continue;
|
|
@@ -149,6 +150,59 @@ export async function startAttachedMilestoneReporter({ projectRoot, write = (lin
|
|
|
149
150
|
};
|
|
150
151
|
}
|
|
151
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Reject a second attached engine when resilience.enginePid is alive (SP-434, GitHub #89).
|
|
155
|
+
*
|
|
156
|
+
* @param {object} params
|
|
157
|
+
* @param {string} params.projectRoot
|
|
158
|
+
* @param {boolean} [params.force] Orphan-terminate prior engine before handoff
|
|
159
|
+
* @param {"start"|"resume"} [params.operation]
|
|
160
|
+
*/
|
|
161
|
+
export function enforceAttachedEngineSingleOwner({ projectRoot, force = false, operation = "resume" }) {
|
|
162
|
+
const loaded = loadSpineBatchState(projectRoot);
|
|
163
|
+
const state = loaded.raw;
|
|
164
|
+
if (!state) {
|
|
165
|
+
return { ok: true };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const batchId = String(state.batchId ?? "");
|
|
169
|
+
const enginePid = readBatchEnginePid(state);
|
|
170
|
+
if (enginePid == null || enginePid === process.pid || !isProcessAlive(enginePid)) {
|
|
171
|
+
return { ok: true };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const fromPhase = String(state.phase ?? "");
|
|
175
|
+
if (force) {
|
|
176
|
+
const terminateResult = terminateStaleDetachedEngine({
|
|
177
|
+
projectRoot,
|
|
178
|
+
state,
|
|
179
|
+
batchId,
|
|
180
|
+
fromPhase,
|
|
181
|
+
allowRunningOrphanTerminate: true,
|
|
182
|
+
});
|
|
183
|
+
saveSpineBatchState(projectRoot, state, { bypassWriteGuard: true });
|
|
184
|
+
return {
|
|
185
|
+
ok: true,
|
|
186
|
+
handoff: true,
|
|
187
|
+
terminated: terminateResult.terminated,
|
|
188
|
+
stalePid: terminateResult.stalePid ?? enginePid,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const operationLabel = operation === "start" ? "start" : "resume";
|
|
193
|
+
const output =
|
|
194
|
+
`Attached batch engine already running (PID ${enginePid}, batch ${batchId}).\n` +
|
|
195
|
+
`Stop the existing engine or run spine batch ${operationLabel} --attached --force to orphan it first.\n`;
|
|
196
|
+
return {
|
|
197
|
+
ok: false,
|
|
198
|
+
exitCode: 1,
|
|
199
|
+
error: "attached_engine_already_running",
|
|
200
|
+
output,
|
|
201
|
+
batchId,
|
|
202
|
+
enginePid,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
152
206
|
/**
|
|
153
207
|
* Run an attached foreground engine while streaming land-loop journal milestones to stdout.
|
|
154
208
|
*
|
|
@@ -156,8 +210,30 @@ export async function startAttachedMilestoneReporter({ projectRoot, write = (lin
|
|
|
156
210
|
* @param {string} params.projectRoot
|
|
157
211
|
* @param {() => Promise<object>} params.runEngine
|
|
158
212
|
* @param {(line: string) => void} [params.write]
|
|
213
|
+
* @param {boolean} [params.force]
|
|
214
|
+
* @param {"start"|"resume"} [params.operation]
|
|
159
215
|
*/
|
|
160
|
-
export async function runAttachedBatchEngine({
|
|
216
|
+
export async function runAttachedBatchEngine({
|
|
217
|
+
projectRoot,
|
|
218
|
+
runEngine,
|
|
219
|
+
write,
|
|
220
|
+
spineBin,
|
|
221
|
+
_force = false,
|
|
222
|
+
operation = "resume",
|
|
223
|
+
}) {
|
|
224
|
+
if (operation === "start") {
|
|
225
|
+
const lock = enforceAttachedEngineSingleOwner({ projectRoot, force: false, operation: "start" });
|
|
226
|
+
if (!lock.ok) {
|
|
227
|
+
return {
|
|
228
|
+
ok: false,
|
|
229
|
+
exitCode: lock.exitCode ?? 1,
|
|
230
|
+
error: lock.error,
|
|
231
|
+
output: lock.output,
|
|
232
|
+
batchId: lock.batchId,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
161
237
|
installAttachedExitFinalizeHandlers({ projectRoot, spineBin });
|
|
162
238
|
const reporter = await startAttachedMilestoneReporter({ projectRoot, write });
|
|
163
239
|
try {
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stub delivery-only fileScopeMustChange detection (GitHub #67, SP-407).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { DEFAULT_TASKS_ROOT } from "../config/spine-init-constants.mjs";
|
|
6
|
+
|
|
7
|
+
/** Repo roots that indicate implementation scope, not stub delivery. */
|
|
8
|
+
const IMPLEMENTATION_ROOT_PREFIXES = [
|
|
9
|
+
"src/",
|
|
10
|
+
"bin/",
|
|
11
|
+
"tests/",
|
|
12
|
+
"scripts/",
|
|
13
|
+
"docs/",
|
|
14
|
+
".github/",
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
/** Exact repo-root files that indicate implementation scope. */
|
|
18
|
+
const IMPLEMENTATION_ROOT_FILES = new Set([
|
|
19
|
+
"package.json",
|
|
20
|
+
"package-lock.json",
|
|
21
|
+
"eslint.config.js",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} pattern
|
|
26
|
+
*/
|
|
27
|
+
function normalizePattern(pattern) {
|
|
28
|
+
return String(pattern ?? "").replace(/\\/g, "/").trim();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {string} tasksRoot
|
|
33
|
+
*/
|
|
34
|
+
function escapeRegex(tasksRoot) {
|
|
35
|
+
return normalizePattern(tasksRoot).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {string} pattern
|
|
40
|
+
*/
|
|
41
|
+
export function isImplementationScopePattern(pattern) {
|
|
42
|
+
const normalized = normalizePattern(pattern);
|
|
43
|
+
if (!normalized) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
if (IMPLEMENTATION_ROOT_FILES.has(normalized)) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
for (const prefix of IMPLEMENTATION_ROOT_PREFIXES) {
|
|
50
|
+
if (normalized === prefix.slice(0, -1) || normalized.startsWith(prefix)) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* True when a contract path pattern targets only stub delivery artifacts.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} pattern
|
|
61
|
+
* @param {string} [tasksRoot]
|
|
62
|
+
*/
|
|
63
|
+
export function isDeliveryArtifactPath(pattern, tasksRoot = DEFAULT_TASKS_ROOT) {
|
|
64
|
+
const normalized = normalizePattern(pattern);
|
|
65
|
+
if (!normalized) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if (isImplementationScopePattern(normalized)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const root = escapeRegex(tasksRoot);
|
|
73
|
+
|
|
74
|
+
if (new RegExp(`^${root}/\\*/STATUS\\.md$`).test(normalized)) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
if (new RegExp(`^${root}/[^/]+/STATUS\\.md$`).test(normalized)) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
if (new RegExp(`^${root}/\\*/\\.DONE$`).test(normalized)) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
if (new RegExp(`^${root}/[^/]+/\\.DONE$`).test(normalized)) {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
if (normalized === ".DONE") {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
if (new RegExp(`^${root}/[^/]+/\\*\\*$`).test(normalized)) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
if (new RegExp(`^${root}/[^/]+/.+`).test(normalized) && !normalized.includes("..")) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* True when every fileScopeMustChange pattern is stub delivery-only.
|
|
101
|
+
*
|
|
102
|
+
* @param {string[]} patterns
|
|
103
|
+
* @param {string} [tasksRoot]
|
|
104
|
+
*/
|
|
105
|
+
export function isStubDeliveryOnlyScope(patterns, tasksRoot = DEFAULT_TASKS_ROOT) {
|
|
106
|
+
if (!Array.isArray(patterns) || patterns.length === 0) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return patterns.every((pattern) => isDeliveryArtifactPath(pattern, tasksRoot));
|
|
110
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve lane branch HEAD at task.started for scoped contract verify (issue #62, SP-415).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { execFileSync } from "node:child_process";
|
|
6
|
+
|
|
7
|
+
const PAYLOAD_COMMIT_KEYS = ["taskStartCommit", "startCommit", "commitSha", "headCommit", "laneBranchHead"];
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {object} event
|
|
11
|
+
* @returns {string|null}
|
|
12
|
+
*/
|
|
13
|
+
function readEventCommitSha(event) {
|
|
14
|
+
const payload = event.payload && typeof event.payload === "object" ? event.payload : {};
|
|
15
|
+
for (const key of PAYLOAD_COMMIT_KEYS) {
|
|
16
|
+
const value = payload[key];
|
|
17
|
+
if (typeof value === "string" && value.trim()) {
|
|
18
|
+
return value.trim();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {object} event
|
|
26
|
+
* @returns {string|null}
|
|
27
|
+
*/
|
|
28
|
+
function readEventLaneId(event) {
|
|
29
|
+
if (typeof event.laneId === "string" && event.laneId) {
|
|
30
|
+
return event.laneId;
|
|
31
|
+
}
|
|
32
|
+
const payload = event.payload && typeof event.payload === "object" ? event.payload : {};
|
|
33
|
+
if (typeof payload.laneId === "string" && payload.laneId) {
|
|
34
|
+
return payload.laneId;
|
|
35
|
+
}
|
|
36
|
+
if (payload.laneNumber != null) {
|
|
37
|
+
return `lane-${payload.laneNumber}`;
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {object} event
|
|
44
|
+
* @returns {string|null}
|
|
45
|
+
*/
|
|
46
|
+
function readEventTaskId(event) {
|
|
47
|
+
if (typeof event.taskId === "string" && event.taskId) {
|
|
48
|
+
return event.taskId;
|
|
49
|
+
}
|
|
50
|
+
const payload = event.payload && typeof event.payload === "object" ? event.payload : {};
|
|
51
|
+
if (typeof payload.taskId === "string" && payload.taskId) {
|
|
52
|
+
return payload.taskId;
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {object} event
|
|
59
|
+
* @returns {string|null}
|
|
60
|
+
*/
|
|
61
|
+
function readEventTimestampIso(event) {
|
|
62
|
+
if (!event || typeof event !== "object") {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const ts = event.timestamp;
|
|
66
|
+
if (typeof ts === "string" && ts.trim()) {
|
|
67
|
+
return ts.trim();
|
|
68
|
+
}
|
|
69
|
+
if (typeof ts === "number" && Number.isFinite(ts)) {
|
|
70
|
+
return new Date(ts).toISOString();
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param {string|null|undefined} eventLaneId
|
|
77
|
+
* @param {string|null|undefined} requestedLaneId
|
|
78
|
+
*/
|
|
79
|
+
function lanesMatch(eventLaneId, requestedLaneId) {
|
|
80
|
+
if (!requestedLaneId) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return eventLaneId === requestedLaneId;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {object} event
|
|
88
|
+
* @param {string|null|undefined} batchId
|
|
89
|
+
*/
|
|
90
|
+
function batchMatches(event, batchId) {
|
|
91
|
+
if (!batchId) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return String(event.batchId ?? "") === batchId;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @param {object[]} journal
|
|
99
|
+
* @param {string|null|undefined} laneId
|
|
100
|
+
* @param {number} beforeIndex
|
|
101
|
+
* @returns {string|null}
|
|
102
|
+
*/
|
|
103
|
+
function resolvePriorLaneCommit(journal, laneId, beforeIndex) {
|
|
104
|
+
for (let index = beforeIndex - 1; index >= 0; index -= 1) {
|
|
105
|
+
const event = journal[index];
|
|
106
|
+
if (String(event?.type ?? "") !== "lane.committed") {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (!lanesMatch(readEventLaneId(event), laneId)) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const payload = event.payload && typeof event.payload === "object" ? event.payload : {};
|
|
113
|
+
const sha = payload.commitSha ?? payload.mergeCommit;
|
|
114
|
+
if (typeof sha === "string" && sha.trim()) {
|
|
115
|
+
return sha.trim();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {string} worktreePath
|
|
123
|
+
* @param {string} timestampIso
|
|
124
|
+
* @returns {string|null}
|
|
125
|
+
*/
|
|
126
|
+
function resolveHeadAtTimestamp(worktreePath, timestampIso) {
|
|
127
|
+
try {
|
|
128
|
+
const output = execFileSync("git", ["rev-list", "-1", `--before=${timestampIso}`, "HEAD"], {
|
|
129
|
+
cwd: worktreePath,
|
|
130
|
+
encoding: "utf-8",
|
|
131
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
132
|
+
timeout: 30_000,
|
|
133
|
+
}).trim();
|
|
134
|
+
return output || null;
|
|
135
|
+
} catch {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {object[]} journal
|
|
142
|
+
* @param {string} taskId
|
|
143
|
+
* @param {string|null|undefined} laneId
|
|
144
|
+
* @param {string|null|undefined} batchId
|
|
145
|
+
* @returns {{ event: object, index: number } | null}
|
|
146
|
+
*/
|
|
147
|
+
function findLastTaskStarted(journal, taskId, laneId, batchId) {
|
|
148
|
+
let match = null;
|
|
149
|
+
for (let index = 0; index < journal.length; index += 1) {
|
|
150
|
+
const event = journal[index];
|
|
151
|
+
if (String(event?.type ?? "") !== "task.started") {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (readEventTaskId(event) !== taskId) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (!lanesMatch(readEventLaneId(event), laneId)) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (!batchMatches(event, batchId)) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
match = { event, index };
|
|
164
|
+
}
|
|
165
|
+
return match;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Resolve parent commit SHA at task.started for per-task scoped contract verify.
|
|
170
|
+
* Returns null when unavailable so callers fall back to main...HEAD diff.
|
|
171
|
+
*
|
|
172
|
+
* @param {{ journal: object[], taskId: string, laneId?: string|null, batchId?: string|null, worktreePath?: string|null }} options
|
|
173
|
+
* @returns {string|null}
|
|
174
|
+
*/
|
|
175
|
+
export function resolveTaskStartCommit({ journal, taskId, laneId, batchId, worktreePath }) {
|
|
176
|
+
if (!Array.isArray(journal) || !taskId) {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const started = findLastTaskStarted(journal, taskId, laneId, batchId);
|
|
181
|
+
if (!started) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const payloadCommit = readEventCommitSha(started.event);
|
|
186
|
+
if (payloadCommit) {
|
|
187
|
+
return payloadCommit;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const eventLaneId = readEventLaneId(started.event);
|
|
191
|
+
const priorLaneCommit = resolvePriorLaneCommit(journal, eventLaneId, started.index);
|
|
192
|
+
if (priorLaneCommit) {
|
|
193
|
+
return priorLaneCommit;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (typeof worktreePath === "string" && worktreePath.trim()) {
|
|
197
|
+
const timestampIso = readEventTimestampIso(started.event);
|
|
198
|
+
if (timestampIso) {
|
|
199
|
+
return resolveHeadAtTimestamp(worktreePath.trim(), timestampIso);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
@@ -12,9 +12,19 @@ import {
|
|
|
12
12
|
isPrelandedFileScopeSatisfied,
|
|
13
13
|
isStubPrelandedFileScopeSatisfied,
|
|
14
14
|
} from "./contract-prelanded.mjs";
|
|
15
|
+
import { appendJournalEvent } from "./journal.mjs";
|
|
15
16
|
|
|
16
17
|
export { resolvePromptRelPath, isFileScopePatternPrelanded, hasSpineTaskDeliveryChanges } from "./contract-prelanded.mjs";
|
|
17
18
|
|
|
19
|
+
/** Default stdout/stderr capture limit for contract testCommand (issue #86). */
|
|
20
|
+
export const CONTRACT_TEST_COMMAND_MAX_BUFFER = 10 * 1024 * 1024;
|
|
21
|
+
|
|
22
|
+
/** Default number of retries for failed testCommand (SP-485, issue #136). */
|
|
23
|
+
export const CONTRACT_TEST_DEFAULT_RETRIES = 1;
|
|
24
|
+
|
|
25
|
+
/** Default delay in ms between testCommand retry attempts. */
|
|
26
|
+
export const CONTRACT_TEST_RETRY_DELAY_MS = 5000;
|
|
27
|
+
|
|
18
28
|
/**
|
|
19
29
|
* @param {string} taskId
|
|
20
30
|
* @param {ReturnType<import("../tasks/packet/parse-prompt.mjs").parseContract>} parsedContract
|
|
@@ -178,7 +188,9 @@ export function verifyStubFileScopeMustChange(
|
|
|
178
188
|
/** @type {string[]} */
|
|
179
189
|
const failures = [];
|
|
180
190
|
for (const pattern of patterns) {
|
|
181
|
-
const matched = changedFiles.some((file) =>
|
|
191
|
+
const matched = changedFiles.some((file) =>
|
|
192
|
+
pattern.endsWith("/") ? file.startsWith(pattern) : matchesContractPattern(file, pattern),
|
|
193
|
+
);
|
|
182
194
|
if (matched) {
|
|
183
195
|
continue;
|
|
184
196
|
}
|
|
@@ -193,10 +205,15 @@ export function verifyStubFileScopeMustChange(
|
|
|
193
205
|
/**
|
|
194
206
|
* @param {string} worktreePath
|
|
195
207
|
* @param {string} [baseBranch]
|
|
208
|
+
* @param {string} [sinceCommit] When set, diff `sinceCommit..HEAD` instead of `baseBranch...HEAD`.
|
|
196
209
|
*/
|
|
197
|
-
export function listChangedFiles(worktreePath, baseBranch = "main") {
|
|
210
|
+
export function listChangedFiles(worktreePath, baseBranch = "main", sinceCommit = undefined) {
|
|
211
|
+
const scopedSince = String(sinceCommit ?? "").trim();
|
|
212
|
+
const diffRange =
|
|
213
|
+
scopedSince.length > 0 ? `${scopedSince}..HEAD` : `${baseBranch}...HEAD`;
|
|
214
|
+
|
|
198
215
|
try {
|
|
199
|
-
const output = execFileSync("git", ["diff", "--name-only",
|
|
216
|
+
const output = execFileSync("git", ["diff", "--name-only", diffRange], {
|
|
200
217
|
cwd: worktreePath,
|
|
201
218
|
encoding: "utf-8",
|
|
202
219
|
stdio: ["ignore", "pipe", "pipe"],
|
|
@@ -207,6 +224,9 @@ export function listChangedFiles(worktreePath, baseBranch = "main") {
|
|
|
207
224
|
.map((line) => line.trim())
|
|
208
225
|
.filter(Boolean);
|
|
209
226
|
} catch {
|
|
227
|
+
if (scopedSince.length > 0) {
|
|
228
|
+
return [];
|
|
229
|
+
}
|
|
210
230
|
try {
|
|
211
231
|
const output = execFileSync("git", ["diff", "--name-only", "HEAD"], {
|
|
212
232
|
cwd: worktreePath,
|
|
@@ -232,16 +252,74 @@ export function matchesContractPattern(file, pattern) {
|
|
|
232
252
|
return micromatch.isMatch(file, pattern, { dot: true });
|
|
233
253
|
}
|
|
234
254
|
|
|
255
|
+
/**
|
|
256
|
+
* @param {number} byteCount
|
|
257
|
+
*/
|
|
258
|
+
function formatMaxBufferLabel(byteCount) {
|
|
259
|
+
const mib = byteCount / (1024 * 1024);
|
|
260
|
+
if (mib >= 1 && Number.isInteger(mib)) {
|
|
261
|
+
return `${mib}MB`;
|
|
262
|
+
}
|
|
263
|
+
if (byteCount >= 1024) {
|
|
264
|
+
return `${Math.round(byteCount / 1024)}KB`;
|
|
265
|
+
}
|
|
266
|
+
return `${byteCount}B`;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Block the current thread for the given duration without busy-waiting.
|
|
271
|
+
* Safe for CLI/batch tools; not suitable for servers.
|
|
272
|
+
*
|
|
273
|
+
* @param {number} ms
|
|
274
|
+
*/
|
|
275
|
+
function sleepSync(ms) {
|
|
276
|
+
if (ms <= 0) return;
|
|
277
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Write a failure log for a contract testCommand attempt to the task's .reviews/ directory.
|
|
282
|
+
* Returns the written path, or null if taskFolder is not provided.
|
|
283
|
+
*
|
|
284
|
+
* @param {string | undefined} taskFolder
|
|
285
|
+
* @param {string} command
|
|
286
|
+
* @param {{ exitCode: number, output: string, bufferOverflow?: boolean }} result
|
|
287
|
+
* @param {number} attempt
|
|
288
|
+
* @param {number} totalAttempts
|
|
289
|
+
* @returns {string | null}
|
|
290
|
+
*/
|
|
291
|
+
export function writeContractFailureLog(taskFolder, command, result, attempt, totalAttempts) {
|
|
292
|
+
if (!taskFolder) return null;
|
|
293
|
+
const reviewsDir = path.join(taskFolder, ".reviews");
|
|
294
|
+
fs.mkdirSync(reviewsDir, { recursive: true });
|
|
295
|
+
const ts = new Date().toISOString().replace(/[:.]/g, "-");
|
|
296
|
+
const logPath = path.join(reviewsDir, `contract-fail-${ts}.log`);
|
|
297
|
+
const header = [
|
|
298
|
+
"Contract testCommand failure log",
|
|
299
|
+
`Command: ${command}`,
|
|
300
|
+
`Exit code: ${result.exitCode}`,
|
|
301
|
+
`Attempt: ${attempt} of ${totalAttempts}`,
|
|
302
|
+
`Timestamp: ${new Date().toISOString()}`,
|
|
303
|
+
`Buffer overflow: ${result.bufferOverflow ? "yes" : "no"}`,
|
|
304
|
+
"---",
|
|
305
|
+
"",
|
|
306
|
+
].join("\n");
|
|
307
|
+
fs.writeFileSync(logPath, header + (result.output ?? ""), "utf-8");
|
|
308
|
+
return logPath;
|
|
309
|
+
}
|
|
310
|
+
|
|
235
311
|
/**
|
|
236
312
|
* @param {string} worktreePath
|
|
237
313
|
* @param {string} command
|
|
314
|
+
* @param {{ maxBuffer?: number }} [options]
|
|
238
315
|
*/
|
|
239
|
-
function runContractTestCommand(worktreePath, command) {
|
|
316
|
+
export function runContractTestCommand(worktreePath, command, options = {}) {
|
|
240
317
|
const trimmed = String(command ?? "").trim();
|
|
241
318
|
if (!trimmed || trimmed === "true") {
|
|
242
319
|
return { ok: true, exitCode: 0, output: "" };
|
|
243
320
|
}
|
|
244
321
|
|
|
322
|
+
const maxBuffer = options.maxBuffer ?? CONTRACT_TEST_COMMAND_MAX_BUFFER;
|
|
245
323
|
const shell = process.env.SHELL || (process.platform === "win32" ? "cmd.exe" : "/bin/sh");
|
|
246
324
|
const shellFlag = process.platform === "win32" ? "/c" : "-c";
|
|
247
325
|
const result = spawnSync(shell, [shellFlag, trimmed], {
|
|
@@ -249,13 +327,25 @@ function runContractTestCommand(worktreePath, command) {
|
|
|
249
327
|
encoding: "utf-8",
|
|
250
328
|
stdio: ["ignore", "pipe", "pipe"],
|
|
251
329
|
timeout: 10 * 60 * 1000,
|
|
252
|
-
maxBuffer
|
|
330
|
+
maxBuffer,
|
|
253
331
|
});
|
|
254
332
|
|
|
255
|
-
const exitCode = Number(result.status ?? 1);
|
|
256
333
|
const stdout = String(result.stdout ?? "");
|
|
257
334
|
const stderr = String(result.stderr ?? "");
|
|
258
335
|
const output = `${stdout}\n${stderr}`;
|
|
336
|
+
|
|
337
|
+
if (result.error?.code === "ENOBUFS") {
|
|
338
|
+
const limitLabel = formatMaxBufferLabel(maxBuffer);
|
|
339
|
+
return {
|
|
340
|
+
ok: false,
|
|
341
|
+
exitCode: Number(result.status ?? 255),
|
|
342
|
+
output,
|
|
343
|
+
bufferOverflow: true,
|
|
344
|
+
summary: `testCommand output exceeded maxBuffer (${limitLabel}); use a scoped testCommand instead of full-suite commands. Command: ${trimmed}`,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const exitCode = Number(result.status ?? 1);
|
|
259
349
|
if (exitCode === 0) {
|
|
260
350
|
return { ok: true, exitCode: 0, output };
|
|
261
351
|
}
|
|
@@ -342,31 +432,84 @@ function findArtifactMatch(worktreePath, artifactPattern) {
|
|
|
342
432
|
* @param {string} worktreePath
|
|
343
433
|
* @param {ReturnType<import("../tasks/packet/parse-prompt.mjs").parseContract>} parsedContract
|
|
344
434
|
* @param {object} [config]
|
|
345
|
-
* @
|
|
435
|
+
* @param {string} [config.baseBranch]
|
|
436
|
+
* @param {string} [config.sinceCommit] When set, scope file-scope checks to `sinceCommit..HEAD` (serialized lanes).
|
|
437
|
+
* @param {string} [config.taskStartCommit] Alias for `sinceCommit` (SP-415 journal resolution).
|
|
438
|
+
* @param {string} [config.projectRoot] Repo root for journal events (optional).
|
|
439
|
+
* @param {string} [config.batchId] Batch ID for journal events (optional).
|
|
440
|
+
* @param {string} [config.taskId] Task ID for journal events (optional).
|
|
441
|
+
* @param {string} [config.taskFolder] Task folder path for writing failure logs to .reviews/ (optional).
|
|
442
|
+
* @returns {{ ok: boolean, checks: Array<{ field: string, ok: boolean, message: string }>, retries?: number }}
|
|
346
443
|
*/
|
|
347
444
|
export function verifyContract(worktreePath, parsedContract, config = {}) {
|
|
348
445
|
/** @type {Array<{ field: string, ok: boolean, message: string }>} */
|
|
349
446
|
const checks = [];
|
|
350
447
|
const baseBranch = config?.baseBranch ?? "main";
|
|
351
|
-
const
|
|
448
|
+
const sinceCommit = config?.sinceCommit ?? config?.taskStartCommit ?? undefined;
|
|
449
|
+
const changedFiles = listChangedFiles(worktreePath, baseBranch, sinceCommit);
|
|
352
450
|
|
|
353
451
|
let testCommandOutput = "";
|
|
354
452
|
let testCommandOk = true;
|
|
453
|
+
let retriedCount = 0;
|
|
355
454
|
if (parsedContract.testCommand) {
|
|
356
|
-
const
|
|
357
|
-
|
|
358
|
-
|
|
455
|
+
const maxRetries = config?.contract?.testRetries ?? CONTRACT_TEST_DEFAULT_RETRIES;
|
|
456
|
+
const retryDelayMs = config?.contract?.testRetryDelayMs ?? CONTRACT_TEST_RETRY_DELAY_MS;
|
|
457
|
+
const totalAttempts = maxRetries + 1;
|
|
458
|
+
let lastResult = null;
|
|
459
|
+
let successAttempt = 0;
|
|
460
|
+
|
|
461
|
+
for (let attempt = 1; attempt <= totalAttempts; attempt++) {
|
|
462
|
+
lastResult = runContractTestCommand(worktreePath, parsedContract.testCommand, {
|
|
463
|
+
maxBuffer: config?.contractTestMaxBuffer,
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
if (lastResult.ok) {
|
|
467
|
+
successAttempt = attempt;
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
writeContractFailureLog(
|
|
472
|
+
config?.taskFolder,
|
|
473
|
+
parsedContract.testCommand,
|
|
474
|
+
lastResult,
|
|
475
|
+
attempt,
|
|
476
|
+
totalAttempts,
|
|
477
|
+
);
|
|
478
|
+
|
|
479
|
+
if (attempt < totalAttempts) {
|
|
480
|
+
retriedCount++;
|
|
481
|
+
if (config?.projectRoot && config?.batchId) {
|
|
482
|
+
appendJournalEvent(config.projectRoot, config.batchId, "contract.test_retry", {
|
|
483
|
+
taskId: config?.taskId,
|
|
484
|
+
attempt,
|
|
485
|
+
exitCode: lastResult.exitCode,
|
|
486
|
+
totalAttempts,
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
sleepSync(retryDelayMs);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
testCommandOutput = lastResult.output;
|
|
494
|
+
testCommandOk = lastResult.ok;
|
|
495
|
+
const attemptLabel = successAttempt > 1
|
|
496
|
+
? ` (passed on attempt ${successAttempt} of ${totalAttempts})`
|
|
497
|
+
: "";
|
|
359
498
|
checks.push({
|
|
360
499
|
field: "testCommand",
|
|
361
|
-
ok:
|
|
362
|
-
message:
|
|
363
|
-
?
|
|
364
|
-
:
|
|
500
|
+
ok: lastResult.ok,
|
|
501
|
+
message: lastResult.ok
|
|
502
|
+
? `testCommand passed${attemptLabel}`
|
|
503
|
+
: lastResult.bufferOverflow
|
|
504
|
+
? `Contract ${lastResult.summary}`
|
|
505
|
+
: `Contract testCommand failed after ${totalAttempts} attempt(s) (exit ${lastResult.exitCode}): ${lastResult.summary || "(no output)"}`,
|
|
365
506
|
});
|
|
366
507
|
}
|
|
367
508
|
|
|
368
509
|
for (const pattern of parsedContract.fileScopeMustChange ?? []) {
|
|
369
|
-
const matched = changedFiles.some((file) =>
|
|
510
|
+
const matched = changedFiles.some((file) =>
|
|
511
|
+
pattern.endsWith("/") ? file.startsWith(pattern) : matchesContractPattern(file, pattern),
|
|
512
|
+
);
|
|
370
513
|
const prelanded = !matched &&
|
|
371
514
|
isPrelandedFileScopeSatisfied(
|
|
372
515
|
worktreePath,
|
|
@@ -444,5 +587,6 @@ export function verifyContract(worktreePath, parsedContract, config = {}) {
|
|
|
444
587
|
return {
|
|
445
588
|
ok: checks.length === 0 || checks.every((check) => check.ok),
|
|
446
589
|
checks,
|
|
590
|
+
...(retriedCount > 0 ? { retries: retriedCount } : {}),
|
|
447
591
|
};
|
|
448
592
|
}
|