From 45ed0ba48dd14ee5bbb6b59cfa16f3ff8b2c7694 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Thu, 19 Nov 2026 12:46:27 +0100 Subject: [PATCH 1/2] fix(daemon,hub): a running job is not counted as ready or live Since #544 a job is `running` while it runs. Two more places read that as "up", without the job exception the daemon's own checks have (#573): - The hub's summary (`Scope::glance`) counted a running job as ready, so a stack whose migration still ran showed as ready on the start page. It now counts as starting; the kind comes from the snapshot, as a service event carries none (#575). - The build timeline marked a job's build live from its start, and kept it live when the job then failed. A job's build is live once the job completed with exit code 0; `GET /v1/services/{name}/builds` passes the service's kind (#571). Closes #571 Closes #575 Bug-Class: running-read-as-ready Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 6 +++ crates/devstack-daemon/src/api.rs | 2 + crates/devstack-daemon/src/timeline.rs | 72 ++++++++++++++++++++----- crates/devstack-hub/src/notify/rules.rs | 26 +++++++++ 4 files changed, 93 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74541260..e18e0f71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,12 @@ and listed with their assets on the start), writes it anew, and a running daemon does so with its shift before it writes its claims. An entry that is no JSON object is now said in the log, too (#570). +- **A running job is no longer counted as ready or live.** Since a job is + `running` while it runs (#544), the hub's summary counted a stack whose + migration still ran as ready on the start page; it now counts as starting + until the job completed (#575). A job's build showed as live in the build + timeline from its start, and stayed live when the job then failed; it is + live once the job completed (#571). - **The MCP server no longer promises to follow an agent into a worktree.** It acts on the tree it was started in; its instructions and the `up` and `lanes` tools said that in a worktree every tool acts on its lane, so an diff --git a/crates/devstack-daemon/src/api.rs b/crates/devstack-daemon/src/api.rs index 9f1a49cd..7673c4d7 100644 --- a/crates/devstack-daemon/src/api.rs +++ b/crates/devstack-daemon/src/api.rs @@ -1598,6 +1598,7 @@ async fn builds( } let view = s.sup.view().services.into_iter().find(|v| v.name == name); let current = view.as_ref().and_then(|v| v.build_id.clone()); + let job = view.as_ref().is_some_and(|v| v.kind == "job"); // The build on its way now, if any: the others the log leaves on their // way were interrupted. let on_way = view @@ -1624,6 +1625,7 @@ async fn builds( current.as_deref(), on_way.as_deref(), limit, + job, ); // What of each is still there: older builds outlive their artifact // and their log in the list. diff --git a/crates/devstack-daemon/src/timeline.rs b/crates/devstack-daemon/src/timeline.rs index 3877b411..81ed9189 100644 --- a/crates/devstack-daemon/src/timeline.rs +++ b/crates/devstack-daemon/src/timeline.rs @@ -36,7 +36,9 @@ fn at_ms(v: &Value) -> Option { /// The builds of `service` in the diagnostic log lines, newest first, at most /// `limit`. `on_way` is the build the service's candidate has now while it -/// builds, starts or switches: only that one is still on its way. +/// builds, starts or switches: only that one is still on its way. `job`: the +/// service is a job, whose build is live once it completed (it is `running` +/// while it runs, #544, #571). pub fn builds( lines: &[String], service: &str, @@ -44,6 +46,7 @@ pub fn builds( current: Option<&str>, on_way: Option<&str>, limit: usize, + job: bool, ) -> Vec { let mut out: Vec = Vec::new(); // A build starts before its id is known (`building` without one). @@ -121,11 +124,13 @@ pub fn builds( if state == "building" && v["status"]["build_id"].is_null() { pending_start.get_or_insert(at); } - // The main instance runs a build: it is live. - if let Some(id) = v["build_id"] - .as_str() - .filter(|_| state == "healthy" || state == "running") - { + // The main instance runs a build, or a job ran it: it is live. + let live = if job { + state == "completed" && v["status"]["exit_code"] == 0 + } else { + state == "healthy" || state == "running" + }; + if let Some(id) = v["build_id"].as_str().filter(|_| live) { match out.iter_mut().find(|e| e.build == id) { Some(e) => { if e.outcome != "live" { @@ -172,10 +177,11 @@ pub fn from_log( current: Option<&str>, on_way: Option<&str>, limit: usize, + job: bool, ) -> Vec { let needle = format!("\"service\":\"{service}\""); let lines = crate::diag::tail(path, 50_000, Some(&needle)); - builds(&lines, service, lane, current, on_way, limit) + builds(&lines, service, lane, current, on_way, limit, job) } #[cfg(test)] @@ -229,9 +235,9 @@ mod tests { r#""event":"service","service":"web","build_id":"zzz","status":{"state":"healthy"}"#, ), ]; - let b = builds(&lines, "api", "", Some("bbb"), None, 10); + let b = builds(&lines, "api", "", Some("bbb"), None, 10, false); assert!( - builds(&lines, "api", "feat", None, None, 10).is_empty(), + builds(&lines, "api", "feat", None, None, 10, false).is_empty(), "another lane stays out" ); let got: Vec<(&str, &str, bool)> = b @@ -254,7 +260,7 @@ mod tests { "from the build start to the switch" ); assert_eq!(b[0].reason.as_deref(), Some("exited with code 3")); - assert_eq!(builds(&lines, "api", "", None, None, 2).len(), 2); + assert_eq!(builds(&lines, "api", "", None, None, 2, false).len(), 2); } #[test] @@ -276,13 +282,53 @@ mod tests { ), ]; for lane in ["", "main"] { - let ids: Vec = builds(&lines, "api", lane, None, None, 10) + let ids: Vec = builds(&lines, "api", lane, None, None, 10, false) .into_iter() .map(|e| e.build) .collect(); assert_eq!(ids, ["bbb", "aaa"], "lane {lane:?}"); } - assert_eq!(builds(&lines, "api", "feat", None, None, 10).len(), 1); + assert_eq!( + builds(&lines, "api", "feat", None, None, 10, false).len(), + 1 + ); + } + + /// #571: a job is `running` while it runs (#544); its build is live once + /// it completed, not while it runs, and not when it failed after. + #[test] + fn a_jobs_build_is_live_once_the_job_completed() { + let svc = |t, id: &str, status: &str| { + ev( + t, + &format!( + "\"event\":\"service\",\"service\":\"migrate\",\"build_id\":\"{id}\",\"status\":{status}" + ), + ) + }; + let lines = vec![ + svc(1, "aaa", r#"{"state":"running"}"#), + svc(2, "aaa", r#"{"state":"completed","exit_code":0}"#), + svc(3, "bbb", r#"{"state":"running"}"#), + svc( + 4, + "bbb", + r#"{"state":"failed","reason":"job exited with code 1"}"#, + ), + svc(5, "ccc", r#"{"state":"running"}"#), + ]; + let got: Vec<(String, String)> = builds(&lines, "migrate", "", Some("ccc"), None, 10, true) + .into_iter() + .map(|e| (e.build, e.outcome)) + .collect(); + assert_eq!( + got, + [("aaa".to_string(), "live".to_string())], + "only the job that completed" + ); + // A long-running service's `running` is live, as before. + let b = builds(&lines, "migrate", "", None, None, 10, false); + assert_eq!(b.len(), 3); } #[test] @@ -305,7 +351,7 @@ mod tests { c(3, "building", "bbb"), ]; let outcomes = |on_way| { - builds(&lines, "api", "", Some("zzz"), on_way, 10) + builds(&lines, "api", "", Some("zzz"), on_way, 10, false) .into_iter() .map(|e| (e.build, e.outcome)) .collect::>() diff --git a/crates/devstack-hub/src/notify/rules.rs b/crates/devstack-hub/src/notify/rules.rs index 2a2a9027..9373b35f 100644 --- a/crates/devstack-hub/src/notify/rules.rs +++ b/crates/devstack-hub/src/notify/rules.rs @@ -89,6 +89,8 @@ const CASCADE_PREFIX: &str = "not started: "; #[derive(Debug, Default, Clone)] struct Svc { + /// `kind`, from the snapshot: a service event carries none. + kind: String, /// `status.state`. state: String, /// `status.reason` and `status.exit_code`, for what the tray shows. @@ -120,6 +122,7 @@ impl Scope { Some(( s["name"].as_str()?.to_string(), Svc { + kind: s["kind"].as_str().unwrap_or("").to_string(), state: state_of(&s["status"]), reason: reason_of(&s["status"]), exit_code: s["status"]["exit_code"].as_i64(), @@ -174,6 +177,8 @@ impl Scope { for name in names { let svc = &self.services[name]; match svc.state.as_str() { + // A job runs until it ends (#544): that is its start (#575). + "running" if svc.kind == "job" => g.starting = true, "running" | "healthy" => g.ready += 1, "completed" if svc.exit_code == Some(0) => g.ready += 1, "building" | "starting" => g.starting = true, @@ -521,6 +526,27 @@ mod tests { assert_eq!(g.attention[1].level, Level::Error); } + /// #575: a job is `running` while it runs (#544), which is its start, + /// not a service that is ready; a long-running service's `running` is. + #[test] + fn a_running_job_counts_as_starting_not_ready() { + let mut s = Scope::default(); + s.apply(&snapshot(json!([ + { "name": "api", "kind": "long-running", "status": { "state": "running" } }, + { "name": "migrate", "kind": "job", "status": { "state": "starting" } } + ]))); + // The service event carries no kind: the snapshot's stays. + s.apply(&service("migrate", json!({ "state": "running" }))); + let g = s.glance(); + assert_eq!((g.ready, g.total, g.starting), (1, 2, true)); + s.apply(&service( + "migrate", + json!({ "state": "completed", "exit_code": 0 }), + )); + let g = s.glance(); + assert_eq!((g.ready, g.starting), (2, false)); + } + #[test] fn what_the_snapshot_shows_is_the_baseline() { let mut s = Scope::default(); From f8928cc40ea55948ce1d9356b7c0c6391e14b214 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Thu, 19 Nov 2026 12:57:07 +0100 Subject: [PATCH 2/2] fix(daemon): a job's build runs, then is live or failed, in the build timeline A rebuild of a job that does not run goes through the candidate, which is cleared before the job starts: the entry stayed `building` and the final pass made it `interrupted` while the job ran, and a job that then failed showed `interrupted` (or `superseded` after the next change) instead of `failed`. The service events now set a job's build to `running` while it runs, `live` once it completed with exit code 0, and `failed` with the status's reason; a `running` build that is not the current one was interrupted. The test replays the candidate events. Code review before 0.51.0, finding 2. Bug-Class: running-read-as-ready Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 3 +- crates/devstack-daemon/src/timeline.rs | 87 +++++++++++++++++++------- docs/dashboard-api.md | 2 +- 3 files changed, 68 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e18e0f71..37ed3563 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,7 +38,8 @@ and listed with their assets on the migration still ran as ready on the start page; it now counts as starting until the job completed (#575). A job's build showed as live in the build timeline from its start, and stayed live when the job then failed; it is - live once the job completed (#571). + `running` while the job runs, live once it completed, and `failed` with + its reason when the job failed (#571). - **The MCP server no longer promises to follow an agent into a worktree.** It acts on the tree it was started in; its instructions and the `up` and `lanes` tools said that in a worktree every tool acts on its lane, so an diff --git a/crates/devstack-daemon/src/timeline.rs b/crates/devstack-daemon/src/timeline.rs index 81ed9189..614c3421 100644 --- a/crates/devstack-daemon/src/timeline.rs +++ b/crates/devstack-daemon/src/timeline.rs @@ -124,26 +124,35 @@ pub fn builds( if state == "building" && v["status"]["build_id"].is_null() { pending_start.get_or_insert(at); } - // The main instance runs a build, or a job ran it: it is live. - let live = if job { - state == "completed" && v["status"]["exit_code"] == 0 - } else { - state == "healthy" || state == "running" + // The main instance runs a build: it is live. A job's build + // runs, then is live once the job completed, or failed. + let outcome = match (job, state) { + (false, "healthy" | "running") => Some("live"), + (true, "running") => Some("running"), + (true, "completed") if v["status"]["exit_code"] == 0 => Some("live"), + (true, "failed" | "completed") => Some("failed"), + _ => None, }; - if let Some(id) = v["build_id"].as_str().filter(|_| live) { + if let (Some(id), Some(outcome)) = (v["build_id"].as_str(), outcome) { + let reason = (outcome == "failed") + .then(|| v["status"]["reason"].as_str().map(str::to_string)) + .flatten(); match out.iter_mut().find(|e| e.build == id) { Some(e) => { - if e.outcome != "live" { - e.outcome = "live".into(); + if e.outcome != outcome { + e.outcome = outcome.into(); e.updated_ms = at; } + if reason.is_some() { + e.reason = reason; + } } None => out.push(BuildEntry { build: id.into(), started_ms: pending_start.take().unwrap_or(at), updated_ms: at, - outcome: "live".into(), - reason: None, + outcome: outcome.into(), + reason, current: false, artifact: false, log: false, @@ -163,6 +172,10 @@ pub fn builds( { e.outcome = "interrupted".into(); } + // A job's run that is not the one running now. + if e.outcome == "running" && !e.current { + e.outcome = "interrupted".into(); + } } out.reverse(); out.truncate(limit); @@ -295,7 +308,10 @@ mod tests { } /// #571: a job is `running` while it runs (#544); its build is live once - /// it completed, not while it runs, and not when it failed after. + /// it completed, `running` while it runs, and `failed` when it failed + /// after. A rebuild of a job that does not run goes through the + /// candidate, which is gone before the job starts (code review before + /// 0.51.0, finding 2). #[test] fn a_jobs_build_is_live_once_the_job_completed() { let svc = |t, id: &str, status: &str| { @@ -306,29 +322,56 @@ mod tests { ), ) }; + let cand = |t, c: &str| { + ev( + t, + &format!("\"event\":\"candidate\",\"service\":\"migrate\",\"candidate\":{c}"), + ) + }; let lines = vec![ + // up: the first build, run once + ev( + 0, + r#""event":"service","service":"migrate","build_id":null,"status":{"state":"building","build_id":null}"#, + ), svc(1, "aaa", r#"{"state":"running"}"#), svc(2, "aaa", r#"{"state":"completed","exit_code":0}"#), - svc(3, "bbb", r#"{"state":"running"}"#), + // a change: built as a candidate, then run, and it fails + cand(3, r#"{"state":"building","build_id":null}"#), + cand(4, r#"{"state":"building","build_id":"bbb"}"#), + cand(5, "null"), + svc(6, "bbb", r#"{"state":"running"}"#), svc( - 4, + 7, "bbb", r#"{"state":"failed","reason":"job exited with code 1"}"#, ), - svc(5, "ccc", r#"{"state":"running"}"#), + // another change, and the job runs now + cand(8, r#"{"state":"building","build_id":"ccc"}"#), + cand(9, "null"), + svc(10, "ccc", r#"{"state":"running"}"#), ]; - let got: Vec<(String, String)> = builds(&lines, "migrate", "", Some("ccc"), None, 10, true) - .into_iter() - .map(|e| (e.build, e.outcome)) - .collect(); + let got = |current| -> Vec<(String, String)> { + builds(&lines, "migrate", "", current, None, 10, true) + .into_iter() + .map(|e| (e.build, e.outcome)) + .collect() + }; + let s = |b: &str, o: &str| (b.to_string(), o.to_string()); assert_eq!( - got, - [("aaa".to_string(), "live".to_string())], - "only the job that completed" + got(Some("ccc")), + [s("ccc", "running"), s("bbb", "failed"), s("aaa", "live")] ); + let b = builds(&lines, "migrate", "", Some("ccc"), None, 10, true); + assert_eq!(b[1].reason.as_deref(), Some("job exited with code 1")); + // The daemon stopped while it ran: the log says no more. + assert_eq!(got(None)[0], s("ccc", "interrupted")); // A long-running service's `running` is live, as before. let b = builds(&lines, "migrate", "", None, None, 10, false); - assert_eq!(b.len(), 3); + assert_eq!( + (b[0].build.as_str(), b[0].outcome.as_str()), + ("ccc", "live") + ); } #[test] diff --git a/docs/dashboard-api.md b/docs/dashboard-api.md index 258b2976..2e4c81dc 100644 --- a/docs/dashboard-api.md +++ b/docs/dashboard-api.md @@ -488,7 +488,7 @@ type StoreView = { | `POST /v1/lanes/{lane}/main` | that lane serves the main ports (`main`: the main tree again), workers move with them. A lane on a node only on a dedicated one (ADR-0025 point 4): the fixed ports forward to its ports here, its workers run on the node, and the main tree takes the ports back by itself when the lane is gone (its lease over, its node silent for longer than a lease lives). The main ports are the fixed ports of `[services.*]`, not a Compose container's published ones: `409` for a stack with no service on a port and none with workers, which would move nothing (#184) | | `POST /v1/main-slot {"owns"}` | only the daemon in a lane container on a node: it holds its requester's main slot, or no longer; its worker services restart with the switch. The requester says it again with each renewal. Answers `{"owns"}` | | `/v1/lanes/{lane}/v1/...` | the lane's own API, every route of this table; `StackView` then carries `lane`, the main one `main_served_by` while a lane serves its ports. `/v1/lanes/main/v1/...` is the main working tree's own `/v1/...` (since 0.33.0, [ADR-0080](adr/0080-the-main-tree-answers-as-a-lane-shared-is-deprecated.md)), so a client can address every tree the same way; no lane is ever named `main` | -| `GET /v1/services/{name}/builds?limit=50` | JSON, the builds of the service newest first, read from the diagnostic log (so across daemon restarts): `[{build, started_ms, updated_ms, outcome, reason, current, artifact, log}]` (`artifact`: the store still has it, so it can be gone back to; `log`: its build log is still kept; older builds outlive both in the list); `outcome` is `live`, `failed`, `superseded` (a newer change replaced it before it went live), `held`, `interrupted` (it was building, starting or switching and is no longer: the daemon stopped, or a stop or restart came between), or the state it is in now (only the candidate's build) | +| `GET /v1/services/{name}/builds?limit=50` | JSON, the builds of the service newest first, read from the diagnostic log (so across daemon restarts): `[{build, started_ms, updated_ms, outcome, reason, current, artifact, log}]` (`artifact`: the store still has it, so it can be gone back to; `log`: its build log is still kept; older builds outlive both in the list); `outcome` is `live`, `failed`, `superseded` (a newer change replaced it before it went live), `held`, `interrupted` (it was building, starting or switching and is no longer: the daemon stopped, or a stop or restart came between), `running` (a job's build while the job runs; live once it completed, `failed` with its reason when it failed), or the state it is in now (only the candidate's build) | | `GET /v1/services/{name}/logs/files` | JSON, the kept logs newest first: `[{file, kind: "run"\|"build", started_ms, modified_ms, build, port, bytes, current}]` | | `GET /v1/logs?grep=…&tail=50` | text, matching lines of every service's current log as `service \| line` (`grep` is required, `400` without it; `tail` counts per service); with `since=` every instance written to within that time | | `GET /v1/services/{name}/logs/stream?tail=200` | SSE: `{"type":"line","text":"..."}` for the last lines (`tail` at most 5000), then every new one; `{"type":"instance","build_id":...}` when another instance takes over (the lines that follow are the new one's). With `build=` it follows the newest instance of that build instead (a candidate's console), from before it exists to after it takes over |