-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdispatcher.test.ts
More file actions
408 lines (339 loc) · 13.6 KB
/
Copy pathdispatcher.test.ts
File metadata and controls
408 lines (339 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import { expect, test, describe, beforeEach, afterEach } from "bun:test";
import {
createDispatcher,
buildPrompt,
adoptPersistedTask,
type Dispatcher,
type GitSnapshot,
type Task,
} from "../src/server/dispatcher";
import { createReportStore, type ReportStore } from "../src/server/report-store";
import type { ProjectConfig } from "../src/server/registry";
import { rm, mkdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { randomUUID } from "node:crypto";
function tmpDir(): string {
return join(tmpdir(), `devbar-test-${randomUUID()}`);
}
const PROJECT: ProjectConfig = {
slug: "test-app",
dir: "/tmp",
model: "sonnet",
effort: "medium",
concurrency: 2,
permission: "plan",
autoDispatch: true,
};
describe("dispatcher", () => {
let resultsDir: string;
let tasksDir: string;
let reportsDir: string;
let store: ReportStore;
let dispatcher: Dispatcher;
async function saveReport(prompt = "fix the bug"): Promise<string> {
const report = await store.save({ prompt, annotations: [] }, "test-app");
return report.id;
}
beforeEach(async () => {
resultsDir = tmpDir();
tasksDir = tmpDir();
reportsDir = tmpDir();
await mkdir(resultsDir, { recursive: true });
await mkdir(tasksDir, { recursive: true });
await mkdir(reportsDir, { recursive: true });
store = createReportStore(reportsDir);
dispatcher = createDispatcher({
store,
resultsDir,
tasksDir,
getProject: (slug) => (slug === "test-app" ? PROJECT : undefined),
command: "echo",
});
});
afterEach(async () => {
await dispatcher.drain();
await rm(resultsDir, { recursive: true, force: true });
await rm(tasksDir, { recursive: true, force: true });
await rm(reportsDir, { recursive: true, force: true });
});
test("enqueue adds a task in queued status", async () => {
const taskId = dispatcher.enqueue(await saveReport(), "test-app");
const task = dispatcher.getTask(taskId);
expect(task).toBeDefined();
expect(task?.status).toBe("queued");
expect(task?.projectSlug).toBe("test-app");
});
test("getTasks filters by project and status", async () => {
dispatcher.enqueue(await saveReport(), "test-app");
expect(dispatcher.getTasks({ project: "test-app" })).toHaveLength(1);
expect(dispatcher.getTasks({ project: "other" })).toHaveLength(0);
expect(dispatcher.getTasks({ status: "queued" })).toHaveLength(1);
});
test("runs a queued task, records events, writes the result", async () => {
const taskId = dispatcher.enqueue(await saveReport(), "test-app");
await dispatcher.process();
await dispatcher.drain();
const task = dispatcher.getTask(taskId);
expect(task?.status).toBe("completed");
expect(task?.result?.exitCode).toBe(0);
const events = dispatcher.getEvents(taskId);
expect(events.some((e) => e.type === "start")).toBe(true);
expect(events.some((e) => e.type === "done")).toBe(true);
const result = JSON.parse(await readFile(join(resultsDir, `${taskId}.json`), "utf-8"));
expect(result.taskId).toBe(taskId);
});
test("persists a task record so a restart can see it", async () => {
const taskId = dispatcher.enqueue(await saveReport(), "test-app");
await dispatcher.process();
await dispatcher.drain();
const persisted = JSON.parse(await readFile(join(tasksDir, `${taskId}.json`), "utf-8"));
expect(persisted.id).toBe(taskId);
expect(persisted.status).toBe("completed");
});
test("marks tasks left running by a dead process as interrupted", async () => {
const taskId = dispatcher.enqueue(await saveReport(), "test-app");
await dispatcher.process();
await dispatcher.drain();
// Rewrite the record as if the process died mid-run, then reload.
const path = join(tasksDir, `${taskId}.json`);
const task = JSON.parse(await readFile(path, "utf-8"));
task.status = "running";
delete task.result;
await Bun.write(path, JSON.stringify(task));
const reloaded = createDispatcher({
store,
resultsDir,
tasksDir,
getProject: () => PROJECT,
command: "echo",
});
await reloaded.ready;
const recovered = reloaded.getTask(taskId);
expect(recovered?.status).toBe("failed");
expect(recovered?.result?.interrupted).toBe(true);
});
test("respects per-project concurrency", async () => {
for (let i = 0; i < 3; i++) dispatcher.enqueue(await saveReport(`report ${i}`), "test-app");
await dispatcher.process();
expect(dispatcher.getTasks({ status: "running" }).length).toBeLessThanOrEqual(
PROJECT.concurrency,
);
await dispatcher.drain();
});
test("dispatchAll enqueues undispatched reports, then skips them", async () => {
await saveReport("a");
await saveReport("b");
const first = await dispatcher.dispatchAll("test-app");
expect(first).toHaveLength(2);
await dispatcher.drain();
const second = await dispatcher.dispatchAll("test-app");
expect(second).toHaveLength(0);
});
test("dispatchAll leaves claimed reports alone", async () => {
const id = await saveReport("claimed by an agent");
await store.setStatus(id, "claimed");
expect(await dispatcher.dispatchAll("test-app")).toHaveLength(0);
});
test("cancel stops a queued task", async () => {
const taskId = dispatcher.enqueue(await saveReport(), "test-app");
expect(dispatcher.cancel(taskId)).toBe(true);
expect(dispatcher.getTask(taskId)?.status).toBe("cancelled");
});
test("a missing report fails the task and frees the slot", async () => {
const taskId = dispatcher.enqueue("does-not-exist", "test-app");
await dispatcher.process();
await dispatcher.drain();
expect(dispatcher.getTask(taskId)?.status).toBe("failed");
// The slot must be free — otherwise the project silently stops dispatching.
const next = dispatcher.enqueue(await saveReport(), "test-app");
await dispatcher.process();
await dispatcher.drain();
expect(dispatcher.getTask(next)?.status).toBe("completed");
});
test("subscribers see task and agent events", async () => {
const seen: string[] = [];
const unsubscribe = dispatcher.subscribe((event) => seen.push(event.kind));
dispatcher.enqueue(await saveReport(), "test-app");
await dispatcher.process();
await dispatcher.drain();
unsubscribe();
expect(seen).toContain("task");
expect(seen).toContain("agent");
});
test("enqueue returns empty string for unknown project", () => {
expect(dispatcher.enqueue("whatever", "unknown")).toBe("");
});
});
describe("dispatcher closes the report out", () => {
let resultsDir: string;
let tasksDir: string;
let reportsDir: string;
let store: ReportStore;
/** A working tree where each named file carries a content stamp. */
function tree(...entries: [string, string][]): GitSnapshot {
return Object.fromEntries(entries);
}
/** A dispatcher whose git snapshots are scripted, one call per invocation. */
function withGit(snapshots: (GitSnapshot | undefined)[], project = PROJECT): Dispatcher {
let call = 0;
return createDispatcher({
store,
resultsDir,
tasksDir,
getProject: (slug) => (slug === "test-app" ? project : undefined),
command: "echo",
gitSnapshot: async () => snapshots[call++],
});
}
beforeEach(async () => {
resultsDir = tmpDir();
tasksDir = tmpDir();
reportsDir = tmpDir();
await mkdir(resultsDir, { recursive: true });
await mkdir(tasksDir, { recursive: true });
await mkdir(reportsDir, { recursive: true });
store = createReportStore(reportsDir);
});
afterEach(async () => {
await Promise.all(
[resultsDir, tasksDir, reportsDir].map((d) => rm(d, { recursive: true, force: true })),
);
});
test("a run that changed files resolves it, with what changed", async () => {
const dispatcher = withGit([tree(), tree(["src/hero.tsx", "12:100"])]);
const reportId = (await store.save({ prompt: "fix it" }, "test-app")).id;
dispatcher.enqueue(reportId, "test-app");
await dispatcher.process();
await dispatcher.drain();
expect((await store.get(reportId))?.status).toBe("resolved");
const resolution = JSON.parse(
await readFile(join((await store.get(reportId))!.dir, "resolution.json"), "utf-8"),
);
expect(resolution.summary).toContain("src/hero.tsx");
});
test("a plan-mode run that changed nothing reopens it and says why", async () => {
// Identical snapshots: the agent proposed and waited for an answer.
const dispatcher = withGit([
tree(["src/other.tsx", "40:100"]),
tree(["src/other.tsx", "40:100"]),
]);
const reportId = (await store.save({ prompt: "fix it" }, "test-app")).id;
const taskId = dispatcher.enqueue(reportId, "test-app");
await dispatcher.process();
await dispatcher.drain();
expect(dispatcher.getTask(taskId)?.status).toBe("completed");
expect(dispatcher.getTask(taskId)?.result?.note).toContain('permission: "auto"');
// Back to "new", not left claiming someone dealt with it.
expect((await store.get(reportId))?.status).toBe("new");
});
test("an auto-permission run that changed nothing says so without blaming plan mode", async () => {
const dispatcher = withGit([tree(), tree()], { ...PROJECT, permission: "auto" });
const reportId = (await store.save({ prompt: "fix it" }, "test-app")).id;
const taskId = dispatcher.enqueue(reportId, "test-app");
await dispatcher.process();
await dispatcher.drain();
expect(dispatcher.getTask(taskId)?.result?.note).toBe("the agent changed nothing.");
expect((await store.get(reportId))?.status).toBe("new");
});
test("editing an already-dirty file counts as a change", async () => {
// Porcelain names the same path before and after, so a path-list
// comparison calls this a no-op and reopens a report that was handled.
const dispatcher = withGit([
tree(["src/hero.tsx", "40:100"]),
tree(["src/hero.tsx", "62:900"]),
]);
const reportId = (await store.save({ prompt: "fix it" }, "test-app")).id;
const taskId = dispatcher.enqueue(reportId, "test-app");
await dispatcher.process();
await dispatcher.drain();
expect(dispatcher.getTask(taskId)?.result?.note).toBeUndefined();
expect(dispatcher.getTask(taskId)?.result?.changedFiles).toEqual(["src/hero.tsx"]);
expect((await store.get(reportId))?.status).toBe("resolved");
});
test("a file the agent reverted to clean counts as a change", async () => {
const dispatcher = withGit([tree(["src/hero.tsx", "40:100"]), tree()]);
const reportId = (await store.save({ prompt: "revert it" }, "test-app")).id;
const taskId = dispatcher.enqueue(reportId, "test-app");
await dispatcher.process();
await dispatcher.drain();
expect(dispatcher.getTask(taskId)?.result?.changedFiles).toEqual(["src/hero.tsx"]);
expect((await store.get(reportId))?.status).toBe("resolved");
});
test("without git it resolves rather than stranding the report", async () => {
const dispatcher = withGit([undefined, undefined]);
const reportId = (await store.save({ prompt: "fix it" }, "test-app")).id;
const taskId = dispatcher.enqueue(reportId, "test-app");
await dispatcher.process();
await dispatcher.drain();
expect(dispatcher.getTask(taskId)?.result?.note).toBeUndefined();
expect((await store.get(reportId))?.status).toBe("resolved");
});
test("reopening does not re-run the report on its own", async () => {
// Reopening is so the report is visibly still waiting, not a retry loop:
// the completed task still guards it against another automatic dispatch.
const dispatcher = withGit([tree(["a", "1:1"]), tree(["a", "1:1"])]);
const reportId = (await store.save({ prompt: "fix it" }, "test-app")).id;
dispatcher.enqueue(reportId, "test-app");
await dispatcher.process();
await dispatcher.drain();
expect((await store.get(reportId))?.status).toBe("new");
expect(await dispatcher.dispatchAll("test-app")).toHaveLength(0);
});
});
describe("buildPrompt", () => {
const report = {
id: "r1",
dir: "/reports/r1",
reportPath: "/reports/r1/report.json",
promptPath: "/reports/r1/prompt.md",
assets: ["/reports/r1/assets/01-image.png"],
status: "new" as const,
createdAt: 0,
};
test("stdin runners get the full report plus where the images live", () => {
const prompt = buildPrompt(report, "# Report\nthe button is misaligned", "stdin");
expect(prompt).toContain("the button is misaligned");
expect(prompt).toContain("/reports/r1/assets");
});
test("argv runners get a short pointer instead", () => {
const prompt = buildPrompt(report, "# Report\nthe button is misaligned", "arg");
expect(prompt.length).toBeLessThan(400);
expect(prompt).toContain("/reports/r1/prompt.md");
});
});
describe("adoptPersistedTask", () => {
function record(status: Task["status"]): Task {
return {
id: "task-1",
reportId: "report-1",
reportPath: "/tmp/report-1",
projectSlug: "test-app",
status,
createdAt: 1,
};
}
test("marks a record its process left behind as interrupted", () => {
const tasks = new Map<string, Task>();
expect(adoptPersistedTask(tasks, record("running"), 42)).toBe(true);
const adopted = tasks.get("task-1");
expect(adopted?.status).toBe("failed");
expect(adopted?.result?.interrupted).toBe(true);
expect(adopted?.completedAt).toBe(42);
});
test("adopts a finished record as-is", () => {
const tasks = new Map<string, Task>();
expect(adoptPersistedTask(tasks, record("completed"), 42)).toBe(false);
expect(tasks.get("task-1")?.status).toBe("completed");
});
test("never overwrites a task the dispatcher already holds", () => {
// The startup reload races the dispatcher: a task enqueued right after
// construction has a stale "queued" snapshot on disk while it is already
// running or done in memory. The live task must win.
const live = record("completed");
const tasks = new Map<string, Task>([[live.id, live]]);
expect(adoptPersistedTask(tasks, record("queued"), 42)).toBe(false);
expect(tasks.get("task-1")).toBe(live);
expect(tasks.get("task-1")?.status).toBe("completed");
});
});