AnyBot/src/evidence.rs

85 lines
3.7 KiB
Rust
Raw Normal View History

feat: 借鏡 pstack/Grok Bot——例行任務執行紀錄、完成看證據、教訓分流、園丁報告、減少操作步驟、網站地圖 1. 例行任務執行紀錄:每次執行記開始/結束、完成/失敗/被停止/中斷、步數與失敗步數、原因,保留最近 20 次(Grok Bot 官方規格);上次失敗或中斷的原因帶進下一次;連續失敗 2 次自動暫停並說明原因;設定頁、手機詳細頁、list_routines 都看得到,沒有紀錄時說「查不到」不說「沒跑過」 2. 完成看證據:evidence.rs 從步驟算出實際成功送出幾次,寫進例行任務紀錄;/goal 評審的摘錄把 Bot 的話標成「不是證據」並附系統統計,對不上以系統為準;評審優先用另一家供應商的模型,失敗退回原本的 3. 教訓分流:自動學技能時,AnyBot 自己的工具問題寫進 tool_issues → 「待改進」清單(同一工具合併計次),不寫進技能;設定 › 更新可看可清除 4. 園丁報告(garden.rs):平日 8:44 掃過去一天的失敗、走冤枉路的執行、自動暫停與重複出現的待改進;週一 8:49 列出失敗多或太耗時的例行任務;沒事不報,可在設定關掉 5. 減少步驟:browser type 多行文字一次放進欄位(先試貼上事件、檢查沒進再用 insertText,Firefox 也行)並回報欄位內容;點進輸入框回「游標已在…」不再說「沒有變化」;元素編號在同一頁重讀時不變;wait 的 after_send 從上次送出算間隔 6. 網站地圖:學技能時同一網站的操作知識整理進「<網站> 網站地圖」(功能/怎麼到/怎麼操作/陷阱),做事的技能只寫特有規則 Mac 465、Linux 469 個測試通過;多行輸入與編號實機測試在 Brave、Firefox 通過。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 15:52:44 +00:00
// 證據: what a stretch of the chat really did, counted from its steps — never from what the bot
// says about them. A bot once reported "2 sent" when one was; the /goal judge read its words and
// a routine's record only knew the turn ended. These counts go to both.
use crate::model::{Message, Role, ToolCall, ToolStatus};
/// What the steps of a stretch of chat did.
#[derive(Debug, Default, PartialEq)]
pub struct Tally {
pub steps: u32,
/// Steps that failed or were refused.
pub failed: u32,
/// Steps that sent something out and went through (a post, a reply, a message, a submit).
pub sent: u32,
/// Those sends, a line each (the tool and what it acted on).
pub sends: Vec<String>,
}
/// Words on a control that mean it sends something out (a reply box's 「回覆」 only opens it).
fn sends_words(label: &str) -> bool {
let low = label.to_lowercase();
["送出", "發佈", "發布", "發文", "傳送", "發送", "貼文", "張貼", "提交"].iter().any(|w| label.contains(w)) || ["send", "post", "publish", "submit", "tweet"].iter().any(|w| low.contains(w))
}
/// Whether a step sends something out of the computer: Enter or ctrl+Enter in a page, typing
/// with submit, a click on a 送出/發佈 control, a message or post tool.
pub fn sends(call: &ToolCall) -> bool {
let v = crate::tools::args_of(&call.args);
let text = |k: &str| v[k].as_str().unwrap_or("").to_string();
match call.name.as_str() {
"browser_press" | "computer_use_press_key" => {
let k = text("key").to_lowercase();
k.contains("enter") || k.contains("return")
}
"browser_type" | "computer_use_typing" => v["submit"].as_bool() == Some(true),
"browser_click" | "computer_use_click" => sends_words(&format!("{} {}", text("text"), text("name"))),
"send_sms" | "post_to_x" | "message_bot" | "send_to_device" => true,
n => crate::macapps::is_mac_tool(n) && n == crate::macapps::SEND_MESSAGE,
}
}
/// Count the steps of `msgs`.
pub fn tally(msgs: &[Message]) -> Tally {
let mut t = Tally::default();
for c in msgs.iter().filter(|m| m.role == Role::Assistant).flat_map(|m| m.tool_calls.iter()) {
t.steps += 1;
match c.status {
ToolStatus::Failed | ToolStatus::Denied => t.failed += 1,
ToolStatus::Done if sends(c) => {
t.sent += 1;
t.sends.push(format!("{} {}", c.name, crate::tools::describe(&c.name, &c.args)).trim().to_string());
}
_ => {}
}
}
t
}
#[cfg(test)]
mod tests {
use super::*;
fn call(name: &str, args: &str, status: ToolStatus) -> ToolCall {
ToolCall { id: "x".into(), name: name.into(), args: args.into(), status }
}
#[test]
fn only_steps_that_sent_and_went_through_count_as_sent() {
let steps = vec![
call("browser_click", r#"{"text":"回覆 with._.22"}"#, ToolStatus::Done),
call("browser_press", r#"{"key":"ctrl+v"}"#, ToolStatus::Done),
call("browser_press", r#"{"key":"ctrl+Return"}"#, ToolStatus::Done),
call("browser_press", r#"{"key":"ctrl+Return"}"#, ToolStatus::Failed),
call("browser_click", r#"{"text":"發佈"}"#, ToolStatus::Done),
call("browser_fill_contact", r#"{"contact_id":"x"}"#, ToolStatus::Denied),
call("browser_type", r#"{"ref":"3","value":"貓抓板","submit":true}"#, ToolStatus::Done),
];
let msgs = vec![Message::user("發吧"), Message { tool_calls: steps, ..Message::assistant("", "") }, Message::assistant("送出 2 則了", "")];
let t = tally(&msgs);
assert_eq!((t.steps, t.failed, t.sent), (7, 2, 3));
assert_eq!(t.sends.len(), 3);
assert!(t.sends[0].starts_with("browser_press"));
}
}