Skip to content

fix(task-execution): prevent double wake nudge when continuation already queued - #701

Closed
dcramer wants to merge 3 commits into
mainfrom
fix/conversation-work-double-enqueue
Closed

dcramer wants to merge 3 commits into
mainfrom
fix/conversation-work-double-enqueue

Conversation

@dcramer

@dcramer dcramer commented Jul 1, 2026 •

Copy link
Copy Markdown
Member

What

completeConversationWork returns "pending" whenever execution.status === "awaiting_resume", which causes processConversationWork to enqueue an extra wake nudge. The problem: scheduleAgentContinue already enqueues its own wake (via requestConversationWork → queue.send → markConversationWorkEnqueued), making the extra nudge a duplicate. Because the queue idempotency key includes nowMs, it is never deduplicated.

When continuations repeatedly fail and reschedule (e.g. the continuation lock is consistently busy), each cycle adds one more message. This caused a 29-hour runaway loop on Jun 29–30 2026 where conversation slack:C0B595QDZLL:1782764912.916989 generated ~2M /api/internal/agent/continue requests at ~22 req/sec.

How

In completeConversationWork, check lastEnqueuedAtMs before returning "pending":

  • lastEnqueuedAtMs !== undefined → an accepted wake was recorded during this lease via markConversationWorkEnqueued. Skip the extra nudge (return "completed"), but still set status: "pending" in durable state so the already-queued message finds work when it arrives. Accepted wakes are conversation-level and can handle both pending mailbox messages and continuation resumes.
  • lastEnqueuedAtMs === undefined → queue.send may have failed before markConversationWorkEnqueued ran. Send a recovery nudge as before.

The fix uses runnable && !hasQueuedWake (stronger than hasPending || (needsRun && !hasQueuedWake)) so that any accepted wake suppresses the extra nudge regardless of whether pending mailbox messages also exist at completion time. Treating hasPending as an unconditional nudge reason would leave a remaining amplification path when all three conditions coexist.

The key invariant: startConversationWork clears lastEnqueuedAtMs when acquiring a lease, so any non-undefined value seen in completeConversationWork was set during the current worker's run.

All scheduleAgentContinue call sites are await-ed; no fire-and-forget race.

Tests

Three new cases in conversation-work.test.ts:

  1. Full scheduleAgentContinue sequence (requestConversationWork + markConversationWorkEnqueued): asserts result is "completed", no extra queue message sent, durable status is "pending", lastEnqueuedAtMs preserved.
  2. queue.send failure (only requestConversationWork, no markConversationWorkEnqueued): asserts result is "pending_requeued", recovery nudge sent.
  3. Adversarial: pending mailbox + continuation + recorded wake coexist: asserts no extra nudge despite hasPending=true, needsRun=true, hasQueuedWake=true.

The existing "requeues work requested while a lease is running" test continues to pass unchanged (calls requestConversationWork without markConversationWorkEnqueued, recovery path fires).

What this does NOT fix

The underlying cause of why a specific continuation keeps failing indefinitely (e.g. permanent lock contention, tool errors on every retry). A separate circuit-breaker (max sliceId for timeout continuations) is worth considering as a follow-up operational guardrail, but is intentionally left out of this PR to keep the scope narrow.

dcramer and others added 2 commits July 1, 2026 00:11
…ady queued

completeConversationWork returns "pending" when conversation status is
"awaiting_resume", which causes processConversationWork to enqueue an
extra wake nudge. scheduleAgentContinue already enqueues its own wake
(requestConversationWork + queue.send + markConversationWorkEnqueued),
so the extra nudge is a duplicate. When continuations repeatedly fail
and reschedule, these duplicates amplify into a runaway queue loop.

Fix: skip the extra nudge when markConversationWorkEnqueued recorded an
accepted wake during the current lease (lastEnqueuedAtMs !== undefined).
Preserve the recovery nudge when queue.send may have failed before
markConversationWorkEnqueued ran (lastEnqueuedAtMs === undefined).

Fixes the loop observed Jun 29-30 2026 where conversation
slack:C0B595QDZLL:1782764912.916989 generated ~2M requests over 29h.

Co-Authored-By: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Co-Authored-By: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@vercel

vercel Bot commented Jul 1, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
junior-docs Ready Ready Preview, Comment Jul 1, 2026 12:24am

Request Review

…orded

Use runnable && !hasQueuedWake instead of hasPending || (needsRun &&
!hasQueuedWake). Any accepted conversation wake is conversation-level
and can process both pending mailbox messages and continuation resumes,
so the hasPending exception is unnecessary and creates a remaining
amplification path when hasPending is true alongside needsRun and a
recorded wake.

Add adversarial test: pending mailbox + continuation + recorded wake
all coexist at completion time — asserts no extra nudge is sent.

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>

Co-Authored-By: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
@dcramer

dcramer commented Jul 1, 2026

Copy link
Copy Markdown
Member Author

Closing in favor of merging the fix directly. The structural refactor is tracked in #705.

@dcramer dcramer closed this Jul 1, 2026

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 99b79caf Deployed Jul 1, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant