pi-spine 2.25.0 → 2.26.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 (35) hide show
  1. package/package.json +1 -1
  2. package/src/batch/abort.mjs +74 -38
  3. package/src/batch/attached-engine-handoff.mjs +2 -2
  4. package/src/batch/attached-runner-promote.mjs +1 -1
  5. package/src/batch/attached-runner-reconcile.mjs +1 -1
  6. package/src/batch/batch-meta-reconstruct.mjs +6 -1
  7. package/src/batch/batch-state-lock.mjs +173 -31
  8. package/src/batch/contract-exec.mjs +46 -45
  9. package/src/batch/contract-spawn.mjs +225 -0
  10. package/src/batch/contract-verify.mjs +2 -1
  11. package/src/batch/detached-wait.mjs +1 -1
  12. package/src/batch/engine-lanes/matrix-run.mjs +6 -6
  13. package/src/batch/engine-lanes/merge.mjs +135 -34
  14. package/src/batch/engine-lanes/review-final.mjs +1 -1
  15. package/src/batch/engine-lanes.mjs +11 -9
  16. package/src/batch/engine.mjs +10 -10
  17. package/src/batch/lifecycle-archive.mjs +76 -1
  18. package/src/batch/lifecycle-cleanup.mjs +110 -0
  19. package/src/batch/lifecycle.mjs +100 -72
  20. package/src/batch/parent-session-monitor.mjs +77 -70
  21. package/src/batch/pause.mjs +92 -25
  22. package/src/batch/post-merge-limbo.mjs +35 -5
  23. package/src/batch/resume-gate-reopen.mjs +24 -8
  24. package/src/batch/resume-multi.mjs +1 -1
  25. package/src/batch/resume.mjs +1 -1
  26. package/src/batch/sequence-run.mjs +26 -8
  27. package/src/batch/sequence-wait.mjs +87 -8
  28. package/src/batch/state-guards.mjs +39 -7
  29. package/src/batch/state-io.mjs +131 -17
  30. package/src/batch/state.mjs +1 -0
  31. package/src/batch/worker-host.mjs +32 -4
  32. package/src/config/defaults.mjs +15 -1
  33. package/src/config/preflight/git-batch.mjs +47 -0
  34. package/src/config/spine-config-schema.mjs +67 -1
  35. package/src/fs/atomic-write.mjs +37 -1
@@ -38,9 +38,23 @@ export const CONTRACT_DEFAULTS = Object.freeze({
38
38
  legacyTaskIdPrefixes: Object.freeze(["TP-"]),
39
39
  });
40
40
 
41
- /** @type {Readonly<{ cleanupWorktreesOnComplete: boolean }>} */
41
+ /**
42
+ * Conflicted paths outside a lane File Scope that keep prefer-orch auto-resolution
43
+ * (SP-798 / #304). A conflicted path outside File Scope is only auto-resolved when
44
+ * it matches one of these globs — and the discarded lane blob is journaled; every
45
+ * other out-of-scope conflict fails closed so lane-committed work is never silently
46
+ * discarded.
47
+ */
48
+ const OUT_OF_SCOPE_MERGE_ALLOW_LIST_DEFAULT = Object.freeze([
49
+ ".spine/rules-manifest.json",
50
+ "package-lock.json",
51
+ "**/package-lock.json",
52
+ ]);
53
+
54
+ /** @type {Readonly<{ cleanupWorktreesOnComplete: boolean; outOfScopeMergeAllowList: readonly string[] }>} */
42
55
  export const LANES_DEFAULTS = Object.freeze({
43
56
  cleanupWorktreesOnComplete: true,
57
+ outOfScopeMergeAllowList: OUT_OF_SCOPE_MERGE_ALLOW_LIST_DEFAULT,
44
58
  });
45
59
 
46
60
  /** @type {Readonly<{ isolatedWorktree: boolean; allowHumanOnBaseBranch: "warn" | "block" | "allow" }>} */
@@ -4,6 +4,9 @@ import path from "node:path";
4
4
  import { execFileSync } from "node:child_process";
5
5
  import { loadSpineConfig } from "../spine-config-load.mjs";
6
6
  import { runReconciliationCheck } from "../../batch/reconcile.mjs";
