Registry Mirror
Skip to content

Conversation

@adenhertog

@adenhertog adenhertog commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Closes #331

Summary

Adds ctx.reply(), which sends a reply straight to the queue that sent the message being handled. Every message from a bus that receives messages now carries a return address in a new replyTo attribute.

Background

The request/reply guide (#330) had repliers publish the reply as an event and route it back through the sticky workflowId. That fans the reply out to every subscriber, mixes up replies when several services ask the same service, needs a topic subscription, and drops replies sent from a workflow handler, because the handler swaps in its own workflowId. The maintainer's decisions on the issue: stamp a return address on every send and publish, allow any message as a reply, send replies point-to-point, and on Lambda use the source queue name when it's known.

Problem

There was no reply primitive, no return address on messages, and no transport call that sends straight to a named queue.

Approach

  • Return address: prepareTransportOptions sets replyTo to transport.endpointName unless the bus is send-only (it has no queue that's read) or the endpoint name is empty. A caller can pass replyTo, or replyTo: undefined to leave it out. On Lambda the transport's endpointName is the SqsTransport's queueName, which must already be the source queue for retries and dead-lettering. A transport without one stamps nothing, so reply() throws ReturnAddressMissing (documented in the Lambda page).
  • HandlerContext.reply(message, attributes?): available in function handlers, class handlers, both workflow styles (WorkflowContext, workflowContext()) and incoming/handler middleware contexts. It inherits correlation and sticky attributes from the request's TransportMessage as it arrived, not from the handling context, so a workflow replier sends the requester's workflowId. The workflow-replies-to-workflow test in bus-instance-reply.integration.ts covers both workflow styles. I checked it fails (2 tests) when the reply takes its sticky attributes from the handling context, which is how publish behaves.
  • Outbox and middleware: a reply goes through dispatchOutgoing with a new OutgoingReplyContext (kind: 'reply', readonly destination). It's buffered and flushed like any send, and dropped when the handler fails (tested).
  • Errors: ReplyOutsideHandlingContext, ReturnAddressMissing (names the request and the reply), TransportReplyNotSupported, plus EndpointNotFound for the in-memory queue. All have a help field.
  • Transport capability: an optional Transport.sendToAddress(endpointName, message, attributes, sendOptions) method, not a TransportSendOptions field. With a field, a transport that doesn't know about it, including any third-party one, would quietly ignore it and publish the reply to its topic, so the reply fans out again with no error. With a method, the bus can tell whether the transport supports replies and throw TransportReplyNotSupported before anything is buffered. It also takes any Message, where send() is typed to commands and publish() to events.
    • In-memory: queues to its own endpoint, even messages with no handler (point-to-point needs no subscription). Any other endpoint throws EndpointNotFound.
    • RabbitMQ: the default exchange, with the queue name as routing key. The return address is the native AMQP replyTo property, so no header needs reserving.
    • SQS: SendMessage to resolveQueueUrl(config, endpointName) (same account and region), with the body shaped like the SNS envelope, so readNextMessage and bus-sqs-lambda read it unchanged. replyTo is a new SNS attribute and is reserved in assertSendOptions / toHeaderAttributeMap.
  • Tests: transportTests now requires sendToAddress. It checks a direct send to the transport's own endpoint arrives through a custom handler with no topic, so it can only arrive point-to-point, with all its attributes including replyTo. It checks a ctx.reply() reaches the requester with its correlation and sticky attributes. The RabbitMQ and SQS integration tests also start requester, replier and bystander buses, and check that the bystander, which is subscribed to the reply type, doesn't receive the reply.
  • Docs: request-reply.md is rewritten around ctx.reply(), with handler and workflow repliers in both styles and a return-address section. The fan-out caveats are gone, and the field-mapping section stays for repliers that can't use reply(). Also updated: message attributes, middleware, handling messages, the testing snippet, the custom transport page and snippet, and the SQS, RabbitMQ and Lambda pages.
  • Breaking (changeset minor, MIGRATING.md entries): HandlerContext has a required reply, OutgoingContext has a reply kind, replyTo is a reserved SQS header, and transportTests needs sendToAddress.

Review follow-ups (latest commit):

  • SQS return address is the queue URL. Transports get an optional returnAddress, which the bus stamps as replyTo, falling back to endpointName. SQS returns the queue URL (from queueArn, or account + region + queueName), so cross-account and cross-region requesters work. The transport method is renamed sendToAddress(address, …) to match. SQS maps QueueDoesNotExist to EndpointNotFound. A bare queue name still resolves in the replier's own account and region.
  • Reply guard: reply() now checks that the current handling context is this request (same raw, still being handled) and that its handler's outbox is still open. New tests cover a kept context called while another message is handled, and a timer that fires after the handler resolved.
  • Unrecoverable: defaultRecoverability() dead-letters ReturnAddressMissing, TransportReplyNotSupported and EndpointNotFound on the first failure (exported as ALWAYS_UNRECOVERABLE).
  • Docs and messages: the docs, changeset and JSDoc now say a reply "isn't delivered through a subscription", since the requester still subscribes to types it handles. Reply-only handlers that need no subscription are left to provision infrastructure at deploy time, not on application start #333. The docs also cover what happens when a reply can't be delivered (SQS and in-memory: EndpointNotFound, dead-lettered; RabbitMQ: silently dropped). The ReplyOutsideHandlingContext and EndpointNotFound help texts are corrected, and the docs nits are fixed.
  • Bystander tests (RabbitMQ, SQS) have a positive control: a published TestReply must reach the bystander. They wait on events instead of sleeping.
  • More breaking notes: RabbitMQ's AMQP replyTo on every message, replyTo in received attributes, and OutgoingContext['message'] widening to Message.

Rebased onto #328 (delayed delivery): delayed sends keep the replyTo stamped before they're stored, and the dispatcher sends it through unchanged (tested in core, and for RabbitMQ through a send-only transport like a scheduler's). ctx.reply() rejects deliverAfter/deliverAt with DelayedReplyNotSupported (always unrecoverable): storing a reply would need a destination column in the Postgres and MongoDB outgoing-message stores, so delayed replies are left as a gap. Cross-region SQS replies go through a client for the queue's region.

Things to look at:

  • RabbitMQ silently drops a reply to a queue that doesn't exist (documented).
  • ctx.reply() answers the message being handled. A workflow that answers in a later step can't use it, and the guide says so.
  • This may conflict with add opentelemetry tracing and metrics middleware #327 (outgoing middleware).

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@adenhertog
adenhertog merged commit 85d12f9 into master Oct 4, 2026
4 checks passed
@adenhertog
adenhertog deleted the issue-331-reply branch October 4, 2026 00:39
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.

add ctx.reply() to answer a request at its return address

1 participant