#3045: [AI-Assisted] fix: preserve pending tasks when subagent completes
Cluster:
Subagent Task Management Fixes
## Summary
Fixes #3031
When a subagent completes and announces its result back to the main agent, the main agent's pending tasks were being lost due to a race condition between the followup queue drain and the announce flow.
## Changes
- **Add persistence layer** for followup queues (similar to subagent runs persistence pattern)
- **Implement 30-second grace period** before deleting empty queues to handle subagent announce race conditions
- **Persist queue state** after enqueue/dequeue operations
- **Track `emptyAt` timestamp** to prevent premature queue deletion
- **Add periodic cleanup** for expired empty queues
This ensures that when a subagent completes and triggers a new main agent invocation, any pending tasks in the followup queue are preserved and restored from disk.
## Files Changed
- `src/auto-reply/reply/queue/state.store.ts` (new) - Persistence layer for followup queues
- `src/auto-reply/reply/queue/state.ts` - Add persistence calls and restore logic
- `src/auto-reply/reply/queue/enqueue.ts` - Persist after enqueue operations
- `src/auto-reply/reply/queue/drain.ts` - Grace period logic and persist after drain
## Testing Status
⚠️ **Lightly tested**
- ✅ TypeScript compiles without errors
- ✅ Linter passes (oxlint)
- ⚠️ No integration tests yet for the specific race condition scenario
- ⚠️ Needs real-world validation with live moltbot instance
## AI-Assisted Development
🤖 This PR was created with assistance from Claude Sonnet 4.5
- Analyzed the codebase to identify root cause
- Designed fix following existing persistence patterns (subagent-registry.ts)
- Generated issue #3031 and implementation code
- Testing level: **lightly tested** (compiles + lints, needs integration testing)
## Checklist
- [x] Code follows existing patterns (persistence similar to `subagentRuns`)
- [x] Linter passes
- [x] TypeScript compiles without errors
- [ ] Integration tested (needs validation)
- [x] Marked as AI-assisted
- [x] References issue #3031
---
Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Adds on-disk persistence for followup queues to prevent pending tasks being lost when a subagent completion triggers a new main-agent invocation. The change introduces a `state.store.ts` JSON registry under `STATE_DIR`, restores queues on first access, persists after enqueue/dequeue/clear operations, and adds a 30s grace period plus periodic cleanup before deleting empty queues to avoid the announce/drain race described in #3031.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge, with a small risk of leaving empty queues around longer than intended due to a timestamp edge case.
- Changes are localized and follow existing persistence patterns, but there is at least one concrete logic issue (`emptyAt` cleared before confirming enqueue) that can defeat the cleanup mechanism, plus some extra steady-state disk I/O and lack of validation on restored JSON.
- src/auto-reply/reply/queue/enqueue.ts, src/auto-reply/reply/queue/drain.ts, src/auto-reply/reply/queue/state.store.ts
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#8471: fix(subagent): add defensive checks for undefined string fields
by adam-smeth · 2026-02-04
77.8%
#18205: fix (agents): add periodic retry timer for failed subagent announces
by MegaPhoenix92 · 2026-02-16
77.6%
#18468: fix(agents): prevent infinite retry loops in sub-agent completion a...
by BinHPdev · 2026-02-16
77.2%
#13105: fix: debounce subagent lifecycle events to prevent premature announ...
by mcaxtr · 2026-02-10
77.1%
#6690: UX: clearer /queue and /subagents output
by kamb5686 · 2026-02-01
76.7%
#21887: fix: drop stale announce-queue items after 5-minute TTL
by John-Rood · 2026-02-20
76.6%
#20328: fix(agents): Add retry with exponential backoff for subagent announ...
by tiny-ship-it · 2026-02-18
76.1%
#21679: fix: Mission Control dashboard issues for agent status, stale sessi...
by thejawdoc · 2026-02-20
76.1%
#23166: fix(agents): restore subagent announce chain from #22223
by tyler6204 · 2026-02-22
76.1%
#13540: feat: persist channel histories to disk
by carrotRakko · 2026-02-10
76.0%