From 786cddd24e2be36d30847300075cec8c45d1b0ca Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 19 Nov 2026 18:42:12 -0300 Subject: replicas: an idle client's check answered covers the units that came while it was asked, so an idle pair stops waking (the user's bar: an idle editor does not wake): every unit a client took marked a change (`Link.take`), and the session's pump that answers a check sends one before the answer (its clock read and its advance, taped: the clock is digested, so "an empty pump sends no unit" is not safe), so each answer was followed by another check 300 ms later, for ever: a check, a session's wake and pump, a unit and an answer every 0.3 s per idle client; strace of a real idle pair (my own detached session and one terminal client, 30 s) showed every wake of both processes was that cycle (the client's timer thread's 298 ms futex wait, its main loop asking, the session's reader taking the 5-byte check, its loop pumping, its writer, the client's reader taking unit and answer) and nothing else; now a unit taken while a check is asked marks no change (the answer comes after it in the stream's order and its digest covers it), and an answer let go (the client not quiet then) marks one; the test `an idle client's check answered covers the units that came while it was asked: it asks no other until something changes` (a Link on a socket pair over a replica joined from an Authority's dump: a change, a check asked, the pump's unit, the answer, then ten quiet seconds ask nothing; a unit after it asks again; an answer let go marks a change) failed on the base and passes; measured on that real pair idle 60 s after a 5 s settle: session 120 ms of CPU and 1236 context switches (20.6 a second: its loop 3.9, its client's writer 6.8 and reader 9.9 a second), client 140 ms and 1656 (27.6 a second) before; 0 ms and 0 switches in both, and in the daemon, after; the gates on the three changes together (i3): tui core-test 843/986 (143 skipped), shell-test 1022/1025, term 15/15, pane 183/184, kitty 45/45, behaviour-test 275/275 and --replay 275/275, process-test the base's two, gui unit-test 1620/1624 + BaselineHarnessMismatch; replicas --clients 5: diverged 0/0/0, unequal 0/0/0, deltas checked/wrong 1907/0, 2028/0, 1812/0, quiet checks 623/0, 835/0, 771/0, stops not on may_wait 0/0/0, corpus 22/22; daemon.py plain, --far, --games, --window-clients, --upgrade both ways pass, and --games 10 of 10 under 24 busy processes (load 11-37); cross_replay 27 digests agree each; the frame and perf gate against tree1 (d2-i3): frames 77/77 byte-identical, corpus2x +0.005% (noise 0.044%), the 97 per-operation counts within 0.1% (largest +0.031%) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/replica.zig | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/core/replica.zig b/core/replica.zig index fb3a81c4..e2775da8 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -3142,7 +3142,9 @@ pub const Link = struct { } if (c.r.moved) c.dirty = true; c.r.moved = false; - c.changed = true; + // One while a check is asked is the answer's: it comes + // after it in the stream and its digest covers it. + if (!c.asked) c.changed = true; }, .screen => |bytes| { defer std.heap.c_allocator.free(bytes); @@ -3212,7 +3214,12 @@ pub const Link = struct { /// another is a divergence (the replica joins again). fn checked(c: *Link, theirs: u64) void { c.asked = false; - if (!c.r.quiet() or c.r.diverged != null) return; + // Not quiet now, the answer is let go: it covers nothing, and the + // next quiet asks again. + if (!c.r.quiet() or c.r.diverged != null) { + c.changed = true; + return; + } const mine = c.r.digest(); c.r.stats.checks += 1; c.r.since_check = 0; @@ -5407,11 +5414,11 @@ const walk_prefixes = prefixes: { /// The argument a word that means nothing without one is walked with. const walk_samples = [_]struct { []const u8, []const u8 }{ - .{ "Look", "mise.toml" }, .{ "Exec", "echo" }, .{ "Mount", "x" }, .{ "Unmount", "x" }, - .{ "Msg", "hi" }, .{ "ThemeFile", "t.zon" }, .{ "Edit", "s/a/b/" }, .{ "Mini", "x" }, - .{ "Find", "x" }, .{ "Grep", "x" }, .{ "Rename", "x" }, .{ "WsSymbols", "x" }, - .{ "Theme", "acme" }, .{ "Font", "Mono 12" }, .{ "TaglineSize", "100" }, .{ "WindowOpacity", "100" }, - .{ "MessageLinger", "1000" }, .{ "InactiveDim", "0" }, .{ "GripWidth", "100" }, + .{ "Look", "mise.toml" }, .{ "Exec", "echo" }, .{ "Mount", "x" }, .{ "Unmount", "x" }, + .{ "Msg", "hi" }, .{ "ThemeFile", "t.zon" }, .{ "Edit", "s/a/b/" }, .{ "Mini", "x" }, + .{ "Find", "x" }, .{ "Grep", "x" }, .{ "Rename", "x" }, .{ "WsSymbols", "x" }, + .{ "Theme", "acme" }, .{ "Font", "Mono 12" }, .{ "TaglineSize", "100" }, .{ "WindowOpacity", "100" }, + .{ "MessageLinger", "1000" }, .{ "InactiveDim", "0" }, .{ "GripWidth", "100" }, }; fn keyName(buf: []u8, k: pardes.Key) []const u8 { @@ -5830,3 +5837,49 @@ test "a far client's replay of the session's Fonts and Font takes the session's r.settle(); try agrees(a, r); } + +// An idle client asked the session's digest once quiet after a change; the +// session's pump that answers it sends a unit before the answer (its clock +// read), which must not count as a change, or an idle pair asks, wakes and +// answers every 300 ms for ever. A unit while asked is the answer's to +// cover; an answer let go (not quiet then) covers nothing. +test "an idle client's check answered covers the units that came while it was asked: it asks no other until something changes" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + var fds: [2]c_int = undefined; + try std.testing.expectEqual(@as(c_int, 0), libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds)); + defer for (fds) |fd| { + _ = libc.close(fd); + }; + var c: Link = .{ .r = r, .fd = fds[0], .now = std.time.ns_per_s }; + const unit = struct { + fn of(au: *Authority) !Said { + return .{ .unit = try std.heap.c_allocator.dupe(u8, try au.local(.tick)) }; + } + }.of; + // Something changed, 300 ms quiet since its last input: asked. + c.take(try unit(a)); + try std.testing.expectEqual(@as(?u32, null), c.quietCheck()); + try std.testing.expect(c.asked); + // The session's pump for it (a unit), then the answer: agreed, and + // nothing to ask after it however long it stays quiet. + c.take(try unit(a)); + c.take(.{ .checked = digestOf(a) }); + try std.testing.expect(!c.asked and c.r.diverged == null); + c.now.? += 10 * std.time.ns_per_s; + try std.testing.expectEqual(@as(?u32, null), c.quietCheck()); + try std.testing.expect(!c.asked); + // A unit after it is a change again. + c.take(try unit(a)); + c.now.? += std.time.ns_per_s; + _ = c.quietCheck(); + try std.testing.expect(c.asked); + // An answer let go (an input of its own pending then) covers nothing: + // asked again once quiet. + _ = try r.pend(.{ .key = .{ .cp = 'j' } }); + c.take(.{ .checked = digestOf(a) }); + try std.testing.expect(!c.asked and c.changed); +} -- cgit v1.3