pi-spine 2.2.0 → 2.3.1

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 (56) hide show
  1. package/bin/spine-cli/verify.mjs +15 -46
  2. package/package.json +1 -1
  3. package/src/batch/attached-runner-promote.mjs +370 -0
  4. package/src/batch/attached-runner-reconcile.mjs +291 -0
  5. package/src/batch/attached-runner.mjs +24 -645
  6. package/src/batch/batch-guards.mjs +69 -0
  7. package/src/batch/contract-exec.mjs +483 -0
  8. package/src/batch/contract-parse.mjs +243 -0
  9. package/src/batch/contract-verify.mjs +32 -707
  10. package/src/batch/detached-diagnostics.mjs +255 -0
  11. package/src/batch/detached-run.mjs +326 -0
  12. package/src/batch/detached-start.mjs +18 -894
  13. package/src/batch/detached-wait.mjs +346 -0
  14. package/src/batch/diagnosis.mjs +35 -4
  15. package/src/batch/engine-lanes/orch-sync.mjs +126 -0
  16. package/src/batch/engine-lanes.mjs +69 -0
  17. package/src/batch/engine.mjs +8 -67
  18. package/src/batch/integrate-git.mjs +21 -0
  19. package/src/batch/integrate.mjs +5 -15
  20. package/src/batch/journal-rebuild-structural.mjs +352 -0
  21. package/src/batch/journal-rebuild.mjs +16 -348
  22. package/src/batch/lane-dirty-check-commit.mjs +361 -0
  23. package/src/batch/lane-dirty-check-git.mjs +298 -0
  24. package/src/batch/lane-dirty-check.mjs +31 -297
  25. package/src/batch/reconcile-batch.mjs +495 -0
  26. package/src/batch/reconcile-classify.mjs +433 -0
  27. package/src/batch/reconcile-diagnosis.mjs +459 -0
  28. package/src/batch/reconcile-light-cache.mjs +61 -0
  29. package/src/batch/reconcile-orphan.mjs +198 -0
  30. package/src/batch/reconcile.mjs +19 -1693
  31. package/src/batch/resume-multi-lanes.mjs +6 -148
  32. package/src/batch/resume-multi-queue.mjs +159 -0
  33. package/src/batch/resume-single-validate.mjs +25 -0
  34. package/src/batch/resume.mjs +3 -20
  35. package/src/batch/review-artifacts.mjs +419 -0
  36. package/src/batch/review-step-run.mjs +383 -0
  37. package/src/batch/review-step.mjs +451 -0
  38. package/src/batch/review.mjs +32 -1199
  39. package/src/batch/salvage-batch-list.mjs +291 -0
  40. package/src/batch/salvage-batch.mjs +10 -284
  41. package/src/batch/sequence-plan.mjs +234 -0
  42. package/src/batch/sequence-run.mjs +418 -0
  43. package/src/batch/sequence-wait.mjs +167 -0
  44. package/src/batch/sequence.mjs +22 -783
  45. package/src/batch/state-guards.mjs +284 -0
  46. package/src/batch/state-io.mjs +135 -0
  47. package/src/batch/state.mjs +27 -396
  48. package/src/batch/worker-heartbeat.mjs +382 -0
  49. package/src/batch/worker-host.mjs +56 -548
  50. package/src/batch/worker-spawn.mjs +326 -0
  51. package/src/batch/worktree.mjs +68 -0
  52. package/src/config/preflight/integrate-plan.mjs +6 -1
  53. package/src/config/preflight/loc-capstone.mjs +274 -0
  54. package/src/config/spine-preflight-lib.mjs +14 -0
  55. package/src/planner/index.mjs +14 -3
  56. package/src/process/terminate-tree.mjs +154 -0
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Sequence wait/land helpers: batch terminal poll and wave land loop (SP-600).
3
+ */
4
+
5
+ import { loadSpineConfig } from "../config/spine-config-load.mjs";
6
+ import { DEFAULT_SEQUENCE_POLL_MS } from "../config/spine-config-schema.mjs";
7
+ import { isProcessAlive } from "../process/liveness.mjs";
8
+ import { approveIntegrateGate, loadGateRecord } from "./gate.mjs";
9
+ import { integrateOrchToBase } from "./integrate.mjs";
10
+ import { completeBatch } from "./lifecycle.mjs";
11
+ import { reconcileBatch } from "./reconcile.mjs";
12
+ import { loadSpineBatchState } from "./state.mjs";
13
+
14
+ const WAVE_BATCH_SETTLED_DIAGNOSES = new Set([
15
+ "completed",
16
+ "completed_manual",
17
+ "needs_integrate",
18
+ "limbo_stale",
19
+ ]);
20
+ const WAVE_BATCH_FAILURE_DIAGNOSES = new Set(["failed", "aborted"]);
21
+ const WAVE_BATCH_WAITING_DIAGNOSES = new Set([
22
+ "running",
23
+ "paused",
24
+ "needs_retry",
25
+ "worker_orphaned",
26
+ "engine_orphaned",
27
+ "state_drift",
28
+ "needs_merge",
29
+ "needs_replan",
30
+ ]);
31
+
32
+ function sleep(ms) {
33
+ return new Promise((resolve) => setTimeout(resolve, ms));
34
+ }
35
+
36
+ export function isSequenceBatchSettled(diagnosis) {
37
+ return Boolean(diagnosis && WAVE_BATCH_SETTLED_DIAGNOSES.has(diagnosis));
38
+ }
39
+
40
+ export function isSequenceBatchFailure(diagnosis) {
41
+ return Boolean(diagnosis && WAVE_BATCH_FAILURE_DIAGNOSES.has(diagnosis));
42
+ }
43
+
44
+ export function isSequenceBatchWaiting(diagnosis) {
45
+ return Boolean(diagnosis && WAVE_BATCH_WAITING_DIAGNOSES.has(diagnosis));
46
+ }
47
+
48
+ export async function waitForSequenceBatchTerminal({
49
+ projectRoot,
50
+ pollIntervalMs = DEFAULT_SEQUENCE_POLL_MS,
51
+ timeoutMs = 120_000,
52
+ enginePid = null,
53
+ reconcileFn = reconcileBatch,
54
+ }) {
55
+ const deadline = Date.now() + timeoutMs;
56
+ let useLightReconcile = false;
57
+ while (Date.now() < deadline || isEngineStillRunning(enginePid, projectRoot)) {
58
+ const reconciliation = reconcileFn({ projectRoot, light: useLightReconcile });
59
+ useLightReconcile = true;
60
+ const diagnosis = reconciliation.diagnosis;
61
+ if (isSequenceBatchFailure(diagnosis)) {
62
+ return {
63
+ ok: false,
64
+ halted: true,
65
+ diagnosis,
66
+ reconciliation,
67
+ batchId: reconciliation.batchId ?? null,
68
+ };
69
+ }
70
+ if (isSequenceBatchSettled(diagnosis)) {
71
+ return {
72
+ ok: true,
73
+ diagnosis,
74
+ reconciliation,
75
+ batchId: reconciliation.batchId ?? null,
76
+ };
77
+ }
78
+ await sleep(pollIntervalMs);
79
+ }
80
+ const reconciliation = reconcileFn({ projectRoot });
81
+ return {
82
+ ok: false,
83
+ error: "timeout_waiting_for_batch",
84
+ diagnosis: reconciliation.diagnosis ?? null,
85
+ reconciliation,
86
+ batchId: reconciliation.batchId ?? null,
87
+ };
88
+ }
89
+
90
+ /**
91
+ * Returns true when a detached engine PID is alive or the batch phase is active,
92
+ * preventing sequence timeout while work is still in progress.
93
+ */
94
+ function isEngineStillRunning(enginePid, projectRoot) {
95
+ if (enginePid && isProcessAlive(enginePid)) return true;
96
+ const { raw } = loadSpineBatchState(projectRoot);
97
+ const pid = raw?.enginePid ?? null;
98
+ if (pid && isProcessAlive(pid)) return true;
99
+ return false;
100
+ }
101
+
102
+ /**
103
+ * @param {object} params
104
+ * @param {string} params.projectRoot
105
+ * @param {string|null} params.batchId
106
+ * @param {boolean} [params.autoApproveGate]
107
+ */
108
+ export function runSequenceWaveLandLoop({ projectRoot, batchId, autoApproveGate = false }) {
109
+ const reconciliation = reconcileBatch({ projectRoot });
110
+ const diagnosis = reconciliation.diagnosis;
111
+ const activeBatchId =
112
+ batchId ?? reconciliation.batchId ?? loadSpineBatchState(projectRoot).raw?.batchId ?? null;
113
+
114
+ if (!isSequenceBatchSettled(diagnosis)) {
115
+ return {
116
+ ok: false,
117
+ step: "land_loop",
118
+ error: "batch_not_settled",
119
+ diagnosis,
120
+ batchId: activeBatchId,
121
+ headline: `Cannot land wave — batch diagnosis is ${diagnosis ?? "unknown"}`,
122
+ };
123
+ }
124
+
125
+ const config = loadSpineConfig(projectRoot).config ?? {};
126
+ const gateRequired = config.gates?.requireBeforeIntegrate !== false;
127
+ const gate = activeBatchId ? loadGateRecord(projectRoot, activeBatchId) : null;
128
+
129
+ if (gateRequired && gate?.status === "pending") {
130
+ if (!autoApproveGate) {
131
+ return {
132
+ ok: false,
133
+ step: "gate_approve",
134
+ error: "gate_approval_required",
135
+ diagnosis,
136
+ batchId: activeBatchId,
137
+ headline: "Integrate gate requires approval — pass autoApproveGate or approve manually",
138
+ suggestedCommand: "spine gate approve",
139
+ };
140
+ }
141
+ const approve = approveIntegrateGate({ projectRoot, batchId: activeBatchId });
142
+ if (!approve.ok) {
143
+ return { ok: false, step: "gate_approve", diagnosis, batchId: activeBatchId, ...approve };
144
+ }
145
+ }
146
+
147
+ const integrate = integrateOrchToBase({ projectRoot, batchId: activeBatchId });
148
+ if (!integrate.ok) {
149
+ return { ok: false, step: "integrate", diagnosis, batchId: activeBatchId, ...integrate };
150
+ }
151
+
152
+ const complete = completeBatch({ projectRoot, batchId: activeBatchId });
153
+ if (!complete.ok) {
154
+ return { ok: false, step: "complete", diagnosis, batchId: activeBatchId, ...complete };
155
+ }
156
+
157
+ return {
158
+ ok: true,
159
+ diagnosis,
160
+ batchId: activeBatchId,
161
+ headline: `Wave batch ${activeBatchId} landed on main`,
162
+ };
163
+ }
164
+
165
+ /**
166
+ * @param {object} ctx
167
+ */