From 4bb6658b0c141973564ccb09fa10d3a952ca88e6 Mon Sep 17 00:00:00 2001 From: daniel wang Date: Mon, 16 Nov 2026 16:21:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BC=AA=E8=BF=B4=E5=8A=A0=E5=9B=BA=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E8=BC=AA=EF=BC=9A=E5=B7=A5=E5=85=B7=E8=85=B3=E6=9C=AC?= =?UTF-8?q?=E9=8E=96=E3=80=81=E5=AF=A9=E6=89=B9=E8=A1=8C=E7=82=BA=E9=8E=96?= =?UTF-8?q?=E3=80=81=E7=B4=94=E5=87=BD=E6=95=B8=E6=80=A7=E8=B3=AA=E6=B8=AC?= =?UTF-8?q?=E8=A9=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - tools.specs.lock.json+tool_specs_are_locked:Bot 看到的工具名、說明、參數 全鎖進檔;要改必須 ANYBOT_LOCK_UPDATE=1 產生 diff 看過,同 commit 改 feature-map (操作電腦與工具的介面从此改不動而不自知) - approval_decisions_are_characterized:32 個固定工具呼叫鎖住 Allow/Ask[grant]/Deny 判定,grant 字串逐字鎖(「一律允許」存的東西);審批行為不會被無意中改弱 - sidebar_ui:shorten_width 冪等與截斷標記、tile_offset 不出界與獨格置中 - app:批准卡/問題卡/composer 高度估算不超上限、內容越多越高(任何視窗寬度) - CLAUDE.md:改善輪迴協定——停止看收斂訊號不收輪數、保險絲 40 輪只防死圈、 每輪 commit 附「已驗證/未驗證」、變差回最後全綠 commit、工具與審批區改動 需實機測試+人類簽名 已驗證: cargo test 506 全綠(含新 5 個);鎖檔由 specs() 產生 未驗證: 本機是 Linux,macOS 實機(axread/botbrowser)未跑;鎖檔對 specs() 的 描述改動會報 diff,此點未做破坏性演練 --- CLAUDE.md | 15 +++ src/app.rs | 33 +++++++ src/app/sidebar_ui.rs | 59 ++++++++++++ src/tools.rs | 70 ++++++++++++++ src/tools.specs.lock.json | 186 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 363 insertions(+) create mode 100644 src/tools.specs.lock.json diff --git a/CLAUDE.md b/CLAUDE.md index 0289494..863f40a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -7,3 +7,18 @@ - 改某個功能前,先讀那一區;改完照那一區「驗證:」那一行確認。範圍大的修改照 `docs/feature-map/README.md` 的全面檢查順序。 - 行為改了,同一個 commit 一起改對應的那一份。新增 Bot 能用的工具、`browser`/`computer` 動作、設定分頁或區塊時,要寫進地圖;`cargo test featuremap::` 會擋下漏寫的。 - 人看的完整規格在 `FEATURES.md`;地圖只寫行為,短到不用讀程式就能照著操作。 + +## 改善輪迴協定(改 AnyBot 時) + +- 輪次不設上限;停止看收斂,不看輪數。收斂=同時滿足: + 1. 硬門檻全綠:`cargo test`(含 `featuremap::`、`layout_audit::`)+動到那區的「驗證:」。 + 2. 獨立驗證:用乾淨脈絡(新 session,不帶 builder 思路)照該區 feature-map 審 diff,提不出新的 P0/P1 問題。 + 3. 固定點:這一輪的 code 跟上一輪一字不差。 + 4. 淨改善:本輪修好的問題數 > 本輪引入的;變差時停,寫報告交還人類,不硬湊輪。 +- 保險絲 40 輪只防死圈:觸發就停下來寫報告、不 commit,不算完成。 +- 每輪一個 commit,訊息尾註 `已驗證: …`/`未驗證: …`(走不到的路寫原因:要實機 Mac、手機、付費帳號等)。 +- 後面的輪變差:回到最後一個全綠的 commit(交最好的,不是最新的)。 +- 動到工具定義(`src/tools.rs` 的 `specs`)、`computer`/`botbrowser`/`axread`/審批路徑時: + `tool_specs_are_locked` 和 `approval_decisions_are_characterized` 會把變動報成 diff——刻意要改才更新鎖檔, + 同 commit 更新 feature-map;這些區還要實機測試(`--ignored`)+人類簽名,輪迴不得自行發佈。 + diff --git a/src/app.rs b/src/app.rs index 7f46325..dde06b4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -8091,4 +8091,37 @@ mod tests { assert_eq!(std::fs::read_to_string(&marker).unwrap().trim(), "clean"); relaunched.wait().unwrap(); } + + /// (Properties of the card-height estimates, from the cards' spec (cards must never push + /// the composer off screen): they never exceed the cap, they grow with the content, and + /// they stay positive at any window width. Snapshots of exact pixels would break on any + /// font change; these do not.) + #[test] + fn card_heights_are_bounded_monotone_and_never_negative() { + use super::*; + for width in [120.0, 420.0, 1000.0, 4000.0] { + for cap in [160.0, 400.0, f32::MAX / 2.0] { + let short = ask_dock_height("執行:ls", width, cap); + let long = ask_dock_height(&format!("執行:{}", "rm -rf ./build && very_long_command --with-a-token ".repeat(80)), width, cap); + assert!((1.0..=cap + 0.01).contains(&short), "short {short} cap {cap} w {width}"); + assert!((1.0..=cap + 0.01).contains(&long), "long {long} cap {cap} w {width}"); + assert!(long >= short, "a longer reason got a shorter card at w {width}"); + let q = |n: usize| crate::ask::Question { question: "請選一個要彙整的資料夾".into(), options: (0..n).map(|i| format!("選項 {i}")).collect(), multiple: false, default: String::new() }; + let (few, many) = (ask_card_height(&q(2), width, cap), ask_card_height(&q(8), width, cap)); + assert!((1.0..=cap + 0.01).contains(&few) && (1.0..=cap + 0.01).contains(&many), "question card over the cap at w {width}"); + assert!(many >= few, "more options made the card shorter at w {width}"); + } + } + // The composer's box: at least one row, never past its own cap. + for text_h in [0.0, 24.0, 3000.0] { + for max_h in [28.0, 120.0, 400.0] { + let h = composer_box_h(text_h, max_h); + assert!((28.0..=max_h.max(28.0)).contains(&h), "composer {h} for text {text_h} max {max_h}"); + } + } + // Wrapped lines: always at least one, and more words never take fewer lines. + let one = wrapped_lines("abc", 300.0, 7.0); + let many = wrapped_lines(&"abc ".repeat(500), 300.0, 7.0); + assert!(one >= 1.0 && many > one); + } } diff --git a/src/app/sidebar_ui.rs b/src/app/sidebar_ui.rs index 10562ea..f511fef 100644 --- a/src/app/sidebar_ui.rs +++ b/src/app/sidebar_ui.rs @@ -1106,4 +1106,63 @@ mod tests { assert!((0.0..1.0).contains(&rand01(42))); assert_ne!(rand01(1), rand01(2)); } + + /// Runs `f` on a real Ui (shorten_width measures pixels, which needs a painter). + fn with_ui(f: impl FnOnce(&egui::Ui) -> T) -> T { + let ctx = egui::Context::default(); + let mut out: Option = None; + let (o, mut f) = (&mut out, Some(f)); + let mut full = ctx.run_ui(egui::RawInput::default(), |ctx| { + egui::CentralPanel::default().show(ctx, |ui| { + if let Some(f) = f.take().filter(|_| o.is_none()) { + *o = Some(f(ui)); + } + }); + }); + full.textures_delta.clear(); + out.unwrap() + } + + /// (Properties, not snapshots: cutting with … must never eat into a second round — + /// a cut string fits, so cutting it again returns it unchanged — and anything too + /// wide must carry the … mark.) + #[test] + fn shorten_width_is_idempotent_and_marks_every_cut() { + for (s, w) in [ + ("short", 200.0), + ("一個很長的中文名字一個很長的中文名字一個很長的中文名字", 100.0), + ("Extremely Long English Name That Keeps Going And Going", 90.0), + ] { + let once = with_ui(|ui| shorten_width(ui, s, w, 12.0)); + let twice = with_ui(|ui| shorten_width(ui, &once, w, 12.0)); + assert_eq!(once, twice, "cutting twice changed the string: {s}"); + let fits = with_ui(|ui| { + ui.painter().layout_no_wrap(s.to_string(), egui::FontId::proportional(12.0), egui::Color32::WHITE).size().x <= w + }); + assert!(fits || once.ends_with('…'), "over-wide text came back unmarked: {s} -> {once}"); + } + } + + /// (Tiles always stay inside the row's width, sit on their own row, and a lone tile + /// in a row is centred — the property 格子排版 relies on at every window width.) + #[test] + fn tiles_stay_inside_and_rows_are_even() { + let (tw, th) = (130.0, 100.0); + for cols in 1..=4usize { + for n in 1..=9usize { + let w = 9.0 * tw; + for i in 0..n { + let v = tile_offset(i, n, cols, tw, th, w); + assert!(v.x >= -0.01 && v.x + tw <= w + 0.01, "tile {i}/{n} cols {cols} sticks out at x {}", v.x); + assert_eq!(v.y, (i / cols) as f32 * th, "tile {i} on the wrong row"); + } + // The last row's lone tile (odd count) sits in the middle. + let last = n - 1; + if n.saturating_sub((last / cols) * cols) == 1 && last % cols == 0 { + let v = tile_offset(last, n, cols, tw, th, w); + assert!((v.x - (w - tw) / 2.0).abs() < 0.01, "lone tile is not centred: x {}", v.x); + } + } + } + } } diff --git a/src/tools.rs b/src/tools.rs index 9cde657..2789785 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -2989,4 +2989,74 @@ mod tests { assert!(matches!(decide_file_out(&t, "/home/bot/bot-2/site", &[]), Decision::Deny(_))); assert!(matches!(decide_file_out(&t, "/etc/passwd", &[]), Decision::Deny(_))); } + + // (The tool specs are the bot's view of the computer: a name, description or parameter + // changed by accident (or by a UI refactor that brushed past this file) silently breaks + // how the bot works. Every change must show up as a reviewed diff in the lock file, and + // the same commit must update the feature-map (cargo test featuremap:: guards the names).) + #[test] + fn tool_specs_are_locked() { + let now = format!("{}\n", serde_json::to_string_pretty(&specs()).unwrap()); + let path = concat!(env!("CARGO_MANIFEST_DIR"), "/src/tools.specs.lock.json"); + if std::env::var("ANYBOT_LOCK_UPDATE").is_ok_and(|v| v == "1") { + std::fs::write(&path, &now).unwrap(); + return; + } + let want = std::fs::read_to_string(&path).unwrap_or_default(); + assert_eq!(now, want, "工具 schema 變了:只有刻意要改才跑 ANYBOT_LOCK_UPDATE=1 cargo test tool_specs_are_locked,看過 diff 才算數,並在同一個 commit 更新 feature-map"); + } + + /// The approval decisions on fixed calls, as Allow / Ask[grant] / Deny. The grant string + /// is exactly what 「一律允許」 stores, so locking these locks the approval behaviour; + /// the reason's wording (which carries paths) is deliberately not part of the shape. + #[test] + fn approval_decisions_are_characterized() { + let wd = wd(); + let shape = |name: &str, args: &str, grants: &[&str]| match decide(name, args, &wd, &grants.iter().map(|s| s.to_string()).collect::>()) { + Decision::Allow => "Allow".to_string(), + Decision::Ask { grant, .. } => format!("Ask[{grant}]"), + Decision::Deny(_) => "Deny".to_string(), + }; + let outside = std::env::temp_dir().join("anybot-outside-file.md"); + let cases: Vec<(&str, String, Vec<&str>, &str)> = vec![ + ("list_dir", r#"{"path":""}"#.into(), vec![], "Allow"), + ("list_dir", r#"{"path":"sub"}"#.into(), vec![], "Allow"), + ("list_dir", r#"{"path":"/etc"}"#.into(), vec![], "Ask[read:anywhere]"), + ("list_dir", format!(r#"{{"path":"{}/.ssh"}}"#, wd.display()), vec![], "Deny"), + ("read_file", r#"{"path":"notes.md"}"#.into(), vec![], "Allow"), + ("read_file", format!(r#"{{"path":"{}"}}"#, outside.display()), vec![], "Ask[read:anywhere]"), + ("read_file", format!(r#"{{"path":"{}/.env"}}"#, wd.display()), vec![], "Deny"), + ("edit_file", r#"{"path":"a.txt"}"#.into(), vec![], "Ask[edit_file]"), + ("edit_file", r#"{"path":"a.txt"}"#.into(), vec!["edit_file"], "Allow"), + ("edit_file", format!(r#"{{"path":"{}"}}"#, outside.display()), vec!["edit_file"], "Ask[]"), + ("edit_file", format!(r#"{{"path":"{}/.env"}}"#, wd.display()), vec!["edit_file"], "Deny"), + ("read_terminal", r#"{"id":1}"#.into(), vec![], "Allow"), + ("write_terminal", r#"{"id":1,"input":"x"}"#.into(), vec![], "Allow"), + ("shell", r#"{"command":""}"#.into(), vec![], "Deny"), + ("shell", r#"{"command":"ls -la"}"#.into(), vec![], "Allow"), + ("shell", r#"{"command":"git status && git log --oneline"}"#.into(), vec![], "Allow"), + ("shell", r#"{"command":"cat f | grep x"}"#.into(), vec![], "Allow"), + ("shell", r#"{"command":"find . -name x"}"#.into(), vec![], "Allow"), + ("shell", r#"{"command":"find . -name x -delete"}"#.into(), vec!["shell:find"], "Ask[]"), + ("shell", r#"{"command":"git commit -m x"}"#.into(), vec![], "Ask[shell:git commit]"), + ("shell", r#"{"command":"git commit -m x"}"#.into(), vec!["shell:git commit"], "Allow"), + // 「一律允許」 on git commit covers a $(…) the model hides inside its own message. + ("shell", r#"{"command":"git commit -m \"$(whoami)\""}"#.into(), vec!["shell:git commit"], "Allow"), + ("shell", r#"{"command":"git commit -m x && rm -rf /"}"#.into(), vec!["shell:git commit"], "Deny"), + ("shell", r#"{"command":"ls; rm -rf ~"}"#.into(), vec![], "Deny"), + ("shell", r#"{"command":"sudo ls"}"#.into(), vec![], "Deny"), + ("shell", r#"{"command":"(sudo ls)"}"#.into(), vec![], "Deny"), + ("shell", r#"{"command":"FOO=1 sudo ls"}"#.into(), vec![], "Deny"), + ("shell", r#"{"command":"timeout 5 sudo ls"}"#.into(), vec![], "Deny"), + // A write redirect: 「一律允許」 on echo must not cover it, so no grant is offered. + ("shell", r#"{"command":"echo hi > out.txt"}"#.into(), vec!["shell:echo"], "Ask[]"), + ("shell", r#"{"command":"ls \"x"}"#.into(), vec![], "Ask[]"), + ("shell", r#"{"command":"bash","interactive":true}"#.into(), vec![], "Ask[]"), + ("shell", r#"{"command":"bash -c 'echo hi'","interactive":true}"#.into(), vec![], "Ask[shell:bash]"), + ]; + for (name, args, grants, want) in &cases { + let got = shape(name, args, grants); + assert_eq!(got.as_str(), *want, "{name} {args} grants={grants:?}"); + } + } } diff --git a/src/tools.specs.lock.json b/src/tools.specs.lock.json new file mode 100644 index 0000000..a45a647 --- /dev/null +++ b/src/tools.specs.lock.json @@ -0,0 +1,186 @@ +[ + { + "description": "List the files and folders in a folder (relative to the work folder unless absolute).", + "name": "list_dir", + "parameters": { + "properties": { + "path": { + "description": "Folder to list; empty means the work folder.", + "type": "string" + } + }, + "required": [], + "type": "object" + } + }, + { + "description": "In the person's own indexed document folders (their 設定 › 一般 › 文件索引), find the passages that answer a question, by meaning as well as words. Use it for 「我上個月那份報表在哪」, 「我寫過的語法筆記」 — questions about THEIR files, not about the computer. Returns the file path and the text of each match.", + "name": "search_documents", + "parameters": { + "properties": { + "limit": { + "description": "How many passages (default 6).", + "type": "integer" + }, + "query": { + "description": "What to look for, in a sentence.", + "type": "string" + } + }, + "required": [ + "query" + ], + "type": "object" + } + }, + { + "description": "Find text in files under a folder. Returns matching lines as path:line:text.", + "name": "search", + "parameters": { + "properties": { + "files_only": { + "description": "Only list the files that match.", + "type": "boolean" + }, + "path": { + "description": "Folder or file to search; empty means the work folder.", + "type": "string" + }, + "pattern": { + "description": "Text to look for (regular expression when ripgrep is installed).", + "type": "string" + } + }, + "required": [ + "pattern" + ], + "type": "object" + } + }, + { + "description": "Read a text file. Lines come back numbered.", + "name": "read_file", + "parameters": { + "properties": { + "limit": { + "description": "How many lines to read.", + "type": "integer" + }, + "offset": { + "description": "First line to read (1-based).", + "type": "integer" + }, + "path": { + "type": "string" + } + }, + "required": [ + "path" + ], + "type": "object" + } + }, + { + "description": "Change a file. Replaces old_text (which must appear exactly once) with new_text. With empty old_text the file is created or overwritten with new_text.", + "name": "edit_file", + "parameters": { + "properties": { + "new_text": { + "type": "string" + }, + "old_text": { + "type": "string" + }, + "path": { + "type": "string" + } + }, + "required": [ + "path", + "new_text" + ], + "type": "object" + } + }, + { + "description": "Run a shell command in the work folder (bash or sh; on a Windows computer Git Bash when it is there, else PowerShell). Its input is closed, so nothing waits for typing: use a program's non-interactive form (flags like -y/--yes; coding agents' print mode, e.g. `claude -p \"…\"`, `pi-bolt -p \"…\"`, `codex exec \"…\"`, `gemini -p \"…\"`). If it runs longer than timeout_ms it keeps running in the background and you get a terminal id to check with read_terminal (pass the same computer to read_terminal/write_terminal). interactive=true runs it in a real terminal instead (互動終端機): for a program that needs one — its own full-screen interface, questions to answer, an editor; you see its screen and type with write_terminal.", + "name": "shell", + "parameters": { + "properties": { + "command": { + "type": "string" + }, + "interactive": { + "description": "Run in a real terminal you can see and type into (default false).", + "type": "boolean" + }, + "timeout_ms": { + "description": "How long to wait before moving it to the background (default 30000). Interactive: how long to wait for its first screen (at most 15000).", + "type": "integer" + } + }, + "required": [ + "command" + ], + "type": "object" + } + }, + { + "description": "Look at a background command: its latest output, or a 互動終端機's screen. wait_ms waits (up to 120000) until it shows something new and goes quiet, or until the text in `until` appears, or it ends — instead of checking again and again.", + "name": "read_terminal", + "parameters": { + "properties": { + "id": { + "type": "integer" + }, + "until": { + "description": "Return as soon as this text is on the screen / in the output.", + "type": "string" + }, + "wait_ms": { + "type": "integer" + } + }, + "required": [ + "id" + ], + "type": "object" + } + }, + { + "description": "Type into a 互動終端機 (a command started with interactive=true): text (each new line presses Enter) and/or keys, then see its screen once it settles (wait_ms, default 4000, up to 120000). close=true sends Ctrl-D. A plain command's input is closed: it can't be typed into.", + "name": "write_terminal", + "parameters": { + "properties": { + "close": { + "type": "boolean" + }, + "id": { + "type": "integer" + }, + "input": { + "description": "Text to type; a new line presses Enter.", + "type": "string" + }, + "keys": { + "description": "Keys to press after the text: enter, tab, shift-tab, esc, backspace, delete, space, up, down, left, right, home, end, pageup, pagedown, ctrl-c, ctrl-d (any ctrl-letter).", + "items": { + "type": "string" + }, + "type": "array" + }, + "stop": { + "description": "true: end this background command (plain or interactive) and everything it started. Do this for the ones you no longer need.", + "type": "boolean" + }, + "wait_ms": { + "type": "integer" + } + }, + "required": [ + "id" + ], + "type": "object" + } + } +]