@cueloop/daemon 0.1.0-alpha.8 → 0.1.0-alpha.80
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 +34 -22
- package/src/api.test.ts +925 -65
- package/src/api.ts +1060 -88
- package/src/capabilities.test.ts +57 -0
- package/src/capabilities.ts +84 -0
- package/src/client.test.ts +24 -0
- package/src/client.ts +526 -70
- package/src/curate.ts +109 -0
- package/src/daemon-version.test.ts +93 -0
- package/src/diff-hot-reload.test.ts +272 -0
- package/src/diff-watcher.ts +274 -0
- package/src/gh.ts +37 -0
- package/src/harness-delivery.test.ts +193 -0
- package/src/harness-state-store.test.ts +65 -0
- package/src/harness-state-store.ts +182 -0
- package/src/herdr-pane.test.ts +240 -0
- package/src/herdr-pane.ts +167 -0
- package/src/herdr-split.test.ts +106 -0
- package/src/herdr-split.ts +92 -0
- package/src/herdr-tab-store.test.ts +96 -0
- package/src/herdr-tab-store.ts +66 -0
- package/src/index.ts +8 -9
- package/src/interruptible-wait.test.ts +127 -0
- package/src/interruptible-wait.ts +66 -0
- package/src/main.ts +4 -1
- package/src/paths.ts +44 -0
- package/src/pr-poller.test.ts +76 -0
- package/src/pr-poller.ts +74 -0
- package/src/project-files.test.ts +124 -0
- package/src/project-files.ts +56 -0
- package/src/protocol.ts +79 -7
- package/src/retention.test.ts +191 -0
- package/src/retention.ts +123 -0
- package/src/server.test.ts +430 -42
- package/src/server.ts +362 -84
- package/src/share-blob.test.ts +112 -0
- package/src/share-blob.ts +55 -0
- package/src/single-instance.test.ts +72 -11
- package/src/store.conformance.test.ts +41 -0
- package/src/store.jsonl.test.ts +159 -0
- package/src/store.ts +264 -34
- package/src/testing/store-conformance.ts +194 -0
- package/src/thread-review.test.ts +394 -0
- package/src/thread-review.ts +324 -0
- package/src/validate.test.ts +214 -35
- package/src/validate.ts +296 -25
- package/src/version.ts +6 -0
- package/src/working-tree.ts +149 -0
- package/src/review.test.ts +0 -167
- package/src/review.ts +0 -174
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared Thread review core: one open/wait/message path for every consumer -
|
|
3
|
+
* CLI commands and agent adapters. openReview resolves the workspace, shapes
|
|
4
|
+
* the artifact, and opens-or-revises by agentSessionId; awaitMessage maps the
|
|
5
|
+
* wait contract onto the agent contract through messageResponse. 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 Annotation,
|
|
14
|
+
type Artifact,
|
|
15
|
+
type ArtifactType,
|
|
16
|
+
type DiffFileContents,
|
|
17
|
+
type Thread,
|
|
18
|
+
type WorkspaceKey,
|
|
19
|
+
type WorkflowKind,
|
|
20
|
+
} from "@cueloop/schema";
|
|
21
|
+
import { messageResponse } from "./api";
|
|
22
|
+
import { ABORTED, pollUntilResolved, raceAbort } from "./interruptible-wait";
|
|
23
|
+
|
|
24
|
+
// Adapters and CLI primitives reach the message mapping through this module too,
|
|
25
|
+
// so a session obtained outside a ReviewHandle maps the same way.
|
|
26
|
+
export { messageResponse };
|
|
27
|
+
|
|
28
|
+
async function git(args: string[], cwd: string): Promise<string | null> {
|
|
29
|
+
try {
|
|
30
|
+
const proc = Bun.spawn(["git", ...args], { cwd, stdout: "pipe", stderr: "ignore" });
|
|
31
|
+
const out = await new Response(proc.stdout).text();
|
|
32
|
+
|
|
33
|
+
if ((await proc.exited) !== 0) return null;
|
|
34
|
+
|
|
35
|
+
return out.trim();
|
|
36
|
+
} catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The oldest root commit reachable from HEAD; the project key that survives moving or re-cloning the repo. */
|
|
42
|
+
function earliestRootCommit(revList: string | null): string | undefined {
|
|
43
|
+
if (!revList) return undefined;
|
|
44
|
+
const roots = revList.split("\n").filter((line) => line.length > 0);
|
|
45
|
+
|
|
46
|
+
// --date-order lists newest first, so the last root is the earliest
|
|
47
|
+
return roots.at(-1);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Workspace key resolution: repo root, branch, and the project identity (root commit + remote) from the cwd. */
|
|
51
|
+
export async function resolveWorkspace(cwd = process.cwd()): Promise<WorkspaceKey> {
|
|
52
|
+
const repoRoot = (await git(["rev-parse", "--show-toplevel"], cwd)) ?? cwd;
|
|
53
|
+
const branch = (await git(["rev-parse", "--abbrev-ref", "HEAD"], cwd)) ?? "detached";
|
|
54
|
+
// a shallow clone's oldest commit is the graft boundary, not the true root, so
|
|
55
|
+
// it would key a different project than a full clone - leave it unset instead
|
|
56
|
+
const shallow = (await git(["rev-parse", "--is-shallow-repository"], cwd)) === "true";
|
|
57
|
+
const rootCommit = shallow
|
|
58
|
+
? undefined
|
|
59
|
+
: earliestRootCommit(await git(["rev-list", "--max-parents=0", "--date-order", "HEAD"], cwd));
|
|
60
|
+
const remote = await git(["remote", "get-url", "origin"], cwd);
|
|
61
|
+
|
|
62
|
+
const workspace: WorkspaceKey = { repoRoot, branch };
|
|
63
|
+
// a repo with no commits (or a shallow clone) has no reliable root, so the thread stays standalone
|
|
64
|
+
if (rootCommit) workspace.rootCommit = rootCommit;
|
|
65
|
+
if (remote) workspace.remote = remote;
|
|
66
|
+
|
|
67
|
+
return workspace;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function firstHeading(markdown: string): string | undefined {
|
|
71
|
+
const headingMatch = markdown.match(/^#\s+(.+)$/m);
|
|
72
|
+
|
|
73
|
+
return headingMatch?.[1]?.trim();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface OpenReviewOptions {
|
|
77
|
+
type: ArtifactType;
|
|
78
|
+
/** Workflow identity keeps plan/refine and diff/review Threads separate. */
|
|
79
|
+
workflow?: WorkflowKind;
|
|
80
|
+
content: string;
|
|
81
|
+
/** Workspace resolution root and meta.cwd; defaults to process.cwd(). */
|
|
82
|
+
cwd?: string;
|
|
83
|
+
/** Pre-resolved workspace key; skips git resolution when the caller already has it. */
|
|
84
|
+
workspace?: WorkspaceKey;
|
|
85
|
+
agent?: string;
|
|
86
|
+
/** When set, a resubmit from the same agent session becomes a revision, not a new session. */
|
|
87
|
+
agentSessionId?: string;
|
|
88
|
+
planPath?: string;
|
|
89
|
+
prototypePath?: string;
|
|
90
|
+
pr?: string;
|
|
91
|
+
herdrPane?: string;
|
|
92
|
+
/**
|
|
93
|
+
* Full file contents per changed file for a working-tree diff, carried onto
|
|
94
|
+
* the artifact so hunk curation produces an exactly applyable patch.
|
|
95
|
+
*/
|
|
96
|
+
files?: DiffFileContents[];
|
|
97
|
+
/** Defaults to a markdown artifact's first heading (plan, reply); diffs get no derived title. */
|
|
98
|
+
title?: string;
|
|
99
|
+
/**
|
|
100
|
+
* Per-file agent notes for diff sessions: the submitting agent's own
|
|
101
|
+
* explanation of each file's change, in dead prose. Stored as annotations
|
|
102
|
+
* with kind "note" anchored at the file path, so they render as regular
|
|
103
|
+
* rail cards and feed the guided walk's agent-note block.
|
|
104
|
+
*/
|
|
105
|
+
notes?: ReviewNote[];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The note contract: one note per changed file, anchored by the file path. */
|
|
109
|
+
export interface ReviewNote {
|
|
110
|
+
path: string;
|
|
111
|
+
body: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The transport-neutral daemon operations used by the shared review lifecycle. */
|
|
115
|
+
export interface ThreadSessionClient {
|
|
116
|
+
sessionList(filter?: { status?: "pending" | "resolved" }): Promise<Thread[]>;
|
|
117
|
+
sessionCreate(workspace: WorkspaceKey, artifact: Artifact): Promise<Thread>;
|
|
118
|
+
sessionSubmitRevision(
|
|
119
|
+
id: string,
|
|
120
|
+
content: string,
|
|
121
|
+
addressedAnnotationIds?: string[],
|
|
122
|
+
files?: DiffFileContents[],
|
|
123
|
+
): Promise<Thread>;
|
|
124
|
+
sessionAnnotate(
|
|
125
|
+
id: string,
|
|
126
|
+
annotation: Omit<Annotation, "createdAt">,
|
|
127
|
+
authorName?: string,
|
|
128
|
+
): Promise<Thread>;
|
|
129
|
+
sessionGet(id: string): Promise<Thread>;
|
|
130
|
+
sessionWait(id: string, timeoutMs: number): Promise<Thread | null>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Anchor a note at its file: quote = the path, no context selectors. */
|
|
134
|
+
async function attachNotes(
|
|
135
|
+
client: ThreadSessionClient,
|
|
136
|
+
sessionId: string,
|
|
137
|
+
notes: ReviewNote[],
|
|
138
|
+
): Promise<void> {
|
|
139
|
+
for (const note of notes) {
|
|
140
|
+
await client.sessionAnnotate(sessionId, {
|
|
141
|
+
id: newAnnotationId(),
|
|
142
|
+
kind: "note",
|
|
143
|
+
anchor: { quote: note.path, prefix: "", suffix: "" },
|
|
144
|
+
body: note.body,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface AwaitMessageOptions {
|
|
150
|
+
/** Total wait budget; Infinity keeps polling until resolved or aborted. */
|
|
151
|
+
timeoutMs: number;
|
|
152
|
+
/** Chunk length for the poll loop; between chunks the session is re-read for progress. */
|
|
153
|
+
pollMs?: number;
|
|
154
|
+
/** Called with the fresh session after each chunk that is still pending. */
|
|
155
|
+
onProgress?: (session: Thread) => void;
|
|
156
|
+
signal?: AbortSignal;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** A resolved review mapped onto the agent contract, plus the full session. */
|
|
160
|
+
export interface MessageResult {
|
|
161
|
+
allow: boolean;
|
|
162
|
+
message: import("@cueloop/schema").Message;
|
|
163
|
+
session: Thread;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export class ReviewHandle {
|
|
167
|
+
constructor(
|
|
168
|
+
private readonly client: Pick<ThreadSessionClient, "sessionWait" | "sessionGet">,
|
|
169
|
+
readonly session: Thread,
|
|
170
|
+
) {}
|
|
171
|
+
|
|
172
|
+
get id(): string {
|
|
173
|
+
return this.session.id;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Block on the message. "pending" means the budget ran out or the signal
|
|
178
|
+
* aborted - the session stays open and the message is collectable later
|
|
179
|
+
* (messages outlive waits). With only timeoutMs this is one long-poll;
|
|
180
|
+
* pollMs/onProgress/signal switch to the chunked loop.
|
|
181
|
+
*/
|
|
182
|
+
async awaitMessage(options: AwaitMessageOptions): Promise<MessageResult | "pending"> {
|
|
183
|
+
const { timeoutMs, pollMs, onProgress, signal } = options;
|
|
184
|
+
|
|
185
|
+
if (pollMs === undefined && onProgress === undefined && signal === undefined) {
|
|
186
|
+
const resolved = await this.client.sessionWait(this.session.id, timeoutMs);
|
|
187
|
+
|
|
188
|
+
return resolved === null ? "pending" : outcome(resolved);
|
|
189
|
+
}
|
|
190
|
+
const chunkMs = pollMs ?? 10_000;
|
|
191
|
+
const deadline = Number.isFinite(timeoutMs) ? Date.now() + timeoutMs : undefined;
|
|
192
|
+
|
|
193
|
+
for (;;) {
|
|
194
|
+
const budget = deadline === undefined ? chunkMs : Math.min(chunkMs, deadline - Date.now());
|
|
195
|
+
|
|
196
|
+
if (budget <= 0 || signal?.aborted) return "pending";
|
|
197
|
+
const resolved = await raceAbort(this.client.sessionWait(this.session.id, budget), signal);
|
|
198
|
+
|
|
199
|
+
if (resolved === ABORTED) return "pending";
|
|
200
|
+
|
|
201
|
+
if (resolved !== null) return outcome(resolved);
|
|
202
|
+
// Still pending after this chunk: re-read to surface reviewer progress.
|
|
203
|
+
const current = await raceAbort(this.client.sessionGet(this.session.id), signal);
|
|
204
|
+
|
|
205
|
+
if (current === ABORTED) return "pending";
|
|
206
|
+
|
|
207
|
+
onProgress?.(current);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function outcome(session: Thread): MessageResult {
|
|
213
|
+
return { ...messageResponse(session), session };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export interface AwaitResolveOptions {
|
|
217
|
+
/** Long-poll chunk length; the wait re-arms each chunk until resolved or aborted. Default 30s. */
|
|
218
|
+
pollMs?: number;
|
|
219
|
+
/** Abort the wait (the harness session shut down); resolves to null. */
|
|
220
|
+
signal?: AbortSignal;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Park until a thread resolves, then return the message outcome; null
|
|
225
|
+
* when the signal aborts first. Where ReviewHandle.awaitMessage needs the handle
|
|
226
|
+
* that opened the review, this needs only a session id - so a background waiter
|
|
227
|
+
* that woke on a session it did not open (a detached Claude Code / Codex waiter,
|
|
228
|
+
* or pi's session_start listener) can collect the same message. This is the
|
|
229
|
+
* wake seam every non-blocking adapter builds on. Loops the daemon long-poll, so
|
|
230
|
+
* a message that lands between chunks is never missed and a session already
|
|
231
|
+
* resolved returns on the first chunk. The held connection also keeps the daemon
|
|
232
|
+
* off its idle-exit path for the whole wait.
|
|
233
|
+
*/
|
|
234
|
+
export async function awaitResolve(
|
|
235
|
+
client: Pick<ThreadSessionClient, "sessionWait">,
|
|
236
|
+
sessionId: string,
|
|
237
|
+
options: AwaitResolveOptions = {},
|
|
238
|
+
): Promise<MessageResult | null> {
|
|
239
|
+
const chunkMs = options.pollMs ?? 30_000;
|
|
240
|
+
const resolved = await pollUntilResolved(
|
|
241
|
+
() => client.sessionWait(sessionId, chunkMs),
|
|
242
|
+
options.signal,
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
return resolved === null ? null : outcome(resolved);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Find the Thread already submitted by this agent in the same workspace and primitive. */
|
|
249
|
+
export async function findExistingReview(
|
|
250
|
+
client: ThreadSessionClient,
|
|
251
|
+
options: OpenReviewOptions,
|
|
252
|
+
): Promise<Thread | undefined> {
|
|
253
|
+
const cwd = options.cwd ?? process.cwd();
|
|
254
|
+
const workspace = options.workspace ?? (await resolveWorkspace(cwd));
|
|
255
|
+
|
|
256
|
+
if (options.agentSessionId === undefined) return undefined;
|
|
257
|
+
|
|
258
|
+
return (await client.sessionList()).find(
|
|
259
|
+
(candidate) =>
|
|
260
|
+
candidate.artifact.meta.agentSessionId === options.agentSessionId &&
|
|
261
|
+
candidate.artifact.meta.agent === options.agent &&
|
|
262
|
+
candidate.artifact.type === options.type &&
|
|
263
|
+
(candidate.artifact.meta.workflow ??
|
|
264
|
+
(candidate.artifact.meta.pr ? "review" : candidate.artifact.type)) ===
|
|
265
|
+
(options.workflow ?? (options.pr ? "review" : options.type)) &&
|
|
266
|
+
candidate.artifact.meta.pr === options.pr &&
|
|
267
|
+
candidate.workspace.repoRoot === workspace.repoRoot &&
|
|
268
|
+
candidate.workspace.branch === workspace.branch,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Open a thread (or revise the agent session's existing one) and hand back the wait surface. */
|
|
273
|
+
export async function openReview(
|
|
274
|
+
client: ThreadSessionClient,
|
|
275
|
+
options: OpenReviewOptions,
|
|
276
|
+
): Promise<ReviewHandle> {
|
|
277
|
+
const cwd = options.cwd ?? process.cwd();
|
|
278
|
+
const workspace = options.workspace ?? (await resolveWorkspace(cwd));
|
|
279
|
+
const existing = await findExistingReview(client, options);
|
|
280
|
+
|
|
281
|
+
if (existing !== undefined) {
|
|
282
|
+
// PR diffs have no full file contents. An absent local-diff capture clears
|
|
283
|
+
// old curatable files, while a PR revision must keep curation disabled.
|
|
284
|
+
const revisionFiles = options.type !== "diff" || options.pr ? undefined : (options.files ?? []);
|
|
285
|
+
let revised = await client.sessionSubmitRevision(
|
|
286
|
+
existing.id,
|
|
287
|
+
options.content,
|
|
288
|
+
[],
|
|
289
|
+
revisionFiles,
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
if (options.notes?.length) {
|
|
293
|
+
await attachNotes(client, revised.id, options.notes);
|
|
294
|
+
revised = await client.sessionGet(revised.id);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return new ReviewHandle(client, revised);
|
|
298
|
+
}
|
|
299
|
+
let session = await client.sessionCreate(workspace, {
|
|
300
|
+
type: options.type,
|
|
301
|
+
content: options.content,
|
|
302
|
+
files: options.files,
|
|
303
|
+
meta: {
|
|
304
|
+
workflow: options.workflow,
|
|
305
|
+
agent: options.agent,
|
|
306
|
+
agentSessionId: options.agentSessionId,
|
|
307
|
+
planPath: options.planPath,
|
|
308
|
+
prototypePath: options.prototypePath,
|
|
309
|
+
pr: options.pr,
|
|
310
|
+
herdrPane: options.herdrPane,
|
|
311
|
+
title:
|
|
312
|
+
options.title ??
|
|
313
|
+
(isMarkdownArtifact(options.type) ? firstHeading(options.content) : undefined),
|
|
314
|
+
cwd,
|
|
315
|
+
},
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
if (options.notes?.length) {
|
|
319
|
+
await attachNotes(client, session.id, options.notes);
|
|
320
|
+
session = await client.sessionGet(session.id);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return new ReviewHandle(client, session);
|
|
324
|
+
}
|