From 514b1ed385cdc2618e68ce13b371f6488a5ee504 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 25 Nov 2026 15:08:20 -0300 Subject: Trail, panes: a walk step never closes a file with unsaved changes, whatever changed it: the pane is held as Keep holds it and the step opens in its reader; and a held pane's reader with unsaved changes is held, not closed with it (the coordinator's structural item after D1's c1: an Edit in a reading pane, then a Look, closed the edited file and lost its change without a word, because every path that changes a file's text had to remember Trail.edited and one forgot); THE CAUSE: Trail.show, the one place a step leaves a file (deinitPane of the pane it replaces), and removePane's tether (a held pane's reader closes with it, SPEC §11) both trusted `held` to say whether the text was the user's, and `held` is set only by the paths that call Trail.edited: File.load holds no pane (the Edit command's way before D1's xtvnwlpo), and Trail.edited holds nothing under `Jumps new-pane`, so a file typed into there is a reading pane again once the session walks; THE FIX: Trail.show reads the text itself (tagline.dirty, the tag's own unsaved mark: a saving file whose revision is not its saved one, or whose file left the disk): a pane it would close holding such a text is kept (Trail.keep) and the step goes where a held pane's jump goes (look.open from it: its reader, made under it; a line past that file's end closes the new reader again and is the miss it was); removePane holds such a reader instead of closing it; D1's c1 stays (an Edit holds at once, so the pane's tag shows it held before any step); RUN (TEST FIRST): Trail.zig "a walk step never closes a file with unsaved changes: ..." changes a reader's text (rank.zig, the Fixture's held pane's reader) by seven paths, then a Look from it at place.zig:8, and asks rank.zig still open with its change, held, and place.zig in its reader: a 9P write to /pane/N/body, an Edit, a pipe `x|tr a-z A-Z` answered, a language server's edit (Ask.applyAction), a paste, a text typed under `Jumps new-pane` with the session then walking, and a new text put in by File.load; on M12 the first five hold already (each calls Trail.edited) and the last two lose the change (`typed_unwalked: the changed rank.zig was closed by the step`, `load: ...`, expected 0 found 2 at Trail.zig:1270); with show's fix the test's last part, the owner closed while its reader holds a File.load's change, fails at Trail.zig:1296 (the reader closed with it); both pass now; a user could hit it after setting `Jumps walk` in a session with an unsaved file (and, before D1's c1, after any Edit), or with any future path that changes a text without Trail.edited; core-test 1109/1280 (171 skipped: M12's 1108 + this test), load 1.1 to 3.6 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Trail.zig | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- core/panes.zig | 7 ++-- 2 files changed, 110 insertions(+), 3 deletions(-) diff --git a/core/Trail.zig b/core/Trail.zig index 73ea1ffb..96d505ae 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -198,7 +198,10 @@ pub fn replaceStep(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { /// acme's Get keeps a window's id: its walk, its serial (`@pN`, a script's /// `/pane/N`), the 9P state its scripts hold and its /log record (a rename), /// the file it left closed as any (Recent keeps its place). A line past the -/// new file's end is a miss that changes nothing. +/// new file's end is a miss that changes nothing. A file with changes it +/// has not saved is never closed by a step, whatever changed it (§6.5): its +/// pane is held, as Keep holds it, and the step opens where a held pane's +/// go, in its reader. fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { const pane = p.panes[id].?; if (pane.file) |f| if (std.mem.eql(u8, f.path, path)) { @@ -207,6 +210,19 @@ fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { lift(p, id); return id; }; + if (pardes.tagline.dirty(pane)) { + keep(pane); + const free = switch (look.open(p, id, path, at, .replace)) { + .opened => |free| free, + .shown, .none => return p.view().active, + }; + if (at.line > panes.File.nlines(p.gpa, &p.panes[free].?.file.?)) { + try p.removePane(free, null); + p.view().active = id; + return error.LineMissing; + } + return free; + } const free = p.freeSlot() orelse return error.NoPaneSlots; const nt = try panes.File.open(p, free, path, at.line); // Refused: closed at the step's end, never placed (Pardes.unplaced). @@ -1193,3 +1209,91 @@ test "Enter on a word of the walk, the keyboard in the tag, goes back to its ste try std.testing.expectEqualStrings("rank.zig:10", f.at(&at)); try std.testing.expect(pane.focus == .body); } + +test "a walk step never closes a file with unsaved changes: a reading pane changed by any path, one that holds it or one that does not, is held as Keep holds it, and the step opens in its reader, the change kept (SPEC §6.5)" { + const th = @import("ninep/harness.zig"); + const tree = @import("ninep/tree.zig"); + const Key = pardes.Key; + const Path = enum { body, edit, pipe, lsp, paste, typed_unwalked, load }; + var lost: usize = 0; + for (std.enums.values(Path)) |path| { + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + const p = f.p; + // rank.zig in the held pane's reader, a reading pane. + f.look(f.held, "rank.zig", 12); + const r = p.view().active; + const pane = p.panes[r].?; + try std.testing.expect(reading(p, pane)); + switch (path) { + // A 9P write to its body. + .body => try std.testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(pane.serial, .body), "changed\n").reply.status), + // An Edit. + .edit => try std.testing.expectEqual(tree.Status.ok, th.wr(p, tree.Node.of(pane.serial, .ctl), "Edit 3a/ changed/\n").reply.status), + // A pipe of its line, answered. + .pipe => { + while (p.nextEffect()) |_| {} + typeKeys(p, "x|"); + p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + var id: ?u32 = null; + while (p.nextEffect()) |e| switch (e) { + .pipe => |asked| id = asked.id, + else => {}, + }; + p.update(.{ .pipe_resp = .{ .id = id.?, .success = true, .outputs = &.{"CHANGED\n"} } }); + }, + // A language server's edit (a code action's records). + .lsp => try std.testing.expect(@import("lsp/Ask.zig").applyAction(p, pane, pane.file.?.revision, "@put 0 0 changed%0A\n")), + // A paste. + .paste => p.update(.{ .paste = "changed\n" }), + // Typed under `Jumps new-pane`, where an edit holds nothing, + // the session then walking. + .typed_unwalked => { + p.settings.jumps = .@"new-pane"; + typeKeys(p, "ichanged"); + p.update(.{ .key = .{ .cp = Key.escape } }); + p.settings.jumps = .walk; + }, + // A new text put in as a load (File.load holds no pane: the + // Edit command's way before it said Trail.edited). + .load => panes.File.load(p, &pane.file.?, try p.gpa.dupe(u8, "changed\n")), + } + const text = try std.testing.allocator.dupe(u8, pane.file.?.content); + defer std.testing.allocator.free(text); + try std.testing.expect(std.mem.indexOf(u8, text, "changed") != null or std.mem.indexOf(u8, text, "CHANGED") != null); + try std.testing.expect(pardes.tagline.dirty(pane)); + // A Look from it at another file. + f.look(r, "place.zig", 8); + var buf: [64]u8 = undefined; + try std.testing.expectEqualStrings("place.zig:8", f.at(&buf)); + const reader_pane = f.active(); + // rank.zig still open with its change, held; place.zig in its reader. + var kept: ?*Pane = null; + for (p.panes) |slot| if (slot) |other| if (other.file) |of| if (std.mem.endsWith(u8, of.path, "/rank.zig")) { + kept = other; + }; + if (kept == null) { + std.debug.print("{t}: the changed rank.zig was closed by the step\n", .{path}); + lost += 1; + continue; + } + try std.testing.expectEqualStrings(text, kept.?.file.?.content); + try std.testing.expect(kept.?.trail.held and kept.? != reader_pane); + try std.testing.expectEqual(kept.?.serial, reader_pane.trail.steps[0].owner); + } + try std.testing.expectEqual(@as(usize, 0), lost); + // Its owner closed: a reader with unsaved changes is held, never closed + // with it (the tether, SPEC §11, closes a reader that only read). + var f: Fixture = .{ .p = undefined, .tmp = undefined }; + try f.init(60); + defer f.deinit(); + f.look(f.held, "rank.zig", 12); + const r = f.p.view().active; + panes.File.load(f.p, &f.p.panes[r].?.file.?, try f.p.gpa.dupe(u8, "changed\n")); + try f.p.removePane(f.held, null); + const kept = f.p.panes[r] orelse return error.TheChangedReaderWasClosed; + try std.testing.expectEqualStrings("changed\n", kept.file.?.content); + try std.testing.expect(kept.trail.held); +} diff --git a/core/panes.zig b/core/panes.zig index fe127acd..280fd3f9 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -1487,8 +1487,11 @@ pub fn removePane(p: *Pardes, id: usize, to: ?usize) error{OutOfMemory}!void { layout.compute(p); }; // A held pane's reader is its tether: it closes with it (pick-ui - // SPEC §11), its rows going where a closed pane's go. - if (Trail.readerOf(p, pane)) |r| try p.removePane(r, null); + // SPEC §11), its rows going where a closed pane's go; one with changes + // it has not saved is held instead, as a step holds it (Trail.show). + if (Trail.readerOf(p, pane)) |r| { + if (tagline.dirty(p.panes[r].?)) Trail.keep(p.panes[r].?) else try p.removePane(r, null); + } // A docked pane's rows go back to its owner and nobody else. const owner = layout.ownerOf(p, id); const place = layout.findPane(p, id); -- cgit v1.3