pi-spine 2.23.0 → 2.25.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 (33) hide show
  1. package/README.md +1 -1
  2. package/bin/spine-worker-runner.mjs +2 -1
  3. package/package.json +5 -5
  4. package/src/batch/attached-runner-promote.mjs +14 -0
  5. package/src/batch/batch-state-io.mjs +120 -45
  6. package/src/batch/engine-crash-guard.mjs +143 -0
  7. package/src/batch/engine-lanes/matrix-run.mjs +28 -2
  8. package/src/batch/engine-lanes/matrix.mjs +143 -8
  9. package/src/batch/integrate-dirty-overlap.mjs +35 -0
  10. package/src/batch/integrate-worktree.mjs +106 -13
  11. package/src/batch/integrate.mjs +34 -93
  12. package/src/batch/journal-checksum.mjs +26 -0
  13. package/src/batch/journal.mjs +32 -13
  14. package/src/batch/lifecycle-archive.mjs +8 -2
  15. package/src/batch/lifecycle.mjs +2 -2
  16. package/src/batch/reconcile-batch.mjs +26 -19
  17. package/src/batch/reconcile-classify.mjs +21 -11
  18. package/src/batch/reconcile-diagnosis-context.mjs +1 -2
  19. package/src/batch/reconcile-diagnosis.mjs +18 -19
  20. package/src/batch/reconcile-light-cache.mjs +0 -1
  21. package/src/batch/reconcile-orphan.mjs +9 -8
  22. package/src/batch/reconcile.mjs +0 -1
  23. package/src/batch/salvage-batch-integrate.mjs +13 -17
  24. package/src/batch/state.mjs +22 -1
  25. package/src/batch/worker-heartbeat.mjs +29 -8
  26. package/src/batch/worker-host.mjs +29 -4
  27. package/src/batch/worker-output.mjs +9 -6
  28. package/src/batch/worker-prompt.mjs +4 -2
  29. package/src/batch/worker-spawn.mjs +99 -12
  30. package/src/config/preflight/discovery.mjs +20 -12
  31. package/src/doctor/run-doctor-checks.mjs +41 -3
  32. package/src/planner/scope.mjs +3 -1
  33. package/src/tasks/packet/discover.mjs +59 -3
@@ -211,19 +211,38 @@ export function exportJournalJsonl(projectRoot, batchId) {
211
211
  }
212
212
 
213
213
  /**
214
- * Parse jsonl journal content into normalized events. Lines whose checksum is
215
- * present but mismatches are skipped (fail-closed per-line) without
216
- * discarding the rest of the file; legacy lines without checksum load as-is.
214
+ * Parse jsonl journal content into normalized events, tolerating torn lines.
215
+ * Each line is parsed in its own try/catch: a line that fails JSON.parse
216
+ * (torn write from a crash or disk-full) or whose present checksum mismatches
217
+ * is skipped (fail-closed per-line) without discarding the rest of the file;
218
+ * legacy lines without checksum load as-is. Alongside the events, returns
219
+ * skipped-line metadata (1-based lineNumber, byteOffset, reason) so diagnosis
220
+ * surfaces can report corruption instead of losing every valid event.
217
221
  * @param {string} content Raw jsonl content.
218
- * @returns {object[]}
222
+ * @returns {{ events: object[], skippedLines: Array<{ lineNumber: number, byteOffset: number, reason: string }> }}
219
223
  */
