← Back to PRs

#3045: [AI-Assisted] fix: preserve pending tasks when subagent completes

by sid1943 open 2026-01-28 00:33 View on GitHub →
## 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