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,364 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batch lifecycle: dismiss, complete, archive-first (FR-BATCH-15/16, §18.6).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { buildDiagnosisOutput } from "./diagnosis.mjs";
|
|
8
|
+
import { assertOrchIntegratable } from "./integrate.mjs";
|
|
9
|
+
import { loadSpineConfig } from "../../bin/spine-config.mjs";
|
|
10
|
+
import { appendJournalEvent } from "./journal.mjs";
|
|
11
|
+
import { recordBatchTerminalMetric } from "./metrics.mjs";
|
|
12
|
+
import { writeBatchPostMortem } from "./postmortem.mjs";
|
|
13
|
+
import { appendBatchHistoryEntry } from "./state.mjs";
|
|
14
|
+
import { loadBatchStateFile, parseBatchState, reconcileBatch } from "./reconcile.mjs";
|
|
15
|
+
|
|
16
|
+
const DISMISS_ALLOWED = new Set(["limbo_stale", "completed_manual", "aborted"]);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {string} projectRoot
|
|
20
|
+
* @param {string} batchId
|
|
21
|
+
*/
|
|
22
|
+
export function archiveBatchStatePath(projectRoot, batchId) {
|
|
23
|
+
return path.join(projectRoot, ".spine", "runtime", batchId, "archive", "batch-state.json");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {string} projectRoot
|
|
28
|
+
* @param {string} batchId
|
|
29
|
+
* @param {unknown} raw
|
|
30
|
+
*/
|
|
31
|
+
function archiveBatchState(projectRoot, batchId, raw) {
|
|
32
|
+
const archivePath = archiveBatchStatePath(projectRoot, batchId);
|
|
33
|
+
fs.mkdirSync(path.dirname(archivePath), { recursive: true });
|
|
34
|
+
fs.writeFileSync(archivePath, `${JSON.stringify(raw, null, 2)}\n`, "utf-8");
|
|
35
|
+
|
|
36
|
+
const fd = fs.openSync(archivePath, "r");
|
|
37
|
+
try {
|
|
38
|
+
fs.fsyncSync(fd);
|
|
39
|
+
} finally {
|
|
40
|
+
fs.closeSync(fd);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return archivePath;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {string|null} batchStatePath
|
|
48
|
+
*/
|
|
49
|
+
function clearActiveBatchState(batchStatePath) {
|
|
50
|
+
if (batchStatePath && fs.existsSync(batchStatePath)) {
|
|
51
|
+
fs.unlinkSync(batchStatePath);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @param {object} signals
|
|
57
|
+
*/
|
|
58
|
+
function hasLiveLanes(signals) {
|
|
59
|
+
if (!signals) return false;
|
|
60
|
+
if (signals.hasRunningTasks || signals.hasPendingTasks) return true;
|
|
61
|
+
const lanes = signals.lanes;
|
|
62
|
+
return (
|
|
63
|
+
Array.isArray(lanes) &&
|
|
64
|
+
lanes.some((lane) => {
|
|
65
|
+
const status = String(lane?.status ?? "").toLowerCase();
|
|
66
|
+
return status === "running" || status === "active";
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {object} ctx
|
|
73
|
+
* @param {string} ctx.projectRoot
|
|
74
|
+
* @param {string} [ctx.reason]
|
|
75
|
+
* @param {boolean} [ctx.force]
|
|
76
|
+
* @param {string|null} [ctx.batchId]
|
|
77
|
+
* @param {string|null} [ctx.batchStatePath]
|
|
78
|
+
*/
|
|
79
|
+
export function dismissBatch(ctx) {
|
|
80
|
+
const { projectRoot, reason, force = false } = ctx;
|
|
81
|
+
const loaded = loadBatchStateFile(projectRoot, ctx.batchStatePath ?? null);
|
|
82
|
+
|
|
83
|
+
if (!loaded.path || !loaded.raw) {
|
|
84
|
+
return {
|
|
85
|
+
ok: false,
|
|
86
|
+
exitCode: 1,
|
|
87
|
+
headline: "No active batch to dismiss",
|
|
88
|
+
suggestedCommand: "spine preflight",
|
|
89
|
+
alternatives: ["spine status --diagnose"],
|
|
90
|
+
diagnosis: null,
|
|
91
|
+
batchId: null,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (loaded.parseError) {
|
|
96
|
+
return {
|
|
97
|
+
ok: false,
|
|
98
|
+
exitCode: 1,
|
|
99
|
+
error: loaded.parseError,
|
|
100
|
+
headline: `Cannot parse batch state: ${loaded.parseError}`,
|
|
101
|
+
suggestedCommand: "spine status --diagnose",
|
|
102
|
+
diagnosis: "failed",
|
|
103
|
+
batchId: null,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const reconciliation = reconcileBatch({
|
|
108
|
+
projectRoot,
|
|
109
|
+
batchStatePath: loaded.path,
|
|
110
|
+
batchState: loaded.raw,
|
|
111
|
+
verbose: true,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const diagnosis = reconciliation.diagnosis;
|
|
115
|
+
const batchId = reconciliation.batchId ?? String(loaded.raw.batchId ?? loaded.raw.id ?? "");
|
|
116
|
+
|
|
117
|
+
if (ctx.batchId && batchId && ctx.batchId !== batchId) {
|
|
118
|
+
return {
|
|
119
|
+
ok: false,
|
|
120
|
+
exitCode: 1,
|
|
121
|
+
error: `Active batch is ${batchId}, not ${ctx.batchId}`,
|
|
122
|
+
headline: `Batch ID mismatch — active batch is ${batchId}`,
|
|
123
|
+
suggestedCommand: "spine status --diagnose",
|
|
124
|
+
diagnosis,
|
|
125
|
+
batchId,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (diagnosis === "running" && hasLiveLanes(reconciliation.signals) && !force) {
|
|
130
|
+
const output = buildDiagnosisOutput(diagnosis, {
|
|
131
|
+
batchId,
|
|
132
|
+
phase: reconciliation.phase,
|
|
133
|
+
});
|
|
134
|
+
return {
|
|
135
|
+
ok: false,
|
|
136
|
+
exitCode: 1,
|
|
137
|
+
error: "Batch is still running with live lanes",
|
|
138
|
+
...output,
|
|
139
|
+
headline: `${output.headline} — use --force to dismiss anyway`,
|
|
140
|
+
suggestedCommand: "spine batch dismiss --force",
|
|
141
|
+
alternatives: ["/spine-status --diagnose", ...output.alternatives],
|
|
142
|
+
batchId,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (diagnosis && !DISMISS_ALLOWED.has(diagnosis) && !force) {
|
|
147
|
+
const output = buildDiagnosisOutput(diagnosis, {
|
|
148
|
+
batchId,
|
|
149
|
+
phase: reconciliation.phase,
|
|
150
|
+
failedTaskId: reconciliation.signals?.failedTaskId ?? null,
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
ok: false,
|
|
154
|
+
exitCode: 1,
|
|
155
|
+
error: `Dismiss not allowed for diagnosis: ${diagnosis}`,
|
|
156
|
+
...output,
|
|
157
|
+
headline: `${output.headline} — dismiss refused (use --force to override)`,
|
|
158
|
+
suggestedCommand: output.suggestedCommand,
|
|
159
|
+
batchId,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const archivePath = archiveBatchState(projectRoot, batchId, loaded.raw);
|
|
164
|
+
const postMortemPath = writeBatchPostMortem({
|
|
165
|
+
projectRoot,
|
|
166
|
+
batchState: loaded.raw,
|
|
167
|
+
reconciliation,
|
|
168
|
+
});
|
|
169
|
+
const endedAt = Date.now();
|
|
170
|
+
appendBatchHistoryEntry(projectRoot, {
|
|
171
|
+
batchId,
|
|
172
|
+
action: "dismissed",
|
|
173
|
+
endedAt,
|
|
174
|
+
diagnosis,
|
|
175
|
+
reason: reason ?? null,
|
|
176
|
+
archivePath: path.relative(projectRoot, archivePath),
|
|
177
|
+
postMortemPath,
|
|
178
|
+
});
|
|
179
|
+
appendJournalEvent(projectRoot, batchId, "batch.dismissed", {
|
|
180
|
+
diagnosis,
|
|
181
|
+
reason: reason ?? null,
|
|
182
|
+
archivePath: path.relative(projectRoot, archivePath),
|
|
183
|
+
});
|
|
184
|
+
const configResult = loadSpineConfig(projectRoot);
|
|
185
|
+
recordBatchTerminalMetric({
|
|
186
|
+
projectRoot,
|
|
187
|
+
batchId,
|
|
188
|
+
batchState: { ...loaded.raw, endedAt },
|
|
189
|
+
diagnosis: diagnosis ?? "dismissed",
|
|
190
|
+
config: configResult.config ?? {},
|
|
191
|
+
});
|
|
192
|
+
clearActiveBatchState(loaded.path);
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
ok: true,
|
|
196
|
+
exitCode: 0,
|
|
197
|
+
batchId,
|
|
198
|
+
diagnosis,
|
|
199
|
+
headline: `Batch ${batchId} dismissed and archived`,
|
|
200
|
+
suggestedCommand: "spine preflight",
|
|
201
|
+
alternatives: ["spine plan all"],
|
|
202
|
+
archivePath,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @param {object} ctx
|
|
208
|
+
* @param {string} ctx.projectRoot
|
|
209
|
+
* @param {boolean} [ctx.detectManualMerge]
|
|
210
|
+
* @param {string|null} [ctx.batchId]
|
|
211
|
+
* @param {string|null} [ctx.batchStatePath]
|
|
212
|
+
*/
|
|
213
|
+
export function completeBatch(ctx) {
|
|
214
|
+
const { projectRoot, detectManualMerge = false } = ctx;
|
|
215
|
+
const loaded = loadBatchStateFile(projectRoot, ctx.batchStatePath ?? null);
|
|
216
|
+
|
|
217
|
+
if (!loaded.path || !loaded.raw) {
|
|
218
|
+
return {
|
|
219
|
+
ok: false,
|
|
220
|
+
exitCode: 1,
|
|
221
|
+
headline: "No active batch to complete",
|
|
222
|
+
suggestedCommand: "spine preflight",
|
|
223
|
+
diagnosis: null,
|
|
224
|
+
batchId: null,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (loaded.parseError) {
|
|
229
|
+
return {
|
|
230
|
+
ok: false,
|
|
231
|
+
exitCode: 1,
|
|
232
|
+
error: loaded.parseError,
|
|
233
|
+
headline: `Cannot parse batch state: ${loaded.parseError}`,
|
|
234
|
+
suggestedCommand: "spine status --diagnose",
|
|
235
|
+
diagnosis: "failed",
|
|
236
|
+
batchId: null,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const reconciliation = reconcileBatch({
|
|
241
|
+
projectRoot,
|
|
242
|
+
batchStatePath: loaded.path,
|
|
243
|
+
batchState: loaded.raw,
|
|
244
|
+
verbose: true,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const diagnosis = reconciliation.diagnosis;
|
|
248
|
+
const batchId = reconciliation.batchId ?? String(loaded.raw.batchId ?? loaded.raw.id ?? "");
|
|
249
|
+
const signals = reconciliation.signals ?? {};
|
|
250
|
+
const mergeSatisfied = !signals.mergeResultsEmpty || Boolean(signals.git?.orchMergedToBase);
|
|
251
|
+
const manualMergeOk =
|
|
252
|
+
detectManualMerge &&
|
|
253
|
+
(diagnosis === "completed_manual" || Boolean(signals.git?.orchMergedToBase));
|
|
254
|
+
const allSuccess = Boolean(signals.allTasksTerminalSuccess);
|
|
255
|
+
|
|
256
|
+
if (ctx.batchId && batchId && ctx.batchId !== batchId) {
|
|
257
|
+
return {
|
|
258
|
+
ok: false,
|
|
259
|
+
exitCode: 1,
|
|
260
|
+
error: `Active batch is ${batchId}, not ${ctx.batchId}`,
|
|
261
|
+
headline: `Batch ID mismatch — active batch is ${batchId}`,
|
|
262
|
+
suggestedCommand: "spine status --diagnose",
|
|
263
|
+
diagnosis,
|
|
264
|
+
batchId,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const allowed =
|
|
269
|
+
diagnosis === "completed" ||
|
|
270
|
+
diagnosis === "completed_manual" ||
|
|
271
|
+
(allSuccess && (mergeSatisfied || manualMergeOk));
|
|
272
|
+
|
|
273
|
+
if (!allowed) {
|
|
274
|
+
const output = buildDiagnosisOutput(diagnosis ?? "paused", {
|
|
275
|
+
batchId,
|
|
276
|
+
phase: reconciliation.phase,
|
|
277
|
+
gitMerged: Boolean(signals.git?.orchMergedToBase),
|
|
278
|
+
});
|
|
279
|
+
return {
|
|
280
|
+
ok: false,
|
|
281
|
+
exitCode: 1,
|
|
282
|
+
error: "Batch is not ready to complete",
|
|
283
|
+
...output,
|
|
284
|
+
headline: `${output.headline} — complete refused`,
|
|
285
|
+
suggestedCommand: detectManualMerge
|
|
286
|
+
? "spine status --diagnose"
|
|
287
|
+
: "spine batch complete --detect-manual-merge",
|
|
288
|
+
alternatives: ["spine batch dismiss", ...(output.alternatives ?? [])],
|
|
289
|
+
batchId,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const batch = parseBatchState(loaded.raw, loaded.path ?? "");
|
|
294
|
+
const integratable = assertOrchIntegratable(projectRoot, {
|
|
295
|
+
baseBranch: batch?.baseBranch ?? String(loaded.raw.baseBranch ?? "main"),
|
|
296
|
+
orchBranch: batch?.orchBranch ?? loaded.raw.orchBranch ?? null,
|
|
297
|
+
mergeResultsEmpty: signals.mergeResultsEmpty,
|
|
298
|
+
orchMergedToBase: Boolean(signals.git?.orchMergedToBase),
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
if (!integratable.ok && !manualMergeOk) {
|
|
302
|
+
const output = buildDiagnosisOutput(
|
|
303
|
+
integratable.failureClass === "NeedsIntegrate" ? "needs_integrate" : (diagnosis ?? "paused"),
|
|
304
|
+
{
|
|
305
|
+
batchId,
|
|
306
|
+
phase: reconciliation.phase,
|
|
307
|
+
gitMerged: false,
|
|
308
|
+
},
|
|
309
|
+
);
|
|
310
|
+
return {
|
|
311
|
+
ok: false,
|
|
312
|
+
exitCode: 1,
|
|
313
|
+
error: integratable.error,
|
|
314
|
+
failureClass: integratable.failureClass,
|
|
315
|
+
...output,
|
|
316
|
+
headline: `${integratable.error} — complete refused`,
|
|
317
|
+
suggestedCommand: integratable.suggestedCommand ?? output.suggestedCommand,
|
|
318
|
+
alternatives: output.alternatives,
|
|
319
|
+
batchId,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const archivePath = archiveBatchState(projectRoot, batchId, loaded.raw);
|
|
324
|
+
const postMortemPath = writeBatchPostMortem({
|
|
325
|
+
projectRoot,
|
|
326
|
+
batchState: loaded.raw,
|
|
327
|
+
reconciliation,
|
|
328
|
+
});
|
|
329
|
+
const endedAt = Date.now();
|
|
330
|
+
appendBatchHistoryEntry(projectRoot, {
|
|
331
|
+
batchId,
|
|
332
|
+
action: "completed",
|
|
333
|
+
endedAt,
|
|
334
|
+
diagnosis: "completed",
|
|
335
|
+
detectManualMerge,
|
|
336
|
+
archivePath: path.relative(projectRoot, archivePath),
|
|
337
|
+
postMortemPath,
|
|
338
|
+
});
|
|
339
|
+
appendJournalEvent(projectRoot, batchId, "batch.completed", {
|
|
340
|
+
detectManualMerge,
|
|
341
|
+
archivePath: path.relative(projectRoot, archivePath),
|
|
342
|
+
lifecycle: "complete",
|
|
343
|
+
});
|
|
344
|
+
const configResult = loadSpineConfig(projectRoot);
|
|
345
|
+
recordBatchTerminalMetric({
|
|
346
|
+
projectRoot,
|
|
347
|
+
batchId,
|
|
348
|
+
batchState: { ...loaded.raw, endedAt },
|
|
349
|
+
diagnosis: "completed",
|
|
350
|
+
config: configResult.config ?? {},
|
|
351
|
+
});
|
|
352
|
+
clearActiveBatchState(loaded.path);
|
|
353
|
+
|
|
354
|
+
return {
|
|
355
|
+
ok: true,
|
|
356
|
+
exitCode: 0,
|
|
357
|
+
batchId,
|
|
358
|
+
diagnosis: "completed",
|
|
359
|
+
headline: `Batch ${batchId} completed and archived`,
|
|
360
|
+
suggestedCommand: "spine preflight",
|
|
361
|
+
alternatives: ["spine plan all"],
|
|
362
|
+
archivePath,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Append-only run-metrics.jsonl writer (handoff §6.5, SP-158).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { METRICS_DEFAULTS } from "../config/defaults.mjs";
|
|
8
|
+
import { readReviewLevel } from "./review.mjs";
|
|
9
|
+
|
|
10
|
+
const REDACT_KEY_PATTERN = /key|token|secret|password|prompt/i;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {object} config
|
|
14
|
+
*/
|
|
15
|
+
export function isMetricsEnabled(config = {}) {
|
|
16
|
+
return config?.metrics?.enabled !== false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} projectRoot
|
|
21
|
+
* @param {object} [config]
|
|
22
|
+
*/
|
|
23
|
+
export function metricsFilePath(projectRoot, config = {}) {
|
|
24
|
+
const relPath = config?.metrics?.path ?? METRICS_DEFAULTS.path;
|
|
25
|
+
return path.join(projectRoot, relPath);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {unknown} value
|
|
30
|
+
* @returns {unknown}
|
|
31
|
+
*/
|
|
32
|
+
function redactMetricValue(value) {
|
|
33
|
+
if (value == null || typeof value !== "object") return value;
|
|
34
|
+
if (Array.isArray(value)) {
|
|
35
|
+
return value.map((entry) => redactMetricValue(entry));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** @type {Record<string, unknown>} */
|
|
39
|
+
const out = {};
|
|
40
|
+
for (const [key, entry] of Object.entries(/** @type {Record<string, unknown>} */ (value))) {
|
|
41
|
+
if (REDACT_KEY_PATTERN.test(key)) {
|
|
42
|
+
out[key] = "[REDACTED]";
|
|
43
|
+
} else if (entry && typeof entry === "object") {
|
|
44
|
+
out[key] = redactMetricValue(entry);
|
|
45
|
+
} else {
|
|
46
|
+
out[key] = entry;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {object} record
|
|
54
|
+
*/
|
|
55
|
+
export function sanitizeMetricRecord(record) {
|
|
56
|
+
return /** @type {typeof record} */ (redactMetricValue(record));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @param {object} task
|
|
61
|
+
* @returns {"completed" | "failed" | "skipped"}
|
|
62
|
+
*/
|
|
63
|
+
export function resolveTaskMetricOutcome(task) {
|
|
64
|
+
if (task.exitReason === "skipped_done_on_disk" || task.status === "skipped") {
|
|
65
|
+
return "skipped";
|
|
66
|
+
}
|
|
67
|
+
if (task.status === "succeeded") {
|
|
68
|
+
return "completed";
|
|
69
|
+
}
|
|
70
|
+
return "failed";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @param {object} params
|
|
75
|
+
* @param {string} params.batchId
|
|
76
|
+
* @param {object} params.task
|
|
77
|
+
* @param {object} [params.config]
|
|
78
|
+
* @param {string} [params.taskFolder]
|
|
79
|
+
*/
|
|
80
|
+
export function buildTaskMetricRecord({ batchId, task, config = {}, taskFolder }) {
|
|
81
|
+
const worker = config?.agents?.worker ?? {};
|
|
82
|
+
const reviewLevel = taskFolder ? readReviewLevel(taskFolder) : Number(task.reviewLevel ?? 0);
|
|
83
|
+
|
|
84
|
+
/** @type {Record<string, unknown>} */
|
|
85
|
+
const record = {
|
|
86
|
+
recordType: "task",
|
|
87
|
+
schemaVersion: 1,
|
|
88
|
+
batchId,
|
|
89
|
+
taskId: task.taskId,
|
|
90
|
+
agentRole: "worker",
|
|
91
|
+
model: typeof worker.model === "string" ? worker.model : "inherit",
|
|
92
|
+
thinking:
|
|
93
|
+
worker.thinking === "off" ||
|
|
94
|
+
worker.thinking === "low" ||
|
|
95
|
+
worker.thinking === "medium" ||
|
|
96
|
+
worker.thinking === "high"
|
|
97
|
+
? worker.thinking
|
|
98
|
+
: "high",
|
|
99
|
+
startedAt: new Date(task.startedAt ?? Date.now()).toISOString(),
|
|
100
|
+
endedAt: new Date(task.endedAt ?? Date.now()).toISOString(),
|
|
101
|
+
outcome: resolveTaskMetricOutcome(task),
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
if (task.exitReason) record.exitReason = task.exitReason;
|
|
105
|
+
if (reviewLevel > 0) record.reviewLevel = reviewLevel;
|
|
106
|
+
if (task.finalVerdict != null) record.finalVerdict = task.finalVerdict;
|
|
107
|
+
if (task.finalAttempts != null) record.finalAttempts = task.finalAttempts;
|
|
108
|
+
if (task.contractOk != null) record.contractOk = task.contractOk;
|
|
109
|
+
if (task.stallKilled === true) record.stallKilled = true;
|
|
110
|
+
|
|
111
|
+
return sanitizeMetricRecord(record);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @param {string} projectRoot
|
|
116
|
+
* @param {object} record
|
|
117
|
+
* @param {object} [config]
|
|
118
|
+
*/
|
|
119
|
+
export function appendTaskMetric(projectRoot, record, config = {}) {
|
|
120
|
+
if (!isMetricsEnabled(config)) return null;
|
|
121
|
+
|
|
122
|
+
const filePath = metricsFilePath(projectRoot, config);
|
|
123
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
124
|
+
const line = `${JSON.stringify(sanitizeMetricRecord(record))}\n`;
|
|
125
|
+
fs.appendFileSync(filePath, line, "utf-8");
|
|
126
|
+
return record;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {object} params
|
|
131
|
+
* @param {string} params.projectRoot
|
|
132
|
+
* @param {string} params.batchId
|
|
133
|
+
* @param {object} params.task
|
|
134
|
+
* @param {object} [params.config]
|
|
135
|
+
* @param {string} [params.taskFolder]
|
|
136
|
+
*/
|
|
137
|
+
export function recordTaskTerminalMetric({ projectRoot, batchId, task, config = {}, taskFolder }) {
|
|
138
|
+
if (!isMetricsEnabled(config)) return null;
|
|
139
|
+
const record = buildTaskMetricRecord({ batchId, task, config, taskFolder });
|
|
140
|
+
return appendTaskMetric(projectRoot, record, config);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {object} params
|
|
145
|
+
* @param {string} params.batchId
|
|
146
|
+
* @param {object} params.batchState
|
|
147
|
+
* @param {string} params.diagnosis
|
|
148
|
+
*/
|
|
149
|
+
export function buildBatchMetricRecord({ batchId, batchState, diagnosis }) {
|
|
150
|
+
const tasks = Array.isArray(batchState?.tasks) ? batchState.tasks : [];
|
|
151
|
+
const startedAt = Number(batchState?.startedAt ?? Date.now());
|
|
152
|
+
const endedAt = Number(batchState?.endedAt ?? Date.now());
|
|
153
|
+
|
|
154
|
+
return sanitizeMetricRecord({
|
|
155
|
+
recordType: "batch",
|
|
156
|
+
schemaVersion: 1,
|
|
157
|
+
batchId,
|
|
158
|
+
endedAt: new Date(endedAt).toISOString(),
|
|
159
|
+
diagnosis: diagnosis ?? "unknown",
|
|
160
|
+
taskCount: tasks.length,
|
|
161
|
+
completedTasks: tasks.filter(
|
|
162
|
+
(task) => task.status === "succeeded" || task.exitReason === "skipped_done_on_disk",
|
|
163
|
+
).length,
|
|
164
|
+
failedTasks: tasks.filter((task) => task.status === "failed" || task.status === "aborted").length,
|
|
165
|
+
durationMs: Math.max(0, endedAt - startedAt),
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @param {string} projectRoot
|
|
171
|
+
* @param {object} record
|
|
172
|
+
* @param {object} [config]
|
|
173
|
+
*/
|
|
174
|
+
export function appendBatchMetric(projectRoot, record, config = {}) {
|
|
175
|
+
if (!isMetricsEnabled(config)) return null;
|
|
176
|
+
|
|
177
|
+
const filePath = metricsFilePath(projectRoot, config);
|
|
178
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
179
|
+
const line = `${JSON.stringify(sanitizeMetricRecord(record))}\n`;
|
|
180
|
+
fs.appendFileSync(filePath, line, "utf-8");
|
|
181
|
+
return record;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @param {object} params
|
|
186
|
+
* @param {string} params.projectRoot
|
|
187
|
+
* @param {string} params.batchId
|
|
188
|
+
* @param {object} params.batchState
|
|
189
|
+
* @param {string} params.diagnosis
|
|
190
|
+
* @param {object} [params.config]
|
|
191
|
+
*/
|
|
192
|
+
export function recordBatchTerminalMetric({
|
|
193
|
+
projectRoot,
|
|
194
|
+
batchId,
|
|
195
|
+
batchState,
|
|
196
|
+
diagnosis,
|
|
197
|
+
config = {},
|
|
198
|
+
}) {
|
|
199
|
+
if (!isMetricsEnabled(config)) return null;
|
|
200
|
+
const record = buildBatchMetricRecord({ batchId, batchState, diagnosis });
|
|
201
|
+
return appendBatchMetric(projectRoot, record, config);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* @param {string} filePath
|
|
206
|
+
* @param {object} [options]
|
|
207
|
+
*/
|
|
208
|
+
export function readMetricsLines(filePath, options = {}) {
|
|
209
|
+
if (!fs.existsSync(filePath)) return [];
|
|
210
|
+
const raw = fs.readFileSync(filePath, "utf-8").trim();
|
|
211
|
+
if (!raw) return [];
|
|
212
|
+
|
|
213
|
+
/** @type {object[]} */
|
|
214
|
+
const lines = [];
|
|
215
|
+
for (const line of raw.split("\n")) {
|
|
216
|
+
if (!line.trim()) continue;
|
|
217
|
+
try {
|
|
218
|
+
lines.push(JSON.parse(line));
|
|
219
|
+
} catch {
|
|
220
|
+
if (!options.skipInvalid) throw new Error(`Invalid metrics JSONL line in ${filePath}`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return lines;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @param {object[]} lines
|
|
228
|
+
* @param {object} [filters]
|
|
229
|
+
*/
|
|
230
|
+
export function filterMetricsLines(lines, filters = {}) {
|
|
231
|
+
let filtered = [...lines];
|
|
232
|
+
if (filters.batchId) {
|
|
233
|
+
filtered = filtered.filter((line) => line.batchId === filters.batchId);
|
|
234
|
+
}
|
|
235
|
+
if (filters.last != null && Number.isFinite(filters.last) && filters.last > 0) {
|
|
236
|
+
filtered = filtered.slice(-filters.last);
|
|
237
|
+
}
|
|
238
|
+
return filtered;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @param {object[]} lines
|
|
243
|
+
*/
|
|
244
|
+
export function formatMetricsTable(lines) {
|
|
245
|
+
if (lines.length === 0) return "No metrics records found.\n";
|
|
246
|
+
|
|
247
|
+
const header = ["Type", "Batch", "Task", "Outcome", "Diagnosis", "Ended"];
|
|
248
|
+
const rows = lines.map((line) => [
|
|
249
|
+
line.recordType ?? "—",
|
|
250
|
+
line.batchId ?? "—",
|
|
251
|
+
line.taskId ?? "—",
|
|
252
|
+
line.outcome ?? line.diagnosis ?? "—",
|
|
253
|
+
line.diagnosis ?? "—",
|
|
254
|
+
line.endedAt ?? "—",
|
|
255
|
+
]);
|
|
256
|
+
|
|
257
|
+
const widths = header.map((col, index) =>
|
|
258
|
+
Math.max(col.length, ...rows.map((row) => String(row[index] ?? "").length)),
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
const formatRow = (row) =>
|
|
262
|
+
row.map((cell, index) => String(cell ?? "").padEnd(widths[index])).join(" ");
|
|
263
|
+
|
|
264
|
+
return `${[formatRow(header), ...rows.map(formatRow)].join("\n")}\n`;
|
|
265
|
+
}
|