Skip to content

feat(engine): persist-first webhook outbox for queue-backed adapters#178

Merged
willwashburn merged 9 commits into
mainfrom
feature/engine-outbox-for-queue-adapters
Jun 10, 2026
Merged

feat(engine): persist-first webhook outbox for queue-backed adapters#178
willwashburn merged 9 commits into
mainfrom
feature/engine-outbox-for-queue-adapters

Conversation

@willwashburn

Copy link
Copy Markdown
Member

Stacked on #175 (feature/node-durable-event-queue). This PR targets that branch and should be retargeted to main once #175 merges. Only the commits past #175's head are new here.

Problem

#175 made the Node self-host adapter durable, but the hosted (Cloudflare) deployment still has a loss window: the engine enqueues webhook events to the CF Queue inside waitUntil after the HTTP response. If the isolate dies between the D1 message insert and the queue send, the webhook event is lost forever — CF Queue retries + DLQ only cover post-enqueue failures.

Change

Move the pending_events outbox insert from the Node adapter into the engine send path, so the persist-first guarantee holds for every adapter:

  • routes/webhookOutbox.ts (new)sendWebhookEvent(c, event) inserts the outbox row synchronously in the request path (single cheap INSERT, awaited before the response), then hands { ...event, outboxId } to eventQueue.send in the background. If the insert itself fails, the route still responds and the event degrades to the legacy fire-and-forget send (no outboxId). All 26 route call sites now go through it.
  • Port contract (ports/event-queue.ts)QueuedEvent.outboxId?: string, backward compatible. Present: row is already durable, adapters must not double-insert, the consumer settles it. Absent: legacy semantics.
  • Node adapterDurableEventQueue.send skips the insert when outboxId is present; poller/lease semantics unchanged.
  • sweepPendingEvents(db, opts) (new) — claims due rows via the same atomic claimDueEvents the Node poller uses and returns them with complete/fail/reschedule settle callbacks, so a scheduled handler (CF cron) can RE-ENQUEUE them to an external queue. It does not deliver directly.
  • Public surfaceenqueueEvent, claimDueEvents, completeEvent, failEvent, rescheduleEvent, sweepPendingEvents, cleanupOldEvents, plus ClaimedEvent/SweptEvent types are exported from the package root so the cloud queue consumer / scheduled handler can import them.

The companion cloud PR (AgentWorkforce/cloud feature/relaycast-outbox-settlement) wires the CF queue consumer to settle rows and the scheduled handler to sweep + re-enqueue.

Tests

Extends the #175 suite (70 passing):

  • Engine send path inserts the row before eventQueue.send is invoked; row remains sweepable when the queue send throws synchronously or rejects asynchronously, and the mutation still returns 201.
  • Node adapter end-to-end still green; send with outboxId does not double-insert (exactly one delivery).
  • Sweep claims due rows exactly once under concurrent sweepers; lease prevents re-claim until expiry; settle callbacks verified.

npm test, npm run typecheck, npm run lint all green in packages/engine.

🤖 Generated with Claude Code

willwashburn and others added 5 commits June 9, 2026 16:00
The Node self-host adapter delivered webhooks fire-and-forget: an
in-process send with 3 inline retries, and any failure or restart lost
the event. The hosted Cloudflare path gets real durability from CF
Queues + DLQ; self-hosters got message loss. The pending_events table
existed in the schema but nothing consumed it.

The Node adapter's event queue now uses pending_events as a consumed
outbox:

- send persists the row first (durable once send resolves), then kicks
  an immediate poll so delivery stays prompt.
- A background poller (configurable interval) claims due rows with a
  single UPDATE ... WHERE id IN (subquery) RETURNING statement —
  atomic claim with attempts++ and a lease on process_after, per the
  no-interactive-transactions doctrine in ports/database.ts. A worker
  that crashes mid-delivery leaves the row reclaimable after the lease.
- Delivery reuses deliverEvent unchanged (HMAC signing, terminal-4xx
  vs retryable classification): success deletes the row, terminal
  failures settle it as failed, retryable failures reschedule with
  capped exponential backoff until max_attempts is exhausted.
