pi-spine 2.25.0 → 2.27.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 +1 -1
- package/bin/spine-review-step.mjs +8 -1
- package/bin/spine-worker-runner.mjs +14 -1
- package/package.json +3 -3
- package/skills/spine-release-operator/SKILL.md +3 -0
- package/src/batch/abort.mjs +74 -38
- package/src/batch/attached-engine-handoff.mjs +2 -2
- package/src/batch/attached-runner-promote.mjs +1 -1
- package/src/batch/attached-runner-reconcile.mjs +1 -1
- package/src/batch/batch-meta-reconstruct.mjs +9 -1
- package/src/batch/batch-state-lock.mjs +173 -31
- package/src/batch/contract-exec.mjs +48 -47
- package/src/batch/contract-parse.mjs +2 -2
- package/src/batch/contract-prelanded.mjs +2 -2
- package/src/batch/contract-spawn.mjs +225 -0
- package/src/batch/contract-verify.mjs +2 -1
- package/src/batch/detached-wait.mjs +1 -1
- package/src/batch/diagnosis-handoff-packet.mjs +74 -0
- package/src/batch/diagnosis-pending-lane.mjs +34 -1
- package/src/batch/engine-lanes/matrix-run.mjs +6 -6
- package/src/batch/engine-lanes/merge.mjs +135 -34
- package/src/batch/engine-lanes/quota-fallback-run.mjs +153 -0
- package/src/batch/engine-lanes/quota-fallback-state.mjs +202 -0
- package/src/batch/engine-lanes/review-code.mjs +1 -0
- package/src/batch/engine-lanes/review-final.mjs +83 -46
- package/src/batch/engine-lanes/review-plan.mjs +22 -2
- package/src/batch/engine-lanes/review-poll.mjs +47 -1
- package/src/batch/engine-lanes.mjs +17 -16
- package/src/batch/engine.mjs +10 -10
- package/src/batch/journal.mjs +15 -0
- package/src/batch/lifecycle-archive.mjs +76 -1
- package/src/batch/lifecycle-cleanup.mjs +110 -0
- package/src/batch/lifecycle.mjs +100 -72
- package/src/batch/metrics.mjs +13 -1
- package/src/batch/parent-session-monitor.mjs +77 -70
- package/src/batch/pause.mjs +92 -25
- package/src/batch/post-merge-limbo.mjs +35 -5
- package/src/batch/provider-quota.mjs +220 -0
- package/src/batch/quota-fallback.mjs +319 -0
- package/src/batch/reconcile-diagnosis-context.mjs +22 -0
- package/src/batch/resume-gate-reopen.mjs +24 -8
- package/src/batch/resume-multi-queue.mjs +25 -10
- package/src/batch/resume-multi.mjs +1 -1
- package/src/batch/resume.mjs +1 -1
- package/src/batch/review-artifacts.mjs +93 -15
- package/src/batch/review-spawn-retry.mjs +106 -0
- package/src/batch/review-spawn.mjs +48 -2
- package/src/batch/review-step-run.mjs +96 -6
- package/src/batch/reviewer-output.mjs +162 -0
- package/src/batch/sequence-run.mjs +26 -8
- package/src/batch/sequence-wait.mjs +87 -8
- package/src/batch/state-guards.mjs +39 -7
- package/src/batch/state-io.mjs +131 -17
- package/src/batch/state.mjs +1 -0
- package/src/batch/worker-host.mjs +81 -7
- package/src/config/cursor-rules/discover.mjs +2 -2
- package/src/config/cursor-rules/match-globs.mjs +6 -4
- package/src/config/defaults.mjs +15 -1
- package/src/config/env-overrides.mjs +41 -4
- package/src/config/preflight/git-batch.mjs +47 -0
- package/src/config/settings-fields.mjs +6 -0
- package/src/config/spine-config-schema.mjs +91 -1
- package/src/doctor/quota-risk.mjs +45 -0
- package/src/fs/atomic-write.mjs +37 -1
- package/src/planner/file-scope.mjs +6 -6
- package/src/tasks/packet/validate-contract.mjs +3 -3
package/src/batch/journal.mjs
CHANGED
|
@@ -348,6 +348,17 @@ export function summarizeJournalEvent(event) {
|
|
|
348
348
|
if (payload.reason) parts.push(String(payload.reason));
|
|
349
349
|
if (payload.error) parts.push(String(payload.error).slice(0, 80));
|
|
350
350
|
if (payload.classification) parts.push(String(payload.classification));
|
|
351
|
+
// SP-810 (partial #329): quota-fallback fields operators act on.
|
|
352
|
+
if (payload.fromProfile && payload.toProfile) parts.push(`${payload.fromProfile} → ${payload.toProfile}`);
|
|
353
|
+
if (payload.poolId) parts.push(`pool ${payload.poolId}`);
|
|
354
|
+
if (Array.isArray(payload.exhaustedPools) && payload.exhaustedPools.length > 0) parts.push(`exhausted: ${payload.exhaustedPools.join(", ")}`);
|
|
355
|
+
const quotaReset = payload.resetAtRaw;
|
|
356
|
+
if (typeof quotaReset === "string" && quotaReset) {
|
|
357
|
+
parts.push(`resets ${quotaReset}`);
|
|
358
|
+
} else if (Array.isArray(quotaReset) && quotaReset.length > 0) {
|
|
359
|
+
parts.push(`resets ${quotaReset.map((reset) => reset ?? "unknown").join(" / ")}`);
|
|
360
|
+
}
|
|
361
|
+
if (Array.isArray(payload.taskIds) && payload.taskIds.length > 0) parts.push(`task(s): ${payload.taskIds.join(", ")}`);
|
|
351
362
|
if (payload.workerPhase) parts.push(`phase ${payload.workerPhase}`);
|
|
352
363
|
if (payload.dirtyPathCount != null && event.type === "lane.progress_snapshot") {
|
|
353
364
|
parts.push(`${payload.dirtyPathCount} dirty path(s)`);
|
|
@@ -428,6 +439,10 @@ export function extractJournalDiagnosisHints(events) {
|
|
|
428
439
|
"batch.merge_blocked",
|
|
429
440
|
"task.failed",
|
|
430
441
|
"matrix.task_failed",
|
|
442
|
+
// SP-810 (partial #329): quota events decide retry-vs-wait — rank with failures.
|
|
443
|
+
"batch.quota_fallback_applied",
|
|
444
|
+
"batch.quota_fallback_exhausted",
|
|
445
|
+
"worker.quota_exhausted",
|
|
431
446
|
"review.crash_recovered",
|
|
432
447
|
"review.skipped_fresh_artifact",
|
|
433
448
|
"review.resumed",
|
|
@@ -6,9 +6,84 @@
|
|
|
6
6
|
import fs from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { appendJournalEvent } from "./journal.mjs";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
clearActiveBatchStateIfMatches,
|
|
11
|
+
loadBatchStateFile,
|
|
12
|
+
readBatchStateId,
|
|
13
|
+
} from "./batch-state-io.mjs";
|
|
10
14
|
import { removeLaneWorktrees, maxLaneNumberFromBatchState } from "./worktree.mjs";
|
|
11
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Phases a resume/pause/engine writer can leave behind between a terminal
|
|
18
|
+
* write's pre-lock read and its lock section (SP-792 / #301). When the state
|
|
19
|
+
* re-read inside the lock holds one of these, the batch is active again and
|
|
20
|
+
* the pre-lock dismiss/complete decision no longer holds — fail closed.
|
|
21
|
+
* `executing` is excluded because it is a limbo phase that legitimately
|
|
22
|
+
* allows dismiss (limbo_stale).
|
|
23
|
+
*/
|
|
24
|
+
const ACTIVE_RESUME_PHASES = new Set(["planning", "running", "paused", "merging"]);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Re-read the active batch state inside the terminal lock section
|
|
28
|
+
* (SP-792 / #301) so archive, history, and clear act on the freshest on-disk
|
|
29
|
+
* snapshot instead of the pre-lock read, which goes stale when a concurrent
|
|
30
|
+
* engine save lands between the read and the lock.
|
|
31
|
+
*
|
|
32
|
+
* Fail-closed contract: returns a ready-to-return non-ok `refusal` when the
|
|
33
|
+
* state file vanished, no longer parses, now describes a different batch, or
|
|
34
|
+
* — with `requireTerminalPhase` — the batch became active again mid-write.
|
|
35
|
+
* Abort passes `requireTerminalPhase: false` because aborting a running
|
|
36
|
+
* batch is its normal use case.
|
|
37
|
+
*
|
|
38
|
+
* @param {object} params
|
|
39
|
+
* @param {string} params.projectRoot
|
|
40
|
+
* @param {string|null} params.batchStatePath path used by the pre-lock read
|
|
41
|
+
* @param {string} params.batchId batch id the reloaded state must still match
|
|
42
|
+
* @param {"abort"|"dismiss"|"complete"} params.action action name for the refusal headline
|
|
43
|
+
* @param {boolean} [params.requireTerminalPhase] re-validate the terminal-phase precondition on the in-lock state
|
|
44
|
+
* @param {boolean} [params.force] `dismiss --force` skips the phase re-validation
|
|
45
|
+
* @param {string|null} [params.diagnosis] pre-lock diagnosis echoed on refusal
|
|
46
|
+
* @returns {{ state: { path: string, raw: object }|null, refusal: object|null }}
|
|
47
|
+
*/
|
|
48
|
+
export function reloadStateForTerminalWrite({
|
|
49
|
+
projectRoot,
|
|
50
|
+
batchStatePath,
|
|
51
|
+
batchId,
|
|
52
|
+
action,
|
|
53
|
+
requireTerminalPhase = false,
|
|
54
|
+
force = false,
|
|
55
|
+
diagnosis = null,
|
|
56
|
+
}) {
|
|
57
|
+
const refuse = () => ({
|
|
58
|
+
ok: false,
|
|
59
|
+
exitCode: 1,
|
|
60
|
+
error: "batch_state_changed_during_terminal_write",
|
|
61
|
+
headline: `Batch state changed during ${action} — re-run spine status --diagnose`,
|
|
62
|
+
suggestedCommand: "spine status --diagnose",
|
|
63
|
+
diagnosis: diagnosis ?? null,
|
|
64
|
+
batchId,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const reloaded = loadBatchStateFile(projectRoot, batchStatePath);
|
|
68
|
+
if (!reloaded.path || !reloaded.raw || reloaded.parseError) {
|
|
69
|
+
return { state: null, refusal: refuse() };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const reloadedBatchId = readBatchStateId(reloaded.raw);
|
|
73
|
+
if (!reloadedBatchId || reloadedBatchId !== batchId) {
|
|
74
|
+
return { state: null, refusal: refuse() };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (requireTerminalPhase && !force) {
|
|
78
|
+
const phase = String(/** @type {{ phase?: string }} */ (reloaded.raw).phase ?? "");
|
|
79
|
+
if (ACTIVE_RESUME_PHASES.has(phase)) {
|
|
80
|
+
return { state: null, refusal: refuse() };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return { state: { path: reloaded.path, raw: reloaded.raw }, refusal: null };
|
|
85
|
+
}
|
|
86
|
+
|
|
12
87
|
/**
|
|
13
88
|
* @param {string} projectRoot
|
|
14
89
|
* @param {string} batchId
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Post-release cleanup for terminal batch lifecycle writes (SP-796 / #302).
|
|
3
|
+
*
|
|
4
|
+
* abort/complete/dismiss hold the global batch-state lock only for state
|
|
5
|
+
* I/O (in-lock reload, archive write, history append, terminal journal
|
|
6
|
+
* event, active-state clear). Post-mortem, metrics, worker termination and
|
|
7
|
+
* worktree cleanup run after the lock is released so concurrent engine
|
|
8
|
+
* saves are never blocked behind them.
|
|
9
|
+
*
|
|
10
|
+
* A failure in this phase must never throw: the batch state was already
|
|
11
|
+
* archived and journaled terminal, so an exception could only turn a
|
|
12
|
+
* finished batch into a reported failure. Each failed step is reported in
|
|
13
|
+
* the caller's `cleanupWarnings` and journaled as `batch.cleanup_failed`
|
|
14
|
+
* `{ step, error }` instead.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
18
|
+
import { appendJournalEvent } from "./journal.mjs";
|
|
19
|
+
import { cleanupBatchLaneWorktrees } from "./lifecycle-archive.mjs";
|
|
20
|
+
import { recordBatchTerminalMetric } from "./metrics.mjs";
|
|
21
|
+
import { writeBatchPostMortem } from "./postmortem.mjs";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Run named cleanup steps after the batch-state lock was released. Steps run
|
|
25
|
+
* in order; a failing step records a warning and journals `batch.cleanup_failed`
|
|
26
|
+
* but never stops the remaining steps or throws to the caller.
|
|
27
|
+
*
|
|
28
|
+
* @param {object} params
|
|
29
|
+
* @param {string} params.projectRoot
|
|
30
|
+
* @param {string} params.batchId
|
|
31
|
+
* @param {Array<{ step: string, run: () => void }>} params.steps
|
|
32
|
+
* @returns {string[]} `cleanupWarnings` — one `"<step>: <error>"` per failed step
|
|
33
|
+
*/
|
|
34
|
+
export function runPostReleaseCleanup({ projectRoot, batchId, steps }) {
|
|
35
|
+
const warnings = [];
|
|
36
|
+
for (const { step, run } of steps) {
|
|
37
|
+
try {
|
|
38
|
+
run();
|
|
39
|
+
} catch (err) {
|
|
40
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
41
|
+
warnings.push(`${step}: ${message}`);
|
|
42
|
+
try {
|
|
43
|
+
appendJournalEvent(projectRoot, batchId, "batch.cleanup_failed", {
|
|
44
|
+
step,
|
|
45
|
+
error: message,
|
|
46
|
+
});
|
|
47
|
+
} catch {
|
|
48
|
+
// Journal I/O failing while reporting a cleanup failure must not
|
|
49
|
+
// mask the terminal result — the warning string still carries it.
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return warnings;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Post-release cleanup shared by dismiss and complete (SP-796 / #302):
|
|
58
|
+
* post-mortem write, terminal metrics, and lane-worktree cleanup — all after
|
|
59
|
+
* the in-lock section archived the state and journaled the terminal event.
|
|
60
|
+
*
|
|
61
|
+
* @param {object} params
|
|
62
|
+
* @param {string} params.projectRoot
|
|
63
|
+
* @param {string} params.batchId
|
|
64
|
+
* @param {object} params.batchState the in-lock snapshot that was archived
|
|
65
|
+
* @param {import("./reconcile.mjs").ReconciliationResult} [params.reconciliation]
|
|
66
|
+
* @param {string} params.diagnosis
|
|
67
|
+
* @param {number} params.endedAt
|
|
68
|
+
* @returns {string[]} `cleanupWarnings`
|
|
69
|
+
*/
|
|
70
|
+
export function runLifecycleCleanupAfterRelease({
|
|
71
|
+
projectRoot,
|
|
72
|
+
batchId,
|
|
73
|
+
batchState,
|
|
74
|
+
reconciliation,
|
|
75
|
+
diagnosis,
|
|
76
|
+
endedAt,
|
|
77
|
+
}) {
|
|
78
|
+
return runPostReleaseCleanup({
|
|
79
|
+
projectRoot,
|
|
80
|
+
batchId,
|
|
81
|
+
steps: [
|
|
82
|
+
{
|
|
83
|
+
step: "post_mortem",
|
|
84
|
+
run: () => {
|
|
85
|
+
writeBatchPostMortem({ projectRoot, batchState, reconciliation });
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
step: "metrics",
|
|
90
|
+
run: () => {
|
|
91
|
+
const config = loadSpineConfig(projectRoot).config ?? {};
|
|
92
|
+
recordBatchTerminalMetric({
|
|
93
|
+
projectRoot,
|
|
94
|
+
batchId,
|
|
95
|
+
batchState: { ...batchState, endedAt },
|
|
96
|
+
diagnosis,
|
|
97
|
+
config,
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
step: "worktrees",
|
|
103
|
+
run: () => {
|
|
104
|
+
const config = loadSpineConfig(projectRoot).config ?? {};
|
|
105
|
+
cleanupBatchLaneWorktrees({ projectRoot, batchId, batchState, config });
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
});
|
|
110
|
+
}
|
package/src/batch/lifecycle.mjs
CHANGED
|
@@ -11,15 +11,14 @@ import {
|
|
|
11
11
|
} from "./diagnosis-pending-lane.mjs";
|
|
12
12
|
import {
|
|
13
13
|
archiveBatchState,
|
|
14
|
-
cleanupBatchLaneWorktrees,
|
|
15
14
|
clearCompletedBatchState,
|
|
15
|
+
reloadStateForTerminalWrite,
|
|
16
16
|
} from "./lifecycle-archive.mjs";
|
|
17
17
|
export { archiveBatchStatePath } from "./lifecycle-archive.mjs";
|
|
18
|
+
import { runLifecycleCleanupAfterRelease } from "./lifecycle-cleanup.mjs";
|
|
18
19
|
import { assertOrchIntegratable } from "./integrate.mjs";
|
|
19
|
-
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
20
20
|
import { appendJournalEvent } from "./journal.mjs";
|
|
21
|
-
import {
|
|
22
|
-
import { writeBatchPostMortem } from "./postmortem.mjs";
|
|
21
|
+
import { postMortemRelPath } from "./postmortem.mjs";
|
|
23
22
|
import { appendBatchHistoryEntry, clearBatchEnginePid, saveSpineBatchState } from "./state.mjs";
|
|
24
23
|
import { withBatchStateLock } from "./batch-state-lock.mjs";
|
|
25
24
|
import { loadBatchStateFile, parseBatchState, reconcileBatch } from "./reconcile.mjs";
|
|
@@ -208,16 +207,29 @@ export function dismissBatch(ctx) {
|
|
|
208
207
|
});
|
|
209
208
|
}
|
|
210
209
|
// Terminal dismiss write section under the global batch-state lock
|
|
211
|
-
// (SP-722 / #264): archive, history entry,
|
|
212
|
-
// must not interleave with a concurrent
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
210
|
+
// (SP-722 / #264; SP-796 / #302): archive, history entry, journal event,
|
|
211
|
+
// and clearing the active state must not interleave with a concurrent
|
|
212
|
+
// engine save or resume writer. Post-mortem, metrics, and worktree
|
|
213
|
+
// cleanup run after the lock is released.
|
|
214
|
+
const inLock = withBatchStateLock(projectRoot, () => {
|
|
215
|
+
// SP-792 / #301: archive from the state re-read inside the lock; fail
|
|
216
|
+
// closed when the batch changed or became active again mid-dismiss.
|
|
217
|
+
const { state: fresh, refusal } = reloadStateForTerminalWrite({
|
|
216
218
|
projectRoot,
|
|
217
|
-
|
|
218
|
-
|
|
219
|
+
batchStatePath: loaded.path,
|
|
220
|
+
batchId,
|
|
221
|
+
action: "dismiss",
|
|
222
|
+
requireTerminalPhase: true,
|
|
223
|
+
force,
|
|
224
|
+
diagnosis,
|
|
219
225
|
});
|
|
226
|
+
if (refusal) return { refusal, archivePath: null, fresh: null, endedAt: 0 };
|
|
227
|
+
|
|
228
|
+
const archivePath = archiveBatchState(projectRoot, batchId, fresh.raw);
|
|
220
229
|
const endedAt = Date.now();
|
|
230
|
+
// Deterministic post-mortem location: the file itself is written after
|
|
231
|
+
// release (SP-796), but the history entry carries the exact path
|
|
232
|
+
// `writeBatchPostMortem` reports, so lookups keep working.
|
|
221
233
|
appendBatchHistoryEntry(projectRoot, {
|
|
222
234
|
batchId,
|
|
223
235
|
action: "dismissed",
|
|
@@ -225,42 +237,46 @@ export function dismissBatch(ctx) {
|
|
|
225
237
|
diagnosis,
|
|
226
238
|
reason: reason ?? null,
|
|
227
239
|
archivePath: path.relative(projectRoot, archivePath),
|
|
228
|
-
postMortemPath,
|
|
240
|
+
postMortemPath: postMortemRelPath(batchId),
|
|
229
241
|
});
|
|
230
242
|
appendJournalEvent(projectRoot, batchId, "batch.dismissed", {
|
|
231
243
|
diagnosis,
|
|
232
244
|
reason: reason ?? null,
|
|
233
245
|
archivePath: path.relative(projectRoot, archivePath),
|
|
234
246
|
});
|
|
235
|
-
|
|
236
|
-
const config = configResult.config ?? {};
|
|
237
|
-
recordBatchTerminalMetric({
|
|
238
|
-
projectRoot,
|
|
239
|
-
batchId,
|
|
240
|
-
batchState: { ...loaded.raw, endedAt },
|
|
241
|
-
diagnosis: diagnosis ?? "dismissed",
|
|
242
|
-
config,
|
|
243
|
-
});
|
|
244
|
-
cleanupBatchLaneWorktrees({
|
|
245
|
-
projectRoot,
|
|
246
|
-
batchId,
|
|
247
|
-
batchState: loaded.raw,
|
|
248
|
-
config,
|
|
249
|
-
});
|
|
250
|
-
clearCompletedBatchState(projectRoot, loaded.path, batchId);
|
|
247
|
+
clearCompletedBatchState(projectRoot, fresh.path, batchId);
|
|
251
248
|
bumpDashboardInvalidateSignal(projectRoot, "batch_dismiss", batchId);
|
|
252
249
|
|
|
253
|
-
return {
|
|
254
|
-
ok: true,
|
|
255
|
-
exitCode: 0,
|
|
256
|
-
batchId,
|
|
257
|
-
diagnosis,
|
|
258
|
-
headline: `Batch ${batchId} dismissed and archived`,
|
|
259
|
-
suggestedCommand: "spine preflight",
|
|
260
|
-
alternatives: ["spine plan all"],
|
|
261
|
-
archivePath,
|
|
262
|
-
};
|
|
250
|
+
return { refusal: null, archivePath, fresh, endedAt };
|
|
263
251
|
});
|
|
252
|
+
|
|
253
|
+
// Cleanup runs only when the in-lock section succeeded (archived and
|
|
254
|
+
// journaled `batch.dismissed`); a refusal archived nothing for this batch.
|
|
255
|
+
if (inLock.refusal) return inLock.refusal;
|
|
256
|
+
|
|
257
|
+
// SP-796 / #302: post-mortem, metrics, and worktree cleanup run after the
|
|
258
|
+
// lock is released; failures are reported as cleanupWarnings +
|
|
259
|
+
// `batch.cleanup_failed`, never thrown after the state was archived.
|
|
260
|
+
const cleanupWarnings = runLifecycleCleanupAfterRelease({
|
|
261
|
+
projectRoot,
|
|
262
|
+
batchId,
|
|
263
|
+
batchState: inLock.fresh.raw,
|
|
264
|
+
reconciliation,
|
|
265
|
+
diagnosis: diagnosis ?? "dismissed",
|
|
266
|
+
endedAt: inLock.endedAt,
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
ok: true,
|
|
271
|
+
exitCode: 0,
|
|
272
|
+
batchId,
|
|
273
|
+
diagnosis,
|
|
274
|
+
headline: `Batch ${batchId} dismissed and archived`,
|
|
275
|
+
suggestedCommand: "spine preflight",
|
|
276
|
+
alternatives: ["spine plan all"],
|
|
277
|
+
archivePath: inLock.archivePath,
|
|
278
|
+
cleanupWarnings,
|
|
279
|
+
};
|
|
264
280
|
}
|
|
265
281
|
|
|
266
282
|
/**
|
|
@@ -414,16 +430,25 @@ export function completeBatch(ctx) {
|
|
|
414
430
|
terminateSupervisorIfRunning(projectRoot, batchId, "batch_complete");
|
|
415
431
|
|
|
416
432
|
// Terminal complete write section under the global batch-state lock
|
|
417
|
-
// (SP-722 / #264)
|
|
418
|
-
//
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
433
|
+
// (SP-722 / #264; SP-796 / #302) — same split as dismiss: state I/O only,
|
|
434
|
+
// post-mortem/metrics/worktrees after release.
|
|
435
|
+
const inLock = withBatchStateLock(projectRoot, () => {
|
|
436
|
+
// SP-792 / #301: archive from the state re-read inside the lock; fail
|
|
437
|
+
// closed when the batch changed or became active again mid-complete.
|
|
438
|
+
const { state: fresh, refusal } = reloadStateForTerminalWrite({
|
|
422
439
|
projectRoot,
|
|
423
|
-
|
|
424
|
-
|
|
440
|
+
batchStatePath: loaded.path,
|
|
441
|
+
batchId,
|
|
442
|
+
action: "complete",
|
|
443
|
+
requireTerminalPhase: true,
|
|
425
444
|
});
|
|
445
|
+
if (refusal) return { refusal, archivePath: null, fresh: null, endedAt: 0 };
|
|
446
|
+
|
|
447
|
+
const archivePath = archiveBatchState(projectRoot, batchId, fresh.raw);
|
|
426
448
|
const endedAt = Date.now();
|
|
449
|
+
// Deterministic post-mortem location: the file itself is written after
|
|
450
|
+
// release (SP-796), but the history entry carries the exact path
|
|
451
|
+
// `writeBatchPostMortem` reports, so lookups keep working.
|
|
427
452
|
appendBatchHistoryEntry(projectRoot, {
|
|
428
453
|
batchId,
|
|
429
454
|
action: "completed",
|
|
@@ -431,39 +456,42 @@ export function completeBatch(ctx) {
|
|
|
431
456
|
diagnosis: "completed",
|
|
432
457
|
detectManualMerge,
|
|
433
458
|
archivePath: path.relative(projectRoot, archivePath),
|
|
434
|
-
postMortemPath,
|
|
459
|
+
postMortemPath: postMortemRelPath(batchId),
|
|
435
460
|
});
|
|
436
461
|
appendJournalEvent(projectRoot, batchId, "batch.completed", {
|
|
437
462
|
detectManualMerge,
|
|
438
463
|
archivePath: path.relative(projectRoot, archivePath),
|
|
439
464
|
lifecycle: "complete",
|
|
440
465
|
});
|
|
441
|
-
|
|
442
|
-
const config = configResult.config ?? {};
|
|
443
|
-
recordBatchTerminalMetric({
|
|
444
|
-
projectRoot,
|
|
445
|
-
batchId,
|
|
446
|
-
batchState: { ...loaded.raw, endedAt },
|
|
447
|
-
diagnosis: "completed",
|
|
448
|
-
config,
|
|
449
|
-
});
|
|
450
|
-
cleanupBatchLaneWorktrees({
|
|
451
|
-
projectRoot,
|
|
452
|
-
batchId,
|
|
453
|
-
batchState: loaded.raw,
|
|
454
|
-
config,
|
|
455
|
-
});
|
|
456
|
-
clearCompletedBatchState(projectRoot, loaded.path, batchId);
|
|
466
|
+
clearCompletedBatchState(projectRoot, fresh.path, batchId);
|
|
457
467
|
|
|
458
|
-
return {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
+
return { refusal: null, archivePath, fresh, endedAt };
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
// Cleanup runs only when the in-lock section succeeded (archived and
|
|
472
|
+
// journaled `batch.completed`); a refusal archived nothing for this batch.
|
|
473
|
+
if (inLock.refusal) return inLock.refusal;
|
|
474
|
+
|
|
475
|
+
// SP-796 / #302: same post-release split as dismiss — failures become
|
|
476
|
+
// cleanupWarnings + `batch.cleanup_failed`, never thrown post-archive.
|
|
477
|
+
const cleanupWarnings = runLifecycleCleanupAfterRelease({
|
|
478
|
+
projectRoot,
|
|
479
|
+
batchId,
|
|
480
|
+
batchState: inLock.fresh.raw,
|
|
481
|
+
reconciliation,
|
|
482
|
+
diagnosis: "completed",
|
|
483
|
+
endedAt: inLock.endedAt,
|
|
468
484
|
});
|
|
485
|
+
|
|
486
|
+
return {
|
|
487
|
+
ok: true,
|
|
488
|
+
exitCode: 0,
|
|
489
|
+
batchId,
|
|
490
|
+
diagnosis: "completed",
|
|
491
|
+
headline: `Batch ${batchId} completed and archived`,
|
|
492
|
+
suggestedCommand: "spine preflight",
|
|
493
|
+
alternatives: ["spine plan all"],
|
|
494
|
+
archivePath: inLock.archivePath,
|
|
495
|
+
cleanupWarnings,
|
|
496
|
+
};
|
|
469
497
|
}
|
package/src/batch/metrics.mjs
CHANGED
|
@@ -133,7 +133,14 @@ export function buildTaskMetricRecord({ batchId, task, config = {}, taskFolder,
|
|
|
133
133
|
batchId,
|
|
134
134
|
taskId: task.taskId,
|
|
135
135
|
agentRole: "worker",
|
|
136
|
-
|
|
136
|
+
// SP-808 (#329): SP-809 pins task.workerModel to the model actually used
|
|
137
|
+
// (fallback profile after a hop); prefer it over the batch-start config.
|
|
138
|
+
model:
|
|
139
|
+
typeof task.workerModel === "string" && task.workerModel !== ""
|
|
140
|
+
? task.workerModel
|
|
141
|
+
: typeof worker.model === "string"
|
|
142
|
+
? worker.model
|
|
143
|
+
: "inherit",
|
|
137
144
|
thinking:
|
|
138
145
|
worker.thinking === "off" ||
|
|
139
146
|
worker.thinking === "low" ||
|
|
@@ -155,6 +162,11 @@ export function buildTaskMetricRecord({ batchId, task, config = {}, taskFolder,
|
|
|
155
162
|
if (task.exitReason) record.exitReason = task.exitReason;
|
|
156
163
|
if (task.exitReason === "contract_failed") record.failureKind = "contract";
|
|
157
164
|
if (task.exitReason === "review_exhausted") record.failureKind = "reviewer";
|
|
165
|
+
// SP-806 (#329): provider quota/overload exits carry their own failure kind
|
|
166
|
+
// so doctor quota-risk signals can fire on run-metrics without extra config.
|
|
167
|
+
if (task.exitReason === "provider_quota_exhausted" || task.exitReason === "provider_overloaded") {
|
|
168
|
+
record.failureKind = "quota";
|
|
169
|
+
}
|
|
158
170
|
if (reviewLevel > 0) record.reviewLevel = reviewLevel;
|
|
159
171
|
if (task.finalVerdict != null) record.finalVerdict = task.finalVerdict;
|
|
160
172
|
if (task.finalAttempts != null) record.finalAttempts = task.finalAttempts;
|
|
@@ -12,11 +12,10 @@ import { resolveAttachedMilestonePollMs } from "../config/spine-config-schema.mj
|
|
|
12
12
|
import { appendJournalEvent } from "./journal.mjs";
|
|
13
13
|
import {
|
|
14
14
|
clearBatchEnginePid,
|
|
15
|
-
loadSpineBatchState,
|
|
16
15
|
readBatchEnginePid,
|
|
17
16
|
recomputeTaskCounters,
|
|
18
|
-
saveSpineBatchState,
|
|
19
17
|
updateSegmentForTask,
|
|
18
|
+
updateSpineBatchState,
|
|
20
19
|
} from "./state.mjs";
|
|
21
20
|
|
|
22
21
|
/**
|
|
@@ -56,6 +55,14 @@ export function isParentSessionLost({
|
|
|
56
55
|
/**
|
|
57
56
|
* Fail-closed reconcile when the attached engine parent session is gone.
|
|
58
57
|
*
|
|
58
|
+
* The whole reconcile is one `updateSpineBatchState` read-modify-write
|
|
59
|
+
* (SP-793 / #301): the phase is re-checked under the lock on the latest
|
|
60
|
+
* committed state, and the persist goes through the write guard with no owner
|
|
61
|
+
* bypass — the recorded owner is the dead parent engine, so the guard allows
|
|
62
|
+
* the write; if ownership was handed to a live detached child meanwhile, the
|
|
63
|
+
* loud guard rejection is the correct outcome instead of clobbering the new
|
|
64
|
+
* owner's state.
|
|
65
|
+
*
|
|
59
66
|
* @param {object} params
|
|
60
67
|
* @param {string} params.projectRoot
|
|
61
68
|
* @param {number} params.parentPid
|
|
@@ -63,85 +70,85 @@ export function isParentSessionLost({
|
|
|
63
70
|
* @returns {{ handled: boolean, batchId?: string, taskIds?: string[] }}
|
|
64
71
|
*/
|
|
65
72
|
export function reconcileParentSessionLost({ projectRoot, parentPid, enginePid = process.pid }) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
73
|
+
/** @type {{ handled: boolean, batchId?: string, taskIds?: string[] }} */
|
|
74
|
+
let outcome = { handled: false };
|
|
75
|
+
updateSpineBatchState(projectRoot, (draft) => {
|
|
76
|
+
const batchId = String(draft.batchId ?? "");
|
|
77
|
+
if (!batchId) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
71
80
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
81
|
+
const phase = String(draft.phase ?? "");
|
|
82
|
+
if (phase === "completed" || phase === "aborted" || phase === "failed") {
|
|
83
|
+
outcome = { handled: false, batchId };
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
76
86
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
87
|
+
appendJournalEvent(projectRoot, batchId, "engine.parent_died", {
|
|
88
|
+
parentPid,
|
|
89
|
+
enginePid,
|
|
90
|
+
signal: "parent_exit",
|
|
91
|
+
});
|
|
81
92
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
});
|
|
93
|
+
const now = Date.now();
|
|
94
|
+
/** @type {string[]} */
|
|
95
|
+
const failedTaskIds = [];
|
|
96
|
+
let changed = false;
|
|
87
97
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
let changed = false;
|
|
92
|
-
|
|
93
|
-
for (const task of state.tasks ?? []) {
|
|
94
|
-
if (!task || typeof task !== "object") continue;
|
|
95
|
-
if (String(task.status ?? "").toLowerCase() !== "running") continue;
|
|
96
|
-
|
|
97
|
-
const taskId = String(task.taskId ?? "");
|
|
98
|
-
if (!taskId) continue;
|
|
99
|
-
|
|
100
|
-
task.status = "failed";
|
|
101
|
-
task.endedAt = now;
|
|
102
|
-
task.exitReason = "parent_exit";
|
|
103
|
-
updateSegmentForTask(state, taskId, "failed");
|
|
104
|
-
failedTaskIds.push(taskId);
|
|
105
|
-
changed = true;
|
|
106
|
-
|
|
107
|
-
const laneNumber = Number(task.laneNumber ?? 1);
|
|
108
|
-
const lane = (state.lanes ?? []).find((entry) => Number(entry?.laneNumber) === laneNumber);
|
|
109
|
-
appendJournalEvent(projectRoot, batchId, "task.failed", {
|
|
110
|
-
taskId,
|
|
111
|
-
laneNumber,
|
|
112
|
-
laneId: lane?.laneId ?? `lane-${laneNumber}`,
|
|
113
|
-
reason: "parent_exit",
|
|
114
|
-
reconciled: true,
|
|
115
|
-
});
|
|
116
|
-
}
|
|
98
|
+
for (const task of draft.tasks ?? []) {
|
|
99
|
+
if (!task || typeof task !== "object") continue;
|
|
100
|
+
if (String(task.status ?? "").toLowerCase() !== "running") continue;
|
|
117
101
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
102
|
+
const taskId = String(task.taskId ?? "");
|
|
103
|
+
if (!taskId) continue;
|
|
104
|
+
|
|
105
|
+
task.status = "failed";
|
|
106
|
+
task.endedAt = now;
|
|
107
|
+
task.exitReason = "parent_exit";
|
|
108
|
+
updateSegmentForTask(draft, taskId, "failed");
|
|
109
|
+
failedTaskIds.push(taskId);
|
|
123
110
|
changed = true;
|
|
111
|
+
|
|
112
|
+
const laneNumber = Number(task.laneNumber ?? 1);
|
|
113
|
+
const lane = (draft.lanes ?? []).find((entry) => Number(entry?.laneNumber) === laneNumber);
|
|
114
|
+
appendJournalEvent(projectRoot, batchId, "task.failed", {
|
|
115
|
+
taskId,
|
|
116
|
+
laneNumber,
|
|
117
|
+
laneId: lane?.laneId ?? `lane-${laneNumber}`,
|
|
118
|
+
reason: "parent_exit",
|
|
119
|
+
reconciled: true,
|
|
120
|
+
});
|
|
124
121
|
}
|
|
125
|
-
}
|
|
126
122
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
123
|
+
for (const lane of draft.lanes ?? []) {
|
|
124
|
+
if (!lane || typeof lane !== "object") continue;
|
|
125
|
+
const workerPid = Number(/** @type {{ workerPid?: number }} */ (lane).workerPid);
|
|
126
|
+
if (Number.isFinite(workerPid) && workerPid > 0 && !isProcessAlive(workerPid)) {
|
|
127
|
+
delete lane.workerPid;
|
|
128
|
+
changed = true;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
132
131
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
132
|
+
const recordedEnginePid = readBatchEnginePid(draft);
|
|
133
|
+
if (recordedEnginePid != null) {
|
|
134
|
+
clearBatchEnginePid(draft);
|
|
135
|
+
changed = true;
|
|
136
|
+
}
|
|
137
137
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
138
|
+
if (phase !== "paused") {
|
|
139
|
+
draft.phase = "paused";
|
|
140
|
+
changed = true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
outcome = { handled: true, batchId, taskIds: failedTaskIds };
|
|
144
|
+
if (!changed) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
141
147
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
148
|
+
recomputeTaskCounters(draft);
|
|
149
|
+
return true;
|
|
150
|
+
});
|
|
151
|
+
return outcome;
|
|
145
152
|
}
|
|
146
153
|
|
|
147
154
|
/**
|