220
- function parseJournalLines(content) {
221
- return content
222
- .split("\n")
223
- .filter(Boolean)
224
- .map((line) => JSON.parse(line))
225
- .filter((parsed) => verifyJournalChecksum(parsed))
226
- .map((parsed) => normalizeJournalEvent(parsed));
224
+ export function parseJournalLines(content) {
225
+ /** @type {object[]} */
226
+ const events = [];
227
+ /** @type {Array<{ lineNumber: number, byteOffset: number, reason: string }>} */
228
+ const skippedLines = [];
229
+ let byteOffset = 0;
230
+ for (const [index, line] of content.split("\n").entries()) {
231
+ if (line) {
232
+ try {
233
+ const parsed = JSON.parse(line);
234
+ if (verifyJournalChecksum(parsed)) {
235
+ events.push(normalizeJournalEvent(parsed));
236
+ } else {
237
+ skippedLines.push({ lineNumber: index + 1, byteOffset, reason: "checksum_mismatch" });
238
+ }
239
+ } catch {
240
+ skippedLines.push({ lineNumber: index + 1, byteOffset, reason: "json_parse_error" });
241
+ }
242
+ }
243
+ byteOffset += Buffer.byteLength(line, "utf-8") + 1;
244
+ }
245
+ return { events, skippedLines };
227
246
  }
228
247
 
229
248
  /**
@@ -235,7 +254,7 @@ export function readJournalEvents(projectRoot, batchId) {
235
254
  const filePath = journalPath(projectRoot, batchId);
236
255
  if (!fs.existsSync(filePath)) return [];
237
256
 
238
- return parseJournalLines(fs.readFileSync(filePath, "utf-8"));
257
+ return parseJournalLines(fs.readFileSync(filePath, "utf-8")).events;
239
258
  }
240
259
 
241
260
  /**
@@ -268,7 +287,7 @@ export function readJournalEventsCached(projectRoot, batchId) {
268
287
  return _journalCache.events;
269
288
  }
270
289
 
271
- const events = parseJournalLines(fs.readFileSync(filePath, "utf-8"));
290
+ const events = parseJournalLines(fs.readFileSync(filePath, "utf-8")).events;
272
291
 
273
292
  _journalCache.filePath = filePath;
274
293
  _journalCache.mtimeMs = stat.mtimeMs;
@@ -38,11 +38,17 @@ export function archiveBatchState(projectRoot, batchId, raw) {
38
38
  }
39
39
 
40
40
  /**
41
+ * Clear the completed batch-state pointer under the global batch-state lock
42
+ * (SP-722 / #264; SP-786 / #303). Delegates to `clearActiveBatchStateIfMatches`,
43
+ * which quarantines corrupt state instead of deleting it and refuses to touch
44
+ * Taskplane-owned `.pi/` state files.
45
+ *
46
+ * @param {string} projectRoot
41
47
  * @param {string|null} batchStatePath
42
48
  * @param {string} batchId
43
49
  */
44
- export function clearCompletedBatchState(batchStatePath, batchId) {
45
- clearActiveBatchStateIfMatches(batchStatePath, batchId);
50
+ export function clearCompletedBatchState(projectRoot, batchStatePath, batchId) {
51
+ clearActiveBatchStateIfMatches(batchStatePath, batchId, projectRoot);
46
52
  }
47
53
 
48
54
  /**
@@ -247,7 +247,7 @@ export function dismissBatch(ctx) {
247
247
  batchState: loaded.raw,
248
248
  config,
249
249
  });
250
- clearCompletedBatchState(loaded.path, batchId);
250
+ clearCompletedBatchState(projectRoot, loaded.path, batchId);
251
251
  bumpDashboardInvalidateSignal(projectRoot, "batch_dismiss", batchId);
252
252
 
253
253
  return {
@@ -453,7 +453,7 @@ export function completeBatch(ctx) {
453
453
  batchState: loaded.raw,
454
454
  config,
455
455
  });
456
- clearCompletedBatchState(loaded.path, batchId);
456
+ clearCompletedBatchState(projectRoot, loaded.path, batchId);
457
457
 
458
458
  return {
459
459
  ok: true,
@@ -1,4 +1,3 @@
1
- // @ts-nocheck
2
1
  /** Main reconcileBatch orchestration (SP-606 / #192). */
3
2
 
4
3
  import fs from "node:fs";
@@ -33,6 +32,7 @@ import {
33
32
  extractJournalDiagnosisHints,
34
33
  findPlanReviewNestedSpawnBlockedFailure,
35
34
  journalPath,
35
+ parseJournalLines,
36
36
  readJournalEvents,
37
37
  } from "./journal.mjs";
38
38
  import { findLatestSalvageInspection } from "./salvage.mjs";
@@ -126,7 +126,7 @@ export function reconcileBatch(ctx, _lightRetry = false) {
126
126
  }
127
127
 
128
128
  const tasksRoot = resolveTasksRoot(projectRoot);
129
- let classifiedTasks = classifyTasks(batch, tasksRoot, projectRoot);
129
+ let classifiedTasks = /** @type {{ taskId: string, classification: string, laneNumber?: number|null }[]} */ (classifyTasks(batch, tasksRoot, projectRoot));
130
130
  const useLightGit =
131
131
  ctx.light === true &&
132
132
  !_lightRetry &&
@@ -142,12 +142,10 @@ export function reconcileBatch(ctx, _lightRetry = false) {
142
142
 
143
143
  const hasRunningTasks = classifiedTasks.some((task) => task.classification === "running");
144
144
  const hasPendingTasks = classifiedTasks.some((task) => task.classification === "pending");
145
- const pendingWithFailedSegment = batch.segments.some(
146
- (segment) => segment.classification === "terminal-failure",
147
- );
148
- const driftTask = batch.tasks.find((task) => {
145
+ const pendingWithFailedSegment = /** @type {Record<string, any>[]} */ (batch.segments).some((segment) => segment.classification === "terminal-failure");
146
+ const driftTask = /** @type {Record<string, any>[]} */ (batch.tasks).find((task) => {
149
147
  if (task.classification !== "pending") return false;
150
- return batch.segments.some(
148
+ return /** @type {Record<string, any>[]} */ (batch.segments).some(
151
149
  (segment) => segment.taskId === task.taskId && segment.classification === "terminal-failure",
152
150
  );
153
151
  });
@@ -161,7 +159,7 @@ export function reconcileBatch(ctx, _lightRetry = false) {
161
159
  classifiedTasks.every((task) => task.classification === "terminal-success");
162
160
  const failedTask = classifiedTasks.find((task) => task.classification === "terminal-failure");
163
161
 
164
- const signals = {
162
+ const signals = /** @type {Record<string, any>} */ ({
165
163
  phase: batch.phase,
166
164
  endedAt: batch.endedAt,
167
165
  failedTasks: hasFailedTasks ? Math.max(batch.failedTasks, 1) : batch.failedTasks,
@@ -178,13 +176,24 @@ export function reconcileBatch(ctx, _lightRetry = false) {
178
176
  segments: batch.segments,
179
177
  lanes: batch.lanes,
180
178
  raw: batch.raw,
181
- };
179
+ });
182
180
 
183
181
  const journalFile = journalPath(projectRoot, batch.batchId);
184
182
  /** @type {object[]} */
185
183
  let journalEvents = [];
186
184
  if (fs.existsSync(journalFile)) {
187
- journalEvents = readJournalEvents(projectRoot, batch.batchId);
185
+ const parsedJournal = parseJournalLines(fs.readFileSync(journalFile, "utf-8"));
186
+ journalEvents = parsedJournal.events;
187
+ if (parsedJournal.skippedLines.length > 0) {
188
+ // SP-780: surface torn/checksum-failed lines so --diagnose reports
189
+ // the corruption instead of silently dropping those events.
190
+ signals.journalCorruptLines = {
191
+ count: parsedJournal.skippedLines.length,
192
+ lines: parsedJournal.skippedLines
193
+ .slice(0, 20)
194
+ .map(({ lineNumber, byteOffset }) => ({ lineNumber, byteOffset })),
195
+ };
196
+ }
188
197
  signals.journalHints = extractJournalDiagnosisHints(journalEvents);
189
198
  signals.journalEvents = journalEvents;
190
199
  }
@@ -193,11 +202,11 @@ export function reconcileBatch(ctx, _lightRetry = false) {
193
202
 
194
203
  if (journalEvents.length > 0 && batch.raw) {
195
204
  let rebuilt = rebuildBatchStateFromJournal(batch.raw, journalEvents);
196
- let drift = detectBatchStateDrift(batch.raw, rebuilt, journalEvents, classifiedTasks);
205
+ let drift = detectBatchStateDrift(batch.raw, rebuilt, /** @type {any} */ (journalEvents), /** @type {any} */ (classifiedTasks));
197
206
  const healed = reconcileBatchStateDrift({
198
207
  projectRoot,
199
208
  state: batch.raw,
200
- classifiedTasks,
209
+ classifiedTasks: /** @type {any} */ (classifiedTasks),
201
210
  journalEvents,
202
211
  drift,
203
212
  });
@@ -213,7 +222,7 @@ export function reconcileBatch(ctx, _lightRetry = false) {
213
222
  }
214
223
  classifiedTasks = classifyTasks(batch, tasksRoot, projectRoot);
215
224
  rebuilt = rebuildBatchStateFromJournal(batch.raw, journalEvents);
216
- drift = detectBatchStateDrift(batch.raw, rebuilt, journalEvents, classifiedTasks);
225
+ drift = detectBatchStateDrift(batch.raw, rebuilt, /** @type {any} */ (journalEvents), /** @type {any} */ (classifiedTasks));
217
226
  }
218
227
  signals.stateDrift = drift;
219
228
  signals.rebuiltFromJournal = rebuilt;
@@ -345,13 +354,13 @@ export function reconcileBatch(ctx, _lightRetry = false) {
345
354
 
346
355
  const mergeGitignoredFailureInferred = inferMergeGitignoredFailure({
347
356
  exitReason,
348
- failureClass: batch.raw?.mergeResults?.find((entry) => entry?.failureClass)?.failureClass ?? null,
349
- lastError: batch.raw?.lastError ?? null,
357
+ failureClass: /** @type {Record<string, any>[] | undefined} */ (batch.raw?.mergeResults)?.find((entry) => entry?.failureClass)?.failureClass ?? null,
358
+ lastError: /** @type {any} */ (batch.raw?.lastError) ?? null,
350
359
  journalEvents,
351
360
  });
352
361
  const mergeFailureSummary = summarizeMergeFailures(
353
362
  batch.mergeResults,
354
- batch.raw?.lastError ?? null,
363
+ /** @type {any} */ (batch.raw?.lastError) ?? null,
355
364
  );
356
365
  signals.mergeFailed = mergeFailureSummary.mergeFailed;
357
366
  signals.failedMerges = mergeFailureSummary.failedMerges;
@@ -376,9 +385,7 @@ export function reconcileBatch(ctx, _lightRetry = false) {
376
385
  : null;
377
386
  const driftTaskStatus =
378
387
  failedTaskId != null
379
- ? String(
380
- (batch.raw?.tasks ?? []).find((entry) => entry?.taskId === failedTaskId)?.status ?? "",
381
- )
388
+ ? String((/** @type {Record<string, any>[]} */ (batch.raw?.tasks) ?? []).find((entry) => entry?.taskId === failedTaskId)?.status ?? "")
382
389
  : null;
383
390
 
384
391
  const activeReviewTaskId =
@@ -1,4 +1,3 @@
1
- // @ts-nocheck
2
1
  /** Task classification and git inspection for batch reconciliation (SP-578). */
3
2
 
4
3
  import fs from "node:fs";
@@ -21,7 +20,7 @@ export function resolveTasksRoot(projectRoot, configResult) {
21
20
  return resolveTasksRootPath(projectRoot, loaded.config);
22
21
  }
23
22
 
24
- /** Align classification with batch-state status (SP-516 / #166, SP-512). @param {object} classified */
23
+ /** Align classification with batch-state status (SP-516 / #166, SP-512). @param {Record<string, any>} classified */
25
24
  export function alignTaskClassificationWithStatus(classified) {
26
25
  const status = String(classified.status ?? "").toLowerCase();
27
26
  let classification = classified.classification;
@@ -43,9 +42,9 @@ export function alignTaskClassificationWithStatus(classified) {
43
42
  return { ...classified, classification };
44
43
  }
45
44
 
46
- /** @param {object} batch @param {string|null} tasksRoot @param {string} [projectRoot] */
45
+ /** @param {Record<string, any>} batch @param {string|null} tasksRoot @param {string} [projectRoot] */
47
46
  export function classifyTasks(batch, tasksRoot, projectRoot = "") {
48
- return batch.tasks.map((task) =>
47
+ return batch.tasks.map((/** @type {any} */ task) =>
49
48
  alignTaskClassificationWithStatus(
50
49
  classifyTaskDoneSemantics(task, {
51
50
  tasksRoot,
@@ -57,7 +56,7 @@ export function classifyTasks(batch, tasksRoot, projectRoot = "") {
57
56
  );
58
57
  }
59
58
 
60
- /** @param {{ projectRoot: string, state: object }} params @returns {{ changed: boolean }} */
59
+ /** @param {{ projectRoot: string, state: Record<string, any> }} params @returns {{ changed: boolean }} */
61
60
  export function syncPersistedClassifications({ projectRoot, state }) {
62
61
  if (!state || typeof state !== "object") {
63
62
  return { changed: false };
@@ -70,7 +69,7 @@ export function syncPersistedClassifications({ projectRoot, state }) {
70
69
 
71
70
  const tasksRoot = resolveTasksRoot(projectRoot);
72
71
  const classified = classifyTasks(batch, tasksRoot, projectRoot);
73
- const alignedById = new Map(classified.map((task) => [String(task.taskId), task]));
72
+ const alignedById = new Map(classified.map((/** @type {any} */ task) => [String(task.taskId), task]));
74
73
  let changed = false;
75
74
 
76
75
  for (const task of state.tasks ?? []) {
@@ -116,6 +115,7 @@ export function syncPersistedClassifications({ projectRoot, state }) {
116
115
  return { changed };
117
116
  }
118
117
 
118
+ /** @param {string} projectRoot */
119
119
  function isInsideGitRepo(projectRoot) {
120
120
  try {
121
121
  execFileSync("git", ["rev-parse", "--is-inside-work-tree"], {
@@ -129,6 +129,7 @@ function isInsideGitRepo(projectRoot) {
129
129
  }
130
130
  }
131
131
 
132
+ /** @param {string} projectRoot @param {string} ref */
132
133
  function gitRefExists(projectRoot, ref) {
133
134
  try {
134
135
  execFileSync("git", ["rev-parse", "--verify", ref], {
@@ -142,6 +143,7 @@ function gitRefExists(projectRoot, ref) {
142
143
  }
143
144
  }
144
145
 
146
+ /** @param {string} projectRoot @param {string} ancestor @param {string} descendant */
145
147
  function gitIsAncestor(projectRoot, ancestor, descendant) {
146
148
  try {
147
149
  execFileSync("git", ["merge-base", "--is-ancestor", ancestor, descendant], {
@@ -155,6 +157,7 @@ function gitIsAncestor(projectRoot, ancestor, descendant) {
155
157
  }
156
158
  }
157
159
 
160
+ /** @param {Record<string, any>} result @param {unknown} err @param {string} context */
158
161
  function recordGitInspectionError(result, err, context) {
159
162
  const message = err instanceof Error ? err.message : String(err);
160
163
  const detail = `${context}: ${message}`;
@@ -163,6 +166,7 @@ function recordGitInspectionError(result, err, context) {
163
166
  : detail;
164
167
  }
165
168
 
169
+ /** @param {string} projectRoot @param {string} batchId @returns {string[]} */
166
170
  function listOrchBranches(projectRoot, batchId) {
167
171
  try {
168
172
  const output = execFileSync("git", ["branch", "-a", "--list", "*orch*"], {
@@ -184,7 +188,7 @@ function listOrchBranches(projectRoot, batchId) {
184
188
  /** @param {{ projectRoot: string, batchId: string, baseBranch: string, orchBranch: string|null }} ctx */
185
189
  export function inspectGitState(ctx) {
186
190
  const { projectRoot, batchId, baseBranch, orchBranch } = ctx;
187
- const result = {
191
+ const result = /** @type {Record<string, any>} */ ({
188
192
  inGitRepo: isInsideGitRepo(projectRoot),
189
193
  baseBranch,
190
194
  orchBranch,
@@ -194,7 +198,7 @@ export function inspectGitState(ctx) {
194
198
  mergedOrchBranch: null,
195
199
  orchCommitsAhead: null,
196
200
  gitInspectionError: null,
197
- };
201
+ });
198
202
 
199
203
  if (!result.inGitRepo) return result;
200
204
 
@@ -273,7 +277,9 @@ export function listGitChangedPaths(projectRoot, refA, refB) {
273
277
  }
274
278
  }
275
279
 
276
- /** Paths from human commits since snapshot, excluding orch ancestry. */
280
+ /** Paths from human commits since snapshot, excluding orch ancestry.
281
+ * @param {string} projectRoot @param {string|null} snapshot @param {string|null} humanHead @param {string|null} orchBranch @returns {string[]}
282
+ */
277
283
  export function listHumanOnlyPaths(projectRoot, snapshot, humanHead, orchBranch) {
278
284
  if (!snapshot || !humanHead || snapshot === humanHead) return [];
279
285
  const args = ["log", "--name-only", "--pretty=format:", `${snapshot}..${humanHead}`];
@@ -293,6 +299,7 @@ export function listHumanOnlyPaths(projectRoot, snapshot, humanHead, orchBranch)
293
299
  }
294
300
  }
295
301
 
302
+ /** @param {string} projectRoot @param {string} ref @param {string} filePath */
296
303
  function pathExistsAtRef(projectRoot, ref, filePath) {
297
304
  try {
298
305
  execFileSync("git", ["cat-file", "-e", `${ref}:${filePath}`], {
@@ -306,6 +313,7 @@ function pathExistsAtRef(projectRoot, ref, filePath) {
306
313
  }
307
314
  }
308
315
 
316
+ /** @param {string} projectRoot @param {string} baseTip @param {string} snapshot */
309
317
  function humanCheckoutNeedsPathSync(projectRoot, baseTip, snapshot) {
310
318
  const landPaths = listGitChangedPaths(projectRoot, snapshot, baseTip);
311
319
  for (const filePath of landPaths) {
@@ -317,7 +325,9 @@ function humanCheckoutNeedsPathSync(projectRoot, baseTip, snapshot) {
317
325
  return false;
318
326
  }
319
327
 
320
- /** Human/base divergence or post-isolated-integrate sync (FR-WT-08 / #91). */
328
+ /** Human/base divergence or post-isolated-integrate sync (FR-WT-08 / #91).
329
+ * @param {Record<string, any>} ctx
330
+ */
321
331
  export function inspectHumanBaseSync(ctx) {
322
332
  const { projectRoot, baseBranch, baseBranchHeadAtStart, orchBranch, git, journalEvents } = ctx;
323
333
  if (!git?.inGitRepo) return null;
@@ -343,7 +353,7 @@ export function inspectHumanBaseSync(ctx) {
343
353
  }
344
354
 
345
355
  const integrateCompleted = Array.isArray(journalEvents)
346
- ? journalEvents.some((event) => event.type === "integrate.completed")
356
+ ? journalEvents.some((/** @type {any} */ event) => event.type === "integrate.completed")
347
357
  : false;
348
358
  const landSucceeded =
349
359
  git.orchMergedToBase ||
@@ -1,10 +1,9 @@
1
- // @ts-nocheck
2
1
  /** Diagnosis output context after reconcile signal enrichment (#201 / SP-645). */
3
2
 
4
3
  import { findPendingLaneLandTasks } from "./diagnosis-pending-lane.mjs";
5
4
 
6
5
  /**
7
- * @param {object} params
6
+ * @param {Record<string, any>} params
8
7
  */
9
8
  export function buildReconcileDiagnosisContext(params) {
10
9
  const {
@@ -1,4 +1,3 @@
1
- // @ts-nocheck
2
1
  /** Diagnosis derivation (SP-596/SP-606 / #192). */
3
2
 
4
3
  import { inferLaunchFailureKind } from "./diagnosis.mjs";
@@ -29,7 +28,7 @@ export {
29
28
  */
30
29
  function resolveFailedExitReason(rawTasks, failedTaskId) {
31
30
  if (!failedTaskId || !Array.isArray(rawTasks)) return null;
32
- const match = rawTasks.find((entry) => {
31
+ const match = rawTasks.find((/** @type {any} */ entry) => {
33
32
  if (!entry || typeof entry !== "object") return false;
34
33
  return String(entry.taskId ?? entry.id ?? "") === failedTaskId;
35
34
  });
@@ -40,7 +39,7 @@ function resolveFailedExitReason(rawTasks, failedTaskId) {
40
39
 
41
40
  /**
42
41
  * @param {string|null} failedTaskId
43
- * @param {object} signals
42
+ * @param {Record<string, any>} signals
44
43
  * @returns {string|null}
45
44
  */
46
45
  function resolvePrimaryFailureExitReason(failedTaskId, signals) {
@@ -48,7 +47,7 @@ function resolvePrimaryFailureExitReason(failedTaskId, signals) {
48
47
  if (fromTask) return fromTask;
49
48
  if (!failedTaskId) return null;
50
49
  if (Array.isArray(signals.segments)) {
51
- const segment = signals.segments.find((entry) => entry?.taskId === failedTaskId);
50
+ const segment = signals.segments.find((/** @type {any} */ entry) => entry?.taskId === failedTaskId);
52
51
  const classification = segment?.classification;
53
52
  if (
54
53
  typeof classification === "string" &&
@@ -75,7 +74,7 @@ function resolvePrimaryFailureExitReason(failedTaskId, signals) {
75
74
  /**
76
75
  * @param {string|null} failedTaskId
77
76
  * @param {string|null} exitReason
78
- * @param {object} signals
77
+ * @param {Record<string, any>} signals
79
78
  * @returns {{ exitReason: string|null, launchFailureKind: string|null }}
80
79
  */
81
80
  function deriveFailureContext(failedTaskId, exitReason, signals) {
@@ -83,7 +82,7 @@ function deriveFailureContext(failedTaskId, exitReason, signals) {
83
82
  exitReason ?? resolvePrimaryFailureExitReason(failedTaskId, signals);
84
83
  if (failedTaskId && signals.tasksRoot) {
85
84
  const taskFolder =
86
- signals.tasks?.find((entry) => entry.taskId === failedTaskId)?.taskFolder ?? null;
85
+ signals.tasks?.find((/** @type {any} */ entry) => entry.taskId === failedTaskId)?.taskFolder ?? null;
87
86
  const stubReason = inferStubExitReasonForTask(signals.tasksRoot, failedTaskId, taskFolder);
88
87
  if (stubReason) {
89
88
  resolvedExitReason = stubReason;
@@ -100,7 +99,7 @@ function deriveFailureContext(failedTaskId, exitReason, signals) {
100
99
  /**
101
100
  * @param {string} diagnosis
102
101
  * @param {string|null} failedTaskId
103
- * @param {object} signals
102
+ * @param {Record<string, any>} signals
104
103
  * @param {string|null} [exitReason]
105
104
  */
106
105
  function withFailureContext(diagnosis, failedTaskId, signals, exitReason = null) {
@@ -114,13 +113,13 @@ function withFailureContext(diagnosis, failedTaskId, signals, exitReason = null)
114
113
  }
115
114
 
116
115
  /**
117
- * @param {object} signals
116
+ * @param {Record<string, any>} signals
118
117
  */
119
118
  function findNeedsReplanTask(signals) {
120
119
  const tasks = signals.raw?.tasks ?? signals.tasks ?? [];
121
120
  return (
122
121
  tasks.find(
123
- (task) =>
122
+ (/** @type {any} */ task) =>
124
123
  task?.exitReason === "needs_replan" &&
125
124
  (task.status === "failed" || task.classification === "terminal-failure"),
126
125
  ) ?? null
@@ -128,11 +127,11 @@ function findNeedsReplanTask(signals) {
128
127
  }
129
128
 
130
129
  /**
131
- * @param {object} signals
130
+ * @param {Record<string, any>} signals
132
131
  */
133
132
  function hasNeedsReplanBlocker(signals) {
134
133
  const tasks = signals.raw?.tasks ?? signals.tasks ?? [];
135
- return tasks.some((task) => task?.exitReason === "needs_replan");
134
+ return tasks.some((/** @type {any} */ task) => task?.exitReason === "needs_replan");
136
135
  }
137
136
 
138
137
  /**
@@ -158,8 +157,8 @@ const POST_DONE_PLAN_REVIEW_EXIT_REASONS = new Set([
158
157
  * The salvage command itself hard-gates on commits-ahead, so pointing at it
159
158
  * without lane commits is safe (dry-run reports none salvageable).
160
159
  *
161
- * @param {object} signals
162
- * @returns {object|null}
160
+ * @param {Record<string, any>} signals
161
+ * @returns {Record<string, any>|null}
163
162
  */
164
163
  function findPostDonePlanReviewSpawnFailedTask(signals) {
165
164
  const tasks = Array.isArray(signals.tasks) ? signals.tasks : [];
@@ -180,7 +179,7 @@ function findPostDonePlanReviewSpawnFailedTask(signals) {
180
179
  }
181
180
 
182
181
  /**
183
- * @param {object} signals
182
+ * @param {Record<string, any>} signals
184
183
  */
185
184
  export function deriveDiagnosis(signals) {
186
185
  const {
@@ -206,7 +205,7 @@ export function deriveDiagnosis(signals) {
206
205
  }
207
206
 
208
207
  if (shouldDiagnosePendingLaneLand(signals)) {
209
- const pendingLaneLandTasks = findPendingLaneLandTasks(signals.tasks);
208
+ const pendingLaneLandTasks = /** @type {any} */ (findPendingLaneLandTasks(signals.tasks));
210
209
  return withFailureContext(
211
210
  "pending_lane_land",
212
211
  pendingLaneLandTasks[0]?.taskId ?? null,
@@ -215,7 +214,7 @@ export function deriveDiagnosis(signals) {
215
214
  }
216
215
 
217
216
  if (signals.stateDrift?.drifted) {
218
- const driftTask = signals.stateDrift.entries.find((entry) => entry.taskId !== "*");
217
+ const driftTask = signals.stateDrift.entries.find((/** @type {any} */ entry) => entry.taskId !== "*");
219
218
  return withFailureContext("state_drift", driftTask?.taskId ?? null, signals);
220
219
  }
221
220
 
@@ -224,12 +223,12 @@ export function deriveDiagnosis(signals) {
224
223
  hasPendingTasks &&
225
224
  Array.isArray(signals.tasks) &&
226
225
  signals.tasks.some(
227
- (task) =>
226
+ (/** @type {any} */ task) =>
228
227
  task?.doneInLane === true &&
229
228
  (task.status === "pending" || task.status === "running" || task.classification === "pending" || task.classification === "running"),
230
229
  )
231
230
  ) {
232
- const driftTask = signals.tasks.find((task) => task?.doneInLane);
231
+ const driftTask = signals.tasks.find((/** @type {any} */ task) => task?.doneInLane);
233
232
  return withFailureContext("needs_retry", driftTask?.taskId ?? null, signals);
234
233
  }
235
234
 
@@ -318,7 +317,7 @@ export function deriveDiagnosis(signals) {
318
317
  phase === "merging" &&
319
318
  endedAt != null &&
320
319
  Array.isArray(signals.raw?.mergeResults) &&
321
- signals.raw.mergeResults.some((entry) => String(entry?.status ?? "").toLowerCase() === "failed")
320
+ signals.raw.mergeResults.some((/** @type {any} */ entry) => String(entry?.status ?? "").toLowerCase() === "failed")
322
321
  ) {
323
322
  return withFailureContext("failed", null, signals);
324
323
  }
@@ -1,4 +1,3 @@
1
- // @ts-nocheck
2
1
  /** Light-reconcile git cache and phase sets (SP-606 / #192). */
3
2
 
4
3
  export const LIMBO_PHASES = new Set(["stopped", "failed", "executing"]);
@@ -1,4 +1,3 @@
1
- // @ts-nocheck
2
1
  /** Orphan reconcile helpers (SP-606 / #192). Post-DONE heal: SP-657 / #205. */
3
2
 
4
3
  import { loadSpineConfig } from "../config/spine-config-load.mjs";
@@ -37,7 +36,7 @@ export function runReconciliationCheck(ctx) {
37
36
  }
38
37
 
39
38
  /**
40
- * @param {unknown[]} lanes
39
+ * @param {Record<string, any>[]} lanes
41
40
  * @param {number|null|undefined} laneNumber
42
41
  */
43
42
  function findLaneForOrphanReconcile(lanes, laneNumber) {
@@ -52,7 +51,7 @@ function findLaneForOrphanReconcile(lanes, laneNumber) {
52
51
  /**
53
52
  * Classify tasks for orphan reconcile (status-only, mirrors resume validation).
54
53
  *
55
- * @param {object[]} tasks
54
+ * @param {Record<string, any>[]} tasks
56
55
  */
57
56
  function classifyTasksForOrphanReconcile(tasks) {
58
57
  return (tasks ?? []).map((task) => {
@@ -66,7 +65,7 @@ function classifyTasksForOrphanReconcile(tasks) {
66
65
  }
67
66
 
68
67
  /**
69
- * @param {object[]} journalEvents
68
+ * @param {Record<string, any>[]} journalEvents
70
69
  * @param {string} taskId
71
70
  * @param {string} eventType
72
71
  */
@@ -111,8 +110,8 @@ function orphanTaskReadyForSkipDoneHeal({ projectRoot, batchId, task, lanes, tas
111
110
  * @param {object} params
112
111
  * @param {string} params.projectRoot
113
112
  * @param {string} params.batchId
114
- * @param {object} params.state
115
- * @param {object} params.task
113
+ * @param {Record<string, any>} params.state
114
+ * @param {Record<string, any>} params.task
116
115
  * @param {object[]} params.journalEvents
117
116
  * @returns {boolean}
118
117
  */
@@ -146,7 +145,7 @@ function healPostDoneOrphanTask({ projectRoot, batchId, state, task, journalEven
146
145
  *
147
146
  * @param {object} params
148
147
  * @param {string} params.projectRoot
149
- * @param {object} params.state
148
+ * @param {Record<string, any>} params.state
150
149
  * @returns {{ reconciled: boolean, taskId?: string|null, kind?: string, exitReason?: string, healedTaskIds?: string[] }}
151
150
  */
152
151
  export function reconcileOrphanRunningState({ projectRoot, state }) {
@@ -202,7 +201,9 @@ export function reconcileOrphanRunningState({ projectRoot, state }) {
202
201
  const taskIdsFailed = [];
203
202
 
204
203
  for (const taskId of taskIdsToConsider) {
205
- const task = (state.tasks ?? []).find((entry) => entry?.taskId === taskId);
204
+ const task = /** @type {Record<string, any>[]} */ (state.tasks ?? []).find(
205
+ (entry) => entry?.taskId === taskId,
206
+ );
206
207
  if (!task || task.status !== "running") continue;
207
208
 
208
209
  if (
@@ -1,4 +1,3 @@
1
- // @ts-nocheck
2
1
  /**
3
2
  * Batch reconciliation (FR-BATCH-12, §17.5).
4
3
  * Re-export shim — classify / diagnosis / batch / orphan / light-cache (SP-578/596/606).