Skip to content

fix(gateway): preserve queued messages at recursion cap - #93881

Closed
JoaoMarcos44 wants to merge 1 commit into
NousResearch:mainfrom
JoaoMarcos44:fix/issue-93871-gateway-recursion-queue
Closed

JoaoMarcos44 wants to merge 1 commit into
NousResearch:mainfrom
JoaoMarcos44:fix/issue-93871-gateway-recursion-queue

Conversation

@JoaoMarcos44

@JoaoMarcos44 JoaoMarcos44 commented Aug 24, 2026 •

Copy link
Copy Markdown

Summary

  • Prevents overflow promotion when the interrupt recursion cap will defer the current pending event.
  • Requeues the pending event through the FIFO path instead of the single-slot overwrite helper.
  • Preserves normal promotion when recursion is allowed.
  • Adds a regression proving all queued events survive in order.

Problem and impact

The gateway queue uses one adapter slot for the head and SessionState.conversation.queued_events for overflow. At the recursion ceiling, the drain path could lose the next queued message permanently while later messages still ran, creating both data loss and order inversion.

Root cause

The drain popped the overflow head into the adapter slot before checking _MAX_INTERRUPT_DEPTH. The cap branch then called merge_pending_message_event(..., merge_text=False), whose text-only fallback assigns into the slot and overwrites the promoted event.

Implementation decision

When the recursion cap will defer the current event, promotion is deferred too. The current event is then placed back through _enqueue_fifo, leaving the overflow tail untouched. This preserves the existing normal path and avoids merging separate user turns.

Scope and compatibility

Files

  • gateway/run.py
  • tests/gateway/test_queue_consumption.py

Verification

  • Local: scripts/run_tests.sh tests/gateway/test_queue_consumption.py -q → 6 passed.
  • Local ruff check: passed.
  • Local git diff --check: passed.
  • Remote required checks: passed at last verification.
  • The real pre-fix reproduction lost msg2 from [msg1, msg2, msg3, msg4]; the regression now preserves the full FIFO chain.

Fixes #93871

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages duplicate This issue or pull request already exists labels Aug 24, 2026
@alt-glitch

Copy link
Copy Markdown

This was generated by AI during triage.

Duplicate of #78015: both repair the recursion-cap promotion path that overwrites a promoted FIFO event and loses a queued turn.

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

Labels

comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gateway recursion cap can drop queued follow-up messages

2 participants