Files
archie/internal/domain/workintake/subjects.go
T
sam 9e8427bc8e feat(nats): provision the ARCHIE_REACTIONS fan-out stream
archie.reaction.* had no matching stream. bootstrap.go:501-502 bound only
ARCHIE_TASKS to archie.task.> under WorkQueuePolicy, so any publish to a
reaction subject made js.PublishMsg fail and the publisher saw a publish error
-- the reason a previously landed reaction producer would have answered HTTP 500
on every webhook delivery.

ARCHIE_TASKS must stay WorkQueuePolicy: under it a second consumer on an
overlapping filter subject silently receives nothing, which is the trap recorded
in CLAUDE.md. Reactions need the opposite property, so they get a second stream:

- workintake.SubjectReactionWildcard ("archie.reaction.>")
- nats.DefaultReactionStreamName ("ARCHIE_REACTIONS")
- nats.FanOutRetention(), a pointer helper so composition can select
  LimitsPolicy without importing the NATS SDK, since nil Retention already means
  WorkQueuePolicy and the zero value (LimitsPolicy) is a valid explicit choice
- a second nats.Connect in connectNATS carrying that stream, closed on failure
  and registered for shutdown cleanup

Red-then-green: TestConnectNATSCreatesReactionStream failed with "publish to
archie.reaction.* = nats: no response from stream, want it to land in
ARCHIE_REACTIONS" before the stream existed.

Step 1 of docs/prds/pr-review-remediation.md (bead archie-core-8li9.1).
2026-09-19 01:51:07 +10:00

132 lines
4.4 KiB
Go

package workintake
import (
"errors"
"fmt"
"strings"
)
// Task subjects. Each encodes the routing kind so a multi-daemon deployment
// can filter by the work it wants.
const (
SubjectTaskBug = "archie.task.bug"
SubjectTaskFeature = "archie.task.feature"
SubjectTaskBootstrap = "archie.task.bootstrap"
SubjectTaskDefault = "archie.task.default"
// SubjectTaskWildcard matches every task subject.
SubjectTaskWildcard = "archie.task.>"
)
// SubjectReactionWildcard matches every reaction subject under archie.reaction.
// Reactions are producer-only fan-out events
// (docs/prds/event-sources-and-reactions.md) delivered on a separate
// LimitsPolicy stream, so every registered consumer sees every matching event
// -- unlike the work-queue ARCHIE_TASKS stream, where one consumer claims each
// message.
const SubjectReactionWildcard = "archie.reaction.>"
// ErrUnknownKind reports a kind with no subject. Routing it to the default
// queue instead would silently misdeliver work.
var ErrUnknownKind = errors.New("workintake: unknown task kind")
// Kind is the routing category an issue's labels select.
//
// It is the single axis both the workflow registry and the task subjects
// route on. The label table was once written twice -- a switch in
// workflow.Route and a subject lookup in the NATS package -- so adding a
// label meant remembering both, and the transport had to know what a forge
// label was.
type Kind string
const (
KindBug Kind = "bug"
KindFeature Kind = "feature"
KindBootstrap Kind = "bootstrap"
// KindDefault routes issues carrying no recognised label.
KindDefault Kind = "default"
)
// labelKinds maps a forge issue label to its routing kind. This is the one
// place the label vocabulary is defined.
var labelKinds = map[string]Kind{
"bug": KindBug,
"feature": KindFeature,
"bootstrap": KindBootstrap,
}
// kindSubjects is the closed set of routable kinds.
var kindSubjects = map[Kind]string{
KindBug: SubjectTaskBug,
KindFeature: SubjectTaskFeature,
KindBootstrap: SubjectTaskBootstrap,
KindDefault: SubjectTaskDefault,
}
// Validate reports whether the kind is routable. The zero kind is accepted
// and means KindDefault, so a caller that does not classify still publishes.
func (k Kind) Validate() error {
if k == "" {
return nil
}
if _, ok := kindSubjects[k]; !ok {
return fmt.Errorf("%w: %q", ErrUnknownKind, string(k))
}
return nil
}
// SubjectForKind renders a kind as its task subject. The zero and any
// unrecognised kind render as SubjectTaskDefault so a message is never
// addressed to an unroutable subject; publishers validate first.
func SubjectForKind(kind Kind) string {
if subject, ok := kindSubjects[kind]; ok {
return subject
}
return SubjectTaskDefault
}
// KindForLabels returns the routing kind for a set of issue labels. The first
// recognised label wins; an empty or unrecognised set is KindDefault.
func KindForLabels(labels []string) Kind {
for _, kind := range KindsForLabels(labels) {
return kind
}
return KindDefault
}
// KindsForLabels returns every recognised kind in label order.
//
// Workflow routing needs the full ordered set rather than just the first:
// when a task is labelled "bug,feature" and no "tdd" workflow is registered,
// it must still fall through to "feasibility" rather than to the default.
func KindsForLabels(labels []string) []Kind {
var kinds []Kind
for _, label := range labels {
if kind, ok := labelKinds[labelValue(label)]; ok {
kinds = append(kinds, kind)
}
}
return kinds
}
// labelValue reduces a forge label to the value labelKinds is keyed on.
//
// Forges namespace labels by convention ("type::feature", "priority::medium")
// and the namespace is the issue tracker's own taxonomy, not part of the
// routing vocabulary, so the value after the last "::" is what identifies a
// kind. Matching the bare string alone meant a repo that namespaces its
// labels -- which this one does -- never matched any kind, and every task
// fell through kind routing to the content-aware fallback in Route.
//
// The namespace itself is deliberately not checked: a closed three-value
// vocabulary (bug, feature, bootstrap) is specific enough that a label ending
// in one of them means that kind whatever the tracker files it under.
func labelValue(label string) string {
trimmed := strings.TrimSpace(label)
if i := strings.LastIndex(trimmed, "::"); i >= 0 {
return strings.TrimSpace(trimmed[i+len("::"):])
}
return trimmed
}