-
Notifications
You must be signed in to change notification settings - Fork 12k
[codex] Remove legacy shell output formatting paths #22706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,10 @@ pub use codex_api::ResponseEvent; | |
| use codex_config::types::Personality; | ||
| use codex_protocol::error::Result; | ||
| use codex_protocol::models::BaseInstructions; | ||
| use codex_protocol::models::FunctionCallOutputBody; | ||
| use codex_protocol::models::ResponseItem; | ||
| use codex_tools::ToolSpec; | ||
| use futures::Stream; | ||
| use serde::Deserialize; | ||
| use serde_json::Value; | ||
| use std::collections::HashSet; | ||
| use std::pin::Pin; | ||
| use std::task::Context; | ||
| use std::task::Poll; | ||
|
|
@@ -64,116 +61,10 @@ impl Default for Prompt { | |
|
|
||
| impl Prompt { | ||
| pub(crate) fn get_formatted_input(&self) -> Vec<ResponseItem> { | ||
| let mut input = self.input.clone(); | ||
|
|
||
| // when using the *Freeform* apply_patch tool specifically, tool outputs | ||
| // should be structured text, not json. Do NOT reserialize when using | ||
| // the Function tool - note that this differs from the check above for | ||
| // instructions. We declare the result as a named variable for clarity. | ||
| let is_freeform_apply_patch_tool_present = self.tools.iter().any(|tool| match tool { | ||
| ToolSpec::Freeform(f) => f.name == "apply_patch", | ||
| _ => false, | ||
| }); | ||
| if is_freeform_apply_patch_tool_present { | ||
| reserialize_shell_outputs(&mut input); | ||
| } | ||
|
|
||
| input | ||
| self.input.clone() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For resumed conversations that already contain legacy Useful? React with 👍 / 👎.
pakrym-oai marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| fn reserialize_shell_outputs(items: &mut [ResponseItem]) { | ||
| let mut shell_call_ids: HashSet<String> = HashSet::new(); | ||
|
|
||
| items.iter_mut().for_each(|item| match item { | ||
| ResponseItem::LocalShellCall { call_id, id, .. } => { | ||
| if let Some(identifier) = call_id.clone().or_else(|| id.clone()) { | ||
| shell_call_ids.insert(identifier); | ||
| } | ||
| } | ||
| ResponseItem::CustomToolCall { | ||
| id: _, | ||
| status: _, | ||
| call_id, | ||
| name, | ||
| input: _, | ||
| } => { | ||
| if name == "apply_patch" { | ||
| shell_call_ids.insert(call_id.clone()); | ||
| } | ||
| } | ||
| ResponseItem::FunctionCall { name, call_id, .. } | ||
| if is_shell_tool_name(name) || name == "apply_patch" => | ||
| { | ||
| shell_call_ids.insert(call_id.clone()); | ||
| } | ||
| ResponseItem::FunctionCallOutput { | ||
| call_id, output, .. | ||
| } | ||
| | ResponseItem::CustomToolCallOutput { | ||
| call_id, output, .. | ||
| } => { | ||
| if shell_call_ids.remove(call_id) | ||
| && let Some(structured) = output | ||
| .text_content() | ||
| .and_then(parse_structured_shell_output) | ||
| { | ||
| output.body = FunctionCallOutputBody::Text(structured); | ||
| } | ||
| } | ||
| _ => {} | ||
| }) | ||
| } | ||
|
|
||
| fn is_shell_tool_name(name: &str) -> bool { | ||
| name == "shell" | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct ExecOutputJson { | ||
| output: String, | ||
| metadata: ExecOutputMetadataJson, | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct ExecOutputMetadataJson { | ||
| exit_code: i32, | ||
| duration_seconds: f32, | ||
| } | ||
|
|
||
| fn parse_structured_shell_output(raw: &str) -> Option<String> { | ||
| let parsed: ExecOutputJson = serde_json::from_str(raw).ok()?; | ||
| Some(build_structured_output(&parsed)) | ||
| } | ||
|
|
||
| fn build_structured_output(parsed: &ExecOutputJson) -> String { | ||
| let mut sections = Vec::new(); | ||
| sections.push(format!("Exit code: {}", parsed.metadata.exit_code)); | ||
| sections.push(format!( | ||
| "Wall time: {} seconds", | ||
| parsed.metadata.duration_seconds | ||
| )); | ||
|
|
||
| let mut output = parsed.output.clone(); | ||
| if let Some((stripped, total_lines)) = strip_total_output_header(&parsed.output) { | ||
| sections.push(format!("Total output lines: {total_lines}")); | ||
| output = stripped.to_string(); | ||
| } | ||
|
|
||
| sections.push("Output:".to_string()); | ||
| sections.push(output); | ||
|
|
||
| sections.join("\n") | ||
| } | ||
|
|
||
| fn strip_total_output_header(output: &str) -> Option<(&str, u32)> { | ||
| let after_prefix = output.strip_prefix("Total output lines: ")?; | ||
| let (total_segment, remainder) = after_prefix.split_once('\n')?; | ||
| let total_lines = total_segment.parse::<u32>().ok()?; | ||
| let remainder = remainder.strip_prefix('\n').unwrap_or(remainder); | ||
| Some((remainder, total_lines)) | ||
| } | ||
|
|
||
| pub struct ResponseStream { | ||
| pub(crate) rx_event: mpsc::Receiver<Result<ResponseEvent>>, | ||
| /// Signals the mapper task that the consumer stopped polling before the | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the freeform
apply_patchtool runs,ToolEmitter::ApplyPatchstill falls through toformat_exec_output_for_model_structuredincodex-rs/core/src/tools/events.rs, so the tool result is initially a JSON string containingoutputandmetadata. This removed formatting pass was the only conversion before the next Responses request; in the existingshell_serializationcoverage forApplyPatchModelOutput::Freeform, the capturedcustom_tool_call_outputis expected to begin withExit code: 0, but this clone now sends the raw JSON wrapper to the model after any successful patch.Useful? React with 👍 / 👎.