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));