@@ -27,6 +27,10 @@ const SPINNER_MS = 120;
2727 const BODY_PREFIX = " | ";
2828 const ERROR_PREFIX = " ! ";
2929
30+/// Lines of scrollback kept in memory so a resize can re-wrap them. Rows are
31+/// rebuilt from these, so the value bounds the memory a session can hold.
32+const HISTORY_LINES = 1000;
33+
3034 /// Display-only elision for tool bodies; `edit` diffs are always shown in full.
3135 const MAX_BODY_ROWS = 12;
3236 const ELIDED_HEAD = 4;
@@ -316,7 +320,10 @@ fn onWinch(_: std.posix.SIG) callconv(.c) void {
316320 }
317321
318322 /// A signal that must restore the terminal before the process ends.
323+/// The restore happens here, not in the event loop: a signal that arrives while
324+/// the loop is blocked on a prompt or a lock must not leave the tty raw.
319325 fn onExitSignal(_: std.posix.SIG) callconv(.c) void {
326+ term.restore();
320327 exit_flag.store(true, .seq_cst);
321328 }
322329
@@ -365,6 +372,7 @@ const Tui = struct {
365372 abort: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
366373
367374 scroll: std.ArrayList(u8) = .empty,
375+ history: std.ArrayList([]const u8) = .empty,
368376 wrote: bool = false,
369377 last_blank: bool = false,
370378 separator: bool = false,
@@ -435,11 +443,19 @@ const Tui = struct {
435443 const blank = clean.len == 0 or (self.separator and self.wrote);
436444 self.separator = false;
437445 if (blank and self.wrote and !self.last_blank) {
446+ self.history.append(platform.gpa, "") catch {};
447+ self.capHistory();
438448 self.scroll.appendSlice(self.a, paintRow(self.sa, "")) catch {};
439449 self.scroll.appendSlice(self.a, "\r\n") catch {};
440450 self.last_blank = true;
441451 }
442452 if (clean.len != 0) {
453+ // `clean` may point at scratch or at the agent's event buffer, both
454+ // of which are reset before a resize can read this line back, so
455+ // the history keeps a copy or drops the line.
456+ if (platform.gpa.dupe(u8, clean)) |owned| {
457+ if (self.history.append(platform.gpa, owned)) |_| self.capHistory() else |_| platform.gpa.free(owned);
458+ } else |_| {}
443459 self.scroll.appendSlice(self.a, paintRow(self.sa, clean)) catch {};
444460 self.scroll.appendSlice(self.a, "\r\n") catch {};
445461 self.wrote = true;
@@ -448,6 +464,36 @@ const Tui = struct {
448464 self.dirty = true;
449465 }
450466
467+ /// Drops the oldest history line past the cap. Both the list and its
468+ /// strings use the process allocator, not a session arena, so an evicted
469+ /// line's memory comes back and a long session stays bounded.
470+ fn capHistory(self: *Tui) void {
471+ while (self.history.items.len > HISTORY_LINES) {
472+ const old = self.history.orderedRemove(0);
473+ if (old.len > 0) platform.gpa.free(old);
474+ }
475+ }
476+
477+ /// Rebuilds the whole scrollback at the current width. The terminal cannot
478+ /// reflow rows it has already been given, so the history is re-wrapped and
479+ /// the screen is rewritten from scratch; the terminal's own scrollback is
480+ /// dropped too, since it holds the old, narrow rendering.
481+ fn reflow(self: *Tui) void {
482+ self.term.refreshSize();
483+ self.live.rows = 0;
484+ self.live.cursor_up = 0;
485+ self.scroll.clearRetainingCapacity();
486+ self.term.write("\x1b[2J\x1b[3J\x1b[H");
487+ const width = @max(self.term.width(), 1);
488+ for (self.history.items) |line| {
489+ for (render.wrapLine(self.s, line, width)) |row| {
490+ self.scroll.appendSlice(self.a, paintRow(self.sa, row)) catch {};
491+ self.scroll.appendSlice(self.a, "\r\n") catch {};
492+ }
493+ }
494+ self.dirty = true;
495+ }
496+
451497 fn commitLines(self: *Tui, lines: []const stream.BodyLine) void {
452498 const width = @max(self.term.width(), 1);
453499 for (renderRows(self.s, lines, width)) |row| self.push(row);
@@ -1149,6 +1195,7 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
11491195 self.bindAllocators();
11501196
11511197 self.term.start();
1198+ defer self.term.stop();
11521199 const act = std.posix.Sigaction{
11531200 .handler = .{ .handler = onWinch },
11541201 .mask = std.posix.sigemptyset(),
@@ -1162,6 +1209,8 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
11621209 };
11631210 std.posix.sigaction(std.posix.SIG.TERM, &term_act, null);
11641211 std.posix.sigaction(std.posix.SIG.INT, &term_act, null);
1212+ std.posix.sigaction(std.posix.SIG.HUP, &term_act, null);
1213+ std.posix.sigaction(std.posix.SIG.QUIT, &term_act, null);
11651214
11661215 self.pushBanner();
11671216 self.draw();
@@ -1216,10 +1265,7 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
12161265 self.dirty = true;
12171266 }
12181267
1219- if (resize_flag.swap(false, .seq_cst)) {
1220- self.term.refreshSize();
1221- self.dirty = true;
1222- }
1268+ if (resize_flag.swap(false, .seq_cst)) self.reflow();
12231269 if (exit_flag.swap(false, .seq_cst)) self.exit();
12241270
12251271 const now = util.nowMs();