@@ -0,0 +1,456 @@
1+import type { Database } from "bun:sqlite";
2+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
3+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
4+import { tmpdir } from "node:os";
5+import { join } from "node:path";
6+import type { AssistantMessage, UserMessage } from "@mariozechner/pi-ai";
7+import {
8+ type FauxProviderRegistration,
9+ fauxAssistantMessage,
10+ fauxToolCall,
11+ registerFauxProvider,
12+} from "@mariozechner/pi-ai";
13+import { type AgentEvent, type AgentTool, runAgentLoop } from "./agent.ts";
14+import {
15+ appendMessage,
16+ createSession,
17+ loadMessages,
18+ openDatabase,
19+} from "./session.ts";
20+import { editTool, executeEdit, executeShell, shellTool } from "./tools.ts";
21+
22+// ---------------------------------------------------------------------------
23+// Helpers
24+// ---------------------------------------------------------------------------
25+
26+let tmp: string;
27+let db: Database;
28+let faux: FauxProviderRegistration;
29+
30+beforeEach(() => {
31+ tmp = mkdtempSync(join(tmpdir(), "mc-agent-"));
32+ db = openDatabase(":memory:");
33+ faux = registerFauxProvider();
34+});
35+
36+afterEach(() => {
37+ faux.unregister();
38+ db.close();
39+ rmSync(tmp, { recursive: true, force: true });
40+});
41+
42+function makeUser(text: string): UserMessage {
43+ return { role: "user", content: text, timestamp: Date.now() };
44+}
45+
46+/** Create standard built-in tools for tests. */
47+function builtinTools(): AgentTool[] {
48+ return [
49+ {
50+ definition: shellTool,
51+ execute: (args, cwd) =>
52+ executeShell({ command: args.command as string }, cwd),
53+ },
54+ {
55+ definition: editTool,
56+ execute: (args, cwd) =>
57+ executeEdit(
58+ {
59+ path: args.path as string,
60+ oldText: args.oldText as string,
61+ newText: args.newText as string,
62+ },
63+ cwd,
64+ ),
65+ },
66+ ];
67+}
68+
69+// ---------------------------------------------------------------------------
70+// Tests
71+// ---------------------------------------------------------------------------
72+
73+describe("agent loop", () => {
74+ test("simple text response appends assistant message", async () => {
75+ faux.setResponses([fauxAssistantMessage("Hello back!")]);
76+
77+ const session = createSession(db, { cwd: tmp });
78+ const userMsg = makeUser("hello");
79+ const turn = appendMessage(db, session.id, userMsg);
80+
81+ const result = await runAgentLoop({
82+ db,
83+ sessionId: session.id,
84+ turn,
85+ model: faux.getModel(),
86+ systemPrompt: "Test",
87+ tools: [],
88+ messages: [userMsg],
89+ cwd: tmp,
90+ });
91+
92+ expect(result.messages).toHaveLength(2);
93+ expect(result.messages[1]!.role).toBe("assistant");
94+ const am = result.messages[1] as AssistantMessage;
95+ expect(am.stopReason).toBe("stop");
96+ });
97+
98+ test("simple text response persists to DB", async () => {
99+ faux.setResponses([fauxAssistantMessage("Hello back!")]);
100+ const session = createSession(db, { cwd: tmp });
101+ const userMsg = makeUser("hello");
102+ const turn = appendMessage(db, session.id, userMsg);
103+
104+ await runAgentLoop({
105+ db,
106+ sessionId: session.id,
107+ turn,
108+ model: faux.getModel(),
109+ systemPrompt: "Test",
110+ tools: [],
111+ messages: [userMsg],
112+ cwd: tmp,
113+ });
114+
115+ const dbMessages = loadMessages(db, session.id);
116+ expect(dbMessages).toHaveLength(2);
117+ expect(dbMessages[0]!.role).toBe("user");
118+ expect(dbMessages[1]!.role).toBe("assistant");
119+ });
120+
121+ test("executes tool calls and appends results", async () => {
122+ faux.setResponses([
123+ // First response: call shell tool
124+ fauxAssistantMessage(
125+ [fauxToolCall("shell", { command: "echo tool-output" })],
126+ { stopReason: "toolUse" },
127+ ),
128+ // Second response: after tool result, reply with text
129+ fauxAssistantMessage("Done! The output was tool-output."),
130+ ]);
131+
132+ const session = createSession(db, { cwd: tmp });
133+ const userMsg = makeUser("run echo");
134+ const turn = appendMessage(db, session.id, userMsg);
135+
136+ const result = await runAgentLoop({
137+ db,
138+ sessionId: session.id,
139+ turn,
140+ model: faux.getModel(),
141+ systemPrompt: "Test",
142+ tools: builtinTools(),
143+ messages: [userMsg],
144+ cwd: tmp,
145+ });
146+
147+ // user → assistant(toolCall) → toolResult → assistant(text)
148+ expect(result.messages).toHaveLength(4);
149+ expect(result.messages[0]!.role).toBe("user");
150+ expect(result.messages[1]!.role).toBe("assistant");
151+ expect(result.messages[2]!.role).toBe("toolResult");
152+ expect(result.messages[3]!.role).toBe("assistant");
153+ expect((result.messages[3] as AssistantMessage).stopReason).toBe("stop");
154+
155+ // DB should match
156+ const dbMessages = loadMessages(db, session.id);
157+ expect(dbMessages).toHaveLength(4);
158+ });
159+
160+ test("handles multiple tool calls in a single response", async () => {
161+ writeFileSync(join(tmp, "file.txt"), "original content");
162+
163+ faux.setResponses([
164+ // One response with two tool calls
165+ fauxAssistantMessage(
166+ [
167+ fauxToolCall("shell", { command: "echo first" }),
168+ fauxToolCall("edit", {
169+ path: "file.txt",
170+ oldText: "original",
171+ newText: "modified",
172+ }),
173+ ],
174+ { stopReason: "toolUse" },
175+ ),
176+ // Follow-up
177+ fauxAssistantMessage("Both done."),
178+ ]);
179+
180+ const session = createSession(db, { cwd: tmp });
181+ const userMsg = makeUser("do both");
182+ const turn = appendMessage(db, session.id, userMsg);
183+
184+ const result = await runAgentLoop({
185+ db,
186+ sessionId: session.id,
187+ turn,
188+ model: faux.getModel(),
189+ systemPrompt: "Test",
190+ tools: builtinTools(),
191+ messages: [userMsg],
192+ cwd: tmp,
193+ });
194+
195+ // user → assistant(2 toolCalls) → toolResult × 2 → assistant(text)
196+ expect(result.messages).toHaveLength(5);
197+ expect(result.messages[2]!.role).toBe("toolResult");
198+ expect(result.messages[3]!.role).toBe("toolResult");
199+ expect(result.messages[4]!.role).toBe("assistant");
200+ });
201+
202+ test("chains multiple tool-use rounds", async () => {
203+ faux.setResponses([
204+ // Round 1: tool call
205+ fauxAssistantMessage(
206+ [fauxToolCall("shell", { command: "echo round1" })],
207+ { stopReason: "toolUse" },
208+ ),
209+ // Round 2: another tool call after seeing result
210+ fauxAssistantMessage(
211+ [fauxToolCall("shell", { command: "echo round2" })],
212+ { stopReason: "toolUse" },
213+ ),
214+ // Round 3: done
215+ fauxAssistantMessage("All rounds complete."),
216+ ]);
217+
218+ const session = createSession(db, { cwd: tmp });
219+ const userMsg = makeUser("chain");
220+ const turn = appendMessage(db, session.id, userMsg);
221+
222+ const result = await runAgentLoop({
223+ db,
224+ sessionId: session.id,
225+ turn,
226+ model: faux.getModel(),
227+ systemPrompt: "Test",
228+ tools: builtinTools(),
229+ messages: [userMsg],
230+ cwd: tmp,
231+ });
232+
233+ // user → assistant(tc) → toolResult → assistant(tc) → toolResult → assistant(text)
234+ expect(result.messages).toHaveLength(6);
235+ expect(result.stopReason).toBe("stop");
236+ });
237+
238+ test("all messages in loop share the same turn number", async () => {
239+ faux.setResponses([
240+ fauxAssistantMessage([fauxToolCall("shell", { command: "echo hi" })], {
241+ stopReason: "toolUse",
242+ }),
243+ fauxAssistantMessage("Done."),
244+ ]);
245+
246+ const session = createSession(db, { cwd: tmp });
247+ const userMsg = makeUser("test turns");
248+ const turn = appendMessage(db, session.id, userMsg);
249+
250+ await runAgentLoop({
251+ db,
252+ sessionId: session.id,
253+ turn,
254+ model: faux.getModel(),
255+ systemPrompt: "Test",
256+ tools: builtinTools(),
257+ messages: [userMsg],
258+ cwd: tmp,
259+ });
260+
261+ // All messages should be in the same turn — undo should remove everything
262+ const dbMessages = loadMessages(db, session.id);
263+ expect(dbMessages).toHaveLength(4); // user + assistant + toolResult + assistant
264+
265+ // Undo should remove all of them (same turn)
266+ const { undoLastTurn } = await import("./session.ts");
267+ undoLastTurn(db, session.id);
268+ expect(loadMessages(db, session.id)).toHaveLength(0);
269+ });
270+
271+ test("emits events during streaming", async () => {
272+ faux.setResponses([fauxAssistantMessage("Hello world")]);
273+
274+ const events: AgentEvent[] = [];
275+
276+ const session = createSession(db, { cwd: tmp });
277+ const userMsg = makeUser("hi");
278+ const turn = appendMessage(db, session.id, userMsg);
279+
280+ await runAgentLoop({
281+ db,
282+ sessionId: session.id,
283+ turn,
284+ model: faux.getModel(),
285+ systemPrompt: "Test",
286+ tools: [],
287+ messages: [userMsg],
288+ cwd: tmp,
289+ onEvent: (e) => events.push(e),
290+ });
291+
292+ const textDeltas = events.filter((e) => e.type === "text_delta");
293+ expect(textDeltas.length).toBeGreaterThan(0);
294+
295+ const doneEvents = events.filter((e) => e.type === "done");
296+ expect(doneEvents).toHaveLength(1);
297+ });
298+
299+ test("emits tool events during tool execution", async () => {
300+ faux.setResponses([
301+ fauxAssistantMessage([fauxToolCall("shell", { command: "echo test" })], {
302+ stopReason: "toolUse",
303+ }),
304+ fauxAssistantMessage("Done."),
305+ ]);
306+
307+ const events: AgentEvent[] = [];
308+
309+ const session = createSession(db, { cwd: tmp });
310+ const userMsg = makeUser("go");
311+ const turn = appendMessage(db, session.id, userMsg);
312+
313+ await runAgentLoop({
314+ db,
315+ sessionId: session.id,
316+ turn,
317+ model: faux.getModel(),
318+ systemPrompt: "Test",
319+ tools: builtinTools(),
320+ messages: [userMsg],
321+ cwd: tmp,
322+ onEvent: (e) => events.push(e),
323+ });
324+
325+ const toolStarts = events.filter((e) => e.type === "tool_start");
326+ const toolEnds = events.filter((e) => e.type === "tool_end");
327+ expect(toolStarts).toHaveLength(1);
328+ expect(toolEnds).toHaveLength(1);
329+ });
330+
331+ test("interrupt preserves partial response", async () => {
332+ faux.setResponses([
333+ fauxAssistantMessage("This response will be interrupted"),
334+ ]);
335+
336+ // Pre-abort so the faux provider immediately returns an aborted message
337+ const controller = new AbortController();
338+ controller.abort();
339+
340+ const session = createSession(db, { cwd: tmp });
341+ const userMsg = makeUser("long task");
342+ const turn = appendMessage(db, session.id, userMsg);
343+
344+ const result = await runAgentLoop({
345+ db,
346+ sessionId: session.id,
347+ turn,
348+ model: faux.getModel(),
349+ systemPrompt: "Test",
350+ tools: [],
351+ messages: [userMsg],
352+ cwd: tmp,
353+ signal: controller.signal,
354+ });
355+
356+ expect(result.stopReason).toBe("aborted");
357+ // Partial message should be in messages
358+ expect(result.messages).toHaveLength(2);
359+ const lastMsg = result.messages[
360+ result.messages.length - 1
361+ ] as AssistantMessage;
362+ expect(lastMsg.role).toBe("assistant");
363+ expect(lastMsg.stopReason).toBe("aborted");
364+
365+ // Should be persisted to DB
366+ const dbMessages = loadMessages(db, session.id);
367+ const dbLast = dbMessages[dbMessages.length - 1] as AssistantMessage;
368+ expect(dbLast.role).toBe("assistant");
369+ expect(dbLast.stopReason).toBe("aborted");
370+ });
371+
372+ test("handles LLM error gracefully", async () => {
373+ faux.setResponses([
374+ fauxAssistantMessage("", {
375+ stopReason: "error",
376+ errorMessage: "Rate limit exceeded",
377+ }),
378+ ]);
379+
380+ const session = createSession(db, { cwd: tmp });
381+ const userMsg = makeUser("trigger error");
382+ const turn = appendMessage(db, session.id, userMsg);
383+
384+ const result = await runAgentLoop({
385+ db,
386+ sessionId: session.id,
387+ turn,
388+ model: faux.getModel(),
389+ systemPrompt: "Test",
390+ tools: [],
391+ messages: [userMsg],
392+ cwd: tmp,
393+ });
394+
395+ expect(result.stopReason).toBe("error");
396+ });
397+
398+ test("handles unknown tool name gracefully", async () => {
399+ faux.setResponses([
400+ fauxAssistantMessage([fauxToolCall("nonexistent_tool", { foo: "bar" })], {
401+ stopReason: "toolUse",
402+ }),
403+ fauxAssistantMessage("I see the error, let me try again."),
404+ ]);
405+
406+ const session = createSession(db, { cwd: tmp });
407+ const userMsg = makeUser("test unknown tool");
408+ const turn = appendMessage(db, session.id, userMsg);
409+
410+ const result = await runAgentLoop({
411+ db,
412+ sessionId: session.id,
413+ turn,
414+ model: faux.getModel(),
415+ systemPrompt: "Test",
416+ tools: builtinTools(),
417+ messages: [userMsg],
418+ cwd: tmp,
419+ });
420+
421+ // Should still complete — the error tool result lets the model self-correct
422+ expect(result.messages.length).toBeGreaterThanOrEqual(4);
423+ // The tool result should be an error
424+ const toolResult = result.messages.find((m) => m.role === "toolResult");
425+ expect(toolResult).toBeDefined();
426+ if (toolResult?.role === "toolResult") {
427+ expect(toolResult.isError).toBe(true);
428+ }
429+ });
430+
431+ test("stopReason 'length' returns without looping", async () => {
432+ faux.setResponses([
433+ fauxAssistantMessage("Partial response that hit the token limit", {
434+ stopReason: "length",
435+ }),
436+ ]);
437+
438+ const session = createSession(db, { cwd: tmp });
439+ const userMsg = makeUser("big question");
440+ const turn = appendMessage(db, session.id, userMsg);
441+
442+ const result = await runAgentLoop({
443+ db,
444+ sessionId: session.id,
445+ turn,
446+ model: faux.getModel(),
447+ systemPrompt: "Test",
448+ tools: [],
449+ messages: [userMsg],
450+ cwd: tmp,
451+ });
452+
453+ expect(result.stopReason).toBe("length");
454+ expect(result.messages).toHaveLength(2);
455+ });
456+});