@@ -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 }