Add per-session code-mode execution limits (#37114)

## What changed

- Add `create_session_with_limits` and session-scoped cell execution limits.
- Clamp execute and wait yield times to the session's `max_yield_time_ms`
  without terminating the running cell.
- Negotiate support with remote code-mode hosts and include non-default limits
  in `session/open`, while keeping unlimited sessions compatible with hosts and
  providers that do not support limits.

## Testing

- Cover yield-time clamping, zero-timeout behavior, and isolation between
  sessions.
- Cover wire serialization, capability negotiation, unsupported hosts, and
  shared process-host execution.

GitOrigin-RevId: 9517321cd605bb87f93eeaa6ba331cc2e346e582
This commit is contained in:
Sean Huang
2026-08-05 16:09:01 +00:00
committed by copyberry
parent 2707dfc219
commit 9d00bb01c0
18 changed files with 730 additions and 57 deletions
@@ -7,6 +7,7 @@ use serde_json::Value as JsonValue;
use crate::CellId;
use crate::CodeModeNestedToolCall;
use crate::CodeModeSessionCellExecutionLimits;
use crate::CodeModeToolKind;
use crate::ExecuteRequest;
use crate::FunctionCallOutputContentItem;
@@ -16,6 +17,38 @@ use crate::ToolDefinition;
use crate::WaitOutcome;
use crate::WaitRequest;
/// The per-cell execution limits carried by a V1 session-open request.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct WireSessionCellExecutionLimits {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_yield_time_ms: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_heap_size_bytes: Option<u64>,
}
impl TryFrom<CodeModeSessionCellExecutionLimits> for WireSessionCellExecutionLimits {
type Error = TryFromIntError;
fn try_from(value: CodeModeSessionCellExecutionLimits) -> Result<Self, Self::Error> {
Ok(Self {
max_yield_time_ms: value.max_yield_time_ms,
max_heap_size_bytes: value.max_heap_size_bytes.map(u64::try_from).transpose()?,
})
}
}
impl TryFrom<WireSessionCellExecutionLimits> for CodeModeSessionCellExecutionLimits {
type Error = TryFromIntError;
fn try_from(value: WireSessionCellExecutionLimits) -> Result<Self, Self::Error> {
Ok(Self {
max_yield_time_ms: value.max_yield_time_ms,
max_heap_size_bytes: value.max_heap_size_bytes.map(usize::try_from).transpose()?,
})
}
}
/// A cell identifier with a wire representation owned by protocol V1.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(transparent)]