From 4be28e0e48d90f37d4f7ec82fd5013e90189a96f Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 24 Jul 2026 07:51:06 +1000 Subject: [PATCH] feat: optional NATS token authentication (archie-core-l1q) Add token_env to [nats] config, resolved and validated at daemon startup (fails early if configured but the named env var is unset). Container workers get NATS_URL/NATS_TOKEN; archie-agent honors both while CLI-supplied URL still takes precedence. Anonymous auth remains the default for single-node deployments. Unblocks archie-core-5zh (promote archie-agent to per-task process). --- .beads/interactions.jsonl | 1 + .beads/issues.jsonl | 2 +- Dockerfile | 2 +- cmd/archie-agent/main.go | 25 ++++++++++---- cmd/archie-agent/main_test.go | 30 ++++++++++++++++ cmd/archied/main.go | 18 +++++++++- cmd/archied/main_test.go | 44 ++++++++++++++++++++++++ config.example.toml | 2 +- docs/prds/archie-core-v2-architecture.md | 12 ++++--- internal/agentexec/nats_test.go | 2 +- internal/nats/client.go | 8 +++-- internal/nats/client_test.go | 33 ++++++++++++++++-- 12 files changed, 159 insertions(+), 20 deletions(-) create mode 100644 cmd/archie-agent/main_test.go create mode 100644 cmd/archied/main_test.go diff --git a/.beads/interactions.jsonl b/.beads/interactions.jsonl index ccd5ca159..67af0c41d 100644 --- a/.beads/interactions.jsonl +++ b/.beads/interactions.jsonl @@ -1,3 +1,4 @@ {"id":"int-fe360a2002888c6426a537d2363f900d","kind":"field_change","created_at":"2026-07-22T16:37:16.342832875Z","actor":"Sam Catlow","issue_id":"archie-core-rig","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Audit complete: 58 Gitea issues (#3-#60) filed at git.catlow.cloud/sam/archie-core. 60 raw findings, 59 verified, 58 successfully filed. 8 agents hit session limit at end (webui/workflow/worktree file-phase)."}} {"id":"int-7e16fccbb985b4942cf8bace2a8bb7d4","kind":"field_change","created_at":"2026-07-23T21:37:42.460096183Z","actor":"Sam Catlow","issue_id":"archie-core-u64","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Implemented concurrent SQLite and NATS task dispatch using containers.max_concurrency, with default same-repo serialization and race-tested dispatcher coverage."}} {"id":"int-dd2e5e0351a12ea84b50ad3e3c3c42e3","kind":"field_change","created_at":"2026-07-23T21:46:52.85072403Z","actor":"Sam Catlow","issue_id":"archie-core-g1l","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Added config.Repo.AllowConcurrent (allow_concurrent toml key) and threaded it through taskDispatcher so opted-in repos skip the default FIFO same-repo serialization while still respecting containers.max_concurrency. Worktrees are already keyed per issue number, so no on-disk collision. Race-tested dispatcher coverage added."}} +{"id":"int-a6f23a24a14200e58047cb705b026703","kind":"field_change","created_at":"2026-07-23T21:49:40.094050867Z","actor":"Sam Catlow","issue_id":"archie-core-l1q","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Completed JetStream task dispatch and NATS agent request/reply, including token_env authentication for archied and canonical NATS_URL/NATS_TOKEN propagation to container workers; anonymous local mode remains supported and race-tested."}} diff --git a/.beads/issues.jsonl b/.beads/issues.jsonl index e3877d11a..1e81070e4 100644 --- a/.beads/issues.jsonl +++ b/.beads/issues.jsonl @@ -1,7 +1,7 @@ {"_type":"issue","id":"archie-core-u64","title":"Daemon: process multiple tasks concurrently (max_concurrency enforcement)","description":"Daemon currently processes one task at a time (docs/prds/archie-core-v2-architecture.md Phase 2). This is the core blocker between current state and any multi-agent scaling mode — needed before container max_concurrency or same-repo concurrency can matter. Wire the daemon's dispatch loop to run N tasks in parallel, respecting a configurable max_concurrency.","design":"internal/container/pool.go already enforces max_concurrency for container lifecycle — reuse that limit/pattern in the daemon's task dispatch rather than inventing a second concurrency control.","acceptance_criteria":"Daemon dispatch loop runs multiple tasks concurrently up to a configured max_concurrency limit; single-task-at-a-time behavior is gone.","status":"closed","priority":1,"issue_type":"feature","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-23T19:36:31Z","created_by":"Sam Catlow","updated_at":"2026-07-23T21:37:42Z","started_at":"2026-07-23T21:29:41Z","closed_at":"2026-07-23T21:37:42Z","close_reason":"Implemented concurrent SQLite and NATS task dispatch using containers.max_concurrency, with default same-repo serialization and race-tested dispatcher coverage.","dependency_count":0,"dependent_count":2,"comment_count":0} {"_type":"issue","id":"archie-core-358","title":"ContainerRunner: implement agentexec.Runner against Docker for sandboxed execution","description":"Phase 2 of docs/prds/archie-core-v2-architecture.md. agentexec.Runner (InProcessRunner/SubprocessRunner) is an interface deliberately designed to be swapped for a container-backed runner without touching workflow orchestration. internal/container/pool.go already handles Docker lifecycle (pull, create, start, release, orphan recovery). Implement ContainerRunner against the existing Runner interface.","design":"Do not redesign the execution boundary — implement strictly against the existing Runner interface per its doc comments.","acceptance_criteria":"ContainerRunner implements agentexec.Runner, runs tasks in ephemeral Docker containers with workspace bind-mounted, and is selectable via [agent] mode = \"nats\" + containers.enabled = true.","status":"open","priority":2,"issue_type":"feature","owner":"sam@catlow.tech","created_at":"2026-07-23T19:36:32Z","created_by":"Sam Catlow","updated_at":"2026-07-23T19:36:32Z","dependencies":[{"issue_id":"archie-core-358","depends_on_id":"archie-core-5zh","type":"blocks","created_at":"2026-07-24T05:36:37Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":1,"comment_count":0} {"_type":"issue","id":"archie-core-5zh","title":"Promote archie-agent to per-task process; move workflow routing out of archied","description":"cmd/archie-agent exists but only executes one workflow stage per invocation over the internal/agentexec stdin/stdout JSON protocol (v1). Phase 1 of the PRD requires promoting it to the PRD's per-task process model, moving workflow.Route/workflow.Run out of archied and into archie-agent.","acceptance_criteria":"workflow.Route and workflow.Run execute inside archie-agent, not archied; archie-agent handles a full task (not a single stage) per invocation.","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-07-23T19:36:32Z","created_by":"Sam Catlow","updated_at":"2026-07-23T19:36:32Z","dependencies":[{"issue_id":"archie-core-5zh","depends_on_id":"archie-core-l1q","type":"blocks","created_at":"2026-07-24T05:36:36Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":1,"comment_count":0} -{"_type":"issue","id":"archie-core-l1q","title":"NATS transport: JetStream task queue + request-reply between daemon and archie-agent","description":"Phase 1 of docs/prds/archie-core-v2-architecture.md. internal/events.Bus (in-process pub/sub, already feeding SQLite + SSE) is the natural seam to replace the transport under. Daemon publishes tasks, archie-agent consumes, results return via NATS. Preserve current inprocess mode via [agent] mode default for local dev.","design":"Add optional [nats] config block (url, token_env), default anonymous auth for single-node, credentials required for multi-node per PRD sec.9 open question 2.","acceptance_criteria":"Daemon can dispatch a task to archie-agent over NATS JetStream and receive results via request-reply; inprocess mode still works unchanged.","status":"in_progress","priority":2,"issue_type":"feature","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-23T19:36:31Z","created_by":"Sam Catlow","updated_at":"2026-07-23T21:43:05Z","started_at":"2026-07-23T21:43:05Z","dependencies":[{"issue_id":"archie-core-l1q","depends_on_id":"archie-core-u64","type":"blocks","created_at":"2026-07-24T05:36:36Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":1,"comment_count":0} +{"_type":"issue","id":"archie-core-l1q","title":"NATS transport: JetStream task queue + request-reply between daemon and archie-agent","description":"Phase 1 of docs/prds/archie-core-v2-architecture.md. internal/events.Bus (in-process pub/sub, already feeding SQLite + SSE) is the natural seam to replace the transport under. Daemon publishes tasks, archie-agent consumes, results return via NATS. Preserve current inprocess mode via [agent] mode default for local dev.","design":"Add optional [nats] config block (url, token_env), default anonymous auth for single-node, credentials required for multi-node per PRD sec.9 open question 2.","acceptance_criteria":"Daemon can dispatch a task to archie-agent over NATS JetStream and receive results via request-reply; inprocess mode still works unchanged.","status":"closed","priority":2,"issue_type":"feature","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-23T19:36:31Z","created_by":"Sam Catlow","updated_at":"2026-07-23T21:49:40Z","started_at":"2026-07-23T21:43:05Z","closed_at":"2026-07-23T21:49:40Z","close_reason":"Completed JetStream task dispatch and NATS agent request/reply, including token_env authentication for archied and canonical NATS_URL/NATS_TOKEN propagation to container workers; anonymous local mode remains supported and race-tested.","dependencies":[{"issue_id":"archie-core-l1q","depends_on_id":"archie-core-u64","type":"blocks","created_at":"2026-07-24T05:36:36Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":1,"comment_count":0} {"_type":"issue","id":"archie-core-rig","title":"Audit archie-core repo for bugs/improvements, file as Gitea issues","description":"Fan out exploration subagents across internal/ subsystems to find bugs, tech debt, and enhancement opportunities. Each confirmed finding gets filed as a Gitea issue on git.catlow.cloud/sam/archie-core.","status":"closed","priority":2,"issue_type":"task","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-22T16:24:07Z","created_by":"Sam Catlow","updated_at":"2026-07-22T16:37:16Z","started_at":"2026-07-22T16:24:09Z","closed_at":"2026-07-22T16:37:16Z","close_reason":"Audit complete: 58 Gitea issues (#3-#60) filed at git.catlow.cloud/sam/archie-core. 60 raw findings, 59 verified, 58 successfully filed. 8 agents hit session limit at end (webui/workflow/worktree file-phase).","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-g1l","title":"Per-repo allow_concurrent override for same-repo multi-agent work","description":"Concurrent agents on the same repo are blocked by default (worktree conflict). PRD sec.9 open question 5 proposes a per-repo config override (allow_concurrent = true) for repos where parallel work is safe (different base branches, different packages). Deferred to Phase 2 in the PRD; depends on the daemon supporting concurrent task dispatch at all.","acceptance_criteria":"Repos with allow_concurrent = true in [[repos]] config can have multiple agents working simultaneously without worktree collisions; repos without it keep current serialized behavior.","status":"closed","priority":3,"issue_type":"feature","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-23T19:36:32Z","created_by":"Sam Catlow","updated_at":"2026-07-23T21:46:53Z","started_at":"2026-07-23T21:43:14Z","closed_at":"2026-07-23T21:46:53Z","close_reason":"Added config.Repo.AllowConcurrent (allow_concurrent toml key) and threaded it through taskDispatcher so opted-in repos skip the default FIFO same-repo serialization while still respecting containers.max_concurrency. Worktrees are already keyed per issue number, so no on-disk collision. Race-tested dispatcher coverage added.","dependencies":[{"issue_id":"archie-core-g1l","depends_on_id":"archie-core-u64","type":"blocks","created_at":"2026-07-24T05:36:37Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-j7v","title":"Persistent volumes with TTL for container/worktree storage","description":"Phase 2 of docs/prds/archie-core-v2-architecture.md. Current worktree lifecycle is fresh-clone-per-task with status-driven cleanup, no TTL. Add persistent volume support with TTL-based expiry to avoid repeated full clones for repeated work on the same repo.","acceptance_criteria":"Container/worktree storage can persist across tasks for the same repo up to a configured TTL, with automatic cleanup after expiry.","status":"open","priority":3,"issue_type":"feature","owner":"sam@catlow.tech","created_at":"2026-07-23T19:36:32Z","created_by":"Sam Catlow","updated_at":"2026-07-23T19:36:32Z","dependencies":[{"issue_id":"archie-core-j7v","depends_on_id":"archie-core-358","type":"blocks","created_at":"2026-07-24T05:36:37Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} diff --git a/Dockerfile b/Dockerfile index e7a9da1bd..cfafbfa43 100644 --- a/Dockerfile +++ b/Dockerfile @@ -71,5 +71,5 @@ ENV PATH="/root/.local/bin:${PATH}" COPY --from=builder /usr/local/bin/archie-agent /usr/local/bin/archie-agent # ── entrypoint ──────────────────────────────────────────────────────── -# The daemon passes -nats-url and other flags at container start. +# The daemon injects NATS_URL and, when configured, NATS_TOKEN. ENTRYPOINT ["archie-agent"] diff --git a/cmd/archie-agent/main.go b/cmd/archie-agent/main.go index 3c8010515..e06fc7156 100644 --- a/cmd/archie-agent/main.go +++ b/cmd/archie-agent/main.go @@ -35,12 +35,13 @@ func main() { } func run() int { - natsURL := flag.String("nats-url", "", "NATS server URL (required)") + natsURLFlag := flag.String("nats-url", "", "NATS server URL (defaults to NATS_URL)") consumer := flag.String("consumer", consumerName, "JetStream consumer name") flag.Parse() - if *natsURL == "" { - fmt.Fprintln(os.Stderr, "error: -nats-url is required") + natsURL, natsToken := natsConnectionSettings(*natsURLFlag, os.Getenv) + if natsURL == "" { + fmt.Fprintln(os.Stderr, "error: -nats-url or NATS_URL is required") flag.Usage() return 1 } @@ -51,13 +52,17 @@ func run() int { defer stop() // Connect to NATS. - nc, err := nats.Connect(*natsURL) + var natsOptions []nats.Option + if natsToken != "" { + natsOptions = append(natsOptions, nats.Token(natsToken)) + } + nc, err := nats.Connect(natsURL, natsOptions...) if err != nil { log.Error("nats connect failed", "err", err) return 1 } defer nc.Close() - log.Info("nats connected", "url", *natsURL) + log.Info("nats connected", "url", natsURL) // JetStream setup. js, err := jetstream.New(nc) @@ -92,7 +97,7 @@ func run() int { return 1 } - log.Info("archie-agent ready", "nats", *natsURL, "consumer", *consumer) + log.Info("archie-agent ready", "nats", natsURL, "consumer", *consumer) // Main loop: fetch, run, reply, ack. for { @@ -136,6 +141,14 @@ func run() int { } } +func natsConnectionSettings(flagURL string, getenv func(string) string) (url, token string) { + url = flagURL + if url == "" { + url = getenv("NATS_URL") + } + return url, getenv("NATS_TOKEN") +} + func handle(ctx context.Context, msg jetstream.Msg, nc *nats.Conn, log *slog.Logger) error { var req agentexec.AgentRequestMessage if err := json.Unmarshal(msg.Data(), &req); err != nil { diff --git a/cmd/archie-agent/main_test.go b/cmd/archie-agent/main_test.go new file mode 100644 index 000000000..a4bae0c12 --- /dev/null +++ b/cmd/archie-agent/main_test.go @@ -0,0 +1,30 @@ +package main + +import "testing" + +func TestNATSConnectionSettings(t *testing.T) { + getenv := func(name string) string { + switch name { + case "NATS_URL": + return "nats://nats.example:4222" + case "NATS_TOKEN": + return "test-nats-token" + default: + return "" + } + } + + t.Run("container environment", func(t *testing.T) { + url, token := natsConnectionSettings("", getenv) + if url != "nats://nats.example:4222" || token != "test-nats-token" { + t.Fatalf("natsConnectionSettings() = (%q, %q)", url, token) + } + }) + + t.Run("flag URL takes precedence", func(t *testing.T) { + url, token := natsConnectionSettings("nats://flag.example:4222", getenv) + if url != "nats://flag.example:4222" || token != "test-nats-token" { + t.Fatalf("natsConnectionSettings() = (%q, %q)", url, token) + } + }) +} diff --git a/cmd/archied/main.go b/cmd/archied/main.go index 760bd5fc4..f0971bc7e 100644 --- a/cmd/archied/main.go +++ b/cmd/archied/main.go @@ -116,7 +116,12 @@ func run() int { // NATS client (optional — SQLite flow unchanged when [nats] is absent). var natsClient *nats.Client if cfg.NATS.URL != "" { - natsClient, err = nats.Connect(ctx, cfg.NATS.URL, log) + natsToken, err := configuredNATSToken(cfg.NATS, os.Getenv) + if err != nil { + log.Error("nats credentials", "err", err) + return 1 + } + natsClient, err = nats.Connect(ctx, cfg.NATS.URL, natsToken, log) if err != nil { log.Error("nats connect failed", "err", err) return 1 @@ -258,6 +263,17 @@ func executionProviders(cfg config.Config) map[string]agentexec.Provider { return providers } +func configuredNATSToken(cfg config.NATSConfig, getenv func(string) string) (string, error) { + if cfg.TokenEnv == "" { + return "", nil + } + token := getenv(cfg.TokenEnv) + if token == "" { + return "", fmt.Errorf("%s is required when nats.token_env is configured", cfg.TokenEnv) + } + return token, nil +} + func configHome() string { if x := os.Getenv("XDG_CONFIG_HOME"); x != "" { return x diff --git a/cmd/archied/main_test.go b/cmd/archied/main_test.go new file mode 100644 index 000000000..38124f98c --- /dev/null +++ b/cmd/archied/main_test.go @@ -0,0 +1,44 @@ +package main + +import ( + "strings" + "testing" + + "github.com/samcharles93/archie-core/internal/config" +) + +func TestConfiguredNATSToken(t *testing.T) { + t.Run("anonymous when token env is not configured", func(t *testing.T) { + token, err := configuredNATSToken(config.NATSConfig{}, func(string) string { + return "" + }) + if err != nil || token != "" { + t.Fatalf("configuredNATSToken() = (%q, %v), want empty token and nil error", token, err) + } + }) + + t.Run("reads configured environment variable", func(t *testing.T) { + token, err := configuredNATSToken( + config.NATSConfig{TokenEnv: "ARCHIE_NATS_SECRET"}, + func(name string) string { + if name == "ARCHIE_NATS_SECRET" { + return "test-nats-token" + } + return "" + }, + ) + if err != nil || token != "test-nats-token" { + t.Fatalf("configuredNATSToken() = (%q, %v)", token, err) + } + }) + + t.Run("rejects missing configured credential", func(t *testing.T) { + _, err := configuredNATSToken( + config.NATSConfig{TokenEnv: "ARCHIE_NATS_SECRET"}, + func(string) string { return "" }, + ) + if err == nil || !strings.Contains(err.Error(), "ARCHIE_NATS_SECRET") { + t.Fatalf("configuredNATSToken() error = %v, want missing variable name", err) + } + }) +} diff --git a/config.example.toml b/config.example.toml index 14be36601..2d0c8d55a 100644 --- a/config.example.toml +++ b/config.example.toml @@ -61,7 +61,7 @@ gate_max_failures = 5 # to run in single-process SQLite mode. # [nats] # url = "nats://localhost:4222" -# token_env = "NATS_TOKEN" +# token_env = "NATS_TOKEN" # optional; named variable must be set # Docker sandbox execution of archie-agent. Requires [nats] and agent.mode = "nats". # [containers] diff --git a/docs/prds/archie-core-v2-architecture.md b/docs/prds/archie-core-v2-architecture.md index c038a13bd..eaceab791 100644 --- a/docs/prds/archie-core-v2-architecture.md +++ b/docs/prds/archie-core-v2-architecture.md @@ -14,7 +14,7 @@ This document describes a target end state. The table below is the ground truth; |---|---|---| | Daemon/agent binary split | **Implemented** | `cmd/archied` and `cmd/archie-agent` both exist. `archie-agent` is a 189-line NATS-connected per-task worker that subscribes to task subjects, runs the full multi-stage workflow, and reports results. Workflow routing and sequencing (`workflow.Route`, `workflow.Run`) now run inside the agent, not the daemon. Three agent modes: `inprocess`, `subprocess`, `nats`. | | Sandbox / "daemon never runs untrusted code" | **Partial** | Default mode is still `inprocess` for development. Docker sandbox mode (`agent.mode = "nats"` + `containers.enabled = true`) runs each task in an ephemeral Docker container with workspace bind-mounted at `/workspace`. `internal/container/pool.go` manages full lifecycle: pull, create, start, release, orphan recovery. Daemon dispatch and container lifecycle both enforce `max_concurrency`; same-repo tasks remain serialized. | -| NATS / JetStream messaging | **Implemented** | `internal/nats/client.go` (176 lines): JetStream connect, stream creation (WorkQueue retention, file storage), pull consumer (durable, ack-wait 5min, max-deliver 3, dedup by Msg-Id), `PublishTask`, `Fetch`, `NewReplyInbox`. Subject hierarchy in `internal/nats/subjects.go`. `[nats]` config block in config.go. `internal/agentexec/nats.go` implements NATS-based agent runner. | +| NATS / JetStream messaging | **Implemented** | `internal/nats/client.go`: JetStream connect, stream creation (WorkQueue retention, file storage), pull consumer (durable, ack-wait 5min, max-deliver 3, dedup by Msg-Id), `PublishTask`, `Fetch`, `NewReplyInbox`, and optional token authentication. Subject hierarchy lives in `internal/nats/subjects.go`; `internal/agentexec/nats.go` implements request/reply agent execution. | | Docker container sandbox | **Implemented** | `internal/container/pool.go` (210 lines): Docker API client, image pull (always/missing/never), container create with workspace bind mount, start, release (stop+remove), orphan recovery on daemon restart. `Dockerfile` at repo root builds archie-agent fat image. `[containers]` config block: enabled, image, max_concurrency, max_uptime, pull_policy. `agent.mode = "nats"` required when containers enabled. | | `/data/` volume, `task.json` | **Partial** | `container.WriteTaskJSON()` writes task payload to the worktree before container acquire. The container pool bind-mounts the worktree at `/data/worktree`. Task payload travels over NATS as the primary channel; `/data/task.json` is a boot-time brief for the container. Worktrees are still ephemeral fresh clones per task. | | `[containers]` config block | **Implemented** | `ContainerConfig` struct in config.go: enabled, image, max_concurrency, max_uptime, pull_policy. Validated on load. | @@ -435,7 +435,7 @@ The agent writes whatever it wants to `response`. The daemon reviews it. No agen ## 8. Implementation phases -> Updated 2026-07-22 against actual repo state (see sec.0). Items marked **done** are already merged; nothing else in +> Updated 2026-07-24 against actual repo state (see sec.0). Items marked **done** exist in the current tree; nothing else in > this document is implemented beyond them. Phase ordering below is unchanged from the original sketch except Phase 3, > which now builds on top of existing Yaegi work instead of assuming a blank slate. @@ -446,9 +446,11 @@ The agent writes whatever it wants to `response`. The daemon reviews it. No agen per invocation over a versioned stdin/stdout JSON protocol (`internal/agentexec`, protocol v1). Still needed: promote it from a single-stage subprocess worker to the PRD's per-task process, and move workflow routing/sequencing (`workflow.Route`/`workflow.Run`) out of `archied` and into it. -- Add NATS to both (JetStream for task queue, request-reply for comms) -- not started. `internal/events.Bus` (in-process - pub/sub, already feeding SQLite + SSE) is the natural seam to replace the transport under. -- Daemon publishes, agent consumes, results return via NATS -- not started +- ~~Add NATS to both (JetStream for task queue, request-reply for comms)~~ -- **done**. `[nats]` is optional, supports + anonymous local mode and token authentication via `token_env`, and leaves `internal/events.Bus` as the local + observability fan-out. +- ~~Daemon publishes, agent consumes, results return via NATS~~ -- **done**. Task discovery uses JetStream work-queue + subjects; autonomous stages use JetStream requests plus core NATS reply inboxes. - Current single-process mode preserved for development (`--local` flag) -- effectively already true: `inprocess` `[agent] mode` is the _default_, not an opt-in fallback, so this constraint is satisfied but should be revisited once subprocess/container modes are the default. diff --git a/internal/agentexec/nats_test.go b/internal/agentexec/nats_test.go index 2ccdb2276..f5e3bccf7 100644 --- a/internal/agentexec/nats_test.go +++ b/internal/agentexec/nats_test.go @@ -251,7 +251,7 @@ func TestNATSRunnerRoundTrip(t *testing.T) { url := srv.ClientURL() ctx := context.Background() - client, err := arnats.Connect(ctx, url, slog.New(slog.DiscardHandler)) + client, err := arnats.Connect(ctx, url, "", slog.New(slog.DiscardHandler)) if err != nil { t.Fatal(err) } diff --git a/internal/nats/client.go b/internal/nats/client.go index b848e34d5..025c748e4 100644 --- a/internal/nats/client.go +++ b/internal/nats/client.go @@ -41,8 +41,12 @@ type Client struct { // Connect dials url, sets up the JetStream stream and pull consumer. // The stream is created with WorkQueue retention and file storage. -func Connect(ctx context.Context, url string, log *slog.Logger) (*Client, error) { - nc, err := nats.Connect(url) +func Connect(ctx context.Context, url, token string, log *slog.Logger) (*Client, error) { + var opts []nats.Option + if token != "" { + opts = append(opts, nats.Token(token)) + } + nc, err := nats.Connect(url, opts...) if err != nil { return nil, fmt.Errorf("nats connect: %w", err) } diff --git a/internal/nats/client_test.go b/internal/nats/client_test.go index 823f4d4d8..2c87d39f3 100644 --- a/internal/nats/client_test.go +++ b/internal/nats/client_test.go @@ -20,12 +20,41 @@ func startEmbedded(t *testing.T) *server.Server { return srv } +func startEmbeddedWithToken(t *testing.T, token string) *server.Server { + t.Helper() + opts := natssrv.DefaultTestOptions + opts.Port = -1 + opts.Authorization = token + srv := natssrv.RunServer(&opts) + t.Cleanup(srv.Shutdown) + if err := srv.EnableJetStream(&server.JetStreamConfig{StoreDir: t.TempDir()}); err != nil { + t.Fatalf("enable jetstream: %v", err) + } + return srv +} + +func TestConnectUsesToken(t *testing.T) { + const token = "test-nats-token" + srv := startEmbeddedWithToken(t, token) + + client, err := Connect( + context.Background(), + srv.ClientURL(), + token, + slog.New(slog.DiscardHandler), + ) + if err != nil { + t.Fatalf("Connect with token: %v", err) + } + client.Close() +} + func TestConnectAndPublish(t *testing.T) { srv := startEmbedded(t) url := srv.ClientURL() ctx := context.Background() - client, err := Connect(ctx, url, slog.New(slog.DiscardHandler)) + client, err := Connect(ctx, url, "", slog.New(slog.DiscardHandler)) if err != nil { t.Fatal(err) } @@ -60,7 +89,7 @@ func TestDedup(t *testing.T) { url := srv.ClientURL() ctx := context.Background() - client, err := Connect(ctx, url, slog.New(slog.DiscardHandler)) + client, err := Connect(ctx, url, "", slog.New(slog.DiscardHandler)) if err != nil { t.Fatal(err) }