From 069a9cfd1f94686d2ad3d4531e13bb15b241ea81 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:11:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E7=A8=8B=E3=80=81=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=E6=B5=81=EF=BC=9A=E7=AC=AC=E4=BA=8C=E6=AC=A1=E7=8D=A8=E7=AB=8B?= =?UTF-8?q?=E5=AF=A9=E6=9F=A5=EF=BC=88=E6=96=B0=E9=96=8B=E8=84=88=E7=B5=A1?= =?UTF-8?q?=E5=AF=A9=20b4408cc=EF=BC=89=E6=89=BE=E5=88=B0=E7=9A=84?= =?UTF-8?q?=E5=95=8F=E9=A1=8C=E4=BF=AE=E5=A5=BD=E2=80=94=E2=80=94=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E6=B5=81=E7=9A=84=206=20=E5=80=8B=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E5=AE=83=E9=80=90=E6=A2=9D=E7=A2=BA=E8=AA=8D=E6=9C=89=E6=95=88?= =?UTF-8?q?=EF=BC=8C=E6=97=A5=E7=A8=8B=E7=9A=84=E6=97=A5=E6=9C=9F=E8=A8=88?= =?UTF-8?q?=E7=AE=97=EF=BC=88=E6=99=82=E5=8D=80=E3=80=81=E6=97=A5=E5=85=89?= =?UTF-8?q?=E7=AF=80=E7=B4=84=E3=80=81=E9=80=B1=E4=B8=80=E9=96=8B=E9=A0=AD?= =?UTF-8?q?=E3=80=81=E6=9C=88=E6=A0=BC=E3=80=81=E8=B7=A8=E5=B9=B4=EF=BC=89?= =?UTF-8?q?=E6=B2=92=E5=95=8F=E9=A1=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - P1:螢幕電腦上,日程卡片的「立即執行」會在螢幕自己身上開一次工作流執行,同步回大腦後那次永遠不會結束、之後的排程都被跳過。現在螢幕上工作流的卡片沒有「立即執行」(跟畫布一樣;例行任務的本來就轉給大腦)。 - 時間到了但 Bot 正忙的例行任務:以前從日程和即將執行消失,現在照原來的時間留著(圓點),即將執行排第一個。 - Bot 用工具刪掉正在跑的工作流:先停止那次(排在 Bot 後面的問題拿掉),跟選單的刪除一樣。 - 換畫布(日程卡片「打開工作流」、選單切換、刪除後換)時放掉上一個工作流的選取,不會在新的裡誤刪同編號的卡片。 - 「僅一次」跑過之後不再每秒存一次檔。 - 每小時的例行任務排滿月看法的六週(上限 200 → 1,100)。 - 還沒修(P2):等著交付的結果只在記憶體裡,Bot 那一輪結束前關掉 AnyBot 會不見。 已驗證: cargo test 860 過(新:日程測試加了「Bot 忙、時間過了還留著且即將執行第一」「每小時排滿六週」;工作流測試加了「工具刪除先停、排隊問題拿掉」「僅一次跑過後 tick 不存檔」);clippy 沒有新的。 未驗證: 螢幕電腦上的卡片(要一台登入同帳號的螢幕電腦);這幾條沒有再開 scratch 照步驟走(單元測試有走)。 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm --- docs/feature-map/schedule.md | 4 ++-- src/app/schedule_ui.rs | 9 +++++++-- src/app/workflow_run.rs | 33 +++++++++++++++++++++++++++++++-- src/app/workflow_ui.rs | 6 ++++-- src/schedule.rs | 30 +++++++++++++++++++++++++----- 5 files changed, 69 insertions(+), 13 deletions(-) diff --git a/docs/feature-map/schedule.md b/docs/feature-map/schedule.md index 67526ce..0671870 100644 --- a/docs/feature-map/schedule.md +++ b/docs/feature-map/schedule.md @@ -5,9 +5,9 @@ ## 子功能 - `schedule-views` 看法:右上「日/週/月」,左上「‹ 今天 ›」往前往後、回到今天,旁邊寫「2026年11月」「11月16日 – 22日」「11月21日 星期六」;右上角寫「時間以 Asia/Taipei 為準」(你的時區)。週和月從週一開始,今天那格是強調色的框。 -- `schedule-entries` 每一筆:跑過的照它的執行紀錄(✓ 完成、✗ 失敗或中斷、■ 停止、圓點執行中),還沒到的照排程算出來(圓點;暫停的變灰、帶 ‖)。週:每天一欄,每筆是時間、Bot 的頭像,下面是名稱。月:每天最多三筆「時間 名稱」,多的寫「還有 N 個」;點那一天換成那天的日看法。日:照時間一列列,寫「名稱 · 時間 · 怎麼重複 · 啟用中/已暫停/完成」。事件觸發的例行任務沒有預定的時間,只看得到跑過的。一個每 5 分鐘跑的,一段期間最多列 200 次。 +- `schedule-entries` 每一筆:跑過的照它的執行紀錄(✓ 完成、✗ 失敗或中斷、■ 停止、圓點執行中),還沒到的照排程算出來(圓點;暫停的變灰、帶 ‖)。週:每天一欄,每筆是時間、Bot 的頭像,下面是名稱。月:每天最多三筆「時間 名稱」,多的寫「還有 N 個」;點那一天換成那天的日看法。日:照時間一列列,寫「名稱 · 時間 · 怎麼重複 · 啟用中/已暫停/完成」。事件觸發的例行任務沒有預定的時間,只看得到跑過的。時間到了但 Bot 正忙的,照原來的時間留著(圓點),等它空下來跑。每小時跑的排滿月看法的六週;每 5 分鐘跑的大約只列得下四天。 - `schedule-coming` 即將執行:所有 Bot 接下來兩週內最先要跑的六筆(暫停的不算),每筆「名稱 · 明天 08:30 · 每天 08:30」和 Bot 的頭像;沒有時說怎麼設一個。 -- `schedule-card` 點任何一筆打開卡片:哪個 Bot、那一次的時間和狀態、怎麼重複、最近一次怎麼樣、要它做什麼;按鈕「打開聊天」「立即執行」「暫停/繼續」。工作流的是「打開工作流」「立即執行」。 +- `schedule-card` 點任何一筆打開卡片:哪個 Bot、那一次的時間和狀態、怎麼重複、最近一次怎麼樣、要它做什麼;按鈕「打開聊天」「立即執行」「暫停/繼續」。工作流的是「打開工作流」「立即執行」(螢幕電腦上沒有「立即執行」:工作流在大腦上跑)。 - `schedule-new` 「+ 新增例行任務」:回到那個 Bot 的聊天,輸入框先放好「每天早上 9 點幫我」,接著說要做什麼;Bot 用 `create_routine` 設好,日程上就出現。 ## 使用者怎麼到 diff --git a/src/app/schedule_ui.rs b/src/app/schedule_ui.rs index 1799fb5..2909557 100644 --- a/src/app/schedule_ui.rs +++ b/src/app/schedule_ui.rs @@ -454,7 +454,9 @@ impl App { } else if ui.add(primary_button("打開聊天", p)).clicked() { act = Some("chat"); } - if ui.add(secondary_button("立即執行")).clicked() { + // (A screen doesn't start a workflow itself: the brain runs them. A routine's + // run is passed on to the brain.) + if !(workflow.is_some() && self.mir.on) && ui.add(secondary_button("立即執行")).clicked() { act = Some("run"); } if let Some(r) = &routine { @@ -478,11 +480,14 @@ impl App { (What::Routine { bot, id }, "run" | "toggle") => self.routine_action(ctx, bot, Some((if a == "run" { "run" } else { "toggle" }, id))), (What::Workflow { id }, "open") => { self.sched.open = false; + if self.wf.current != id { + self.wf_let_go(); + } self.wf.current = id; self.wf.fitted = 0; self.wf_open(); } - (What::Workflow { id }, "run") => { + (What::Workflow { id }, "run") if !self.mir.on => { self.wf_start(ctx, id, "手動"); } _ => {} diff --git a/src/app/workflow_run.rs b/src/app/workflow_run.rs index 1296c5f..eb08f74 100644 --- a/src/app/workflow_run.rs +++ b/src/app/workflow_run.rs @@ -286,6 +286,15 @@ impl App { let tz = self.state.timezone.clone(); let showing = self.wf.open.then_some(self.wf.current); let who = crate::workflow::Who { me, bots: &bots, timezone: &tz }; + // (Deleting one that runs: its run stops first, as from the menu — a question still in + // line for a bot comes out of the line.) + if v["action"].as_str().is_some_and(|a| a.trim().eq_ignore_ascii_case("delete")) { + if let Ok(id) = crate::workflow::target(&self.state.workflows, &v, showing) { + if self.state.workflows.iter().any(|w| w.id == id && w.live_run().is_some()) { + self.wf_stop(id); + } + } + } let (text, ok) = match crate::workflow::tool(&mut self.state.workflows, &who, &v, showing, crate::routines::now()) { Err(e) => (e, false), Ok(told) => { @@ -309,6 +318,7 @@ impl App { } // Deleted: the canvas goes to another one (or back to the chat). if !self.state.workflows.iter().any(|w| w.id == id) && self.wf.current == id { + self.wf_let_go(); match self.state.workflows.first().map(|w| w.id) { Some(other) => self.wf.current = other, None => self.wf.open = false, @@ -364,8 +374,12 @@ impl App { }; match w.next_run { None => { - w.next_run = crate::routines::next_run(&s, w.created_at, now); - self.dirty = true; + // (A 僅一次 that ran has no time left: nothing changes, nothing to save.) + let next = crate::routines::next_run(&s, w.created_at, now); + if next.is_some() { + w.next_run = next; + self.dirty = true; + } } Some(t) if t <= now => { w.next_run = crate::routines::next_run(&s, w.created_at, now); @@ -570,6 +584,21 @@ mod tests { assert!(!ok && text.contains("已經在跑了"), "{text}"); let (_, ok) = call(&mut app, serde_json::json!({"action": "stop", "id": 2})); assert!(ok && app.state.workflows[1].runs.last().unwrap().state == RunState::Stopped); + // Deleted while it runs: the run stops first — its question in line comes out. + let (_, ok) = call(&mut app, serde_json::json!({"action": "run", "id": 2})); + assert!(ok && app.waiting_wakes.iter().any(|(_, m)| m.text.contains("第 1/2 步"))); + let (text, ok) = call(&mut app, serde_json::json!({"action": "delete", "id": 2})); + assert!(ok && text.contains("已刪除"), "{text}"); + assert!(!app.waiting_wakes.iter().any(|(_, m)| m.text.contains("第 1/2 步")), "the question went with it"); + // A 僅一次 that ran has no time left: the tick saves nothing. + let (_, ok) = call(&mut app, serde_json::json!({"action": "create", "name": "一次", "schedule": {"frequency": "daily", "time": "09:00", "once": true}, "steps": [{"prompt": "x"}]})); + assert!(ok); + let w = app.state.workflows.last_mut().unwrap(); + w.created_at -= 3 * 86_400; + w.next_run = None; + app.dirty = false; + app.tick_workflows(&ctx); + assert!(!app.dirty && app.state.workflows.last().unwrap().next_run.is_none()); } /// Stopping a run whose question still waits in line for a busy bot takes the question out diff --git a/src/app/workflow_ui.rs b/src/app/workflow_ui.rs index ad4bcb5..55b6a75 100644 --- a/src/app/workflow_ui.rs +++ b/src/app/workflow_ui.rs @@ -213,7 +213,7 @@ impl App { /// Another workflow on the canvas: nothing picked, typed, moved or drawn carries over (ids /// start from 1 in each one). - fn wf_let_go(&mut self) { + pub(super) fn wf_let_go(&mut self) { self.wf.selected = None; self.wf.selected_edge = None; self.wf.editing = None; @@ -379,9 +379,11 @@ impl App { ui.painter().text(r.rect.right_center() - vec2(12.0, 0.0), egui::Align2::RIGHT_CENTER, format!("{n} {}", t("個步驟")), egui::FontId::proportional(FONT_XS), p.text3); if r.clicked() { self.wf_keep_version(); + if self.wf.current != id { + self.wf_let_go(); + } self.wf.current = id; self.wf.fitted = 0; - self.wf.selected = None; ui.close(); } } diff --git a/src/schedule.rs b/src/schedule.rs index d60ff66..2a9b01e 100644 --- a/src/schedule.rs +++ b/src/schedule.rs @@ -41,9 +41,9 @@ pub struct Item { pub status: Status, } -/// At most this many times of one routine or workflow in one range (one every 5 minutes would -/// fill a month). -pub const MAX_PER_THING: usize = 200; +/// At most this many times of one routine or workflow in one range: an hourly one fills the six +/// weeks a month shows (1,008); one every 5 minutes fills about four days. +pub const MAX_PER_THING: usize = 1_100; /// Everything on the calendar from `from` up to `to` (seconds), oldest first: the runs that /// started then, and — from `now` on — the times still to come. @@ -67,6 +67,11 @@ pub fn items(state: &State, from: i64, to: i64, now: i64) -> Vec { } if r.trigger.is_none() { let status = if r.paused { Status::Paused } else { Status::Coming }; + // (A time that came while its bot was busy: it runs once the bot is free — shown + // at its time until then.) + if let Some(due) = r.next_run.filter(|t| !r.paused && *t < now && *t >= from && *t < to) { + out.push(Item { at: due, what, bot: b.id, title: r.title.clone(), when: when.clone(), status: Status::Coming }); + } for at in times(&r.schedule, r.created_at, from.max(now), to) { out.push(Item { at, what, bot: b.id, title: r.title.clone(), when: when.clone(), status }); } @@ -102,7 +107,8 @@ pub fn items(state: &State, from: i64, to: i64, now: i64) -> Vec { /// The next few times coming, across everything (即將執行): paused ones left out. pub fn coming(state: &State, now: i64, n: usize) -> Vec { - let mut out: Vec = items(state, now, now + 14 * 86_400, now).into_iter().filter(|i| i.status == Status::Coming).collect(); + // (From a day back: one whose time came while its bot was busy is still to run.) + let mut out: Vec = items(state, now - 86_400, now + 14 * 86_400, now).into_iter().filter(|i| i.status == Status::Coming).collect(); out.truncate(n); out } @@ -214,8 +220,22 @@ mod tests { ); assert!(next.iter().all(|i| i.status == Status::Coming && i.title != "週報")); assert!(next.windows(2).all(|w| w[0].at <= w[1].at)); + // A time that came while its bot was busy (next_run still before now): shown at its time + // and first in 即將執行 until it runs. + let due = now - 3 * 3600; + st.bots[0].routines[1].next_run = Some(due); + assert!(items(&st, from, to, now).iter().any(|i| i.title == "晨報" && i.at == due && i.status == Status::Coming)); + assert_eq!(coming(&st, now, 1)[0].at, due); + st.bots[0].routines[1].next_run = None; + // An hourly one fills the six weeks of a month. + let six_weeks = from + 42 * 86_400; + st.bots[0].routines.push(routine(6, "每小時", sched(serde_json::json!({"frequency": "hourly", "time": "00:15", "timezone": tz})), created)); + let hourly: Vec = items(&st, from, six_weeks, from).iter().filter(|i| i.title == "每小時").map(|i| i.at).collect(); + assert_eq!(hourly.len(), 42 * 24); + assert!(*hourly.last().unwrap() > six_weeks - 3600); + st.bots[0].routines.retain(|r| r.id != 6); // One every 5 minutes: capped. st.bots[0].routines.push(routine(5, "盯盤", sched(serde_json::json!({"frequency": "minutely", "interval": 5, "time": "00:00", "timezone": tz})), created)); - assert_eq!(items(&st, from, to, now).iter().filter(|i| i.title == "盯盤").count(), MAX_PER_THING); + assert_eq!(items(&st, from, from + 42 * 86_400, now).iter().filter(|i| i.title == "盯盤").count(), MAX_PER_THING); } }