pi-spine 1.0.2 → 1.2.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/README.md +61 -489
- package/bin/get-version.mjs +1 -13
- package/bin/spine-batch.mjs +162 -51
- package/bin/spine-cli/batch.mjs +3 -3
- package/bin/spine-cli/lane-logs.mjs +24 -0
- package/bin/spine-cli/scenarios.mjs +298 -0
- package/bin/spine-cli/wait.mjs +30 -0
- package/bin/spine-cli/watch.mjs +27 -0
- package/bin/spine-config.mjs +1 -212
- package/bin/spine-doctor.mjs +2 -418
- package/bin/spine-init.mjs +1 -398
- package/bin/spine-issue.mjs +196 -0
- package/bin/spine-journal.mjs +1 -0
- package/bin/spine-preflight.mjs +1 -700
- package/bin/spine-review-step.mjs +6 -4
- package/bin/spine-run.mjs +30 -0
- package/bin/spine-status.mjs +19 -1
- package/bin/spine-tasks.mjs +112 -13
- package/bin/spine-worker-runner.mjs +24 -14
- package/bin/spine.mjs +52 -2
- package/extensions/spine/worker-tools.ts +14 -12
- package/package.json +5 -2
- package/skills/create-spine-tasks/SKILL.md +135 -6
- package/skills/create-spine-tasks/references/clarify-template.md +73 -0
- package/skills/create-spine-tasks/references/contract-template.md +19 -0
- package/skills/create-spine-tasks/references/prompt-template.md +2 -0
- package/skills/create-spine-tasks/references/requirements-checklist-template.md +94 -0
- package/src/batch/abort.mjs +3 -12
- package/src/batch/agent-session-worker.mjs +45 -5
- package/src/batch/attached-engine-handoff.mjs +188 -0
- package/src/batch/attached-runner.mjs +312 -0
- package/src/batch/contract-prelanded.mjs +180 -0
- package/src/batch/contract-verify.mjs +140 -2
- package/src/batch/detached-start.mjs +105 -7
- package/src/batch/diagnosis-alternatives.mjs +46 -0
- package/src/batch/diagnosis-merge-failure.mjs +79 -0
- package/src/batch/diagnosis-retry-limbo.mjs +34 -0
- package/src/batch/diagnosis-stub.mjs +109 -0
- package/src/batch/diagnosis-task-done.mjs +110 -0
- package/src/batch/diagnosis-worker-done-missing.mjs +96 -0
- package/src/batch/diagnosis.mjs +174 -62
- package/src/batch/engine-lanes/merge.mjs +168 -77
- package/src/batch/engine-lanes/queue.mjs +4 -1
- package/src/batch/engine-lanes/review.mjs +32 -74
- package/src/batch/engine-lanes/watch.mjs +29 -0
- package/src/batch/engine-lanes.mjs +1 -0
- package/src/batch/engine-scope.mjs +42 -5
- package/src/batch/engine.mjs +115 -68
- package/src/batch/evidence.mjs +81 -17
- package/src/batch/gate.mjs +46 -17
- package/src/batch/git-helpers.mjs +96 -0
- package/src/batch/heartbeat.mjs +73 -7
- package/src/batch/integrate.mjs +3 -5
- package/src/batch/journal.mjs +25 -0
- package/src/batch/lane-commit.mjs +90 -5
- package/src/batch/lifecycle.mjs +131 -4
- package/src/batch/macro-phase.mjs +226 -0
- package/src/batch/merge/adoption-doc-merge.mjs +243 -0
- package/src/batch/merge/wave-merge-state.mjs +108 -0
- package/src/batch/metrics.mjs +74 -5
- package/src/batch/orphan-detect.mjs +74 -0
- package/src/batch/pause.mjs +223 -0
- package/src/batch/post-merge-limbo.mjs +345 -17
- package/src/batch/reconcile.mjs +473 -50
- package/src/batch/resume-common.mjs +6 -2
- package/src/batch/resume-engine.mjs +58 -1
- package/src/batch/resume-lane-reviews.mjs +16 -0
- package/src/batch/resume-multi-lanes.mjs +56 -1
- package/src/batch/resume-multi-validate.mjs +148 -8
- package/src/batch/resume-multi.mjs +45 -44
- package/src/batch/resume.mjs +64 -47
- package/src/batch/retry.mjs +56 -5
- package/src/batch/review-shared.mjs +196 -0
- package/src/batch/review-spawn.mjs +326 -0
- package/src/batch/review.mjs +178 -212
- package/src/batch/rules-manifest-drift.mjs +157 -0
- package/src/batch/salvage.mjs +2 -1
- package/src/batch/sequence.mjs +454 -0
- package/src/batch/state.mjs +18 -3
- package/src/batch/status-json.mjs +67 -0
- package/src/batch/task-stall-budget.mjs +94 -10
- package/src/batch/worker-host.mjs +185 -37
- package/src/batch/worker-output.mjs +196 -3
- package/src/batch/worktree.mjs +10 -0
- package/src/cli/handoff.mjs +1 -1
- package/src/cli/issue-draft.mjs +180 -0
- package/src/cli/journal-follow.mjs +239 -0
- package/src/cli/lane-logs.mjs +291 -0
- package/src/cli/rules.mjs +1 -1
- package/src/cli/sequence.mjs +108 -0
- package/src/cli/settings-set.mjs +3 -22
- package/src/cli/settings-slash.mjs +27 -5
- package/src/cli/wait.mjs +187 -0
- package/src/cli/watch.mjs +160 -0
- package/src/config/agent-model-resolve.mjs +48 -0
- package/src/config/cursor-rules/discover.mjs +3 -21
- package/src/config/defaults.mjs +7 -1
- package/src/config/settings-fields.mjs +48 -0
- package/src/config/spine-config-load.mjs +221 -0
- package/src/config/spine-init-constants.mjs +424 -0
- package/src/config/spine-preflight-lib.mjs +1180 -0
- package/src/dashboard/lane-throughput.mjs +351 -0
- package/src/dashboard/public/dashboard.css +25 -1
- package/src/dashboard/public/dashboard.js +224 -40
- package/src/dashboard/public/index.html +15 -9
- package/src/dashboard/server.mjs +4 -0
- package/src/dashboard/snapshot.mjs +387 -6
- package/src/dashboard/view.mjs +170 -15
- package/src/doctor/agent-model-inherit.mjs +61 -6
- package/src/doctor/run-doctor-checks.mjs +483 -0
- package/src/doctor/sequence-safety.mjs +57 -0
- package/src/doctor/stale-path.mjs +22 -0
- package/src/doctor/stale-worktrees.mjs +102 -0
- package/src/fixtures/scenario-registry.mjs +194 -0
- package/src/fs/atomic-write.mjs +49 -0
- package/src/migrate/taskplane-config.mjs +1 -1
- package/src/planner/file-scope.mjs +246 -0
- package/src/planner/format-plan.mjs +9 -7
- package/src/planner/index.mjs +6 -2
- package/src/planner/pending.mjs +92 -1
- package/src/planner/wave-scope.mjs +136 -0
- package/src/planner/waves.mjs +133 -0
- package/src/tasks/analyze/index.mjs +453 -0
- package/src/tasks/packet/index.mjs +1 -0
- package/src/tasks/packet/parse-prompt.mjs +42 -1
- package/src/tasks/packet/validate-prompt.mjs +124 -0
- package/src/util/command-exists.mjs +19 -0
- package/templates/docs/constitution.md +61 -0
- package/templates/spine-config.json +9 -2
package/src/batch/worktree.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @ts-check
|
|
1
2
|
/**
|
|
2
3
|
* Git worktree provisioning for pi-spine lanes (PRD §9.4).
|
|
3
4
|
*/
|
|
@@ -177,6 +178,10 @@ export function ensureOrchBranch(projectRoot, baseBranch, orchBranch) {
|
|
|
177
178
|
|
|
178
179
|
/**
|
|
179
180
|
* @param {object} params
|
|
181
|
+
* @param {string} params.projectRoot
|
|
182
|
+
* @param {string} params.batchId
|
|
183
|
+
* @param {number} [params.laneNumber]
|
|
184
|
+
* @param {string} params.orchBranch
|
|
180
185
|
*/
|
|
181
186
|
export function provisionLaneWorktree({
|
|
182
187
|
projectRoot,
|
|
@@ -227,6 +232,11 @@ export function removeLaneWorktrees(projectRoot, batchId, maxLaneNumber) {
|
|
|
227
232
|
|
|
228
233
|
/**
|
|
229
234
|
* @param {object} params
|
|
235
|
+
* @param {string} params.projectRoot
|
|
236
|
+
* @param {string} params.worktreePath
|
|
237
|
+
* @param {string} params.batchId
|
|
238
|
+
* @param {number} [params.laneNumber]
|
|
239
|
+
* @param {object} [params.config]
|
|
230
240
|
*/
|
|
231
241
|
export function runWorktreeSetupHook({
|
|
232
242
|
projectRoot,
|
package/src/cli/handoff.mjs
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import fs from "node:fs";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
|
|
8
|
-
import { loadSpineConfig } from "
|
|
8
|
+
import { loadSpineConfig } from "../config/spine-config-load.mjs";
|
|
9
9
|
import { HANDOFF_DEFAULTS } from "../config/defaults.mjs";
|
|
10
10
|
import {
|
|
11
11
|
classifyTasks,
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub issue draft body assembly (issue #60 Tier 1b).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
|
|
8
|
+
import { reconcileBatch } from "../batch/reconcile.mjs";
|
|
9
|
+
import { runDoctorChecks } from "../doctor/run-doctor-checks.mjs";
|
|
10
|
+
import { PACKAGE_ROOT } from "../config/spine-init-constants.mjs";
|
|
11
|
+
import {
|
|
12
|
+
assembleHandoffData,
|
|
13
|
+
redactHandoffSecrets,
|
|
14
|
+
redactHandoffText,
|
|
15
|
+
} from "./handoff.mjs";
|
|
16
|
+
|
|
17
|
+
const VALID_ISSUE_TYPES = new Set(["bug", "enhancement", "question"]);
|
|
18
|
+
|
|
19
|
+
const ISSUE_TYPE_LABELS = {
|
|
20
|
+
bug: "bug",
|
|
21
|
+
enhancement: "enhancement",
|
|
22
|
+
question: "question",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {ReturnType<typeof runDoctorChecks>} doctorResult
|
|
27
|
+
*/
|
|
28
|
+
function formatDoctorSummary(doctorResult) {
|
|
29
|
+
return doctorResult.checks
|
|
30
|
+
.map((check) => {
|
|
31
|
+
const status = check.warning ? "WARN" : check.ok ? "OK" : "FAIL";
|
|
32
|
+
const detail = check.detail ? ` (${check.detail})` : "";
|
|
33
|
+
return `- ${status} ${check.label}${detail}`;
|
|
34
|
+
})
|
|
35
|
+
.join("\n");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {ReturnType<typeof reconcileBatch>} reconciliation
|
|
40
|
+
*/
|
|
41
|
+
function formatDiagnosisBlock(reconciliation) {
|
|
42
|
+
const lines = [
|
|
43
|
+
`- **Diagnosis:** ${reconciliation.diagnosis ?? "idle"}`,
|
|
44
|
+
`- **Headline:** ${reconciliation.headline ?? "—"}`,
|
|
45
|
+
`- **Suggested command:** ${reconciliation.suggestedCommand ?? "—"}`,
|
|
46
|
+
];
|
|
47
|
+
if (reconciliation.batchId) {
|
|
48
|
+
lines.push(`- **Batch ID:** ${reconciliation.batchId}`);
|
|
49
|
+
}
|
|
50
|
+
if (reconciliation.phase) {
|
|
51
|
+
lines.push(`- **Phase:** ${reconciliation.phase}`);
|
|
52
|
+
}
|
|
53
|
+
return lines.join("\n");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {ReturnType<typeof assembleHandoffData>} handoff
|
|
58
|
+
*/
|
|
59
|
+
function formatJournalExcerpt(handoff) {
|
|
60
|
+
if (!handoff.journalTail?.length) {
|
|
61
|
+
return "- (none)";
|
|
62
|
+
}
|
|
63
|
+
return handoff.journalTail
|
|
64
|
+
.map((entry) => {
|
|
65
|
+
const suffix = entry.taskId
|
|
66
|
+
? ` ${entry.taskId}`
|
|
67
|
+
: entry.laneId
|
|
68
|
+
? ` ${entry.laneId}`
|
|
69
|
+
: "";
|
|
70
|
+
return `- ${entry.timestamp} ${entry.type}${suffix}`;
|
|
71
|
+
})
|
|
72
|
+
.join("\n");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param {ReturnType<typeof reconcileBatch>} reconciliation
|
|
77
|
+
*/
|
|
78
|
+
function formatCommandsRun(reconciliation) {
|
|
79
|
+
const lines = [`- ${reconciliation.suggestedCommand ?? "spine preflight"}`];
|
|
80
|
+
for (const alt of reconciliation.alternatives ?? []) {
|
|
81
|
+
if (alt && !lines.includes(`- ${alt}`)) {
|
|
82
|
+
lines.push(`- ${alt}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return lines.join("\n");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function readPackageVersion() {
|
|
89
|
+
try {
|
|
90
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(PACKAGE_ROOT, "package.json"), "utf-8"));
|
|
91
|
+
return pkg.version || "unknown";
|
|
92
|
+
} catch {
|
|
93
|
+
return "unknown";
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Render issue draft markdown sections for GitHub filing.
|
|
99
|
+
*
|
|
100
|
+
* @param {object} sections
|
|
101
|
+
* @param {string} sections.summary
|
|
102
|
+
* @param {string} sections.environment
|
|
103
|
+
* @param {string} sections.commandsRun
|
|
104
|
+
* @param {string} sections.diagnosis
|
|
105
|
+
* @param {string} sections.journalExcerpt
|
|
106
|
+
* @param {string} [sections.expected]
|
|
107
|
+
* @param {string} [sections.actual]
|
|
108
|
+
*/
|
|
109
|
+
export function formatIssueDraftMarkdown(sections) {
|
|
110
|
+
const lines = [
|
|
111
|
+
"## Summary",
|
|
112
|
+
sections.summary,
|
|
113
|
+
"",
|
|
114
|
+
"## Environment",
|
|
115
|
+
sections.environment,
|
|
116
|
+
"",
|
|
117
|
+
"## Commands run",
|
|
118
|
+
sections.commandsRun,
|
|
119
|
+
"",
|
|
120
|
+
"## Diagnosis",
|
|
121
|
+
sections.diagnosis,
|
|
122
|
+
"",
|
|
123
|
+
"## Journal excerpt",
|
|
124
|
+
sections.journalExcerpt,
|
|
125
|
+
"",
|
|
126
|
+
"## Expected",
|
|
127
|
+
sections.expected ?? "(describe expected behavior)",
|
|
128
|
+
"",
|
|
129
|
+
"## Actual",
|
|
130
|
+
sections.actual ?? "(describe actual behavior)",
|
|
131
|
+
"",
|
|
132
|
+
];
|
|
133
|
+
return redactHandoffText(lines.join("\n"));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Build a GitHub issue draft from live project state.
|
|
138
|
+
*
|
|
139
|
+
* @param {object} options
|
|
140
|
+
* @param {string} options.projectRoot
|
|
141
|
+
* @param {"bug"|"enhancement"|"question"} [options.issueType]
|
|
142
|
+
* @param {string} [options.title]
|
|
143
|
+
* @param {string} [options.batchId]
|
|
144
|
+
*/
|
|
145
|
+
export function buildIssueDraftBody(options) {
|
|
146
|
+
const { projectRoot, issueType = "bug", title, batchId } = options;
|
|
147
|
+
if (!VALID_ISSUE_TYPES.has(issueType)) {
|
|
148
|
+
throw new Error(`Invalid issueType: ${issueType} (expected bug, enhancement, or question)`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const reconciliation = reconcileBatch({ projectRoot, verbose: true });
|
|
152
|
+
const doctorResult = runDoctorChecks(projectRoot);
|
|
153
|
+
const handoff = assembleHandoffData(projectRoot, batchId ?? reconciliation.batchId ?? undefined);
|
|
154
|
+
const version = readPackageVersion();
|
|
155
|
+
|
|
156
|
+
const sections = redactHandoffSecrets({
|
|
157
|
+
summary: reconciliation.headline ?? "pi-spine operator issue draft",
|
|
158
|
+
environment: [
|
|
159
|
+
`- pi-spine version: ${version}`,
|
|
160
|
+
`- Node: v${process.versions.node}`,
|
|
161
|
+
"",
|
|
162
|
+
formatDoctorSummary(doctorResult),
|
|
163
|
+
].join("\n"),
|
|
164
|
+
commandsRun: formatCommandsRun(reconciliation),
|
|
165
|
+
diagnosis: formatDiagnosisBlock(reconciliation),
|
|
166
|
+
journalExcerpt: formatJournalExcerpt(handoff),
|
|
167
|
+
expected: "(describe expected behavior)",
|
|
168
|
+
actual: "(describe actual behavior)",
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const body = formatIssueDraftMarkdown(sections);
|
|
172
|
+
const resolvedTitle = redactHandoffText(title ?? reconciliation.headline ?? "pi-spine issue");
|
|
173
|
+
const labels = [ISSUE_TYPE_LABELS[issueType]];
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
title: resolvedTitle,
|
|
177
|
+
body,
|
|
178
|
+
labels,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live tail of batch journal events (issue #45, SP-361).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import {
|
|
7
|
+
journalPath,
|
|
8
|
+
normalizeJournalEvent,
|
|
9
|
+
summarizeJournalEvent,
|
|
10
|
+
} from "../batch/journal.mjs";
|
|
11
|
+
import { loadBatchStateFile, reconcileBatch } from "../batch/reconcile.mjs";
|
|
12
|
+
|
|
13
|
+
export const JOURNAL_FOLLOW_HEADER =
|
|
14
|
+
" time | type | lane | task | summary\n" +
|
|
15
|
+
" --------------------+-----------------------+--------+----------+------------------";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {string} timestamp
|
|
19
|
+
*/
|
|
20
|
+
export function formatReplayTime(timestamp) {
|
|
21
|
+
const date = new Date(timestamp);
|
|
22
|
+
if (Number.isNaN(date.getTime())) return String(timestamp);
|
|
23
|
+
return date.toISOString().replace("T", " ").slice(0, 19);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {object} event
|
|
28
|
+
*/
|
|
29
|
+
export function formatJournalFollowLine(event) {
|
|
30
|
+
const time = formatReplayTime(event.timestamp).padEnd(19);
|
|
31
|
+
const type = String(event.type ?? "").padEnd(21);
|
|
32
|
+
const lane = String(event.laneId ?? "—").padEnd(6);
|
|
33
|
+
const task = String(event.taskId ?? "—").padEnd(8);
|
|
34
|
+
const summary = summarizeJournalEvent(event);
|
|
35
|
+
return ` ${time} | ${type} | ${lane} | ${task} | ${summary}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {string} projectRoot
|
|
40
|
+
* @param {string|null|undefined} batchId
|
|
41
|
+
*/
|
|
42
|
+
export function resolveFollowBatchId(projectRoot, batchId) {
|
|
43
|
+
if (batchId) return batchId;
|
|
44
|
+
|
|
45
|
+
const reconciliation = reconcileBatch({ projectRoot });
|
|
46
|
+
if (reconciliation.batchId) return reconciliation.batchId;
|
|
47
|
+
|
|
48
|
+
const loaded = loadBatchStateFile(projectRoot, null);
|
|
49
|
+
if (!loaded.raw) return null;
|
|
50
|
+
const resolved = loaded.raw.batchId ?? loaded.raw.id;
|
|
51
|
+
return resolved != null && String(resolved) ? String(resolved) : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @param {string[]} args
|
|
56
|
+
*/
|
|
57
|
+
export function parseJournalFollowArgs(args) {
|
|
58
|
+
const batchIdx = args.indexOf("--batch");
|
|
59
|
+
const batchId = batchIdx >= 0 ? args[batchIdx + 1] ?? null : null;
|
|
60
|
+
const laneIdx = args.indexOf("--lane");
|
|
61
|
+
const laneId = laneIdx >= 0 ? args[laneIdx + 1] ?? null : null;
|
|
62
|
+
const json = args.includes("--json");
|
|
63
|
+
return { batchId, laneId, json };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {object} event
|
|
68
|
+
* @param {string|null|undefined} laneId
|
|
69
|
+
*/
|
|
70
|
+
export function matchesLaneFilter(event, laneId) {
|
|
71
|
+
if (!laneId) return true;
|
|
72
|
+
return event.laneId === laneId;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param {string} rawLine
|
|
77
|
+
* @param {{ laneId?: string|null, json?: boolean }} options
|
|
78
|
+
* @returns {string|null}
|
|
79
|
+
*/
|
|
80
|
+
export function formatJournalFollowOutputLine(rawLine, options = {}) {
|
|
81
|
+
const { laneId = null, json = false } = options;
|
|
82
|
+
const trimmed = rawLine.trim();
|
|
83
|
+
if (!trimmed) return null;
|
|
84
|
+
|
|
85
|
+
if (json) {
|
|
86
|
+
if (!laneId) return `${trimmed}\n`;
|
|
87
|
+
try {
|
|
88
|
+
const event = normalizeJournalEvent(JSON.parse(trimmed));
|
|
89
|
+
if (!matchesLaneFilter(event, laneId)) return null;
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return `${trimmed}\n`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
const event = normalizeJournalEvent(JSON.parse(trimmed));
|
|
98
|
+
if (!matchesLaneFilter(event, laneId)) return null;
|
|
99
|
+
return `${formatJournalFollowLine(event)}\n`;
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {string} chunk
|
|
107
|
+
* @param {string} partial
|
|
108
|
+
* @param {{ laneId?: string|null, json?: boolean, onLine: (line: string) => void }} options
|
|
109
|
+
* @returns {string}
|
|
110
|
+
*/
|
|
111
|
+
export function drainJournalFollowChunk(chunk, partial, options) {
|
|
112
|
+
let buffer = partial + chunk;
|
|
113
|
+
const parts = buffer.split("\n");
|
|
114
|
+
const remainder = parts.pop() ?? "";
|
|
115
|
+
|
|
116
|
+
for (const line of parts) {
|
|
117
|
+
const formatted = formatJournalFollowOutputLine(line, options);
|
|
118
|
+
if (formatted) options.onLine(formatted);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return remainder;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @param {string} filePath
|
|
126
|
+
* @param {{ laneId?: string|null, json?: boolean, onLine: (line: string) => void, headerPrinted?: { value: boolean } }} options
|
|
127
|
+
* @param {typeof fs} fileSystem
|
|
128
|
+
* @returns {{ offset: number, partial: string }}
|
|
129
|
+
*/
|
|
130
|
+
export function readJournalFollowSnapshot(filePath, options, fileSystem = fs) {
|
|
131
|
+
let offset = 0;
|
|
132
|
+
let partial = "";
|
|
133
|
+
const content = fileSystem.readFileSync(filePath, "utf-8");
|
|
134
|
+
const stat = fileSystem.statSync(filePath);
|
|
135
|
+
offset = stat.size;
|
|
136
|
+
|
|
137
|
+
if (!options.json && content.trim() && options.headerPrinted && !options.headerPrinted.value) {
|
|
138
|
+
options.onLine(`${JOURNAL_FOLLOW_HEADER}\n`);
|
|
139
|
+
options.headerPrinted.value = true;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
partial = drainJournalFollowChunk(content, partial, options);
|
|
143
|
+
return { offset, partial };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const defaultDeps = {
|
|
147
|
+
fs,
|
|
148
|
+
stdout: process.stdout,
|
|
149
|
+
onSignal: (signal, handler) => process.on(signal, handler),
|
|
150
|
+
offSignal: (signal, handler) => process.off(signal, handler),
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @param {object} options
|
|
155
|
+
* @param {string} options.projectRoot
|
|
156
|
+
* @param {string[]} [options.args]
|
|
157
|
+
* @param {boolean} [options.follow]
|
|
158
|
+
* @param {typeof defaultDeps} [options.deps]
|
|
159
|
+
*/
|
|
160
|
+
export async function runJournalFollow(options) {
|
|
161
|
+
const { projectRoot, args = [], follow = true, deps = defaultDeps } = options;
|
|
162
|
+
const { batchId: argBatchId, laneId, json } = parseJournalFollowArgs(args);
|
|
163
|
+
const batchId = resolveFollowBatchId(projectRoot, argBatchId);
|
|
164
|
+
|
|
165
|
+
if (!batchId) {
|
|
166
|
+
return {
|
|
167
|
+
exitCode: 1,
|
|
168
|
+
output:
|
|
169
|
+
"No active batch for journal follow (pass --batch {id} or start a batch)\n",
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const filePath = journalPath(projectRoot, batchId);
|
|
174
|
+
if (!deps.fs.existsSync(filePath)) {
|
|
175
|
+
return {
|
|
176
|
+
exitCode: 1,
|
|
177
|
+
output: `Journal not found for batch ${batchId}: ${filePath}\n`,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** @type {string[]} */
|
|
182
|
+
const lines = [];
|
|
183
|
+
const headerPrinted = { value: false };
|
|
184
|
+
const formatOptions = {
|
|
185
|
+
laneId,
|
|
186
|
+
json,
|
|
187
|
+
headerPrinted,
|
|
188
|
+
onLine: (line) => {
|
|
189
|
+
lines.push(line);
|
|
190
|
+
deps.stdout.write(line);
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
let { offset, partial } = readJournalFollowSnapshot(filePath, formatOptions, deps.fs);
|
|
195
|
+
|
|
196
|
+
if (!follow) {
|
|
197
|
+
return { exitCode: 0, output: lines.join("") };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return new Promise((resolve) => {
|
|
201
|
+
/** @type {import("node:fs").FSWatcher|null} */
|
|
202
|
+
let watcher = null;
|
|
203
|
+
|
|
204
|
+
const cleanup = (exitCode = 0) => {
|
|
205
|
+
if (watcher) watcher.close();
|
|
206
|
+
resolve({ exitCode, output: lines.join("") });
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const onChange = () => {
|
|
210
|
+
try {
|
|
211
|
+
const stat = deps.fs.statSync(filePath);
|
|
212
|
+
if (stat.size < offset) {
|
|
213
|
+
offset = 0;
|
|
214
|
+
partial = "";
|
|
215
|
+
}
|
|
216
|
+
if (stat.size <= offset) return;
|
|
217
|
+
|
|
218
|
+
const toRead = stat.size - offset;
|
|
219
|
+
const fd = deps.fs.openSync(filePath, "r");
|
|
220
|
+
try {
|
|
221
|
+
const buffer = Buffer.alloc(toRead);
|
|
222
|
+
deps.fs.readSync(fd, buffer, 0, toRead, offset);
|
|
223
|
+
offset = stat.size;
|
|
224
|
+
partial = drainJournalFollowChunk(buffer.toString("utf-8"), partial, formatOptions);
|
|
225
|
+
} finally {
|
|
226
|
+
deps.fs.closeSync(fd);
|
|
227
|
+
}
|
|
228
|
+
} catch {
|
|
229
|
+
// Transient read races while the engine appends are expected during follow.
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
watcher = deps.fs.watch(filePath, onChange);
|
|
234
|
+
const onSigInt = () => cleanup(0);
|
|
235
|
+
const onSigTerm = () => cleanup(0);
|
|
236
|
+
deps.onSignal("SIGINT", onSigInt);
|
|
237
|
+
deps.onSignal("SIGTERM", onSigTerm);
|
|
238
|
+
});
|
|
239
|
+
}
|