mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Preserve tool output truncation budgets across resume and fork (#44248)
## Why Replaying tool outputs under a different model can expand or shrink the history shown to the model if truncation uses the new model's budget. ## What changed Save the originating history truncation budget on function and custom tool outputs and reuse it during replay, preserving existing tool-specific overrides. Include the existing 20% serialization allowance once, before converting byte budgets to tokens. Rename the metadata field to `history_truncation_token_limit` while retaining `fallback_token_limit_override` as its serialized name for compatibility. ## Testing Add regression coverage for resume and fork with different model budgets, including custom tool outputs. Extend unit coverage for existing overrides and byte-budget conversion. GitOrigin-RevId: a62c8fd3e43198e2252b150fba72f022939c1070
This commit is contained in:
1 parent
b64de2f3ad
commit
aa88a0333c
15 files changed
+287
-47
No files matched your search
@@ -21,6 +21,7 @@ use codex_utils_output_truncation::approx_token_count;
|
||||
use codex_utils_output_truncation::formatted_truncate_text;
|
||||
use codex_utils_output_truncation::truncate_function_output_payload;
|
||||
use codex_utils_output_truncation::truncate_text;
|
||||
use codex_utils_output_truncation::with_serialization_allowance;
|
||||
use serde::Serialize;
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::num::NonZeroUsize;
|
||||
@@ -124,7 +125,7 @@ impl ToolOutput for McpToolOutput {
|
||||
}
|
||||
|
||||
fn fallback_token_limit_override(&self) -> Option<usize> {
|
||||
Some((self.truncation_policy * 1.2).token_budget())
|
||||
Some(with_serialization_allowance(self.truncation_policy).token_budget())
|
||||
}
|
||||
|
||||
fn to_response_item(&self, call_id: &str, _payload: &ToolPayload) -> ResponseInputItem {
|
||||
@@ -173,7 +174,7 @@ impl McpToolOutput {
|
||||
// History receives this budget in tokens. Code Mode keeps the raw result.
|
||||
truncate_function_output_payload(
|
||||
&mut payload,
|
||||
self.truncation_policy * 1.2,
|
||||
with_serialization_allowance(self.truncation_policy),
|
||||
estimate_audio_token_count,
|
||||
);
|
||||
payload
|
||||
@@ -515,7 +516,7 @@ impl ExecCommandToolOutput {
|
||||
|
||||
fn response_text(&self) -> String {
|
||||
let header = self.response_header();
|
||||
let output_budget = (self.truncation_policy * 1.2)
|
||||
let output_budget = with_serialization_allowance(self.truncation_policy)
|
||||
.byte_budget()
|
||||
.saturating_sub(header.len().saturating_add(/*rhs*/ 1));
|
||||
let mut policy = self.model_output_policy();
|
||||
|
||||
@@ -13,6 +13,7 @@ use codex_protocol::mcp::CallToolResult;
|
||||
use codex_protocol::models::function_call_output_content_items_to_text;
|
||||
use codex_protocol::protocol::TruncationPolicy;
|
||||
use codex_utils_output_truncation::truncate_text;
|
||||
use codex_utils_output_truncation::with_serialization_allowance;
|
||||
use rmcp::model::ListResourceTemplatesResult;
|
||||
use rmcp::model::ListResourcesResult;
|
||||
use rmcp::model::PaginatedRequestParams;
|
||||
@@ -362,7 +363,7 @@ where
|
||||
})?;
|
||||
// Match regular MCP tool outputs by bounding the copy persisted to the
|
||||
// rollout and injected into model context.
|
||||
let content = truncate_text(&content, truncation_policy * 1.2);
|
||||
let content = truncate_text(&content, with_serialization_allowance(truncation_policy));
|
||||
|
||||
Ok(FunctionToolOutput::from_text(content, Some(true)))
|
||||
}
|
||||
|
||||
@@ -203,7 +203,7 @@ impl AnyToolResult {
|
||||
metadata: result
|
||||
.fallback_token_limit_override()
|
||||
.map(|limit| CodexHarnessMetadata {
|
||||
fallback_token_limit_override: Some(limit),
|
||||
history_truncation_token_limit: Some(limit),
|
||||
..Default::default()
|
||||
}),
|
||||
}
|
||||
|
||||
@@ -629,7 +629,7 @@ fn post_tool_use_feedback_output_preserves_fallback_token_limit_override(
|
||||
output: FunctionCallOutputPayload::from_text("hook feedback".to_string()),
|
||||
}),
|
||||
metadata: Some(CodexHarnessMetadata {
|
||||
fallback_token_limit_override: Some(expected_token_limit),
|
||||
history_truncation_token_limit: Some(expected_token_limit),
|
||||
..Default::default()
|
||||
}),
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user