summaryrefslogtreecommitdiff
path: root/dev/tui
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-25 18:35:03 -0300
committerGabriel Schneider <[email protected]>2026-11-27 12:39:39 -0300
commit9890f44e4a18f08b6dcc62a69484b8e93640e1e6 (patch)
tree6f5faa4da41dc0fb72bad31d4637df9c39965554 /dev/tui
parent24dc19130fd6a063bb7e86fa21a02d7af4c3b163 (diff)
downloadpardes-9890f44e4a18f08b6dcc62a69484b8e93640e1e6.tar.gz
pardes-9890f44e4a18f08b6dcc62a69484b8e93640e1e6.zip
tty: a terminal pane whose program is mid-frame (synchronized output, DEC 2026) keeps showing its last whole frame until the frame ends or a second has gone, as a terminal holds its picture (ghostty's sync_reset_ms): the user's report of 2026-11-27, "it seems to skip some stuff and then show repeated things", worst in long coding-agent sessions; THE CAUSE: agent TUIs (Claude Code, codex) wrap each redraw in ?2026h…?2026l, and a long session's redraw (Ink's 2J 3J H and the whole transcript) spans many 64 KiB pty reads; pardes drew the pane after each read, so the tty showed the transcript's top again (the repeat) and then the jump to its end (the skip); THE CHANGE: abi Modes.sync (ghostty's synchronized_output, read in terminal.zig's modes), surface Region.held set by draw.zig on a terminal body whose program is mid-frame, and tui.zig's Held: per pane, a held body's rect shows the cells (and the cursor) the tty last showed there; it lets go when the frame ends, after max_ns (1 s, counted from the first held draw), or on a resize or a moved rect; Screen.moving() keeps the loop drawing while one is held; other panes draw live; core/Term.zig's S1 table: modes draw.zig:2; the dump and the digest unchanged (Modes is not dumped; the digest hashes ghostty's modes already); NOT COVERED: the GUI and the 9P /screen file (the raw canonical surface) still show half frames; RUN (TEST FIRST): tui.zig "a terminal mid-frame (synchronized output) shows what the tty last showed of it, until the frame ends or a second has gone" (fails with max_ns 0, passes now); REAL CHECK (.scratch/ttyskip/harness.py: pardes --tty in a pty under pyte, an Ink-like generator in pane 1 over 9P, every outer frame (vaxis's own ?2026l) checked against the app's completed frames replayed from its byte log), 60 s ink runs, ReleaseSafe: the installed 24dc19130fd6 showed 362 of 3166 outer frames that were no completed app frame, this change 0 of 3270, final screens equal; Debug 112/1035 before, 0/965 after (a 60 s Debug run 5/2803: pardes more than 1 s behind lets a torn frame through by design, a 10 s cap gave 0); GATES (Debug tui): core-test, shell-test (1390/1394, 3 skipped), process-test 0 of 149 failed (3 skipped), term-test
Diffstat (limited to 'dev/tui')
-rw-r--r--dev/tui/tui.zig126
1 files changed, 124 insertions, 2 deletions
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig
index 8cb06b05..35fac823 100644
--- a/dev/tui/tui.zig
+++ b/dev/tui/tui.zig
@@ -831,6 +831,7 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
defer start.close();
if (opts.pair == null) try screen.enter(try vaxis.Tty.init(io, &tty_buf));
defer screen.glide.deinit(gpa);
+ defer screen.held.deinit(gpa);
defer screen.art.deinit(gpa);
var paste_buf: std.Io.Writer.Allocating = .init(gpa);
defer paste_buf.deinit();
@@ -942,6 +943,8 @@ const Screen = struct {
glide: pardes.glide.Glide = .{},
/// The cursor's jump trail (JumpTrail), on the tty's own clock.
trail: JumpTrail = .{},
+ /// What it last showed of a terminal mid-frame (synchronized output).
+ held: Held = .{},
/// `fd` made raw, as vaxis.Tty.init makes /dev/tty: the terminal of a
/// forked session is no controlling terminal of its, which /dev/tty is.
@@ -1123,6 +1126,7 @@ const Own = struct {
};
o.screen.art.deinit(gpa);
o.screen.glide.deinit(gpa);
+ o.screen.held.deinit(gpa);
o.frame_arena.deinit();
if (o.kept) |k| _ = libc.close(k.fd);
}
@@ -1786,7 +1790,7 @@ const Shell = struct {
/// The tty's own motions (its glide, a jump's trail) are under way.
fn moving(s: *const Shell) bool {
const sc = s.term() orelse return false;
- return sc.glide.moving() or sc.trail.active;
+ return sc.glide.moving() or sc.trail.active or sc.held.holding;
}
/// The tty's own motions draw their frames themselves: the last frame
@@ -1842,7 +1846,8 @@ const Shell = struct {
// The pictures it shows as glyph art, in a copy of the frame: all
// of them where the terminal has no kitty graphics.
const cell_h = terminalCellPixels(vx.screen.height_pix, vx.screen.height, 16);
- const arted = sc.art.frame(s.gpa, s.frame.allocator(), canonical, vx.caps.kitty_graphics, cell_h) catch canonical;
+ const kept = sc.held.frame(s.gpa, s.frame.allocator(), canonical, now) catch canonical;
+ const arted = sc.art.frame(s.gpa, s.frame.allocator(), kept, vx.caps.kitty_graphics, cell_h) catch kept;
sc.glide.frame(s.gpa, arted, now, null, null);
s.frames.scroll = @round(sc.glide.follow.offset);
const glided = glideRows(s.frame.allocator(), arted, &sc.glide) catch arted;
@@ -2272,6 +2277,88 @@ fn colortermTruecolor() bool {
return std.mem.eql(u8, value, "truecolor") or std.mem.eql(u8, value, "24bit");
}
+/// Synchronized output (2026, `Region.held`): a terminal's body whose
+/// program is mid-frame keeps the cells this tty last showed there, as a
+/// terminal's renderer pauses (ghostty's), a pane at a time (tmux's), and
+/// for a second at most (ghostty's sync_reset_ms): a program that never
+/// ends its frame is drawn as it is. Drawn as it came, an agent's redraw of
+/// a long transcript (many reads of the pty) showed the transcript's start
+/// again and then jumped to its end.
+const Held = struct {
+ /// The cells it last showed, its size then, and each body's place.
+ last: []pardes.Cell = &.{},
+ cols: u16 = 0,
+ rows: u16 = 0,
+ cursor: @FieldType(pardes.Surface, "cursor") = null,
+ rects: [pardes.MAX_PANES]?pardes.Rect = @splat(null),
+ /// When each pane's hold began.
+ since: [pardes.MAX_PANES]?u64 = @splat(null),
+ /// One is held: the tty draws a frame a tick until it lets go.
+ holding: bool = false,
+
+ const max_ns = std.time.ns_per_s;
+
+ fn deinit(h: *Held, gpa: std.mem.Allocator) void {
+ gpa.free(h.last);
+ h.* = .{};
+ }
+
+ /// `s` with each held body's rect as it was last shown (a copy in
+ /// `arena`), or `s` itself when none is.
+ fn frame(h: *Held, gpa: std.mem.Allocator, arena: std.mem.Allocator, s: *const pardes.Surface, now: u64) !*const pardes.Surface {
+ var copy: ?*pardes.Surface = null;
+ var rects: [pardes.MAX_PANES]?pardes.Rect = @splat(null);
+ h.holding = false;
+ for (s.regionList()) |region| {
+ if (region.kind != .body or region.owner >= rects.len) continue;
+ const id = region.owner;
+ const r = region.rect;
+ rects[id] = r;
+ if (!region.held) {
+ h.since[id] = null;
+ continue;
+ }
+ const began = h.since[id] orelse now;
+ h.since[id] = began;
+ // No picture of it to keep: a new size or place shows it as it is.
+ if (now -| began >= max_ns or h.cols != s.cols or h.rows != s.rows or !std.meta.eql(h.rects[id], r) or
+ r.x + r.w > s.cols or r.y + r.h > s.rows) continue;
+ h.holding = true;
+ const out = copy orelse made: {
+ const c = try arena.create(pardes.Surface);
+ c.* = s.*;
+ c.cells = try arena.dupe(pardes.Cell, s.cells);
+ copy = c;
+ break :made c;
+ };
+ for (r.y..r.y + r.h) |y| @memcpy(out.cells[y * s.cols + r.x ..][0..r.w], h.last[y * s.cols + r.x ..][0..r.w]);
+ if (inside(out.cursor, r) or inside(h.cursor, r)) out.cursor = h.cursor;
+ }
+ for (&h.since, rects) |*began, r| if (r == null) {
+ began.* = null;
+ };
+ const out: *const pardes.Surface = copy orelse s;
+ if (h.last.len != out.cells.len) {
+ gpa.free(h.last);
+ h.last = &.{};
+ h.cols = 0;
+ h.rows = 0;
+ h.last = try gpa.alloc(pardes.Cell, out.cells.len);
+ }
+ @memcpy(h.last, out.cells);
+ h.cols = out.cols;
+ h.rows = out.rows;
+ h.cursor = out.cursor;
+ h.rects = rects;
+ return out;
+ }
+
+ fn inside(cursor: @FieldType(pardes.Surface, "cursor"), r: pardes.Rect) bool {
+ const c = cursor orelse return false;
+ return c.x >= r.x and c.x < r.x + r.w and c.y >= r.y and c.y < r.y + r.h;
+ }
+};
+
/// The tty's picture of the glide (glide.zig, anim 12): the body's rows
/// moved by the offset rounded to whole rows (the position rounded, never
/// the step, so a slowing glide's steps come further apart), the rows that
@@ -3252,6 +3339,41 @@ test "a fade sends the text it dims, not the blanks beside it" {
try std.testing.expect(std.mem.indexOfScalar(u8, sent[1], 'a') != null);
}
+test "a terminal mid-frame (synchronized output) shows what the tty last showed of it, until the frame ends or a second has gone" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // no emulator
+ const gpa = std.testing.allocator;
+ const core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 8 });
+ defer core.deinit();
+ while (core.nextEffect()) |_| {}
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ var held: Held = .{};
+ defer held.deinit(gpa);
+ // The tty's frame after `bytes` came, at `t`: whether it shows `word`.
+ const shows = struct {
+ fn f(c: *pardes.Pardes, h: *Held, a: *std.heap.ArenaAllocator, bytes: []const u8, t: u64, word: []const u8) !bool {
+ if (bytes.len > 0) c.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } });
+ _ = a.reset(.retain_capacity);
+ const s = try h.frame(std.testing.allocator, a.allocator(), try c.render(a.allocator()), t);
+ var text: std.ArrayList(u8) = .empty;
+ for (s.cells) |*cell| try text.appendSlice(a.allocator(), cell.grapheme());
+ return std.mem.indexOf(u8, text.items, word) != null;
+ }
+ }.f;
+ const s = std.time.ns_per_s;
+ try std.testing.expect(try shows(core, &held, &arena, "first frame", s, "first frame"));
+ // A redraw that came in two reads: the first half is not shown.
+ try std.testing.expect(try shows(core, &held, &arena, "\x1b[?2026h\x1b[2J\x1b[Hsecond", s + s / 10, "first frame"));
+ try std.testing.expect(held.holding);
+ try std.testing.expect(try shows(core, &held, &arena, " frame\x1b[?2026l", s + s / 5, "second frame"));
+ try std.testing.expect(!held.holding);
+ // One never ended: drawn as it is after a second.
+ try std.testing.expect(try shows(core, &held, &arena, "\x1b[?2026h\x1b[2J\x1b[Hthird", 2 * s, "second frame"));
+ try std.testing.expect(try shows(core, &held, &arena, "", 3 * s - 1, "second frame"));
+ try std.testing.expect(try shows(core, &held, &arena, "", 3 * s, "third"));
+ try std.testing.expect(!held.holding);
+}
+
test "the tty draws a glide at whole rows: once a flick or a spin slows, its steps a tick never grow and its one-row steps come further apart, to where the core is" {
const gpa = std.testing.allocator;
const core = try pardes.Pardes.init(gpa, .{ .cols = 80, .rows = 30, .tty_only = true });