diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Core.zig | 191 | ||||
| -rw-r--r-- | core/dump.zig | 11 | ||||
| -rw-r--r-- | core/replica.zig | 2 | ||||
| -rw-r--r-- | core/rollback.zig | 37 | ||||
| -rw-r--r-- | core/root.zig | 3 | ||||
| -rw-r--r-- | core/tape.zig | 394 |
6 files changed, 387 insertions, 251 deletions
diff --git a/core/Core.zig b/core/Core.zig index 0e37f945..5bb0809e 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -78,7 +78,6 @@ const recent_files = @import("recent.zig"); const sel_slot = pardes.sel_slot; const syntax = pardes.syntax; const tagline = pardes.tagline; -const tape = pardes.tape; const test_api = pardes.test_api; const themes = pardes.themes; const turn = &pardes.turn; @@ -195,6 +194,13 @@ image_gpa: std.mem.Allocator, kinds: pardes.Kind.Registry = .{}, tree_sitter_gpa: std.mem.Allocator, opts: Options, +/// The tape this core's steps record on, replay from or go through (a +/// session nobody records: `tape.none`), as `gpa` is its allocator: this +/// process's own, in no digest, dump or rollback copy (`replica.local`, +/// rollback.zig's `unsaved`). Set when the core is made (`Options.tape`; +/// `tape.adoptCore` for a client's join, `tape.adopt` for a Restore) and +/// by the session's clients (replica.zig `Peers`). +tape: *pardes.tape.Tape, resize_count: usize = 0, panes: [MAX_PANES]?*Pane = @splat(null), @@ -446,6 +452,7 @@ pub const replica = .{ // How this process was started, and the last dump it made for // its host to write: a session loaded elsewhere has its own. "opts", "dump_out", "dump_export", + "tape", }, .derived = .{ "shell_rows", "diff_output", "locations_cache", "retired_panes", "tty_filter_palette", "body_chrome" }, // The host's handles, the 9P server's fids and log, the state file @@ -514,6 +521,9 @@ pub fn create(gpa: std.mem.Allocator, opts: Options) !*Pardes { .kinds = .started(opts.addons), .tree_sitter_gpa = opts.tree_sitter_allocator orelse gpa, .opts = opts, + // The global still set beside the field (P20): a core made while + // a tape records (PARDES_RECORD, a replica's step) is its. + .tape = opts.tape orelse pardes.tape.on orelse &pardes.tape.none, .tty_toggle = opts.tty_toggle, .views = .{ View{ .frontend = .{ .windowed = opts.windowed }, @@ -538,14 +548,13 @@ pub fn create(gpa: std.mem.Allocator, opts: Options) !*Pardes { /// A new session, its tape written from here when PARDES_RECORD asks. pub fn init(gpa: std.mem.Allocator, opts: Options) !*Pardes { - tape.startFromEnv(); - const taped = tape.enter(null, .{ .init = opts }); - const p = initSession(gpa, opts) catch |err| { - tape.leave(null, taped); - return err; - }; - tape.leave(p, taped); - return p; + pardes.tape.startFromEnv(); + const taped = pardes.tape.enter(null, .{ .init = opts }); + // The tape adopts the core made, none on a failure. + var made: ?*Pardes = null; + defer pardes.tape.leave(made, taped); + made = try initSession(gpa, opts); + return made.?; } fn initSession(gpa: std.mem.Allocator, opts: Options) !*Pardes { @@ -700,7 +709,7 @@ pub fn recentGpa(_: *const Pardes) std.mem.Allocator { pub fn loadRecent(p: *Pardes) void { // A replica's Recent is its own, in memory: the recording session's // state file is every session's of its user (core/tape.zig). - if (tape.replaying()) p.db.kind = .off; + if (pardes.tape.replaying()) p.db.kind = .off; p.db.readRecent(p); } @@ -763,11 +772,11 @@ pub fn recentDot(pane: *const Pane) ?struct { path: []const u8, line: u32, col: /// reads them: its process's options, so the authority's /// (`tape.authority`); a replica's are its own process's. pub fn configDir(p: *Pardes) ?[]const u8 { - return tape.authority(optsConfigDir, .{p}); + return pardes.tape.authority(optsConfigDir, .{p}); } pub fn startupPath(p: *Pardes) ?[]const u8 { - return tape.authority(optsStartupPath, .{p}); + return pardes.tape.authority(optsStartupPath, .{p}); } fn optsConfigDir(p: *Pardes) ?[]const u8 { @@ -782,7 +791,7 @@ fn optsStartupPath(p: *Pardes) ?[]const u8 { /// in scratch. Recent is the authority's (every window of its user /// writes it): a replica has its answer (`tape.authority`). pub fn recentByHash(p: *Pardes, hash: u64) ?recent_files.Entry { - return tape.authority(recentFound, .{ p, hash }); + return pardes.tape.authority(recentFound, .{ p, hash }); } fn recentFound(p: *Pardes, hash: u64) ?recent_files.Entry { @@ -795,9 +804,9 @@ fn recentFound(p: *Pardes, hash: u64) ?recent_files.Entry { } pub fn deinit(p: *Pardes) void { - const taped = tape.enter(p, .{ .deinit = tape.current(p) }); - tape.forget(p); - defer tape.leave(null, taped); + const taped = pardes.tape.enter(p, .{ .deinit = pardes.tape.current(p) }); + pardes.tape.forget(p); + defer pardes.tape.leave(null, taped); if (std.c.getenv("PARDES_FRAMES")) |path| p.frames.dump(std.mem.span(path)); // Every file open now is closed with its dot, and the list kept. for (p.panes) |slot| if (slot) |pane| p.noteClosed(pane); @@ -863,8 +872,8 @@ pub fn endSession(p: *Pardes) void { /// shell feedback after write_dump: remember where the dump landed (the /// topbar grows a `Restore <path>` word pointing at it) pub fn setLastDump(p: *Pardes, path: []const u8) void { - const taped = tape.enter(p, .{ .set_last_dump = path }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .set_last_dump = path }); + defer pardes.tape.leave(p, taped); p.pointRestoreAt(path); ctlfs.events.notePath(p, "dump", path); } @@ -892,8 +901,8 @@ pub fn pointRestoreAt(p: *Pardes, path: []const u8) void { /// the shell polls this each frame: a pending Restore's dump path, or null pub fn takeRestore(p: *Pardes) ?[]const u8 { - const taped = tape.enter(p, .take_restore); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .take_restore); + defer pardes.tape.leave(p, taped); const r = p.restore_req; p.restore_req = null; return r; @@ -917,7 +926,7 @@ fn reportFocus(p: *Pardes) void { // session's step makes the write, off the tape on both sides // (tape.hostCall's pty_write), so the switch shows at once and the // program hears of it a round trip later. - if (tape.speculating() and tape.screenState()) return; + if (pardes.tape.speculating() and pardes.tape.screenState()) return; if (v.focus_told) |old| if (p.panes[old.id]) |pane| { if (pane.serial == old.serial and pardes.Term.modes(pane.terminal).focus) p.emitWrite(old.id, "\x1b[O"); }; @@ -969,8 +978,8 @@ pub fn parkPendingWrite(p: *Pardes, id: usize, bytes: []const u8) void { /// The shell reports the spawned pane's working directory (and later cwd /// changes it observes, e.g. via /proc/<pid>/cwd before each frame). pub fn setCwd(p: *Pardes, id: usize, cwd: []const u8) void { - const taped = tape.enter(p, .{ .set_cwd = .{ .id = id, .cwd = cwd } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .set_cwd = .{ .id = id, .cwd = cwd } }); + defer pardes.tape.leave(p, taped); const pane = p.panes[id] orelse return; // A command pane stays the directory's it was run for (newCommand). if (pane.command != null) return; @@ -1005,8 +1014,8 @@ pub fn shellBin(p: *const Pardes) []const u8 { /// or null with no room. An input, as every host write into the core /// is: a replica of the session makes the same pane. pub fn adoptShell(p: *Pardes) ?u8 { - const taped = tape.enter(p, .adopt_shell); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .adopt_shell); + defer pardes.tape.leave(p, taped); var newest: u32 = 0; for (p.panes) |slot| if (slot) |pn| { newest = @max(newest, pn.serial); @@ -1022,16 +1031,16 @@ pub fn adoptShell(p: *Pardes) ?u8 { /// A terminal's reader failed (no end of file: the host closed its pty): /// its pane takes keys again, and why is said. pub fn readerFailed(p: *Pardes, id: u8, err: anyerror) void { - const taped = tape.enter(p, .{ .reader_failed = .{ .id = id, .err = err } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .reader_failed = .{ .id = id, .err = err } }); + defer pardes.tape.leave(p, taped); const pane = p.panes[id] orelse return; pane.body.mode = .normal; p.reportError(id, "terminal reader", err); } pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_marks: bool) void { - const taped = tape.enter(p, .{ .acknowledge_shell = .{ .id = id, .executable = executable, .prompt_marks = prompt_marks } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .acknowledge_shell = .{ .id = id, .executable = executable, .prompt_marks = prompt_marks } }); + defer pardes.tape.leave(p, taped); if (id < MAX_PANES) if (p.panes[id]) |pane| { ctlfs.pty.shellGone(p, pane, true); // a respawn replaced whatever ran pane.shell_failed = false; @@ -1068,8 +1077,8 @@ pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_ma /// them and a terminal's replica of a window's session must step as /// the window does. pub fn takeFontRequest(p: *Pardes) ?[]const u8 { - const taped = tape.enter(p, .take_font_request); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .take_font_request); + defer pardes.tape.leave(p, taped); if (!p.settings.font.pending or p.font_request_taken) return null; const path = p.settings.font.requested_path.get(); if (path.len == 0) return null; @@ -1083,8 +1092,8 @@ pub fn observeFont( effective_size_hundredths: u16, unit: config.Runtime.FontSizeUnit, ) bool { - const taped = tape.enter(p, .{ .observe_font = .{ .name = effective_name, .size = effective_size_hundredths, .unit = unit } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .observe_font = .{ .name = effective_name, .size = effective_size_hundredths, .unit = unit } }); + defer pardes.tape.leave(p, taped); if (!p.settings.font.effective_name.set(effective_name)) return false; p.settings.font.effective_size_hundredths = effective_size_hundredths; p.settings.font.effective_size_unit = unit; @@ -1099,8 +1108,8 @@ pub fn acknowledgeFont( effective_size_hundredths: u16, unit: config.Runtime.FontSizeUnit, ) bool { - const taped = tape.enter(p, .{ .acknowledge_font = .{ .name = effective_name, .size = effective_size_hundredths, .unit = unit } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .acknowledge_font = .{ .name = effective_name, .size = effective_size_hundredths, .unit = unit } }); + defer pardes.tape.leave(p, taped); if (!p.settings.font.pending or !p.font_request_taken) return false; const changed = !std.mem.eql( u8, @@ -1116,8 +1125,8 @@ pub fn acknowledgeFont( } pub fn rejectFont(p: *Pardes) void { - const taped = tape.enter(p, .reject_font); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .reject_font); + defer pardes.tape.leave(p, taped); if (!p.settings.font.pending or !p.font_request_taken) return; p.settings.font.pending = false; p.font_request_taken = false; @@ -1144,8 +1153,8 @@ pub inline fn view(p: anytype) if (@typeInfo(@TypeOf(p)).pointer.attrs.@"const") /// `pardes --attach`ed), as the live one is laid out; null at /// Views.max. Not live until `toView`. pub fn openView(p: *Pardes) ?u8 { - const taped = tape.enter(p, .open_view); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .open_view); + defer pardes.tape.leave(p, taped); // Every view taken: one kept for a client that has not come back // for it (Views.away) goes first. ponytail: no timer lets one go; // until its place is wanted it costs a parked slot, and a terminal @@ -1166,8 +1175,8 @@ pub fn openView(p: *Pardes) ?u8 { /// The client of view `k` gone: its view goes, the others stay. The /// last one stays the session's own. pub fn closeView(p: *Pardes, k: u8) void { - const taped = tape.enter(p, .{ .close_view = k }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .close_view = k }); + defer pardes.tape.leave(p, taped); Views.close(p, k); p.sync(); // What its sync queued (a terminal it sized, sized again) done here, @@ -1181,8 +1190,8 @@ pub fn closeView(p: *Pardes, k: u8) void { /// goes, and the session's own is laid out as it was (Views.closeKeeping), /// which the client's rejoin copies. Settled as `closeView` is. pub fn leaveView(p: *Pardes, k: u8) void { - const taped = tape.enter(p, .{ .leave_view = k }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .leave_view = k }); + defer pardes.tape.leave(p, taped); Views.closeKeeping(p, k); p.sync(); p.settle(); @@ -1191,8 +1200,8 @@ pub fn leaveView(p: *Pardes, k: u8) void { /// The client of view `k` gone, to join again for it (an Upgrade's /// rejoin, a divergence's): its view kept as it is (Views.away). pub fn awayView(p: *Pardes, k: u8) void { - const taped = tape.enter(p, .{ .away_view = k }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .away_view = k }); + defer pardes.tape.leave(p, taped); Views.away(p, k); } @@ -1200,8 +1209,8 @@ pub fn awayView(p: *Pardes, k: u8) void { /// as it left it; false when it was let go meanwhile. Settled as /// `openView` is, for the join's dump next. pub fn rejoinView(p: *Pardes, k: u8) bool { - const taped = tape.enter(p, .{ .rejoin_view = k }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .rejoin_view = k }); + defer pardes.tape.leave(p, taped); if (!Views.isAway(p, k)) return false; p.views_away &= ~(@as(u8, 1) << @intCast(k)); p.settle(); @@ -1212,8 +1221,8 @@ pub fn rejoinView(p: *Pardes, k: u8) bool { /// Into view `k`, before the input its client sent. pub fn toView(p: *Pardes, k: u8) void { if (k == p.live) return; - const taped = tape.enter(p, .{ .to_view = k }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .to_view = k }); + defer pardes.tape.leave(p, taped); Views.to(p, k); } @@ -1221,14 +1230,14 @@ pub fn toView(p: *Pardes, k: u8) void { /// file reloading under it, a theme file rewritten on disk. `pump` draws /// only when it has a reason to, and this is the reason. pub fn invalidate(p: *Pardes) void { - const taped = tape.enter(p, .invalidate); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .invalidate); + defer pardes.tape.leave(p, taped); p.needs_frame = true; } pub fn reloadWatchedFile(p: *Pardes, id: u8, bytes: []const u8) bool { - const taped = tape.enter(p, .{ .reload_watched_file = .{ .id = id, .bytes = bytes } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .reload_watched_file = .{ .id = id, .bytes = bytes } }); + defer pardes.tape.leave(p, taped); p.invalidate(); look.invalidateLookHover(p, id); const applied = p.applyWatchedFileChanged(id, bytes); @@ -1245,8 +1254,8 @@ pub fn emit(p: *Pardes, e: Effect) void { } pub fn nextEffect(p: *Pardes) ?Effect { - const taped = tape.enter(p, .next_effect); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .next_effect); + defer pardes.tape.leave(p, taped); p.refillPendingWrites(); if (p.effects_len == 0) { p.effects_head = 0; @@ -1331,8 +1340,8 @@ pub fn hostKillJob(p: *const Pardes, id: usize) bool { /// on it, or written to it from now on, hears `error shell gone` /// rather than wait for a prompt that never comes. pub fn shellFailed(p: *Pardes, id: u8, err: anyerror) void { - const taped = tape.enter(p, .{ .shell_failed = .{ .id = id, .err = err } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .shell_failed = .{ .id = id, .err = err } }); + defer pardes.tape.leave(p, taped); const pane = p.panes[id] orelse return; pane.shell_failed = true; ctlfs.pty.shellGone(p, pane, false); @@ -1354,8 +1363,8 @@ pub fn shellFailed(p: *Pardes, id: u8, err: anyerror) void { /// A shell started again in a terminal whose shell runs (pty/ctl's /// exec) did not start: the host kept the running one, so only said. pub fn restartFailed(p: *Pardes, id: u8, err: anyerror) void { - const taped = tape.enter(p, .{ .restart_failed = .{ .id = id, .err = err } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .restart_failed = .{ .id = id, .err = err } }); + defer pardes.tape.leave(p, taped); if (p.panes[id] == null) return; p.sayShellFailure(id, err); } @@ -1368,7 +1377,7 @@ fn sayShellFailure(p: *Pardes, id: u8, err: anyerror) void { // The script is there; the program its `#!` names is not. var said: pardes.Kind.Buf(320) = .{}; var interp: [256]u8 = undefined; - const name = tape.value(.interpreter, missingInterpreter, .{ pane.shell orelse p.shellBin(), &interp }); + const name = pardes.tape.value(.interpreter, missingInterpreter, .{ pane.shell orelse p.shellBin(), &interp }); p.reportFailure(id, if (name) |n| said.print("shell: interpreter {s} not found", .{n}) else "shell: interpreter not found"); } else { p.reportError(id, "shell", err); @@ -1393,13 +1402,13 @@ fn parentTrouble(path: []const u8, err: anyerror) struct { dir: []const u8, kind const local = filesystem.localPath(dir); const kind: ?std.Io.File.Kind = if (local) |l| exec.kindOf(l) else .directory; const missing = err == error.FileNotFound or err == error.NotDir or kind != .directory; - const denied = missing and (if (local) |l| tape.value(.denied, filesystem.deniedAbove, .{l}) else false); + const denied = missing and (if (local) |l| pardes.tape.value(.denied, filesystem.deniedAbove, .{l}) else false); return .{ .dir = dir, .kind = kind, .missing = missing, .denied = denied }; } pub fn saveFailed(p: *Pardes, id: u8, path: []const u8, err: anyerror) void { - const taped = tape.enter(p, .{ .save_failed = .{ .id = id, .path = path, .err = err } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .save_failed = .{ .id = id, .path = path, .err = err } }); + defer pardes.tape.leave(p, taped); if (p.panes[id]) |pane| if (pane.file) |*f| { // The `-%` spelling fs.zig already uses for "make this dirty". f.saved_revision = f.revision -% 1; @@ -1422,8 +1431,8 @@ pub fn saveFailed(p: *Pardes, id: u8, path: []const u8, err: anyerror) void { /// Save's does. pub fn dumpFailed(p: *Pardes, path: []const u8, err: anyerror) void { const v = p.view(); - const taped = tape.enter(p, .{ .dump_failed = .{ .path = path, .err = err } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .dump_failed = .{ .path = path, .err = err } }); + defer pardes.tape.leave(p, taped); var what: [limits.host_path_cap + 8]u8 = undefined; const operation = std.fmt.bufPrint(&what, "Dump {s}", .{path[0..@min(path.len, limits.host_path_cap)]}) catch unreachable; // cap + 5 fits cap + 8 // Into a DumpDir that is not there: said so, ENOENT, as a Save is; @@ -1465,8 +1474,8 @@ pub fn animationActive(p: *const Pardes) bool { pub fn pump(p: *Pardes, host: Host) !void { p.host = host; - const taped = tape.enter(p, .pump); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .pump); + defer pardes.tape.leave(p, taped); // The host as the tape sees it, while it records. const h = p.host; const v = h.vtable; @@ -1550,8 +1559,8 @@ pub fn settle(p: *Pardes) void { /// authority's tape of it to read the same. The session's own pump /// draws it after. pub fn step(p: *Pardes) void { - const taped = tape.enter(p, .step); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .step); + defer pardes.tape.leave(p, taped); p.settle(); } @@ -1562,8 +1571,8 @@ pub fn step(p: *Pardes) void { /// that changes a pane costs a frame, which is why a round trip on a /// local socket costs microseconds and not a vsync. pub fn serveFs(p: *Pardes, req: ctlfs.Req) ctlfs.Reply { - const taped = tape.enter(p, .{ .serve_fs = req }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .serve_fs = req }); + defer pardes.tape.leave(p, taped); // The held write of an Edit whose commands run, asked again by a // retry of its park: still waiting, or its answer, never run twice. if (p.fs.edit_hold) |*h| if (h.names(req)) { @@ -1604,8 +1613,8 @@ pub fn serveFs(p: *Pardes, req: ctlfs.Req) ctlfs.Reply { pub fn update(p: *Pardes, ev: Event) void { const v = p.view(); - const taped = tape.enter(p, .{ .update = ev }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .update = ev }); + defer pardes.tape.leave(p, taped); defer pardes.panes.File.syncTwins(p); defer p.reportFocus(); // A tick with no deadline due is the one event that cannot change @@ -1756,15 +1765,15 @@ fn fsReport(p: *Pardes) void { pub fn readSessionFacts(p: *Pardes) void { // A dump made since carries them all: its shell is never empty. if (p.session_shell_len != 0) return; - if (p.session_dir_len == null) if (tape.value(.env, processDir, .{&p.session_dir})) |dir| { + if (p.session_dir_len == null) if (pardes.tape.value(.env, processDir, .{&p.session_dir})) |dir| { std.mem.copyForwards(u8, &p.session_dir, dir); p.session_dir_len = @intCast(dir.len); }; - if (tape.value(.env, filesystem.processHome, .{})) |home| if (home.len <= p.session_home.len) { + if (pardes.tape.value(.env, filesystem.processHome, .{})) |home| if (home.len <= p.session_home.len) { @memcpy(p.session_home[0..home.len], home); p.session_home_len = @intCast(home.len); }; - const shell = tape.value(.env, config.defaultShell, .{}); + const shell = pardes.tape.value(.env, config.defaultShell, .{}); const kept = shell[0..@min(shell.len, p.session_shell.len)]; @memcpy(p.session_shell[0..kept.len], kept); p.session_shell_len = @intCast(kept.len); @@ -1808,8 +1817,8 @@ pub const SavePath = Effect.Buf(effect_path_cap); /// What was said of a failure just now on pane `id` is also the waiting /// 9P write's (9p_io, `fs.late_failure`). pub fn noteLateFailure(p: *Pardes, id: usize) void { - const taped = tape.enter(p, .{ .note_late_failure = id }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .note_late_failure = id }); + defer pardes.tape.leave(p, taped); // Said by reportFailure just now: its words fitted whole, the // reason kept; else what the message row has. const said = if (p.fs.said_at == p.fs.failures and p.fs.said_len > 0) @@ -1824,8 +1833,8 @@ pub fn noteLateFailure(p: *Pardes, id: usize) void { /// under it now, a drag's moving end and a look-hover's word. pub fn presented(p: *Pardes) void { const v = p.view(); - const taped = tape.enter(p, .presented); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .presented); + defer pardes.tape.leave(p, taped); if (v.pointer_inside and v.drag != .none) { v.hover_col = v.pointer_raw_col; v.hover_row = v.pointer_raw_row; @@ -1837,8 +1846,8 @@ pub fn presented(p: *Pardes) void { /// The window's answer to a WindowOpacity: done, or `failure` says why /// not, `applied` the opacity it keeps. pub fn windowOpacityDone(p: *Pardes, applied: u8, failure: ?[]const u8) void { - const taped = tape.enter(p, .{ .window_opacity_done = .{ .applied = applied, .failure = failure } }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .window_opacity_done = .{ .applied = applied, .failure = failure } }); + defer pardes.tape.leave(p, taped); p.settings.window_opacity_pending = false; const reason = failure orelse return; // Do not claim a value the compositor refused; an explicit retry of @@ -1970,7 +1979,7 @@ pub fn sync(p: *Pardes) void { // A directory pane's entries follow its width, as acme's // columns do (Dir.fit). if (pane.file) |file| if (file.listing != null and r.h > pane.tag_rows) pardes.panes.Dir.fit(p, pane, pane.cols); - if (pane.pending_command.wait == .input) pane.releasePendingCommand(p, id, tape.value(.prompt, struct { + if (pane.pending_command.wait == .input) pane.releasePendingCommand(p, id, pardes.tape.value(.prompt, struct { fn ready(at: *const Pane) bool { return pardes.Term.prompt(at.terminal).input_ready; } @@ -1982,7 +1991,7 @@ pub fn sync(p: *Pardes) void { // its shell's FIRST prompt, or never: typed later it would land // in whatever runs then, or in the middle of a line someone // typed (Pane.greeting). - if (pane.greet and pane.isTerminal() and p.resize_count > 0) switch (tape.value(.prompt, Pane.greeting, .{pane})) { + if (pane.greet and pane.isTerminal() and p.resize_count > 0) switch (pardes.tape.value(.prompt, Pane.greeting, .{pane})) { .wait => {}, .never => pane.greet = false, .now => { @@ -2107,8 +2116,8 @@ pub fn nextWake(p: *const Pardes) ?u64 { /// the look-hover delay) is jumped to its end in one step and drawn /// once. Idle, time is not banked: the next wait starts from now. pub fn advance(p: *Pardes, now_ns: u64) void { - const taped = tape.enter(p, .{ .advance = now_ns }); - defer tape.leave(p, taped); + const taped = pardes.tape.enter(p, .{ .advance = now_ns }); + defer pardes.tape.leave(p, taped); // The first time a shell's clock is seen, core time starts there. const started = p.clock.started; p.clock.started = true; @@ -2119,7 +2128,7 @@ pub fn advance(p: *Pardes, now_ns: u64) void { // The session's ticks are the recording session's: a picture still // decoding or a fade still moving asks for frames the way a message // waiting to go does, and a replica that draws nothing has none. - while (tape.value(.wake, nextWake, .{p})) |due| { + while (pardes.tape.value(.wake, nextWake, .{p})) |due| { if (due > now_ns) break; // Only waiting until `due`: its counters move there at once. const held = (due - p.clock.stepped_ns) / frame; @@ -2241,9 +2250,9 @@ pub const Boot = enum { }; pub const Options = struct { - /// Not taped: the client's list of distrusted kinds, and where its - /// runner keeps its frame statistics. - pub const replica = .{ .local = .{ "distrusted", "frames" } }; + /// Not taped: the client's list of distrusted kinds, where its runner + /// keeps its frame statistics, and the core's tape. + pub const replica = .{ .local = .{ "distrusted", "frames", "tape" } }; tty_only: bool = false, /// The first view's frontend is a window (`Pardes.frontend`), as a /// window's build says: an option, taped with the rest, so that a @@ -2288,6 +2297,8 @@ pub const Options = struct { /// A client's: the kinds it caught breaking PA_PLAIN, its frontend's /// list, which outlives each core it joins with. In no tape and no dump. distrusted: ?*pardes.Addon.Distrusted = null, + /// The core's tape (`Pardes.tape`); null: `tape.on`'s, or `tape.none`. + tape: ?*pardes.tape.Tape = null, /// What the host's scan of the addon directories found (Addon.scan). addons: []const pardes.Kind.Loaded = &.{}, tree_sitter_allocator: ?std.mem.Allocator = null, diff --git a/core/dump.zig b/core/dump.zig index d8d87fc6..a37f46b9 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -536,12 +536,11 @@ pub fn restore(p: *Pardes, bytes: []const u8, from: []const u8) !*Pardes { pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, bytes: []const u8) !*Pardes { pardes.tape.startFromEnv(); const taped = pardes.tape.enter(null, .{ .init_from_dump = .{ .opts = opts, .bytes = bytes } }); - const p = (if (Handover.carries(gpa, bytes)) carriedWindow(gpa, opts, bytes) else loadAndRevive(gpa, opts, bytes, null, null)) catch |err| { - pardes.tape.leave(null, taped); - return err; - }; - pardes.tape.leave(p, taped); - return p; + // The tape adopts the core made, none on a failure. + var made: ?*Pardes = null; + defer pardes.tape.leave(made, taped); + made = try (if (Handover.carries(gpa, bytes)) carriedWindow(gpa, opts, bytes) else loadAndRevive(gpa, opts, bytes, null, null)); + return made.?; } /// A window of its own for panes a grip carried out of another (Handover): diff --git a/core/replica.zig b/core/replica.zig index 31a06214..f290cdb3 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -2201,6 +2201,7 @@ pub const Peers = struct { ps.t.whole_digest = std.c.getenv("PARDES_REPLICA_TRACE") == null; ps.screens = .init(ps.gpa); tape.on = &ps.t; + core.tape = &ps.t; ps.recording = true; ps.samples = std.c.getenv("PARDES_REPLICA_STATS") != null; } @@ -2405,6 +2406,7 @@ pub const Peers = struct { ps.screens.deinit(core); if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real; tape.on = null; + core.tape = core.opts.tape orelse &tape.none; tape.release(&ps.t); ps.recording = false; } diff --git a/core/rollback.zig b/core/rollback.zig index a6c6e5bd..53c7b210 100644 --- a/core/rollback.zig +++ b/core/rollback.zig @@ -523,11 +523,12 @@ fn annotatedStruct(comptime T: type) bool { /// own: on the host's heap (Pardes.recentGpa), which the epoch never puts a /// free off for, so their bytes put back would point at what is freed. A /// step reads what the authority holds of them off the tape -/// (tape.authority), never a replica's. +/// (tape.authority), never a replica's. And the core's tape (`Pardes.tape`, +/// `Options.tape`): the process's, which a rollback leaves where it is. fn unsaved(comptime name: []const u8) bool { return std.mem.eql(u8, name, "effects") or std.mem.eql(u8, name, "in_q") or std.mem.eql(u8, name, "frames") or std.mem.eql(u8, name, "own_frames") or std.mem.eql(u8, name, "shell_rows") or std.mem.eql(u8, name, "echo") or std.mem.eql(u8, name, "store") or - std.mem.eql(u8, name, "recent"); + std.mem.eql(u8, name, "recent") or std.mem.eql(u8, name, "tape"); } fn annotated(comptime T: type, comptime kind: []const u8, comptime name: []const u8) bool { @@ -801,3 +802,35 @@ test "a rollback leaves the store and Recent as a speculation left them: they li p.deinit(); try std.testing.expectEqual(@as(usize, 0), e.live.count()); } + +test "a core's tape is this process's own: no digest or dump holds it, and a rollback leaves it where it was set" { + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + // Given none, a core's tape is the one nobody records. + try std.testing.expect(p.tape == &pardes.tape.none); + var other = pardes.tape.memory(std.testing.allocator); + defer pardes.tape.release(&other); + const digest = walk.digest(Pardes, p); + const dumped = try pardes.dump.byteDump(p); + p.tape = &other; + try std.testing.expectEqual(digest, walk.digest(Pardes, p)); + const again = try pardes.dump.byteDump(p); + try std.testing.expectEqualSlices(u8, dumped, again); + gpa.free(again); + gpa.free(dumped); + p.tape = &pardes.tape.none; + // Handed another while a copy is held (a client joins: replica.zig's + // Peers), it keeps it when the copy is put back. + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + p.tape = &other; + try std.testing.expect(c.restore()); + try std.testing.expect(p.tape == &other); + p.tape = &pardes.tape.none; + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} diff --git a/core/root.zig b/core/root.zig index 04909bd7..154467e5 100644 --- a/core/root.zig +++ b/core/root.zig @@ -480,6 +480,7 @@ const allowed_vars = [_]struct { []const u8, []const u8, []const u8 }{ .{ "syntax.zig", "handed handed_exts handed_n", "the languages an editor library's user hands in" }, .{ "syntax.zig", "held_trees", "the trees of the core being stepped, switched per step" }, .{ "tape.zig", "on", "the tape of the core being stepped, switched per step" }, + .{ "tape.zig", "none", "the tape of a session nobody records: never written" }, .{ "walk.zig", "in_blob blobs_out", "a walk's context, set and put back (threadlocal)" }, .{ "walk.zig", "state bytes", "a type's table, made once" }, .{ "term/TerminalImages.zig", "started", "the PNG resize has its allocator, set at start" }, @@ -630,7 +631,7 @@ const machine_lines = [_]struct { []const u8, usize, []const u8 }{ .{ "rollback.zig", 1, "a test helper's directory" }, .{ "root.zig", 4, "dev's modules re-exported (host_io, file_watch, glide, fonts)" }, .{ "selection_pipe.zig", 14, "a pipe's child processes and their files: the host's `pipe` entry" }, - .{ "tape.zig", 10, "the tape's own files and host_io's types: a recording, a replay" }, + .{ "tape.zig", 11, "the tape's own files and host_io's types: a recording, a replay; the `io` a tape hands a site's body" }, .{ "lsp/lsp.zig", 1, "the language server's client (dev): the host's `lsp` entry" }, .{ "ninep/ctl.zig", 1, "the session's pid in a 9P read: the session's server" }, .{ "ninep/events.zig", 2, "the 9P event log's clock: the session's server" }, diff --git a/core/tape.zig b/core/tape.zig index 270147c2..52eb6c03 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -15,8 +15,11 @@ //! made, and each digest is checked against the recorded one -- by two //! replicas at different addresses and a third that never draws. //! -//! The local path pays a null check a call: `on` is null unless -//! PARDES_RECORD names where to write, `<prefix><pid>.tape`. +//! The local path pays a check a call: `on` is null, and a core's tape is +//! `none` (it records nothing), unless PARDES_RECORD names where to write, +//! `<prefix><pid>.tape`. A core holds its tape (`Pardes.tape`) as it holds +//! its allocator; the free functions read `on`, set beside it, until the +//! last of their callers is handed the tape (P20). const std = @import("std"); const pardes = @import("root.zig"); const walk = @import("walk.zig"); @@ -27,6 +30,10 @@ const libc = std.c; pub var on: ?*Tape = null; +/// The tape of a session nobody records (`Tape.through`): a core's when it +/// is given none, so `Pardes.tape` is never null. Nothing writes it. +pub var none: Tape = .{ .gpa = std.heap.page_allocator, .replay = false, .through = true, .scratch = .init(std.heap.page_allocator), .arena = .init(std.heap.page_allocator) }; + /// Every call into the core a host makes (a 9P connection task's too). pub const Input = union(enum) { init: pardes.Options, @@ -217,6 +224,12 @@ const set_mask = 0xff; pub const Tape = struct { gpa: std.mem.Allocator, + /// Zig's `io`, handed to a site's body: in core/, a body is the only + /// code that holds one. + io: std.Io = std.Io.Threaded.global_single_threaded.io(), + /// A session nobody records (`none`): every call goes through as it + /// is, and nothing is taped. + through: bool = false, replay: bool, /// The core whose digest is taken (a Restore replaces it). core: ?*Pardes = null, @@ -598,7 +611,10 @@ pub const Tape = struct { defer t.gpa.free(bytes); var notes: std.ArrayList(u8) = .empty; defer notes.deinit(t.gpa); - const q = pardes.dump.load(t.gpa, p.opts, bytes, ¬es) catch |err| return t.diverge("dump load failed: {t}", .{err}); + // Its own core, on no tape (the session's options name this one). + var opts = p.opts; + opts.tape = &none; + const q = pardes.dump.load(t.gpa, opts, bytes, ¬es) catch |err| return t.diverge("dump load failed: {t}", .{err}); defer q.deinit(); if (notes.items.len > 0) return t.diverge("dump of this build had notes: {s}", .{notes.items}); var want: Parts = undefined; @@ -657,6 +673,7 @@ pub const Tape = struct { opts.image_allocator = gpa; opts.tree_sitter_allocator = gpa; opts.frame_allocator = gpa; + opts.tape = t; t.core = switch (input) { .init => Pardes.init(gpa, opts), .init_from_dump => |d| pardes.dump.initFromDump(gpa, opts, d.bytes), @@ -673,6 +690,7 @@ pub const Tape = struct { .restore => |rs| { const nc = pardes.dump.restore(p, rs.bytes, rs.from) catch |err| return t.diverge("restore failed: {t}", .{err}); t.core = nc; + nc.tape = t; nc.host = t.hostOf(t.mask); }, // The session is over (or the core a Restore replaced is @@ -728,6 +746,199 @@ pub const Tape = struct { } t.finishInput(); } + + // ---- the entry points, on the handle a step is given (`Pardes.tape`, + // or a `tape: *Tape` parameter); the free functions below are the + // same on `on` until the last of their callers is moved ---- + + /// A syscall of the core's own, `f(args)`, made out of the turn (a 9P + /// request through a mount of this very session is answered meanwhile): + /// recorded with what it returned, or answered from the tape and never + /// made. A result with slices is decoded into `a` on replay, the allocator + /// `f` would have used. + pub fn site(t: *Tape, comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + return t.call(s, a, f, args, true); + } + + /// A read of the host's that is no syscall out of the turn (the clock, the + /// environment) or yields for itself: as `site`. + pub fn value(t: *Tape, comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + return t.call(s, null, f, args, false); + } + + /// A read of what the authority alone holds (`.authority` state a step + /// makes digested state from: the session's closed panes, and Recent, which + /// every session of its user writes; the 9P server's readers and socket), + /// taped as `value` is; a speculation that reaches it waits for the + /// authority's answer, as at a host call. A replica's own is never read. + /// What it returns is the tape's arena's on a replay: `f` returns what + /// lives to the step's end (scratch, or the authority's own), and the + /// caller copies what it keeps. + /// + /// A yes or no (whether a script reads a pane's events, whether the + /// session serves a socket) is guessed by a speculation, mosh's way: the + /// authority's last answer to that question, as a replayed unit gave it, + /// recorded as the answer, so its unit confirms the guess or rolls it back. + /// A Look or a click's command in a pane then shows at once, not a round + /// trip late. ponytail: bools alone; a list (the closed panes, Recent) + /// changes with what the user does, and is asked of the authority. + pub fn authority(t: *Tape, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + if (comptime @typeInfo(@TypeOf(f)).@"fn".return_type.? == bool) { + if (t.speculating) if (t.answers.get(question(f, args))) |was| { + const id: u16 = @as(u16, host_fns.len) + @backingInt(Site.authority); + t.record(.host, u16, &id); + t.record(.ret, bool, &was); + return was; + }; + if (t.replay) { + const got = t.value(.authority, f, args); + t.answers.put(t.gpa, question(f, args), got) catch {}; + return got; + } + } + t.poison("the authority's own state"); + return t.value(.authority, f, args); + } + + /// 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(t: *Tape, since_ns: u64, budget_ns: u64) bool { + return t.value(.budget, overNow, .{ since_ns, budget_ns }); + } + + fn call(t: *Tape, comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; + comptime { + const Listed = if (@typeInfo(R) == .error_union) anyerror!@typeInfo(R).error_union.payload else R; + for (results) |T| { + if (T == Listed) break; + } else @compileError("tape: the site ." ++ @tagName(s) ++ " returns " ++ @typeName(R) ++ ", which tape.results does not list"); + } + // No tape where there is no host to record or replay one. + if (t.through) return if (yields) out(f, args) else @call(.auto, f, args); + const id: u16 = @as(u16, host_fns.len) + @backingInt(s); + if (comptime s == .read_file) t.reading = args[1]; + defer if (comptime s == .read_file) { + t.reading = null; + }; + if (t.replay) { + // Diverged (said in `diverged`): what a speculation gets, harmless, + // for the rest of the input. A replica joins again; a replay says so. + if (!t.expectHost(id, @tagName(s))) return poisonOf(R); + var ret = t.last_ret; + return walk.decode(R, &ret, a orelse t.arena.allocator()) catch { + t.diverge("the tape's answer to {t} is short", .{s}); + return poisonOf(R); + }; + } + // The host's own (a shell's spawn asking the shell), or one inside a + // site's syscall: the outer call is the record. + if (t.host_side) return if (yields) out(f, args) else @call(.auto, f, args); + // A syscall a speculating replica must not make: its input waits for + // the authority's answer. + if (yields and t.speculating) { + t.poisoned = true; + t.poisoned_by = @tagName(s); + return poisonOf(R); + } + if (comptime s == .clock or s == .mtime) if (if (t.speculating) t.guess else t.stamp) |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; + const r = if (yields) out(f, args) else @call(.auto, f, args); + t.host_side = false; + t.noteWrites(); + t.record(.ret, R, &r); + return r; + } + + /// Asked by `turn.yield`: a syscall a replica must never make. + pub fn replaying(t: *const Tape) bool { + return t.replay or t.speculating; + } + + /// A syscall of the core's that is no site, made by a replica: what it + /// returns is the authority's to say. A speculation waits for it, and a + /// replay has diverged. + pub fn stray(t: *Tape) void { + if (t.through) return; + if (t.speculating) t.poisoned = true else t.diverge("a syscall that is no tape site, made replaying", .{}); + } + + /// Inside a step: one being recorded, a replica's replay of one, or its + /// speculation. A read made between steps (a frame drawn) is none. + pub fn stepping(t: *const Tape) bool { + return t.depth > 0 or t.replay or t.speculating; + } + + /// A replica's core under screen-state terminals: its emulators are + /// written by `apply` alone, never by its own steps. + pub fn screenReplica(t: *const Tape) bool { + return t.screen_state and (t.replay or t.speculating); + } + + /// An authority recording for screen-state replicas. + pub fn screenAuthority(t: *const Tape) bool { + return t.screen_state and !t.replay and !t.speculating; + } + + /// What a speculation reached is the authority's (`why`): the input is + /// undone and waits for the authority's order. + pub fn poison(t: *Tape, why: []const u8) void { + if (t.speculating) { + t.poisoned = true; + t.poisoned_by = why; + } + } + + /// A replica's speculation leaves what is the authority's be (a terminal's + /// emulator, a kind's state): true, and the input waits for its order. + pub fn authorityOnly(t: *Tape, comptime why: []const u8) bool { + if (!t.speculating) return false; + t.poison(why); + return true; + } + + /// A speculation that moves a terminal's view: a screen-state replica's + /// may (its rollback puts the view back, replica.zig `views`, and a delta + /// sets it), any other stops (`authorityOnly`). + pub fn viewOnly(t: *Tape, comptime why: []const u8) bool { + if (!t.speculating) return false; + if (t.screen_state) { + t.touched = true; + return false; + } + t.poison(why); + return true; + } + + /// A speculation read a terminal's rows or moved its view (`Tape.touched`). + pub fn touchTerminal(t: *Tape) void { + if (t.speculating) t.touched = true; + } + + /// The core a Restore made is the session's from here. + pub fn adopt(t: *Tape, p: *Pardes) void { + p.tape = t; + if (t.replay or t.through) return; + t.core = p; + t.wrap(p); + } + + /// Whether `p` is the core being recorded (a Restore's old one is not). + pub fn current(t: *const Tape, p: *const Pardes) bool { + return t.core == null or t.core == p; + } + + /// The session's core is freed: no digest of it after. + pub fn forget(t: *Tape, p: *Pardes) void { + if (t.core == p) t.core = null; + } }; const pardes_fields = walk.fieldNames(Pardes); @@ -818,6 +1029,12 @@ fn partsDiffer(got: []const u64, want: []const u64, a: std.mem.Allocator) []cons pub const Entered = struct { outer: bool = false }; pub inline fn enter(p: ?*Pardes, input: Input) Entered { + // A core's tape is the one `on` says while both are (P20: the global + // goes at the last step): every entry a safe build makes checks it. + // With none on, a client's core is entered between its steps (its own + // frame drawn, Replica.draw), its tape idle. + if (std.debug.runtime_safety) if (p) |core| if (if (on) |t| core.tape != t else core.tape.stepping()) + @panic("tape: a core's field is not the tape being stepped (tape.on)"); const t = on orelse return .{}; return t.enterInput(p, &input); } @@ -826,24 +1043,18 @@ pub inline fn leave(p: ?*Pardes, e: Entered) void { if (e.outer) on.?.leaveInput(p); } -/// The core a Restore made is the session's from here. +// The entry points on `on`, the methods' (`Tape.site` and the rest). + pub fn adopt(p: *Pardes) void { - const t = on orelse return; - if (t.replay) return; - t.core = p; - t.wrap(p); + (on orelse &none).adopt(p); } -/// Whether `p` is the core being recorded (a Restore's old one is not). pub fn current(p: *const Pardes) bool { - const t = on orelse return true; - return t.core == null or t.core == p; + return (on orelse &none).current(p); } -/// The session's core is freed: no digest of it after. pub fn forget(p: *Pardes) void { - const t = on orelse return; - if (t.core == p) t.core = null; + (on orelse &none).forget(p); } /// The Host function `i`, through the tape. @@ -999,54 +1210,16 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype return r; } -/// A syscall of the core's own, `f(args)`, made out of the turn (a 9P -/// request through a mount of this very session is answered meanwhile): -/// recorded with what it returned, or answered from the tape and never -/// made. A result with slices is decoded into `a` on replay, the allocator -/// `f` would have used. pub fn site(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { - return call(s, a, f, args, true); + return (on orelse &none).site(s, a, f, args); } -/// A read of the host's that is no syscall out of the turn (the clock, the -/// environment) or yields for itself: as `site`. pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { - return call(s, null, f, args, false); + return (on orelse &none).value(s, f, args); } -/// A read of what the authority alone holds (`.authority` state a step -/// makes digested state from: the session's closed panes, and Recent, which -/// every session of its user writes; the 9P server's readers and socket), -/// taped as `value` is; a speculation that reaches it waits for the -/// authority's answer, as at a host call. A replica's own is never read. -/// What it returns is the tape's arena's on a replay: `f` returns what -/// lives to the step's end (scratch, or the authority's own), and the -/// caller copies what it keeps. -/// -/// A yes or no (whether a script reads a pane's events, whether the -/// session serves a socket) is guessed by a speculation, mosh's way: the -/// authority's last answer to that question, as a replayed unit gave it, -/// recorded as the answer, so its unit confirms the guess or rolls it back. -/// A Look or a click's command in a pane then shows at once, not a round -/// trip late. ponytail: bools alone; a list (the closed panes, Recent) -/// changes with what the user does, and is asked of the authority. pub fn authority(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { - if (comptime @typeInfo(@TypeOf(f)).@"fn".return_type.? == bool) if (on) |t| { - const key = question(f, args); - if (t.speculating) if (t.answers.get(key)) |was| { - const id: u16 = @as(u16, host_fns.len) + @backingInt(Site.authority); - t.record(.host, u16, &id); - t.record(.ret, bool, &was); - return was; - }; - if (t.replay) { - const got = value(.authority, f, args); - t.answers.put(t.gpa, key, got) catch {}; - return got; - } - }; - poison("the authority's own state"); - return value(.authority, f, args); + return (on orelse &none).authority(f, args); } /// What `authority` was asked: `f` and its arguments as they are. @@ -1056,57 +1229,6 @@ fn question(comptime f: anytype, args: anytype) u64 { return h.final(); } -fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { - const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; - comptime { - const Listed = if (@typeInfo(R) == .error_union) anyerror!@typeInfo(R).error_union.payload else R; - for (results) |T| { - if (T == Listed) break; - } else @compileError("tape: the site ." ++ @tagName(s) ++ " returns " ++ @typeName(R) ++ ", which tape.results does not list"); - } - // No tape where there is no host to record or replay one. - const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); - const id: u16 = @as(u16, host_fns.len) + @backingInt(s); - if (comptime s == .read_file) t.reading = args[1]; - defer if (comptime s == .read_file) { - t.reading = null; - }; - if (t.replay) { - // Diverged (said in `diverged`): what a speculation gets, harmless, - // for the rest of the input. A replica joins again; a replay says so. - if (!t.expectHost(id, @tagName(s))) return poisonOf(R); - var ret = t.last_ret; - return walk.decode(R, &ret, a orelse t.arena.allocator()) catch { - t.diverge("the tape's answer to {t} is short", .{s}); - return poisonOf(R); - }; - } - // The host's own (a shell's spawn asking the shell), or one inside a - // site's syscall: the outer call is the record. - if (t.host_side) return if (yields) out(f, args) else @call(.auto, f, args); - // A syscall a speculating replica must not make: its input waits for - // the authority's answer. - if (yields and t.speculating) { - t.poisoned = true; - t.poisoned_by = @tagName(s); - return poisonOf(R); - } - if (comptime s == .clock or s == .mtime) if (if (t.speculating) t.guess else t.stamp) |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; - const r = if (yields) out(f, args) else @call(.auto, f, args); - t.host_side = false; - t.noteWrites(); - t.record(.ret, R, &r); - return r; -} - pub const Guess = struct { real_ns: i64, mono_ns: i64, gmtoff: i32 }; /// A record's bytes by hash kept for a replica's replays (`files`, the @@ -1141,6 +1263,7 @@ pub fn recordDigest(t: *Tape) void { /// one it replays into. pub fn adoptCore(t: *Tape, p: *Pardes) void { t.core = p; + p.tape = t; p.host = t.hostOf(t.mask); } @@ -1155,10 +1278,8 @@ pub fn resync(t: *Tape) void { 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 { - return value(.budget, overNow, .{ since_ns, budget_ns }); + return (on orelse &none).over(since_ns, budget_ns); } fn overNow(since_ns: u64, budget_ns: u64) bool { @@ -1172,88 +1293,57 @@ pub fn out(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".retur return @call(.auto, f, args); } -/// Asked by `turn.yield`: a syscall a replica must never make. pub fn replaying() bool { - return if (on) |t| t.replay or t.speculating else false; + return (on orelse &none).replaying(); } -/// A syscall of the core's that is no site, made by a replica: what it -/// returns is the authority's to say. A speculation waits for it, and a -/// replay has diverged. pub fn stray() void { - const t = on orelse return; - if (t.speculating) t.poisoned = true else t.diverge("a syscall that is no tape site, made replaying", .{}); + (on orelse &none).stray(); } /// A replica speculating (core/replica.zig): state only the authority /// changes (a terminal's emulator) is left alone, and the input waits. pub fn speculating() bool { - return if (on) |t| t.speculating else false; + return (on orelse &none).speculating; } -/// Inside a step: one being recorded, a replica's replay of one, or its -/// speculation. A read made between steps (a frame drawn) is none. pub fn stepping() bool { - return if (on) |t| t.depth > 0 or t.replay or t.speculating else false; + return (on orelse &none).stepping(); } /// Screen-state terminals, on either side (`Tape.screen_state`). pub fn screenState() bool { - return if (on) |t| t.screen_state else false; + return (on orelse &none).screen_state; } -/// A replica's core under screen-state terminals: its emulators are -/// written by `apply` alone, never by its own steps. pub fn screenReplica() bool { - return if (on) |t| t.screen_state and (t.replay or t.speculating) else false; + return (on orelse &none).screenReplica(); } -/// An authority recording for screen-state replicas. pub fn screenAuthority() bool { - return if (on) |t| t.screen_state and !t.replay and !t.speculating else false; + return (on orelse &none).screenAuthority(); } -/// What a speculation reached is the authority's (`why`): the input is -/// undone and waits for the authority's order. pub fn poison(why: []const u8) void { - if (on) |t| if (t.speculating) { - t.poisoned = true; - t.poisoned_by = why; - }; + (on orelse &none).poison(why); } /// A replica's replay came to another state than the authority's (`why`): -/// it joins again. +/// it joins again. (No method: `Tape.diverged` is the field it writes.) pub fn diverged(why: []const u8) void { if (on) |t| if (t.replay) t.diverge("{s}", .{why}); } -/// A replica's speculation leaves what is the authority's be (a terminal's -/// emulator, a kind's state): true, and the input waits for its order. pub fn authorityOnly(comptime why: []const u8) bool { - if (!speculating()) return false; - poison(why); - return true; + return (on orelse &none).authorityOnly(why); } -/// A speculation that moves a terminal's view: a screen-state replica's -/// may (its rollback puts the view back, replica.zig `views`, and a delta -/// sets it), any other stops (`authorityOnly`). pub fn viewOnly(comptime why: []const u8) bool { - if (!speculating()) return false; - if (screenState()) { - touchTerminal(); - return false; - } - poison(why); - return true; + return (on orelse &none).viewOnly(why); } -/// A speculation read a terminal's rows or moved its view (`Tape.touched`). pub fn touchTerminal() void { - if (on) |t| if (t.speculating) { - t.touched = true; - }; + (on orelse &none).touchTerminal(); } /// A harmless stand-in for what a host call or a syscall would have |
