// 功能地圖 (Lauren Tan's feature map, in pstack's create-verification-skill shape): AnyBot as // behaviour, one file per area under docs/feature-map/, each answering the same four questions — // what is there, how a person gets to it, how to drive it, what usually lies. Two readers: // - the bots: anybot_guide reads one area — its behaviour only (`bot_view`: no verification // recipe). AnyBot is a product the bots work inside, and they used to learn it by trial and // error (a contact fill used 107 times as a typing tool) and write what they found into their // task skills; // - agents changing AnyBot: they open the file for the change and drive it the way it says, // with the verify-anybot skill (the commands are in each area's 「怎麼操作(verify-anybot)」). // Kept as code: the tests below fail when a tool a bot can be given, a browser or screen action, // or a settings tab or section isn't in the map — a new feature comes with its entry — and when // an area breaks the shape docs/feature-map/README.md 「每份檔的規定」 gives it. use serde_json::{json, Value}; /// One area of the map. pub struct Area { pub slug: &'static str, pub title: &'static str, pub text: &'static str, } /// The map's index: who reads it, the shared conventions, the order of a full sweep (for people /// and agents changing AnyBot; a bot gets `index_line` instead). #[cfg(test)] pub const INDEX: &str = include_str!("../docs/feature-map/README.md"); macro_rules! area { ($slug:literal, $title:literal) => { Area { slug: $slug, title: $title, text: include_str!(concat!("../docs/feature-map/", $slug, ".md")) } }; } /// The areas, in the order of a full sweep. pub const AREAS: &[Area] = &[ area!("chat", "聊天"), area!("cards", "問題卡、生活卡片與表情"), area!("bots", "Bot 清單與選單"), area!("profile", "Bot 設定與能力"), area!("models", "模型與 ChatGPT 方案"), area!("team", "同事與群組"), area!("branch", "支線(/btw)"), area!("memory", "記憶"), area!("skills", "技能"), area!("replay", "自動學與重播"), area!("pages", "頁面"), area!("pageview", "頁面的版面與圖表"), area!("routines", "例行任務"), area!("goal", "目標(/goal)"), area!("plan", "計畫(/plan)"), area!("tasks", "任務"), area!("computers", "電腦:大腦與手腳"), area!("box", "Bot 的電腦"), area!("files", "檔案與指令"), area!("deliver", "交出結果:傳檔、打開、發佈網站"), area!("claims", "完成宣稱檢查"), area!("browser", "瀏覽器"), area!("screen", "螢幕操作"), area!("vault", "保險庫"), area!("connectors", "連接器(MCP)"), area!("market", "連接器市集與設定頁"), area!("apps", "Mac App 與電話"), area!("channels", "訊息管道"), area!("phone", "手機頁面"), area!("account", "帳號與直接連線"), area!("safety", "安全與核准"), area!("sandbox", "安全規則與沙盒"), area!("autonomy", "直接操作、一律放行與自動審核"), area!("settings", "設定總覽"), area!("appearance", "外觀與語音"), area!("garden", "園丁、待改進與更新"), area!("activity", "操作紀錄"), ]; /// The tool a bot reads the map with. pub const TOOL: &str = "anybot_guide"; pub fn spec() -> Value { let topics: Vec<&str> = AREAS.iter().map(|a| a.slug).collect(); json!({"name": TOOL, "description": "Read one area of AnyBot's own feature map: what AnyBot can do there, where the person finds it, which of your tools drive it, and what usually goes wrong. Read it when the person asks about AnyBot itself (can you…, where do I set…, why did…), before working around something AnyBot may already do, and when one of your tools behaves unexpectedly. Don't guess AnyBot's features or settings from memory.", "parameters": {"type": "object", "properties": { "topic": {"type": "string", "enum": topics, "description": "The area (the list is in your instructions)."}}, "required": ["topic"]}}) } /// One line for the instructions: the areas there are (the same every turn, so it caches). pub fn index_line() -> String { let list: Vec = AREAS.iter().map(|a| format!("{} {}", a.slug, a.title)).collect(); format!("\n\nAnyBot 功能地圖({TOOL} 讀一區;問到 AnyBot 本身、要繞路之前、工具反應怪怪的,先讀,不要憑印象):{}。", list.join("、")) } /// What a bot reads of an area: the behaviour — the opening, 子功能, 使用者怎麼到, its own /// 「- Bot:」 line and the 陷阱 — without the verify-anybot recipe (前提, the steps, 自動測試) or /// the traps that are only about a verification run (「- (驗證)…」). The rest of the file is for /// the agents changing AnyBot; a bot reading `$CTL …` would take it for its tools, or for facts. pub fn bot_view(text: &str) -> String { let mut out: Vec<&str> = vec![]; let (mut section, mut drop_trap, mut recipe) = ("", false, false); for line in text.lines() { if let Some(h) = line.strip_prefix("## ") { section = h.trim_end(); out.push(if section.starts_with("怎麼操作") { "## 怎麼操作" } else { line }); continue; } if section.starts_with("怎麼操作") { // Only the Bot line(s) above 前提; from 前提 on it is the recipe. recipe |= line.starts_with("前提:"); if !recipe && (line.trim().is_empty() || line.starts_with("- Bot")) { out.push(line); } continue; } if section == "陷阱" && line.starts_with("- ") { drop_trap = line.starts_with("- (驗證)"); } if !(section == "陷阱" && drop_trap) { out.push(line); } } out.join("\n").trim().to_string() } /// The area a bot asked for (by slug or title), as the bot reads it. pub fn read(topic: &str) -> Result { let t = topic.trim(); AREAS .iter() .find(|a| a.slug.eq_ignore_ascii_case(t) || a.title == t || a.title.starts_with(t) && !t.is_empty()) .map(|a| bot_view(a.text)) .ok_or_else(|| format!("沒有「{t}」這一區。可以讀:{}", AREAS.iter().map(|a| a.slug).collect::>().join("、"))) } /// Every tool a bot can be given, from where the window puts them together (app.rs), as it is /// offered (the one `browser` and `computer` tools among them). pub fn every_tool() -> Vec { let names = |specs: &[Value]| specs.iter().filter_map(|t| t["name"].as_str().map(String::from)).collect::>(); let one = vec!["x".to_string()]; let mut all = names(&crate::appctl::specs()); all.extend(names(&crate::tools::specs_for(&one, "x"))); all.extend(names(&crate::web::specs_for(&one, "x"))); all.extend(names(&crate::computer::specs_for(&one, "x", true))); all.extend(names(&crate::macapps::specs())); all.extend(names(&crate::phone::specs())); all.extend(names(&crate::vault::specs_for(&crate::vault::Offer { login: true, card: true, contact: true }))); all.extend(crate::ask::spec()["name"].as_str().map(String::from)); all.extend([crate::plan::SUBMIT_TOOL, crate::plan::UPDATE_TOOL, crate::outputs::TOOL].map(String::from)); all.extend(crate::teach::view_step_spec()["name"].as_str().map(String::from)); all.extend([crate::mcp::FIND_TOOL, crate::mcp::USE_TOOL, crate::agent::REPLAY_TOOL, crate::tools::HANDOFF_TOOL, "show_card", "react", "send_to_device", "send_file", "open_for_person", "publish_site", TOOL].map(String::from)); all.sort(); all.dedup(); all } #[cfg(test)] mod tests { use super::*; /// The whole map as the bots can read it (every area's `bot_view`; the README isn't theirs). fn map_text() -> String { AREAS.iter().map(|a| bot_view(a.text)).collect::>().join("\n") } /// One area as a bot reads it. fn bot_area(slug: &str) -> String { bot_view(AREAS.iter().find(|a| a.slug == slug).unwrap().text) } fn actions(specs: &[Value], tool: &str) -> Vec { specs.iter().filter(|t| t["name"] == tool).flat_map(|t| t["parameters"]["properties"]["action"]["enum"].as_array().cloned().unwrap_or_default()).filter_map(|a| a.as_str().map(String::from)).collect() } #[test] fn every_tool_a_bot_can_be_given_is_on_the_map() { let map = map_text(); let missing: Vec = every_tool().into_iter().filter(|n| !map.contains(&format!("`{n}`"))).collect(); assert!(missing.is_empty(), "these tools aren't on the feature map (docs/feature-map/, written as `name`): {missing:?}"); } #[test] fn every_browser_and_screen_action_is_in_its_area() { let one = vec!["x".to_string()]; let area = bot_area; for (slug, tool, specs) in [("browser", crate::browser::TOOL, crate::web::specs_for(&one, "x")), ("screen", crate::computer::TOOL, crate::computer::specs_for(&one, "x", true))] { let missing: Vec = actions(&specs, tool).into_iter().filter(|a| !area(slug).contains(&format!("`{a}`"))).collect(); assert!(missing.is_empty(), "{slug}.md doesn't name these `{tool}` actions: {missing:?}"); } } #[test] fn every_settings_tab_and_section_is_in_the_settings_area() { let settings = bot_area("settings"); let sources = [include_str!("app.rs"), include_str!("app/remote_ui.rs"), include_str!("app/vault_ui.rs"), include_str!("app/voice_ui.rs"), include_str!("app/channels_ui.rs"), include_str!("app/computers_ui.rs"), include_str!("app/mirror_ui.rs")]; let mut wanted: Vec = vec![]; for src in sources { for pat in ["setting_title(ui, \"", "setting_row(ui, p, \""] { for part in src.split(pat).skip(1) { if let Some(name) = part.split('"').next().filter(|n| !n.is_empty()) { wanted.push(name.to_string()); } } } } // The tabs of the settings window. let tabs = include_str!("app.rs").split("(SettingsTab::General, \"").nth(1).and_then(|r| r.split(']').next()).unwrap_or(""); wanted.extend(tabs.split("\", Icon::").filter_map(|p| p.rsplit('"').next()).filter(|n| !n.is_empty() && !n.contains(')')).map(String::from)); wanted.push("一般".into()); // What a bot may change itself. let keys = crate::appctl::specs().into_iter().find(|t| t["name"] == "update_settings").map(|t| t["parameters"]["properties"].as_object().map(|o| o.keys().cloned().collect::>()).unwrap_or_default()).unwrap_or_default(); wanted.extend(keys.into_iter().map(|k| format!("`{k}`"))); wanted.sort(); wanted.dedup(); assert!(wanted.len() > 15, "the settings scan found too little: {wanted:?}"); let missing: Vec<&String> = wanted.iter().filter(|w| !settings.contains(w.as_str())).collect(); assert!(missing.is_empty(), "settings.md doesn't mention: {missing:?}"); } #[test] fn every_area_answers_the_four_questions_and_the_index_lists_it() { for a in AREAS { for h in ["## 子功能", "## 使用者怎麼到", "## 怎麼操作", "## 陷阱"] { assert!(a.text.contains(h), "{}.md has no 「{h}」", a.slug); } assert!(a.text.contains("- Bot:"), "{}.md: 怎麼操作 needs a 「- Bot:」 line (what a bot inside AnyBot uses)", a.slug); assert!(a.text.starts_with(&format!("# {}", a.title)), "{}.md should start with 「# {}」", a.slug, a.title); assert!(INDEX.contains(&format!("({}.md)", a.slug)), "README.md doesn't list {}.md", a.slug); // What a bot reads is short enough to take in one go (her rule: behaviour-level, no // source needed); the whole file, recipe included, stays readable too. let bot = bot_view(a.text).chars().count(); assert!(bot < 6000, "{}.md: what a bot reads is too long ({bot} chars): tighten it or split the area (README 「每份檔的規定」)", a.slug); assert!(a.text.chars().count() < 9000, "{}.md is too long ({} chars): tighten it or split the area (README 「每份檔的規定」)", a.slug, a.text.chars().count()); } } // The verify-anybot shape (README 「每份檔的規定」, pstack's feature entry contract): every area's // third heading names the harness, and its steps follow the contract. /// The third heading of an area in the verify-anybot shape. const DRIVE: &str = "## 怎麼操作(verify-anybot)"; fn converted(text: &str) -> bool { text.lines().any(|l| l.trim_end() == DRIVE) } /// The lines under one H2, up to the next. fn section<'a>(text: &'a str, h2: &str) -> Vec<&'a str> { text.lines().skip_while(|l| l.trim_end() != h2).skip(1).take_while(|l| !l.starts_with("## ")).collect() } /// The IDs a 子功能 section gives, one per top-level bullet (`area-thing`). fn sub_ids(text: &str) -> Vec> { section(text, "## 子功能").into_iter().filter(|l| l.starts_with("- ")).map(|l| l.strip_prefix("- `").and_then(|r| r.split_once('`')).map(|(id, _)| id)).collect() } /// The IDs a step says it covers: 「- **名稱**(covers `a`、`b`):…」. fn covers(step: &str) -> Vec<&str> { step.split_once("**(covers ").and_then(|(_, r)| r.split_once(')')).map(|(c, _)| c.split('`').skip(1).step_by(2).collect()).unwrap_or_default() } /// Where a converted area breaks the contract (empty: it keeps it). fn contract_problems(a: &Area) -> Vec { let mut bad = vec![]; let h2: Vec<&str> = a.text.lines().filter(|l| l.starts_with("## ")).map(str::trim_end).collect(); let want = ["## 子功能", "## 使用者怎麼到", DRIVE, "## 陷阱"]; if h2 != want { bad.push(format!("the H2s are exactly {want:?}, in this order (found {h2:?})")); } let intro: Vec<&str> = a.text.lines().skip(1).take_while(|l| !l.starts_with("## ")).filter(|l| !l.trim().is_empty()).collect(); if intro.len() != 1 || intro[0].starts_with(['-', '#', '`']) { bad.push("the H1 is followed by exactly one paragraph".into()); } // 子功能: every bullet starts with its ID. let prefix = format!("{}-", a.slug); let kebab = |id: &str| id.split('-').all(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit())); let mut ids: Vec<&str> = vec![]; for id in sub_ids(a.text) { match id { Some(id) if id.starts_with(&prefix) && kebab(id) => { if ids.contains(&id) { bad.push(format!("`{id}` is given twice")); } ids.push(id); } Some(id) => bad.push(format!("`{id}`: a 子功能 ID is ASCII kebab-case starting with `{prefix}`")), None => bad.push(format!("every 子功能 bullet starts with its ID in backticks (`{prefix}…`)")), } } if ids.is_empty() { bad.push("子功能 lists no IDs".into()); } // 怎麼操作: the Bot line, 前提, the steps, the automated tests last. let drive = section(a.text, DRIVE); let lines: Vec<&str> = drive.iter().copied().filter(|l| !l.trim().is_empty()).collect(); let at = |p: &dyn Fn(&str) -> bool| lines.iter().position(|l| p(l)); let bot = at(&|l| l.starts_with("- Bot:")); let pre = at(&|l| l.starts_with("前提:")); let first_step = at(&|l| l.starts_with("- **")); let last_step = lines.iter().rposition(|l| l.starts_with("- **")); let auto = at(&|l| l.starts_with("- 自動測試:")); if bot != Some(0) { bad.push("怎麼操作 starts with the 「- Bot:」 line".into()); } if !(bot < pre && pre.is_some()) { bad.push("a 「前提:」 line comes after the Bot line".into()); } if !(pre < first_step && first_step.is_some()) { bad.push("labeled steps (「- **名稱**(covers `…`):…指令:…看到:…」) come after 前提".into()); } if auto.is_none() || auto != lines.iter().rposition(|l| l.starts_with("- ")) || auto < last_step { bad.push("the last bullet is 「- 自動測試:…」".into()); } if lines.iter().any(|l| l.starts_with("- 驗證:")) { bad.push("the old 「- 驗證:」 line is replaced by 前提, the steps and 自動測試".into()); } for step in lines.iter().filter(|l| l.starts_with("- **")) { let head: String = step.chars().take(40).collect(); for need in ["**(covers `", "指令:", "看到:"] { if !step.contains(need) { bad.push(format!("a step has no 「{need}」: {head}…")); } } for id in covers(step) { if !ids.contains(&id) { bad.push(format!("a step covers `{id}`, which 子功能 doesn't list: {head}…")); } } } // Every sub-feature is covered by a step (one the scratch can't reach still says so). let covered: Vec<&str> = lines.iter().filter(|l| l.starts_with("- **")).flat_map(|l| covers(l)).collect(); for id in &ids { if !covered.contains(id) { bad.push(format!("`{id}` isn't covered by any step")); } } bad } #[test] fn the_contract_check_catches_what_breaks_it() { let goal = AREAS.iter().find(|a| a.slug == "goal").unwrap().text; let broken = |from: &str, to: &str| { assert!(goal.contains(from), "the example no longer has 「{from}」"); contract_problems(&Area { slug: "goal", title: "目標(/goal)", text: Box::leak(goal.replacen(from, to, 1).into_boxed_str()) }) }; for (from, to, says) in [ ("- `goal-set` 設定", "- 設定", "starts with its ID"), ("- `goal-set` 設定", "- `set` 設定", "ASCII kebab-case"), ("- `goal-next` 下一輪", "- `goal-set` 下一輪", "given twice"), ("前提:", "前提", "前提"), ("- 自動測試:", "- 測試:", "自動測試"), ("指令:`$CTL phone send '/goal' &&", "`$CTL phone send '/goal' &&", "指令:"), ("(covers `goal-restart`)", "(covers `goal-reboot`)", "doesn't list"), ("(covers `goal-restart`)", "(covers `goal-set`)", "`goal-restart` isn't covered"), ("## 陷阱", "## 注意", "exactly"), ] { let bad = broken(from, to); assert!(bad.iter().any(|b| b.contains(says)), "changing 「{from}」 to 「{to}」 should be caught ({says}); got {bad:?}"); } } #[test] fn every_area_is_drivable_the_way_its_format_says() { for a in AREAS { assert!(converted(a.text), "{}.md: the third heading must be 「{DRIVE}」", a.slug); let bad = contract_problems(a); assert!(bad.is_empty(), "{}.md breaks README 「每份檔的規定」:\n- {}", a.slug, bad.join("\n- ")); } } #[test] fn sub_feature_ids_are_unique_in_the_map() { let mut seen: std::collections::HashMap<&str, &str> = Default::default(); for a in AREAS { for id in sub_ids(a.text).into_iter().flatten() { if let Some(other) = seen.insert(id, a.slug) { assert_eq!(other, a.slug, "`{id}` is a 子功能 ID in both {other}.md and {}.md", a.slug); } } } } #[test] fn every_area_is_in_the_verify_anybot_format() { let old: Vec<&str> = AREAS.iter().filter(|a| !converted(a.text)).map(|a| a.slug).collect(); assert!(old.is_empty(), "still in the old 「- 驗證:」 format: {old:?}"); } #[test] fn the_index_carries_the_contract() { for h in ["## 誰讀、怎麼用", "## 基本前提", "## 操作慣例", "## 證據與略過", "## 每份檔的規定", "## 怎麼維護", "## 全面檢查順序"] { assert!(INDEX.contains(h), "README.md has no 「{h}」"); } for need in [DRIVE, "- Bot:", "前提:", "(covers `", "指令:", "看到:", "- 自動測試:"] { assert!(INDEX.contains(need), "README.md 「每份檔的規定」 doesn't spell out 「{need}」"); } // The reference example the contract points at is in the shape the test checks. let goal = AREAS.iter().find(|a| a.slug == "goal").unwrap(); assert!(converted(goal.text) && contract_problems(goal).is_empty(), "goal.md is the reference example: keep it in the verify-anybot shape"); } #[test] fn what_a_bot_reads_is_the_behaviour_not_the_recipe() { for a in AREAS { let v = read(a.slug).unwrap(); for keep in ["## 子功能", "## 使用者怎麼到", "## 怎麼操作", "- Bot:", "## 陷阱"] { assert!(v.contains(keep), "what a bot reads of {}.md lost 「{keep}」", a.slug); } // Harness-only words stay out of the parts a bot reads (子功能, 使用者怎麼到, the Bot line, // the traps): mark a verification-only trap 「(驗證)」, keep the rest in 前提 or a step. for harness in ["$CTL", "前提:", "指令:", "看到:", "自動測試", "(驗證)", "model-reply", "[[call", "scratch", "verify-anybot", "假模型"] { assert!(!v.contains(harness), "what a bot reads of {}.md has 「{harness}」 — a verification detail, not AnyBot:\n{}", a.slug, v.lines().find(|l| l.contains(harness)).unwrap_or("")); } // Every sub-feature and every trap but the verification ones are still there. for id in sub_ids(a.text).into_iter().flatten() { assert!(v.contains(&format!("`{id}`")), "what a bot reads of {}.md lost `{id}`", a.slug); } let traps = section(a.text, "## 陷阱").into_iter().filter(|l| l.starts_with("- ") && !l.starts_with("- (驗證)")); for t in traps { assert!(v.contains(t), "what a bot reads of {}.md lost the trap 「{t}」", a.slug); } } // The trimming itself, on a small area. let v = bot_view("# 標題\n\n一段話。\n\n## 子功能\n\n- `x-a` 甲。\n\n## 使用者怎麼到\n\n- 從這裡。\n\n## 怎麼操作(verify-anybot)\n\n- Bot:用 `tool`。\n\n前提:\n\n- Bot 1 的聊天開著。\n\n- **一步**(covers `x-a`):做。指令:`$CTL x`。看到:好。\n- 自動測試:`cargo test x::`。\n\n## 陷阱\n\n- 真的坑。\n- (驗證)只跟驗證有關。\n - 它的子項目。\n- 另一個坑。\n"); assert_eq!(v, "# 標題\n\n一段話。\n\n## 子功能\n\n- `x-a` 甲。\n\n## 使用者怎麼到\n\n- 從這裡。\n\n## 怎麼操作\n\n- Bot:用 `tool`。\n\n## 陷阱\n\n- 真的坑。\n- 另一個坑。"); } #[test] fn a_bot_reads_one_area_by_its_name() { assert!(read("routines").unwrap().starts_with("# 例行任務")); assert!(read("例行任務").unwrap().starts_with("# 例行任務")); assert!(read("nope").unwrap_err().contains("routines")); assert!(index_line().contains("routines 例行任務") && index_line().contains(TOOL)); assert_eq!(spec()["parameters"]["properties"]["topic"]["enum"].as_array().unwrap().len(), AREAS.len()); } }