diff options
| -rw-r--r-- | docs/next-store.md | 276 | ||||
| -rw-r--r-- | docs/typ/reference.typ | 2 | ||||
| -rw-r--r-- | src/Query.zig | 6 | ||||
| -rw-r--r-- | src/Store.zig | 999 | ||||
| -rw-r--r-- | src/dump.zig | 4 | ||||
| -rw-r--r-- | src/main.zig | 14 | ||||
| -rw-r--r-- | src/pardes.zig | 25 | ||||
| -rw-r--r-- | src/recent.zig | 114 | ||||
| -rw-r--r-- | src/replica.zig | 4 | ||||
| -rw-r--r-- | src/tape.zig | 4 | ||||
| -rw-r--r-- | test/process/look_and_exec.zig | 2 | ||||
| -rw-r--r-- | test/process/other_files.zig | 14 |
12 files changed, 419 insertions, 1045 deletions
diff --git a/docs/next-store.md b/docs/next-store.md index 91fb340c..89997ee2 100644 --- a/docs/next-store.md +++ b/docs/next-store.md @@ -1,4 +1,4 @@ -# The store: bounded state shared by every pardes window +# The store: Recent's file, and this session's closed panes > We should have some persistent state that is bounded (a houndred or so > MiB at most) stored on the fs and shared among all pardes windows. It will @@ -6,237 +6,61 @@ > picker and this should include panes from the current session that were > deleted/closed. -## What was there +## The decision (2026-11-05) -`recent.zig` kept 200 files in `$XDG_STATE_HOME/pardes/recent`, one line a -file. Each process read it once at start and, at the end of any step that -changed it, wrote the whole of its own list back (`saveRecent`: truncate, -write). With two windows that is last-writer-wins: window A opens `a.zig`, -window B opens `b.zig`, whichever saves last erases the other's entry, and -neither ever sees the other's files until it restarts. A crash between the -truncate and the write leaves an empty or half file (the reader passed bad -lines over, so it never stopped pardes starting, but the list was gone). -Closed panes were nowhere: a closed file's dot was its Recent entry, a -closed scratch, terminal or +Errors was gone. +**Recent is a file of at most 200 lines**, `$XDG_STATE_HOME/pardes/recent` +(else `~/.local/state/pardes/recent`), a line a file, the newest first: +`<line> <col> <aline> <acol> <path>`. Every session reads it at start, and +again on use (Recent, `/recent`) when a stat of it says another session +wrote it since. A step that changed the list writes it at its end: `flock` +on the file (held by another session: the next step tries again, so a +frame never waits for one; at exit the lock is waited for), the file read +again with this session's changes put over it (`recent.List.merge`) when +another session wrote it since, and the whole list written over it in +place. A line that is no entry is passed over: a missing, unreadable or +foreign file is an empty list, a reader meeting a write half done reads it +again once its stat moves on, a line a crash tore is one entry fewer, and +pardes always starts. A rename of a new file over it (atomic) cost twice +the write in Debug for nothing the lenient read does not already give. -## The format (argued first; the code follows it) +**Closed panes are this session's, in memory**: the newest 64, within +64 MiB together, each a one-pane dump (`dump.savePane`, at most 8 MiB; a +terminal past it is kept without its history, any other pane is not kept) +and where it was. Panes lists them after the open panes, `(closed)`; +Enter, or a Look at `@pN`, brings one back beside the pane that took its +rows. Restore keeps them; the session's end drops them. -One file, `$XDG_STATE_HOME/pardes/store`, an append-only log of framed -records, plain enough to `cat`: +## What went, and why -``` -@<kind> <len> <crc32, 8 hex>\n<body: len bytes>\n -``` +The store this replaced (2026-10-24, Store.zig 1,135 lines) was one +append-only log of CRC-framed records (`@h`, `@r`, `@c`, `@x`) under +flock, with a resync past torn or garbage records, a compaction by rename, +a 100 MiB cap, and each closed pane a self-describing dump in the file, +read back at its offset. Closed panes were only ever listed by the session +that closed them, so only the file and its bound were shared: an +in-memory list is the same behaviour for far less code and no format (the +position the store's own note held least firmly). Recent needs no log: 200 +lines rewritten whole under the lock is a few kilobytes a step that opens +or closes a file. -The first record is `@h` with the body `pardes-store 1`; a file that does -not start with it is not a store (garbage, an older format) and reads as -empty. The kinds: +What a user loses: the closed panes of other sessions, which the log kept +on disk until a compaction and nothing ever listed. -| kind | body | meaning | -|---|---|---| -| `h` | `pardes-store 1` | the header, a version | -| `r` | `<line> <col> <aline> <acol> <path>` | a recent file and its place: the old `recent` line, unchanged | -| `c` | `<session> <pid> <serial> <column> <gainer> <below> <owner> <rows>\t<name>\n<dump>` | a pane closed in session `<session>`, with where it was, and the pane itself as a one-pane Dump | -| `x` | `<session> <serial>` | that closed pane was reopened: no longer listed | - -Folding the log in order gives the state: a path's latest `r` wins and its -order is the order of last writes (so every window folds the same list); a -`c` not followed by its `x` is a closed pane. - -**Why a closed pane is a Dump.** `dump.zig` already turns any pane into -data and back: a file's dot and scroll (its text only when unsaved), a -scratch's or an output pane's text with its origin and rows' metadata, a -terminal's scrollback (a shell comes back running under its old history, a -command pane finished, as Restore does), a PDF's page, an image. Reusing it -means Panes can bring back every kind of pane with the same fidelity as -Restore, through the same validation (`dump.readZon` checks a record before -anything is built from it), and nothing new to keep in step when a pane -grows a field. - -**Why on disk and not in memory**, since only this session reads its own -closed panes: their text is what makes them big (a terminal's history, a -long +Errors, a discarded unsaved buffer), and the store is where the bound -lives; in memory each window keeps only an index (serial, name, place, -offset). Restore keeps them (same process, same session). A session that -crashed leaves its records for the next compaction to drop. Position, not a -verdict: an in-memory list would be simpler, and I would accept that -pushback; the cap argument is what tipped it. - -**Bounds.** The file never passes `max_bytes` (100 MiB): an append that -would compacts first. A compaction keeps the newest live records within -half of that, so it is not redone at once. A record is at most `max_record` -(8 MiB): a terminal whose dump is bigger is kept without its history (it -comes back as a fresh shell in its directory), any other pane that big is -not kept. Recent keeps `recent.cap` (200) files; a session keeps its 64 -newest closed panes. - -## The sharing - -- **Writers** take `flock(LOCK_EX | LOCK_NB)` on `store.lock`, a file of - its own (the store's inode changes at each compaction, so locking it - would let two writers hold locks on two different files). Busy, the - records wait in memory for the next step: a window never blocks a frame - on another's compaction. At exit the lock is waited for. -- Under the lock a writer first reads what others appended since it last - read (so its list's order is the log's), then writes its records with one - positional write at the end of the file, then notes the new end. -- **Readers** take no lock. On use (Recent, Panes, `/recent`, before an - append) one `stat` of the path: the same inode and size as last read, - nothing to do; the same inode and longer, read the tail; another inode - (a compaction renamed a new file over it) or shorter, read it all again. -- **Torn and garbage records.** A record whose length runs past the end of - the file is being written (or its writer died): reading stops before it - and tries again next time. A complete record with a bad CRC or a bad - header is garbage: the reader resynchronises at the next `\n@` that - starts a valid record, and marks the file for compaction, which the next - writer does under the lock (rewriting only valid records). A missing, - unreadable or foreign file is an empty store: pardes always starts. -- **Compaction**, under the lock: fold the whole file, write the live - records (Recent, oldest first so the fold gives the same order; closed - panes of sessions whose pid is alive, copied byte for byte) to - `store.new`, `fsync`, rename it over `store`. Readers see the new inode. - Triggered when the file is past twice its live size plus 1 MiB, when it - holds garbage, or when an append would pass the cap. -- **Startup reads headers, not bodies**: a new session has no closed panes - of its own, so every `c` body is skipped by its length; only `r` bodies - (a line each) are read. - -### Rejected - -1. **Rewrite the whole file under a lock** (today's scheme, made safe with - read-merge-write and a rename): correct, but every open or close costs - the whole file, and with closed panes' text in it that is megabytes per - Del. -2. **SQLite**: one more C library to vendor and hook into Zig's allocators - (the shared-deps policy), WAL needs shared memory, for three record - kinds and a fold. -3. **A file per entry** (maildir): atomic by rename and lock-free, but the - bound means listing and statting a directory of thousands of files on - every use, two windows race deleting the same oldest, and "what changed" - is not one `stat`. -4. **A file per window, merged by readers**: no lock, but a merge needs - wall clocks across processes for order, every read opens N files, and - dead windows' files need a reaper that races the living. -5. **A fixed-size mmap'd ring**: always 100 MiB on disk, cross-process - writes into shared pages need atomics, a writer that dies mid-record - corrupts in place, and a format change is a migration. -6. **A watcher thread / inotify** (spec says no) or **a daemon** (none - exists; pardes must start without one): a `stat` on use is cheaper than - either and as fresh as anything that is looked at. - -The old `recent` file was read once into a store that did not exist yet, -so nobody's list was lost; with the store in use, that import went -(2026-11-04). - -## As built - -| piece | where | -|---|---| -| the log: framing, reader, resync, fold, flush, compaction | `src/Store.zig` (`Pardes.store`) | -| Recent as records: `Entry.changed`, `List.note` (a record read), `changedFirst` | `src/recent.zig` | -| a pane to and from a Dump, one at a time | `dump.dumpPane`, `dump.restorePane` (lifted out of `dumpState` / `initDump`) | -| a closed pane kept: before its teardown, its rows' gainer after | `Pardes.removePane`, `Pardes.removeColumn` (`Store.keep`, `Store.close`) | -| back where it was | `Store.reopen` → `layout.giveBack` (rows from the gainer alone), `layout.attachFrom` for an attachment whose owner is open, else `exec.placeNew` | -| Panes: open panes, then this session's closed ones, `@p7 notes/a.md (closed)`, dimmer (the gutter's colour between the match marks) | `Query.startSource` / `makeNameRows` / `land` | -| Recent and `/recent` read the store on use | `Query.startSource`, `ninep/tree.zig` | -| the path | `main.storePath`: `$XDG_STATE_HOME/pardes/store` (`Options.store_path`, was `recent_path`) | - -Which closes are kept: every `removePane` / `removeColumn` of a pane the -user had (announced: a pane made and refused in one step never was), not -a query pane nor a +Preview. Exit and Restore keep nothing (Restore's new -core takes the old session's list, `Store.clone`). Brought back, a pane is -a new pane (a fresh serial, as Restore gives); its row goes (`x` record). - -A Look at `@p7` naming a pane this session closed brings it back instead of -the "no pane @p7" miss: the row's text is its address, as everywhere. That -is the only change to Look, in its miss branch. - -**The turn.** Every host call is made out of the turn (`Store.host(f, -args)`, one call each), so a store under a mount of this session's own -tree cannot deadlock, and nothing in the store or Recent changes while a -step is out. `/recent` (a 9P read) reads the store only when no step is -out; otherwise it answers the list as it was. - -## Measured (Debug, the `zz store timings` test, STORE_TIMES=<dir>) - -A store as a running session leaves it: 200 files and 90 closed panes of -1 MiB (94 MB). - -| what | cost | -|---|---| -| a session starting on it (`load`: headers only, every `c` body skipped) | 3.4 ms | -| a refresh with nothing new (one `stat`) | ~1 µs | -| a flush of one record (lock, read the tail, one write) | 36 µs | -| a compaction of the 94 MB file (CRC of every record, copy the live, rename) | 278 ms, to 51 MB (the newest within half the cap) | +The first session of a build with this reads the old `store` once more, +when it is newer than `recent`: its `@r` records' bodies are this file's +lines. The old file is left in place for an older pardes still running. ## Tests -`store: two windows share Recent...` (two cores on one file, a lock held by -a third party delays a write and loses nothing, a fresh window folds the -same list), `store: the cap holds...` (3000 files and 30 closed 20 KB -scratches against a 48 KiB / 256 KiB cap: never past it, compactions -happen, the newest 200 files and the newest closed pane survive), -`store: a garbage or torn file...` (a foreign file, a torn header, junk, -then a whole record; a half-written record waited for; mended by the next -writer), `store: a closed pane comes back...` (from the file, not memory; -its rect, the gainer's and another column's as before the close; listed -`(closed)` in Panes, Enter brings it back, unlisted after a re-read; another -session lists none). `test/fs.py store_shared_between_windows`: two live -sessions over 9P. The related suites (dump, Restore, query, Find, Grep, -Del, Delcol, attach, collapse, jump, Edit, recent) pass. The tty build -compiles. - -Found, not mine: `fs.py recent_survives_restart` fails on the converge -baseline too, at its Recent step (the query pane's rows are relative -names now, the test looks for absolute ones). - -## Open questions, and my position on each - -1. **Closed panes on disk or in memory?** On disk (argued above). The - honest counter: only this session reads them, and a Del now costs a - dump and a write. Measured, a flush is 36 µs; the dump of a big - terminal is the cost (its history base64'd), capped at 8 MiB. -2. **Recent's cap stays 200**, now for every window together. Shared, it - fills faster; 1000 is cheap for the fuzzy scorer. I kept 200 to change - one thing at a time; I would raise it. -3. **"open" means open here.** A file open in another window reads - `(closed)` in this one's Recent (its place is the other window's last - close). Saying `open elsewhere` needs the store to know which windows - are alive and what they hold (a record per open, its pid): doable, - not done. -4. **Compaction is on the loop** (278 ms for 94 MB, Debug). It happens - when the file doubles past what was live, or reaches the cap, which a - window that closes many big terminals does. Off the loop it would need - the disk engine's worker and the lock held across frames; I would do - it only if it shows up. A store of Recent alone is ~20 KB to rewrite - (not timed separately). -5. **A crashed session's closed panes** are dropped at the next - compaction (its pid is gone). Keeping them a day for a "what was I - doing" word would be cheap: the records are there. -6. **A dirty file pane closed with the second Del** comes back with its - unsaved text (it is in its dump, by Restore's rules; not tested - here). That is a feature (undo a discard) - and a surprise (the discard was asked for twice). I kept it; say if a - discard should be final. - -## What I would push back on - -- "shared among all pardes windows" for the closed panes: they are this - session's by the spec, so the sharing is only of the file and its bound. - If the bound is the only reason, an in-memory list with a byte budget - is less code (no `c`/`x` records, no offsets); I chose the disk for the - memory and Restore, but this is the part I hold least firmly. -- The 100 MiB: nothing in Recent comes near it; only closed terminals' - history does. A smaller cap (16 MiB) would bound compaction to ~50 ms - and lose little. 100 MiB is what was asked, so it is the default. -- Reopening a closed pane makes a new serial: `@p7` in old text names a - pane that is now `@p12`. Restore does the same and logs `restored 7 12`; - the store could log the same, not done. - -## Rough - -- The dim tint of a closed Panes row is the gutter colour (`lineno`), not - checked by eye in every theme. -- A Delcol'd pane comes back where `placeNew` puts it (its column is gone). -- An attachment whose owner was closed with it comes back as a plain pane - next to wherever placement puts it (its owner's serial is new). +Store.zig: two windows share Recent (each reads the other's writes on use; +a lock held by a third party delays a write, never loses it); the file +holds the newest 200, newest first; a foreign file's Recent lines are read +and the next write mends it; the old store read once, its newest records +first; a session keeps its newest 64 closed panes; a closed pane comes back +where it was, its rows from the pane that took them, listed `(closed)` in +Panes and unlisted once back, and another session lists none. +recent.zig: a merge puts this session's changes over the file's list. +test/process/other_files.zig: a file opened and closed is in `/recent` +closed and still is in the next session; two sessions share Recent, and a +Del'd pane comes back from `@p` only where it was. test/behaviour/ +other_files.zig: `/recent` and Recent's rows. diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index a7db9e2d..de886c87 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -942,7 +942,7 @@ at 200, ending in `…`. Control characters become spaces. pane from the root). - #file("/recent"): up to 200 files, PDFs and images, most recent first, `open <path>` or `closed <path>` (open in this window or not), every - window's: kept in `$XDG_STATE_HOME/pardes/store`, shared by every pardes + window's: kept in `$XDG_STATE_HOME/pardes/recent`, shared by every pardes process, each reading what the others wrote when it is next read. #word("Recent") lists them in a query pane (line 1 `Recent`, the rest narrowing them by a fuzzy match), an open one at its dot now and a diff --git a/src/Query.zig b/src/Query.zig index cb14df2b..b6c564b1 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -593,7 +593,7 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz // Then the panes this session closed, the latest first, each // `(closed)` and dimmer (makeNameRows): Enter brings one back // where it was. Its name alone is matched, not the word. The - // store's, which a replica has from the authority. + // session's, which a replica has from the authority. for (pardes.tape.authority(Store.closedNames, .{p})) |c| { var buf: [4096 + 48]u8 = undefined; const row = std.fmt.bufPrint(&buf, config.pane_addr ++ "{d} {s}" ++ closed_word, .{ c.serial, Names.relative(c.name, dir) }) catch continue; @@ -626,8 +626,8 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz /// What a Panes row of a closed pane ends with. const closed_word = " (closed)"; -/// Every window's files, as a Recent query lists them: what the others -/// wrote since, read first. The store's (`tape.authority`): in `out`. +/// Every session's files, as a Recent query lists them: what the others +/// wrote since, read first. The session's (`tape.authority`): in `out`. fn recentRows(p: *Pardes, out: *std.Io.Writer.Allocating) []const u8 { p.store.refresh(p); Output.recentText(p, &out.writer, true) catch {}; diff --git a/src/Store.zig b/src/Store.zig index 547e7bd6..02e5f83f 100644 --- a/src/Store.zig +++ b/src/Store.zig @@ -1,25 +1,21 @@ -//! The store: the state every pardes window shares, one bounded file under -//! $XDG_STATE_HOME/pardes (Options.store_path), an append-only log of -//! framed records, `@<kind> <len> <crc32>\n<body>\n`: the files opened -//! lately (Recent, recent.zig) and the panes each session closed, each a -//! one-pane Dump, which Panes brings back where it was. docs/next-store.md -//! argues the format and the sharing; in short: +//! What outlives a pane: Recent's file, which every session of the user's +//! shares, and the panes this session closed, which Panes brings back. //! -//! - A writer takes `flock` on `store.lock` (never blocking a frame: busy, -//! its records wait for the next step), reads what others appended since -//! it last read, and appends its own with one write at the end. -//! - A reader takes no lock: on use, one stat of the path; longer, it reads -//! the tail; another inode (a compaction), all of it again. -//! - A record running past the end is being written: read next time. One -//! that is garbage is passed over to the next valid one, and the next -//! writer compacts. A missing, foreign or garbage file is an empty store: -//! pardes always starts. -//! - Compaction (under the lock): the live records, the closed panes of -//! sessions still running, into `store.new`, renamed over the store. The -//! file never passes `max_bytes`. +//! Recent's file (Options.recent_path, $XDG_STATE_HOME/pardes/recent) is +//! the list, a line a file, the newest first: `<line> <col> <aline> <acol> +//! <path>` (recent.zig). A session reads it at start, and again on use +//! (Recent, /recent) when a stat says another session wrote it since. A +//! step that changed the list writes it at its end: `flock` on the file +//! (held by another session, the next step tries again: a frame never +//! waits for one), the file read again under this session's changes +//! (`recent.List.merge`) when another session wrote it since, and the +//! whole list written over it in place. A line that is no entry is passed +//! over, so a missing, unreadable or foreign file is an empty list (and a +//! line a crash tore, one entry fewer): pardes always starts. //! -//! With no path (no home, the web, a test that gives none) nothing is -//! kept past the session, and a closed pane's dump is held here instead. +//! The closed panes are this session's alone, and in memory: each a +//! one-pane Dump (dump.savePane) and where it was, the newest 64 within +//! 64 MiB. Restore keeps them (`clone`); the session's end drops them. const Store = @This(); const std = @import("std"); @@ -29,23 +25,16 @@ const layout = @import("layout.zig"); const exec = @import("exec.zig"); const recent = @import("recent.zig"); const Pardes = pardes.Pardes; -const Pane = pardes.Pane; -/// The file is never longer: an append that would pass it compacts first, -/// and a compaction keeps the newest live records within half of it. -pub const max_bytes: u64 = 100 << 20; -/// A closed pane's record is never longer: a terminal's that would be is -/// kept without its history, any other pane's is not kept. -pub const max_record: usize = 8 << 20; -/// A session keeps this many closed panes, the newest. +/// This session keeps this many closed panes, the newest. pub const max_closed = 64; -/// The first record's body, its version; then a generation, new at each -/// compaction, so an inode number used again is no other file read on. -const magic = "pardes-store 1"; -/// A header line `@k <len> <crc>\n` is never longer. -const max_head = 32; -/// A closed pane's first line, its place and name, is never longer. -const max_meta = 4096 + 128; +/// A closed pane's dump is never longer: a terminal's that would be is +/// kept without its history, any other pane's is not kept. +pub const max_dump: usize = 8 << 20; +/// The closed panes' dumps together are never longer: the oldest go. +pub const max_bytes: usize = 64 << 20; +/// Recent's file is read this far at most (200 lines of the longest paths). +const max_file = 1 << 20; /// A pane closed in this session, as Panes lists it and brings it back. pub const Closed = struct { @@ -59,104 +48,66 @@ pub const Closed = struct { /// The pane it was attached to, by serial (0 none). owner: u32, rows: u16, - /// What its Panes row shows: its file, command or directory (gpa). + /// What its Panes row shows: its file, command or directory. name: []u8, - /// Its record's offset in the file read as `inode`. - at: u64 = 0, - /// Its record's body while it is in no file (not written yet, or no - /// file to write it to); empty once written. - body: []u8 = &.{}, - /// Where its record is in `pending`, until that is written. - pending_at: ?usize = null, + /// The pane, as dump.savePane made it. + dump: []u8, }; -/// The store file; null, nothing is kept past the session. +/// Recent's file; null, nothing is kept past the session. path: ?[]const u8 = null, -/// Its bound: `max_bytes`, less in a test. -cap: u64 = max_bytes, -/// This session's, on its closed panes' records. -session: u64 = 0, -pid: i32 = 0, -/// The file as read so far: its inode, generation and how far. -inode: std.Io.File.INode = std.mem.zeroes(std.Io.File.INode), -gen: u64 = 0, -end: u64 = 0, -/// Its length when first read whole: a file past twice that (and 1 MiB) -/// is compacted. -base: u64 = 0, -/// A record that is not one was passed over: the next writer compacts. -garbage: bool = false, -/// Read once at start (or there was none): only then is anything written, -/// so a session never writes over a store it did not read. +/// The file as this session last read or wrote it: a stat that says +/// otherwise is another session's write. +inode: std.Io.File.INode = 0, +size: u64 = 0, +mtime: i96 = 0, +/// Read once at start (or there was none): only then is it written, so a +/// session never writes over a list it did not read. loaded: bool = false, -/// Framed records waiting for the lock. -pending: std.ArrayList(u8) = .empty, -/// This session's closed panes, oldest first. +/// This session's closed panes, oldest first (recentGpa's). closed: std.ArrayList(Closed) = .empty, /// The panes closing now went to another window (Handover.finish): open /// there, they are no closed panes of this session, so none is kept. away: bool = false, -fn clockNs() i128 { - var ts: std.c.timespec = .{ .sec = 0, .nsec = 0 }; - _ = std.c.clock_gettime(.REALTIME, &ts); - return @as(i128, ts.sec) * std.time.ns_per_s + ts.nsec; -} - fn ioOf() std.Io { return std.Io.Threaded.global_single_threaded.io(); } /// One host file call, `f(args)`, made out of the turn (pardes.turn): a -/// store under a mount of this session's own tree is answered by this +/// file under a mount of this session's own tree is answered by this /// session. Only the call: while a step is out nothing may change the core, -/// so the store and Recent change between these, never inside one. +/// so Recent changes between these, never inside one. const host = pardes.tape.out; const cwd = std.Io.Dir.cwd; const File = std.Io.File; const Dir = std.Io.Dir; -/// A new session's store at `path`: its id from the pid and the clock. -pub fn init(path: ?[]const u8) Store { - var s: Store = .{ .path = path }; - if (comptime pardes.hosted) { - s.pid = std.c.getpid(); - const now = clockNs(); - s.session = std.hash.Wyhash.hash(@intCast(s.pid), std.mem.asBytes(&now)); - } else s.session = 1; - return s; -} - pub fn deinit(s: *Store, gpa: std.mem.Allocator) void { for (s.closed.items) |c| freeClosed(gpa, c); s.closed.deinit(gpa); - s.pending.deinit(gpa); } fn freeClosed(gpa: std.mem.Allocator, c: Closed) void { gpa.free(c.name); - gpa.free(c.body); + gpa.free(c.dump); } /// A copy for the session a Restore makes: the same session (the same -/// process), its closed panes still its own. What is pending is the old -/// session's to write (its deinit does); the copies hold their bodies -/// until they are read back from the file. +/// process), its closed panes still its own. pub fn clone(s: *const Store, gpa: std.mem.Allocator) Store { var copy = s.*; - copy.pending = .empty; copy.closed = .empty; for (s.closed.items) |c| { const name = gpa.dupe(u8, c.name) catch break; - const body = gpa.dupe(u8, c.body) catch { + const body = gpa.dupe(u8, c.dump) catch { gpa.free(name); break; }; var kept = c; kept.name = name; - kept.body = body; - kept.pending_at = null; + kept.dump = body; copy.closed.append(gpa, kept) catch { freeClosed(gpa, kept); break; @@ -165,363 +116,141 @@ pub fn clone(s: *const Store, gpa: std.mem.Allocator) Store { return copy; } -// ---- reading ---- - -/// A window of the file, read ahead: records are parsed from it, and a -/// body that is skipped is never read. -const Reader = struct { - file: std.Io.File, - io: std.Io, - size: u64, - buf: []u8, - at: u64 = 0, - len: usize = 0, - - /// Bytes `[from, from + n)`, `n` at most the window; null past the end. - fn bytes(r: *Reader, from: u64, n: usize) ?[]const u8 { - if (n > r.buf.len or from + n > r.size) return null; - if (from < r.at or from + n > r.at + r.len) { - r.len = host(File.readPositionalAll, .{ r.file, r.io, r.buf, from }) catch return null; - r.at = from; - if (n > r.len) return null; - } - const o: usize = @intCast(from - r.at); - return r.buf[o..][0..n]; - } -}; - -const Rec = struct { - kind: u8, - at: u64, - body: u64, - len: usize, - crc: u32, - - fn end(rec: Rec) u64 { - return rec.body + rec.len + 1; - } -}; - -const Next = union(enum) { rec: Rec, partial, bad }; - -/// The record at `at`: whole, still being written (`partial`: its length -/// runs past the end), or not a record. -fn next(r: *Reader, at: u64) Next { - if (at >= r.size) return .partial; - const head = r.bytes(at, @intCast(@min(max_head, r.size - at))) orelse return .partial; - const nl = std.mem.indexOfScalar(u8, head, '\n') orelse return if (head.len < max_head) .partial else .bad; - const line = head[0..nl]; - if (line.len < 5 or line[0] != '@' or line[2] != ' ') return .bad; - var fields = std.mem.splitScalar(u8, line[3..], ' '); - const len = std.fmt.parseInt(usize, fields.next() orelse return .bad, 10) catch return .bad; - const crc_text = fields.next() orelse return .bad; - if (crc_text.len != 8 or fields.next() != null or len > max_record + max_meta) return .bad; - const crc = std.fmt.parseInt(u32, crc_text, 16) catch return .bad; - const rec: Rec = .{ .kind = line[1], .at = at, .body = at + nl + 1, .len = len, .crc = crc }; - if (rec.end() > r.size) return .partial; - const tail = r.bytes(rec.end() - 1, 1) orelse return .partial; - if (tail[0] != '\n') return .bad; - return .{ .rec = rec }; -} - -/// Whether `rec`'s body is the one its CRC says. -fn verify(r: *Reader, rec: Rec) bool { - var crc = std.hash.Crc32.init(); - var at = rec.body; - var left = rec.len; - while (left > 0) { - const n = @min(left, r.buf.len); - crc.update(r.bytes(at, n) orelse return false); - at += n; - left -= n; - } - return crc.final() == rec.crc; -} - -/// After garbage: the next `\n@` that starts a whole, valid record (or one -/// still being written), else the end. -fn resync(r: *Reader, from: u64) u64 { - var at = from; - while (at + 1 < r.size) : (at += 1) { - const b = r.bytes(at, 2) orelse return r.size; - if (b[0] != '\n' or b[1] != '@') continue; - switch (next(r, at + 1)) { - .rec => |rec| if (verify(r, rec)) return at + 1, - .partial => return at + 1, - .bad => {}, - } - } - return r.size; -} +// ---- Recent's file ---- -/// A closed pane's first line: `<session> <pid> <serial> <column> -/// <gainer> <below> <owner> <rows>\t<name>`. -const Meta = struct { - session: u64, - pid: i32, - serial: u32, - column: u32, - gainer: u32, - below: bool, - owner: u32, - rows: u16, - name: []const u8, - /// Where its dump starts in the body. - dump_at: usize, -}; - -fn parseMeta(body: []const u8) ?Meta { - const nl = std.mem.indexOfScalar(u8, body, '\n') orelse return null; - const tab = std.mem.indexOfScalar(u8, body[0..nl], '\t') orelse return null; - var f = std.mem.splitScalar(u8, body[0..tab], ' '); - var m: Meta = undefined; - m.session = std.fmt.parseInt(u64, f.next() orelse return null, 16) catch return null; - m.pid = std.fmt.parseInt(i32, f.next() orelse return null, 10) catch return null; - m.serial = std.fmt.parseInt(u32, f.next() orelse return null, 10) catch return null; - m.column = std.fmt.parseInt(u32, f.next() orelse return null, 10) catch return null; - m.gainer = std.fmt.parseInt(u32, f.next() orelse return null, 10) catch return null; - m.below = (std.fmt.parseInt(u1, f.next() orelse return null, 10) catch return null) == 1; - m.owner = std.fmt.parseInt(u32, f.next() orelse return null, 10) catch return null; - m.rows = std.fmt.parseInt(u16, f.next() orelse return null, 10) catch return null; - if (f.next() != null) return null; - m.name = body[tab + 1 .. nl]; - m.dump_at = nl + 1; - return m; -} - -/// An `x` record: a session's closed pane brought back, `<session> <serial>`. -const Reopened = struct { session: u64, serial: u32 }; - -fn parseReopened(b: []const u8) ?Reopened { - const sp = std.mem.indexOfScalar(u8, b, ' ') orelse return null; - return .{ - .session = std.fmt.parseInt(u64, b[0..sp], 16) catch return null, - .serial = std.fmt.parseInt(u32, b[sp + 1 ..], 10) catch return null, - }; -} - -/// The store read at start. +/// Recent read at start: the file, and the store it replaced. pub fn load(s: *Store, p: *Pardes) void { const path = s.path orelse return; if (comptime !pardes.hosted) return; - defer s.loaded = true; + s.loaded = true; + s.refresh(p); + // ponytail: the store this file replaced (until 2026-11-05), a log + // whose `@r` records' bodies are this file's lines, oldest first: read + // once more, over the file's list, while it is the newer of the two + // (an older pardes still running writes it, so it is left there). const io = ioOf(); - const file = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch return; - defer host(File.close, .{ file, io }); - const st = host(File.stat, .{ file, io }) catch return; - s.readFile(p, file, st); + var old_buf: [4096 + 8]u8 = undefined; + const old = std.fmt.bufPrint(&old_buf, "{s}/store", .{std.fs.path.dirname(path) orelse return}) catch return; + const st = host(Dir.statFile, .{ cwd(), io, old, Dir.StatFileOptions{} }) catch return; + if (st.mtime.nanoseconds <= s.mtime) return; + const gpa = p.recentGpa(); + const bytes = host(Dir.readFileAlloc, .{ cwd(), io, old, gpa, std.Io.Limit.limited(128 << 20) }) catch return; + defer gpa.free(bytes); + var lines = std.mem.splitScalar(u8, bytes, '\n'); + while (lines.next()) |line| { + const e = recent.List.parseLine(line) orelse continue; + const open = if (p.recent.get(e.path)) |here| here.open else false; + p.recent.opened(gpa, e.path); + p.recent.closed(gpa, e.path, e.nums[0], e.nums[1], e.nums[2], e.nums[3]); + // Open here, it stays open: the cap never drops it. + if (open) p.recent.opened(gpa, e.path); + } } -/// On use: what other windows wrote since this one last read, read. One -/// stat when nothing changed. +/// On use: what other sessions wrote since this one last read or wrote +/// the file, read. One stat when nothing changed. pub fn refresh(s: *Store, p: *Pardes) void { const path = s.path orelse return; if (comptime !pardes.hosted) return; if (!s.loaded) return; const io = ioOf(); const st = host(Dir.statFile, .{ cwd(), io, path, Dir.StatFileOptions{} }) catch return; - if (st.inode == s.inode and st.size == s.end) return; + if (st.inode == s.inode and st.size == s.size and st.mtime.nanoseconds == s.mtime) return; const file = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch return; defer host(File.close, .{ file, io }); - const fst = host(File.stat, .{ file, io }) catch return; - s.readFile(p, file, fst); + s.read(p, file); } -/// `file` read on from where this session left it, or all of it again -/// when it is another file than the one read so far. -fn readFile(s: *Store, p: *Pardes, file: File, st: File.Stat) void { +/// `file` read into Recent, under this session's changes, when it is not +/// as this session last read or wrote it; then noted as read. +fn read(s: *Store, p: *Pardes, file: File) void { + const io = ioOf(); + const st = host(File.stat, .{ file, io }) catch return; + if (st.inode == s.inode and st.size == s.size and st.mtime.nanoseconds == s.mtime) return; const gpa = p.recentGpa(); - const buf = gpa.alloc(u8, 64 << 10) catch return; + const buf = gpa.alloc(u8, @min(st.size, max_file)) catch return; defer gpa.free(buf); - var r: Reader = .{ .file = file, .io = ioOf(), .size = st.size, .buf = buf }; - var whole = st.inode != s.inode or st.size < s.end; - // The same inode number may be a new file (one compaction freed it, - // the next took it): its header's generation says. - if (!whole and s.end > 0) { - switch (next(&r, 0)) { - .rec => |rec| whole = rec.kind != 'h' or headerGen(&r, rec) != s.gen, - else => whole = true, - } - } - if (whole) { - s.inode = st.inode; - s.end = 0; - s.gen = 0; - s.base = st.size; - s.garbage = false; - // Its closed panes are read again from it, at their offsets there; - // the ones in no file yet stay. - var w: usize = 0; - for (s.closed.items) |c| { - if (c.body.len == 0) { - freeClosed(gpa, c); - continue; - } - s.closed.items[w] = c; - w += 1; - } - s.closed.shrinkRetainingCapacity(w); - } - if (st.size == s.end) return; - s.end = s.scan(p, &r, s.end); -} - -fn headerGen(r: *Reader, rec: Rec) ?u64 { - if (rec.len > 64) return null; - const b = r.bytes(rec.body, rec.len) orelse return null; - if (!std.mem.startsWith(u8, b, magic ++ " ")) return null; - return std.fmt.parseInt(u64, b[magic.len + 1 ..], 16) catch null; + const n = host(File.readPositionalAll, .{ file, io, buf, 0 }) catch return; + p.recent.merge(gpa, buf[0..n]); + s.inode = st.inode; + s.size = st.size; + s.mtime = st.mtime.nanoseconds; } -/// The records from `from` applied; where reading stopped (before a -/// record still being written, or the end). -fn scan(s: *Store, p: *Pardes, r: *Reader, from: u64) u64 { - var at = from; - while (at < r.size) { - switch (next(r, at)) { - .partial => return at, - .bad => { - s.garbage = true; - // A file that does not start as a store is none: empty. - if (at == 0) return r.size; - at = resync(r, at + 1); - }, - .rec => |rec| { - if (at == 0) { - s.gen = (if (rec.kind == 'h') headerGen(r, rec) else null) orelse { - s.garbage = true; - return r.size; - }; - } else if (!s.apply(p, r, rec)) s.garbage = true; - at = rec.end(); - }, - } - } - return at; +/// Recent written out when it changed, at the end of a step and at the +/// end (`wait`: the lock is waited for there, never in a step). +pub fn flush(s: *Store, p: *Pardes, wait: bool) void { + if (!p.recent.dirty) return; + if (s.path) |path| if (comptime pardes.hosted) { + if (!s.loaded) return; + // Another session's lock: the next step tries again. + if (!s.write(p, path, wait)) return; + }; + // Written, or nowhere to write it (or the disk refused it: kept for + // no one, as before). + for (p.recent.entries.items) |*e| e.changed = false; + p.recent.dirty = false; } -/// One record read: noted in Recent, or a closed pane of this session's -/// listed or unlisted. False when it is not what its header says. -fn apply(s: *Store, p: *Pardes, r: *Reader, rec: Rec) bool { +/// Under the file's lock: the file read again when another session wrote +/// it since, this session's changes over it, the whole list written in +/// place. False when another session holds the lock (and `wait` is not +/// set). +fn write(s: *Store, p: *Pardes, path: []const u8, wait: bool) bool { + const io = ioOf(); + const file = host(Dir.createFile, .{ cwd(), io, path, Dir.CreateFileOptions{ .read = true, .truncate = false } }) catch return true; + // Closing it lets go of the lock. + defer host(File.close, .{ file, io }); + if (wait) { + host(File.lock, .{ file, io, File.Lock.exclusive }) catch return true; + } else if (!(host(File.tryLock, .{ file, io, File.Lock.exclusive }) catch return true)) return false; + s.read(p, file); + p.recent.changedFirst(); const gpa = p.recentGpa(); - switch (rec.kind) { - 'r' => { - if (rec.len > 8192) return false; - const b = r.bytes(rec.body, rec.len) orelse return false; - if (std.hash.Crc32.hash(b) != rec.crc) return false; - const e = recent.List.parseLine(b) orelse return false; - p.recent.note(gpa, e.path, e.nums[0], e.nums[1], e.nums[2], e.nums[3]); - }, - 'c' => { - // Only its first line is read: a closed pane is this session's - // or none of its business, and its dump is read when it comes - // back (`reopen`, which checks the CRC). - const b = r.bytes(rec.body, @min(rec.len, max_meta)) orelse return false; - const m = parseMeta(b) orelse return false; - if (m.session != s.session) return true; - s.list(gpa, m, rec.at, &.{}); - }, - 'x' => { - if (rec.len > 64) return false; - const b = r.bytes(rec.body, rec.len) orelse return false; - if (std.hash.Crc32.hash(b) != rec.crc) return false; - const x = parseReopened(b) orelse return false; - if (x.session == s.session) s.unlist(gpa, x.serial); - }, - // A header in the middle is no record. - 'h' => return false, - // A kind a newer pardes writes: passed over. - else => {}, - } + var text: std.Io.Writer.Allocating = .init(gpa); + defer text.deinit(); + const entries = p.recent.entries.items; + for (entries[0..@min(entries.len, recent.cap)]) |e| recent.List.writeEntry(e, &text.writer) catch return true; + // In place: a reader that meets it half written reads it again once + // its stat moves on, and a line a crash tore is passed over. Out of + // disk, it is kept for no one, as before. + host(File.writePositionalAll, .{ file, io, text.written(), 0 }) catch return true; + host(File.setLength, .{ file, io, text.written().len }) catch return true; + // This session's own write is not read back at the next refresh. + const st = host(File.stat, .{ file, io }) catch return true; + s.inode = st.inode; + s.size = st.size; + s.mtime = st.mtime.nanoseconds; return true; } -/// A closed pane listed: from the file (`body` empty, its record at `at`) -/// or not in one yet. In the order closed, the oldest going past -/// `max_closed`. -fn list(s: *Store, gpa: std.mem.Allocator, m: Meta, at: u64, body: []u8) void { - for (s.closed.items) |c| if (c.serial == m.serial) { - gpa.free(body); - return; - }; - const name = gpa.dupe(u8, m.name) catch { - gpa.free(body); - return; - }; - const c: Closed = .{ .serial = m.serial, .column = m.column, .gainer = m.gainer, .below = m.below, .owner = m.owner, .rows = m.rows, .name = name, .at = at, .body = body }; - if (s.closed.items.len >= max_closed) freeClosed(gpa, s.closed.orderedRemove(0)); - // One read from the file goes before those in none yet: they are newer. - var i = s.closed.items.len; - if (body.len == 0) while (i > 0 and s.closed.items[i - 1].body.len > 0) : (i -= 1) {}; - s.closed.insert(gpa, i, c) catch freeClosed(gpa, c); -} - -fn unlist(s: *Store, gpa: std.mem.Allocator, serial: u32) void { - for (s.closed.items, 0..) |c, i| if (c.serial == serial) { - freeClosed(gpa, s.closed.orderedRemove(i)); - return; - }; -} - -/// A closed pane as Panes lists it: its serial and what its row shows. -pub const Named = struct { serial: u32, name: []const u8 }; - -/// This session's closed panes, the latest first (scratch; the names the -/// store's): what a step lists of them (`tape.authority`). -pub fn closedNames(p: *Pardes) []const Named { - const items = p.store.closed.items; - const out = p.scratch.allocator().alloc(Named, items.len) catch return &.{}; - for (out, 0..) |*o, i| { - const c = items[items.len - 1 - i]; - o.* = .{ .serial = c.serial, .name = c.name }; - } - return out; -} - -/// The closed pane with this serial, if this session has it. -pub fn closedBySerial(s: *const Store, serial: u32) ?*Closed { - for (s.closed.items) |*c| if (c.serial == serial) return c; - return null; -} - -// ---- writing ---- - -fn frame(gpa: std.mem.Allocator, out: *std.ArrayList(u8), kind: u8, parts: []const []const u8) !void { - var len: usize = 0; - var crc = std.hash.Crc32.init(); - for (parts) |part| { - len += part.len; - crc.update(part); - } - try out.print(gpa, "@{c} {d} {x:0>8}\n", .{ kind, len, crc.final() }); - for (parts) |part| try out.appendSlice(gpa, part); - try out.append(gpa, '\n'); -} +// ---- the closed panes ---- -/// Pane `id`, about to close, kept as a record: what it was (a one-pane -/// Dump) and where (its column, its rows, what it was attached to). Null -/// for a pane that is no pane of the user's: a query pane, a preview, one -/// carried to another window (`away`), one made and refused in the same -/// step (never announced), or one whose dump fails or is too big. `close` finishes it once its rows have gone. +/// Pane `id`, about to close, kept: what it was (a one-pane Dump) and +/// where (its column, its rows, what it was attached to). Null for a pane +/// that is no pane of the user's: a query pane, a preview, one carried to +/// another window (`away`), one made and refused in the same step (never +/// announced), or one whose dump fails or is too big. `close` finishes it +/// once its rows have gone. pub fn keep(s: *Store, p: *Pardes, id: usize) ?Kept { if (s.away) return null; const pane = p.panes[id] orelse return null; if (pane.fs.unannounced or pane.query != null) return null; if (pane.file) |f| if (f.output) |o| if (o.from == .preview) return null; - // An output pane's record holds its text whole: one past the bound is + // An output pane's dump holds its text whole: one past the bound is // known before a dump of it is made, which for a 256 MiB +Pager held // the session up for seconds only to be thrown away. - if (pane.file) |f| if (f.output != null and f.content.len > max_record) return null; + if (pane.file) |f| if (f.output != null and f.content.len > max_dump) return null; const gpa = p.recentGpa(); var owned = dump.savePane(gpa, pane, false) catch return null; // A terminal's history or a file's undo is what makes it big: kept // without them it comes back a fresh shell in its directory, or its // file as it is on disk. - if (owned.len > max_record) { + if (owned.len > max_dump) { gpa.free(owned); owned = dump.savePane(gpa, pane, true) catch return null; } - if (owned.len > max_record) { + if (owned.len > max_dump) { gpa.free(owned); return null; } @@ -530,7 +259,7 @@ pub fn keep(s: *Store, p: *Pardes, id: usize) ?Kept { gpa.free(owned); return null; }; - // One line, a tab its end: a newline or a tab in it is a space. + // One line: a newline or a tab in it is a space. for (what[0..name.len], 0..) |c, i| name[i] = if (c == '\n' or c == '\t') ' ' else c; const at = layout.findPane(p, id); const owner = layout.ownerOf(p, id); @@ -562,7 +291,8 @@ pub const Kept = struct { }; /// The pane `k` kept is gone, its rows given to `gainer` (a slot, after -/// they moved; null none): listed, and its record framed for the store. +/// they moved; null none): listed, the oldest going past `max_closed` or +/// `max_bytes`. pub fn close(s: *Store, p: *Pardes, k: Kept, gainer: ?usize) void { const gpa = p.recentGpa(); var below = false; @@ -573,229 +303,50 @@ pub fn close(s: *Store, p: *Pardes, k: Kept, gainer: ?usize) void { // and the rows went down. if (layout.findPane(p, g)) |f| below = f.idx < k.idx; }; - var meta_buf: [max_meta]u8 = undefined; - const meta = std.fmt.bufPrint(&meta_buf, "{x:0>16} {d} {d} {d} {d} {d} {d} {d}\t{s}\n", .{ - s.session, s.pid, k.serial, k.column, gainer_serial, @intFromBool(below), k.owner, k.rows, k.name, - }) catch { - k.free(gpa); - return; - }; - const m = parseMeta(meta).?; - const body = std.mem.concat(gpa, u8, &.{ meta, k.dump }) catch { - k.free(gpa); - return; - }; - gpa.free(k.dump); - const pending_at = s.pending.items.len; - const framed = if (s.path != null) blk: { - frame(gpa, &s.pending, 'c', &.{body}) catch break :blk false; - break :blk true; - } else false; - s.list(gpa, m, 0, body); - gpa.free(k.name); - if (framed) if (s.closedBySerial(m.serial)) |c| { - c.pending_at = pending_at; - }; -} - -/// Written out when anything changed, at the end of a step and at the end -/// (`wait`: the lock is waited for there, never in a step). -pub fn flush(s: *Store, p: *Pardes, wait: bool) void { - const gpa = p.recentGpa(); - if (!p.recent.dirty and s.pending.items.len == 0) return; - // No file: nothing to write it to, ever. - if (comptime !pardes.hosted) return s.forget(p); - const path: ?[]const u8 = s.path orelse return s.forget(p); - if (!s.loaded) return; - const io = ioOf(); - var name_buf: [4096 + 16]u8 = undefined; - const lock_path = std.fmt.bufPrint(&name_buf, "{s}.lock", .{path.?}) catch return; - const lock = host(Dir.createFile, .{ cwd(), io, lock_path, Dir.CreateFileOptions{ .truncate = false } }) catch return; - // Closing it lets go of the lock. - defer host(File.close, .{ lock, io }); - if (wait) { - host(File.lock, .{ lock, io, File.Lock.exclusive }) catch return; - } else if (!(host(File.tryLock, .{ lock, io, File.Lock.exclusive }) catch return)) return; - const file = host(Dir.createFile, .{ cwd(), io, path.?, Dir.CreateFileOptions{ .read = true, .truncate = false } }) catch return; - defer host(File.close, .{ file, io }); - const st = host(File.stat, .{ file, io }) catch return; - // Others' records first, so this window's go after them here too. - s.readFile(p, file, st); - // Under the lock, a record still "being written" is one whose writer - // died: garbage. - if (s.end < st.size) s.garbage = true; - p.recent.changedFirst(); - var i = p.recent.entries.items.len; - var line_buf: [4096 + 64]u8 = undefined; - while (i > 0) : (i -= 1) { - const e = &p.recent.entries.items[i - 1]; - if (!e.changed) continue; - e.changed = false; - var w = std.Io.Writer.fixed(&line_buf); - recent.List.writeEntry(e.*, &w) catch continue; - const line = std.mem.trimEnd(u8, w.buffered(), "\n"); - if (line.len > 0) frame(gpa, &s.pending, 'r', &.{line}) catch {}; - } - p.recent.dirty = false; - if (s.garbage or st.size == 0 or st.size + s.pending.items.len > s.cap or st.size > 2 * s.base + (1 << 20)) { - s.compact(p, file, st, path.?); - return; + var bytes = k.dump.len; + for (s.closed.items) |c| bytes += c.dump.len; + while (s.closed.items.len > 0 and (s.closed.items.len >= max_closed or bytes > max_bytes)) { + const gone = s.closed.orderedRemove(0); + bytes -= gone.dump.len; + freeClosed(gpa, gone); } - host(File.writePositionalAll, .{ file, io, s.pending.items, st.size }) catch { - // Out of disk: kept for no one, as the old list was. - s.dropPending(); - return; - }; - for (s.closed.items) |*c| if (c.pending_at) |off| { - c.at = st.size + off; - c.pending_at = null; - gpa.free(c.body); - c.body = &.{}; - }; - s.end = st.size + s.pending.items.len; - s.pending.clearRetainingCapacity(); -} - -fn forget(s: *Store, p: *Pardes) void { - for (p.recent.entries.items) |*e| e.changed = false; - p.recent.dirty = false; - s.dropPending(); + s.closed.append(gpa, .{ .serial = k.serial, .column = k.column, .gainer = gainer_serial, .below = below, .owner = k.owner, .rows = k.rows, .name = k.name, .dump = k.dump }) catch k.free(gpa); } -fn dropPending(s: *Store) void { - s.pending.clearRetainingCapacity(); - for (s.closed.items) |*c| c.pending_at = null; -} - -fn alive(pid: i32) bool { - if (pid <= 0) return false; - const rc = std.c.kill(pid, @enumFromInt(0)); - return rc == 0 or std.c.errno(rc) == .PERM; -} +/// A closed pane as Panes lists it: its serial and what its row shows. +pub const Named = struct { serial: u32, name: []const u8 }; -/// Under the lock: the live records into `<path>.new`, this window's -/// pending ones after them, renamed over the store; then read again whole. -fn compact(s: *Store, p: *Pardes, file: File, st: File.Stat, path: []const u8) void { - const gpa = p.recentGpa(); - const io = ioOf(); - defer s.dropPending(); - const buf = gpa.alloc(u8, 64 << 10) catch return; - defer gpa.free(buf); - var r: Reader = .{ .file = file, .io = io, .size = st.size, .buf = buf }; - var fold: recent.List = .{}; - defer fold.deinit(gpa); - const Live = struct { at: u64, end: u64, session: u64, pid: i32, serial: u32, keep: bool = false }; - var lives: std.ArrayList(Live) = .empty; - defer lives.deinit(gpa); - var gone: std.ArrayList(Reopened) = .empty; - defer gone.deinit(gpa); - var at: u64 = 0; - while (at < r.size) { - switch (next(&r, at)) { - .partial => break, - .bad => at = resync(&r, at + 1), - .rec => |rec| { - at = rec.end(); - if (rec.kind == 'h' or !verify(&r, rec)) continue; - const b = r.bytes(rec.body, @min(rec.len, max_meta)) orelse continue; - switch (rec.kind) { - 'r' => if (recent.List.parseLine(b)) |e| fold.note(gpa, e.path, e.nums[0], e.nums[1], e.nums[2], e.nums[3]), - 'c' => if (parseMeta(b)) |m| lives.append(gpa, .{ .at = rec.at, .end = rec.end(), .session = m.session, .pid = m.pid, .serial = m.serial }) catch {}, - 'x' => if (parseReopened(b)) |x| gone.append(gpa, x) catch {}, - else => {}, - } - }, - } - } - // The closed panes kept: of running sessions, not brought back, the - // newest `max_closed` a session, the newest within half the cap. - var budget: u64 = s.cap / 2 -| s.pending.items.len; - var k = lives.items.len; - while (k > 0) : (k -= 1) { - const l = &lives.items[k - 1]; - const reopened = for (gone.items) |g| { - if (g.session == l.session and g.serial == l.serial) break true; - } else false; - if (reopened or (l.session != s.session and !alive(l.pid))) continue; - var newer: usize = 0; - for (lives.items[k..]) |o| if (o.keep and o.session == l.session) { - newer += 1; - }; - if (newer >= max_closed or l.end - l.at > budget) continue; - budget -= l.end - l.at; - l.keep = true; - } - var name_buf: [4096 + 16]u8 = undefined; - const new_path = std.fmt.bufPrint(&name_buf, "{s}.new", .{path}) catch return; - const out = host(Dir.createFile, .{ cwd(), io, new_path, Dir.CreateFileOptions{} }) catch return; - var ok = false; - defer if (!ok) { - host(File.close, .{ out, io }); - host(Dir.deleteFile, .{ cwd(), io, new_path }) catch {}; - }; - var head: std.ArrayList(u8) = .empty; - defer head.deinit(gpa); - var gen_buf: [64]u8 = undefined; - const now = clockNs(); - const gen = std.hash.Wyhash.hash(s.session, std.mem.asBytes(&now)); - frame(gpa, &head, 'h', &.{std.fmt.bufPrint(&gen_buf, magic ++ " {x}", .{gen}) catch return}) catch return; - var line_buf: [4096 + 64]u8 = undefined; - var i = fold.entries.items.len; - while (i > 0) : (i -= 1) { - var w = std.Io.Writer.fixed(&line_buf); - recent.List.writeEntry(fold.entries.items[i - 1], &w) catch continue; - const line = std.mem.trimEnd(u8, w.buffered(), "\n"); - if (line.len > 0) frame(gpa, &head, 'r', &.{line}) catch return; +/// This session's closed panes, the latest first (scratch; the names the +/// store's): what a step lists of them (`tape.authority`). +pub fn closedNames(p: *Pardes) []const Named { + const items = p.store.closed.items; + const out = p.scratch.allocator().alloc(Named, items.len) catch return &.{}; + for (out, 0..) |*o, i| { + const c = items[items.len - 1 - i]; + o.* = .{ .serial = c.serial, .name = c.name }; } - host(File.writePositionalAll, .{ out, io, head.items, 0 }) catch return; - var off: u64 = head.items.len; - for (lives.items) |l| if (l.keep) { - var from = l.at; - while (from < l.end) { - const n: usize = @intCast(@min(buf.len, l.end - from)); - const b = r.bytes(from, n) orelse return; - host(File.writePositionalAll, .{ out, io, b, off }) catch return; - off += n; - from += n; - } - }; - host(File.writePositionalAll, .{ out, io, s.pending.items, off }) catch return; - host(File.sync, .{ out, io }) catch return; - host(File.close, .{ out, io }); - ok = true; - host(Dir.rename, .{ cwd(), new_path, cwd(), path, io }) catch { - host(Dir.deleteFile, .{ cwd(), io, new_path }) catch {}; - return; - }; - // Every closed pane of this session is in the new file now: read again - // from it, at its offsets there. - for (s.closed.items) |c| freeClosed(gpa, c); - s.closed.clearRetainingCapacity(); - s.inode = 0; - const again = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch return; - defer host(File.close, .{ again, io }); - const nst = host(File.stat, .{ again, io }) catch return; - s.readFile(p, again, nst); + return out; } -// ---- bringing a closed pane back ---- +/// The closed pane with this serial, if this session has it. +pub fn closedBySerial(s: *const Store, serial: u32) ?*Closed { + for (s.closed.items) |*c| if (c.serial == serial) return c; + return null; +} /// Closed pane `serial` of this session made again, where it was: beside /// the pane its rows went to, taking them from that pane alone (attached /// again to its owner when that is still open), else placed as a new pane -/// from `from`. False when it is not this session's, or its record is gone -/// or bad; the keyboard on it otherwise. +/// from `from`. False when it is not this session's, or its dump will not +/// load; the keyboard on it otherwise. pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool { - const gpa = p.recentGpa(); - // The store's: a replica has the authority's (`tape.authority`). - const place = pardes.tape.authority(record, .{ s, p, serial }) orelse return false; - const body = place.body; - const m = parseMeta(body) orelse return false; + // This session's: a replica has the authority's (`tape.authority`). + const place = pardes.tape.authority(record, .{ s, serial }) orelse return false; const free = p.freeSlot() orelse { p.reportError(from, "Panes", error.NoPaneSlots); return false; }; - _ = dump.loadPane(p, free, body[m.dump_at..]) catch |err| { + _ = dump.loadPane(p, free, place.body) catch |err| { p.reportError(from, "Panes", err); return false; }; @@ -814,59 +365,25 @@ pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool { } // No room anywhere: refused, it closes at the step's end, still listed. if (p.unplaced.isSet(free)) return false; - // Brought back: no longer listed, here or when read again. - var rec_buf: [64]u8 = undefined; - const rec = std.fmt.bufPrint(&rec_buf, "{x:0>16} {d}", .{ s.session, serial }) catch unreachable; - if (s.path != null) frame(gpa, &s.pending, 'x', &.{rec}) catch {}; - s.unlist(gpa, serial); + // Brought back: no longer listed. + for (s.closed.items, 0..) |c, i| if (c.serial == serial) { + freeClosed(p.recentGpa(), s.closed.orderedRemove(i)); + break; + }; p.active = free; if (layout.findPane(p, free)) |f| p.setActiveColumn(f.col); p.sync(); return true; } -/// Where a closed pane goes back, and its record's body (scratch). +/// Where a closed pane goes back, and its dump. pub const Record = struct { gainer: u32, below: bool, owner: u32, rows: u16, body: []const u8 }; -/// Closed pane `serial`'s record, the store read on first; null when it is -/// not this session's, or its record is gone or bad. -fn record(s: *Store, p: *Pardes, serial: u32) ?Record { - s.refresh(p); - const c = (s.closedBySerial(serial) orelse return null).*; - const body = s.bodyOf(p, c) orelse return null; - // Read from the file: this call's to free (`unlist` frees one held). - defer if (c.body.len == 0) p.recentGpa().free(body); - const copy = p.scratch.allocator().dupe(u8, body) catch return null; - return .{ .gainer = c.gainer, .below = c.below, .owner = c.owner, .rows = c.rows, .body = copy }; -} - -/// A closed pane's record body: held here, or read from the file and -/// checked against its CRC (gpa; the caller frees it when it is not -/// `c.body`). -fn bodyOf(s: *Store, p: *Pardes, c: Closed) ?[]u8 { - if (c.body.len > 0) return c.body; - const path = s.path orelse return null; - if (comptime !pardes.hosted) return null; - const gpa = p.recentGpa(); - const io = ioOf(); - const file = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch return null; - defer host(File.close, .{ file, io }); - const st = host(File.stat, .{ file, io }) catch return null; - if (st.inode != s.inode) return null; - var head_buf: [max_head]u8 = undefined; - var r: Reader = .{ .file = file, .io = io, .size = st.size, .buf = &head_buf }; - const rec = switch (next(&r, c.at)) { - .rec => |rec| rec, - else => return null, - }; - if (rec.kind != 'c') return null; - const body = gpa.alloc(u8, rec.len) catch return null; - const n = host(File.readPositionalAll, .{ file, io, body, rec.body }) catch 0; - if (n != rec.len or std.hash.Crc32.hash(body) != rec.crc) { - gpa.free(body); - return null; - } - return body; +/// Closed pane `serial`'s record (its dump the store's, which outlives the +/// step); null when it is not this session's. +fn record(s: *Store, serial: u32) ?Record { + const c = s.closedBySerial(serial) orelse return null; + return .{ .gainer = c.gainer, .below = c.below, .owner = c.owner, .rows = c.rows, .body = c.dump }; } /// `@p<serial>` alone, the pane and no place in it: its serial, for a pane @@ -887,15 +404,15 @@ pub fn closedAddress(word: []const u8) ?u32 { const testing = std.testing; -/// The store file of a test, in its own directory (never the user's). +/// Recent's file of a test, in its own directory (never the user's). fn testPath(tmp: *std.testing.TmpDir) ![]u8 { var base_buf: [std.fs.max_path_bytes]u8 = undefined; const dir = base_buf[0..try tmp.dir.realPath(testing.io, &base_buf)]; - return std.fs.path.join(testing.allocator, &.{ dir, "store" }); + return std.fs.path.join(testing.allocator, &.{ dir, "recent" }); } fn testWindow(path: []const u8) !*Pardes { - return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .store_path = path }); + return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .recent_path = path }); } fn fileSize(path: []const u8) u64 { @@ -913,14 +430,13 @@ test "store: two windows share Recent, each reading the other's writes on use; a defer a.deinit(); const b = try testWindow(path); defer b.deinit(); - try testing.expect(a.store.session != b.store.session); a.recent.opened(a.recentGpa(), "/w/a.zig"); a.saveRecent(); b.recent.opened(b.recentGpa(), "/w/b.zig"); b.saveRecent(); a.recent.closed(a.recentGpa(), "/w/a.zig", 7, 3, 0, 0); a.saveRecent(); - // Each has both, in the order of the log: a closed last, then b. + // Each has both, in the order they were written: a closed last, then b. a.store.refresh(a); b.store.refresh(b); for ([_]*Pardes{ a, b }) |w| { @@ -929,10 +445,8 @@ test "store: two windows share Recent, each reading the other's writes on use; a try testing.expectEqualStrings("/w/b.zig", w.recent.entries.items[1].path); try testing.expectEqual(@as(u32, 7), w.recent.get("/w/a.zig").?.line); } - // Another window holding the lock: b's change waits in memory... - const lock_path = try std.fmt.allocPrint(testing.allocator, "{s}.lock", .{path}); - defer testing.allocator.free(lock_path); - const lock = try std.Io.Dir.cwd().createFile(testing.io, lock_path, .{ .truncate = false }); + // Another window holding the file's lock: b's change waits in memory... + const lock = try std.Io.Dir.cwd().openFile(testing.io, path, .{ .mode = .read_write }); try lock.lock(testing.io, .exclusive); b.recent.opened(b.recentGpa(), "/w/c.zig"); const size = fileSize(path); @@ -945,14 +459,14 @@ test "store: two windows share Recent, each reading the other's writes on use; a try testing.expect(fileSize(path) > size); a.store.refresh(a); try testing.expectEqualStrings("/w/c.zig", a.recent.entries.items[0].path); - // A window started now folds the same list. + // A window started now reads the same list. const c = try testWindow(path); defer c.deinit(); try testing.expectEqual(@as(usize, 3), c.recent.entries.items.len); for (a.recent.entries.items, c.recent.entries.items) |x, y| try testing.expectEqualStrings(x.path, y.path); } -test "store: the cap holds, compaction keeps the newest, Recent keeps its 200" { +test "store: Recent's file holds the newest 200, the newest first, and a window started on it reads them so" { if (comptime !pardes.hosted) return; var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); @@ -960,104 +474,95 @@ test "store: the cap holds, compaction keeps the newest, Recent keeps its 200" { defer testing.allocator.free(path); const a = try testWindow(path); defer a.deinit(); - a.store.cap = 48 << 10; var name: [32]u8 = undefined; - var inodes: usize = 0; - var last_inode = a.store.inode; - for (0..3000) |i| { + for (0..recent.cap + 100) |i| { const file = std.fmt.bufPrint(&name, "/w/file{d}.zig", .{i}) catch unreachable; a.recent.opened(a.recentGpa(), file); a.recent.closed(a.recentGpa(), file, 1, 1, 0, 0); a.saveRecent(); - try testing.expect(fileSize(path) <= a.store.cap); - if (a.store.inode != last_inode) inodes += 1; - last_inode = a.store.inode; } - try testing.expect(inodes > 2); try testing.expectEqual(@as(usize, recent.cap), a.recent.entries.items.len); - try testing.expectEqualStrings("/w/file2999.zig", a.recent.entries.items[0].path); + const text = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(1 << 20)); + defer testing.allocator.free(text); + try testing.expectEqual(@as(usize, recent.cap), std.mem.count(u8, text, "\n")); + try testing.expect(std.mem.startsWith(u8, text, "1 1 0 0 /w/file299.zig\n")); const b = try testWindow(path); defer b.deinit(); try testing.expectEqual(@as(usize, recent.cap), b.recent.entries.items.len); - try testing.expectEqualStrings("/w/file2999.zig", b.recent.entries.items[0].path); - try testing.expectEqualStrings("/w/file2800.zig", b.recent.entries.items[recent.cap - 1].path); - // Closed panes' text counts too: the newest stay, within the cap. - a.store.cap = 256 << 10; - var text: [20 << 10]u8 = @splat('t'); - var serial: u32 = 0; - for (0..30) |i| { - a.newScratchBelow(a.active); - const id = a.active; - const f = &a.panes[id].?.file.?; - text[0] = @intCast('a' + i % 26); - @import("File.zig").setContent(a, f, try a.gpa.dupe(u8, &text)); - serial = a.panes[id].?.serial; - // Made outside a step: announced as a step's end would. - pardes.ctlfs.events.announce(a); - try a.removePane(id, null); - a.saveRecent(); - try testing.expect(fileSize(path) <= a.store.cap); - } - try testing.expect(a.store.closed.items.len < 30); - try testing.expectEqual(serial, a.store.closed.items[a.store.closed.items.len - 1].serial); - try testing.expect(a.store.reopen(a, serial, a.active)); - try testing.expectEqual(@as(u8, 'a' + 29 % 26), a.panes[a.active].?.file.?.content[0]); + try testing.expectEqualStrings("/w/file299.zig", b.recent.entries.items[0].path); + try testing.expectEqualStrings("/w/file100.zig", b.recent.entries.items[recent.cap - 1].path); } -test "store: a garbage or torn file never stops pardes, records past garbage are read, the next writer mends it" { +test "store: a foreign file never stops pardes: its lines that are Recent's are read, the next write mends it" { if (comptime !pardes.hosted) return; var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); const path = try testPath(&tmp); defer testing.allocator.free(path); - try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = path, .data = "\x00\xffnot a store at all\n@r 9 zz\n" }); + try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = path, .data = "\x00\xffnot a list at all\n7 3 0 0 /w/ok.zig\n@r 9 zz\n1 2 /w/short.zig\n" }); const a = try testWindow(path); defer a.deinit(); - try testing.expectEqual(@as(usize, 0), a.recent.entries.items.len); - try testing.expect(a.store.garbage); + try testing.expectEqual(@as(usize, 1), a.recent.entries.items.len); + try testing.expectEqual(@as(u32, 7), a.recent.get("/w/ok.zig").?.line); a.recent.opened(a.recentGpa(), "/w/a.zig"); a.saveRecent(); - try testing.expect(!a.store.garbage); - // A torn record at the end (its writer died), then garbage, then a - // whole record: the whole one is read, the rest passed over. - var tail: std.ArrayList(u8) = .empty; - defer tail.deinit(testing.allocator); - try tail.appendSlice(testing.allocator, "@r 40 0000"); - try tail.appendSlice(testing.allocator, "\x00junk\n@x not a header\n"); - try frame(testing.allocator, &tail, 'r', &.{"3 1 0 0 /w/after.zig"}); + const text = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(1 << 20)); + defer testing.allocator.free(text); + try testing.expectEqualStrings("0 0 0 0 /w/a.zig\n7 3 0 0 /w/ok.zig\n", text); +} + +test "store: the store Recent's file replaced is read once, its newest records first, over the file's list" { + if (comptime !pardes.hosted) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + const path = try testPath(&tmp); + defer testing.allocator.free(path); + // An older list, then the store written after it: its records oldest + // first, a closed pane's dump between them. + try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = path, .data = "5 5 0 0 /w/listed.zig\n" }); + const store = try std.fmt.allocPrint(testing.allocator, "{s}/store", .{std.fs.path.dirname(path).?}); + defer testing.allocator.free(store); + try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = store, .data = "@h 31 3c6ce28a\npardes-store 1 ca2a9583be7d1ba3\n@r 20 0\n1 1 0 0 /w/old.zig\n@c 12 0\n0 1 7 0 0 0 0 9\t/w/x\n.{}\n@r 21 0\n2 2 0 0 /w/both.zig\n@r 20 0\n9 9 0 0 /w/old.zig\n" }); { - const f = try std.Io.Dir.cwd().openFile(testing.io, path, .{ .mode = .read_write }); + // Made newer than the list, as on a disk where both were written. + const f = try std.Io.Dir.cwd().openFile(testing.io, store, .{ .mode = .read_write }); defer f.close(testing.io); - try f.writePositionalAll(testing.io, tail.items, (try f.stat(testing.io)).size); + const st = try f.stat(testing.io); + try f.setTimestamps(testing.io, .{ .modify_timestamp = .{ .new = .{ .nanoseconds = st.mtime.nanoseconds + std.time.ns_per_s } } }); } + const a = try testWindow(path); + defer a.deinit(); + try testing.expectEqual(@as(usize, 3), a.recent.entries.items.len); + try testing.expectEqualStrings("/w/old.zig", a.recent.entries.items[0].path); + try testing.expectEqual(@as(u32, 9), a.recent.entries.items[0].line); + try testing.expectEqualStrings("/w/both.zig", a.recent.entries.items[1].path); + try testing.expectEqualStrings("/w/listed.zig", a.recent.entries.items[2].path); + // Written to the file at the step's end; read from it alone after. + a.saveRecent(); const b = try testWindow(path); defer b.deinit(); - try testing.expect(b.recent.get("/w/a.zig") != null); - try testing.expectEqual(@as(u32, 3), b.recent.get("/w/after.zig").?.line); - try testing.expect(b.store.garbage); - // A record half written (the length runs past the end) is waited for, - // not garbage: a reader reads it once it is whole. - var half: std.ArrayList(u8) = .empty; - defer half.deinit(testing.allocator); - try frame(testing.allocator, &half, 'r', &.{"5 1 0 0 /w/late.zig"}); - { - const f = try std.Io.Dir.cwd().openFile(testing.io, path, .{ .mode = .read_write }); - defer f.close(testing.io); - const end = (try f.stat(testing.io)).size; - try f.writePositionalAll(testing.io, half.items[0..10], end); - a.store.refresh(a); - try testing.expect(a.recent.get("/w/late.zig") == null); - try f.writePositionalAll(testing.io, half.items[10..], end + 10); + for (a.recent.entries.items, b.recent.entries.items) |x, y| try testing.expectEqualStrings(x.path, y.path); +} + +test "store: this session keeps its newest 64 closed panes" { + if (comptime !pardes.hosted) return; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48 }); + defer p.deinit(); + var serial: u32 = 0; + var text: [16]u8 = undefined; + for (0..max_closed + 6) |i| { + p.newScratchBelow(p.active); + const id = p.active; + @import("File.zig").setContent(p, &p.panes[id].?.file.?, try p.gpa.dupe(u8, std.fmt.bufPrint(&text, "scratch {d}\n", .{i}) catch unreachable)); + serial = p.panes[id].?.serial; + // Made outside a step: announced as a step's end would. + pardes.ctlfs.events.announce(p); + try p.removePane(id, null); } - a.store.refresh(a); - try testing.expectEqual(@as(u32, 5), a.recent.get("/w/late.zig").?.line); - // The next write compacts it: a reader then finds no garbage. - b.recent.opened(b.recentGpa(), "/w/b.zig"); - b.saveRecent(); - const c = try testWindow(path); - defer c.deinit(); - try testing.expect(!c.store.garbage); - for ([_][]const u8{ "/w/a.zig", "/w/after.zig", "/w/late.zig", "/w/b.zig" }) |want| try testing.expect(c.recent.get(want) != null); + try testing.expectEqual(@as(usize, max_closed), p.store.closed.items.len); + try testing.expectEqual(serial, p.store.closed.items[max_closed - 1].serial); + try testing.expect(p.store.reopen(p, serial, p.active)); + try testing.expectEqualStrings("scratch 69\n", p.panes[p.active].?.file.?.content); } test "store: a closed pane comes back where it was, its rows from the pane that took them, nothing else moving; Panes lists it, marked" { @@ -1084,9 +589,6 @@ test "store: a closed pane comes back where it was, its rows from the pane that try p.removePane(sid, null); layout.compute(p); try testing.expectEqualDeep(before[far], p.rects[far]); - // Written to the store: brought back from the file, not memory. - p.saveRecent(); - try testing.expectEqual(@as(usize, 0), p.store.closedBySerial(serial).?.body.len); try testing.expect(p.store.reopen(p, serial, 0)); const back = p.active; layout.compute(p); @@ -1102,7 +604,6 @@ test "store: a closed pane comes back where it was, its rows from the pane that const again = p.panes[back].?.serial; pardes.ctlfs.events.announce(p); try p.removePane(back, null); - p.saveRecent(); p.active = 0; p.update(.{ .key = .{ .cp = ' ', .text = " " } }); for ("bb") |ch| p.update(.{ .key = .{ .cp = ch, .text = &.{ch} } }); @@ -1115,18 +616,14 @@ test "store: a closed pane comes back where it was, its rows from the pane that } else return error.NotListed; const line = @import("modal.zig").lineSlice(p.panes[qid].?.file.?.content, @intCast(row + 1)); try testing.expect(std.mem.endsWith(u8, line, "(closed)")); - // Enter on it: back, and listed no more (nor in the store when read). + // Enter on it: back, and listed no more. panes_mod.Query.land(p, qid, row); const found = for (p.panes) |slot| { if (slot) |pp| if (pp.file) |f| if (std.mem.eql(u8, f.content, "kept text\n")) break true; } else false; try testing.expect(found); try testing.expect(p.store.closedBySerial(again) == null); - p.saveRecent(); - p.store.inode = 0; - p.store.refresh(p); - try testing.expect(p.store.closedBySerial(again) == null); - // Another session lists none of this one's. + // Another session on the same file lists none of this one's. const w = try testWindow(path); defer w.deinit(); try testing.expectEqual(@as(usize, 0), w.store.closed.items.len); diff --git a/src/dump.zig b/src/dump.zig index 2cff503a..153e4a82 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -499,8 +499,8 @@ fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool /// Pane `pane` alone as a dump (`gpa`'s): what it shares with another /// pane is its own in it (a directory it follows), and nothing says where /// it was. `slim`: without a terminal's history, a file's undo, or a clean -/// file's text, which is read from its file again (a store's record past -/// its bound). +/// file's text, which is read from its file again (a closed pane's dump +/// past its bound, Store.zig). pub fn savePane(gpa: std.mem.Allocator, pane: *pardes.panes.Pane, slim: bool) ![]u8 { // The fields it changes put back after, not the whole pane (88 KB) kept // aside: a store record is made at every close. diff --git a/src/main.zig b/src/main.zig index 3dec406c..a7c0dcc0 100644 --- a/src/main.zig +++ b/src/main.zig @@ -777,7 +777,7 @@ fn nativeMain(std_init: std.process.Init) !void { opts.startup_config = found.bytes; opts.startup_config_path = found.path; opts.config_dir = found.dir; - opts.store_path = storePath(init.io, arena, init.environ_map); + opts.recent_path = recentPath(init.io, arena, init.environ_map); if (found.dir) |d| @import("crash.zig").setDir(d); if (attach) |name| if (std.mem.lastIndexOfScalar(u8, name, ':')) |colon| { remoteAttach(init, opts, name[0..colon], name[colon + 1 ..]); @@ -1136,11 +1136,11 @@ test { if (comptime pardes.font_picker) _ = @import("fonts.zig"); } -/// Where the store every window shares is kept (Store.zig): -/// $XDG_STATE_HOME/pardes/store, else ~/.local/state/pardes/store, the -/// directory made if need be; null with no home to keep it in (Recent and -/// the closed panes then last the session). -fn storePath(io: std.Io, arena: std.mem.Allocator, env: *const std.process.Environ.Map) ?[]const u8 { +/// Where Recent's file, which every session shares, is kept (Store.zig): +/// $XDG_STATE_HOME/pardes/recent, else ~/.local/state/pardes/recent, the +/// directory made if need be; null with no home to keep it in (Recent then +/// lasts the session). +fn recentPath(io: std.Io, arena: std.mem.Allocator, env: *const std.process.Environ.Map) ?[]const u8 { const dir = dir: { if (env.get("XDG_STATE_HOME")) |base| if (base.len != 0 and std.fs.path.isAbsolute(base)) break :dir std.fs.path.join(arena, &.{ base, "pardes" }) catch return null; @@ -1149,5 +1149,5 @@ fn storePath(io: std.Io, arena: std.mem.Allocator, env: *const std.process.Envir break :dir std.fs.path.join(arena, &.{ home, ".local", "state", "pardes" }) catch return null; }; std.Io.Dir.cwd().createDirPath(io, dir) catch return null; - return std.fs.path.join(arena, &.{ dir, "store" }) catch null; + return std.fs.path.join(arena, &.{ dir, "recent" }) catch null; } diff --git a/src/pardes.zig b/src/pardes.zig index e65e48eb..32bf9cba 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -3719,10 +3719,10 @@ pub const Options = struct { ninep_tcp: ?[]const u8 = null, mounts: []const filesystem.Mount = &.{}, config_dir: ?[]const u8 = null, - /// The store every window shares (Store.zig): the files opened lately, - /// the panes closed. Set by the host that has a state directory; none, - /// they last as long as the session. - store_path: ?[]const u8 = null, + /// Recent's file, which every session of the user's shares (Store.zig). + /// Set by the host that has a state directory; none, Recent lasts as + /// long as the session. + recent_path: ?[]const u8 = null, startup_config_path: ?[]const u8 = null, image_allocator: ?std.mem.Allocator = null, pdf_allocator: ?std.mem.Allocator = null, @@ -3800,8 +3800,8 @@ pub const Pardes = struct { /// The files opened lately, closed ones too (recent.zig): `Recent`, /// /recent, and the jumplist's way back to a closed pane. recent: recent_files.List = .{}, - /// Where Recent is kept, shared with every other window, and the panes - /// this session closed, which Panes brings back (Store.zig). + /// Recent's file, shared with every other session, and the panes this + /// session closed, which Panes brings back (Store.zig). store: Store = .{}, /// acme's Incl: the directories a look at a name that is nowhere else /// (`stdio.h`, `<foo.h>`) is tried in, in order. The session's, seeded @@ -4119,9 +4119,10 @@ pub const Pardes = struct { "opts", "dump_out", }, .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 store file - // every window of the user shares (and Recent read from it), and - // the files of a host with no file system of its own. + // The host's handles, the 9P server's fids and log, Recent's file + // every session of the user shares (and Recent read from it) with + // this session's closed panes, and the files of a host with no file + // system of its own. .authority = .{ "host", "fs", "store", "recent", "fallback" }, .len = .{ .{ "jumps", "njumps" }, .{ "look_src", "n_look_src" }, .{ "leader_keys", "leader_n" }, .{ "col_x", "ncol" }, .{ "col_w", "ncol" }, .{ "session_dir", "session_dir_len" } }, }; @@ -4318,11 +4319,11 @@ pub const Pardes = struct { return if (comptime hosted) std.heap.smp_allocator else p.gpa; } - /// The store read once at start: a new session's, Recent from it. + /// Recent's file read once at start: a new session's. pub fn loadRecent(p: *Pardes) void { // A replica's store is its own, in memory: the recording session's - // file is every window's of its user (src/tape.zig). - p.store = Store.init(if (tape.replaying()) null else p.opts.store_path); + // file is every session's of its user (src/tape.zig). + p.store = .{ .path = if (tape.replaying()) null else p.opts.recent_path }; p.store.load(p); } diff --git a/src/recent.zig b/src/recent.zig index b8e275f5..4f744750 100644 --- a/src/recent.zig +++ b/src/recent.zig @@ -1,11 +1,11 @@ //! The files opened lately, most recent first, those since closed too, as //! `Recent` lists them and a jump to a closed pane reopens them: a path //! and where the dot was when it closed. Kept across sessions, and shared -//! by every window, in the store (Store.zig) when the host gives one: each -//! change is a record there, and the records other windows wrote are -//! `note`d here as they are read. acme has nothing like it (its dump and -//! Load are the nearest): a file closed by accident, its path forgotten, -//! is found here again. +//! by every session of the user's, in Recent's file (Store.zig) when the +//! host gives one: written whole at the end of a step that changed it, and +//! read back under this session's changes (`merge`). acme has nothing like +//! it (its dump and Load are the nearest): a file closed by accident, its +//! path forgotten, is found here again. const std = @import("std"); @@ -24,8 +24,9 @@ pub const Entry = struct { /// cap drops. ponytail: a pane renamed away leaves its old path marked /// open until the session ends; kept rather than tracked by pane. open: bool = false, - /// Changed here since the store last heard of it: written out as a - /// record at the end of the step (Store.flush). + /// Changed here since the file was last written: it goes over what the + /// file says when it is read again, and is written at the end of the + /// step (Store.flush). changed: bool = false, }; @@ -90,29 +91,53 @@ pub const List = struct { }; } - /// A record read from the store (another window's, or this one's read - /// again): `path` was opened or closed there last, at that place. It - /// goes first, with that place; whether it is open HERE stays as it - /// was, and nothing is changed for the store to hear again. - pub fn note(l: *List, gpa: std.mem.Allocator, path: []const u8, line: u32, col: u32, aline: u32, acol: u32) void { - if (path.len == 0) return; - var entry: Entry = if (l.find(path)) |i| l.entries.orderedRemove(i) else blk: { - l.dropOldest(gpa); - break :blk .{ .path = gpa.dupe(u8, path) catch return }; - }; - entry.line = line; - entry.col = col; - entry.aline = aline; - entry.acol = acol; - l.entries.insert(gpa, 0, entry) catch { - gpa.free(entry.path); - entry = undefined; - }; + /// The list again from its file's `text`, which other sessions write + /// too: this session's changes not written yet first (they are the + /// newest), then the file's lines in their order, then the files open + /// here that the file has lost (another session's cap dropped them). + /// Whether a file is open HERE stays as it was. A line is matched only + /// against those two, a few: the file holds each path once. + pub fn merge(l: *List, gpa: std.mem.Allocator, text: []const u8) void { + var old = l.entries; + l.entries = .empty; + var opens: std.ArrayList(Entry) = .empty; + defer { + for (opens.items) |e| gpa.free(e.path); + opens.deinit(gpa); + } + for (old.items) |e| { + if (e.changed) { + l.entries.append(gpa, e) catch gpa.free(e.path); + } else if (e.open) { + opens.append(gpa, e) catch gpa.free(e.path); + } else gpa.free(e.path); + } + old.deinit(gpa); + const changed = l.entries.items.len; + var lines = std.mem.splitScalar(u8, text, '\n'); + next: while (lines.next()) |line| { + if (l.entries.items.len >= cap) break; + const e = parseLine(line) orelse continue; + for (l.entries.items[0..changed]) |c| if (std.mem.eql(u8, c.path, e.path)) continue :next; + const at = for (opens.items, 0..) |o, k| { + if (std.mem.eql(u8, o.path, e.path)) break k; + } else null; + var entry: Entry = if (at) |k| opens.orderedRemove(k) else .{ .path = gpa.dupe(u8, e.path) catch break }; + entry.line = e.nums[0]; + entry.col = e.nums[1]; + entry.aline = e.nums[2]; + entry.acol = e.nums[3]; + l.entries.append(gpa, entry) catch { + gpa.free(entry.path); + break; + }; + } + l.entries.appendSlice(gpa, opens.items) catch return; + opens.clearRetainingCapacity(); } - /// This window's changes first again, in their order, after others' - /// records were noted over them: they are written after those, so - /// that is where every window's fold puts them. + /// This session's changes first, in their order: written so, they are + /// where a read of the file puts them too (`merge`), above the rest. pub fn changedFirst(l: *List) void { var w: usize = 0; for (0..l.entries.items.len) |i| if (l.entries.items[i].changed) { @@ -144,14 +169,14 @@ pub const List = struct { return l.entries.items[l.find(path) orelse return null]; } - /// A store record's body, `<line> <col> <aline> <acol> <path>`, a line. + /// An entry as a line of the file, `<line> <col> <aline> <acol> <path>`. pub fn writeEntry(e: Entry, w: *std.Io.Writer) !void { // A name with a newline in it would read back as two lines: not kept. if (std.mem.indexOfScalar(u8, e.path, '\n') == null) try w.print("{d} {d} {d} {d} {s}\n", .{ e.line, e.col, e.aline, e.acol, e.path }); } - /// One record's body read back: `<line> <col> <aline> <acol> <path>`. + /// A line of the file read back: `<line> <col> <aline> <acol> <path>`. pub fn parseLine(line: []const u8) ?struct { nums: [4]u32, path: []const u8 } { var nums: [4]u32 = @splat(0); var rest = line; @@ -180,7 +205,7 @@ test "the list keeps a path once, most recent first, its dot from closing, cappe var w = std.Io.Writer.fixed(&buf); for (l.entries.items) |e| try List.writeEntry(e, &w); try std.testing.expectEqualStrings("7 3 0 0 /a\n0 0 0 0 /b\n", w.buffered()); - // Each line a store record's body, read back. + // Each a line of the file, read back. const back = List.parseLine("7 3 0 0 /a").?; try std.testing.expectEqual(@as(u32, 7), back.nums[0]); try std.testing.expectEqualStrings("/a", back.path); @@ -210,3 +235,30 @@ test "the cap drops the oldest closed file, never an open one" { try std.testing.expect(l.get("/open") != null); try std.testing.expect(l.get("/shut") == null); } + +test "a merge puts this session's changes over the file's list, keeps what is open here, and drops what the file lost" { + const gpa = std.testing.allocator; + var l: List = .{}; + defer l.deinit(gpa); + l.opened(gpa, "/mine"); + l.opened(gpa, "/kept"); + l.closed(gpa, "/kept", 4, 2, 0, 0); + l.opened(gpa, "/gone"); + l.closed(gpa, "/gone", 1, 1, 0, 0); + // Written: nothing changed since. + for (l.entries.items) |*e| e.changed = false; + // Another session closed /kept at 9:1 and opened /theirs; its cap lost + // /gone and /mine, which is open here. + l.closed(gpa, "/new", 3, 3, 0, 0); + l.merge(gpa, "0 0 0 0 /theirs\n9 1 0 0 /kept\nnot a line\n"); + var order: [4][]const u8 = undefined; + for (l.entries.items, 0..) |e, i| order[i] = e.path; + try std.testing.expectEqual(@as(usize, 4), l.entries.items.len); + try std.testing.expectEqualStrings("/new", order[0]); + try std.testing.expectEqualStrings("/theirs", order[1]); + try std.testing.expectEqualStrings("/kept", order[2]); + try std.testing.expectEqualStrings("/mine", order[3]); + try std.testing.expectEqual(@as(u32, 9), l.get("/kept").?.line); + try std.testing.expect(l.get("/mine").?.open); + try std.testing.expect(!l.get("/theirs").?.open); +} diff --git a/src/replica.zig b/src/replica.zig index fa309648..70b7c4bc 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -668,8 +668,8 @@ pub const Replica = struct { o.tree_sitter_allocator = epoch; o.image_allocator = o.image_allocator orelse std.heap.c_allocator; o.pdf_allocator = o.pdf_allocator orelse std.heap.c_allocator; - // The session's store and Recent are the authority's. - o.store_path = null; + // The session's closed panes and Recent are the authority's. + o.recent_path = null; // Its emulators off the epoch, as `terminal.create` makes a screen // replica's: deltas write them while a copy is held. pardes.dump.terminal_gpa = terminal.emulator_gpa; diff --git a/src/tape.zig b/src/tape.zig index ced8b9bf..0e8bbfe6 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -844,8 +844,8 @@ pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@Ty } /// A read of what the authority alone holds (`.authority` state a step -/// makes digested state from: the store's closed panes and Recent, which -/// every window of its user writes; the 9P server's readers and socket), +/// 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 diff --git a/test/process/look_and_exec.zig b/test/process/look_and_exec.zig index 5a42b844..5ad5e25b 100644 --- a/test/process/look_and_exec.zig +++ b/test/process/look_and_exec.zig @@ -230,7 +230,7 @@ fn canary(gpa: std.mem.Allocator, exe: [:0]const u8) !void { try check(has(try restored.read(a, "/recent"), try std.fmt.allocPrint(a, "open {s}/after.txt\n", .{root})), "no open in Recent", .{}); { const deadline = process.nowMs() + 10_000; - while (!has(d.get(a, ".local/state/pardes/store") catch "", d.abs("after.txt"))) { + while (!has(d.get(a, ".local/state/pardes/recent") catch "", d.abs("after.txt"))) { if (process.nowMs() > deadline) return error.RecentNotKept; process.nap(50); } diff --git a/test/process/other_files.zig b/test/process/other_files.zig index ad58dd10..17cc198c 100644 --- a/test/process/other_files.zig +++ b/test/process/other_files.zig @@ -7,7 +7,7 @@ pub const cases = .{ .{ "other-files: kbdin and mousein drive a detached session as the user would, each write done before it is answered", keysAndMouse }, .{ "other-files: a held connection lists a directory, stats a file, writes and reads, is refused in words and removes a pane", heldConnection }, .{ "other-files: a file opened and closed is in /recent closed, and still is in the next session; Recent's row opens it again", recentSurvivesRestart }, - .{ "other-files: two sessions share one store: a file opened in one is in the other's /recent, a Del'd pane comes back from @p only where it was", storeShared }, + .{ "other-files: two sessions share Recent: a file opened in one is in the other's /recent, a Del'd pane comes back from @p only where it was", recentShared }, }; extern "c" fn system(cmd: [*:0]const u8) c_int; @@ -28,9 +28,9 @@ fn removeRoot(root: []const u8) void { _ = system(std.fmt.bufPrintSentinel(&cmd, "rm -rf -- '{s}'", .{root}, 0) catch return); } -fn storeExists(root: []const u8) bool { +fn recentExists(root: []const u8) bool { var buf: [4200]u8 = undefined; - return std.c.access(std.fmt.bufPrintSentinel(&buf, "{s}/.local/state/pardes/store", .{root}, 0) catch return false, 0) == 0; + return std.c.access(std.fmt.bufPrintSentinel(&buf, "{s}/.local/state/pardes/recent", .{root}, 0) catch return false, 0) == 0; } /// A script types and clicks as the user does: no sleep or poll between a @@ -111,7 +111,7 @@ fn recentSurvivesRestart(gpa: std.mem.Allocator, exe: [:0]const u8) !void { try c.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{serial}), "Del\n"); try std.testing.expect(has(try c.read(a, "/recent"), try std.fmt.allocPrint(a, "closed {s}\n", .{kept}))); } - try std.testing.expect(storeExists(root)); + try std.testing.expect(recentExists(root)); var two = try process.Detached.start(gpa, exe, "recent2", .{ .root = root }); defer two.deinit(); const c = try process.Conn.dial(gpa, two.socket()); @@ -139,10 +139,10 @@ fn delTwice(c: *process.Conn, ctl: []const u8) !void { c.write(ctl, "Del\n") catch try c.write(ctl, "Del\n"); } -/// The store (Store.zig) shared with no restart; a pane Del'd in one +/// Recent's file (Store.zig) shared with no restart; a pane Del'd in one /// comes back where it was from a look at its @p serial there, its text /// with it, and only there. -fn storeShared(gpa: std.mem.Allocator, exe: [:0]const u8) !void { +fn recentShared(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); const a = arena.allocator(); @@ -181,5 +181,5 @@ fn storeShared(gpa: std.mem.Allocator, exe: [:0]const u8) !void { try std.testing.expect(!has(body, "kept across a Del")); } } - try std.testing.expect(storeExists(root)); + try std.testing.expect(recentExists(root)); } |
