Skip to content

fix: defer early session.idle instead of dropping it (#1517)#1537

Open
code-yeongyu wants to merge 1 commit intodevfrom
fix/background-task-early-idle-hang
Open

fix: defer early session.idle instead of dropping it (#1517)#1537
code-yeongyu wants to merge 1 commit intodevfrom
fix/background-task-early-idle-hang

Conversation

@code-yeongyu
Copy link
Owner

@code-yeongyu code-yeongyu commented Feb 6, 2026

Summary

Fixes #1517 — Background tasks launched via delegate_task(run_in_background=true) permanently hang when the agent responds in under 5 seconds.

Root Cause

The session.idle handler in BackgroundManager has a guard that rejects idle events that fire within MIN_IDLE_TIME_MS (5s) of task start. Since session.idle is edge-triggered (fires once, not periodically), dropping the early event means it never fires again → task stuck forever.

Fix

Instead of dropping the early idle event, defer it using setTimeout(remainingMs). When the deferred timer fires, it re-checks whether the task is still running before proceeding with completion validation. This preserves the original intent (don't complete prematurely) while not losing the idle signal.

Changes

  • src/features/background-agent/manager.ts: Replace early idle return with setTimeout deferral + cleanup logic
  • src/features/background-agent/manager.test.ts: Add TDD tests for early idle deferral behavior

Testing

  • ✅ New tests for early idle deferral (3 cases)
  • ✅ All existing tests pass (no regression)
  • bun run typecheck passes
  • bun run build passes

Summary by cubic

Prevents background tasks from hanging when session.idle fires within 5s by deferring the event instead of dropping it. Keeps the minimum idle window while ensuring the idle signal is not lost.

  • Bug Fixes
    • Defer early session.idle with a timer, then re-check task state and re-handle the event.
    • Track and clear deferred idle timers on completion, abort/remove, and shutdown to avoid leaks.
    • Add tests for early deferral, after-threshold handling, and no-op when the task completes before the deferred idle fires.

Written for commit 796bf79. Summary will update on new commits.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Fast-Completing Background Tasks Get Stuck

1 participant