xonecas.io
79113
Ma

mini-coding-agent / commit fecb92d

feat(tui): good old human polish (a first pass).

xonecas2026-10-09 fecb92d1ee1e3b02e20564c35fe73e51a68a58ce parent c2abd82

10 files changed

known-issues.md+13−4
@@ -4,14 +4,23 @@ These come from other people using mini
44
55 ---
66
7-> High frequency of errors using muse models.
7+> `waiting for provider` state takes longer and longer as a session progresses
88
9-More than one person said they kept getting `! stream read failed`, over and over. I was able to reproduce it with muse-1.3
9+Something is greatly affecting performance, and it gets worse each subsequent turn.
10+Testes with a fresh session + small prompt and the models answers in miliseconds.
1011
11-> Editor area is unstyled/uses the host terminal colors.
12+At the same time, same model, same provider, same build, with a context of 6%, turns stay in waiting for provider for over 30 seconds at a time.
1213
13-I've confirmed this in our stress script runs screenshots. We keep having issues with background coverage regressions, we need to pay attention to this when doing TUI runs during verification. Looking at the text output is clearly not working.
14+using `btop` to monitor usage during these longs waiting for provider show almost no network activity, 0 cpu and 30mb of memory, seems like the process is just idle. But a curl to the same provider/model/key responds immediatly.
15+
16+> High frequency of errors using muse models.
17+
18+More than one person said they kept getting `! stream read failed`, over and over. I was able to reproduce it with muse-1.3
1419
1520 > Ocasional TUI freeze during bash calls.
1621
1722 Several reports of the TUI freezing on some bash calls not all.
23+
24+> Editor area is unstyled/uses the host terminal colors.
25+
26+I've confirmed this in our stress script runs screenshots. We keep having issues with background coverage regressions, we need to pay attention to this when doing TUI runs during verification. Looking at the text output is clearly not working.
src/agent.zig+13−1
@@ -8,7 +8,7 @@ const session = @import("session.zig");
88 const config = @import("config.zig");
99 const time = @import("time.zig");
1010
11-pub const Phase = enum { preparing, waiting_model, streaming, running_tool, pausing, idle };
11+pub const Phase = enum { preparing, waiting_model, streaming, running_tool, snapshotting, pausing, idle };
1212
1313 pub const Event = union(enum) {
1414 phase: struct { phase: Phase, detail: ?[]const u8 = null },
@@ -75,6 +75,16 @@ fn unlockMessages(opts: Options) void {
7575
7676 const StreamCtx = struct { listener: Listener, started: bool = false };
7777
78+const ToolPhaseCtx = struct { listener: Listener, name: []const u8 };
79+
80+fn onToolPhase(ctx: *anyopaque, phase: common.ToolPhase) void {
81+ const c: *ToolPhaseCtx = @ptrCast(@alignCast(ctx));
82+ switch (phase) {
83+ .snapshotting => c.listener.emit(.{ .phase = .{ .phase = .snapshotting } }),
84+ .running => c.listener.emit(.{ .phase = .{ .phase = .running_tool, .detail = c.name } }),
85+ }
86+}
87+
7888 fn onApiEvent(ctx: *anyopaque, event: api.Event) void {
7989 const stream_ctx: *StreamCtx = @ptrCast(@alignCast(ctx));
8090 const listener = stream_ctx.listener;
@@ -245,11 +255,13 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interacti
245255 var result: ?common.Result = null;
246256 if (run_tools) {
247257 listener.emit(.{ .phase = .{ .phase = .running_tool, .detail = call.name } });
258+ var phase_ctx = ToolPhaseCtx{ .listener = listener, .name = call.name };
248259 result = tools.execute(opts.a, scratch, call.name, call.arguments, .{
249260 .cancel = opts.cancel,
250261 .supports_images = opts.supports_images,
251262 .tools = opts.config.tools,
252263 .on_output = .{ .ctx = &stream_ctx.listener, .on_chunk = onToolOutput },
264+ .on_phase = .{ .ctx = &phase_ctx, .on_phase = onToolPhase },
253265 .snapshot_ignore_dirs = opts.config.snapshot_ignore_dirs,
254266 .snapshot_uses_gitignore = opts.config.snapshot_uses_gitignore,
255267 });
src/tools/bash.zig+4−0
@@ -62,7 +62,9 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
6262 };
6363
6464 const ignore = snapshot.Ignore{ .dirs = ctx.snapshot_ignore_dirs, .uses_gitignore = ctx.snapshot_uses_gitignore };
65+ if (ctx.on_phase) |p| p.call(.snapshotting);
6566 const before = snapshot.capture(scratch, ignore) catch return fail(a, "bash failed: out of memory", .{});
67+ if (ctx.on_phase) |p| p.call(.running);
6668
6769 var child = std.process.spawn(platform.io, .{
6870 .argv = &.{ "bash", "-c", args.command },
@@ -157,7 +159,9 @@ pub fn run(a: std.mem.Allocator, scratch: std.mem.Allocator, args_json: []const
157159 out.appendSlice(a, std.fmt.allocPrint(scratch, "exit code: {s}", .{label}) catch "exit code: unknown") catch
158160 return fail(a, "bash failed: out of memory", .{});
159161
162+ if (ctx.on_phase) |p| p.call(.snapshotting);
160163 const after = snapshot.capture(scratch, ignore) catch return fail(a, "bash failed: out of memory", .{});
164+ if (ctx.on_phase) |p| p.call(.running);
161165 const diffs = snapshot.diffTrees(scratch, before, after) catch return fail(a, "bash failed: out of memory", .{});
162166
163167 return .{
src/tools/common.zig+12−0
@@ -11,11 +11,23 @@ pub const OutputFn = struct {
1111 }
1212 };
1313
14+pub const ToolPhase = enum { snapshotting, running };
15+
16+pub const PhaseFn = struct {
17+ ctx: *anyopaque,
18+ on_phase: *const fn (ctx: *anyopaque, phase: ToolPhase) void,
19+
20+ pub fn call(self: PhaseFn, phase: ToolPhase) void {
21+ self.on_phase(self.ctx, phase);
22+ }
23+};
24+
1425 pub const Context = struct {
1526 cancel: *const std.atomic.Value(bool),
1627 supports_images: bool,
1728 tools: []const config.ToolName = &.{},
1829 on_output: ?OutputFn = null,
30+ on_phase: ?PhaseFn = null,
1931 snapshot_ignore_dirs: []const []const u8 = &.{},
2032 snapshot_uses_gitignore: bool = true,
2133 };
src/tools/index.zig+3−3
@@ -17,10 +17,10 @@ const bash_params =
1717 \\{"type":"object","required":["command"],"properties":{"command":{"type":"string","description":"Command to run"},"timeout":{"type":"integer","description":"Timeout in seconds; defaults to 120"}},"additionalProperties":false}
1818 ;
1919
20-const read_description = "Read a file. Returns its text. Returns the whole file unless offset and range give a line window. Prefer bash for search or binary files.";
21-const read_image_description = "Read a file. Returns its text, or the image itself when the file is a png, jpg, or webp. Returns the whole file unless offset and range give a line window. Prefer bash for search or binary files.";
20+const read_description = "Read a file. Returns its text. Returns the whole file unless offset and range give a line window.";
21+const read_image_description = "Read a file. Returns its text, or the image itself when the file is a png, jpg, or webp. Returns the whole file unless offset and range give a line window.";
2222 const edit_description = "Edit a file by exact text replacement. oldText must occur exactly once. With empty oldText, create a new file (fails if it exists).";
23-const bash_description = "Run a bash command in the current working directory ({s}).";
23+const bash_description = "Run a bash command. Commands run in dir: {s}";
2424
2525 pub fn execute(a: std.mem.Allocator, scratch: std.mem.Allocator, name: []const u8, args_json: []const u8, ctx: common.Context) common.Result {
2626 const enabled = for (ctx.tools) |tool| {
src/tui/render.zig+0−1
@@ -263,4 +263,3 @@ pub fn bodyRows(a: std.mem.Allocator, lines: []const BodyLine, width: usize) []c
263263 for (rows[tail_at..]) |r| out.append(a, r) catch {};
264264 return out.items;
265265 }
266-
src/tui/stream.zig+0−39
@@ -1,5 +1,4 @@
11 const std = @import("std");
2-const theme = @import("theme.zig");
32 const highlight = @import("highlight.zig");
43 const render = @import("render.zig");
54
@@ -164,41 +163,3 @@ fn emitHighlighted(a: std.mem.Allocator, text: []const u8, out: *std.ArrayList(B
164163 }
165164 for (lines.items) |l| out.append(a, .{ .text = l }) catch {};
166165 }
167-
168-pub const TailStream = struct {
169- arena: std.heap.ArenaAllocator,
170- a: std.mem.Allocator = undefined,
171- rest: std.ArrayList(u8) = .empty,
172-
173- pub fn init(backing: std.mem.Allocator) TailStream {
174- return .{ .arena = std.heap.ArenaAllocator.init(backing) };
175- }
176-
177- pub fn bind(self: *TailStream) void {
178- self.a = self.arena.allocator();
179- }
180-
181- pub fn feed(self: *TailStream, delta: []const u8) void {
182- self.rest.appendSlice(self.a, delta) catch {};
183- if (std.mem.lastIndexOfScalar(u8, self.rest.items, '\n')) |nl| {
184- const keep = self.rest.items[nl + 1 ..];
185- std.mem.copyForwards(u8, self.rest.items[0..keep.len], keep);
186- self.rest.items.len = keep.len;
187- }
188- }
189-
190- pub fn pending(self: *TailStream, out_a: std.mem.Allocator) []BodyLine {
191- var out: std.ArrayList(BodyLine) = .empty;
192- if (self.rest.items.len > 0) {
193- const text = render.stripAnsi(out_a, self.rest.items);
194- out.append(out_a, .{ .text = text, .style = .{ .fg = theme.current.comment } }) catch {};
195- }
196- return out.items;
197- }
198-
199- pub fn reset(self: *TailStream) void {
200- self.rest = .empty;
201- _ = self.arena.reset(.retain_capacity);
202- self.a = self.arena.allocator();
203- }
204-};
src/tui/tool_view.zig+23−33
@@ -8,36 +8,17 @@ const BodyLine = render.BodyLine;
88
99 pub const PendingCall = struct { name: []const u8, summary: []const u8 };
1010
11-const state_width = "running".len;
12-const state_pad = std.fmt.comptimePrint("{{s: <{d}}}", .{state_width});
13-
1411 fn callHead(a: std.mem.Allocator, name: []const u8) []const u8 {
1512 return styles.teal(a, std.fmt.allocPrint(a, "-> {s}", .{name}) catch "->");
1613 }
1714
18-fn spaces(a: std.mem.Allocator, n: usize) []const u8 {
19- const buf = a.alloc(u8, n) catch return "";
20- @memset(buf, ' ');
21- return buf;
22-}
23-
24-pub fn callRows(a: std.mem.Allocator, call: PendingCall, running: bool) []const []const u8 {
25- const padded = std.fmt.allocPrint(a, state_pad, .{if (running) "running" else "queued"}) catch "";
26- const word = if (running) styles.teal(a, padded) else styles.dim(a, padded);
27- const head = callHead(a, call.name);
28- const text = render.expandTabs(a, render.sanitize(a, call.summary), 4);
29- const indent = spaces(a, state_width + 1 + "-> ".len + call.name.len + 1);
30- var out: std.ArrayList([]const u8) = .empty;
31- var it = std.mem.splitScalar(u8, text, '\n');
32- var i: usize = 0;
33- while (it.next()) |line| : (i += 1) {
34- if (i == 0) {
35- out.append(a, std.fmt.allocPrint(a, "{s} {s} {s}", .{ word, head, line }) catch line) catch {};
36- } else {
37- out.append(a, std.fmt.allocPrint(a, "{s}{s}", .{ indent, line }) catch line) catch {};
38- }
39- }
40- return out.items;
15+pub fn relativize(cwd: []const u8, path: []const u8) []const u8 {
16+ if (!std.fs.path.isAbsolute(path) or cwd.len == 0) return path;
17+ if (!std.mem.startsWith(u8, path, cwd)) return path;
18+ const rest = path[cwd.len..];
19+ if (rest.len == 0) return ".";
20+ if (rest[0] != '/') return path;
21+ return rest[1..];
4122 }
4223
4324 pub fn callSummary(a: std.mem.Allocator, name: []const u8, args_json: []const u8) []const u8 {
@@ -53,36 +34,37 @@ pub fn callSummary(a: std.mem.Allocator, name: []const u8, args_json: []const u8
5334 return args_json;
5435 }
5536
56-pub fn callBody(a: std.mem.Allocator, name: []const u8, summary: []const u8) []const BodyLine {
37+pub fn callBody(a: std.mem.Allocator, name: []const u8, summary: []const u8, meta: ?[]const u8) []const BodyLine {
5738 const head = callHead(a, name);
5839 const body = if (std.mem.eql(u8, name, "bash"))
5940 highlight.highlightOn(a, "bash", summary, .{})
6041 else
6142 summary;
62- const indent = spaces(a, "-> ".len + name.len + 2);
6343 var out: std.ArrayList(BodyLine) = .empty;
6444 var it = std.mem.splitScalar(u8, body, '\n');
6545 var i: usize = 0;
6646 while (it.next()) |line| : (i += 1) {
6747 const text = if (i == 0)
68- std.fmt.allocPrint(a, "{s} {s}", .{ head, line }) catch line
48+ std.fmt.allocPrint(a, "{s} {s}{s}", .{
49+ head,
50+ line,
51+ if (meta) |m| styles.dim(a, std.fmt.allocPrint(a, " [{s}]", .{m}) catch "") else "",
52+ }) catch line
6953 else
70- std.fmt.allocPrint(a, "{s}{s}", .{ indent, line }) catch line;
54+ line;
7155 out.append(a, .{ .text = text }) catch {};
7256 }
7357 return out.items;
7458 }
7559
76-pub fn resultLines(a: std.mem.Allocator, name: []const u8, text: []const u8, is_error: bool) []const BodyLine {
60+pub fn resultLines(a: std.mem.Allocator, name: []const u8, text: []const u8) []const BodyLine {
7761 const trimmed = std.mem.trimEnd(u8, text, " \t\r\n");
7862 var lines: std.ArrayList([]const u8) = .empty;
7963 var it = std.mem.splitScalar(u8, trimmed, '\n');
8064 while (it.next()) |l| lines.append(a, l) catch {};
8165 if (std.mem.eql(u8, name, "bash")) {
8266 if (lines.items.len > 0 and std.mem.startsWith(u8, lines.items[lines.items.len - 1], "exit code: ")) {
83- const exit = lines.items[lines.items.len - 1]["exit code: ".len..];
8467 _ = lines.pop();
85- if (is_error) lines.append(a, styles.red(a, std.fmt.allocPrint(a, "exit {s}", .{exit}) catch "exit")) catch {};
8668 }
8769 } else if (std.mem.eql(u8, name, "edit")) {
8870 const verb = if (std.mem.startsWith(u8, lines.items[0], "edited "))
@@ -101,3 +83,11 @@ pub fn resultLines(a: std.mem.Allocator, name: []const u8, text: []const u8, is_
10183 for (lines.items) |l| out.append(a, .{ .text = l }) catch {};
10284 return out.items;
10385 }
86+
87+pub fn exitLine(a: std.mem.Allocator, text: []const u8, is_error: bool) ?[]const u8 {
88+ const trimmed = std.mem.trimEnd(u8, text, " \t\r\n");
89+ const last = if (std.mem.lastIndexOfScalar(u8, trimmed, '\n')) |i| trimmed[i + 1 ..] else trimmed;
90+ if (!std.mem.startsWith(u8, last, "exit code: ")) return null;
91+ const label = std.fmt.allocPrint(a, " └ exit {s}", .{last["exit code: ".len..]}) catch return null;
92+ return if (is_error) styles.red(a, label) else styles.dim(a, label);
93+}
src/tui/tui.zig+104−82
@@ -80,6 +80,9 @@ const Tui = struct {
8080 detail: ?[]const u8 = null,
8181 writing_tool: ?[]const u8 = null,
8282 writing_bytes: usize = 0,
83+ stream_kind: enum { none, reasoning, text, tool } = .none,
84+ text_bytes: usize = 0,
85+ reasoning_bytes: usize = 0,
8386 active: bool = false,
8487 paused: bool = false,
8588 pause_requested: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
@@ -92,7 +95,6 @@ const Tui = struct {
9295 scrollback: screen.Scrollback = .{},
9396
9497 reply: stream.MarkdownStream,
95- activity: stream.TailStream,
9698 pending_calls: std.ArrayList(PendingCall) = .empty,
9799 streamed: std.ArrayList(u8) = .empty,
98100 turn_start: i64 = 0,
@@ -137,7 +139,6 @@ const Tui = struct {
137139 .q = undefined,
138140 .model_arena = std.heap.ArenaAllocator.init(a),
139141 .reply = undefined,
140- .activity = undefined,
141142 };
142143 }
143144
@@ -152,8 +153,6 @@ const Tui = struct {
152153 self.editor.s = self.s;
153154 self.reply = stream.MarkdownStream.init(self.backing);
154155 self.reply.bind();
155- self.activity = stream.TailStream.init(self.backing);
156- self.activity.bind();
157156 self.scrollback.a = self.a;
158157 self.scrollback.scratch = self.sa;
159158 }
@@ -209,29 +208,75 @@ const Tui = struct {
209208 const model = self.opts.model orelse return styles.dim(self.s, "no model configured");
210209 self.messages_mutex.lockUncancelable(platform.io);
211210 const used = usage_mod.estimateContextTokens(self.messages.items, self.opts.system_prompt, self.opts.tools_json);
211+ const cache = usage_mod.lastUsage(self.messages.items);
212212 self.messages_mutex.unlock(platform.io);
213- const usage = usage_mod.contextUsageLine(self.s, used, model);
213+ const info = std.fmt.allocPrint(self.s, "{s} · {s}", .{
214+ usage_mod.contextUsageLine(self.s, used, model),
215+ usage_mod.cacheLine(self.s, cache),
216+ }) catch "ctx";
214217 if (self.paused or self.phase == .pausing) {
215- return std.fmt.allocPrint(self.s, "{s} · {s}", .{ styles.dim(self.s, "paused - type steering, Enter to submit"), usage }) catch usage;
218+ return std.fmt.allocPrint(self.s, "{s} · {s}", .{ styles.dim(self.s, "paused - type steering, Enter to submit"), info }) catch info;
216219 }
217220 if (self.active and self.pause_requested.load(.seq_cst)) {
218- return std.fmt.allocPrint(self.s, "{s} · {s}", .{ styles.dim(self.s, "pausing - waiting for the step boundary"), usage }) catch usage;
219- }
220- if (!self.active or self.phase == .idle) return usage;
221- const label = switch (self.phase) {
222- .preparing => "preparing",
223- .waiting_model => "waiting for provider",
224- .streaming => if (self.writing_tool) |w| blk: {
225- if (self.writing_bytes == 0) break :blk std.fmt.allocPrint(self.s, "writing {s}", .{w}) catch "streaming";
226- const tokenest = (self.writing_bytes + 3) / 4;
227- break :blk std.fmt.allocPrint(self.s, "writing {s} · ~{s} tok", .{ w, usage_mod.formatTokens(self.s, tokenest) }) catch "streaming";
228- } else "streaming",
229- .running_tool => std.fmt.allocPrint(self.s, "running {s}", .{self.detail orelse "tool"}) catch "running tool",
230- else => "idle",
231- };
221+ return std.fmt.allocPrint(self.s, "{s} · {s}", .{ styles.dim(self.s, "pausing - waiting for the step boundary"), info }) catch info;
222+ }
223+ if (!self.active or self.phase == .idle) return info;
232224 const elapsed = @max(0, @divFloor(time.nowMs() - self.turn_start, 1000));
233- const ch = spinnerChar(self.frame);
234- return std.fmt.allocPrint(self.s, "{s} · {s}", .{ styles.dim(self.s, std.fmt.allocPrint(self.s, "{s} {s} · {d}s", .{ ch, label, elapsed }) catch label), usage }) catch usage;
225+ return std.fmt.allocPrint(self.s, "{s} {s} · {s} · {s}", .{
226+ styles.teal(self.s, spinnerChar(self.frame)),
227+ self.stateLabel(),
228+ styles.dim(self.s, std.fmt.allocPrint(self.s, "{d}s", .{elapsed}) catch ""),
229+ info,
230+ }) catch info;
231+ }
232+
233+ fn stateLabel(self: *Tui) []const u8 {
234+ const a = self.s;
235+ var text: []const u8 = "idle";
236+ var color: ?[]const u8 = null;
237+ var bytes: usize = 0;
238+ switch (self.phase) {
239+ .preparing => text = "preparing",
240+ .waiting_model => text = "waiting for provider",
241+ .snapshotting => {
242+ text = "snapshotting";
243+ color = theme.current.warn;
244+ },
245+ .running_tool => {
246+ text = std.fmt.allocPrint(a, "running tool call {s}", .{self.detail orelse "tool"}) catch "running tool call";
247+ color = theme.current.prompt;
248+ },
249+ .streaming => switch (self.stream_kind) {
250+ .tool => {
251+ text = std.fmt.allocPrint(a, "writing tool call {s}", .{self.writing_tool orelse "tool"}) catch "writing tool call";
252+ color = theme.current.prompt;
253+ bytes = self.writing_bytes;
254+ },
255+ .reasoning => {
256+ text = "reasoning";
257+ color = theme.current.prompt;
258+ bytes = self.reasoning_bytes;
259+ },
260+ .text => {
261+ text = "writing response";
262+ color = theme.current.prompt;
263+ bytes = self.text_bytes;
264+ },
265+ .none => text = "streaming",
266+ },
267+ else => {},
268+ }
269+ var out: std.ArrayList(u8) = .empty;
270+ out.appendSlice(a, if (color) |c| styles.styledWith(a, .{ .fg = c }, text) else styles.dim(a, text)) catch {};
271+ if (bytes > 0) {
272+ const n = usage_mod.formatTokens(a, (bytes + 3) / 4);
273+ out.appendSlice(a, styles.dim(a, std.fmt.allocPrint(a, " · ~{s} tok", .{n}) catch "")) catch {};
274+ }
275+ const queued = self.pending_calls.items.len -| @intFromBool(self.phase == .running_tool);
276+ if (queued > 0) {
277+ out.appendSlice(a, styles.dim(a, std.fmt.allocPrint(a, " · {d} queued", .{queued}) catch "")) catch {};
278+ }
279+ return out.items;
235280 }
236281
237282 fn spinnerChar(frame: usize) []const u8 {
@@ -246,53 +291,17 @@ const Tui = struct {
246291 return " ";
247292 }
248293
249- fn queueRows(self: *Tui, width: usize, budget: usize) []const []const u8 {
250- if (budget == 0) return &.{};
251- var out: std.ArrayList([]const u8) = .empty;
252- var used: usize = 0;
253- for (self.pending_calls.items, 0..) |call, i| {
254- for (tool_view.callRows(self.s, call, i == 0 and self.phase == .running_tool)) |row| {
255- const h = physicalRows(row, width);
256- if (used + h > budget and out.items.len > 0) break;
257- used += h;
258- out.append(self.s, row) catch {};
259- }
260- }
261- return out.items;
262- }
263-
264294 fn draw(self: *Tui) void {
265295 if (self.closed) return;
266296 self.resetScratch();
267297 const width = @max(self.term.width(), 1);
268298 const height = @max(self.term.height() - 1, 1);
269299 const status = self.statusLine();
270-
271- var inflight = self.reply.pending(self.s);
272- if (inflight.len == 0) inflight = self.activity.pending(self.s);
273- const rows = render.plainRows(self.s, inflight);
274-
275300 const status_rows = physicalRows(status, width);
276- const room = if (height > status_rows + 1) height - status_rows - 1 else 0;
277- const queue = self.queueRows(width, room);
278- var queue_height: usize = 0;
279- for (queue) |r| queue_height += physicalRows(r, width);
280- const body_budget = if (room > queue_height) room - queue_height else 0;
281- var body_start = rows.len;
282- var body_used: usize = 0;
283- while (body_start > 0) {
284- const h = physicalRows(rows[body_start - 1], width);
285- if (body_used + h > body_budget) break;
286- body_used += h;
287- body_start -= 1;
288- }
289- const body = rows[body_start..];
290- const editor_budget = height -| (status_rows + queue_height + body_used);
301+ const editor_budget = height -| status_rows;
291302 const ed = self.editor.layout(width, @max(editor_budget, 1));
292303
293304 var lines: std.ArrayList([]const u8) = .empty;
294- for (body) |r| lines.append(self.s, r) catch {};
295- for (queue) |r| lines.append(self.s, r) catch {};
296305 lines.append(self.s, status) catch {};
297306 const caret_line = lines.items.len + ed.cursor_row;
298307 for (ed.rows) |r| lines.append(self.s, r) catch {};
@@ -318,35 +327,45 @@ const Tui = struct {
318327 self.phase = p.phase;
319328 self.keepName(&self.detail, p.detail);
320329 if (p.phase == .pausing) self.paused = true;
330+ if (p.phase == .waiting_model) {
331+ self.stream_kind = .none;
332+ self.text_bytes = 0;
333+ self.reasoning_bytes = 0;
334+ }
321335 },
322336 .text => |delta| {
323- self.activity.reset();
337+ self.stream_kind = .text;
338+ self.text_bytes += delta.len;
324339 self.streamed.appendSlice(self.a, delta) catch {};
325340 self.commitLines(self.reply.feed(self.s, delta));
326341 },
327342 .reasoning => |delta| {
328- if (self.reply.pending(self.s).len == 0) self.activity.feed(delta);
343+ self.stream_kind = .reasoning;
344+ self.reasoning_bytes += delta.len;
329345 },
330346 .tool_call => |tc| {
347+ self.stream_kind = .none;
331348 self.keepName(&self.writing_tool, null);
332349 self.commitLines(self.reply.flush(self.s));
333- self.activity.reset();
350+ const summary = tool_view.callSummary(self.a, tc.name, tc.arguments);
351+ const display = if (std.mem.eql(u8, tc.name, "read") or std.mem.eql(u8, tc.name, "edit"))
352+ tool_view.relativize(self.opts.session.cwd, summary)
353+ else
354+ summary;
334355 self.pending_calls.append(self.a, .{
335356 .name = self.a.dupe(u8, tc.name) catch "",
336- .summary = tool_view.callSummary(self.a, tc.name, tc.arguments),
357+ .summary = display,
337358 }) catch {};
338359 },
339360 .tool_call_start => |name| {
361+ self.stream_kind = .tool;
340362 self.keepName(&self.writing_tool, name);
341363 self.writing_bytes = 0;
342364 },
343365 .tool_args => |n| self.writing_bytes = n,
344- .tool_output => |chunk| self.activity.feed(chunk),
366+ .tool_output => {},
345367 .message => |am| self.commitMessage(am),
346- .tool_result => |tr| {
347- self.activity.reset();
348- self.commitToolResult(tr.name, tr.text, tr.is_error, tr.diffs, tr.body);
349- },
368+ .tool_result => |tr| self.commitToolResult(tr.name, tr.text, tr.is_error, tr.diffs, tr.body),
350369 .err => |m| self.endTurn(styles.red(self.s, std.fmt.allocPrint(self.s, "! {s}", .{m}) catch "! error")),
351370 .no_model => self.endTurn(styles.red(self.s, "! no model configured")),
352371 .cancelled => self.endTurn(styles.red(self.s, "! cancelled")),
@@ -360,14 +379,13 @@ const Tui = struct {
360379 self.keepName(&self.detail, null);
361380 self.keepName(&self.writing_tool, null);
362381 self.paused = false;
363- self.activity.reset();
382+ self.stream_kind = .none;
364383 self.commitLines(self.reply.flush(self.s));
365384 self.flushCalls();
366385 self.note(line);
367386 }
368387
369388 fn commitMessage(self: *Tui, am: *types.AssistantMessage) void {
370- self.activity.reset();
371389 self.commitLines(self.reply.flush(self.s));
372390 defer self.streamed.clearRetainingCapacity();
373391 const text = types.assistantText(self.s, am) catch return;
@@ -381,19 +399,23 @@ const Tui = struct {
381399 fn commitToolResult(self: *Tui, name: []const u8, text: []const u8, is_error: bool, diffs: []const common.FileDiff, body: ?[]const u8) void {
382400 self.scrollback.separator = true;
383401 const shown = render.stripAnsi(self.s, text);
402+ const meta: ?[]const u8 = if (!is_error) body else null;
384403 if (self.pending_calls.items.len > 0) {
385404 const call = self.pending_calls.orderedRemove(0);
386- self.commitLines(tool_view.callBody(self.s, call.name, call.summary));
387- }
388- const width = @max(self.term.width() - BODY_PREFIX.len, 1);
389- const lines: []const render.BodyLine = if (!is_error and body != null)
390- &.{.{ .text = body.? }}
391- else
392- tool_view.resultLines(self.s, name, shown, is_error);
393- const rows = if (std.mem.eql(u8, name, "edit")) render.plainRows(self.s, lines) else render.bodyRows(self.s, lines, width);
394- for (rows, 0..) |row, i| {
395- const prefix = if (is_error and i == rows.len - 1) styles.red(self.s, ERROR_PREFIX) else styles.dim(self.s, BODY_PREFIX);
396- self.push(std.fmt.allocPrint(self.s, "{s}{s}", .{ prefix, row }) catch row);
405+ self.commitLines(tool_view.callBody(self.s, call.name, call.summary, meta));
406+ }
407+ if (meta == null) {
408+ const width = @max(self.term.width() - BODY_PREFIX.len, 1);
409+ const is_bash = std.mem.eql(u8, name, "bash");
410+ const bash_exit: ?[]const u8 = if (is_bash) tool_view.exitLine(self.s, shown, is_error) else null;
411+ const lines = tool_view.resultLines(self.s, name, shown);
412+ const rows = if (std.mem.eql(u8, name, "edit")) render.plainRows(self.s, lines) else render.bodyRows(self.s, lines, width);
413+ const mark_error = is_error and bash_exit == null;
414+ for (rows, 0..) |row, i| {
415+ const prefix = if (mark_error and i == rows.len - 1) styles.red(self.s, ERROR_PREFIX) else styles.dim(self.s, BODY_PREFIX);
416+ self.push(std.fmt.allocPrint(self.s, "{s}{s}", .{ prefix, row }) catch row);
417+ }
418+ if (bash_exit) |status_line| self.push(status_line);
397419 }
398420 if (diffs.len > 0) {
399421 for (render.plainRows(self.s, diff_view.diffRows(self.s, diffs))) |row| {
@@ -406,7 +428,7 @@ const Tui = struct {
406428 fn flushCalls(self: *Tui) void {
407429 for (self.pending_calls.items) |call| {
408430 self.scrollback.separator = true;
409- self.commitLines(tool_view.callBody(self.s, call.name, call.summary));
431+ self.commitLines(tool_view.callBody(self.s, call.name, call.summary, null));
410432 }
411433 self.pending_calls.clearRetainingCapacity();
412434 }
src/tui/usage.zig+21−0
@@ -63,3 +63,24 @@ pub fn contextUsageLine(a: std.mem.Allocator, used: u64, model: *const types.Mod
6363 const text = std.fmt.allocPrint(a, "ctx {s} · {d}%", .{ sizes, @as(u64, @intFromFloat(percent + 0.5)) }) catch "ctx";
6464 return if (percent >= 85) styles.yellow(a, text) else styles.dim(a, text);
6565 }
66+
67+pub fn lastUsage(messages: []const types.Message) types.Usage {
68+ var out: types.Usage = .{};
69+ for (messages) |m| {
70+ if (m != .assistant) continue;
71+ const am = m.assistant;
72+ if (am.stop_reason == .aborted or am.stop_reason == .err) continue;
73+ if (am.usage.total_tokens == 0) continue;
74+ out = am.usage;
75+ }
76+ return out;
77+}
78+
79+pub fn cacheLine(a: std.mem.Allocator, usage: types.Usage) []const u8 {
80+ const prompt = usage.input + usage.cache_read + usage.cache_write;
81+ const pct: u64 = if (prompt == 0)
82+ 0
83+ else
84+ @intFromFloat(@as(f64, @floatFromInt(usage.cache_read)) / @as(f64, @floatFromInt(prompt)) * 100.0 + 0.5);
85+ return styles.dim(a, std.fmt.allocPrint(a, "cache {d}%", .{pct}) catch "cache");
86+}