@cueloop/daemon 0.1.0-alpha.9 → 0.1.0-alpha.90

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 (60) hide show
  1. package/package.json +39 -22
  2. package/src/api.test.ts +1060 -65
  3. package/src/api.ts +1398 -89
  4. package/src/capabilities.test.ts +57 -0
  5. package/src/capabilities.ts +89 -0
  6. package/src/client.test.ts +24 -0
  7. package/src/client.ts +669 -86
  8. package/src/curate.ts +109 -0
  9. package/src/daemon-version.test.ts +142 -0
  10. package/src/diff-hot-reload.test.ts +419 -0
  11. package/src/diff-watcher.ts +293 -0
  12. package/src/gh.test.ts +59 -0
  13. package/src/gh.ts +74 -0
  14. package/src/ghostty-thread-surface-store.test.ts +49 -0
  15. package/src/ghostty-thread-surface-store.ts +101 -0
  16. package/src/ghostty-thread-surface.test.ts +398 -0
  17. package/src/ghostty-thread-surface.ts +267 -0
  18. package/src/harness-delivery.test.ts +268 -0
  19. package/src/harness-state-store.test.ts +93 -0
  20. package/src/harness-state-store.ts +213 -0
  21. package/src/herdr-split.test.ts +106 -0
  22. package/src/herdr-split.ts +92 -0
  23. package/src/herdr-thread-surface-store.test.ts +81 -0
  24. package/src/herdr-thread-surface-store.ts +64 -0
  25. package/src/herdr-thread-surface.test.ts +411 -0
  26. package/src/herdr-thread-surface.ts +355 -0
  27. package/src/index.ts +8 -9
  28. package/src/interruptible-wait.test.ts +127 -0
  29. package/src/interruptible-wait.ts +66 -0
  30. package/src/jj-working-tree.ts +131 -0
  31. package/src/main.ts +4 -1
  32. package/src/paths.ts +49 -0
  33. package/src/pr-poller.test.ts +102 -0
  34. package/src/pr-poller.ts +73 -0
  35. package/src/project-files.test.ts +124 -0
  36. package/src/project-files.ts +56 -0
  37. package/src/protocol.ts +79 -7
  38. package/src/retention.test.ts +191 -0
  39. package/src/retention.ts +123 -0
  40. package/src/server.test.ts +453 -42
  41. package/src/server.ts +433 -88
  42. package/src/share-blob.test.ts +112 -0
  43. package/src/share-blob.ts +55 -0
  44. package/src/single-instance.test.ts +162 -13
  45. package/src/store.conformance.test.ts +41 -0
  46. package/src/store.jsonl.test.ts +159 -0
  47. package/src/store.ts +264 -34
  48. package/src/testing/store-conformance.ts +194 -0
  49. package/src/thread-review.test.ts +483 -0
  50. package/src/thread-review.ts +398 -0
  51. package/src/thread-surface-config.test.ts +63 -0
  52. package/src/thread-surface-config.ts +61 -0
  53. package/src/validate.test.ts +282 -37
  54. package/src/validate.ts +398 -33
  55. package/src/vcs-source.test.ts +180 -0
  56. package/src/vcs-source.ts +228 -0
  57. package/src/version.ts +6 -0
  58. package/src/working-tree.ts +149 -0
  59. package/src/review.test.ts +0 -167
  60. package/src/review.ts +0 -174
