From 280cb057399374dac9d5568bb1a1d648264a7cfe Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Tue, 27 Oct 2026 12:45:13 +0100 Subject: [PATCH 1/2] fix(lanes): a lane on a node keeps a whole node_modules while new inputs are prepared (#305) When the lockfile changed in the worktree of a lane on a node, `prepare` ran `npm ci` in the copy the worktree's junction points to. `npm ci` empties node_modules first; when it then failed (a locked file, out of memory) the worktree was left with a few packages and nothing in the log. The lane now has two copies (`linked/src`, `linked/next/src`): new inputs are prepared in the one the worktree is not linked to, its mark removed first, and the link moves once it is whole. A failure keeps the copy it had (`linked (stale)`, with why). A failed prepare is logged as `prepare_failed`. ADR-0063 addendum. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 + crates/devstack-daemon/src/supervisor/copy.rs | 232 ++++++++++++++---- ...worktree-to-a-copy-from-the-local-store.md | 19 +- docs/adr/README.md | 2 +- docs/dashboard-api.md | 4 +- docs/stack-file.md | 6 +- ui/src/api/types.ts | 2 +- ui/src/components/StackHeader.vue | 2 +- 8 files changed, 216 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66d360fc..6b1fbdfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,14 @@ and listed with their assets on the ### Fixed +- **A lane on a node keeps a whole `node_modules` while new inputs are + prepared** (#305, ADR-0063 addendum): when the lockfile changed in its + worktree, `prepare` ran `npm ci` in the very copy the worktree was linked + to, which empties `node_modules` first; a `prepare` that then failed left + the worktree with a few packages and nothing in the log. The new inputs + are prepared in a second copy and the link moves once it is whole; a + failure keeps the copy it had (`linked (stale)`, with why) and is logged + (`prepare_failed`). - **A measured CPU need no longer locks a repository out of the pool** (ADR-0066): a build takes every core that is free, so a lane's measured CPU peak rose with each run on an idler node until it was more than any diff --git a/crates/devstack-daemon/src/supervisor/copy.rs b/crates/devstack-daemon/src/supervisor/copy.rs index a79171fd..741b040f 100644 --- a/crates/devstack-daemon/src/supervisor/copy.rs +++ b/crates/devstack-daemon/src/supervisor/copy.rs @@ -401,6 +401,11 @@ impl Supervisor { std::thread::sleep(Duration::from_millis(100)); }; if code != 0 { + crate::diag::warn( + "prepare_failed", + serde_json::json!({ "lane": self.lane, "service": name, "run": job.run, + "exit_code": code, "cwd": job.cwd, "log": log }), + ); return Err(format!( "prepare `{}` exited with code {code}\n{}", job.run, @@ -495,14 +500,20 @@ impl Supervisor { /// The worktree of a lane that runs on a node, linked to a copy on this /// machine of what each `prepare` with `link` makes, for its git hooks - /// (ADR-0063): `/linked/src`, `dir` the lane's state here. The - /// copy comes from this machine's store (ADR-0062) when it has the key - /// of the worktree's inputs; else `prepare` runs once in that copy, - /// which gets the worktree's source for it, and its outputs go into the - /// store. `stack` is the worktree's stack file (the main tree's where it - /// has none). `report` hears every path's state as it changes. Nothing - /// runs when the key is the one prepared there before and its - /// directories are there: a look at the inputs. + /// (ADR-0063): `/linked/src` or `/linked/next/src`, `dir` the + /// lane's state here. The copy comes from this machine's store + /// (ADR-0062) when it has the key of the worktree's inputs; else + /// `prepare` runs once in that copy, which gets the worktree's source + /// for it, and its outputs go into the store. `stack` is the worktree's + /// stack file (the main tree's where it has none). `report` hears every + /// path's state as it changes. Nothing runs when the key is the one + /// prepared there before and its directories are there: a look at the + /// inputs. + /// + /// New inputs are prepared in the copy the link does not point to, and + /// the link moves once that succeeded (ADR-0063 addendum 2026-10-27): + /// while it runs, and when it fails, the worktree keeps the last copy + /// that was whole (`linked (stale)`), never one `npm ci` is emptying. pub(crate) fn link_node_lane( &self, worktree: &Path, @@ -512,9 +523,10 @@ impl Supervisor { cancel: &dyn Fn() -> bool, ) { let linked = dir.join("linked"); + let copies = node_copies(&linked); let mut views: Vec = Vec::new(); - let snap = match Snapshot::open(worktree, &linked) { - Ok(s) => s, + let files = match Snapshot::open(worktree, &copies[0]).and_then(|s| s.list()) { + Ok(f) => f, Err(e) => { crate::diag::warn( "prepare_node_link_failed", @@ -523,9 +535,7 @@ impl Supervisor { return; } }; - let root = snap.dir.clone(); - let files = snap.list().unwrap_or_default(); - let snap = Arc::new(std::sync::Mutex::new(snap)); + let roots = copies.clone().map(|c| c.join("src")); for (name, svc) in &stack.services { let Some(p) = svc.prepare.as_ref().filter(|p| !p.link.is_empty()) else { continue; @@ -566,45 +576,88 @@ impl Supervisor { continue; } }; - let mark = linked.join("prepared").join(name); - let there = p.link.iter().all(|l| root.join(l).is_dir()); - let done = there && std::fs::read_to_string(&mark).is_ok_and(|k| k == key); + // The copy the worktree is linked to now, and those made for + // this key already. + let current = (0..2).find(|&i| { + p.link + .iter() + .any(|rel| links_to(&worktree.join(rel), &roots[i].join(rel))) + }); + let ready = |i: usize| { + p.link.iter().all(|l| roots[i].join(l).is_dir()) + && std::fs::read_to_string(copies[i].join("prepared").join(name)) + .is_ok_and(|k| k == key) + }; let mut state = "linked"; - if !done { - let mut now = views.clone(); - now.extend(at("preparing", None)); - report(&now); - let cwd = p.cwd.as_ref().or(svc.cwd.as_ref()); - let sync = snap.clone(); - let job = PrepareJob { - key, - mark, - cwd: cwd.map_or_else(|| root.clone(), |c| root.join(c)), - run: p.run.clone(), - store: Ok(StoreUse { - root: root.clone(), - outputs: p.outputs.clone(), - version: p.version.clone(), - }), - before_run: Some(Box::new(move || { - sync.lock() - .unwrap() - .sync() - .map(|_| ()) - .map_err(|e| e.to_string()) - })), - }; - match self.run_prepare(name, job, cancel) { - Ok(v) if v.source == "store" => state = "linked (store)", - Ok(_) => {} - Err(e) => { - views.extend(at("not linked", Some(e))); - continue; + let to = match current + .filter(|&i| ready(i)) + .or_else(|| (0..2).find(|&i| ready(i))) + { + Some(i) => i, + None => { + // The other copy than the one the worktree has: it + // keeps that one until this one is whole. + let to = current.map_or(0, |i| 1 - i); + let mark = copies[to].join("prepared").join(name); + // Half-made, it is no copy of any key. + let _ = std::fs::remove_file(&mark); + let mut now = views.clone(); + now.extend(at("preparing", None)); + report(&now); + let snap = match Snapshot::open(worktree, &copies[to]) { + Ok(s) => Arc::new(std::sync::Mutex::new(s)), + Err(e) => { + views.extend(at("not linked", Some(e.to_string()))); + continue; + } + }; + let root = roots[to].clone(); + let cwd = p.cwd.as_ref().or(svc.cwd.as_ref()); + let job = PrepareJob { + key, + mark, + cwd: cwd.map_or_else(|| root.clone(), |c| root.join(c)), + run: p.run.clone(), + store: Ok(StoreUse { + root: root.clone(), + outputs: p.outputs.clone(), + version: p.version.clone(), + }), + before_run: Some(Box::new(move || { + snap.lock() + .unwrap() + .sync() + .map(|_| ()) + .map_err(|e| e.to_string()) + })), + }; + match self.run_prepare(name, job, cancel) { + Ok(v) if v.source == "store" => state = "linked (store)", + Ok(_) => {} + Err(e) if current.is_some() => { + views.extend(at( + "linked (stale)", + Some(format!( + "the worktree keeps the copy made for its inputs before: {e}" + )), + )); + continue; + } + Err(e) => { + views.extend(at("not linked", Some(e))); + continue; + } } + to } - } + }; for rel in &p.link { - let (state, why) = match link_one(worktree, &root, rel) { + // A link of ours to the other copy moves to this one. + let link = worktree.join(rel); + if (0..2).any(|i| i != to && links_to(&link, &roots[i].join(rel))) { + let _ = remove_link(&link); + } + let (state, why) = match link_one(worktree, &roots[to], rel) { Linked::Made | Linked::There(true) => (state, None), Linked::There(false) => ( "not linked", @@ -719,7 +772,7 @@ impl Supervisor { dir: &Path, stack: &devstack_config::Stack, ) { - let copy = dir.join("linked").join("src"); + let roots = node_copies(&dir.join("linked")).map(|c| c.join("src")); for rel in stack .services .values() @@ -727,7 +780,7 @@ impl Supervisor { .flat_map(|p| &p.link) { let link = worktree.join(rel); - if links_to(&link, ©.join(rel)) { + if roots.iter().any(|r| links_to(&link, &r.join(rel))) { let r = remove_link(&link); crate::diag::info( "prepare_unlinked", @@ -1104,7 +1157,8 @@ const STORE_REMOVE_WAIT: Duration = Duration::from_secs(5); pub struct NodeLink { pub path: String, pub service: String, - /// `linked`, `linked (store)`, `preparing`, `not linked`. + /// `linked`, `linked (store)`, `preparing`, `linked (stale)`, `not + /// linked`. pub state: String, #[serde(skip_serializing_if = "Option::is_none")] pub why: Option, @@ -1153,6 +1207,14 @@ enum Linked { Not(String), } +/// The two copies a lane on a node's worktree is linked to in turn +/// (ADR-0063 addendum 2026-10-27), each a snapshot with its `src` and its +/// `prepared` marks: `linked` itself, the one there was before, and +/// `linked/next`. +fn node_copies(linked: &Path) -> [PathBuf; 2] { + [linked.to_path_buf(), linked.join("next")] +} + /// `rel` in `repo` linked to `rel` in `copy` (ADR-0044): only where the /// worktree has nothing of that name, and only where git ignores the link /// (`git add -A` would commit it; it is taken away again). @@ -1348,6 +1410,74 @@ mod tests { let last = seen.lock().unwrap().last().cloned().unwrap(); assert_eq!(last[0].state, "linked", "{last:?}"); + // New inputs, and their prepare fails: the worktree keeps the copy + // it had, whole, and says so (#305). + std::fs::write(wt.join("package-lock.json"), "{\"v\":2}").unwrap(); + let failing = devstack_config::from_str( + &text.replace("run = \"npm ci\"", "run = \"exit 3\""), + Path::new("t"), + ) + .unwrap(); + sup.link_node_lane(&wt, &lane, &failing, &report, &|| false); + let last = seen.lock().unwrap().last().cloned().unwrap(); + assert_eq!(last[0].state, "linked (stale)", "{last:?}"); + assert!( + last[0] + .why + .as_deref() + .unwrap() + .contains("exited with code 3"), + "{last:?}" + ); + assert_eq!( + std::fs::read_to_string(wt.join("deps/eslint/index.js")).unwrap(), + "lint" + ); + // Once the store has them, the other copy gets them and the link + // moves there; the first copy stays as it was. + let p2 = failing.services["web"].prepare.as_ref().unwrap(); + let key2 = prepare_key( + p2, + &failing.services["web"], + [( + "package-lock.json", + blake3::hash(b"{\"v\":2}").to_hex().to_string(), + )] + .into_iter(), + ) + .unwrap(); + std::fs::write(made.join("deps/eslint/index.js"), "lint 2").unwrap(); + sup.store() + .put( + "web", + &store_key_of(&key2, &p2.outputs, None), + &made, + &p2.outputs, + Duration::from_secs(60), + ) + .unwrap(); + sup.link_node_lane(&wt, &lane, &failing, &report, &|| false); + let last = seen.lock().unwrap().last().cloned().unwrap(); + assert_eq!(last[0].state, "linked (store)", "{last:?}"); + assert_eq!( + std::fs::read_to_string(wt.join("deps/eslint/index.js")).unwrap(), + "lint 2" + ); + assert!(lane.join("linked/next/src/deps/eslint/index.js").is_file()); + assert_eq!( + std::fs::read_to_string(lane.join("linked/src/deps/eslint/index.js")).unwrap(), + "lint" + ); + // Back to the first inputs: that copy is still theirs, nothing runs. + std::fs::write(wt.join("package-lock.json"), "{\"v\":1}").unwrap(); + sup.link_node_lane(&wt, &lane, &stack, &report, &|| false); + let last = seen.lock().unwrap().last().cloned().unwrap(); + assert_eq!(last[0].state, "linked", "{last:?}"); + assert_eq!( + std::fs::read_to_string(wt.join("deps/eslint/index.js")).unwrap(), + "lint" + ); + // The lane stops: the link goes, the copy stays for the next start. sup.unlink_node_lane(&wt, &lane, &stack); assert!(std::fs::symlink_metadata(wt.join("deps")).is_err()); diff --git a/docs/adr/0063-a-lane-on-a-node-links-its-worktree-to-a-copy-from-the-local-store.md b/docs/adr/0063-a-lane-on-a-node-links-its-worktree-to-a-copy-from-the-local-store.md index 5637a07a..073f2f11 100644 --- a/docs/adr/0063-a-lane-on-a-node-links-its-worktree-to-a-copy-from-the-local-store.md +++ b/docs/adr/0063-a-lane-on-a-node-links-its-worktree-to-a-copy-from-the-local-store.md @@ -1,6 +1,6 @@ # ADR-0063: A lane on a node links its worktree to a copy from the local store -- **Status:** Accepted (2026-10-26); built, noted 2026-10-26 +- **Status:** Accepted (2026-10-26); built, noted 2026-10-26; addendum 2026-10-27 (two copies: new inputs are prepared beside the one the worktree has) - **Date:** 2026-10-26 - **Deciders:** project owner - **Related:** [#277](https://github.com/nuetzliches/devstack/issues/277), [#159](https://github.com/nuetzliches/devstack/issues/159), [ADR-0044](0044-a-lanes-prepared-directories-can-be-linked-into-its-worktree.md) (point 5 replaced here for lanes on a node), [ADR-0062](0062-what-prepare-makes-is-kept-in-a-store-per-machine-and-repository-keyed-by-its-inputs.md), [ADR-0025](0025-a-lane-can-run-on-another-node.md), [ADR-0029](0029-source-sync-and-port-forwarding-for-remote-lanes.md) @@ -104,6 +104,23 @@ lane header (`ui/src/components/StackHeader.vue`). `prepare` for a worktree runs under the main tree's one-at-a-time lock and logs as the main tree's service. +## Addendum 2026-10-27: two copies, the link moves when the new one is whole + +Point 2 ran `prepare` for new inputs in the copy the worktree was linked to. +`npm ci` empties `node_modules` first: while it ran, the worktree's hooks +found a few packages, and when it failed (a file held open, a machine out +of memory, #305) the link stayed on what was left, with nothing in the log. + +A lane on a node now has two copies, `linked/src` and `linked/next/src`, each +with its own `prepared/` marks. New inputs are prepared (from the +store, or by running `prepare`) in the copy the worktree is *not* linked to; +its mark goes first, so a half-made copy is no copy of any key. Once it +succeeded the link moves there. When it fails the worktree keeps the copy +it had, made for its inputs before, and the path says `linked (stale)` with +why; the next look tries again. Inputs that go back to those of the other +copy only move the link. A failed `prepare` is logged (`prepare_failed`, +with its log). The disk a lane's copies take doubles at most. + ## What this ADR does not say - That the lane on the node uses this machine's store. It uses its node's. diff --git a/docs/adr/README.md b/docs/adr/README.md index ff51ba24..53d8d49c 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -111,7 +111,7 @@ stay. | [0060](0060-the-tray-is-a-client-of-the-hub.md) | The tray is a client of the hub, a process of its own, on every desktop | Accepted | | [0061](0061-the-dashboard-opens-in-its-installed-app.md) | The dashboard opens in its installed app, when there is one | Accepted | | [0062](0062-what-prepare-makes-is-kept-in-a-store-per-machine-and-repository-keyed-by-its-inputs.md) | What `prepare` makes is kept in a store per machine and repository, keyed by its inputs | Accepted; measured and built on the developer's machine, and on nodes, noted 2026-10-26 | -| [0063](0063-a-lane-on-a-node-links-its-worktree-to-a-copy-from-the-local-store.md) | A lane on a node links its worktree to a copy from the local store | Accepted; built, noted 2026-10-26 | +| [0063](0063-a-lane-on-a-node-links-its-worktree-to-a-copy-from-the-local-store.md) | A lane on a node links its worktree to a copy from the local store | Accepted; built, noted 2026-10-26; addendum 2026-10-27 (two copies, the link moves when the new one is whole) | | [0064](0064-every-compose-call-is-told-the-lane-it-runs-for.md) | Every Compose call is told the lane it runs for | Accepted; addendum 2026-10-26 (a lane on a node: native `{lane}`, `{lane_suffix}`, `DEVSTACK_LANE` follow its name) | | [0065](0065-the-store-is-warmed-for-a-ref-before-a-lane-needs-it.md) | The store is warmed for a ref before a lane needs it | Accepted; built, noted 2026-10-26 | | [0066](0066-a-measured-cpu-need-is-capped-at-half-of-what-its-node-gives-lanes.md) | A measured CPU need is capped at half of what its node gives lanes | Accepted | diff --git a/docs/dashboard-api.md b/docs/dashboard-api.md index afc4f6a6..67226909 100644 --- a/docs/dashboard-api.md +++ b/docs/dashboard-api.md @@ -267,8 +267,8 @@ type LaneView = { removing?: boolean; // its worktree is gone: it stops and its data goes (ADR-0024 point 7); gone from the list after notices?: Notice[]; // its tree's while it runs here, and what the main tree knows of it (`worktree_missing`) links?: { path: string; service: string; - state: "linked" | "linked (store)" | "preparing" | "not linked"; - why?: string }[]; // a lane on a node: what `prepare` makes, linked into its worktree from a copy on this machine (ADR-0063) + state: "linked" | "linked (store)" | "preparing" | "linked (stale)" | "not linked"; + why?: string }[]; // a lane on a node: what `prepare` makes, linked into its worktree from a copy on this machine (ADR-0063); `linked (stale)`: the prepare for new inputs failed (`why`), the worktree keeps the copy for its inputs before }; ``` diff --git a/docs/stack-file.md b/docs/stack-file.md index 92757a1b..ef610178 100644 --- a/docs/stack-file.md +++ b/docs/stack-file.md @@ -602,8 +602,10 @@ source) and keeps its outputs in the store; then it links the worktree there, by the rules above. The worktree's hooks get what is built for this machine (Windows here, Linux on the node). `devstack lane list`, `devstack status` and the dashboard say per path `linked`, `linked (store)`, -`preparing` or `not linked` with why; a hook that runs while it says -`preparing` does not find it yet. Without `outputs` it is not linked, and +`preparing`, `linked (stale)` or `not linked` with why. New inputs are +prepared in a second copy while the worktree keeps the one it has; the link +moves once that copy is whole, and stays where it was when its `prepare` +fails (`linked (stale)`, with why). Without `outputs` it is not linked, and says so. The links go when the lane stops on its node, moves or goes; the copy stays for its next start. diff --git a/ui/src/api/types.ts b/ui/src/api/types.ts index 8cdb02a2..a7a53329 100644 --- a/ui/src/api/types.ts +++ b/ui/src/api/types.ts @@ -183,7 +183,7 @@ export type LaneView = { export type NodeLink = { path: string; service: string; - state: 'linked' | 'linked (store)' | 'preparing' | 'not linked'; + state: 'linked' | 'linked (store)' | 'preparing' | 'linked (stale)' | 'not linked'; why?: string; }; diff --git a/ui/src/components/StackHeader.vue b/ui/src/components/StackHeader.vue index 5d275d38..5ed9fa8c 100644 --- a/ui/src/components/StackHeader.vue +++ b/ui/src/components/StackHeader.vue @@ -357,7 +357,7 @@ function openWith(o: Opener) { ยท {{ k.path }}: {{ k.state }} From cdc8dd0d11498b702d1355ed8300d4807bac1685 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Tue, 27 Oct 2026 12:54:37 +0100 Subject: [PATCH 2/2] test: a prepare that cannot spawn (no shim in a unit test off Windows) fails all the same Co-Authored-By: Claude Opus 5.5 --- crates/devstack-daemon/src/supervisor/copy.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/devstack-daemon/src/supervisor/copy.rs b/crates/devstack-daemon/src/supervisor/copy.rs index 741b040f..cc6ed01d 100644 --- a/crates/devstack-daemon/src/supervisor/copy.rs +++ b/crates/devstack-daemon/src/supervisor/copy.rs @@ -1421,12 +1421,11 @@ mod tests { sup.link_node_lane(&wt, &lane, &failing, &report, &|| false); let last = seen.lock().unwrap().last().cloned().unwrap(); assert_eq!(last[0].state, "linked (stale)", "{last:?}"); + // Elsewhere a unit test cannot spawn (no shim): a failure all the same. + let why = last[0].why.as_deref().unwrap(); assert!( - last[0] - .why - .as_deref() - .unwrap() - .contains("exited with code 3"), + why.contains("keeps the copy made for its inputs before") + && (!cfg!(windows) || why.contains("exited with code 3")), "{last:?}" ); assert_eq!(