summaryrefslogtreecommitdiff
path: root/core/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-22 21:55:10 -0300
committerGabriel Schneider <[email protected]>2026-11-22 21:59:50 -0300
commit18e674964e5db67d35e1cf5ded128757ced22a98 (patch)
treef99ed84b30ecfcbc028b2c9c16deadc282dd06ce /core/replica.zig
parent9acf9f558de00f4588b51dee2cd56b9150c7fb35 (diff)
downloadpardes-18e674964e5db67d35e1cf5ded128757ced22a98.tar.gz
pardes-18e674964e5db67d35e1cf5ded128757ced22a98.zip
replicas: one decision a unit, by one count in the core (pardes-term-join-11, item 2; P54, the coordinator's lean, held against the code first): `Replica.confirm` asked one question four ways from three sources ("did applying this unit write state outside the rollback copy, an emulator?"): the speculation's own observation (`Pending.terminal` from `Tape.touched`, set by `touchTerminal` at a grid it owed and at every READ of a terminal's rows), the unit's records (`tape.feedsTerminal`), a process-wide counter (`Term.writes`, seven increments, shared by every side of the soak's one process) and a dead read (`read_terminal`, always false); round 10's predicate over the records failed because no record names the unit whose `sync` reflows a grid, a load or a teardown; now the core counts its emulators' writes itself (`Pardes.outside`, a `Term.Outside`): a field, so copied with a speculation, digested and dumped, a function of the tape, the same on every side; every write of the `calls` table's WRITE rows takes it first and counts (core/Term.zig `open`, `close`, `revive`, `reload`, `feed`, `resize`, `endOutputHere`, and `replies` when it drained any), a comptime block fails the build if a WRITE row's entry does not take it (the compiler holds the call sites; S1's `calls` test holds that a new write is listed); `resize` owes under a speculation (counted, not made: the grid's guard moved there from panes.zig `resizeGrid`); a load sets it (the walk loads the field; `dumpRead` counts nothing); the one read in `confirm`: on the fast path the count's movement over the input's speculation (`Pending.outside`, a number, equal to the unit's by the path's premise: moved, the unit is replayed once on the confirmed core), on the replay paths its movement over `replayUnit` on the core that applied it (moved over a copy held ahead, the copy goes), and on the neutral path no read at all: the session's digest after the unit holds the count, so a unit that wrote an emulator never ends where it began (`feedsTerminal` deleted with its one use); the predicted echo is judged at every unit that moved it; DELETED: `Tape.touched`, both `touchTerminal`s and the read-touch in edit.zig `shellRows` (a read writes nothing outside the copy; on the one road a speculation reads the confirmed emulator), `Term.writes` and its seven increments, `feedsTerminal`, `read_terminal`; where the lean does not stretch, said and not built: a picture's pixels, a kind's state and the Store's closed list are written BY a speculation, so the fast path must let the copy go, not replay: `panesMoved`, the serials and `retiring` stay for them; the dump changes (a core field): the wire stays at 31 (its history line says so); tests: the existing path tests green (fast, neutral, ahead, owed grid, "a view that sized a terminal closing leaves nothing queued ..." among them); new: a session's reflow of a terminal's grid over a copy held ahead is the count's to decide (a unit with no output record: the copy goes, a rollback does not reflow again), and the count digested (one side's emulator written outside any unit, a colour set no other digested field shows: `partsApart` names `outside` alone); core-test 917/1079 (162 skipped), shell-test 1121/1124 (3 skipped), the gate below
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.zig113
1 files changed, 76 insertions, 37 deletions
diff --git a/core/replica.zig b/core/replica.zig
index f4a3284c..0ae302e8 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -413,9 +413,10 @@ pub const Stats = struct {
}
};
-/// `terminal`: its speculation read a terminal or moved one's view (or a
-/// terminal had the keyboard), so what it did depends on the emulator.
-const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, terminal: bool = false, guess: ?tape.Guess = null };
+/// `outside`: how far its speculation moved its core's count of writes
+/// outside the rollback copy (Pardes.outside): not 0, it owes an emulator
+/// a write (a grid's size), which only its replay makes.
+const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0, outside: u64 = 0, guess: ?tape.Guess = null };
pub fn parseUnit(msg: []const u8) ?Unit {
if (msg.len < unit_head) return null;
@@ -690,9 +691,14 @@ 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.
- defer if (r.diverged == null and tape.feedsTerminal(u.tape)) if (r.t.core) |c| if (c.echo != null) r.echo.fed(c);
+ // THE ONE QUESTION of a unit: did applying it write state outside
+ // the rollback copy (an emulator: Pardes.outside, the count of those
+ // writes, moved over its apply, read off the core that applied it)?
+ // Such a unit is applied once, to the confirmed core, never kept to
+ // replay over a copy; and its emulators' new state is what the
+ // predicted echo is judged against.
+ var moved = false;
+ defer if (r.diverged == null and moved) 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
@@ -716,19 +722,20 @@ pub const Replica = struct {
// nothing ends on again (`neutral`); none, not known.
r.last_digest.clearRetainingCapacity();
if (lastDigest(u.tape)) |d| r.last_digest.appendSlice(r.gpa, d) catch {};
- if (r.pending.items[0].terminal) {
- // Right, but what it did wrote an emulator (or owes one a
- // write: a grid's size): replayed later (a rollback's
- // `since`) it would write it again. So it is taken now, over
- // the emulator its step read, and the ones on top applied
- // again over it.
+ // Its speculation's own count (`Pending.outside`: the fast
+ // path's premise, the same records, makes it this unit's): it
+ // owes an emulator a write (a grid's size), which a speculation
+ // never makes: kept, the write would be missing; replayed later
+ // (a rollback's `since`), made then. So it is taken now, on the
+ // confirmed core, and the ones on top applied again over it.
+ moved = r.pending.items[0].outside != 0;
+ if (moved) {
r.toConfirmed();
if (tape.replayUnit(&r.t, u.tape)) |why| return r.fail(why);
r.freePending(r.pending.orderedRemove(0));
r.dirty = true;
return;
}
- const read_terminal = r.pending.items[0].terminal;
r.freePending(r.pending.orderedRemove(0));
r.applied -= 1;
if (r.applied == 0) {
@@ -752,11 +759,10 @@ pub const Replica = struct {
// input kept to replay over it as a unit of the session's
// that comes while idle is: a step writes some bytes of the
// megabyte the copy is, walked once in `rebase_after` units,
- // not once a key. Not over one that read a terminal (replayed
- // later it would read the deltas since), nor once a pane
- // opened, closed or was torn down since the copy (what it
- // holds outside the copy was made or freed: `ahead`'s rule).
- if (!read_terminal and !r.panesMoved()) return r.keepSince(u.tape);
+ // not once a key. Not once a pane opened, closed or was torn
+ // down since the copy (what it holds outside the copy was
+ // made or freed: `ahead`'s rule).
+ if (!r.panesMoved()) return r.keepSince(u.tape);
r.copy.release();
rollback.Epoch.select(null);
r.clearSince();
@@ -771,13 +777,11 @@ pub const Replica = struct {
r.last_digest.clearRetainingCapacity();
if (ends) |d| r.last_digest.appendSlice(r.gpa, d) catch {};
}
- // A terminal's output, or a join's reloads, never taken so (the one
- // rule): what it writes outside the copy is applied once, at its
- // place, and much of it no digest covers (a colour set, a mode, the
- // pages a reload remakes).
- if (r.applied > 0 and u.origin != r.id and ends != null and std.mem.eql(u8, ends.?, r.last_digest.items) and
- !tape.feedsTerminal(u.tape))
- {
+ // The one question needs no read here: the session's digest after
+ // the unit holds its count of writes outside the copy, so a unit that
+ // wrote an emulator (a terminal's output, a colour set no other
+ // digested field shows, a join's reloads) never ends where it began.
+ if (r.applied > 0 and u.origin != r.id and ends != null and std.mem.eql(u8, ends.?, r.last_digest.items)) {
// Nothing a digest covers moved: what was applied on top is
// what the session will apply on top of it. Replayed when the
// copy is next put back.
@@ -806,7 +810,7 @@ pub const Replica = struct {
// replayed by a rollback were, if at all, as they came).
const check = ends != null and r.checkDue();
r.t.checks = check;
- const writes = Term.writes;
+ const outside = if (r.t.core) |c| c.outside.n else 0;
const replayed = tape.replayUnit(&r.t, u.tape);
r.t.checks = false;
if (replayed) |why| {
@@ -820,6 +824,7 @@ pub const Replica = struct {
// draw nothing. Read after the replay: before it, it is what the
// units before asked (and a first unit has no core yet).
if (r.core().needs_frame) r.moved = true;
+ moved = r.core().outside.n != outside;
if (check) {
r.since_check = 0;
r.stats.checks += 1;
@@ -837,15 +842,15 @@ pub const Replica = struct {
// or freed here, and is not put back with the copy: a rollback
// replaying the unit again would close a terminal freed already (the
// soak, a `Del` of the shell). Let go with the copy.
- // The one rule: a unit that wrote an emulator (a terminal's output,
- // a grid reflowed, a view moved, one reloaded at a join) wrote what
- // no copy holds: kept, a rollback would replay it into what it
- // wrote already (the bytes fed twice). It goes with the copy: the
- // state is the confirmed one, nothing being on top.
+ // The one question: a unit that wrote an emulator (a terminal's
+ // output, a grid reflowed, one opened, closed or reloaded at a join)
+ // wrote what no copy holds: kept, a rollback would replay it into
+ // what it wrote already (the bytes fed twice). It goes with the
+ // copy: the state is the confirmed one, nothing being on top.
const c = r.core();
for (c.retired_panes.current, c.retired_panes.previous) |x, y| retiring = retiring or x != null or y != null;
for (c.panes, serials) |slot, n| retiring = retiring or (if (slot) |pane| pane.serial else 0) != n;
- if (retiring or Term.writes != writes) {
+ if (retiring or moved) {
r.copy.release();
rollback.Epoch.select(null);
return r.clearSince();
@@ -952,6 +957,7 @@ pub const Replica = struct {
tape.beginSpeculation(&r.t);
// The time it was sent with, every time it is applied.
r.t.guess = p.guess;
+ const outside = c.outside.n;
c.toView(.at(r.view));
// Input into a terminal: what reaches its program (a key in tty
// mode, a paste, a mouse report) is a pty write, the authority's,
@@ -960,9 +966,9 @@ pub const Replica = struct {
// the same bytes in the same order as the session's: it is applied.
c.update(in.event);
c.step();
- // It owes an emulator a write it skipped (a grid's size): replayed
- // when confirmed.
- p.terminal = r.t.touched;
+ // What it owes outside the copy (an emulator's grid: Term.resize
+ // counts the write a speculation skips): replayed when confirmed.
+ p.outside = c.outside.n -% outside;
const clean = tape.endSpeculation(&r.t);
const dt = nowNs() - t0;
// Where a blob was written, should one be: the input that did.
@@ -3973,6 +3979,39 @@ test "bytes: a session's output taken over a copy held ahead is fed once: a roll
try sameTerminals(b.a, b.r);
}
+test "bytes: a session's reflow of a terminal's grid over a copy held ahead is the count's to decide: the copy goes, and a rollback does not reflow the grid again" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const b = try BytesPair.init();
+ defer b.deinit();
+ try b.output("a line long enough to wrap once the grid is narrower than it is now, and then some more\r\n");
+ b.r.ready();
+ try std.testing.expect(b.r.copy.held);
+ // The unit carries no output: its sync reflows the grid, which no record
+ // names; the core that applied it counted the write.
+ const before = b.r.core().outside.n;
+ b.r.confirm(try b.a.own(.{ .resize = .{ .cols = 40, .rows = 20 } }));
+ b.r.settle();
+ try std.testing.expect(b.r.core().outside.n != before);
+ try std.testing.expect(!b.r.copy.held);
+ try b.rollback();
+ try agrees(b.a, b.r);
+ try sameTerminals(b.a, b.r);
+}
+
+test "the count of writes outside the copy is digested: a session's emulator written where its client's is not (a colour set, which no other digested field shows) is a digest that names `outside`" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const b = try BytesPair.init();
+ defer b.deinit();
+ try std.testing.expectEqualStrings("", try partsApart(b.a, b.r, arena.allocator()));
+ // A write of one side's emulator that no unit carries, as a stray one
+ // would be: the count alone says it.
+ const pane = b.a.core.panes[b.shell.index()].?;
+ _ = pardes.Term.feed(&b.a.core.outside, pane.terminal.?, "\x1b[31m", &.{}, &.{}, &.{}, &.{});
+ try std.testing.expectEqualStrings("outside ", try partsApart(b.a, b.r, arena.allocator()));
+}
+
test "bytes: what a terminal's emulator gains while a copy is held is its own: a rollback frees none of it" {
if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
const b = try BytesPair.init();
@@ -4245,12 +4284,12 @@ test "bytes: a client joins from a dump with its terminals whole, and every side
var line: [128]u8 = undefined;
for (0..400) |i| try b.output(try std.fmt.bufPrint(&line, "\x1b[3{d}m{d}\x1b]8;;https://example.com/{d}\x1b\\ link\x1b]8;;\x1b\\\x1b[0m\r\n", .{ i % 8, i, i }));
try b.output("half a colour: \x1b[38;2;10;20");
- const reloads = pardes.Term.writes;
+ const reloads = b.r.core().outside.n;
const joined = (try b.a.join(2, null)).?;
defer gpa.free(joined.dump);
b.r.confirm(joined.unit);
try std.testing.expect(b.r.diverged == null);
- try std.testing.expect(pardes.Term.writes != reloads);
+ try std.testing.expect(b.r.core().outside.n != reloads);
const late = try Replica.initDump(gpa, 2, joined.view, joined.dump, .{ .cols = 80, .rows = 24 });
defer late.deinit();
late.core().echo = null;