mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Support negotiated server-side truncation in Code Mode clients (#58125)
## Why Host-side truncation can bound Code Mode output before it crosses the transport, including script errors that would otherwise exceed the frame limit. ## What changed - Negotiate server-side output truncation for stdio and gRPC sessions, with client-side fallback for older hosts. - Forward output budgets for execution, waiting, and termination when server-side truncation is selected. Avoid truncating host-formatted output or appending script errors a second time. - Preserve the negotiated mode across reconnects and reject hosts that cannot maintain it. ## Testing Add transport and integration coverage for output budgets, oversized script errors, reconnect compatibility, and preservation of output-limit notices with a zero-token budget. GitOrigin-RevId: 2075101e4845eca64f45491c27a3b326788645c8
This commit is contained in:
@@ -5,17 +5,21 @@ use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Stdio;
|
||||
use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_code_mode_protocol::CellId;
|
||||
use codex_code_mode_protocol::CodeModeOutputTruncation;
|
||||
use codex_code_mode_protocol::CodeModeSessionCellExecutionLimits;
|
||||
use codex_code_mode_protocol::CodeModeSessionDelegate;
|
||||
use codex_code_mode_protocol::CodeModeSessionOptions;
|
||||
use codex_code_mode_protocol::ExecuteRequest;
|
||||
use codex_code_mode_protocol::StartedCell;
|
||||
use codex_code_mode_protocol::TerminateRequest;
|
||||
use codex_code_mode_protocol::WaitOutcome;
|
||||
use codex_code_mode_protocol::WaitRequest;
|
||||
use codex_code_mode_protocol::host::BOUNDED_OUTPUT_CAPABILITY;
|
||||
use codex_code_mode_protocol::host::Capability;
|
||||
use codex_code_mode_protocol::host::CapabilitySet;
|
||||
use codex_code_mode_protocol::host::ClientHello;
|
||||
@@ -209,9 +213,14 @@ impl Connection {
|
||||
.map_err(|error| error.to_string())?;
|
||||
let yield_capability =
|
||||
Capability::new(YIELD_OBSERVATION_CAPABILITY).map_err(|error| error.to_string())?;
|
||||
let optional_capabilities =
|
||||
CapabilitySet::try_new([session_limits_capability, yield_capability])
|
||||
.map_err(|error| error.to_string())?;
|
||||
let output_capability =
|
||||
Capability::new(BOUNDED_OUTPUT_CAPABILITY).map_err(|error| error.to_string())?;
|
||||
let optional_capabilities = CapabilitySet::try_new([
|
||||
session_limits_capability,
|
||||
yield_capability,
|
||||
output_capability,
|
||||
])
|
||||
.map_err(|error| error.to_string())?;
|
||||
let hello = ClientHello::new(
|
||||
SupportedProtocolVersions::try_new([ProtocolVersion::V1])
|
||||
.map_err(|err| err.to_string())?,
|
||||
@@ -326,27 +335,42 @@ impl Connection {
|
||||
self.alive.load(Ordering::Acquire)
|
||||
}
|
||||
|
||||
fn supports(&self, capability: &str) -> bool {
|
||||
self.capabilities
|
||||
.iter()
|
||||
.any(|value| value.as_str() == capability)
|
||||
}
|
||||
|
||||
pub(super) async fn open_session(
|
||||
&self,
|
||||
session: RemoteSession,
|
||||
limits: CodeModeSessionCellExecutionLimits,
|
||||
mut options: CodeModeSessionOptions,
|
||||
pinned: &OnceLock<CodeModeOutputTruncation>,
|
||||
) -> Result<SessionCleanup, String> {
|
||||
if limits != CodeModeSessionCellExecutionLimits::default()
|
||||
&& !self
|
||||
.capabilities
|
||||
.iter()
|
||||
.any(|capability| capability.as_str() == SESSION_RESOURCE_LIMITS_CAPABILITY)
|
||||
if options.cell_execution_limits != CodeModeSessionCellExecutionLimits::default()
|
||||
&& !self.supports(SESSION_RESOURCE_LIMITS_CAPABILITY)
|
||||
{
|
||||
return Err(format!(
|
||||
"code-mode host does not support session resource limits: missing `{SESSION_RESOURCE_LIMITS_CAPABILITY}` capability"
|
||||
));
|
||||
}
|
||||
let pinned_mode = pinned.get().copied();
|
||||
let output_truncation = if self.supports(BOUNDED_OUTPUT_CAPABILITY) {
|
||||
pinned_mode.unwrap_or(options.output_truncation)
|
||||
} else if pinned_mode == Some(CodeModeOutputTruncation::Server) {
|
||||
return Err(
|
||||
"code-mode host cannot preserve server output truncation after reconnect".into(),
|
||||
);
|
||||
} else {
|
||||
CodeModeOutputTruncation::Client
|
||||
};
|
||||
options.output_truncation = output_truncation;
|
||||
let cleanup = SessionCleanup::new();
|
||||
let cancellation = CallerCancellation::new();
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
self.send(DriverCommand::OpenSession {
|
||||
session,
|
||||
limits,
|
||||
options,
|
||||
cleanup: cleanup.clone(),
|
||||
caller_cancellation: cancellation.token(),
|
||||
response_tx,
|
||||
@@ -355,6 +379,7 @@ impl Connection {
|
||||
let result = self.receive(response_rx).await;
|
||||
cancellation.disarm();
|
||||
result?;
|
||||
pinned.get_or_init(|| output_truncation);
|
||||
Ok(cleanup)
|
||||
}
|
||||
|
||||
@@ -424,9 +449,7 @@ impl Connection {
|
||||
&self,
|
||||
signal: Option<CancellationToken>,
|
||||
) -> Option<CancellationToken> {
|
||||
self.capabilities
|
||||
.iter()
|
||||
.any(|capability| capability.as_str() == YIELD_OBSERVATION_CAPABILITY)
|
||||
self.supports(YIELD_OBSERVATION_CAPABILITY)
|
||||
.then_some(signal)
|
||||
.flatten()
|
||||
}
|
||||
@@ -434,13 +457,13 @@ impl Connection {
|
||||
pub(super) async fn terminate(
|
||||
&self,
|
||||
session: RemoteSession,
|
||||
cell_id: CellId,
|
||||
request: TerminateRequest,
|
||||
) -> Result<WaitOutcome, String> {
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
self.with_transport_deadline(Duration::ZERO, "terminate", async {
|
||||
self.send(DriverCommand::Terminate {
|
||||
session,
|
||||
cell_id,
|
||||
request,
|
||||
response_tx,
|
||||
})
|
||||
.await?;
|
||||
|
||||
Reference in New Issue
Block a user