@psimona/darkf 0.0.0-stage → 0.4.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 +28 -2
- package/bunfig.toml +13 -0
- package/examples/article-classifier/data/articles.ts +38 -0
- package/examples/article-classifier/scripts/next.ts +7 -0
- package/examples/article-classifier/scripts/queue.ts +33 -0
- package/examples/article-classifier/scripts/record.ts +9 -0
- package/examples/article-classifier/workflow.ts +44 -0
- package/examples/haiku-commit/scripts/check.ts +24 -0
- package/examples/haiku-commit/workflow.ts +28 -0
- package/examples/haiku-readme/scripts/test.ts +13 -0
- package/examples/haiku-readme/scripts/write.ts +10 -0
- package/examples/haiku-readme/workflow.ts +41 -0
- package/examples/task-queue/scripts/next.ts +20 -0
- package/examples/task-queue/scripts/record.ts +20 -0
- package/examples/task-queue/supervise.ts +57 -0
- package/examples/task-queue/workflow.ts +33 -0
- package/feedback/2026-10-30-self-update.md +29 -0
- package/package.json +30 -4
- package/src/cli/detached.test.ts +560 -0
- package/src/cli/index.ts +1040 -0
- package/src/cli/update.test.ts +486 -0
- package/src/cli/update.ts +191 -0
- package/src/core/graph.test.ts +172 -0
- package/src/core/graph.ts +148 -0
- package/src/core/index.ts +4 -0
- package/src/core/journal.ts +21 -0
- package/src/core/mermaid.test.ts +34 -0
- package/src/core/mermaid.ts +95 -0
- package/src/core/types.ts +279 -0
- package/src/engine/eventLog.ts +17 -0
- package/src/engine/index.ts +2 -0
- package/src/engine/run.test.ts +1068 -0
- package/src/engine/run.ts +359 -0
- package/src/exports.ts +2 -0
- package/src/kit/agents/claude-code.test.ts +37 -0
- package/src/kit/agents/claude-code.ts +180 -0
- package/src/kit/agents/handoff.test.ts +128 -0
- package/src/kit/agents/handoff.ts +69 -0
- package/src/kit/agents/opencode.test.ts +136 -0
- package/src/kit/agents/opencode.ts +327 -0
- package/src/kit/registry.test.ts +386 -0
- package/src/kit/registry.ts +296 -0
- package/src/kit/status.test.ts +423 -0
- package/src/kit/status.ts +282 -0
- package/tsconfig.json +4 -0
- package/workflows/dev-loop/scripts/begin.ts +22 -0
- package/workflows/dev-loop/scripts/check.ts +19 -0
- package/workflows/dev-loop/scripts/finish.ts +114 -0
- package/workflows/dev-loop/workflow.ts +135 -0
- package/workflows/series-ordering/scripts/verify.ts +18 -0
- package/workflows/series-ordering/workflow.ts +80 -0
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { type CrashReason, EXIT, FAILED, type NodeEvent, type NodeResult } from "../core/index.ts";
|
|
3
|
+
import {
|
|
4
|
+
agentStallWarning,
|
|
5
|
+
classifyTerminal,
|
|
6
|
+
foldJournal,
|
|
7
|
+
foldReport,
|
|
8
|
+
nudgeWarning,
|
|
9
|
+
stallWarning,
|
|
10
|
+
} from "./status.ts";
|
|
11
|
+
|
|
12
|
+
const R = "run-id";
|
|
13
|
+
function begin(seed: NodeResult = {}): NodeEvent {
|
|
14
|
+
return {
|
|
15
|
+
runId: R,
|
|
16
|
+
state: EXIT,
|
|
17
|
+
scopePath: [],
|
|
18
|
+
status: { phase: "succeeded", result: seed },
|
|
19
|
+
timestamp: 1000,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function end(final: NodeResult = {}): NodeEvent {
|
|
23
|
+
return {
|
|
24
|
+
runId: R,
|
|
25
|
+
state: EXIT,
|
|
26
|
+
scopePath: [],
|
|
27
|
+
status: { phase: "succeeded", result: final },
|
|
28
|
+
timestamp: 3000,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function running(
|
|
32
|
+
state: string,
|
|
33
|
+
ts: number,
|
|
34
|
+
attempt = 1,
|
|
35
|
+
agentSessionId?: string,
|
|
36
|
+
nudgeAttempt?: number,
|
|
37
|
+
nudgeReason?: CrashReason["kind"],
|
|
38
|
+
): NodeEvent {
|
|
39
|
+
return {
|
|
40
|
+
runId: R,
|
|
41
|
+
state,
|
|
42
|
+
scopePath: [],
|
|
43
|
+
status: {
|
|
44
|
+
phase: "running",
|
|
45
|
+
attempt,
|
|
46
|
+
startedAt: ts,
|
|
47
|
+
lastHeartbeat: ts,
|
|
48
|
+
agentSessionId,
|
|
49
|
+
nudgeAttempt,
|
|
50
|
+
nudgeReason,
|
|
51
|
+
},
|
|
52
|
+
timestamp: ts,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function succeeded(state: string, ts: number, result: NodeResult = {}): NodeEvent {
|
|
56
|
+
return { runId: R, state, scopePath: [], status: { phase: "succeeded", result }, timestamp: ts };
|
|
57
|
+
}
|
|
58
|
+
function crashed(state: string, ts: number, reason: CrashReason, attempt = 1): NodeEvent {
|
|
59
|
+
return {
|
|
60
|
+
runId: R,
|
|
61
|
+
state,
|
|
62
|
+
scopePath: [],
|
|
63
|
+
status: { phase: "crashed", reason, attempt },
|
|
64
|
+
timestamp: ts,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function failedEnd(result: NodeResult = {}): NodeEvent {
|
|
68
|
+
return {
|
|
69
|
+
runId: R,
|
|
70
|
+
state: FAILED,
|
|
71
|
+
scopePath: [],
|
|
72
|
+
status: { phase: "failed", result },
|
|
73
|
+
timestamp: 3000,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function journalText(events: NodeEvent[]): string {
|
|
77
|
+
return events.map((e) => JSON.stringify(e)).join("\n");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
test("foldJournal on an empty journal returns the empty digest", () => {
|
|
81
|
+
expect(foldJournal("")).toEqual({
|
|
82
|
+
current: null,
|
|
83
|
+
visits: {},
|
|
84
|
+
runtimeMs: null,
|
|
85
|
+
hasBegin: false,
|
|
86
|
+
hasEnd: false,
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("foldJournal on a succeeded journal detects begin/end, final node, visits, runtime", () => {
|
|
91
|
+
const seed = { output: { message: "hi" } };
|
|
92
|
+
const final = { output: "done" };
|
|
93
|
+
const digest = foldJournal(
|
|
94
|
+
journalText([begin(seed), running("S", 2000), succeeded("S", 2500, final), end(final)]),
|
|
95
|
+
);
|
|
96
|
+
expect(digest.hasBegin).toBe(true);
|
|
97
|
+
expect(digest.hasEnd).toBe(true);
|
|
98
|
+
expect(digest.seed).toEqual(seed);
|
|
99
|
+
expect(digest.finalResult).toEqual(final);
|
|
100
|
+
expect(digest.current).toEqual({ state: EXIT, phase: "succeeded" });
|
|
101
|
+
expect(digest.visits).toEqual({ S: 1 });
|
|
102
|
+
expect(digest.runtimeMs).toBe(3000 - 1000);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("foldJournal on a crashed journal records the crash and no end marker", () => {
|
|
106
|
+
const reason: CrashReason = { kind: "runner-error", error: "boom" };
|
|
107
|
+
const digest = foldJournal(
|
|
108
|
+
journalText([begin(), running("Solo", 2000), crashed("Solo", 2600, reason, 2)]),
|
|
109
|
+
);
|
|
110
|
+
expect(digest.hasBegin).toBe(true);
|
|
111
|
+
expect(digest.hasEnd).toBe(false);
|
|
112
|
+
expect(digest.current).toEqual({ state: "Solo", phase: "crashed" });
|
|
113
|
+
expect(digest.crash).toEqual({ attempt: 2, reason });
|
|
114
|
+
expect(digest.visits).toEqual({ Solo: 1 });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("foldJournal on a FAILED-terminated journal detects hasEnd/finalResult, same as succeeded", () => {
|
|
118
|
+
const final = { output: { kind: "stuck_signature" } };
|
|
119
|
+
const digest = foldJournal(
|
|
120
|
+
journalText([
|
|
121
|
+
begin(),
|
|
122
|
+
running("RunFailure", 2000),
|
|
123
|
+
succeeded("RunFailure", 2500),
|
|
124
|
+
failedEnd(final),
|
|
125
|
+
]),
|
|
126
|
+
);
|
|
127
|
+
expect(digest.hasBegin).toBe(true);
|
|
128
|
+
expect(digest.hasEnd).toBe(true);
|
|
129
|
+
expect(digest.finalResult).toEqual(final);
|
|
130
|
+
expect(digest.current).toEqual({ state: FAILED, phase: "failed" });
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("foldJournal on a still-running journal shows the running node", () => {
|
|
134
|
+
const digest = foldJournal(journalText([begin(), running("Write", 2000)]));
|
|
135
|
+
expect(digest.current).toEqual({
|
|
136
|
+
state: "Write",
|
|
137
|
+
phase: "running",
|
|
138
|
+
startedAt: 2000,
|
|
139
|
+
lastHeartbeat: 2000,
|
|
140
|
+
});
|
|
141
|
+
expect(digest.hasEnd).toBe(false);
|
|
142
|
+
expect(digest.visits).toEqual({ Write: 1 });
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test("foldJournal surfaces a running node's agentSessionId, omits it when absent", () => {
|
|
146
|
+
const withSession = foldJournal(journalText([begin(), running("Write", 2000, 1, "sess-123")]));
|
|
147
|
+
expect(withSession.current).toEqual({
|
|
148
|
+
state: "Write",
|
|
149
|
+
phase: "running",
|
|
150
|
+
startedAt: 2000,
|
|
151
|
+
lastHeartbeat: 2000,
|
|
152
|
+
agentSessionId: "sess-123",
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const withoutSession = foldJournal(journalText([begin(), running("Write", 2000)]));
|
|
156
|
+
expect(withoutSession.current?.agentSessionId).toBeUndefined();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("foldJournal: heartbeats neither inflate visits nor reset liveness", () => {
|
|
160
|
+
const digest = foldJournal(
|
|
161
|
+
journalText([
|
|
162
|
+
begin(),
|
|
163
|
+
running("Write", 2000),
|
|
164
|
+
running("Write", 2030), // heartbeat — same dispatch still open
|
|
165
|
+
running("Write", 2060), // heartbeat
|
|
166
|
+
]),
|
|
167
|
+
);
|
|
168
|
+
expect(digest.visits).toEqual({ Write: 1 });
|
|
169
|
+
expect(digest.current?.phase).toBe("running");
|
|
170
|
+
expect(digest.current?.lastHeartbeat).toBe(2060);
|
|
171
|
+
expect(digest.runtimeMs).toBe(2060 - 1000); // heartbeats advance the span
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("stallWarning fires only for a running node with stale heartbeats", () => {
|
|
175
|
+
const NOW = 200_000;
|
|
176
|
+
const stale = foldJournal(journalText([begin(), running("Write", 2000)]));
|
|
177
|
+
// lastHeartbeat = 2000, now = 200_000 → far past the 90s stall window
|
|
178
|
+
expect(stallWarning(stale, NOW)).toContain("stall suspected");
|
|
179
|
+
const fresh = foldJournal(journalText([begin(), running("Write", NOW - 1000)]));
|
|
180
|
+
expect(stallWarning(fresh, NOW)).toBeNull();
|
|
181
|
+
const done = foldJournal(
|
|
182
|
+
journalText([
|
|
183
|
+
begin(),
|
|
184
|
+
running("Write", 2000),
|
|
185
|
+
{
|
|
186
|
+
runId: R,
|
|
187
|
+
state: "Write",
|
|
188
|
+
scopePath: [],
|
|
189
|
+
status: { phase: "succeeded", result: {} },
|
|
190
|
+
timestamp: 3000,
|
|
191
|
+
},
|
|
192
|
+
]),
|
|
193
|
+
);
|
|
194
|
+
expect(stallWarning(done, NOW)).toBeNull();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test("foldJournal surfaces a running node's nudgeAttempt/nudgeReason, omits them when absent", () => {
|
|
198
|
+
const nudging = foldJournal(
|
|
199
|
+
journalText([begin(), running("Write", 2000, 1, "sess-1", 2, "handoff-missing")]),
|
|
200
|
+
);
|
|
201
|
+
expect(nudging.current).toEqual({
|
|
202
|
+
state: "Write",
|
|
203
|
+
phase: "running",
|
|
204
|
+
startedAt: 2000,
|
|
205
|
+
lastHeartbeat: 2000,
|
|
206
|
+
agentSessionId: "sess-1",
|
|
207
|
+
nudgeAttempt: 2,
|
|
208
|
+
nudgeReason: "handoff-missing",
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const plain = foldJournal(journalText([begin(), running("Write", 2000)]));
|
|
212
|
+
expect(plain.current?.nudgeAttempt).toBeUndefined();
|
|
213
|
+
expect(plain.current?.nudgeReason).toBeUndefined();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
function runningWithAgentProgress(
|
|
217
|
+
state: string,
|
|
218
|
+
heartbeatTs: number,
|
|
219
|
+
agentStepCount: number,
|
|
220
|
+
agentIdleMs: number,
|
|
221
|
+
): NodeEvent {
|
|
222
|
+
return {
|
|
223
|
+
runId: R,
|
|
224
|
+
state,
|
|
225
|
+
scopePath: [],
|
|
226
|
+
status: {
|
|
227
|
+
phase: "running",
|
|
228
|
+
attempt: 1,
|
|
229
|
+
startedAt: heartbeatTs,
|
|
230
|
+
lastHeartbeat: heartbeatTs,
|
|
231
|
+
agentStepCount,
|
|
232
|
+
agentIdleMs,
|
|
233
|
+
},
|
|
234
|
+
timestamp: heartbeatTs,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
test("foldJournal surfaces a running node's agentStepCount/agentIdleMs, omits them when absent", () => {
|
|
239
|
+
const withProgress = foldJournal(
|
|
240
|
+
journalText([begin(), runningWithAgentProgress("Write", 2000, 5, 1200)]),
|
|
241
|
+
);
|
|
242
|
+
expect(withProgress.current?.agentStepCount).toBe(5);
|
|
243
|
+
expect(withProgress.current?.agentIdleMs).toBe(1200);
|
|
244
|
+
|
|
245
|
+
const plain = foldJournal(journalText([begin(), running("Write", 2000)]));
|
|
246
|
+
expect(plain.current?.agentStepCount).toBeUndefined();
|
|
247
|
+
expect(plain.current?.agentIdleMs).toBeUndefined();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test("agentStallWarning fires only once agent idle time (as of now) exceeds the threshold", () => {
|
|
251
|
+
const NOW = 20 * 60_000; // 20 minutes
|
|
252
|
+
// lastHeartbeat at t=0, agentIdleMs=1000 as of that heartbeat → live idle
|
|
253
|
+
// is ~20 minutes by `now`, past the 15-minute default.
|
|
254
|
+
const stale = foldJournal(journalText([begin(), runningWithAgentProgress("Write", 0, 3, 1000)]));
|
|
255
|
+
const warning = agentStallWarning(stale, NOW);
|
|
256
|
+
expect(warning).toContain("agent stall suspected");
|
|
257
|
+
expect(warning).toContain("steps so far: 3");
|
|
258
|
+
|
|
259
|
+
// Fresh heartbeat, same idle-as-of-heartbeat value → live idle is small.
|
|
260
|
+
const fresh = foldJournal(
|
|
261
|
+
journalText([begin(), runningWithAgentProgress("Write", NOW - 1000, 3, 1000)]),
|
|
262
|
+
);
|
|
263
|
+
expect(agentStallWarning(fresh, NOW)).toBeNull();
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test("agentStallWarning never fires without agentIdleMs evidence (script dispatch, or no progress reported yet)", () => {
|
|
267
|
+
const NOW = 20 * 60_000;
|
|
268
|
+
const noProgress = foldJournal(journalText([begin(), running("Build", 0)]));
|
|
269
|
+
expect(agentStallWarning(noProgress, NOW)).toBeNull();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test("nudgeWarning fires only for a running node with an active nudge", () => {
|
|
273
|
+
const nudging = foldJournal(
|
|
274
|
+
journalText([begin(), running("Write", 2000, 1, "sess-1", 2, "handoff-missing")]),
|
|
275
|
+
);
|
|
276
|
+
expect(nudgeWarning(nudging)).toBe("nudging attempt 2/3 (handoff-missing) — supervisor decides");
|
|
277
|
+
|
|
278
|
+
const plain = foldJournal(journalText([begin(), running("Write", 2000)]));
|
|
279
|
+
expect(nudgeWarning(plain)).toBeNull();
|
|
280
|
+
|
|
281
|
+
const done = foldJournal(
|
|
282
|
+
journalText([
|
|
283
|
+
begin(),
|
|
284
|
+
running("Write", 2000, 1, "sess-1", 2, "handoff-missing"),
|
|
285
|
+
succeeded("Write", 2500, {}),
|
|
286
|
+
]),
|
|
287
|
+
);
|
|
288
|
+
expect(nudgeWarning(done)).toBeNull();
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("foldJournal counts distinct visits, not retry attempts", () => {
|
|
292
|
+
// Write visited once (two attempts), then Test visited once
|
|
293
|
+
const events = [
|
|
294
|
+
begin(),
|
|
295
|
+
running("Write", 1100, 1),
|
|
296
|
+
running("Write", 1200, 2),
|
|
297
|
+
succeeded("Write", 1300),
|
|
298
|
+
running("Test", 1400, 1),
|
|
299
|
+
];
|
|
300
|
+
const digest = foldJournal(journalText(events));
|
|
301
|
+
expect(digest.visits).toEqual({ Write: 1, Test: 1 });
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test("foldJournal tolerates a torn trailing line (parseEventLog skips it)", () => {
|
|
305
|
+
const digest = foldJournal(`${journalText([begin(), running("S", 2000)])}\n{torn`);
|
|
306
|
+
expect(digest.current).toEqual({
|
|
307
|
+
state: "S",
|
|
308
|
+
phase: "running",
|
|
309
|
+
startedAt: 2000,
|
|
310
|
+
lastHeartbeat: 2000,
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test("classifyTerminal: journal's last line is authoritative for succeeded/crashed", () => {
|
|
315
|
+
expect(classifyTerminal(journalText([begin(), end()]), false)).toBe("succeeded");
|
|
316
|
+
expect(classifyTerminal(journalText([begin(), end()]), true)).toBe("succeeded"); // PID state irrelevant
|
|
317
|
+
expect(
|
|
318
|
+
classifyTerminal(
|
|
319
|
+
journalText([begin(), crashed("S", 2000, { kind: "timeout", elapsedMs: 1 })]),
|
|
320
|
+
true,
|
|
321
|
+
),
|
|
322
|
+
).toBe("crashed");
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
test("classifyTerminal: a FAILED-tagged last line reads as failed, not crashed", () => {
|
|
326
|
+
expect(
|
|
327
|
+
classifyTerminal(journalText([begin(), succeeded("RunFailure", 2000), failedEnd()]), true),
|
|
328
|
+
).toBe("failed");
|
|
329
|
+
expect(
|
|
330
|
+
classifyTerminal(journalText([begin(), succeeded("RunFailure", 2000), failedEnd()]), false),
|
|
331
|
+
).toBe("failed"); // PID state irrelevant, same as succeeded/crashed
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
test("classifyTerminal: no terminal marker -> running if PID alive, else orphaned", () => {
|
|
335
|
+
const notDone = journalText([begin(), running("S", 2000)]);
|
|
336
|
+
expect(classifyTerminal(notDone, true)).toBe("running");
|
|
337
|
+
expect(classifyTerminal(notDone, false)).toBe("orphaned");
|
|
338
|
+
expect(classifyTerminal("", false)).toBe("orphaned"); // missing journal + dead PID
|
|
339
|
+
expect(classifyTerminal("", true)).toBe("running");
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
describe("foldReport", () => {
|
|
343
|
+
test("one dispatch row per running->terminal pair, attempt-normalized", () => {
|
|
344
|
+
// Terminal journal events do NOT carry `attempt` — only running events do.
|
|
345
|
+
// The fold must match them despite that (regression: rows were dropped).
|
|
346
|
+
const journal = [
|
|
347
|
+
{
|
|
348
|
+
runId: "r",
|
|
349
|
+
state: "[*]",
|
|
350
|
+
scopePath: [],
|
|
351
|
+
status: { phase: "succeeded", result: {} },
|
|
352
|
+
timestamp: 0,
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
runId: "r",
|
|
356
|
+
state: "Implement",
|
|
357
|
+
scopePath: [],
|
|
358
|
+
status: { phase: "running", attempt: 1 },
|
|
359
|
+
timestamp: 100,
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
runId: "r",
|
|
363
|
+
state: "Implement",
|
|
364
|
+
scopePath: [],
|
|
365
|
+
status: { phase: "succeeded", result: { event: "implemented" } },
|
|
366
|
+
timestamp: 600,
|
|
367
|
+
},
|
|
368
|
+
{
|
|
369
|
+
runId: "r",
|
|
370
|
+
state: "Verify",
|
|
371
|
+
scopePath: [],
|
|
372
|
+
status: { phase: "running", attempt: 1 },
|
|
373
|
+
timestamp: 600,
|
|
374
|
+
},
|
|
375
|
+
{
|
|
376
|
+
runId: "r",
|
|
377
|
+
state: "Verify",
|
|
378
|
+
scopePath: [],
|
|
379
|
+
status: { phase: "succeeded", result: { event: "green" } },
|
|
380
|
+
timestamp: 1600,
|
|
381
|
+
},
|
|
382
|
+
]
|
|
383
|
+
.map((e) => JSON.stringify(e))
|
|
384
|
+
.join("\n");
|
|
385
|
+
|
|
386
|
+
const report = foldReport(journal);
|
|
387
|
+
expect(report.spanMs).toBe(1600);
|
|
388
|
+
expect(report.dispatches).toHaveLength(2);
|
|
389
|
+
expect(report.dispatches[0]).toEqual({
|
|
390
|
+
state: "Implement",
|
|
391
|
+
durationMs: 500,
|
|
392
|
+
phase: "succeeded",
|
|
393
|
+
event: "implemented",
|
|
394
|
+
reason: undefined,
|
|
395
|
+
});
|
|
396
|
+
expect(report.dispatches[1]).toEqual({
|
|
397
|
+
state: "Verify",
|
|
398
|
+
durationMs: 1000,
|
|
399
|
+
phase: "succeeded",
|
|
400
|
+
event: "green",
|
|
401
|
+
reason: undefined,
|
|
402
|
+
});
|
|
403
|
+
expect(report.visits).toEqual({ Implement: 1, Verify: 1 });
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
test("a FAILED-tagged end marker is its own dispatch row, not silently dropped", () => {
|
|
407
|
+
const journal = journalText([
|
|
408
|
+
begin(),
|
|
409
|
+
running("RunFailure", 100),
|
|
410
|
+
succeeded("RunFailure", 600, { event: "gave_up" }),
|
|
411
|
+
failedEnd({ event: "gave_up", output: { kind: "cap_exceeded" } }),
|
|
412
|
+
]);
|
|
413
|
+
|
|
414
|
+
const report = foldReport(journal);
|
|
415
|
+
expect(report.dispatches).toHaveLength(2);
|
|
416
|
+
expect(report.dispatches[1]).toEqual({
|
|
417
|
+
state: FAILED,
|
|
418
|
+
durationMs: 0,
|
|
419
|
+
phase: "failed",
|
|
420
|
+
event: "gave_up",
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
});
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { type CrashReason, DONE, FAILED, type NodeResult, parseEventLog } from "../core/index.ts";
|
|
2
|
+
import { journalTerminal, type RegistryStatus } from "./registry.ts";
|
|
3
|
+
|
|
4
|
+
export interface JournalDigest {
|
|
5
|
+
current: {
|
|
6
|
+
state: string;
|
|
7
|
+
phase: string;
|
|
8
|
+
/** Present while a dispatch is open (running events carry it). */
|
|
9
|
+
startedAt?: number;
|
|
10
|
+
lastHeartbeat?: number;
|
|
11
|
+
/** The agent-dispatch session for this running node, when its runner
|
|
12
|
+
* reported one (`NodeStatus.agentSessionId`) — e.g. a resumable claude-code
|
|
13
|
+
* `--resume` session. Absent for script dispatches or runners that
|
|
14
|
+
* don't report one. */
|
|
15
|
+
agentSessionId?: string;
|
|
16
|
+
/** Present while the engine is nudging this attempt (1..3) — see
|
|
17
|
+
* `nudgeWarning` and docs/specs/session-nudge/spec.md. */
|
|
18
|
+
nudgeAttempt?: number;
|
|
19
|
+
/** The nudgeable `CrashReason` kind that triggered the current nudge. */
|
|
20
|
+
nudgeReason?: CrashReason["kind"];
|
|
21
|
+
/** Latest agent-reported step count, when the runner reports progress —
|
|
22
|
+
* see `agentStallWarning` and docs/specs/agent-liveness/spec.md. */
|
|
23
|
+
agentStepCount?: number;
|
|
24
|
+
/** Milliseconds idle as of the last heartbeat — see `agentStallWarning`. */
|
|
25
|
+
agentIdleMs?: number;
|
|
26
|
+
} | null;
|
|
27
|
+
crash?: { attempt: number; reason: CrashReason };
|
|
28
|
+
visits: Record<string, number>;
|
|
29
|
+
runtimeMs: number | null;
|
|
30
|
+
hasBegin: boolean;
|
|
31
|
+
hasEnd: boolean;
|
|
32
|
+
seed?: NodeResult;
|
|
33
|
+
finalResult?: NodeResult;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Fold a journal's event stream into the one-shot digest `status` prints and
|
|
37
|
+
// `monitor` builds on. `visits` counts distinct visits per node: a visit
|
|
38
|
+
// begins at a `running` event with attempt 1 (retries reuse the same visit, so
|
|
39
|
+
// a node's attempts > 1 does not add a visit). `hasBegin`/`hasEnd` are read
|
|
40
|
+
// from the first/last lines — the journal's own DONE/FAILED-tagged markers.
|
|
41
|
+
export function foldJournal(journalText: string): JournalDigest {
|
|
42
|
+
const events = parseEventLog(journalText);
|
|
43
|
+
const digest: JournalDigest = {
|
|
44
|
+
current: null,
|
|
45
|
+
visits: {},
|
|
46
|
+
runtimeMs: null,
|
|
47
|
+
hasBegin: false,
|
|
48
|
+
hasEnd: false,
|
|
49
|
+
};
|
|
50
|
+
if (events.length === 0) return digest;
|
|
51
|
+
|
|
52
|
+
const first = events[0]!;
|
|
53
|
+
const last = events.at(-1)!;
|
|
54
|
+
|
|
55
|
+
if (first.state === DONE && first.status.phase === "succeeded") {
|
|
56
|
+
digest.hasBegin = true;
|
|
57
|
+
digest.seed = first.status.result;
|
|
58
|
+
}
|
|
59
|
+
// Both terminal gradings end the run — a failed run's `hasEnd` must stay
|
|
60
|
+
// true, or it reads as indistinguishable from one still in progress.
|
|
61
|
+
if (last.state === DONE && last.status.phase === "succeeded") {
|
|
62
|
+
digest.hasEnd = true;
|
|
63
|
+
digest.finalResult = last.status.result;
|
|
64
|
+
}
|
|
65
|
+
if (last.state === FAILED && last.status.phase === "failed") {
|
|
66
|
+
digest.hasEnd = true;
|
|
67
|
+
digest.finalResult = last.status.result;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const lastStatus = last.status;
|
|
71
|
+
digest.current = {
|
|
72
|
+
state: last.state,
|
|
73
|
+
phase: lastStatus.phase,
|
|
74
|
+
...(lastStatus.phase === "running"
|
|
75
|
+
? {
|
|
76
|
+
startedAt: lastStatus.startedAt,
|
|
77
|
+
lastHeartbeat: lastStatus.lastHeartbeat,
|
|
78
|
+
agentSessionId: lastStatus.agentSessionId,
|
|
79
|
+
nudgeAttempt: lastStatus.nudgeAttempt,
|
|
80
|
+
nudgeReason: lastStatus.nudgeReason,
|
|
81
|
+
agentStepCount: lastStatus.agentStepCount,
|
|
82
|
+
agentIdleMs: lastStatus.agentIdleMs,
|
|
83
|
+
}
|
|
84
|
+
: {}),
|
|
85
|
+
};
|
|
86
|
+
if (last.status.phase === "crashed") {
|
|
87
|
+
digest.crash = { attempt: last.status.attempt, reason: last.status.reason };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Visit counting with heartbeat tolerance: a visit begins when a
|
|
91
|
+
// (state, attempt, scope) dispatch OPENS — heartbeat events repeat the same
|
|
92
|
+
// key while it stays open and must not inflate the count.
|
|
93
|
+
const open = new Set<string>();
|
|
94
|
+
for (const e of events) {
|
|
95
|
+
if (e.state === DONE || e.state === FAILED) continue;
|
|
96
|
+
const st = e.status;
|
|
97
|
+
if (st.phase !== "running") continue;
|
|
98
|
+
const key = `${e.state}|${st.attempt}|${(e.scopePath ?? []).join(">")}`;
|
|
99
|
+
if (open.has(key)) continue;
|
|
100
|
+
open.add(key);
|
|
101
|
+
if (st.attempt === 1) {
|
|
102
|
+
digest.visits[e.state] = (digest.visits[e.state] ?? 0) + 1;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
digest.runtimeMs = last.timestamp - first.timestamp;
|
|
107
|
+
return digest;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export type TerminalState =
|
|
111
|
+
| "succeeded"
|
|
112
|
+
| "failed"
|
|
113
|
+
| "crashed"
|
|
114
|
+
| "orphaned"
|
|
115
|
+
| "host-died"
|
|
116
|
+
| "failed-start"
|
|
117
|
+
| "running";
|
|
118
|
+
|
|
119
|
+
// The terminal-state classifier. The journal's last line is authoritative for
|
|
120
|
+
// succeeded/crashed; only when the journal shows no terminal marker does the
|
|
121
|
+
// PID decide (alive -> running, dead -> orphaned/host-died). The registry's
|
|
122
|
+
// own status/finishedAt are a best-effort cache that is never the authority,
|
|
123
|
+
// so they are deliberately not an input here — a stale "running" can never
|
|
124
|
+
// mask a journal-proven success into a false orphan. (Reading the registry to
|
|
125
|
+
// get the entry's pid and journal path is the resolution layer's job, in
|
|
126
|
+
// registry.ts.)
|
|
127
|
+
//
|
|
128
|
+
// `cachedStatus === "failed-start"` is the one deliberate exception to that
|
|
129
|
+
// rule: a failed-start run never gets a journal at all (the worker died
|
|
130
|
+
// before `run()` was ever called), so there is no journal-proven outcome
|
|
131
|
+
// this could ever mask — the registry is the only record that will ever
|
|
132
|
+
// exist. See docs/specs/_built/registry-lifecycle/spec.md.
|
|
133
|
+
export function classifyTerminal(
|
|
134
|
+
journalText: string,
|
|
135
|
+
pidAlive: boolean,
|
|
136
|
+
opts?: { cachedStatus?: RegistryStatus; hostDied?: boolean },
|
|
137
|
+
): TerminalState {
|
|
138
|
+
if (opts?.cachedStatus === "failed-start") return "failed-start";
|
|
139
|
+
const outcome = journalTerminal(journalText);
|
|
140
|
+
if (outcome) return outcome;
|
|
141
|
+
if (pidAlive) return "running";
|
|
142
|
+
if (opts?.hostDied) return "host-died";
|
|
143
|
+
return "orphaned";
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// --- run report ---
|
|
147
|
+
|
|
148
|
+
export interface ReportDispatch {
|
|
149
|
+
state: string;
|
|
150
|
+
durationMs: number;
|
|
151
|
+
phase: "succeeded" | "failed" | "crashed";
|
|
152
|
+
event?: string;
|
|
153
|
+
reason?: unknown;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export interface ReportDigest {
|
|
157
|
+
spanMs: number;
|
|
158
|
+
dispatches: ReportDispatch[];
|
|
159
|
+
visits: Record<string, number>;
|
|
160
|
+
crashReasons: string[];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Per-dispatch fold for `darkf report`: one row per running→terminal pair
|
|
164
|
+
* (attempt-aware), plus the visit histogram and crash-reason summary. This is
|
|
165
|
+
* the efficacy/efficiency view the one-shot status digest deliberately is not. */
|
|
166
|
+
export function foldReport(journalText: string): ReportDigest {
|
|
167
|
+
const events = parseEventLog(journalText);
|
|
168
|
+
const digest: ReportDigest = { spanMs: 0, dispatches: [], visits: {}, crashReasons: [] };
|
|
169
|
+
if (events.length === 0) return digest;
|
|
170
|
+
|
|
171
|
+
digest.spanMs = events.at(-1)!.timestamp - events[0]!.timestamp;
|
|
172
|
+
const openAt = new Map<string, number>();
|
|
173
|
+
for (const e of events) {
|
|
174
|
+
// DONE's begin/end markers are skipped — neither is a real node's
|
|
175
|
+
// dispatch. FAILED's end marker is NOT skipped: it is `run_failure`'s
|
|
176
|
+
// own terminal grading, with no preceding "running" open of its own (see
|
|
177
|
+
// the `phase === "failed"` branch below), and it's the one piece of
|
|
178
|
+
// evidence that the run ended in failure — dropping it here would
|
|
179
|
+
// silently drop it from the report entirely.
|
|
180
|
+
if (e.state === DONE) continue;
|
|
181
|
+
const st = e.status;
|
|
182
|
+
// Terminal events do not repeat the attempt field — normalize to 1.
|
|
183
|
+
const attempt = st.phase === "running" ? st.attempt : 1;
|
|
184
|
+
const key = `${e.state}|${attempt}|${(e.scopePath ?? []).join(">")}`;
|
|
185
|
+
// Heartbeat tolerance: a running event for an already-open key is a
|
|
186
|
+
// heartbeat — it must not re-open (reset the duration start) or inflate
|
|
187
|
+
// the visit histogram.
|
|
188
|
+
if (st.phase === "running" && openAt.has(key)) continue;
|
|
189
|
+
if (st.phase === "running") {
|
|
190
|
+
// a dispatch opens (first attempt or retry): count attempt-1 opens as
|
|
191
|
+
// visits and start the duration clock
|
|
192
|
+
if (attempt === 1) digest.visits[e.state] = (digest.visits[e.state] ?? 0) + 1;
|
|
193
|
+
openAt.set(key, e.timestamp);
|
|
194
|
+
} else if (st.phase === "succeeded") {
|
|
195
|
+
const started = openAt.get(key);
|
|
196
|
+
if (started === undefined) continue;
|
|
197
|
+
openAt.delete(key);
|
|
198
|
+
digest.dispatches.push({
|
|
199
|
+
state: e.state,
|
|
200
|
+
durationMs: e.timestamp - started,
|
|
201
|
+
phase: "succeeded",
|
|
202
|
+
event: st.result?.event,
|
|
203
|
+
});
|
|
204
|
+
} else if (st.phase === "crashed") {
|
|
205
|
+
const started = openAt.get(key);
|
|
206
|
+
if (started === undefined) continue;
|
|
207
|
+
openAt.delete(key);
|
|
208
|
+
digest.dispatches.push({
|
|
209
|
+
state: e.state,
|
|
210
|
+
durationMs: e.timestamp - started,
|
|
211
|
+
phase: "crashed",
|
|
212
|
+
reason: st.reason,
|
|
213
|
+
});
|
|
214
|
+
digest.crashReasons.push(JSON.stringify(st.reason));
|
|
215
|
+
} else if (st.phase === "failed") {
|
|
216
|
+
// The FAILED sentinel is never dispatched (no "running" open of its
|
|
217
|
+
// own to pair with), unlike every other row in this fold — report it
|
|
218
|
+
// as a zero-duration bookkeeping entry, the terminal grading itself
|
|
219
|
+
// rather than a timed dispatch.
|
|
220
|
+
digest.dispatches.push({
|
|
221
|
+
state: e.state,
|
|
222
|
+
durationMs: 0,
|
|
223
|
+
phase: "failed",
|
|
224
|
+
event: st.result?.event,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return digest;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// --- stall detection (warn-only; the supervisor owns the reaction) ---
|
|
232
|
+
|
|
233
|
+
export const STALL_AFTER_MS = 3 * 30_000;
|
|
234
|
+
|
|
235
|
+
/** Warn-only stall check for an open dispatch: heartbeats older than
|
|
236
|
+
* STALL_AFTER_MS mean the runner stopped proving liveness. Never acts —
|
|
237
|
+
* informing the supervisor is the whole contract. */
|
|
238
|
+
export function stallWarning(digest: JournalDigest, now: number = Date.now()): string | null {
|
|
239
|
+
const hb = digest.current?.lastHeartbeat;
|
|
240
|
+
if (digest.current?.phase !== "running" || hb === undefined) return null;
|
|
241
|
+
const silentFor = now - hb;
|
|
242
|
+
if (silentFor <= STALL_AFTER_MS) return null;
|
|
243
|
+
return `stall suspected: no heartbeat for ${Math.round(silentFor / 1000)}s (state: ${digest.current.state}) — supervisor decides`;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Placeholder default, not yet measured against real runs — see
|
|
247
|
+
// docs/specs/agent-liveness/spec.md's open questions (a long tool-execution
|
|
248
|
+
// gap, e.g. a 10-minute build, looks identical to a hung provider; the
|
|
249
|
+
// warning's step count is what lets the supervisor tell them apart).
|
|
250
|
+
export const AGENT_STALL_MS = 15 * 60_000;
|
|
251
|
+
|
|
252
|
+
/** Warn-only agent-level stall check: distinct from `stallWarning` (engine
|
|
253
|
+
* heartbeat liveness, proves only that the engine loop is alive) — this
|
|
254
|
+
* watches whether the AGENT is making step progress. A hung LLM backend
|
|
255
|
+
* still produces healthy engine heartbeats; this is what catches that.
|
|
256
|
+
* Absent `agentIdleMs` (script dispatch, or a runner that doesn't report
|
|
257
|
+
* progress) never warns — there's no evidence either way. Never acts. */
|
|
258
|
+
export function agentStallWarning(digest: JournalDigest, now: number = Date.now()): string | null {
|
|
259
|
+
const hb = digest.current?.lastHeartbeat;
|
|
260
|
+
const idleAsOfHeartbeat = digest.current?.agentIdleMs;
|
|
261
|
+
if (digest.current?.phase !== "running" || hb === undefined || idleAsOfHeartbeat === undefined) {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
const liveIdleMs = idleAsOfHeartbeat + Math.max(0, now - hb);
|
|
265
|
+
if (liveIdleMs <= AGENT_STALL_MS) return null;
|
|
266
|
+
const steps = digest.current.agentStepCount ?? 0;
|
|
267
|
+
return `agent stall suspected: no step event for ${Math.round(liveIdleMs / 60_000)}m (state: ${digest.current.state}, steps so far: ${steps}) — supervisor decides`;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Warn-only nudge-in-progress note, same contract as `stallWarning`: a
|
|
271
|
+
* nudge is already visible the instant the engine issues it
|
|
272
|
+
* (`nudgeAttempt`/`nudgeReason` are set then, not gated behind a
|
|
273
|
+
* heartbeat) — this is purely a display string, never acts.
|
|
274
|
+
* "3" mirrors darkf-engine's own fixed nudge budget (run.ts); kept as a
|
|
275
|
+
* literal here rather than imported, same as this module already treats
|
|
276
|
+
* other engine constants (e.g. `STALL_AFTER_MS`) as display-only
|
|
277
|
+
* knowledge, not a shared dependency. */
|
|
278
|
+
export function nudgeWarning(digest: JournalDigest): string | null {
|
|
279
|
+
const { nudgeAttempt, nudgeReason } = digest.current ?? {};
|
|
280
|
+
if (digest.current?.phase !== "running" || nudgeAttempt === undefined) return null;
|
|
281
|
+
return `nudging attempt ${nudgeAttempt}/3 (${nudgeReason}) — supervisor decides`;
|
|
282
|
+
}
|