From 757cdaa826131dc5ff2d3ff0b270b8abe041c81a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 02:40:21 -0300 Subject: plumber: external ports (docs/next-plumber.md, phase 3): every port a rule names has a file, plumb/ (0444), listed after rules and send in the order rules named them and never dropped (Plumb.ports, dumped: seeded at boot from the rules in force, grown by each apply, refused past 32), its node a third family in the tree beside panes and columns (Node.ofPort, Target.port; port_bit is 1 << 40, clear of the host tree's and the archive's high bits, which 1 << 57 hit); an open reads (Open.what.port, an events.Queue of its own, 64 KiB): the messages sent from then on, packed, one a read, a short read the front and the next the rest, a read with none held until one comes (p.fs.news, answered by the listener as event reads are); every open gets its copy (fanOut, plumber(4)'s queuesend), one over the queue is not sent to it and the log says so; a port pardes reads itself (edit, web, a loaded kind) hands its readers copies without asking anyone, so a look stays speculated whole; another port's readers are the authority's to know, asked through tape.authority (a replica's own look waits for the first answer and guesses the last), with none its rule set's start runs, or its client, the message held in p.fs.plumb_held for the port's first opener (Plumb.opened, plumber(4)'s queueheld); test: plumber.zig, two opens of plumb/edit each read a look's message while the file opens, an open made after reads nothing, a client's message reaches the first opener and the next goes to the reader with nothing started, a port is not written; monkey9p.zig gains the plumb/ files, rules and messages that start no program, the ports' reads as held reads, and plumb/rules' rest read at the close (whose refusal is the log's alone, as a line file's: restAtClose); replicas.zig: a look to a port a program reads, the second of a replica's own speculated whole and confirmed, digests equal; tree.zig's readdir test lists web and edit; the reference's tree and plumber section, fs-help and the changelog say so --- src/ninep/ctl.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ninep/ctl.zig') diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 7bd586dc..7cdd3327 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -366,7 +366,7 @@ pub fn readResults(p: *Pardes, req: Req) Reply { const looking = if (tree.Node.target(req.node)) |t| switch (t) { .top => |f| f == .look, .pane => |pt| pt.file == .look, - .col => false, + .col, .port => false, } else false; for (kept) |serial| if (!looking or p.paneBySerial(serial) != null) out.print(p.gpa, "{d}\n", .{serial}) catch return Reply.fail(req.tag, E.NOMEM); -- cgit v1.3 From 0a75f3f333e795e741c83c1193e3b833d40caf46 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 02:44:25 -0300 Subject: plumber: the pager sends (docs/next-plumber.md, phase 4): a +Pager filled with a diff (File.looksLikeDiff, pager_diff) is a plumbing message, as acme's `plumb -i` sends what it shows: fillPager's last call is Plumb.paged, which sends src pager, dst edit, wdir the +Pager's directory, type text/x-diff, `action=showdata filename=` (quoted as mesg.c quotes), the text as data; the default rules' last set makes it showdiff (attr delete action, attr add action=showdiff, plumb to edit), and the edit port's showdata and showdiff (Plumb.show, acme's plumbshow) show the data in the pane filename names, the sender's own first, else a new clean pane of that name, showdiff then restacking it in a +Diff in its place (hunks.paged, the code the pager's hook called, unchanged); text paged that is no diff is not sent, where the record had every page sent: the look rules would read it whole, `type is text` holding (a `matches` over a MiB of output, up to the regular expressions' 300 ms budget, at each page), and a page of one line naming a file would ask the disk; the rules in force are read once and kept until their text changes (Plumb.parsed, by its hash, derived state: each process's own), measured in a fast build per send: 29k instructions to read the default rules, a URL's send 37k before, 8.3k after, a word's look 69k before, 40k after, a look that opens a file 262k before (its send 63k), 233k after (34k); test: a diff paged is stacked by the default rule, text that is no diff stays a +Pager, without the rule the +Pager stays as text, and another sender's showdata and showdiff; the design record gains what phases 2-4 did otherwise (detached starts with no output kept, only diffs paged sent, own ports asking nobody, the rules read once); plumber.zig's defaults end with the paged rule; the reference, the default rules' file and the changelog say so --- docs/next-plumber.md | 29 ++++++++++ docs/typ/reference.typ | 10 +++- src/CHANGELOG.md | 7 +++ src/Plumb.zig | 130 ++++++++++++++++++++++++++++++++++++++++++++- src/hunks.zig | 16 +++--- src/ninep/ctl.zig | 2 +- src/plumbing | 7 +++ test/behaviour/plumber.zig | 2 +- 8 files changed, 190 insertions(+), 13 deletions(-) (limited to 'src/ninep/ctl.zig') diff --git a/docs/next-plumber.md b/docs/next-plumber.md index 76a86bea..178814b3 100644 --- a/docs/next-plumber.md +++ b/docs/next-plumber.md @@ -867,6 +867,35 @@ The options as round 1 put them: $0`, `9 page $file`), at the cost of a host effect generalising `open_link`. +## What phases 2-4 did otherwise (2026-11-17) + +- **Q4 is (b) now**, the coordinator's lean while the user was away: a + program a rule starts runs detached, as Plan 9's plumber starts one + (Effect.start, every host's host_io.startDetached: setsid, stdin, stdout + and stderr `/dev/null`, every other fd closed, argv exec'd in wdir with no + shell). Its output is not the session's log, as the lean said: that needs + an inbox message and an Event through the tui, window, macOS and daemon + hosts, which other agents are changing. A failure to start (the program + not found, its directory gone) is said and logged, the child's errno + carried back by a close-on-exec pipe. Its directory and words fit 256 + bytes, an effect's inline payload. +- **Only a diff paged is sent** (§5.4 had every page sent). Text paged is + `type text`, which the look rules do see, contrary to §5.4: their + `matches` would run over the whole output (up to a MiB, and the regular + expressions' 300 ms budget) at each page. A diff is `text/x-diff`, which + every look rule refuses at its first pattern. +- **A port pardes reads itself (edit, web, a kind) asks nobody** whether a + program reads it: its readers' copies are the authority's own to hand + out, and pardes reads it anyway, so a URL's look still speculates whole. + Only another port's dispose asks `tape.authority`. +- **The rules in force are read once** and kept until their text changes + (by its hash, derived state): per send the parse was 29k instructions + in a fast build, about half of a URL's send and 40% of a word's look. +- **`showdata` from another sender** makes a clean pane of its `filename` + (acme's plumbshow); the pager's own message is shown already. +- **plumb/rules' rest read at the close**, refused there, is an `err` in + the log (Plan 9 loses it); the T1 monkey models it as a line file's. + ## Not verified - **Nothing was built or run.** All sizes are estimates, and the test counts are diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index 4d077f71..987f9f9b 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -1073,7 +1073,10 @@ own: a file or directory opened, else the word searched for. a message over its 64 KiB queue is not sent to it (an `err` in the log says so). Messages sent before the open are not its. - The ports pardes reads itself, whatever else reads them: `edit` (the - data opened as a #word("Look") opens it, at `addr=` when given), `web` + data opened as a #word("Look") opens it, at `addr=` when given; with + `action=showdata` the data itself shown, as acme's `plumb -i`, in the + pane `filename=` names, else a new clean one of that name, and with + `action=showdiff` then stacked in a +Diff in its place), `web` (the host's browser, a URL of at most 256 bytes) and each loaded kind by its name (`image`, `pdf`). A message to another port goes to its readers; with none, its rule set's `start` runs (the message dropped), @@ -1096,7 +1099,10 @@ with the file, which declines one with no hunk, opened as text then); an existing directory or file to `edit`, Look's own open (a kind's file in its kind); a commit hash looked at in the editor, in a git work tree, to #word("Diff") with it, `git show` paged (outside one, Diff declines and -the word is searched for): +the word is searched for); a diff paged into a +Pager, which the pager +sends to `edit` (`src` `pager`, `type` `text/x-diff`, `action=showdata` +and the +Pager's `filename`), made `showdiff`: stacked in a +Diff in the ++Pager's place. Text paged that is no diff is not sent: #raw(read("../../src/plumbing"), block: true) diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 823b0e13..0537f575 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,13 @@ ## Unreleased +- A diff paged (`git show | pardes -`) is a plumbing message, as acme's + `plumb -i` sends what it shows: `type text/x-diff` to `edit`, which a + default rule makes `showdiff`, its +Pager stacked in a +Diff as before. + Take the rule out and the +Pager stays as text; a program reading + `plumb/edit` sees the diff. The `edit` port shows any message's data + with `action=showdata` (stacked with `showdiff`). + - A port the plumbing rules name is a file a program reads, `plumb/`, as Plan 9's plumber serves one: every open reads its own copy of what is sent there from then on (`cat plumb/edit` sees each diff --git a/src/Plumb.zig b/src/Plumb.zig index e2edef77..5299a8c3 100644 --- a/src/Plumb.zig +++ b/src/Plumb.zig @@ -29,6 +29,13 @@ written: bool = false, /// named, never dropped (plumber(4)'s directory): plumb/'s files, /// a port's index its node. ports: std.ArrayList([]u8) = .empty, +/// The rules in force read, kept until their text changes (by its hash): +/// a send parses nothing (29k instructions a send in a fast build, half +/// of what a URL's costs). Each process's own. +parsed: ?struct { hash: u64, rules: Rules } = null, +parsed_arena: ?std.heap.ArenaAllocator = null, + +pub const replica = .{ .derived = .{ "parsed", "parsed_arena" } }; /// The default rules, which `include basic` names, as Plan 9's names /// /lib/plumb/basic. @@ -50,6 +57,7 @@ pub const e_client = "no port for \"client\" rule"; pub const e_empty = "empty argument list"; pub fn deinit(pl: *Plumb, gpa: std.mem.Allocator) void { + if (pl.parsed_arena) |*a| a.deinit(); pl.text.deinit(gpa); for (pl.ports.items) |name| gpa.free(name); pl.ports.deinit(gpa); @@ -503,6 +511,22 @@ fn rulesOf(p: *Pardes, arena: std.mem.Allocator) Parser.Error!Rules { return ps.rules; } +/// The rules in force, read once for every send until they change. A +/// send made while a rule's word runs (a look it makes) reads them into +/// the step's scratch: the send it runs in holds the kept ones. +fn inForceRules(p: *Pardes) Parser.Error!Rules { + const pl = &p.plumber; + const hash = std.hash.Wyhash.hash(0, source(p)); + if (pl.parsed) |c| if (c.hash == hash) return c.rules; + if (p.plumb_declined != null) return rulesOf(p, p.scratch.allocator()); + pl.parsed = null; + if (pl.parsed_arena) |*a| a.deinit(); + pl.parsed_arena = .init(p.gpa); + const rules = try rulesOf(p, pl.parsed_arena.?.allocator()); + pl.parsed = .{ .hash = hash, .rules = rules }; + return rules; +} + /// The rules in force as plumb/rules reads them, the defaults printed /// (in the step's scratch) while none were written. pub fn inForce(p: *Pardes) []const u8 { @@ -839,7 +863,7 @@ pub const e_declined = "no matching plumb rule"; /// the step's scratch. pub fn send(p: *Pardes, from: ?usize, msg: Msg, seen: ?Seen) Sent { const arena = p.scratch.allocator(); - var s: Send = .{ .p = p, .from = from, .arena = arena, .rules = rulesOf(p, arena) catch return .{ .refused = "plumb/rules: the rules in force do not read" } }; + var s: Send = .{ .p = p, .from = from, .arena = arena, .rules = inForceRules(p) catch return .{ .refused = "plumb/rules: the rules in force do not read" } }; if (seen) |look_seen| { s.looked = msg.data; s.memo_name = look_seen.name; @@ -960,6 +984,13 @@ fn deliver(s: *Send, id: usize, port: Port, m: *const Msg) Sent { if (m.data.len > 256) p.reportFailure(id, "plumb: web: a URL is at most 256 bytes") else p.emit(.{ .open_link = .from(m.data) }); }, .edit => { + // acme's plumbshow, for `plumb -i`: the data shown, not a name. + var action_buf: [4096]u8 = undefined; + const action = attrValue(m.attr, "action", &action_buf) orelse ""; + if (std.mem.eql(u8, action, "showdata") or std.mem.eql(u8, action, "showdiff")) { + show(s, id, m, std.mem.eql(u8, action, "showdiff")); + return .done; + } var buf: [4096]u8 = undefined; const addr = attrValue(m.attr, "addr", &buf) orelse ""; // A look's own name, as it sent it: its own look opens it. @@ -983,6 +1014,57 @@ fn deliver(s: *Send, id: usize, port: Port, m: *const Msg) Sent { return .done; } +/// The edit port's `showdata` and `showdiff` (acme's plumbshow): the data +/// in the pane `filename` names, the pane that sent it first (a +Pager's +/// own text), else a new one so named (`+Plumbed` with none), clean; +/// `showdiff` then stacks it in a +Diff in that pane's place (hunks.paged), +/// as a diff paged always was. +fn show(s: *Send, id: usize, m: *const Msg, diff: bool) void { + const p = s.p; + var buf: [4096]u8 = undefined; + const filename = attrValue(m.attr, "filename", &buf) orelse ""; + const named = std.mem.eql(u8, filename, if (p.panes[id]) |pn| (if (pn.file) |f| f.path else "") else ""); + const shown: usize = if (named) id else shown: { + const name = if (filename.len > 0) filename else "+Plumbed"; + const free = p.freeSlot() orelse return p.reportError(id, "plumb", error.NoPaneSlots); + pardes.panes.newScratchFor(p, id, .scratch); + const made = p.panes[free] orelse return; + @import("ninep/pane.zig").nameBuffer(p, free, name, false) catch |err| return p.reportError(id, "plumb", err); + const f = &made.file.?; + _ = pardes.panes.File.replace(p, f, 0, f.content.len, m.data); + f.saved_revision = f.revision; + break :shown free; + }; + if (diff) @import("hunks.zig").paged(p, shown); +} + +/// A +Pager filled (ninep/ctl.zig fillPager) with a diff (File.looksLikeDiff: +/// `git show`, a mailed patch): sent to `edit`, as acme's `plumb -i` +/// sends what it shows, `type text/x-diff`, `action=showdata` and the +/// +Pager's name; the default rules make it `showdiff`, stacked in a +Diff +/// in the +Pager's place. Other text paged is not sent: the look rules +/// would read it whole (a `matches` over a MiB of output, up to the +/// regular expressions' budget, at each page). +pub fn paged(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + const f = pane.file orelse return; + if (!f.pager_diff) return; + var attr: std.Io.Writer.Allocating = .init(p.scratch.allocator()); + attr.writer.writeAll("action=showdata filename=") catch return; + // mesg.c's quote, for a name with a blank, a quote, `=` or a tab. + const quote = std.mem.indexOfAny(u8, f.path, " '=\t") != null; + if (quote) attr.writer.writeByte('\'') catch return; + for (f.path) |c| { + if (c == '\'') attr.writer.writeByte('\'') catch return; + attr.writer.writeByte(c) catch return; + } + if (quote) attr.writer.writeByte('\'') catch return; + switch (send(p, id, .{ .src = "pager", .dst = "edit", .wdir = Pardes.paneDir(pane), .type = "text/x-diff", .attr = attr.written(), .data = f.content }, null)) { + .done, .look => {}, + .refused => |why| p.reportFailure(id, why), + } +} + /// A message to a loaded kind's port: its data, a file, opened in that kind /// (or its pane given the keyboard, when one shows it). fn openInKind(s: *Send, id: usize, k: u8, m: *const Msg) void { @@ -1436,3 +1518,49 @@ test "plumb: the Plumb word opens the plumbing file, include basic until one is defer q.deinit(); try testing.expectEqualStrings("type\tis\ttext\ndata\tmatches\t'y.*'\nplumb\tto\tweb\n\n", inForce(q)); } + +test "plumb: a diff paged is sent to edit, stacked in a +Diff by the default rule; without it the +Pager stays, and a reader of plumb/edit gets it" { + const p = try Pardes.init(testing.allocator, .{ .tty_only = true }); + defer p.deinit(); + const page = "commit 0123456789abcdef0123456789abcdef01234567\ndiff --git a/x b/x\n--- a/x\n+++ b/x\n@@ -1 +1 @@\n-old\n+new\n"; + const Page = struct { + fn fill(pp: *Pardes, text: []const u8) !usize { + const id = try pardes.panes.Output.openPager(pp, 0, "/pardes-plumb-page"); + var held: std.ArrayList(u8) = .empty; + defer held.deinit(pp.gpa); + try held.appendSlice(pp.gpa, text); + try @import("ninep/ctl.zig").fillPager(pp, pp.panes[id].?.serial, &held); + return id; + } + fn diffs(pp: *Pardes) usize { + var n: usize = 0; + for (pp.panes) |slot| if (slot) |pn| if (pn.file) |f| { + n += @intFromBool(std.mem.indexOf(u8, f.path, "/+Diff") != null); + }; + return n; + } + }; + // The defaults: the +Pager stacked into a +Diff, and gone. + const stacked = try Page.fill(p, page); + try testing.expect(p.panes[stacked] == null or !std.mem.endsWith(u8, p.panes[stacked].?.file.?.path, "+Pager")); + try testing.expectEqual(@as(usize, 1), Page.diffs(p)); + // Text that is no diff is not sent: its +Pager stays, as ever. + const plain = try Page.fill(p, "just text\n"); + try testing.expect(std.mem.endsWith(u8, p.panes[plain].?.file.?.path, "+Pager")); + // The paged-diff rule taken out: the edit port shows the data where + // it is, and the +Pager stays as text. + try testing.expect(apply(p, false, "plumb/rules", "type is text\narg isfile $data\nplumb to edit\n") == null); + const kept = try Page.fill(p, page); + try testing.expect(std.mem.endsWith(u8, p.panes[kept].?.file.?.path, "+Pager")); + try testing.expectEqualStrings(page, p.panes[kept].?.file.?.content); + try testing.expectEqual(@as(usize, 1), Page.diffs(p)); + // From another sender: shown in a pane of its filename's, clean, and + // with showdiff stacked. + try testing.expect(send(p, 0, .{ .src = "test", .dst = "edit", .type = "text", .attr = "action=showdata filename=/elsewhere/notes", .data = "a note\n" }, null) == .done); + const shown = p.panes[p.view().active].?; + try testing.expectEqualStrings("/elsewhere/notes", shown.file.?.path); + try testing.expectEqualStrings("a note\n", shown.file.?.content); + try testing.expectEqual(shown.file.?.revision, shown.file.?.saved_revision); + try testing.expect(send(p, 0, .{ .src = "test", .dst = "edit", .type = "text/x-diff", .attr = "action=showdiff filename=/elsewhere/change", .data = page }, null) == .done); + try testing.expectEqual(@as(usize, 2), Page.diffs(p)); +} diff --git a/src/hunks.zig b/src/hunks.zig index f316fe48..2fa86ccd 100644 --- a/src/hunks.zig +++ b/src/hunks.zig @@ -601,10 +601,10 @@ fn trimmedWidth(section: []const u8) usize { // ---- the +Diff in the editor ---- // -// Its word (Diff), the open and the text it goes back to; and the hook -// ninep/ctl.zig's filled +Pager calls, which the pager's plumbing message -// replaces. A bare look at a .diff and at a commit hash are plumbing -// rules that start Diff (src/plumbing). +// Its word (Diff), the open and the text it goes back to, and the +// restack of a paged diff. A bare look at a .diff, a commit hash and a +// paged diff are plumbing rules (src/plumbing) that start Diff or ask the +// edit port to show the diff (Plumb.show). /// The `Diff` word (builtins.zig registers it). Bare, on a +Diff, the diff /// it stacks as text; on a diff, its hunks stacked in a +Diff. With a file @@ -674,10 +674,10 @@ fn shows(pane: *const Pane) bool { return pane.colorAlgo() == .diff or (f.output != null and std.meta.eql(f.output.?.from, Output.Origin{ .cmd = .Diff })); } -/// The hook of a +Pager filled with a diff (ninep/ctl.zig's fillPager: -/// `git show`, difftui's patches): its hunks stacked in a +Diff in the -/// +Pager's place, the +Pager closed. The keyboard stays where it was, as -/// a page's. +/// A pane holding a diff (a +Pager filled with one: `git show`, difftui's +/// patches; the edit port's `showdiff`, Plumb.show): its hunks stacked in +/// a +Diff in its place, the pane closed. The keyboard stays where it was, +/// as a page's. pub fn paged(p: *Pardes, id: usize) void { const f = &p.panes[id].?.file.?; const keyboard = p.view().active; diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 7cdd3327..ffd29b4e 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -718,7 +718,7 @@ pub fn fillPager(p: *Pardes, serial: u32, held: *std.ArrayList(u8)) error{OutOfM panes.File.setContent(p, f, page.text); f.pager_spans = page.spans; f.saved_revision = f.revision; - if (f.pager_diff) @import("../hunks.zig").paged(p, id); + pardes.Plumb.paged(p, id); } /// The editor's step: the text of /pager opens closed unread goes in. diff --git a/src/plumbing b/src/plumbing index 1a446f74..4e8cd702 100644 --- a/src/plumbing +++ b/src/plumbing @@ -42,3 +42,10 @@ data matches '[0-9a-f]{7,40}' data matches '[0-9a-f]*[0-9][0-9a-f]*' data matches '[0-9a-f]*[a-f][0-9a-f]*' plumb start Diff $0 + +# a diff paged (git show, git log -p) is shown stacked in a +Diff in +# its +Pager's place +type is text/x-diff +attr delete action +attr add action=showdiff +plumb to edit diff --git a/test/behaviour/plumber.zig b/test/behaviour/plumber.zig index 4144901b..4726714b 100644 --- a/test/behaviour/plumber.zig +++ b/test/behaviour/plumber.zig @@ -14,7 +14,7 @@ test "plumber: plumb/rules reads the rules in force, a write adds rule sets, > r try std.testing.expect(has(try s.read("/plumb/rules"), "type\tis\ttext\ndata\tmatches\t'https?://.*'\nplumb\tto\tweb\n\n")); // Added, the last rule set at the close. try s.write("/plumb/rules", "type is text\ndata matches 'gopher://.*'\nplumb to web\n"); - try std.testing.expect(std.mem.endsWith(u8, try s.read("/plumb/rules"), "plumb\tstart\tDiff $0\n\ntype\tis\ttext\ndata\tmatches\t'gopher://.*'\nplumb\tto\tweb\n\n")); + try std.testing.expect(std.mem.endsWith(u8, try s.read("/plumb/rules"), "attr\tadd\taction=showdiff\nplumb\tto\tedit\n\ntype\tis\ttext\ndata\tmatches\t'gopher://.*'\nplumb\tto\tweb\n\n")); // A rule set refused at the write it is whole in, said with its line; // the rules stay as they were. const before = try s.read("/plumb/rules"); -- cgit v1.3