pi-spine 2.4.0 → 2.6.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 (38) hide show
  1. package/bin/spine.mjs +22 -0
  2. package/package.json +1 -1
  3. package/src/batch/attached-runner-promote.mjs +95 -11
  4. package/src/batch/blocker-codes.mjs +63 -0
  5. package/src/batch/diagnosis-alternatives.mjs +18 -0
  6. package/src/batch/diagnosis-gate-ready.mjs +23 -0
  7. package/src/batch/diagnosis-pending-lane.mjs +63 -0
  8. package/src/batch/diagnosis-retry-command.mjs +23 -0
  9. package/src/batch/diagnosis-tail-state.mjs +68 -0
  10. package/src/batch/diagnosis.mjs +69 -52
  11. package/src/batch/engine-lanes/commit.mjs +2 -0
  12. package/src/batch/engine-lanes.mjs +2 -3
  13. package/src/batch/evidence-command.mjs +125 -4
  14. package/src/batch/gate-posture-approve.mjs +345 -0
  15. package/src/batch/gate-posture-defaults.mjs +64 -0
  16. package/src/batch/gate-posture-evaluate.mjs +133 -0
  17. package/src/batch/gate-posture-streak.mjs +290 -0
  18. package/src/batch/gate-revision.mjs +95 -0
  19. package/src/batch/gate.mjs +89 -117
  20. package/src/batch/integrate.mjs +1 -0
  21. package/src/batch/lane-commit.mjs +77 -8
  22. package/src/batch/lifecycle-archive.mjs +71 -0
  23. package/src/batch/lifecycle.mjs +27 -67
  24. package/src/batch/orphan-detect.mjs +68 -5
  25. package/src/batch/post-merge-limbo.mjs +137 -22
  26. package/src/batch/reconcile-batch.mjs +40 -43
  27. package/src/batch/reconcile-diagnosis-context.mjs +88 -0
  28. package/src/batch/reconcile-diagnosis.mjs +14 -32
  29. package/src/batch/resume-multi-validate.mjs +51 -9
  30. package/src/batch/retry.mjs +18 -2
  31. package/src/batch/sequence-wait.mjs +20 -6
  32. package/src/config/gate-posture-config.mjs +190 -0
  33. package/src/config/pi-spine-root.mjs +22 -0
  34. package/src/config/spine-config-load.mjs +33 -0
  35. package/src/doctor/duplicate-install.mjs +100 -0
  36. package/src/doctor/run-doctor-checks.mjs +10 -1
  37. package/src/doctor/worktree-health.mjs +4 -0
  38. package/templates/spine-config.json +3 -2
@@ -23,8 +23,14 @@ export {
23
23
  inferMergeGitignoredFailure,
24
24
  summarizeMergeFailures,
25
25
  } from "./diagnosis-merge-failure.mjs";
26
- import { buildRunningTailHeadline } from "./diagnosis-tail-state.mjs";
27
- export { buildRunningTailHeadline, isRunningWithoutActiveWorkers } from "./diagnosis-tail-state.mjs";
26
+ import { buildRunningTailHeadline, isPostIntegrateEngineLimbo, buildPostIntegrateEngineLimboHeadline } from "./diagnosis-tail-state.mjs";
27
+ export {
28
+ buildPostIntegrateEngineLimboHeadline,
29
+ buildRunningTailHeadline,
30
+ isEngineStillRunning,
31
+ isPostIntegrateEngineLimbo,
32
+ isRunningWithoutActiveWorkers,
33
+ } from "./diagnosis-tail-state.mjs";
28
34
  export { buildReviewHonorHeadlineSuffix, findLatestReviewHonorSignal } from "./review.mjs";
