From a22b579ad2df8d1811f0e194fa1bbc06e2282cf8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 03:44:57 -0300 Subject: term: the terminal a module of its own (docs/next-terminal.md, Phase 1): src/term/term.zig roots it, importing std, ghostty-vt, wuffs, zstbi and plain, never the core, and gives the core one table, `ops` (plain.term.Ops: about 60 entries, the records they take and give and the opaque handles State, Encoder, Track and Replica, in src/plain/term.zig), its own functions put in it through a cast refused at compile time unless each parameter and result matches the table's but for the handles and every error is one the table names; src/Term.zig, the core's side, takes `ops` from the term module when the build's term_options say the core has one (every core module but core-test's), null otherwise, and Term.State is a handle with the walk's hooks, so a dump keeps a terminal as it did (its Parts by name through walk.save, then ghostty's snapshot) and the digest is the emulator's; echo.zig comes back to the core (src/echo.zig: predicted echo is the replica's presentation, its five reads of the emulator through Term.grid, modes, prompt and Term.echoCell); screen_state's Peer is a plain record both sides name (its noteSent a function of screen_state's), its Encoder, Track and Replica handles made and freed through the table (replica.Screens makes its encoder at the first encode); a prompt's cut is told in columns and measured in its row's text by the core (Term.promptPrefix, File.rawAtDisplay); TerminalImages' decode bound moves from memory.limits to the decoder it bounds; build.zig: core_lean, the core module without the terminal, is core-test's, and wireCore no longer gives any core module ghostty-vt, wuffs or zstbi (the term module has them), so core-test's compile names none of ghostty, wuffs, zstbi, simdutf, highway or -lc++ (from --verbose); term-test runs the module's own tests (the pager's, screen_state's, the luminance table's); the tests of a terminal through a core move as they were to test/terminal.zig, in pane-test, which imports both (`of` reads the emulator behind a handle; the dump round trip frees through State.dumpFree), and tests elsewhere that read the emulator do it through Term.emulatorOf, a test in core-test skipping first; core-test skips a test whose terminal Term.open refuses; Authority.init releases its tape when the core it boots fails (it leaked); tests: core-test (no terminal) 409 of 941, 532 skipped, shell-test (the same tests with the terminal) 984 of 984, pane-test 180 of 181 (1 skipped, as before), term-test 15 of 15, kitty-test 45 of 45, replicas checked/wrong 0, hxdiff 658 cases 0 mismatches, behaviour-test 268 of 272 (the base's four addon ABI tests), lib-test --- src/replica.zig | 59 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 21 deletions(-) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index b746ddb6..a60f54af 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -32,6 +32,7 @@ const daemon = @import("daemon.zig"); const writeAll = daemon.writeAll; const c_heap = @import("c_heap"); const Term = pardes.Term; +const Echo = @import("echo.zig").Echo; const Allocator = std.mem.Allocator; const Pardes = pardes.Pardes; @@ -168,6 +169,7 @@ pub const Authority = struct { const a = try gpa.create(Authority); errdefer gpa.destroy(a); a.* = .{ .gpa = gpa, .t = tape.memory(gpa), .core = undefined, .host = host }; + errdefer tape.release(&a.t); a.t.screen_state = screens; // One word a digest: every pty read is a unit. a.t.whole_digest = screens and std.c.getenv("PARDES_REPLICA_TRACE") == null; @@ -329,7 +331,8 @@ fn writeScreenHead(out: *std.ArrayListUnmanaged(u8), gpa: Allocator, pane: u8, s /// pane, and what each peer was sent of it. pub const Screens = struct { gpa: Allocator, - enc: Term.Encoder, + /// Made the first time a terminal is encoded (`encoder`). + enc: ?*Term.Encoder = null, tracks: [pardes.MAX_PANES]?Tracked = @splat(null), peers: std.ArrayListUnmanaged(*[pardes.MAX_PANES]PeerOf) = .empty, free_peers: std.ArrayListUnmanaged(u16) = .empty, @@ -337,16 +340,16 @@ pub const Screens = struct { /// Nanoseconds: what each flush spent encoding, for every peer. encode_ns: u64 = 0, - const Tracked = struct { serial: u32, state: *Term.State, track: Term.Track }; + const Tracked = struct { serial: u32, state: *Term.State, track: *Term.Track }; const PeerOf = struct { serial: u32 = 0, peer: Term.Peer = .{}, shadow: pardes.Kind.ShadowSent = .{} }; pub fn init(gpa: Allocator) Screens { - return .{ .gpa = gpa, .enc = .init(gpa) }; + return .{ .gpa = gpa }; } /// `core` is the session's, its terminals still there. pub fn deinit(s: *Screens, core: *Pardes) void { - for (&s.tracks, 0..) |*slot, id| if (slot.*) |*tr| Term.untrack(&tr.track, s.gpa, if (s.live(core, id, tr)) tr.state else null); + for (&s.tracks, 0..) |*slot, id| if (slot.*) |*tr| Term.untrack(tr.track, s.gpa, if (s.live(core, id, tr)) tr.state else null); for (s.peers.items) |p| { for (p) |*pp| { pp.peer.deinit(s.gpa); @@ -357,7 +360,7 @@ pub const Screens = struct { s.peers.deinit(s.gpa); s.free_peers.deinit(s.gpa); s.out.deinit(s.gpa); - s.enc.deinit(); + if (s.enc) |e| Term.encoderFree(e); } fn live(_: *const Screens, core: *Pardes, id: usize, tr: *const Tracked) bool { @@ -405,7 +408,7 @@ pub const Screens = struct { try writeScreenHead(&s.out, s.gpa, @intCast(id), pane.serial, kind_delta); var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); defer s.out = w.toArrayList(); - if (!try Term.encode(state, &s.enc, &tr.track, &pp.peer, &w.writer)) continue; + if (!try Term.encode(state, try s.encoder(), tr.track, &pp.peer, &w.writer)) continue; try out.append(s.gpa, try s.gpa.dupe(u8, w.written())); } } @@ -483,7 +486,7 @@ pub const Screens = struct { try writeScreenHead(&s.out, s.gpa, @intCast(id), pane.serial, kind_rows); var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); defer s.out = w.toArrayList(); - try Term.encodeRows(state, &s.enc, &tr.track, lo, hi, &w.writer); + try Term.encodeRows(state, try s.encoder(), tr.track, lo, hi, &w.writer); try out.append(s.gpa, try s.gpa.dupe(u8, w.written())); lo = hi; } @@ -504,12 +507,20 @@ pub const Screens = struct { if (s.tracks[id]) |*tr| { if (s.live(core, id, tr)) return tr; // Its pane closed (its emulator, the pin with it, gone). - Term.untrack(&tr.track, s.gpa, null); + Term.untrack(tr.track, s.gpa, null); + s.tracks[id] = null; } - s.tracks[id] = .{ .serial = pane.serial, .state = pane.terminal.?, .track = .init(s.gpa) }; + s.tracks[id] = .{ .serial = pane.serial, .state = pane.terminal.?, .track = try Term.track(s.gpa) }; return &s.tracks[id].?; } + /// The encoder, made the first time a terminal is encoded. + fn encoder(s: *Screens) !*Term.Encoder { + if (s.enc) |e| return e; + s.enc = try Term.encoder(s.gpa); + return s.enc.?; + } + /// A peer's message (`Replica.screen`'s, `Replica.screenRequest`'s): /// an answer for it (owned), or null. pub fn take(s: *Screens, core: *Pardes, peer: u16, msg: []const u8) !?[]u8 { @@ -531,7 +542,7 @@ pub const Screens = struct { try writeScreenHead(&s.out, s.gpa, m.pane, m.serial, kind_rows); var w: std.Io.Writer.Allocating = .fromArrayList(s.gpa, &s.out); defer s.out = w.toArrayList(); - try Term.encodeRows(state, &s.enc, &tr.track, std.mem.readInt(u64, m.payload[0..8], .little), std.mem.readInt(u64, m.payload[8..16], .little), &w.writer); + try Term.encodeRows(state, try s.encoder(), tr.track, std.mem.readInt(u64, m.payload[0..8], .little), std.mem.readInt(u64, m.payload[8..16], .little), &w.writer); return try s.gpa.dupe(u8, w.written()); }, else => {}, @@ -680,11 +691,11 @@ pub const Replica = struct { blobs: pardes.Kind.Blobs = .{}, /// Its predicted echo (src/term/echo.zig), drawn over its core's terminals: /// fed by its keys, its units and its deltas here, and by `echoFrame`. - echo: Term.Echo = .{}, + echo: Echo = .{}, const ScreenOf = struct { serial: u32, - rep: Term.Replica = .{}, + rep: *Term.Replica, /// A delta was refused, and a reset asked for: those in flight /// before it are refused unasked. refused: bool = false, @@ -758,7 +769,7 @@ pub const Replica = struct { } pub fn deinit(r: *Replica) void { - for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(pardes.panes.emulator_gpa); + for (&r.screens) |*slot| if (slot.*) |*sc| Term.replicaFree(sc.rep, pardes.panes.emulator_gpa); if (r.t.core) |c| { const was = Stepping.begin(&r.t, &r.epoch); if (r.copy.held) _ = r.copy.restore(); @@ -1156,8 +1167,13 @@ pub const Replica = struct { const state = pane.terminal orelse return null; const slot = &r.screens[m.pane]; if (slot.* == null or slot.*.?.serial != m.serial) { - if (slot.*) |*old| old.rep.deinit(pardes.panes.emulator_gpa); - slot.* = .{ .serial = m.serial }; + if (slot.*) |*old| Term.replicaFree(old.rep, pardes.panes.emulator_gpa); + slot.* = null; + const rep = Term.replica(pardes.panes.emulator_gpa) catch { + r.fail("no memory for a terminal's replica"); + return null; + }; + slot.* = .{ .serial = m.serial, .rep = rep }; } const sc = &slot.*.?; // The emulator is the authority's: on the C heap, outside the @@ -1166,7 +1182,7 @@ pub const Replica = struct { switch (m.kind) { kind_delta => { const t0 = nowNs(); - const applied = Term.apply(state, &sc.rep, m.payload) catch |err| { + const applied = Term.apply(state, sc.rep, m.payload) catch |err| { if (err != error.BaseMismatch) r.fail("a terminal's delta did not apply"); if (sc.refused) return null; sc.refused = true; @@ -1174,7 +1190,7 @@ pub const Replica = struct { }; if (r.stats.samples) r.stats.apply_ns.append(r.gpa, nowNs() - t0) catch {}; sc.refused = false; - r.echo.delta(c, m.pane, sc.rep.top); + r.echo.delta(c, m.pane, Term.replicaTop(sc.rep)); // Its rows as the core keeps them read are another grid's. c.shell_rows.markStale(pane); // Its view is the authority's now: a rollback puts it back @@ -1187,7 +1203,7 @@ pub const Replica = struct { }, kind_rows => { sc.asked = null; - Term.applyRows(state, &sc.rep, m.payload) catch r.fail("a terminal's rows did not apply"); + Term.applyRows(state, sc.rep, m.payload) catch r.fail("a terminal's rows did not apply"); c.shell_rows.markStale(pane); r.overTerminals(); return null; @@ -1222,13 +1238,13 @@ pub const Replica = struct { const pane = c.panes[id] orelse continue; if (pane.serial != sc.serial) continue; const state = pane.terminal orelse continue; - const shown = Term.shown(state, &sc.rep); + const shown = Term.shown(state, sc.rep); var holes: std.ArrayList([2]u64) = .empty; defer holes.deinit(r.gpa); - sc.rep.missing(shown[0], shown[1], &holes, r.gpa) catch return null; + Term.missing(sc.rep, shown[0], shown[1], &holes, r.gpa) catch return null; if (holes.items.len == 0) continue; const want: [2]u64 = .{ holes.items[0][0], holes.items[holes.items.len - 1][1] }; - Term.blank(state, &sc.rep, want[0], want[1]); + Term.blank(state, sc.rep, want[0], want[1]); sc.asked = want; var range: [16]u8 = undefined; std.mem.writeInt(u64, range[0..8], want[0], .little); @@ -3772,6 +3788,7 @@ test "a replica joined from a themed dump paints its taglines in the restored th } test "a host's Adopt and a terminal reader's failure are inputs: a replica makes the same pane" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell, opened by a key const gpa = std.testing.allocator; // Its terminals as screen state, as a session's for its clients are. const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), true); -- cgit v1.3