From db6039c5018aec3a3f72dbd1c9773d2f74f8fa0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Sat, 21 Nov 2026 19:32:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E6=B5=81=EF=BC=9A=E9=87=8D?= =?UTF-8?q?=E9=96=8B=E4=B9=8B=E5=BE=8C=E4=B8=8D=E6=9C=83=E6=9C=89=E5=8D=A1?= =?UTF-8?q?=E4=BD=8F=E7=9A=84=E5=9F=B7=E8=A1=8C=EF=BC=88=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=AC=A1=E7=8D=A8=E7=AB=8B=E5=AF=A9=E6=9F=A5=E7=9A=84=20P1?= =?UTF-8?q?=EF=BC=89=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 以前:上一步結束、交付還沒開始的那幾秒(靠記憶體裡的 wf.advance,下一次 tick 才走),當機或強制結束後重開,那次執行永遠「進行中」,之後的排程都被擋、立即執行回「已經在跑了」。交付在等 Bot 時那一步被刪,也一樣卡到重開。 - 現在:每秒的 tick 也接手每一個能往下走的執行——還沒開始的那一步、等交付而 Bot 已經空下來的(那一步被刪了就照「這一步已經被刪掉了」收尾)。不再靠記憶體裡的佇列。 - 重開時只把「Bot 正在回答的問題」那種標成中斷(可能做到一半);還沒開始的、等交付的照樣接著走。 已驗證: cargo test 861 過(新:重開後清掉佇列,下一次 tick 照樣跑完;等交付時那一步被刪就收尾;沒在做的那次重開不標中斷、Bot 在回答的那次才標);clippy 沒有新的。 未驗證: 沒有真的讓 AnyBot 當掉再重開(靠單元測試)。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm --- src/app/workflow_run.rs | 50 +++++++++++++++++++++++++++++++---------- src/workflow.rs | 13 +++++++---- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/app/workflow_run.rs b/src/app/workflow_run.rs index 7573477..773f54e 100644 --- a/src/app/workflow_run.rs +++ b/src/app/workflow_run.rs @@ -381,26 +381,31 @@ impl App { for wf in std::mem::take(&mut self.wf.advance) { self.wf_advance(ctx, wf); } - // 交付 that waited for its bot's turn to end: once the bot is free, it goes on. - let free: Vec<(u64, u64, u64)> = self + // Runs that can go on: a 交付 whose bot is free now (or whose step was deleted while it + // waited), and a step not started yet — after a restart nothing else moves those on. + let ready: Vec<(u64, Option)> = self .state .workflows .iter() .flat_map(|w| { - w.runs.iter().filter(|r| r.state == RunState::Running).filter_map(move |r| { - let s = r.current().filter(|s| s.state == StepState::Running)?; - matches!(w.node(s.node).map(|n| &n.kind), Some(NodeKind::Deliver { .. })).then_some((w.id, r.id, s.node, s.bot)) + w.runs.iter().filter(|r| r.state == RunState::Running).filter_map(move |r| match r.current() { + None => Some((w.id, None, 0)), + Some(s) if s.state == StepState::Waiting => Some((w.id, None, 0)), + Some(s) if s.state == StepState::Running && s.asked_at == 0 && s.bot != 0 => Some((w.id, Some(s.node), s.bot)), + _ => None, }) }) - .filter(|(_, _, _, bot)| !self.streams.contains_key(bot)) - .map(|(w, r, n, _)| (w, r, n)) + .filter(|(_, waiting, bot)| waiting.is_none() || !self.streams.contains_key(bot)) + .map(|(w, n, _)| (w, n)) .collect(); - for (wf, run, node) in free { - self.wf_run_mut(wf, run, |r| { - if let Some(s) = r.steps.iter_mut().find(|s| s.node == node) { - s.state = StepState::Waiting; + for (wf, waiting) in ready { + if let Some(node) = waiting { + if let Some(r) = self.state.workflows.iter_mut().find(|w| w.id == wf).and_then(|w| w.runs.iter_mut().rev().find(|r| r.state == RunState::Running)) { + if let Some(s) = r.steps.iter_mut().find(|s| s.node == node) { + s.state = StepState::Waiting; + } } - }); + } self.wf_advance(ctx, wf); } // A step whose bot was deleted while it worked never ends: the run stops, saying why. @@ -688,6 +693,27 @@ mod tests { app.tick_workflows(&ctx); assert_eq!(run(&app).state, RunState::Done); assert!(app.state.bot(bot).unwrap().messages.last().unwrap().text.starts_with("工作流「早報」的結果")); + // After a restart: a run left between two steps (the 交付 not started — the in-memory + // queue is gone) goes on at the next tick; one whose waiting 交付 was deleted ends. + app.streams.insert(bot, busy()); + assert!(app.wf_start(&ctx, 1, "手動")); + answer(&mut app, bot, "四則新聞"); + app.wf.advance.clear(); + app.streams.remove(&bot); + app.tick_workflows(&ctx); + assert_eq!(run(&app).state, RunState::Done, "moved on without the queue"); + app.streams.insert(bot, busy()); + assert!(app.wf_start(&ctx, 1, "手動")); + answer(&mut app, bot, "五則新聞"); + app.tick_workflows(&ctx); + assert_eq!(run(&app).current().map(|s| s.state), Some(StepState::Running), "the 交付 waits for the bot"); + app.state.workflows[0].remove(3); + app.streams.remove(&bot); + app.tick_workflows(&ctx); + assert_eq!((run(&app).state, run(&app).why.as_str()), (RunState::Failed, "這一步已經被刪掉了")); + app.state.workflows[0].nodes.push(crate::workflow::Node { id: 3, kind: NodeKind::Deliver { bot }, x: 0.0, y: 400.0 }); + app.state.workflows[0].connect(2, 3); + app.streams.insert(bot, busy()); // A bot deleted while on its step. app.state.bots.push(app.state.bots[0].clone()); let other = app.state.bots.iter().map(|b| b.id).max().unwrap() + 1; diff --git a/src/workflow.rs b/src/workflow.rs index 9c59194..fb2a300 100644 --- a/src/workflow.rs +++ b/src/workflow.rs @@ -264,10 +264,10 @@ impl Workflow { /// the person stays: it can still be answered. pub fn mark_interrupted(&mut self, now: i64) -> bool { let mut cut = false; - let delivering: Vec = self.nodes.iter().filter(|n| matches!(n.kind, NodeKind::Deliver { .. })).map(|n| n.id).collect(); for r in self.runs.iter_mut().filter(|r| r.state == RunState::Running) { - // (A 交付 waiting for its bot's turn to end is done after the restart.) - if r.order.get(r.at).is_some_and(|n| delivering.contains(n)) { + // (Only a bot's question underway can be half done; a step not started yet, or a 交付 + // waiting for its bot, goes on after the restart.) + if !r.current().is_some_and(|s| s.state == StepState::Running && s.asked_at != 0) { continue; } if let Some(s) = r.order.get(r.at).copied().and_then(|n| r.steps.iter_mut().find(|s| s.node == n)) { @@ -1139,7 +1139,12 @@ mod tests { call(&mut wfs, json!({"action": "edit", "remove": [2]})).unwrap(); let t = call(&mut wfs, json!({"action": "edit", "steps": [{"prompt": "新的"}]})).unwrap(); assert!(t.touched[0] > 2, "{:?}", t.touched); - // Closed while running: interrupted; while waiting for the person: kept. + // Closed with nothing underway (a step not started yet): it goes on after the restart. + assert!(!wfs[0].mark_interrupted(2_500)); + // Closed while a bot was on its question: interrupted; while waiting for the person: kept. + let r = wfs[0].runs.last_mut().unwrap(); + r.steps[1].state = StepState::Running; + r.steps[1].asked_at = 2_100; assert!(wfs[0].mark_interrupted(3_000)); let r = wfs[0].runs.last().unwrap(); assert_eq!((r.state, r.why.as_str(), r.steps[1].state), (RunState::Failed, INTERRUPTED, StepState::Failed));