pi-spine 1.2.0 → 1.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.
Files changed (60) hide show
  1. package/README.md +4 -4
  2. package/bin/spine-cli/verify.mjs +3 -0
  3. package/bin/spine-plan.mjs +49 -1
  4. package/bin/spine-worker-runner.mjs +136 -4
  5. package/package.json +1 -1
  6. package/skills/create-spine-tasks/SKILL.md +7 -2
  7. package/skills/create-spine-tasks/references/contract-template.md +95 -3
  8. package/src/batch/attached-runner.mjs +81 -5
  9. package/src/batch/contract-stub-delivery.mjs +110 -0
  10. package/src/batch/contract-task-start.mjs +204 -0
  11. package/src/batch/contract-verify.mjs +55 -8
  12. package/src/batch/detached-start.mjs +72 -2
  13. package/src/batch/diagnosis-launch-failure.mjs +114 -0
  14. package/src/batch/diagnosis-primary-failure.mjs +89 -0
  15. package/src/batch/diagnosis-tail-state.mjs +57 -0
  16. package/src/batch/diagnosis.mjs +22 -113
  17. package/src/batch/engine-lanes/commit.mjs +145 -0
  18. package/src/batch/engine-lanes/review.mjs +67 -87
  19. package/src/batch/engine-lanes.mjs +21 -79
  20. package/src/batch/engine.mjs +46 -0
  21. package/src/batch/heartbeat.mjs +2 -2
  22. package/src/batch/integrate-assert.mjs +69 -0
  23. package/src/batch/integrate-worktree.mjs +354 -0
  24. package/src/batch/integrate.mjs +195 -102
  25. package/src/batch/journal-rebuild.mjs +48 -2
  26. package/src/batch/journal.mjs +63 -0
  27. package/src/batch/lane-dirty-check.mjs +437 -0
  28. package/src/batch/limbo-detect.mjs +34 -0
  29. package/src/batch/macro-phase.mjs +76 -0
  30. package/src/batch/metrics.mjs +2 -0
  31. package/src/batch/pause.mjs +18 -7
  32. package/src/batch/post-merge-limbo.mjs +3 -28
  33. package/src/batch/reconcile.mjs +79 -9
  34. package/src/batch/resume-common.mjs +27 -0
  35. package/src/batch/resume-multi-lanes.mjs +11 -0
  36. package/src/batch/resume-multi.mjs +2 -0
  37. package/src/batch/resume.mjs +8 -1
  38. package/src/batch/retry.mjs +41 -10
  39. package/src/batch/rules-manifest-drift.mjs +43 -2
  40. package/src/batch/sequence-state.mjs +290 -0
  41. package/src/batch/sequence.mjs +113 -51
  42. package/src/batch/state.mjs +16 -0
  43. package/src/batch/worker-host.mjs +1 -0
  44. package/src/cli/sequence.mjs +14 -1
  45. package/src/cli/settings-set.mjs +33 -3
  46. package/src/config/model-id.mjs +103 -0
  47. package/src/config/spine-init-constants.mjs +1 -0
  48. package/src/config/spine-preflight-lib.mjs +23 -1
  49. package/src/dashboard/public/dashboard.js +14 -3
  50. package/src/dashboard/running-tail-state.mjs +31 -0
  51. package/src/dashboard/server.mjs +4 -0
  52. package/src/dashboard/snapshot.mjs +260 -28
  53. package/src/dashboard/view.mjs +112 -2
  54. package/src/doctor/agent-models.mjs +53 -0
  55. package/src/doctor/batch-size-guidance.mjs +30 -2
  56. package/src/doctor/pi-extension-conflict.mjs +169 -0
  57. package/src/doctor/run-doctor-checks.mjs +6 -0
  58. package/src/planner/scope.mjs +5 -1
  59. package/src/tasks/packet/parse-prompt.mjs +31 -1
  60. package/src/tasks/packet/validate-contract.mjs +108 -0
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Primary task failure headlines and commands (SP-421 / GitHub #74).
3
+ */
4
+
5
+ export const PRIMARY_TASK_FAILURE_EXIT_REASONS = new Set([
6
+ "DirtyWorktree",
7
+ "GitignoredDirtyWorktree",
8
+ "lane_commit_failed",
9
+ "review_exhausted",
10
+ "contract_failed",
11
+ "needs_replan",
12
+ "prompt_parse_failed",
13
+ ]);
14
+
15
+ const LANE_COMMIT_EXIT_REASONS = new Set(["DirtyWorktree", "lane_commit_failed"]);
16
+
17
+ /**
18
+ * @param {string|null|undefined} exitReason
19
+ */
20
+ export function isPrimaryTaskFailureExitReason(exitReason) {
21
+ return PRIMARY_TASK_FAILURE_EXIT_REASONS.has(exitReason ?? "");
22
+ }
23
+
24
+ /**
25
+ * @param {string} batchLabel
26
+ * @param {object} ctx
27
+ * @param {string|null} [ctx.exitReason]
28
+ * @param {string|null} [ctx.failedTaskId]
29
+ * @param {boolean} [ctx.mergeGitignoredFailure]
30
+ */
31
+ export function buildPrimaryFailureHeadline(batchLabel, ctx = {}) {
32
+ if (LANE_COMMIT_EXIT_REASONS.has(ctx.exitReason ?? "")) {
33
+ if (ctx.exitReason === "GitignoredDirtyWorktree" || ctx.mergeGitignoredFailure) {
34
+ return ctx.failedTaskId
35
+ ? `${batchLabel} task ${ctx.failedTaskId} left gitignored dirty files on the lane branch`
36
+ : `${batchLabel} lane commit refused gitignored-only dirty files`;
37
+ }
38
+ if (ctx.exitReason === "DirtyWorktree") {
39
+ return ctx.failedTaskId
40
+ ? `${batchLabel} task ${ctx.failedTaskId} left dirty lane worktree after completion — clean coverage artifacts, then retry`
41
+ : `${batchLabel} lane worktree dirty after completion — clean coverage artifacts, then retry`;
42
+ }
43
+ return ctx.failedTaskId
44
+ ? `${batchLabel} task ${ctx.failedTaskId} completed but lane commit failed`
45
+ : `${batchLabel} completed but lane commit failed`;
46
+ }
47
+ if (ctx.exitReason === "review_exhausted") {
48
+ return ctx.failedTaskId
49
+ ? `${batchLabel} task ${ctx.failedTaskId} exhausted final review attempts — fix scope or implementation, then retry`
50
+ : `${batchLabel} review attempts exhausted — fix scope or implementation, then retry`;
51
+ }
52
+ if (ctx.exitReason === "contract_failed") {
53
+ return ctx.failedTaskId
54
+ ? `${batchLabel} task ${ctx.failedTaskId} failed contract verification — fix PROMPT.md scope, then retry`
55
+ : `${batchLabel} contract verification failed — fix PROMPT.md scope, then retry`;
56
+ }
57
+ return null;
58
+ }
59
+
60
+ /**
61
+ * @param {object} ctx
62
+ * @param {string|null} [ctx.exitReason]
63
+ * @param {string|null} [ctx.failedTaskId]
64
+ * @param {string} [ctx.tasksRoot]
65
+ */
66
+ export function buildPrimaryFailureSuggestedCommand(ctx = {}) {
67
+ if (LANE_COMMIT_EXIT_REASONS.has(ctx.exitReason ?? "")) {
68
+ if (ctx.exitReason === "DirtyWorktree") {
69
+ return ctx.failedTaskId
70
+ ? `git checkout -- extension/coverage && spine batch retry ${ctx.failedTaskId}`
71
+ : "git checkout -- extension/coverage && spine batch retry";
72
+ }
73
+ return ctx.failedTaskId
74
+ ? `spine batch retry ${ctx.failedTaskId}`
75
+ : "spine status --diagnose";
76
+ }
77
+ if (ctx.exitReason === "review_exhausted") {
78
+ return ctx.failedTaskId
79
+ ? `spine batch retry ${ctx.failedTaskId}`
80
+ : "spine status --diagnose";
81
+ }
82
+ if (ctx.exitReason === "contract_failed") {
83
+ const tasksRoot = ctx.tasksRoot ?? "spine-tasks";
84
+ return ctx.failedTaskId
85
+ ? `edit ${tasksRoot}/${ctx.failedTaskId}/PROMPT.md then spine batch retry ${ctx.failedTaskId}`
86
+ : "spine status --diagnose";
87
+ }
88
+ return null;
89
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Running diagnosis headlines when batch phase is active but no workers are scheduled (GitHub #68).
3
+ */
4
+ import { isRunningWithoutActiveWorkers } from "../dashboard/running-tail-state.mjs";
5
+ import { macroPhaseLabel } from "./macro-phase.mjs";
6
+
7
+ export { isRunningWithoutActiveWorkers };
8
+
9
+ /**
10
+ * @param {string} batchLabel
11
+ * @param {object} ctx
12
+ * @returns {string|null}
13
+ */
14
+ export function buildRunningTailHeadline(batchLabel, ctx = {}) {
15
+ if (!isRunningWithoutActiveWorkers(ctx)) {
16
+ return null;
17
+ }
18
+
19
+ const macroPhase = ctx.macroPhase ?? null;
20
+
21
+ if (ctx.postMergeLimbo || macroPhase === "gating") {
22
+ if (ctx.integrateGateOpen) {
23
+ return `${batchLabel} gate opened — approve to continue land loop`;
24
+ }
25
+ if (ctx.stalePathSpine) {
26
+ return `${batchLabel} merged but gate not opened — use node bin/spine.mjs batch resume --attached (PATH spine may be stale)`;
27
+ }
28
+ return `${batchLabel} finalizing land loop — opening integrate gate…`;
29
+ }
30
+
31
+ if (macroPhase === "integrating") {
32
+ return `${batchLabel} integrating orch branch to main…`;
33
+ }
34
+
35
+ if (macroPhase === "merging" || ctx.phase === "merging") {
36
+ return `${batchLabel} tasks done — merging lane branches…`;
37
+ }
38
+
39
+ if (macroPhase === "reviewing") {
40
+ return `${batchLabel} running reviews — no workers scheduled`;
41
+ }
42
+
43
+ if (macroPhase === "planning") {
44
+ return `${batchLabel} planning — no workers scheduled`;
45
+ }
46
+
47
+ if (macroPhase != null && macroPhase !== "executing") {
48
+ const label = macroPhaseLabel(macroPhase);
49
+ return `${batchLabel} ${label.toLowerCase()} — finalizing batch`;
50
+ }
51
+
52
+ if (ctx.gitMerged === false || ctx.allTasksTerminalSuccess === true) {
53
+ return `${batchLabel} tasks done — merging lane branches…`;
54
+ }
55
+
56
+ return `${batchLabel} finalizing batch — no active workers`;
57
+ }
@@ -17,18 +17,25 @@ import {
17
17
  summarizeMergeFailures,
18
18
  } from "./diagnosis-merge-failure.mjs";
19
19
  export { buildMergeFailureHeadline, summarizeMergeFailures } from "./diagnosis-merge-failure.mjs";
20
+ import { buildRunningTailHeadline } from "./diagnosis-tail-state.mjs";
21
+ export { buildRunningTailHeadline, isRunningWithoutActiveWorkers } from "./diagnosis-tail-state.mjs";
20
22
  import {
21
23
  buildStubFailureHeadline,
22
24
  buildStubFailureSuggestedCommand,
23
25
  STUB_EXIT_REASONS,
24
26
  } from "./diagnosis-stub.mjs";
27
+ /** @typedef {import("./reconcile.mjs").ReconciliationResult} ReconciliationResult */
25
28
  export {
26
29
  classifyTaskDoneSemantics,
27
30
  resolveTaskFolderPath,
28
31
  resolveTaskLaneWorktreePath,
29
32
  TASK_DONE_FLAG_FIELD_NAMES,
30
33
  } from "./diagnosis-task-done.mjs";
31
- /** @typedef {import("./reconcile.mjs").ReconciliationResult} ReconciliationResult */
34
+ import {
35
+ buildPrimaryFailureHeadline,
36
+ buildPrimaryFailureSuggestedCommand,
37
+ } from "./diagnosis-primary-failure.mjs";
38
+ export { inferLaunchFailureFromWorkerOutputTail, inferLaunchFailureKind } from "./diagnosis-launch-failure.mjs";
32
39
 
33
40
  export const DIAGNOSIS_TAXONOMY = [
34
41
  "running",
@@ -49,7 +56,6 @@ export const DIAGNOSIS_TAXONOMY = [
49
56
  ];
50
57
 
51
58
  const NO_PAUSE_DIAGNOSES = new Set(["limbo_stale", "completed_manual", "needs_integrate", "engine_orphaned"]);
52
- const LANE_COMMIT_EXIT_REASONS = new Set(["DirtyWorktree", "lane_commit_failed"]);
53
59
  const GITIGNORED_MERGE_FAILURE_CLASSES = new Set(["merge_failed_gitignored", "GitignoredDirtyWorktree"]);
54
60
  const REVIEW_SPAWN_FAILURE_EXIT_REASONS = new Set([
55
61
  "code_review_spawn_failed",
@@ -58,66 +64,6 @@ const REVIEW_SPAWN_FAILURE_EXIT_REASONS = new Set([
58
64
  "final_review_timeout",
59
65
  ]);
60
66
 
61
- /**
62
- * @param {string} haystack
63
- * @param {object} [hints]
64
- * @param {boolean} [hints.launchingPhase]
65
- * @returns {string|null}
66
- */
67
- function classifyLaunchFailureHaystack(haystack, hints = {}) {
68
- const normalized = haystack.toLowerCase();
69
-
70
- if (hints.launchingPhase) {
71
- if (normalized.includes("pi_spine_root") || normalized.includes("config_pi_spine_root")) {
72
- return "pi_spine_root";
73
- }
74
- return "launch_failed";
75
- }
76
-
77
- if (normalized.includes("pi_spine_root") || normalized.includes("config_pi_spine_root")) {
78
- return "pi_spine_root";
79
- }
80
-
81
- if (
82
- normalized.includes("not a git repository") ||
83
- normalized.includes("worktree") ||
84
- normalized.includes("worktreeunhealthy")
85
- ) {
86
- return "worktree_unhealthy";
87
- }
88
-
89
- return null;
90
- }
91
-
92
- /**
93
- * @param {object|null} failedEvent
94
- * @param {string|null} exitReason
95
- * @returns {string|null}
96
- */
97
- function classifyLaunchFailureFromEvent(failedEvent, exitReason) {
98
- const payload = failedEvent?.payload && typeof failedEvent.payload === "object" ? failedEvent.payload : {};
99
- const haystack = [
100
- exitReason,
101
- payload.classification,
102
- payload.output,
103
- payload.exitReason,
104
- ].filter(Boolean).join("\n");
105
-
106
- return classifyLaunchFailureHaystack(haystack, {
107
- launchingPhase:
108
- payload.workerPhase === "launching" || payload.classification === "launch_failed",
109
- });
110
- }
111
-
112
- /**
113
- * @param {string|null|undefined} outputText
114
- * @returns {string|null}
115
- */
116
- export function inferLaunchFailureFromWorkerOutputTail(outputText) {
117
- if (!outputText || !String(outputText).trim()) return null;
118
- return classifyLaunchFailureHaystack(String(outputText));
119
- }
120
-
121
67
  /**
122
68
  * @param {object} [ctx]
123
69
  * @param {string|null} [ctx.exitReason]
@@ -166,46 +112,6 @@ export function buildGitignoredMergeRepairCommand(taskBranch, gitignoredPaths) {
166
112
  return `${branchHint} && ${pathHint} && spine batch resume --force`;
167
113
  }
168
114
 
169
- /**
170
- * @param {object} ctx
171
- * @param {string|null} [ctx.exitReason]
172
- * @param {string|null} [ctx.launchFailureKind]
173
- * @param {object[]} [ctx.journalEvents]
174
- * @param {string|null} [ctx.failedTaskId]
175
- * @returns {string|null}
176
- */
177
- export function inferLaunchFailureKind(ctx = {}) {
178
- const { exitReason, journalEvents, failedTaskId } = ctx;
179
- if (exitReason === "launch_failed" || exitReason === "worker_launch_failed") {
180
- return "launch_failed";
181
- }
182
-
183
- const kinds = [];
184
- if (Array.isArray(journalEvents)) {
185
- const taskFailedEvents = journalEvents.filter((event) => event.type === "task.failed");
186
- const prioritized = failedTaskId
187
- ? [
188
- ...taskFailedEvents.filter(
189
- (event) => (event.taskId ?? event.payload?.taskId) === failedTaskId,
190
- ),
191
- ...taskFailedEvents.filter(
192
- (event) => (event.taskId ?? event.payload?.taskId) !== failedTaskId,
193
- ),
194
- ]
195
- : taskFailedEvents;
196
-
197
- for (const failedEvent of prioritized) {
198
- const kind = classifyLaunchFailureFromEvent(failedEvent, exitReason);
199
- if (kind) kinds.push(kind);
200
- }
201
- }
202
-
203
- if (kinds.includes("pi_spine_root")) return "pi_spine_root";
204
- if (kinds.includes("launch_failed")) return "launch_failed";
205
- if (kinds.includes("worktree_unhealthy")) return "worktree_unhealthy";
206
- return null;
207
- }
208
-
209
115
  /**
210
116
  * @param {string} diagnosis
211
117
  * @param {object} [ctx]
@@ -240,6 +146,10 @@ export function buildSuggestedCommand(diagnosis, ctx = {}) {
240
146
  if (STUB_EXIT_REASONS.has(ctx.exitReason ?? "")) {
241
147
  return buildStubFailureSuggestedCommand(ctx);
242
148
  }
149
+ {
150
+ const primaryCommand = buildPrimaryFailureSuggestedCommand(ctx);
151
+ if (primaryCommand) return primaryCommand;
152
+ }
243
153
  if (ctx.launchFailureKind === "pi_spine_root" || ctx.launchFailureKind === "launch_failed") {
244
154
  return "spine doctor";
245
155
  }
@@ -358,22 +268,16 @@ export function buildHeadline(diagnosis, ctx = {}) {
358
268
  if (STUB_EXIT_REASONS.has(ctx.exitReason ?? "")) {
359
269
  return buildStubFailureHeadline(batchLabel, ctx);
360
270
  }
271
+ {
272
+ const primaryHeadline = buildPrimaryFailureHeadline(batchLabel, ctx);
273
+ if (primaryHeadline) return primaryHeadline;
274
+ }
361
275
  if (ctx.launchFailureKind === "pi_spine_root" || ctx.launchFailureKind === "launch_failed") {
362
276
  return `${batchLabel} failed at worker launch — fix PI_SPINE_ROOT/devcontainer, then retry`;
363
277
  }
364
278
  if (ctx.launchFailureKind === "worktree_unhealthy") {
365
279
  return `${batchLabel} failed at worker launch — repair lane worktree git, then retry`;
366
280
  }
367
- if (LANE_COMMIT_EXIT_REASONS.has(ctx.exitReason ?? "")) {
368
- if (ctx.exitReason === "GitignoredDirtyWorktree" || ctx.mergeGitignoredFailure) {
369
- return ctx.failedTaskId
370
- ? `${batchLabel} task ${ctx.failedTaskId} left gitignored dirty files on the lane branch`
371
- : `${batchLabel} lane commit refused gitignored-only dirty files`;
372
- }
373
- return ctx.failedTaskId
374
- ? `${batchLabel} task ${ctx.failedTaskId} completed but lane commit failed`
375
- : `${batchLabel} completed but lane commit failed`;
376
- }
377
281
  if (REVIEW_SPAWN_FAILURE_EXIT_REASONS.has(ctx.exitReason ?? "")) {
378
282
  const reviewKind =
379
283
  ctx.exitReason === "final_review_spawn_failed" ||
@@ -448,8 +352,13 @@ export function buildHeadline(diagnosis, ctx = {}) {
448
352
  return ctx.failedTaskId
449
353
  ? `Task ${ctx.failedTaskId} needs replan — edit PROMPT.md before retry`
450
354
  : `${batchLabel} has tasks needing replan — edit PROMPT.md before retry`;
451
- case "running":
355
+ case "running": {
356
+ const tailHeadline = buildRunningTailHeadline(batchLabel, ctx);
357
+ if (tailHeadline) {
358
+ return tailHeadline;
359
+ }
452
360
  return `${batchLabel} is running`;
361
+ }
453
362
  case "paused": {
454
363
  const pending = ctx.pendingTaskCount ?? 0;
455
364
  if (pending > 1) {
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Lane commit and post-commit dirty validation (SP-427 / #73).
3
+ */
4
+
5
+ import { appendJournalEvent } from "../journal.mjs";
6
+ import { commitLaneWorktree, gitPorcelain } from "../lane-commit.mjs";
7
+ import {
8
+ resolvePostLaneCommitPorcelain,
9
+ sanitizeOutOfScopeCoverageBeforeLaneCommit,
10
+ } from "../lane-dirty-check.mjs";
11
+ import {
12
+ recomputeTaskCounters,
13
+ saveSpineBatchState,
14
+ updateSegmentForTask,
15
+ } from "../state.mjs";
16
+ import { recordLaneTaskMetric } from "./queue.mjs";
17
+
18
+ const DIRTY_AFTER_COMMIT_MESSAGE =
19
+ "Lane worktree still has uncommitted changes after auto-commit — commit manually or fix worker output";
20
+
21
+ /**
22
+ * @param {object} params
23
+ * @param {string} params.worktreePath
24
+ * @param {string} params.taskBranch
25
+ * @param {string} params.taskId
26
+ * @param {string} params.batchId
27
+ * @param {string} params.taskFolder
28
+ * @param {string} [params.projectRoot]
29
+ * @param {string[]} [params.fileScopePaths]
30
+ * @param {string[]} [params.ignorePatterns]
31
+ * @param {object} params.task
32
+ * @param {object} params.lane
33
+ * @param {number} params.laneNumber
34
+ * @param {string} params.laneCorrelationId
35
+ * @param {object} params.state
36
+ * @param {object} params.config
37
+ * @returns {{ ok: true, laneCommit: object } | { ok: false, error: string, output?: string }}
38
+ */
39
+ export function commitLaneAndValidateWorktree({
40
+ worktreePath,
41
+ taskBranch,
42
+ taskId,
43
+ batchId,
44
+ taskFolder,
45
+ projectRoot,
46
+ fileScopePaths,
47
+ ignorePatterns = [],
48
+ task,
49
+ lane,
50
+ laneNumber,
51
+ laneCorrelationId,
52
+ state,
53
+ config,
54
+ }) {
55
+ const preCommitPorcelain = gitPorcelain(worktreePath);
56
+ sanitizeOutOfScopeCoverageBeforeLaneCommit(worktreePath, fileScopePaths, {
57
+ projectRoot,
58
+ porcelain: preCommitPorcelain,
59
+ });
60
+
61
+ const laneCommit = commitLaneWorktree({
62
+ worktreePath,
63
+ taskBranch,
64
+ taskId,
65
+ batchId,
66
+ taskFolder,
67
+ projectRoot,
68
+ });
69
+ if (!laneCommit.ok) {
70
+ task.status = "failed";
71
+ task.endedAt = Date.now();
72
+ task.exitReason = laneCommit.failureClass ?? "lane_commit_failed";
73
+ updateSegmentForTask(state, taskId, "failed");
74
+ recomputeTaskCounters(state);
75
+ saveSpineBatchState(projectRoot, state);
76
+ appendJournalEvent(projectRoot, batchId, "task.failed", {
77
+ taskId,
78
+ laneNumber,
79
+ laneId: lane.laneId,
80
+ correlationId: laneCorrelationId,
81
+ classification: laneCommit.failureClass ?? "lane_commit_failed",
82
+ exitCode: 1,
83
+ output: laneCommit.error,
84
+ gitignoredPaths: laneCommit.gitignoredPaths ?? null,
85
+ });
86
+ recordLaneTaskMetric({
87
+ projectRoot,
88
+ batchId,
89
+ task,
90
+ config,
91
+ taskFolder,
92
+ });
93
+ return {
94
+ ok: false,
95
+ error: "lane_commit_failed",
96
+ output: laneCommit.error,
97
+ };
98
+ }
99
+
100
+ if (laneCommit.committed) {
101
+ appendJournalEvent(projectRoot, batchId, "lane.committed", {
102
+ taskId,
103
+ laneNumber,
104
+ commitSha: laneCommit.commitSha,
105
+ });
106
+ }
107
+
108
+ const remainingDirty = resolvePostLaneCommitPorcelain(worktreePath, {
109
+ fileScopePaths,
110
+ ignorePatterns,
111
+ projectRoot,
112
+ porcelain: gitPorcelain(worktreePath),
113
+ });
114
+ if (remainingDirty) {
115
+ task.status = "failed";
116
+ task.endedAt = Date.now();
117
+ task.exitReason = "DirtyWorktree";
118
+ updateSegmentForTask(state, taskId, "failed");
119
+ recomputeTaskCounters(state);
120
+ saveSpineBatchState(projectRoot, state);
121
+ appendJournalEvent(projectRoot, batchId, "task.failed", {
122
+ taskId,
123
+ laneNumber,
124
+ laneId: lane.laneId,
125
+ correlationId: laneCorrelationId,
126
+ classification: "DirtyWorktree",
127
+ exitCode: 1,
128
+ output: DIRTY_AFTER_COMMIT_MESSAGE,
129
+ });
130
+ recordLaneTaskMetric({
131
+ projectRoot,
132
+ batchId,
133
+ task,
134
+ config,
135
+ taskFolder,
136
+ });
137
+ return {
138
+ ok: false,
139
+ error: "dirty_after_lane_commit",
140
+ output: DIRTY_AFTER_COMMIT_MESSAGE,
141
+ };
142
+ }
143
+
144
+ return { ok: true, laneCommit };
145
+ }
@@ -7,6 +7,7 @@ import path from "node:path";
7
7
  import { recordLaneTaskMetric } from "./queue.mjs";
8
8
  import { REVIEW_DEFAULTS } from "../../config/defaults.mjs";
9
9
  import { parseContract } from "../../tasks/packet/parse-prompt.mjs";
10
+ import { resolveTaskStartCommit } from "../contract-task-start.mjs";
10
11
  import { shouldRunContractVerify, shouldRunContractVerifyForWorker, verifyContract } from "../contract-verify.mjs";
11
12
  import { appendJournalEvent, readJournalEvents } from "../journal.mjs";
12
13
  import {
@@ -264,6 +265,56 @@ function removeDoneFile(taskFolder) {
264
265
  }
265
266
  }
266
267
 
268
+ /**
269
+ * @param {object} params
270
+ */
271
+ function recordContractVerifyTaskFailure({
272
+ projectRoot,
273
+ state,
274
+ batchId,
275
+ task,
276
+ lane,
277
+ laneCorrelationId,
278
+ contractVerifyResult,
279
+ config,
280
+ taskFolder,
281
+ }) {
282
+ const taskId = task.taskId;
283
+ const laneNumber = lane.laneNumber;
284
+ task.status = "failed";
285
+ task.endedAt = Date.now();
286
+ task.exitReason = "contract_failed";
287
+ task.contractOk = false;
288
+ updateSegmentForTask(state, taskId, "failed");
289
+ recomputeTaskCounters(state);
290
+ saveSpineBatchState(projectRoot, state);
291
+ appendJournalEvent(projectRoot, batchId, "contract.failed", {
292
+ taskId,
293
+ laneNumber,
294
+ laneId: lane.laneId,
295
+ correlationId: laneCorrelationId,
296
+ checks: contractVerifyResult?.checks ?? [],
297
+ });
298
+ appendJournalEvent(projectRoot, batchId, "task.failed", {
299
+ taskId,
300
+ laneNumber,
301
+ laneId: lane.laneId,
302
+ correlationId: laneCorrelationId,
303
+ classification: "contract_failed",
304
+ exitCode: 1,
305
+ contractOk: false,
306
+ failureKind: "contract",
307
+ });
308
+ recordLaneTaskMetric({
309
+ projectRoot,
310
+ batchId,
311
+ task,
312
+ config,
313
+ taskFolder,
314
+ lane,
315
+ });
316
+ }
317
+
267
318
  /**
268
319
  * @param {object} params
269
320
  */
@@ -636,9 +687,18 @@ async function runFinalReviewPhase({
636
687
  const promptMarkdown = fs.readFileSync(path.join(taskFolderInWorktree, "PROMPT.md"), "utf-8");
637
688
  const parsedContract = parseContract(promptMarkdown);
638
689
  if (shouldRunContractVerifyForWorker(promptMarkdown, parsedContract, config)) {
690
+ const journalEvents = readJournalEvents(projectRoot, batchId);
691
+ const sinceCommit = resolveTaskStartCommit({
692
+ journal: journalEvents,
693
+ taskId,
694
+ laneId: lane.laneId,
695
+ batchId,
696
+ worktreePath: wt,
697
+ });
639
698
  contractVerifyResult = verifyContract(wt, parsedContract, {
640
699
  ...config,
641
700
  baseBranch,
701
+ sinceCommit: sinceCommit ?? undefined,
642
702
  });
643
703
  task.contractOk = contractVerifyResult.ok;
644
704
  saveSpineBatchState(projectRoot, state);
@@ -651,99 +711,19 @@ async function runFinalReviewPhase({
651
711
  checks: contractVerifyResult.checks,
652
712
  });
653
713
  if (!contractVerifyResult.ok) {
654
- finalAttempt++;
655
- task.finalAttempts = finalAttempt;
656
- saveSpineBatchState(projectRoot, state);
657
- appendJournalEvent(projectRoot, batchId, "task.verdict_recorded", {
658
- taskId,
659
- laneNumber,
660
- laneId: lane.laneId,
661
- correlationId: laneCorrelationId,
662
- reviewType: "final",
663
- verdict: "REVISE",
664
- feedback: contractVerifyResult.checks
665
- .filter((check) => !check.ok)
666
- .map((check) => check.message)
667
- .join("; "),
668
- finalAttempt,
669
- contractOk: false,
670
- });
671
-
672
- if (finalAttempt >= maxFinalAttempts) {
673
- removeDoneFile(taskFolderInWorktree);
674
- appendJournalEvent(projectRoot, batchId, "review.exhausted", {
675
- taskId,
676
- laneNumber,
677
- laneId: lane.laneId,
678
- correlationId: laneCorrelationId,
679
- finalAttempt,
680
- maxFinalAttempts,
681
- });
682
- recordFinalReviewTaskFailure({
683
- projectRoot,
684
- state,
685
- batchId,
686
- task,
687
- lane,
688
- laneCorrelationId,
689
- exitReason: "review_exhausted",
690
- verdict: "REVISE",
691
- finalAttempt,
692
- config,
693
- taskFolder: taskFolderInWorktree,
694
- });
695
- return { ok: false, exitReason: "review_exhausted", verdict: "REVISE" };
696
- }
697
-
698
714
  removeDoneFile(taskFolderInWorktree);
699
- const reworkResult = await runWorker({
700
- worktreePath: wt,
701
- taskFolder: taskFolderInWorktree,
715
+ recordContractVerifyTaskFailure({
702
716
  projectRoot,
717
+ state,
703
718
  batchId,
704
- laneNumber,
705
- taskId,
706
- laneBranch: taskBranch,
719
+ task,
720
+ lane,
707
721
  laneCorrelationId,
708
- fileScopePaths,
722
+ contractVerifyResult,
709
723
  config,
710
- onHeartbeat: (timestamp) => {
711
- lane.lastHeartbeatAt = timestamp;
712
- saveSpineBatchState(projectRoot, state);
713
- },
714
- onWorkerPid: (pid) => {
715
- if (pid > 0) {
716
- lane.workerPid = pid;
717
- saveSpineBatchState(projectRoot, state);
718
- }
719
- },
720
- });
721
- if (!reworkResult.ok) {
722
- const aborted = reworkResult.classification === "aborted";
723
- task.status = aborted ? "aborted" : "failed";
724
- task.endedAt = Date.now();
725
- task.exitReason = reworkResult.classification ?? "worker_failed";
726
- updateSegmentForTask(state, taskId, aborted ? "aborted" : "failed");
727
- recomputeTaskCounters(state);
728
- saveSpineBatchState(projectRoot, state);
729
- recordLaneTaskMetric({
730
- projectRoot,
731
- batchId,
732
- task,
733
- config,
734
- taskFolder: taskFolderInWorktree,
735
- });
736
- return { ok: false, aborted, workerResult: reworkResult };
737
- }
738
-
739
- appendJournalEvent(projectRoot, batchId, "lane.completed", {
740
- laneNumber,
741
- laneId: lane.laneId,
742
- taskId,
743
- correlationId: laneCorrelationId,
744
- phase: "final_rework",
724
+ taskFolder: taskFolderInWorktree,
745
725
  });
746
- continue;
726
+ return { ok: false, exitReason: "contract_failed", verdict: "CONTRACT_FAIL" };
747
727
  }
748
728
  }
749
729