mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Raise the per-value additional context limit to 10,000 tokens (#63843)
## What changed Increase the truncation threshold from 1,000 to 10,000 tokens so more of each additional context value reaches the model. Add a 40 KiB bucket boundary to the `codex.additional_context.value_bytes` histogram. ## Testing Update the integration tests to check middle truncation and size metrics at the new limit, verify that an application context value at the limit survives compaction, and check that representative truncated values fit in the new histogram bucket. GitOrigin-RevId: 260dae5c01816e2383c10d81ddb90b8e937fd8c0
This commit is contained in:
@@ -26,7 +26,7 @@ use serde::Deserialize;
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::HashMap;
|
||||
|
||||
const MAX_ADDITIONAL_CONTEXT_VALUE_TOKENS: usize = 1_000;
|
||||
const MAX_ADDITIONAL_CONTEXT_VALUE_TOKENS: usize = 10_000;
|
||||
const MAX_DERIVED_TOOL_NAME_CHARS: usize = 128;
|
||||
|
||||
/// A delivered client submission replaces the active sources. Extension inputs only update their own keys.
|
||||
|
||||
@@ -888,7 +888,7 @@ async fn additional_context_values_are_truncated_before_model_input(
|
||||
) -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
const MAX_EXPECTED_EXTERNAL_CONTEXT_TEXT_BYTES: usize = 5 * 1024;
|
||||
const MAX_EXPECTED_EXTERNAL_CONTEXT_TEXT_BYTES: usize = 40 * 1024;
|
||||
|
||||
let server = start_mock_server().await;
|
||||
let request = mount_sse_once(
|
||||
@@ -906,8 +906,8 @@ async fn additional_context_values_are_truncated_before_model_input(
|
||||
})
|
||||
.build_with_auto_env(&server)
|
||||
.await?;
|
||||
let long_browser_value = format!("browser-head-{}browser-tail", "b".repeat(40_000));
|
||||
let long_automation_value = format!("automation-head-{}automation-tail", "a".repeat(40_000));
|
||||
let long_browser_value = format!("browser-head-{}browser-tail", "b".repeat(80_000));
|
||||
let long_automation_value = format!("automation-head-{}automation-tail", "a".repeat(80_000));
|
||||
let untruncated_browser_fragment =
|
||||
format!("<external_browser_info>{long_browser_value}</external_browser_info>");
|
||||
let untruncated_automation_fragment =
|
||||
@@ -953,7 +953,7 @@ async fn additional_context_values_are_truncated_before_model_input(
|
||||
};
|
||||
assert!(automation_text.starts_with(&format!(
|
||||
"<automation_info>automation-head-{}",
|
||||
"a".repeat(1024)
|
||||
"a".repeat(19_000)
|
||||
)));
|
||||
assert!(automation_text.contains("tokens truncated"));
|
||||
assert!(automation_text.ends_with("automation-tail</automation_info>"));
|
||||
@@ -974,14 +974,14 @@ async fn additional_context_values_are_truncated_before_model_input(
|
||||
assert_eq!(outputs[0]["name"], "browser_info");
|
||||
assert_eq!(outputs[0]["namespace"], "agent");
|
||||
let body = outputs[0]["output"].as_str().expect("text output");
|
||||
assert!(body.starts_with(&format!("browser-head-{}", "b".repeat(1024))));
|
||||
assert!(body.starts_with(&format!("browser-head-{}", "b".repeat(19_000))));
|
||||
assert!(body.ends_with("browser-tail"));
|
||||
assert!(body.contains("tokens truncated"));
|
||||
assert!(body.len() <= MAX_EXPECTED_EXTERNAL_CONTEXT_TEXT_BYTES);
|
||||
let mut hidden_edit = long_automation_value.clone();
|
||||
hidden_edit.replace_range(20_000..20_001, "c");
|
||||
hidden_edit.replace_range(40_000..40_001, "c");
|
||||
let mut changed_middle = long_browser_value.clone();
|
||||
changed_middle.replace_range(20_000..20_001, "c");
|
||||
changed_middle.replace_range(40_000..40_001, "c");
|
||||
let changed_prefix = changed_middle.replacen("browser-head", "browser-view", 1);
|
||||
for (value, expected_count) in [(changed_middle, 1), (changed_prefix, 2)] {
|
||||
let next = mount_sse_once(&server, sse(vec![ev_completed("updated-context")])).await;
|
||||
@@ -1045,7 +1045,7 @@ async fn additional_context_values_are_truncated_before_model_input(
|
||||
assert_eq!(user_text, "summarize context");
|
||||
assert!(external_text.starts_with(&format!(
|
||||
"<external_browser_info>browser-head-{}",
|
||||
"b".repeat(1024)
|
||||
"b".repeat(19_000)
|
||||
)));
|
||||
assert!(external_text.contains("tokens truncated"));
|
||||
assert!(external_text.ends_with("browser-tail</external_browser_info>"));
|
||||
@@ -1097,32 +1097,32 @@ async fn additional_context_metrics_record_submitted_sizes_and_truncation(
|
||||
"application_limit",
|
||||
application,
|
||||
None,
|
||||
"a".repeat(4_000),
|
||||
"a".repeat(40_000),
|
||||
false,
|
||||
),
|
||||
(
|
||||
"application_over",
|
||||
application,
|
||||
None,
|
||||
"a".repeat(4_001),
|
||||
"a".repeat(40_001),
|
||||
true,
|
||||
),
|
||||
(
|
||||
"opaque_named_limit",
|
||||
untrusted,
|
||||
Some("browser_limit"),
|
||||
"b".repeat(4_000),
|
||||
"b".repeat(40_000),
|
||||
false,
|
||||
),
|
||||
(
|
||||
"opaque_named_over",
|
||||
untrusted,
|
||||
Some("browser_over"),
|
||||
"b".repeat(4_001),
|
||||
"b".repeat(40_001),
|
||||
true,
|
||||
),
|
||||
// Size is measured in UTF-8 bytes; the unnamed entry must not acquire a key-derived label.
|
||||
("opaque_unnamed", untrusted, None, "é".repeat(2_001), true),
|
||||
("opaque_unnamed", untrusted, None, "é".repeat(20_001), true),
|
||||
("opaque_empty", untrusted, None, String::new(), false),
|
||||
];
|
||||
let values = cases
|
||||
@@ -1242,7 +1242,7 @@ async fn additional_context_metrics_record_submitted_sizes_and_truncation(
|
||||
expected_counts.insert(tags.clone(), 2);
|
||||
// The marker is outside the retained-text budget, so a truncated value can grow.
|
||||
let after_bytes = if truncated {
|
||||
4_000 + "…1 tokens truncated…".len()
|
||||
40_000 + "…1 tokens truncated…".len()
|
||||
} else {
|
||||
value.len()
|
||||
};
|
||||
|
||||
@@ -1139,12 +1139,13 @@ async fn additional_context_survives_compaction(remote: bool) -> Result<()> {
|
||||
let untrusted = |value: &str| {
|
||||
AdditionalContextEntry::new(value.to_owned(), AdditionalContextKind::Untrusted)
|
||||
};
|
||||
let app = "🧭".repeat(10_000);
|
||||
let mut context = BTreeMap::from([
|
||||
("page".to_owned(), untrusted("old selection")),
|
||||
("removed".to_owned(), untrusted("closed tab")),
|
||||
(
|
||||
"app".to_owned(),
|
||||
AdditionalContextEntry::new("editor".to_owned(), AdditionalContextKind::Application),
|
||||
AdditionalContextEntry::new(app.clone(), AdditionalContextKind::Application),
|
||||
),
|
||||
]);
|
||||
let submit = async |prompt: &str, context| -> Result<()> {
|
||||
@@ -1183,9 +1184,9 @@ async fn additional_context_survives_compaction(remote: bool) -> Result<()> {
|
||||
let app_messages = follow_up
|
||||
.message_input_texts("developer")
|
||||
.into_iter()
|
||||
.filter(|text| text == "<app>editor</app>")
|
||||
.filter(|text| text.starts_with("<app>"))
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(app_messages, ["<app>editor</app>"]);
|
||||
assert_eq!(app_messages, [format!("<app>{app}</app>")]);
|
||||
// The context belongs to the compacted window, before the preserved second prompt.
|
||||
let input = follow_up.input();
|
||||
let second_prompt = input
|
||||
|
||||
@@ -99,11 +99,11 @@ pub const THREAD_STARTED_METRIC: &str = "codex.thread.started";
|
||||
pub const ADDITIONAL_CONTEXT_COUNT_METRIC: &str = "codex.additional_context";
|
||||
/// UTF-8 value bytes before/after truncation, excluding message wrappers; after includes the marker.
|
||||
pub const ADDITIONAL_CONTEXT_VALUE_BYTES_METRIC: &str = "codex.additional_context.value_bytes";
|
||||
/// Logarithmic byte buckets through 4 MiB, with a 4 KiB boundary near the truncation cap.
|
||||
/// Logarithmic byte buckets through 4 MiB, with boundaries near the previous and current truncation caps.
|
||||
pub static ADDITIONAL_CONTEXT_VALUE_BYTES_BUCKETS: LazyLock<Vec<f64>> = LazyLock::new(|| {
|
||||
let mut buckets = context_log_buckets(/*max_exponent*/ 22.0)
|
||||
.into_iter()
|
||||
.chain([4096.0])
|
||||
.chain([4096.0, 40_960.0])
|
||||
.collect::<Vec<_>>();
|
||||
buckets.sort_by(f64::total_cmp);
|
||||
buckets.dedup();
|
||||
|
||||
@@ -140,7 +140,7 @@ fn histogram_uses_explicit_bucket_boundaries() -> Result<()> {
|
||||
#[test]
|
||||
fn additional_context_histogram_bounds_truncated_observations() -> Result<()> {
|
||||
let (metrics, exporter) = build_metrics_with_defaults(&[])?;
|
||||
let samples = [4_024, 4_027, 4_043];
|
||||
let samples = [40_024, 40_027, 40_043];
|
||||
for value in samples {
|
||||
metrics.histogram_with_boundaries(
|
||||
ADDITIONAL_CONTEXT_VALUE_BYTES_METRIC,
|
||||
@@ -155,12 +155,12 @@ fn additional_context_histogram_bounds_truncated_observations() -> Result<()> {
|
||||
&latest_metrics(&exporter),
|
||||
ADDITIONAL_CONTEXT_VALUE_BYTES_METRIC,
|
||||
);
|
||||
assert_eq!((sum, count), (12_094.0, 3));
|
||||
assert_eq!((sum, count), (120_094.0, 3));
|
||||
assert_eq!(counts.iter().sum::<u64>(), count);
|
||||
assert_eq!(counts.last(), Some(&0));
|
||||
for (upper_bound, bucket_count) in bounds.iter().zip(&counts) {
|
||||
assert!(
|
||||
*bucket_count == 0 || *upper_bound <= 4_096.0,
|
||||
*bucket_count == 0 || *upper_bound <= 40_960.0,
|
||||
"post-truncation observations landed in a bucket ending at {upper_bound} bytes"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user