dependencies-and-contracts.md rule 1 says domains must not import
internal/infrastructure. internal/daemon and internal/agentexec both did,
and for exactly the thing the doc carves out: "internal/eventbus is an
approved shared application contract ... it MUST remain broker-neutral."
The Publisher/Consumer/Requester interfaces existed but lived inside the
NATS package, so a caller could depend on the behaviour yet still had to
import the implementation to name it. They now live in internal/eventbus,
and nats.Client asserts against eventbus.Bus.
The larger violation was ownership of meaning. The doc says "message schemas
stay with their owning domains; the event bus transports messages, it does
not own their meaning." The bus exported TaskEnvelope and a PublishTask
method, so the transport knew what a unit of work was, and it exported the
archie.agent.* subject helpers, so it also owned the agent protocol.
- TaskEnvelope, its routing Kind, the label vocabulary and the task
subjects move to internal/domain/workintake, which encodes and decodes
its own bytes. This starts internal/domain/ per organisation.md.
- The agent request/response/system subjects move to internal/agentexec,
which defines what those messages mean.
- internal/workflow keeps only the kind-to-registry mapping; the label
table stays single and lives with the domain that reads forge issues.
- The bus now carries opaque payloads on plain-string subjects.
Consumers declare the smallest interface they need rather than taking the
whole Bus: daemon.TaskBus is publish-unique/fetch/request, agentexec.
RequestBus is publish-request/reply-inbox, and archie-agent's stageBus is
fetch/respond. None can reach what it does not use.
Two defects this surfaced:
- archie-agent declared its own JetStream stream with a second copy of the
name, subjects, retention, dedup window, MaxDeliver and TTLs that
nats.Connect already sets -- two definitions of one stream, free to
diverge. Config now takes Subjects and FilterSubject from composition,
which is what let the agent use the shared client and delete its
hand-rolled setup.
- Replying to a request needs core NATS, not the JetStream publish path:
a reply inbox is ephemeral and belongs to no stream, so a durable
publish to it fails. The old code hid this by using the raw connection.
The contract now distinguishes Respond from Publish. The agentexec
round-trip test caught it after I had made the same mistake in
archie-agent.
Also translated the SDK's ErrNoResponders into eventbus.ErrNoResponders, so
the daemon's taskrun retry no longer imports the NATS SDK to name a
transient condition. The SDK is now confined to the nats package, the *rpc
packages (themselves NATS infrastructure), and cmd/*.
The agentexec test fake previously hand-rolled a JetStream consumer; it now
drains the client's own consumer, which is both closer to production and
avoids conflicting consumers on a work-queue stream.
Verified: go build, go vet, gofmt clean; full test suite passes.
124 lines
4.5 KiB
Go
124 lines
4.5 KiB
Go
package eventbus
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
)
|
|
|
|
// Sentinel outcomes callers match with errors.Is. These are bus semantics,
|
|
// not broker details, so they belong in the contract: an implementation that
|
|
// cannot express them cannot satisfy it.
|
|
var (
|
|
// ErrNoMessage reports that a fetch completed normally with nothing
|
|
// queued. Polling an idle queue is an expected outcome, not a failure.
|
|
ErrNoMessage = errors.New("eventbus: no message available")
|
|
|
|
// ErrNotConnected reports use of a bus whose connection is closed or was
|
|
// never established.
|
|
ErrNotConnected = errors.New("eventbus: not connected")
|
|
|
|
// ErrNoReplyAddress reports a message carrying nowhere to send a reply,
|
|
// so a request cannot be answered.
|
|
ErrNoReplyAddress = errors.New("eventbus: message has no reply address")
|
|
|
|
// ErrNoResponders reports a request sent to a subject nobody is
|
|
// listening on. It is usually transient -- a worker that has connected
|
|
// but not yet subscribed -- so callers typically retry within a deadline
|
|
// rather than failing immediately.
|
|
ErrNoResponders = errors.New("eventbus: no responders")
|
|
)
|
|
|
|
// Message is one delivered message. The payload is opaque: decoding it is the
|
|
// owning domain's job.
|
|
type Message interface {
|
|
// Data returns the raw payload.
|
|
Data() []byte
|
|
|
|
// Subject returns the address the message arrived on.
|
|
Subject() string
|
|
|
|
// ReplyAddress returns where a response should be sent, or
|
|
// ErrNoReplyAddress when the sender expects no reply. How the address
|
|
// travels is the implementation's concern.
|
|
ReplyAddress() (string, error)
|
|
|
|
// Ack marks the message handled so it is not redelivered.
|
|
Ack() error
|
|
|
|
// Nak returns the message for redelivery.
|
|
Nak() error
|
|
}
|
|
|
|
// Publisher sends messages.
|
|
type Publisher interface {
|
|
// Publish sends payload to subject.
|
|
Publish(ctx context.Context, subject string, payload []byte) error
|
|
|
|
// PublishUnique sends payload to subject carrying an idempotency key.
|
|
// Republishing the same key within the implementation's deduplication
|
|
// window is suppressed.
|
|
//
|
|
// This is a required semantic rather than a broker detail: work intake
|
|
// rediscovers the same issue on every poll, so at-least-once delivery
|
|
// without deduplication would enqueue it repeatedly.
|
|
PublishUnique(ctx context.Context, subject, idempotencyKey string, payload []byte) error
|
|
|
|
// PublishRequest sends payload to subject, recording replyTo as the
|
|
// address for the response. It reports ErrNoReplyAddress when replyTo is
|
|
// empty, since a request nobody can answer is a caller error rather than
|
|
// a fire-and-forget publish.
|
|
PublishRequest(ctx context.Context, subject, replyTo string, payload []byte) error
|
|
|
|
// Respond answers a request on the address taken from
|
|
// Message.ReplyAddress.
|
|
//
|
|
// This is distinct from Publish, not a convenience over it: a reply
|
|
// address is an ephemeral inbox belonging to one waiting caller, not a
|
|
// durable subject. Sending a reply through the durable path fails,
|
|
// because the inbox is not part of any stream.
|
|
Respond(ctx context.Context, replyAddress string, payload []byte) error
|
|
}
|
|
|
|
// Consumer receives messages.
|
|
type Consumer interface {
|
|
// Fetch returns the next message, or ErrNoMessage if none arrives before
|
|
// the implementation's poll deadline.
|
|
Fetch(ctx context.Context) (Message, error)
|
|
|
|
// Subscribe delivers messages matching filterSubject to handle until ctx
|
|
// is done. A handler returning an error causes redelivery.
|
|
Subscribe(ctx context.Context, filterSubject string, handle func(Message) error) error
|
|
}
|
|
|
|
// Requester performs request/reply.
|
|
type Requester interface {
|
|
// Request sends payload to subject and waits for a single response.
|
|
Request(ctx context.Context, subject string, payload []byte) ([]byte, error)
|
|
|
|
// NewReplyInbox creates an address for one expected response. Use it when
|
|
// the reply must be awaited separately from sending, which Request cannot
|
|
// express -- publishing work and collecting its result are distinct steps
|
|
// when the responder may not exist yet.
|
|
NewReplyInbox() (ReplyInbox, error)
|
|
}
|
|
|
|
// ReplyInbox is a one-shot address awaiting a single response.
|
|
type ReplyInbox interface {
|
|
// Subject is the address to advertise to the responder.
|
|
Subject() string
|
|
|
|
// Next blocks until the reply arrives or ctx is done.
|
|
Next(ctx context.Context) ([]byte, error)
|
|
|
|
// Close releases the inbox.
|
|
Close() error
|
|
}
|
|
|
|
// Bus is the full contract. Depend on the narrowest interface that fits:
|
|
// a publisher-only caller should accept Publisher, not Bus.
|
|
type Bus interface {
|
|
Publisher
|
|
Consumer
|
|
Requester
|
|
}
|