From e2139789c1fa0877f00db1ebe28cc4cc56cce8b5 Mon Sep 17 00:00:00 2001 From: Paul Otto <219596+potto007@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:56:29 -0700 Subject: [PATCH] fix: start the stall clock when a link goes busy Github-Issue: #368 --- src/store.rs | 4 ++++ src/wf.rs | 18 +++++++++++++----- tests/all/lifecycle.rs | 24 +++++++++++++++++++++++- tests/all/setup_steps.rs | 1 + tests/all/wf.rs | 7 ++++--- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/store.rs b/src/store.rs index 2fe02cb..d4b34e0 100644 --- a/src/store.rs +++ b/src/store.rs @@ -119,6 +119,8 @@ pub struct Link { pub project: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub paused_at: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub busy_since: Option, #[serde(flatten)] pub identity: Identity, #[serde(default, skip_serializing_if = "Option::is_none")] @@ -1349,6 +1351,7 @@ impl Store { nudges: 0, escalated: false, bad_run: None, + busy_since: None, settled_at: None, ..ln }; @@ -2323,6 +2326,7 @@ mod tests { base: Default::default(), project: None, paused_at: None, + busy_since: None, identity: Identity::default(), brief: None, pr: None, diff --git a/src/wf.rs b/src/wf.rs index aed00e2..aa3b73c 100644 --- a/src/wf.rs +++ b/src/wf.rs @@ -895,6 +895,7 @@ impl Wf { closed_how: None, unread: old.as_ref().map(|o| o.unread.clone()).unwrap_or_default(), bad_run: None, + busy_since: None, settled_at: None, restored_at: None, base: Default::default(), @@ -1755,13 +1756,18 @@ impl Wf { } let updated = th["updatedAt"].as_str(); ln.last_seen = updated.map(String::from); + let status = th["status"].as_str().unwrap_or(""); + let busy = + ACTIVE.contains(&status) || truthy(&th["blockedOnThreadWait"]) || truthy(&th["pendingBackgroundTasks"]); + if !busy { + ln.busy_since = None; + } else if ln.busy_since.is_none() { + ln.busy_since = Some(now.clone()); + } if ln.paused_at.is_some() { self.reach.hit(ReachPoint::PausedLinkSkipped); return Ok(()); } - let status = th["status"].as_str().unwrap_or(""); - let busy = - ACTIVE.contains(&status) || truthy(&th["blockedOnThreadWait"]) || truthy(&th["pendingBackgroundTasks"]); let idle = self.mins(updated).unwrap_or(0.0); let latest_run = th["latestRunId"].as_str().map(String::from); if truthy(&th["pendingRequestCount"]) || status == "waiting" { @@ -1777,14 +1783,16 @@ impl Wf { return Ok(()); } if busy { - let quiet = self.mins(Some(&ln.last_up)).unwrap_or(0.0); + let since = + ln.busy_since.as_deref().filter(|b| *b > ln.last_up.as_str()).unwrap_or(&ln.last_up).to_string(); + let quiet = self.mins(Some(&since)).unwrap_or(0.0); if ln.gate.is_none() && quiet >= p.stall as f64 { let text = format!( "{} has been busy but sent nothing for {quiet:.0} min. t3.t3_thread_read shows where it is: {}", ln.label, Self::where_is(ln) ); - let key = format!("stall@{}", ln.last_up); + let key = format!("stall@{since}"); if !ln.reported.contains(&key) { self.reach.hit(ReachPoint::StallReported); } diff --git a/tests/all/lifecycle.rs b/tests/all/lifecycle.rs index c90d4f0..a8bbc1f 100644 --- a/tests/all/lifecycle.rs +++ b/tests/all/lifecycle.rs @@ -88,7 +88,8 @@ async fn a_worker_busy_past_its_stall_minutes_is_reported_once() { link_worker(&h).await; h.fake.start_run(WORKER, None); - h.tick(239).await; + h.tick(1).await; + h.tick(240).await; let early = reach(&h, ReachPoint::StallReported); h.tick(241).await; h.tick(300).await; @@ -101,6 +102,27 @@ async fn a_worker_busy_past_its_stall_minutes_is_reported_once() { assert!(sent_to(&h, WORKER).is_empty()); } +#[tokio::test] +async fn a_worker_woken_after_a_long_idle_gets_its_full_stall_minutes() { + let h = H::new(); + link_worker(&h).await; + h.tick(300).await; + h.clock.set(&at(400)); + h.fake.start_run(WORKER, None); + + h.tick(401).await; + h.tick(639).await; + let early = reach(&h, ReachPoint::StallReported); + h.tick(641).await; + + assert_eq!(early, 0); + assert_eq!(reach(&h, ReachPoint::StallReported), 1); + let reports: Vec = + sent_to(&h, TICKET).into_iter().filter(|m| m.contains("busy but sent nothing")).collect(); + assert_eq!(reports.len(), 1, "{reports:?}"); + assert!(reports[0].contains("for 240 min"), "{}", reports[0]); +} + #[tokio::test] async fn an_idle_worker_is_escalated_after_its_missed_nudges() { let h = H::new(); diff --git a/tests/all/setup_steps.rs b/tests/all/setup_steps.rs index 1dba664..49b584c 100644 --- a/tests/all/setup_steps.rs +++ b/tests/all/setup_steps.rs @@ -487,6 +487,7 @@ fn retire_t3wf_warns_and_nudges_open_links_without_blocking() { base: Default::default(), project: None, paused_at: None, + busy_since: None, identity: Default::default(), brief: None, pr: None, diff --git a/tests/all/wf.rs b/tests/all/wf.rs index 3322a8f..c2794af 100644 --- a/tests/all/wf.rs +++ b/tests/all/wf.rs @@ -291,13 +291,13 @@ async fn the_gate_kind_opens_the_gate_and_wakes() { async fn a_busy_child_is_left_alone_but_a_long_silence_is_reported_once() { let h = H::new(); link_ticket_worker(&h).await; - h.fake.update(WORKER, json!({"status": "running", "updatedAt": at(200)})); - assert!(h.tick(200).await.is_empty()); + h.fake.update(WORKER, json!({"status": "running", "updatedAt": at(1)})); + assert!(h.tick(1).await.is_empty()); h.fake.update(WORKER, json!({"status": "idle", "pendingBackgroundTasks": [{"id": "b1"}], "updatedAt": at(100)})); assert!(h.tick(200).await.is_empty()); let out = h.tick(241).await; assert!(out.iter().any(|a| a.starts_with("report to mcp:ticket")), "{out:?}"); - assert!(h.last_message().contains("sent nothing for 241 min"), "{}", h.last_message()); + assert!(h.last_message().contains("sent nothing for 240 min"), "{}", h.last_message()); let n = h.sent().len(); h.tick(400).await; assert_eq!(h.sent().len(), n); @@ -567,6 +567,7 @@ fn bare_link(child: &str, parent: &str, label: &str) -> Link { base: Default::default(), project: None, paused_at: None, + busy_since: None, identity: Default::default(), brief: None, pr: None,