summaryrefslogtreecommitdiff
path: root/src/dump.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 22:18:34 -0300
committerGabriel Schneider <[email protected]>2026-10-29 23:37:00 -0300
commitddcfd860ef05c37f5536038d8828cc81c0c4125c (patch)
tree447d95dc0769273f2919747e30f5a24faec3fc21 /src/dump.zig
parent2d86687b98f389232b213dfa67cfa731b0280fbb (diff)
downloadpardes-ddcfd860ef05c37f5536038d8828cc81c0c4125c.tar.gz
pardes-ddcfd860ef05c37f5536038d8828cc81c0c4125c.zip
upgrade: the Upgrade word (SPC s U, a tag, 9P) moves a running session onto the pardes installed where it started, in place, by exec at a step boundary: its dump in the DumpDir, its pty masters, terminals, stubs and daemon link kept open and named on the new command line, every view and running shell its own again; the daemon re-execs too, its registry on its command line; a build that cannot load the dump falls back to the old binary, or starts afresh with the shells adopted and the dump kept (test/daemon.py --upgrade, --upgrade --gui)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/dump.zig')
-rw-r--r--src/dump.zig71
1 files changed, 61 insertions, 10 deletions
diff --git a/src/dump.zig b/src/dump.zig
index 2cef46e2..cb639568 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -321,7 +321,7 @@ pub fn restore(p: *Pardes, bytes: []const u8, from: []const u8) !*Pardes {
var opts = p.opts;
opts.cols = p.screen_w;
opts.rows = p.screen_h;
- const replacement = try loadAndRevive(p.gpa, opts, bytes, p);
+ const replacement = try loadAndRevive(p.gpa, opts, bytes, p, null);
replacement.pointRestoreAt(from);
// The tape follows the replacement from here (it is the session).
pardes.tape.adopt(replacement);
@@ -332,7 +332,7 @@ 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 (std.mem.startsWith(u8, bytes, Handover.carried_magic)) carriedWindow(gpa, opts, bytes) else loadAndRevive(gpa, opts, bytes, null)) catch |err| {
+ const p = (if (std.mem.startsWith(u8, bytes, Handover.carried_magic)) carriedWindow(gpa, opts, bytes) else loadAndRevive(gpa, opts, bytes, null, null)) catch |err| {
pardes.tape.leave(null, taped);
return err;
};
@@ -346,18 +346,39 @@ fn carriedWindow(gpa: std.mem.Allocator, opts: Options, bytes: []const u8) !*Par
const p = try Pardes.create(gpa, opts);
errdefer p.deinit();
_ = try Handover.adopt(p, bytes);
- try reviveSession(p, null);
+ try reviveSession(p, null, false);
return p;
}
-fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previous: ?*const Pardes) !*Pardes {
+/// An upgraded session's dump, loaded as the session it was (upgrade.zig):
+/// every view kept, its settings the dump's (the startup config is not run
+/// again), and the terminals whose shells run on (`kept`) not revived: the
+/// host gives each its pty back. Untaped: a recording ends at an upgrade.
+pub fn resumeFromDump(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, kept: *const [MAX_PANES]bool) !*Pardes {
+ return loadAndRevive(gpa, opts, bytes, null, kept);
+}
+
+/// Where an upgrade leaves its dump: the DumpDir (a relative Restore finds
+/// it there by name), as `pardes-upgrade-<pid>.dump`; never $PARDES_DUMP,
+/// which is a Dump's.
+pub fn upgradePath(buf: *[1024:0]u8, dir_setting: []const u8) ?[:0]const u8 {
+ var dir_buf: [900]u8 = undefined;
+ const dir = directory(&dir_buf, dir_setting) orelse return null;
+ _ = std.Io.Dir.cwd().createDirPathStatus(std.Io.Threaded.global_single_threaded.io(), dir, .fromMode(0o755)) catch {};
+ return std.fmt.bufPrintSentinel(buf, "{s}/pardes-upgrade-{d}.dump", .{ dir, std.c.getpid() }, 0) catch null;
+}
+
+fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previous: ?*const Pardes, kept: ?*const [MAX_PANES]bool) !*Pardes {
var notes: std.ArrayList(u8) = .empty;
defer notes.deinit(gpa);
var on_disk: OnDisk = @splat(false);
const p = try loadKnowing(gpa, opts, bytes, &notes, &on_disk);
errdefer p.deinit();
- for (p.panes, on_disk, 0..) |slot, was_on_disk, i| if (slot) |pane| revivePane(p, i, pane, was_on_disk);
- try reviveSession(p, previous);
+ for (p.panes, on_disk, 0..) |slot, was_on_disk, i| if (slot) |pane| {
+ if (kept) |k| if (k[i] and pane.terminal != null) continue;
+ revivePane(p, i, pane, was_on_disk);
+ };
+ try reviveSession(p, previous, kept != null);
// What another build's dump could not give, said where the user looks
// (stderr is a tty session's screen).
if (notes.items.len > 0) {
@@ -370,11 +391,12 @@ fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previ
/// A loaded core given to this host (with `revivePane` for each of its
/// panes): the store and Recent read (a Restore's carried from the session
-/// it replaces) and its ThemeFile asked for again.
-fn reviveSession(p: *Pardes, previous: ?*const Pardes) !void {
+/// it replaces) and its ThemeFile asked for again. An upgrade's (`resumed`)
+/// keeps its views, whose clients come back with it, and its settings.
+fn reviveSession(p: *Pardes, previous: ?*const Pardes, resumed: bool) !void {
// ponytail: one client loads it, so it is one view, the live one's; a
// replica joining (phase 5) takes `load`'s, every view.
- views.only(p);
+ if (!resumed) views.only(p);
if (previous) |old| {
// Never a serial or a request number the old session gave out.
p.next_serial = @max(p.next_serial, old.next_serial);
@@ -403,7 +425,7 @@ fn reviveSession(p: *Pardes, previous: ?*const Pardes) !void {
// The dump's screen until this host says its own (a resize): two
// reflows would lose where a terminal's view was.
p.sync();
- exec.applyStartupConfig(p);
+ if (!resumed) exec.applyStartupConfig(p);
colors.finishThemeInitialization(p);
p.sync();
p.presentation.enabled = true;
@@ -640,6 +662,35 @@ test "a loaded terminal's history stays above its banner, the cursor where it wa
try std.testing.expect(std.mem.indexOf(u8, text, "two") .? < std.mem.indexOf(u8, text, "restored history").?);
}
+test "an upgraded session's dump keeps its views and its running terminal as they were, and settings the startup config would change" {
+ if (comptime !pardes.panes.terminal.enabled or isolated) return error.SkipZigTest;
+ const gpa = std.testing.allocator;
+ const term = pardes.panes.terminal;
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12, .startup_config = "Verbose off\n" });
+ defer p.deinit();
+ while (p.nextEffect()) |_| {}
+ term.feedOutput(p, p.panes[0].?, "one\r\n\x1b[?1049h$ vim");
+ try std.testing.expect(p.executeBuiltinLine(p.active, "Verbose on"));
+ _ = p.openView().?;
+ const bytes = try dumpBytes(p);
+ defer gpa.free(bytes);
+ var kept: [MAX_PANES]bool = @splat(false);
+ kept[0] = true;
+ const q = try resumeFromDump(gpa, .{ .tty_only = true, .cols = 40, .rows = 12, .startup_config = "Verbose off\n" }, bytes, &kept);
+ defer q.deinit();
+ try std.testing.expectEqual(@as(usize, 2), views.count(q));
+ try std.testing.expect(q.settings.verbose);
+ // The program's own screen, no banner, and no shell asked for.
+ try std.testing.expectEqual(term.gridCursor(p.panes[0].?), term.gridCursor(q.panes[0].?));
+ try std.testing.expect(q.panes[0].?.terminal.?.vt.screens.active_key == .alternate);
+ while (q.nextEffect()) |e| try std.testing.expect(e != .spawn);
+ // Not kept, the same terminal is a new shell's under its history.
+ kept[0] = false;
+ const r = try resumeFromDump(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }, bytes, &kept);
+ defer r.deinit();
+ try std.testing.expect(r.panes[0].?.terminal.?.vt.screens.active_key == .primary);
+}
+
test "a session read by name, field by field as another build reads it, is the same session" {
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true });