pi-spine 2.21.0 → 2.23.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.
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Heal failed-task gates after a successful salvage integrate (#292 / SP-763).
3
+ *
4
+ * When `spine batch salvage --lane N --integrate` lands lane work on the base
5
+ * branch, the active batch state still records the salvaged tasks as failed
6
+ * (status "failed", failed segment, failedTasks counter), so
7
+ * `spine batch complete` keeps refusing (`needs_retry` / `pending_lane_land`)
8
+ * and operators are forced to `dismiss --force`. Once the land succeeded there
9
+ * is no unresolved failure left for those tasks, so the failed-task gate is
10
+ * healed here: each salvageable task is promoted with `recordTaskSucceeded`
11
+ * (task status, segments, and succeeded/failed counters in one step) and a
12
+ * reconciled `task.completed` journal event keeps journal-derived rebuilds in
13
+ * agreement — the same promotion `reconcileBatchStateDrift` applies on
14
+ * done-in-lane terminal evidence.
15
+ *
16
+ * Tasks outside the salvage set are never touched: non-salvageable failures
17
+ * (contract/review failures) and evidence-less worker deaths remain failed and
18
+ * keep blocking complete (SP-763 completion criteria).
19
+ */
20
+
21
+ import { loadBatchStateFile } from "./batch-state-io.mjs";
22
+ import { appendJournalEvent } from "./journal.mjs";
23
+ import { recordTaskSucceeded, saveSpineBatchState } from "./state.mjs";
24
+
25
+ /**
26
+ * Heal the failed-task gate for a lane a salvage integrate just landed, and
27
+ * journal `batch.salvage_heal_failed` when the heal itself fails without
28
+ * failing the integrate — the merge has already landed (#292 / SP-763).
29
+ *
30
+ * @param {string} projectRoot
31
+ * @param {string} batchId
32
+ * @param {object} lane
33
+ * @param {string|null} mergeCommit
34
+ * @returns {{ ok: boolean, healedTaskIds: string[], healError: string|null }}
35
+ */
36
+ export function healAfterSalvageLand(projectRoot, batchId, lane, mergeCommit) {
37
+ const heal = healSalvagedTasksAfterIntegrate(projectRoot, batchId, {
38
+ salvageableTasks: lane.salvageableTasks ?? [],
39
+ mergeCommit,
40
+ });
41
+ if (!heal.ok) {
42
+ appendJournalEvent(projectRoot, batchId, "batch.salvage_heal_failed", {
43
+ laneNumber: lane.laneNumber ?? null,
44
+ salvageableTasks: lane.salvageableTasks ?? [],
45
+ reason: heal.reason,
46
+ error: heal.error,
47
+ });
48
+ }
49
+ return { ok: heal.ok, healedTaskIds: heal.healedTaskIds, healError: heal.ok ? null : heal.error };
50
+ }
51
+
52
+ /**
53
+ * Heal the failed-task gate for tasks whose lane work a salvage integrate just
54
+ * landed on the base branch. Never throws into the salvage path: the merge has
55
+ * already landed, so a heal problem is reported as a result, not a command
56
+ * failure.
57
+ *
58
+ * @param {string} projectRoot
59
+ * @param {string} batchId
60
+ * @param {object} [options]
61
+ * @param {string[]} [options.salvageableTasks] Task ids the salvage landed.
62
+ * @param {string|null} [options.mergeCommit] Salvage merge commit, when known.
63
+ * @returns {{ ok: boolean, healedTaskIds: string[], reason: string|null, error: string|null }}
64
+ */
65
+ export function healSalvagedTasksAfterIntegrate(projectRoot, batchId, options = {}) {
66
+ const salvageableTasks = (options.salvageableTasks ?? []).map((taskId) => String(taskId ?? "")).filter(Boolean);
67
+ const mergeCommit = options.mergeCommit ?? null;
68
+ if (salvageableTasks.length === 0) {
69
+ return { ok: true, healedTaskIds: [], reason: "no_salvageable_tasks", error: null };
70
+ }
71
+
72
+ try {
73
+ const loaded = loadBatchStateFile(projectRoot);
74
+ // Salvage also runs against archived batches; without an active state
75
+ // there is no gate to heal and `complete` is not applicable anyway.
76
+ if (!loaded.path || !loaded.raw) {
77
+ return { ok: true, healedTaskIds: [], reason: "no_active_batch_state", error: null };
78
+ }
79
+ if (loaded.parseError) {
80
+ return { ok: false, healedTaskIds: [], reason: "batch_state_unreadable", error: loaded.parseError };
81
+ }
82
+ const stateBatchId = String(loaded.raw.batchId ?? loaded.raw.id ?? "");
83
+ if (stateBatchId !== batchId) {
84
+ return { ok: true, healedTaskIds: [], reason: "batch_id_mismatch", error: null };
85
+ }
86
+
87
+ const healedTaskIds = [];
88
+ for (const taskId of salvageableTasks) {
89
+ const task = (loaded.raw.tasks ?? []).find(
90
+ (entry) => entry && typeof entry === "object" && entry.taskId === taskId,
91
+ );
92
+ if (!task) continue;
93
+ const status = String(task.status ?? "").toLowerCase();
94
+ if (status === "succeeded" || status === "skipped") continue;
95
+ if (!recordTaskSucceeded(loaded.raw, taskId, { exitReason: "done", doneFileFound: true })) continue;
96
+ healedTaskIds.push(taskId);
97
+ appendJournalEvent(projectRoot, batchId, "task.completed", {
98
+ taskId,
99
+ laneNumber: task.laneNumber ?? null,
100
+ reconciled: true,
101
+ reconcileReason: "salvage_integrated",
102
+ mergeCommit,
103
+ });
104
+ }
105
+
106
+ if (healedTaskIds.length === 0) {
107
+ return { ok: true, healedTaskIds, reason: "nothing_to_heal", error: null };
108
+ }
109
+
110
+ saveSpineBatchState(projectRoot, loaded.raw);
111
+ return { ok: true, healedTaskIds, reason: null, error: null };
112
+ } catch (err) {
113
+ const message = err instanceof Error ? err.message : String(err);
114
+ return { ok: false, healedTaskIds: [], reason: "heal_threw", error: message };
115
+ }
116
+ }
@@ -16,6 +16,7 @@ import {
16
16
  gateBlockedOnMissingGate,
17
17
  openGateFromSalvageEvidence,
18
18
  } from "./salvage-batch-integrate-gate.mjs";
19
+ import { healAfterSalvageLand } from "./salvage-batch-integrate-heal.mjs";
19
20
  import { laneTaskBranch } from "./worktree.mjs";
20
21
  import { listSalvageableLanes } from "./salvage-batch-list.mjs";
21
22
 
@@ -141,10 +142,15 @@ export async function integrateSalvageableLane(projectRoot, batchId, laneNumber,
141
142
  }
142
143
 
143
144
  if (commitsAhead <= 0) {
145
+ // Lane work is already fully merged (manual land or re-run) — heal the
146
+ // failed-task gate on the same landed-work evidence (#292 / SP-763).
147
+ const heal = healAfterSalvageLand(projectRoot, resolvedBatchId, lane, null);
144
148
  return {
145
149
  ok: true,
146
150
  exitCode: 0,
147
151
  alreadyMerged: true,
152
+ healedTaskIds: heal.healedTaskIds,
153
+ healError: heal.healError,
148
154
  batchId: resolvedBatchId,
149
155
  laneNumber: laneNum,
150
156
  baseBranch,
@@ -385,6 +391,9 @@ export async function integrateSalvageableLane(projectRoot, batchId, laneNumber,
385
391
  };
386
392
  }
387
393
 
394
+ // Landed lane work clears the salvaged tasks' failure gate (#292 / SP-763).
395
+ const heal = healAfterSalvageLand(projectRoot, resolvedBatchId, lane, mergeCommit);
396
+
388
397
  appendJournalEvent(projectRoot, resolvedBatchId, "batch.salvage_integrated", {
389
398
  laneNumber: laneNum,
390
399
  taskBranch,
@@ -393,6 +402,7 @@ export async function integrateSalvageableLane(projectRoot, batchId, laneNumber,
393
402
  commitsAhead,
394
403
  salvageableTasks: lane.salvageableTasks,
395
404
  excludedTasks: lane.excludedTasks,
405
+ healedTaskIds: heal.healedTaskIds,
396
406
  });
397
407
 
398
408
  return {
@@ -406,6 +416,8 @@ export async function integrateSalvageableLane(projectRoot, batchId, laneNumber,
406
416
  commitsAhead,
407
417
  salvageableTasks: lane.salvageableTasks,
408
418
  excludedTasks: lane.excludedTasks,
419
+ healedTaskIds: heal.healedTaskIds,
420
+ healError: heal.healError,
409
421
  gateOpenedBySalvage,
410
422
  headline: `Salvaged lane ${laneNum} (${taskBranch}) into ${baseBranch}`,
411
423
  suggestedCommand: "spine status --diagnose",
@@ -455,6 +467,12 @@ export function formatSalvageIntegrateOutput(result, options = {}) {
455
467
  if (result.mergeCommit) {
456
468
  lines.push(` Merge commit: ${result.mergeCommit}`);
457
469
  }
470
+ if (result.healedTaskIds?.length) {
471
+ lines.push(` Healed tasks: ${result.healedTaskIds.join(", ")}`);
472
+ }
473
+ if (result.healError) {
474
+ lines.push(` Heal failed: ${result.healError} — failed-task gate not cleared (see journal)`);
475
+ }
458
476
 
459
477
  if (result.suggestedCommand) {
460
478
  lines.push("", ` → ${result.suggestedCommand}`);
@@ -0,0 +1,178 @@
1
+ import { execFileSync } from "node:child_process";
2
+
3
+ const TRACKED_GITIGNORED_CHECK_ID = "tracked-gitignored";
4
+ const TRACKED_GITIGNORED_LABEL = "tracked gitignored paths";
5
+ const PREVIEW_LIMIT = 3;
6
+
7
+ /**
8
+ * @typedef {object} TrackedGitignoredPathsResult
9
+ * @property {string[]} paths Tracked paths that also match ignore patterns.
10
+ * @property {string | null} error Non-null when the git query itself failed.
11
+ */
12
+
13
+ /**
14
+ * @typedef {object} TrackedGitignoredPreflightCheck
15
+ * @property {"tracked-gitignored"} id
16
+ * @property {true} ok Always advisory — this check never blocks preflight (#289).
17
+ * @property {string} message
18
+ * @property {true} [warning]
19
+ * @property {{ paths: string[] }} [details]
20
+ * @property {string} [suggestedCommand]
21
+ */
22
+
23
+ /**
24
+ * @typedef {object} TrackedGitignoredDoctorCheck
25
+ * @property {"tracked gitignored paths"} label
26
+ * @property {true} ok Always advisory — this check never increments issueCount (#289).
27
+ * @property {true} [warning]
28
+ * @property {string} detail
29
+ * @property {string} [suggestedCommand]
30
+ */
31
+
32
+ /**
33
+ * @param {"tracked-gitignored"} id
34
+ * @param {true} ok
35
+ * @param {string} message
36
+ * @param {Partial<Omit<TrackedGitignoredPreflightCheck, "id" | "ok" | "message">>} [extra]
37
+ * @returns {TrackedGitignoredPreflightCheck}
38
+ */
39
+ function makeCheck(id, ok, message, extra = {}) {
40
+ return { id, ok, message, ...extra };
41
+ }
42
+
43
+ /**
44
+ * List tracked files that also match .gitignore / exclude patterns (issue #289).
45
+ *
46
+ * Bare `git ls-files -i` is fatal on supported git versions ("ls-files -i must
47
+ * be used with either -o or -c"), so `-c` is passed explicitly: the signal we
48
+ * want is exactly "tracked AND ignore-matched". Files that were committed
49
+ * before being ignored keep mutating in lane worktrees and can fail
50
+ * DirtyWorktree after a PASS.
51
+ *
52
+ * @param {string} projectRoot
53
+ * @returns {TrackedGitignoredPathsResult}
54
+ */
55
+ export function listTrackedGitignoredPaths(projectRoot) {
56
+ try {
57
+ const output = execFileSync(
58
+ "git",
59
+ ["ls-files", "-i", "-c", "--exclude-standard"],
60
+ {
61
+ cwd: projectRoot,
62
+ encoding: "utf-8",
63
+ stdio: ["ignore", "pipe", "pipe"],
64
+ timeout: 5000,
65
+ },
66
+ ).trim();
67
+ const paths = output
68
+ .split(/\r?\n/)
69
+ .map((line) => line.trim())
70
+ .filter(Boolean);
71
+ return { paths, error: null };
72
+ } catch (err) {
73
+ return {
74
+ paths: [],
75
+ error: err instanceof Error ? err.message : String(err),
76
+ };
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Bounded remediation command: untrack while keeping the working copy, letting
82
+ * .gitignore keep it out of future commits (#289).
83
+ *
84
+ * @param {string[]} paths
85
+ * @returns {string}
86
+ */
87
+ export function trackedGitignoredRemediation(paths) {
88
+ return `git rm -r --cached -- ${paths.slice(0, PREVIEW_LIMIT).join(" ")}`;
89
+ }
90
+
91
+ /**
92
+ * @param {string[]} paths
93
+ * @returns {string}
94
+ */
95
+ function previewWithSuffix(paths) {
96
+ const preview = paths.slice(0, PREVIEW_LIMIT).join(", ");
97
+ const suffix =
98
+ paths.length > PREVIEW_LIMIT ? ` +${paths.length - PREVIEW_LIMIT} more` : "";
99
+ return `${preview}${suffix}`;
100
+ }
101
+
102
+ /**
103
+ * Advisory preflight check (#289): warn when tracked paths also match
104
+ * .gitignore. Non-blocking by default — operators may track such paths on
105
+ * purpose, so the check never fails; it only suggests the untrack remediation.
106
+ *
107
+ * @param {object} ctx
108
+ * @param {string} ctx.projectRoot
109
+ * @returns {TrackedGitignoredPreflightCheck}
110
+ */
111
+ export function checkTrackedGitignoredWarn(ctx) {
112
+ const { projectRoot } = ctx;
113
+ const { paths, error } = listTrackedGitignoredPaths(projectRoot);
114
+
115
+ if (error) {
116
+ return makeCheck(
117
+ TRACKED_GITIGNORED_CHECK_ID,
118
+ true,
119
+ `tracked gitignored check skipped: ${error}`,
120
+ );
121
+ }
122
+ if (paths.length === 0) {
123
+ return makeCheck(
124
+ TRACKED_GITIGNORED_CHECK_ID,
125
+ true,
126
+ "no tracked files match .gitignore",
127
+ );
128
+ }
129
+
130
+ return makeCheck(
131
+ TRACKED_GITIGNORED_CHECK_ID,
132
+ true,
133
+ `${paths.length} tracked file(s) also match .gitignore (${previewWithSuffix(paths)}) — they mutate outside version control and can fail DirtyWorktree after PASS; untrack but keep working copies, relying on .gitignore`,
134
+ {
135
+ warning: true,
136
+ details: { paths },
137
+ suggestedCommand: trackedGitignoredRemediation(paths),
138
+ },
139
+ );
140
+ }
141
+
142
+ /**
143
+ * Doctor advisory (#289) — same signal as checkTrackedGitignoredWarn, shaped
144
+ * for doctor output ({label, ok, warning, detail, suggestedCommand}).
145
+ *
146
+ * @param {object} ctx
147
+ * @param {string} ctx.projectRoot
148
+ * @returns {TrackedGitignoredDoctorCheck}
149
+ */
150
+ export function buildTrackedGitignoredDoctorCheck(ctx) {
151
+ const { projectRoot } = ctx;
152
+ const { paths, error } = listTrackedGitignoredPaths(projectRoot);
153
+
154
+ if (error) {
155
+ return {
156
+ label: TRACKED_GITIGNORED_LABEL,
157
+ ok: true,
158
+ detail: `skipped: ${error}`,
159
+ };
160
+ }
161
+ if (paths.length === 0) {
162
+ return {
163
+ label: TRACKED_GITIGNORED_LABEL,
164
+ ok: true,
165
+ detail: "none",
166
+ };
167
+ }
168
+
169
+ return {
170
+ label: TRACKED_GITIGNORED_LABEL,
171
+ ok: true,
172
+ warning: true,
173
+ detail:
174
+ `${paths.length} tracked file(s) also match .gitignore (${previewWithSuffix(paths)}) — ` +
175
+ "untrack but keep working copies, relying on .gitignore",
176
+ suggestedCommand: trackedGitignoredRemediation(paths),
177
+ };
178
+ }
@@ -29,6 +29,7 @@ import {
29
29
  runPreflightPlanCheck,
30
30
  } from "./preflight/integrate-plan.mjs";
31
31
  import { checkLocCapstoneReadiness } from "./preflight/loc-capstone.mjs";
32
+ import { checkTrackedGitignoredWarn } from "./preflight/tracked-gitignored.mjs";
32
33
  import { INTEGRATE_DEFAULTS } from "./defaults.mjs";
33
34
  import { parseContract } from "../tasks/packet/parse-prompt.mjs";
34
35
  import { hasReleaseCriticalContract, isStubWorkerMode } from "../batch/contract-verify.mjs";
@@ -78,6 +79,13 @@ export {
78
79
  listPendingLocCapstoneTasks,
79
80
  } from "./preflight/loc-capstone.mjs";
80
81
 
82
+ export {
83
+ buildTrackedGitignoredDoctorCheck,
84
+ checkTrackedGitignoredWarn,
85
+ listTrackedGitignoredPaths,
86
+ trackedGitignoredRemediation,
87
+ } from "./preflight/tracked-gitignored.mjs";
88
+
81
89
  const CONCURRENT_DEV_LABEL = "concurrent development on base branch";
82
90
 
83
91
  /**
@@ -355,6 +363,7 @@ export function runBatchPreflight(options) {
355
363
  checks.push(checkStalePathSpine(ctx));
356
364
 
357
365
  checks.push(checkGitClean(ctx));
366
+ checks.push(checkTrackedGitignoredWarn(ctx));
358
367
  checks.push(
359
368
  buildCoexistencePreflightCheck({
360
369
  projectRoot,
@@ -17,6 +17,7 @@ import {
17
17
  import { buildStalePathDoctorCheck } from "./stale-path.mjs";
18
18
  import { buildCoexistenceDoctorCheck } from "./coexistence.mjs";
19
19
  import { buildConcurrentDevDoctorCheck } from "../config/spine-preflight-lib.mjs";
20
+ import { buildTrackedGitignoredDoctorCheck } from "../config/preflight/tracked-gitignored.mjs";
20
21
  import { buildTaskPacketSizeDoctorCheck } from "./task-packet-size.mjs";
21
22
  import {
22
23
  buildPiWorkerTimeoutDoctorCheck,
@@ -576,6 +577,10 @@ export function runDoctorChecks(projectRoot = process.cwd()) {
576
577
  } catch {
577
578
  // Git ls-files unavailable; skip tracked-metrics advisory.
578
579
  }
580
+
581
+ // Advisory only (#289): tracked files that also match .gitignore mutate in
582
+ // lane worktrees and fail DirtyWorktree after PASS; warn with remediation.
583
+ checks.push(buildTrackedGitignoredDoctorCheck({ projectRoot }));
579
584
  }
580
585
 
581
586
  const rulesRootPath = path.join(projectRoot, CURSOR_RULES_ROOT_REL);