Registry Mirror
Skip to content

Conversation

@adenhertog

@adenhertog adenhertog commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Closes #262

Summary

Core now owns recoverability: withRecoverability(policy) replaces withRetryStrategy, transports stop counting attempts, and every dead-lettered message carries a bus-failure header saying why and where it failed.

Background

Each transport decided when a message ran out of retries (maxRetries in InMemoryQueue and RabbitMQ, maxReceiveCount in SQS), and dead-lettered messages carried no failure information. The issue body is the approved spec; the replay API is cut to #270 and the audit queue is a docs recipe.

Problem

  • The attempt limit lived in three places with three meanings, and SQS's redrive policy dead-lettered messages where core couldn't attach anything.
  • No failure metadata on dead-lettered messages, and no way to dead-letter errors that retrying can't fix.
  • failMessage() called transport.fail straight away, then the bus also deleted or returned the message. Confirmed on master with a throwaway RabbitMQ test: a handler that calls ctx.failMessage() and then throws was handled 6 times and left 6 copies in the DLQ in 3 s (one per retry). returnMessage() followed by a throw also returned the message twice, so it came back as two copies.

Approach

  • bus-core: recoverability/ adds RecoverabilityPolicy (({ error, message, attributes, failedAttempts }) => retry(delay) | deadLetter()), defaultRecoverability({ maxAttempts = 10, delay = exponentialBackoff(), unrecoverable = [] }) (same delays as DefaultRetryStrategy), causedBy() (finds an error type through HandlerDispatchRejected.rejections, AggregateError and cause, so unrecoverable errors inside workflow handlers work), MessageFailure, createMessageFailure() (truncates message to 1,000 and stack to 4,000 chars, unwraps a single-handler HandlerDispatchRejected) and FAILURE_HEADER/toFailureHeader/fromFailureHeader. RetryStrategy, DefaultRetryStrategy, withRetryStrategy and CoreDependencies.retryStrategy are removed.

  • Settled once: BusInstance.settleMessage runs after the incoming chain and calls exactly one of deleteMessage, returnMessage(message, delay) or fail(message, failure). failMessage()/returnMessage() now only flag the lifecycle context. failMessage wins over a throw and skips the policy. returnMessage without a throw goes to the policy as a ReturnMessageRequested failure (it counted toward maxRetries before too). A policy that throws, or returns anything but deadLetter() or retry() with a finite delay ≥ 0 (undefined, a promise, NaN), is logged and the message is dead-lettered rather than hot-looping; retry() clamps its delay to [0, MAX_RETRY_DELAY].

  • Outbox (maintainer decision): a handler that resolves after failMessage() or returnMessage() was called counts as failed. Its outbox is discarded and a workflow handler's returned state isn't saved. Sibling handlers that already resolved have flushed (documented, until Durable transactional outbox #260). The discard is the first step of BusInstance.flushOutbox, before anything is dispatched, so add delayed delivery with deliverAfter and deliverAt #328 can add its immediate/delayed split after it.

  • Transport contract (breaking): TransportMessage.failedAttempts, returnMessage(message, delay) (never dead-letters), fail(message, failure) (must also remove the message; the bus no longer deletes after it). bus-failure is reserved by every transport.

    • InMemoryQueue: seenCount → failedAttempts, maxRetries removed, reads hand out a copy carrying the count (the bus freezes the message).
    • bus-rabbitmq: maxRetries removed. TTL bucket queues take core's delay, and fail copies to the DLQ with bus-failure and then acks. The DLQ copy drops the failedAttempts header, so a shovelled replay gets all its attempts again. Unparseable messages get a bus-failure too.
    • bus-sqs: failedAttempts = ApproximateReceiveCount - 1 (toFailedAttempts, exported), visibility timeout = core's delay, bus-failure is one SQS message attribute on the DLQ copy. The maxReceiveCount default goes from 10 to 15 as a crash-loop backstop. Existing queues get it at initialize() through the attribute sync.
  • Receivers apply the policy: retry → transport.returnMessage(delay) and report failed; dead-letter → transport.fail and report handled. BusSqsLambdaReceiver sets failedAttempts from the record's receive count.

  • Checklist

    • core owns the policy, RetryStrategy/maxRetries removed: confirmed & fixed.
    • RabbitMQ already used core's RetryStrategy since delay rabbitmq retries with the core retry strategy #285: already fixed on master (claim verified; only maxRetries was transport-owned).
    • failMessage + throw on RabbitMQ dead-letters and retries: confirmed & fixed (the repro above; regression tests in rabbitmq-transport.integration.ts and bus-instance-recoverability.integration.ts).
    • transportTests additions (failedAttempts counts up, dead-letter at maxAttempts, unrecoverable on first failure, bus-failure on every dead letter): done. In-memory, RabbitMQ and SQS all pass.
    • custom policy unit tested as a plain function: done (default-recoverability.spec.ts, caused-by.spec.ts, docs snippet).
    • Lambda receiver applies the policy: done (unit + LocalStack integration).
    • replay API/CLI: deferred to Monitoring dashboard: failed messages, endpoint health and workflow inspection #270 (docs point to SQS redrive and RabbitMQ shovels). Audit queue: docs recipe in guide/middleware#auditing-handled-messages.
  • Docs: /guide/recoverability replaces /guide/retry-strategies (redirect stub added). Transport, middleware and custom-transport pages, the READMEs, MIGRATING.md and the package CLAUDE.mds are updated. Changeset is minor with Breaking: notes.

  • Tests: pnpm build, lint, format, docs:typecheck/docs:build/docs:check-redirects/docs:check-readmes, check:message-types, check:packages, and the full jest run (64 suites, 956 tests, including RabbitMQ, LocalStack SQS/Lambda, Postgres and MongoDB integration).

  • Overlap: this PR merges before add delayed delivery with deliverAfter and deliverAt #328 (delayed delivery), which adapts to it. Its delayed store slots into flushOutbox after the discard check.

  • This is original work under the clean-room policy, not ported, translated or copied from another messaging framework

  • Added a changeset (pnpm changeset) for user-facing changes to published packages, or none is needed

  • Docs: updated docs/ for user-facing changes, or none needed

🤖 Generated with Claude Code

adenhertog and others added 2 commits October 3, 2026 13:30
…-lettered messages

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…date policy results

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@adenhertog
adenhertog merged commit 5e28d0d into master Oct 3, 2026
5 checks passed
@adenhertog
adenhertog deleted the issue-262-recoverability branch October 3, 2026 10:08
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.

Configurable recoverability policy and error queue with failure metadata

1 participant