Files
codex/codex-rs/core/src/session/daemon_recovery.rs
T
Rohan Mehta 41cbd5b9b2 Add per-turn prompt cache TTL options (#59480)
## What changed

- Expose `PromptCacheTtl` through `TurnStartOptions` and `RecoverTurnRequest`, supporting `30m` and `12h` minimum cache lifetimes.
- Carry the selected TTL through turn execution and compaction into `prompt_cache_options.ttl` on Responses HTTP and WebSocket requests. Omit the field when unset and keep the selection scoped to the turn.
- Include prompt cache options in WebSocket request reuse checks.

## Testing

Add coverage for both TTL values and omission on a subsequent turn without an override, and extend WebSocket serialization coverage to include prompt cache options.

GitOrigin-RevId: 8529306e6ad6030fe77c5e6bf19f3810717c0c5e
2026-11-04 19:58:24 +00:00

58 lines
2.3 KiB
Rust

//! Captures interrupted turns for daemon recovery and restores persisted turn attribution.
//! Callers must flush the rollout after capturing a live turn before persisting its snapshot.
use super::Session;
use codex_protocol::protocol::EnvironmentConfigState;
use codex_protocol::protocol::TurnEnvironmentSelection;
/// Turn input and turn-start injections have entered the rollout writer.
pub(super) struct RecordedTurnInput;
impl Session {
pub(crate) async fn recovered_turn_start_options(
&self,
turn_id: &str,
) -> crate::TurnStartOptions {
self.state
.lock()
.await
.recovered_turn_start_options(turn_id)
}
/// Captures a regular turn only after its input is recorded. The caller must flush the rollout.
pub(crate) async fn interrupted_turn(
&self,
) -> Option<(String, crate::TurnStartOptions, TurnEnvironmentSelection)> {
let active = self.active_turn.lock().await;
let task = active.as_ref()?.task.as_ref()?;
if task.kind != crate::state::TaskKind::Regular || task.cancellation_token.is_cancelled() {
return None;
}
let context = &task.turn_context;
context.extension_data.get::<RecordedTurnInput>()?;
let settings = context.next_step_settings.load();
let environments = self.services.turn_environments.snapshot_now();
// Remote identities/configuration are not persisted across daemon restarts.
if environments.environments.len() != 1 {
return None;
}
let environment = environments.single_local_environment()?.selection();
if environment.config != EnvironmentConfigState::FromThread {
return None;
}
Some((
context.sub_id.clone(),
crate::TurnStartOptions {
final_output_json_schema: context.final_output_json_schema.clone(),
service_tier: Some(settings.service_tier.clone().unwrap_or_else(|| {
codex_protocol::config_types::SERVICE_TIER_DEFAULT_REQUEST_VALUE.to_string()
})),
prompt_cache_ttl: context.prompt_cache_ttl,
cyber_access_program: context.cyber_access_program,
..Default::default()
},
environment,
))
}
}