@@ -11,7 +11,9 @@ const max_head = 10_000;
1111 const max_tail = 6_000;
1212 const truncated_marker = "\n\n... output truncated ...\n\n";
1313
14-const Args = struct { command: []const u8 };
14+const default_timeout_s = 120;
15+
16+const Args = struct { command: []const u8, timeout: ?u64 = null };
1517
1618 const Acc = struct {
1719 a: std.mem.Allocator,
@@ -90,15 +92,27 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
9092 var cancelled = false;
9193 var broken = false;
9294 var oom = false;
95+ var timed_out = false;
96+
97+ const timeout_s = args.timeout orelse default_timeout_s;
98+ const start = std.Io.Clock.awake.now(platform.io);
99+ const deadline = start.addDuration(.fromSeconds(@intCast(@min(timeout_s, std.math.maxInt(i64)))));
93100
94101 while (open[0] or open[1]) {
95102 if (ctx.cancel.load(.acquire)) {
96103 cancelled = true;
97104 break;
98105 }
106+ const now = std.Io.Clock.awake.now(platform.io);
107+ if (now.durationTo(deadline).nanoseconds <= 0) {
108+ timed_out = true;
109+ break;
110+ }
111+ const remaining = now.durationTo(deadline).toMilliseconds();
112+ const wait_ms: i32 = @intCast(@min(100, @max(1, remaining)));
99113 // A poll failure must not leave the command running: the tool returns
100114 // at once, so nothing would ever collect it or its process group.
101- const ready = std.posix.poll(&fds, 100) catch {
115+ const ready = std.posix.poll(&fds, wait_ms) catch {
102116 broken = true;
103117 break;
104118 };
@@ -129,7 +143,7 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
129143 if (oom) break;
130144 }
131145
132- if (cancelled or oom or broken) killGroup(pid);
146+ if (cancelled or timed_out or oom or broken) killGroup(pid);
133147 const term = child.wait(platform.io) catch std.process.Child.Term{ .unknown = 0 };
134148
135149 if (oom) return fail(a, "bash failed: out of memory", .{});
@@ -141,7 +155,9 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
141155 .exited => |c| c,
142156 else => null,
143157 };
144- const label: []const u8 = if (code) |c|
158+ const label: []const u8 = if (timed_out)
159+ std.fmt.allocPrint(scratch, "timed out after {d}s", .{timeout_s}) catch "timed out"
160+ else if (code) |c|
145161 std.fmt.allocPrint(scratch, "{d}", .{c}) catch "unknown"
146162 else if (cancelled)
147163 "aborted"