合併: 輪迴加固(loop-hardening):工具說明鎖檔、核准行為的特性測試、排版的性質測試、改善輪迴協定
全部收進來: - tool_specs_are_locked:工具說明(tools.rs 的 specs)一改就報 diff;鎖檔已經更新,跟分支比只多了 shell 的 monitor(M3 刻意加的,功能地圖已寫)。 - approval_decisions_are_characterized:固定一組呼叫的 Allow/Ask/Deny;在現在的程式上原封不動通過(11/16 之後核准行為沒有走樣)。 - 卡片高度、輸入框、斷行、名字截斷、格子排版的性質測試:都通過(名字截斷的測試改用 FONT_SM,現在的 theme 規則不准寫死字級;兩個多餘的 & 拿掉)。 - CLAUDE.md 的改善輪迴協定。 app.rs 的衝突只是兩邊都在測試模組最後加了測試,兩邊都留。 已驗證:cargo test 613 通過、clippy -D warnings 乾淨。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
d2afaaf6ee
15
CLAUDE.md
15
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`)+人類簽名,輪迴不得自行發佈。
|
||||
|
||||
|
|
|
|||
33
src/app.rs
33
src/app.rs
|
|
@ -8972,4 +8972,37 @@ mod tests {
|
|||
run(&mut app);
|
||||
assert_eq!(ctx.global_style().spacing.item_spacing, egui::vec2(8.0, 5.0));
|
||||
}
|
||||
|
||||
/// (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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1120,4 +1120,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<T>(f: impl FnOnce(&egui::Ui) -> T) -> T {
|
||||
let ctx = egui::Context::default();
|
||||
let mut out: Option<T> = 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, FONT_SM));
|
||||
let twice = with_ui(|ui| shorten_width(ui, &once, w, FONT_SM));
|
||||
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(FONT_SM), 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
70
src/tools.rs
70
src/tools.rs
|
|
@ -3285,4 +3285,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::<Vec<_>>()) {
|
||||
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:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,190 @@
|
|||
[
|
||||
{
|
||||
"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"
|
||||
},
|
||||
"monitor": {
|
||||
"description": "Watch it (監看): it goes to the background at once and every new line it prints wakes you with those lines (a second's lines come together) — e.g. `tail -f app.log | grep --line-buffered ERROR`, a deploy's log, a test watcher. Filter it to the lines that matter: too many lines (over 200 in 10 s) stop it. Stop it with write_terminal stop. Not on another AnyBot of the account.",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
]
|
||||
Loading…
Reference in New Issue