Preserve parent cache affinity for ephemeral forks (#44862)

## Why

ChatGPT derives Responses cache affinity from the `session-id` header. Ephemeral forks need to reuse their parent's cache routing while retaining their own session and thread identities.

## What changed

- Inherit the parent session ID as the `prompt_cache_key` for ephemeral root forks.
- Use the prompt cache key for root-agent Responses `session-id` headers, including WebSocket handshakes. Preserve the actual session identity in turn metadata and leave non-root-agent routing unchanged.
- Keep enabled goal tool definitions visible on ephemeral threads, but reject execution with `Goal tools require a persistent thread.` and disable automatic goal continuation without persistent state.

## Testing

Add regression coverage for inherited cache routing with distinct fork identities, matching parent and fork tool definitions, WebSocket session headers, and rejection of ephemeral goal tool execution.

GitOrigin-RevId: d235e97630068b27f7ebd562dd23ac6266ebc1b9
This commit is contained in:
jif authored and copyberry committed 2026-09-11 16:41:56 +00:00
1 parent 0818b6550b
commit bc5957eac9
8 files changed
+167 -22

No files matched your search

+24 -13
View File
@@ -15,8 +15,10 @@ use codex_extension_api::ThreadResumeInput;
use codex_extension_api::ThreadStartInput;
use codex_extension_api::ThreadStopInput;
use codex_extension_api::TokenUsageContributor;
use codex_extension_api::ToolCall;
use codex_extension_api::ToolCallOutcome;
use codex_extension_api::ToolContributor;
use codex_extension_api::ToolExecutor;
use codex_extension_api::ToolFinishInput;
use codex_extension_api::ToolLifecycleContributor;
use codex_extension_api::ToolLifecycleFuture;
@@ -101,11 +103,12 @@ where
Box::pin(async move {
let config = (self.goal_config)(input.config);
let enabled = config.enabled;
let tools_available_for_thread = input.persistent_thread_state_available
&& !matches!(
input.session_source,
SessionSource::SubAgent(SubAgentSource::Review)
);
let tools_visible_for_thread = !matches!(
input.session_source,
SessionSource::SubAgent(SubAgentSource::Review)
);
let tools_available_for_thread =
input.persistent_thread_state_available && tools_visible_for_thread;
input.thread_store.insert(config);
let accounting_state = input
.thread_store
@@ -149,6 +152,7 @@ where
analytics: self.analytics.clone(),
enabled,
tools_available_for_thread,
tools_visible_for_thread,
root_accounting_state,
},
)
@@ -538,16 +542,16 @@ where
.get::<GoalExtensionConfig>()
.and_then(|config| config.max_goal_token_budget);
vec![
Arc::new(GoalToolExecutor::get(
let tools = [
GoalToolExecutor::get(
runtime.thread_id(),
Arc::clone(&self.state_dbs),
runtime.accounting_state(),
self.analytics.clone(),
self.event_emitter.clone(),
self.metrics.clone(),
)),
Arc::new(GoalToolExecutor::create(
),
GoalToolExecutor::create(
runtime.thread_id(),
Arc::clone(&self.state_dbs),
runtime.accounting_state(),
@@ -555,16 +559,23 @@ where
self.event_emitter.clone(),
self.metrics.clone(),
max_goal_token_budget,
)),
Arc::new(GoalToolExecutor::update(
),
GoalToolExecutor::update(
runtime.thread_id(),
Arc::clone(&self.state_dbs),
runtime.accounting_state(),
self.analytics.clone(),
self.event_emitter.clone(),
self.metrics.clone(),
)),
]
),
];
tools
.into_iter()
.map(|mut tool| {
tool.execution_allowed = runtime.tools_available();
Arc::new(tool) as Arc<dyn for<'call> ToolExecutor<ToolCall<'call>>>
})
.collect()
}
}
+8 -1
View File
@@ -33,6 +33,7 @@ pub(crate) struct GoalRuntimeConfig {
pub(crate) analytics: GoalAnalytics,
pub(crate) enabled: bool,
pub(crate) tools_available_for_thread: bool,
pub(crate) tools_visible_for_thread: bool,
pub(crate) root_accounting_state: Option<Arc<GoalAccountingState>>,
}
@@ -54,6 +55,7 @@ struct GoalRuntimeInner {
root_accounting_state: Option<Arc<GoalAccountingState>>,
enabled: AtomicBool,
tools_available_for_thread: bool,
tools_visible_for_thread: bool,
goal_state_lock: Semaphore,
}
@@ -107,6 +109,7 @@ impl GoalRuntimeHandle {
root_accounting_state: config.root_accounting_state,
enabled: AtomicBool::new(config.enabled),
tools_available_for_thread: config.tools_available_for_thread,
tools_visible_for_thread: config.tools_visible_for_thread,
goal_state_lock: Semaphore::new(/*permits*/ 1),
}),
}
@@ -121,6 +124,10 @@ impl GoalRuntimeHandle {
}
pub(crate) fn tools_visible(&self) -> bool {
self.is_enabled() && self.inner.tools_visible_for_thread
}
pub(crate) fn tools_available(&self) -> bool {
self.is_enabled() && self.inner.tools_available_for_thread
}
@@ -416,7 +423,7 @@ impl GoalRuntimeHandle {
}
pub(crate) async fn continue_if_idle(&self) -> Result<(), String> {
if !self.tools_visible() {
if !self.tools_available() {
self.inner.accounting_state.clear_active_goal();
return Ok(());
}
+9
View File
@@ -29,6 +29,7 @@ use crate::spec::create_update_goal_tool;
#[derive(Clone)]
pub(crate) struct GoalToolExecutor {
pub(crate) execution_allowed: bool,
kind: GoalToolKind,
thread_id: ThreadId,
state_db: Arc<codex_state::StateRuntime>,
@@ -84,6 +85,7 @@ impl GoalToolExecutor {
) -> Self {
Self {
kind: GoalToolKind::Get,
execution_allowed: true,
thread_id,
state_db,
accounting_state,
@@ -105,6 +107,7 @@ impl GoalToolExecutor {
) -> Self {
Self {
kind: GoalToolKind::Create,
execution_allowed: true,
thread_id,
state_db,
accounting_state,
@@ -125,6 +128,7 @@ impl GoalToolExecutor {
) -> Self {
Self {
kind: GoalToolKind::Update,
execution_allowed: true,
thread_id,
state_db,
accounting_state,
@@ -161,6 +165,11 @@ impl<'call> ToolExecutor<ToolCall<'call>> for GoalToolExecutor {
'call: 'a,
{
Box::pin(async move {
if !self.execution_allowed {
return Err(FunctionCallError::RespondToModel(
"Goal tools require a persistent thread.".to_string(),
));
}
match self.kind {
GoalToolKind::Get => self.handle_get(invocation).await,
GoalToolKind::Create => self.handle_create(invocation).await,