Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Add bounded transcript rendering for Guardian v2 (#38414)

felixxia-oai73862481e52c
diff --git a/codex-rs/ext/guardian-v2/src/lib.rs b/codex-rs/ext/guardian-v2/src/lib.rs
index 12d2241a45..597ddfe21b 100644
--- a/codex-rs/ext/guardian-v2/src/lib.rs
+++ b/codex-rs/ext/guardian-v2/src/lib.rs
@@ -1,5 +1,7 @@
mod extension;
mod sampler;
+#[allow(dead_code, reason = "Consumed by the follow-up classifier PR")]
+mod transcript;
pub use extension::install;
pub use sampler::LunaSampler;
diff --git a/codex-rs/ext/guardian-v2/src/transcript.rs b/codex-rs/ext/guardian-v2/src/transcript.rs
new file mode 100644
index 0000000000..ba6dcb4d83
--- /dev/null
+++ b/codex-rs/ext/guardian-v2/src/transcript.rs
@@ -0,0 +1,193 @@
+use std::collections::HashMap;
+
+use codex_extension_api::ResponseItem;
+use codex_protocol::models::ContentItem;
+use codex_protocol::models::ReasoningItemContent;
+use codex_protocol::models::ReasoningItemReasoningSummary;
+use codex_protocol::models::plaintext_agent_message_content;
+
+// Provisional approximation of an 80k-token transcript budget.
+const MAX_TRANSCRIPT_BYTES: usize = 320 * 1024;
+const MANUAL_APPROVAL_DEVELOPER_PREFIX: &str =
+ "The user has manually approved a specific action that was previously `Rejected`.";
+
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub(crate) enum TranscriptSource {
+ ToolCalls,
+ ToolOutputs,
+ Reasoning,
+}
+
+#[derive(Clone, Debug, Eq, PartialEq)]
+pub(crate) struct TranscriptConfig {
+ pub(crate) sources: Vec<TranscriptSource>,
+}
+
+impl Default for TranscriptConfig {
+ fn default() -> Self {
+ Self {
+ sources: vec![
+ TranscriptSource::ToolCalls,
+ TranscriptSource::ToolOutputs,
+ TranscriptSource::Reasoning,
+ ],
+ }
+ }
+}
+
+impl TranscriptConfig {
+ pub(crate) fn build<'a>(&self, items: impl IntoIterator<Item = &'a ResponseItem>) -> String {
+ let mut transcript = String::new();
+ let mut tool_names_by_call_id = HashMap::new();
+ let mut entry_number = 0;
+
+ for item in items {
+ let (role, text) = match item {
+ ResponseItem::Message { role, content, .. } => {
+ let text = content
+ .iter()
+ .filter_map(|item| match item {
+ ContentItem::InputText { text } | ContentItem::OutputText { text }
+ if !text.is_empty() =>
+ {
+ Some(text.as_str())
+ }
+ ContentItem::InputText { .. }
+ | ContentItem::OutputText { .. }
+ | ContentItem::InputImage { .. }
+ | ContentItem::InputAudio { .. } => None,
+ })
+ .collect::<Vec<_>>()
+ .join("\n");
+ if text.trim().is_empty() {
+ continue;
+ }
+ let include_message = match role.as_str() {
+ "user" | "assistant" => true,
+ "developer" => text.starts_with(MANUAL_APPROVAL_DEVELOPER_PREFIX),
+ _ => false,
+ };
+ if !include_message {
+ continue;
+ }
+ (role.clone(), text)
+ }
+ ResponseItem::AgentMessage {
+ author, content, ..
+ } => {
+ let Some(text) = plaintext_agent_message_content(content) else {
+ continue;
+ };
+ (
+ "assistant".to_owned(),
+ format!("Agent message from {author}:\n{text}"),
+ )
+ }
+ ResponseItem::FunctionCall {
+ name,
+ arguments,
+ call_id,
+ ..
+ }
+ | ResponseItem::CustomToolCall {
+ name,
+ input: arguments,
+ call_id,
+ ..
+ } => {
+ tool_names_by_call_id.insert(call_id.as_str(), name.as_str());
+ if !self.sources.contains(&TranscriptSource::ToolCalls) {
+ continue;
+ }
+ (format!("tool {name} call"), arguments.clone())
+ }
+ ResponseItem::FunctionCallOutput {
+ call_id, output, ..
+ }
+ | ResponseItem::CustomToolCallOutput {
+ call_id, output, ..
+ } => {
+ if !self.sources.contains(&TranscriptSource::ToolOutputs) {
+ continue;
+ }
+ let Some(output) = output.body.to_text() else {
+ continue;
+ };
+ let role = tool_names_by_call_id.get(call_id.as_str()).map_or_else(
+ || "tool result".to_owned(),
+ |name| format!("tool {name} result"),
+ );
+ (role, output)
+ }
+ ResponseItem::Reasoning {
+ summary, content, ..
+ } => {
+ if !self.sources.contains(&TranscriptSource::Reasoning) {
+ continue;
+ }
+ let text = summary
+ .iter()
+ .map(|item| match item {
+ ReasoningItemReasoningSummary::SummaryText { text } => text.as_str(),
+ })
+ .chain(content.iter().flatten().map(|item| match item {
+ ReasoningItemContent::ReasoningText { text }
+ | ReasoningItemContent::Text { text } => text.as_str(),
+ }))
+ .filter(|text| !text.trim().is_empty())
+ .collect::<Vec<_>>()
+ .join("\n");
+ ("reasoning".to_owned(), text)
+ }
+ ResponseItem::LocalShellCall { action, .. } => {
+ if !self.sources.contains(&TranscriptSource::ToolCalls) {
+ continue;
+ }
+ let Ok(text) = serde_json::to_string(action) else {
+ continue;
+ };
+ ("tool shell call".to_owned(), text)
+ }
+ ResponseItem::WebSearchCall { action, .. } => {
+ if !self.sources.contains(&TranscriptSource::ToolCalls) {
+ continue;
+ }
+ let Some(action) = action else {
+ continue;
+ };
+ let Ok(text) = serde_json::to_string(action) else {
+ continue;
+ };
+ ("tool web_search call".to_owned(), text)
+ }
+ ResponseItem::AdditionalTools { .. }
+ | ResponseItem::ImageGenerationCall { .. }
+ | ResponseItem::ToolSearchCall { .. }
+ | ResponseItem::ToolSearchOutput { .. }
+ | ResponseItem::Compaction { .. }
+ | ResponseItem::CompactionTrigger { .. }
+ | ResponseItem::ContextCompaction { .. }
+ | ResponseItem::Other => continue,
+ };
+ if text.trim().is_empty() {
+ continue;
+ }
+ entry_number += 1;
+ transcript.push_str(&format!("[{entry_number}] {role}: {text}\n"));
+
+ if transcript.len() > MAX_TRANSCRIPT_BYTES {
+ let mut first_retained_byte = transcript.len() - MAX_TRANSCRIPT_BYTES;
+ while !transcript.is_char_boundary(first_retained_byte) {
+ first_retained_byte += 1;
+ }
+ transcript.drain(..first_retained_byte);
+ }
+ }
+
+ transcript
+ }
+}
+
+#[cfg(test)]
+#[path = "transcript_tests.rs"]
+mod tests;
diff --git a/codex-rs/ext/guardian-v2/src/transcript_tests.rs b/codex-rs/ext/guardian-v2/src/transcript_tests.rs
new file mode 100644
index 0000000000..bbcb92a1ea
--- /dev/null
+++ b/codex-rs/ext/guardian-v2/src/transcript_tests.rs
@@ -0,0 +1,290 @@
+use codex_extension_api::ResponseItem;
+use codex_protocol::models::AgentMessageInputContent;
+use codex_protocol::models::ContentItem;
+use codex_protocol::models::FunctionCallOutputContentItem;
+use codex_protocol::models::FunctionCallOutputPayload;
+use codex_protocol::models::ReasoningItemContent;
+use codex_protocol::models::ReasoningItemReasoningSummary;
+use pretty_assertions::assert_eq;
+
+use super::MANUAL_APPROVAL_DEVELOPER_PREFIX;
+use super::MAX_TRANSCRIPT_BYTES;
+use super::TranscriptConfig;
+use super::TranscriptSource;
+
+#[test]
+fn transcript_keeps_conversation_and_configured_sources() {
+ let items = vec![
+ ResponseItem::Message {
+ id: None,
+ role: "user".to_string(),
+ content: vec![ContentItem::InputText {
+ text: "Inspect the workspace.".to_string(),
+ }],
+ phase: None,
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::FunctionCall {
+ id: None,
+ name: "exec_command".to_string(),
+ namespace: None,
+ arguments: "{}".to_string(),
+ encrypted_function_args: None,
+ call_id: "call-1".to_string(),
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::FunctionCallOutput {
+ id: None,
+ call_id: "call-1".to_string(),
+ output: FunctionCallOutputPayload::from_text("Workspace inspected.".to_string()),
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::Reasoning {
+ id: None,
+ summary: vec![ReasoningItemReasoningSummary::SummaryText {
+ text: "Review the current files.".to_string(),
+ }],
+ content: Some(vec![ReasoningItemContent::ReasoningText {
+ text: "Plaintext reasoning.".to_string(),
+ }]),
+ encrypted_content: Some("encrypted-reasoning-blob".to_string()),
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::Message {
+ id: None,
+ role: "developer".to_string(),
+ content: Vec::new(),
+ phase: None,
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ];
+
+ let transcript = TranscriptConfig::default().build(&items);
+ assert_eq!(
+ transcript,
+ concat!(
+ "[1] user: Inspect the workspace.\n",
+ "[2] tool exec_command call: {}\n",
+ "[3] tool exec_command result: Workspace inspected.\n",
+ "[4] reasoning: Review the current files.\n",
+ "Plaintext reasoning.\n",
+ )
+ );
+
+ let output_and_reasoning = TranscriptConfig {
+ sources: vec![TranscriptSource::ToolOutputs, TranscriptSource::Reasoning],
+ };
+
+ let transcript = output_and_reasoning.build(&items);
+ assert_eq!(
+ transcript,
+ concat!(
+ "[1] user: Inspect the workspace.\n",
+ "[2] tool exec_command result: Workspace inspected.\n",
+ "[3] reasoning: Review the current files.\n",
+ "Plaintext reasoning.\n",
+ )
+ );
+
+ let calls_only = TranscriptConfig {
+ sources: vec![TranscriptSource::ToolCalls],
+ };
+
+ let transcript = calls_only.build(&items);
+ assert_eq!(
+ transcript,
+ concat!(
+ "[1] user: Inspect the workspace.\n",
+ "[2] tool exec_command call: {}\n",
+ )
+ );
+}
+
+#[test]
+fn transcript_retains_the_most_recent_bounded_content() {
+ let items = vec![
+ ResponseItem::Message {
+ id: None,
+ role: "user".to_string(),
+ content: vec![ContentItem::InputText {
+ text: "é".repeat(MAX_TRANSCRIPT_BYTES),
+ }],
+ phase: None,
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::Message {
+ id: None,
+ role: "assistant".to_string(),
+ content: vec![ContentItem::OutputText {
+ text: "latest response".to_string(),
+ }],
+ phase: None,
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ];
+
+ let transcript = TranscriptConfig::default().build(&items);
+
+ assert!(transcript.len() <= MAX_TRANSCRIPT_BYTES);
+ assert!(transcript.contains("latest response"));
+}
+
+#[test]
+fn transcript_keeps_only_manual_approval_developer_messages() {
+ let approval_text = format!("{MANUAL_APPROVAL_DEVELOPER_PREFIX}\n\nApproved action:\n{{}}");
+ let items = vec![
+ ResponseItem::Message {
+ id: None,
+ role: "developer".to_string(),
+ content: vec![ContentItem::InputText {
+ text: "ordinary developer context".to_string(),
+ }],
+ phase: None,
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::Message {
+ id: None,
+ role: "developer".to_string(),
+ content: vec![ContentItem::InputText {
+ text: approval_text.clone(),
+ }],
+ phase: None,
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ];
+
+ let transcript = TranscriptConfig::default().build(&items);
+ assert_eq!(transcript, format!("[1] developer: {approval_text}\n"));
+}
+
+#[test]
+fn transcript_omits_media_payloads_and_keeps_readable_content() {
+ let oversized_image = "A".repeat(MAX_TRANSCRIPT_BYTES + 1);
+ let items = vec![
+ ResponseItem::Message {
+ id: None,
+ role: "user".to_string(),
+ content: vec![
+ ContentItem::InputText {
+ text: "Review this screenshot.".to_string(),
+ },
+ ContentItem::InputImage {
+ image_url: format!("data:image/png;base64,{oversized_image}"),
+ detail: None,
+ },
+ ContentItem::InputAudio {
+ audio_url: "data:audio/wav;base64,audio-payload".to_string(),
+ },
+ ],
+ phase: None,
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::FunctionCallOutput {
+ id: None,
+ call_id: "call-1".to_string(),
+ output: FunctionCallOutputPayload::from_content_items(vec![
+ FunctionCallOutputContentItem::InputText {
+ text: "Screenshot captured.".to_string(),
+ },
+ FunctionCallOutputContentItem::InputImage {
+ image_url: "data:image/png;base64,tool-image".to_string(),
+ detail: None,
+ },
+ FunctionCallOutputContentItem::InputAudio {
+ audio_url: "data:audio/wav;base64,tool-audio".to_string(),
+ },
+ ]),
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::ImageGenerationCall {
+ id: None,
+ status: "completed".to_string(),
+ revised_prompt: Some("A screenshot.".to_string()),
+ result: oversized_image,
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::ToolSearchCall {
+ id: None,
+ call_id: Some("search-1".to_string()),
+ status: Some("completed".to_string()),
+ execution: "client".to_string(),
+ arguments: serde_json::json!({ "query": "Find screenshot tools" }),
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::ToolSearchOutput {
+ id: None,
+ call_id: Some("search-1".to_string()),
+ status: "completed".to_string(),
+ execution: "client".to_string(),
+ tools: vec![serde_json::json!({ "name": "capture_screenshot" })],
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ];
+
+ let transcript = TranscriptConfig::default().build(&items);
+ assert_eq!(
+ transcript,
+ concat!(
+ "[1] user: Review this screenshot.\n",
+ "[2] tool result: Screenshot captured.\n",
+ )
+ );
+}
+
+#[test]
+fn transcript_omits_encrypted_messages_arguments_and_tool_outputs() {
+ let items = vec![
+ ResponseItem::AgentMessage {
+ id: None,
+ author: "worker".to_string(),
+ recipient: "parent".to_string(),
+ content: vec![AgentMessageInputContent::EncryptedContent {
+ encrypted_content: "encrypted-agent-message".to_string(),
+ }],
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::AgentMessage {
+ id: None,
+ author: "worker".to_string(),
+ recipient: "parent".to_string(),
+ content: vec![AgentMessageInputContent::InputText {
+ text: "The workspace is ready.".to_string(),
+ }],
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::FunctionCall {
+ id: None,
+ name: "exec_command".to_string(),
+ namespace: None,
+ arguments: "{}".to_string(),
+ encrypted_function_args: Some(vec!["encrypted-function-arguments".to_string()]),
+ call_id: "call-1".to_string(),
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ResponseItem::CustomToolCallOutput {
+ id: None,
+ call_id: "call-1".to_string(),
+ name: None,
+ output: FunctionCallOutputPayload::from_content_items(vec![
+ FunctionCallOutputContentItem::InputText {
+ text: "Command completed.".to_string(),
+ },
+ FunctionCallOutputContentItem::EncryptedContent {
+ encrypted_content: "encrypted-tool-output".to_string(),
+ },
+ ]),
+ internal_chat_message_metadata_passthrough: None,
+ },
+ ];
+
+ let transcript = TranscriptConfig::default().build(&items);
+ assert_eq!(
+ transcript,
+ concat!(
+ "[1] assistant: Agent message from worker:\n",
+ "The workspace is ready.\n",
+ "[2] tool exec_command call: {}\n",
+ "[3] tool exec_command result: Command completed.\n",
+ )
+ );
+}