summaryrefslogtreecommitdiff
path: root/dev
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-23 17:10:08 -0300
committerGabriel Schneider <[email protected]>2026-11-23 17:26:17 -0300
commit2f3e7717dfa61421c71475130f540f9db6f7478c (patch)
tree919808260209ff82d9f2b651e72cefe208185700 /dev
parentaf78ec0dcb1ed40fd64001474f46b1b03a72475b (diff)
downloadpardes-2f3e7717dfa61421c71475130f540f9db6f7478c.tar.gz
pardes-2f3e7717dfa61421c71475130f540f9db6f7478c.zip
tty input: a terminal's reply of any length is taken, up to a stated cap past which it is dropped and said on the notice line, never an end of input: dev/tui/tui.zig `readInput` carried an unended sequence in a 1024-byte buffer and returned `error.InputSequenceTooLong` once it was full, and `inputReader` then posted `.quit`: a clipboard read's answer (OSC 52) over about 759 bytes of clipboard ENDED THE SESSION (the test below on the base: `InputSequenceTooLong` at tui.zig:163; kitty and ghostty send it, a user hits it by pasting the system clipboard from a session in a terminal); now the buffer grows (doubling, from the clipboard allocator vaxis parses with) up to `max_sequence`, the base64 of the paste limit's text with its framing (5461 KiB: a clipboard the core would paste whole is taken whole), and back to 1024 bytes after; a sequence past it is dropped (a string keeps its introducer and an `x` after it, so vaxis's parser still finds its end and passes it over as no reply it knows; anything else is let go) and said once, a new `Msg.reply_dropped` that the shell turns into the session's `setMessage` (taped, as `noticeNewer`'s line is) or a client's `link.notify`: "a terminal reply past 5461 KiB dropped: a clipboard past 4096 KiB is not pasted"; the read stays linear at megabytes (a pty hands 4 KiB a read): what was carried is not searched for frame answers again (only an answer cut across the read), not copied onto itself, and a string still arriving is not parsed again until a BEL or an ESC comes; a bracketed paste past the paste limit was cut SILENTLY (the tty kept exactly the limit, so `applyPaste` never saw more and never said so): it keeps one byte more and the core cuts and says "paste cut at its limit"; tests: "a clipboard reply of 4 KB or 1 MB is taken, whole, in a pty's reads and cut inside its end, and input goes on after it" (BEL and ST ends; fails on the base), "a clipboard reply past the cap is dropped and said once, and the input after it, another reply, is taken; a DCS and a kitty graphics answer the same", "a bracketed paste past the paste limit is cut and said, and a reply dropped is said, on the notice line" (fails on the base at "paste cut"); the test reader hands a part longer than the reader's room in reads of that room, as a tty does; shell-test Debug 1159/1162 (3 skipped; base 1156/1159)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'dev')
-rw-r--r--dev/tui/tui.zig238
1 files changed, 222 insertions, 16 deletions
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig
index 9320e522..f0011273 100644
--- a/dev/tui/tui.zig
+++ b/dev/tui/tui.zig
@@ -119,6 +119,9 @@ const Msg = union(enum) {
paste: []const u8,
paste_start,
paste_end,
+ /// A sequence past max_sequence was dropped (a clipboard's answer past
+ /// the paste limit): said on the notice line.
+ reply_dropped,
fs_ready,
/// A replica's reader posted: the step boundary takes it in.
replica_mail,
@@ -154,23 +157,61 @@ fn inputReader(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) void {
loop.postEvent(.quit) catch {};
}
+/// The longest sequence the terminal's input carries whole: a clipboard
+/// read's answer (OSC 52) of the paste limit's text, in base64, with its
+/// framing. One longer is dropped and said (`reply_dropped`).
+const max_sequence: usize = std.base64.standard.Encoder.calcSize(max_paste_bytes) + 16;
+
+/// A string sequence's start (OSC, DCS, APC, PM, SOS): it ends at a BEL or
+/// an ESC, however long it is.
+fn isString(head: []const u8) bool {
+ return head[0] == 0x1b and std.mem.indexOfScalar(u8, "]P_^X", head[1]) != null;
+}
+
fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void {
try loop.postEvent(.{ .winsize = try tty.getWinsize() });
var parser: vaxis.Parser = .{};
- var buf: [1024]u8 = undefined;
+ // Grown while one sequence is longer (a clipboard's answer: megabytes),
+ // up to max_sequence, and back to its first size after.
+ const gpa = vx.opts.system_clipboard_allocator orelse std.heap.c_allocator;
+ var buf = try gpa.alloc(u8, 1024);
+ defer gpa.free(buf);
var carried: usize = 0;
+ // The sequence carried is past max_sequence, and said so.
+ var dropping = false;
while (true) { // stopInput cancels the task; the read then fails with Canceled
- if (carried == buf.len) return error.InputSequenceTooLong;
+ if (carried == buf.len and buf.len < max_sequence) {
+ if (gpa.realloc(buf, @min(2 * buf.len, max_sequence))) |grown| buf = grown else |_| {}
+ }
+ if (carried == buf.len) {
+ // Dropped, and said once. A string keeps its introducer and a
+ // byte after it that makes it no reply vaxis knows, so the parse
+ // still finds its end and passes it over; anything else (a CSI
+ // of megabytes) is let go where it stands.
+ if (!dropping) try loop.postEvent(.reply_dropped);
+ dropping = true;
+ carried = if (isString(buf)) 3 else 0;
+ buf[2] = 'x';
+ }
const received = try tty.read(buf[carried..]);
if (received == 0) return;
// The terminal's answers to frames (FrameAck) come out first,
// wherever they sit: never events, and never glued to a key (an
- // Escape pressed just before one would read as Alt on it).
- const end = feed.ack.take(buf[0 .. carried + received], loop);
+ // Escape pressed just before one would read as Alt on it). What
+ // was carried is clear of them but for one cut across the read.
+ const from = carried -| (FrameAck.answer.len - 1);
+ const end = from + feed.ack.take(buf[from .. carried + received], loop);
if (end == 0) {
carried = 0;
continue;
}
+ // A string still arriving (a clipboard's answer in a pty's 4 KiB
+ // reads) has no end till a BEL or an ESC comes: it is read on, not
+ // parsed again from its start at every read.
+ if (carried > 2 and isString(buf) and std.mem.indexOfAny(u8, buf[carried - 1 .. end], "\x07\x1b") == null) {
+ carried = end;
+ continue;
+ }
if (feed.shm_probing.load(.acquire)) if (std.mem.indexOf(u8, buf[0..end], std.fmt.comptimePrint("\x1b_Gi={d};", .{kitty_shm_probe_id}))) |at| {
feed.shm_probing.store(false, .release);
feed.shm.store(std.mem.startsWith(u8, buf[at..end], std.fmt.comptimePrint("\x1b_Gi={d};OK", .{kitty_shm_probe_id})), .release);
@@ -217,7 +258,7 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void {
if (result.event) |event| if (event == .key_press and event.key_press.isModifier()) continue;
if (result.event) |event|
vaxis.loop.handleEventGeneric(loop, vx, &feed.cache, Msg, event, vx.opts.system_clipboard_allocator) catch |err| {
- if (event == .paste) if (vx.opts.system_clipboard_allocator) |gpa| gpa.free(@constCast(event.paste));
+ if (event == .paste) if (vx.opts.system_clipboard_allocator) |clip| clip.free(@constCast(event.paste));
return err;
};
// The last answer to the startup queries is no event of its
@@ -229,7 +270,13 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void {
};
}
carried = end - consumed;
- std.mem.copyForwards(u8, buf[0..carried], buf[consumed..end]);
+ if (consumed > 0) {
+ std.mem.copyForwards(u8, buf[0..carried], buf[consumed..end]);
+ dropping = false;
+ }
+ if (buf.len > 1024 and carried <= 1024) if (gpa.realloc(buf, 1024)) |first| {
+ buf = first;
+ } else |_| {};
}
}
@@ -333,10 +380,13 @@ const TestInput = struct {
};
/// A terminal that hands over `parts`, a read each (an empty one passed
-/// over), and says more follows (within `gap_ms`) while some are left.
+/// over; one longer than the reader's room in reads of that room, as a tty
+/// does), and says more follows (within `gap_ms`) while some are left.
const TestReader = struct {
parts: []const []const u8,
next: usize = 0,
+ /// How much of `parts[next]` was read.
+ at: usize = 0,
gap_ms: u32 = 0,
asked: bool = false,
@@ -351,11 +401,15 @@ const TestReader = struct {
fn read(r: *TestReader, buf: []u8) !usize {
while (r.next < r.parts.len) {
- const part = r.parts[r.next];
- r.next += 1;
- if (part.len == 0) continue;
- @memcpy(buf[0..part.len], part);
- return part.len;
+ const part = r.parts[r.next][r.at..];
+ const n = @min(part.len, buf.len);
+ @memcpy(buf[0..n], part[0..n]);
+ r.at += n;
+ if (n == part.len) {
+ r.next += 1;
+ r.at = 0;
+ }
+ if (n > 0) return n;
}
return 0;
}
@@ -1556,7 +1610,7 @@ const Shell = struct {
if (!s.peers.recording) return false;
switch (ev) {
.key_press => if (s.in_paste) return false,
- .mouse, .paste, .paste_end, .winsize, .focus_in, .focus_out => {},
+ .mouse, .paste, .paste_end, .reply_dropped, .winsize, .focus_in, .focus_out => {},
else => return false,
}
if (s.took_output or s.took_input) {
@@ -1583,14 +1637,14 @@ const Shell = struct {
s.frames.input(pardes.FrameStats.nowNs());
s.input_seen = true;
},
- .paste, .paste_start, .winsize => s.input_seen = true,
+ .paste, .paste_start, .reply_dropped, .winsize => s.input_seen = true,
else => {},
}
if (s.input_seen) s.input_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
if (event != .replica_mail) s.mail_only = false;
// The terminal's input goes into its view.
switch (event) {
- .key_press, .mouse, .paste, .paste_start, .paste_end, .winsize, .focus_in, .focus_out, .quit => {
+ .key_press, .mouse, .paste, .paste_start, .paste_end, .reply_dropped, .winsize, .focus_in, .focus_out, .quit => {
if (s.local() and s.client == null) core.toView(.at(s.own));
},
else => {},
@@ -1659,7 +1713,8 @@ const Shell = struct {
.winsize => |ws| if (!s.detached()) s.resized(s.screen, ws),
.key_press => |key| if (s.in_paste) {
const bytes = vaxis_input.pasteBytes(key);
- const room = max_paste_bytes -| s.paste_buf.written().len;
+ // A byte past the limit kept: the core cuts there and says so.
+ const room = max_paste_bytes + 1 -| s.paste_buf.written().len;
s.paste_buf.writer.writeAll(bytes[0..@min(bytes.len, room)]) catch {};
} else s.feed(vaxis_input.keyEvent(key)),
.mouse => |m| if (mouseEvent(m)) |ev| s.feed(ev),
@@ -1677,6 +1732,7 @@ const Shell = struct {
if (pasted.len > 0) s.feed(.{ .paste = pasted });
s.paste_buf.clearRetainingCapacity();
},
+ .reply_dropped => if (s.client) |c| c.link.notify("{s}", .{reply_dropped_words}) else core.setMessage(.at(core.view().active), reply_dropped_words),
.fs_ready => {},
}
return false;
@@ -2098,6 +2154,7 @@ fn wakeFs(ctx: ?*anyopaque) void {
}
const max_paste_bytes: usize = pardes.memory.limits.max_paste_bytes;
+const reply_dropped_words = std.fmt.comptimePrint("a terminal reply past {d} KiB dropped: a clipboard past {d} KiB is not pasted", .{ max_sequence / 1024, max_paste_bytes / 1024 });
fn mouseEvent(m: vaxis.Mouse) ?pardes.Event {
const button: pardes.Mouse.Button = switch (m.button) {
@@ -2365,6 +2422,155 @@ test "terminal replies and malformed sequences never become typed text" {
try std.testing.expect(alt_up and clicked);
}
+/// The terminal's answer to a clipboard read (OSC 52) of `text`, between a
+/// typed `x` and `y`, ended by `end` (BEL or ST).
+fn testClipboardReply(gpa: std.mem.Allocator, text: []const u8, end: []const u8) ![]u8 {
+ const enc = std.base64.standard.Encoder;
+ const head = "x\x1b]52;c;";
+ const reply = try gpa.alloc(u8, head.len + enc.calcSize(text.len) + end.len + 1);
+ @memcpy(reply[0..head.len], head);
+ const b64 = enc.encode(reply[head.len..], text);
+ @memcpy(reply[head.len + b64.len ..][0..end.len], end);
+ reply[reply.len - 1] = 'y';
+ return reply;
+}
+
+/// The loop's events after a `testClipboardReply`, one word each.
+fn testReplyEvents(gpa: std.mem.Allocator, loop: *Loop, text: []const u8) ![]u8 {
+ var got: std.ArrayList(u8) = .empty;
+ errdefer got.deinit(gpa);
+ while (try loop.tryEvent()) |event| switch (event) {
+ .winsize => {},
+ .key_press => |key| try got.print(gpa, "{u} ", .{key.codepoint}),
+ .paste => |bytes| {
+ defer gpa.free(@constCast(bytes));
+ try got.appendSlice(gpa, if (std.mem.eql(u8, bytes, text)) "clipboard " else "other ");
+ },
+ else => |other| try got.print(gpa, "{t} ", .{other}),
+ };
+ return got.toOwnedSlice(gpa);
+}
+
+test "a clipboard reply of 4 KB or 1 MB is taken, whole, in a pty's reads and cut inside its end, and input goes on after it" {
+ const gpa = std.testing.allocator;
+ var t: TestInput = undefined;
+ try t.init();
+ defer t.deinit();
+ // 3 KiB and 768 KiB of clipboard: a 4 KiB and a 1 MiB reply. Up to
+ // 2026-11-23 a reply over 1 KiB of unread input ended it, and the
+ // session quit (InputSequenceTooLong).
+ for ([_]usize{ 3 << 10, 768 << 10 }) |len| {
+ const text = try gpa.alloc(u8, len);
+ defer gpa.free(text);
+ for (text, 0..) |*b, i| b.* = @truncate(i *% 131 +% i / 7);
+ for ([_][]const u8{ "\x07", "\x1b\\" }) |end| {
+ const reply = try testClipboardReply(gpa, text, end);
+ defer gpa.free(reply);
+ // A pty's reads: 4 KiB at most.
+ const pieces = try gpa.alloc([]const u8, (reply.len + 4095) / 4096);
+ defer gpa.free(pieces);
+ for (pieces, 0..) |*piece, i| piece.* = reply[i * 4096 .. @min(reply.len, (i + 1) * 4096)];
+ // Cut just before the end's last byte (inside an ST).
+ const cut = reply.len - 2;
+ const ways = [_][]const []const u8{ &.{reply}, pieces, &.{ reply[0..cut], reply[cut..] } };
+ for (ways, 0..) |parts, way| {
+ var reader: TestReader = .{ .parts = parts };
+ try readInput(&t.loop, &reader, &t.vx, &t.feed);
+ const got = try testReplyEvents(gpa, &t.loop, text);
+ defer gpa.free(got);
+ std.testing.expectEqualStrings("x clipboard y ", got) catch |err| {
+ std.debug.print("clipboard of {d} bytes, end {f}, way {d}\n", .{ len, std.ascii.hexEscape(end, .lower), way });
+ return err;
+ };
+ }
+ }
+ }
+}
+
+test "a clipboard reply past the cap is dropped and said once, and the input after it, another reply, is taken; a DCS and a kitty graphics answer the same" {
+ const gpa = std.testing.allocator;
+ var t: TestInput = undefined;
+ try t.init();
+ defer t.deinit();
+ const big = try gpa.alloc(u8, max_paste_bytes + 12);
+ defer gpa.free(big);
+ @memset(big, 'b');
+ for ([_][]const u8{ "\x07", "\x1b\\" }) |end| {
+ const too_long = try testClipboardReply(gpa, big, end);
+ defer gpa.free(too_long);
+ try std.testing.expect(too_long.len > max_sequence);
+ const reply = try testClipboardReply(gpa, "clip", end);
+ defer gpa.free(reply);
+ // In a pty's 4 KiB reads, the reply after them.
+ const pieces = try gpa.alloc([]const u8, (too_long.len + 4095) / 4096 + 1);
+ defer gpa.free(pieces);
+ for (pieces[0 .. pieces.len - 1], 0..) |*piece, i| piece.* = too_long[i * 4096 .. @min(too_long.len, (i + 1) * 4096)];
+ pieces[pieces.len - 1] = reply;
+ const cut = too_long.len - 2;
+ const ways = [_][]const []const u8{ &.{ too_long, reply }, pieces, &.{ too_long[0..cut], too_long[cut..], reply } };
+ for (ways, 0..) |parts, way| {
+ var reader: TestReader = .{ .parts = parts };
+ try readInput(&t.loop, &reader, &t.vx, &t.feed);
+ const got = try testReplyEvents(gpa, &t.loop, "clip");
+ defer gpa.free(got);
+ std.testing.expectEqualStrings("x reply_dropped y x clipboard y ", got) catch |err| {
+ std.debug.print("end {f}, way {d}\n", .{ std.ascii.hexEscape(end, .lower), way });
+ return err;
+ };
+ }
+ }
+ // A DCS and a kitty graphics answer (APC): 4 KiB taken (no event of
+ // pardes's), one past the cap dropped and said.
+ for ([_][]const u8{ "\x1bP", "\x1b_G" }) |intro| for ([_]usize{ 4096, max_sequence }) |len| {
+ const long = try gpa.alloc(u8, 1 + intro.len + len + 3);
+ defer gpa.free(long);
+ long[0] = 'x';
+ @memcpy(long[1..][0..intro.len], intro);
+ @memset(long[1 + intro.len .. long.len - 3], 'a');
+ @memcpy(long[long.len - 3 ..], "\x1b\\y");
+ var reader: TestReader = .{ .parts = &.{long} };
+ try readInput(&t.loop, &reader, &t.vx, &t.feed);
+ const got = try testReplyEvents(gpa, &t.loop, "");
+ defer gpa.free(got);
+ try std.testing.expectEqualStrings(if (len == max_sequence) "x reply_dropped y " else "x y ", got);
+ };
+}
+
+test "a bracketed paste past the paste limit is cut and said, and a reply dropped is said, on the notice line" {
+ const gpa = std.testing.allocator;
+ const core = try pardes.Pardes.init(gpa, .{ .tty_only = true });
+ defer core.deinit();
+ const pane = try core.setTestFile("");
+ pane.body.mode = .insert;
+ var no_peers: replica.Peers = .{};
+ var paste_buf: std.Io.Writer.Allocating = .init(gpa);
+ defer paste_buf.deinit();
+ var shell: Shell = .{
+ .io = std.testing.io,
+ .gpa = gpa,
+ .lsp_gpa = gpa,
+ .core = core,
+ .frames = core.frames,
+ .loop = undefined,
+ .frame = undefined,
+ .paste_buf = &paste_buf,
+ .watches = undefined,
+ .peers = &no_peers,
+ .ptys = undefined,
+ };
+ const big = try gpa.alloc(u8, max_paste_bytes);
+ defer gpa.free(big);
+ @memset(big, 'x');
+ // Up to 2026-11-23 the tty kept the limit's bytes and no more, so the
+ // core never saw the paste was longer: cut, and nothing said.
+ const paste = [_]Msg{ .paste_start, .{ .key_press = .{ .codepoint = 'x', .text = big } }, .{ .key_press = .{ .codepoint = 'y', .text = "y" } }, .paste_end };
+ for (paste) |ev| _ = shell.apply(ev);
+ try std.testing.expectEqual(max_paste_bytes, pane.file.?.content.len);
+ try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "paste cut") != null);
+ _ = shell.apply(.reply_dropped);
+ try std.testing.expectEqualStrings(reply_dropped_words, pane.msg[0..pane.msg_len]);
+}
+
test "terminal input split at every byte with a gap reads as it does whole" {
const gpa = std.testing.allocator;
var t: TestInput = undefined;