diff --git a/docs/feature-map/workflow.md b/docs/feature-map/workflow.md index 4c6e61a..1f2f2d4 100644 --- a/docs/feature-map/workflow.md +++ b/docs/feature-map/workflow.md @@ -39,7 +39,7 @@ - Bot 步驟用那個 Bot 自己的聊天:它正忙(例如就是它叫開始的那一輪),第一步排在後面,等那一輪結束才問。 - 檢查的回答要以「通過」或「不通過:原因」開頭,看不出來算失敗。 - 改正在跑的工作流:那次照開始時的步驟走,刪掉的步驟輪到時會停下來(新加的步驟不會用到刪掉的編號)。 -- AnyBot 關掉時正在跑的那次:再開時標成失敗「AnyBot 在執行中被關掉,這一步可能做到一半」;停在等你確認的那次留著。還在等你確認時排程到了,不會再開一次。 +- AnyBot 關掉時正在跑的那次:再開時標成失敗「AnyBot 在執行中被關掉,這一步可能做到一半」;停在等你確認的那次留著,等著交付的那次重開後照樣交付。還在等你確認時排程到了,不會再開一次。 - 步驟的 Bot 在做的時候被刪掉:那次停下來,寫「這一步的 Bot 已經不在了」。 - 螢幕電腦只能看:工作流在大腦上改和跑。 - (驗證)有真資料的桌面截圖前 500 格左右是啟動畫面:`ANYBOT_SHOT_FRAMES` 至少 520。 diff --git a/src/app/workflow_run.rs b/src/app/workflow_run.rs index eb08f74..5e03308 100644 --- a/src/app/workflow_run.rs +++ b/src/app/workflow_run.rs @@ -37,7 +37,8 @@ impl App { // working on the question itself is stopped. let on = self.state.workflows.iter().find(|w| w.id == wf).and_then(|w| w.runs.iter().rev().find(|r| r.live())).and_then(|r| r.current().filter(|s| s.state == StepState::Running && s.bot != 0).map(|s| (s.bot, s.asked_at))); let mut busy = None; - if let Some((bot, at)) = on { + // (A 交付 waiting for its bot asked nothing: its time is 0.) + if let Some((bot, at)) = on.filter(|(_, at)| *at != 0) { let before = self.waiting_wakes.len(); self.waiting_wakes.retain(|(b, m)| !(*b == bot && m.at == at)); let in_chat = self.state.bot(bot).is_some_and(|b| b.messages.iter().any(|m| m.role == Role::User && m.at == at)); @@ -144,24 +145,32 @@ impl App { return; } NodeKind::Deliver { bot } => { - let selected = self.state.selected; - let Some(b) = self.state.bot_mut(bot) else { + if self.state.bot(bot).is_none() { self.wf_fail(wf, run_id, node_id, "要交付的 Bot 已經不在了"); return; - }; - let text = if last.trim().is_empty() { format!("工作流「{name}」跑完了(前面的步驟沒有結果)。") } else { format!("工作流「{name}」的結果:\n\n{last}") }; - let msg = Message { at: crate::model::now_ms(), ..Message::assistant(&text, &format!("工作流「{name}」")) }; + } // (A bot answering right now: its reply streams into its last message — the - // result goes in once that turn is over, not in the middle of it.) + // step waits, on the run's record (saved: a restart still delivers it), and + // the result goes in once that turn is over.) if self.streams.contains_key(&bot) { - self.wf.deliveries.push((bot, msg)); - } else { - b.messages.push(msg); + self.wf_run_mut(wf, run_id, |r| { + if let Some(s) = r.steps.iter_mut().find(|s| s.node == node_id) { + s.state = StepState::Running; + s.bot = bot; + s.asked_at = 0; + } + }); + return; + } + let selected = self.state.selected; + let text = if last.trim().is_empty() { format!("工作流「{name}」跑完了(前面的步驟沒有結果)。") } else { format!("工作流「{name}」的結果:\n\n{last}") }; + if let Some(b) = self.state.bot_mut(bot) { + b.messages.push(Message { at: crate::model::now_ms(), ..Message::assistant(&text, &format!("工作流「{name}」")) }); if bot != selected { b.unread = true; } - self.save_now = true; } + self.save_now = true; self.wf_done(wf, run_id, node_id, last); } NodeKind::Notify { text } => { @@ -235,7 +244,7 @@ impl App { // that started the run, say — the question waits in line, and the turn that // ends first isn't the answer.) let asked = |at: i64| self.state.bot(bot).is_some_and(|b| b.messages.iter().any(|m| m.role == Role::User && m.at == at)); - if let Some(s) = r.current().filter(|s| s.state == StepState::Running && s.bot == bot && asked(s.asked_at)) { + if let Some(s) = r.current().filter(|s| s.state == StepState::Running && s.bot == bot && s.asked_at != 0 && asked(s.asked_at)) { let check = matches!(w.node(s.node).map(|n| &n.kind), Some(NodeKind::Check { .. })); found.push((w.id, r.id, s.node, check, s.asked_at)); } @@ -336,18 +345,27 @@ impl App { for wf in std::mem::take(&mut self.wf.advance) { self.wf_advance(ctx, wf); } - // 交付 that waited for a bot's turn to end. - let selected = self.state.selected; - for (bot, msg) in std::mem::take(&mut self.wf.deliveries) { - if self.streams.contains_key(&bot) { - self.wf.deliveries.push((bot, msg)); - } else if let Some(b) = self.state.bot_mut(bot) { - b.messages.push(msg); - if bot != selected { - b.unread = true; + // 交付 that waited for its bot's turn to end: once the bot is free, it goes on. + let free: Vec<(u64, u64, u64)> = 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)) + }) + }) + .filter(|(_, _, _, bot)| !self.streams.contains_key(bot)) + .map(|(w, r, n, _)| (w, r, 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; } - self.save_now = true; - } + }); + self.wf_advance(ctx, wf); } // A step whose bot was deleted while it worked never ends: the run stops, saying why. let gone: Vec<(u64, u64, u64)> = self @@ -479,13 +497,14 @@ mod tests { let asked = answer(app, bot, "通過"); assert!(asked.contains("至少三則") && asked.contains("三則新聞:A、B、C"), "{asked}"); app.tick_workflows(&ctx); + // (The bot is still answering in this test: the 交付 waits for that turn to be over.) + assert_eq!(run(app).current().map(|s| (s.node, s.state)), Some((4, StepState::Running))); + app.streams.remove(&bot); + app.tick_workflows(&ctx); let r = run(app); assert_eq!(r.state, RunState::Done, "{r:?}"); assert!(r.steps.iter().all(|s| s.state == StepState::Done), "{r:?}"); assert_eq!(r.step_of(3).map(|s| s.output.as_str()), Some("三則新聞:A、B、C"), "a check hands on what it checked"); - // (The bot is still answering in this test: the result goes in once that turn is over.) - app.streams.remove(&bot); - app.tick_workflows(&ctx); let last = app.state.bot(bot).unwrap().messages.last().unwrap(); assert_eq!((last.text.as_str(), last.via.as_str()), ("工作流「早報」的結果:\n\n三則新聞:A、B、C", "工作流「早報」")); // What was run is kept as a version. @@ -625,10 +644,13 @@ mod tests { let n = app.state.bot(bot).unwrap().messages.len(); answer(&mut app, bot, "三則新聞"); app.tick_workflows(&ctx); - assert_eq!(run(&app).state, RunState::Done); + assert_eq!(run(&app).state, RunState::Running, "the 交付 waits on the run (saved with it)"); assert_eq!(app.state.bot(bot).unwrap().messages.len(), n + 2, "the question and the answer only: the result waits"); + // A restart meanwhile doesn't lose it: the waiting 交付 isn't marked interrupted. + assert!(!app.state.workflows[0].mark_interrupted(1)); app.streams.remove(&bot); app.tick_workflows(&ctx); + assert_eq!(run(&app).state, RunState::Done); assert!(app.state.bot(bot).unwrap().messages.last().unwrap().text.starts_with("工作流「早報」的結果")); // A bot deleted while on its step. app.state.bots.push(app.state.bots[0].clone()); diff --git a/src/app/workflow_ui.rs b/src/app/workflow_ui.rs index 55b6a75..96b9050 100644 --- a/src/app/workflow_ui.rs +++ b/src/app/workflow_ui.rs @@ -68,8 +68,6 @@ pub(super) struct WfUi { pub flash: Option<(u64, Vec, Instant)>, /// A bot changed it: show what changed if it is out of view. pub reveal: bool, - /// 交付 waiting for its bot's turn to end (its reply streams into its last message). - pub deliveries: Vec<(u64, Message)>, } /// How long what a bot changed stays lit on the canvas. diff --git a/src/workflow.rs b/src/workflow.rs index 60f8787..a2e05b7 100644 --- a/src/workflow.rs +++ b/src/workflow.rs @@ -264,7 +264,12 @@ 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)) { + continue; + } if let Some(s) = r.order.get(r.at).copied().and_then(|n| r.steps.iter_mut().find(|s| s.node == n)) { s.state = StepState::Failed; }