From 56982a1e13140a6ba2ec7481b92056edf896c3d3 Mon Sep 17 00:00:00 2001 From: fffonion Date: Mon, 9 Nov 2026 07:02:56 +0800 Subject: [PATCH 1/7] fix(gateway): wire configured providers and headless HTTPS transport --- docs/configuration.md | 9 +- src/bin/rustscript-agent-gateway.rs | 228 +++++++++++- src/gateway/mod.rs | 313 ++++++++++++++++ tests/gateway_provider_config_tests.rs | 482 +++++++++++++++++++++++++ 4 files changed, 1020 insertions(+), 12 deletions(-) create mode 100644 tests/gateway_provider_config_tests.rs diff --git a/docs/configuration.md b/docs/configuration.md index 39dd456..b4f86f7 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -62,7 +62,14 @@ the resolved home and forwards `config_present` plus the loaded every decision: a present config is authoritative, and a present-but-invalid config source fails closed with no env fallback; a missing config falls back to `RUSTSCRIPT_AGENT_SOURCE`, then the deprecated `RUSTSCRIPT_AGENT_SCRIPT`, -then the bundled coding entry. +then the bundled coding entry. The same loaded document supplies the gateway's +default `model.provider`/`model.model` and registers each configured provider's +public `protocol`, `base_url`, and optional named `auth` credential ID as a +`ProviderProfile` before the listener binds. A missing document keeps the +built-in `local-agent` defaults; an invalid provider profile fails startup. +The operator provider factory uses headless native HTTPS and never opens a +browser or a callback listener; the workspace host still receives no OAuth +transport. The gateway rejects a blank value. When the workspace host resolves the home, `AgentPaths` requires a non-empty absolute path with no parent-directory diff --git a/src/bin/rustscript-agent-gateway.rs b/src/bin/rustscript-agent-gateway.rs index 357ca2c..ec8e8bf 100644 --- a/src/bin/rustscript-agent-gateway.rs +++ b/src/bin/rustscript-agent-gateway.rs @@ -12,7 +12,8 @@ use std::{ use rustscript_agent::{ AgentGatewayConfig, AgentGatewayState, ConfigFile, TelegramConfig, build_agent_gateway_app, - config_file::{AgentPaths, CompactionSettings, ConfigFileError}, + config::ProviderProfile, + config_file::{AgentPaths, ConfigFileError}, gateway::telegram::{TelegramAdapter, spawn_telegram_adapter}, plugins::{InstallAuthorization, PublisherKey}, }; @@ -57,16 +58,21 @@ async fn main() -> Result<(), Box> { let explicit_home = env::var_os("RUSTSCRIPT_AGENT_HOME").is_some(); let paths = AgentPaths::resolve() .map_err(|_| std::io::Error::other("agent home could not be resolved"))?; - let (config_present, config_source, compaction_settings) = - match ConfigFile::load(paths.config_path()) { - Ok(config) => (true, Some(config.agent.source), config.compaction), - Err(ConfigFileError::MissingFile { .. }) => { - (false, None, CompactionSettings::default()) - } - Err(_) => { - return Err(std::io::Error::other("agent config.yaml could not be loaded").into()); - } - }; + let loaded_config = match ConfigFile::load(paths.config_path()) { + Ok(config) => Some(config), + Err(ConfigFileError::MissingFile { .. }) => None, + Err(_) => { + return Err(std::io::Error::other("agent config.yaml could not be loaded").into()); + } + }; + let config_present = loaded_config.is_some(); + let config_source = loaded_config + .as_ref() + .map(|config| config.agent.source.clone()); + let compaction_settings = loaded_config + .as_ref() + .map(|config| config.compaction.clone()) + .unwrap_or_default(); if let Some(hosts) = env_value("RUSTSCRIPT_AGENT_ALLOW_HOSTS", "PD_EDGE_AGENT_ALLOW_HOSTS")? { config.http.allowed_hosts = split_list(&hosts); } @@ -161,6 +167,12 @@ async fn main() -> Result<(), Box> { rustscript_agent::config::ClientDisconnectPolicy::parse(&value) .map_err(std::io::Error::other)?; } + // The loaded document is authoritative for the gateway default + // model/provider and for the public provider profiles. A missing + // document keeps the built-in local-agent defaults; an invalid profile + // fails startup before any listener or platform adapter exists. + let (mut config, provider_profiles) = + apply_loaded_config(config, loaded_config.as_ref()).map_err(std::io::Error::other)?; // Collect config and legacy source facts mechanically. RSS owns their // priority; the config and compaction values come from one bounded snapshot. @@ -204,6 +216,13 @@ async fn main() -> Result<(), Box> { let state = state .with_operator_auth_home(home.clone()) .map_err(std::io::Error::other)?; + let service = state.service(); + for profile in provider_profiles { + service + .set_provider_profile(profile) + .map_err(|_| std::io::Error::other("agent provider profile could not be registered"))?; + } + drop(service); let state = if explicit_home { state.with_workspace_home(home) } else { @@ -333,6 +352,47 @@ async fn main() -> Result<(), Box> { Ok(()) } +/// Applies the loaded `config.yaml` document to the existing gateway +/// boundaries: the default model/provider and one validated public +/// `ProviderProfile` per configured provider. Only public request-shaping +/// fields enter the profile; credential values stay in the native AuthStore. +fn apply_loaded_config( + mut config: AgentGatewayConfig, + loaded: Option<&ConfigFile>, +) -> Result<(AgentGatewayConfig, Vec), String> { + let Some(loaded) = loaded else { + return Ok((config, Vec::new())); + }; + config.model = loaded.model.model.clone(); + config.provider = Some(loaded.model.provider.clone()); + let mut profiles = Vec::new(); + for (name, provider) in &loaded.providers { + let mut options = serde_json::Map::new(); + options.insert( + "profile".to_string(), + serde_json::Value::String(name.clone()), + ); + options.insert( + "protocol".to_string(), + serde_json::Value::String(provider.protocol.clone()), + ); + options.insert( + "base_url".to_string(), + serde_json::Value::String(provider.base_url.clone()), + ); + if let Some(credential_id) = provider.auth.as_ref() { + options.insert( + "credential_id".to_string(), + serde_json::Value::String(credential_id.clone()), + ); + } + let profile = ProviderProfile::new(name.clone(), serde_json::Value::Object(options)) + .map_err(|_| "agent provider profile is invalid".to_string())?; + profiles.push(profile); + } + Ok((config, profiles)) +} + /// Builds the optional Telegram adapter configuration from the environment. /// The token is required when Telegram is enabled; the allowlists default to /// empty (deny-by-default). The token is never echoed by this binary. @@ -835,3 +895,149 @@ mod gateway_registration_tests { ); } } + +#[cfg(test)] +mod gateway_provider_config_tests { + use super::apply_loaded_config; + use rustscript_agent::{ + AdmitRunRequest, AgentGatewayConfig, AgentGatewayState, ConfigFile, RunContext, + config::ProviderProfile, config_file::CompactionSettings, + }; + use serde_json::{Value, json}; + + fn fixture_home() -> std::path::PathBuf { + let root = std::env::var_os("RUSTSCRIPT_AGENT_TEST_TMP") + .map(std::path::PathBuf::from) + .unwrap_or_else(std::env::temp_dir) + .join(format!( + "gateway-provider-config-unit-{}", + uuid::Uuid::new_v4() + )); + std::fs::create_dir_all(&root).expect("unit home"); + root + } + + async fn admitted_context(loaded: Option<&ConfigFile>, provider: Option<&str>) -> RunContext { + let (config, profiles) = + apply_loaded_config(AgentGatewayConfig::default(), loaded).expect("config applies"); + let state = AgentGatewayState::with_agent_selection_and_compaction( + config, + json!({}), + None, + CompactionSettings::default(), + ) + .expect("gateway state"); + let state = state + .with_operator_auth_home(fixture_home()) + .expect("operator auth home"); + let service = state.service(); + for profile in profiles { + service + .set_provider_profile(profile) + .expect("provider profile registers"); + } + let admitted = service + .admit(AdmitRunRequest { + input: json!("context probe"), + session_id: None, + model: None, + provider: provider.map(ToOwned::to_owned), + parent_run_id: None, + instructions: None, + platform: "unit".to_string(), + idempotency_key: None, + idempotency_hash: None, + graph_id: None, + workspace_branch: None, + }) + .await + .expect("admission"); + service + .run_context(&admitted.run_id) + .expect("admitted run context is cached") + } + + fn profile_options(profiles: &[ProviderProfile], name: &str) -> Option { + profiles + .iter() + .find(|profile| profile.name == name) + .map(|profile| profile.options().clone()) + } + + const CONFIG_WITH_AUTH: &str = "version: 1\nmodel:\n provider: openai\n model: fixture-model\n\ + providers:\n openai:\n protocol: openai-chat-completions\n \ + base_url: https://127.0.0.1:9/v1\n auth: demo-primary\n"; + + #[tokio::test] + async fn present_config_reaches_real_admission_context() { + let loaded = ConfigFile::from_str(CONFIG_WITH_AUTH).expect("fixture config"); + let context = admitted_context(Some(&loaded), None).await; + assert_eq!(context.model, "fixture-model"); + assert_eq!(context.provider.as_deref(), Some("openai")); + assert_eq!( + context.provider_options, + json!({ + "profile": "openai", + "protocol": "openai-chat-completions", + "base_url": "https://127.0.0.1:9/v1", + "credential_id": "demo-primary" + }) + ); + assert_eq!(context.metadata["provider_profile"], "openai"); + } + + #[tokio::test] + async fn missing_config_keeps_builtin_local_agent_admission() { + let context = admitted_context(None, None).await; + assert_eq!(context.model, "local-agent"); + assert_eq!(context.provider.as_deref(), Some("local-agent")); + assert_eq!( + context.provider_options, + json!({"profile": "local-agent", "protocol": "local-agent"}) + ); + assert_eq!(context.metadata["provider_profile"], "local-agent"); + } + + #[tokio::test] + async fn no_auth_and_unselected_provider_do_not_supply_credentials() { + let loaded = ConfigFile::from_str( + "version: 1\nmodel:\n provider: openai\n model: fixture-model\n\ + providers:\n openai:\n protocol: openai-chat-completions\n \ + base_url: https://127.0.0.1:9/v1\n other:\n protocol: openai-chat-completions\n \ + base_url: https://127.0.0.1:8/v1\n auth: other-primary\n", + ) + .expect("fixture config"); + let (_, profiles) = + apply_loaded_config(AgentGatewayConfig::default(), Some(&loaded)).expect("config"); + assert_eq!( + profile_options(&profiles, "other"), + Some(json!({ + "profile": "other", + "protocol": "openai-chat-completions", + "base_url": "https://127.0.0.1:8/v1", + "credential_id": "other-primary" + })) + ); + assert_eq!( + profile_options(&profiles, "openai"), + Some(json!({ + "profile": "openai", + "protocol": "openai-chat-completions", + "base_url": "https://127.0.0.1:9/v1" + })) + ); + let context = admitted_context(Some(&loaded), Some("openai")).await; + assert_eq!( + context.provider_options, + json!({ + "profile": "openai", + "protocol": "openai-chat-completions", + "base_url": "https://127.0.0.1:9/v1" + }) + ); + assert!( + context.provider_options.get("credential_id").is_none(), + "an unselected provider's credential must not enter the admitted context" + ); + } +} diff --git a/src/gateway/mod.rs b/src/gateway/mod.rs index 66276fb..34b8339 100644 --- a/src/gateway/mod.rs +++ b/src/gateway/mod.rs @@ -27,6 +27,14 @@ use std::{ use parking_lot::RwLock; use rustscript_vm::HttpConfig; +use crate::auth::{ + host::{RunOAuthCancel, SystemOAuthClock}, + native_https::NativeHttpsTransport, + oauth::{ + BoundedHttpsTransport, BrowserOpener, CallbackMode, CallbackWaiter, OAuthError, OAuthHost, + OAuthIoContext, PreparedCallbackSession, RawCallbackInput, + }, +}; use crate::config::AgentGatewayConfig; use crate::config_file::{AgentPaths, CompactionSettings}; use crate::domain::{json_to_vm_value, vm_value_to_json}; @@ -85,6 +93,58 @@ pub struct AgentGatewayState { source_metadata: Option>, } +/// Headless callback policy for operator provider transport. Login callbacks +/// are unavailable in the gateway process: no loopback listener is bound and +/// no manual input is read. The typed transport failure is the same one the +/// rest of the provider transport path already maps to `transport_error`. +struct HeadlessCallbackDeny; + +impl CallbackWaiter for HeadlessCallbackDeny { + fn prepare( + &self, + _mode: CallbackMode, + _context: &OAuthIoContext, + ) -> Result>, OAuthError> { + Err(headless_oauth_unavailable()) + } + + fn wait_one(&self, _context: &OAuthIoContext) -> Result { + Err(headless_oauth_unavailable()) + } +} + +/// Headless browser policy for operator provider transport. The service has no +/// user desktop session to open; this never executes a browser command. +struct HeadlessBrowserDeny; + +impl BrowserOpener for HeadlessBrowserDeny { + fn open(&self, _url: &str, _context: &OAuthIoContext) -> Result<(), OAuthError> { + Err(headless_oauth_unavailable()) + } +} + +fn headless_oauth_unavailable() -> OAuthError { + OAuthError::Transport { + message: "headless_oauth_unavailable".to_string(), + } +} + +/// Builds the single headless OAuth host shape used by the operator provider +/// factory. The transport is injected so the same construction can be driven +/// by a typed fixture; the deny policies have no state and never bind or exec. +fn headless_oauth_host( + transport: Arc, + cancellation: crate::runtime::RunCancellation, +) -> Arc { + Arc::new(OAuthHost::new( + transport, + Arc::new(HeadlessCallbackDeny), + Arc::new(HeadlessBrowserDeny), + Arc::new(SystemOAuthClock), + Arc::new(RunOAuthCancel::new(cancellation)), + )) +} + impl AgentGatewayState { pub fn new(config: AgentGatewayConfig) -> Result { // Empty selection: every priority/default decision (here, the bundled @@ -105,10 +165,16 @@ impl AgentGatewayState { /// Installs operator authentication for the default provider child only. /// AuthStore admission remains lazy until a finite provider invocation. + /// The provider factory receives the existing headless native HTTPS + /// transport: no browser, callback listener, manual input, or scripted + /// fixture enters this path. pub fn with_operator_auth_home(self, home: impl Into) -> Result { let home = Self::validated_operator_home(home)?.home; + let transport: Arc = Arc::new(NativeHttpsTransport::new()); let factory: crate::runtime::agent_host::AuthHostFactory = Arc::new(move |cancellation| { + let oauth = headless_oauth_host(Arc::clone(&transport), cancellation.clone()); crate::auth::host::AuthHost::new(home.clone(), cancellation) + .map(|host| host.with_oauth_host(oauth)) }); Ok(self.attach_operator_auth_factory(factory)) } @@ -689,3 +755,250 @@ fn parse_source_decision(decision: &serde_json::Value) -> Result Err("unsupported_agent_source: descriptor is unsupported".to_string()), } } + +#[cfg(test)] +mod headless_provider_transport_tests { + use std::{ + fs, + path::{Path, PathBuf}, + sync::Arc, + time::Duration, + }; + + use rustscript_vm::{ + CompileSourceFileOptions, HostFunctionRegistry, SourceFlavor, Value, Vm, VmStatus, + compile_source_at_path_with_flavor_and_options, + }; + + use super::{HeadlessBrowserDeny, HeadlessCallbackDeny, headless_oauth_host}; + use crate::{ + auth::{ + host::{AuthHost, RunOAuthCancel, SystemOAuthClock, auth_host_catalog}, + oauth::{ + BrowserOpener, CallbackMode, CallbackWaiter, OAuthIoContext, ScriptedHttpsTransport, + }, + }, + runtime::RunCancellation, + }; + + fn fixture_home() -> PathBuf { + let base = PathBuf::from( + std::env::var("RUSTSCRIPT_AGENT_TEST_TMP").expect("assigned task lease temp"), + ); + let home = base.join(format!( + "gateway-headless-provider-{}-{}", + std::process::id(), + uuid::Uuid::new_v4() + )); + fs::create_dir_all(&home).expect("create private fixture home"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&home, fs::Permissions::from_mode(0o700)) + .expect("fixture home mode"); + } + let config = format!( + "version: 1\n\ + agent:\n source: bundled:coding\n max_turns: 64\n max_tool_calls: 128\n max_tool_output_bytes: 1048576\n\ + model:\n provider: openai-codex\n model: fixture-model\n\ + providers:\n openai-codex:\n protocol: openai-chat-completions\n base_url: https://provider.synthetic.test/v1\n auth: fixture-codex\n\ + workspaces:\n allowed_roots:\n - {base}\n default: {base}\n\ + approvals:\n read: allow\n write: ask\n process: ask\n", + base = base.display() + ); + fs::write(home.join("config.yaml"), config).expect("write synthetic config"); + // The exact repository fixture, with only its fixed expiry moved far + // enough into the future to keep the synthetic credential active. + let fixtures = + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/config_auth_rss"); + let auth = fs::read_to_string(fixtures.join("auth.yaml")) + .expect("read synthetic auth fixture") + .replace("1788440000000", "1900000000000"); + fs::write(home.join("auth.yaml"), auth).expect("write synthetic auth fixture"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + for name in ["config.yaml", "auth.yaml"] { + fs::set_permissions(home.join(name), fs::Permissions::from_mode(0o600)) + .expect("fixture file mode"); + } + } + home + } + + fn record(fields: Vec<(&str, Value)>) -> Value { + Value::map( + fields + .into_iter() + .map(|(name, value)| (Value::string(name), value)) + .collect(), + ) + } + + fn field<'a>(value: &'a Value, name: &str) -> &'a Value { + let Value::Map(entries) = value else { + panic!("expected RSS record, got {value:?}"); + }; + entries + .iter() + .find_map(|(key, value)| match key { + Value::String(key) if key.as_ref() == name => Some(value), + _ => None, + }) + .unwrap_or_else(|| panic!("missing RSS field {name}")) + } + + fn invoke(vm: &mut Vm, name: &str, args: &[Value]) -> Value { + let callable = vm + .resolve_exported_callable(name) + .unwrap_or_else(|error| panic!("resolve RSS entry {name}: {error}")); + vm.invoke_callable(callable, args) + .unwrap_or_else(|error| panic!("invoke RSS entry {name}: {error}")) + } + + fn compile_vm(home: &Path, transport: Arc) -> (Vm, AuthHost, Value) { + let module_path = + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rss/auth/fallible_contract.rss"); + let source = fs::read_to_string(&module_path).expect("read production fallible RSS"); + let catalog = auth_host_catalog(); + let compiled = compile_source_at_path_with_flavor_and_options( + &module_path, + &source, + SourceFlavor::RustScript, + CompileSourceFileOptions::default().with_host_api_catalog(catalog.clone()), + ) + .unwrap_or_else(|error| panic!("compile production fallible RSS: {error}")); + let mut vm = Vm::new_shared(Arc::new(compiled.program)); + let cancellation = RunCancellation::with_timeout(Duration::from_secs(30)); + let auth = AuthHost::new(home.to_path_buf(), cancellation.clone()) + .expect("native auth host") + .with_oauth_host(headless_oauth_host(transport, cancellation)); + let mut registry = HostFunctionRegistry::restricted(); + auth.register(&mut registry, catalog.as_ref()) + .expect("register real auth host catalog"); + registry + .bind_vm_cached(&mut vm) + .expect("bind restricted production registry"); + assert!(matches!(vm.run().expect("run RSS root"), VmStatus::Halted)); + let bound = auth.bind_vm(&mut vm).expect("bind native host home"); + (vm, auth, field(&bound, "host_home").clone()) + } + + /// The production headless constructor sends one real provider request + /// through the typed transport fixture: the configured authority/path, + /// the synthetic access authorization, and no secret in the body. The + /// cleanup entry runs after the send. This proves the send contract only; + /// the packaged binary TCP/TLS test remains the production-wiring proof. + #[test] + fn headless_factory_sends_configured_provider_request_with_synthetic_authorization() { + let home = fixture_home(); + let transport = Arc::new(ScriptedHttpsTransport::new()); + transport.push_raw(200, r#"{"object":"list","data":[]}"#); + let (mut vm, _auth, host_home) = compile_vm(&home, transport.clone()); + let policy = invoke(&mut vm, "snapshot", std::slice::from_ref(&host_home)); + let access = invoke(&mut vm, "issue_access", std::slice::from_ref(&policy)); + let request = record(vec![ + ("policy_handle", policy.clone()), + ("credential_id", Value::string("fixture-codex")), + ("method", Value::string("POST")), + ("path", Value::string("/v1/chat/completions")), + ( + "public_headers", + Value::array(vec![ + record(vec![ + ("name", Value::string("accept")), + ("value", Value::string("application/json")), + ]), + record(vec![ + ("name", Value::string("content-type")), + ("value", Value::string("application/json")), + ]), + ]), + ), + ( + "public_body", + Value::string(r#"{"model":"fixture-model","messages":[]}"#), + ), + ]); + let usage = record(vec![ + ("kind", Value::string("access")), + ("access_handle", access), + ("refresh_handle", Value::Null), + ("code_handle", Value::Null), + ("verifier_handle", Value::Null), + ("device_handle", Value::Null), + ]); + let result = invoke( + &mut vm, + "transport_result_then_cleanup", + &[request, usage, policy], + ); + assert_eq!( + field(&result, "cleanup_completed"), + &Value::Bool(true), + "the transport cleanup entry must run after the send" + ); + let transport_result = field(&result, "transport"); + assert_eq!( + field(transport_result, "ok"), + &Value::Bool(true), + "the typed transport fixture must accept the provider request" + ); + assert_eq!( + field(transport_result, "error"), + &Value::Null, + "a successful fixture send has no transport failure" + ); + + let sent = transport.take_sent(); + assert_eq!(sent.len(), 1, "exactly one provider request is sent"); + assert_eq!(sent[0].method, "POST"); + assert_eq!( + sent[0].url, "https://provider.synthetic.test/v1/chat/completions", + "the configured base_url supplies the admitted authority and path" + ); + assert_eq!( + sent[0].headers.get("authorization").map(String::as_str), + Some("Bearer SYNTHETIC_ACCESS_TOKEN"), + "the synthetic access credential is injected host-side" + ); + assert!( + !sent[0].body.contains("SYNTHETIC_ACCESS_TOKEN"), + "the access secret must never enter the request body" + ); + assert!( + !sent[0].body.to_ascii_lowercase().contains("authorization"), + "the body carries only the public provider payload" + ); + } + + /// The private deny policies fail at the OAuth boundary with the existing + /// typed transport error. They hold no state and have no bind/exec path. + #[test] + fn headless_denies_callback_and_browser_without_listener_or_exec() { + let cancellation = RunCancellation::with_timeout(Duration::from_secs(30)); + let context = OAuthIoContext::new( + cancellation + .deadline_instant() + .expect("finite OAuth context deadline"), + Arc::new(SystemOAuthClock), + Arc::new(RunOAuthCancel::new(cancellation)), + ); + let prepare = match HeadlessCallbackDeny.prepare(CallbackMode::Browser, &context) { + Ok(_) => panic!("headless callback preparation must fail closed"), + Err(error) => error, + }; + assert_eq!(prepare.code(), "transport_error"); + let wait = match HeadlessCallbackDeny.wait_one(&context) { + Ok(_) => panic!("headless callback waiting must fail closed"), + Err(error) => error, + }; + assert_eq!(wait.code(), "transport_error"); + let open = + match HeadlessBrowserDeny.open("https://synthetic.example/oauth/authorize", &context) { + Ok(()) => panic!("headless browser opening must fail closed"), + Err(error) => error, + }; + assert_eq!(open.code(), "transport_error"); + } +} diff --git a/tests/gateway_provider_config_tests.rs b/tests/gateway_provider_config_tests.rs new file mode 100644 index 0000000..e8ec17e --- /dev/null +++ b/tests/gateway_provider_config_tests.rs @@ -0,0 +1,482 @@ +//! Gateway startup wiring of the loaded `config.yaml` into the existing +//! `AgentGatewayConfig` / `ProviderProfile` boundaries. +//! +//! These cases drive the packaged `rustscript-agent-gateway` binary over real +//! loopback HTTP. A present config must supply the default model/provider and +//! the public provider profiles (protocol, base URL, named credential ID) +//! before the listener binds; a missing config keeps the built-in local-agent +//! defaults, and a present-but-invalid config must fail closed before listen. + +use std::path::{Path, PathBuf}; +use std::process::Stdio; +use std::time::{Duration, Instant}; + +use serde_json::{Value, json}; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; + +fn test_root(label: &str) -> PathBuf { + let root = std::env::var_os("RUSTSCRIPT_AGENT_TEST_TMP") + .map(PathBuf::from) + .unwrap_or_else(|| std::env::temp_dir().join("gateway-provider-config-tests")) + .join(format!( + "gateway-provider-config-{label}-{}", + uuid::Uuid::new_v4() + )); + std::fs::create_dir_all(&root).expect("gateway provider config test root"); + root +} + +fn home(root: &Path) -> PathBuf { + root.join(".rustscript-agent") +} + +fn write_config(root: &Path, document: &str) { + let home = home(root); + std::fs::create_dir_all(&home).expect("agent home"); + std::fs::write(home.join("config.yaml"), document).expect("write config.yaml"); +} + +/// A synthetic api_key credential fixture. The access-token value is a fixed +/// non-production string so the transport attempt can be observed without any +/// real secret. +fn write_auth_fixture(root: &Path, credential_id: &str, provider: &str) { + let home = home(root); + std::fs::create_dir_all(&home).expect("agent home"); + let document = format!( + "version: 1\ncredentials:\n {credential_id}:\n provider: {provider}\n \ + kind: api_key\n source: fixture\n token_type: Bearer\n \ + access_token: fixture-not-a-real-credential\n expires_at_ms: 1900000000000\n" + ); + let path = home.join("auth.yaml"); + std::fs::write(&path, document).expect("write auth.yaml"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)) + .expect("auth.yaml mode"); + std::fs::set_permissions(&home, std::fs::Permissions::from_mode(0o700)) + .expect("agent home mode"); + } +} + +fn spawn_gateway(root: &Path, stderr_path: &Path) -> tokio::process::Child { + let stderr_file = std::fs::File::create(stderr_path).expect("gateway stderr file"); + // Clear the inherited environment so a developer or CI shell cannot hand + // real platform tokens or provider settings to the packaged binary. Only + // the explicit test inputs and the executable search path remain. + let path = std::env::var_os("PATH").expect("the test process must carry PATH"); + tokio::process::Command::new(env!("CARGO_BIN_EXE_rustscript-agent-gateway")) + .env_clear() + .env("PATH", path) + .env("HOME", root) + .env("TMPDIR", root) + .env("TMP", root) + .env("TEMP", root) + .env("TEST_TMPDIR", root) + .env("RUSTSCRIPT_AGENT_TEST_TMP", root) + .env("RUSTSCRIPT_AGENT_TEST_TMPDIR", root) + .env("RUSTSCRIPT_AGENT_GATEWAY_ADDR", "127.0.0.1:0") + .env("RUSTSCRIPT_AGENT_ALLOW_ANONYMOUS", "1") + .current_dir(root) + .stdout(Stdio::null()) + .stderr(Stdio::from(stderr_file)) + .kill_on_drop(true) + .spawn() + .expect("gateway binary should spawn") +} + +async fn wait_for_listening( + child: &mut tokio::process::Child, + stderr_path: &Path, + timeout: Duration, +) -> Option { + let deadline = Instant::now() + timeout; + loop { + if let Ok(text) = std::fs::read_to_string(stderr_path) + && let Some(position) = text.find("listening on http://") + { + let rest = &text[position + "listening on http://".len()..]; + let address = rest.lines().next().unwrap_or("").trim().to_string(); + if !address.is_empty() { + return Some(address); + } + } + if let Ok(Some(_)) = child.try_wait() { + return None; + } + if Instant::now() >= deadline { + return None; + } + tokio::time::sleep(Duration::from_millis(25)).await; + } +} + +async fn stop_child(child: &mut tokio::process::Child) { + let Some(pid) = child.id() else { + return; + }; + let signal = std::process::Command::new("kill") + .args(["-INT", &pid.to_string()]) + .status() + .expect("send SIGINT to the gateway child"); + assert!(signal.success(), "the gateway child must accept SIGINT"); + match tokio::time::timeout(Duration::from_secs(15), child.wait()).await { + Ok(result) => { + result.expect("the gateway child must be reaped after SIGINT"); + } + Err(_) => { + child + .start_kill() + .expect("the gateway child must accept SIGKILL after its bound"); + let result = tokio::time::timeout(Duration::from_secs(15), child.wait()) + .await + .expect("the gateway child must exit after SIGKILL"); + result.expect("the gateway child must be reaped after SIGKILL"); + } + } +} + +async fn http_request(host: &str, method: &str, path: &str, body: &str) -> String { + let mut stream = tokio::net::TcpStream::connect(host) + .await + .expect("connect to gateway"); + let request = if method == "GET" { + format!("GET {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n") + } else { + format!( + "{method} {path} HTTP/1.1\r\nHost: {host}\r\nContent-Type: application/json\r\n\ + Content-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ) + }; + stream + .write_all(request.as_bytes()) + .await + .expect("write gateway request"); + let mut response = Vec::new(); + let _ = tokio::time::timeout(Duration::from_secs(20), stream.read_to_end(&mut response)).await; + String::from_utf8_lossy(&response).into_owned() +} + +fn response_body(response: &str) -> &str { + response.split("\r\n\r\n").nth(1).unwrap_or("") +} + +fn response_json(response: &str) -> Value { + serde_json::from_str(response_body(response)).expect("gateway JSON response") +} + +fn assert_status(response: &str, expected: &str) { + let first = response.lines().next().unwrap_or(""); + assert!( + first.contains(expected), + "expected HTTP status {expected}, got first line {first:?}" + ); +} + +async fn create_session(host: &str, body: &Value) -> Value { + let response = http_request(host, "POST", "/api/sessions", &body.to_string()).await; + assert_status(&response, "201"); + response_json(&response) +} + +async fn admit_run(host: &str, session_id: &str, input: &str) -> String { + let body = json!({ "session_id": session_id, "input": input }); + let response = http_request(host, "POST", "/v1/runs", &body.to_string()).await; + assert_status(&response, "202"); + let run = response_json(&response); + run["run_id"].as_str().expect("admitted run id").to_string() +} + +/// Reads the run SSE body until the named event appears or the bounded +/// observation window closes. A subscription made before the run's live +/// delivery sender exists can end after replaying partial history; reconnect +/// and replay until the event is observed. The bound only prevents a hung +/// test. +async fn read_run_events(host: &str, run_id: &str, event: &str) -> String { + let deadline = Instant::now() + Duration::from_secs(60); + let needle = format!("\"event\":\"{event}\""); + let mut collected = String::new(); + loop { + if collected.contains(&needle) || Instant::now() >= deadline { + return collected; + } + let attempt_deadline = (Instant::now() + Duration::from_secs(5)).min(deadline); + let body = read_events_once(host, run_id, &needle, attempt_deadline).await; + collected.push_str(&body); + if !collected.contains(&needle) { + tokio::time::sleep(Duration::from_millis(50)).await; + } + } +} + +async fn read_events_once(host: &str, run_id: &str, needle: &str, deadline: Instant) -> String { + let mut stream = match tokio::net::TcpStream::connect(host).await { + Ok(stream) => stream, + Err(_) => return String::new(), + }; + let request = format!( + "GET /v1/runs/{run_id}/events HTTP/1.1\r\nHost: {host}\r\n\ + Accept: text/event-stream\r\nConnection: close\r\n\r\n" + ); + if stream.write_all(request.as_bytes()).await.is_err() { + return String::new(); + } + let mut body = Vec::new(); + loop { + if body + .windows(needle.len()) + .any(|window| window == needle.as_bytes()) + { + break; + } + if Instant::now() >= deadline { + break; + } + let mut chunk = [0u8; 4096]; + match tokio::time::timeout(Duration::from_millis(200), stream.read(&mut chunk)).await { + Ok(Ok(0)) => break, + Ok(Ok(read)) => body.extend_from_slice(&chunk[..read]), + Ok(Err(_)) => break, + Err(_) => {} + } + } + String::from_utf8_lossy(&body).into_owned() +} + +/// Extracts `error_code` from the first `model.failed` SSE data record. +fn model_failed_code(events: &str) -> Option { + for line in events.lines() { + let Some(payload) = line.strip_prefix("data: ") else { + continue; + }; + let Ok(value) = serde_json::from_str::(payload) else { + continue; + }; + if value["event"] == "model.failed" { + return value["error_code"].as_str().map(ToOwned::to_owned); + } + } + None +} + +const CONFIG_WITH_AUTH: &str = "version: 1\nmodel:\n provider: openai\n model: fixture-model\n\ + providers:\n openai:\n protocol: openai-chat-completions\n \ + base_url: https://127.0.0.1:9/v1\n auth: demo-primary\n"; + +/// The loaded config supplies the gateway's default model/provider, visible +/// through the real HTTP model and session surfaces. +#[tokio::test(flavor = "multi_thread")] +async fn binary_present_config_supplies_model_and_provider_defaults() { + let root = test_root("defaults"); + write_config(&root, CONFIG_WITH_AUTH); + let stderr_path = root.join("stderr.log"); + let mut child = spawn_gateway(&root, &stderr_path); + let address = match wait_for_listening(&mut child, &stderr_path, Duration::from_secs(30)).await + { + Some(address) => address, + None => { + let stderr = std::fs::read_to_string(&stderr_path).unwrap_or_default(); + panic!("the configured gateway must bind and listen: {stderr}"); + } + }; + + let models = http_request(&address, "GET", "/v1/models", "").await; + assert_status(&models, "200"); + let models = response_json(&models); + assert_eq!(models["data"][0]["id"], "fixture-model"); + assert_eq!(models["data"][0]["owned_by"], "openai"); + + let session = create_session(&address, &json!({})).await; + assert_eq!(session["session"]["model"], "fixture-model"); + assert_eq!(session["session"]["provider"], "openai"); + + stop_child(&mut child).await; +} + +/// A named credential selected by the startup profile must reach the real RSS +/// provider dispatch and the packaged headless native HTTPS factory. The +/// test's synthetic listener accepts the native TCP connection and closes it +/// before trust establishment completes, so the observed result is the +/// transport-layer failure code from the real TLS attempt. The pre-fix binary +/// stopped before any connection (`transport_unavailable`); a credential +/// admission failure would answer `missing_credential`. Neither is accepted +/// here, and this is not a provider authentication or model success. +#[tokio::test(flavor = "multi_thread")] +async fn binary_named_credential_reaches_provider_dispatch() { + let root = test_root("named-credential"); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0") + .await + .expect("bind the synthetic provider listener"); + let provider_port = listener + .local_addr() + .expect("synthetic provider listener address") + .port(); + let config = format!( + "version: 1\nmodel:\n provider: openai\n model: fixture-model\n\ + providers:\n openai:\n protocol: openai-chat-completions\n \ + base_url: https://127.0.0.1:{provider_port}/v1\n auth: demo-primary\n" + ); + write_config(&root, &config); + write_auth_fixture(&root, "demo-primary", "openai"); + let stderr_path = root.join("stderr.log"); + let mut child = spawn_gateway(&root, &stderr_path); + let address = match wait_for_listening(&mut child, &stderr_path, Duration::from_secs(30)).await + { + Some(address) => address, + None => { + let stderr = std::fs::read_to_string(&stderr_path).unwrap_or_default(); + panic!("the configured gateway must bind and listen: {stderr}"); + } + }; + // One semaphore permit per accepted native TCP connection. The listener + // never completes a TLS handshake: the connection is closed immediately. + let accepted = std::sync::Arc::new(tokio::sync::Semaphore::new(0)); + let accept_permits = std::sync::Arc::clone(&accepted); + let accept_task = tokio::spawn(async move { + loop { + let Ok((stream, _peer)) = listener.accept().await else { + break; + }; + drop(stream); + accept_permits.add_permits(1); + } + }); + + let session = create_session(&address, &json!({ "provider": "openai" })).await; + let session_id = session["session"]["id"].as_str().expect("session id"); + let run_id = admit_run(&address, session_id, "credential propagation").await; + let events = read_run_events(&address, &run_id, "model.failed").await; + let code = model_failed_code(&events); + // Assert the typed transport result before waiting for the connection so + // the pre-fix binary fails immediately instead of consuming the bound. + assert_eq!( + code.as_deref(), + Some("transport_error"), + "the packaged factory must attempt the configured native HTTPS request; \ + transport_unavailable means the OAuth/HTTPS host is still missing, \ + missing_credential means the named credential never reached the store; \ + actual events: {events}" + ); + assert!( + !events.contains("\"event\":\"model.completed\""), + "a closed TLS handshake must not be reported as a completed model turn: {events}" + ); + let permit = tokio::time::timeout(Duration::from_secs(10), accepted.acquire()) + .await + .expect("the packaged factory must open a native TCP connection") + .expect("the accept semaphore stays open"); + drop(permit); + stop_child(&mut child).await; + accept_task.abort(); + let _ = accept_task.await; +} + +/// Without the credential selector, the same run must still answer the typed +/// `missing_credential` failure. It must not borrow the credential belonging to +/// another configured provider. +#[tokio::test(flavor = "multi_thread")] +async fn binary_no_auth_keeps_missing_credential_without_cross_use() { + let root = test_root("no-auth"); + write_config( + &root, + "version: 1\nmodel:\n provider: openai\n model: fixture-model\n\ + providers:\n openai:\n protocol: openai-chat-completions\n \ + base_url: https://127.0.0.1:9/v1\n other:\n protocol: openai-chat-completions\n \ + base_url: https://127.0.0.1:8/v1\n auth: other-primary\n", + ); + write_auth_fixture(&root, "other-primary", "other"); + let stderr_path = root.join("stderr.log"); + let mut child = spawn_gateway(&root, &stderr_path); + let address = match wait_for_listening(&mut child, &stderr_path, Duration::from_secs(30)).await + { + Some(address) => address, + None => { + let stderr = std::fs::read_to_string(&stderr_path).unwrap_or_default(); + panic!("the configured gateway must bind and listen: {stderr}"); + } + }; + + let session = create_session(&address, &json!({ "provider": "openai" })).await; + let session_id = session["session"]["id"].as_str().expect("session id"); + let run_id = admit_run(&address, session_id, "no credential selected").await; + let events = read_run_events(&address, &run_id, "model.failed").await; + let code = model_failed_code(&events); + stop_child(&mut child).await; + + assert_eq!( + code.as_deref(), + Some("missing_credential"), + "the selected provider has no credential, and another provider's \ + credential must not be borrowed; actual events: {events}" + ); +} + +/// A missing config keeps the built-in local-agent defaults. +#[tokio::test(flavor = "multi_thread")] +async fn binary_missing_config_keeps_local_agent_defaults() { + let root = test_root("missing-config"); + let stderr_path = root.join("stderr.log"); + let mut child = spawn_gateway(&root, &stderr_path); + let address = match wait_for_listening(&mut child, &stderr_path, Duration::from_secs(30)).await + { + Some(address) => address, + None => { + let stderr = std::fs::read_to_string(&stderr_path).unwrap_or_default(); + panic!("the default gateway must bind and listen: {stderr}"); + } + }; + + let models = http_request(&address, "GET", "/v1/models", "").await; + assert_status(&models, "200"); + let models = response_json(&models); + assert_eq!(models["data"][0]["id"], "local-agent"); + assert_eq!(models["data"][0]["owned_by"], "local-agent"); + + let session = create_session(&address, &json!({})).await; + assert_eq!(session["session"]["model"], "local-agent"); + assert_eq!(session["session"]["provider"], "local-agent"); + + stop_child(&mut child).await; +} + +/// A present-but-invalid config fails before the listener binds and its static +/// startup error never echoes the configured value. +#[tokio::test(flavor = "multi_thread")] +async fn binary_present_invalid_config_fails_before_listen_without_echo() { + let root = test_root("invalid-config"); + let sentinel = "fixture-invalid-host-sentinel.example"; + write_config( + &root, + &format!( + "version: 1\nmodel:\n provider: openai\n model: fixture-model\n\ + providers:\n openai:\n protocol: openai-chat-completions\n \ + base_url: http://{sentinel}/v1\n" + ), + ); + let stderr_path = root.join("stderr.log"); + let mut child = spawn_gateway(&root, &stderr_path); + let listening = wait_for_listening(&mut child, &stderr_path, Duration::from_secs(25)).await; + let status = if listening.is_some() { + stop_child(&mut child).await; + panic!("an invalid config must fail before the listener binds"); + } else { + tokio::time::timeout(Duration::from_secs(20), child.wait()) + .await + .expect("the invalid gateway must exit") + .expect("child wait") + }; + let stderr = std::fs::read_to_string(&stderr_path).unwrap_or_default(); + assert!( + !status.success(), + "an invalid config must exit non-zero: {stderr}" + ); + assert!( + stderr.contains("agent config.yaml could not be loaded"), + "startup must report the static config error: {stderr}" + ); + assert!( + !stderr.contains(sentinel), + "startup diagnostics must not echo the invalid value: {stderr}" + ); +} From 58e18bec5abd750d07376875f269fbec4c9d524f Mon Sep 17 00:00:00 2001 From: fffonion Date: Mon, 9 Nov 2026 11:26:38 +0800 Subject: [PATCH 2/7] fix(agent): preserve native tool schemas in provider requests --- rss/agent/main.rss | 41 ++- tests/gateway_provider_config_tests.rs | 340 ++++++++++++++++++++++++- 2 files changed, 379 insertions(+), 2 deletions(-) diff --git a/rss/agent/main.rss b/rss/agent/main.rss index 582592c..434b1bd 100644 --- a/rss/agent/main.rss +++ b/rss/agent/main.rss @@ -338,7 +338,46 @@ fn seed_messages(context: map, messages: array) -> array { seeded } +// Native `RunContext.tool_schemas` entries are validated registry +// `ToolDescriptor` maps (`{name, description, toolset, risk_class, schema}`). +// The canonical LlmRequest contract still carries the schema as a JSON string +// in `schema_json`, which the Chat/Gemini/Anthropic codecs splice into their +// wire bodies; convert only at this boundary so native and canonical shapes +// stay distinct everywhere else. +fn native_tool_to_canonical(descriptor: map) -> map { + let schema: map = descriptor["schema"]; + let schema_json: string = json::encode(schema); + { + name: ctx_string(descriptor, "name", ""), + description: ctx_string(descriptor, "description", ""), + schema_json: schema_json + } +} + +fn native_tools_to_canonical(descriptors: array) -> array { + let mut tools: array = []; + let mut index = 0; + while index < descriptors.length { + let descriptor: map = array_map(descriptors, index); + tools[tools.length] = native_tool_to_canonical(descriptor); + index += 1; + } + tools +} + fn tools_from_context(context: map) -> array { + let mut tools: array = ctx_array(context, "tools"); + if tools.length == 0 { + let native_descriptors: array = ctx_array(context, "tool_schemas"); + tools = native_tools_to_canonical(native_descriptors); + } + tools +} + +// Local tool dispatch and admitted-registry authority consume the native +// descriptor shape (risk class, toolset, schema object); it must not see the +// canonical LLM conversion above. +fn native_tools_from_context(context: map) -> array { let mut tools: array = ctx_array(context, "tools"); if tools.length == 0 { tools = ctx_array(context, "tool_schemas"); @@ -613,7 +652,7 @@ fn dispatch_reviewed(context: map, call: map) -> map { let identity: string = ctx_string(metadata, "registry_identity", ""); let dispatch_input: map = { call: call, - registry: tools_from_context(context), + registry: native_tools_from_context(context), registry_identity: identity, admitted_registry_identity: identity, run_id: ctx_string(context, "run_id", ""), diff --git a/tests/gateway_provider_config_tests.rs b/tests/gateway_provider_config_tests.rs index e8ec17e..7aca704 100644 --- a/tests/gateway_provider_config_tests.rs +++ b/tests/gateway_provider_config_tests.rs @@ -14,6 +14,26 @@ use std::time::{Duration, Instant}; use serde_json::{Value, json}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; +#[cfg(feature = "config-fixture")] +#[path = "support/provider_opaque_fixture.rs"] +#[allow(dead_code)] +mod provider_opaque_fixture; + +#[cfg(feature = "config-fixture")] +use provider_opaque_fixture::FixtureHome; +#[cfg(feature = "config-fixture")] +use rustscript_agent::auth::oauth::{ + BoundedHttpsTransport, OAuthError, OAuthIoContext, PreparedHttpsRequest, RawHttpsResponse, +}; +#[cfg(feature = "config-fixture")] +use rustscript_agent::config::ProviderProfile; +#[cfg(feature = "config-fixture")] +use rustscript_agent::{AdmitRunRequest, AgentGatewayConfig, AgentGatewayState}; +#[cfg(feature = "config-fixture")] +use std::collections::BTreeMap; +#[cfg(feature = "config-fixture")] +use std::sync::{Arc, Mutex}; + fn test_root(label: &str) -> PathBuf { let root = std::env::var_os("RUSTSCRIPT_AGENT_TEST_TMP") .map(PathBuf::from) @@ -68,7 +88,7 @@ fn spawn_gateway(root: &Path, stderr_path: &Path) -> tokio::process::Child { tokio::process::Command::new(env!("CARGO_BIN_EXE_rustscript-agent-gateway")) .env_clear() .env("PATH", path) - .env("HOME", root) + .env("RUSTSCRIPT_AGENT_HOME", home(root)) .env("TMPDIR", root) .env("TMP", root) .env("TEMP", root) @@ -480,3 +500,321 @@ async fn binary_present_invalid_config_fails_before_listen_without_echo() { "startup diagnostics must not echo the invalid value: {stderr}" ); } + +#[cfg(feature = "config-fixture")] +const SCHEMA_WIRE_PROVIDER: &str = "openai"; +#[cfg(feature = "config-fixture")] +const SCHEMA_WIRE_BASE_URL: &str = "https://provider.synthetic.test/v1"; +#[cfg(feature = "config-fixture")] +const SCHEMA_WIRE_CREDENTIAL: &str = "schema-wire-fixture-credential"; +#[cfg(feature = "config-fixture")] +const SCHEMA_WIRE_ACCESS: &str = "SYNTHETIC_SCHEMA_WIRE_ACCESS"; +#[cfg(feature = "config-fixture")] +const SCHEMA_WIRE_MODEL: &str = "fixture-model"; +#[cfg(feature = "config-fixture")] +const SCHEMA_WIRE_ANSWER: &str = "fixture-final-reply"; + +/// A strict synthetic Chat Completions upstream. It validates the complete +/// provider wire request in private memory and rejects schema-shaped defects +/// with the typed 400 a strict server would return; only a fully valid wire +/// request receives the fixed synthetic final reply. It never persists, logs, +/// or returns the observed request body. +#[cfg(feature = "config-fixture")] +struct StrictChatTransport { + expected_tools: Vec<(String, String, Value)>, + calls: Mutex, + violations: Mutex>, +} + +#[cfg(feature = "config-fixture")] +impl StrictChatTransport { + fn new(expected_tools: Vec<(String, String, Value)>) -> Self { + Self { + expected_tools, + calls: Mutex::new(0), + violations: Mutex::new(Vec::new()), + } + } + + fn reject(&self, code: &str) -> RawHttpsResponse { + RawHttpsResponse { + status: 400, + headers: BTreeMap::from([("content-type".to_string(), "application/json".to_string())]), + body: format!( + "{{\"error\":{{\"type\":\"invalid_request_error\",\"code\":\"{code}\",\ + \"message\":\"synthetic upstream rejected the provider wire request\"}}}}" + ) + .into_bytes(), + } + } +} + +#[cfg(feature = "config-fixture")] +impl BoundedHttpsTransport for StrictChatTransport { + fn send( + &self, + request: &PreparedHttpsRequest, + context: &OAuthIoContext, + ) -> Result { + context.check()?; + *self.calls.lock().expect("strict transport call lock") += 1; + let parsed: Value = match serde_json::from_str(&request.body) { + Ok(value) => value, + Err(_) => { + self.violations + .lock() + .expect("strict transport violation lock") + .push("provider request body is not JSON".to_string()); + return Ok(self.reject("invalid_request")); + } + }; + let mut violations = self + .violations + .lock() + .expect("strict transport violation lock"); + let mut schema_invalid = false; + if request.method != "POST" { + violations.push("provider request method is not POST".to_string()); + } + if request.url != format!("{SCHEMA_WIRE_BASE_URL}/chat/completions") { + violations + .push("provider request URL is not the synthetic fixture endpoint".to_string()); + } + if parsed["model"] != json!(SCHEMA_WIRE_MODEL) { + violations.push("provider request model is not the fixture model".to_string()); + } + match parsed["tools"].as_array() { + None => violations.push("provider request has no tools array".to_string()), + Some(tools) => { + if tools.len() != self.expected_tools.len() { + violations.push(format!( + "provider request tool count {} does not match the native registry", + tools.len() + )); + } + for (index, tool) in tools.iter().enumerate() { + let Some((name, description, schema)) = self.expected_tools.get(index) else { + violations.push(format!("unexpected provider tool at index {index}")); + continue; + }; + if tool["function"]["name"].as_str() != Some(name.as_str()) { + violations.push(format!("provider tool order mismatch at index {index}")); + continue; + } + if tool["function"]["description"].as_str() != Some(description.as_str()) { + violations.push(format!( + "tool {name} description must equal the native descriptor" + )); + continue; + } + let parameters = &tool["function"]["parameters"]; + if !parameters.is_object() { + schema_invalid = true; + violations.push(format!("tool {name} parameters must be a JSON object")); + } else if parameters != schema { + schema_invalid = true; + violations.push(format!( + "tool {name} parameters must equal the native registry schema" + )); + } + } + } + } + if !violations.is_empty() { + let code = if schema_invalid { + "invalid_tool_schema" + } else { + "invalid_request" + }; + return Ok(self.reject(code)); + } + drop(violations); + Ok(RawHttpsResponse { + status: 200, + headers: BTreeMap::from([("content-type".to_string(), "application/json".to_string())]), + body: format!( + "{{\"id\":\"chatcmpl-schema-wire\",\"object\":\"chat.completion\",\ + \"model\":\"{SCHEMA_WIRE_MODEL}\",\"choices\":[{{\"index\":0,\ + \"message\":{{\"role\":\"assistant\",\"content\":\"{SCHEMA_WIRE_ANSWER}\"}},\ + \"finish_reason\":\"stop\"}}],\"usage\":{{\"prompt_tokens\":1,\ + \"completion_tokens\":1,\"total_tokens\":2}}}}" + ) + .into_bytes(), + }) + } +} + +/// The complete bundled coding pipeline must hand the canonical Chat codec +/// the native registry schemas as schema_json strings: every wire tool +/// `parameters` is then a JSON object equal to the matching native descriptor +/// schema, in registry order, while the admission snapshot itself stays +/// native and unchanged. +#[cfg(feature = "config-fixture")] +#[tokio::test(flavor = "multi_thread")] +async fn bundled_chat_pipeline_emits_native_tool_schemas_as_wire_objects() { + let root = test_root("schema-wire"); + let workspace_home = root.join("workspace-home"); + let workspace = root.join("project"); + std::fs::create_dir_all(&workspace_home).expect("workspace home"); + std::fs::create_dir_all(&workspace).expect("workspace"); + let workspace_config = format!( + "version: 1\nmodel:\n provider: {SCHEMA_WIRE_PROVIDER}\n model: {SCHEMA_WIRE_MODEL}\n\ + providers:\n {SCHEMA_WIRE_PROVIDER}:\n protocol: openai-chat-completions\n \ + base_url: '{SCHEMA_WIRE_BASE_URL}'\nworkspaces:\n allowed_roots:\n - '{}'\n \ + default: '{}'\n", + workspace.display(), + workspace.display() + ); + std::fs::write(workspace_home.join("config.yaml"), workspace_config) + .expect("workspace authority config"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&workspace_home, std::fs::Permissions::from_mode(0o700)) + .expect("workspace home mode"); + std::fs::set_permissions( + workspace_home.join("config.yaml"), + std::fs::Permissions::from_mode(0o600), + ) + .expect("workspace config mode"); + } + + let home = FixtureHome::for_profile( + "schema-wire", + SCHEMA_WIRE_PROVIDER, + SCHEMA_WIRE_BASE_URL, + SCHEMA_WIRE_CREDENTIAL, + SCHEMA_WIRE_ACCESS, + ); + let native_before = rustscript_agent::bundled_tool_registry() + .expect("bundled native registry") + .snapshot() + .schemas(); + let expected_tools: Vec<(String, String, Value)> = native_before + .as_array() + .expect("native descriptor array") + .iter() + .map(|descriptor| { + ( + descriptor["name"] + .as_str() + .expect("native descriptor name") + .to_string(), + descriptor["description"] + .as_str() + .expect("native descriptor description") + .to_string(), + descriptor["schema"].clone(), + ) + }) + .collect(); + assert!( + !expected_tools.is_empty(), + "the bundled registry must expose native tool descriptors" + ); + let transport = Arc::new(StrictChatTransport::new(expected_tools)); + + let db_path = root.join("gateway.sqlite3"); + let state = AgentGatewayState::with_sqlite_path( + AgentGatewayConfig { + provider: Some(SCHEMA_WIRE_PROVIDER.to_string()), + model: SCHEMA_WIRE_MODEL.to_string(), + run_timeout: Duration::from_secs(30), + ..AgentGatewayConfig::default() + }, + &db_path, + ) + .expect("bundled coding gateway state") + .with_workspace_home(workspace_home.clone()); + let service = state.service(); + service.set_auth_host_factory(home.auth_factory(Arc::clone(&transport))); + service.upsert_provider_profile( + ProviderProfile::new( + SCHEMA_WIRE_PROVIDER, + json!({ + "profile": SCHEMA_WIRE_PROVIDER, + "protocol": "openai-chat-completions", + "base_url": SCHEMA_WIRE_BASE_URL, + "credential_id": SCHEMA_WIRE_CREDENTIAL + }), + ) + .expect("public fixture provider profile"), + ); + let admitted = service + .admit(AdmitRunRequest { + input: json!({"message": "return the fixture final reply"}), + model: Some(SCHEMA_WIRE_MODEL.to_string()), + provider: Some(SCHEMA_WIRE_PROVIDER.to_string()), + platform: "gateway_provider_config_tests".to_string(), + ..AdmitRunRequest::default() + }) + .await + .expect("admission through the real service"); + let context_before = service + .run_context(&admitted.run_id) + .expect("admitted native run context") + .tool_schemas; + assert!( + context_before == native_before, + "admission context must retain the native tool descriptors" + ); + service + .clone() + .run_worker(admitted.run_id.clone(), "ignored".to_string()) + .await; + let context_after = service + .run_context(&admitted.run_id) + .expect("retained native run context") + .tool_schemas; + assert!( + context_after == context_before, + "the native tool_schemas snapshot must not be rewritten" + ); + let native_after = rustscript_agent::bundled_tool_registry() + .expect("bundled native registry after the run") + .snapshot() + .schemas(); + assert!( + native_after == native_before, + "the bundled native registry descriptors must not be mutated" + ); + + let violations = transport + .violations + .lock() + .expect("strict transport violation lock") + .clone(); + assert!( + violations.is_empty(), + "strict synthetic upstream must accept every tool schema: {violations:?}" + ); + assert_eq!( + *transport.calls.lock().expect("strict transport call lock"), + 1, + "one provider turn" + ); + let events = service.run_events(&admitted.run_id); + let completed = events + .iter() + .filter(|event| event["event"] == "run.completed") + .count(); + assert_eq!( + completed, 1, + "the bundled coding pipeline must complete one run" + ); + let assistants: Vec = service + .session_messages(&admitted.session_id) + .into_iter() + .filter(|message| message["role"] == "assistant") + .collect(); + assert_eq!(assistants.len(), 1, "one assistant final reply"); + assert!( + assistants[0].to_string().contains(SCHEMA_WIRE_ANSWER), + "the synthetic final reply must persist" + ); + + drop(service); + drop(state); + drop(home); + std::fs::remove_dir_all(&root).expect("remove schema-wire fixture root"); +} From 6257c958423f685be927328d56ed3b3251af545a Mon Sep 17 00:00:00 2001 From: fffonion Date: Mon, 9 Nov 2026 12:37:14 +0800 Subject: [PATCH 3/7] fix(telegram): deliver buffered final assistant replies --- src/gateway/telegram_render.rs | 81 +++++++++++++++++++++++++- tests/gateway_provider_config_tests.rs | 24 ++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/src/gateway/telegram_render.rs b/src/gateway/telegram_render.rs index 4a61bd0..52d0418 100644 --- a/src/gateway/telegram_render.rs +++ b/src/gateway/telegram_render.rs @@ -85,7 +85,21 @@ impl EventRenderer { )), "compact.started" => send_line("[compact] started"), "compact.completed" => send_line("[compact] completed"), - "run.completed" => send_line("[done]"), + "run.completed" => { + let mut actions = Vec::new(); + // Buffered provider runs can publish final output without a + // text-delta event. Deliver that output before the status; + // an existing delta-owned message already carries the reply. + if self.current_message_id.is_none() + && let Some(text) = data.pointer("/output/text").and_then(Value::as_str) + && !text.is_empty() + { + self.current_text.clear(); + actions.extend(send_line(text)); + } + actions.extend(send_line("[done]")); + actions + } "run.failed" => send_line(&format!("[failed] {}", error_message(data))), "run.cancelled" => send_line("[stopped]"), // run.started, model.started, model.completed, tool.started, @@ -239,6 +253,71 @@ mod tests { use super::*; use serde_json::json; + #[test] + fn buffered_terminal_reply_is_sent_before_done() { + let mut renderer = EventRenderer::new(); + assert_eq!( + renderer.on_event( + "run.completed", + &json!({"output": {"text": "buffered final answer"}}), + ), + vec![ + RenderAction::Send { + text: "buffered final answer".to_string() + }, + RenderAction::Send { + text: "[done]".to_string() + } + ], + ); + assert!(renderer.flush().is_empty()); + } + + #[test] + fn buffered_terminal_reply_respects_utf16_chunks() { + let mut renderer = EventRenderer::new(); + let answer = "a".repeat(4095) + "\u{1f642}"; + let mut actions = renderer.on_event("run.completed", &json!({"output": {"text": answer}})); + assert_eq!( + actions.pop(), + Some(RenderAction::Send { + text: "[done]".to_string() + }) + ); + let texts: Vec<_> = actions + .into_iter() + .map(|action| match action { + RenderAction::Send { text } => text, + other => panic!("buffered reply must use plain sends: {other:?}"), + }) + .collect(); + assert_eq!(texts.concat(), answer); + assert!( + texts + .iter() + .all(|text| utf16_len(text) <= TELEGRAM_MAX_UTF16) + ); + assert!(renderer.flush().is_empty()); + } + + #[test] + fn terminal_output_does_not_duplicate_a_delta_owned_reply() { + for event_type in ["model.delta", "message.delta"] { + let mut renderer = EventRenderer::new(); + renderer.on_event(event_type, &json!({"delta": "final answer"})); + renderer.note_sent(41); + assert_eq!( + renderer.on_event( + "run.completed", + &json!({"output": {"text": "final answer"}}) + ), + vec![RenderAction::Send { + text: "[done]".to_string() + }], + ); + } + } + #[test] fn utf16_len_counts_code_units_not_chars() { assert_eq!(utf16_len("abc"), 3); diff --git a/tests/gateway_provider_config_tests.rs b/tests/gateway_provider_config_tests.rs index 7aca704..0a8ef6e 100644 --- a/tests/gateway_provider_config_tests.rs +++ b/tests/gateway_provider_config_tests.rs @@ -813,6 +813,30 @@ async fn bundled_chat_pipeline_emits_native_tool_schemas_as_wire_objects() { "the synthetic final reply must persist" ); + let mut renderer = rustscript_agent::gateway::EventRenderer::new(); + let mut delivered = Vec::new(); + for event in &events { + let kind = event["event"].as_str().expect("canonical event kind"); + for action in renderer.on_event(kind, &event["data"]) { + match action { + rustscript_agent::gateway::RenderAction::Send { text } => delivered.push(text), + rustscript_agent::gateway::RenderAction::SendDelta { text } => { + renderer.note_sent(delivered.len() as i64 + 1); + delivered.push(text); + } + rustscript_agent::gateway::RenderAction::Edit { text, .. } => delivered.push(text), + } + } + } + assert_eq!( + delivered + .iter() + .filter(|text| text.as_str() == SCHEMA_WIRE_ANSWER) + .count(), + 1, + "the real bundled pipeline must render its buffered final reply, not only a done status" + ); + drop(service); drop(state); drop(home); From 0703a6a523e5a42d55e2e8ab543d40161ac7b2b4 Mon Sep 17 00:00:00 2001 From: fffonion Date: Mon, 9 Nov 2026 13:19:50 +0800 Subject: [PATCH 4/7] fix(llm): accept tool-call messages without content --- rss/llm/openai_chat.rss | 3 +- tests/provider_opaque_tool_loop_tests.rs | 21 +++++++++-- tests/provider_openai_opaque_tests.rs | 44 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/rss/llm/openai_chat.rss b/rss/llm/openai_chat.rss index f431980..bc5f178 100644 --- a/rss/llm/openai_chat.rss +++ b/rss/llm/openai_chat.rss @@ -485,10 +485,11 @@ fn chat_parse_message( /// (chat completions) or an array of `{type, text}` parts (Responses, /// Anthropic-style transcripts). Part arrays are joined over their text-ish /// parts (`text`/`output_text`). +/// Tool-call-only assistant messages may omit content and contribute no text. fn chat_message_text(message: map) -> string { let direct: string = types::request_string(message, "content"); let mut text: string = direct; - if type(message["content"]) == "array" { + if message.has("content") && type(message["content"]) == "array" { let parts: array = message["content"]; let mut joined: string = ""; let mut i = 0; diff --git a/tests/provider_opaque_tool_loop_tests.rs b/tests/provider_opaque_tool_loop_tests.rs index 5baa4be..0b50fa8 100644 --- a/tests/provider_opaque_tool_loop_tests.rs +++ b/tests/provider_opaque_tool_loop_tests.rs @@ -158,6 +158,10 @@ fn run_tool_followup(retry: bool) { } fn run_tool_followup_with_mode(retry: bool, stateless: bool) { + run_tool_followup_with_content(retry, stateless, false); +} + +fn run_tool_followup_with_content(retry: bool, stateless: bool, omit_content: bool) { let home = FixtureHome::for_profile( "tool-followup", "openai", @@ -168,10 +172,18 @@ fn run_tool_followup_with_mode(retry: bool, stateless: bool) { let workspace = Workspace::new(); let durable = Arc::new(ReadLifecycle::default()); let transport = Arc::new(ScriptedHttpsTransport::new()); - transport.push_json(200, json!({"id": "tool-model", "choices": [{"finish_reason": "tool_calls", "message": { + let mut tool_response = json!({"id": "tool-model", "choices": [{"finish_reason": "tool_calls", "message": { "role": "assistant", "content": null, "tool_calls": [{"id": "read-1", "type": "function", "function": {"name": "read_file", "arguments": "{\"path\":\"note.txt\"}"}}] - }}]})); + }}]}); + if omit_content { + let removed = tool_response["choices"][0]["message"] + .as_object_mut() + .expect("assistant message") + .remove("content"); + assert_eq!(removed, Some(JsonValue::Null)); + } + transport.push_json(200, tool_response); if retry { transport.push_json(429, json!({"error": {"message": "rate limit", "type": "rate_limit_error", "code": "rate_limit_exceeded"}})); } @@ -318,6 +330,11 @@ fn run_tool_followup_with_mode(retry: bool, stateless: bool) { fn authenticated_tool_read_reaches_real_provider_followup() { run_tool_followup(false); } + +#[test] +fn tool_call_without_content_reaches_real_provider_followup() { + run_tool_followup_with_content(false, true, true); +} #[test] fn rate_limited_followup_keeps_tool_result_without_reexecution() { run_tool_followup(true); diff --git a/tests/provider_openai_opaque_tests.rs b/tests/provider_openai_opaque_tests.rs index 612ffda..ddf24b4 100644 --- a/tests/provider_openai_opaque_tests.rs +++ b/tests/provider_openai_opaque_tests.rs @@ -181,6 +181,50 @@ fn canonical_request() -> JsonValue { }) } +#[test] +fn opaque_openai_chat_accepts_tool_calls_without_content() { + let home = FixtureHome::new(); + let transport = Arc::new(ScriptedHttpsTransport::new()); + transport.push_json( + 200, + json!({ + "choices": [{ + "finish_reason": "tool_calls", + "message": { + "role": "assistant", + "tool_calls": [{ + "id": "call_without_content", + "type": "function", + "function": {"name": "lookup", "arguments": "{\"query\":\"catalog\"}"} + }] + } + }], + "usage": {"prompt_tokens": 3, "completion_tokens": 4, "total_tokens": 7} + }), + ); + let runner = fixture_runner(&home, Arc::clone(&transport)); + let value = runner + .run_with_context(context( + canonical_request(), + json!({"provider": "openai"}), + "synthetic-openai", + )) + .expect("a tool-call-only assistant message must decode without a content key"); + let result = vm_value_to_json(&value); + assert_eq!(result["ok"], true); + assert_eq!(result["response"]["text"], ""); + assert_eq!(result["response"]["stop_reason"], "tool_calls"); + assert_eq!( + result["response"]["tool_calls"], + json!([{ + "id": "call_without_content", + "name": "lookup", + "arguments": {"query": "catalog"} + }]) + ); + assert_eq!(transport.take_sent().len(), 1); +} + #[test] fn opaque_openai_chat_uses_native_auth_and_returns_canonical_text_usage_and_tool_calls() { let home = FixtureHome::new(); From 0380e5ab43d38ca210a68119d49cf064c852c1b4 Mon Sep 17 00:00:00 2001 From: fffonion Date: Mon, 9 Nov 2026 20:58:47 +0800 Subject: [PATCH 5/7] fix(agent): continue conversation after denied tool actions --- rss/llm/openai_chat.rss | 24 +++- rss/tools/dispatch.rss | 11 +- src/service.rs | 39 +++++- tests/provider_durable_replay_loop_tests.rs | 128 ++++++++++++++++---- 4 files changed, 170 insertions(+), 32 deletions(-) diff --git a/rss/llm/openai_chat.rss b/rss/llm/openai_chat.rss index bc5f178..14ee87f 100644 --- a/rss/llm/openai_chat.rss +++ b/rss/llm/openai_chat.rss @@ -60,6 +60,12 @@ struct WireMessage { tool_calls: array? } +struct WireToolError { + error: string, + message: string, + content: string +} + struct WireStreamOptions { include_usage: bool } @@ -228,7 +234,7 @@ fn chat_append_user_message(wire: array, message: map, index: int) if part.has("type") { if types::part_type(part) == "tool_result" { let tool_call_id: string = types::request_string(part, "tool_call_id"); - let content: string = types::request_string(part, "content"); + let content: string = chat_tool_result_output(part); result[result.length] = { role: "tool", content: content, @@ -251,6 +257,22 @@ fn chat_append_user_message(wire: array, message: map, index: int) result } +/// Preserve public failure details when a tool produced no text output. +fn chat_tool_result_output(part: map) -> string { + let content: string = types::request_string(part, "content"); + if types::request_bool(part, "is_error", false) => { + let error: map = types::request_map(part, "error"); + let failure: WireToolError = { + error: types::request_string(error, "code"), + message: types::request_string(error, "message"), + content: content + }; + json::encode(failure) + } else => { + content + } +} + /// The `[{type: "text", text}]` parts of one canonical user message. fn chat_user_text_parts(message: map) -> array { let parts: array = types::message_content(message); diff --git a/rss/tools/dispatch.rss b/rss/tools/dispatch.rss index 2b9c86c..6322fe8 100644 --- a/rss/tools/dispatch.rss +++ b/rss/tools/dispatch.rss @@ -467,11 +467,14 @@ pub fn approval_context(input: map) -> map { result } -/// Create a terminal typed tool result for Guardian/replay admission failures. +/// Denied or unavailable reviews become failed tool observations, not grants. +/// Broken run authority, replay, storage and cancellation still stop the run. pub fn approval_failure(input: map, code: string, message: string) -> map { let mut failure: map = fail_envelope(call_from_input(input), code, message, true); - // This trusted approval boundary always stops the run; ordinary tool - // errors retain their existing code-based recoverability below envelope. - failure.terminal = true; + let recoverable: bool = code == "guardian_denied" + || code == "guardian_policy_denied" + || code == "guardian_malformed_assessment" + || code == "guardian_provider_failed"; + failure.terminal = !recoverable; failure } diff --git a/src/service.rs b/src/service.rs index 9424387..6377555 100644 --- a/src/service.rs +++ b/src/service.rs @@ -3217,9 +3217,46 @@ impl AgentService { let persistence = self.inner.persistence.as_ref().ok_or_else(|| { CompactionFailure::host("durable storage is unavailable for compaction") })?; - let (generation, history, previous_summary) = + let (generation, mut history, previous_summary) = load_durable_compaction_snapshot(persistence, session_id) .map_err(CompactionFailure::host)?; + // Reviews are durable provider steps, not assistant conversation turns. + // Resolve their identities from the native request boundary, never from + // the model's response text. + let review_messages = { + let store = self.inner.store.read(); + let run_ids: HashSet<_> = store + .sessions + .get(session_id) + .into_iter() + .flat_map(|session| &session.messages) + .filter_map(|message| message.run_id.as_deref()) + .collect(); + let mut ids = HashSet::new(); + for run_id in run_ids { + if let Some(run) = store.runs.get(run_id) { + for event in &run.events { + if event.event == "model.requested" + && event + .data + .get("step_key") + .and_then(JsonValue::as_str) + .is_some_and(|key| key.starts_with("review:")) + && let Some(turn) = event.data.get("turn").and_then(JsonValue::as_u64) + { + ids.insert(durable_message_id(run_id, "turn", &turn.to_string())); + } + } + } + } + ids + }; + history.retain(|message| { + !message + .get("id") + .and_then(JsonValue::as_str) + .is_some_and(|id| review_messages.contains(id)) + }); if cancellation.requested().is_some() || cancellation.deadline_passed() { return Err(CompactionFailure::rss( "cancelled".to_string(), diff --git a/tests/provider_durable_replay_loop_tests.rs b/tests/provider_durable_replay_loop_tests.rs index 30801f1..1a7f389 100644 --- a/tests/provider_durable_replay_loop_tests.rs +++ b/tests/provider_durable_replay_loop_tests.rs @@ -843,7 +843,7 @@ async fn guardian_missing_or_unrecognized_user_context_never_uses_optional_fallb } #[tokio::test(flavor = "multi_thread")] -async fn guardian_denial_is_durable_terminal_and_uses_explicit_model_override() { +async fn guardian_denial_is_durable_and_business_followup_survives_restart() { let temp = PathBuf::from( std::env::var_os("RUSTSCRIPT_AGENT_TEST_TMP").expect("assigned fixture temp"), ); @@ -863,6 +863,7 @@ async fn guardian_denial_is_durable_terminal_and_uses_explicit_model_override() &transport.inner, json!({"role": "assistant", "content": guardian_denial}), ); + queue_text_response(&transport.inner); let db_path = root.join("gateway.sqlite3"); let mut config = service_config(); config.approvals.reviewer_model = Some("guardian-review-override".to_string()); @@ -952,18 +953,18 @@ async fn guardian_denial_is_durable_terminal_and_uses_explicit_model_override() assert_eq!( named_event_count(&events, "run.failed"), - 1, + 0, "events={events:?}" ); - assert_eq!(named_event_count(&events, "run.completed"), 0); + assert_eq!(named_event_count(&events, "run.completed"), 1); assert_eq!(named_event_count(&events, "tool.completed"), 0); assert_eq!(approval["rows"][0][7], "denied"); assert_eq!(wrote, None); - assert_eq!(observed, vec![false, false]); + assert_eq!(observed, vec![false, false, false]); assert_eq!( sent.len(), - 2, - "deny must stop before another parent provider turn" + 3, + "the denied action stays unexecuted while business feedback can continue" ); let guardian_request: JsonValue = serde_json::from_str(&sent[1].body).expect("Guardian wire request JSON"); @@ -1011,6 +1012,7 @@ async fn guardian_critical_allow_is_rejected_before_native_write() { &transport.inner, json!({"role": "assistant", "content": guardian_assessment}), ); + queue_text_response(&transport.inner); let db_path = root.join("gateway.sqlite3"); let mut config = service_config(); @@ -1101,18 +1103,18 @@ async fn guardian_critical_allow_is_rejected_before_native_write() { assert_eq!( named_event_count(&events, "run.failed"), - 1, + 0, "events={events:?}" ); - assert_eq!(named_event_count(&events, "run.completed"), 0); + assert_eq!(named_event_count(&events, "run.completed"), 1); assert_eq!(named_event_count(&events, "tool.completed"), 0); assert_eq!(approval["rows"][0][7], "denied"); assert_eq!(wrote, None); - assert_eq!(observed, vec![false, false]); + assert_eq!(observed, vec![false, false, false]); assert_eq!( sent.len(), - 2, - "deny must stop before another parent provider turn" + 3, + "the denied action stays unexecuted while business feedback can continue" ); let guardian_request: JsonValue = serde_json::from_str(&sent[1].body).expect("Guardian wire request JSON"); @@ -1134,6 +1136,72 @@ async fn guardian_critical_allow_is_rejected_before_native_write() { ); } +#[tokio::test(flavor = "multi_thread")] +async fn guardian_denial_reaches_business_followup_without_executing_action() { + let evidence = run_guardian_service_case( + "denial-feedback", + "Explain the available options without writing any files.", + "DENIED_WRITE_MUST_NOT_APPEAR", + &[r#"{"outcome":"deny","risk_level":"low","user_authorization":"unknown"}"#], + None, + None, + None, + ) + .await; + assert_eq!( + named_event_count(&evidence.events, "run.completed"), + 1, + "the conversation must continue after an action denial" + ); + assert_eq!(named_event_count(&evidence.events, "run.failed"), 0); + assert_eq!(evidence.approval["rows"][0][7], "denied"); + assert_eq!(evidence.effect_contents, None); + assert!(evidence.observed_effects.iter().all(|effect| !effect)); + assert_eq!(named_event_count(&evidence.events, "tool.started"), 0); + assert_eq!( + evidence.sent.len(), + 3, + "business request, Guardian denial, then business follow-up" + ); + let followup: JsonValue = + serde_json::from_str(&evidence.sent[2].body).expect("business follow-up JSON"); + let messages = followup["messages"].as_array().expect("follow-up messages"); + assert!( + messages.iter().any(|message| message["role"] == "tool" + && message["content"] + .as_str() + .is_some_and(|text| text.contains("guardian_denied"))), + "the persisted denial must reach the next actual model request" + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn guardian_assessment_is_not_inserted_between_tool_call_and_result() { + let evidence = run_guardian_service_case( + "private-review", + "Explain the available options without writing any files.", + "DENIED_WRITE_MUST_NOT_APPEAR", + &[r#"{"outcome":"deny","risk_level":"low","user_authorization":"unknown"}"#], + None, + None, + None, + ) + .await; + assert_eq!(evidence.sent.len(), 3); + let followup: JsonValue = + serde_json::from_str(&evidence.sent[2].body).expect("business follow-up"); + let messages = followup["messages"].as_array().expect("messages"); + assert!( + !messages.iter().any(|message| message["role"] == "assistant" + && message["content"] + .as_str() + .is_some_and(|text| text.contains("\"outcome\""))), + "Guardian assessment must remain outside the business conversation" + ); + assert_eq!(evidence.approval["rows"][0][7], "denied"); + assert_eq!(evidence.effect_contents, None); +} + #[tokio::test(flavor = "multi_thread")] async fn guardian_high_risk_requires_clear_user_authorization() { let evidence = run_guardian_service_case( @@ -1147,11 +1215,11 @@ async fn guardian_high_risk_requires_clear_user_authorization() { ) .await; assert_eq!(evidence.approval["rows"][0][7], "denied"); - assert_eq!(named_event_count(&evidence.events, "run.failed"), 1); - assert_eq!(named_event_count(&evidence.events, "run.completed"), 0); + assert_eq!(named_event_count(&evidence.events, "run.failed"), 0); + assert_eq!(named_event_count(&evidence.events, "run.completed"), 1); assert_eq!(evidence.effect_contents, None); - assert_eq!(evidence.observed_effects, vec![false, false]); - assert_eq!(evidence.sent.len(), 2); + assert_eq!(evidence.observed_effects, vec![false, false, false]); + assert_eq!(evidence.sent.len(), 3); } #[tokio::test(flavor = "multi_thread")] @@ -1274,13 +1342,18 @@ async fn guardian_malformed_exhaustion_never_falls_back_to_user() { ) .await; assert_eq!(evidence.approval["rows"][0][7], "denied"); - assert_eq!(named_event_count(&evidence.events, "run.failed"), 1); + assert_eq!(named_event_count(&evidence.events, "run.failed"), 0); + assert_eq!(named_event_count(&evidence.events, "run.completed"), 1); + assert_eq!(named_event_count(&evidence.events, "approval.required"), 0); assert_eq!(evidence.effect_contents, None); - assert_eq!(evidence.observed_effects, vec![false, false, false, false]); + assert_eq!( + evidence.observed_effects, + vec![false, false, false, false, false] + ); assert_eq!( evidence.sent.len(), - 4, - "three malformed review attempts, no user fallback or parent turn" + 5, + "three malformed review attempts, no user fallback, then business feedback" ); } @@ -1307,8 +1380,9 @@ async fn guardian_action_arguments_remain_untrusted_and_tool_free() { assert!(guardian_wire["tools"].as_array().is_none_or(Vec::is_empty)); assert_eq!(evidence.approval["rows"][0][7], "denied"); assert_eq!(evidence.effect_contents, None); - assert_eq!(evidence.observed_effects, vec![false, false]); - assert_eq!(evidence.sent.len(), 2); + assert_eq!(evidence.observed_effects, vec![false, false, false]); + assert_eq!(evidence.sent.len(), 3); + assert_eq!(named_event_count(&evidence.events, "run.completed"), 1); } #[tokio::test(flavor = "multi_thread")] @@ -1337,6 +1411,7 @@ async fn guardian_provider_failure_never_falls_back_to_user() { json!({"error": {"type": "server_error", "code": "service_unavailable", "message": "synthetic reviewer failure"}}), ); } + queue_text_response(&transport.inner); let db_path = root.join("gateway.sqlite3"); let mut config = service_config(); config.approvals.required = Some(false); @@ -1380,14 +1455,15 @@ async fn guardian_provider_failure_never_falls_back_to_user() { fs::remove_dir_all(&root).expect("remove private provider failure fixture"); assert_eq!(approval["rows"][0][7], "denied"); - assert_eq!(named_event_count(&events, "run.failed"), 1); - assert_eq!(named_event_count(&events, "run.completed"), 0); + assert_eq!(named_event_count(&events, "run.failed"), 0); + assert_eq!(named_event_count(&events, "run.completed"), 1); + assert_eq!(named_event_count(&events, "approval.required"), 0); assert_eq!(wrote, None); - assert_eq!(observed, vec![false, false, false, false]); + assert_eq!(observed, vec![false, false, false, false, false]); assert_eq!( sent.len(), - 4, - "three reviewer failures, no user or parent retry" + 5, + "three reviewer failures, no user fallback, then business feedback" ); } From 8721ceb992a797e0ff2b46021fce0c7226f35be9 Mon Sep 17 00:00:00 2001 From: fffonion Date: Mon, 9 Nov 2026 21:46:54 +0800 Subject: [PATCH 6/7] fix(gateway): preserve native tool names in completion events --- src/service.rs | 32 +++++++++++++++++++++ src/service/managed_effects.rs | 2 +- src/service/managed_graph_tests.rs | 9 ++++++ tests/provider_durable_replay_loop_tests.rs | 23 +++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/service.rs b/src/service.rs index 6377555..0b3667d 100644 --- a/src/service.rs +++ b/src/service.rs @@ -4490,6 +4490,7 @@ impl AgentService { .clock(Arc::new(SystemClock)) .tokens(Arc::new(UuidIssuer)) .durable(Arc::new(ServiceDurableLifecycle { + run_id: run_id.to_string(), store: Arc::clone(&self.inner.store), events: Arc::clone(&events), managed: handle @@ -10563,6 +10564,7 @@ impl CancellationFlag for HandleCancelFlag { } struct ServiceDurableLifecycle { + run_id: String, store: Arc>, events: Arc, managed: Option>, @@ -10727,6 +10729,35 @@ impl DurableToolLifecycle for ServiceDurableLifecycle { .map_err(map_event_commit_error); } let tool_result = canonical_tool_result(result)?; + let name = { + let store = self.store.read(); + let run = store + .runs + .get(&self.run_id) + .ok_or(LifecycleError::MissingParent)?; + let session = store + .sessions + .get(&run.session_id) + .ok_or(LifecycleError::MissingParent)?; + session + .messages + .iter() + .filter(|message| { + message.role == "assistant" + && message.run_id.as_deref() == Some(self.run_id.as_str()) + }) + .flat_map(|message| decode_message_blocks(&message.content)) + .find_map(|block| { + if block.block_type == "tool_call" + && block.tool_call_id.as_deref() == Some(call_id) + { + block.name + } else { + None + } + }) + .ok_or(LifecycleError::MissingParent)? + }; let event_type = if tool_result.ok { "tool.completed" } else { @@ -10734,6 +10765,7 @@ impl DurableToolLifecycle for ServiceDurableLifecycle { }; let mut data = json!({ "tool_call_id": call_id, + "name": name, "ok": tool_result.ok, "truncated": tool_result.truncated, }); diff --git a/src/service/managed_effects.rs b/src/service/managed_effects.rs index d171117..c5cb949 100644 --- a/src/service/managed_effects.rs +++ b/src/service/managed_effects.rs @@ -526,7 +526,7 @@ fn projection( } else { "tool.failed" }; - let mut data = json!({"tool_call_id":identity.call_id,"ok":result.ok,"truncated":result.truncated,"managed_publication":publication}); + let mut data = json!({"tool_call_id":identity.call_id,"name":identity.tool_name,"ok":result.ok,"truncated":result.truncated,"managed_publication":publication}); if let Some(error) = &result.error { data["error_code"] = json!(error.code); } diff --git a/src/service/managed_graph_tests.rs b/src/service/managed_graph_tests.rs index f3c4933..9b5de27 100644 --- a/src/service/managed_graph_tests.rs +++ b/src/service/managed_graph_tests.rs @@ -218,6 +218,15 @@ async fn begun_call_finishes_and_exact_replay_does_not_execute_again() { .unwrap() .unwrap(); assert!(record.outcome.is_some()); + let events = service.run_events(&run.run_id); + let completed = events + .iter() + .find(|event| event["event"] == "tool.completed") + .expect("managed tool completion"); + assert_eq!( + completed["data"]["name"], "terminal", + "managed completion must retain the selected tool name" + ); service.shutdown().await.unwrap(); } diff --git a/tests/provider_durable_replay_loop_tests.rs b/tests/provider_durable_replay_loop_tests.rs index 1a7f389..6cecd6c 100644 --- a/tests/provider_durable_replay_loop_tests.rs +++ b/tests/provider_durable_replay_loop_tests.rs @@ -1202,6 +1202,29 @@ async fn guardian_assessment_is_not_inserted_between_tool_call_and_result() { assert_eq!(evidence.effect_contents, None); } +#[tokio::test(flavor = "multi_thread")] +async fn native_tool_completion_preserves_requested_name_for_telegram() { + let evidence = run_guardian_service_case( + "named-completion", + "Write the requested note.", + "AUTHORIZED_NOTE", + &[r#"{"outcome":"allow","risk_level":"low","user_authorization":"high"}"#], + None, + None, + None, + ) + .await; + let completed = evidence + .events + .iter() + .find(|event| event["event"] == "tool.completed") + .expect("actual tool completion"); + assert_eq!( + completed["data"]["name"], "write_file", + "completion metadata must retain the authoritative requested tool name" + ); +} + #[tokio::test(flavor = "multi_thread")] async fn guardian_high_risk_requires_clear_user_authorization() { let evidence = run_guardian_service_case( From e32bde9c5cfec58f2d6e26c546d768cc21db1e76 Mon Sep 17 00:00:00 2001 From: fffonion Date: Mon, 9 Nov 2026 22:27:56 +0800 Subject: [PATCH 7/7] test(agent): verify managed business feedback after Guardian denial --- .../effect_commit_recovery/authority_matrix.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/effect_commit_recovery/authority_matrix.rs b/tests/effect_commit_recovery/authority_matrix.rs index 29cb606..0e1b837 100644 --- a/tests/effect_commit_recovery/authority_matrix.rs +++ b/tests/effect_commit_recovery/authority_matrix.rs @@ -16,8 +16,8 @@ async fn managed_guardian_ask_retains_original_allow_deny_and_review_counts_with if allow { provider.push_ok(response(Some(json!({"id":"reviewed-terminal","name":"terminal","arguments":{"argv":["/bin/cat","reviewed.txt"],"timeout_ms":1000}})))); provider.push_ok(guardian); - provider.push_ok(response(None)); } + provider.push_ok(response(None)); fixture .state .service() @@ -41,8 +41,8 @@ async fn managed_guardian_ask_retains_original_allow_deny_and_review_counts_with .await; assert_eq!( provider.call_count(), - if allow { 5 } else { 2 }, - "one Guardian review per write/execute action, no synthetic approvals; events={:?}", + if allow { 5 } else { 3 }, + "one Guardian review per action, followed by business feedback without synthetic approvals; events={:?}", fixture .state .service() @@ -57,6 +57,15 @@ async fn managed_guardian_ask_retains_original_allow_deny_and_review_counts_with )) .collect::>() ); + let events = fixture.state.service().run_events(&run.run_id); + assert_eq!( + events + .iter() + .filter(|event| event["event"] == "run.completed") + .count(), + 1 + ); + assert!(events.iter().all(|event| event["event"] != "run.failed")); if allow { for call in ["reviewed-write", "reviewed-terminal"] { let request = ManagedFixture::request(&run, call); @@ -83,6 +92,9 @@ async fn managed_guardian_ask_retains_original_allow_deny_and_review_counts_with } assert_eq!(provider.call_count(), 5); } else { + assert!(events.iter().all(|event| event["event"] != "tool.started")); + assert!(events.iter().any(|event| event["event"] == "tool.failed" + && event["data"]["error_code"] == "guardian_denied")); assert!( fixture .state