Wait for memory consolidation shutdown before finishing jobs (#36140)

## Why

A phase-two memory job could release its lease while its consolidation agent was
still shutting down, allowing another worker to race the existing agent.

## What changed

- Await consolidation-agent shutdown before validating artifacts and completing
  the job.
- Keep the lease until it expires if shutdown fails, and remove the agent from
  the thread manager only after shutdown succeeds.

## Testing

Update startup tests to wait for the phase-two job lifecycle to finish and verify
that the consolidation thread has been removed.

GitOrigin-RevId: 7ac3a7369529822ac95fd2ee9dcb27d39c191d20
This commit is contained in:
jif authored and copyberry committed 2026-07-30 11:52:38 +00:00
1 parent 9588f660be
commit 5ad367fb95
3 files changed
+41 -38

No files matched your search

+11 -12
View File
@@ -406,6 +406,17 @@ mod agent {
{
emit_token_usage_metrics(context.as_ref(), &token_usage);
}
if let Err(err) = context
.shutdown_consolidation_agent(SpawnedConsolidationAgent { thread_id, thread })
.await
{
warn!("failed to auto-close global memory consolidation agent {thread_id}: {err}");
// Keep the existing lease until it expires so another worker cannot race a
// consolidation agent whose shutdown has not completed.
return;
}
let artifacts_valid = if agent_completed {
match validate_consolidation_artifacts(&memory_root).await {
Ok(()) => true,
@@ -469,18 +480,6 @@ mod agent {
} else if !agent_completed {
job::failed(context.as_ref(), &db, &claim, "failed_agent").await;
}
let cleanup_context = Arc::clone(&context);
tokio::spawn(async move {
if let Err(err) = cleanup_context
.shutdown_consolidation_agent(SpawnedConsolidationAgent { thread_id, thread })
.await
{
warn!(
"failed to auto-close global memory consolidation agent {thread_id}: {err}"
);
}
});
});
}