mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Add tool lifecycle extension contributor (#23309)
## Why Extensions that need to track runtime progress currently have no typed host signal for tool execution. The goal extension in particular needs to observe tool attempts without inspecting tool payloads, owning tool implementations, or staying coupled to core-only runtime plumbing. This adds a narrow lifecycle contributor API for host-owned tool execution: extensions can observe when an accepted tool call starts and how it finishes, while policy hooks and tool handlers continue to own payload rewriting, blocking, and execution. Relevant code: - [`ToolLifecycleContributor`](https://github.com/openai/codex/blob/3ad2850ffc7d8a1da19c65a92425637a59098f1b/codex-rs/ext/extension-api/src/contributors.rs#L119) defines the extension-facing observer contract. - [`tool_lifecycle.rs`](https://github.com/openai/codex/blob/3ad2850ffc7d8a1da19c65a92425637a59098f1b/codex-rs/ext/extension-api/src/contributors/tool_lifecycle.rs) defines the typed start/finish inputs, source, and outcome enums. - [`notify_tool_start` / `notify_tool_finish`](https://github.com/openai/codex/blob/3ad2850ffc7d8a1da19c65a92425637a59098f1b/codex-rs/core/src/tools/lifecycle.rs) bridges core tool dispatch into the extension registry. ## What Changed - Added `ToolLifecycleContributor` to `codex-extension-api`, including: - `ToolStartInput` - `ToolFinishInput` - `ToolCallSource` - `ToolCallOutcome` - Added registration and lookup support on `ExtensionRegistryBuilder` / `ExtensionRegistry`. - Wired core tool dispatch to notify lifecycle contributors for: - accepted tool starts - completed tool calls, including the tool output success marker - pre-tool-use blocks - failures before or after the handler runs - cancellation/abort in the parallel tool path - Registered the goal extension as a lifecycle contributor and added the outcome filter it will use for goal progress accounting. ## Test Coverage - Added `dispatch_notifies_tool_lifecycle_contributors` to cover lifecycle notification ordering and outcomes for successful and handler-failed tool calls.
This commit is contained in:
1 parent
4dbca61e20
commit
c69cde3547
11 files changed
+777
-34
No files matched your search
@@ -23,6 +23,97 @@ impl ToolExecutor<ToolInvocation> for TestHandler {
|
||||
|
||||
impl CoreToolRuntime for TestHandler {}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum LifecycleTestResult {
|
||||
Ok { success: bool },
|
||||
Err,
|
||||
}
|
||||
|
||||
struct LifecycleTestHandler {
|
||||
tool_name: codex_tools::ToolName,
|
||||
result: LifecycleTestResult,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl ToolExecutor<ToolInvocation> for LifecycleTestHandler {
|
||||
fn tool_name(&self) -> codex_tools::ToolName {
|
||||
self.tool_name.clone()
|
||||
}
|
||||
|
||||
async fn handle(
|
||||
&self,
|
||||
_invocation: ToolInvocation,
|
||||
) -> Result<Box<dyn crate::tools::context::ToolOutput>, FunctionCallError> {
|
||||
match self.result.clone() {
|
||||
LifecycleTestResult::Ok { success } => Ok(Box::new(
|
||||
crate::tools::context::FunctionToolOutput::from_text(
|
||||
"ok".to_string(),
|
||||
Some(success),
|
||||
),
|
||||
)),
|
||||
LifecycleTestResult::Err => Err(FunctionCallError::RespondToModel(
|
||||
"handler failed".to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CoreToolRuntime for LifecycleTestHandler {}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
enum RecordedToolLifecycle {
|
||||
Start {
|
||||
call_id: String,
|
||||
tool_name: codex_tools::ToolName,
|
||||
},
|
||||
Finish {
|
||||
call_id: String,
|
||||
tool_name: codex_tools::ToolName,
|
||||
outcome: codex_extension_api::ToolCallOutcome,
|
||||
},
|
||||
}
|
||||
|
||||
struct ToolLifecycleRecorder {
|
||||
records: Arc<std::sync::Mutex<Vec<RecordedToolLifecycle>>>,
|
||||
}
|
||||
|
||||
impl codex_extension_api::ToolLifecycleContributor for ToolLifecycleRecorder {
|
||||
fn on_tool_start<'a>(
|
||||
&'a self,
|
||||
input: codex_extension_api::ToolStartInput<'a>,
|
||||
) -> codex_extension_api::ToolLifecycleFuture<'a> {
|
||||
let records = Arc::clone(&self.records);
|
||||
let record = RecordedToolLifecycle::Start {
|
||||
call_id: input.call_id.to_string(),
|
||||
tool_name: input.tool_name.clone(),
|
||||
};
|
||||
Box::pin(async move {
|
||||
records
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.push(record);
|
||||
})
|
||||
}
|
||||
|
||||
fn on_tool_finish<'a>(
|
||||
&'a self,
|
||||
input: codex_extension_api::ToolFinishInput<'a>,
|
||||
) -> codex_extension_api::ToolLifecycleFuture<'a> {
|
||||
let records = Arc::clone(&self.records);
|
||||
let record = RecordedToolLifecycle::Finish {
|
||||
call_id: input.call_id.to_string(),
|
||||
tool_name: input.tool_name.clone(),
|
||||
outcome: input.outcome,
|
||||
};
|
||||
Box::pin(async move {
|
||||
records
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.push(record);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handler_looks_up_namespaced_aliases_explicitly() {
|
||||
let namespace = "mcp__codex_apps__gmail";
|
||||
@@ -61,3 +152,106 @@ fn handler_looks_up_namespaced_aliases_explicitly() {
|
||||
.is_some_and(|handler| Arc::ptr_eq(handler, &namespaced_handler))
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dispatch_notifies_tool_lifecycle_contributors() -> anyhow::Result<()> {
|
||||
let (mut session, turn) = crate::session::tests::make_session_and_context().await;
|
||||
let records = Arc::new(std::sync::Mutex::new(Vec::new()));
|
||||
let mut builder = codex_extension_api::ExtensionRegistryBuilder::<crate::config::Config>::new();
|
||||
builder.tool_lifecycle_contributor(Arc::new(ToolLifecycleRecorder {
|
||||
records: Arc::clone(&records),
|
||||
}));
|
||||
session.services.extensions = Arc::new(builder.build());
|
||||
|
||||
let ok_tool = codex_tools::ToolName::plain("ok_tool");
|
||||
let failing_tool = codex_tools::ToolName::plain("failing_tool");
|
||||
let ok_handler = Arc::new(LifecycleTestHandler {
|
||||
tool_name: ok_tool.clone(),
|
||||
result: LifecycleTestResult::Ok { success: false },
|
||||
}) as Arc<dyn CoreToolRuntime>;
|
||||
let failing_handler = Arc::new(LifecycleTestHandler {
|
||||
tool_name: failing_tool.clone(),
|
||||
result: LifecycleTestResult::Err,
|
||||
}) as Arc<dyn CoreToolRuntime>;
|
||||
let registry = ToolRegistry::new(HashMap::from([
|
||||
(ok_tool.clone(), ok_handler),
|
||||
(failing_tool.clone(), failing_handler),
|
||||
]));
|
||||
let session = Arc::new(session);
|
||||
let turn = Arc::new(turn);
|
||||
|
||||
registry
|
||||
.dispatch_any(test_invocation(
|
||||
Arc::clone(&session),
|
||||
Arc::clone(&turn),
|
||||
"ok-call",
|
||||
ok_tool.clone(),
|
||||
))
|
||||
.await?;
|
||||
let err = match registry
|
||||
.dispatch_any(test_invocation(
|
||||
Arc::clone(&session),
|
||||
Arc::clone(&turn),
|
||||
"failing-call",
|
||||
failing_tool.clone(),
|
||||
))
|
||||
.await
|
||||
{
|
||||
Ok(_) => panic!("failing handler should return an error"),
|
||||
Err(err) => err,
|
||||
};
|
||||
assert_eq!(err.to_string(), "handler failed");
|
||||
|
||||
let expected = vec![
|
||||
RecordedToolLifecycle::Start {
|
||||
call_id: "ok-call".to_string(),
|
||||
tool_name: ok_tool.clone(),
|
||||
},
|
||||
RecordedToolLifecycle::Finish {
|
||||
call_id: "ok-call".to_string(),
|
||||
tool_name: ok_tool,
|
||||
outcome: codex_extension_api::ToolCallOutcome::Completed { success: false },
|
||||
},
|
||||
RecordedToolLifecycle::Start {
|
||||
call_id: "failing-call".to_string(),
|
||||
tool_name: failing_tool.clone(),
|
||||
},
|
||||
RecordedToolLifecycle::Finish {
|
||||
call_id: "failing-call".to_string(),
|
||||
tool_name: failing_tool,
|
||||
outcome: codex_extension_api::ToolCallOutcome::Failed {
|
||||
handler_executed: true,
|
||||
},
|
||||
},
|
||||
];
|
||||
let actual = records
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.drain(..)
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn test_invocation(
|
||||
session: Arc<crate::session::session::Session>,
|
||||
turn: Arc<crate::session::turn_context::TurnContext>,
|
||||
call_id: &str,
|
||||
tool_name: codex_tools::ToolName,
|
||||
) -> ToolInvocation {
|
||||
ToolInvocation {
|
||||
session,
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(
|
||||
crate::turn_diff_tracker::TurnDiffTracker::new(),
|
||||
)),
|
||||
call_id: call_id.to_string(),
|
||||
tool_name,
|
||||
source: crate::tools::context::ToolCallSource::Direct,
|
||||
payload: ToolPayload::Function {
|
||||
arguments: "{}".to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user