工作流:等 Bot 講完才放進聊天的交付,改記在執行紀錄上(會存檔),重開 AnyBot 也不會不見。
- 以前是放在記憶體的一個清單(wf.deliveries),那次執行已經先標成完成:Bot 那一輪結束前關掉 AnyBot,結果就沒了(第二次審查的 P2)。 - 現在交付那一步停在「進行中」記在那次執行上;Bot 空下來時(每秒檢查)才放進聊天、再往下走。重開時這樣的執行不標成中斷,照樣交付。 - 等交付的那一步沒有問 Bot 問題(時間記 0):停止和「這一輪是不是這步的回答」都不拿 0 去比,避免碰到時間是 0 的舊訊息。 已驗證: cargo test 860 過(交付等那一輪、中間 mark_interrupted 不動它、Bot 空下來後交付並完成;原本的逐步測試改成先等那一輪結束);clippy 沒有新的。 未驗證: 沒有開 scratch 實際重開 AnyBot 看交付(截圖模式不走啟動收尾),靠單元測試。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
This commit is contained in:
parent
069a9cfd1f
commit
b62cf1d98e
|
|
@ -39,7 +39,7 @@
|
||||||
- Bot 步驟用那個 Bot 自己的聊天:它正忙(例如就是它叫開始的那一輪),第一步排在後面,等那一輪結束才問。
|
- Bot 步驟用那個 Bot 自己的聊天:它正忙(例如就是它叫開始的那一輪),第一步排在後面,等那一輪結束才問。
|
||||||
- 檢查的回答要以「通過」或「不通過:原因」開頭,看不出來算失敗。
|
- 檢查的回答要以「通過」或「不通過:原因」開頭,看不出來算失敗。
|
||||||
- 改正在跑的工作流:那次照開始時的步驟走,刪掉的步驟輪到時會停下來(新加的步驟不會用到刪掉的編號)。
|
- 改正在跑的工作流:那次照開始時的步驟走,刪掉的步驟輪到時會停下來(新加的步驟不會用到刪掉的編號)。
|
||||||
- AnyBot 關掉時正在跑的那次:再開時標成失敗「AnyBot 在執行中被關掉,這一步可能做到一半」;停在等你確認的那次留著。還在等你確認時排程到了,不會再開一次。
|
- AnyBot 關掉時正在跑的那次:再開時標成失敗「AnyBot 在執行中被關掉,這一步可能做到一半」;停在等你確認的那次留著,等著交付的那次重開後照樣交付。還在等你確認時排程到了,不會再開一次。
|
||||||
- 步驟的 Bot 在做的時候被刪掉:那次停下來,寫「這一步的 Bot 已經不在了」。
|
- 步驟的 Bot 在做的時候被刪掉:那次停下來,寫「這一步的 Bot 已經不在了」。
|
||||||
- 螢幕電腦只能看:工作流在大腦上改和跑。
|
- 螢幕電腦只能看:工作流在大腦上改和跑。
|
||||||
- (驗證)有真資料的桌面截圖前 500 格左右是啟動畫面:`ANYBOT_SHOT_FRAMES` 至少 520。
|
- (驗證)有真資料的桌面截圖前 500 格左右是啟動畫面:`ANYBOT_SHOT_FRAMES` 至少 520。
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,8 @@ impl App {
|
||||||
// working on the question itself is stopped.
|
// 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 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;
|
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();
|
let before = self.waiting_wakes.len();
|
||||||
self.waiting_wakes.retain(|(b, m)| !(*b == bot && m.at == at));
|
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));
|
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;
|
return;
|
||||||
}
|
}
|
||||||
NodeKind::Deliver { bot } => {
|
NodeKind::Deliver { bot } => {
|
||||||
let selected = self.state.selected;
|
if self.state.bot(bot).is_none() {
|
||||||
let Some(b) = self.state.bot_mut(bot) else {
|
|
||||||
self.wf_fail(wf, run_id, node_id, "要交付的 Bot 已經不在了");
|
self.wf_fail(wf, run_id, node_id, "要交付的 Bot 已經不在了");
|
||||||
return;
|
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
|
// (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) {
|
if self.streams.contains_key(&bot) {
|
||||||
self.wf.deliveries.push((bot, msg));
|
self.wf_run_mut(wf, run_id, |r| {
|
||||||
} else {
|
if let Some(s) = r.steps.iter_mut().find(|s| s.node == node_id) {
|
||||||
b.messages.push(msg);
|
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 {
|
if bot != selected {
|
||||||
b.unread = true;
|
b.unread = true;
|
||||||
}
|
}
|
||||||
self.save_now = true;
|
|
||||||
}
|
}
|
||||||
|
self.save_now = true;
|
||||||
self.wf_done(wf, run_id, node_id, last);
|
self.wf_done(wf, run_id, node_id, last);
|
||||||
}
|
}
|
||||||
NodeKind::Notify { text } => {
|
NodeKind::Notify { text } => {
|
||||||
|
|
@ -235,7 +244,7 @@ impl App {
|
||||||
// that started the run, say — the question waits in line, and the turn that
|
// that started the run, say — the question waits in line, and the turn that
|
||||||
// ends first isn't the answer.)
|
// 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));
|
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 { .. }));
|
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));
|
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) {
|
for wf in std::mem::take(&mut self.wf.advance) {
|
||||||
self.wf_advance(ctx, wf);
|
self.wf_advance(ctx, wf);
|
||||||
}
|
}
|
||||||
// 交付 that waited for a bot's turn to end.
|
// 交付 that waited for its bot's turn to end: once the bot is free, it goes on.
|
||||||
let selected = self.state.selected;
|
let free: Vec<(u64, u64, u64)> = self
|
||||||
for (bot, msg) in std::mem::take(&mut self.wf.deliveries) {
|
.state
|
||||||
if self.streams.contains_key(&bot) {
|
.workflows
|
||||||
self.wf.deliveries.push((bot, msg));
|
.iter()
|
||||||
} else if let Some(b) = self.state.bot_mut(bot) {
|
.flat_map(|w| {
|
||||||
b.messages.push(msg);
|
w.runs.iter().filter(|r| r.state == RunState::Running).filter_map(move |r| {
|
||||||
if bot != selected {
|
let s = r.current().filter(|s| s.state == StepState::Running)?;
|
||||||
b.unread = true;
|
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.
|
// A step whose bot was deleted while it worked never ends: the run stops, saying why.
|
||||||
let gone: Vec<(u64, u64, u64)> = self
|
let gone: Vec<(u64, u64, u64)> = self
|
||||||
|
|
@ -479,13 +497,14 @@ mod tests {
|
||||||
let asked = answer(app, bot, "通過");
|
let asked = answer(app, bot, "通過");
|
||||||
assert!(asked.contains("至少三則") && asked.contains("三則新聞:A、B、C"), "{asked}");
|
assert!(asked.contains("至少三則") && asked.contains("三則新聞:A、B、C"), "{asked}");
|
||||||
app.tick_workflows(&ctx);
|
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);
|
let r = run(app);
|
||||||
assert_eq!(r.state, RunState::Done, "{r:?}");
|
assert_eq!(r.state, RunState::Done, "{r:?}");
|
||||||
assert!(r.steps.iter().all(|s| s.state == StepState::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");
|
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();
|
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", "工作流「早報」"));
|
assert_eq!((last.text.as_str(), last.via.as_str()), ("工作流「早報」的結果:\n\n三則新聞:A、B、C", "工作流「早報」"));
|
||||||
// What was run is kept as a version.
|
// What was run is kept as a version.
|
||||||
|
|
@ -625,10 +644,13 @@ mod tests {
|
||||||
let n = app.state.bot(bot).unwrap().messages.len();
|
let n = app.state.bot(bot).unwrap().messages.len();
|
||||||
answer(&mut app, bot, "三則新聞");
|
answer(&mut app, bot, "三則新聞");
|
||||||
app.tick_workflows(&ctx);
|
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");
|
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.streams.remove(&bot);
|
||||||
app.tick_workflows(&ctx);
|
app.tick_workflows(&ctx);
|
||||||
|
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("工作流「早報」的結果"));
|
||||||
// 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());
|
||||||
|
|
|
||||||
|
|
@ -68,8 +68,6 @@ pub(super) struct WfUi {
|
||||||
pub flash: Option<(u64, Vec<u64>, Instant)>,
|
pub flash: Option<(u64, Vec<u64>, Instant)>,
|
||||||
/// A bot changed it: show what changed if it is out of view.
|
/// A bot changed it: show what changed if it is out of view.
|
||||||
pub reveal: bool,
|
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.
|
/// How long what a bot changed stays lit on the canvas.
|
||||||
|
|
|
||||||
|
|
@ -264,7 +264,12 @@ 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.)
|
||||||
|
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)) {
|
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;
|
s.state = StepState::Failed;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue