recallnest 2.5.4 → 2.6.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.
Files changed (81) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +34 -9
  3. package/README_CN.md +37 -12
  4. package/package.json +4 -4
  5. package/src/__tests__/access-tracker-novelty.test.ts +66 -0
  6. package/src/__tests__/api-server-security.test.ts +102 -0
  7. package/src/__tests__/belief-history.test.ts +220 -0
  8. package/src/__tests__/capture-engine.test.ts +93 -3
  9. package/src/__tests__/category-min-scores.test.ts +83 -0
  10. package/src/__tests__/cluster-consolidation.test.ts +167 -4
  11. package/src/__tests__/conflict-store.test.ts +46 -2
  12. package/src/__tests__/consolidation-engine.test.ts +97 -0
  13. package/src/__tests__/contracts.test.ts +46 -0
  14. package/src/__tests__/decay-exemption.test.ts +45 -0
  15. package/src/__tests__/dream-pipeline.test.ts +464 -5
  16. package/src/__tests__/freshness.test.ts +186 -0
  17. package/src/__tests__/hotness-blend.test.ts +207 -0
  18. package/src/__tests__/llm-client-pivot.test.ts +628 -0
  19. package/src/__tests__/maintenance-locks.test.ts +15 -0
  20. package/src/__tests__/memory-boundaries.test.ts +32 -0
  21. package/src/__tests__/noise-prototype-bank.test.ts +280 -0
  22. package/src/__tests__/pii-redact.test.ts +19 -0
  23. package/src/__tests__/pivot-apply.test.ts +373 -0
  24. package/src/__tests__/pivot-distill-supervisor.test.ts +457 -0
  25. package/src/__tests__/pivot-distill.test.ts +1178 -0
  26. package/src/__tests__/preference-matcher.test.ts +54 -2
  27. package/src/__tests__/pull-from-macbook-sync-only.test.ts +679 -0
  28. package/src/__tests__/recall-gate.test.ts +266 -0
  29. package/src/__tests__/resolve-db-path.test.ts +130 -0
  30. package/src/__tests__/resolve-tier.test.ts +64 -0
  31. package/src/__tests__/retrieval-eval.test.ts +163 -0
  32. package/src/__tests__/retriever-lazy-init-dispatch.test.ts +123 -0
  33. package/src/__tests__/skill-promotion.test.ts +53 -0
  34. package/src/__tests__/stable-context-length-contract.test.ts +62 -0
  35. package/src/__tests__/store-read-consistency.test.ts +122 -0
  36. package/src/__tests__/store-schema-migration.test.ts +87 -0
  37. package/src/__tests__/store-upsert.test.ts +17 -0
  38. package/src/__tests__/term-registry-terminal-roster.test.ts +127 -0
  39. package/src/__tests__/ui-server-security.test.ts +93 -0
  40. package/src/access-tracker.ts +53 -3
  41. package/src/adaptive-retrieval.ts +11 -0
  42. package/src/api-server.ts +9 -2
  43. package/src/auto-recall.ts +61 -1
  44. package/src/belief-history.ts +164 -0
  45. package/src/capture-engine.ts +88 -11
  46. package/src/cli.ts +109 -14
  47. package/src/conflict-engine.ts +24 -3
  48. package/src/consolidation-engine.ts +146 -12
  49. package/src/context-composer-stable-selection.ts +7 -1
  50. package/src/data-checkup.ts +1 -1
  51. package/src/decay-engine.ts +59 -7
  52. package/src/doctor.ts +4 -2
  53. package/src/dream-pipeline.ts +359 -11
  54. package/src/env-config.ts +41 -0
  55. package/src/eval.ts +154 -11
  56. package/src/freshness.ts +192 -0
  57. package/src/http-request-policy.ts +148 -0
  58. package/src/ingest.ts +277 -2
  59. package/src/kg-extractor.ts +7 -1
  60. package/src/llm-client.ts +106 -11
  61. package/src/mcp-server.ts +2 -1
  62. package/src/mcp-tools-advanced.ts +13 -0
  63. package/src/mcp-tools-core.ts +5 -1
  64. package/src/memory-boundaries.ts +23 -2
  65. package/src/memory-health-rebalance.ts +4 -0
  66. package/src/memory-output.ts +16 -2
  67. package/src/memory-store-port.ts +3 -0
  68. package/src/noise-prototype-bank.ts +408 -0
  69. package/src/pii-detector.ts +52 -2
  70. package/src/pivot-apply.ts +399 -0
  71. package/src/preference-matcher.ts +37 -3
  72. package/src/recall-gate.ts +305 -0
  73. package/src/retrieval-profiles.ts +46 -4
  74. package/src/retriever.ts +95 -18
  75. package/src/retroactive-boost.ts +6 -2
  76. package/src/runtime-config.ts +70 -5
  77. package/src/skill-promotion.ts +58 -2
  78. package/src/store.ts +54 -19
  79. package/src/term-registry.ts +35 -0
  80. package/src/ui-server.ts +7 -0
  81. package/src/version.ts +19 -0
