Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
231 changes: 180 additions & 51 deletions crates/devstack-daemon/src/supervisor/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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): `<dir>/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): `<dir>/linked/src` or `<dir>/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,
Expand All @@ -512,9 +523,10 @@ impl Supervisor {
cancel: &dyn Fn() -> bool,
) {
let linked = dir.join("linked");
let copies = node_copies(&linked);
let mut views: Vec<NodeLink> = 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",
Expand All @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -719,15 +772,15 @@ 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()
.filter_map(|s| s.prepare.as_ref())
.flat_map(|p| &p.link)
{
let link = worktree.join(rel);
if links_to(&link, &copy.join(rel)) {
if roots.iter().any(|r| links_to(&link, &r.join(rel))) {
let r = remove_link(&link);
crate::diag::info(
"prepare_unlinked",
Expand Down Expand Up @@ -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<String>,
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -1348,6 +1410,73 @@ 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:?}");
// Elsewhere a unit test cannot spawn (no shim): a failure all the same.
let why = last[0].why.as_deref().unwrap();
assert!(
why.contains("keeps the copy made for its inputs before")
&& (!cfg!(windows) || why.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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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/<service>` 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.
Expand Down
2 changes: 1 addition & 1 deletion docs/adr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
4 changes: 2 additions & 2 deletions docs/dashboard-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
```

Expand Down
Loading
Loading