29
35
  import {
30
36
  buildStubFailureHeadline,
@@ -53,44 +59,20 @@ export {
53
59
  } from "./diagnosis-parent-exit.mjs";
54
60
  import { buildReviewHonorHeadlineSuffix } from "./review.mjs";
55
61
  export { inferLaunchFailureFromWorkerOutputTail, inferLaunchFailureKind } from "./diagnosis-launch-failure.mjs";
56
-
57
- const INVALID_BARE_RETRY_FORCE = /^spine batch retry --force$/;
58
-
59
- /**
60
- * Gate-ready batches must not headline historical merge/gitignored blockers (#195 / FR-REL231-01).
61
- *
62
- * @param {string} diagnosis
63
- * @param {object} [ctx]
64
- * @param {boolean} [ctx.allTasksTerminalSuccess]
65
- * @param {boolean} [ctx.integrateGateOpen]
66
- * @returns {boolean}
67
- */
68
- export function isGateReadyHeadlineContext(diagnosis, ctx = {}) {
69
- if (diagnosis === "needs_integrate") {
70
- return true;
71
- }
72
- if (ctx.allTasksTerminalSuccess === true && ctx.integrateGateOpen === true) {
73
- return true;
74
- }
75
- return false;
76
- }
77
-
78
- /**
79
- * Retry suggestions must include a task id; journal salvage payloads may be stale.
80
- *
81
- * @param {string|null|undefined} command
82
- * @param {string|null|undefined} taskId
83
- * @returns {string|null}
84
- */
85
- export function sanitizeRetrySuggestedCommand(command, taskId) {
86
- if (typeof command !== "string" || command.length === 0) {
87
- return command ?? null;
88
- }
89
- if (INVALID_BARE_RETRY_FORCE.test(command.trim())) {
90
- return taskId ? `spine batch retry ${taskId}` : "spine status --diagnose";
91
- }
92
- return command;
93
- }
62
+ import { isGateReadyHeadlineContext } from "./diagnosis-gate-ready.mjs";
63
+ export { isGateReadyHeadlineContext };
64
+ import { sanitizeRetrySuggestedCommand } from "./diagnosis-retry-command.mjs";
65
+ export { sanitizeRetrySuggestedCommand };
66
+ import {
67
+ buildPendingLaneLandHeadline,
68
+ buildPendingLaneLandSuggestedCommand,
69
+ } from "./diagnosis-pending-lane.mjs";
70
+ export {
71
+ buildPendingLaneLandHeadline,
72
+ buildPendingLaneLandSuggestedCommand,
73
+ findPendingLaneLandTasks,
74
+ shouldDiagnosePendingLaneLand,
75
+ } from "./diagnosis-pending-lane.mjs";
94
76
 
95
77
  export const DIAGNOSIS_TAXONOMY = [
96
78
  "running",
@@ -108,6 +90,8 @@ export const DIAGNOSIS_TAXONOMY = [
108
90
  "limbo_stale",
109
91
  "human_base_diverged",
110
92
  "integrate_isolated_ok",
93
+ "pending_lane_land",
94
+ "engine_still_running",
111
95
  "failed",
112
96
  "aborted",
113
97
  ];
@@ -118,6 +102,7 @@ const NO_PAUSE_DIAGNOSES = new Set([
118
102
  "needs_integrate",
119
103
  "engine_orphaned",
120
104
  "integrate_isolated_ok",
105
+ "engine_still_running",
121
106
  ]);
122
107
  const REVIEW_SPAWN_FAILURE_EXIT_REASONS = new Set([
123
108
  "code_review_spawn_failed",
@@ -138,6 +123,7 @@ const REVIEW_SPAWN_FAILURE_EXIT_REASONS = new Set([
138
123
  * @param {boolean} [ctx.mergeFailed]
139
124
  * @param {string|null} [ctx.taskBranch]
140
125
  * @param {string[]|null} [ctx.gitignoredPaths]
126
+ * @param {object[]} [ctx.pendingLaneLandTasks]
141
127
  */
142
128
  export function buildSuggestedCommand(diagnosis, ctx = {}) {
143
129
  const preferGateReady = isGateReadyHeadlineContext(diagnosis, ctx);
@@ -157,6 +143,8 @@ export function buildSuggestedCommand(diagnosis, ctx = {}) {
157
143
  }
158
144
 
159
145
  switch (diagnosis) {
146
+ case "engine_still_running":
147
+ return "spine wait --until completed,failed,needs_integrate --timeout 2h";
160
148
  case "limbo_stale":
161
149
  case "completed_manual":
162
150
  return "spine batch dismiss";
@@ -182,10 +170,8 @@ export function buildSuggestedCommand(diagnosis, ctx = {}) {
182
170
  if (STUB_EXIT_REASONS.has(ctx.exitReason ?? "")) {
183
171
  return buildStubFailureSuggestedCommand(ctx);
184
172
  }
185
- {
186
- const primaryCommand = buildPrimaryFailureSuggestedCommand(ctx);
187
- if (primaryCommand) return primaryCommand;
188
- }
173
+ const primaryCommand = buildPrimaryFailureSuggestedCommand(ctx);
174
+ if (primaryCommand) return primaryCommand;
189
175
  if (ctx.launchFailureKind === "pi_spine_root" || ctx.launchFailureKind === "launch_failed") {
190
176
  return "spine doctor";
191
177
  }
@@ -274,6 +260,8 @@ export function buildSuggestedCommand(diagnosis, ctx = {}) {
274
260
  return "spine sync-base";
275
261
  case "integrate_isolated_ok":
276
262
  return "spine sync-base";
263
+ case "pending_lane_land":
264
+ return buildPendingLaneLandSuggestedCommand(ctx.batchId, ctx.pendingLaneLandTasks ?? []);
277
265
  default:
278
266
  return "spine status --diagnose";
279
267
  }
@@ -298,6 +286,8 @@ export function buildSuggestedCommand(diagnosis, ctx = {}) {
298
286
  * @param {string|null} [ctx.lastError]
299
287
  * @param {number} [ctx.succeededTasks]
300
288
  * @param {number} [ctx.totalTasks]
289
+ * @param {object[]} [ctx.pendingLaneLandTasks]
290
+ * @param {string|null|undefined} [ctx.baseBranch]
301
291
  */
302
292
  export function buildHeadline(diagnosis, ctx = {}) {
303
293
  const batchLabel = ctx.batchId ? `Batch ${ctx.batchId}` : "Batch";
@@ -317,6 +307,17 @@ export function buildHeadline(diagnosis, ctx = {}) {
317
307
  }
318
308
 
319
309
  switch (diagnosis) {
310
+ case "engine_still_running": {
311
+ const enginePid = ctx.enginePid;
312
+ const pidSuffix =
313
+ enginePid != null && Number.isFinite(Number(enginePid))
314
+ ? ` (PID ${enginePid})`
315
+ : "";
316
+ if (ctx.gitMerged === true) {
317
+ return `${batchLabel} resume engine still running after integrate${pidSuffix} — wait, abort, or batch complete after exit`;
318
+ }
319
+ return `${batchLabel} engine still running${pidSuffix} — complete refused`;
320
+ }
320
321
  case "limbo_stale":
321
322
  return `${batchLabel} finished but state is stale — dismiss to clear`;
322
323
  case "completed_manual":
@@ -325,10 +326,8 @@ export function buildHeadline(diagnosis, ctx = {}) {
325
326
  if (STUB_EXIT_REASONS.has(ctx.exitReason ?? "")) {
326
327
  return buildStubFailureHeadline(batchLabel, ctx);
327
328
  }
328
- {
329
- const primaryHeadline = buildPrimaryFailureHeadline(batchLabel, ctx);
330
- if (primaryHeadline) return primaryHeadline;
331
- }
329
+ const primaryHeadline = buildPrimaryFailureHeadline(batchLabel, ctx);
330
+ if (primaryHeadline) return primaryHeadline;
332
331
  if (ctx.launchFailureKind === "pi_spine_root" || ctx.launchFailureKind === "launch_failed") {
333
332
  return `${batchLabel} failed at worker launch — fix PI_SPINE_ROOT/devcontainer, then retry`;
334
333
  }
@@ -413,6 +412,9 @@ export function buildHeadline(diagnosis, ctx = {}) {
413
412
  ? `Task ${ctx.failedTaskId} needs replan — edit PROMPT.md before retry`
414
413
  : `${batchLabel} has tasks needing replan — edit PROMPT.md before retry`;
415
414
  case "running": {
415
+ if (isPostIntegrateEngineLimbo(ctx)) {
416
+ return buildPostIntegrateEngineLimboHeadline(batchLabel, ctx);
417
+ }
416
418
  const reviewHonorSuffix = buildReviewHonorHeadlineSuffix(ctx.reviewHonorSignal);
417
419
  if (reviewHonorSuffix && ctx.reviewHonorSignal?.taskId) {
418
420
  return `${batchLabel} task ${ctx.reviewHonorSignal.taskId}: ${reviewHonorSuffix}`;
@@ -444,6 +446,8 @@ export function buildHeadline(diagnosis, ctx = {}) {
444
446
  }
445
447
  case "integrate_isolated_ok":
446
448
  return `${batchLabel} — isolated integrate landed; sync human checkout with base`;
449
+ case "pending_lane_land":
450
+ return buildPendingLaneLandHeadline(batchLabel, ctx);
447
451
  default:
448
452
  return `${batchLabel} requires attention (phase: ${ctx.phase ?? "unknown"})`;
449
453
  }
@@ -456,16 +460,29 @@ export function shouldNeverSuggestPause(diagnosis) {
456
460
  return NO_PAUSE_DIAGNOSES.has(diagnosis);
457
461
  }
458
462
 
463
+ /**
464
+ * @param {string} diagnosis
465
+ * @param {object} ctx
466
+ * @returns {string}
467
+ */
468
+ export function resolveEffectiveDiagnosis(diagnosis, ctx = {}) {
469
+ if (diagnosis === "running" && isPostIntegrateEngineLimbo(ctx)) {
470
+ return "engine_still_running";
471
+ }
472
+ return diagnosis;
473
+ }
474
+
459
475
  /**
460
476
  * @param {string} diagnosis
461
477
  * @param {object} ctx
462
478
  * @returns {Pick<ReconciliationResult, "diagnosis" | "headline" | "suggestedCommand" | "alternatives">}
463
479
  */
464
480
  export function buildDiagnosisOutput(diagnosis, ctx = {}) {
481
+ const effectiveDiagnosis = resolveEffectiveDiagnosis(diagnosis, ctx);
465
482
  return {
466
- diagnosis,
467
- headline: buildHeadline(diagnosis, ctx),
468
- suggestedCommand: buildSuggestedCommand(diagnosis, ctx),
469
- alternatives: buildAlternatives(diagnosis, ctx),
483
+ diagnosis: effectiveDiagnosis,
484
+ headline: buildHeadline(effectiveDiagnosis, ctx),
485
+ suggestedCommand: buildSuggestedCommand(effectiveDiagnosis, ctx),
486
+ alternatives: buildAlternatives(effectiveDiagnosis, ctx),
470
487
  };
471
488
  }
@@ -72,6 +72,8 @@ export function commitLaneAndValidateWorktree({
72
72
  batchId,
73
73
  taskFolder,
74
74
  projectRoot,
75
+ ignorePatterns,
76
+ fileScopePaths,
75
77
  });
76
78
  if (!laneCommit.ok) {
77
79
  task.status = "failed";
@@ -23,6 +23,7 @@ import {
23
23
  import { runWorker } from "./worker-host.mjs";
24
24
  import { runCodeReviewPhase, runFinalReviewPhase } from "./engine-lanes/review.mjs";
25
25
  import { ensureLaneSyncedForSharedScopeDeps } from "./engine-lanes/orch-sync.mjs";
26
+ import { resolveWorktreeSetupIgnorePaths } from "../config/spine-config-load.mjs";
26
27
 
27
28
  export {
28
29
  buildTasksAndLanesFromPlan,
@@ -347,9 +348,7 @@ export async function runTaskOnLane({
347
348
  }
348
349
  }
349
350
 
350
- const ignorePatterns = Array.isArray(config?.worktreeSetupIgnorePaths)
351
- ? config.worktreeSetupIgnorePaths
352
- : [];
351
+ const ignorePatterns = resolveWorktreeSetupIgnorePaths(config);
353
352
  const commitResult = commitLaneAndValidateWorktree({
354
353
  worktreePath: wt,
355
354
  taskBranch,
@@ -5,6 +5,7 @@
5
5
 
6
6
  import path from "node:path";
7
7
  import { execFileSync } from "node:child_process";
8
+ import { validateWorkerLaunchScriptPath } from "../config/worker-launch-script.mjs";
8
9
 
9
10
  /**
10
11
  * Allowed first-token executables for evidence commands.
@@ -13,6 +14,20 @@ import { execFileSync } from "node:child_process";
13
14
  */
14
15
  export const ALLOWED_EVIDENCE_EXECUTABLES = new Set(["npm", "node", "pnpm", "yarn", "npx"]);
15
16
 
17
+ /**
18
+ * Interpreter basenames allowed only as project-local relative paths under
19
+ * `.venv/` or `venv/` (see {@link isAllowedProjectLocalInterpreter}).
20
+ * Bare names (PATH lookup) and absolute/outside-project paths stay rejected.
21
+ * @type {Set<string>}
22
+ */
23
+ export const ALLOWED_PROJECT_LOCAL_INTERPRETERS = new Set(["python", "python3"]);
24
+
25
+ /** Relative venv roots that may host an allowed interpreter (posix-normalized). */
26
+ const PROJECT_LOCAL_VENV_PREFIXES = [".venv/", "venv/"];
27
+
28
+ /** Relative prefix for validated gate-evidence scripts (posix-normalized). */
29
+ const EVIDENCE_SCRIPTS_PREFIX = "scripts/";
30
+
16
31
  const SHELL_METACHAR_PATTERN = /[;|&`<>]|>>|\$\(|\$\{/;
17
32
 
18
33
  export class EvidenceCommandError extends Error {
@@ -58,11 +73,116 @@ export function parseEvidenceCommandArgv(command) {
58
73
  }
59
74
 
60
75
  const executable = path.basename(argv[0]);
61
- if (!ALLOWED_EVIDENCE_EXECUTABLES.has(executable)) {
62
- throw new EvidenceCommandError(`evidence executable not allowed: ${executable}`);
76
+ if (ALLOWED_EVIDENCE_EXECUTABLES.has(executable)) {
77
+ return argv;
78
+ }
79
+ if (isAllowedProjectLocalInterpreter(argv[0])) {
80
+ return argv;
81
+ }
82
+ if (isAllowedEvidenceScriptsPath(argv[0])) {
83
+ return argv;
84
+ }
85
+
86
+ throw new EvidenceCommandError(`evidence executable not allowed: ${executable}`);
87
+ }
88
+
89
+ /**
90
+ * Allow project-local interpreters for gate evidence (e.g. `.venv/bin/python`).
91
+ *
92
+ * Exact rule: first token must be a relative path (not absolute), with no `..`
93
+ * segments after posix normalization, whose normalized form starts with
94
+ * `.venv/` or `venv/`, and whose basename is in
95
+ * {@link ALLOWED_PROJECT_LOCAL_INTERPRETERS} (`python` or `python3`).
96
+ * Rejects bare interpreter names and absolute/outside-project paths.
97
+ *
98
+ * @param {string} firstToken
99
+ * @returns {boolean}
100
+ */
101
+ export function isAllowedProjectLocalInterpreter(firstToken) {
102
+ if (typeof firstToken !== "string" || !firstToken) {
103
+ return false;
104
+ }
105
+ if (path.isAbsolute(firstToken)) {
106
+ return false;
107
+ }
108
+
109
+ const posixToken = firstToken.replaceAll("\\", "/");
110
+ const segments = posixToken.split("/");
111
+ if (segments.some((segment) => segment === "..")) {
112
+ return false;
113
+ }
114
+
115
+ const normalized = path.posix.normalize(posixToken);
116
+ if (normalized.startsWith("../") || normalized === "..") {
117
+ return false;
118
+ }
119
+ if (path.posix.isAbsolute(normalized)) {
120
+ return false;
121
+ }
122
+
123
+ const underVenv = PROJECT_LOCAL_VENV_PREFIXES.some((prefix) => normalized.startsWith(prefix));
124
+ if (!underVenv) {
125
+ return false;
126
+ }
127
+
128
+ const basename = path.posix.basename(normalized);
129
+ return ALLOWED_PROJECT_LOCAL_INTERPRETERS.has(basename);
130
+ }
131
+
132
+ /**
133
+ * Lightweight parse-time check: first token is a relative path under `scripts/`
134
+ * with no parent traversal. Full symlink/root validation runs at execution via
135
+ * {@link validateWorkerLaunchScriptPath} (same sandbox as workerLaunchScript).
136
+ *
137
+ * @param {string} firstToken
138
+ * @returns {boolean}
139
+ */
140
+ export function isAllowedEvidenceScriptsPath(firstToken) {
141
+ if (typeof firstToken !== "string" || !firstToken) {
142
+ return false;
143
+ }
144
+ if (path.isAbsolute(firstToken)) {
145
+ return false;
146
+ }
147
+
148
+ const posixToken = firstToken.replaceAll("\\", "/");
149
+ const segments = posixToken.split("/");
150
+ if (segments.some((segment) => segment === "..")) {
151
+ return false;
152
+ }
153
+
154
+ const normalized = path.posix.normalize(posixToken);
155
+ if (normalized.startsWith("../") || normalized === "..") {
156
+ return false;
157
+ }
158
+ if (path.posix.isAbsolute(normalized)) {
159
+ return false;
160
+ }
161
+
162
+ return normalized.startsWith(EVIDENCE_SCRIPTS_PREFIX);
163
+ }
164
+
165
+ /**
166
+ * Resolve and validate a scripts/ evidence argv for execFile (no shell).
167
+ *
168
+ * @param {string} projectRoot
169
+ * @param {string[]} argv
170
+ * @returns {string[]}
171
+ */
172
+ export function resolveEvidenceScriptsArgv(projectRoot, argv) {
173
+ if (!isAllowedEvidenceScriptsPath(argv[0])) {
174
+ return argv;
175
+ }
176
+
177
+ const validated = validateWorkerLaunchScriptPath(projectRoot, argv[0]);
178
+ if (!validated.ok) {
179
+ throw new EvidenceCommandError(validated.message ?? "evidence script path invalid");
180
+ }
181
+ if (!validated.scriptPath) {
182
+ throw new EvidenceCommandError("evidence script path invalid");
63
183
  }
64
184
 
65
- return argv;
185
+ return [validated.scriptPath, ...argv.slice(1)];
66
186
  }
67
187
 
68
188
  /**
@@ -128,7 +248,8 @@ export function runEvidenceCommand(projectRoot, command, maxBytes = 256 * 1024)
128
248
 
129
249
  try {
130
250
  const argv = parseEvidenceCommandArgv(command);
131
- const output = execFileSync(argv[0], argv.slice(1), {
251
+ const execArgv = resolveEvidenceScriptsArgv(projectRoot, argv);
252
+ const output = execFileSync(execArgv[0], execArgv.slice(1), {
132
253
  cwd: projectRoot,
133
254
  encoding: "utf-8",
134
255
  stdio: ["ignore", "pipe", "pipe"],