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.
- package/LICENSE +21 -0
- package/README.md +563 -0
- package/bin/get-version.mjs +35 -0
- package/bin/spine-batch.mjs +425 -0
- package/bin/spine-cli/batch.mjs +47 -0
- package/bin/spine-cli/gate.mjs +10 -0
- package/bin/spine-cli/integrate.mjs +10 -0
- package/bin/spine-cli/plan.mjs +12 -0
- package/bin/spine-cli/shared.mjs +73 -0
- package/bin/spine-cli/status.mjs +18 -0
- package/bin/spine-cli/verify.mjs +199 -0
- package/bin/spine-config.mjs +212 -0
- package/bin/spine-dashboard.mjs +178 -0
- package/bin/spine-deps.mjs +69 -0
- package/bin/spine-doctor.mjs +468 -0
- package/bin/spine-gate.mjs +137 -0
- package/bin/spine-init.mjs +398 -0
- package/bin/spine-integrate.mjs +77 -0
- package/bin/spine-journal.mjs +198 -0
- package/bin/spine-migrate-from-taskplane.mjs +136 -0
- package/bin/spine-plan.mjs +83 -0
- package/bin/spine-preflight.mjs +700 -0
- package/bin/spine-report-progress.d.mts +28 -0
- package/bin/spine-report-progress.mjs +126 -0
- package/bin/spine-review-step.d.mts +24 -0
- package/bin/spine-review-step.mjs +124 -0
- package/bin/spine-rules.mjs +35 -0
- package/bin/spine-settings.mjs +186 -0
- package/bin/spine-state.mjs +110 -0
- package/bin/spine-status.mjs +79 -0
- package/bin/spine-tasks.mjs +293 -0
- package/bin/spine-worker-runner.mjs +314 -0
- package/bin/spine.mjs +404 -0
- package/extensions/spine/settings-slash.ts +20 -0
- package/extensions/spine/slash-commands.ts +806 -0
- package/extensions/spine/worker-tools.ts +279 -0
- package/extensions/spine-orchestrator.ts +12 -0
- package/package.json +72 -0
- package/skills/.gitkeep +0 -0
- package/skills/create-spine-tasks/SKILL.md +375 -0
- package/skills/create-spine-tasks/references/context-template.md +25 -0
- package/skills/create-spine-tasks/references/contract-template.md +98 -0
- package/skills/create-spine-tasks/references/explore-template.md +63 -0
- package/skills/create-spine-tasks/references/prompt-template.md +276 -0
- package/src/batch/abort.mjs +272 -0
- package/src/batch/agent-session-worker.mjs +297 -0
- package/src/batch/batch-state-io.mjs +53 -0
- package/src/batch/contract-verify.mjs +310 -0
- package/src/batch/detached-start.mjs +778 -0
- package/src/batch/diagnosis.mjs +378 -0
- package/src/batch/engine-lanes/merge.mjs +632 -0
- package/src/batch/engine-lanes/queue.mjs +208 -0
- package/src/batch/engine-lanes/review.mjs +970 -0
- package/src/batch/engine-lanes.mjs +388 -0
- package/src/batch/engine-scope.mjs +251 -0
- package/src/batch/engine.mjs +445 -0
- package/src/batch/evidence-command.mjs +157 -0
- package/src/batch/evidence.mjs +203 -0
- package/src/batch/gate.mjs +359 -0
- package/src/batch/git-commit-env.mjs +46 -0
- package/src/batch/git-exec.mjs +26 -0
- package/src/batch/heartbeat.mjs +430 -0
- package/src/batch/integrate.mjs +414 -0
- package/src/batch/journal-rebuild.mjs +388 -0
- package/src/batch/journal.mjs +373 -0
- package/src/batch/lane-commit.mjs +153 -0
- package/src/batch/lifecycle.mjs +364 -0
- package/src/batch/metrics.mjs +265 -0
- package/src/batch/orphan-detect.mjs +210 -0
- package/src/batch/post-merge-limbo.mjs +80 -0
- package/src/batch/postmortem.mjs +296 -0
- package/src/batch/readers/spine-state.mjs +95 -0
- package/src/batch/readers/taskplane-state.mjs +136 -0
- package/src/batch/reconcile.mjs +729 -0
- package/src/batch/resume-common.mjs +104 -0
- package/src/batch/resume-engine.mjs +66 -0
- package/src/batch/resume-multi-lanes.mjs +479 -0
- package/src/batch/resume-multi-validate.mjs +187 -0
- package/src/batch/resume-multi.mjs +254 -0
- package/src/batch/resume.mjs +475 -0
- package/src/batch/retry.mjs +257 -0
- package/src/batch/review-scope.mjs +136 -0
- package/src/batch/review.mjs +1045 -0
- package/src/batch/salvage.mjs +332 -0
- package/src/batch/state.mjs +680 -0
- package/src/batch/task-stall-budget.mjs +88 -0
- package/src/batch/worker-host.mjs +683 -0
- package/src/batch/worker-output.mjs +253 -0
- package/src/batch/worker-prompt.mjs +84 -0
- package/src/batch/worktree.mjs +290 -0
- package/src/cli/deps.mjs +141 -0
- package/src/cli/handoff.mjs +364 -0
- package/src/cli/rules.mjs +429 -0
- package/src/cli/settings-set.mjs +171 -0
- package/src/cli/settings-show.mjs +140 -0
- package/src/cli/settings-slash.mjs +107 -0
- package/src/config/contract.mjs +56 -0
- package/src/config/cursor-rules/discover.mjs +416 -0
- package/src/config/cursor-rules/index.mjs +39 -0
- package/src/config/cursor-rules/match-globs.mjs +132 -0
- package/src/config/cursor-rules/parse-frontmatter.mjs +222 -0
- package/src/config/cursor-rules/priority.mjs +26 -0
- package/src/config/cursor-rules/profile.mjs +403 -0
- package/src/config/cursor-rules/select.mjs +259 -0
- package/src/config/defaults.mjs +36 -0
- package/src/config/env-overrides.mjs +207 -0
- package/src/config/merge-defaults.mjs +47 -0
- package/src/config/pi-spine-root.mjs +93 -0
- package/src/config/reviewer-context.mjs +243 -0
- package/src/config/settings-fields.mjs +265 -0
- package/src/config/worker-backend.mjs +116 -0
- package/src/config/worker-context.mjs +402 -0
- package/src/config/worker-launch-script.mjs +130 -0
- package/src/config/worktree-setup-hook.mjs +122 -0
- package/src/dashboard/public/dashboard.css +77 -0
- package/src/dashboard/public/dashboard.js +349 -0
- package/src/dashboard/public/index.html +80 -0
- package/src/dashboard/server.mjs +155 -0
- package/src/dashboard/snapshot.mjs +414 -0
- package/src/dashboard/sse.mjs +15 -0
- package/src/dashboard/view.mjs +292 -0
- package/src/doctor/agent-model-inherit.mjs +101 -0
- package/src/doctor/batch-size-guidance.mjs +72 -0
- package/src/doctor/coexistence.mjs +312 -0
- package/src/doctor/rules-manifest.mjs +133 -0
- package/src/doctor/stale-path.mjs +156 -0
- package/src/doctor/stall-config.mjs +108 -0
- package/src/doctor/suggest-max-parallel.mjs +53 -0
- package/src/doctor/task-packet-size.mjs +110 -0
- package/src/doctor/worktree-health.mjs +71 -0
- package/src/migrate/taskplane-config.mjs +116 -0
- package/src/planner/cycles.mjs +63 -0
- package/src/planner/format-plan.mjs +132 -0
- package/src/planner/graph.mjs +99 -0
- package/src/planner/index.mjs +125 -0
- package/src/planner/lanes.mjs +111 -0
- package/src/planner/pending.mjs +39 -0
- package/src/planner/scope.mjs +142 -0
- package/src/process/liveness.mjs +21 -0
- package/src/tasks/packet/discover.mjs +40 -0
- package/src/tasks/packet/index.mjs +81 -0
- package/src/tasks/packet/merge-deps.mjs +79 -0
- package/src/tasks/packet/parse-prompt.mjs +445 -0
- package/src/tasks/packet/parse-status.mjs +118 -0
- package/src/tasks/packet/validate-contract.mjs +115 -0
- package/src/tasks/packet/validate-prompt.mjs +99 -0
- package/src/worker-tools/report-progress.d.mts +18 -0
- package/src/worker-tools/report-progress.mjs +59 -0
- package/src/worker-tools/request-gate.d.mts +17 -0
- package/src/worker-tools/request-gate.mjs +54 -0
- package/templates/agents/reviewer.md +123 -0
- package/templates/agents/supervisor.md +59 -0
- package/templates/agents/worker.md +122 -0
- package/templates/rules-profile.json +27 -0
- package/templates/spine-config.json +64 -0
- package/templates/tasks/CONTEXT.md +30 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded worker stdout/stderr capture on terminal failure (FR-STALL-01).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { appendJournalEvent } from "./journal.mjs";
|
|
8
|
+
|
|
9
|
+
const DEFAULT_MAX_BYTES = 262_144;
|
|
10
|
+
const DEFAULT_TAIL_LINES = 200;
|
|
11
|
+
const TRUNCATION_MARKER = "\n...[worker output truncated]...\n";
|
|
12
|
+
|
|
13
|
+
const BUILTIN_REDACT_PATTERNS = [
|
|
14
|
+
/DATABASE_URL=\S+/gi,
|
|
15
|
+
/(?:postgres|mysql|mongodb)(?:\+srv)?:\/\/[^\s]+/gi,
|
|
16
|
+
/(?:bearer|token|secret|password|api[_-]?key)\s*[:=]\s*\S+/gi,
|
|
17
|
+
/sk-[a-zA-Z0-9]{20,}/g,
|
|
18
|
+
/ghp_[a-zA-Z0-9]{20,}/g,
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {object} [config]
|
|
23
|
+
*/
|
|
24
|
+
export function resolveWorkerOutputConfig(config = {}) {
|
|
25
|
+
const lanes = config.lanes ?? {};
|
|
26
|
+
const maxBytes = Number(lanes.workerOutputMaxBytes);
|
|
27
|
+
const tailLines = Number(lanes.workerOutputTailLines);
|
|
28
|
+
/** @type {RegExp[]} */
|
|
29
|
+
const denyPatterns = [];
|
|
30
|
+
if (Array.isArray(lanes.workerOutputDenyPatterns)) {
|
|
31
|
+
for (const pattern of lanes.workerOutputDenyPatterns) {
|
|
32
|
+
if (typeof pattern !== "string" || !pattern.trim()) continue;
|
|
33
|
+
try {
|
|
34
|
+
denyPatterns.push(new RegExp(pattern, "gi"));
|
|
35
|
+
} catch {
|
|
36
|
+
// skip invalid config patterns
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
maxBytes: Number.isFinite(maxBytes) && maxBytes > 0 ? maxBytes : DEFAULT_MAX_BYTES,
|
|
43
|
+
tailLines: Number.isFinite(tailLines) && tailLines > 0 ? tailLines : DEFAULT_TAIL_LINES,
|
|
44
|
+
retainOnSuccess: lanes.retainWorkerOutputOnSuccess === true,
|
|
45
|
+
denyPatterns,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {string} text
|
|
51
|
+
* @param {ReturnType<typeof resolveWorkerOutputConfig>} outputConfig
|
|
52
|
+
*/
|
|
53
|
+
export function redactWorkerOutput(text, outputConfig) {
|
|
54
|
+
if (!text) return "";
|
|
55
|
+
let out = text;
|
|
56
|
+
for (const pattern of BUILTIN_REDACT_PATTERNS) {
|
|
57
|
+
out = out.replace(pattern, "[REDACTED]");
|
|
58
|
+
}
|
|
59
|
+
for (const pattern of outputConfig.denyPatterns) {
|
|
60
|
+
out = out.replace(pattern, "[REDACTED]");
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {string} raw
|
|
67
|
+
* @param {ReturnType<typeof resolveWorkerOutputConfig>} outputConfig
|
|
68
|
+
*/
|
|
69
|
+
export function captureWorkerOutputTail(raw, outputConfig) {
|
|
70
|
+
if (!raw) return "";
|
|
71
|
+
|
|
72
|
+
const lines = raw.split(/\r?\n/);
|
|
73
|
+
const tail =
|
|
74
|
+
lines.length > outputConfig.tailLines
|
|
75
|
+
? lines.slice(-outputConfig.tailLines).join("\n")
|
|
76
|
+
: raw;
|
|
77
|
+
|
|
78
|
+
const encoded = Buffer.from(tail, "utf-8");
|
|
79
|
+
if (encoded.byteLength <= outputConfig.maxBytes) {
|
|
80
|
+
return tail;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const markerBytes = Buffer.byteLength(TRUNCATION_MARKER, "utf-8");
|
|
84
|
+
const budget = Math.max(0, outputConfig.maxBytes - markerBytes);
|
|
85
|
+
let slice = encoded.subarray(encoded.byteLength - budget).toString("utf-8");
|
|
86
|
+
const newline = slice.indexOf("\n");
|
|
87
|
+
if (newline >= 0 && newline < slice.length - 1) {
|
|
88
|
+
slice = slice.slice(newline + 1);
|
|
89
|
+
}
|
|
90
|
+
return `${TRUNCATION_MARKER}${slice}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @param {string} classification
|
|
95
|
+
* @param {boolean} ok
|
|
96
|
+
* @param {ReturnType<typeof resolveWorkerOutputConfig>} outputConfig
|
|
97
|
+
*/
|
|
98
|
+
export function shouldCaptureWorkerOutput(classification, ok, outputConfig) {
|
|
99
|
+
if (ok) return outputConfig.retainOnSuccess;
|
|
100
|
+
return classification === "stall_timeout" || classification === "failed" || classification === "aborted";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param {string} projectRoot
|
|
105
|
+
* @param {string} batchId
|
|
106
|
+
* @param {number} laneNumber
|
|
107
|
+
* @param {string} taskId
|
|
108
|
+
*/
|
|
109
|
+
export function workerOutputLogPath(projectRoot, batchId, laneNumber, taskId) {
|
|
110
|
+
return path.join(
|
|
111
|
+
projectRoot,
|
|
112
|
+
".spine",
|
|
113
|
+
"runtime",
|
|
114
|
+
batchId,
|
|
115
|
+
"lanes",
|
|
116
|
+
`lane-${laneNumber}`,
|
|
117
|
+
`worker-output-${taskId}.log`,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {string} batchId
|
|
123
|
+
* @param {number} laneNumber
|
|
124
|
+
* @param {string} taskId
|
|
125
|
+
*/
|
|
126
|
+
export function workerOutputLogRef(batchId, laneNumber, taskId) {
|
|
127
|
+
return path.join(
|
|
128
|
+
".spine",
|
|
129
|
+
"runtime",
|
|
130
|
+
batchId,
|
|
131
|
+
"lanes",
|
|
132
|
+
`lane-${laneNumber}`,
|
|
133
|
+
`worker-output-${taskId}.log`,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {object} params
|
|
139
|
+
*/
|
|
140
|
+
export function persistWorkerOutputLog({
|
|
141
|
+
projectRoot,
|
|
142
|
+
batchId,
|
|
143
|
+
laneNumber,
|
|
144
|
+
taskId,
|
|
145
|
+
rawOutput,
|
|
146
|
+
config = {},
|
|
147
|
+
}) {
|
|
148
|
+
const outputConfig = resolveWorkerOutputConfig(config);
|
|
149
|
+
const captured = captureWorkerOutputTail(redactWorkerOutput(rawOutput, outputConfig), outputConfig);
|
|
150
|
+
if (!captured.trim()) return null;
|
|
151
|
+
|
|
152
|
+
const logPath = workerOutputLogPath(projectRoot, batchId, laneNumber, taskId);
|
|
153
|
+
fs.mkdirSync(path.dirname(logPath), { recursive: true });
|
|
154
|
+
fs.writeFileSync(logPath, captured, "utf-8");
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
logPath,
|
|
158
|
+
logRef: workerOutputLogRef(batchId, laneNumber, taskId),
|
|
159
|
+
output: captured,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @param {object} params
|
|
165
|
+
*/
|
|
166
|
+
export function recordStallKilled({
|
|
167
|
+
projectRoot,
|
|
168
|
+
batchId,
|
|
169
|
+
laneNumber,
|
|
170
|
+
taskId,
|
|
171
|
+
correlationId,
|
|
172
|
+
exitCode,
|
|
173
|
+
logPath,
|
|
174
|
+
logRef,
|
|
175
|
+
stallDeadline,
|
|
176
|
+
signals,
|
|
177
|
+
}) {
|
|
178
|
+
appendJournalEvent(projectRoot, batchId, "lane.stall_killed", {
|
|
179
|
+
laneNumber,
|
|
180
|
+
taskId,
|
|
181
|
+
correlationId,
|
|
182
|
+
exitCode,
|
|
183
|
+
logPath: logRef ?? logPath,
|
|
184
|
+
stallDeadline,
|
|
185
|
+
progressSignals: signals,
|
|
186
|
+
statusMtimeMs: signals?.statusMtimeMs,
|
|
187
|
+
lastCommitAtMs: signals?.lastCommitAtMs,
|
|
188
|
+
fileScopeMtimeMs: signals?.fileScopeMtimeMs,
|
|
189
|
+
stepCompletedAtMs: signals?.stepCompletedAtMs,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* @param {object} params
|
|
195
|
+
*/
|
|
196
|
+
export function finalizeWorkerOutput({
|
|
197
|
+
rawOutput = "",
|
|
198
|
+
classification,
|
|
199
|
+
ok,
|
|
200
|
+
projectRoot,
|
|
201
|
+
batchId,
|
|
202
|
+
laneNumber,
|
|
203
|
+
taskId,
|
|
204
|
+
correlationId,
|
|
205
|
+
exitCode,
|
|
206
|
+
stallDeadline,
|
|
207
|
+
signals,
|
|
208
|
+
config = {},
|
|
209
|
+
}) {
|
|
210
|
+
const outputConfig = resolveWorkerOutputConfig(config);
|
|
211
|
+
if (!shouldCaptureWorkerOutput(classification, ok, outputConfig)) {
|
|
212
|
+
return { output: rawOutput, logPath: null, logRef: null };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const redacted = redactWorkerOutput(rawOutput, outputConfig);
|
|
216
|
+
const output = captureWorkerOutputTail(redacted, outputConfig) || redacted;
|
|
217
|
+
|
|
218
|
+
if (!projectRoot || !batchId || laneNumber == null || !taskId) {
|
|
219
|
+
return { output, logPath: null, logRef: null };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const persisted = persistWorkerOutputLog({
|
|
223
|
+
projectRoot,
|
|
224
|
+
batchId,
|
|
225
|
+
laneNumber,
|
|
226
|
+
taskId,
|
|
227
|
+
rawOutput,
|
|
228
|
+
config,
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
if (classification === "stall_timeout" && persisted) {
|
|
232
|
+
recordStallKilled({
|
|
233
|
+
projectRoot,
|
|
234
|
+
batchId,
|
|
235
|
+
laneNumber,
|
|
236
|
+
taskId,
|
|
237
|
+
correlationId,
|
|
238
|
+
exitCode: exitCode ?? 124,
|
|
239
|
+
logPath: persisted.logPath,
|
|
240
|
+
logRef: persisted.logRef,
|
|
241
|
+
stallDeadline,
|
|
242
|
+
signals,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
output: persisted?.output ?? output,
|
|
248
|
+
logPath: persisted?.logPath ?? null,
|
|
249
|
+
logRef: persisted?.logRef ?? null,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export { TRUNCATION_MARKER, DEFAULT_MAX_BYTES, DEFAULT_TAIL_LINES };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared worker tail prompt text for pi runners (SP-067).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { buildWorkerContextAsync } from "../config/worker-context.mjs";
|
|
8
|
+
|
|
9
|
+
export function buildReviewLevelHint(reviewLevel) {
|
|
10
|
+
if (reviewLevel <= 0) {
|
|
11
|
+
return "";
|
|
12
|
+
}
|
|
13
|
+
let hint =
|
|
14
|
+
"When Review Level ≥ 1, run plan review only at step checkpoints: spine review step --step N --type plan (or spine_review_step with type=plan). ";
|
|
15
|
+
if (reviewLevel >= 2) {
|
|
16
|
+
hint +=
|
|
17
|
+
"Do not run code or final review from inside the worker — the batch engine runs those after .DONE. ";
|
|
18
|
+
} else {
|
|
19
|
+
hint +=
|
|
20
|
+
"Do not run final review from the worker — the batch engine runs final review after .DONE. ";
|
|
21
|
+
}
|
|
22
|
+
hint +=
|
|
23
|
+
"On REVISE, fix feedback before continuing. On review spawn failure, stop with non-zero exit. After creating .DONE, exit immediately with no further tool calls or reviewer spawns. ";
|
|
24
|
+
return hint;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const WORKER_TOOLS_HINT =
|
|
28
|
+
"Prefer spine_review_step, spine_report_progress, and spine_request_gate Pi tools over bash when available. ";
|
|
29
|
+
|
|
30
|
+
export function buildCommitBoundaryHint(taskIdHint) {
|
|
31
|
+
return `Commit at step boundaries when you change files (feat(${taskIdHint}): complete Step N — {step title}). `;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function buildDoneCheckpointHint(donePath) {
|
|
35
|
+
return (
|
|
36
|
+
`The batch engine auto-commits any remaining uncommitted work when you create ${donePath}, ` +
|
|
37
|
+
"but uncommitted changes without .DONE fail the batch. " +
|
|
38
|
+
`Create ${donePath} only when all completion criteria are met.`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function buildWorkerTailPrompt({
|
|
43
|
+
worktreePath,
|
|
44
|
+
taskFolder,
|
|
45
|
+
donePath,
|
|
46
|
+
taskIdHint = path.basename(taskFolder).match(/^([A-Z]+-\d+)/)?.[1] ?? "TASK-ID",
|
|
47
|
+
reviewLevel = 0,
|
|
48
|
+
includePromptInclude = false,
|
|
49
|
+
config = {},
|
|
50
|
+
projectRoot = worktreePath || process.cwd(),
|
|
51
|
+
taskFileScope = [],
|
|
52
|
+
journal,
|
|
53
|
+
}) {
|
|
54
|
+
const workerAgentPath = worktreePath
|
|
55
|
+
? path.join(worktreePath, ".spine", "agents", "worker.md")
|
|
56
|
+
: null;
|
|
57
|
+
const promptPath = path.join(taskFolder, "PROMPT.md");
|
|
58
|
+
const agentAppend =
|
|
59
|
+
workerAgentPath && fs.existsSync(workerAgentPath) ? `\n\n@${workerAgentPath}` : "";
|
|
60
|
+
const promptInclude =
|
|
61
|
+
includePromptInclude && fs.existsSync(promptPath) ? `\n\n@${promptPath}` : "";
|
|
62
|
+
|
|
63
|
+
const context = await buildWorkerContextAsync({
|
|
64
|
+
config,
|
|
65
|
+
projectRoot,
|
|
66
|
+
taskFileScope,
|
|
67
|
+
journal,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
`Complete this task in the worktree (${worktreePath || "."}). Follow PROMPT.md, keep STATUS.md current, run npm test. ` +
|
|
72
|
+
WORKER_TOOLS_HINT +
|
|
73
|
+
buildReviewLevelHint(reviewLevel) +
|
|
74
|
+
buildCommitBoundaryHint(taskIdHint) +
|
|
75
|
+
buildDoneCheckpointHint(donePath) +
|
|
76
|
+
context.text +
|
|
77
|
+
agentAppend +
|
|
78
|
+
promptInclude
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function taskIdFromFolder(taskFolder) {
|
|
83
|
+
return path.basename(taskFolder).match(/^([A-Z]+-\d+)/)?.[1] ?? "TASK-ID";
|
|
84
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Git worktree provisioning for pi-spine lanes (PRD §9.4).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
8
|
+
import { resolveWorktreeSetupHook } from "../config/worktree-setup-hook.mjs";
|
|
9
|
+
|
|
10
|
+
const WORKTREE_SETUP_HOOK_TIMEOUT_MS = 120_000;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} projectRoot
|
|
14
|
+
* @param {string[]} args
|
|
15
|
+
*/
|
|
16
|
+
function git(projectRoot, args) {
|
|
17
|
+
return execFileSync("git", args, {
|
|
18
|
+
cwd: projectRoot,
|
|
19
|
+
encoding: "utf-8",
|
|
20
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
21
|
+
}).trim();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} fromDir
|
|
26
|
+
* @param {string} toPath
|
|
27
|
+
*/
|
|
28
|
+
function posixRelative(fromDir, toPath) {
|
|
29
|
+
const from = fs.realpathSync.native?.(fromDir) ?? fs.realpathSync(fromDir);
|
|
30
|
+
const to = fs.realpathSync.native?.(toPath) ?? fs.realpathSync(toPath);
|
|
31
|
+
return path.relative(from, to).split(path.sep).join("/");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {string} gitfilePath
|
|
36
|
+
* @param {string} resolveDir
|
|
37
|
+
*/
|
|
38
|
+
function readGitfileAdminDir(gitfilePath, resolveDir) {
|
|
39
|
+
const content = fs.readFileSync(gitfilePath, "utf-8").trim();
|
|
40
|
+
const match = content.match(/^gitdir:\s*(.+)$/);
|
|
41
|
+
if (!match) {
|
|
42
|
+
throw new Error(`Invalid lane worktree gitfile: ${gitfilePath}`);
|
|
43
|
+
}
|
|
44
|
+
const rawGitdir = match[1].trim();
|
|
45
|
+
return path.isAbsolute(rawGitdir) ? rawGitdir : path.resolve(resolveDir, rawGitdir);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @param {string} projectRoot
|
|
50
|
+
* @param {string} worktreePath
|
|
51
|
+
*/
|
|
52
|
+
export function findAdminDirForWorktree(projectRoot, worktreePath) {
|
|
53
|
+
const laneGitFile = path.resolve(path.join(worktreePath, ".git"));
|
|
54
|
+
const worktreesDir = path.join(projectRoot, ".git", "worktrees");
|
|
55
|
+
if (!fs.existsSync(worktreesDir)) {
|
|
56
|
+
throw new Error(`Missing git worktrees admin dir: ${worktreesDir}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (const entry of fs.readdirSync(worktreesDir, { withFileTypes: true })) {
|
|
60
|
+
if (!entry.isDirectory()) continue;
|
|
61
|
+
const adminDir = path.join(worktreesDir, entry.name);
|
|
62
|
+
const gitdirFile = path.join(adminDir, "gitdir");
|
|
63
|
+
if (!fs.existsSync(gitdirFile)) continue;
|
|
64
|
+
|
|
65
|
+
const content = fs.readFileSync(gitdirFile, "utf-8").trim();
|
|
66
|
+
const absLaneGit = path.isAbsolute(content)
|
|
67
|
+
? content
|
|
68
|
+
: path.resolve(path.dirname(gitdirFile), content);
|
|
69
|
+
const resolvedLaneGit = fs.realpathSync.native?.(absLaneGit) ?? fs.realpathSync(absLaneGit);
|
|
70
|
+
const resolvedTarget = fs.realpathSync.native?.(laneGitFile) ?? fs.realpathSync(laneGitFile);
|
|
71
|
+
if (resolvedLaneGit === resolvedTarget) {
|
|
72
|
+
return adminDir;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
throw new Error(`No git worktree admin metadata found for ${worktreePath}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Rewrite lane `.git` gitfile and admin `gitdir` to relative posix paths.
|
|
81
|
+
* Uses the admin directory git assigned (e.g. lane-111), not lane-N alone.
|
|
82
|
+
*
|
|
83
|
+
* @param {object} params
|
|
84
|
+
* @param {string} params.projectRoot
|
|
85
|
+
* @param {string} params.worktreePath
|
|
86
|
+
*/
|
|
87
|
+
export function normalizeLaneWorktreeGitPaths({ projectRoot, worktreePath }) {
|
|
88
|
+
const laneGitFile = path.join(worktreePath, ".git");
|
|
89
|
+
if (!fs.existsSync(laneGitFile)) {
|
|
90
|
+
throw new Error(`Lane worktree .git file missing: ${laneGitFile}`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let adminWtDir;
|
|
94
|
+
try {
|
|
95
|
+
adminWtDir = readGitfileAdminDir(laneGitFile, worktreePath);
|
|
96
|
+
if (!fs.existsSync(adminWtDir)) {
|
|
97
|
+
adminWtDir = findAdminDirForWorktree(projectRoot, worktreePath);
|
|
98
|
+
}
|
|
99
|
+
} catch {
|
|
100
|
+
adminWtDir = findAdminDirForWorktree(projectRoot, worktreePath);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const laneGitdirRel = posixRelative(worktreePath, adminWtDir);
|
|
104
|
+
fs.writeFileSync(laneGitFile, `gitdir: ${laneGitdirRel}\n`, "utf-8");
|
|
105
|
+
|
|
106
|
+
const adminGitdirFile = path.join(adminWtDir, "gitdir");
|
|
107
|
+
if (!fs.existsSync(adminGitdirFile)) {
|
|
108
|
+
throw new Error(`Admin worktree gitdir missing: ${adminGitdirFile}`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const laneGitTarget = path.resolve(worktreePath, ".git");
|
|
112
|
+
const currentAdmin = fs.readFileSync(adminGitdirFile, "utf-8").trim();
|
|
113
|
+
const absLaneGit = path.isAbsolute(currentAdmin)
|
|
114
|
+
? currentAdmin
|
|
115
|
+
: path.resolve(path.dirname(adminGitdirFile), currentAdmin);
|
|
116
|
+
const resolvedLaneGit = fs.existsSync(absLaneGit) ? absLaneGit : laneGitTarget;
|
|
117
|
+
const adminGitdirRel = posixRelative(path.dirname(adminGitdirFile), resolvedLaneGit);
|
|
118
|
+
fs.writeFileSync(adminGitdirFile, `${adminGitdirRel}\n`, "utf-8");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {string} worktreePath
|
|
123
|
+
*/
|
|
124
|
+
export function assertLaneWorktreeGitHealthy(worktreePath) {
|
|
125
|
+
try {
|
|
126
|
+
git(worktreePath, ["rev-parse", "HEAD"]);
|
|
127
|
+
git(worktreePath, ["status", "--porcelain"]);
|
|
128
|
+
} catch (err) {
|
|
129
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
130
|
+
throw new Error(`Lane worktree git unhealthy (${worktreePath}): ${message}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Idempotent normalize + health assert for resume repair.
|
|
136
|
+
*
|
|
137
|
+
* @param {object} params
|
|
138
|
+
* @param {string} params.projectRoot
|
|
139
|
+
* @param {string} params.worktreePath
|
|
140
|
+
* @param {number} params.laneNumber
|
|
141
|
+
*/
|
|
142
|
+
export function repairLaneWorktreeGitMetadata({ projectRoot, worktreePath }) {
|
|
143
|
+
normalizeLaneWorktreeGitPaths({ projectRoot, worktreePath });
|
|
144
|
+
assertLaneWorktreeGitHealthy(worktreePath);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @param {string} projectRoot
|
|
149
|
+
* @param {string} batchId
|
|
150
|
+
* @param {number} laneNumber
|
|
151
|
+
*/
|
|
152
|
+
export function laneWorktreePath(projectRoot, batchId, laneNumber = 1) {
|
|
153
|
+
return path.join(projectRoot, ".worktrees", `spine-${batchId}`, `lane-${laneNumber}`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {string} batchId
|
|
158
|
+
* @param {number} [laneNumber]
|
|
159
|
+
*/
|
|
160
|
+
export function laneTaskBranch(batchId, laneNumber = 1) {
|
|
161
|
+
return `task/spine-lane-${laneNumber}-${batchId}`;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* @param {string} projectRoot
|
|
166
|
+
* @param {string} baseBranch
|
|
167
|
+
* @param {string} orchBranch
|
|
168
|
+
*/
|
|
169
|
+
export function ensureOrchBranch(projectRoot, baseBranch, orchBranch) {
|
|
170
|
+
try {
|
|
171
|
+
git(projectRoot, ["rev-parse", "--verify", orchBranch]);
|
|
172
|
+
return;
|
|
173
|
+
} catch {
|
|
174
|
+
git(projectRoot, ["branch", orchBranch, baseBranch]);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @param {object} params
|
|
180
|
+
*/
|
|
181
|
+
export function provisionLaneWorktree({
|
|
182
|
+
projectRoot,
|
|
183
|
+
batchId,
|
|
184
|
+
laneNumber = 1,
|
|
185
|
+
orchBranch,
|
|
186
|
+
}) {
|
|
187
|
+
const worktreePath = laneWorktreePath(projectRoot, batchId, laneNumber);
|
|
188
|
+
const taskBranch = laneTaskBranch(batchId, laneNumber);
|
|
189
|
+
|
|
190
|
+
if (fs.existsSync(worktreePath)) {
|
|
191
|
+
throw new Error(`Worktree already exists: ${worktreePath}`);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
fs.mkdirSync(path.dirname(worktreePath), { recursive: true });
|
|
195
|
+
git(projectRoot, ["worktree", "add", "-b", taskBranch, worktreePath, orchBranch]);
|
|
196
|
+
normalizeLaneWorktreeGitPaths({ projectRoot, worktreePath });
|
|
197
|
+
|
|
198
|
+
return { worktreePath, taskBranch, orchBranch };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* @param {string} projectRoot
|
|
203
|
+
* @param {string} batchId
|
|
204
|
+
* @param {number} [laneNumber]
|
|
205
|
+
*/
|
|
206
|
+
export function removeLaneWorktree(projectRoot, batchId, laneNumber = 1) {
|
|
207
|
+
const worktreePath = laneWorktreePath(projectRoot, batchId, laneNumber);
|
|
208
|
+
if (!fs.existsSync(worktreePath)) return;
|
|
209
|
+
|
|
210
|
+
try {
|
|
211
|
+
git(projectRoot, ["worktree", "remove", "--force", worktreePath]);
|
|
212
|
+
} catch {
|
|
213
|
+
fs.rmSync(worktreePath, { recursive: true, force: true, maxRetries: 3 });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* @param {string} projectRoot
|
|
219
|
+
* @param {string} batchId
|
|
220
|
+
* @param {number} maxLaneNumber
|
|
221
|
+
*/
|
|
222
|
+
export function removeLaneWorktrees(projectRoot, batchId, maxLaneNumber) {
|
|
223
|
+
for (let laneNumber = 1; laneNumber <= maxLaneNumber; laneNumber++) {
|
|
224
|
+
removeLaneWorktree(projectRoot, batchId, laneNumber);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* @param {object} params
|
|
230
|
+
*/
|
|
231
|
+
export function runWorktreeSetupHook({
|
|
232
|
+
projectRoot,
|
|
233
|
+
worktreePath,
|
|
234
|
+
batchId,
|
|
235
|
+
laneNumber,
|
|
236
|
+
config = {},
|
|
237
|
+
}) {
|
|
238
|
+
const hookPath = resolveWorktreeSetupHook(projectRoot, config);
|
|
239
|
+
if (!hookPath) {
|
|
240
|
+
return { ok: true, skipped: true, durationMs: 0 };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const startedAt = Date.now();
|
|
244
|
+
const result = spawnSync(hookPath, {
|
|
245
|
+
cwd: worktreePath,
|
|
246
|
+
env: {
|
|
247
|
+
...process.env,
|
|
248
|
+
SPINE_PROJECT_ROOT: projectRoot,
|
|
249
|
+
SPINE_WORKTREE: worktreePath,
|
|
250
|
+
SPINE_BATCH_ID: batchId,
|
|
251
|
+
SPINE_LANE_NUMBER: String(laneNumber),
|
|
252
|
+
},
|
|
253
|
+
encoding: "utf-8",
|
|
254
|
+
timeout: WORKTREE_SETUP_HOOK_TIMEOUT_MS,
|
|
255
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
256
|
+
});
|
|
257
|
+
const durationMs = Date.now() - startedAt;
|
|
258
|
+
|
|
259
|
+
if (result.error) {
|
|
260
|
+
throw new Error(`worktree setup hook failed: ${result.error.message}`);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const stdout = result.stdout ?? "";
|
|
264
|
+
const lines = stdout.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
|
|
265
|
+
const lastLine = lines.at(-1);
|
|
266
|
+
if (!lastLine) {
|
|
267
|
+
throw new Error("worktree setup hook produced no JSON on stdout");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
let parsed;
|
|
271
|
+
try {
|
|
272
|
+
parsed = JSON.parse(lastLine);
|
|
273
|
+
} catch (err) {
|
|
274
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
275
|
+
throw new Error(`worktree setup hook returned invalid JSON: ${message}`);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (parsed.ok !== true) {
|
|
279
|
+
const message = typeof parsed.error === "string" && parsed.error.trim()
|
|
280
|
+
? parsed.error.trim()
|
|
281
|
+
: "hook returned ok: false";
|
|
282
|
+
throw new Error(`worktree setup hook failed: ${message}`);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (result.status !== 0) {
|
|
286
|
+
throw new Error(`worktree setup hook exited with code ${result.status ?? "unknown"}`);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return { ok: true, durationMs };
|
|
290
|
+
}
|