refactor: 核准關卡的規則搬到 gate.rs,用表格測試
工程基礎計畫 E9(grok-build 把權限判斷放在獨立、可測的 crate)。行為不變。 - gate::by_kind(關卡順序第 6 步):Mac 的 App(讀一次、改一次、送出每次)、螢幕(自己的桌面不問、看一次、每個 App 操作一次、不能點 AnyBot 自己)、帳號裡別台電腦的螢幕(看和操作各一次)、瀏覽器(讀已登入的分頁問一次)、其他照 tools::decide_on。要找目標 App 才問系統(照原本只在操作這台螢幕時)。 - gate::always_ask(第 8 步):付款/傳送/刪除這類點擊、寄信和傳訊息、沙盒外的 AppleScript,每次都問;拒絕的還是拒絕。回傳要不要清掉自動審核的備註、是不是不能一律允許。 - agent::spawn 只呼叫這兩個(少了約 100 行);要等人回答的第 3–5 步、要問模型的第 7 步留在原地。SCREEN_CONTROL_GRANT 和 screen_control_ask 搬到 gate.rs(說明照舊)。 - 功能地圖 safety.md 註明第 6、8 步的規則在 gate.rs。 已驗證:cargo test 569 全過(原本測核准的 agent 測試都過);新的表格測試 gate::tests 兩個(第 6 步 16 種情況、第 8 步 8 種);cargo clippy -D warnings 過。 未驗證:無。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3ef5f8b752
commit
10d29281c3
|
|
@ -23,7 +23,7 @@ Bot 要動到你的電腦、對外送出或做收不回來的事時,先問你
|
||||||
- 「在這台電腦上執行」:一律允許/每次詢問(預設)/永不允許。永不允許時直接拒絕、不問。
|
- 「在這台電腦上執行」:一律允許/每次詢問(預設)/永不允許。永不允許時直接拒絕、不問。
|
||||||
- 管理員鎖定:可鎖「直接操作」(AllowDirectMode)、「在這台電腦上執行」(AllowLocalExecution)、「經這台電腦轉送流量」(AllowBoxTrafficViaThisMac)。macOS 用 `defaults` 網域 com.anybot.AnyBot(可由 MDM 強制);其他系統讀 /etc/anybot/policy.json(優先)或家目錄 .anybot/policy.json。true/1/yes 允許;false/0/no 或看不懂的值鎖成關。每次打開設定重讀。
|
- 管理員鎖定:可鎖「直接操作」(AllowDirectMode)、「在這台電腦上執行」(AllowLocalExecution)、「經這台電腦轉送流量」(AllowBoxTrafficViaThisMac)。macOS 用 `defaults` 網域 com.anybot.AnyBot(可由 MDM 強制);其他系統讀 /etc/anybot/policy.json(優先)或家目錄 .anybot/policy.json。true/1/yes 允許;false/0/no 或看不懂的值鎖成關。每次打開設定重讀。
|
||||||
- 沒人在旁邊的一輪(例行任務、事件觸發、訊息管道)等核准超過 15 分鐘,自動結束、算出錯,免得卡住後面的排程和訊息。
|
- 沒人在旁邊的一輪(例行任務、事件觸發、訊息管道)等核准超過 15 分鐘,自動結束、算出錯,免得卡住後面的排程和訊息。
|
||||||
- 每一步的關卡順序:
|
- 每一步的關卡順序(第 6、8 步的規則在程式的 gate.rs,有一張表格測試):
|
||||||
1. 不用問:App 操作工具(刪除例行任務、教同事技能、裝外部技能、改自己或別的 Bot 的設定這類會先問)、讀公開網頁、AnyBot 瀏覽器(別人傳來的訊息不能用瀏覽器)。
|
1. 不用問:App 操作工具(刪除例行任務、教同事技能、裝外部技能、改自己或別的 Bot 的設定這類會先問)、讀公開網頁、AnyBot 瀏覽器(別人傳來的訊息不能用瀏覽器)。
|
||||||
2. 決定在哪台電腦;直接操作開著時,第 3、5 步算已開。
|
2. 決定在哪台電腦;直接操作開著時,第 3、5 步算已開。
|
||||||
3. 這個 Bot 的電腦工具沒開 → 問。
|
3. 這個 Bot 的電腦工具沒開 → 問。
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@
|
||||||
- 做法:每一步記下輸入、輸出、快取讀寫 token(供應商回什麼記什麼),寫進工作紀錄;一輪結束加總。`/usage`(或對話詳細資訊)看最近一天、一週,每個 Bot、每個模型。
|
- 做法:每一步記下輸入、輸出、快取讀寫 token(供應商回什麼記什麼),寫進工作紀錄;一輪結束加總。`/usage`(或對話詳細資訊)看最近一天、一週,每個 Bot、每個模型。
|
||||||
- 做完:跑一輪後工作紀錄有用量;`/usage` 列得出來。
|
- 做完:跑一輪後工作紀錄有用量;`/usage` 列得出來。
|
||||||
|
|
||||||
### E9. 核准關卡變成一個純函式 ⬜
|
### E9. 核准關卡變成一個純函式 ✅(第 6、8 步在 gate.rs;第 3–5 步要等人回答、第 7 步要問模型,留在 spawn)
|
||||||
- 做法:safety.md 的「每一步的關卡順序」九步,從 `agent::spawn` 抽成 `gate::decide(…) -> Gate`(輸入:工具、參數、電腦、開關、規則;輸出:直接做/問/拒絕和理由),一張表格式測試涵蓋每一步。`spawn` 只呼叫它。
|
- 做法:safety.md 的「每一步的關卡順序」九步,從 `agent::spawn` 抽成 `gate::decide(…) -> Gate`(輸入:工具、參數、電腦、開關、規則;輸出:直接做/問/拒絕和理由),一張表格式測試涵蓋每一步。`spawn` 只呼叫它。
|
||||||
- 做完:表格測試涵蓋九步和直接操作、一律放行、陌生人訊息;行為不變(舊測試全過)。
|
- 做完:表格測試涵蓋九步和直接操作、一律放行、陌生人訊息;行為不變(舊測試全過)。
|
||||||
|
|
||||||
|
|
|
||||||
120
src/agent.rs
120
src/agent.rs
|
|
@ -755,20 +755,6 @@ fn bad_action(tool: &str, args: &str) -> String {
|
||||||
format!("沒有執行:{tool} 要用 action 指定做什麼,這一步{given}。可以用的有:{}。參數直接放在最外層,例如 {example}。", actions.join("、"))
|
format!("沒有執行:{tool} 要用 action 指定做什麼,這一步{given}。可以用的有:{}。參數直接放在最外層,例如 {example}。", actions.join("、"))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 「一律允許」 for this computer's whole screen: what is offered when the desktop can't say
|
|
||||||
/// which app a step lands in (GNOME / KDE on Wayland never can; wlroots can't for a point).
|
|
||||||
const SCREEN_CONTROL_GRANT: &str = "computer:control";
|
|
||||||
|
|
||||||
/// What a screen step on this computer asks: (the grant 「一律允許」 keeps, the question). Per app
|
|
||||||
/// when the desktop names it; for the screen as a whole when it can't — the question then says
|
|
||||||
/// so. (An empty grant there used to make every click, key and scroll ask with no 「一律允許」.)
|
|
||||||
fn screen_control_ask(app: Option<&str>, via: &str, what: &str) -> (String, String) {
|
|
||||||
match app.map(str::trim).filter(|a| !a.is_empty()) {
|
|
||||||
Some(a) => (format!("app:{a}"), format!("讓 {via} 看到「{a}」,並在其中點擊和輸入:{what}")),
|
|
||||||
None => (SCREEN_CONTROL_GRANT.to_string(), format!("讓 {via} 在這台電腦的螢幕上點擊和輸入:{what}\n(這個桌面看不出是哪個 App;一律允許後,這台電腦的螢幕操作不再逐步詢問,付款、刪除、送出這類動作仍會問。)")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Failures in a row of one tool, all saying the same, after which it isn't run this turn.
|
/// Failures in a row of one tool, all saying the same, after which it isn't run this turn.
|
||||||
const SAME_FAILURES_STOP: usize = 3;
|
const SAME_FAILURES_STOP: usize = 3;
|
||||||
|
|
||||||
|
|
@ -3084,72 +3070,14 @@ pub fn spawn(job: Job, bot_id: u64, tx: Sender<(u64, StreamEvent)>, cancel: Arc<
|
||||||
if cancel.load(Ordering::Relaxed) {
|
if cancel.load(Ordering::Relaxed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Step 6 (gate.rs): what kind of step it is decides. Can't reach that computer, or
|
||||||
|
// the person said no to the pop-up: nothing to ask (reported below).
|
||||||
let decision = match (&target, &gate) {
|
let decision = match (&target, &gate) {
|
||||||
// Looking at the screen and using the mouse/keyboard each ask once (or always).
|
(Ok(t), Ok(())) => {
|
||||||
// The Mac's own apps: reading asks once per app, writing once per app (or each
|
let place = if is_screen && on_box { crate::gate::Place::OwnBox } else if on_device.is_some() { crate::gate::Place::Device(&t.name) } else { crate::gate::Place::Elsewhere };
|
||||||
// time), sending every single time (the original never keeps a grant for it).
|
crate::gate::by_kind(&call.name, &call.args, &what, &job.via_name, t, place, || computer::target_app(&call.name, &call.args), &grants)
|
||||||
(Ok(_), Ok(())) if is_mac => {
|
|
||||||
let app = crate::macapps::app_of(&call.name);
|
|
||||||
if crate::macapps::is_send(&call.name) && call.name == crate::macapps::SEND_MESSAGE && grants.iter().any(|g| g == "messages:no-ask") {
|
|
||||||
Decision::Allow
|
|
||||||
} else if crate::macapps::is_send(&call.name) {
|
|
||||||
Decision::Ask { reason: format!("用你的「{app}」{what}"), grant: String::new() }
|
|
||||||
} else if crate::macapps::is_write(&call.name) {
|
|
||||||
let grant = format!("macapp-write:{app}");
|
|
||||||
if grants.contains(&grant) { Decision::Allow } else { Decision::Ask { reason: format!("讓 {} 修改你的「{app}」:{what}", job.via_name), grant } }
|
|
||||||
} else {
|
|
||||||
let grant = format!("macapp:{app}");
|
|
||||||
if grants.contains(&grant) || grants.contains(&format!("macapp-write:{app}")) { Decision::Allow } else { Decision::Ask { reason: format!("讓 {} 讀取你的「{app}」:{what}", job.via_name), grant } }
|
|
||||||
}
|
}
|
||||||
}
|
_ => Decision::Allow,
|
||||||
// Its own desktop: nothing to ask, like the original's box.
|
|
||||||
(Ok(_), Ok(())) if is_screen && on_box => Decision::Allow,
|
|
||||||
(Ok(_), Ok(())) if is_screen && call.name == "computer_use_check_permissions" => Decision::Allow,
|
|
||||||
// 裝置連結: another of the person's computers; seeing and operating it ask once each.
|
|
||||||
(Ok(t), Ok(())) if is_screen && on_device.is_some() => {
|
|
||||||
let looks = matches!(call.name.as_str(), "computer_use_screenshot" | "computer_use_zoom" | "computer_use_show");
|
|
||||||
let grant = if looks { format!("device-see:{}", t.name) } else { format!("device-control:{}", t.name) };
|
|
||||||
if grants.contains(&grant) {
|
|
||||||
Decision::Allow
|
|
||||||
} else {
|
|
||||||
Decision::Ask { reason: format!("{}「{}」的螢幕:{what}", if looks { "看" } else { "操作" }, t.name), grant }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(Ok(_), Ok(())) if is_screen => {
|
|
||||||
if matches!(call.name.as_str(), "computer_use_screenshot" | "computer_use_zoom" | "computer_use_show") {
|
|
||||||
if grants.iter().any(|g| g == "computer:see") {
|
|
||||||
Decision::Allow
|
|
||||||
} else {
|
|
||||||
Decision::Ask { reason: format!("看你的螢幕畫面:{what}"), grant: "computer:see".into() }
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Per app, like the reference assistant: 「the reference assistant 可以看到 {app},並在其中點擊和輸入」.
|
|
||||||
let detected = computer::target_app(&call.name, &call.args);
|
|
||||||
if detected.as_deref().is_some_and(|a| computer::is_self_app(a, None)) {
|
|
||||||
Decision::Deny("Bot 不能點擊或操作 AnyBot 自己的視窗或核准按鈕。".into())
|
|
||||||
} else {
|
|
||||||
let (grant, reason) = screen_control_ask(detected.as_deref(), &job.via_name, &what);
|
|
||||||
if grants.iter().any(|g| g == SCREEN_CONTROL_GRANT || *g == grant) {
|
|
||||||
Decision::Allow
|
|
||||||
} else {
|
|
||||||
Decision::Ask { reason, grant }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Opening a link is visible and harmless; reading a logged-in tab asks first.
|
|
||||||
(Ok(_), Ok(())) if is_web => {
|
|
||||||
if call.name == "browser_read" && web::read_needs_permission(&call.args) && !grants.iter().any(|g| g == "browser:read") {
|
|
||||||
Decision::Ask { reason: "讀取你瀏覽器目前分頁的內容(包含已登入的頁面):目前的分頁".into(), grant: "browser:read".into() }
|
|
||||||
} else {
|
|
||||||
Decision::Allow
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(Ok(t), Ok(())) => tools::decide_on(&call.name, &call.args, t, &grants),
|
|
||||||
// Can't reach that computer: reported as a failed step below, not a safety block.
|
|
||||||
(Err(_), _) => Decision::Allow,
|
|
||||||
// The person said no to the pop-up: nothing more to ask.
|
|
||||||
(_, Err(_)) => Decision::Allow,
|
|
||||||
};
|
};
|
||||||
// 自動審核: when the rules say yes, continue without asking (JOecNC);
|
// 自動審核: when the rules say yes, continue without asking (JOecNC);
|
||||||
// when an "always ask" rule matches, stop and ask (Pca/iV).
|
// when an "always ask" rule matches, stop and ask (Pca/iV).
|
||||||
|
|
@ -3214,30 +3142,16 @@ pub fn spawn(job: Job, bot_id: u64, tx: Sender<(u64, StreamEvent)>, cancel: Arc<
|
||||||
.flatten()
|
.flatten()
|
||||||
.or_else(|| sensitive_in_history(&call.name, &parsed_args, &history))
|
.or_else(|| sensitive_in_history(&call.name, &parsed_args, &history))
|
||||||
.is_some_and(|l| computer::sensitive_words(&l));
|
.is_some_and(|l| computer::sensitive_words(&l));
|
||||||
let decision = if sensitive_click && gate.is_ok() && target.is_ok() && !matches!(decision, Decision::Deny(_)) {
|
// Step 8 (gate.rs): asked every time, over steps 6 and 7.
|
||||||
review_note.clear();
|
|
||||||
Decision::Ask { reason: format!("畫面上的目標說的是敏感操作:{what}(付款、傳送、刪除這類事,每一次都會先問你)"), grant: String::new() }
|
|
||||||
} else {
|
|
||||||
decision
|
|
||||||
};
|
|
||||||
// Sending mail or messages is approved one by one, even in direct mode — unless
|
|
||||||
// the person explicitly turned on `messages_no_ask` for Messages.
|
|
||||||
let skip_msg_ask = call.name == crate::macapps::SEND_MESSAGE && grants.iter().any(|g| g == "messages:no-ask");
|
let skip_msg_ask = call.name == crate::macapps::SEND_MESSAGE && grants.iter().any(|g| g == "messages:no-ask");
|
||||||
let decision = if is_mac && crate::macapps::is_send(&call.name) && !skip_msg_ask && gate.is_ok() && target.is_ok() {
|
|
||||||
review_note.clear();
|
|
||||||
Decision::Ask { reason: format!("用你的「{}」{what}(每一封都會先問你)", crate::macapps::app_of(&call.name)), grant: String::new() }
|
|
||||||
} else {
|
|
||||||
decision
|
|
||||||
};
|
|
||||||
// AppleScript that runs outside the sandbox can drive the person's apps: asked the
|
|
||||||
// first time even in direct mode, until they choose 「一律允許」.
|
|
||||||
let on_persons = matches!(target.as_ref().map(|t| &t.place), Ok(tools::Place::Local { .. } | tools::Place::Device { .. }));
|
let on_persons = matches!(target.as_ref().map(|t| &t.place), Ok(tools::Place::Local { .. } | tools::Place::Device { .. }));
|
||||||
let script = parsed_args["command"].as_str().unwrap_or("").to_string();
|
let decision = if gate.is_ok() && target.is_ok() {
|
||||||
let decision = if call.name == "shell" && on_persons && gate.is_ok() && !matches!(decision, Decision::Deny(_)) && tools::osascript_outside(&script).is_ok() && !grants.iter().any(|g| g == tools::OSASCRIPT_GRANT) {
|
let a = crate::gate::always_ask(&call.name, &parsed_args, &what, &job.via_name, decision, sensitive_click, on_persons, &grants);
|
||||||
|
if a.overridden {
|
||||||
review_note.clear();
|
review_note.clear();
|
||||||
must_ask = true;
|
}
|
||||||
let short: String = script.chars().take(200).collect();
|
must_ask |= a.must_ask;
|
||||||
Decision::Ask { reason: format!("讓 {} 在沙盒外執行 AppleScript(可以指揮你電腦上的 App):{short}", job.via_name), grant: tools::OSASCRIPT_GRANT.into() }
|
a.decision
|
||||||
} else {
|
} else {
|
||||||
decision
|
decision
|
||||||
};
|
};
|
||||||
|
|
@ -4919,13 +4833,13 @@ echo '<meta charset=utf-8><title>done</title>送出了' > done.html; (setsid pyt
|
||||||
fn a_desktop_that_cannot_name_the_app_still_offers_always() {
|
fn a_desktop_that_cannot_name_the_app_still_offers_always() {
|
||||||
// GNOME / KDE on Wayland: no app name, so the grant is the screen's (it used to be none,
|
// GNOME / KDE on Wayland: no app name, so the grant is the screen's (it used to be none,
|
||||||
// and every click asked with no 「一律允許」).
|
// and every click asked with no 「一律允許」).
|
||||||
let (grant, q) = screen_control_ask(None, "小幫手", "點一下 (400, 300)");
|
let (grant, q) = crate::gate::screen_control_ask(None, "小幫手", "點一下 (400, 300)");
|
||||||
assert_eq!(grant, SCREEN_CONTROL_GRANT);
|
assert_eq!(grant, crate::gate::SCREEN_CONTROL_GRANT);
|
||||||
assert!(q.contains("看不出是哪個 App") && q.contains("付款"), "{q}");
|
assert!(q.contains("看不出是哪個 App") && q.contains("付款"), "{q}");
|
||||||
let (grant, q) = screen_control_ask(Some("firefox"), "小幫手", "點一下");
|
let (grant, q) = crate::gate::screen_control_ask(Some("firefox"), "小幫手", "點一下");
|
||||||
assert_eq!(grant, "app:firefox");
|
assert_eq!(grant, "app:firefox");
|
||||||
assert!(q.contains("「firefox」"));
|
assert!(q.contains("「firefox」"));
|
||||||
assert_eq!(screen_control_ask(Some(" "), "小幫手", "x").0, SCREEN_CONTROL_GRANT);
|
assert_eq!(crate::gate::screen_control_ask(Some(" "), "小幫手", "x").0, crate::gate::SCREEN_CONTROL_GRANT);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,190 @@
|
||||||
|
//! 核准關卡的規則: the part of a step's approval that is rules alone, out of the turn loop
|
||||||
|
//! (agent::spawn) so it can be read in one place and tested as a table. What safety.md's
|
||||||
|
//! 「每一步的關卡順序」 says, steps 6 and 8:
|
||||||
|
//!
|
||||||
|
//! - `by_kind` (step 6): what kind of step it is decides — the Mac's own apps (reading once per
|
||||||
|
//! app, writing once per app, sending every time), the screen (its own desktop free; looking
|
||||||
|
//! once; operating once per app; never AnyBot's own window), another computer's screen (seeing
|
||||||
|
//! and operating once each), the browser (reading a logged-in tab once), everything else by
|
||||||
|
//! tools::decide_on (commands and files).
|
||||||
|
//! - `always_ask` (step 8): what is asked every time whatever came before — a click on a target
|
||||||
|
//! that says paying, sending or deleting; mail and messages; AppleScript outside the sandbox.
|
||||||
|
//!
|
||||||
|
//! What stays in the loop: asking the person to turn computer tools or this computer on (steps
|
||||||
|
//! 3–5, they wait for an answer), and the 自動審核 model call (step 7). Engineering plan E9.
|
||||||
|
|
||||||
|
use crate::tools::{self, Decision};
|
||||||
|
|
||||||
|
/// 「一律允許」 for this computer's whole screen: what is offered when the desktop can't say
|
||||||
|
/// which app a step lands in (GNOME / KDE on Wayland never can; wlroots can't for a point).
|
||||||
|
pub const SCREEN_CONTROL_GRANT: &str = "computer:control";
|
||||||
|
|
||||||
|
const LOOKS: [&str; 3] = ["computer_use_screenshot", "computer_use_zoom", "computer_use_show"];
|
||||||
|
|
||||||
|
/// What a screen step on this computer asks: (the grant 「一律允許」 keeps, the question). Per app
|
||||||
|
/// when the desktop names it; for the screen as a whole when it can't — the question then says
|
||||||
|
/// so. (An empty grant there used to make every click, key and scroll ask with no 「一律允許」.)
|
||||||
|
pub fn screen_control_ask(app: Option<&str>, via: &str, what: &str) -> (String, String) {
|
||||||
|
match app.map(str::trim).filter(|a| !a.is_empty()) {
|
||||||
|
Some(a) => (format!("app:{a}"), format!("讓 {via} 看到「{a}」,並在其中點擊和輸入:{what}")),
|
||||||
|
None => (SCREEN_CONTROL_GRANT.to_string(), format!("讓 {via} 在這台電腦的螢幕上點擊和輸入:{what}\n(這個桌面看不出是哪個 App;一律允許後,這台電腦的螢幕操作不再逐步詢問,付款、刪除、送出這類動作仍會問。)")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where a step runs, as the rules need it.
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
pub enum Place<'a> {
|
||||||
|
/// The bot's own desktop (the box).
|
||||||
|
OwnBox,
|
||||||
|
/// Another computer of the person's account (by its name).
|
||||||
|
Device(&'a str),
|
||||||
|
/// Anywhere else (this computer, an SSH computer…).
|
||||||
|
Elsewhere,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Step 6: the decision by what kind of step it is. `what` is the step in words (for the card),
|
||||||
|
/// `via` the bot's name, `app` finds the app a screen step aims at (computer::target_app: only
|
||||||
|
/// asked for an operating step on this computer's screen).
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub fn by_kind(name: &str, args: &str, what: &str, via: &str, target: &tools::Target, place: Place, app: impl FnOnce() -> Option<String>, grants: &[String]) -> Decision {
|
||||||
|
let has = |g: &str| grants.iter().any(|x| x == g);
|
||||||
|
if crate::macapps::is_mac_tool(name) {
|
||||||
|
let app = crate::macapps::app_of(name);
|
||||||
|
return if crate::macapps::is_send(name) && name == crate::macapps::SEND_MESSAGE && has("messages:no-ask") {
|
||||||
|
Decision::Allow
|
||||||
|
} else if crate::macapps::is_send(name) {
|
||||||
|
Decision::Ask { reason: format!("用你的「{app}」{what}"), grant: String::new() }
|
||||||
|
} else if crate::macapps::is_write(name) {
|
||||||
|
let grant = format!("macapp-write:{app}");
|
||||||
|
if has(&grant) { Decision::Allow } else { Decision::Ask { reason: format!("讓 {via} 修改你的「{app}」:{what}"), grant } }
|
||||||
|
} else {
|
||||||
|
let grant = format!("macapp:{app}");
|
||||||
|
if has(&grant) || has(&format!("macapp-write:{app}")) { Decision::Allow } else { Decision::Ask { reason: format!("讓 {via} 讀取你的「{app}」:{what}"), grant } }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if crate::computer::is_computer_tool(name) {
|
||||||
|
if place == Place::OwnBox || name == "computer_use_check_permissions" {
|
||||||
|
return Decision::Allow;
|
||||||
|
}
|
||||||
|
let looks = LOOKS.contains(&name);
|
||||||
|
if let Place::Device(device) = place {
|
||||||
|
let grant = if looks { format!("device-see:{device}") } else { format!("device-control:{device}") };
|
||||||
|
return if has(&grant) { Decision::Allow } else { Decision::Ask { reason: format!("{}「{device}」的螢幕:{what}", if looks { "看" } else { "操作" }), grant } };
|
||||||
|
}
|
||||||
|
if looks {
|
||||||
|
return if has("computer:see") { Decision::Allow } else { Decision::Ask { reason: format!("看你的螢幕畫面:{what}"), grant: "computer:see".into() } };
|
||||||
|
}
|
||||||
|
let app = app();
|
||||||
|
if app.as_deref().is_some_and(|a| crate::computer::is_self_app(a, None)) {
|
||||||
|
return Decision::Deny("Bot 不能點擊或操作 AnyBot 自己的視窗或核准按鈕。".into());
|
||||||
|
}
|
||||||
|
let (grant, reason) = screen_control_ask(app.as_deref(), via, what);
|
||||||
|
return if has(SCREEN_CONTROL_GRANT) || has(&grant) { Decision::Allow } else { Decision::Ask { reason, grant } };
|
||||||
|
}
|
||||||
|
if crate::web::is_web_tool(name) {
|
||||||
|
return if name == "browser_read" && crate::web::read_needs_permission(args) && !has("browser:read") {
|
||||||
|
Decision::Ask { reason: "讀取你瀏覽器目前分頁的內容(包含已登入的頁面):目前的分頁".into(), grant: "browser:read".into() }
|
||||||
|
} else {
|
||||||
|
Decision::Allow
|
||||||
|
};
|
||||||
|
}
|
||||||
|
tools::decide_on(name, args, target, grants)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What step 8 found (the decision, and whether it may not be granted away or reviewed).
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub struct Always {
|
||||||
|
pub decision: Decision,
|
||||||
|
/// Asked by step 8 itself: the review's note doesn't stand.
|
||||||
|
pub overridden: bool,
|
||||||
|
/// No 「一律允許」, no review, no direct mode past it.
|
||||||
|
pub must_ask: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Step 8: what is asked every time, over whatever steps 6 and 7 said (a refusal stays one).
|
||||||
|
/// `sensitive_click`: the click's target reads like paying, sending or deleting;
|
||||||
|
/// `on_persons`: the step runs on this computer or another of the person's.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
pub fn always_ask(name: &str, args: &serde_json::Value, what: &str, via: &str, decision: Decision, sensitive_click: bool, on_persons: bool, grants: &[String]) -> Always {
|
||||||
|
let has = |g: &str| grants.iter().any(|x| x == g);
|
||||||
|
if matches!(decision, Decision::Deny(_)) {
|
||||||
|
return Always { decision, overridden: false, must_ask: false };
|
||||||
|
}
|
||||||
|
if sensitive_click {
|
||||||
|
return Always { decision: Decision::Ask { reason: format!("畫面上的目標說的是敏感操作:{what}(付款、傳送、刪除這類事,每一次都會先問你)"), grant: String::new() }, overridden: true, must_ask: false };
|
||||||
|
}
|
||||||
|
let skip_msg_ask = name == crate::macapps::SEND_MESSAGE && has("messages:no-ask");
|
||||||
|
if crate::macapps::is_mac_tool(name) && crate::macapps::is_send(name) && !skip_msg_ask {
|
||||||
|
return Always { decision: Decision::Ask { reason: format!("用你的「{}」{what}(每一封都會先問你)", crate::macapps::app_of(name)), grant: String::new() }, overridden: true, must_ask: false };
|
||||||
|
}
|
||||||
|
let script = args["command"].as_str().unwrap_or("");
|
||||||
|
if name == "shell" && on_persons && tools::osascript_outside(script).is_ok() && !has(tools::OSASCRIPT_GRANT) {
|
||||||
|
let short: String = script.chars().take(200).collect();
|
||||||
|
return Always { decision: Decision::Ask { reason: format!("讓 {via} 在沙盒外執行 AppleScript(可以指揮你電腦上的 App):{short}"), grant: tools::OSASCRIPT_GRANT.into() }, overridden: true, must_ask: true };
|
||||||
|
}
|
||||||
|
Always { decision, overridden: false, must_ask: false }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn here() -> tools::Target {
|
||||||
|
tools::Target { name: "這台".into(), place: tools::Place::Local { workdir: std::env::temp_dir() } }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ask_grant(d: &Decision) -> Option<&str> {
|
||||||
|
match d {
|
||||||
|
Decision::Ask { grant, .. } => Some(grant.as_str()),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Step 6 as a table: (tool, place, app, grants) → allowed, asked (with what grant), refused.
|
||||||
|
#[test]
|
||||||
|
fn step_six_by_kind() {
|
||||||
|
let g = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
|
||||||
|
let t = here();
|
||||||
|
let d = |name: &str, args: &str, place: Place, app: Option<&str>, grants: &[String]| by_kind(name, args, "做事", "Bot", &t, place, || app.map(String::from), grants);
|
||||||
|
// The Mac's apps.
|
||||||
|
let send = crate::macapps::SEND_MESSAGE;
|
||||||
|
assert_eq!(ask_grant(&d(send, "{}", Place::Elsewhere, None, &[])), Some(""), "sending: every time, nothing to keep");
|
||||||
|
assert_eq!(d(send, "{}", Place::Elsewhere, None, &g(&["messages:no-ask"])), Decision::Allow);
|
||||||
|
// The screen.
|
||||||
|
assert_eq!(d("computer_use_click", "{}", Place::OwnBox, None, &[]), Decision::Allow, "its own desktop");
|
||||||
|
assert_eq!(ask_grant(&d("computer_use_screenshot", "{}", Place::Elsewhere, None, &[])), Some("computer:see"));
|
||||||
|
assert_eq!(d("computer_use_screenshot", "{}", Place::Elsewhere, None, &g(&["computer:see"])), Decision::Allow);
|
||||||
|
assert_eq!(ask_grant(&d("computer_use_click", "{}", Place::Elsewhere, Some("Safari"), &[])), Some("app:Safari"));
|
||||||
|
assert_eq!(d("computer_use_click", "{}", Place::Elsewhere, Some("Safari"), &g(&[SCREEN_CONTROL_GRANT])), Decision::Allow);
|
||||||
|
assert_eq!(ask_grant(&d("computer_use_click", "{}", Place::Elsewhere, None, &[])), Some(SCREEN_CONTROL_GRANT));
|
||||||
|
assert!(matches!(d("computer_use_click", "{}", Place::Elsewhere, Some("AnyBot"), &g(&[SCREEN_CONTROL_GRANT])), Decision::Deny(_)), "never AnyBot's own window");
|
||||||
|
// Another computer's screen.
|
||||||
|
assert_eq!(ask_grant(&d("computer_use_screenshot", "{}", Place::Device("筆電"), None, &[])), Some("device-see:筆電"));
|
||||||
|
assert_eq!(ask_grant(&d("computer_use_click", "{}", Place::Device("筆電"), None, &g(&["device-see:筆電"]))), Some("device-control:筆電"));
|
||||||
|
// The browser: opening is free.
|
||||||
|
assert_eq!(d("browser_open", r#"{"url":"https://example.com"}"#, Place::Elsewhere, None, &[]), Decision::Allow);
|
||||||
|
// Commands go by the command rules.
|
||||||
|
assert!(matches!(d("shell", r#"{"command":"sudo rm -rf /"}"#, Place::Elsewhere, None, &[]), Decision::Deny(_)));
|
||||||
|
assert_eq!(d("shell", r#"{"command":"ls"}"#, Place::Elsewhere, None, &[]), Decision::Allow);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Step 8 as a table: what is asked every time, over an allow; a refusal stays one.
|
||||||
|
#[test]
|
||||||
|
fn step_eight_always_asks() {
|
||||||
|
let a = |name: &str, args: serde_json::Value, d: Decision, sensitive: bool, persons: bool, grants: &[String]| always_ask(name, &args, "做事", "Bot", d, sensitive, persons, grants);
|
||||||
|
let none = serde_json::json!({});
|
||||||
|
let r = a("computer_use_click", none.clone(), Decision::Allow, true, true, &[]);
|
||||||
|
assert!(r.overridden && !r.must_ask && ask_grant(&r.decision) == Some(""), "a pay/send/delete click");
|
||||||
|
let r = a(crate::macapps::SEND_MESSAGE, none.clone(), Decision::Allow, false, true, &[]);
|
||||||
|
assert!(r.overridden && ask_grant(&r.decision) == Some(""), "a message, each one");
|
||||||
|
let r = a(crate::macapps::SEND_MESSAGE, none.clone(), Decision::Allow, false, true, &["messages:no-ask".into()]);
|
||||||
|
assert_eq!(r.decision, Decision::Allow, "unless the person said messages may go without asking");
|
||||||
|
let osa = serde_json::json!({"command": "osascript -e 'tell application \"Music\" to pause'"});
|
||||||
|
let r = a("shell", osa.clone(), Decision::Allow, false, true, &[]);
|
||||||
|
assert!(r.must_ask && ask_grant(&r.decision) == Some(tools::OSASCRIPT_GRANT), "AppleScript outside the sandbox: {r:?}");
|
||||||
|
assert_eq!(a("shell", osa.clone(), Decision::Allow, false, true, &[tools::OSASCRIPT_GRANT.into()]).decision, Decision::Allow);
|
||||||
|
assert_eq!(a("shell", osa, Decision::Allow, false, false, &[]).decision, Decision::Allow, "not on the person's computer");
|
||||||
|
let deny = Decision::Deny("不行".into());
|
||||||
|
assert_eq!(a("computer_use_click", none, deny.clone(), true, true, &[]).decision, deny, "a refusal stays a refusal");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -75,6 +75,7 @@ mod always;
|
||||||
mod secrets;
|
mod secrets;
|
||||||
mod logfile;
|
mod logfile;
|
||||||
mod net;
|
mod net;
|
||||||
|
mod gate;
|
||||||
mod usage;
|
mod usage;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod testkit;
|
mod testkit;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue