Conversation
…-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>
3 tasks done
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #262
Summary
Core now owns recoverability:
withRecoverability(policy)replaceswithRetryStrategy, transports stop counting attempts, and every dead-lettered message carries abus-failureheader saying why and where it failed.Background
Each transport decided when a message ran out of retries (
maxRetriesinInMemoryQueueand RabbitMQ,maxReceiveCountin 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
failMessage()calledtransport.failstraight away, then the bus also deleted or returned the message. Confirmed on master with a throwaway RabbitMQ test: a handler that callsctx.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/addsRecoverabilityPolicy(({ error, message, attributes, failedAttempts }) => retry(delay) | deadLetter()),defaultRecoverability({ maxAttempts = 10, delay = exponentialBackoff(), unrecoverable = [] })(same delays asDefaultRetryStrategy),causedBy()(finds an error type throughHandlerDispatchRejected.rejections,AggregateErrorandcause, so unrecoverable errors inside workflow handlers work),MessageFailure,createMessageFailure()(truncates message to 1,000 and stack to 4,000 chars, unwraps a single-handlerHandlerDispatchRejected) andFAILURE_HEADER/toFailureHeader/fromFailureHeader.RetryStrategy,DefaultRetryStrategy,withRetryStrategyandCoreDependencies.retryStrategyare removed.Settled once:
BusInstance.settleMessageruns after the incoming chain and calls exactly one ofdeleteMessage,returnMessage(message, delay)orfail(message, failure).failMessage()/returnMessage()now only flag the lifecycle context.failMessagewins over a throw and skips the policy.returnMessagewithout a throw goes to the policy as aReturnMessageRequestedfailure (it counted towardmaxRetriesbefore too). A policy that throws, or returns anything butdeadLetter()orretry()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()orreturnMessage()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 ofBusInstance.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-failureis reserved by every transport.InMemoryQueue:seenCount→failedAttempts,maxRetriesremoved, reads hand out a copy carrying the count (the bus freezes the message).maxRetriesremoved. TTL bucket queues take core's delay, andfailcopies to the DLQ withbus-failureand then acks. The DLQ copy drops thefailedAttemptsheader, so a shovelled replay gets all its attempts again. Unparseable messages get abus-failuretoo.failedAttempts = ApproximateReceiveCount - 1(toFailedAttempts, exported), visibility timeout = core's delay,bus-failureis one SQS message attribute on the DLQ copy. ThemaxReceiveCountdefault goes from 10 to 15 as a crash-loop backstop. Existing queues get it atinitialize()through the attribute sync.Receivers apply the policy: retry →
transport.returnMessage(delay)and report failed; dead-letter →transport.failand report handled.BusSqsLambdaReceiversetsfailedAttemptsfrom the record's receive count.Checklist
RetryStrategy/maxRetriesremoved: confirmed & fixed.RetryStrategysince delay rabbitmq retries with the core retry strategy #285: already fixed on master (claim verified; onlymaxRetrieswas transport-owned).failMessage+ throw on RabbitMQ dead-letters and retries: confirmed & fixed (the repro above; regression tests inrabbitmq-transport.integration.tsandbus-instance-recoverability.integration.ts).transportTestsadditions (failedAttempts counts up, dead-letter atmaxAttempts, unrecoverable on first failure,bus-failureon every dead letter): done. In-memory, RabbitMQ and SQS all pass.default-recoverability.spec.ts,caused-by.spec.ts, docs snippet).guide/middleware#auditing-handled-messages.Docs:
/guide/recoverabilityreplaces/guide/retry-strategies(redirect stub added). Transport, middleware and custom-transport pages, the READMEs,MIGRATING.mdand the packageCLAUDE.mds are updated. Changeset isminorwith 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
flushOutboxafter 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 neededDocs: updated
docs/for user-facing changes, or none needed🤖 Generated with Claude Code