Files
archie/internal/workflow/routing.go
T
sam a8efbf5046 refactor: route tasks on a typed kind, not forge labels
The event bus was inspecting forge issue labels ("bug", "feature",
"bootstrap") to pick a subject. That put a forge vocabulary inside
infrastructure and duplicated the label table already owned by
workflow.Route -- adding a label meant remembering both places, and
nothing would have caught it if you forgot.

The label table now lives once, in internal/workflow, which already owns
label semantics:

  - workflow.Kind names the routing category, with labelKinds as the
    single label vocabulary and kindWorkflows mapping kind to registry
    entry. Route consumes the ordered kind list so a "bug,feature" task
    still falls through to feasibility when no tdd workflow is
    registered, exactly as the switch did.
  - nats.TaskKind is a closed, validated set of subject tokens.
    PublishTask rejects an unknown kind rather than silently delivering
    it to the default queue; the zero kind means default, so messages
    queued before this field decode unchanged.
  - The daemon classifies at the publish site and sets TaskEnvelope.Kind.

Also fixes two defects this exposed:

  - drainNATS never reached its SQLite fallback. Replacing the old
    (nil, nil) no-message signal with ErrNoMessage in 492ca20 made that
    case continue at the top of the loop, but no-message is precisely
    when the fallback must run -- requeued tasks (waiting_human approval,
    retry-parked) never go through a NATS publish, so nothing would ever
    arrive to trigger them. go vet caught it as unreachable code.
  - config.classifyFeatureFile silently overwrote duplicate feature
    files, so config.gateway.yaml and conf.d/gateway.yaml together
    applied one at random. Its own doc comment recorded the bug
    ("or "" on success/duplicate"). The known-feature list in the error
    message is now derived from knownFeatureNames instead of being
    spelled out a second time beside it.

A test also called t.Fatalf from a non-test goroutine, where it does not
stop the test.

Verified: go build, go vet, gofmt clean. Pre-existing gateway (18) and
telegram (1) failures are unchanged in count -- they are lost routing
wiring from the parallel telegram merges, tracked separately.
2026-07-29 10:43:52 +10:00

83 lines
2.4 KiB
Go

package workflow
import "strings"
// Kind is the routing category an issue's labels select.
//
// It is the single axis both the workflow registry and the event bus route
// on. Before this existed the label table was written twice -- once here as
// a switch in Route, once in the NATS package as a subject lookup -- so
// adding a label meant remembering both, and the event bus had to know what
// a forge label was.
type Kind string
const (
KindBug Kind = "bug"
KindFeature Kind = "feature"
KindBootstrap Kind = "bootstrap"
// KindDefault is the routing category for 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,
}
// kindWorkflows names the registry entry each kind prefers. Bootstrap
// exercises the full pipeline deterministically (no LLM spend) -- invites,
// clone, push, PR, labels.
var kindWorkflows = map[Kind]string{
KindBug: "tdd",
KindFeature: "feasibility",
KindBootstrap: "bootstrap",
}
// 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.
//
// Route 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[strings.TrimSpace(label)]; ok {
kinds = append(kinds, kind)
}
}
return kinds
}
// SplitLabels parses the comma-separated label form used on store.Task,
// dropping blanks so "bug,," and a trailing comma do not yield empty labels.
func SplitLabels(raw string) []string {
if raw == "" {
return nil
}
parts := strings.Split(raw, ",")
labels := make([]string, 0, len(parts))
for _, part := range parts {
if trimmed := strings.TrimSpace(part); trimmed != "" {
labels = append(labels, trimmed)
}
}
if len(labels) == 0 {
return nil
}
return labels
}