From a249379bf8878a7c8cabdf6d40eb033c7b096495 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Thu, 10 Sep 2026 13:57:25 +0800 Subject: [PATCH] fix(machine): format the three items and stop reading the tree per persist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `rustfmt` is a required check and was red on three hunks of this branch: `owner_of`'s signature, the `--cwd` argv in `tab_new`'s adopt test, and the two `tab_create` calls in `the_document_being_replaced_is_kept_beside_it`. `keep_a_generation` also read the whole document before asking whether it was going to keep anything. It runs on every persist — pane facts alone flush every couple of seconds — and answers "too soon" on almost all of them, so that was a full read of `machine.json` per write to produce one copy every five minutes. The spacing check moves ahead of the read; the `NotFound` arm still covers the machine that has never written a tree. And a line continuation was missing from an assertion message in `a_typed_name_waits_for_the_create_rather_than_racing_it`, so the failure would have printed eighteen spaces mid-sentence. Claude-Session: https://claude.ai/code/session_01JRqYZ9E153WpSHGS2AW3BM --- crates/tty7-cli/src/commands.rs | 9 +++++++-- crates/tty7-core/src/core/machine.rs | 23 +++++++++++++++-------- src/ui/tree_sync.rs | 3 ++- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/crates/tty7-cli/src/commands.rs b/crates/tty7-cli/src/commands.rs index 10b88fc3..ce9b8d04 100644 --- a/crates/tty7-cli/src/commands.rs +++ b/crates/tty7-cli/src/commands.rs @@ -772,7 +772,10 @@ fn adopt_pane( } /// The workspace a pane was spawned for, if it is still on this machine. -fn owner_of(info: &tty7_core::daemon::protocol::PaneInfo, machine: &Machine) -> Option { +fn owner_of( + info: &tty7_core::daemon::protocol::PaneInfo, + machine: &Machine, +) -> Option { let owner = info.owner.as_deref()?; machine .workspaces @@ -2201,7 +2204,9 @@ mod tests { .push_back(ReplyOk::TabTree(Box::new(Tab::leaf(37)))); run_cli( - &["tty7", "tab", "new", "web", "--pane", "%37", "--cwd", "C:\\else"], + &[ + "tty7", "tab", "new", "web", "--pane", "%37", "--cwd", "C:\\else", + ], &Context::default(), &mut backend, ); diff --git a/crates/tty7-core/src/core/machine.rs b/crates/tty7-core/src/core/machine.rs index 5479709c..2a0fdb29 100644 --- a/crates/tty7-core/src/core/machine.rs +++ b/crates/tty7-core/src/core/machine.rs @@ -1304,12 +1304,9 @@ fn backup_path(path: &Path, generation: usize) -> PathBuf { /// Returns `Ok(())` when there was nothing to do: no tree yet, or the newest /// generation is younger than `spacing`. fn keep_a_generation(path: &Path, spacing: Duration) -> io::Result<()> { - let bytes = match std::fs::read(path) { - Ok(bytes) => bytes, - // Nothing has been written yet, so there is no previous generation. - Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(()), - Err(e) => return Err(e), - }; + // Asked before the tree is read: this runs on every persist and answers + // "nothing to do" on almost all of them, so reading the whole document + // first would be a full read per write for one copy every five minutes. let newest = backup_path(path, 0); let too_soon = std::fs::metadata(&newest) .and_then(|meta| meta.modified()) @@ -1318,6 +1315,12 @@ fn keep_a_generation(path: &Path, spacing: Duration) -> io::Result<()> { if too_soon { return Ok(()); } + let bytes = match std::fs::read(path) { + Ok(bytes) => bytes, + // Nothing has been written yet, so there is no previous generation. + Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(()), + Err(e) => return Err(e), + }; // Oldest first, so nothing is overwritten before it has been moved down. // A generation that is not there yet simply has nothing to move. for generation in (1..BACKUP_GENERATIONS).rev() { @@ -2701,8 +2704,12 @@ mod tests { let store = MachineStore::open(&path); let ws = store.workspace_create(None, None, None).unwrap(); - store.tab_create(ws.id, None, seed(1, "/work"), None, None).unwrap(); - store.tab_create(ws.id, None, seed(2, "/work"), None, None).unwrap(); + store + .tab_create(ws.id, None, seed(1, "/work"), None, None) + .unwrap(); + store + .tab_create(ws.id, None, seed(2, "/work"), None, None) + .unwrap(); let kept = backup_path(&path, 0); assert!( diff --git a/src/ui/tree_sync.rs b/src/ui/tree_sync.rs index 524e1b60..be7f0ef3 100644 --- a/src/ui/tree_sync.rs +++ b/src/ui/tree_sync.rs @@ -2909,7 +2909,8 @@ mod tests { assert_eq!(parked.name, "deploy"); assert_eq!( parked.workspace, ws, - "and it is parked against the workspace it was typed for, so a window that walks into a different one cannot spend it (#716)" + "and it is parked against the workspace it was typed for, so a window \ + that walks into a different one cannot spend it (#716)" ); }); }