xonecas.io
79113
Ma

mini-coding-agent / commit 2baef73

feat(reverse history): you can search past inputs

xonecas2026-10-09 2baef7328da5e6169d77ae1d5114d3e162d70e07 parent da12484

7 files changed

README.md+3−0
@@ -54,6 +54,7 @@ tool output go to stderr, the assistant text to stdout; a failed turn exits 1.
5454 | `Esc` | pause the turn at the next step boundary |
5555 | `Ctrl+C` | cancel the turn |
5656 | `Ctrl+D` | exit on an empty draft |
57+| `Ctrl+R` | reverse search past input |
5758 | `Tab` | complete command or path |
5859
5960 While a turn is paused (`Esc`), submitting a message steers the running turn.
@@ -91,6 +92,7 @@ A full example:
9192 "tools": ["edit", "read", "bash"],
9293 "snapshotIgnoreDirs": [".git"],
9394 "snapshotUsesGitignore": true,
95+ "historySize": 100,
9496 "customProviders": [
9597 {
9698 "id": "local",
@@ -132,6 +134,7 @@ Keys:
132134 | `customProviders` | `[]` | in-tree provider definitions |
133135 | `snapshotIgnoreDirs` | `[]` | directory names skipped when the `bash` tool snapshots the cwd for diffs |
134136 | `snapshotUsesGitignore` | `true` | also skip directories named in `.gitignore` when snapshotting |
137+| `historySize` | `100` | past inputs kept in `<state dir>/mini-coding-agent/history` for `Ctrl+R` |
135138
136139 Unknown keys are rejected. `provider` and `model` must both be set to make a
137140 request; a custom provider must list the model in its `models`.
feedback.md+14−0
@@ -0,0 +1,14 @@
1+# Code/memory quality notes
2+
3+- No leak detection: `gpa` is never `deinit`'d, so `DebugAllocator` leak reports never fire. Leaks would be silent.
4+- **libc `regcomp` into a `[512]u8` opaque buffer** for tree-sitter `match?` predicates — clever but fragile and ABI-shaped.
5+- `tools.json` / `filesystem.listDir` have deeply nested `catch return out.written()` / `catch &.{}` chains — hard to read.
6+- `platform.printErr` heap-allocates per message instead of a stack `[512]u8` buffer.
7+
8+# Bugs
9+
10+`opencode-go/claude-haiku-5-5 · low` fails to stream:
11+
12+```json
13+{"type":"error","error":{"type":"invalid_request_error","message":"Upstream request failed: [invalid_request_error] \"thinking.type.enabled\" is not supported for this model. Use \"thinking.type.adaptive\" and \"output_config.effort\" to control thinking behavior."}}
14+```
src/config.zig+19−5
@@ -48,9 +48,11 @@ pub const Config = struct {
4848 custom_providers: []const CustomProvider,
4949 snapshot_ignore_dirs: []const []const u8,
5050 snapshot_uses_gitignore: bool,
51+ history_size: usize,
5152 };
5253
5354 const File = struct {
55+ historySize: ?usize = null,
5456 sessionsDir: ?[]const u8 = null,
5557 systemPrompt: ?[]const u8 = null,
5658 discoverAgentFiles: ?bool = null,
@@ -76,14 +78,24 @@ pub fn configPath(a: std.mem.Allocator) []const u8 {
7678 return filesystem.join(a, &.{ configDir(a), "config.json" }) catch "config.json";
7779 }
7880
79-fn sessionsDir(a: std.mem.Allocator) []const u8 {
80- const state = platform.getEnv("XDG_STATE_HOME") orelse blk: {
81- const home = platform.home() orelse return "sessions";
82- break :blk filesystem.join(a, &.{ home, ".local/state" }) catch return "sessions";
81+fn stateDir(a: std.mem.Allocator) []const u8 {
82+ return platform.getEnv("XDG_STATE_HOME") orelse blk: {
83+ const home = platform.home() orelse return "";
84+ break :blk filesystem.join(a, &.{ home, ".local/state" }) catch "";
8385 };
84- return filesystem.join(a, &.{ state, "mini-coding-agent/sessions" }) catch "sessions";
8586 }
8687
88+pub fn historyPath(a: std.mem.Allocator) []const u8 {
89+ const state = stateDir(a);
90+ if (state.len == 0) return "history";
91+ return filesystem.join(a, &.{ state, "mini-coding-agent/history" }) catch "history";
92+}
93+
94+fn sessionsDir(a: std.mem.Allocator) []const u8 {
95+ const state = stateDir(a);
96+ if (state.len == 0) return "sessions";
97+ return filesystem.join(a, &.{ state, "mini-coding-agent/sessions" }) catch "sessions";
98+}
8799 pub fn load(a: std.mem.Allocator, override: ?[]const u8) !Config {
88100 var cfg = try defaults(a);
89101 try readInto(a, configPath(a), false, &cfg);
@@ -125,6 +137,7 @@ fn readInto(a: std.mem.Allocator, path: []const u8, required: bool, cfg: *Config
125137 if (file.programStatus) |v| cfg.program_status = v;
126138 if (file.snapshotIgnoreDirs) |v| cfg.snapshot_ignore_dirs = v;
127139 if (file.snapshotUsesGitignore) |v| cfg.snapshot_uses_gitignore = v;
140+ if (file.historySize) |v| cfg.history_size = v;
128141 }
129142
130143 pub const Update = struct {
@@ -175,5 +188,6 @@ fn defaults(a: std.mem.Allocator) !Config {
175188 .custom_providers = &.{},
176189 .snapshot_ignore_dirs = &.{},
177190 .snapshot_uses_gitignore = true,
191+ .history_size = 100,
178192 };
179193 }
src/history.zig+50−0
@@ -0,0 +1,50 @@
1+const std = @import("std");
2+const platform = @import("platform.zig");
3+const filesystem = @import("filesystem.zig");
4+
5+pub const Store = struct {
6+ path: []const u8,
7+ max: usize,
8+ entries: std.ArrayList([]const u8) = .empty,
9+ a: std.mem.Allocator,
10+
11+ pub fn init(a: std.mem.Allocator, path: []const u8, max: usize) Store {
12+ var self = Store{ .path = path, .max = @max(max, 1), .a = a };
13+ self.load();
14+ return self;
15+ }
16+
17+ 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');
20+ while (it.next()) |line| {
21+ const trimmed = std.mem.trim(u8, line, "\r");
22+ if (trimmed.len == 0) continue;
23+ self.entries.append(self.a, self.a.dupe(u8, trimmed) catch return) catch return;
24+ }
25+ }
26+
27+ pub fn add(self: *Store, text: []const u8) void {
28+ const trimmed = std.mem.trim(u8, text, " \t\r\n");
29+ if (trimmed.len == 0) return;
30+ for (self.entries.items, 0..) |e, i| {
31+ if (std.mem.eql(u8, e, trimmed)) {
32+ _ = self.entries.orderedRemove(i);
33+ break;
34+ }
35+ }
36+ self.entries.append(self.a, self.a.dupe(u8, trimmed) catch return) catch return;
37+ while (self.entries.items.len > self.max) _ = self.entries.orderedRemove(0);
38+ self.flush();
39+ }
40+
41+ fn flush(self: *Store) void {
42+ const start = if (self.entries.items.len > self.max) self.entries.items.len - self.max else 0;
43+ var out: std.ArrayList(u8) = .empty;
44+ for (self.entries.items[start..]) |e| {
45+ out.appendSlice(self.a, e) catch return;
46+ out.append(self.a, '\n') catch return;
47+ }
48+ filesystem.writeFile(self.path, out.items) catch {};
49+ }
50+};
src/models.zig+10−2
@@ -200,13 +200,21 @@ pub fn listing(
200200 const info = db.info(provider_id, model.id) orelse continue;
201201 try out.append(a, .{ .id = model.id, .name = info.name });
202202 }
203- return out.toOwnedSlice(a);
203+ const items = try out.toOwnedSlice(a);
204+ std.mem.sort(Entry, items, {}, entryLess);
205+ return items;
206+}
207+
208+fn entryLess(_: void, a: Entry, b: Entry) bool {
209+ return std.mem.order(u8, a.id, b.id) == .lt;
204210 }
205211
206212 fn customListing(a: std.mem.Allocator, p: *const config.CustomProvider) ![]Entry {
207213 var out: std.ArrayList(Entry) = .empty;
208214 for (p.models) |m| try out.append(a, .{ .id = m.id, .name = m.name orelse m.id });
209- return out.toOwnedSlice(a);
215+ const items = try out.toOwnedSlice(a);
216+ std.mem.sort(Entry, items, {}, entryLess);
217+ return items;
210218 }
211219
212220 pub fn supportedLevels(a: std.mem.Allocator, cfg: *const config.Config, provider_id: []const u8, model_id: []const u8) []const []const u8 {
src/tui/input.zig+2−0
@@ -22,6 +22,7 @@ pub const Key = union(enum) {
2222 escape,
2323 interrupt,
2424 eof,
25+ reverse_search,
2526 };
2627
2728 const SHIFT: u32 = 1;
@@ -70,6 +71,7 @@ fn lookup(code: u32, mods: u32) ?Key {
7071 100 * 8 + CTRL => .eof,
7172 101 * 8 + CTRL => .end,
7273 106 * 8 + CTRL => .newline,
74+ 114 * 8 + CTRL => .reverse_search,
7375 119 * 8 + CTRL => .word_back,
7476 else => null,
7577 };
src/tui/tui.zig+102−3
@@ -20,6 +20,7 @@ const usage_mod = @import("usage.zig");
2020 const diff_view = @import("diff_view.zig");
2121 const tool_view = @import("tool_view.zig");
2222 const screen = @import("screen.zig");
23+const history = @import("../history.zig");
2324 const status = @import("../status.zig");
2425
2526 const SPINNER = "⠀⠁⠂⠃⠄⠅⠆⠇⡀⡁⡂⡃⡄⡅⡆⡇⠈⠉⠊⠋⠌⠍⠎⠏⡈⡉⡊⡋⡌⡍⡎⡏⠐⠑⠒⠓⠔⠕⠖⠗⡐⡑⡒⡓⡔⡕⡖⡗⠘⠙⠚⠛⠜⠝⠞⠟⡘⡙⡚⡛⡜⡝⡞⡟⠠⠡⠢⠣⠤⠥⠦⠧⡠⡡⡢⡣⡤⡥⡦⡧⠨⠩⠪⠫⠬⠭⠮⠯⡨⡩⡪⡫⡬⡭⡮⡯⠰⠱⠲⠳⠴⠵⠶⠷⡰⡱⡲⡳⡴⡵⡶⡷⠸⠹⠺⠻⠼⠽⠾⠿⡸⡹⡺⡻⡼⡽⡾⡿⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏⢐ …
@@ -34,6 +35,12 @@ const PendingCall = tool_view.PendingCall;
3435
3536 const Command = commands.Command;
3637
38+const Search = struct {
39+ query: std.ArrayList(u8) = .empty,
40+ saved: []const u8 = "",
41+ match: ?[]const u8 = null,
42+};
43+
3744 var resize_flag = std.atomic.Value(bool).init(false);
3845 var exit_flag = std.atomic.Value(bool).init(false);
3946
@@ -49,6 +56,7 @@ fn onExitSignal(_: std.posix.SIG) callconv(.c) void {
4956 const Tui = struct {
5057 opts: *agent.Options,
5158 cfg: *const config.Config,
59+ history: history.Store = undefined,
5260 tool_names: []const config.ToolName,
5361 term: term.Terminal = .{},
5462 editor: editor_mod.Editor,
@@ -105,6 +113,8 @@ const Tui = struct {
105113 last_input_ms: i64 = 0,
106114 command_active: bool = false,
107115
116+ search: ?Search = null,
117+
108118 prompt_open: bool = false,
109119 prompt_options: []const []const u8 = &.{},
110120 prompt_ids: []const []const u8 = &.{},
@@ -303,16 +313,48 @@ const Tui = struct {
303313
304314 var lines: std.ArrayList([]const u8) = .empty;
305315 lines.append(self.s, status_line) catch {};
306- const caret_line = lines.items.len + ed.cursor_row;
307- for (ed.rows) |r| lines.append(self.s, r) catch {};
316+ var caret_line: usize = 0;
317+ var caret_col: usize = 0;
318+ if (self.search) |sr| {
319+ const prompt = std.fmt.allocPrint(self.s, "reverse: {s}", .{sr.query.items}) catch "reverse: ";
320+ caret_line = 1;
321+ caret_col = "reverse: ".len + sr.query.items.len;
322+ lines.append(self.s, prompt) catch {};
323+ if (self.searchMatch(&sr)) |m| {
324+ const text = render.sanitize(self.s, m);
325+ const budget_rows = @max(height -| 2, 1);
326+ var used: usize = 0;
327+ var it = std.mem.splitScalar(u8, text, '\n');
328+ while (it.next()) |raw| {
329+ const part = if (raw.len == 0) " " else raw;
330+ for (render.wrapLine(self.s, part, width)) |row| {
331+ if (used >= budget_rows) break;
332+ used += 1;
333+ lines.append(self.s, styles.dim(self.s, row)) catch {};
334+ }
335+ }
336+ } else {
337+ lines.append(self.s, styles.dim(self.s, "no matches")) catch {};
338+ }
339+ } else {
340+ for (ed.rows) |r| lines.append(self.s, r) catch {};
341+ caret_line = 1 + ed.cursor_row;
342+ caret_col = ed.cursor_col;
343+ }
308344
309- const frame_text = self.live.draw(self.s, width, lines.items, caret_line, ed.cursor_col, self.scrollback.buf.items);
345+ const frame_text = self.live.draw(self.s, width, lines.items, caret_line, caret_col, self.scrollback.buf.items);
310346 self.term.write(frame_text);
311347 self.scrollback.clear();
312348 _ = self.sarena.reset(.retain_capacity);
313349 self.sa = self.sarena.allocator();
314350 }
315351
352+ fn searchMatch(self: *Tui, sr: *const Search) ?[]const u8 {
353+ for (self.history.entries.items) |entry| {
354+ if (sr.query.items.len == 0 or std.ascii.indexOfIgnoreCase(entry, sr.query.items) != null) return entry;
355+ }
356+ return null;
357+ }
316358 fn keepName(self: *Tui, slot: *?[]const u8, value: ?[]const u8) void {
317359 if (slot.*) |old| self.a.free(old);
318360 slot.* = if (value) |v| self.a.dupe(u8, v) catch null else null;
@@ -456,8 +498,63 @@ const Tui = struct {
456498 self.pending_calls.clearRetainingCapacity();
457499 }
458500
501+ fn openSearch(self: *Tui) void {
502+ const draft = self.editor.contents();
503+ self.search = .{ .saved = self.a.dupe(u8, draft) catch return };
504+ self.editor.clear();
505+ self.dirty = true;
506+ }
507+
508+ fn closeSearch(self: *Tui, restore: bool, chosen: ?[]const u8) void {
509+ const search = self.search orelse return;
510+ self.search = null;
511+ self.editor.clear();
512+ defer self.dirty = true;
513+ if (chosen) |text| self.editor.setText(text);
514+ if (restore) self.editor.setText(search.saved);
515+ }
516+
517+ fn searchKey(self: *Tui, key: input.Key) void {
518+ const search = &(self.search orelse return);
519+ switch (key) {
520+ .text => |t| search.query.appendSlice(self.a, t) catch {},
521+ .backspace => {
522+ if (search.query.items.len > 0) {
523+ var start = search.query.items.len - 1;
524+ while (start > 0 and (search.query.items[start] & 0xc0) == 0x80) start -= 1;
525+ search.query.shrinkRetainingCapacity(start);
526+ }
527+ },
528+ .interrupt => return self.closeSearch(true, null),
529+ .escape => return self.closeSearch(true, null),
530+ .submit => return self.searchSubmit(),
531+ else => {},
532+ }
533+ self.dirty = true;
534+ }
535+
536+ fn searchSubmit(self: *Tui) void {
537+ var match: ?[]const u8 = null;
538+ const query = self.search.?.query.items;
539+ for (self.history.entries.items) |entry| {
540+ if (query.len == 0 or std.ascii.indexOfIgnoreCase(entry, query) != null) {
541+ match = entry;
542+ break;
543+ }
544+ }
545+ if (match) |text| self.closeSearch(false, text) else self.dirty = true;
546+ }
547+
459548 fn handleKey(self: *Tui, key: input.Key) void {
460549 self.resetScratch();
550+ if (self.search != null) {
551+ self.searchKey(key);
552+ return;
553+ }
554+ if (key == .reverse_search) {
555+ self.openSearch();
556+ return;
557+ }
461558 if (key == .eof) {
462559 if (!self.active and !self.command_active and self.editor.contents().len == 0) self.exit();
463560 return;
@@ -545,6 +642,7 @@ const Tui = struct {
545642 return;
546643 };
547644 self.commitUser(text);
645+ self.history.add(text);
548646 self.startTurn();
549647 }
550648
@@ -874,6 +972,7 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
874972 const self = try gpa.create(Tui);
875973 self.* = Tui.init(gpa, opts_ptr, cfg, tool_names);
876974 self.bindAllocators();
975+ self.history = history.Store.init(gpa, config.historyPath(gpa), cfg.history_size);
877976
878977 self.term.start();
879978 defer self.term.stop();