@@ -71,6 +71,8 @@ const Tui = struct {
7171 s: std.mem.Allocator,
7272 sarena: std.heap.ArenaAllocator,
7373 sa: std.mem.Allocator,
74+ calls: std.heap.ArenaAllocator,
75+ ca: std.mem.Allocator = undefined,
7476
7577 input_bytes: std.ArrayList(u8) = .empty,
7678 input_batch: std.ArrayList(u8) = .empty,
@@ -150,6 +152,7 @@ const Tui = struct {
150152 .s = undefined,
151153 .sarena = std.heap.ArenaAllocator.init(a),
152154 .sa = undefined,
155+ .calls = std.heap.ArenaAllocator.init(a),
153156 .qarena = std.heap.ArenaAllocator.init(a),
154157 .q = undefined,
155158 .model_arena = std.heap.ArenaAllocator.init(a),
@@ -161,6 +164,7 @@ const Tui = struct {
161164 self.a = self.arena.allocator();
162165 self.s = self.scratch.allocator();
163166 self.sa = self.sarena.allocator();
167+ self.ca = self.calls.allocator();
164168 self.q = self.qarena.allocator();
165169 self.model_m = self.model_arena.allocator();
166170 self.editor = editor_mod.Editor.init(self.backing);
@@ -368,8 +372,8 @@ const Tui = struct {
368372 return null;
369373 }
370374 fn keepName(self: *Tui, slot: *?[]const u8, value: ?[]const u8) void {
371- if (slot.*) |old| self.a.free(old);
372- slot.* = if (value) |v| self.a.dupe(u8, v) catch null else null;
375+ if (slot.*) |old| self.backing.free(old);
376+ slot.* = if (value) |v| self.backing.dupe(u8, v) catch null else null;
373377 }
374378
375379 fn handleAgentEvent(self: *Tui, event: agent.Event) void {
@@ -406,13 +410,13 @@ const Tui = struct {
406410 self.stream_kind = .none;
407411 self.keepName(&self.writing_tool, null);
408412 self.commitLines(self.reply.flush(self.s));
409- const summary = tool_view.callSummary(self.a, tc.name, tc.arguments);
413+ const summary = tool_view.callSummary(self.ca, tc.name, tc.arguments);
410414 const display = if (std.mem.eql(u8, tc.name, "read") or std.mem.eql(u8, tc.name, "edit"))
411415 tool_view.relativize(self.opts.session.cwd, summary)
412416 else
413417 summary;
414418 self.pending_calls.append(self.a, .{
415- .name = self.a.dupe(u8, tc.name) catch "",
419+ .name = self.ca.dupe(u8, tc.name) catch "",
416420 .summary = display,
417421 }) catch {};
418422 },
@@ -508,28 +512,32 @@ const Tui = struct {
508512 self.commitLines(tool_view.callBody(self.s, call.name, call.summary, null));
509513 }
510514 self.pending_calls.clearRetainingCapacity();
515+ _ = self.calls.reset(.retain_capacity);
516+ self.ca = self.calls.allocator();
511517 }
512518
513519 fn openSearch(self: *Tui) void {
514520 const draft = self.editor.contents();
515- self.search = .{ .saved = self.a.dupe(u8, draft) catch return };
521+ self.search = .{ .saved = self.backing.dupe(u8, draft) catch return };
516522 self.editor.clear();
517523 self.dirty = true;
518524 }
519525
520526 fn closeSearch(self: *Tui, restore: bool, chosen: ?[]const u8) void {
521- const search = self.search orelse return;
527+ var search = self.search orelse return;
522528 self.search = null;
523529 self.editor.clear();
524530 defer self.dirty = true;
525531 if (chosen) |text| self.editor.setText(text);
526532 if (restore) self.editor.setText(search.saved);
533+ self.backing.free(search.saved);
534+ search.query.deinit(self.backing);
527535 }
528536
529537 fn searchKey(self: *Tui, key: input.Key) void {
530538 const search = &(self.search orelse return);
531539 switch (key) {
532- .text => |t| search.query.appendSlice(self.a, t) catch {},
540+ .text => |t| search.query.appendSlice(self.backing, t) catch {},
533541 .backspace => {
534542 if (search.query.items.len > 0) {
535543 var start = search.query.items.len - 1;
@@ -723,11 +731,26 @@ const Tui = struct {
723731
724732 fn newSession(self: *Tui) void {
725733 self.opts.session.close();
726- const cwd = std.process.currentPathAlloc(platform.io, self.a) catch ".";
727- self.opts.session.* = session_mod.Session.init(self.a, self.cfg.sessions_dir, cwd);
734+ const cwd = std.process.currentPathAlloc(platform.io, self.backing) catch ".";
735+ self.opts.session.* = session_mod.Session.init(self.backing, self.cfg.sessions_dir, cwd);
728736 self.messages_mutex.lockUncancelable(platform.io);
729737 self.messages.clearRetainingCapacity();
730738 self.messages_mutex.unlock(platform.io);
739+ self.pending_calls.clearRetainingCapacity();
740+ self.streamed.clearRetainingCapacity();
741+ self.reply.reset();
742+ if (self.search) |*sr| {
743+ self.backing.free(sr.saved);
744+ sr.query.deinit(self.backing);
745+ self.search = null;
746+ }
747+ self.keepName(&self.detail, null);
748+ self.keepName(&self.writing_tool, null);
749+ self.steering_text = "";
750+ self.phase = .idle;
751+ self.stream_kind = .none;
752+ _ = self.calls.reset(.retain_capacity);
753+ self.ca = self.calls.allocator();
731754 self.dirty = true;
732755 }