@@ -0,0 +1,483 @@
1
+ /** The shared review core against a real DaemonServer in a temp home: workspace resolution, title derivation, open-or-revise by agentSessionId, and both awaitMessage shapes (one long-poll and the chunked loop with progress and abort). */
2
+
3
+ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
4
+ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ import type { Thread } from "@cueloop/schema";
8
+ import { DaemonServer } from "./server";
9
+ import { DaemonClient } from "./client";
10
+ import { awaitResolve, openReview, resolveWorkspace } from "./thread-review";
11
+ import { VcsSourceManager } from "./vcs-source";
12
+
13
+ const PLAN = "# Rollout Plan\n\nShip it in two stages.\n";
14
+
15
+ let home: string;
16
+ let server: DaemonServer;
17
+ let client: DaemonClient;
18
+
19
+ beforeEach(async () => {
20
+ home = mkdtempSync(join(tmpdir(), "cueloop-review-core-"));
21
+ server = new DaemonServer({ home, idleExitMs: 0 });
22
+ server.start();
23
+ client = await DaemonClient.connect({ home });
24
+ });
25
+ afterEach(() => {
26
+ client.close();
27
+ server.stop();
28
+ rmSync(home, { recursive: true, force: true });
29
+ });
30
+
31
+ function sh(args: string[], cwd: string): void {
32
+ const gitResult = Bun.spawnSync(args, { cwd, stdout: "ignore", stderr: "ignore" });
33
+
34
+ if (gitResult.exitCode !== 0) throw new Error(`${args.join(" ")} failed`);
35
+ }
36
+
37
+ describe("resolveWorkspace", () => {
38
+ test("a git repo resolves to its root and branch", async () => {
39
+ const repo = mkdtempSync(join(tmpdir(), "cueloop-ws-"));
40
+
41
+ try {
42
+ // Arrange
43
+ sh(["git", "init", "-q", "-b", "main"], repo);
44
+ sh(["git", "config", "user.email", "t@t"], repo);
45
+ sh(["git", "config", "user.name", "t"], repo);
46
+ sh(["git", "commit", "-q", "--allow-empty", "-m", "init"], repo);
47
+
48
+ // Act
49
+ const ws = await resolveWorkspace(repo);
50
+
51
+ // Assert
52
+ expect(ws.branch).toBe("main");
53
+ // macOS tmpdirs can resolve through /private; compare the tail
54
+ expect(ws.repoRoot.endsWith(repo.split("/").at(-1)!)).toBe(true);
55
+ } finally {
56
+ rmSync(repo, { recursive: true, force: true });
57
+ }
58
+ });
59
+
60
+ test("outside a repo the cwd itself is the workspace, branch detached", async () => {
61
+ // Act
62
+ const ws = await resolveWorkspace(home);
63
+
64
+ // Assert
65
+ expect(ws).toEqual({ repoRoot: home, branch: "detached" });
66
+ });
67
+ });
68
+
69
+ describe("openReview", () => {
70
+ test("a submitted JJ diff follows the same change through a rewrite", async () => {
71
+ const repo = mkdtempSync(join(tmpdir(), "cueloop-review-jj-"));
72
+
73
+ try {
74
+ sh(["git", "init", "-q", "-b", "main"], repo);
75
+ sh(["git", "config", "user.email", "t@t"], repo);
76
+ sh(["git", "config", "user.name", "t"], repo);
77
+ writeFileSync(join(repo, "a.txt"), "before\n");
78
+ sh(["git", "add", "a.txt"], repo);
79
+ sh(["git", "commit", "-qm", "initial"], repo);
80
+ sh(["jj", "git", "init", "--colocate", repo], repo);
81
+ writeFileSync(join(repo, "a.txt"), "first\n");
82
+ const sources = new VcsSourceManager(join(home, "vcs-config.toml"));
83
+ const firstDiff = await sources.capture(repo, "jj");
84
+ const first = await openReview(client, {
85
+ type: "diff",
86
+ content: firstDiff.patch,
87
+ cwd: repo,
88
+ vcs: "jj",
89
+ agent: "codex",
90
+ agentSessionId: "one-agent-session",
91
+ });
92
+
93
+ writeFileSync(join(repo, "a.txt"), "second\n");
94
+ const secondDiff = await sources.capture(repo, "jj");
95
+ const second = await openReview(client, {
96
+ type: "diff",
97
+ content: secondDiff.patch,
98
+ cwd: repo,
99
+ vcs: "jj",
100
+ agent: "codex",
101
+ agentSessionId: "one-agent-session",
102
+ });
103
+
104
+ expect(first.session.artifact.meta.vcs).toBe("jj");
105
+ expect(first.session.artifact.files?.[0]?.newContents).toBe("first\n");
106
+ expect(first.session.artifact.meta.vcsChangeId).toBe(
107
+ second.session.artifact.meta.vcsChangeId,
108
+ );
109
+ expect(second.id).toBe(first.id);
110
+ expect(second.session.revisions.map((revision) => revision.content)).toEqual([
111
+ firstDiff.patch,
112
+ secondDiff.patch,
113
+ ]);
114
+ const unmatched = await openReview(client, {
115
+ type: "diff",
116
+ content: "diff --git a/elsewhere b/elsewhere\n",
117
+ cwd: repo,
118
+ vcs: "jj",
119
+ agent: "codex",
120
+ agentSessionId: "one-agent-session",
121
+ });
122
+
123
+ expect(unmatched.id).toBe(first.id);
124
+ expect(unmatched.session.artifact.files).toEqual([]);
125
+ expect(unmatched.session.artifact.meta.vcsRevisionId).toBeUndefined();
126
+ sh(["jj", "new", "-m", "next change"], repo);
127
+ writeFileSync(join(repo, "b.txt"), "third\n");
128
+ const thirdDiff = await sources.capture(repo, "jj");
129
+ const third = await openReview(client, {
130
+ type: "diff",
131
+ content: thirdDiff.patch,
132
+ cwd: repo,
133
+ vcs: "jj",
134
+ agent: "codex",
135
+ agentSessionId: "one-agent-session",
136
+ });
137
+
138
+ expect(third.id).not.toBe(first.id);
139
+ const unmatchedAfterTwoChanges = await openReview(client, {
140
+ type: "diff",
141
+ content: "diff --git a/unmatched b/unmatched\n",
142
+ cwd: repo,
143
+ vcs: "jj",
144
+ agent: "codex",
145
+ agentSessionId: "one-agent-session",
146
+ });
147
+
148
+ expect(unmatchedAfterTwoChanges.id).not.toBe(first.id);
149
+ expect(unmatchedAfterTwoChanges.id).not.toBe(third.id);
150
+ expect((await client.sessionGet(third.id)).artifact.meta.vcsChangeId).toBe(
151
+ third.session.artifact.meta.vcsChangeId,
152
+ );
153
+ } finally {
154
+ rmSync(repo, { recursive: true, force: true });
155
+ }
156
+ });
157
+
158
+ test("shapes the artifact: agent, derived title, cwd", async () => {
159
+ // Act
160
+ const review = await openReview(client, {
161
+ type: "plan",
162
+ content: PLAN,
163
+ cwd: home,
164
+ agent: "test-agent",
165
+ });
166
+
167
+ // Assert
168
+ const session = review.session;
169
+
170
+ expect(session.status).toBe("pending");
171
+ expect(session.artifact.type).toBe("plan");
172
+ expect(session.artifact.meta.agent).toBe("test-agent");
173
+ expect(session.artifact.meta.title).toBe("Rollout Plan");
174
+ expect(session.artifact.meta.cwd).toBe(home);
175
+ expect(session.workspace).toEqual({ repoRoot: home, branch: "detached" });
176
+ });
177
+
178
+ test("an explicit title wins; diffs never derive one from content", async () => {
179
+ // Act
180
+ const titled = await openReview(client, {
181
+ type: "plan",
182
+ content: PLAN,
183
+ cwd: home,
184
+ title: "Custom",
185
+ });
186
+
187
+ // Assert
188
+ expect(titled.session.artifact.meta.title).toBe("Custom");
189
+
190
+ // Act
191
+ const diff = await openReview(client, {
192
+ type: "diff",
193
+ content: "# not a heading\n",
194
+ cwd: home,
195
+ });
196
+
197
+ // Assert
198
+ expect(diff.session.artifact.meta.title).toBeUndefined();
199
+ });
200
+
201
+ test("a pre-resolved workspace skips git resolution", async () => {
202
+ // Arrange
203
+ const ws = { repoRoot: "/elsewhere", branch: "feature" };
204
+
205
+ // Act
206
+ const review = await openReview(client, {
207
+ type: "diff",
208
+ content: "d",
209
+ cwd: home,
210
+ workspace: ws,
211
+ });
212
+
213
+ // Assert
214
+ expect(review.session.workspace).toEqual(ws);
215
+ });
216
+
217
+ test("the same agentSessionId revises instead of opening a new session", async () => {
218
+ // Arrange
219
+ const first = await openReview(client, {
220
+ type: "plan",
221
+ content: PLAN,
222
+ cwd: home,
223
+ agentSessionId: "agent-1",
224
+ });
225
+
226
+ // Act
227
+ const second = await openReview(client, {
228
+ type: "plan",
229
+ content: PLAN + "\nStage three.\n",
230
+ cwd: home,
231
+ agentSessionId: "agent-1",
232
+ });
233
+
234
+ // Assert
235
+ expect(second.id).toBe(first.id);
236
+ expect(second.session.revisions.length).toBe(2);
237
+ expect(second.session.status).toBe("pending");
238
+
239
+ // Act
240
+ const other = await openReview(client, {
241
+ type: "plan",
242
+ content: PLAN,
243
+ cwd: home,
244
+ agentSessionId: "agent-2",
245
+ });
246
+
247
+ // Assert
248
+ expect(other.id).not.toBe(first.id);
249
+ });
250
+
251
+ test("the same agentSessionId with a different type opens a new session, never a type mismatch", async () => {
252
+ // Arrange - the agent session's plan review is open
253
+ const plan = await openReview(client, {
254
+ type: "plan",
255
+ content: PLAN,
256
+ cwd: home,
257
+ agentSessionId: "agent-cross",
258
+ });
259
+
260
+ // Act - the same agent session now submits a reply for review
261
+ const reply = await openReview(client, {
262
+ type: "reply",
263
+ content: "# Findings\n\nThe cache is fine.\n",
264
+ cwd: home,
265
+ agentSessionId: "agent-cross",
266
+ });
267
+
268
+ // Assert - a fresh reply session; the plan session keeps its type and history
269
+ expect(reply.id).not.toBe(plan.id);
270
+ expect(reply.session.artifact.type).toBe("reply");
271
+ expect(reply.session.revisions.length).toBe(1);
272
+ expect((await client.sessionGet(plan.id)).artifact.type).toBe("plan");
273
+ });
274
+
275
+ test("per-file notes land as note annotations anchored at the file path", async () => {
276
+ // Act
277
+ const review = await openReview(client, {
278
+ type: "diff",
279
+ content: "d",
280
+ cwd: home,
281
+ notes: [
282
+ { path: "src/a.ts", body: "Renames b and adds c." },
283
+ { path: "src/b.ts", body: "Swaps the line." },
284
+ ],
285
+ });
286
+
287
+ // Assert
288
+ const notes = review.session.annotations;
289
+
290
+ expect(notes.length).toBe(2);
291
+ expect(notes.every((annotation) => annotation.kind === "note")).toBe(true);
292
+ expect(notes.map((annotation) => annotation.anchor.quote)).toEqual(["src/a.ts", "src/b.ts"]);
293
+ expect(notes[0]!.body).toBe("Renames b and adds c.");
294
+ });
295
+ });
296
+
297
+ describe("awaitMessage: one long-poll (the hook shape)", () => {
298
+ test("times out to pending; a later wait collects the stored message", async () => {
299
+ // Arrange
300
+ const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
301
+
302
+ // Assert
303
+ expect(await review.awaitMessage({ timeoutMs: 50 })).toBe("pending");
304
+
305
+ // Act
306
+ await client.sessionSendMessage(review.id, "approved", "Fine.");
307
+ const message = await review.awaitMessage({ timeoutMs: 1_000 });
308
+
309
+ // Assert
310
+ expect(message).not.toBe("pending");
311
+ if (message === "pending") throw new Error("unreachable");
312
+ expect(message.allow).toBe(true);
313
+ expect(message.message.body).toContain("Fine.");
314
+ expect(message.session.message!.outcome).toBe("approved");
315
+ });
316
+
317
+ test("changes_requested maps to allow=false with feedback.md attached", async () => {
318
+ // Arrange
319
+ const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
320
+ const waiting = review.awaitMessage({ timeoutMs: 5_000 });
321
+
322
+ // Act
323
+ await client.sessionSendMessage(review.id, "changes_requested", "One stage only.");
324
+ const message = await waiting;
325
+
326
+ // Assert
327
+ if (message === "pending") throw new Error("expected a message");
328
+ expect(message.allow).toBe(false);
329
+ expect(message.message.body).toContain("# Review: changes requested");
330
+ expect(message.message.body).toContain("One stage only.");
331
+ });
332
+ });
333
+
334
+ describe("awaitMessage: chunked loop (the pi shape)", () => {
335
+ test("onProgress sees fresh sessions between chunks; the message ends the loop", async () => {
336
+ // Arrange
337
+ const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
338
+ const seen: Thread[] = [];
339
+ const waiting = review.awaitMessage({
340
+ timeoutMs: Infinity,
341
+ pollMs: 100,
342
+ onProgress: (progress) => seen.push(progress),
343
+ });
344
+
345
+ // Act
346
+ await client.sessionAnnotate(review.id, {
347
+ id: "a1",
348
+ kind: "comment",
349
+ anchor: { quote: "two stages", prefix: "in ", suffix: "." },
350
+ body: "Name them.",
351
+ });
352
+
353
+ // Assert
354
+ // give the loop a chunk to observe the annotation before resolving
355
+ for (let i = 0; i < 100 && !seen.some((snapshot) => snapshot.annotations.length === 1); i++)
356
+ await Bun.sleep(20);
357
+ expect(seen.some((snapshot) => snapshot.annotations.length === 1)).toBe(true);
358
+
359
+ // Act
360
+ await client.sessionSendMessage(review.id, "changes_requested", "Too vague.");
361
+ const message = await waiting;
362
+
363
+ // Assert
364
+ if (message === "pending") throw new Error("expected a message");
365
+ expect(message.allow).toBe(false);
366
+ expect(message.session.annotations.length).toBe(1);
367
+ }, 15_000);
368
+
369
+ test("abort surfaces as pending and leaves the session collectable", async () => {
370
+ // Arrange
371
+ const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
372
+ const controller = new AbortController();
373
+ const waiting = review.awaitMessage({
374
+ timeoutMs: Infinity,
375
+ pollMs: 100,
376
+ signal: controller.signal,
377
+ });
378
+
379
+ // Act
380
+ controller.abort();
381
+
382
+ // Assert
383
+ expect(await waiting).toBe("pending");
384
+ expect((await client.sessionGet(review.id)).status).toBe("pending");
385
+ });
386
+
387
+ test("a finite budget runs out to pending", async () => {
388
+ // Arrange
389
+ const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
390
+
391
+ // Assert
392
+ expect(await review.awaitMessage({ timeoutMs: 120, pollMs: 50 })).toBe("pending");
393
+ });
394
+ });
395
+
396
+ describe("awaitResolve: the adapter wake seam (session id only)", () => {
397
+ test("resolves to the outcome when the message lands during the wait", async () => {
398
+ // Arrange
399
+ const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
400
+ const waiting = awaitResolve(client, review.id, { pollMs: 100 });
401
+
402
+ // Act
403
+ await client.sessionSendMessage(review.id, "approved", "Ship it.");
404
+ const message = await waiting;
405
+
406
+ // Assert
407
+ expect(message).not.toBeNull();
408
+ expect(message!.allow).toBe(true);
409
+ expect(message!.message.body).toContain("Ship it.");
410
+ expect(message!.session.message!.outcome).toBe("approved");
411
+ });
412
+
413
+ test("returns the stored message immediately when the session already resolved", async () => {
414
+ // Arrange - a detached waiter that only attaches after the human decided
415
+ const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
416
+
417
+ await client.sessionSendMessage(review.id, "changes_requested", "One stage only.");
418
+
419
+ // Act
420
+ const message = await awaitResolve(client, review.id);
421
+
422
+ // Assert
423
+ expect(message).not.toBeNull();
424
+ expect(message!.allow).toBe(false);
425
+ expect(message!.message.body).toContain("One stage only.");
426
+ });
427
+
428
+ test("returns null when the signal aborts first", async () => {
429
+ // Arrange
430
+ const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
431
+ const controller = new AbortController();
432
+ const waiting = awaitResolve(client, review.id, { pollMs: 100, signal: controller.signal });
433
+
434
+ // Act
435
+ controller.abort();
436
+
437
+ // Assert
438
+ expect(await waiting).toBeNull();
439
+ expect((await client.sessionGet(review.id)).status).toBe("pending");
440
+ });
441
+ });
442
+
443
+ describe("awaitResolve: the held wait keeps the daemon alive", () => {
444
+ test("a pending session does not idle-exit while a waiter is parked, then the message lands", async () => {
445
+ // Arrange - a daemon with an aggressive idle timer and its own home
446
+ const idleHome = mkdtempSync(join(tmpdir(), "cueloop-idle-"));
447
+ let idleExits = 0;
448
+ const idleServer = new DaemonServer({
449
+ home: idleHome,
450
+ idleExitMs: 30,
451
+ onIdleExit: () => idleExits++,
452
+ });
453
+
454
+ idleServer.start();
455
+ const waiterClient = await DaemonClient.connect({ home: idleHome });
456
+
457
+ try {
458
+ const review = await openReview(waiterClient, { type: "plan", content: PLAN, cwd: idleHome });
459
+ const waiting = awaitResolve(waiterClient, review.id, { pollMs: 40 });
460
+
461
+ // Act - wait well past the idle window while the review is still pending
462
+ await Bun.sleep(120);
463
+
464
+ // Assert - the parked waiter and the pending session both keep it alive
465
+ expect(idleExits).toBe(0);
466
+ expect((await waiterClient.ping()).pid).toBe(process.pid);
467
+
468
+ // Act - the human returns a message; the parked wait collects it
469
+ const resolver = await DaemonClient.connect({ home: idleHome });
470
+
471
+ await resolver.sessionSendMessage(review.id, "approved", "Good to go.");
472
+ resolver.close();
473
+ const message = await waiting;
474
+
475
+ // Assert
476
+ expect(message!.allow).toBe(true);
477
+ } finally {
478
+ waiterClient.close();
479
+ idleServer.stop();
480
+ rmSync(idleHome, { recursive: true, force: true });
481
+ }
482
+ }, 15_000);
483
+ });