//! The core as a child process of the UI. The UI writes requests to the //! child's input and reads messages from its output. const std = @import("std"); const protocol = @import("protocol.zig"); const remote = @import("remote.zig"); const Request = protocol.ui.Request; const Message = protocol.core.Message; const Allocator = std.mem.Allocator; const Io = std.Io; pub const Child = struct { gpa: Allocator, io: Io, process: std.process.Child, stdin: ?Io.File, stdout: Io.File, stderr: Io.File, in: Io.File.Reader, out: Io.File.Writer, in_buffer: [4096]u8, out_buffer: [4096]u8, end: remote.UiEnd, ui: protocol.Sink(Message), ended: protocol.Sink([]const u8), errors: Tail, listening: Io.Future(void), reading: Io.Future(void), /// How long a child may take to end after its input closed. const patience: Io.Duration = .fromSeconds(3); /// Starts `argv` as a core. The messages of the core go to `ui`. When the /// core's output ends, `ended` gets the last lines that the core wrote to /// its error output; `ended` owns that text. Nothing goes to `ui` after /// that. `stop` frees the child. pub fn start( gpa: Allocator, io: Io, argv: []const []const u8, ui: protocol.Sink(Message), ended: protocol.Sink([]const u8), ) !*Child { const self = try gpa.create(Child); errdefer gpa.destroy(self); var process = try std.process.spawn(io, .{ .argv = argv, .stdin = .pipe, .stdout = .pipe, .stderr = .pipe, }); // The child keeps the pipes, so that `wait` does not close a pipe // that a task reads. const stdin = process.stdin.?; const stdout = process.stdout.?; const stderr = process.stderr.?; process.stdin = null; process.stdout = null; process.stderr = null; self.* = .{ .gpa = gpa, .io = io, .process = process, .stdin = stdin, .stdout = stdout, .stderr = stderr, .in = undefined, .out = undefined, .in_buffer = undefined, .out_buffer = undefined, .end = undefined, .ui = ui, .ended = ended, .errors = .{}, .listening = undefined, .reading = undefined, }; self.in = stdout.readerStreaming(io, &self.in_buffer); self.out = stdin.writerStreaming(io, &self.out_buffer); self.end = .{ .gpa = gpa, .out = &self.out.interface }; errdefer { stdin.close(io); self.process.kill(io); stdout.close(io); stderr.close(io); } self.listening = try io.concurrent(listen, .{self}); errdefer self.listening.await(io); self.reading = try io.concurrent(read, .{self}); return self; } /// Where the UI sends its requests. Use it from one task at a time, and /// not after `stop`. pub fn sink(self: *Child) protocol.Sink(Request) { return self.end.sink(); } /// The last lines that the core wrote to its error output. Complete /// after `ended` got its text; call it from the task that reads that. pub fn lastOutput(self: *const Child) []const u8 { return self.errors.lastLines(8); } /// Ends the input of the core, waits until the core ended and frees the /// child. Kills a core that does not end. pub fn stop(self: *Child) void { const io = self.io; if (self.stdin) |stdin| { stdin.close(io); self.stdin = null; } self.reap(); self.reading.await(io); self.stdout.close(io); self.stderr.close(io); self.gpa.destroy(self); } fn reap(self: *Child) void { const io = self.io; const Done = union(enum) { exited: std.process.Child.WaitError!std.process.Child.Term, late: Io.Cancelable!void }; var buffer: [2]Done = undefined; var select: Io.Select(Done) = .init(io, &buffer); select.concurrent(.exited, std.process.Child.wait, .{ &self.process, io }) catch { self.process.kill(io); return; }; select.concurrent(.late, Io.sleep, .{ io, patience, .awake }) catch {}; const exited = if (select.await()) |first| first == .exited else |_| false; select.cancelDiscard(); if (!exited) { self.process.kill(io); } } fn listen(self: *Child) void { self.errors.fill(self.io, self.stderr) catch {}; } fn read(self: *Child) void { self.end.pump(&self.in.interface, self.ui) catch {}; // A core that waits for room in its output could not end. _ = self.in.interface.discardRemaining() catch {}; self.listening.await(self.io); const output = self.gpa.dupe(u8, self.lastOutput()) catch &.{}; if (!self.ended.send(output)) { self.gpa.free(output); } } }; /// The end of a stream of text. const Tail = struct { buffer: [2 * keep]u8 = undefined, length: usize = 0, const keep = 2048; fn fill(tail: *Tail, io: Io, file: Io.File) Io.File.Reader.Error!void { while (true) { if (tail.length == tail.buffer.len) { @memmove(tail.buffer[0..keep], tail.buffer[tail.buffer.len - keep ..]); tail.length = keep; } const size = file.readStreaming(io, &.{tail.buffer[tail.length..]}) catch |err| switch (err) { error.EndOfStream => return, else => |e| return e, }; tail.length += size; } } /// The last `count` lines, without the white space around them. fn lastLines(tail: *const Tail, count: usize) []const u8 { const text = std.mem.trim(u8, tail.buffer[0..tail.length], &std.ascii.whitespace); var start = text.len; for (0..count) |_| { start = std.mem.lastIndexOfScalar(u8, text[0..start], '\n') orelse return text; } return text[start + 1 ..]; } }; const testing = std.testing; const helpers = @import("testing.zig"); const Messages = helpers.EventLog(Message); fn tailOf(text: []const u8) Tail { var tail: Tail = .{}; @memcpy(tail.buffer[0..text.len], text); tail.length = text.len; return tail; } test "the last lines of a tail leave out earlier lines and the white space around" { try testing.expectEqualStrings("", tailOf("").lastLines(2)); try testing.expectEqualStrings("", tailOf("\n\n").lastLines(2)); try testing.expectEqualStrings("one", tailOf("one\n").lastLines(2)); try testing.expectEqualStrings("one\ntwo", tailOf("one\ntwo\n").lastLines(2)); try testing.expectEqualStrings("two\nthree", tailOf("one\ntwo\nthree\n\n").lastLines(2)); try testing.expectEqualStrings("three", tailOf("one\ntwo\nthree").lastLines(1)); } test "a tail keeps the end of a long stream" { const io = testing.io; const pipe = try remote.TestPipe.open(); defer pipe.read.close(io); var tail: Tail = .{}; var filling = try io.concurrent(Tail.fill, .{ &tail, io, pipe.read }); var buffer: [256]u8 = undefined; var writer = pipe.write.writerStreaming(io, &buffer); for (0..2000) |index| { try writer.interface.print("line {d}\n", .{index}); } try writer.interface.flush(); pipe.write.close(io); try filling.await(io); try testing.expectEqualStrings("line 1998\nline 1999", tail.lastLines(2)); } /// Keeps the text that a child reports when its core ended. const Ending = struct { happened: Io.Event = .unset, text: ?[]const u8 = null, fn deinit(self: *Ending) void { if (self.text) |text| testing.allocator.free(text); } fn sink(self: *Ending) protocol.Sink([]const u8) { return .{ .ctx = self, .sendFn = send }; } fn send(ctx: *anyopaque, text: []const u8) bool { const self: *Ending = @ptrCast(@alignCast(ctx)); self.text = text; self.happened.set(testing.io); return true; } fn wait(self: *Ending) ![]const u8 { try self.happened.waitTimeout(testing.io, .{ .duration = .{ .raw = .fromSeconds(10), .clock = .awake } }); return self.text.?; } }; /// The path of the jig that the build made. fn builtJig() ![]const u8 { return testing.environ.getAlloc(testing.allocator, "JIG_EXE") catch error.SkipZigTest; } test "the built jig with --embed is a core that answers a prompt and ends with its input" { var arena_state: std.heap.ArenaAllocator = .init(testing.allocator); defer arena_state.deinit(); const exe = try builtJig(); defer testing.allocator.free(exe); var messages: Messages = .{}; defer messages.deinit(); var ending: Ending = .{}; defer ending.deinit(); const child = try Child.start(testing.allocator, testing.io, &.{ exe, "--embed", "--model", "fake" }, messages.sink(), ending.sink()); var stopped = false; defer if (!stopped) child.stop(); const core = child.sink(); try testing.expect(core.send(.{ .id = 0, .method = .{ .attach = .{ .sink = messages.sink() } } })); try messages.waitFor(.response, 1); try testing.expectEqualStrings("fake", messages.events.items[0].response.result.attached.model); try testing.expect(core.send(.{ .id = 1, .method = .{ .submit = try testing.allocator.dupe(u8, "ping") } })); try messages.waitFor(.agent_end, 1); try testing.expectEqualStrings("pong", try messages.text(arena_state.allocator())); stopped = true; child.stop(); try testing.expectEqualStrings("", try ending.wait()); } test "a core that cannot start reports why" { const exe = try builtJig(); defer testing.allocator.free(exe); var messages: Messages = .{}; defer messages.deinit(); var ending: Ending = .{}; defer ending.deinit(); const child = try Child.start(testing.allocator, testing.io, &.{ exe, "--embed", "--model", "nope" }, messages.sink(), ending.sink()); defer child.stop(); try testing.expect(std.mem.startsWith(u8, try ending.wait(), "unknown model")); }