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,430 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lane heartbeat and progress-aware stall helpers (FR-WORK-09, PRD §18.4, FR-STALL-02).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { execFileSync } from "node:child_process";
|
|
8
|
+
import { appendJournalEvent, readJournalEvents } from "./journal.mjs";
|
|
9
|
+
|
|
10
|
+
const DEFAULT_STALL_TIMEOUT_MIN = 60;
|
|
11
|
+
const DEFAULT_GRACE_AFTER_PROGRESS_MIN = 15;
|
|
12
|
+
const DEFAULT_HEARTBEAT_INTERVAL_MIN = 10;
|
|
13
|
+
const DEFAULT_CHECKPOINT_WARNING_MIN = 10;
|
|
14
|
+
const DEFAULT_POST_DONE_GRACE_MIN = 4;
|
|
15
|
+
const POLL_INTERVAL_MS = 30_000;
|
|
16
|
+
|
|
17
|
+
const CHECKPOINT_WARNING_SUGGESTION =
|
|
18
|
+
"Commit step work and call spine_report_progress so stall grace tracks checkpoints, not file edits alone.";
|
|
19
|
+
|
|
20
|
+
/** @typedef {"worker_alive" | "checkpoint" | "file_scope_activity"} HeartbeatKind */
|
|
21
|
+
/** @typedef {"launching" | "pi" | "verify" | "unknown"} WorkerPhase */
|
|
22
|
+
|
|
23
|
+
/** @type {readonly HeartbeatKind[]} */
|
|
24
|
+
export const HEARTBEAT_KINDS = Object.freeze([
|
|
25
|
+
"worker_alive",
|
|
26
|
+
"checkpoint",
|
|
27
|
+
"file_scope_activity",
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
/** @type {readonly WorkerPhase[]} */
|
|
31
|
+
export const WORKER_PHASES = Object.freeze(["launching", "pi", "verify", "unknown"]);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {object} [config]
|
|
35
|
+
*/
|
|
36
|
+
export function resolveStallConfig(config = {}) {
|
|
37
|
+
const lanes = config.lanes ?? {};
|
|
38
|
+
const stallTimeoutMinutes = Number(lanes.stallTimeoutMinutes) || DEFAULT_STALL_TIMEOUT_MIN;
|
|
39
|
+
const stallGraceAfterProgressMinutes =
|
|
40
|
+
Number(lanes.stallGraceAfterProgressMinutes) || DEFAULT_GRACE_AFTER_PROGRESS_MIN;
|
|
41
|
+
const heartbeatIntervalMinutes =
|
|
42
|
+
Number(lanes.heartbeatIntervalMinutes) || DEFAULT_HEARTBEAT_INTERVAL_MIN;
|
|
43
|
+
const checkpointWarningMinutes =
|
|
44
|
+
Number(lanes.checkpointWarningMinutes) || DEFAULT_CHECKPOINT_WARNING_MIN;
|
|
45
|
+
const postDoneGraceMinutes =
|
|
46
|
+
Number(lanes.postDoneGraceMinutes) || DEFAULT_POST_DONE_GRACE_MIN;
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
stallTimeoutMs: stallTimeoutMinutes * 60 * 1000,
|
|
50
|
+
graceAfterProgressMs: stallGraceAfterProgressMinutes * 60 * 1000,
|
|
51
|
+
heartbeatIntervalMs: heartbeatIntervalMinutes * 60 * 1000,
|
|
52
|
+
checkpointWarningMs: checkpointWarningMinutes * 60 * 1000,
|
|
53
|
+
postDoneGraceMs: postDoneGraceMinutes * 60 * 1000,
|
|
54
|
+
extendGraceOnFileScope: lanes.extendGraceOnFileScope === true,
|
|
55
|
+
pollIntervalMs: POLL_INTERVAL_MS,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @param {string} worktreePath
|
|
61
|
+
* @param {string[]} args
|
|
62
|
+
*/
|
|
63
|
+
function git(worktreePath, args) {
|
|
64
|
+
try {
|
|
65
|
+
return execFileSync("git", args, {
|
|
66
|
+
cwd: worktreePath,
|
|
67
|
+
encoding: "utf-8",
|
|
68
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
69
|
+
}).trim();
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Max mtime among existing file-scope paths (FR-WORK-10 activity signal; warning only by default).
|
|
77
|
+
* @param {string} worktreePath
|
|
78
|
+
* @param {string[]} [fileScopePaths]
|
|
79
|
+
*/
|
|
80
|
+
function resolveFileScopeMtimeMs(worktreePath, fileScopePaths) {
|
|
81
|
+
if (!Array.isArray(fileScopePaths) || fileScopePaths.length === 0) return null;
|
|
82
|
+
let max = null;
|
|
83
|
+
for (const rel of fileScopePaths) {
|
|
84
|
+
if (!rel || typeof rel !== "string") continue;
|
|
85
|
+
const target = path.join(worktreePath, rel);
|
|
86
|
+
if (!fs.existsSync(target)) continue;
|
|
87
|
+
const stat = fs.statSync(target);
|
|
88
|
+
if (!stat.isFile()) continue;
|
|
89
|
+
max = max === null ? stat.mtimeMs : Math.max(max, stat.mtimeMs);
|
|
90
|
+
}
|
|
91
|
+
return max;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Scoped git porcelain paths under File Scope or the task folder (FR-STALL-02 activity).
|
|
96
|
+
* @param {string} worktreePath
|
|
97
|
+
* @param {string[]} [fileScopePaths]
|
|
98
|
+
* @param {string} [taskFolder]
|
|
99
|
+
*/
|
|
100
|
+
export function resolveScopedDirtyPaths(worktreePath, fileScopePaths, taskFolder) {
|
|
101
|
+
const porcelain = git(worktreePath, ["status", "--porcelain"]);
|
|
102
|
+
if (!porcelain) return [];
|
|
103
|
+
|
|
104
|
+
const scopePrefixes = [];
|
|
105
|
+
if (Array.isArray(fileScopePaths)) {
|
|
106
|
+
for (const rel of fileScopePaths) {
|
|
107
|
+
if (!rel || typeof rel !== "string") continue;
|
|
108
|
+
scopePrefixes.push(rel.endsWith("/") ? rel : `${rel}/`);
|
|
109
|
+
scopePrefixes.push(rel);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
let taskRel = null;
|
|
113
|
+
if (taskFolder) {
|
|
114
|
+
taskRel = path.relative(worktreePath, taskFolder);
|
|
115
|
+
if (taskRel && !taskRel.startsWith("..")) {
|
|
116
|
+
scopePrefixes.push(taskRel.endsWith("/") ? taskRel : `${taskRel}/`);
|
|
117
|
+
scopePrefixes.push(taskRel);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (scopePrefixes.length === 0) return [];
|
|
122
|
+
|
|
123
|
+
const inScope = (filePath) =>
|
|
124
|
+
scopePrefixes.some((prefix) => filePath === prefix || filePath.startsWith(prefix));
|
|
125
|
+
|
|
126
|
+
const dirty = new Set();
|
|
127
|
+
for (const line of porcelain.split("\n")) {
|
|
128
|
+
if (!line.trim()) continue;
|
|
129
|
+
let filePath = line.length > 2 && line[2] === " " ? line.slice(3) : line.slice(2);
|
|
130
|
+
filePath = filePath.trim();
|
|
131
|
+
const renamed = filePath.includes(" -> ") ? filePath.split(" -> ").pop()?.trim() : filePath;
|
|
132
|
+
if (renamed && inScope(renamed)) dirty.add(renamed);
|
|
133
|
+
}
|
|
134
|
+
return [...dirty].sort();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {object[]} events
|
|
139
|
+
* @param {object} filter
|
|
140
|
+
* @param {number} [filter.laneNumber]
|
|
141
|
+
* @param {string} [filter.laneId]
|
|
142
|
+
* @param {string} [filter.taskId]
|
|
143
|
+
*/
|
|
144
|
+
export function findLatestStepCompletedMs(events, { laneNumber, laneId, taskId }) {
|
|
145
|
+
let latest = null;
|
|
146
|
+
const expectedLaneId =
|
|
147
|
+
laneNumber != null ? `lane-${laneNumber}` : typeof laneId === "string" ? laneId : null;
|
|
148
|
+
|
|
149
|
+
for (const event of events) {
|
|
150
|
+
if (event.type !== "task.step_completed") continue;
|
|
151
|
+
if (taskId && event.taskId !== taskId) continue;
|
|
152
|
+
if (expectedLaneId && event.laneId && event.laneId !== expectedLaneId) continue;
|
|
153
|
+
|
|
154
|
+
const ts = Date.parse(event.timestamp);
|
|
155
|
+
if (Number.isNaN(ts)) continue;
|
|
156
|
+
latest = latest === null ? ts : Math.max(latest, ts);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return latest;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @param {object} params
|
|
164
|
+
* @param {string[]} [params.fileScopePaths]
|
|
165
|
+
* @param {object} [params.journalContext]
|
|
166
|
+
* @param {string} [params.journalContext.projectRoot]
|
|
167
|
+
* @param {string} [params.journalContext.batchId]
|
|
168
|
+
* @param {number} [params.journalContext.laneNumber]
|
|
169
|
+
* @param {string} [params.journalContext.laneId]
|
|
170
|
+
* @param {string} [params.journalContext.taskId]
|
|
171
|
+
*/
|
|
172
|
+
export function collectProgressSignals({
|
|
173
|
+
worktreePath,
|
|
174
|
+
taskFolder,
|
|
175
|
+
laneBranch,
|
|
176
|
+
fileScopePaths,
|
|
177
|
+
journalContext,
|
|
178
|
+
}) {
|
|
179
|
+
const statusPath = path.join(taskFolder, "STATUS.md");
|
|
180
|
+
let statusMtimeMs = null;
|
|
181
|
+
if (fs.existsSync(statusPath)) {
|
|
182
|
+
statusMtimeMs = fs.statSync(statusPath).mtimeMs;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
let lastCommitAtMs = null;
|
|
186
|
+
if (laneBranch) {
|
|
187
|
+
const ts = git(worktreePath, ["log", "-1", "--format=%ct", laneBranch]);
|
|
188
|
+
if (ts) {
|
|
189
|
+
const parsed = Number(ts);
|
|
190
|
+
if (!Number.isNaN(parsed)) lastCommitAtMs = parsed * 1000;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const fileScopeMtimeMs = resolveFileScopeMtimeMs(worktreePath, fileScopePaths);
|
|
195
|
+
const dirtyPaths = resolveScopedDirtyPaths(worktreePath, fileScopePaths, taskFolder);
|
|
196
|
+
|
|
197
|
+
let stepCompletedAtMs = null;
|
|
198
|
+
if (journalContext?.projectRoot && journalContext?.batchId) {
|
|
199
|
+
const events = readJournalEvents(journalContext.projectRoot, journalContext.batchId);
|
|
200
|
+
stepCompletedAtMs = findLatestStepCompletedMs(events, {
|
|
201
|
+
laneNumber: journalContext.laneNumber,
|
|
202
|
+
laneId: journalContext.laneId,
|
|
203
|
+
taskId: journalContext.taskId,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
statusMtimeMs,
|
|
209
|
+
lastCommitAtMs,
|
|
210
|
+
fileScopeMtimeMs,
|
|
211
|
+
stepCompletedAtMs,
|
|
212
|
+
dirtyPaths,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Checkpoint signals extend stall grace (STATUS, lane commit, task.step_completed).
|
|
218
|
+
* @param {object | null} prev
|
|
219
|
+
* @param {object} next
|
|
220
|
+
*/
|
|
221
|
+
export function checkpointSignalsChanged(prev, next) {
|
|
222
|
+
if (!prev) {
|
|
223
|
+
return Boolean(next.statusMtimeMs || next.lastCommitAtMs || next.stepCompletedAtMs);
|
|
224
|
+
}
|
|
225
|
+
if (prev.statusMtimeMs !== next.statusMtimeMs) return true;
|
|
226
|
+
if (prev.lastCommitAtMs !== next.lastCommitAtMs) return true;
|
|
227
|
+
if (prev.stepCompletedAtMs !== next.stepCompletedAtMs) return true;
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Activity signals are warning-only unless extendGraceOnFileScope is enabled.
|
|
233
|
+
* @param {object | null} prev
|
|
234
|
+
* @param {object} next
|
|
235
|
+
*/
|
|
236
|
+
export function activitySignalsChanged(prev, next) {
|
|
237
|
+
if (!prev) {
|
|
238
|
+
return Boolean(next.fileScopeMtimeMs || (next.dirtyPaths?.length ?? 0) > 0);
|
|
239
|
+
}
|
|
240
|
+
if (prev.fileScopeMtimeMs !== next.fileScopeMtimeMs) return true;
|
|
241
|
+
const prevDirty = (prev.dirtyPaths ?? []).join("\0");
|
|
242
|
+
const nextDirty = (next.dirtyPaths ?? []).join("\0");
|
|
243
|
+
if (prevDirty !== nextDirty) return true;
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* @param {object | null} prev
|
|
249
|
+
* @param {object} next
|
|
250
|
+
* @param {object} [options]
|
|
251
|
+
* @param {boolean} [options.extendGraceOnFileScope]
|
|
252
|
+
*/
|
|
253
|
+
export function progressSignalsChanged(prev, next, options = {}) {
|
|
254
|
+
const checkpoint = checkpointSignalsChanged(prev, next);
|
|
255
|
+
if (checkpoint) return true;
|
|
256
|
+
if (options.extendGraceOnFileScope) return activitySignalsChanged(prev, next);
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* @param {object} signals
|
|
262
|
+
*/
|
|
263
|
+
export function resolveLastCheckpointMs(signals) {
|
|
264
|
+
const parts = [signals.statusMtimeMs, signals.lastCommitAtMs, signals.stepCompletedAtMs].filter(
|
|
265
|
+
(v) => v != null,
|
|
266
|
+
);
|
|
267
|
+
if (parts.length === 0) return null;
|
|
268
|
+
return Math.max(...parts);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* @param {object} signals
|
|
273
|
+
*/
|
|
274
|
+
export function hasActivitySignals(signals) {
|
|
275
|
+
return Boolean(signals.fileScopeMtimeMs || (signals.dirtyPaths?.length ?? 0) > 0);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* @param {object} params
|
|
280
|
+
* @param {number} params.now
|
|
281
|
+
* @param {number} params.lastCheckpointAt
|
|
282
|
+
* @param {object} params.signals
|
|
283
|
+
* @param {object} params.stallConfig
|
|
284
|
+
* @param {boolean} params.activitySinceCheckpoint
|
|
285
|
+
* @param {WorkerPhase} [params.workerPhase]
|
|
286
|
+
*/
|
|
287
|
+
export function shouldEmitCheckpointWarning({
|
|
288
|
+
now,
|
|
289
|
+
lastCheckpointAt,
|
|
290
|
+
signals,
|
|
291
|
+
stallConfig,
|
|
292
|
+
activitySinceCheckpoint,
|
|
293
|
+
workerPhase = "unknown",
|
|
294
|
+
}) {
|
|
295
|
+
if (workerPhase === "launching") return false;
|
|
296
|
+
if (!activitySinceCheckpoint || !hasActivitySignals(signals)) return false;
|
|
297
|
+
return now - lastCheckpointAt >= stallConfig.checkpointWarningMs;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @param {object} params
|
|
302
|
+
*/
|
|
303
|
+
export function computeStallDeadline({ startedAt, lastProgressAt, stallConfig }) {
|
|
304
|
+
const hardDeadline = startedAt + stallConfig.stallTimeoutMs;
|
|
305
|
+
const progressDeadline = lastProgressAt + stallConfig.graceAfterProgressMs;
|
|
306
|
+
return Math.max(hardDeadline, progressDeadline);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @param {object} params
|
|
311
|
+
* @param {WorkerPhase} [params.workerPhase]
|
|
312
|
+
* @param {HeartbeatKind} [params.heartbeatKind]
|
|
313
|
+
*/
|
|
314
|
+
export function resolveHeartbeatKind({
|
|
315
|
+
workerPhase,
|
|
316
|
+
checkpointChanged = false,
|
|
317
|
+
activityChanged = false,
|
|
318
|
+
}) {
|
|
319
|
+
if (workerPhase === "launching") return "worker_alive";
|
|
320
|
+
if (checkpointChanged) return "checkpoint";
|
|
321
|
+
if (activityChanged) return "file_scope_activity";
|
|
322
|
+
return "worker_alive";
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Progress-class heartbeat fields are omitted during launcher preflight so retries
|
|
327
|
+
* do not reuse stale STATUS / git checkpoint signals from a prior attempt.
|
|
328
|
+
*
|
|
329
|
+
* @param {object} signals
|
|
330
|
+
* @param {WorkerPhase} workerPhase
|
|
331
|
+
* @param {HeartbeatKind} heartbeatKind
|
|
332
|
+
*/
|
|
333
|
+
export function buildHeartbeatPayloadFields(signals, workerPhase, heartbeatKind) {
|
|
334
|
+
const includeProgressFields =
|
|
335
|
+
workerPhase !== "launching" &&
|
|
336
|
+
(heartbeatKind === "checkpoint" || heartbeatKind === "file_scope_activity");
|
|
337
|
+
|
|
338
|
+
if (!includeProgressFields) {
|
|
339
|
+
return {
|
|
340
|
+
statusMtimeMs: null,
|
|
341
|
+
lastCommitAtMs: null,
|
|
342
|
+
fileScopeMtimeMs: null,
|
|
343
|
+
dirtyPathCount: 0,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return {
|
|
348
|
+
statusMtimeMs: signals.statusMtimeMs,
|
|
349
|
+
lastCommitAtMs: signals.lastCommitAtMs,
|
|
350
|
+
fileScopeMtimeMs: signals.fileScopeMtimeMs,
|
|
351
|
+
dirtyPathCount: signals.dirtyPaths?.length ?? 0,
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* @param {object} params
|
|
357
|
+
* @param {WorkerPhase} [params.workerPhase]
|
|
358
|
+
* @param {HeartbeatKind} [params.heartbeatKind]
|
|
359
|
+
*/
|
|
360
|
+
export function recordLaneHeartbeat({
|
|
361
|
+
projectRoot,
|
|
362
|
+
batchId,
|
|
363
|
+
laneNumber,
|
|
364
|
+
taskId,
|
|
365
|
+
signals,
|
|
366
|
+
correlationId,
|
|
367
|
+
workerPhase = "unknown",
|
|
368
|
+
heartbeatKind = "worker_alive",
|
|
369
|
+
}) {
|
|
370
|
+
const progressFields = buildHeartbeatPayloadFields(signals, workerPhase, heartbeatKind);
|
|
371
|
+
appendJournalEvent(projectRoot, batchId, "lane.heartbeat", {
|
|
372
|
+
laneNumber,
|
|
373
|
+
taskId,
|
|
374
|
+
correlationId,
|
|
375
|
+
heartbeatKind,
|
|
376
|
+
workerPhase,
|
|
377
|
+
...progressFields,
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @param {object} params
|
|
383
|
+
*/
|
|
384
|
+
export function recordCheckpointWarning({
|
|
385
|
+
projectRoot,
|
|
386
|
+
batchId,
|
|
387
|
+
laneNumber,
|
|
388
|
+
taskId,
|
|
389
|
+
signals,
|
|
390
|
+
lastCheckpointAt,
|
|
391
|
+
correlationId,
|
|
392
|
+
}) {
|
|
393
|
+
appendJournalEvent(projectRoot, batchId, "lane.checkpoint_warning", {
|
|
394
|
+
laneNumber,
|
|
395
|
+
taskId,
|
|
396
|
+
correlationId,
|
|
397
|
+
lastCheckpointAt,
|
|
398
|
+
statusMtimeMs: signals.statusMtimeMs,
|
|
399
|
+
lastCommitAtMs: signals.lastCommitAtMs,
|
|
400
|
+
fileScopeMtimeMs: signals.fileScopeMtimeMs,
|
|
401
|
+
stepCompletedAtMs: signals.stepCompletedAtMs,
|
|
402
|
+
dirtyPaths: signals.dirtyPaths ?? [],
|
|
403
|
+
suggestion: CHECKPOINT_WARNING_SUGGESTION,
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* @param {object} params
|
|
409
|
+
*/
|
|
410
|
+
export function recordStallWarning({
|
|
411
|
+
projectRoot,
|
|
412
|
+
batchId,
|
|
413
|
+
laneNumber,
|
|
414
|
+
taskId,
|
|
415
|
+
signals,
|
|
416
|
+
stallDeadline,
|
|
417
|
+
correlationId,
|
|
418
|
+
}) {
|
|
419
|
+
appendJournalEvent(projectRoot, batchId, "lane.stall_warning", {
|
|
420
|
+
laneNumber,
|
|
421
|
+
taskId,
|
|
422
|
+
correlationId,
|
|
423
|
+
stallDeadline,
|
|
424
|
+
statusMtimeMs: signals.statusMtimeMs,
|
|
425
|
+
lastCommitAtMs: signals.lastCommitAtMs,
|
|
426
|
+
fileScopeMtimeMs: signals.fileScopeMtimeMs,
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export { CHECKPOINT_WARNING_SUGGESTION, POLL_INTERVAL_MS };
|