- Startup resumes leftover due rows, so deliveries survive restarts.
- cleanupOldEvents (24h) is wired into the poll cadence so settled
  rows are pruned.

The EventQueue port contract and the Cloudflare path are unchanged;
InProcessEventQueue is renamed to DurableEventQueue (engine-internal,
no external importers).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Move the pending_events outbox insert from the Node adapter into the
engine send path so every adapter gets the same durability guarantee:
routes insert the row synchronously in the request path (single cheap
INSERT via routes/webhookOutbox.ts), then hand the row id to
eventQueue.send in the background. If the queue send is lost (Workers
isolate dies after the response, queue outage), the row stays pending
and is re-enqueued by the sweep instead of vanishing.

- QueuedEvent gains an optional outboxId; adapters that receive it must
  not insert a second row and the consumer settles the row after
  delivery. Absent outboxId keeps the legacy contract.
- DurableEventQueue.send skips the insert when outboxId is present
  (no double-insert / double-delivery on the Node path).
- New sweepPendingEvents(db, opts) claims due rows via the same atomic
  claimDueEvents the Node poller uses and returns them with
  complete/fail/reschedule settle callbacks so a scheduled handler can
  re-enqueue to an external queue without delivering directly.
- Export the outbox primitives (enqueueEvent, claimDueEvents,
  completeEvent, failEvent, rescheduleEvent, sweepPendingEvents,
  cleanupOldEvents) from the engine package root for queue consumers.

Tests: route-level persist-first ordering incl. sync/async queue-send
failures leaving the row sweepable; Node adapter no-double-insert;
sweep exactly-once claims under concurrent sweepers, lease expiry, and
settle callbacks.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@codeant-ai

codeant-ai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Your free trial PR review limit of 300 PRs has been reached. Please upgrade your plan to continue using CodeAnt AI.

@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@agent-relay-code[bot], we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 19 minutes and 21 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5ff0f4c1-1321-447b-ab86-457af8646c8c

📥 Commits

Reviewing files that changed from the base of the PR and between 42b4382 and 3b4e20c.

📒 Files selected for processing (21)
  • memory/workspace/.relay/state.json
  • packages/engine/src/adapters/node/__tests__/event-queue.test.ts
  • packages/engine/src/adapters/node/event-queue.ts
  • packages/engine/src/engine/__tests__/eventQueue.test.ts
  • packages/engine/src/engine/eventQueue.ts
  • packages/engine/src/index.ts
  • packages/engine/src/middleware/idempotency.ts
  • packages/engine/src/ports/event-queue.ts
  • packages/engine/src/routes/__tests__/webhookOutbox.test.ts
  • packages/engine/src/routes/action.ts
  • packages/engine/src/routes/agent.ts
  • packages/engine/src/routes/channel.ts
  • packages/engine/src/routes/dm.ts
  • packages/engine/src/routes/file.ts
  • packages/engine/src/routes/groupDm.ts
  • packages/engine/src/routes/inboundWebhook.ts
  • packages/engine/src/routes/message.ts
  • packages/engine/src/routes/reaction.ts
  • packages/engine/src/routes/receipt.ts
  • packages/engine/src/routes/thread.ts
  • packages/engine/src/routes/webhookOutbox.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/engine-outbox-for-queue-adapters

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codeant-ai

codeant-ai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Your free trial PR review limit of 300 PRs has been reached. Please upgrade your plan to continue using CodeAnt AI.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2fdff64ae3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/engine/src/routes/message.ts Outdated
@codeant-ai

codeant-ai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Your free trial PR review limit of 300 PRs has been reached. Please upgrade your plan to continue using CodeAnt AI.

Base automatically changed from feature/node-durable-event-queue to main June 10, 2026 14:26
willwashburn and others added 2 commits June 10, 2026 10:33
Resolve webhook outbox conflicts and move idempotent webhook outbox insertion before success record storage.
@willwashburn willwashburn merged commit 77f049c into main Jun 10, 2026
5 checks passed
@willwashburn willwashburn deleted the feature/engine-outbox-for-queue-adapters branch June 10, 2026 14:40
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.

1 participant