xonecas.io
79113
Ma

mini-coding-agent / commit 46c09fd

fix: more responsive abort/pause.

xonecas2026-10-06 46c09fdf38f4b29b3baefcb88aed6c4ea67ffc7d parent d64a672

7 files changed

mac-build-issue.md+0−13
@@ -1,13 +0,0 @@
1-```
2-mini-coding-agent on  main via ↯ took 43s
3-❯ zig build
4-./build.zig.zon:24:20: error: unable to unpack git files: NotACommit
5- .url = "git+https://github.com/tree-sitter/tree-sitter-python?ref=v0.25.0#d326e4cad262cf681656e130960e49dfc04c03ea",
6- ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7-./build.zig.zon:28:20: error: unable to unpack git files: NotACommit
8- .url = "git+https://github.com/tree-sitter/tree-sitter-go?ref=v0.25.0#6048bfc6e5238eaf062c2221bd934489c39fbb61",
9- ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10-
11-mini-coding-agent on  main via ↯ v0.17.0 took 37s
12-```
13-Seems like zig 0.17.x on mac is not building.
notes.md+5−13
@@ -6,12 +6,7 @@ Some of my notes might be because of lack of Zig experience.
66
77 ## Notes
88
9-*Issues with pausing and canceling being delayed* - When the user presses either pause or cancel in the TUI, there is no immediate feedback, the visible result only happens at the next step. This is correct for pause, but incorrect for cancel. Cancel means stop right now, pause means pause at the next turn boundary. At some point the two got merged into both waiting for a turn boundary to act. And neither is properly displayed in the TUI status (no `pausing` state, canceling shouldn't exist, it …
10-~**Comment on line 142 of `src/agent.zig`** - Completely wrong about cancellation and shouldn't be there. Pausing and steering land at turn boundaries, canceling is immediate!~ Comment is gone!
11-`src/agent.zig` - cancel needs to be checked more often, between each phase. once and before tool calls is not responsive enough.
12-
13-**resize, width measurements, line wrapping** - these still behave in suprising ways sometimes, breaking highligting, wrapping at weird limits, stange states after resize.
14-A definite research about the best approach to a clean rendering method is required.
9+**highlighting updates** - Let's update the code so we use tree-sitter to highlight the diffs, instead of the manual process we do now. We are also going to start highlighting the committed bash calls arguments (not on the preview).
1510
1611 `src/config.zig` - When we fixed the issue with the model specific api being at provider level for opencode in a recent commit, apparently we didn't do it for custom providers. Again, the models need to carry the metadata, the provider is just a label grouping.
1712
@@ -33,16 +28,13 @@ A definite research about the best approach to a clean rendering method is requi
3328 I suggest the above structure, so we match the work we did for opencode. Basically the mistake here is assuming all providers use the same api for all models, which is not true. Not even for local envs.
3429 Another note here is that again, I'm shocked at how cumbersome it is to read and validate JSON.
3530
36-`src/main.zig` - has some headless concerns mixed into the entry point of the app. Worth considering extracting headless into `src/headless.zig`. Single concern files are easier to make a mental model around.
31+**resize, width measurements, line wrapping** - these still behave in suprising ways sometimes, breaking highligting, wrapping at weird limits, stange states after resize.
32+A definite research about the best approach to a clean rendering method is required.
33+
34+**`src/main.zig` - has some headless concerns mixed into the entry point of the app. Worth considering extracting headless into `src/headless.zig`. Single concern files are easier to make a mental model around.
3735
3836 `src/diff.zig` - We are rendering a whole header with index and divider, just to strip it in the TUI. Seems counter productive.
3937
4038 `src/tui/tui.zig` - This file has a lot of responsibilities, making it very big. Token estimation and commands at least, could be extracted.
4139
4240 `src/tui/theme.zig` - will eventually grow too much, each theme we add will make that file worse. A refactor for later, since it's ok right now.
43-
44-# Actions post review
45-
46-- Address the JSON issues, and it's helpers. Zig does have a good JSON support, we just ignored it and hand rolled ours. We loose some data to show on error messages.
47-
48-- No more comments, any new comment needs to be worth it's salt.
src/agent.zig+47−25
@@ -92,7 +92,6 @@ fn steer(opts: Options, messages: *std.ArrayList(types.Message), content: []cons
9292 }
9393
9494 fn pauseStep(run: Run) ?[]const u8 {
95- if (run.opts.cancel.load(.acquire)) return null;
9695 const interaction = run.interaction orelse return "";
9796 if (!interaction.pauseRequested()) return "";
9897 interaction.clearPause();
@@ -103,6 +102,12 @@ fn pauseStep(run: Run) ?[]const u8 {
103102 return steering;
104103 }
105104
105+fn cancelled(opts: Options, listener: Listener) bool {
106+ if (!opts.cancel.load(.acquire)) return false;
107+ listener.emit(.cancelled);
108+ return true;
109+}
110+
106111 const Run = struct {
107112 opts: Options,
108113 messages: *std.ArrayList(types.Message),
@@ -125,6 +130,7 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interacti
125130 _ = arena.reset(.retain_capacity);
126131 const scratch = arena.allocator();
127132
133+ if (cancelled(opts, listener)) return;
128134 const steering = pauseStep(run) orelse {
129135 listener.emit(.cancelled);
130136 return;
@@ -134,6 +140,7 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interacti
134140 return;
135141 };
136142
143+ if (cancelled(opts, listener)) return;
137144 listener.emit(.{ .phase = .{ .phase = .preparing } });
138145 opts.session.appendRequest(scratch, .{
139146 .provider = model.provider,
@@ -147,6 +154,7 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interacti
147154 return;
148155 };
149156
157+ if (cancelled(opts, listener)) return;
150158 listener.emit(.{ .phase = .{ .phase = .waiting_model } });
151159 var stream_ctx = StreamCtx{ .listener = listener };
152160 const assistant = opts.a.create(types.AssistantMessage) catch {
@@ -168,6 +176,7 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interacti
168176 return;
169177 };
170178
179+ if (cancelled(opts, listener)) return;
171180 messages.append(opts.a, .{ .assistant = assistant }) catch {
172181 listener.emit(.{ .err = "out of memory" });
173182 return;
@@ -197,35 +206,43 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interacti
197206
198207 var held: std.ArrayList([]const u8) = .empty;
199208 defer held.deinit(opts.a);
209+ var run_tools = true;
200210 for (assistant.content.items) |block| {
201211 if (block != .tool_call) continue;
202212 const call = block.tool_call;
203213
204- const step = pauseStep(run) orelse {
205- listener.emit(.cancelled);
206- return;
207- };
208- if (step.len > 0) held.append(opts.a, step) catch {
209- listener.emit(.{ .err = "out of memory" });
210- return;
211- };
212-
213- listener.emit(.{ .phase = .{ .phase = .running_tool, .detail = call.name } });
214+ if (run_tools) {
215+ if (opts.cancel.load(.acquire)) {
216+ run_tools = false;
217+ } else if (pauseStep(run)) |step| {
218+ if (step.len > 0) held.append(opts.a, step) catch {
219+ listener.emit(.{ .err = "out of memory" });
220+ return;
221+ };
222+ } else {
223+ run_tools = false;
224+ }
225+ }
226+ if (run_tools and opts.cancel.load(.acquire)) run_tools = false;
214227
215- const result = tools.execute(opts.a, scratch, call.name, call.arguments, .{
216- .cancel = opts.cancel,
217- .supports_images = opts.supports_images,
218- .on_output = .{ .ctx = &stream_ctx.listener, .on_chunk = onToolOutput },
219- .snapshot_ignore_dirs = opts.config.snapshot_ignore_dirs,
220- .snapshot_uses_gitignore = opts.config.snapshot_uses_gitignore,
221- });
228+ var result: ?common.Result = null;
229+ if (run_tools) {
230+ listener.emit(.{ .phase = .{ .phase = .running_tool, .detail = call.name } });
231+ result = tools.execute(opts.a, scratch, call.name, call.arguments, .{
232+ .cancel = opts.cancel,
233+ .supports_images = opts.supports_images,
234+ .on_output = .{ .ctx = &stream_ctx.listener, .on_chunk = onToolOutput },
235+ .snapshot_ignore_dirs = opts.config.snapshot_ignore_dirs,
236+ .snapshot_uses_gitignore = opts.config.snapshot_uses_gitignore,
237+ });
238+ }
222239
223240 const tool_message = types.Message{ .tool_result = .{
224241 .tool_call_id = call.id,
225242 .tool_name = call.name,
226- .text = result.text,
227- .images = result.images,
228- .is_error = result.is_error,
243+ .text = if (result) |r| r.text else "aborted",
244+ .images = if (result) |r| r.images else &.{},
245+ .is_error = if (result) |r| r.is_error else true,
229246 .timestamp = util.nowMs(),
230247 } };
231248 messages.append(opts.a, tool_message) catch {
@@ -238,12 +255,17 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interacti
238255 };
239256 listener.emit(.{ .tool_result = .{
240257 .name = call.name,
241- .text = result.text,
242- .is_error = result.is_error,
243- .diffs = result.diffs,
244- .body = result.body,
258+ .text = tool_message.tool_result.text,
259+ .is_error = tool_message.tool_result.is_error,
260+ .diffs = if (result) |r| r.diffs else &.{},
261+ .body = if (result) |r| r.body else null,
245262 } });
246263 }
264+ if (!run_tools) {
265+ listener.emit(.cancelled);
266+ return;
267+ }
268+ if (cancelled(opts, listener)) return;
247269 const joined = tryJoin(opts.a, held.items) catch {
248270 listener.emit(.{ .err = "out of memory" });
249271 return;
src/config.zig+1−1
@@ -159,6 +159,6 @@ fn defaults(a: std.mem.Allocator) !Config {
159159 .theme = theme.default_id,
160160 .custom_providers = &.{},
161161 .snapshot_ignore_dirs = &.{},
162- .snapshot_uses_gitignore = false,
162+ .snapshot_uses_gitignore = true,
163163 };
164164 }
src/tools/common.zig+1−1
@@ -15,7 +15,7 @@ pub const Context = struct {
1515 supports_images: bool,
1616 on_output: ?OutputFn = null,
1717 snapshot_ignore_dirs: []const []const u8 = &.{},
18- snapshot_uses_gitignore: bool = false,
18+ snapshot_uses_gitignore: bool = true,
1919 };
2020
2121 pub const FileDiff = struct {
src/tools/snapshot.zig+1−1
@@ -22,7 +22,7 @@ pub const Tree = std.StringHashMap(FileState);
2222
2323 pub const Ignore = struct {
2424 dirs: []const []const u8 = &.{},
25- uses_gitignore: bool = false,
25+ uses_gitignore: bool = true,
2626 };
2727
2828 fn ignoreSet(tmp: std.mem.Allocator, ignore: Ignore) !std.StringHashMap(void) {
src/tui/tui.zig+17−1
@@ -321,6 +321,7 @@ const Tui = struct {
321321 active: bool = false,
322322 paused: bool = false,
323323 pause_requested: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
324+ aborting: bool = false,
324325 turn_done: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
325326 steering_text: []const u8 = "",
326327 steering_ready: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
@@ -482,6 +483,9 @@ const Tui = struct {
482483 if (self.paused or self.phase == .pausing) {
483484 return std.fmt.allocPrint(self.s, "{s} · {s}", .{ styles.dim(self.s, "paused - type steering, Enter to submit"), usage }) catch usage;
484485 }
486+ if (self.active and self.pause_requested.load(.seq_cst)) {
487+ return std.fmt.allocPrint(self.s, "{s} · {s}", .{ styles.dim(self.s, "pausing - waiting for the step boundary"), usage }) catch usage;
488+ }
485489 if (!self.active or self.phase == .idle) return usage;
486490 const label = switch (self.phase) {
487491 .preparing => "preparing",
@@ -566,6 +570,7 @@ const Tui = struct {
566570
567571 fn handleAgentEvent(self: *Tui, event: agent.Event) void {
568572 if (self.closed) return;
573+ if (self.aborting) return;
569574 self.resetScratch();
570575 switch (event) {
571576 .phase => |p| {
@@ -671,6 +676,10 @@ const Tui = struct {
671676 self.abort.store(true, .seq_cst);
672677 self.steering_ready.store(true, .seq_cst);
673678 self.steering_text = "";
679+ if (!self.aborting) {
680+ self.aborting = true;
681+ self.endTurn(styles.red(self.s, "! cancelled"));
682+ }
674683 self.dirty = true;
675684 } else if (self.command_active) {
676685 self.command_active = false;
@@ -746,6 +755,7 @@ const Tui = struct {
746755 fn startTurn(self: *Tui) void {
747756 self.active = true;
748757 self.abort.store(false, .seq_cst);
758+ self.pause_requested.store(false, .seq_cst);
749759 self.turn_done.store(false, .seq_cst);
750760 self.phase = .preparing;
751761 self.keepName(&self.detail, null);
@@ -910,10 +920,15 @@ const Tui = struct {
910920 }
911921 fn requestSteering(ctx: *anyopaque) []const u8 {
912922 const self: *Tui = @ptrCast(@alignCast(ctx));
923+ if (self.abort.load(.seq_cst)) return "";
913924 self.paused = true;
914925 self.dirty = true;
915926 while (!self.steering_ready.load(.seq_cst)) {
916- if (self.abort.load(.seq_cst)) return "";
927+ if (self.abort.load(.seq_cst)) {
928+ self.paused = false;
929+ self.steering_ready.store(false, .seq_cst);
930+ return "";
931+ }
917932 std.Io.sleep(platform.io, .{ .nanoseconds = 10 * std.time.ns_per_ms }, .boot) catch {};
918933 }
919934 self.steering_ready.store(false, .seq_cst);
@@ -1144,6 +1159,7 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
11441159 if (self.turn_done.load(.seq_cst) and self.active) {
11451160 self.active = false;
11461161 self.paused = false;
1162+ self.aborting = false;
11471163 self.phase = .idle;
11481164 self.dirty = true;
11491165 }