fix: 追進度第二輪審查——參數不是物件不再讓 App 當掉、SSH 主機可以追(先問)

- P1:follow_up 的參數不是 JSON 物件時直接回錯誤(原本會在畫面執行緒 panic,大腦也會倒)
- P1:可以指名一台 SSH 主機跑檢查(一定先問使用者);使用者自己的電腦(這台、帳號裡其他電腦)仍不行
- ssh 連不上(結束代碼 255)、指令找不到(126、127)算檢查失敗,三次就叫 Bot 回來,不必等 15 分鐘
- Bot 自己的電腦記成空字串(「自己的」),Bot 改名不影響
- 別人傳來的訊息那一輪連列出、停止追進度也不行

已驗證: cargo test 756 通過、clippy 無新警告;verify-anybot goal.md「追進度」重跑(好了叫醒、追完歸零)、指定這台電腦被拒;ctl down 無殘留
未驗證: 真的 SSH 主機上的檢查(要真的機器與金鑰);本 commit 沒有再做獨立審查;新 App 工具與審批需實機測試+人類簽名,不發佈

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
王性驊 2026-11-19 23:56:27 +08:00
parent f1f43420d2
commit 8f514f43dd
4 changed files with 52 additions and 32 deletions

View File

@ -753,7 +753,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。
- **追進度(follow_up)**:Bot 啟動要等很久的事(下載、算圖、訓練、部署、別台機器上的工作)後,登記一個很快就跑完的檢查指令(du、tail log、curl 狀態、SSH 一行都可以)、done_when(輸出出現什麼算好了,/…/ 是正規表示式)、可選的 fail_when、then(好了要接著做什麼),就可以結束這一輪。
- 大腦每 every_secs(15–3600,預設 60)秒在那台電腦上跑一次檢查,不叫模型;輸出有變、而且距離上一行超過 10 分鐘,聊天裡記一行「[追進度 #N]「…」還在進行(追了 M 分鐘):最後一行」。
- 好了、出現 fail_when、輸出 15 分鐘(或十次檢查)沒變、超過 give_up_after_mins(預設 180,最多 1440)、或檢查指令連續 3 次失敗,就叫醒 Bot:「[追進度 #N]「…」好了/看起來失敗了/可能卡住了…」,附最後的輸出和 then,告訴它接著怎麼做。
- 檢查只在 Bot 自己的電腦上跑(它會無人看著一再執行,所以不在使用者的電腦上;別台機器從那裡用 SSH 或 curl 看),登記時就把電腦寫死。那個檢查指令照 shell 的規則和使用者的 PreToolUse hook 決定要不要問(問的話卡片寫明之後會每 N 秒自動執行),被安全規則或 hook 擋的不登記;之後不再一次次問。別人傳來的訊息(訊息管道、團隊成員)那一輪不能登記。一次檢查最多跑 every_secs(10–60 秒),沒跑完就停掉、算一次失敗;結束代碼不是 0 但有輸出的照樣比對。list 看、stop 停;一個 Bot 同時最多 8 個。存在 Bot 身上,重開 AnyBot 照樣追;只有大腦跑,螢幕電腦不跑。
- 檢查在 Bot 自己的電腦上跑,或在 Bot 指名的一台 SSH 主機上跑(那是使用者的機器:一定先問);不在使用者自己的電腦(這台、帳號裡的其他電腦)上跑——它會無人看著一再執行;要看那裡的進度,從 Bot 的電腦用 SSH 或 curl 看。登記時就把電腦寫死(Bot 自己的記成「自己的」,改名也不影響)。參數不是 JSON 物件的不登記。ssh 連不上(結束代碼 255)、指令找不到(126、127)算檢查失敗。那個檢查指令照 shell 的規則和使用者的 PreToolUse hook 決定要不要問(問的話卡片寫明之後會每 N 秒自動執行),被安全規則或 hook 擋的不登記;之後不再一次次問。別人傳來的訊息(訊息管道、團隊成員)那一輪不能登記。一次檢查最多跑 every_secs(10–60 秒),沒跑完就停掉、算一次失敗;結束代碼不是 0 但有輸出的照樣比對。list 看、stop 停;一個 Bot 同時最多 8 個。存在 Bot 身上,重開 AnyBot 照樣追;只有大腦跑,螢幕電腦不跑。
- 系統指示要求:要等的事不要 sleep 卡在指令裡、也不要說「我去盯」就結束;「說了就做」的提醒也會叫它用 follow_up。起因:Bot 說「我去盯它跑」之後沒有人叫它回來,使用者只好一直問「好了沒」。
- **主動回報**(系統指示):事情做到一個段落、或追進度叫它回來時,主動用一兩句話回報做到哪、接下來做什麼;發現問題先自己處理。
- **回合結束有三種**:

View File

@ -10,7 +10,7 @@
- `goal-next` 下一輪:沒達成時,系統自己送出一則「[目標]還沒達成:理由」開始下一輪。
- `goal-brake` 煞車:做滿 20 輪先停下來問;連續 3 輪沒有用任何工具(只是在跟評審說話)就停;連續兩輪評審說差的是同一件事(字稍微不同也算)就停,看起來卡住了;Bot 在等使用者回答時停;評審沒有回應時停。使用者傳任何一句話就接著做(滿 20 輪的話再給 20 輪)。
- `goal-done` 達成或做不到:聊天裡留一行結果(花了幾輪),目標清掉。達成、做不到或停下來時,使用者不在電腦前(而且通知開著)會跳通知,手機也收得到。
- `goal-followup` 追進度:Bot 啟動要等很久的事(下載、算圖、訓練、別台機器上的工作)後,登記一個看進度的指令和「看到什麼算好」就結束這一輪,不用你問「好了沒」。大腦定期在那台電腦上跑那個指令(不叫模型):有進展時聊天裡報一行「[追進度 #N]「…」還在進行…」(最多每 10 分鐘一行);好了、失敗、太久沒變化(約 15 分鐘)、超過時間(預設 3 小時)或指令一直失敗,就叫 Bot 回來接著做。檢查只在 Bot 自己的電腦上跑(別台用 SSH 或 curl 從那裡看),登記時照 `shell` 的規則和你的 hook 問一次,之後不再一次次問;別人傳來的訊息不能登記。重開 AnyBot 照樣繼續追。
- `goal-followup` 追進度:Bot 啟動要等很久的事(下載、算圖、訓練、別台機器上的工作)後,登記一個看進度的指令和「看到什麼算好」就結束這一輪,不用你問「好了沒」。大腦定期在那台電腦上跑那個指令(不叫模型):有進展時聊天裡報一行「[追進度 #N]「…」還在進行…」(最多每 10 分鐘一行);好了、失敗、太久沒變化(約 15 分鐘)、超過時間(預設 3 小時)或指令一直失敗,就叫 Bot 回來接著做。檢查在 Bot 自己的電腦上跑,或指名一台 SSH 主機(一定先問你);不在你自己的電腦上跑(要看就從 Bot 的電腦用 SSH 或 curl),登記時照 `shell` 的規則和你的 hook 問一次,之後不再一次次問;別人傳來的訊息不能登記。重開 AnyBot 照樣繼續追。
- `goal-restart` 重開:目標存在 Bot 身上,重開 AnyBot 還在;進行中的會先暫停(「AnyBot 重新啟動過」),等使用者一句話才繼續。
## 使用者怎麼到

View File

@ -16,24 +16,21 @@ impl App {
/// chosen computer changing later doesn't move it. Then: as `shell` there would decide it,
/// and the person's PreToolUse hooks; a turn from someone other than the person can't.
pub(super) fn followup_ask(&self, bot_id: u64, args: &str) -> Result<(Option<String>, String), String> {
// (Someone else's turn may not look at or stop the person's follow-ups either.)
if self.strangers.contains(&bot_id) {
return Err("這則訊息是別人傳來的(訊息管道或團隊成員):不能用追進度。".into());
}
let mut v: serde_json::Value = serde_json::from_str(args).unwrap_or(serde_json::Value::Null);
if !v.is_object() {
return Err("follow_up 的參數要是一個 JSON 物件".into());
}
if followup::action_of(&v) != followup::Action::Register {
return Ok((None, args.to_string()));
}
if self.strangers.contains(&bot_id) {
return Err("這則訊息是別人傳來的(訊息管道或團隊成員):不能登記追進度。".into());
}
let Some(b) = self.state.bot(bot_id) else { return Err("找不到這個 Bot".into()) };
let targets = self.targets_for(b);
let roles = self.roles_for(b, &targets);
let own = roles.own.clone();
let named = v["computer"].as_str().unwrap_or("").trim().to_string();
if !named.is_empty() && named != own {
return Err(format!("追進度的檢查只在你自己的電腦「{own}」上跑(它會無人看著一再執行)。要看別台的進度,從「{own}」用 SSH 或 curl 看,computer 留空。"));
}
let Some(target) = targets.into_iter().find(|t| t.name == own) else {
return Err(format!("你自己的電腦「{own}」現在不能用,沒辦法登記追進度。"));
};
let (target, name) = self.followup_target(b, &named)?;
let ssh = matches!(target.place, tools::Place::Ssh { .. });
let check = v["check"].as_str().unwrap_or("").trim().to_string();
let every = v["every_secs"].as_u64().unwrap_or(followup::DEFAULT_EVERY_SECS).clamp(followup::MIN_EVERY_SECS, followup::MAX_EVERY_SECS);
let shell_args = serde_json::json!({"command": check}).to_string();
@ -48,25 +45,36 @@ impl App {
return Err(format!("被 hook 擋下:{why}"));
}
}
v["computer"] = serde_json::json!(own);
// Written in: an SSH machine by its name; the bot's own computer as empty (「its own」,
// whatever the bot is called later).
v["computer"] = serde_json::json!(if ssh { name.clone() } else { String::new() });
let args = v.to_string();
let card = |reason: &str| format!("追進度:之後每 {every} 秒會在「{name}」自動執行這個檢查(不再一次次問你),直到好了或停掉。{reason}");
match tools::decide_on("shell", &shell_args, &target, &b.allow_rules) {
// (Another machine is the person's: asked every time it is registered.)
tools::Decision::Allow if ssh => Ok((Some(card("")), args)),
tools::Decision::Allow => Ok((None, args)),
tools::Decision::Ask { reason, .. } => Ok((Some(format!("追進度:之後每 {every} 秒會在「{own}」自動執行這個檢查(不再一次次問你),直到好了或停掉。\n{reason}")), args)),
tools::Decision::Ask { reason, .. } => Ok((Some(card(&format!("\n{reason}"))), args)),
tools::Decision::Deny(why) => Err(format!("這個檢查指令不能用:{why}")),
}
}
/// The computer a follow-up runs on (the one written in when it was registered).
/// The computer a follow-up runs on: the bot's own (empty, or its name), or an SSH machine
/// it names — never the person's computers (this one, the account's others), where `shell`
/// has more doors than one decision (在這台電腦上執行, its admin lock, the steps asked every
/// time) and the check would run again and again with nobody watching.
fn followup_target(&self, b: &Bot, named: &str) -> Result<(tools::Target, String), String> {
let targets = self.targets_for(b);
let roles = self.roles_for(b, &targets);
// (One registered before the computer was written in: the bot's own.)
let name = if named.trim().is_empty() { roles.own.clone() } else { named.trim().to_string() };
if name != roles.own {
return Err(format!("追進度只在 Bot 自己的電腦上跑,「{name}」不是"));
let own = roles.own.clone();
let name = if named.trim().is_empty() { own.clone() } else { named.trim().to_string() };
let Some(t) = targets.into_iter().find(|t| t.name == name) else {
return Err(format!("「{name}」現在連不到"));
};
if name != own && !matches!(t.place, tools::Place::Ssh { .. }) {
return Err(format!("追進度的檢查只在你自己的電腦「{own}」或 SSH 主機上跑(它會無人看著一再執行),「{name}」是使用者的電腦。要看那裡的進度,從「{own}」用 SSH 或 curl 看,computer 留空。"));
}
targets.into_iter().find(|t| t.name == name).map(|t| (t, name.clone())).ok_or_else(|| format!("「{name}」現在連不到"))
Ok((t, name))
}
pub(super) fn followup_tick(&mut self, ctx: &egui::Context) {
@ -138,19 +146,21 @@ mod tests {
let ctx = egui::Context::default();
let mut app = App::new(&ctx, Some(std::env::temp_dir().join("anybot-followup-ask.png")));
let id = app.state.bots[0].id;
let b = app.state.bot(id).unwrap().clone();
let own = app.roles_for(&b, &app.targets_for(&b)).own.clone();
let call = |extra: &str| format!(r#"{{"check":"ls","done_when":"x"{extra}}}"#);
match app.followup_ask(id, &call(r#","stop":null"#)) {
Ok((_, args)) => assert_eq!(serde_json::from_str::<serde_json::Value>(&args).unwrap()["computer"], own.as_str(), "written in"),
Err(e) => assert!(e.contains("不能用") || e.contains("現在不能用"), "{e}"),
Ok((_, args)) => assert_eq!(serde_json::from_str::<serde_json::Value>(&args).unwrap()["computer"], "", "its own computer, as 「its own」"),
Err(e) => assert!(e.contains("連不到"), "{e}"),
}
let other = app.followup_ask(id, &call(r#","computer":"王性驊的Mac mini""#)).unwrap_err();
assert!(other.contains("只在你自己的電腦"), "{other}");
app.strangers.insert(id);
assert!(app.followup_ask(id, &call("")).unwrap_err().contains("別人傳來的"));
let this = crate::model::THIS_COMPUTER_NAME;
let other = app.followup_ask(id, &call(&format!(r#","computer":"{this}""#))).unwrap_err();
assert!(other.contains("使用者的電腦") || other.contains("連不到"), "{other}");
// Not an object (a model that sent its arguments as a string): refused, never a panic.
assert!(app.followup_ask(id, r#""{\"check\":\"ls\"}""#).unwrap_err().contains("JSON 物件"));
// Listing and stopping ask nothing (and change nothing to ask about).
assert!(app.followup_ask(id, r#"{"list":true}"#).unwrap().0.is_none());
app.strangers.insert(id);
assert!(app.followup_ask(id, &call("")).unwrap_err().contains("別人傳來的"));
assert!(app.followup_ask(id, r#"{"list":true}"#).is_err(), "someone else can't look at them either");
}
/// 追進度 end to end on the brain: registered by the tool, a check that moved on is one line

View File

@ -98,7 +98,7 @@ pub fn spec() -> Value {
"then": {"type": "string", "description": "What you will do once it is done (given back to you when woken). Optional."},
"every_secs": {"type": "integer", "description": "Seconds between checks (15–3600, default 60)."},
"give_up_after_mins": {"type": "integer", "description": "Minutes before it gives up and wakes you (default 180, at most 1440)."},
"computer": {"type": "string", "description": "Leave empty: checks run on your own computer (they repeat with nobody watching, so never on the person's). Reach other machines from there — an SSH one-liner or curl as the check."},
"computer": {"type": "string", "description": "Leave empty: checks run on your own computer. Or an SSH machine as list_computers names it (asked once). Never the person's own computers — the check repeats with nobody watching; reach those from your computer with SSH or curl."},
"stop": {"type": "integer", "description": "End the follow-up with this number instead."},
"list": {"type": "boolean", "description": "Show the follow-ups running now instead."}}}})
}
@ -225,7 +225,7 @@ pub fn judge(f: &mut FollowUp, got: Result<String, String>, now_ms: i64) -> Verd
}
// The command ran and said something but ended with an error code (`test -f x && echo
// DONE` before it is done): what it said is read like any output, not a broken check.
Err(e) if e.contains("結束代碼") => judge(f, Ok(e), now_ms),
Err(e) if e.contains("結束代碼") && !could_not_run(&e) => judge(f, Ok(e), now_ms),
Err(e) => {
f.errors += 1;
if f.errors >= ERRORS_IN_A_ROW {
@ -239,6 +239,13 @@ pub fn judge(f: &mut FollowUp, got: Result<String, String>, now_ms: i64) -> Verd
}
}
/// An exit code that says the check never got to look (126/127: not runnable or not found;
/// 255: ssh couldn't connect): a failed check, not 「not done yet」.
fn could_not_run(e: &str) -> bool {
let code: String = e.split("結束代碼").nth(1).unwrap_or("").trim_start().chars().take_while(char::is_ascii_digit).collect();
matches!(code.as_str(), "126" | "127" | "255")
}
/// When this AnyBot started (ms).
fn started_here() -> i64 {
static AT: OnceLock<i64> = OnceLock::new();
@ -447,6 +454,9 @@ mod tests {
assert_eq!(judge(&mut f, Err("[box] 結束代碼 1\n還沒好".into()), 60_000), Verdict::Wait);
assert_eq!(f.errors, 0);
assert_eq!(judge(&mut f, Err("[box] 結束代碼 0\nDL_DONE".into()), 120_000), Verdict::Done);
let mut f = fu(0);
assert_eq!(judge(&mut f, Err("[box] 結束代碼 255\nssh: connect to host gpu port 22: Connection refused".into()), 60_000), Verdict::Wait);
assert_eq!(f.errors, 1, "ssh couldn't connect: a failed check");
assert_eq!(moved_to_background("[box] 指令跑了超過 60 秒,已移到背景繼續執行(背景指令 #12)。用 read_terminal…"), Some(12));
assert_eq!(moved_to_background("12G /m"), None);
}