@cueloop/daemon 0.1.0-alpha.6 → 0.1.0-alpha.61
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/package.json +30 -22
- package/src/api.test.ts +292 -46
- package/src/api.ts +301 -52
- package/src/capabilities.test.ts +38 -0
- package/src/capabilities.ts +30 -0
- package/src/client.test.ts +24 -0
- package/src/client.ts +151 -38
- package/src/diff-hot-reload.test.ts +170 -0
- package/src/diff-watcher.ts +105 -0
- package/src/herdr-pane.test.ts +243 -0
- package/src/herdr-pane.ts +154 -0
- package/src/herdr-split.test.ts +106 -0
- package/src/herdr-split.ts +88 -0
- package/src/herdr-tab-store.test.ts +96 -0
- package/src/herdr-tab-store.ts +69 -0
- package/src/index.ts +8 -8
- package/src/main.ts +4 -1
- package/src/paths.ts +9 -0
- package/src/protocol.ts +51 -7
- package/src/retention.test.ts +182 -0
- package/src/retention.ts +122 -0
- package/src/review.test.ts +370 -0
- package/src/review.ts +289 -0
- package/src/server.test.ts +169 -20
- package/src/server.ts +193 -84
- package/src/share-blob.test.ts +62 -0
- package/src/share-blob.ts +55 -0
- package/src/single-instance.test.ts +38 -11
- package/src/store.ts +13 -5
- package/src/validate.test.ts +196 -14
- package/src/validate.ts +138 -38
- package/src/working-tree.ts +113 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/** The shared review core against a real DaemonServer in a temp home: workspace resolution, title derivation, open-or-revise by agentSessionId, and both awaitVerdict 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 } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import type { ReviewSession } from "@cueloop/schema";
|
|
8
|
+
import { DaemonServer } from "./server";
|
|
9
|
+
import { DaemonClient } from "./client";
|
|
10
|
+
import { awaitResolve, openReview, resolveWorkspace } from "./review";
|
|
11
|
+
|
|
12
|
+
const PLAN = "# Rollout Plan\n\nShip it in two stages.\n";
|
|
13
|
+
|
|
14
|
+
let home: string;
|
|
15
|
+
let server: DaemonServer;
|
|
16
|
+
let client: DaemonClient;
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
home = mkdtempSync(join(tmpdir(), "cueloop-review-core-"));
|
|
20
|
+
server = new DaemonServer({ home, idleExitMs: 0 });
|
|
21
|
+
server.start();
|
|
22
|
+
client = await DaemonClient.connect({ home });
|
|
23
|
+
});
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
client.close();
|
|
26
|
+
server.stop();
|
|
27
|
+
rmSync(home, { recursive: true, force: true });
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
function sh(args: string[], cwd: string): void {
|
|
31
|
+
const gitResult = Bun.spawnSync(args, { cwd, stdout: "ignore", stderr: "ignore" });
|
|
32
|
+
|
|
33
|
+
if (gitResult.exitCode !== 0) throw new Error(`${args.join(" ")} failed`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("resolveWorkspace", () => {
|
|
37
|
+
test("a git repo resolves to its root and branch", async () => {
|
|
38
|
+
const repo = mkdtempSync(join(tmpdir(), "cueloop-ws-"));
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
// Arrange
|
|
42
|
+
sh(["git", "init", "-q", "-b", "main"], repo);
|
|
43
|
+
sh(["git", "config", "user.email", "t@t"], repo);
|
|
44
|
+
sh(["git", "config", "user.name", "t"], repo);
|
|
45
|
+
sh(["git", "commit", "-q", "--allow-empty", "-m", "init"], repo);
|
|
46
|
+
|
|
47
|
+
// Act
|
|
48
|
+
const ws = await resolveWorkspace(repo);
|
|
49
|
+
|
|
50
|
+
// Assert
|
|
51
|
+
expect(ws.branch).toBe("main");
|
|
52
|
+
// macOS tmpdirs can resolve through /private; compare the tail
|
|
53
|
+
expect(ws.repoRoot.endsWith(repo.split("/").at(-1)!)).toBe(true);
|
|
54
|
+
} finally {
|
|
55
|
+
rmSync(repo, { recursive: true, force: true });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("outside a repo the cwd itself is the workspace, branch detached", async () => {
|
|
60
|
+
// Act
|
|
61
|
+
const ws = await resolveWorkspace(home);
|
|
62
|
+
|
|
63
|
+
// Assert
|
|
64
|
+
expect(ws).toEqual({ repoRoot: home, branch: "detached" });
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("openReview", () => {
|
|
69
|
+
test("shapes the artifact: agent, derived title, cwd", async () => {
|
|
70
|
+
// Act
|
|
71
|
+
const review = await openReview(client, {
|
|
72
|
+
type: "plan",
|
|
73
|
+
content: PLAN,
|
|
74
|
+
cwd: home,
|
|
75
|
+
agent: "test-agent",
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Assert
|
|
79
|
+
const session = review.session;
|
|
80
|
+
|
|
81
|
+
expect(session.status).toBe("pending");
|
|
82
|
+
expect(session.artifact.type).toBe("plan");
|
|
83
|
+
expect(session.artifact.meta.agent).toBe("test-agent");
|
|
84
|
+
expect(session.artifact.meta.title).toBe("Rollout Plan");
|
|
85
|
+
expect(session.artifact.meta.cwd).toBe(home);
|
|
86
|
+
expect(session.workspace).toEqual({ repoRoot: home, branch: "detached" });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("an explicit title wins; diffs never derive one from content", async () => {
|
|
90
|
+
// Act
|
|
91
|
+
const titled = await openReview(client, {
|
|
92
|
+
type: "plan",
|
|
93
|
+
content: PLAN,
|
|
94
|
+
cwd: home,
|
|
95
|
+
title: "Custom",
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Assert
|
|
99
|
+
expect(titled.session.artifact.meta.title).toBe("Custom");
|
|
100
|
+
|
|
101
|
+
// Act
|
|
102
|
+
const diff = await openReview(client, {
|
|
103
|
+
type: "diff",
|
|
104
|
+
content: "# not a heading\n",
|
|
105
|
+
cwd: home,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Assert
|
|
109
|
+
expect(diff.session.artifact.meta.title).toBeUndefined();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("a pre-resolved workspace skips git resolution", async () => {
|
|
113
|
+
// Arrange
|
|
114
|
+
const ws = { repoRoot: "/elsewhere", branch: "feature" };
|
|
115
|
+
|
|
116
|
+
// Act
|
|
117
|
+
const review = await openReview(client, {
|
|
118
|
+
type: "diff",
|
|
119
|
+
content: "d",
|
|
120
|
+
cwd: home,
|
|
121
|
+
workspace: ws,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// Assert
|
|
125
|
+
expect(review.session.workspace).toEqual(ws);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("the same agentSessionId revises instead of opening a new session", async () => {
|
|
129
|
+
// Arrange
|
|
130
|
+
const first = await openReview(client, {
|
|
131
|
+
type: "plan",
|
|
132
|
+
content: PLAN,
|
|
133
|
+
cwd: home,
|
|
134
|
+
agentSessionId: "agent-1",
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Act
|
|
138
|
+
const second = await openReview(client, {
|
|
139
|
+
type: "plan",
|
|
140
|
+
content: PLAN + "\nStage three.\n",
|
|
141
|
+
cwd: home,
|
|
142
|
+
agentSessionId: "agent-1",
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// Assert
|
|
146
|
+
expect(second.id).toBe(first.id);
|
|
147
|
+
expect(second.session.revisions.length).toBe(2);
|
|
148
|
+
expect(second.session.status).toBe("pending");
|
|
149
|
+
|
|
150
|
+
// Act
|
|
151
|
+
const other = await openReview(client, {
|
|
152
|
+
type: "plan",
|
|
153
|
+
content: PLAN,
|
|
154
|
+
cwd: home,
|
|
155
|
+
agentSessionId: "agent-2",
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Assert
|
|
159
|
+
expect(other.id).not.toBe(first.id);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("per-file notes land as note annotations anchored at the file path", async () => {
|
|
163
|
+
// Act
|
|
164
|
+
const review = await openReview(client, {
|
|
165
|
+
type: "diff",
|
|
166
|
+
content: "d",
|
|
167
|
+
cwd: home,
|
|
168
|
+
notes: [
|
|
169
|
+
{ path: "src/a.ts", body: "Renames b and adds c." },
|
|
170
|
+
{ path: "src/b.ts", body: "Swaps the line." },
|
|
171
|
+
],
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// Assert
|
|
175
|
+
const notes = review.session.annotations;
|
|
176
|
+
|
|
177
|
+
expect(notes.length).toBe(2);
|
|
178
|
+
expect(notes.every((annotation) => annotation.kind === "note")).toBe(true);
|
|
179
|
+
expect(notes.map((annotation) => annotation.anchor.quote)).toEqual(["src/a.ts", "src/b.ts"]);
|
|
180
|
+
expect(notes[0]!.body).toBe("Renames b and adds c.");
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe("awaitVerdict: one long-poll (the hook shape)", () => {
|
|
185
|
+
test("times out to pending; a later wait collects the stored verdict", async () => {
|
|
186
|
+
// Arrange
|
|
187
|
+
const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
|
|
188
|
+
|
|
189
|
+
// Assert
|
|
190
|
+
expect(await review.awaitVerdict({ timeoutMs: 50 })).toBe("pending");
|
|
191
|
+
|
|
192
|
+
// Act
|
|
193
|
+
await client.sessionResolve(review.id, "approve", "Fine.");
|
|
194
|
+
const verdict = await review.awaitVerdict({ timeoutMs: 1_000 });
|
|
195
|
+
|
|
196
|
+
// Assert
|
|
197
|
+
expect(verdict).not.toBe("pending");
|
|
198
|
+
if (verdict === "pending") throw new Error("unreachable");
|
|
199
|
+
expect(verdict.allow).toBe(true);
|
|
200
|
+
expect(verdict.feedback).toContain("Fine.");
|
|
201
|
+
expect(verdict.session.verdict!.kind).toBe("approve");
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("request_changes maps to allow=false with feedback.md attached", async () => {
|
|
205
|
+
// Arrange
|
|
206
|
+
const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
|
|
207
|
+
const waiting = review.awaitVerdict({ timeoutMs: 5_000 });
|
|
208
|
+
|
|
209
|
+
// Act
|
|
210
|
+
await client.sessionResolve(review.id, "request_changes", "One stage only.");
|
|
211
|
+
const verdict = await waiting;
|
|
212
|
+
|
|
213
|
+
// Assert
|
|
214
|
+
if (verdict === "pending") throw new Error("expected a verdict");
|
|
215
|
+
expect(verdict.allow).toBe(false);
|
|
216
|
+
expect(verdict.feedback).toContain("# Review: request changes");
|
|
217
|
+
expect(verdict.feedback).toContain("One stage only.");
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe("awaitVerdict: chunked loop (the pi shape)", () => {
|
|
222
|
+
test("onProgress sees fresh sessions between chunks; the verdict ends the loop", async () => {
|
|
223
|
+
// Arrange
|
|
224
|
+
const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
|
|
225
|
+
const seen: ReviewSession[] = [];
|
|
226
|
+
const waiting = review.awaitVerdict({
|
|
227
|
+
timeoutMs: Infinity,
|
|
228
|
+
pollMs: 100,
|
|
229
|
+
onProgress: (progress) => seen.push(progress),
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Act
|
|
233
|
+
await client.sessionAnnotate(review.id, {
|
|
234
|
+
id: "a1",
|
|
235
|
+
kind: "comment",
|
|
236
|
+
anchor: { quote: "two stages", prefix: "in ", suffix: "." },
|
|
237
|
+
body: "Name them.",
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Assert
|
|
241
|
+
// give the loop a chunk to observe the annotation before resolving
|
|
242
|
+
for (let i = 0; i < 100 && !seen.some((snapshot) => snapshot.annotations.length === 1); i++)
|
|
243
|
+
await Bun.sleep(20);
|
|
244
|
+
expect(seen.some((snapshot) => snapshot.annotations.length === 1)).toBe(true);
|
|
245
|
+
|
|
246
|
+
// Act
|
|
247
|
+
await client.sessionResolve(review.id, "request_changes", "Too vague.");
|
|
248
|
+
const verdict = await waiting;
|
|
249
|
+
|
|
250
|
+
// Assert
|
|
251
|
+
if (verdict === "pending") throw new Error("expected a verdict");
|
|
252
|
+
expect(verdict.allow).toBe(false);
|
|
253
|
+
expect(verdict.session.annotations.length).toBe(1);
|
|
254
|
+
}, 15_000);
|
|
255
|
+
|
|
256
|
+
test("abort surfaces as pending and leaves the session collectable", async () => {
|
|
257
|
+
// Arrange
|
|
258
|
+
const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
|
|
259
|
+
const controller = new AbortController();
|
|
260
|
+
const waiting = review.awaitVerdict({
|
|
261
|
+
timeoutMs: Infinity,
|
|
262
|
+
pollMs: 100,
|
|
263
|
+
signal: controller.signal,
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
// Act
|
|
267
|
+
controller.abort();
|
|
268
|
+
|
|
269
|
+
// Assert
|
|
270
|
+
expect(await waiting).toBe("pending");
|
|
271
|
+
expect((await client.sessionGet(review.id)).status).toBe("pending");
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test("a finite budget runs out to pending", async () => {
|
|
275
|
+
// Arrange
|
|
276
|
+
const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
|
|
277
|
+
|
|
278
|
+
// Assert
|
|
279
|
+
expect(await review.awaitVerdict({ timeoutMs: 120, pollMs: 50 })).toBe("pending");
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
describe("awaitResolve: the adapter wake seam (session id only)", () => {
|
|
284
|
+
test("resolves to the outcome when the verdict lands during the wait", async () => {
|
|
285
|
+
// Arrange
|
|
286
|
+
const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
|
|
287
|
+
const waiting = awaitResolve(client, review.id, { pollMs: 100 });
|
|
288
|
+
|
|
289
|
+
// Act
|
|
290
|
+
await client.sessionResolve(review.id, "approve", "Ship it.");
|
|
291
|
+
const verdict = await waiting;
|
|
292
|
+
|
|
293
|
+
// Assert
|
|
294
|
+
expect(verdict).not.toBeNull();
|
|
295
|
+
expect(verdict!.allow).toBe(true);
|
|
296
|
+
expect(verdict!.feedback).toContain("Ship it.");
|
|
297
|
+
expect(verdict!.session.verdict!.kind).toBe("approve");
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
test("returns the stored verdict immediately when the session already resolved", async () => {
|
|
301
|
+
// Arrange - a detached waiter that only attaches after the human decided
|
|
302
|
+
const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
|
|
303
|
+
|
|
304
|
+
await client.sessionResolve(review.id, "request_changes", "One stage only.");
|
|
305
|
+
|
|
306
|
+
// Act
|
|
307
|
+
const verdict = await awaitResolve(client, review.id);
|
|
308
|
+
|
|
309
|
+
// Assert
|
|
310
|
+
expect(verdict).not.toBeNull();
|
|
311
|
+
expect(verdict!.allow).toBe(false);
|
|
312
|
+
expect(verdict!.feedback).toContain("One stage only.");
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test("returns null when the signal aborts first", async () => {
|
|
316
|
+
// Arrange
|
|
317
|
+
const review = await openReview(client, { type: "plan", content: PLAN, cwd: home });
|
|
318
|
+
const controller = new AbortController();
|
|
319
|
+
const waiting = awaitResolve(client, review.id, { pollMs: 100, signal: controller.signal });
|
|
320
|
+
|
|
321
|
+
// Act
|
|
322
|
+
controller.abort();
|
|
323
|
+
|
|
324
|
+
// Assert
|
|
325
|
+
expect(await waiting).toBeNull();
|
|
326
|
+
expect((await client.sessionGet(review.id)).status).toBe("pending");
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
describe("awaitResolve: the held wait keeps the daemon alive", () => {
|
|
331
|
+
test("a pending session does not idle-exit while a waiter is parked, then the verdict lands", async () => {
|
|
332
|
+
// Arrange - a daemon with an aggressive idle timer and its own home
|
|
333
|
+
const idleHome = mkdtempSync(join(tmpdir(), "cueloop-idle-"));
|
|
334
|
+
let idleExits = 0;
|
|
335
|
+
const idleServer = new DaemonServer({
|
|
336
|
+
home: idleHome,
|
|
337
|
+
idleExitMs: 30,
|
|
338
|
+
onIdleExit: () => idleExits++,
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
idleServer.start();
|
|
342
|
+
const waiterClient = await DaemonClient.connect({ home: idleHome });
|
|
343
|
+
|
|
344
|
+
try {
|
|
345
|
+
const review = await openReview(waiterClient, { type: "plan", content: PLAN, cwd: idleHome });
|
|
346
|
+
const waiting = awaitResolve(waiterClient, review.id, { pollMs: 40 });
|
|
347
|
+
|
|
348
|
+
// Act - wait well past the idle window while the review is still pending
|
|
349
|
+
await Bun.sleep(120);
|
|
350
|
+
|
|
351
|
+
// Assert - the parked waiter and the pending session both keep it alive
|
|
352
|
+
expect(idleExits).toBe(0);
|
|
353
|
+
expect((await waiterClient.ping()).pid).toBe(process.pid);
|
|
354
|
+
|
|
355
|
+
// Act - the human returns a verdict; the parked wait collects it
|
|
356
|
+
const resolver = await DaemonClient.connect({ home: idleHome });
|
|
357
|
+
|
|
358
|
+
await resolver.sessionResolve(review.id, "approve", "Good to go.");
|
|
359
|
+
resolver.close();
|
|
360
|
+
const verdict = await waiting;
|
|
361
|
+
|
|
362
|
+
// Assert
|
|
363
|
+
expect(verdict!.allow).toBe(true);
|
|
364
|
+
} finally {
|
|
365
|
+
waiterClient.close();
|
|
366
|
+
idleServer.stop();
|
|
367
|
+
rmSync(idleHome, { recursive: true, force: true });
|
|
368
|
+
}
|
|
369
|
+
}, 15_000);
|
|
370
|
+
});
|
package/src/review.ts
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared review core: one open/wait/verdict path for every consumer -
|
|
3
|
+
* CLI commands and agent adapters. openReview resolves the workspace, shapes
|
|
4
|
+
* the artifact, and opens-or-revises by agentSessionId; awaitVerdict maps the
|
|
5
|
+
* wait contract onto the agent contract through verdictResponse. An
|
|
6
|
+
* adapter keeps only two bespoke parts: parsing its host's event shape and
|
|
7
|
+
* serializing the decision in its host's contract.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
isMarkdownArtifact,
|
|
12
|
+
newAnnotationId,
|
|
13
|
+
type ArtifactType,
|
|
14
|
+
type DiffFileContents,
|
|
15
|
+
type ReviewSession,
|
|
16
|
+
type WorkspaceKey,
|
|
17
|
+
} from "@cueloop/schema";
|
|
18
|
+
import { verdictResponse } from "./api";
|
|
19
|
+
import type { DaemonClient } from "./client";
|
|
20
|
+
|
|
21
|
+
// Adapters and CLI verbs reach the verdict mapping through this module too,
|
|
22
|
+
// so a session obtained outside a ReviewHandle maps the same way.
|
|
23
|
+
export { verdictResponse };
|
|
24
|
+
|
|
25
|
+
async function git(args: string[], cwd: string): Promise<string | null> {
|
|
26
|
+
try {
|
|
27
|
+
const proc = Bun.spawn(["git", ...args], { cwd, stdout: "pipe", stderr: "ignore" });
|
|
28
|
+
const out = await new Response(proc.stdout).text();
|
|
29
|
+
|
|
30
|
+
if ((await proc.exited) !== 0) return null;
|
|
31
|
+
|
|
32
|
+
return out.trim();
|
|
33
|
+
} catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Workspace key resolution: repo root + branch from the cwd. */
|
|
39
|
+
export async function resolveWorkspace(cwd = process.cwd()): Promise<WorkspaceKey> {
|
|
40
|
+
const repoRoot = (await git(["rev-parse", "--show-toplevel"], cwd)) ?? cwd;
|
|
41
|
+
const branch = (await git(["rev-parse", "--abbrev-ref", "HEAD"], cwd)) ?? "detached";
|
|
42
|
+
|
|
43
|
+
return { repoRoot, branch };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function firstHeading(markdown: string): string | undefined {
|
|
47
|
+
const headingMatch = markdown.match(/^#\s+(.+)$/m);
|
|
48
|
+
|
|
49
|
+
return headingMatch?.[1]?.trim();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface OpenReviewOptions {
|
|
53
|
+
type: ArtifactType;
|
|
54
|
+
content: string;
|
|
55
|
+
/** Workspace resolution root and meta.cwd; defaults to process.cwd(). */
|
|
56
|
+
cwd?: string;
|
|
57
|
+
/** Pre-resolved workspace key; skips git resolution when the caller already has it. */
|
|
58
|
+
workspace?: WorkspaceKey;
|
|
59
|
+
agent?: string;
|
|
60
|
+
/** When set, a resubmit from the same agent session becomes a revision, not a new session. */
|
|
61
|
+
agentSessionId?: string;
|
|
62
|
+
planPath?: string;
|
|
63
|
+
prototypePath?: string;
|
|
64
|
+
pr?: string;
|
|
65
|
+
herdrPane?: string;
|
|
66
|
+
/**
|
|
67
|
+
* Full file contents per changed file for a working-tree diff, carried onto
|
|
68
|
+
* the artifact so hunk curation produces an exactly applyable patch.
|
|
69
|
+
*/
|
|
70
|
+
files?: DiffFileContents[];
|
|
71
|
+
/** Defaults to a markdown artifact's first heading (plan, reply); diffs get no derived title. */
|
|
72
|
+
title?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Per-file agent notes for diff sessions: the submitting agent's own
|
|
75
|
+
* explanation of each file's change, in dead prose. Stored as annotations
|
|
76
|
+
* with kind "note" anchored at the file path, so they render as regular
|
|
77
|
+
* rail cards and feed the guided walk's agent-note block.
|
|
78
|
+
*/
|
|
79
|
+
notes?: ReviewNote[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The note contract: one note per changed file, anchored by the file path. */
|
|
83
|
+
export interface ReviewNote {
|
|
84
|
+
path: string;
|
|
85
|
+
body: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Anchor a note at its file: quote = the path, no context selectors. */
|
|
89
|
+
async function attachNotes(
|
|
90
|
+
client: DaemonClient,
|
|
91
|
+
sessionId: string,
|
|
92
|
+
notes: ReviewNote[],
|
|
93
|
+
): Promise<void> {
|
|
94
|
+
for (const note of notes) {
|
|
95
|
+
await client.sessionAnnotate(sessionId, {
|
|
96
|
+
id: newAnnotationId(),
|
|
97
|
+
kind: "note",
|
|
98
|
+
anchor: { quote: note.path, prefix: "", suffix: "" },
|
|
99
|
+
body: note.body,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface AwaitVerdictOptions {
|
|
105
|
+
/** Total wait budget; Infinity keeps polling until resolved or aborted. */
|
|
106
|
+
timeoutMs: number;
|
|
107
|
+
/** Chunk length for the poll loop; between chunks the session is re-read for progress. */
|
|
108
|
+
pollMs?: number;
|
|
109
|
+
/** Called with the fresh session after each chunk that is still pending. */
|
|
110
|
+
onProgress?: (session: ReviewSession) => void;
|
|
111
|
+
signal?: AbortSignal;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** A resolved review mapped onto the agent contract, plus the full session. */
|
|
115
|
+
export interface VerdictOutcome {
|
|
116
|
+
allow: boolean;
|
|
117
|
+
feedback: string;
|
|
118
|
+
session: ReviewSession;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Sentinel distinguishing an abort from any daemon response. */
|
|
122
|
+
const ABORTED = Symbol("aborted");
|
|
123
|
+
|
|
124
|
+
function raceAbort<T>(
|
|
125
|
+
promise: Promise<T>,
|
|
126
|
+
signal: AbortSignal | undefined,
|
|
127
|
+
): Promise<T | typeof ABORTED> {
|
|
128
|
+
if (!signal) return promise;
|
|
129
|
+
if (signal.aborted) {
|
|
130
|
+
promise.catch(() => {});
|
|
131
|
+
|
|
132
|
+
return Promise.resolve(ABORTED);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return new Promise<T | typeof ABORTED>((resolve, reject) => {
|
|
136
|
+
const onAbort = () => {
|
|
137
|
+
// The daemon request keeps running until the client closes; swallow its
|
|
138
|
+
// eventual rejection so the abort path never leaks an unhandled error.
|
|
139
|
+
promise.catch(() => {});
|
|
140
|
+
resolve(ABORTED);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
144
|
+
promise.then(
|
|
145
|
+
(value) => {
|
|
146
|
+
signal.removeEventListener("abort", onAbort);
|
|
147
|
+
resolve(value);
|
|
148
|
+
},
|
|
149
|
+
(error) => {
|
|
150
|
+
signal.removeEventListener("abort", onAbort);
|
|
151
|
+
reject(error);
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export class ReviewHandle {
|
|
158
|
+
constructor(
|
|
159
|
+
private readonly client: DaemonClient,
|
|
160
|
+
readonly session: ReviewSession,
|
|
161
|
+
) {}
|
|
162
|
+
|
|
163
|
+
get id(): string {
|
|
164
|
+
return this.session.id;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Block on the verdict. "pending" means the budget ran out or the signal
|
|
169
|
+
* aborted - the session stays open and the verdict is collectable later
|
|
170
|
+
* (verdicts outlive waits). With only timeoutMs this is one long-poll;
|
|
171
|
+
* pollMs/onProgress/signal switch to the chunked loop.
|
|
172
|
+
*/
|
|
173
|
+
async awaitVerdict(options: AwaitVerdictOptions): Promise<VerdictOutcome | "pending"> {
|
|
174
|
+
const { timeoutMs, pollMs, onProgress, signal } = options;
|
|
175
|
+
|
|
176
|
+
if (pollMs === undefined && onProgress === undefined && signal === undefined) {
|
|
177
|
+
const resolved = await this.client.sessionWait(this.session.id, timeoutMs);
|
|
178
|
+
|
|
179
|
+
return resolved === null ? "pending" : outcome(resolved);
|
|
180
|
+
}
|
|
181
|
+
const chunkMs = pollMs ?? 10_000;
|
|
182
|
+
const deadline = Number.isFinite(timeoutMs) ? Date.now() + timeoutMs : undefined;
|
|
183
|
+
|
|
184
|
+
for (;;) {
|
|
185
|
+
const budget = deadline === undefined ? chunkMs : Math.min(chunkMs, deadline - Date.now());
|
|
186
|
+
|
|
187
|
+
if (budget <= 0 || signal?.aborted) return "pending";
|
|
188
|
+
const resolved = await raceAbort(this.client.sessionWait(this.session.id, budget), signal);
|
|
189
|
+
|
|
190
|
+
if (resolved === ABORTED) return "pending";
|
|
191
|
+
if (resolved !== null) return outcome(resolved);
|
|
192
|
+
// Still pending after this chunk: re-read to surface reviewer progress.
|
|
193
|
+
const current = await raceAbort(this.client.sessionGet(this.session.id), signal);
|
|
194
|
+
|
|
195
|
+
if (current === ABORTED) return "pending";
|
|
196
|
+
onProgress?.(current);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function outcome(session: ReviewSession): VerdictOutcome {
|
|
202
|
+
return { ...verdictResponse(session), session };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface AwaitResolveOptions {
|
|
206
|
+
/** Long-poll chunk length; the wait re-arms each chunk until resolved or aborted. Default 30s. */
|
|
207
|
+
pollMs?: number;
|
|
208
|
+
/** Abort the wait (the harness session shut down); resolves to null. */
|
|
209
|
+
signal?: AbortSignal;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Park until a review session resolves, then return the verdict outcome; null
|
|
214
|
+
* when the signal aborts first. Where ReviewHandle.awaitVerdict needs the handle
|
|
215
|
+
* that opened the review, this needs only a session id - so a background waiter
|
|
216
|
+
* that woke on a session it did not open (a detached Claude Code / Codex waiter,
|
|
217
|
+
* or pi's session_start listener) can collect the same verdict. This is the
|
|
218
|
+
* wake seam every non-blocking adapter builds on. Loops the daemon long-poll, so
|
|
219
|
+
* a verdict that lands between chunks is never missed and a session already
|
|
220
|
+
* resolved returns on the first chunk. The held connection also keeps the daemon
|
|
221
|
+
* off its idle-exit path for the whole wait.
|
|
222
|
+
*/
|
|
223
|
+
export async function awaitResolve(
|
|
224
|
+
client: DaemonClient,
|
|
225
|
+
sessionId: string,
|
|
226
|
+
options: AwaitResolveOptions = {},
|
|
227
|
+
): Promise<VerdictOutcome | null> {
|
|
228
|
+
const chunkMs = options.pollMs ?? 30_000;
|
|
229
|
+
const { signal } = options;
|
|
230
|
+
|
|
231
|
+
for (;;) {
|
|
232
|
+
if (signal?.aborted) return null;
|
|
233
|
+
const resolved = await raceAbort(client.sessionWait(sessionId, chunkMs), signal);
|
|
234
|
+
|
|
235
|
+
if (resolved === ABORTED) return null;
|
|
236
|
+
if (resolved !== null) return outcome(resolved);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Open a review session (or revise the agent session's existing one) and hand back the wait surface. */
|
|
241
|
+
export async function openReview(
|
|
242
|
+
client: DaemonClient,
|
|
243
|
+
options: OpenReviewOptions,
|
|
244
|
+
): Promise<ReviewHandle> {
|
|
245
|
+
const cwd = options.cwd ?? process.cwd();
|
|
246
|
+
const workspace = options.workspace ?? (await resolveWorkspace(cwd));
|
|
247
|
+
|
|
248
|
+
// Resubmits from the same agent session become revisions, not new sessions.
|
|
249
|
+
if (options.agentSessionId !== undefined) {
|
|
250
|
+
const existing = (await client.sessionList()).find(
|
|
251
|
+
(candidate) => candidate.artifact.meta.agentSessionId === options.agentSessionId,
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
if (existing !== undefined) {
|
|
255
|
+
let revised = await client.sessionSubmitRevision(existing.id, options.content);
|
|
256
|
+
|
|
257
|
+
if (options.notes?.length) {
|
|
258
|
+
await attachNotes(client, revised.id, options.notes);
|
|
259
|
+
revised = await client.sessionGet(revised.id);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return new ReviewHandle(client, revised);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
let session = await client.sessionCreate(workspace, {
|
|
266
|
+
type: options.type,
|
|
267
|
+
content: options.content,
|
|
268
|
+
files: options.files,
|
|
269
|
+
meta: {
|
|
270
|
+
agent: options.agent,
|
|
271
|
+
agentSessionId: options.agentSessionId,
|
|
272
|
+
planPath: options.planPath,
|
|
273
|
+
prototypePath: options.prototypePath,
|
|
274
|
+
pr: options.pr,
|
|
275
|
+
herdrPane: options.herdrPane,
|
|
276
|
+
title:
|
|
277
|
+
options.title ??
|
|
278
|
+
(isMarkdownArtifact(options.type) ? firstHeading(options.content) : undefined),
|
|
279
|
+
cwd,
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
if (options.notes?.length) {
|
|
284
|
+
await attachNotes(client, session.id, options.notes);
|
|
285
|
+
session = await client.sessionGet(session.id);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return new ReviewHandle(client, session);
|
|
289
|
+
}
|