Drain unified exec launch failure events before session shutdown (#64346)

## Why

A launch failure can still be publishing its command events after the turn
is interrupted. If the session shuts down during publication, the failed
command can be lost from persisted thread history.

## What changed

Track in-flight launch failure publications and wait for them to finish
before running session-end hooks and emitting the thread-stop event.
Once shutdown closes the tracker, skip new launch failure publications.

## Testing

Add a regression test that pauses publication for a command with a missing
working directory, interrupts the turn, and requests shutdown. It checks
that shutdown waits and that persisted history records the command as failed
in the interrupted turn.

GitOrigin-RevId: 1fb33d1fd3c11939c0013f5feb4edb737702b0ad
This commit is contained in:
jif
2026-11-27 15:57:38 +00:00
committed by copyberry
parent 1a1f005766
commit c8cb4e36ed
4 changed files with 105 additions and 1 deletions
+4
View File
@@ -466,6 +466,10 @@ pub(super) async fn shutdown_session_runtime(sess: &Arc<Session>) {
sess.drain_code_mode_messages().await;
let writes = &sess.services.unified_exec_manager.launch_failure_writes;
writes.close();
writes.wait().await;
crate::hook_runtime::run_session_end_hooks(sess).await;
emit_thread_stop_lifecycle(sess).await;
}
@@ -1,6 +1,6 @@
//! Reports terminal process-creation failures as command attempts without a process handle.
//! Approval rejections retain their own lifecycle, and sandbox denials remain retryable.
//! Once started, failure event publication survives caller cancellation.
//! Session shutdown drains failure publications that outlive the caller.
use crate::tools::events::ToolEmitter;
use crate::tools::events::ToolEventCtx;
@@ -42,6 +42,16 @@ pub(super) async fn with_launch_failure_events(
.plugin_attribution_for_command(&req.resolved.command, &cwd)
})
};
// Register before checking closure so shutdown waits for every admitted write.
let writes = &ctx
.session
.services
.unified_exec_manager
.launch_failure_writes;
let write = writes.token();
if writes.is_closed() {
return result;
}
let emitter = ToolEmitter::unified_exec(
&req.resolved.command,
req.cwd.clone(),
@@ -71,6 +81,7 @@ pub(super) async fn with_launch_failure_events(
ToolEventStage::Failure(ToolEventFailure::Message(message)),
)
.await;
drop(write);
};
if let Err(err) = tokio::spawn(publish.in_current_span()).await {
tracing::warn!(%err, "failed to publish unified exec launch failure");
+3
View File
@@ -35,6 +35,7 @@ use rand::Rng;
use rand::rng;
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;
use tokio_util::task::TaskTracker;
use crate::session::session::Session;
use crate::session::step_context::StepContext;
@@ -158,6 +159,7 @@ impl ProcessStore {
}
pub(crate) struct UnifiedExecProcessManager {
pub(crate) launch_failure_writes: TaskTracker,
process_store: Mutex<ProcessStore>,
max_write_stdin_yield_time_ms: u64,
}
@@ -165,6 +167,7 @@ pub(crate) struct UnifiedExecProcessManager {
impl UnifiedExecProcessManager {
pub(crate) fn new(max_write_stdin_yield_time_ms: u64) -> Self {
Self {
launch_failure_writes: TaskTracker::new(),
process_store: Mutex::new(ProcessStore::default()),
max_write_stdin_yield_time_ms: max_write_stdin_yield_time_ms
.max(MIN_EMPTY_YIELD_TIME_MS),