Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions crates/tw-control/src/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
//! 别的都不用点头:只改数据(哪怕是工具调用插件的)、停用、删、排顺序,装、打开、改、批准
//! 不碰工具调用的插件。
//!
//! **判断只认真的编出来的 manifest**,不认显示用的缓存(它是用户目录里的一个文件,被人改了
//! 只是显示不对);**读不出旧的那一份要什么权限的按改得了算**:它此刻跑不了,可一旦又跑得
//! **判断只认真的编出来的 manifest**,不认休眠的插件手里显示用的那一份(缓存是用户目录里的
//! 一个文件,被人改了只是显示不对;照源码读的那一份说不出钩子,也不保证编得过);**读不出旧的那一份要什么权限的按改得了算**:它此刻跑不了,可一旦又跑得
//! 了(运行时恢复了),网页替它做的事就生效了。所以这里也不假设调用方看过什么:源码在这里
//! 再编一遍,批准时磁盘上的文件得正好是调用方看过的那一份(哈希核对)。

Expand Down Expand Up @@ -601,7 +601,7 @@ fn plugins_in(text: &str) -> Option<Vec<tw_config::Plugin>> {
}

/// 批准的那一份改不改得了回答里的工具调用。改得了、**或者读不出来**(批准的那份字节没了、
/// 编不成)就是 `Some(名字)` —— 读不出来按改得了算。真的编一遍,不认显示用的缓存
/// 编不成)就是 `Some(名字)` —— 读不出来按改得了算。真的编一遍,不认显示用的那一份
async fn approved_steers(s: &ControlState, id: &str, sha256: &str) -> Option<String> {
match compiled_manifest(s, id, sha256).await {
Some(m) if !steers(&m) => None,
Expand Down Expand Up @@ -756,8 +756,8 @@ async fn store(
}

/// 这个插件**真的编出来**的 manifest。开着的插件手里就有;休眠的(停用着、运行时没起)、
/// 加载出错的,把批准的那份字节编一遍。**安全上的判断只认它**,不认显示用的缓存。读不到
/// 批准的那份字节、编不成是 None
/// 加载出错的,把批准的那份字节编一遍。**安全上的判断只认它**,不认休眠的插件手里显示用的
/// 那一份(缓存里的、照源码读的)。读不到批准的那份字节、编不成是 None
async fn compiled_manifest(s: &ControlState, id: &str, sha256: &str) -> Option<Manifest> {
let held = s
.gateway
Expand Down
167 changes: 159 additions & 8 deletions crates/tw-control/tests/plugin_defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,58 @@ async fn the_shipped_defaults_go_in_turned_off_without_starting_the_sandbox() {
assert_eq!(engine.count(), 0);
}

/// core 升级之后(显示用的缓存是上一版写的,整份作废;或者缓存没了):随 core 发的那几个
/// 停用着,列表照样说得全它们是什么 —— 照文件里写着的读,**不起运行时**,也不把读出来的写进
/// 缓存。说得全:名字、说明、权限、处理哪几种请求、范围、出错时怎么办、设置
#[tokio::test]
async fn after_an_upgrade_the_shipped_defaults_still_list_in_full_without_the_sandbox() {
let b = bed_with("real", Counting::new(real()));
Seeder::shipped().seed(&b.mgr).await;
let stored = std::fs::read_to_string(cache_file(&b)).unwrap();
let version = tw_gateway::plugin::manifests::version();
assert!(stored.contains(&version), "{stored}");
for stale in [Some(stored.replace(&version, "0.63.0/old/3")), None] {
match &stale {
Some(text) => std::fs::write(cache_file(&b), text).unwrap(),
None => std::fs::remove_file(cache_file(&b)).unwrap(),
}
let engine = Counting::new(real());
let again = b.restart(engine.clone());
assert_eq!(Seeder::shipped().seed(&again.mgr).await, Seeded::default());
for (id, _) in tw_gateway::plugin::defaults::ALL {
let m = tw_gateway::plugin::defaults::manifest(id).unwrap();
let v = again.plugin(id).await;
assert_eq!(v["status"], json!({"kind": "disabled"}), "{id}: {v}");
assert_eq!(v["name"], m.name.as_str(), "{id}: {v}");
assert_eq!(v["description"], json!(m.description), "{id}: {v}");
assert_eq!(v["permissions"], json!(m.permissions), "{id}: {v}");
assert_eq!(v["requests"], json!(m.requests), "{id}: {v}");
assert_eq!(v["on_error"], json!(m.on_error), "{id}: {v}");
assert_eq!(v["reply_mode"], json!(m.reply_mode), "{id}: {v}");
assert_eq!(
v["scope"],
json!({"clients": m.scope.clients, "models": m.scope.models,
"upstreams": m.scope.upstreams}),
"{id}: {v}"
);
let schema: Vec<Value> = m
.settings
.iter()
.map(|s| json!({"key": s.key, "kind": s.kind, "label": s.label, "value": s.value}))
.collect();
assert_eq!(v["settings_schema"], json!(schema), "{id}: {v}");
let a = again.gw.runtime().plugins.get(id).unwrap().clone();
assert!(a.ready().unwrap().dormant(), "{id}");
}
assert_eq!(engine.count(), 0, "listing started the sandbox");
assert_eq!(
std::fs::read_to_string(cache_file(&b)).ok(),
stale,
"the manifests read from the files went into the cache"
);
}
}

/// 打开一个默认插件:这时才真的编(起运行时),编出来就能跑
#[tokio::test]
async fn enabling_a_default_compiles_it_and_then_it_runs() {
Expand Down Expand Up @@ -905,35 +957,134 @@ async fn a_tampered_manifest_cache_cannot_hide_tool_calls_from_the_confirmation(
);
}

/// 缓存对不上(版本不对、是别的字节的):不认,插件只按 id 和状态列出来,也不为此编
/// 缓存对不上(版本不对、是别的字节的、写坏了,或者干脆没有):不认,也不为此编 —— 列表
/// 照插件文件里写着的显示,不是缓存里那一条说的。照文件读的那一份不写回缓存
#[tokio::test]
async fn a_cache_entry_that_does_not_match_is_ignored() {
let b = bed();
let src = install_calls(&b).await;
let stored: Value =
let mut stored: Value =
serde_json::from_str(&std::fs::read_to_string(cache_file(&b)).unwrap()).unwrap();
let sha_now = sha(&src);
assert!(stored["manifests"][&sha_now].is_object(), "{stored}");
// 缓存里那一条要是被用上了,列表上就是这个名字
stored["manifests"][&sha_now]["name"] = json!("缓存里的");

let mut old_version = stored.clone();
old_version["version"] = json!("0.0.0/old/1");
let mut other_bytes = stored.clone();
let entry = other_bytes["manifests"][&sha_now].clone();
other_bytes["manifests"] = json!({ sha_now.replace(|c: char| c != '0', "0"): entry });
for (what, file) in [
("another version", old_version.to_string()),
("other bytes", other_bytes.to_string()),
("a broken file", "{ not json".to_string()),
("another version", Some(old_version.to_string())),
("other bytes", Some(other_bytes.to_string())),
("a broken file", Some("{ not json".to_string())),
("no file", None),
] {
std::fs::write(cache_file(&b), file).unwrap();
match &file {
Some(text) => std::fs::write(cache_file(&b), text).unwrap(),
None => std::fs::remove_file(cache_file(&b)).unwrap(),
}
let engine = Counting::new(Arc::new(FakeEngine));
let again = b.restart(engine.clone());
let v = again.plugin("calls").await;
assert_eq!(v["name"], "calls", "{what}: {v}");
assert_eq!(v["permissions"], json!([]), "{what}: {v}");
assert_eq!(v["name"], "改工具调用", "{what}: {v}");
assert_eq!(v["permissions"], json!(["reply_tool_calls"]), "{what}: {v}");
assert_eq!(v["requests"], json!(["conversation"]), "{what}: {v}");
assert_eq!(v["settings_schema"][0]["label"], "方式", "{what}: {v}");
assert_eq!(v["settings_schema"][0]["value"], "a", "{what}: {v}");
assert_eq!(v["status"], json!({"kind": "disabled"}), "{what}: {v}");
assert_eq!(engine.count(), 0, "{what}");
// 照文件读的那一份没有写进去:缓存原样(别的字节的那一条照旧清掉)
let after = std::fs::read_to_string(cache_file(&b)).ok();
if what == "other bytes" {
let after: Value = serde_json::from_str(after.as_deref().unwrap()).unwrap();
assert_eq!(after["manifests"], json!({}), "{what}: {after}");
} else {
assert_eq!(after, file, "{what}: the cache was written");
}
}
}

/// 编出来的说它改得了工具调用,照文件读的那一份说它只改系统提示(模块代码在运行时多要了
/// 权限 —— 真的沙箱不收这种插件,这里用一个这么「编」的引擎)
struct Steering;

struct Steered {
manifest: tw_gateway::plugin::Manifest,
sha256: [u8; 32],
}

impl PluginHost for Steered {
fn manifest(&self) -> &tw_gateway::plugin::Manifest {
&self.manifest
}
fn sha256(&self) -> [u8; 32] {
self.sha256
}
}

impl Engine for Steering {
fn load(&self, source: &[u8]) -> Result<Arc<dyn PluginHost>, LoadError> {
let host = FakeEngine.load(source)?;
let mut manifest = host.manifest().clone();
manifest
.permissions
.push(tw_api::Permission::ReplyToolCalls);
manifest.hooks.tool_call = true;
Ok(Arc::new(Steered {
manifest,
sha256: host.sha256(),
}))
}
}

/// 缓存作废之后,休眠的插件列表上是照文件读的那一份;**判断不认它**:网页那条路打开它、改它的
/// 代码,照样先真的编一遍、按编出来的权限要点头
#[tokio::test]
async fn a_manifest_read_without_compiling_is_not_what_the_confirmation_trusts() {
let b = bed();
let src = source(
json!({"name": "只改系统提示", "api": 1, "permissions": ["system"]}),
&["onRequest"],
);
let (st, v) = call(
&b.app,
"POST",
"/plugins",
Some(json!({"source": src, "id": "quiet", "enabled": false})),
)
.await;
assert_eq!(st, StatusCode::OK, "{v}");
std::fs::remove_file(cache_file(&b)).unwrap();

let engine = Counting::new(Arc::new(Steering));
let again = b.restart(engine.clone());
let v = again.plugin("quiet").await;
assert_eq!(v["name"], "只改系统提示", "{v}");
assert_eq!(v["permissions"], json!(["system"]), "{v}");
assert_eq!(engine.count(), 0);
for (what, source) in [
("turning it on", src.clone()),
("a code change", format!("{src}// 多一行\n")),
] {
let (st, v) = call(
&again.app,
"PUT",
"/plugins/quiet",
Some(json!({"source": source, "enabled": what == "turning it on",
"base_version": again.version().await})),
)
.await;
assert_eq!(st, StatusCode::FORBIDDEN, "{what}: {v}");
assert_eq!(v["code"], "control.plugin.needs_confirmation", "{what}");
}
assert!(
engine.count() > 0,
"the decision was not made on a compiled manifest"
);
assert!(!again.entry("quiet").unwrap().enabled);
assert_eq!(again.read(again.file("quiet")), src);
}

/// `wsl-paths` 改得了回答里的工具调用:网页那条路打不开它,确认过的那条打得开;改它的
Expand Down
67 changes: 67 additions & 0 deletions crates/tw-gateway/src/plugin/defaults/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,73 @@ mod tests {
assert!(changed[0].1.contains("value: \"English\""), "{changed:?}");
}

/// 不编、照源码读出来的(休眠时列表上显示的那一份,[`crate::plugin::source::display_manifest`])
/// 和真的沙箱编出来的一个字段一个字段都一样,只差钩子(说不出来,空着)。用户在界面上改过
/// 出错时怎么办、范围和设置的值之后也一样
#[test]
fn every_default_reads_without_compiling_as_the_sandbox_compiles_it() {
let engine = crate::plugin::default_engine();
for (id, source) in ALL {
let shipped = source.to_string();
let settings: std::collections::BTreeMap<_, _> = engine
.load(source.as_bytes())
.unwrap_or_else(|e| panic!("{id} does not load: {}", e.msg()))
.manifest()
.settings
.iter()
.map(|s| {
let v = match s.kind {
tw_api::SettingKind::String => tw_api::SettingValue::String("改过".into()),
tw_api::SettingKind::Number => tw_api::SettingValue::Number(7.5),
tw_api::SettingKind::Boolean => {
tw_api::SettingValue::Bool(s.value != serde_json::json!(true))
}
};
(s.key.clone(), v)
})
.collect();
let edited = crate::plugin::source::rewrite(
source,
tw_api::OnError::Skip,
&tw_api::PluginScope {
clients: vec!["codex".into()],
models: vec!["claude-*".into(), "gpt-*".into()],
upstreams: vec!["relay".into()],
},
&settings,
)
.unwrap_or_else(|e| panic!("{id}: {e:?}"));
for (what, src) in [("shipped", shipped), ("edited", edited)] {
let compiled = engine.load(src.as_bytes()).unwrap().manifest().clone();
let shown = crate::plugin::source::display_manifest(src.as_bytes())
.unwrap_or_else(|| panic!("{id} ({what}) does not read without compiling"));
assert_eq!(shown.name, compiled.name, "{id} ({what})");
assert_eq!(shown.api, compiled.api, "{id} ({what})");
assert_eq!(shown.description, compiled.description, "{id} ({what})");
assert_eq!(shown.permissions, compiled.permissions, "{id} ({what})");
assert_eq!(shown.requests, compiled.requests, "{id} ({what})");
assert_eq!(shown.scope, compiled.scope, "{id} ({what})");
assert_eq!(shown.on_error, compiled.on_error, "{id} ({what})");
assert_eq!(shown.reply_mode, compiled.reply_mode, "{id} ({what})");
assert_eq!(shown.settings, compiled.settings, "{id} ({what})");
assert_eq!(
shown.hooks,
crate::plugin::Hooks::default(),
"{id} ({what})"
);
// 上面一条一条比过了;这一条兜住以后加的字段
assert_eq!(
crate::plugin::Manifest {
hooks: compiled.hooks,
..shown
},
compiled,
"{id} ({what})"
);
}
}
}

/// 每一个都在真的沙箱里编得成:装不上的默认插件只会在日志里留一行
#[test]
fn every_default_compiles_in_the_real_sandbox() {
Expand Down
5 changes: 3 additions & 2 deletions crates/tw-gateway/src/plugin/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ pub trait PluginHost: Send + Sync {
fn sha256(&self) -> [u8; 32];

/// **休眠的插件**:停用着、运行时没起,还没真的编过(见 [`crate::plugin::load`])。
/// 它跑不了任何钩子,`manifest()` 是缓存里的那一份(或者只有名字的占位),**只拿来
/// 显示** —— 要跑它(试跑)、要按它的权限做判断,先真的编一遍
/// 它跑不了任何钩子,`manifest()` 是缓存里的那一份、照源码读的那一份(钩子是空的),
/// 或者只有名字的占位,**只拿来显示** —— 要跑它(试跑)、要按它的权限做判断,先真的
/// 编一遍
fn dormant(&self) -> bool {
false
}
Expand Down
Loading
Loading