| 1 | //! Redacted thinking capture end to end: a safeguard redacted block arrives |
| 2 | //! complete on its start event, must be parked in wire order next to the |
| 3 | //! readable thinking blocks, and must ride the follow-up request byte-exact. |
| 4 | |
| 5 | use super::*; |
| 6 | use crate::llm_client::mock::{MockLlmClient, canned}; |
| 7 | use crate::tools::spec::{ApprovalRequirement, ToolCapability, ToolSpec}; |
| 8 | |
| 9 | /// A no-op mutating-free tool that auto-approves, so the streamed call is |
| 10 | /// admitted and the follow-up request is issued. |
| 11 | struct RedactedFixtureTool; |
| 12 | |
| 13 | #[async_trait::async_trait] |
| 14 | impl ToolSpec for RedactedFixtureTool { |
| 15 | fn name(&self) -> &str { |
| 16 | "redacted_fixture_read" |
| 17 | } |
| 18 | fn description(&self) -> &str { |
| 19 | "Fixture tool that closes a thinking-heavy turn." |
| 20 | } |
| 21 | fn input_schema(&self) -> Value { |
| 22 | json!({"type": "object"}) |
| 23 | } |
| 24 | fn capabilities(&self) -> Vec<ToolCapability> { |
| 25 | Vec::new() |
| 26 | } |
| 27 | fn approval_requirement(&self) -> ApprovalRequirement { |
| 28 | ApprovalRequirement::Auto |
| 29 | } |
| 30 | async fn execute(&self, _: Value, _: &ToolContext) -> Result<ToolResult, ToolError> { |
| 31 | Ok(ToolResult::success("read")) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | #[tokio::test] |
| 36 | async fn redacted_thinking_persists_in_wire_order_into_the_followup_request() { |
| 37 | let workspace = tempdir().unwrap(); |
| 38 | let mock = Arc::new(MockLlmClient::new(vec![ |
| 39 | vec![ |
| 40 | canned::message_start("redacted-turn"), |
| 41 | // Block 0: readable signed thinking, finished by its stop event. |
| 42 | StreamEvent::ContentBlockStart { |
| 43 | index: 0, |
| 44 | content_block: ContentBlockStart::Thinking { |
| 45 | thinking: "first block".to_string(), |
| 46 | }, |
| 47 | }, |
| 48 | StreamEvent::ContentBlockDelta { |
| 49 | index: 0, |
| 50 | delta: Delta::ThinkingDelta { |
| 51 | thinking: " continued".to_string(), |
| 52 | }, |
| 53 | }, |
| 54 | StreamEvent::ContentBlockDelta { |
| 55 | index: 0, |
| 56 | delta: Delta::SignatureDelta { |
| 57 | signature: "sig-1".to_string(), |
| 58 | }, |
| 59 | }, |
| 60 | canned::block_stop(0), |
| 61 | // Block 1: the redacted block — complete at start, no deltas. |
| 62 | StreamEvent::ContentBlockStart { |
| 63 | index: 1, |
| 64 | content_block: ContentBlockStart::RedactedThinking { |
| 65 | data: "ENC_REDACTED_1".to_string(), |
| 66 | }, |
| 67 | }, |
| 68 | canned::block_stop(1), |
| 69 | // The tool call closes the turn and forces the follow-up request. |
| 70 | canned::tool_use_block_start(2, "toolu_redacted", "redacted_fixture_read"), |
| 71 | canned::tool_input_delta(2, "{}"), |
| 72 | canned::block_stop(2), |
| 73 | canned::message_delta("tool_use", None), |
| 74 | canned::message_stop(), |
| 75 | ], |
| 76 | canned::simple_text_turn("done"), |
| 77 | ])); |
| 78 | let (mut engine, _handle) = Engine::new_with_model_client( |
| 79 | deterministic_engine_config(workspace.path()), |
| 80 | &Config::default(), |
| 81 | mock.clone(), |
| 82 | ); |
| 83 | engine.session.auto_approve = true; |
| 84 | let mut registry = crate::tools::ToolRegistry::new(ToolContext::new(workspace.path())); |
| 85 | registry.register(Arc::new(RedactedFixtureTool)); |
| 86 | let tools = Some(registry.to_api_tools_with_cache(true)); |
| 87 | let surface = test_tool_surface(&engine, registry, tools, AppMode::Agent); |
| 88 | let mut turn = crate::core::turn::TurnContext::new(4); |
| 89 | |
| 90 | let (status, error) = engine.run_turn(&mut turn, surface, None, None).await; |
| 91 | |
| 92 | assert_eq!(status, TurnOutcomeStatus::Completed, "{error:?}"); |
| 93 | // The persisted assistant turn keeps the whole sequence in wire order. |
| 94 | let tool_call_turn = |message: &&Message| { |
| 95 | message.role == Role::Assistant |
| 96 | && message |
| 97 | .content |
| 98 | .iter() |
| 99 | .any(|block| matches!(block, ContentBlock::ToolUse { .. })) |
| 100 | }; |
| 101 | let persisted = engine |
| 102 | .session |
| 103 | .messages |
| 104 | .iter() |
| 105 | .rev() |
| 106 | .find(tool_call_turn) |
| 107 | .expect("the turn persists the tool-call assistant message"); |
| 108 | let mut blocks = persisted.content.iter(); |
| 109 | match (blocks.next(), blocks.next(), blocks.next(), blocks.next()) { |
| 110 | ( |
| 111 | Some(ContentBlock::Thinking { |
| 112 | thinking, |
| 113 | signature, |
| 114 | state: None, |
| 115 | redacted_data: None, |
| 116 | .. |
| 117 | }), |
| 118 | Some(ContentBlock::Thinking { |
| 119 | thinking: empty, |
| 120 | signature: None, |
| 121 | state: None, |
| 122 | redacted_data: Some(data), |
| 123 | }), |
| 124 | Some(ContentBlock::ToolUse { .. }), |
| 125 | None, |
| 126 | ) => { |
| 127 | assert_eq!(thinking, "first block continued"); |
| 128 | assert_eq!(signature.as_deref(), Some("sig-1")); |
| 129 | assert!( |
| 130 | empty.is_empty(), |
| 131 | "a redacted block carries no readable text" |
| 132 | ); |
| 133 | assert_eq!(data, "ENC_REDACTED_1"); |
| 134 | } |
| 135 | other => panic!("unexpected persisted assistant blocks: {other:?}"), |
| 136 | } |
| 137 | |
| 138 | // The follow-up request replays the same sequence, redacted payload |
| 139 | // included byte-exact. |
| 140 | assert!( |
| 141 | mock.call_count() >= 2, |
| 142 | "the admitted tool call forces a follow-up request" |
| 143 | ); |
| 144 | let requests = mock.captured_requests(); |
| 145 | let followup = requests[1] |
| 146 | .messages |
| 147 | .iter() |
| 148 | .rev() |
| 149 | .find(tool_call_turn) |
| 150 | .expect("follow-up request carries the prior assistant turn"); |
| 151 | let mut replayed = followup.content.iter(); |
| 152 | match (replayed.next(), replayed.next(), replayed.next()) { |
| 153 | ( |
| 154 | Some(ContentBlock::Thinking { |
| 155 | thinking, |
| 156 | signature, |
| 157 | .. |
| 158 | }), |
| 159 | Some(ContentBlock::Thinking { |
| 160 | redacted_data: Some(data), |
| 161 | .. |
| 162 | }), |
| 163 | Some(ContentBlock::ToolUse { .. }), |
| 164 | ) => { |
| 165 | assert_eq!(thinking, "first block continued"); |
| 166 | assert_eq!(signature.as_deref(), Some("sig-1")); |
| 167 | assert_eq!(data, "ENC_REDACTED_1", "payload replays byte-exact"); |
| 168 | } |
| 169 | other => panic!("unexpected replayed assistant blocks: {other:?}"), |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// A stream that parks a redacted block and then dies without a completion |
| 174 | /// must not be silently re-issued: the parked block is received content, so |
| 175 | /// the death surfaces its error instead of double-billing for the turn. |
| 176 | #[tokio::test] |
| 177 | async fn stream_dying_after_a_redacted_block_is_not_treated_as_empty() { |
| 178 | let workspace = tempdir().unwrap(); |
| 179 | let mock = Arc::new(MockLlmClient::new(vec![vec![ |
| 180 | canned::message_start("redacted-then-die"), |
| 181 | StreamEvent::ContentBlockStart { |
| 182 | index: 0, |
| 183 | content_block: ContentBlockStart::RedactedThinking { |
| 184 | data: "ENC_REDACTED_DIE".to_string(), |
| 185 | }, |
| 186 | }, |
| 187 | canned::block_stop(0), |
| 188 | StreamEvent::Error { |
| 189 | error: json!({ "message": "Model not exist." }), |
| 190 | }, |
| 191 | ]])); |
| 192 | let (mut engine, _handle) = Engine::new_with_model_client( |
| 193 | deterministic_engine_config(workspace.path()), |
| 194 | &Config::default(), |
| 195 | mock.clone(), |
| 196 | ); |
| 197 | let surface = test_tool_surface( |
| 198 | &engine, |
| 199 | crate::tools::ToolRegistry::new(ToolContext::new(workspace.path())), |
| 200 | None, |
| 201 | AppMode::Agent, |
| 202 | ); |
| 203 | let mut turn = crate::core::turn::TurnContext::new(4); |
| 204 | |
| 205 | let (status, error) = engine.run_turn(&mut turn, surface, None, None).await; |
| 206 | |
| 207 | assert_eq!(status, TurnOutcomeStatus::Failed); |
| 208 | assert!( |
| 209 | error |
| 210 | .as_deref() |
| 211 | .is_some_and(|e| e.contains("Model not exist.")), |
| 212 | "{error:?}" |
| 213 | ); |
| 214 | assert_eq!( |
| 215 | mock.call_count(), |
| 216 | 1, |
| 217 | "a stream that captured a redacted block authorizes no silent re-run" |
| 218 | ); |
| 219 | } |
| 220 |