summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-21 17:31:13 -0300
committerGabriel Schneider <[email protected]>2026-11-22 02:33:28 -0300
commit245dd088a5b502c72f94c85bf1cc5bd0ba7ebb56 (patch)
tree8772fea20505fb9d5a959bcf2fc646a81c614994 /core/replica.zig
parent242095c9e7e2eca11af92b9a0d563d640cfa65f2 (diff)
downloadpardes-245dd088a5b502c72f94c85bf1cc5bd0ba7ebb56.tar.gz
pardes-245dd088a5b502c72f94c85bf1cc5bd0ba7ebb56.zip
terminals: every client's terminals by the pty's bytes, the one road (the user's ruling of 2026-11-22: "just start the structural fix"): a session's units carry its ptys' bytes and each client feeds its own emulator in the tape's order (`Peers.bytes`, set by runtime/session.zig unless PARDES_TERM_SCREEN, a working name for the way back until screen state's road is deleted), and the welcome says the road (wire 29: one byte before `blobs`; test/daemon.py `WIRE`), which a client loads its dump on BEFORE its digest check (`fromDump`, join's and confirmBoot's one load, written twice before, and `road`): the pair's switch (`zwrkukrx`, duplicated here) checked a client's dump on screen state and turned it after, so under bytes every `pardes --attach` to a session with a terminal was refused as "loaded as another session" (daemon.py:544 client C, a far client's first frame never drawn); the pair's own boot takes the road from its environment (its session is its child) and the welcome confirms it; the predicted echo stays on the one road: echo.zig's overlay was already drawn over the frame, only its arming (gated on screen state) and its retirement (a delta's version) were the old road's: armed whenever a replica has an echo, retired at each confirmed unit that fed a terminal (`Echo.fed`, the grid's active top), echo.zig's tests run on the bytes road (17/17 under the filter); the pump-lag test (a session's own step reading a terminal right after its output, in one pump, through `Peers`) passes here: there is no flush to be behind; test/process.zig and test/daemon.py keep PARDES_TERM_SCREEN (`KEEP`) for the way back's row; measured against screen state (reports/pardes-term-join-7.md, its table): typing at a far prompt 16 KB against 22 KB, a full-screen program 80 KB against 103 KB, a 1 MiB `cat` through 1 Mbit 607 KB and 5.2 s with one 2.2 s rejoin against 27 KB and 0.4 s, the echo predicted on both (10 ms at 200 ms RTT, 222 ms unpredicted); gated: core-test 904/1068, shell-test 1110/1113, term-test 19/19, process-test 0 of 145 and as pairs 0 of 145, the bytes soak 3 of 3 clean, daemon.py every mode on Debug and ReleaseSafe (--restore the base's), --upgrade from the installed build 3 of 3 and between the two builds both ways, --far whole 7 of 7 on ReleaseSafe, the frame and perf gate against tree3 PASS (77/77, corpus2x -0.053%, 121 counts within 0.1%, client cells within +0.045%), the cross-builds
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/replica.zig')
-rw-r--r--core/replica.zig148
1 files changed, 129 insertions, 19 deletions
diff --git a/core/replica.zig b/core/replica.zig
index e26a9f17..993d9e2c 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -912,7 +912,7 @@ pub const Replica = struct {
/// can be, its echo predicted.
pub fn applyPended(r: *Replica, ev: pardes.Event) void {
r.settle();
- if (r.t.screen_state and r.diverged == null) r.echo.typed(r.core(), r.view, ev, r.next_seq - 1, r.applied == r.pending.items.len);
+ if (r.diverged == null and r.core().echo != null) r.echo.typed(r.core(), r.view, ev, r.next_seq - 1, r.applied == r.pending.items.len);
}
/// One unit of the authority's stream, in its order.
@@ -922,6 +922,9 @@ pub const Replica = struct {
r.stats.units += 1;
r.since_check +|= 1;
if (u.origin == r.id) if (r.t.core) |c| r.echo.confirmed(c, u.seq);
+ // The pty's bytes fed its emulators: its predicted echo judged
+ // against them, as a screen-state delta judges it (`screen`).
+ defer if (!r.t.screen_state and r.diverged == null and tape.feedsTerminal(u.tape)) if (r.t.core) |c| if (c.echo != null) r.echo.fed(c);
// A copy taken ahead (`ready`), nothing on top: it stays the point a
// rollback goes back to, the unit replayed on it and kept in `since`,
// not let go and taken again (a megabyte walked) for each unit of
@@ -2162,8 +2165,9 @@ pub const Peers = struct {
/// of each terminal as each version left it (test/replicas.zig).
flushed: ?struct { ctx: *anyopaque, f: *const fn (*anyopaque, *Pardes, *Screens) void } = null,
/// Its clients' terminals cross as the pty's bytes in its units, each
- /// client's emulator its own (P29, P32), not as screen state: the
- /// soak's (test/replicas.zig `--bytes`), until the switch.
+ /// client's emulator its own (P29, P32), not as screen state: every
+ /// session's (runtime/session.zig) but under PARDES_TERM_SCREEN, the
+ /// soak's `--bytes`; its welcome says so to each client.
bytes: bool = false,
pub const Note = union(enum) { join: Joining, input: Msg, screen: Msg, gone: *Peer, check: *Peer, joined: *Peer };
@@ -2421,7 +2425,7 @@ pub const Peers = struct {
// (a pair's client, its own boot: `writeJoin`).
const digest = tape.digestOf(core);
peer.digest = digest;
- peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = peer.view, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len;
+ peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = peer.view, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len), .bytes = ps.bytes } }) catch unreachable).len;
if (ps.threads) peer.writer = std.Thread.spawn(.{}, Peer.writeLoop, .{peer}) catch null;
if (ps.threads) peer.reader = std.Thread.spawn(.{}, Peer.readLoop, .{peer}) catch null;
if (ps.threads and (peer.writer == null or peer.reader == null)) {
@@ -2755,6 +2759,9 @@ pub const Joined = struct {
/// Its replica is its own boot, the welcome not read yet (`ownBoot`,
/// `confirmBoot`).
booted: bool = false,
+ /// Its session's terminals come as the pty's bytes, not screen state
+ /// (the welcome's word, Peers.bytes).
+ bytes: bool = false,
};
/// A blob a far join was sent: where it is in the dump, and its hash.
@@ -2835,6 +2842,7 @@ fn joinOn(gpa: Allocator, fd: c_int, session: ?[]const u8, why: []u8, have: ?*Ha
r.j.gmtoff = w.gmtoff;
r.j.digest = w.digest;
r.j.semantics = w.semantics;
+ r.j.bytes = w.bytes;
if (r.boot == w.digest) {
r.done = true;
return false;
@@ -3248,10 +3256,19 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o
if (have) |h| h.store(gpa, j) else gpa.free(j.dump);
j.dump = &.{};
}
+ return fromDump(gpa, j, o, have, why, t0);
+}
+
+/// The replica of the dump `j` brought, on the road its welcome says, its
+/// load checked by the session's digest, and what it takes of `j`; null,
+/// and why in `why`, when it does not load as the session.
+fn fromDump(gpa: Allocator, j: *Joined, o: pardes.Options, have: ?*Have, why: []u8, t0: u64) ?*Replica {
const r = Replica.initDump(gpa, j.client, j.view, j.dump, o) catch |err| {
_ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {};
return null;
};
+ // Before the check: on the bytes road the digest covers its emulators.
+ road(r, j.bytes);
if (!r.loaded(j.digest)) {
r.deinit();
_ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {};
@@ -3261,6 +3278,14 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o
return r;
}
+/// Its terminals its own emulators, fed the pty's bytes its units carry
+/// (`bytes`, its session's Peers.bytes), or the session's screen state;
+/// its echo predicted on either (core/echo.zig).
+fn road(r: *Replica, bytes: bool) void {
+ r.t.screen_state = !bytes;
+ r.core().echo = &r.echo;
+}
+
/// What a replica joined by `j` takes of it, and of the environment.
fn takeJoined(r: *Replica, j: *Joined, have: ?*Have, t0: u64) void {
// PARDES_NO_ECHO: no predicted echo (the gates' baseline).
@@ -3298,7 +3323,10 @@ fn ownBoot(gpa: Allocator, j: *Joined, fd: c_int, o: pardes.Options, why: *[600]
_ = std.fmt.bufPrint(why, "the session hung up\x00", .{}) catch {};
return null;
}
- j.* = .{ .fd = fd, .pid = 0, .client = r.id, .view = r.view, .gmtoff = gmtoff(), .dump = &.{}, .digest = digest, .semantics = wire.semantics, .booted = true };
+ // Its session is this process's child, on the road this environment
+ // says (runtime/session.zig): the welcome says it again.
+ j.* = .{ .fd = fd, .pid = 0, .client = r.id, .view = r.view, .gmtoff = gmtoff(), .dump = &.{}, .digest = digest, .semantics = wire.semantics, .booted = true, .bytes = std.c.getenv("PARDES_TERM_SCREEN") == null };
+ road(r, j.bytes);
takeJoined(r, j, null, t0);
return r;
}
@@ -3317,22 +3345,14 @@ pub fn confirmBoot(gpa: Allocator, j: *Joined, r: *Replica, o: pardes.Options, w
if (j.dump.len == 0) {
r.id = j.client;
r.view = j.view;
+ road(r, j.bytes);
return r;
}
defer {
gpa.free(j.dump);
j.dump = &.{};
}
- const loaded = Replica.initDump(gpa, j.client, j.view, j.dump, o) catch |err| {
- _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {};
- return null;
- };
- if (!loaded.loaded(j.digest)) {
- loaded.deinit();
- _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {};
- return null;
- }
- takeJoined(loaded, j, null, t0);
+ const loaded = fromDump(gpa, j, o, null, why, t0) orelse return null;
r.deinit();
return loaded;
}
@@ -5062,12 +5082,15 @@ fn firstOnPair(bytes: bool) !void {
var j: Joined = undefined;
var why: [600]u8 = @splat(0);
const r = if (bytes) r: {
+ // Joined by its dump as any client loads one (`fromDump`): the
+ // welcome says the road, and its load is checked on it.
j = joinOn(gpa, fds[1], null, &why, null, null, null) orelse return error.JoinFailed;
defer gpa.free(j.dump);
- const r = try Replica.initDump(gpa, j.client, j.view, j.dump, opts);
- r.t.screen_state = false;
- r.core().echo = null;
- break :r r;
+ try std.testing.expect(j.bytes);
+ break :r fromDump(gpa, &j, opts, null, &why, nowNs()) orelse {
+ std.debug.print("the join: {s}\n", .{std.mem.sliceTo(&why, 0)});
+ return error.JoinFailed;
+ };
} else r: {
var o = opts;
o.pair = fds[1];
@@ -6599,6 +6622,93 @@ test "a command typed into a shell with prompt marks under screen state: every s
try agrees(a, r);
}
+test "a step of the session's own that reads a terminal right after its output, in one pump: its client replays it over the terminal as that output left it (the pty's bytes in the unit, fed in the tape's order)" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const gpa = std.testing.allocator;
+ const ShellHost = struct {
+ fn taken(_: ?*anyopaque, _: pardes.Pane.Id) bool {
+ return false;
+ }
+ fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {}
+ const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write };
+ };
+ const core = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty });
+ defer core.deinit();
+ core.host = .{ .vtable = &ShellHost.vtable };
+ try core.pump(core.host);
+ const shell: u8 = for (core.panes, 0..) |slot, id| {
+ if (slot) |pane| if (pane.isTerminal()) break @intCast(id);
+ } else return error.NoShell;
+ core.view().active = shell;
+ // The session as runtime/session.zig runs it; the client's link in
+ // memory.
+ var ps: Peers = .{ .gpa = gpa, .io = std.testing.io, .threads = false, .bytes = true };
+ defer ps.deinit();
+ defer ps.dropAll(core);
+ var fds: [2]c_int = undefined;
+ if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket;
+ defer _ = libc.close(fds[1]);
+ ps.handed(fds[0], null);
+ ps.boundary(core);
+ const peer = for (ps.list) |slot| {
+ if (slot) |x| if (x.fd == fds[0]) break x;
+ } else return error.Refused;
+ const f = (try wire.framed(peer.welcome[0..peer.welcome_len])).?;
+ const w = (try wire.decodeServer(f.tag, f.payload)).welcome;
+ var word: [16]u8 = undefined;
+ const far = try wire.encodeClient(&word, .{ .far = false });
+ _ = peer.take(far[0], far[wire.header_len..]);
+ ps.post(.{ .joined = peer });
+ var whole: std.ArrayListUnmanaged(u8) = .empty;
+ defer whole.deinit(gpa);
+ var pos: usize = 0;
+ for (peer.blobs.items) |b| {
+ try whole.appendSlice(gpa, peer.dump[pos..b.at]);
+ try whole.appendSlice(gpa, b.bytes);
+ pos = b.at;
+ }
+ try whole.appendSlice(gpa, peer.dump[pos..]);
+ const r = try Replica.initDump(gpa, w.client, w.view.index(), whole.items, .{ .cols = 80, .rows = 24, .boot = .tty });
+ defer r.deinit();
+ road(r, w.bytes);
+ try std.testing.expect(r.loaded(w.digest));
+ var link: Link = .{ .r = r, .fd = fds[1] };
+ defer link.deinit(gpa);
+ const Down = struct {
+ // What the session queued for its client, taken in order.
+ fn all(from: *Peer, l: *Link) !void {
+ var at: usize = 0;
+ while (try wire.framed(from.out.items[at..])) |m| : (at += m.total) l.take(switch (std.enums.fromInt(wire.ServerTag, m.tag).?) {
+ .unit => .{ .unit = try std.heap.c_allocator.dupe(u8, m.payload) },
+ .screen => .{ .screen = try std.heap.c_allocator.dupe(u8, m.payload) },
+ else => return error.UnexpectedFrame,
+ });
+ from.out.clearRetainingCapacity();
+ }
+ };
+ const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07";
+ core.update(.{ .output = .{ .pane = .at(shell), .bytes = prompt } });
+ try core.pump(core.host);
+ ps.boundary(core);
+ try Down.all(peer, &link);
+ try std.testing.expect(r.diverged == null);
+ try std.testing.expectEqual(tape.digestOf(core), r.digest());
+ // One pump: its shell echoes a line typed at the prompt, then an Exec of
+ // the session's own (a 9P ctl write, a timer's) asks whether the prompt
+ // is empty to type at. The session reads "no" and opens a command pane;
+ // the client must read the same.
+ core.update(.{ .output = .{ .pane = .at(shell), .bytes = "typed" } });
+ core.update(.{ .command = "Exec true" });
+ try core.pump(core.host);
+ ps.boundary(core);
+ try Down.all(peer, &link);
+ if (r.diverged) |why| {
+ std.debug.print("the client: {s}\n", .{why});
+ return error.Diverged;
+ }
+ try std.testing.expectEqual(tape.digestOf(core), r.digest());
+}
+
test "a far client's own Del of a pane whose kind's state is not plain, rolled back by another's unit: the pane comes back whole, and is closed again with the session's order" {
const gpa = std.testing.allocator;
const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);