fix(cron): wait for spawned subagents before marking cron job complete - #4304
Closed
michaelxer wants to merge 0 commit into
Closed
fix(cron): wait for spawned subagents before marking cron job complete#4304michaelxer wants to merge 0 commit into
michaelxer wants to merge 0 commit into
Conversation
michaelxer
force-pushed
the
michaelxer/cron-await-subagent-4290
branch
from
June 11, 2026 20:07
5f396c7 to
99013ab
Compare
This was referenced Jun 12, 2026
michaelxer
force-pushed
the
michaelxer/cron-await-subagent-4290
branch
from
June 12, 2026 16:44
99013ab to
7ff8e02
Compare
This was referenced Jun 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Cron jobs that spawn subagents via the spawn tool are marked as completed as soon as the agent's main turn finishes (
process_directreturns), while the subagent'sasyncio.Taskis still running in the background.From the logs in #4290:
The cron service records the run as "ok" and advances to the next scheduled time before the subagent has produced any output. If the job is one-shot (
kind: "at") it may even be deleted before the subagent finishes.Fixes #4290
Fix
SubagentManageralready tracks per-session background tasks in_session_tasks. This PR adds:SubagentManager.await_by_session(session_key)-- waits for all running tasks spawned under that session key to finish (mirrors the existingcancel_by_session).Call in
on_cron_job-- afteragent.process_direct()returns, the cron handler now callsawait agent.subagents.await_by_session(f"cron:{job.id}")before evaluating the response or returning. The cron service therefore holds the job open until every background subagent completes.This does not change the spawn tool's behaviour for interactive sessions -- only the cron path awaits.
Testing
Four new tests in
tests/agent/test_subagent_lifecycle.py(TestAwaitBySession):test_waits_for_running_taskstest_no_tasks_returns_immediatelytest_already_done_returns_immediatelytest_waits_for_multiple_tasksFull suite: 122 passed (subagent + cron + lifecycle tests).
Limitations
This fix ensures the cron job waits for subagent completion. It does not yet feed the subagent's result back into the agent's cron turn as additional context -- that would require a multi-turn cron execution model and is a separate enhancement.