From 4d7ec38102594936261663303a40fca0332351fd Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Sat, 31 Oct 2026 22:30:37 -0500 Subject: [PATCH] Test the agent loop and the fake provider --- build.zig | 28 ++- src/agent.zig | 434 +++++++++++++++++++++++++++++++++++++++++ src/provider.zig | 38 ++++ src/providers.zig | 1 + src/providers/fake.zig | 228 ++++++++++++++++++++++ 5 files changed, 719 insertions(+), 10 deletions(-) create mode 100644 src/agent.zig create mode 100644 src/provider.zig create mode 100644 src/providers.zig create mode 100644 src/providers/fake.zig diff --git a/build.zig b/build.zig index d30224c..24ee003 100644 --- a/build.zig +++ b/build.zig @@ -1,18 +1,26 @@ const std = @import("std"); +/// Test roots: each file runs its own tests and the tests of the modules it imports. +const test_roots = [_][]const u8{ + "src/tools.zig", + "src/agent.zig", + "src/providers.zig", +}; + pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const tools_tests = b.addTest(.{ - .root_module = b.createModule(.{ - .root_source_file = b.path("src/tools.zig"), - .target = target, - .optimize = optimize, - }), - }); - const run_tools_tests = b.addRunArtifact(tools_tests); - const test_step = b.step("test", "Run tests"); - test_step.dependOn(&run_tools_tests.step); + for (test_roots) |root| { + const tests = b.addTest(.{ + .root_module = b.createModule(.{ + .root_source_file = b.path(root), + .target = target, + .optimize = optimize, + }), + }); + const run_tests = b.addRunArtifact(tests); + test_step.dependOn(&run_tests.step); + } } diff --git a/src/agent.zig b/src/agent.zig new file mode 100644 index 0000000..2d6f166 --- /dev/null +++ b/src/agent.zig @@ -0,0 +1,434 @@ +const std = @import("std"); + +const provider = @import("provider.zig"); +const tools = @import("tools.zig"); + +pub const ToolCall = struct { + id: []const u8, + name: []const u8, + args_json: []const u8, +}; + +pub const Assistant = struct { + text: []const u8, + tool_calls: []const ToolCall, +}; + +pub const ToolResult = struct { + call_id: []const u8, + text: []const u8, +}; + +pub const Message = union(enum) { + user: []const u8, + assistant: Assistant, + tool_result: ToolResult, +}; + +pub const NoticeLevel = enum { err, interrupted }; + +pub const Event = union(enum) { + text_delta: []const u8, + thinking_delta: []const u8, + tool_call: ToolCall, + tool_result: struct { call_id: []const u8, output: tools.Output }, + notice: struct { level: NoticeLevel, text: []const u8 }, + run_end, +}; + +/// The receiver owns every string in an event and frees it with its own allocator. +pub const Sink = struct { + ctx: *anyopaque, + send: *const fn (ctx: *anyopaque, event: Event) void, +}; + +/// Safety cap for one user prompt. +pub const max_requests = 50; + +pub const Agent = struct { + gpa: std.mem.Allocator, + io: std.Io, + provider: provider.Provider, + abort: *const std.atomic.Value(bool), + system_prompt: []const u8, + history: std.ArrayList(Message) = .empty, + + pub fn init( + gpa: std.mem.Allocator, + io: std.Io, + streamer: provider.Provider, + abort: *const std.atomic.Value(bool), + system_prompt: []const u8, + ) Agent { + return .{ + .gpa = gpa, + .io = io, + .provider = streamer, + .abort = abort, + .system_prompt = system_prompt, + }; + } + + pub fn deinit(self: *Agent) void { + for (self.history.items) |message| freeMessage(self.gpa, message); + self.history.deinit(self.gpa); + } + + /// Runs one prompt: every failure becomes a notice event and a run_end event always follows. + pub fn run(self: *Agent, prompt: []const u8, sink: Sink) void { + _ = self; + _ = prompt; + _ = sink; + } +}; + +fn freeMessage(gpa: std.mem.Allocator, message: Message) void { + switch (message) { + .user => |text| gpa.free(text), + .assistant => |assistant| { + gpa.free(assistant.text); + for (assistant.tool_calls) |call| freeToolCall(gpa, call); + gpa.free(assistant.tool_calls); + }, + .tool_result => |result| { + gpa.free(result.call_id); + gpa.free(result.text); + }, + } +} + +fn freeToolCall(gpa: std.mem.Allocator, call: ToolCall) void { + gpa.free(call.id); + gpa.free(call.name); + gpa.free(call.args_json); +} + +const testing = std.testing; + +test "the agent loop forwards a text-only run" { + var provider_state = Scripted.init(testing.allocator, &.{ + .{ .text = &.{"pong"}, .finish = .stop }, + }); + defer provider_state.deinit(); + + var abort = std.atomic.Value(bool).init(false); + var agent: Agent = .init(testing.allocator, testing.io, provider_state.asProvider(), &abort, "system"); + defer agent.deinit(); + + var collector = Collector.init(testing.allocator); + defer collector.deinit(); + + agent.run("ping", collector.sink()); + _ = try testing.expect(collector.count(.text_delta) == 1); + _ = try testing.expect(collector.count(.run_end) == 1); + _ = try testing.expect(collector.notices() == 0); + try testing.expectEqual(@as(usize, 2), agent.history.items.len); + try testing.expectEqualStrings("pong", agent.history.items[1].assistant.text); +} + +const Scripted = struct { + gpa: std.mem.Allocator, + runs: []const Run, + position: usize = 0, + cycle: bool = false, + + const Run = struct { + text: []const []const u8 = &.{}, + thinking: []const []const u8 = &.{}, + calls: []const Call = &.{}, + finish: provider.Finish = .stop, + fail: ?provider.StreamError = null, + interrupt_midway: bool = false, + }; + + const Call = struct { + id: []const u8, + name: []const u8, + args_json: []const u8, + }; + + fn init(gpa: std.mem.Allocator, runs: []const Run) Scripted { + return .{ .gpa = gpa, .runs = runs }; + } + + fn deinit(self: *Scripted) void { + _ = self; + } + + fn asProvider(self: *Scripted) provider.Provider { + return .{ .ctx = self, .stream = stream }; + } + + /// Set by the scripted stream when it wants the loop to observe an interrupt. + var abort_target: ?*std.atomic.Value(bool) = null; + + fn stream(ctx: *anyopaque, request: provider.Request, out: *provider.StreamSink, abort: *const std.atomic.Value(bool)) provider.StreamError!provider.Finish { + _ = request; + const self: *Scripted = @ptrCast(@alignCast(ctx)); + const run = if (self.cycle) self.runs[self.position % self.runs.len] else self.runs[self.position]; + self.position += 1; + for (run.text) |delta| { + if (run.interrupt_midway) { + out.text_delta(out.ctx, delta); + @constCast(abort).store(true, .release); + return error.Aborted; + } + out.text_delta(out.ctx, delta); + } + for (run.thinking) |delta| out.thinking_delta(out.ctx, delta); + for (run.calls) |call| out.tool_call(out.ctx, .{ .id = call.id, .name = call.name, .args_json = call.args_json }); + if (run.fail) |failure| return failure; + return run.finish; + } +}; + +const Collector = struct { + gpa: std.mem.Allocator, + events: std.ArrayList(Owned) = .empty, + + const Owned = struct { + kind: Kind, + text: []const u8 = "", + call_id: []const u8 = "", + }; + + const Kind = enum { text_delta, thinking_delta, tool_call, tool_result, err_notice, interrupted_notice, run_end }; + + fn init(gpa: std.mem.Allocator) Collector { + return .{ .gpa = gpa }; + } + + fn deinit(self: *Collector) void { + for (self.events.items) |event| { + if (event.text.len > 0) self.gpa.free(event.text); + if (event.call_id.len > 0) self.gpa.free(event.call_id); + } + self.events.deinit(self.gpa); + } + + fn sink(self: *Collector) Sink { + return .{ .ctx = self, .send = send }; + } + + fn send(ctx: *anyopaque, event: Event) void { + const self: *Collector = @ptrCast(@alignCast(ctx)); + var owned: Owned = .{ .kind = .run_end }; + switch (event) { + .text_delta => |text| owned = .{ .kind = .text_delta, .text = text }, + .thinking_delta => |text| owned = .{ .kind = .thinking_delta, .text = text }, + .tool_call => |call| owned = .{ .kind = .tool_call, .text = call.name, .call_id = call.id }, + .tool_result => |result| owned = .{ .kind = .tool_result, .text = result.output.text, .call_id = result.call_id }, + .notice => |notice| owned = .{ + .kind = if (notice.level == .err) .err_notice else .interrupted_notice, + .text = notice.text, + }, + .run_end => {}, + } + self.events.append(self.gpa, owned) catch return; + } + + fn count(self: Collector, kind: Kind) usize { + var total: usize = 0; + for (self.events.items) |event| { + if (event.kind == kind) total += 1; + } + return total; + } + + fn notices(self: Collector) usize { + return self.count(.err_notice) + self.count(.interrupted_notice); + } +}; + +fn scratchFile(arena: std.mem.Allocator, name: []const u8) ![]const u8 { + var tmp = testing.tmpDir(.{}); + var buffer: [std.fs.max_path_bytes]u8 = undefined; + const length = try tmp.dir.realPath(testing.io, &buffer); + return std.fs.path.join(arena, &.{ buffer[0..length], name }); +} + +fn toolCall(id: []const u8, name: []const u8, args_json: []const u8) Scripted.Call { + return .{ .id = id, .name = name, .args_json = args_json }; +} + +test "a tool call runs and the loop continues to a final answer" { + var arena_state = std.heap.ArenaAllocator.init(testing.allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + const path = try scratchFile(arena, "agent.txt"); + const args = try std.fmt.allocPrint(arena, "{{\"path\":\"{s}\",\"content\":\"hi\"}}", .{path}); + + var scripted = Scripted.init(testing.allocator, &.{ + .{ .calls = &.{toolCall("call-1", "write", args)}, .finish = .tool_calls }, + .{ .text = &.{"done"}, .finish = .stop }, + }); + var abort = std.atomic.Value(bool).init(false); + var agent: Agent = .init(testing.allocator, testing.io, scripted.asProvider(), &abort, "system"); + defer agent.deinit(); + var collector = Collector.init(testing.allocator); + defer collector.deinit(); + + agent.run("go", collector.sink()); + + _ = try testing.expectEqual(@as(usize, 4), agent.history.items.len); + try testing.expectEqualStrings("done", agent.history.items[3].assistant.text); + try testing.expectEqualStrings("call-1", agent.history.items[2].tool_result.call_id); + try testing.expect(std.mem.startsWith(u8, agent.history.items[2].tool_result.text, "wrote 2 bytes to ")); + _ = try testing.expectEqual(@as(usize, 1), collector.count(.tool_call)); + _ = try testing.expectEqual(@as(usize, 1), collector.count(.tool_result)); + _ = try testing.expectEqual(@as(usize, 0), collector.notices()); +} + +test "two tool calls in one message run in order" { + var scripted = Scripted.init(testing.allocator, &.{ + .{ + .calls = &.{ + toolCall("call-a", "bash", "{\"command\":\"echo a\",\"timeout\":10,\"cwd\":\".\"}"), + toolCall("call-b", "bash", "{\"command\":\"echo b\",\"timeout\":10,\"cwd\":\".\"}"), + }, + .finish = .tool_calls, + }, + .{ .text = &.{"done"}, .finish = .stop }, + }); + var abort = std.atomic.Value(bool).init(false); + var agent: Agent = .init(testing.allocator, testing.io, scripted.asProvider(), &abort, "system"); + defer agent.deinit(); + var collector = Collector.init(testing.allocator); + defer collector.deinit(); + + agent.run("go", collector.sink()); + + _ = try testing.expectEqual(@as(usize, 2), collector.count(.tool_result)); + try testing.expectEqualStrings("call-a", agent.history.items[2].tool_result.call_id); + try testing.expectEqualStrings("call-b", agent.history.items[3].tool_result.call_id); + try testing.expect(std.mem.startsWith(u8, agent.history.items[2].tool_result.text, "a\n[exit code 0")); +} + +test "a truncated stream answers every call with an error and keeps going" { + var scripted = Scripted.init(testing.allocator, &.{ + .{ + .calls = &.{toolCall("call-1", "bash", "{\"command\":\"echo hi\",\"timeout\":10,\"cwd\":\".\"}")}, + .finish = .length, + }, + .{ .text = &.{"done"}, .finish = .stop }, + }); + var abort = std.atomic.Value(bool).init(false); + var agent: Agent = .init(testing.allocator, testing.io, scripted.asProvider(), &abort, "system"); + defer agent.deinit(); + var collector = Collector.init(testing.allocator); + defer collector.deinit(); + + agent.run("go", collector.sink()); + + _ = try testing.expectEqual(@as(usize, 4), agent.history.items.len); + const result = agent.history.items[2].tool_result; + try testing.expect(std.mem.indexOf(u8, result.text, "truncated") != null); + try testing.expectEqualStrings("done", agent.history.items[3].assistant.text); + _ = try testing.expectEqual(@as(usize, 0), collector.count(.tool_result)); +} + +test "a provider error ends the run with a notice" { + var scripted = Scripted.init(testing.allocator, &.{ + .{ .fail = error.Http, .finish = .stop }, + }); + var abort = std.atomic.Value(bool).init(false); + var agent: Agent = .init(testing.allocator, testing.io, scripted.asProvider(), &abort, "system"); + defer agent.deinit(); + var collector = Collector.init(testing.allocator); + defer collector.deinit(); + + agent.run("go", collector.sink()); + + _ = try testing.expectEqual(@as(usize, 1), collector.count(.err_notice)); + _ = try testing.expectEqual(@as(usize, 1), collector.count(.run_end)); + _ = try testing.expectEqual(@as(usize, 1), agent.history.items.len); +} + +test "the loop stops at the request cap" { + var scripted = Scripted.init(testing.allocator, &.{ + .{ .calls = &.{toolCall("call-1", "bash", "{\"command\":\"true\",\"timeout\":10,\"cwd\":\".\"}")}, .finish = .tool_calls }, + }); + scripted.cycle = true; + var abort = std.atomic.Value(bool).init(false); + var agent: Agent = .init(testing.allocator, testing.io, scripted.asProvider(), &abort, "system"); + defer agent.deinit(); + var collector = Collector.init(testing.allocator); + defer collector.deinit(); + + agent.run("go", collector.sink()); + + _ = try testing.expectEqual(max_requests, scripted.position); + _ = try testing.expectEqual(@as(usize, 1), collector.count(.err_notice)); + _ = try testing.expectEqual(@as(usize, 1), collector.count(.run_end)); +} + +test "an interrupt while streaming drops the partial answer from the history" { + var scripted = Scripted.init(testing.allocator, &.{ + .{ .text = &.{"par"}, .finish = .stop, .interrupt_midway = true }, + }); + var abort = std.atomic.Value(bool).init(false); + var agent: Agent = .init(testing.allocator, testing.io, scripted.asProvider(), &abort, "system"); + defer agent.deinit(); + var collector = Collector.init(testing.allocator); + defer collector.deinit(); + + agent.run("go", collector.sink()); + + _ = try testing.expectEqual(@as(usize, 1), agent.history.items.len); + _ = try testing.expectEqual(@as(usize, 1), collector.count(.text_delta)); + _ = try testing.expectEqual(@as(usize, 1), collector.count(.interrupted_notice)); + _ = try testing.expectEqual(@as(usize, 1), collector.count(.run_end)); +} + +test "an interrupt during a tool keeps the history valid" { + var scripted = Scripted.init(testing.allocator, &.{ + .{ + .calls = &.{ + toolCall("call-1", "bash", "{\"command\":\"sleep 30\",\"timeout\":60,\"cwd\":\".\"}"), + toolCall("call-2", "bash", "{\"command\":\"echo two\",\"timeout\":10,\"cwd\":\".\"}"), + }, + .finish = .tool_calls, + }, + }); + var abort = std.atomic.Value(bool).init(false); + var agent: Agent = .init(testing.allocator, testing.io, scripted.asProvider(), &abort, "system"); + defer agent.deinit(); + var collector = Collector.init(testing.allocator); + defer collector.deinit(); + + const thread = try std.Thread.spawn(.{}, struct { + fn set(flag: *std.atomic.Value(bool)) void { + std.Io.sleep(testing.io, .fromMilliseconds(200), .awake) catch {}; + flag.store(true, .release); + } + }.set, .{&abort}); + defer thread.join(); + + agent.run("go", collector.sink()); + + _ = try testing.expectEqual(@as(usize, 3), agent.history.items.len); + try testing.expectEqualStrings("call-1", agent.history.items[2].tool_result.call_id); + try testing.expectEqualStrings("aborted by user", agent.history.items[2].tool_result.text); + _ = try testing.expectEqual(@as(usize, 2), collector.count(.tool_result)); + _ = try testing.expectEqual(@as(usize, 1), collector.count(.interrupted_notice)); +} + +test "a second prompt after an interrupt works" { + var scripted = Scripted.init(testing.allocator, &.{ + .{ .text = &.{"par"}, .finish = .stop, .interrupt_midway = true }, + .{ .text = &.{"pong"}, .finish = .stop }, + }); + var abort = std.atomic.Value(bool).init(false); + var agent: Agent = .init(testing.allocator, testing.io, scripted.asProvider(), &abort, "system"); + defer agent.deinit(); + var collector = Collector.init(testing.allocator); + defer collector.deinit(); + + agent.run("first", collector.sink()); + abort.store(false, .release); + agent.run("second", collector.sink()); + + _ = try testing.expectEqual(@as(usize, 3), agent.history.items.len); + try testing.expectEqualStrings("second", agent.history.items[1].user); + try testing.expectEqualStrings("pong", agent.history.items[2].assistant.text); +} diff --git a/src/provider.zig b/src/provider.zig new file mode 100644 index 0000000..8f3d694 --- /dev/null +++ b/src/provider.zig @@ -0,0 +1,38 @@ +const std = @import("std"); +const agent = @import("agent.zig"); +const tools = @import("tools.zig"); + +pub const Request = struct { + system: []const u8, + messages: []const agent.Message, + tools: []const tools.Definition, +}; + +pub const Finish = enum { stop, length, tool_calls }; + +pub const StreamSink = struct { + ctx: *anyopaque, + text_delta: *const fn (ctx: *anyopaque, delta: []const u8) void, + thinking_delta: *const fn (ctx: *anyopaque, delta: []const u8) void, + /// Called once the arguments of a call are complete. + tool_call: *const fn (ctx: *anyopaque, call: agent.ToolCall) void, +}; + +pub const StreamError = error{ Aborted, Http, Provider, OutOfMemory }; + +pub const Provider = struct { + ctx: *anyopaque, + stream: *const fn ( + ctx: *anyopaque, + request: Request, + out: *StreamSink, + abort: *const std.atomic.Value(bool), + ) StreamError!Finish, + /// Detail for the last Http or Provider error, valid until the next call. + last_error: *const fn (ctx: *anyopaque) []const u8 = noError, + + fn noError(ctx: *anyopaque) []const u8 { + _ = ctx; + return ""; + } +}; diff --git a/src/providers.zig b/src/providers.zig new file mode 100644 index 0000000..5a5e888 --- /dev/null +++ b/src/providers.zig @@ -0,0 +1 @@ +pub const fake = @import("providers/fake.zig"); diff --git a/src/providers/fake.zig b/src/providers/fake.zig new file mode 100644 index 0000000..facc523 --- /dev/null +++ b/src/providers/fake.zig @@ -0,0 +1,228 @@ +//! A scripted provider for offline UI and loop work. + +const std = @import("std"); +const agent = @import("../agent.zig"); +const provider = @import("../provider.zig"); +const tools = @import("../tools.zig"); + +pub const Fake = struct { + io: std.Io, + /// Delay between characters for the `slow` script. + slow_delay: std.Io.Duration = .fromSeconds(1), + /// Requests already served for the current prompt. + step: u8 = 0, + + pub fn asProvider(self: *Fake) provider.Provider { + return .{ .ctx = self, .stream = stream }; + } + + fn stream( + ctx: *anyopaque, + request: provider.Request, + out: *provider.StreamSink, + abort: *const std.atomic.Value(bool), + ) provider.StreamError!provider.Finish { + const self: *Fake = @ptrCast(@alignCast(ctx)); + const prompt = lastUser(request.messages); + + if (std.mem.eql(u8, prompt, "ping")) return say(out, "pong", .stop); + if (std.mem.eql(u8, prompt, "tools")) return self.toolTour(out); + if (std.mem.eql(u8, prompt, "sleep")) return sleepCall(out); + if (std.mem.eql(u8, prompt, "slow")) return self.slow(out, abort); + + out.text_delta(out.ctx, prompt); + return .stop; + } + + fn toolTour(self: *Fake, out: *provider.StreamSink) provider.StreamError!provider.Finish { + if (self.step > 0) return say(out, "done", .stop); + self.step = 1; + out.text_delta(out.ctx, "running the tools"); + out.tool_call(out.ctx, .{ + .id = "call-read", + .name = "read", + .args_json = "{\"files\":[{\"path\":\"README.md\"}]}", + }); + out.tool_call(out.ctx, .{ + .id = "call-write", + .name = "write", + .args_json = "{\"path\":\"fake.txt\",\"content\":\"hi\"}", + }); + out.tool_call(out.ctx, .{ + .id = "call-edit", + .name = "edit", + .args_json = "{\"files\":[{\"path\":\"fake.txt\",\"changes\":[{\"pos\":0,\"old\":\"hi\",\"new\":\"ho\"}]}]}", + }); + out.tool_call(out.ctx, .{ + .id = "call-bash", + .name = "bash", + .args_json = "{\"command\":\"echo fake\",\"timeout\":10,\"cwd\":\".\"}", + }); + return .tool_calls; + } + + fn slow(self: *Fake, out: *provider.StreamSink, abort: *const std.atomic.Value(bool)) provider.StreamError!provider.Finish { + for (0..60) |_| { + if (abort.load(.acquire)) return error.Aborted; + out.text_delta(out.ctx, "."); + self.io.sleep(self.slow_delay, .awake) catch {}; + } + return .stop; + } + + fn say(out: *provider.StreamSink, text: []const u8, finish: provider.Finish) provider.Finish { + out.text_delta(out.ctx, text); + return finish; + } + + fn sleepCall(out: *provider.StreamSink) provider.Finish { + out.tool_call(out.ctx, .{ + .id = "call-sleep", + .name = "bash", + .args_json = "{\"command\":\"sleep 60\",\"timeout\":120,\"cwd\":\".\"}", + }); + return .tool_calls; + } + + fn lastUser(messages: []const agent.Message) []const u8 { + var index = messages.len; + while (index > 0) { + index -= 1; + switch (messages[index]) { + .user => |text| return text, + else => {}, + } + } + return ""; + } +}; + +const testing = std.testing; + +const Recorder = struct { + gpa: std.mem.Allocator, + text: std.ArrayList(u8) = .empty, + calls: std.ArrayList(agent.ToolCall) = .empty, + + fn init(gpa: std.mem.Allocator) Recorder { + return .{ .gpa = gpa }; + } + + fn deinit(self: *Recorder) void { + self.text.deinit(self.gpa); + for (self.calls.items) |call| { + self.gpa.free(call.id); + self.gpa.free(call.name); + self.gpa.free(call.args_json); + } + self.calls.deinit(self.gpa); + } + + fn sink(self: *Recorder) provider.StreamSink { + return .{ .ctx = self, .text_delta = onText, .thinking_delta = onText, .tool_call = onCall }; + } + + fn onText(ctx: *anyopaque, delta: []const u8) void { + const self: *Recorder = @ptrCast(@alignCast(ctx)); + self.text.appendSlice(self.gpa, delta) catch {}; + } + + fn onCall(ctx: *anyopaque, call: agent.ToolCall) void { + const self: *Recorder = @ptrCast(@alignCast(ctx)); + self.calls.append(self.gpa, .{ + .id = self.gpa.dupe(u8, call.id) catch return, + .name = self.gpa.dupe(u8, call.name) catch return, + .args_json = self.gpa.dupe(u8, call.args_json) catch return, + }) catch {}; + } +}; + +fn runScript(fake: *Fake, prompt: []const u8, recorder: *Recorder) !provider.Finish { + const user = [_]agent.Message{.{ .user = prompt }}; + var abort = std.atomic.Value(bool).init(false); + var sink = recorder.sink(); + return fake.asProvider().stream(fake, .{ + .system = "system", + .messages = &user, + .tools = &tools.definitions, + }, &sink, &abort); +} + +test "ping streams pong" { + const gpa = testing.allocator; + var fake: Fake = .{ .io = testing.io }; + var recorder = Recorder.init(gpa); + defer recorder.deinit(); + + try testing.expectEqual(provider.Finish.stop, try runScript(&fake, "ping", &recorder)); + try testing.expectEqualStrings("pong", recorder.text.items); +} + +test "tools makes four calls and then a final text" { + const gpa = testing.allocator; + var fake: Fake = .{ .io = testing.io }; + var recorder = Recorder.init(gpa); + defer recorder.deinit(); + + try testing.expectEqual(provider.Finish.tool_calls, try runScript(&fake, "tools", &recorder)); + try testing.expectEqual(@as(usize, 4), recorder.calls.items.len); + try testing.expectEqualStrings("read", recorder.calls.items[0].name); + try testing.expectEqualStrings("write", recorder.calls.items[1].name); + try testing.expectEqualStrings("edit", recorder.calls.items[2].name); + try testing.expectEqualStrings("bash", recorder.calls.items[3].name); + + recorder.text.clearRetainingCapacity(); + try testing.expectEqual(provider.Finish.stop, try runScript(&fake, "tools", &recorder)); + try testing.expectEqual(@as(usize, 0), recorder.calls.items.len); + try testing.expectEqualStrings("done", recorder.text.items); +} + +test "sleep asks for a bash sleep call" { + const gpa = testing.allocator; + var fake: Fake = .{ .io = testing.io }; + var recorder = Recorder.init(gpa); + defer recorder.deinit(); + + try testing.expectEqual(provider.Finish.tool_calls, try runScript(&fake, "sleep", &recorder)); + try testing.expectEqual(@as(usize, 1), recorder.calls.items.len); + try testing.expectEqualStrings("bash", recorder.calls.items[0].name); + try testing.expect(std.mem.indexOf(u8, recorder.calls.items[0].args_json, "sleep 60") != null); +} + +test "other prompts are echoed" { + const gpa = testing.allocator; + var fake: Fake = .{ .io = testing.io }; + var recorder = Recorder.init(gpa); + defer recorder.deinit(); + + try testing.expectEqual(provider.Finish.stop, try runScript(&fake, "hello there", &recorder)); + try testing.expectEqualStrings("hello there", recorder.text.items); +} + +test "slow streams until the abort flag is set" { + const gpa = testing.allocator; + var fake: Fake = .{ .io = testing.io, .slow_delay = .fromMilliseconds(1) }; + var recorder = Recorder.init(gpa); + defer recorder.deinit(); + + var abort = std.atomic.Value(bool).init(false); + const user = [_]agent.Message{.{ .user = "slow" }}; + var sink = recorder.sink(); + defer abort.store(true, .release); + + const thread = try std.Thread.spawn(.{}, struct { + fn set(flag: *std.atomic.Value(bool)) void { + std.Io.sleep(testing.io, .fromMilliseconds(20), .awake) catch {}; + flag.store(true, .release); + } + }.set, .{&abort}); + defer thread.join(); + + try testing.expectError(error.Aborted, fake.asProvider().stream(&fake, .{ + .system = "system", + .messages = &user, + .tools = &tools.definitions, + }, &sink, &abort)); + try testing.expect(recorder.text.items.len > 0); + try testing.expect(recorder.text.items.len < 60); +} -- knotmirror 0.0.1