工作流:重開之後不會有卡住的執行(第三次獨立審查的 P1)。

- 以前:上一步結束、交付還沒開始的那幾秒(靠記憶體裡的 wf.advance,下一次 tick 才走),當機或強制結束後重開,那次執行永遠「進行中」,之後的排程都被擋、立即執行回「已經在跑了」。交付在等 Bot 時那一步被刪,也一樣卡到重開。
- 現在:每秒的 tick 也接手每一個能往下走的執行——還沒開始的那一步、等交付而 Bot 已經空下來的(那一步被刪了就照「這一步已經被刪掉了」收尾)。不再靠記憶體裡的佇列。
- 重開時只把「Bot 正在回答的問題」那種標成中斷(可能做到一半);還沒開始的、等交付的照樣接著走。

已驗證: cargo test 861 過(新:重開後清掉佇列,下一次 tick 照樣跑完;等交付時那一步被刪就收尾;沒在做的那次重開不標中斷、Bot 在回答的那次才標);clippy 沒有新的。
未驗證: 沒有真的讓 AnyBot 當掉再重開(靠單元測試)。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
This commit is contained in:
王性驊 2026-11-21 19:32:36 +08:00
parent 3c78dbf65f
commit db6039c501
2 changed files with 47 additions and 16 deletions

View File

@ -381,26 +381,31 @@ impl App {
for wf in std::mem::take(&mut self.wf.advance) { for wf in std::mem::take(&mut self.wf.advance) {
self.wf_advance(ctx, wf); self.wf_advance(ctx, wf);
} }
// 交付 that waited for its bot's turn to end: once the bot is free, it goes on. // Runs that can go on: a 交付 whose bot is free now (or whose step was deleted while it
let free: Vec<(u64, u64, u64)> = self // waited), and a step not started yet — after a restart nothing else moves those on.
let ready: Vec<(u64, Option<u64>)> = self
.state .state
.workflows .workflows
.iter() .iter()
.flat_map(|w| { .flat_map(|w| {
w.runs.iter().filter(|r| r.state == RunState::Running).filter_map(move |r| { w.runs.iter().filter(|r| r.state == RunState::Running).filter_map(move |r| match r.current() {
let s = r.current().filter(|s| s.state == StepState::Running)?; None => Some((w.id, None, 0)),
matches!(w.node(s.node).map(|n| &n.kind), Some(NodeKind::Deliver { .. })).then_some((w.id, r.id, s.node, s.bot)) 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)) .filter(|(_, waiting, bot)| waiting.is_none() || !self.streams.contains_key(bot))
.map(|(w, r, n, _)| (w, r, n)) .map(|(w, n, _)| (w, n))
.collect(); .collect();
for (wf, run, node) in free { for (wf, waiting) in ready {
self.wf_run_mut(wf, run, |r| { 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) { if let Some(s) = r.steps.iter_mut().find(|s| s.node == node) {
s.state = StepState::Waiting; s.state = StepState::Waiting;
} }
}); }
}
self.wf_advance(ctx, wf); self.wf_advance(ctx, wf);
} }
// A step whose bot was deleted while it worked never ends: the run stops, saying why. // 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); app.tick_workflows(&ctx);
assert_eq!(run(&app).state, RunState::Done); assert_eq!(run(&app).state, RunState::Done);
assert!(app.state.bot(bot).unwrap().messages.last().unwrap().text.starts_with("工作流「早報」的結果")); 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. // A bot deleted while on its step.
app.state.bots.push(app.state.bots[0].clone()); app.state.bots.push(app.state.bots[0].clone());
let other = app.state.bots.iter().map(|b| b.id).max().unwrap() + 1; let other = app.state.bots.iter().map(|b| b.id).max().unwrap() + 1;

View File

@ -264,10 +264,10 @@ impl Workflow {
/// the person stays: it can still be answered. /// the person stays: it can still be answered.
pub fn mark_interrupted(&mut self, now: i64) -> bool { pub fn mark_interrupted(&mut self, now: i64) -> bool {
let mut cut = false; let mut cut = false;
let delivering: Vec<u64> = 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) { 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.) // (Only a bot's question underway can be half done; a step not started yet, or a 交付
if r.order.get(r.at).is_some_and(|n| delivering.contains(n)) { // 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; continue;
} }
if let Some(s) = r.order.get(r.at).copied().and_then(|n| r.steps.iter_mut().find(|s| s.node == n)) { 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(); call(&mut wfs, json!({"action": "edit", "remove": [2]})).unwrap();
let t = call(&mut wfs, json!({"action": "edit", "steps": [{"prompt": "新的"}]})).unwrap(); let t = call(&mut wfs, json!({"action": "edit", "steps": [{"prompt": "新的"}]})).unwrap();
assert!(t.touched[0] > 2, "{:?}", t.touched); 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)); assert!(wfs[0].mark_interrupted(3_000));
let r = wfs[0].runs.last().unwrap(); let r = wfs[0].runs.last().unwrap();
assert_eq!((r.state, r.why.as_str(), r.steps[1].state), (RunState::Failed, INTERRUPTED, StepState::Failed)); assert_eq!((r.state, r.why.as_str(), r.steps[1].state), (RunState::Failed, INTERRUPTED, StepState::Failed));