Python
fix(sdk): regenerate policy-blocked signed tool batches safely
Rohit Pcfed61057c31
diff --git a/docs/harness/guides/providers.md b/docs/harness/guides/providers.mdindex 83e3539e..a58031fc 100644--- a/docs/harness/guides/providers.md+++ b/docs/harness/guides/providers.md@@ -146,6 +146,12 @@ invent signatures or bypass Google's signature validation. Use a fresh sessionwhen changing providers. A provider-side proxy must preserve Google's`extra_content.google.thought_signature` fields for this flow to work.+If a guardrail blocks or changes arguments in a signed tool-call batch, the+harness discards that candidate before any tool runs and asks the model for a+fresh compliant response. This uses the existing `blockedOutputOptions.maxRetries`+limit; exhausting it stops the turn. It never pairs modified arguments with an+old signature or keeps denied arguments in conversation history.+The compatible adapter accepts Google's complete tool calls without an `index`,as well as indexed argument fragments. It never guesses how to combine partialunindexed calls, and incomplete streams cannot authorize tool execution.diff --git a/harness/runtime/src/chat_completions.rs b/harness/runtime/src/chat_completions.rsindex c5917e74..f73823af 100644--- a/harness/runtime/src/chat_completions.rs+++ b/harness/runtime/src/chat_completions.rs@@ -49,6 +49,17 @@ pub fn message_signature(item: &Value) -> bool {&& item["block"]["target"] == "message"}+pub(crate) fn signed_tool_id(item: &Value) -> Option<&str> {+ if item["type"] == "reasoning"+ && item["provider"] == GOOGLE_PRIVATE+ && item["block"]["target"] == "tool_call"+ {+ item["block"]["id"].as_str()+ } else {+ None+ }+}+fn content(value: &Value) -> Result<Value, Error> {if value.is_string() {return Ok(value.clone());diff --git a/harness/runtime/src/runtime_safety.rs b/harness/runtime/src/runtime_safety.rsindex fe504498..9ca2e994 100644--- a/harness/runtime/src/runtime_safety.rs+++ b/harness/runtime/src/runtime_safety.rs@@ -148,10 +148,16 @@ impl Runtime {.filter_map(|v| v["text"].as_str()).collect::<Vec<_>>().join("\n");- let (sanitized, blocked) = self+ let (sanitized, mut blocked) = self.check_content(sid, &config, Phase::ModelOutput, text.clone(), None, None).await?;let mut rejected_calls = vec![];+ // Google may sign only the first call in a parallel batch. Treat+ // every call in that candidate as part of the signed response.+ let signed = completion+ .output+ .iter()+ .any(|item| crate::chat_completions::signed_tool_id(item).is_some());if blocked.is_none() {for call in completion.output@@ -182,7 +188,7 @@ impl Runtime {Phase::ToolCall,args.to_string(),Some(qualified),- Some(args),+ Some(args.clone()),call["call_id"].as_str().map(String::from),).await?;@@ -191,6 +197,17 @@ impl Runtime {feedback_to_model,}) = decision{+ if signed {+ // A required signature belongs to the untouched+ // provider call. Neither persist denied arguments+ // nor mutate the signed call. Discard this whole+ // candidate and use the existing bounded retry.+ blocked = Some(Decision::Block {+ code,+ feedback_to_model,+ });+ break;+ }call["arguments"] = json!("{}");rejected_calls.push(json!({"type":"function_call_output","call_id":call["call_id"],"output":json!({"isError":true,"executed":false,"code":code,"message":feedback_to_model}).to_string()}));continue;@@ -198,7 +215,16 @@ impl Runtime {let checked: Value = serde_json::from_str(&checked).map_err(|_| Blocked)?;validate_arguments(&schema["parameters"], &checked).map_err(|_| Blocked)?;permissions::hard_check_session(&self.bundle, &session, alias, &checked)?;- call["arguments"] = json!(checked.to_string());+ if signed {+ if checked != args {+ blocked = Some(Decision::block("SIGNED_TOOL_REWRITE"));+ break;+ }+ // Even harmless JSON reformatting is unnecessary: keep+ // the original arguments paired with their signature.+ } else {+ call["arguments"] = json!(checked.to_string());+ }}}if let Some(decision) = blocked {diff --git a/harness/runtime/src/runtime_safety_tests.rs b/harness/runtime/src/runtime_safety_tests.rsindex 520abdf1..dde00cf3 100644--- a/harness/runtime/src/runtime_safety_tests.rs+++ b/harness/runtime/src/runtime_safety_tests.rs@@ -68,6 +68,133 @@ fn no_leak(rt: &Runtime, sid: &str) {}}+fn signed_write(arguments: &str) -> Vec<Value> {+ vec![+ json!({"type":"reasoning","provider":"google-chat-completions","binding":"fixture-binding","block":{"type":"thought_signature","target":"tool_call","id":"signed-call","signature":"OPAQUE_SIGNED_CALL"}}),+ json!({"type":"function_call","name":"write_file","call_id":"signed-call","arguments":arguments}),+ ]+}+fn write_schema() -> Value {+ json!({"type":"function","name":"write_file","parameters":{"type":"object","properties":{"path":{"type":"string"},"content":{"type":"string"}},"required":["path","content"]}})+}+fn signed_tool_config(action: Action) -> Config {+ Config {+ guardrails: Guardrails {+ tool_calls: vec![crate::safety::ToolRules {+ tools: vec!["example-tools.write_file".into()],+ rules: vec![Rule::regex("tool-secret", "SECRET_[A-Z]+", action)],+ }],+ ..Default::default()+ },+ ..Default::default()+ }+}++#[tokio::test]+async fn signed_tool_redaction_and_denial_discard_candidate_with_bounded_retry() {+ for action in [Action::redact(), Action::Block] {+ for recovers in [true, false] {+ let seen = Arc::new(AtomicUsize::new(0));+ let count = seen.clone();+ let mut model = Model::new("test".into(), true);+ model.sample_hook = Some(Arc::new(move |instructions, history, tx| {+ let n = count.fetch_add(1, Ordering::SeqCst);+ let raw = serde_json::to_string(&history).unwrap();+ assert!(!raw.contains("SECRET_SENTINEL"));+ assert!(!raw.contains("OPAQUE_SIGNED_CALL"));+ if n > 0 {+ assert!(instructions.contains("application policy"));+ }+ let output = if n > 0 && recovers {+ vec![message("I can offer a safe alternative.")]+ } else {+ // Only the first call is signed; an unsafe parallel sibling+ // must also cause the entire candidate to be discarded.+ let mut batch = signed_write("{\"path\":\"safe.txt\",\"content\":\"safe\"}");+ batch.push(json!({"type":"function_call","name":"write_file","call_id":"parallel-call","arguments":"{\"path\":\"no-write.txt\",\"content\":\"SECRET_SENTINEL\"}"}));+ batch+ };+ Box::pin(async move {+ tx.send(Progress::Complete(crate::provider::Completion {+ output,+ usage: Some(crate::provider::Usage {+ output_tokens: 10,+ ..Default::default()+ }),+ }))+ .await+ .unwrap();+ Ok(())+ })+ }));+ let (tmp, rt, sid) = fixture(signed_tool_config(action.clone()), model).await;+ rt.store+ .lock()+ .unwrap()+ .accept(&sid, "signed-check", "write a file")+ .unwrap();+ let result = rt+ .drive(&sid, Default::default(), vec![write_schema()])+ .await;+ if recovers {+ result.unwrap();+ } else {+ assert!(result.unwrap_err().is::<safety::Blocked>());+ }+ assert_eq!(seen.load(Ordering::SeqCst), 2);+ assert!(!tmp.path().join("work/safe.txt").exists());+ assert!(!tmp.path().join("work/no-write.txt").exists());+ no_leak(&rt, &sid);+ let db = rt.store.lock().unwrap();+ assert!(!serde_json::to_string(&db.get(&sid).unwrap().history)+ .unwrap()+ .contains("OPAQUE_SIGNED_CALL"));+ let events = db.events(&sid, 0).unwrap();+ assert!(!events.iter().any(|e| e.kind == "tool_started"));+ assert_eq!(+ events+ .iter()+ .filter(|e| e.kind == "safety_output_retry")+ .count(),+ 1+ );+ }+ }+}++#[tokio::test]+async fn allowed_signed_tool_arguments_keep_the_exact_original_encoding() {+ let arguments = "{ \"path\": \"safe.txt\", \"content\": \"safe\" }";+ let mut model = Model::new("test".into(), true);+ model.sample_hook = Some(Arc::new(move |_, _, tx| {+ Box::pin(async move {+ tx.send(Progress::Complete(crate::provider::Completion {+ output: signed_write(arguments),+ usage: Some(crate::provider::Usage {+ output_tokens: 10,+ ..Default::default()+ }),+ }))+ .await+ .unwrap();+ Ok(())+ })+ }));+ let (_tmp, rt, sid) = fixture(signed_tool_config(Action::redact()), model).await;+ rt.store+ .lock()+ .unwrap()+ .accept(&sid, "signed-pass", "write a file")+ .unwrap();+ let history = rt.store.lock().unwrap().get(&sid).unwrap().history;+ let completion = rt.sample(&sid, &history, &[write_schema()]).await.unwrap();+ assert_eq!(+ completion.output[0]["block"]["signature"],+ "OPAQUE_SIGNED_CALL"+ );+ assert_eq!(completion.output[1]["arguments"], arguments);+}+#[tokio::test]async fn user_block_precedes_admission_and_never_enters_history() {let seen = Arc::new(AtomicUsize::new(0));diff --git a/harness/runtime/tests/core.rs b/harness/runtime/tests/core.rsindex 7da56f86..16e0a71a 100644--- a/harness/runtime/tests/core.rs+++ b/harness/runtime/tests/core.rs@@ -428,15 +428,15 @@ fn history_pages_advance_across_private_items_without_disclosing_them() {#[test]fn google_signatures_survive_storage_but_never_enter_display_history() {- use openworker_runtime::provider::StreamDecoder;let dir = tempfile::tempdir().unwrap();let mut db = Store::open(dir.path()).unwrap();let mut s = session();- let mut decoder =- openworker_runtime::chat_completions::Decoder::with_binding("binding".into(), true);- decoder.event(&json!({"choices":[{"index":0,"delta":{"tool_calls":[{"id":"call-1","type":"function","function":{"name":"app__lookup","arguments":"{}"},"extra_content":{"google":{"thought_signature":"OPAQUE_PRIVATE"}}}]},"finish_reason":"stop"}]})).unwrap();- decoder.done().unwrap();- s.history.extend(decoder.finish().unwrap().output);+ // Adapter unit tests validate generation; this test checks durable storage+ // and public views without exposing the private transport decoder trait.+ s.history.extend([+ json!({"type":"reasoning","provider":"google-chat-completions","binding":"binding","block":{"type":"thought_signature","target":"tool_call","id":"call-1","signature":"OPAQUE_PRIVATE"}}),+ json!({"type":"function_call","call_id":"call-1","name":"app__lookup","arguments":"{}"}),+ ]);s.history.push(json!({"type":"function_call_output","call_id":"call-1","output":"available"}));db.save(&s, "created", json!({})).unwrap();