Make extension tools thread scoped

This commit is contained in:
Eric Traut
2026-05-27 22:21:08 -07:00
parent 3e272a15cf
commit e7a165a29c
10 changed files with 65 additions and 97 deletions
+3
View File
@@ -1092,6 +1092,9 @@ impl Session {
contributor
.on_thread_start(codex_extension_api::ThreadStartInput {
config: config.as_ref(),
session_source: &session_configuration.session_source,
persistent_thread_state_available: !config.ephemeral
&& state_db_ctx.is_some(),
response_item_injector: Arc::clone(&response_item_injector),
session_store: &sess.services.session_extension_data,
thread_store: &sess.services.thread_extension_data,
+1 -1
View File
@@ -1096,7 +1096,7 @@ pub(crate) async fn built_tools(
mcp_tools,
deferred_mcp_tools,
discoverable_tools,
extension_tool_executors: extension_tool_executors(sess, turn_context),
extension_tool_executors: extension_tool_executors(sess),
dynamic_tools: turn_context.dynamic_tools.as_slice(),
},
)))
+4 -9
View File
@@ -219,7 +219,6 @@ impl ToolRouter {
pub(crate) fn extension_tool_executors(
session: &Session,
turn_context: &TurnContext,
) -> Vec<Arc<dyn ToolExecutor<ExtensionToolCall>>> {
session
.services
@@ -227,14 +226,10 @@ pub(crate) fn extension_tool_executors(
.tool_contributors()
.iter()
.flat_map(|contributor| {
contributor.tools_for_turn(codex_extension_api::ToolContributionInput {
session_store: &session.services.session_extension_data,
thread_store: &session.services.thread_extension_data,
turn_store: turn_context.extension_data.as_ref(),
session_source: &turn_context.session_source,
persistent_thread_state_available: !turn_context.config.ephemeral
&& session.state_db().is_some(),
})
contributor.tools(
&session.services.session_extension_data,
&session.services.thread_extension_data,
)
})
.collect()
}
+1 -1
View File
@@ -347,7 +347,7 @@ async fn extension_tool_executors_are_model_visible_and_dispatchable() -> anyhow
deferred_mcp_tools: None,
mcp_tools: None,
discoverable_tools: None,
extension_tool_executors: extension_tool_executors(&session, &turn),
extension_tool_executors: extension_tool_executors(&session),
dynamic_tools: turn.dynamic_tools.as_slice(),
},
);
@@ -5,7 +5,6 @@ use std::sync::Arc;
use codex_protocol::items::TurnItem;
use codex_protocol::protocol::ReviewDecision;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::TokenUsageInfo;
use codex_tools::ToolCall;
use codex_tools::ToolExecutor;
@@ -177,29 +176,6 @@ pub trait ToolContributor: Send + Sync {
session_store: &ExtensionData,
thread_store: &ExtensionData,
) -> Vec<Arc<dyn ToolExecutor<ToolCall>>>;
/// Returns the native tools visible for the current turn, when the host has
/// turn context available.
fn tools_for_turn(
&self,
input: ToolContributionInput<'_>,
) -> Vec<Arc<dyn ToolExecutor<ToolCall>>> {
self.tools(input.session_store, input.thread_store)
}
}
/// Context supplied while collecting extension-owned tools for a turn.
pub struct ToolContributionInput<'a> {
/// Store scoped to the host session runtime.
pub session_store: &'a ExtensionData,
/// Store scoped to this thread runtime.
pub thread_store: &'a ExtensionData,
/// Store scoped to the current turn runtime.
pub turn_store: &'a ExtensionData,
/// Source for the current turn's session.
pub session_source: &'a SessionSource,
/// Whether persistent state is available for the current thread.
pub persistent_thread_state_available: bool,
}
/// Contributor for host-owned tool lifecycle gates.
@@ -1,5 +1,7 @@
use std::sync::Arc;
use codex_protocol::protocol::SessionSource;
use crate::ExtensionData;
use crate::ResponseInjectionItem;
use crate::ResponseItemInjector;
@@ -28,6 +30,10 @@ impl IdleTurnPolicy {
pub struct ThreadStartInput<'a, C> {
/// Host configuration visible at thread start.
pub config: &'a C,
/// Source for this thread's session.
pub session_source: &'a SessionSource,
/// Whether persistent state is available for this thread.
pub persistent_thread_state_available: bool,
/// Host-provided helper for injecting model-visible input into this
/// thread's active turn.
pub response_item_injector: Arc<dyn ResponseItemInjector>,
-1
View File
@@ -45,7 +45,6 @@ pub use contributors::ThreadStopInput;
pub use contributors::TokenUsageContributor;
pub use contributors::ToolCallOutcome;
pub use contributors::ToolCallSource;
pub use contributors::ToolContributionInput;
pub use contributors::ToolContributor;
pub use contributors::ToolFinishInput;
pub use contributors::ToolLifecycleContributor;
+7 -18
View File
@@ -13,7 +13,6 @@ use codex_extension_api::ThreadLifecycleContributor;
use codex_extension_api::ThreadStartInput;
use codex_extension_api::TokenUsageContributor;
use codex_extension_api::ToolCallOutcome;
use codex_extension_api::ToolContributionInput;
use codex_extension_api::ToolContributor;
use codex_extension_api::ToolFinishInput;
use codex_extension_api::ToolLifecycleContributor;
@@ -84,6 +83,11 @@ where
let Ok(thread_id) = ThreadId::from_string(input.thread_store.level_id()) else {
return;
};
let tools_visible = input.persistent_thread_state_available
&& !matches!(
input.session_source,
SessionSource::SubAgent(SubAgentSource::Review)
);
let runtime = input.thread_store.get_or_init::<GoalRuntimeHandle>(|| {
GoalRuntimeHandle::new(
thread_id,
@@ -92,7 +96,7 @@ where
self.metrics.clone(),
Arc::clone(&input.response_item_injector),
accounting_state,
enabled,
/*tools_visible*/ tools_visible,
)
});
runtime.set_enabled(enabled);
@@ -366,21 +370,6 @@ where
) -> Vec<Arc<dyn codex_extension_api::ToolExecutor<codex_extension_api::ToolCall>>> {
self.goal_tools_for_thread(thread_store)
}
fn tools_for_turn(
&self,
input: ToolContributionInput<'_>,
) -> Vec<Arc<dyn codex_extension_api::ToolExecutor<codex_extension_api::ToolCall>>> {
if !input.persistent_thread_state_available
|| matches!(
input.session_source,
SessionSource::SubAgent(SubAgentSource::Review)
)
{
return Vec::new();
}
self.goal_tools_for_thread(input.thread_store)
}
}
impl<C> GoalExtension<C>
@@ -394,7 +383,7 @@ where
let Some(runtime) = goal_runtime_handle(thread_store) else {
return Vec::new();
};
if !runtime.is_enabled() {
if !runtime.is_enabled() || !runtime.tools_visible() {
return Vec::new();
}
+8 -2
View File
@@ -28,6 +28,7 @@ struct GoalRuntimeInner {
response_item_injector: Arc<dyn ResponseItemInjector>,
accounting_state: Arc<GoalAccountingState>,
enabled: AtomicBool,
tools_visible: bool,
}
pub(crate) struct AccountedGoalProgress {
@@ -49,7 +50,7 @@ impl GoalRuntimeHandle {
metrics: GoalMetrics,
response_item_injector: Arc<dyn ResponseItemInjector>,
accounting_state: Arc<GoalAccountingState>,
enabled: bool,
tools_visible: bool,
) -> Self {
Self {
inner: Arc::new(GoalRuntimeInner {
@@ -59,7 +60,8 @@ impl GoalRuntimeHandle {
metrics,
response_item_injector,
accounting_state,
enabled: AtomicBool::new(enabled),
enabled: AtomicBool::new(false),
tools_visible,
}),
}
}
@@ -72,6 +74,10 @@ impl GoalRuntimeHandle {
self.inner.enabled.load(Ordering::Relaxed)
}
pub(crate) fn tools_visible(&self) -> bool {
self.inner.tools_visible
}
pub(crate) fn thread_id(&self) -> ThreadId {
self.inner.thread_id
}
@@ -15,7 +15,6 @@ use codex_extension_api::ThreadStartInput;
use codex_extension_api::ToolCall;
use codex_extension_api::ToolCallOutcome;
use codex_extension_api::ToolCallSource;
use codex_extension_api::ToolContributionInput;
use codex_extension_api::ToolExecutor;
use codex_extension_api::ToolFinishInput;
use codex_extension_api::ToolPayload;
@@ -140,12 +139,15 @@ async fn goal_tools_hidden_for_review_turns() -> anyhow::Result<()> {
let runtime = test_runtime().await?;
let thread_id = test_thread_id()?;
seed_thread_metadata(runtime.as_ref(), thread_id).await?;
let harness = GoalExtensionHarness::new(runtime, thread_id).await?;
let tools = harness.tools_for_turn(
let harness = GoalExtensionHarness::new_with_thread_start(
runtime,
thread_id,
SessionSource::SubAgent(SubAgentSource::Review),
/*persistent_thread_state_available*/ true,
);
)
.await?;
let tools = harness.tools();
assert!(tools.is_empty());
Ok(())
@@ -156,12 +158,15 @@ async fn goal_tools_hidden_without_persistent_thread_state() -> anyhow::Result<(
let runtime = test_runtime().await?;
let thread_id = test_thread_id()?;
seed_thread_metadata(runtime.as_ref(), thread_id).await?;
let harness = GoalExtensionHarness::new(runtime, thread_id).await?;
let tools = harness.tools_for_turn(
let harness = GoalExtensionHarness::new_with_thread_start(
runtime,
thread_id,
SessionSource::Cli,
/*persistent_thread_state_available*/ false,
);
)
.await?;
let tools = harness.tools();
assert!(tools.is_empty());
Ok(())
@@ -1147,6 +1152,8 @@ async fn installed_tools(
contributor
.on_thread_start(ThreadStartInput {
config: &(),
session_source: &SessionSource::Cli,
persistent_thread_state_available: true,
response_item_injector: Arc::new(NoopResponseItemInjector),
session_store: &session_store,
thread_store: &thread_store,
@@ -1157,17 +1164,7 @@ async fn installed_tools(
registry
.tool_contributors()
.iter()
.flat_map(|contributor| {
let session_source = SessionSource::Cli;
let turn_store = ExtensionData::new("turn-1");
contributor.tools_for_turn(ToolContributionInput {
session_store: &session_store,
thread_store: &thread_store,
turn_store: &turn_store,
session_source: &session_source,
persistent_thread_state_available: true,
})
})
.flat_map(|contributor| contributor.tools(&session_store, &thread_store))
.collect()
}
@@ -1182,6 +1179,21 @@ impl GoalExtensionHarness {
async fn new(
runtime: Arc<codex_state::StateRuntime>,
thread_id: ThreadId,
) -> anyhow::Result<Self> {
Self::new_with_thread_start(
runtime,
thread_id,
SessionSource::Cli,
/*persistent_thread_state_available*/ true,
)
.await
}
async fn new_with_thread_start(
runtime: Arc<codex_state::StateRuntime>,
thread_id: ThreadId,
session_source: SessionSource,
persistent_thread_state_available: bool,
) -> anyhow::Result<Self> {
let sink = Arc::new(RecordingEventSink::default());
let mut builder = ExtensionRegistryBuilder::<()>::new();
@@ -1199,6 +1211,8 @@ impl GoalExtensionHarness {
contributor
.on_thread_start(ThreadStartInput {
config: &(),
session_source: &session_source,
persistent_thread_state_available,
response_item_injector: Arc::new(NoopResponseItemInjector),
session_store: &session_store,
thread_store: &thread_store,
@@ -1214,30 +1228,10 @@ impl GoalExtensionHarness {
}
fn tools(&self) -> Vec<Arc<dyn ToolExecutor<ToolCall>>> {
self.tools_for_turn(
SessionSource::Cli,
/*persistent_thread_state_available*/ true,
)
}
fn tools_for_turn(
&self,
session_source: SessionSource,
persistent_thread_state_available: bool,
) -> Vec<Arc<dyn ToolExecutor<ToolCall>>> {
let turn_store = ExtensionData::new("turn-1");
self.registry
.tool_contributors()
.iter()
.flat_map(|contributor| {
contributor.tools_for_turn(ToolContributionInput {
session_store: &self.session_store,
thread_store: &self.thread_store,
turn_store: &turn_store,
session_source: &session_source,
persistent_thread_state_available,
})
})
.flat_map(|contributor| contributor.tools(&self.session_store, &self.thread_store))
.collect()
}