pi-spine 2.14.1 → 2.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/skills/spine-release-operator/SKILL.md +60 -7
- package/skills/spine-release-operator/references/issue-intake-checklist.md +1 -1
- package/skills/spine-release-operator/references/post-integrate-regression-gate.md +1 -0
- package/src/batch/abort.mjs +82 -68
- package/src/batch/batch-id.mjs +103 -0
- package/src/batch/batch-state-lock.mjs +357 -0
- package/src/batch/contract-exec.mjs +12 -8
- package/src/batch/diagnosis.mjs +12 -7
- package/src/batch/engine-lanes/review-code.mjs +224 -0
- package/src/batch/engine-lanes/review-final.mjs +318 -0
- package/src/batch/engine-lanes/review-plan.mjs +315 -0
- package/src/batch/engine-lanes/review-poll.mjs +427 -0
- package/src/batch/engine-lanes/review-stub.mjs +146 -0
- package/src/batch/engine-lanes/review.mjs +4 -1532
- package/src/batch/engine.mjs +1 -1
- package/src/batch/journal.mjs +9 -32
- package/src/batch/lifecycle.mjs +106 -95
- package/src/batch/metrics.mjs +13 -26
- package/src/batch/reconcile-orphan.mjs +3 -2
- package/src/batch/salvage-batch-list.mjs +12 -1
- package/src/batch/state-guards.mjs +26 -3
- package/src/batch/state-io.mjs +101 -29
- package/src/batch/state.mjs +7 -13
- package/src/batch/worker-output.mjs +5 -16
- package/src/cli/handoff.mjs +10 -27
- package/src/cli/journal-follow.mjs +11 -2
- package/src/cli/lane-logs.mjs +9 -1
- package/src/config/cursor-rules/profile.mjs +5 -1
- package/src/config/defaults.mjs +10 -1
- package/src/config/settings-fields.mjs +14 -0
- package/src/planner/file-scope.mjs +109 -7
- package/src/process/liveness.mjs +188 -1
- package/src/tasks/analyze/index.mjs +14 -27
- package/src/tasks/packet/parse-prompt.mjs +138 -3
- package/src/util/secret-redact.mjs +140 -0
package/src/batch/engine.mjs
CHANGED
|
@@ -172,7 +172,7 @@ export async function startBatch({
|
|
|
172
172
|
};
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
const batchId = generateBatchId();
|
|
175
|
+
const batchId = generateBatchId(new Date(), projectRoot);
|
|
176
176
|
const baseBranch = config.baseBranch ?? "main";
|
|
177
177
|
const orchBranch = `orch/spine-${batchId}`;
|
|
178
178
|
|
package/src/batch/journal.mjs
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
computeJournalChecksum,
|
|
12
12
|
verifyJournalChecksum,
|
|
13
13
|
} from "./journal-checksum.mjs";
|
|
14
|
+
import { capPayloadBytes, redactSecretsDeep } from "../util/secret-redact.mjs";
|
|
14
15
|
|
|
15
16
|
export { verifyJournalChecksum };
|
|
16
17
|
|
|
@@ -52,8 +53,6 @@ export const STRUCTURAL_JOURNAL_EVENT_TYPES = Object.freeze(
|
|
|
52
53
|
|
|
53
54
|
const META_KEYS = new Set(["correlationId", "laneId", "laneNumber", "taskId", "payload"]);
|
|
54
55
|
|
|
55
|
-
const REDACT_KEY_PATTERN = /key|token|secret|password/i;
|
|
56
|
-
|
|
57
56
|
/**
|
|
58
57
|
* @param {string} projectRoot
|
|
59
58
|
* @param {string} batchId
|
|
@@ -63,46 +62,24 @@ export function journalPath(projectRoot, batchId) {
|
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
/**
|
|
65
|
+
* Redact secrets in a journal payload via the shared policy (SP-716):
|
|
66
|
+
* denylisted keys are masked and string values are scanned for
|
|
67
|
+
* value-shaped secrets (sk-, ghp_, bearer, connection strings, ...).
|
|
68
|
+
*
|
|
66
69
|
* @param {unknown} value
|
|
67
70
|
* @returns {unknown}
|
|
68
71
|
*/
|
|
69
72
|
export function redactSecrets(value) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (Array.isArray(value)) {
|
|
73
|
-
return value.map((entry) => redactSecrets(entry));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** @type {Record<string, unknown>} */
|
|
77
|
-
const out = {};
|
|
78
|
-
for (const [key, entry] of Object.entries(/** @type {Record<string, unknown>} */ (value))) {
|
|
79
|
-
if (REDACT_KEY_PATTERN.test(key)) {
|
|
80
|
-
out[key] = "[REDACTED]";
|
|
81
|
-
} else if (entry && typeof entry === "object") {
|
|
82
|
-
out[key] = redactSecrets(entry);
|
|
83
|
-
} else {
|
|
84
|
-
out[key] = entry;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return out;
|
|
73
|
+
return redactSecretsDeep(value);
|
|
88
74
|
}
|
|
89
75
|
|
|
90
76
|
/**
|
|
77
|
+
* Cap payload size by UTF-8 bytes (SP-716).
|
|
78
|
+
*
|
|
91
79
|
* @param {Record<string, unknown>} payload
|
|
92
80
|
*/
|
|
93
81
|
export function capPayloadSize(payload) {
|
|
94
|
-
|
|
95
|
-
if (Buffer.byteLength(serialized, "utf-8") <= MAX_PAYLOAD_BYTES) {
|
|
96
|
-
return payload;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const truncated = {
|
|
100
|
-
_truncated: true,
|
|
101
|
-
_originalBytes: Buffer.byteLength(serialized, "utf-8"),
|
|
102
|
-
_maxBytes: MAX_PAYLOAD_BYTES,
|
|
103
|
-
preview: serialized.slice(0, MAX_PAYLOAD_BYTES - 256),
|
|
104
|
-
};
|
|
105
|
-
return truncated;
|
|
82
|
+
return capPayloadBytes(payload, MAX_PAYLOAD_BYTES);
|
|
106
83
|
}
|
|
107
84
|
|
|
108
85
|
/**
|
package/src/batch/lifecycle.mjs
CHANGED
|
@@ -21,6 +21,7 @@ import { appendJournalEvent } from "./journal.mjs";
|
|
|
21
21
|
import { recordBatchTerminalMetric } from "./metrics.mjs";
|
|
22
22
|
import { writeBatchPostMortem } from "./postmortem.mjs";
|
|
23
23
|
import { appendBatchHistoryEntry, clearBatchEnginePid, saveSpineBatchState } from "./state.mjs";
|
|
24
|
+
import { withBatchStateLock } from "./batch-state-lock.mjs";
|
|
24
25
|
import { loadBatchStateFile, parseBatchState, reconcileBatch } from "./reconcile.mjs";
|
|
25
26
|
import { readAliveBatchEnginePid } from "./batch-state-io.mjs";
|
|
26
27
|
import { terminateLaneWorkers } from "./worker-host.mjs";
|
|
@@ -206,55 +207,60 @@ export function dismissBatch(ctx) {
|
|
|
206
207
|
force,
|
|
207
208
|
});
|
|
208
209
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
endedAt
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
210
|
+
// Terminal dismiss write section under the global batch-state lock
|
|
211
|
+
// (SP-722 / #264): archive, history entry, and clearing the active state
|
|
212
|
+
// must not interleave with a concurrent engine save or resume writer.
|
|
213
|
+
return withBatchStateLock(projectRoot, () => {
|
|
214
|
+
const archivePath = archiveBatchState(projectRoot, batchId, loaded.raw);
|
|
215
|
+
const postMortemPath = writeBatchPostMortem({
|
|
216
|
+
projectRoot,
|
|
217
|
+
batchState: loaded.raw,
|
|
218
|
+
reconciliation,
|
|
219
|
+
});
|
|
220
|
+
const endedAt = Date.now();
|
|
221
|
+
appendBatchHistoryEntry(projectRoot, {
|
|
222
|
+
batchId,
|
|
223
|
+
action: "dismissed",
|
|
224
|
+
endedAt,
|
|
225
|
+
diagnosis,
|
|
226
|
+
reason: reason ?? null,
|
|
227
|
+
archivePath: path.relative(projectRoot, archivePath),
|
|
228
|
+
postMortemPath,
|
|
229
|
+
});
|
|
230
|
+
appendJournalEvent(projectRoot, batchId, "batch.dismissed", {
|
|
231
|
+
diagnosis,
|
|
232
|
+
reason: reason ?? null,
|
|
233
|
+
archivePath: path.relative(projectRoot, archivePath),
|
|
234
|
+
});
|
|
235
|
+
const configResult = loadSpineConfig(projectRoot);
|
|
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(loaded.path, batchId);
|
|
251
|
+
bumpDashboardInvalidateSignal(projectRoot, "batch_dismiss", batchId);
|
|
247
252
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
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
|
+
};
|
|
263
|
+
});
|
|
258
264
|
}
|
|
259
265
|
|
|
260
266
|
/**
|
|
@@ -407,52 +413,57 @@ export function completeBatch(ctx) {
|
|
|
407
413
|
|
|
408
414
|
terminateSupervisorIfRunning(projectRoot, batchId, "batch_complete");
|
|
409
415
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
endedAt
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
416
|
+
// Terminal complete write section under the global batch-state lock
|
|
417
|
+
// (SP-722 / #264): archive, history entry, and clearing the active state
|
|
418
|
+
// must not interleave with a concurrent engine save or resume writer.
|
|
419
|
+
return withBatchStateLock(projectRoot, () => {
|
|
420
|
+
const archivePath = archiveBatchState(projectRoot, batchId, loaded.raw);
|
|
421
|
+
const postMortemPath = writeBatchPostMortem({
|
|
422
|
+
projectRoot,
|
|
423
|
+
batchState: loaded.raw,
|
|
424
|
+
reconciliation,
|
|
425
|
+
});
|
|
426
|
+
const endedAt = Date.now();
|
|
427
|
+
appendBatchHistoryEntry(projectRoot, {
|
|
428
|
+
batchId,
|
|
429
|
+
action: "completed",
|
|
430
|
+
endedAt,
|
|
431
|
+
diagnosis: "completed",
|
|
432
|
+
detectManualMerge,
|
|
433
|
+
archivePath: path.relative(projectRoot, archivePath),
|
|
434
|
+
postMortemPath,
|
|
435
|
+
});
|
|
436
|
+
appendJournalEvent(projectRoot, batchId, "batch.completed", {
|
|
437
|
+
detectManualMerge,
|
|
438
|
+
archivePath: path.relative(projectRoot, archivePath),
|
|
439
|
+
lifecycle: "complete",
|
|
440
|
+
});
|
|
441
|
+
const configResult = loadSpineConfig(projectRoot);
|
|
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(loaded.path, batchId);
|
|
447
457
|
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
+
return {
|
|
459
|
+
ok: true,
|
|
460
|
+
exitCode: 0,
|
|
461
|
+
batchId,
|
|
462
|
+
diagnosis: "completed",
|
|
463
|
+
headline: `Batch ${batchId} completed and archived`,
|
|
464
|
+
suggestedCommand: "spine preflight",
|
|
465
|
+
alternatives: ["spine plan all"],
|
|
466
|
+
archivePath,
|
|
467
|
+
};
|
|
468
|
+
});
|
|
458
469
|
}
|
package/src/batch/metrics.mjs
CHANGED
|
@@ -7,9 +7,12 @@ import fs from "node:fs";
|
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { execFileSync } from "node:child_process";
|
|
9
9
|
import { METRICS_DEFAULTS } from "../config/defaults.mjs";
|
|
10
|
+
import { redactSecretsDeep } from "../util/secret-redact.mjs";
|
|
10
11
|
import { readReviewLevel } from "./review.mjs";
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
// Metrics additionally redacts prompt text (worker prompts may embed
|
|
14
|
+
// secrets) while preserving usage counters for cost rollup.
|
|
15
|
+
const METRICS_REDACT_KEY_PATTERN = /key|token|secret|password|prompt/i;
|
|
13
16
|
const USAGE_KEYS = new Set(["tokensIn", "tokensOut", "estimatedUsd"]);
|
|
14
17
|
|
|
15
18
|
/**
|
|
@@ -80,34 +83,18 @@ export function isRunMetricsAppendOnlyDrift(
|
|
|
80
83
|
}
|
|
81
84
|
|
|
82
85
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
|
|
86
|
-
function redactMetricValue(value) {
|
|
87
|
-
if (value == null || typeof value !== "object") return value;
|
|
88
|
-
if (Array.isArray(value)) {
|
|
89
|
-
return value.map((entry) => redactMetricValue(entry));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** @type {Record<string, unknown>} */
|
|
93
|
-
const out = {};
|
|
94
|
-
for (const [key, entry] of Object.entries(/** @type {Record<string, unknown>} */ (value))) {
|
|
95
|
-
if (REDACT_KEY_PATTERN.test(key) && !USAGE_KEYS.has(key)) {
|
|
96
|
-
out[key] = "[REDACTED]";
|
|
97
|
-
} else if (entry && typeof entry === "object") {
|
|
98
|
-
out[key] = redactMetricValue(entry);
|
|
99
|
-
} else {
|
|
100
|
-
out[key] = entry;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return out;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
86
|
+
* Redact secret-like fields via the shared policy (SP-716), keeping the
|
|
87
|
+
* metrics-specific prompt denylist and usage-counter exemptions.
|
|
88
|
+
*
|
|
107
89
|
* @param {object} record
|
|
108
90
|
*/
|
|
109
91
|
export function sanitizeMetricRecord(record) {
|
|
110
|
-
return /** @type {typeof record} */ (
|
|
92
|
+
return /** @type {typeof record} */ (
|
|
93
|
+
redactSecretsDeep(record, {
|
|
94
|
+
keyPattern: METRICS_REDACT_KEY_PATTERN,
|
|
95
|
+
allowedKeys: USAGE_KEYS,
|
|
96
|
+
})
|
|
97
|
+
);
|
|
111
98
|
}
|
|
112
99
|
|
|
113
100
|
/**
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
5
5
|
import { resolveTasksRoot } from "../config/spine-preflight-lib.mjs";
|
|
6
|
-
import { isProcessAlive } from "../process/liveness.mjs";
|
|
6
|
+
import { isEngineProcessAlive, isProcessAlive } from "../process/liveness.mjs";
|
|
7
7
|
import { classifyTaskDoneSemantics } from "./diagnosis-task-done.mjs";
|
|
8
8
|
import {
|
|
9
9
|
appendJournalEvent,
|
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
saveSpineBatchState,
|
|
25
25
|
updateSegmentForTask,
|
|
26
26
|
} from "./state.mjs";
|
|
27
|
+
import { readBatchEngineStartedAt } from "./state-guards.mjs";
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* @param {object} ctx
|
|
@@ -271,7 +272,7 @@ export function reconcileOrphanRunningState({ projectRoot, state }) {
|
|
|
271
272
|
}
|
|
272
273
|
|
|
273
274
|
const enginePid = readBatchEnginePid(state);
|
|
274
|
-
if (enginePid != null && !
|
|
275
|
+
if (enginePid != null && !isEngineProcessAlive(enginePid, readBatchEngineStartedAt(state))) {
|
|
275
276
|
clearBatchEnginePid(state);
|
|
276
277
|
changed = true;
|
|
277
278
|
}
|
|
@@ -12,6 +12,7 @@ import { journalPath, readJournalEvents } from "./journal.mjs";
|
|
|
12
12
|
import { rebuildBatchStateFromJournal } from "./journal-rebuild.mjs";
|
|
13
13
|
import { loadBatchStateFile } from "./reconcile.mjs";
|
|
14
14
|
import { laneTaskBranch } from "./worktree.mjs";
|
|
15
|
+
import { classifyTaskDoneSemantics } from "./diagnosis-task-done.mjs";
|
|
15
16
|
|
|
16
17
|
/** Exit reasons that block salvage even when a lane commit exists. */
|
|
17
18
|
export const NON_SALVAGEABLE_EXIT_REASONS = new Set([
|
|
@@ -199,12 +200,22 @@ export function listSalvageableLanes(projectRoot, batchId) {
|
|
|
199
200
|
const baseBranch = String(rebuilt.baseBranch ?? seedState?.baseBranch ?? "main");
|
|
200
201
|
const committedTaskIds = laneCommittedTaskIds(journalEvents);
|
|
201
202
|
const seedTasks = seedState?.tasks;
|
|
203
|
+
const tasksRoot = seedState?.tasksRoot ?? "spine-tasks";
|
|
202
204
|
|
|
203
205
|
/** @type {Map<number, { laneNumber: number, salvageableTasks: string[], excludedTasks: string[] }>} */
|
|
204
206
|
const laneMap = new Map();
|
|
205
207
|
|
|
206
208
|
for (const rawTask of rebuilt.tasks ?? []) {
|
|
207
|
-
|
|
209
|
+
let task = enrichTaskWithSeedEvidence(rawTask, seedTasks);
|
|
210
|
+
if (task.doneInLane !== true && task.doneOnMain !== true) {
|
|
211
|
+
task = classifyTaskDoneSemantics(task, {
|
|
212
|
+
projectRoot,
|
|
213
|
+
batchId: resolvedBatchId,
|
|
214
|
+
tasksRoot,
|
|
215
|
+
lanes: seedState?.lanes ?? [],
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
208
219
|
const laneNumber = Number(task?.laneNumber);
|
|
209
220
|
if (!Number.isFinite(laneNumber) || laneNumber <= 0) continue;
|
|
210
221
|
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import fs from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
8
|
-
import {
|
|
8
|
+
import { isEngineProcessAlive } from "../process/liveness.mjs";
|
|
9
9
|
|
|
10
10
|
/** @type {ReadonlySet<string>} */
|
|
11
11
|
export const ACTIVE_PHASES = new Set(["planning", "running", "paused"]);
|
|
@@ -54,16 +54,17 @@ export function evaluateBatchStateWriteGuard(projectRoot, state) {
|
|
|
54
54
|
const incomingBatchId = String(state.batchId ?? "");
|
|
55
55
|
const onDiskPhase = String(onDisk.phase ?? "");
|
|
56
56
|
const ownerPid = readBatchEnginePid(onDisk);
|
|
57
|
+
const ownerStartedAt = readBatchEngineStartedAt(onDisk);
|
|
57
58
|
if (
|
|
58
59
|
incomingBatchId &&
|
|
59
60
|
onDiskBatchId &&
|
|
60
61
|
incomingBatchId !== onDiskBatchId &&
|
|
61
62
|
TERMINAL_BATCH_PHASES.has(onDiskPhase) &&
|
|
62
|
-
(!ownerPid || !
|
|
63
|
+
(!ownerPid || !isEngineProcessAlive(ownerPid, ownerStartedAt))
|
|
63
64
|
) {
|
|
64
65
|
return { allowed: true };
|
|
65
66
|
}
|
|
66
|
-
if (ownerPid && ownerPid !== process.pid &&
|
|
67
|
+
if (ownerPid && ownerPid !== process.pid && isEngineProcessAlive(ownerPid, ownerStartedAt)) {
|
|
67
68
|
return { allowed: false, reason: "stale_engine_pid" };
|
|
68
69
|
}
|
|
69
70
|
} catch {
|
|
@@ -97,6 +98,28 @@ export function readBatchEnginePid(raw) {
|
|
|
97
98
|
return null;
|
|
98
99
|
}
|
|
99
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Stored engine start time (epoch ms) paired with `enginePid` for PID-reuse-safe
|
|
103
|
+
* liveness checks (SP-715 / #259).
|
|
104
|
+
*
|
|
105
|
+
* @param {unknown} raw
|
|
106
|
+
* @returns {number|null}
|
|
107
|
+
*/
|
|
108
|
+
export function readBatchEngineStartedAt(raw) {
|
|
109
|
+
if (!raw || typeof raw !== "object") return null;
|
|
110
|
+
/** @type {Record<string, unknown>} */
|
|
111
|
+
const state = /** @type {Record<string, unknown>} */ (raw);
|
|
112
|
+
const resilience =
|
|
113
|
+
state.resilience && typeof state.resilience === "object"
|
|
114
|
+
? /** @type {Record<string, unknown>} */ (state.resilience)
|
|
115
|
+
: null;
|
|
116
|
+
const fromResilience = Number(resilience?.engineStartedAt);
|
|
117
|
+
if (Number.isFinite(fromResilience) && fromResilience > 0) return fromResilience;
|
|
118
|
+
const topLevel = Number(state.engineStartedAt);
|
|
119
|
+
if (Number.isFinite(topLevel) && topLevel > 0) return topLevel;
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
100
123
|
/**
|
|
101
124
|
* @param {object} state
|
|
102
125
|
* @param {number} enginePid
|
package/src/batch/state-io.mjs
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import fs from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { writeJsonAtomic } from "../fs/atomic-write.mjs";
|
|
9
|
+
import { withBatchStateLock } from "./batch-state-lock.mjs";
|
|
9
10
|
import {
|
|
10
11
|
clearBatchEnginePid,
|
|
11
12
|
evaluateBatchStateWriteGuard,
|
|
@@ -52,27 +53,35 @@ function archivedBatchStatePath(projectRoot, batchId) {
|
|
|
52
53
|
}
|
|
53
54
|
|
|
54
55
|
/**
|
|
56
|
+
* Persist batch state under the global batch-state lock (SP-722 / #264).
|
|
57
|
+
*
|
|
58
|
+
* The guard evaluation and the write run inside one critical section so the
|
|
59
|
+
* check-then-act pair cannot race a concurrent writer from another process
|
|
60
|
+
* (engine vs. CLI complete/resume/abort).
|
|
61
|
+
*
|
|
55
62
|
* @param {string} projectRoot
|
|
56
63
|
* @param {object} state
|
|
57
64
|
* @param {{ bypassWriteGuard?: boolean }} [options]
|
|
58
65
|
*/
|
|
59
66
|
export function saveSpineBatchState(projectRoot, state, options = {}) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
+
return withBatchStateLock(projectRoot, () => {
|
|
68
|
+
const guard = options.bypassWriteGuard
|
|
69
|
+
? { allowed: true }
|
|
70
|
+
: evaluateBatchStateWriteGuard(projectRoot, state);
|
|
71
|
+
if (!guard.allowed) {
|
|
72
|
+
const loaded = loadSpineBatchState(projectRoot);
|
|
73
|
+
return loaded.raw ?? state;
|
|
74
|
+
}
|
|
67
75
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
const filePath = spineBatchStatePath(projectRoot);
|
|
77
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
78
|
+
if (TERMINAL_BATCH_PHASES.has(String(state.phase ?? ""))) {
|
|
79
|
+
clearBatchEnginePid(state);
|
|
80
|
+
}
|
|
81
|
+
const next = { ...state, updatedAt: Date.now() };
|
|
82
|
+
writeJsonAtomic(filePath, next);
|
|
83
|
+
return next;
|
|
84
|
+
});
|
|
76
85
|
}
|
|
77
86
|
|
|
78
87
|
/**
|
|
@@ -111,25 +120,88 @@ export function resolveBatchStateFileForValidation(projectRoot, batchId = null)
|
|
|
111
120
|
}
|
|
112
121
|
|
|
113
122
|
/**
|
|
123
|
+
* Quarantine a corrupt batch-history file instead of silently resetting it to
|
|
124
|
+
* `[]`. The audit trail is operator-facing state: losing it silently hides
|
|
125
|
+
* evidence of what batches ran, so the original bytes are preserved under
|
|
126
|
+
* `.spine/runtime/` and a loud error is emitted (#261).
|
|
127
|
+
*
|
|
114
128
|
* @param {string} projectRoot
|
|
115
|
-
* @param {
|
|
129
|
+
* @param {string} filePath
|
|
130
|
+
* @param {string} reason
|
|
131
|
+
* @returns {string|null} quarantine path, or null if quarantine failed
|
|
116
132
|
*/
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
let history = [];
|
|
123
|
-
if (fs.existsSync(filePath)) {
|
|
133
|
+
function quarantineCorruptBatchHistory(projectRoot, filePath, reason) {
|
|
134
|
+
const quarantineDir = path.join(projectRoot, ".spine", "runtime");
|
|
135
|
+
const quarantinePath = path.join(quarantineDir, `batch-history.json.corrupt.${Date.now()}`);
|
|
136
|
+
try {
|
|
137
|
+
fs.mkdirSync(quarantineDir, { recursive: true });
|
|
124
138
|
try {
|
|
125
|
-
|
|
126
|
-
if (Array.isArray(parsed)) history = parsed;
|
|
139
|
+
fs.renameSync(filePath, quarantinePath);
|
|
127
140
|
} catch {
|
|
128
|
-
|
|
141
|
+
// Fall back to copy+unlink when rename is unsupported (e.g. cross-device).
|
|
142
|
+
fs.copyFileSync(filePath, quarantinePath);
|
|
143
|
+
fs.unlinkSync(filePath);
|
|
129
144
|
}
|
|
145
|
+
} catch (err) {
|
|
146
|
+
console.error(
|
|
147
|
+
`[spine] batch-history.json is corrupt (${reason}) and quarantine failed: ${
|
|
148
|
+
err instanceof Error ? err.message : String(err)
|
|
149
|
+
}. Refusing to overwrite the corrupt file.`,
|
|
150
|
+
);
|
|
151
|
+
return null;
|
|
130
152
|
}
|
|
153
|
+
console.error(
|
|
154
|
+
`[spine] batch-history.json is corrupt (${reason}); quarantined to ${path.relative(
|
|
155
|
+
projectRoot,
|
|
156
|
+
quarantinePath,
|
|
157
|
+
)}. Starting a fresh history — inspect the quarantined file to recover prior entries.`,
|
|
158
|
+
);
|
|
159
|
+
return quarantinePath;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Append an entry to `.spine/batch-history.json` atomically under the
|
|
164
|
+
* global batch-state lock (SP-722 / #264).
|
|
165
|
+
*
|
|
166
|
+
* The write goes through `writeJsonAtomic` (temp file + rename) so a crash or
|
|
167
|
+
* concurrent reader never observes a truncated history (#261). A corrupt
|
|
168
|
+
* existing file is quarantined, never silently reset to `[]`. The full
|
|
169
|
+
* read-modify-write runs under the same lock as batch-state writes so
|
|
170
|
+
* concurrent processes cannot lose history entries.
|
|
171
|
+
*
|
|
172
|
+
* @param {string} projectRoot
|
|
173
|
+
* @param {object} entry
|
|
174
|
+
*/
|
|
175
|
+
export function appendBatchHistoryEntry(projectRoot, entry) {
|
|
176
|
+
return withBatchStateLock(projectRoot, () => {
|
|
177
|
+
const filePath = batchHistoryPath(projectRoot);
|
|
178
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
179
|
+
|
|
180
|
+
/** @type {object[]} */
|
|
181
|
+
let history = [];
|
|
182
|
+
if (fs.existsSync(filePath)) {
|
|
183
|
+
try {
|
|
184
|
+
const parsed = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
185
|
+
if (Array.isArray(parsed)) {
|
|
186
|
+
history = parsed;
|
|
187
|
+
} else {
|
|
188
|
+
if (quarantineCorruptBatchHistory(projectRoot, filePath, "root value is not an array") === null) {
|
|
189
|
+
throw new Error(`Refusing to append batch history: ${filePath} is corrupt and could not be quarantined`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
} catch (err) {
|
|
193
|
+
if (err instanceof SyntaxError) {
|
|
194
|
+
if (quarantineCorruptBatchHistory(projectRoot, filePath, err.message) === null) {
|
|
195
|
+
throw new Error(`Refusing to append batch history: ${filePath} is corrupt and could not be quarantined`);
|
|
196
|
+
}
|
|
197
|
+
} else {
|
|
198
|
+
throw err;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
131
202
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
203
|
+
history.push(entry);
|
|
204
|
+
writeJsonAtomic(filePath, history);
|
|
205
|
+
return filePath;
|
|
206
|
+
});
|
|
135
207
|
}
|
package/src/batch/state.mjs
CHANGED
|
@@ -34,19 +34,13 @@ export {
|
|
|
34
34
|
clearBatchEnginePid,
|
|
35
35
|
validateBatchState,
|
|
36
36
|
} from "./state-guards.mjs";
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const d = String(now.getUTCDate()).padStart(2, "0");
|
|
45
|
-
const h = String(now.getUTCHours()).padStart(2, "0");
|
|
46
|
-
const min = String(now.getUTCMinutes()).padStart(2, "0");
|
|
47
|
-
const s = String(now.getUTCSeconds()).padStart(2, "0");
|
|
48
|
-
return `${y}${m}${d}T${h}${min}${s}`;
|
|
49
|
-
}
|
|
37
|
+
export {
|
|
38
|
+
BATCH_ID_PATTERN,
|
|
39
|
+
batchIdRejectionReason,
|
|
40
|
+
generateBatchId,
|
|
41
|
+
isValidBatchId,
|
|
42
|
+
validateBatchId,
|
|
43
|
+
} from "./batch-id.mjs";
|
|
50
44
|
|
|
51
45
|
/**
|
|
52
46
|
* Reset lane stall clocks at resume handoff so dashboard does not show pre-resume heartbeats as stale.
|