Preserve active recovery turns when clients reconnect (#61014)

## Why

A recovery turn can occupy the core turn slot before `TurnStarted` is published and thread statuses reflect it. During this window, `thread/resume` with configuration overrides could treat an unsubscribed thread as idle and shut it down, interrupting recovery.

## What changed

Require the core turn slot to be empty before replacing a loaded thread during resume. Expose `CodexThread::has_active_turn()` and reuse it in the residency unload check.

## Testing

Add a regression test that blocks paginated history writes to hold a recovery turn before status publication, reconnects with a configuration override, and verifies that the client receives completion for the preserved recovery turn.

GitOrigin-RevId: f295038c8502a9b2849ddc08614db4a3cb5610cf
This commit is contained in:
Anton Panasenko
2026-11-12 04:17:58 +00:00
committed by copyberry
parent 6bfdc75f0d
commit b75ec48a2c
4 changed files with 186 additions and 21 deletions
@@ -4766,12 +4766,14 @@ impl ThreadRequestProcessor {
matches!(existing_thread.agent_status().await, AgentStatus::Running);
// Parent-owned V2 children must not be rebuilt from public resume overrides.
// A recovery turn can already own the core slot before either status changes.
if can_accept_direct_input(
existing_thread.multi_agent_version(),
&config_snapshot.session_source,
) && !has_subscribers
&& matches!(loaded_status, ThreadStatus::Idle)
&& !is_running
&& !existing_thread.has_active_turn().await
{
// A loaded idle thread is only a cache entry. Shut it down
// before removing it so cold resume cannot duplicate a