xonecas.io
79113
Ma

mini-coding-agent / commit dd21933

fix: read() is more forgiving now

xonecas2026-10-26 dd21933c71713a6c3a9c157ab8ee1310f8bd93f5 parent abed8fe

4 files changed

src/filesystem.zig+10−0
@@ -15,6 +15,16 @@ pub fn readFileAlloc(a: std.mem.Allocator, path: []const u8, max: usize) ![]u8 {
1515 return std.Io.Dir.cwd().readFileAlloc(platform.io, path, a, .limited(max));
1616 }
1717
18+/// Reads at most `max` bytes from the start of `path`; a larger file is
19+/// clipped instead of failing.
20+pub fn readFilePrefix(a: std.mem.Allocator, path: []const u8, max: usize) ![]u8 {
21+ const file = try std.Io.Dir.cwd().openFile(platform.io, path, .{});
22+ defer file.close(platform.io);
23+ const size = (try file.stat(platform.io)).size;
24+ const buf = try a.alloc(u8, @intCast(@min(size, max)));
25+ return buf[0..try file.readPositionalAll(platform.io, buf, 0)];
26+}
27+
1828 pub fn writeFile(path: []const u8, data: []const u8) !void {
1929 if (std.fs.path.dirname(path)) |dir| {
2030 std.Io.Dir.cwd().createDirPath(platform.io, dir) catch {};
src/tools.zig+12−0
@@ -59,6 +59,18 @@ pub fn fail(a: std.mem.Allocator, fallback: []const u8, comptime fmt: []const u8
5959 };
6060 }
6161
62+/// Failure to touch `path`; the common filesystem errors get a plain sentence
63+/// instead of a bare error name.
64+pub fn ioFail(a: std.mem.Allocator, what: []const u8, path: []const u8, e: anyerror) Result {
65+ const message = switch (e) {
66+ error.FileNotFound => std.fmt.allocPrint(a, "{s} failed: {s} does not exist", .{ what, path }),
67+ error.IsDir => std.fmt.allocPrint(a, "{s} failed: {s} is a directory", .{ what, path }),
68+ error.AccessDenied, error.PermissionDenied => std.fmt.allocPrint(a, "{s} failed: {s}: permission denied", .{ what, path }),
69+ else => std.fmt.allocPrint(a, "{s} failed: {s}: {s}", .{ what, path, @errorName(e) }),
70+ } catch return .{ .text = what, .is_error = true };
71+ return .{ .text = message, .is_error = true };
72+}
73+
6274 pub const Kind = enum { string, integer };
6375
6476 pub const Param = struct {
src/tools/edit.zig+3−5
@@ -48,7 +48,7 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
4848 if (args.oldText.len == 0) {
4949 if (filesystem.fileExists(path)) return fail(a, "edit failed: {s} already exists", .{path});
5050 if (ctx.cancel.load(.acquire)) return fail(a, "edit cancelled", .{});
51- filesystem.writeFile(path, args.newText) catch |e| return fail(a, "edit failed: {s}: {s}", .{ path, @errorName(e) });
51+ filesystem.writeFile(path, args.newText) catch |e| return tools.ioFail(a, "edit", path, e);
5252 const cleaned = text.utf8Clean(scratch, args.newText) catch return fail(a, "edit failed: out of memory", .{});
5353 const patch = diff.unified(scratch, "", cleaned) catch return fail(a, "edit failed: out of memory", .{});
5454 return .{
@@ -57,9 +57,7 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
5757 };
5858 }
5959
60- const before = filesystem.readFileAlloc(scratch, path, 1 << 30) catch |e| {
61- return fail(a, "edit failed: {s}: {s}", .{ path, @errorName(e) });
62- };
60+ const before = filesystem.readFileAlloc(scratch, path, 1 << 30) catch |e| return tools.ioFail(a, "edit", path, e);
6361 const at = std.mem.indexOf(u8, before, args.oldText) orelse
6462 return fail(a, "edit failed: oldText not found in {s}", .{path});
6563 var count: usize = 1;
@@ -73,7 +71,7 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
7371 const after = std.mem.concat(scratch, u8, &.{ before[0..at], args.newText, before[at + args.oldText.len ..] }) catch
7472 return fail(a, "edit failed: out of memory", .{});
7573 if (ctx.cancel.load(.acquire)) return fail(a, "edit cancelled", .{});
76- filesystem.writeFile(path, after) catch |e| return fail(a, "edit failed: {s}: {s}", .{ path, @errorName(e) });
74+ filesystem.writeFile(path, after) catch |e| return tools.ioFail(a, "edit", path, e);
7775 const clean_before = text.utf8Clean(scratch, before) catch return fail(a, "edit failed: out of memory", .{});
7876 const clean_after = text.utf8Clean(scratch, after) catch return fail(a, "edit failed: out of memory", .{});
7977 const patch = diff.unified(scratch, clean_before, clean_after) catch return fail(a, "edit failed: out of memory", .{});
src/tools/read.zig+33−6
@@ -49,11 +49,18 @@ const image_mime = [_]struct { ext: []const u8, mime: []const u8 }{
4949
5050 const max_text_chars = 100_000;
5151 const max_image_base64_bytes = 5 * 1024 * 1024;
52+const max_file_bytes = 1 << 30;
53+const clipped_file_bytes = max_text_chars * 4;
5254
5355 fn fail(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) tools.Result {
5456 return tools.fail(a, "read failed", fmt, args);
5557 }
5658
59+/// A non-error result the model reads as information, not as a failure.
60+fn warn(a: std.mem.Allocator, comptime fmt: []const u8, args: anytype) tools.Result {
61+ return .{ .text = std.fmt.allocPrint(a, fmt, args) catch "read", .is_error = false };
62+}
63+
5764 fn mimeFor(path: []const u8) ?[]const u8 {
5865 const ext = std.fs.path.extension(path);
5966 for (image_mime) |entry| {
@@ -110,10 +117,22 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
110117 if (args.offset) |o| if (o < 1) return fail(a, "read failed: offset must be at least 1", .{});
111118 if (args.range) |r| if (r < 1) return fail(a, "read failed: range must be at least 1", .{});
112119
113- const data = filesystem.readFileAlloc(a, path, 1 << 30) catch |e|
114- return fail(a, "read failed: {s}: {s}", .{ path, @errorName(e) });
120+ var clipped = false;
121+ const data = read: {
122+ break :read filesystem.readFileAlloc(a, path, max_file_bytes) catch |e| switch (e) {
123+ error.StreamTooLong => {
124+ clipped = true;
125+ break :read filesystem.readFilePrefix(a, path, clipped_file_bytes) catch |e2|
126+ return tools.ioFail(a, "read", path, e2);
127+ },
128+ else => return tools.ioFail(a, "read", path, e),
129+ };
130+ };
115131
116132 if (mimeFor(path)) |mime| {
133+ if (clipped) {
134+ return fail(a, "read failed: {s} is over {d} bytes, too large to read as an image", .{ path, max_file_bytes });
135+ }
117136 if (!ctx.supports_images) {
118137 return fail(a, "read failed: {s} is an image and the current model does not accept image input", .{path});
119138 }
@@ -140,8 +159,12 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
140159
141160 const content = text.utf8Clean(a, data) catch return fail(a, "read failed: out of memory", .{});
142161 const offset = args.offset orelse 1;
143- const slice = lineSlice(content, offset, args.range) orelse
144- return fail(a, "read failed: {s} has fewer than {d} lines", .{ path, offset });
162+ const slice = lineSlice(content, offset, args.range) orelse {
163+ if (clipped) {
164+ return warn(a, "{s}: only the first {d} bytes were read, and offset {d} is past them", .{ path, content.len, offset });
165+ }
166+ return warn(a, "{s} has {d} lines; offset {d} is past the end", .{ path, countLines(content), offset });
167+ };
145168
146169 var sent = slice;
147170 if (slice.len > max_text_chars) {
@@ -149,9 +172,13 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
149172 while (cut > 0 and slice[cut] & 0xC0 == 0x80) cut -= 1;
150173 sent = slice[0..cut];
151174 }
152- const truncated = sent.len < slice.len;
175+ const truncated = clipped or sent.len < slice.len;
176+ const note = if (clipped)
177+ std.fmt.allocPrint(a, "... truncated ... only the first {d} bytes of {s} were read\n", .{ content.len, path }) catch "truncated"
178+ else
179+ std.fmt.allocPrint(a, "... truncated ... {s} is {d} characters\n", .{ path, slice.len }) catch "truncated";
153180 const result_text = if (truncated)
154- std.fmt.allocPrint(a, "{s}\n\n... truncated ... {s} is {d} characters\n", .{ sent, path, slice.len }) catch "read failed"
181+ std.fmt.allocPrint(a, "{s}\n\n{s}", .{ sent, note }) catch "read failed"
155182 else
156183 sent;
157184 return .{ .text = result_text, .is_error = false, .body = bodyLine(a, args, countLines(sent), truncated) };