Restore V2 agent identities on root thread resume (#32837)

## Why

After a cold resume, a Multi-Agent V2 root thread must still be able to
address its persisted descendants even though their runtimes are not reopened
eagerly.

## What changed

- Restore metadata for open descendant agents when resuming a V2 root thread.
- Preserve each descendant's path, nickname, and role while leaving its runtime
  unloaded until a message targets it.
- Reuse the spawn metadata preparation path so restored identities retain the
  same reservation and naming checks as newly spawned agents.

## Testing

Added an end-to-end cold-resume test that follows up with a persisted agent by
name and verifies that the original agent thread is loaded on demand.

GitOrigin-RevId: a1663c1ae60eefadb948cd39f933cd3bbb49a492
This commit is contained in:
jif authored and copyberry committed 2026-07-13 17:24:10 +00:00
1 parent 19a000259d
commit 088239294a
5 files changed
+363 -19

No files matched your search

+71
View File
@@ -95,6 +95,77 @@ fn is_multi_agent_v2_usage_hint_message(item: &ResponseItem, usage_hint_texts: &
}
impl AgentControl {
/// Restore persisted V2 agent identities without reopening their runtimes.
pub(crate) async fn restore_v2_agent_metadata(
&self,
config: &Config,
root_thread_id: ThreadId,
) {
self.state.register_root_thread(root_thread_id);
let Ok(state) = self.upgrade() else {
return;
};
let Some(agent_graph_store) = state.agent_graph_store() else {
return;
};
let descendant_ids = match agent_graph_store
.list_thread_spawn_descendants(
root_thread_id,
Some(codex_agent_graph_store::ThreadSpawnEdgeStatus::Open),
)
.await
{
Ok(descendant_ids) => descendant_ids,
Err(err) => {
warn!("failed to restore persisted V2 agent metadata for {root_thread_id}: {err}");
return;
}
};
for thread_id in descendant_ids {
if self.state.agent_metadata_for_thread(thread_id).is_some() {
continue;
}
let restore_result = async {
let stored_thread = state
.read_stored_thread(ReadThreadParams {
thread_id,
include_archived: true,
include_history: false,
})
.await?;
let stored_agent_path = stored_thread
.agent_path
.as_deref()
.map(AgentPath::try_from)
.transpose()
.map_err(|err| {
CodexErr::InvalidRequest(format!("invalid stored agent path: {err}"))
})?;
let mut reservation = self.state.reserve_spawn_slot(/*max_threads*/ None)?;
let mut metadata = self.prepare_agent_metadata(
&mut reservation,
config,
stored_agent_path.or_else(|| stored_thread.source.get_agent_path()),
stored_thread
.agent_role
.or_else(|| stored_thread.source.get_agent_role()),
stored_thread
.agent_nickname
.or_else(|| stored_thread.source.get_nickname()),
)?;
metadata.agent_id = Some(thread_id);
reservation.commit(metadata);
Ok::<(), CodexErr>(())
}
.await;
if let Err(err) = restore_result {
warn!("failed to restore V2 agent metadata for {thread_id}: {err}");
}
}
}
/// Spawn a new agent thread and submit the initial prompt.
#[cfg(test)]
pub(crate) async fn spawn_agent(