From 57aa8d2f9ae851fa9806abbd1fd04abf1fd2b57f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 20:56:03 -0300 Subject: replicas' core for a socket: a client's input settles as a `step` (no clock, no wait, no frame) on both sides, every kind of input speculated, the session's clock guessed, views no frontend here draws left undrawn, and a drawn view's selections given back to its slot pardes.zig: `step`, the middle of a pump, is a client input's own top-level input on the authority and in the replica's speculation, so the two tapes read alike; a pump skips the live view when it is `undrawn` (a replica client's, drawn by its own process); `settle` is the pump's whole middle, the Upgrade's too. tape.zig: a `.step` input; `unattended` is a watched field; a speculation's clock reads answer a guess of the session's clocks (`Guess`); `resync` says the host and its writes again for a joiner; `adoptCore`; a short ret is shown in a trace. views.zig: `drawOne` draws a replica client's own view between steps; `drawParked` gives a drawn view's selections back to its slot, for drawOthers too. rollback.zig: std's lists by walk.isList (a safe build's have a third field). Co-Authored-By: Claude Opus 5.5 --- src/tape.zig | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'src/tape.zig') diff --git a/src/tape.zig b/src/tape.zig index b1080149..c2ddbb9d 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -36,6 +36,7 @@ pub const Input = union(enum) { update: pardes.Event, serve_fs: pardes.ctlfs.Req, pump, + step, perform: pardes.Effect, next_effect, advance: u64, @@ -93,9 +94,12 @@ pub const Site = enum(u16) { }; /// Fields a host writes straight into the core between its calls. -const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active" }; +const watched = .{ "native_images", "native_image_patches", "present_skipped", "shell_continuous", "needs_frame", "quit", "windowed", "active", "unattended" }; const Kind = enum(u8) { input, done, host, ret, set, digest }; +/// A digest record's kind byte (replica.zig reads a unit's last). +pub const digest_kind: u8 = @backingInt(Kind.digest); +pub const set_kind: u8 = @backingInt(Kind.set); const host_fns = @typeInfo(VTable).@"struct".field_names; /// The `set` that says which Host functions the recording host had. const set_mask = 0xff; @@ -163,6 +167,10 @@ pub const Tape = struct { /// output carries one, at the pty's read rate), not one a field; what /// differs is then not named. whole_digest: bool = false, + /// A replica client's guess of the session's clocks for a speculation + /// (src/replica.zig): its clock reads (a message's stamp, a file's + /// mtime) answer this instead of the clocks here. + guess: ?Guess = null, fn record(t: *Tape, kind: Kind, comptime T: type, v: *const T) void { t.scratch.clearRetainingCapacity(); @@ -501,6 +509,7 @@ pub const Tape = struct { .update => |ev| p.update(ev), .serve_fs => |req| _ = p.serveFs(req), .pump => p.pump(t.hostOf(t.mask)) catch {}, + .step => p.step(), .perform => |e| p.perform(e), .next_effect => _ = p.nextEffect(), .advance => |now| p.advance(now), @@ -758,6 +767,12 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt t.poisoned = true; return poisonOf(R); } + if (comptime s == .clock or s == .mtime) if (t.speculating) if (t.guess) |g| { + const r = guessed(R, g); + t.record(.host, u16, &id); + t.record(.ret, R, &r); + return r; + }; t.record(.host, u16, &id); t.seeFields(); t.host_side = true; @@ -768,6 +783,43 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt return r; } +pub const Guess = struct { real_ns: i64, mono_ns: i64, gmtoff: i32 }; + +/// A clock read of the session's, as a replica client guesses it: a file's +/// mtime (realtime seconds), a message's clock (monotonic ms), a message's +/// time of day (in the session's zone). +fn guessed(comptime R: type, g: Guess) R { + if (R == u32) return std.math.cast(u32, @divFloor(g.real_ns, std.time.ns_per_s)) orelse 0; + if (R == i64) return @divFloor(g.mono_ns, std.time.ns_per_ms); + if (R == [8]u8) { + var clock: [8]u8 = "--:--:--".*; + const notime = if (libc.getenv("PARDES_NOTIME")) |v| std.mem.span(v).len != 0 else false; + if (notime) return clock; + const day = @mod(@divFloor(g.real_ns, std.time.ns_per_s) + g.gmtoff, 86400); + _ = std.fmt.bufPrint(&clock, "{d:0>2}:{d:0>2}:{d:0>2}", .{ @as(u32, @intCast(@divFloor(day, 3600))), @as(u32, @intCast(@mod(@divFloor(day, 60), 60))), @as(u32, @intCast(@mod(day, 60))) }) catch {}; + return clock; + } + @compileError("tape: no guess of a clock read as " ++ @typeName(R)); +} + +/// A core made outside the tape (a replica client's, from a dump) is the +/// one it replays into. +pub fn adoptCore(t: *Tape, p: *Pardes) void { + t.core = p; + p.host = t.hostOf(t.mask); +} + +/// A replica joins the session recorded on `t` (src/replica.zig): what the +/// host wrote in the core and which host it is are said again at the next +/// input, for a core made from a dump, which has neither. +pub fn resync(t: *Tape) void { + t.seen = @splat(0); + if (t.core) |p| if (p.host.vtable == &t.vtable) { + p.host = t.real; + }; + t.real = .{}; +} + /// A budget of `budget_ns` from `since_ns` on the monotonic clock is spent: /// what the clock said is taped, so a replica stops its work where this did. pub fn over(since_ns: u64, budget_ns: u64) bool { @@ -964,6 +1016,7 @@ pub fn describe(bytes: []const u8, w: *std.Io.Writer) void { const id = std.mem.bytesToValue(u16, payload[0..2]); w.print("host {s}", .{if (id < host_fns.len) host_fns[id] else @tagName(@as(Site, @fromBackingInt(@intCast(id - host_fns.len))))}) catch {}; }, + .ret => if (payload.len <= 8) w.print("ret {x}", .{payload}) catch {} else w.print("ret ({d} bytes)", .{payload.len}) catch {}, else => w.print("{t} ({d} bytes)", .{ kind, payload.len }) catch {}, } w.writeByte('\n') catch {}; -- cgit v1.3