pi-spine 1.0.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 (156) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +563 -0
  3. package/bin/get-version.mjs +35 -0
  4. package/bin/spine-batch.mjs +425 -0
  5. package/bin/spine-cli/batch.mjs +47 -0
  6. package/bin/spine-cli/gate.mjs +10 -0
  7. package/bin/spine-cli/integrate.mjs +10 -0
  8. package/bin/spine-cli/plan.mjs +12 -0
  9. package/bin/spine-cli/shared.mjs +73 -0
  10. package/bin/spine-cli/status.mjs +18 -0
  11. package/bin/spine-cli/verify.mjs +199 -0
  12. package/bin/spine-config.mjs +212 -0
  13. package/bin/spine-dashboard.mjs +178 -0
  14. package/bin/spine-deps.mjs +69 -0
  15. package/bin/spine-doctor.mjs +468 -0
  16. package/bin/spine-gate.mjs +137 -0
  17. package/bin/spine-init.mjs +398 -0
  18. package/bin/spine-integrate.mjs +77 -0
  19. package/bin/spine-journal.mjs +198 -0
  20. package/bin/spine-migrate-from-taskplane.mjs +136 -0
  21. package/bin/spine-plan.mjs +83 -0
  22. package/bin/spine-preflight.mjs +700 -0
  23. package/bin/spine-report-progress.d.mts +28 -0
  24. package/bin/spine-report-progress.mjs +126 -0
  25. package/bin/spine-review-step.d.mts +24 -0
  26. package/bin/spine-review-step.mjs +124 -0
  27. package/bin/spine-rules.mjs +35 -0
  28. package/bin/spine-settings.mjs +186 -0
  29. package/bin/spine-state.mjs +110 -0
  30. package/bin/spine-status.mjs +79 -0
  31. package/bin/spine-tasks.mjs +293 -0
  32. package/bin/spine-worker-runner.mjs +314 -0
  33. package/bin/spine.mjs +404 -0
  34. package/extensions/spine/settings-slash.ts +20 -0
  35. package/extensions/spine/slash-commands.ts +806 -0
  36. package/extensions/spine/worker-tools.ts +279 -0
  37. package/extensions/spine-orchestrator.ts +12 -0
  38. package/package.json +72 -0
  39. package/skills/.gitkeep +0 -0
  40. package/skills/create-spine-tasks/SKILL.md +375 -0
  41. package/skills/create-spine-tasks/references/context-template.md +25 -0
  42. package/skills/create-spine-tasks/references/contract-template.md +98 -0
  43. package/skills/create-spine-tasks/references/explore-template.md +63 -0
  44. package/skills/create-spine-tasks/references/prompt-template.md +276 -0
  45. package/src/batch/abort.mjs +272 -0
  46. package/src/batch/agent-session-worker.mjs +297 -0
  47. package/src/batch/batch-state-io.mjs +53 -0
  48. package/src/batch/contract-verify.mjs +310 -0
  49. package/src/batch/detached-start.mjs +778 -0
  50. package/src/batch/diagnosis.mjs +378 -0
  51. package/src/batch/engine-lanes/merge.mjs +632 -0
  52. package/src/batch/engine-lanes/queue.mjs +208 -0
  53. package/src/batch/engine-lanes/review.mjs +970 -0
  54. package/src/batch/engine-lanes.mjs +388 -0
  55. package/src/batch/engine-scope.mjs +251 -0
  56. package/src/batch/engine.mjs +445 -0
  57. package/src/batch/evidence-command.mjs +157 -0
  58. package/src/batch/evidence.mjs +203 -0
  59. package/src/batch/gate.mjs +359 -0
  60. package/src/batch/git-commit-env.mjs +46 -0
  61. package/src/batch/git-exec.mjs +26 -0
  62. package/src/batch/heartbeat.mjs +430 -0
  63. package/src/batch/integrate.mjs +414 -0
  64. package/src/batch/journal-rebuild.mjs +388 -0
  65. package/src/batch/journal.mjs +373 -0
  66. package/src/batch/lane-commit.mjs +153 -0
  67. package/src/batch/lifecycle.mjs +364 -0
  68. package/src/batch/metrics.mjs +265 -0
  69. package/src/batch/orphan-detect.mjs +210 -0
  70. package/src/batch/post-merge-limbo.mjs +80 -0
  71. package/src/batch/postmortem.mjs +296 -0
  72. package/src/batch/readers/spine-state.mjs +95 -0
  73. package/src/batch/readers/taskplane-state.mjs +136 -0
  74. package/src/batch/reconcile.mjs +729 -0
  75. package/src/batch/resume-common.mjs +104 -0
  76. package/src/batch/resume-engine.mjs +66 -0
  77. package/src/batch/resume-multi-lanes.mjs +479 -0
  78. package/src/batch/resume-multi-validate.mjs +187 -0
  79. package/src/batch/resume-multi.mjs +254 -0
  80. package/src/batch/resume.mjs +475 -0
  81. package/src/batch/retry.mjs +257 -0
  82. package/src/batch/review-scope.mjs +136 -0
  83. package/src/batch/review.mjs +1045 -0
  84. package/src/batch/salvage.mjs +332 -0
  85. package/src/batch/state.mjs +680 -0
  86. package/src/batch/task-stall-budget.mjs +88 -0
  87. package/src/batch/worker-host.mjs +683 -0
  88. package/src/batch/worker-output.mjs +253 -0
  89. package/src/batch/worker-prompt.mjs +84 -0
  90. package/src/batch/worktree.mjs +290 -0
  91. package/src/cli/deps.mjs +141 -0
  92. package/src/cli/handoff.mjs +364 -0
  93. package/src/cli/rules.mjs +429 -0
  94. package/src/cli/settings-set.mjs +171 -0
  95. package/src/cli/settings-show.mjs +140 -0
  96. package/src/cli/settings-slash.mjs +107 -0
  97. package/src/config/contract.mjs +56 -0
  98. package/src/config/cursor-rules/discover.mjs +416 -0
  99. package/src/config/cursor-rules/index.mjs +39 -0
  100. package/src/config/cursor-rules/match-globs.mjs +132 -0
  101. package/src/config/cursor-rules/parse-frontmatter.mjs +222 -0
  102. package/src/config/cursor-rules/priority.mjs +26 -0
  103. package/src/config/cursor-rules/profile.mjs +403 -0
  104. package/src/config/cursor-rules/select.mjs +259 -0
  105. package/src/config/defaults.mjs +36 -0
  106. package/src/config/env-overrides.mjs +207 -0
  107. package/src/config/merge-defaults.mjs +47 -0
  108. package/src/config/pi-spine-root.mjs +93 -0
  109. package/src/config/reviewer-context.mjs +243 -0
  110. package/src/config/settings-fields.mjs +265 -0
  111. package/src/config/worker-backend.mjs +116 -0
  112. package/src/config/worker-context.mjs +402 -0
  113. package/src/config/worker-launch-script.mjs +130 -0
  114. package/src/config/worktree-setup-hook.mjs +122 -0
  115. package/src/dashboard/public/dashboard.css +77 -0
  116. package/src/dashboard/public/dashboard.js +349 -0
  117. package/src/dashboard/public/index.html +80 -0
  118. package/src/dashboard/server.mjs +155 -0
  119. package/src/dashboard/snapshot.mjs +414 -0
  120. package/src/dashboard/sse.mjs +15 -0
  121. package/src/dashboard/view.mjs +292 -0
  122. package/src/doctor/agent-model-inherit.mjs +101 -0
  123. package/src/doctor/batch-size-guidance.mjs +72 -0
  124. package/src/doctor/coexistence.mjs +312 -0
  125. package/src/doctor/rules-manifest.mjs +133 -0
  126. package/src/doctor/stale-path.mjs +156 -0
  127. package/src/doctor/stall-config.mjs +108 -0
  128. package/src/doctor/suggest-max-parallel.mjs +53 -0
  129. package/src/doctor/task-packet-size.mjs +110 -0
  130. package/src/doctor/worktree-health.mjs +71 -0
  131. package/src/migrate/taskplane-config.mjs +116 -0
  132. package/src/planner/cycles.mjs +63 -0
  133. package/src/planner/format-plan.mjs +132 -0
  134. package/src/planner/graph.mjs +99 -0
  135. package/src/planner/index.mjs +125 -0
  136. package/src/planner/lanes.mjs +111 -0
  137. package/src/planner/pending.mjs +39 -0
  138. package/src/planner/scope.mjs +142 -0
  139. package/src/process/liveness.mjs +21 -0
  140. package/src/tasks/packet/discover.mjs +40 -0
  141. package/src/tasks/packet/index.mjs +81 -0
  142. package/src/tasks/packet/merge-deps.mjs +79 -0
  143. package/src/tasks/packet/parse-prompt.mjs +445 -0
  144. package/src/tasks/packet/parse-status.mjs +118 -0
  145. package/src/tasks/packet/validate-contract.mjs +115 -0
  146. package/src/tasks/packet/validate-prompt.mjs +99 -0
  147. package/src/worker-tools/report-progress.d.mts +18 -0
  148. package/src/worker-tools/report-progress.mjs +59 -0
  149. package/src/worker-tools/request-gate.d.mts +17 -0
  150. package/src/worker-tools/request-gate.mjs +54 -0
  151. package/templates/agents/reviewer.md +123 -0
  152. package/templates/agents/supervisor.md +59 -0
  153. package/templates/agents/worker.md +122 -0
  154. package/templates/rules-profile.json +27 -0
  155. package/templates/spine-config.json +64 -0
  156. package/templates/tasks/CONTEXT.md +30 -0
