Files
archie/internal/infrastructure/eventbus/nats/doc.go
T
sam 492ca209f9 refactor(nats): rebuild as infrastructure/eventbus/nats with real boundaries
Relocating internal/nats as-is would have cemented its problems into the
approved layout, so this is a rewrite rather than a move.

What was wrong:
- Conn() *nats.Conn, PublishMsg(*nats.Msg) and Fetch() jetstream.Msg pushed
  SDK types into all four consumers, so the broker was not replaceable --
  the opposite of what dependencies-and-contracts.md requires.
- PublishTask took seven positional parameters, six of them strings, where a
  transposed argument was a silent bug.
- Labels travelled as a comma-separated string and were re-split on every
  publish.
- Fetch returned (nil, nil) for 'no message', which every caller had to
  remember to special-case.
- The logger field was stored and never used.
- Stream name, consumer name and every timeout were hardcoded constants.
- Errors were inconsistently wrapped; several paths returned bare errors.
- Connect repeated nc.Close() on each failure branch.

What replaces it:
- One concern per file: config, errors, subjects, message, client,
  publisher, consumer.
- Publisher/Consumer/Requester interfaces with compile-time assertions, so
  callers depend on behaviour and can be faked without a broker.
- A Message wrapper keeps jetstream types behind the boundary; consumers no
  longer import the NATS SDK at all.
- TaskEnvelope with []string labels and JSON methods preserving the
  comma-separated wire form for already-queued messages.
- Sentinel errors (ErrNoMessage, ErrNotConnected, ErrNoReplyAddress,
  ErrInvalidConfig) and %w wrapping with operation context throughout.
- Config with validation and defaults; a single deferred cleanup in Connect.
- CoreConn() as the one documented escape hatch, for the *rpc packages that
  are themselves NATS infrastructure.

Consumers migrated: cmd/archied, cmd/archie-agent, internal/daemon,
internal/agentexec. Behaviour, subjects and wire format unchanged.
2026-07-29 10:19:09 +10:00

27 lines
1.1 KiB
Go

// Package nats is the NATS JetStream transport for task distribution and
// agent communication.
//
// It is infrastructure: it owns the broker connection, stream and consumer
// lifecycle, and the wire encoding of messages. It does not own workflow
// routing, task state, or any other domain behaviour.
//
// The package deliberately keeps NATS SDK types behind its own boundary.
// Callers receive [Message], not jetstream.Msg, and publish [TaskEnvelope] or
// [Request], not *nats.Msg. That keeps the broker replaceable and stops SDK
// types leaking into domain packages, per
// docs/architecture/dependencies-and-contracts.md.
//
// Layout:
//
// config.go connection and stream/consumer tuning
// errors.go sentinel errors callers match on
// subjects.go subject naming, pure and dependency-free
// message.go wire payloads and the Message wrapper
// client.go connection lifecycle
// publisher.go publishing
// consumer.go fetching
//
// When no NATS URL is configured the daemon uses its local SQLite claim flow
// instead; nothing here is required for single-process operation.
package nats