Repository navigation
[EPIC-Y][LOU-Y4.2] Background sub-agents are cancelled or awaited when the lead run ends - #92
Merged
Merged
Conversation
…n the lead run ends
Add ExecuteOptions.onRunEnd, called exactly once per execute()/stream()
run in a finally (any finishReason, or the error). The subagent runtime
wraps it: background tasks still queued or running when the run ends are
cancelled (or awaited with awaitBackgroundOnFinish), the run resolves once
their runs have stopped, and result.backgroundTasks reports them.
createAgent({ subagentOptions }) forwards maxConcurrent and
awaitBackgroundOnFinish without mutating the caller's subagents value.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016bY29ibQgv3MDdjAbUT4YR
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.
What
ExecuteOptions.onRunEnd?: ({ result } | { error }) => void | Promise<void>: a small, generic run-lifecycle hook.AgentExecutorcalls it exactly once perexecute()/stream()run, in afinallyaroundwithExtensions()+runAgentLoop(). That covers everyfinishReason(aborted,awaiting-approval,max-steps, ...), a rejected run, and a sub-agent catalog that fails before the loop starts. The run settles after the hook returns; if the hook throws, the run rejects.AgentExecutor.tshas about 20 lines of code changes.resume.tsandtoolCallExecution.tsare untouched.withSubagents()takes the run'sonRunEndand wraps it with the background-task end, whichBackgroundTasks.finish()handles:onEventafter the run resolves.awaitBackgroundOnFinish: true: the run waits for those tasks before it resolves. An aborted or failed lead run still cancels them.result.backgroundTaskslists every background task of the run, in theagent_statusshape ({ taskId, agent, status, elapsedMs, error?, approvalId?, toolName? }). The field is absent when the run started none. The user's ownonRunEndruns after this, so it sees the field.SubagentOptions.awaitBackgroundOnFinishis new.createAgent({ subagentOptions: { maxConcurrent, awaitBackgroundOnFinish } })is accepted and forwarded: the option type plus the onerunOptions.subagentsline increateAgent.ts. It usessubagentsWithOptions(), which copies thesubagentsrecord or catalog and puts the new options over any already attached withwithSubagentOptions(). The caller's value is not mutated, so it can be shared between agents.awaitBackgroundOnFinish). The resumed run is a separateexecute(), possibly in another process. Without touchingresume.tsit has no link to the paused run's tasks, so keeping them alive could not be done simply. This is documented.Why
LOU-Y4 (#91) left background children running after the lead run finished, with their events still reaching
onEvent. The executor had no run-end hook that the sub-agent runtime could use. This completes LOU-Y4.Acceptance criteria
ExecuteOptions.onRunEnd), called exactly once perexecute()/stream()run in afinally, including on abort, error,'awaiting-approval'and'max-steps'. TheAgentExecutor.tschange is small, andresume.ts/toolCallExecution.tsare untouched.result.backgroundTasks: [{ taskId, agent, status, ... }]reports them (documented).withSubagentOptions({ awaitBackgroundOnFinish: true })waits for them before the run resolves and includes their final statuses.resume.ts); documented.createAgent({ subagentOptions: { maxConcurrent, awaitBackgroundOnFinish } })is accepted and forwarded (options type + the onesubagentsline increateAgent.ts).awaitBackgroundOnFinishwaits and reports; the hook runs once on abort, error, max-steps, awaiting-approval and stream;createAgent({ subagentOptions })forwards and does not mutatesubagents; lead failure and lead approval pause cancel children.How verified
npx tsc --noEmit: no errorsnpm run lint: 0 errors, 405 warnings. The changed files add no warnings: the only one in them is the existinganyinExecutionEvent.npm run test:types: Type Errors no errorsnpx vitest run: Test Files 145 passed (145), Tests 2102 passed | 3 skipped (2105)npm run build && npm run build --workspace=packages/create-loushy-agent: successnpm run docs:verify-snippets -- --skip-build: all 108 snippet(s) type-check against src/; 6 also run cleanlynpm run docs:llms:check: passnpm run test:coverage: 145 files / 2102 tests passed, exit 0npm run fallow: exit 0. Dead code: no issues; 0 above the complexity threshold. The one clone group it lists is unrelated to this PR:AnthropicProvider.ts/OpenAIProvider.ts.🤖 Generated with Claude Code
https://claude.ai/code/session_016bY29ibQgv3MDdjAbUT4YR
Generated by Claude Code