@@ -0,0 +1,332 @@
1
+ /**
2
+ * FR-STALL-03A/03B: salvage inspection and optional WIP commit on stall (no `.DONE`).
3
+ *
4
+ * Scope (intentionally narrow): paths listed in the task File Scope plus everything
5
+ * under the task folder in the lane worktree — not the full worktree (see brief OQ #3).
6
+ */
7
+
8
+ import fs from "node:fs";
9
+ import path from "node:path";
10
+ import { execFileSync } from "node:child_process";
11
+ import { resolveScopedDirtyPaths } from "./heartbeat.mjs";
12
+ import { appendJournalEvent } from "./journal.mjs";
13
+ import { evidenceDir } from "./evidence.mjs";
14
+
15
+ const INDEX_CONFLICT_PORCELAIN = /^(UU|AA|DD|AU|UA|DU|UD)/;
16
+
17
+ /**
18
+ * @param {object} [config]
19
+ */
20
+ export function resolveSalvageConfig(config = {}) {
21
+ const lanes = config.lanes ?? {};
22
+ return {
23
+ autoCommitOnStall: lanes.autoCommitOnStall === true,
24
+ };
25
+ }
26
+
27
+ /**
28
+ * @param {string} worktreePath
29
+ * @param {string[]} paths
30
+ */
31
+ function readScopedDiffStat(worktreePath, paths) {
32
+ if (paths.length === 0) return "";
33
+ try {
34
+ return execFileSync("git", ["diff", "--stat", "--", ...paths], {
35
+ cwd: worktreePath,
36
+ encoding: "utf-8",
37
+ stdio: ["ignore", "pipe", "pipe"],
38
+ }).trim();
39
+ } catch {
40
+ return "";
41
+ }
42
+ }
43
+
44
+ /**
45
+ * @param {string} worktreePath
46
+ * @param {string[]} args
47
+ */
48
+ function git(worktreePath, args) {
49
+ return execFileSync("git", args, {
50
+ cwd: worktreePath,
51
+ encoding: "utf-8",
52
+ stdio: ["ignore", "pipe", "pipe"],
53
+ }).trim();
54
+ }
55
+
56
+ /**
57
+ * @param {string} worktreePath
58
+ */
59
+ export function hasWorktreeIndexConflicts(worktreePath) {
60
+ for (const ref of ["MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD"]) {
61
+ try {
62
+ execFileSync("git", ["rev-parse", "-q", "--verify", ref], {
63
+ cwd: worktreePath,
64
+ stdio: ["ignore", "pipe", "pipe"],
65
+ });
66
+ return true;
67
+ } catch {
68
+ // not in this in-progress operation
69
+ }
70
+ }
71
+
72
+ try {
73
+ const unmerged = execFileSync("git", ["ls-files", "-u"], {
74
+ cwd: worktreePath,
75
+ encoding: "utf-8",
76
+ stdio: ["ignore", "pipe", "pipe"],
77
+ }).trim();
78
+ if (unmerged) return true;
79
+ } catch {
80
+ return false;
81
+ }
82
+
83
+ let porcelain = "";
84
+ try {
85
+ porcelain = execFileSync("git", ["status", "--porcelain"], {
86
+ cwd: worktreePath,
87
+ encoding: "utf-8",
88
+ stdio: ["ignore", "pipe", "pipe"],
89
+ });
90
+ } catch {
91
+ return false;
92
+ }
93
+ for (const line of porcelain.split("\n")) {
94
+ if (!line.trim()) continue;
95
+ if (INDEX_CONFLICT_PORCELAIN.test(line)) return true;
96
+ }
97
+ return false;
98
+ }
99
+
100
+ /**
101
+ * @param {object} params
102
+ * @param {string} [params.batchPhase]
103
+ */
104
+ export function resolveSalvageCommitRefusal({ batchPhase, worktreePath }) {
105
+ if (String(batchPhase ?? "") === "merging") {
106
+ return { refused: true, reason: "merge_in_progress" };
107
+ }
108
+ if (hasWorktreeIndexConflicts(worktreePath)) {
109
+ return { refused: true, reason: "index_conflicts" };
110
+ }
111
+ return { refused: false, reason: null };
112
+ }
113
+
114
+ /**
115
+ * @param {object} params
116
+ * @param {string} params.worktreePath
117
+ * @param {string} params.taskBranch
118
+ * @param {string} params.taskId
119
+ * @param {string[]} params.dirtyPaths
120
+ * @param {string} [params.batchPhase]
121
+ */
122
+ export function attemptScopedSalvageCommit({
123
+ worktreePath,
124
+ taskBranch,
125
+ taskId,
126
+ dirtyPaths,
127
+ batchPhase,
128
+ }) {
129
+ if (!Array.isArray(dirtyPaths) || dirtyPaths.length === 0) {
130
+ return { committed: false, refused: true, reason: "no_scoped_dirty_paths" };
131
+ }
132
+
133
+ const refusal = resolveSalvageCommitRefusal({ worktreePath, batchPhase });
134
+ if (refusal.refused) {
135
+ return { committed: false, refused: true, reason: refusal.reason };
136
+ }
137
+
138
+ const iso = new Date().toISOString();
139
+ const message = `wip(${taskId}): stall salvage ${iso}`;
140
+
141
+ try {
142
+ git(worktreePath, ["checkout", taskBranch]);
143
+ git(worktreePath, ["add", "--", ...dirtyPaths]);
144
+ git(worktreePath, ["commit", "-m", message]);
145
+ const commitSha = git(worktreePath, ["rev-parse", "HEAD"]);
146
+ return { committed: true, refused: false, reason: null, commitSha, message };
147
+ } catch (err) {
148
+ const error = err instanceof Error ? err.message : String(err);
149
+ return { committed: false, refused: true, reason: "hook_or_commit_failed", error };
150
+ }
151
+ }
152
+
153
+ /**
154
+ * @param {object} params
155
+ * @param {string} params.worktreePath
156
+ * @param {string[]} [params.fileScopePaths]
157
+ * @param {string} params.taskFolder
158
+ */
159
+ export function inspectLaneSalvage({ worktreePath, fileScopePaths = [], taskFolder }) {
160
+ const dirtyPaths = resolveScopedDirtyPaths(worktreePath, fileScopePaths, taskFolder);
161
+ const changedFileCount = dirtyPaths.length;
162
+ const diffStat = readScopedDiffStat(worktreePath, dirtyPaths);
163
+ const salvageable = changedFileCount > 0;
164
+ const recommendedAction = salvageable
165
+ ? "retry_with_salvage_review"
166
+ : "no_scoped_uncommitted_changes";
167
+
168
+ return {
169
+ dirtyPaths,
170
+ diffStat,
171
+ changedFileCount,
172
+ salvageable,
173
+ recommendedAction,
174
+ };
175
+ }
176
+
177
+ /**
178
+ * @param {object} params
179
+ * @param {boolean} params.salvageable
180
+ * @param {string} [params.taskId]
181
+ */
182
+ export function resolveSalvageRetryCommand({ salvageable, taskId }) {
183
+ if (salvageable && taskId) return `spine batch retry ${taskId}`;
184
+ return null;
185
+ }
186
+
187
+ /**
188
+ * @param {object} params
189
+ */
190
+ export function writeSalvageEvidence({
191
+ projectRoot,
192
+ batchId,
193
+ taskId,
194
+ inspection,
195
+ recommendedAction,
196
+ classification,
197
+ salvageCommit,
198
+ }) {
199
+ const dir = evidenceDir(projectRoot, batchId);
200
+ fs.mkdirSync(dir, { recursive: true });
201
+ const rel = `evidence/salvage-${taskId}.json`;
202
+ const abs = path.join(dir, `salvage-${taskId}.json`);
203
+ const payload = {
204
+ taskId,
205
+ batchId,
206
+ classification: classification ?? null,
207
+ salvageable: inspection.salvageable,
208
+ changedFileCount: inspection.changedFileCount,
209
+ dirtyPaths: inspection.dirtyPaths,
210
+ diffStat: inspection.diffStat,
211
+ recommendedAction,
212
+ retryCommand: resolveSalvageRetryCommand({
213
+ salvageable: inspection.salvageable,
214
+ taskId,
215
+ }),
216
+ salvageCommit: salvageCommit ?? null,
217
+ recordedAt: new Date().toISOString(),
218
+ };
219
+ fs.writeFileSync(abs, `${JSON.stringify(payload, null, 2)}\n`, "utf-8");
220
+ return rel;
221
+ }
222
+
223
+ /**
224
+ * @param {object[]} events
225
+ * @param {string|null|undefined} taskId
226
+ */
227
+ export function findLatestSalvageInspection(events, taskId) {
228
+ if (!taskId) return null;
229
+ for (let i = events.length - 1; i >= 0; i -= 1) {
230
+ const event = events[i];
231
+ if (event.type !== "lane.salvage_inspection") continue;
232
+ if (event.taskId !== taskId) continue;
233
+ return event.payload && typeof event.payload === "object" ? event.payload : {};
234
+ }
235
+ return null;
236
+ }
237
+
238
+ /**
239
+ * Run salvage inspection, journal, and evidence for a terminal worker failure without `.DONE`.
240
+ *
241
+ * @param {object} params
242
+ */
243
+ export function recordTaskFailureSalvage({
244
+ projectRoot,
245
+ batchId,
246
+ laneNumber,
247
+ laneId,
248
+ taskId,
249
+ correlationId,
250
+ worktreePath,
251
+ fileScopePaths = [],
252
+ taskFolder,
253
+ workerResult,
254
+ config,
255
+ batchPhase,
256
+ taskBranch,
257
+ }) {
258
+ if (workerResult.doneFound) return {};
259
+ if (workerResult.classification === "aborted") return {};
260
+
261
+ const salvageConfig = resolveSalvageConfig(config);
262
+ const inspection = inspectLaneSalvage({ worktreePath, fileScopePaths, taskFolder });
263
+ const retryCommand = resolveSalvageRetryCommand({
264
+ salvageable: inspection.salvageable,
265
+ taskId,
266
+ });
267
+ const recommendedAction =
268
+ retryCommand ??
269
+ (inspection.salvageable ? "review_scoped_changes" : "spine status --diagnose");
270
+
271
+ appendJournalEvent(projectRoot, batchId, "lane.salvage_inspection", {
272
+ laneNumber,
273
+ laneId,
274
+ taskId,
275
+ correlationId,
276
+ classification: workerResult.classification ?? null,
277
+ dirtyPaths: inspection.dirtyPaths,
278
+ diffStat: inspection.diffStat,
279
+ changedFileCount: inspection.changedFileCount,
280
+ salvageable: inspection.salvageable,
281
+ recommendedAction,
282
+ retryCommand,
283
+ });
284
+
285
+ let salvageCommit = null;
286
+ if (salvageConfig.autoCommitOnStall && inspection.salvageable && taskBranch) {
287
+ const result = attemptScopedSalvageCommit({
288
+ worktreePath,
289
+ taskBranch,
290
+ taskId,
291
+ dirtyPaths: inspection.dirtyPaths,
292
+ batchPhase,
293
+ });
294
+ salvageCommit = {
295
+ committed: result.committed === true,
296
+ refused: result.refused === true,
297
+ reason: result.reason ?? null,
298
+ commitSha: result.commitSha ?? null,
299
+ message: result.message ?? null,
300
+ error: result.error ?? null,
301
+ };
302
+ appendJournalEvent(projectRoot, batchId, "lane.salvage_commit", {
303
+ laneNumber,
304
+ laneId,
305
+ taskId,
306
+ correlationId,
307
+ ...salvageCommit,
308
+ dirtyPaths: inspection.dirtyPaths,
309
+ });
310
+ }
311
+
312
+ const salvageEvidenceRef = writeSalvageEvidence({
313
+ projectRoot,
314
+ batchId,
315
+ taskId,
316
+ inspection,
317
+ recommendedAction,
318
+ classification: workerResult.classification,
319
+ salvageCommit,
320
+ });
321
+
322
+ return {
323
+ salvageable: inspection.salvageable,
324
+ changedFileCount: inspection.changedFileCount,
325
+ salvageEvidenceRef,
326
+ retryCommand,
327
+ salvageCommitted: salvageCommit?.committed === true,
328
+ salvageCommitSha: salvageCommit?.commitSha ?? null,
329
+ salvageCommitRefused: salvageCommit?.refused === true,
330
+ salvageCommitReason: salvageCommit?.reason ?? null,
331
+ };
332
+ }