fix(extensions): complete turn tool migration

This commit is contained in:
Dylan Hurd
2026-06-11 23:52:40 -07:00
parent c8bf7a1372
commit ea20e4a9fd
13 changed files with 243 additions and 228 deletions
@@ -1139,7 +1139,9 @@ async fn installed_tools_with_start(
registry
.tool_contributors()
.iter()
.flat_map(|contributor| contributor.tools(&session_store, &thread_store))
.flat_map(|contributor| {
contributor.tools(&session_store, &thread_store, &ExtensionData::new("turn-1"))
})
.collect()
}
@@ -1197,10 +1199,13 @@ impl GoalExtensionHarness {
}
fn tools(&self) -> Vec<Arc<dyn ToolExecutor<ToolCall>>> {
let turn_store = ExtensionData::new("turn-1");
self.registry
.tool_contributors()
.iter()
.flat_map(|contributor| contributor.tools(&self.session_store, &self.thread_store))
.flat_map(|contributor| {
contributor.tools(&self.session_store, &self.thread_store, &turn_store)
})
.collect()
}
+11 -7
View File
@@ -81,7 +81,11 @@ fn tools_are_not_contributed_when_dedicated_tools_disabled() {
assert!(
extension
.tools(&ExtensionData::new("session"), &thread_store)
.tools(
&ExtensionData::new("session"),
&thread_store,
&ExtensionData::new("turn"),
)
.is_empty()
);
}
@@ -90,6 +94,7 @@ fn tools_are_not_contributed_when_dedicated_tools_disabled() {
fn tools_are_contributed_when_enabled_with_dedicated_tools() {
let extension = MemoriesExtension::default();
let thread_store = ExtensionData::new("thread");
let turn_store = ExtensionData::new("turn");
thread_store.insert(MemoriesExtensionConfig {
enabled: true,
dedicated_tools: true,
@@ -97,11 +102,7 @@ fn tools_are_contributed_when_enabled_with_dedicated_tools() {
});
let tool_names = extension
.tools(
&ExtensionData::new("session"),
&thread_store,
&ExtensionData::new("turn"),
)
.tools(&ExtensionData::new("session"), &thread_store, &turn_store)
.into_iter()
.map(|tool| tool.tool_name())
.collect::<Vec<_>>();
@@ -123,6 +124,7 @@ fn install_registers_dedicated_tool_contributor() {
crate::install(&mut builder, /*metrics_client*/ None);
let registry = builder.build();
let thread_store = ExtensionData::new("thread");
let turn_store = ExtensionData::new("turn");
thread_store.insert(MemoriesExtensionConfig {
enabled: true,
dedicated_tools: true,
@@ -132,7 +134,9 @@ fn install_registers_dedicated_tool_contributor() {
let tool_names = registry
.tool_contributors()
.iter()
.flat_map(|contributor| contributor.tools(&ExtensionData::new("session"), &thread_store))
.flat_map(|contributor| {
contributor.tools(&ExtensionData::new("session"), &thread_store, &turn_store)
})
.map(|tool| tool.tool_name())
.collect::<Vec<_>>();
+4 -54
View File
@@ -5,6 +5,7 @@ use codex_core_skills::HostLoadedSkills;
use codex_extension_api::ConfigContributor;
use codex_extension_api::ContextContributor;
use codex_extension_api::ExtensionData;
use codex_extension_api::ExtensionFuture;
use codex_extension_api::ExtensionRegistryBuilder;
use codex_extension_api::PromptFragment;
use codex_extension_api::ThreadLifecycleContributor;
@@ -39,7 +40,7 @@ impl ContextContributor for SkillSearchExtension {
&'a self,
_session_store: &'a ExtensionData,
thread_store: &'a ExtensionData,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<PromptFragment>> + Send + 'a>> {
) -> ExtensionFuture<'a, Vec<PromptFragment>> {
Box::pin(async move {
let Some(config) = thread_store.get::<SkillSearchExtensionConfig>() else {
return Vec::new();
@@ -112,56 +113,5 @@ pub fn install(registry: &mut ExtensionRegistryBuilder<Config>) {
}
#[cfg(test)]
mod tests {
use codex_extension_api::PromptSlot;
use codex_extension_api::ToolContributor;
use codex_extension_api::ToolName;
use pretty_assertions::assert_eq;
use super::*;
use crate::tool::SKILL_SEARCH_TOOL_NAME;
#[tokio::test]
async fn prompt_contribution_is_gated_by_feature_config() {
let extension = SkillSearchExtension;
let session_store = ExtensionData::new("session");
let thread_store = ExtensionData::new("thread");
assert!(
extension
.contribute(&session_store, &thread_store)
.await
.is_empty()
);
thread_store.insert(SkillSearchExtensionConfig { enabled: true });
let fragments = extension.contribute(&session_store, &thread_store).await;
assert_eq!(fragments.len(), 1);
assert_eq!(fragments[0].slot(), PromptSlot::DeveloperPolicy);
assert!(fragments[0].text().contains(SKILL_SEARCH_TOOL_NAME));
}
#[test]
fn tool_contribution_is_gated_by_feature_config() {
let extension = SkillSearchExtension;
let session_store = ExtensionData::new("session");
let thread_store = ExtensionData::new("thread");
let turn_store = ExtensionData::new("turn");
assert!(
extension
.tools(&session_store, &thread_store, &turn_store)
.is_empty()
);
thread_store.insert(SkillSearchExtensionConfig { enabled: true });
let tool_names = extension
.tools(&session_store, &thread_store, &turn_store)
.into_iter()
.map(|tool| tool.tool_name())
.collect::<Vec<_>>();
assert_eq!(tool_names, vec![ToolName::plain(SKILL_SEARCH_TOOL_NAME)]);
}
}
#[path = "extension_tests.rs"]
mod tests;
@@ -0,0 +1,51 @@
use codex_extension_api::PromptSlot;
use codex_extension_api::ToolContributor;
use codex_extension_api::ToolName;
use pretty_assertions::assert_eq;
use super::*;
use crate::tool::SKILL_SEARCH_TOOL_NAME;
#[tokio::test]
async fn prompt_contribution_is_gated_by_feature_config() {
let extension = SkillSearchExtension;
let session_store = ExtensionData::new("session");
let thread_store = ExtensionData::new("thread");
assert!(
extension
.contribute(&session_store, &thread_store)
.await
.is_empty()
);
thread_store.insert(SkillSearchExtensionConfig { enabled: true });
let fragments = extension.contribute(&session_store, &thread_store).await;
assert_eq!(fragments.len(), 1);
assert_eq!(fragments[0].slot(), PromptSlot::DeveloperPolicy);
assert!(fragments[0].text().contains(SKILL_SEARCH_TOOL_NAME));
}
#[test]
fn tool_contribution_is_gated_by_feature_config() {
let extension = SkillSearchExtension;
let session_store = ExtensionData::new("session");
let thread_store = ExtensionData::new("thread");
let turn_store = ExtensionData::new("turn");
assert!(
extension
.tools(&session_store, &thread_store, &turn_store)
.is_empty()
);
thread_store.insert(SkillSearchExtensionConfig { enabled: true });
let tool_names = extension
.tools(&session_store, &thread_store, &turn_store)
.into_iter()
.map(|tool| tool.tool_name())
.collect::<Vec<_>>();
assert_eq!(tool_names, vec![ToolName::plain(SKILL_SEARCH_TOOL_NAME)]);
}
+4 -141
View File
@@ -4,7 +4,7 @@ use bm25::Document;
use bm25::Language;
use bm25::SearchEngine;
use bm25::SearchEngineBuilder;
use codex_core::skills::SkillMetadata;
use codex_core_skills::SkillMetadata;
use codex_extension_api::FunctionCallError;
use codex_extension_api::ResponsesApiTool;
use codex_extension_api::ToolCall;
@@ -115,7 +115,7 @@ impl ToolExecutor<ToolCall> for SkillSearchTool {
BTreeMap::from([
(
"limit".to_string(),
JsonSchema::number(Some(format!(
JsonSchema::integer(Some(format!(
"Maximum number of skills to return (defaults to {DEFAULT_SKILL_SEARCH_LIMIT} and is capped at {MAX_SKILL_SEARCH_LIMIT})."
))),
),
@@ -170,7 +170,6 @@ fn parse_args(call: &ToolCall) -> Result<SkillSearchArgs, FunctionCallError> {
serde_json::from_value(value).map_err(|err| FunctionCallError::RespondToModel(err.to_string()))
}
#[derive(Clone, Debug, PartialEq, Eq)]
struct PlainTextToolOutput {
text: String,
}
@@ -196,141 +195,5 @@ impl ToolOutput for PlainTextToolOutput {
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use codex_core::skills::SkillPolicy;
use codex_extension_api::ConversationHistory;
use codex_extension_api::NoopTurnItemEmitter;
use codex_protocol::models::FunctionCallOutputBody;
use codex_protocol::protocol::SkillScope;
use codex_protocol::protocol::TruncationPolicy;
use codex_tools::ToolPayload;
use codex_utils_absolute_path::test_support::PathBufExt;
use codex_utils_absolute_path::test_support::test_path_buf;
use pretty_assertions::assert_eq;
use serde_json::json;
use super::*;
fn skill(name: &str, description: &str, path: &str) -> SkillMetadata {
SkillMetadata {
name: name.to_string(),
description: description.to_string(),
short_description: None,
interface: None,
dependencies: None,
policy: Some(SkillPolicy {
allow_implicit_invocation: Some(true),
products: Vec::new(),
}),
path_to_skills_md: test_path_buf(path).abs(),
scope: SkillScope::User,
plugin_id: None,
}
}
fn call(arguments: serde_json::Value) -> ToolCall {
ToolCall {
turn_id: "turn-skill-search".to_string(),
call_id: "call-skill-search".to_string(),
tool_name: ToolName::plain(SKILL_SEARCH_TOOL_NAME),
model: "gpt-test".to_string(),
truncation_policy: TruncationPolicy::Bytes(1024),
conversation_history: ConversationHistory::default(),
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
environments: Vec::new(),
payload: ToolPayload::Function {
arguments: arguments.to_string(),
},
}
}
async fn output_text(tool: &SkillSearchTool, arguments: serde_json::Value) -> String {
let output = tool
.handle(call(arguments))
.await
.expect("skill_search should return output");
let response = output.to_response_item(
"call-skill-search",
&ToolPayload::Function {
arguments: "{}".to_string(),
},
);
let ResponseInputItem::FunctionCallOutput { output, .. } = response else {
panic!("expected function output");
};
let FunctionCallOutputBody::Text(text) = output.body else {
panic!("expected text output");
};
text
}
#[tokio::test]
async fn search_returns_plain_text_skill_lines() {
let tool = SkillSearchTool::new(vec![
skill("slides", "Build presentation decks", "/tmp/slides/SKILL.md"),
skill("sheets", "Analyze spreadsheet data", "/tmp/sheets/SKILL.md"),
]);
let expected_path = test_path_buf("/tmp/slides/SKILL.md")
.abs()
.to_string_lossy()
.replace('\\', "/");
assert_eq!(
output_text(&tool, json!({ "query": "presentation deck" })).await,
format!("- slides: Build presentation decks (file: {expected_path})")
);
}
#[tokio::test]
async fn search_returns_empty_text_when_no_skill_matches() {
let tool = SkillSearchTool::new(vec![skill(
"slides",
"Build presentation decks",
"/tmp/slides/SKILL.md",
)]);
assert_eq!(output_text(&tool, json!({ "query": "quantum" })).await, "");
}
#[tokio::test]
async fn search_defaults_to_eight_and_caps_requested_limit_at_sixteen() {
let skills = (0..20)
.map(|index| {
skill(
&format!("common-{index}"),
"Common workflow",
&format!("/tmp/common-{index}/SKILL.md"),
)
})
.collect();
let tool = SkillSearchTool::new(skills);
let default_output = output_text(&tool, json!({ "query": "common workflow" })).await;
let capped_output =
output_text(&tool, json!({ "query": "common workflow", "limit": 100 })).await;
assert_eq!(default_output.lines().count(), DEFAULT_SKILL_SEARCH_LIMIT);
assert_eq!(capped_output.lines().count(), MAX_SKILL_SEARCH_LIMIT);
}
#[tokio::test]
async fn search_rejects_empty_query_and_zero_limit() {
let tool = SkillSearchTool::new(Vec::new());
let empty_query = tool
.handle(call(json!({ "query": " " })))
.await
.err()
.expect("empty query should fail");
let zero_limit = tool
.handle(call(json!({ "query": "docs", "limit": 0 })))
.await
.err()
.expect("zero limit should fail");
assert_eq!(empty_query.to_string(), "query must not be empty");
assert_eq!(zero_limit.to_string(), "limit must be greater than zero");
}
}
#[path = "tool_tests.rs"]
mod tests;
+136
View File
@@ -0,0 +1,136 @@
use std::sync::Arc;
use codex_core_skills::SkillPolicy;
use codex_extension_api::ConversationHistory;
use codex_extension_api::NoopTurnItemEmitter;
use codex_protocol::models::FunctionCallOutputBody;
use codex_protocol::protocol::SkillScope;
use codex_protocol::protocol::TruncationPolicy;
use codex_tools::ToolPayload;
use codex_utils_absolute_path::test_support::PathBufExt;
use codex_utils_absolute_path::test_support::test_path_buf;
use pretty_assertions::assert_eq;
use serde_json::json;
use super::*;
fn skill(name: &str, description: &str, path: &str) -> SkillMetadata {
SkillMetadata {
name: name.to_string(),
description: description.to_string(),
short_description: None,
interface: None,
dependencies: None,
policy: Some(SkillPolicy {
allow_implicit_invocation: Some(true),
products: Vec::new(),
}),
path_to_skills_md: test_path_buf(path).abs(),
scope: SkillScope::User,
plugin_id: None,
}
}
fn call(arguments: serde_json::Value) -> ToolCall {
ToolCall {
turn_id: "turn-skill-search".to_string(),
call_id: "call-skill-search".to_string(),
tool_name: ToolName::plain(SKILL_SEARCH_TOOL_NAME),
model: "gpt-test".to_string(),
truncation_policy: TruncationPolicy::Bytes(1024),
conversation_history: ConversationHistory::default(),
turn_item_emitter: Arc::new(NoopTurnItemEmitter),
environments: Vec::new(),
payload: ToolPayload::Function {
arguments: arguments.to_string(),
},
}
}
async fn output_text(tool: &SkillSearchTool, arguments: serde_json::Value) -> String {
let output = tool
.handle(call(arguments))
.await
.expect("skill_search should return output");
let response = output.to_response_item(
"call-skill-search",
&ToolPayload::Function {
arguments: "{}".to_string(),
},
);
let ResponseInputItem::FunctionCallOutput { output, .. } = response else {
panic!("expected function output");
};
let FunctionCallOutputBody::Text(text) = output.body else {
panic!("expected text output");
};
text
}
#[tokio::test]
async fn search_returns_plain_text_skill_lines() {
let tool = SkillSearchTool::new(vec![
skill("slides", "Build presentation decks", "/tmp/slides/SKILL.md"),
skill("sheets", "Analyze spreadsheet data", "/tmp/sheets/SKILL.md"),
]);
let expected_path = test_path_buf("/tmp/slides/SKILL.md")
.abs()
.to_string_lossy()
.replace('\\', "/");
assert_eq!(
output_text(&tool, json!({ "query": "presentation deck" })).await,
format!("- slides: Build presentation decks (file: {expected_path})")
);
}
#[tokio::test]
async fn search_returns_empty_text_when_no_skill_matches() {
let tool = SkillSearchTool::new(vec![skill(
"slides",
"Build presentation decks",
"/tmp/slides/SKILL.md",
)]);
assert_eq!(output_text(&tool, json!({ "query": "quantum" })).await, "");
}
#[tokio::test]
async fn search_defaults_to_eight_and_caps_requested_limit_at_sixteen() {
let skills = (0..20)
.map(|index| {
skill(
&format!("common-{index}"),
"Common workflow",
&format!("/tmp/common-{index}/SKILL.md"),
)
})
.collect();
let tool = SkillSearchTool::new(skills);
let default_output = output_text(&tool, json!({ "query": "common workflow" })).await;
let capped_output =
output_text(&tool, json!({ "query": "common workflow", "limit": 100 })).await;
assert_eq!(default_output.lines().count(), DEFAULT_SKILL_SEARCH_LIMIT);
assert_eq!(capped_output.lines().count(), MAX_SKILL_SEARCH_LIMIT);
}
#[tokio::test]
async fn search_rejects_empty_query_and_zero_limit() {
let tool = SkillSearchTool::new(Vec::new());
let empty_query = tool
.handle(call(json!({ "query": " " })))
.await
.err()
.expect("empty query should fail");
let zero_limit = tool
.handle(call(json!({ "query": "docs", "limit": 0 })))
.await
.err()
.expect("zero limit should fail");
assert_eq!(empty_query.to_string(), "query must not be empty");
assert_eq!(zero_limit.to_string(), "limit must be greater than zero");
}
+2 -1
View File
@@ -164,6 +164,7 @@ mod tests {
let registry = builder.build();
let session_store = ExtensionData::new("session");
let thread_store = ExtensionData::new("11111111-1111-4111-8111-111111111111");
let turn_store = ExtensionData::new("turn");
thread_store.insert(WebSearchExtensionConfig {
available: true,
provider: ModelProviderInfo::create_openai_provider(/*base_url*/ None),
@@ -173,7 +174,7 @@ mod tests {
let tool_names = registry
.tool_contributors()
.iter()
.flat_map(|contributor| contributor.tools(&session_store, &thread_store))
.flat_map(|contributor| contributor.tools(&session_store, &thread_store, &turn_store))
.map(|tool| (tool.tool_name(), tool.supports_parallel_tool_calls()))
.collect::<Vec<_>>();