xonecas.io
79113
Ma

mini-coding-agent / commit 0a26613

fix: history newline fix, exapnd paths for llm tools

xonecas2026-10-17 0a26613210c93bfe8c468389470830a15decebd9 parent 0b71c6d

4 files changed

src/filesystem.zig+6−0
@@ -5,6 +5,12 @@ pub fn join(a: std.mem.Allocator, parts: []const []const u8) ![]u8 {
55 return std.fs.path.join(a, parts);
66 }
77
8+pub fn expandTilde(a: std.mem.Allocator, path: []const u8) []const u8 {
9+ if (!std.mem.startsWith(u8, path, "~/")) return path;
10+ const home = platform.home() orelse return path;
11+ return join(a, &.{ home, path[2..] }) catch path;
12+}
13+
814 pub fn readFileAlloc(a: std.mem.Allocator, path: []const u8, max: usize) ![]u8 {
915 return std.Io.Dir.cwd().readFileAlloc(platform.io, path, a, .limited(max));
1016 }
src/history.zig+32−4
@@ -15,12 +15,14 @@ pub const Store = struct {
1515 }
1616
1717 fn load(self: *Store) void {
18- const text = filesystem.readFileAlloc(self.a, self.path, 1 << 20) catch return;
19- var it = std.mem.splitScalar(u8, text, '\n');
18+ const raw = filesystem.readFileAlloc(self.a, self.path, 1 << 20) catch return;
19+ var it = std.mem.splitScalar(u8, raw, '\n');
2020 while (it.next()) |line| {
2121 const trimmed = std.mem.trim(u8, line, "\r");
2222 if (trimmed.len == 0) continue;
23- self.entries.append(self.a, self.a.dupe(u8, trimmed) catch return) catch return;
23+ const decoded = self.unescape(trimmed) catch continue;
24+ if (decoded.len == 0) continue;
25+ self.entries.append(self.a, self.a.dupe(u8, decoded) catch return) catch return;
2426 }
2527 }
2628
@@ -38,11 +40,37 @@ pub const Store = struct {
3840 self.flush();
3941 }
4042
43+ /// Escape backslash and newline so each entry stays one line in the file.
44+ fn escape(self: *Store, e: []const u8) []const u8 {
45+ var out: std.ArrayList(u8) = .empty;
46+ for (e) |c| {
47+ if (c == '\\' or c == '\n') out.append(self.a, '\\') catch return e;
48+ out.append(self.a, if (c == '\n') 'n' else c) catch return e;
49+ }
50+ return out.items;
51+ }
52+
53+ fn unescape(self: *Store, s: []const u8) ![]const u8 {
54+ if (std.mem.indexOfScalar(u8, s, '\\') == null) return s;
55+ var out: std.ArrayList(u8) = .empty;
56+ var i: usize = 0;
57+ while (i < s.len) {
58+ if (s[i] == '\\' and i + 1 < s.len) {
59+ i += 1;
60+ out.append(self.a, if (s[i] == 'n') '\n' else s[i]) catch return s;
61+ } else {
62+ out.append(self.a, s[i]) catch return s;
63+ }
64+ i += 1;
65+ }
66+ return out.items;
67+ }
68+
4169 fn flush(self: *Store) void {
4270 const start = if (self.entries.items.len > self.max) self.entries.items.len - self.max else 0;
4371 var out: std.ArrayList(u8) = .empty;
4472 for (self.entries.items[start..]) |e| {
45- out.appendSlice(self.a, e) catch return;
73+ out.appendSlice(self.a, self.escape(e)) catch return;
4674 out.append(self.a, '\n') catch return;
4775 }
4876 filesystem.writeFile(self.path, out.items) catch {};
src/tools/edit.zig+1−1
@@ -44,7 +44,7 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
4444 const args = std.json.parseFromSliceLeaky(Args, scratch, args_json, .{ .ignore_unknown_fields = true }) catch {
4545 return fail(a, "edit failed: path, oldText and newText must be strings", .{});
4646 };
47- const path = args.path;
47+ const path = filesystem.expandTilde(scratch, args.path);
4848
4949 if (args.oldText.len == 0) {
5050 if (filesystem.fileExists(path)) return fail(a, "edit failed: {s} already exists", .{path});
src/tools/read.zig+1−1
@@ -106,7 +106,7 @@ fn bodyLine(a: std.mem.Allocator, args: Args, lines: usize, truncated: bool) ?[]
106106 pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const u8, ctx: tools.Context) tools.Result {
107107 const args = std.json.parseFromSliceLeaky(Args, scratch, args_json, .{ .ignore_unknown_fields = true }) catch
108108 return fail(a, "read failed: invalid arguments", .{});
109- const path = args.path;
109+ const path = filesystem.expandTilde(scratch, args.path);
110110 if (args.offset) |o| if (o < 1) return fail(a, "read failed: offset must be at least 1", .{});
111111 if (args.range) |r| if (r < 1) return fail(a, "read failed: range must be at least 1", .{});