mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Skip SessionEnd hooks when child threads shut down (#64000)
## Why A hosted child thread can inherit the root session ID and use a custom session source. The existing subagent source check can miss that child, allowing its shutdown to fire `SessionEnd` while the root is still active. ## What changed Before preparing `SessionEnd` hooks, skip threads whose ID does not match the session ID. ## Testing Extend the shutdown test to close a hosted child with an inherited session ID before closing the root and verify that exactly one `SessionEnd` hook invocation is logged for the session. GitOrigin-RevId: 4c80a17724cab57003b33080482d00f3cc2afea4
This commit is contained in:
@@ -29,6 +29,7 @@ use codex_hooks::hook_execution_mode_label;
|
||||
use codex_hooks::hook_handler_type_label;
|
||||
use codex_otel::HOOK_RUN_DURATION_METRIC;
|
||||
use codex_otel::HOOK_RUN_METRIC;
|
||||
use codex_protocol::SessionId;
|
||||
use codex_protocol::items::FunctionCallOutputItem;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::models::ContentItem;
|
||||
@@ -471,6 +472,11 @@ pub(crate) async fn run_turn_stop_hooks(
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
pub(crate) async fn run_session_end_hooks(sess: &Arc<Session>) {
|
||||
// A hosted child can share the root's session ID without a SubAgent session source.
|
||||
if sess.session_id() != SessionId::from(sess.thread_id()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Building a turn can read plugins and skills from a remote executor. Check
|
||||
// the captured hook declarations before spending any of the shutdown deadline.
|
||||
let environments = sess.services.turn_environments.snapshot_now();
|
||||
|
||||
@@ -8,7 +8,10 @@ use codex_config::HookStateToml;
|
||||
use codex_config::McpServerConfig;
|
||||
use codex_config::test_support::CloudConfigBundleFixture;
|
||||
use codex_core::AgentRuntimeOwnership;
|
||||
use codex_core::AgentTurnAdmission;
|
||||
use codex_core::ForkSnapshot;
|
||||
use codex_core::InheritedSessionId;
|
||||
use codex_core::RestrictRootCapabilities;
|
||||
use codex_core::StartThreadOptions;
|
||||
use codex_core::TurnInput;
|
||||
use codex_core::TurnInputRequest;
|
||||
@@ -41,6 +44,7 @@ use codex_protocol::protocol::Op;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
use codex_protocol::protocol::ThreadSettingsOverrides;
|
||||
use codex_protocol::protocol::ThreadSource;
|
||||
use codex_protocol::protocol::TurnSettingsUpdate;
|
||||
use codex_protocol::protocol::TurnSettingsUpdateOutcome;
|
||||
use codex_protocol::request_permissions::PermissionGrantScope;
|
||||
@@ -1603,6 +1607,24 @@ async fn session_end_flushes_transcript_and_ignores_control_output() -> Result<(
|
||||
let test = builder.build(&server).await?;
|
||||
|
||||
test.submit_turn("persist this before shutdown").await?;
|
||||
let mut options = test.start_thread_options().await;
|
||||
options.session_source = Some(SessionSource::Custom("host".into()));
|
||||
options.thread_source = Some(ThreadSource::Subagent);
|
||||
options
|
||||
.thread_extension_init
|
||||
.insert(AgentRuntimeOwnership::Host);
|
||||
options
|
||||
.thread_extension_init
|
||||
.insert(Arc::new(super::agent_control::HostRuntime) as Arc<dyn AgentTurnAdmission>);
|
||||
options
|
||||
.thread_extension_init
|
||||
.insert(RestrictRootCapabilities);
|
||||
options
|
||||
.thread_extension_init
|
||||
.insert(InheritedSessionId(test.session_configured.session_id));
|
||||
let child = test.thread_manager.start_thread(options).await?;
|
||||
child.thread.shutdown_and_wait().await?;
|
||||
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
|
||||
let inputs = read_hook_inputs_from_log(
|
||||
@@ -1611,6 +1633,10 @@ async fn session_end_flushes_transcript_and_ignores_control_output() -> Result<(
|
||||
.as_path(),
|
||||
)?;
|
||||
assert_eq!(inputs.len(), 1);
|
||||
assert_eq!(
|
||||
inputs[0]["session_id"],
|
||||
test.session_configured.session_id.to_string()
|
||||
);
|
||||
assert_eq!(inputs[0]["hook_event_name"], "SessionEnd");
|
||||
assert_eq!(inputs[0]["reason"], "other");
|
||||
assert_eq!(inputs[0]["transcript_exists"], true);
|
||||
|
||||
Reference in New Issue
Block a user