7
+ import { isBatchArchived } from "../../batch/state-guards.mjs";
8
+ import { loadGateRecord } from "../../batch/gate-evidence-read.mjs";
9
+ import { readJournalEvents } from "../../batch/journal.mjs";
7
10
  import {
8
11
  isRulesManifestGeneratedAtOnlyDrift,
9
12
  RULES_MANIFEST_REL_PATH,
@@ -130,6 +133,33 @@ function isHealthyActiveBatch(batchState) {
130
133
  return false;
131
134
  }
132
135
 
136
+ /**
137
+ * A terminal `completed` batch-state whose archive already exists (a phantom
138
+ * recreated by a late land-loop write, #293) or whose integrate gate was
139
+ * approved / integrate already ran needs `spine batch complete` to archive and
140
+ * clear the file — never a self-referential `spine preflight` suggestion
141
+ * (SP-790 / #293).
142
+ *
143
+ * @param {string} projectRoot
144
+ * @param {object} batchState
145
+ */
146
+ function completedBatchNeedsBatchComplete(projectRoot, batchState) {
147
+ const phase = String(batchState?.phase ?? batchState?.status ?? "");
148
+ if (phase !== "completed") return false;
149
+ const batchId = String(batchState?.batchId ?? batchState?.id ?? "");
150
+ if (!batchId) return false;
151
+ if (isBatchArchived(projectRoot, batchId)) return true;
152
+ const gate = loadGateRecord(projectRoot, batchId);
153
+ if (gate && String(gate.status ?? "") === "approved") return true;
154
+ try {
155
+ return readJournalEvents(projectRoot, batchId).some(
156
+ (event) => event.type === "integrate.completed",
157
+ );
158
+ } catch {
159
+ return false;
160
+ }
161
+ }
162
+
133
163
  /**
134
164
  * @param {object} ctx
135
165
  * @param {string} ctx.projectRoot
@@ -297,6 +327,23 @@ export function checkNoActiveBatch(ctx) {
297
327
  );
298
328
  }
299
329
 
330
+ if (completedBatchNeedsBatchComplete(ctx.projectRoot, batchState)) {
331
+ const batchId = batchState.batchId ?? batchState.id ?? "unknown";
332
+ return makeCheck(
333
+ "no-active-batch",
334
+ false,
335
+ `completed batch ${batchId} awaiting archive`,
336
+ {
337
+ details: {
338
+ batchStatePath: path.relative(ctx.projectRoot, batchStatePath),
339
+ batchId,
340
+ phase: "completed",
341
+ },
342
+ suggestedCommand: "spine batch complete",
343
+ },
344
+ );
345
+ }
346
+
300
347
  if (isHealthyActiveBatch(batchState)) {
301
348
  const batchId = batchState.batchId ?? batchState.id ?? "unknown";
302
349
  const phase = batchState.phase ?? batchState.status ?? "unknown";
@@ -12,17 +12,25 @@ export const DEFAULT_SEQUENCE_POLL_MS = 5_000;
12
12
  /** Default dashboard SSE poll interval (wired in SP-453). */
13
13
  export const DEFAULT_DASHBOARD_POLL_MS = 2_000;
14
14
 
15
+ /** Default hard cap for the sequence batch terminal wait (SP-802 / #307). */
16
+ export const DEFAULT_SEQUENCE_MAX_WAIT_MS = 24 * 60 * 60 * 1000;
17
+
18
+ /** Default no-progress window before the sequence wait exits as stalled (SP-802 / #307). */
19
+ export const DEFAULT_SEQUENCE_STALL_MS = 30 * 60 * 1000;
20
+
15
21
  /** Lower bound for orchestrator poll intervals. */
16
22
  export const MIN_ORCHESTRATOR_POLL_MS = 100;
17
23
 
18
24
  /** Upper bound for orchestrator poll intervals. */
19
25
  export const MAX_ORCHESTRATOR_POLL_MS = 60_000;
20
26
 
21
- /** @type {Readonly<{ attachedMilestonePollMs: number; sequencePollMs: number; dashboardPollMs: number }>} */
27
+ /** @type {Readonly<{ attachedMilestonePollMs: number; sequencePollMs: number; dashboardPollMs: number; sequenceMaxWaitMs: number; sequenceStallMs: number }>} */
22
28
  export const ORCHESTRATOR_DEFAULTS = Object.freeze({
23
29
  attachedMilestonePollMs: DEFAULT_ATTACHED_MILESTONE_POLL_MS,
24
30
  sequencePollMs: DEFAULT_SEQUENCE_POLL_MS,
25
31
  dashboardPollMs: DEFAULT_DASHBOARD_POLL_MS,
32
+ sequenceMaxWaitMs: DEFAULT_SEQUENCE_MAX_WAIT_MS,
33
+ sequenceStallMs: DEFAULT_SEQUENCE_STALL_MS,
26
34
  });
27
35
 
28
36
  /**
@@ -54,6 +62,30 @@ function validatePositivePollMs(value, fieldLabel) {
54
62
  return { ok: true, value: parsed };
55
63
  }
56
64
 
65
+ /**
66
+ * Positive-integer validation without the poll-interval range clamp: wait/stall
67
+ * budgets legitimately exceed MAX_ORCHESTRATOR_POLL_MS (SP-802 / #307).
68
+ *
69
+ * @param {unknown} value
70
+ * @param {string} fieldLabel
71
+ * @returns {{ ok: true, value: number } | { ok: false, code: string, message: string, suggestedCommand: string }}
72
+ */
73
+ function validatePositiveDurationMs(value, fieldLabel) {
74
+ if (value == null) {
75
+ return { ok: true, value: 0 };
76
+ }
77
+ const parsed = Number(value);
78
+ if (!Number.isFinite(parsed) || !Number.isInteger(parsed) || parsed <= 0) {
79
+ return {
80
+ ok: false,
81
+ code: "CONFIG_ORCHESTRATOR_INVALID",
82
+ message: `${fieldLabel} must be a positive integer when set`,
83
+ suggestedCommand: "spine init --force",
84
+ };
85
+ }
86
+ return { ok: true, value: parsed };
87
+ }
88
+
57
89
  /**
58
90
  * @param {Record<string, any>} config
59
91
  * @returns {null | { code: string; message: string; suggestedCommand: string }}
@@ -82,6 +114,16 @@ export function validateOrchestratorConfig(config) {
82
114
  }
83
115
  }
84
116
 
117
+ for (const [key, label] of [
118
+ ["sequenceMaxWaitMs", "orchestrator.sequenceMaxWaitMs"],
119
+ ["sequenceStallMs", "orchestrator.sequenceStallMs"],
120
+ ]) {
121
+ const result = validatePositiveDurationMs(orchestrator[key], label);
122
+ if (!result.ok) {
123
+ return result;
124
+ }
125
+ }
126
+
85
127
  return null;
86
128
  }
87
129
 
@@ -121,6 +163,30 @@ export function resolveDashboardPollMs({ config = {} } = {}) {
121
163
  return DEFAULT_DASHBOARD_POLL_MS;
122
164
  }
123
165
 
166
+ /**
167
+ * @param {object} [params]
168
+ * @param {object} [params.config]
169
+ */
170
+ export function resolveSequenceMaxWaitMs({ config = {} } = {}) {
171
+ const configured = Number(config.orchestrator?.sequenceMaxWaitMs);
172
+ if (Number.isFinite(configured) && configured > 0) {
173
+ return configured;
174
+ }
175
+ return DEFAULT_SEQUENCE_MAX_WAIT_MS;
176
+ }
177
+
178
+ /**
179
+ * @param {object} [params]
180
+ * @param {object} [params.config]
181
+ */
182
+ export function resolveSequenceStallMs({ config = {} } = {}) {
183
+ const configured = Number(config.orchestrator?.sequenceStallMs);
184
+ if (Number.isFinite(configured) && configured > 0) {
185
+ return configured;
186
+ }
187
+ return DEFAULT_SEQUENCE_STALL_MS;
188
+ }
189
+
124
190
  // --- Named agent model profiles (SP-664 / GitHub #216) ---
125
191
 
126
192
  /**
@@ -14,6 +14,33 @@ export function makeAtomicTempPath(targetPath) {
14
14
  return `${targetPath}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`;
15
15
  }
16
16
 
17
+ const IGNORABLE_DIR_FSYNC_CODES = new Set(["EISDIR", "EPERM", "EINVAL", "EBADF"]);
18
+
19
+ /**
20
+ * Durability requires flushing the parent directory entry after the rename so
21
+ * the rename itself survives a crash. Some platforms (win32, network and
22
+ * exotic filesystems) cannot open or fsync a directory at all, so the four
23
+ * codes that indicate that situation are tolerated; anything else is real I/O
24
+ * trouble and propagates to the caller.
25
+ * @param {string} dir
26
+ */
27
+ function fsyncDirBestEffort(dir) {
28
+ let dirFd;
29
+ try {
30
+ dirFd = fs.openSync(dir, "r");
31
+ } catch (err) {
32
+ if (IGNORABLE_DIR_FSYNC_CODES.has(/** @type {any} */ (err)?.code)) return;
33
+ throw err;
34
+ }
35
+ try {
36
+ fs.fsyncSync(dirFd);
37
+ } catch (err) {
38
+ if (!IGNORABLE_DIR_FSYNC_CODES.has(/** @type {any} */ (err)?.code)) throw err;
39
+ } finally {
40
+ fs.closeSync(dirFd);
41
+ }
42
+ }
43
+
17
44
  /**
18
45
  * @param {string} filePath
19
46
  * @param {string} content
@@ -25,8 +52,17 @@ export function writeTextAtomic(filePath, content) {
25
52
  const tmpPath = makeAtomicTempPath(filePath);
26
53
 
27
54
  try {
28
- fs.writeFileSync(tmpPath, content, "utf-8");
55
+ // Durable temp write: fsync the file contents before the rename so the
56
+ // data reaches disk before the entry swap makes it authoritative (#302).
57
+ const fd = fs.openSync(tmpPath, "w");
58
+ try {
59
+ fs.writeSync(fd, content, null, "utf-8");
60
+ fs.fsyncSync(fd);
61
+ } finally {
62
+ fs.closeSync(fd);
63
+ }
29
64
  fs.renameSync(tmpPath, filePath);
65
+ fsyncDirBestEffort(dir);
30
66
  } catch (err) {
31
67
  try {
32
68
  if (fs.existsSync(tmpPath)) {