xonecas.io
79113
Ma

mini-coding-agent / commit 155b63d

feat: diffs for the model after bash calls

xonecas2026-10-26 155b63df30f28ae5fd9f8a72f1c9599c6c4c1d81 parent 252852a

6 files changed

README.md+2−0
@@ -115,6 +115,7 @@ A full example:
115115 "herdr": false,
116116 "snapshotIgnoreDirs": [".git"],
117117 "snapshotUsesGitignore": true,
118+ "snapshotDiffsInContext": false,
118119 "historySize": 100,
119120 "maxDiffLines": 2000,
120121 "customProviders": [
@@ -160,6 +161,7 @@ Keys:
160161 | `herdr` | `false` | report agent state to a Herdr pane; only active inside one (`HERDR_ENV=1` with `HERDR_BIN_PATH` and `HERDR_PANE_ID`) |
161162 | `snapshotIgnoreDirs` | `[]` | directory names skipped when the `bash` tool snapshots the cwd for diffs |
162163 | `snapshotUsesGitignore` | `true` | also skip paths matched by the `.gitignore` files under the cwd when snapshotting |
164+| `snapshotDiffsInContext` | `false` | send the `bash` tool's snapshot diffs to the model inside the tool result |
163165 | `historySize` | `100` | past inputs kept in `<state dir>/mini-coding-agent/history` for `Shift+Up`/`Shift+Down` and `Ctrl+R` |
164166 | `maxDiffLines` | `2000` | diffs longer than this render as a "too large" notice instead of highlighting every line |
165167
src/agent.zig+3−2
@@ -270,13 +270,14 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(message_mod.Message), int
270270 .on_phase = .{ .ctx = &phase_ctx, .on_phase = onToolPhase },
271271 .snapshot_ignore_dirs = opts.config.snapshot_ignore_dirs,
272272 .snapshot_uses_gitignore = opts.config.snapshot_uses_gitignore,
273+ .snapshot_diffs_in_context = opts.config.snapshot_diffs_in_context,
273274 });
274275 }
275276
276277 const tool_message = message_mod.Message{ .tool_result = .{
277278 .tool_call_id = call.id,
278279 .tool_name = call.name,
279- .text = if (result) |r| r.text else "aborted",
280+ .text = if (result) |r| r.model_text orelse r.text else "aborted",
280281 .images = if (result) |r| r.images else &.{},
281282 .is_error = if (result) |r| r.is_error else true,
282283 .timestamp = time.nowMs(),
@@ -291,7 +292,7 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(message_mod.Message), int
291292 };
292293 listener.emit(.{ .tool_result = .{
293294 .name = call.name,
294- .text = tool_message.tool_result.text,
295+ .text = if (result) |r| r.text else "aborted",
295296 .is_error = tool_message.tool_result.is_error,
296297 .diffs = if (result) |r| r.diffs else &.{},
297298 .body = if (result) |r| r.body else null,
src/config.zig+4−0
@@ -48,6 +48,7 @@ pub const Config = struct {
4848 custom_providers: []const CustomProvider,
4949 snapshot_ignore_dirs: []const []const u8,
5050 snapshot_uses_gitignore: bool,
51+ snapshot_diffs_in_context: bool,
5152 history_size: usize,
5253 max_diff_lines: usize,
5354 };
@@ -69,6 +70,7 @@ const File = struct {
6970 showImages: ?bool = null,
7071 snapshotIgnoreDirs: ?[]const []const u8 = null,
7172 snapshotUsesGitignore: ?bool = null,
73+ snapshotDiffsInContext: ?bool = null,
7274 };
7375
7476 /// An empty environment variable is treated as unset, per the XDG spec.
@@ -147,6 +149,7 @@ fn readInto(a: std.mem.Allocator, path: []const u8, required: bool, cfg: *Config
147149 if (file.showImages) |v| cfg.show_images = v;
148150 if (file.snapshotIgnoreDirs) |v| cfg.snapshot_ignore_dirs = v;
149151 if (file.snapshotUsesGitignore) |v| cfg.snapshot_uses_gitignore = v;
152+ if (file.snapshotDiffsInContext) |v| cfg.snapshot_diffs_in_context = v;
150153 if (file.historySize) |v| cfg.history_size = v;
151154 if (file.maxDiffLines) |v| cfg.max_diff_lines = v;
152155 }
@@ -200,6 +203,7 @@ fn defaults(a: std.mem.Allocator) !Config {
200203 .custom_providers = &.{},
201204 .snapshot_ignore_dirs = &.{},
202205 .snapshot_uses_gitignore = true,
206+ .snapshot_diffs_in_context = false,
203207 .history_size = 100,
204208 .max_diff_lines = 2000,
205209 };
src/tools.zig+3−0
@@ -33,6 +33,7 @@ pub const Context = struct {
3333 on_phase: ?PhaseFn = null,
3434 snapshot_ignore_dirs: []const []const u8 = &.{},
3535 snapshot_uses_gitignore: bool = true,
36+ snapshot_diffs_in_context: bool = false,
3637 };
3738
3839 pub const FileDiff = struct {
@@ -47,6 +48,8 @@ pub const Result = struct {
4748 images: []const message_mod.ImageContent = &.{},
4849 diffs: []const FileDiff = &.{},
4950 body: ?[]const u8 = null,
51+ /// Text the model sees instead of `text`; the TUI still renders `text`.
52+ model_text: ?[]const u8 = null,
5053 };
5154
5255 pub fn fail(a: std.mem.Allocator, fallback: []const u8, comptime fmt: []const u8, args: anytype) Result {
src/tools/bash.zig+10−0
@@ -186,9 +186,19 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
186186 if (ctx.on_phase) |p| p.call(.running);
187187 const diffs = snapshot.diffTrees(scratch, before, after) catch return fail(a, "bash failed: out of memory", .{});
188188
189+ var model_text: ?[]const u8 = null;
190+ if (ctx.snapshot_diffs_in_context and diffs.len > 0) {
191+ const patch = snapshot.diffText(scratch, diffs) catch return fail(a, "bash failed: out of memory", .{});
192+ if (patch.len > 0) {
193+ model_text = std.fmt.allocPrint(a, "{s}\n{s}", .{ out.items, patch }) catch
194+ return fail(a, "bash failed: out of memory", .{});
195+ }
196+ }
197+
189198 return .{
190199 .text = out.items,
191200 .is_error = if (code) |c| c != 0 else true,
192201 .diffs = diffs,
202+ .model_text = model_text,
193203 };
194204 }
src/tools/snapshot.zig+16−0
@@ -150,6 +150,22 @@ fn diffOne(a: std.mem.Allocator, path: []const u8, before: ?FileState, after: ?F
150150 return .{ .path = path, .note = noteFor(a, if (af.content == null) af.kind else bf.kind, "changed") };
151151 }
152152
153+pub fn diffText(a: std.mem.Allocator, diffs: []const tools.FileDiff) ![]const u8 {
154+ var out: std.Io.Writer.Allocating = .init(a);
155+ const w = &out.writer;
156+ for (diffs) |d| {
157+ if (d.patch) |patch| {
158+ if (patch.len == 0) continue;
159+ try w.print("--- {s}\n", .{d.path});
160+ try w.writeAll(patch);
161+ if (patch[patch.len - 1] != '\n') try w.writeByte('\n');
162+ } else if (d.note) |note| {
163+ try w.print("--- {s}: {s}\n", .{ d.path, note });
164+ }
165+ }
166+ return out.written();
167+}
168+
153169 pub fn diffTrees(a: std.mem.Allocator, before: Tree, after: Tree) ![]tools.FileDiff {
154170 var paths: std.ArrayList([]const u8) = .empty;
155171 var it = before.iterator();