#13105: fix: debounce subagent lifecycle events to prevent premature announce with stale data
agents
size: M
trusted-contributor
experienced-contributor
Cluster:
Subagent Enhancements and Features
## Summary
Fixes #13011
When a subagent's first API call fails and the embedded runner retries, the lifecycle listener in subagent-registry.ts fires on the first attempt's agent_end event, triggering the announce flow prematurely with stale data. The parent receives (no output), runtime 0s, and tokens n/a despite the retry completing successfully.
**Root cause:** The lifecycle listener treated every per-attempt phase end event as run-level completion, immediately triggering beginSubagentCleanup + runSubagentAnnounceFlow. Between retry attempts, isEmbeddedPiRunActive returns false (the session handle is cleared between subscriptions), so the announce flow's existing safeguards could not detect the in-progress retry.
**Fix:** Debounce lifecycle end events by 3 seconds before triggering announce. If a retry start event arrives during the debounce window, the pending timer is canceled. Only after the final attempt settles (no new lifecycle events for 3s) does the announce fire with the correct data. Run-level error events still trigger immediately without debounce.
Also preserves the original startedAt from registration time across retry start events so total wall-clock runtime is reported correctly.
## Changes
- src/agents/subagent-registry.ts: Added debounce logic for lifecycle end events; extracted triggerCleanupAndAnnounce helper; preserve startedAt across retries
- src/agents/subagent-registry.lifecycle-debounce.test.ts: 6 new tests covering: no immediate announce on first end, debounced announce after settle, timer cancellation on retry start, startedAt preservation, immediate error handling, dedup on rapid events
## Test plan
- [x] All 6 new tests fail before the fix, pass after
- [x] All 19 existing tests in subagent-registry.persistence.test.ts and subagent-announce.format.test.ts pass
- [x] pnpm build succeeds
- [x] pnpm check (format + lint) succeeds
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adjusts `src/agents/subagent-registry.ts` to prevent premature subagent “announce” runs when embedded subagent executions retry: lifecycle `end` events are now debounced (3s) so intermediate attempt completions don’t trigger cleanup/announce with stale `endedAt/outcome`. A new helper (`triggerCleanupAndAnnounce`) centralizes the cleanup + announce flow, and pending debounce timers are canceled on retry `start`, on `releaseSubagentRun`, and during test resets. A new Vitest suite (`src/agents/subagent-registry.lifecycle-debounce.test.ts`) exercises the debounce behavior, timer cancellation on retry start, startedAt preservation, immediate handling for run-level `error`, and de-duping rapid end events.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- Changes are scoped to subagent lifecycle handling and add deterministic debounce/timer-cancellation logic to avoid premature announce on retries; new targeted tests cover the key edge cases and cleanup paths.
- No files require special attention
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#18468: fix(agents): prevent infinite retry loops in sub-agent completion a...
by BinHPdev · 2026-02-16
84.8%
#18205: fix (agents): add periodic retry timer for failed subagent announces
by MegaPhoenix92 · 2026-02-16
84.6%
#23166: fix(agents): restore subagent announce chain from #22223
by tyler6204 · 2026-02-22
82.5%
#22719: fix(agents): make subagent announce timeout configurable (restore 6...
by Valadon · 2026-02-21
82.1%
#7584: Tests: align subagent announce wait expectations
by justinhuangcode · 2026-02-03
82.0%
#8471: fix(subagent): add defensive checks for undefined string fields
by adam-smeth · 2026-02-04
80.9%
#17721: fix: abort child run on subagent timeout + retry with backoff + sta...
by IrriVisionTechnologies · 2026-02-16
80.3%
#17001: fix: retry sub-agent announcements with backoff instead of silently...
by luisecab · 2026-02-15
80.2%
#20328: fix(agents): Add retry with exponential backoff for subagent announ...
by tiny-ship-it · 2026-02-18
80.1%
#8893: fix: enhance subagent error reporting with diagnostic context
by joetomasone · 2026-02-04
79.5%