diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 21:03:38 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 21:20:55 -0300 |
| commit | e89ee60270267cb268ce642d79fb3fb277e84463 (patch) | |
| tree | 7d699d277e2a23d51a77dec2d8a0e298bad02549 /src/terminal.zig | |
| parent | 10a7e8768304478555dfe0b619423f5a4ad47d76 (diff) | |
| download | pardes-e89ee60270267cb268ce642d79fb3fb277e84463.tar.gz pardes-e89ee60270267cb268ce642d79fb3fb277e84463.zip | |
phase 5 part B, terminals: a replica's terminal fed by screen-state deltas, several in flight against the version last sent, its history the authority's to the row, and the authority's own steps batched into a unit a frame
screen_state.zig: each state the authority sends is a version of its terminal (Track), made once for every peer; a peer's delta is against the version it was last sent (the stream is ordered and loses nothing), up to 32 in flight, a peer at the bound waiting for an acknowledgement and then getting the newest; peers at one base are sent the same bytes. A replica refuses a delta against another base and is started afresh. It keeps the authority's history extent to the row (no byte limit of its own, trimmed to the authority's count), takes its viewport and its cursor's semantic content, and grows a flood's rows in bulk. The soak now keeps several in flight, lands them late in order, loses one now and then, and checks the history extent and the viewport too.
replica.zig: Screens (the authority's tracks and peers) and Replica.screen/screenRequest, messages of u8 pane, u32 serial, u8 kind (a delta, rows; an ack, a row request, a reset). Deltas are applied with the replica's epoch paused, so an emulator outlives a rollback. Authority.output/pending: its own steps (a pty's output) go into one unit, sent before any other input it steps and at a frame, with one digest; under screen state a digest is one word.
tape.zig, terminal.zig: under screen state an output is recorded without its bytes, pty writes are off the tape on both sides, no digest covers an emulator, and a replica's core never ingests or resizes one (a resize's own core work, the pointer selections cleared, still runs). A terminal counts its writes (State.changes); its MarkedHandler cycle rides in each delta.
test/replicas.zig: the session's shell runs a TUI, then 8 MB of yes read 4 KB at a time, while three clients type; deltas go out before each input the authority steps and once a frame, acknowledgements and row requests come back over the links, every applied delta is checked against the authority's screen at that version and every terminal at quiescence.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/terminal.zig')
| -rw-r--r-- | src/terminal.zig | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/terminal.zig b/src/terminal.zig index bcb70110..6c3eeda7 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -158,6 +158,9 @@ pub const State = struct { mouse_cell: if (enabled) ?ghostty_vt.Coordinate else void = if (enabled) null else {}, /// The kitty graphics its program placed, as drawn (TerminalImages.zig). images: TerminalImages = if (images_enabled) .{} else {}, + /// Counts the emulator's writes (output, a resize, its view scrolled): + /// a new count is a new state for its replicas (screen_state.Track). + changes: u32 = 0, /// The emulator as its program has made it, for the replay digest /// (src/walk.zig): the active screen's text, how much history is above @@ -169,6 +172,15 @@ pub const State = struct { /// on (docs/next-daemon.md). pub fn digest(s: *const State, h: *pardes.walk.Hasher) void { if (comptime !enabled) return; + // Under screen-state terminals the emulator is the authority's, and + // a replica's is what the last delta made it: compared as screens + // (test/replicas.zig), not step by step. + if (pardes.tape.screenState()) return; + s.digestScreen(h); + } + + pub fn digestScreen(s: *const State, h: *pardes.walk.Hasher) void { + if (comptime !enabled) return; const screen = s.vt.screens.active; const sb = screen.pages.scrollbar(); const cur = screen.cursor; @@ -531,6 +543,41 @@ pub fn panFor(p: *const Pardes, pane: *const Pane, r: pardes.Rect) u16 { return @intCast(@min(pane.rows - shown, (at + 1) -| shown)); } +/// What pardes made of a terminal's stream that its emulator does not hold +/// (the OSC 133 cycle, MarkedHandler), for a replica's delta: phase u8, +/// started, prompts, declined u32, continuation and lost u8, a finished +/// command's seq u32 and status i32 (all ones: none; i32 min: no status). +// ponytail: the output pins stay the authority's (its 9P pty/run reads them). +pub const marks_len = 1 + 4 * 3 + 2 + 4 + 4; + +pub fn marksOf(state: *const State, buf: *[marks_len]u8) []const u8 { + if (comptime !enabled) return &.{}; + const m = &state.stream.handler; + var w: std.Io.Writer = .fixed(buf); + w.writeByte(@intFromEnum(m.phase)) catch unreachable; + inline for (.{ m.started, m.prompts, m.declined }) |n| w.writeInt(u32, n, .little) catch unreachable; + w.writeAll(&.{ @intFromBool(m.continuation), @intFromBool(m.lost) }) catch unreachable; + w.writeInt(u32, if (m.finished) |f| f.seq else std.math.maxInt(u32), .little) catch unreachable; + w.writeInt(i32, if (m.finished) |f| f.status orelse std.math.minInt(i32) else 0, .little) catch unreachable; + return w.buffered(); +} + +/// A replica's terminal takes its authority's marks (`marksOf`). +pub fn takeMarks(state: *State, bytes: []const u8) void { + if (comptime !enabled) return; + if (bytes.len != marks_len) return; + const m = &state.stream.handler; + m.phase = @enumFromInt(bytes[0]); + m.started = std.mem.readInt(u32, bytes[1..5], .little); + m.prompts = std.mem.readInt(u32, bytes[5..9], .little); + m.declined = std.mem.readInt(u32, bytes[9..13], .little); + m.continuation = bytes[13] != 0; + m.lost = bytes[14] != 0; + const seq = std.mem.readInt(u32, bytes[15..19], .little); + const status = std.mem.readInt(i32, bytes[19..23], .little); + m.finished = if (seq == std.math.maxInt(u32)) null else .{ .seq = seq, .status = if (status == std.math.minInt(i32)) null else status }; +} + /// A replica's speculation leaves an emulator be: it is the authority's /// (docs/next-daemon.md, phase 5), so the input waits for its order. fn authorityOnly() bool { @@ -545,6 +592,7 @@ pub fn scrollGrid(pane: *Pane, delta: i32) void { if (authorityOnly()) return; const state = pane.terminal orelse return; state.vt.screens.active.scroll(.{ .delta_row = delta }); + state.changes +%= 1; } /// Is the viewport already showing the last row of output? @@ -559,7 +607,9 @@ pub fn followOutput(pane: *Pane) void { if (comptime !enabled) return; if (authorityOnly()) return; const state = pane.terminal orelse return; + const moved = state.vt.screens.active.pages.viewport != .active; state.vt.screens.active.scroll(.active); + state.changes +%= @intFromBool(moved); } /// Reflow the grid. A failed reflow keeps the grid it had rather than dropping @@ -572,6 +622,9 @@ pub fn resizeGrid(pane: *Pane, gpa: std.mem.Allocator, cols: u16, rows: u16) voi pane.clearPointerSelection(button); pane.sel[button].state = .none; }; + // A replica's emulator is sized by its authority: the next delta says. + if (pardes.tape.screenReplica()) return; + state.changes +%= 1; state.vt.resize(gpa, .{ .cols = cols, .rows = rows }) catch {}; } @@ -801,6 +854,7 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { }); state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa }); state.reply_len = 0; + state.changes = 0; // Every field its default (create leaves them what the heap had, which // a digest then reads: two replicas must agree, src/replica.zig). state.reply_dropped = 0; @@ -856,6 +910,7 @@ pub fn reviveShell(pane: *Pane) void { /// then apply different views. fn ingest(pane: *Pane, bytes: []const u8) void { pane.terminal.?.stream.nextSlice(bytes); + pane.terminal.?.changes +%= 1; } test "replay ownership is terminal-only and construction rolls back on allocation failure" { @@ -1017,6 +1072,8 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { if (comptime !enabled) return; if (pane.terminal == null) return; if (authorityOnly()) return; + // A replica's emulator is what the authority's deltas make it. + if (pardes.tape.screenReplica()) return; const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or pane.body.msel.active or pane.body.nsel > 0 or pane.body.last_edit != null or pane.look_at != null or pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pointerRowCount(pane) > 0; |