@@ -0,0 +1,457 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import {
3
+ chmodSync,
4
+ existsSync,
5
+ mkdtempSync,
6
+ readFileSync,
7
+ statSync,
8
+ writeFileSync,
9
+ } from "node:fs";
10
+ import { tmpdir } from "node:os";
11
+ import { join, resolve } from "node:path";
12
+ import { spawn, spawnSync } from "node:child_process";
13
+
14
+ import {
15
+ SUPERVISED_TASKS,
16
+ tasksForMode,
17
+ type SupervisorMode,
18
+ } from "../../scripts/pivot-distill-supervisor.js";
19
+
20
+ const BUN = Bun.which("bun")!;
21
+ const REPO_ROOT = resolve(import.meta.dir, "../..");
22
+ const SUPERVISOR = join(REPO_ROOT, "scripts", "pivot-distill-supervisor.ts");
23
+ const LOADED_LABELS = [
24
+ "com.recallnest.incremental-ingest",
25
+ "com.recallnest.pull-from-macbook",
26
+ ] as const;
27
+ const PRIOR_ABSENT_LABEL = "com.anxianjingya.agy-conversations-sync";
28
+
29
+ interface FakeHarness {
30
+ root: string;
31
+ stateDir: string;
32
+ launchState: string;
33
+ launchLog: string;
34
+ probeLog: string;
35
+ childMarker: string;
36
+ launchctl: string;
37
+ pgrep: string;
38
+ childScript: string;
39
+ env: NodeJS.ProcessEnv;
40
+ }
41
+
42
+ function executable(path: string, source: string): void {
43
+ writeFileSync(path, source, { mode: 0o700 });
44
+ chmodSync(path, 0o700);
45
+ }
46
+
47
+ function fakeHarness(loaded: readonly string[] = LOADED_LABELS): FakeHarness {
48
+ const root = mkdtempSync(join(tmpdir(), "pivot-supervisor-"));
49
+ const stateDir = join(root, "controller");
50
+ const launchState = join(root, "launch-state.json");
51
+ const launchLog = join(root, "launchctl.log");
52
+ const probeLog = join(root, "pgrep.log");
53
+ const childMarker = join(root, "child-started");
54
+ const launchctl = join(root, "fake-launchctl.ts");
55
+ const pgrep = join(root, "fake-pgrep.ts");
56
+ const childScript = join(root, "fake-child.ts");
57
+ writeFileSync(launchState, JSON.stringify([...loaded]));
58
+
59
+ executable(launchctl, `#!/usr/bin/env bun
60
+ import { appendFileSync, readFileSync, writeFileSync } from "node:fs";
61
+ import { basename } from "node:path";
62
+ const statePath = process.env.FAKE_LAUNCH_STATE!;
63
+ const logPath = process.env.FAKE_LAUNCH_LOG!;
64
+ const [operation, ...args] = process.argv.slice(2);
65
+ let loaded = JSON.parse(readFileSync(statePath, "utf8")) as string[];
66
+ const targetLabel = (target: string | undefined): string => (target || "").split("/").at(-1) || "";
67
+ let label = operation === "bootstrap"
68
+ ? basename(args[1] || "", ".plist")
69
+ : targetLabel(args[0]);
70
+ appendFileSync(logPath, operation + "\\t" + label + "\\n");
71
+ if (operation === "print") {
72
+ if (process.env.FAKE_PRINT_ERROR_LABEL === label) process.exit(64);
73
+ if (
74
+ process.env.FAKE_PRINT_ERROR_AFTER_BOOTSTRAP_LABEL === label &&
75
+ readFileSync(logPath, "utf8").includes("bootstrap\\t" + label + "\\n")
76
+ ) process.exit(65);
77
+ if (loaded.includes(label)) process.exit(0);
78
+ console.error('Could not find service "' + label + '" in domain for user gui: 501');
79
+ process.exit(113);
80
+ }
81
+ if (operation === "bootout") {
82
+ if (!loaded.includes(label)) process.exit(3);
83
+ loaded = loaded.filter((entry) => entry !== label);
84
+ writeFileSync(statePath, JSON.stringify(loaded));
85
+ process.exit(0);
86
+ }
87
+ if (operation === "bootstrap") {
88
+ if (!loaded.includes(label)) loaded.push(label);
89
+ writeFileSync(statePath, JSON.stringify(loaded));
90
+ process.exit(0);
91
+ }
92
+ process.exit(64);
93
+ `);
94
+
95
+ executable(pgrep, `#!/usr/bin/env bun
96
+ import { appendFileSync } from "node:fs";
97
+ const args = process.argv.slice(2);
98
+ if (args.length !== 2 || args[0] !== "-f") process.exit(64);
99
+ const pattern = args[1] || "";
100
+ appendFileSync(process.env.FAKE_PROBE_LOG!, args.join("\\t") + "\\n");
101
+ if (process.env.FAKE_ORPHAN === "1") {
102
+ console.log("4321 orphan-for-" + pattern);
103
+ process.exit(0);
104
+ }
105
+ process.exit(1);
106
+ `);
107
+
108
+ executable(childScript, `#!/usr/bin/env bun
109
+ import { writeFileSync } from "node:fs";
110
+ const [behavior, argument] = process.argv.slice(2);
111
+ writeFileSync(process.env.FAKE_CHILD_MARKER!, behavior || "unknown");
112
+ if (behavior === "estimate") {
113
+ const fakeHash = process.env.FAKE_EMPTY_HASHES === "1" ? "" : "b".repeat(64);
114
+ const summary = {
115
+ failedSessions: Number(process.env.FAKE_FAILED_SESSIONS || 0),
116
+ changedSessions: Number(process.env.FAKE_CHANGED_SESSIONS || 0),
117
+ frozenBundle: { bundleHash: fakeHash, requestProfileHash: fakeHash },
118
+ };
119
+ writeFileSync(argument, JSON.stringify({ summary }));
120
+ console.log(JSON.stringify(summary));
121
+ process.exit(0);
122
+ }
123
+ if (behavior === "fail") process.exit(Number(argument || 7));
124
+ if (behavior === "wait") setInterval(() => {}, 1_000);
125
+ console.log(JSON.stringify({ ok: true }));
126
+ `);
127
+
128
+ const home = join(root, "home");
129
+ const env: NodeJS.ProcessEnv = {
130
+ ...process.env,
131
+ PIVOT_SUPERVISOR_HOME: home,
132
+ PIVOT_SUPERVISOR_LAUNCHCTL: launchctl,
133
+ PIVOT_SUPERVISOR_PGREP: pgrep,
134
+ FAKE_LAUNCH_STATE: launchState,
135
+ FAKE_LAUNCH_LOG: launchLog,
136
+ FAKE_PROBE_LOG: probeLog,
137
+ FAKE_CHILD_MARKER: childMarker,
138
+ };
139
+ return {
140
+ root,
141
+ stateDir,
142
+ launchState,
143
+ launchLog,
144
+ probeLog,
145
+ childMarker,
146
+ launchctl,
147
+ pgrep,
148
+ childScript,
149
+ env,
150
+ };
151
+ }
152
+
153
+ function supervisorArgs(
154
+ harness: FakeHarness,
155
+ mode: SupervisorMode,
156
+ childArgs: string[],
157
+ supervisorFlags: string[] = [],
158
+ ): string[] {
159
+ return [
160
+ SUPERVISOR,
161
+ "--mode",
162
+ mode,
163
+ "--state-dir",
164
+ harness.stateDir,
165
+ ...supervisorFlags,
166
+ "--",
167
+ BUN,
168
+ harness.childScript,
169
+ ...childArgs,
170
+ ];
171
+ }
172
+
173
+ function run(
174
+ harness: FakeHarness,
175
+ mode: SupervisorMode,
176
+ childArgs: string[],
177
+ supervisorFlags: string[] = [],
178
+ extraEnv: NodeJS.ProcessEnv = {},
179
+ ) {
180
+ return spawnSync(BUN, supervisorArgs(harness, mode, childArgs, supervisorFlags), {
181
+ env: { ...harness.env, ...extraEnv },
182
+ encoding: "utf8",
183
+ timeout: 15_000,
184
+ });
185
+ }
186
+
187
+ function launchLogLines(harness: FakeHarness): string[] {
188
+ if (!existsSync(harness.launchLog)) return [];
189
+ return readFileSync(harness.launchLog, "utf8").trim().split("\n").filter(Boolean);
190
+ }
191
+
192
+ function loadedAfter(harness: FakeHarness): string[] {
193
+ return JSON.parse(readFileSync(harness.launchState, "utf8")) as string[];
194
+ }
195
+
196
+ function expectRestored(harness: FakeHarness): void {
197
+ const lines = launchLogLines(harness);
198
+ for (const label of LOADED_LABELS) {
199
+ const bootout = lines.indexOf(`bootout\t${label}`);
200
+ const bootstrap = lines.indexOf(`bootstrap\t${label}`);
201
+ expect(bootout).toBeGreaterThanOrEqual(0);
202
+ expect(bootstrap).toBeGreaterThan(bootout);
203
+ expect(lines.slice(bootstrap + 1)).toContain(`print\t${label}`);
204
+ }
205
+ expect(lines).not.toContain(`bootstrap\t${PRIOR_ABSENT_LABEL}`);
206
+ expect(loadedAfter(harness).sort()).toEqual([...LOADED_LABELS].sort());
207
+ const statePath = join(harness.stateDir, "prior-state.json");
208
+ const state = JSON.parse(readFileSync(statePath, "utf8")) as { status: string };
209
+ expect(state.status).toBe("restored");
210
+ expect(statSync(harness.stateDir).mode & 0o777).toBe(0o700);
211
+ expect(statSync(statePath).mode & 0o777).toBe(0o600);
212
+ }
213
+
214
+ async function waitForFile(path: string, timeoutMs = 5_000): Promise<void> {
215
+ const deadline = Date.now() + timeoutMs;
216
+ while (!existsSync(path)) {
217
+ if (Date.now() >= deadline) throw new Error(`Timed out waiting for ${path}`);
218
+ await Bun.sleep(20);
219
+ }
220
+ }
221
+
222
+ describe("pivot distill LaunchAgent supervisor policy", () => {
223
+ it("derives all task selection from the exact ten-item inventory", () => {
224
+ expect(SUPERVISED_TASKS.map((task) => [task.label, task.tier])).toEqual([
225
+ ["com.recallnest.incremental-ingest", "required"],
226
+ ["com.recallnest.pull-from-macbook", "required"],
227
+ ["com.anxianjingya.agy-conversations-sync", "required"],
228
+ ["com.anxianjingya.macbook-mirror-pull", "required"],
229
+ ["com.anxianjingya.conversation-truth-refresh", "required"],
230
+ ["com.recallnest.dream-consolidation", "required"],
231
+ ["com.recallnest.dream-memory-weekly", "required"],
232
+ ["com.recallnest.weekly-distill", "required"],
233
+ ["com.recallnest.daily-optimize", "required"],
234
+ ["com.anxianjingya.repos-autopull", "recommended"],
235
+ ]);
236
+ expect(SUPERVISED_TASKS.every((task) => task.reason.length > 0 && task.processPatterns.length > 0)).toBe(true);
237
+ expect(SUPERVISED_TASKS.flatMap((task) => task.processPatterns)).toEqual(expect.arrayContaining([
238
+ "[a]gy-conversations-sync[.]sh",
239
+ "[a]ntigravity-brain-to-jsonl[.]py",
240
+ "[a]ntigravity-db-to-jsonl[.]py",
241
+ "[c]li[.]ts import",
242
+ "[r]efresh-conversation-truth[.]py",
243
+ "[r]ecallnest-optimize[.]js",
244
+ ]));
245
+ expect(tasksForMode("sync-only")).toHaveLength(9);
246
+ expect(tasksForMode("estimate")).toHaveLength(9);
247
+ expect(tasksForMode("transport")).toHaveLength(9);
248
+ expect(tasksForMode("full")).toHaveLength(10);
249
+ });
250
+
251
+ it("treats failedSessions in an exit-zero estimate as failure and restores prior state", () => {
252
+ const harness = fakeHarness();
253
+ const resultJson = join(harness.root, "estimate.json");
254
+ const result = run(
255
+ harness,
256
+ "estimate",
257
+ ["estimate", resultJson],
258
+ ["--result-json", resultJson],
259
+ { FAKE_FAILED_SESSIONS: "2" },
260
+ );
261
+ expect(result.status).toBe(1);
262
+ expect(result.stderr).toContain("failedSessions=2");
263
+ expectRestored(harness);
264
+ });
265
+
266
+ it("treats changedSessions in an exit-zero estimate as failure and restores prior state", () => {
267
+ const harness = fakeHarness();
268
+ const resultJson = join(harness.root, "estimate.json");
269
+ const result = run(
270
+ harness,
271
+ "estimate",
272
+ ["estimate", resultJson],
273
+ ["--result-json", resultJson],
274
+ { FAKE_CHANGED_SESSIONS: "1" },
275
+ );
276
+ expect(result.status).toBe(1);
277
+ expect(result.stderr).toContain("changedSessions=1");
278
+ expectRestored(harness);
279
+ });
280
+
281
+ it("rejects negative counters and empty hashes in an exit-zero estimate", () => {
282
+ const negative = fakeHarness();
283
+ const negativeJson = join(negative.root, "estimate.json");
284
+ const negativeResult = run(
285
+ negative,
286
+ "estimate",
287
+ ["estimate", negativeJson],
288
+ ["--result-json", negativeJson],
289
+ { FAKE_FAILED_SESSIONS: "-1" },
290
+ );
291
+ expect(negativeResult.status).toBe(1);
292
+ expect(negativeResult.stderr).toContain("failedSessions=-1");
293
+ expectRestored(negative);
294
+
295
+ const emptyHash = fakeHarness();
296
+ const emptyHashJson = join(emptyHash.root, "estimate.json");
297
+ const emptyHashResult = run(
298
+ emptyHash,
299
+ "estimate",
300
+ ["estimate", emptyHashJson],
301
+ ["--result-json", emptyHashJson],
302
+ { FAKE_EMPTY_HASHES: "1" },
303
+ );
304
+ expect(emptyHashResult.status).toBe(1);
305
+ expect(emptyHashResult.stderr).toContain("missing bundleHash/requestProfileHash");
306
+ expectRestored(emptyHash);
307
+ });
308
+
309
+ it("accepts only a zero-failure estimate with a frozen bundle, then restores prior state", () => {
310
+ const harness = fakeHarness();
311
+ const resultJson = join(harness.root, "estimate.json");
312
+ const result = run(
313
+ harness,
314
+ "estimate",
315
+ ["estimate", resultJson],
316
+ ["--result-json", resultJson],
317
+ );
318
+ expect(result.status).toBe(0);
319
+ expectRestored(harness);
320
+ });
321
+
322
+ it("restores after an authorized transport child fails", () => {
323
+ const harness = fakeHarness();
324
+ const result = run(harness, "transport", ["fail", "7"], ["--allow-external-llm"]);
325
+ expect(result.status).toBe(1);
326
+ expect(result.stderr).toContain("child command failed (exit 7)");
327
+ expectRestored(harness);
328
+ });
329
+
330
+ it("runs full with both authorizations, includes recommended tasks, and restores", () => {
331
+ const harness = fakeHarness();
332
+ const result = run(
333
+ harness,
334
+ "full",
335
+ ["ok"],
336
+ ["--allow-external-llm", "--allow-full-run"],
337
+ );
338
+ expect(result.status).toBe(0);
339
+ expect(launchLogLines(harness)).toContain("print\tcom.anxianjingya.repos-autopull");
340
+ expect(readFileSync(harness.probeLog, "utf8")).toContain("-f\t[r]epos-autopull");
341
+ expectRestored(harness);
342
+ });
343
+
344
+ it("terminates the child, restores, and exits 143 on SIGTERM", async () => {
345
+ const harness = fakeHarness();
346
+ const child = spawn(BUN, supervisorArgs(harness, "sync-only", ["wait"]), {
347
+ env: harness.env,
348
+ stdio: ["ignore", "pipe", "pipe"],
349
+ });
350
+ await waitForFile(harness.childMarker);
351
+ child.kill("SIGTERM");
352
+ const exitCode = await new Promise<number | null>((resolveExit) => child.once("close", resolveExit));
353
+ expect(exitCode).toBe(143);
354
+ expectRestored(harness);
355
+ });
356
+
357
+ it("refuses missing transport authorization before any launchctl call", () => {
358
+ const harness = fakeHarness();
359
+ const result = run(harness, "transport", ["ok"]);
360
+ expect(result.status).toBe(1);
361
+ expect(result.stderr).toContain("transport refused");
362
+ expect(launchLogLines(harness)).toEqual([]);
363
+ expect(existsSync(harness.stateDir)).toBe(false);
364
+ });
365
+
366
+ it("fails closed on an operational launchctl print error before any mutation", () => {
367
+ const harness = fakeHarness();
368
+ const result = run(harness, "sync-only", ["ok"], [], {
369
+ FAKE_PRINT_ERROR_LABEL: "com.anxianjingya.agy-conversations-sync",
370
+ });
371
+ expect(result.status).toBe(1);
372
+ expect(result.stderr).toContain("launchctl print failed for com.anxianjingya.agy-conversations-sync (exit 64)");
373
+ expect(launchLogLines(harness).some((line) => line.startsWith("bootout\t"))).toBe(false);
374
+ expect(launchLogLines(harness).some((line) => line.startsWith("bootstrap\t"))).toBe(false);
375
+ expect(existsSync(harness.childMarker)).toBe(false);
376
+ });
377
+
378
+ it("continues restoring later tasks when one post-bootstrap print fails", () => {
379
+ const harness = fakeHarness();
380
+ const first = LOADED_LABELS[0];
381
+ const result = run(harness, "sync-only", ["ok"], [], {
382
+ FAKE_PRINT_ERROR_AFTER_BOOTSTRAP_LABEL: first,
383
+ });
384
+ expect(result.status).toBe(1);
385
+ expect(result.stderr).toContain(`launchctl print failed for ${first} (exit 65)`);
386
+ for (const label of LOADED_LABELS.slice(1)) {
387
+ expect(launchLogLines(harness)).toContain(`bootstrap\t${label}`);
388
+ }
389
+ expect(loadedAfter(harness).sort()).toEqual([...LOADED_LABELS].sort());
390
+ const state = JSON.parse(
391
+ readFileSync(join(harness.stateDir, "prior-state.json"), "utf8"),
392
+ ) as { status: string };
393
+ expect(state.status).toBe("restore-failed");
394
+ });
395
+
396
+ it("blocks on an orphan process without killing it, then restores", () => {
397
+ const harness = fakeHarness();
398
+ const result = run(harness, "sync-only", ["ok"], [], { FAKE_ORPHAN: "1" });
399
+ expect(result.status).toBe(1);
400
+ expect(result.stderr).toContain("blocked: orphan process remains");
401
+ expect(existsSync(harness.childMarker)).toBe(false);
402
+ expectRestored(harness);
403
+ });
404
+
405
+ it("does not overwrite an unrecovered prior-state record", () => {
406
+ const harness = fakeHarness();
407
+ const first = run(harness, "sync-only", ["fail", "9"]);
408
+ expect(first.status).toBe(1);
409
+ const statePath = join(harness.stateDir, "prior-state.json");
410
+ const state = JSON.parse(readFileSync(statePath, "utf8")) as Record<string, unknown>;
411
+ state.status = "active";
412
+ state.outcome = "simulated-crash";
413
+ writeFileSync(statePath, `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600 });
414
+ writeFileSync(harness.launchLog, "");
415
+
416
+ const second = run(harness, "sync-only", ["ok"]);
417
+ expect(second.status).toBe(1);
418
+ expect(second.stderr).toContain("run --recover first");
419
+ expect(launchLogLines(harness)).toEqual([]);
420
+ const preserved = JSON.parse(readFileSync(statePath, "utf8")) as Record<string, unknown>;
421
+ expect(preserved.outcome).toBe("simulated-crash");
422
+ });
423
+
424
+ it("--recover restores only prior-loaded tasks and keeps prior-absent tasks absent", () => {
425
+ const harness = fakeHarness();
426
+ const first = run(harness, "sync-only", ["fail", "9"]);
427
+ expect(first.status).toBe(1);
428
+ const statePath = join(harness.stateDir, "prior-state.json");
429
+ const state = JSON.parse(readFileSync(statePath, "utf8")) as Record<string, unknown>;
430
+ state.status = "active";
431
+ delete state.restoredAt;
432
+ writeFileSync(statePath, `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600 });
433
+ writeFileSync(harness.launchState, "[]");
434
+ writeFileSync(harness.launchLog, "");
435
+
436
+ const recovered = spawnSync(BUN, [
437
+ SUPERVISOR,
438
+ "--recover",
439
+ "--state-dir",
440
+ harness.stateDir,
441
+ ], {
442
+ env: harness.env,
443
+ encoding: "utf8",
444
+ timeout: 15_000,
445
+ });
446
+ expect(recovered.status).toBe(0);
447
+ const lines = launchLogLines(harness);
448
+ expect(lines.some((line) => line.startsWith("bootout\t"))).toBe(false);
449
+ for (const label of LOADED_LABELS) {
450
+ const bootstrap = lines.indexOf(`bootstrap\t${label}`);
451
+ expect(bootstrap).toBeGreaterThanOrEqual(0);
452
+ expect(lines.slice(bootstrap + 1)).toContain(`print\t${label}`);
453
+ }
454
+ expect(lines).not.toContain(`bootstrap\t${PRIOR_ABSENT_LABEL}`);
455
+ expect(loadedAfter(harness).sort()).toEqual([...LOADED_LABELS].sort());
456
+ });
457
+ });