From 81ce920434e83850d922ea4f71caf85043e47a39 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 24 Jul 2026 08:59:04 +1000 Subject: [PATCH] feat: register storerpc/forgerpc/worktreerpc servers in archied (archie-core-5zh.9) Add registerTaskRPCServers() in cmd/archied/main.go: subscribes all three RPC servers on the daemon's NATS connection whenever [nats] is configured, using the same Store/Forge/worktree.Manager instances the daemon already owns. archie-agent containers now have a live responder for the Store, Forge, and worktree Prepare/Push calls proxied in archie-core-5zh.1-3. Test spins an embedded NATS server and round-trips real storerpc/forgerpc/ worktreerpc clients against the registered handlers. --- .beads/interactions.jsonl | 1 + .beads/issues.jsonl | 2 +- cmd/archied/main.go | 74 +++++++++++++++++++++++---- cmd/archied/main_test.go | 103 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 12 deletions(-) diff --git a/.beads/interactions.jsonl b/.beads/interactions.jsonl index 6dfa41ca1..d623987cd 100644 --- a/.beads/interactions.jsonl +++ b/.beads/interactions.jsonl @@ -8,3 +8,4 @@ {"id":"int-22fa22c9e086bd94634514c7a7708cfb","kind":"field_change","created_at":"2026-07-23T22:28:08.383511654Z","actor":"Sam Catlow","issue_id":"archie-core-5zh.4","extra":{"field":"status","new_value":"closed","old_value":"open","reason":"Superseded by finer-grained sub-issues 5zh.5 (TaskConfig payload), 5zh.6 (TaskContext interface refactor), 5zh.7 (archie-agent full-task handler), 5zh.8 (daemon.go handoff), 5zh.9 (register RPC servers in archied). The original single acceptance criterion (workflow.Route/Run execute in archie-agent) is satisfied once 5zh.7 and 5zh.8 land; splitting further because implementation revealed real design forks (Prepare needing the push token, tc.Cfg needing a curated non-secret payload) that each warrant their own test-first pass."}} {"id":"int-52ef9df12424463a13205bb9f244a308","kind":"field_change","created_at":"2026-07-23T22:51:47.945822737Z","actor":"Sam Catlow","issue_id":"archie-core-5zh.5","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Implemented curated non-secret TaskConfig/TaskForge payload with detached map snapshots, stable JSON Duration encoding, full Repo JSON tags, and regression coverage. Focused config tests, go vet, go fix -diff, and full race suite pass; repository-wide lint remains blocked only by 36 unrelated pre-existing findings."}} {"id":"int-5834a8b380a56b2badadcf9575e40716","kind":"field_change","created_at":"2026-07-23T22:56:17.803871719Z","actor":"Sam Catlow","issue_id":"archie-core-5zh.6","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Trimmed workflow.TaskContext.Forge/Store/Trees to new interfaces (Forger, TaskStore, Trees) in internal/workflow/workflow.go, scoped to exactly the methods stages call mid-run (4 Forge methods, 2 Store methods, 6 Trees methods). Go's structural typing meant zero changes were needed anywhere else — forge.Forge (already an interface), *store.Store, and *worktree.Manager all satisfy the new interfaces automatically, and storerpc.Client/forgerpc.Client/worktreerpc.Client already match by construction from 5zh.1-3. Full build, race suite, vet, and staticcheck all clean with no behavior change. archie-core-5zh.7 (archie-agent full-task handler) and 5zh.9 (register RPC servers in archied) are now unblocked."}} +{"id":"int-e854b54f66c451e2fe7bba43a2debaf0","kind":"field_change","created_at":"2026-07-23T22:58:57.94105369Z","actor":"Sam Catlow","issue_id":"archie-core-5zh.9","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"Added registerTaskRPCServers() in cmd/archied/main.go, wiring storerpc.Server/forgerpc.Server/worktreerpc.Server onto the daemon's NATS connection whenever [nats] is configured, alongside the existing worktree.Manager and forge/store instances the daemon already owns. Test spins an embedded NATS server, registers via the composed function, then round-trips real storerpc/forgerpc/worktreerpc clients against it to prove all three are reachable. archie-core-5zh.7 (archie-agent full-task handler) can now be built and tested end-to-end against a real archied."}} diff --git a/.beads/issues.jsonl b/.beads/issues.jsonl index 5ca1fa021..e1222ecf0 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-5zh.7","title":"archie-agent: run full task (workflow.Route + workflow.Run) via RPC-backed TaskContext","description":"The actual stage-loop move for archie-core-5zh, depending on the TaskConfig payload and TaskContext interface refactor. cmd/archie-agent gains a handler for a new 'run whole task' NATS message (task summary + TaskConfig + repo config, no secrets). On receipt it: builds its own workflow.Registry via skillbuild.BuildRegistry against its mounted worktree (/data/worktree — same pattern the daemon already uses, no registry needs to cross the wire); calls workflow.Route itself; builds a TaskContext using storerpc.Client (Store), forgerpc.Client (Forge), and a hybrid Trees (worktreerpc.Client for Prepare/Push, a local *worktree.Manager with no token for CommitAll/Diff/ChangedFiles/ChangedLines against the bind-mounted dir); calls workflow.Run; reports completion once instead of round-tripping per stage.","acceptance_criteria":"When agent.mode=nats and containers are enabled, archie-agent runs an entire task's workflow (all stages) per invocation, not one stage per round trip; inprocess/subprocess modes are unaffected.","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-07-23T22:28:00Z","created_by":"Sam Catlow","updated_at":"2026-07-23T22:28:00Z","dependencies":[{"issue_id":"archie-core-5zh.7","depends_on_id":"archie-core-5zh","type":"parent-child","created_at":"2026-07-24T08:27:59Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-5zh.7","depends_on_id":"archie-core-5zh.5","type":"blocks","created_at":"2026-07-24T08:28:07Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-5zh.7","depends_on_id":"archie-core-5zh.6","type":"blocks","created_at":"2026-07-24T08:28:07Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":2,"dependent_count":1,"comment_count":0} {"_type":"issue","id":"archie-core-5zh.8","title":"daemon.go: hand off whole task to archie-agent in sandboxed NATS+container mode","description":"Depends on the archie-agent full-task handler existing. internal/daemon/daemon.go's process() currently always calls workflow.Route/workflow.Run itself, dispatching per-stage LLM calls to d.Agent (NATSRunner does this over NATS today). When d.Nats != nil \u0026\u0026 d.ContainerPool != nil (the sandboxed path this whole PRD phase targets), process() should instead publish one 'run whole task' message to archie-agent and await its completion report, skipping local workflow.Route/workflow.Run entirely. inprocess/subprocess modes (no container pool) keep calling workflow.Run in the daemon exactly as today — this only changes behavior for the fully-sandboxed path.","acceptance_criteria":"With containers enabled, daemon.process() delegates the entire task to archie-agent in one round trip; without containers, behavior is unchanged (verified by existing daemon tests continuing to pass).","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-07-23T22:28:00Z","created_by":"Sam Catlow","updated_at":"2026-07-23T22:28:00Z","dependencies":[{"issue_id":"archie-core-5zh.8","depends_on_id":"archie-core-5zh.7","type":"blocks","created_at":"2026-07-24T08:28:07Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-5zh.8","depends_on_id":"archie-core-5zh","type":"parent-child","created_at":"2026-07-24T08:28:00Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} -{"_type":"issue","id":"archie-core-5zh.9","title":"Register storerpc/forgerpc/worktreerpc servers in archied startup","description":"Depends on the TaskContext interface refactor. cmd/archied/main.go builds the Daemon with Store/Forge/Trees but never registers the storerpc.Server/forgerpc.Server/worktreerpc.Server handlers, so archie-agent's RPC calls currently have no responder in production. Wire all three Register(nc) calls into archied's startup when cfg.NATS is configured, alongside the existing NATS client connect.","acceptance_criteria":"When [nats] is configured, archied subscribes to all storerpc/forgerpc/worktreerpc subjects at startup; an archie-agent container can successfully call Store/Forge/worktree operations against a running archied.","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-07-23T22:28:00Z","created_by":"Sam Catlow","updated_at":"2026-07-23T22:28:00Z","dependencies":[{"issue_id":"archie-core-5zh.9","depends_on_id":"archie-core-5zh","type":"parent-child","created_at":"2026-07-24T08:28:00Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-5zh.9","depends_on_id":"archie-core-5zh.6","type":"blocks","created_at":"2026-07-24T08:28:08Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} +{"_type":"issue","id":"archie-core-5zh.9","title":"Register storerpc/forgerpc/worktreerpc servers in archied startup","description":"Depends on the TaskContext interface refactor. cmd/archied/main.go builds the Daemon with Store/Forge/Trees but never registers the storerpc.Server/forgerpc.Server/worktreerpc.Server handlers, so archie-agent's RPC calls currently have no responder in production. Wire all three Register(nc) calls into archied's startup when cfg.NATS is configured, alongside the existing NATS client connect.","acceptance_criteria":"When [nats] is configured, archied subscribes to all storerpc/forgerpc/worktreerpc subjects at startup; an archie-agent container can successfully call Store/Forge/worktree operations against a running archied.","status":"closed","priority":2,"issue_type":"task","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-23T22:28:00Z","created_by":"Sam Catlow","updated_at":"2026-07-23T22:58:58Z","started_at":"2026-07-23T22:56:35Z","closed_at":"2026-07-23T22:58:58Z","close_reason":"Added registerTaskRPCServers() in cmd/archied/main.go, wiring storerpc.Server/forgerpc.Server/worktreerpc.Server onto the daemon's NATS connection whenever [nats] is configured, alongside the existing worktree.Manager and forge/store instances the daemon already owns. Test spins an embedded NATS server, registers via the composed function, then round-trips real storerpc/forgerpc/worktreerpc clients against it to prove all three are reachable. archie-core-5zh.7 (archie-agent full-task handler) can now be built and tested end-to-end against a real archied.","dependencies":[{"issue_id":"archie-core-5zh.9","depends_on_id":"archie-core-5zh","type":"parent-child","created_at":"2026-07-24T08:28:00Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-5zh.9","depends_on_id":"archie-core-5zh.6","type":"blocks","created_at":"2026-07-24T08:28:08Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-5zh.5","title":"Curated TaskConfig payload for archie-agent (no secrets)","description":"Groundwork for archie-core-5zh.4. workflow.TaskContext.Cfg (config.Config) is read throughout stages (Models, Budgets, Dispatch incl. label mapping, DiffCapLines, Notify.Webhook, Forge.Host) but the full Config also carries forge/NATS credentials that must never enter the agent container. Add a config.TaskConfig struct — same field names/shapes as the subset config.Config exposes (Models, Budgets, Dispatch, DiffCapLines, Notify, Forge) — with json tags, plus a constructor (e.g. config.Config.ForTask()) that builds it from the daemon's live config. Add json tags to config.Repo's remaining fields (only AllowConcurrent has them today) since tc.Repo also needs to cross the wire.","acceptance_criteria":"config.TaskConfig marshals/unmarshals via JSON and carries exactly the fields workflow stages read; no token/credential field is reachable from it; config.Repo fully round-trips through JSON.","status":"closed","priority":2,"issue_type":"task","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-23T22:27:59Z","created_by":"Sam Catlow","updated_at":"2026-07-23T22:51:48Z","started_at":"2026-07-23T22:43:06Z","closed_at":"2026-07-23T22:51:48Z","close_reason":"Implemented curated non-secret TaskConfig/TaskForge payload with detached map snapshots, stable JSON Duration encoding, full Repo JSON tags, and regression coverage. Focused config tests, go vet, go fix -diff, and full race suite pass; repository-wide lint remains blocked only by 36 unrelated pre-existing findings.","dependencies":[{"issue_id":"archie-core-5zh.5","depends_on_id":"archie-core-5zh","type":"parent-child","created_at":"2026-07-24T08:27:58Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":1,"comment_count":0} {"_type":"issue","id":"archie-core-5zh.6","title":"TaskContext interface refactor: Forge/Store/Trees as trimmed interfaces","description":"Groundwork for archie-core-5zh.4. workflow.TaskContext currently types Forge/Store/Trees as concrete daemon types (forge.Forge interface already, *store.Store, *worktree.Manager). Define trimmed interfaces in internal/workflow scoped to exactly the methods stages call (Forge: Comment/CloseIssue/CreatePR/SetStateLabel; Store: Update/Transition; Trees: Prepare/CommitAll/Push/Diff/ChangedFiles/ChangedLines) so storerpc.Client, forgerpc.Client, and a worktreerpc-backed implementation can satisfy them from inside archie-agent, while *store.Store/*worktree.Manager and the real forge.Forge implementations keep satisfying them unchanged for the daemon's existing inprocess/subprocess paths.","acceptance_criteria":"TaskContext.Forge/Store/Trees are interface-typed; daemon.go compiles and all existing tests pass unchanged (concrete types satisfy the new interfaces structurally); no behavior change for inprocess/subprocess modes.","status":"closed","priority":2,"issue_type":"task","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-23T22:27:59Z","created_by":"Sam Catlow","updated_at":"2026-07-23T22:56:18Z","started_at":"2026-07-23T22:54:26Z","closed_at":"2026-07-23T22:56:18Z","close_reason":"Trimmed workflow.TaskContext.Forge/Store/Trees to new interfaces (Forger, TaskStore, Trees) in internal/workflow/workflow.go, scoped to exactly the methods stages call mid-run (4 Forge methods, 2 Store methods, 6 Trees methods). Go's structural typing meant zero changes were needed anywhere else — forge.Forge (already an interface), *store.Store, and *worktree.Manager all satisfy the new interfaces automatically, and storerpc.Client/forgerpc.Client/worktreerpc.Client already match by construction from 5zh.1-3. Full build, race suite, vet, and staticcheck all clean with no behavior change. archie-core-5zh.7 (archie-agent full-task handler) and 5zh.9 (register RPC servers in archied) are now unblocked.","dependencies":[{"issue_id":"archie-core-5zh.6","depends_on_id":"archie-core-5zh","type":"parent-child","created_at":"2026-07-24T08:27:59Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":2,"comment_count":0} {"_type":"issue","id":"archie-core-5zh.2","title":"Forge RPC proxy: archie-agent → archied for forge API calls","description":"Phase 1 groundwork for archie-core-5zh. Stages call Forge.SetStateLabel/Comment/CreatePR/CloseIssue/CreateIssue/React/RepliesAfter mid-workflow. Forge holds live API tokens (Gitea/GitHub) that must stay daemon-side per the PRD's 'daemon never runs untrusted code, owns all secrets' boundary. Once workflow.Run moves into archie-agent, these calls need to become NATS request/reply RPCs to the daemon rather than shipping forge credentials into the agent container.","acceptance_criteria":"archie-agent can invoke the forge.Forge interface methods used by workflow stages over NATS; archied executes them against the real forge client and replies; no forge API tokens are present in the agent container.","status":"closed","priority":2,"issue_type":"task","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-07-23T21:55:44Z","created_by":"Sam Catlow","updated_at":"2026-07-23T22:02:41Z","started_at":"2026-07-23T22:00:46Z","closed_at":"2026-07-23T22:02:41Z","close_reason":"Added internal/forgerpc: core-NATS request/reply RPC for the four forge.Forge methods workflow stages actually call mid-run (Comment, CloseIssue, CreatePR, SetStateLabel). Confirmed via grep that all other Forge methods (AssignedIssues, IssuesWithLabel, React, RepliesAfter, PRState, CreateIssue, AcceptInvitations, VerifyPush) are daemon-only poll/reconcile-loop calls, never invoked from a workflow stage, so nothing else needs proxying. archied stays the sole holder of forge API tokens. Not yet wired into daemon.go or workflow.TaskContext — deferred to archie-core-5zh.4.","dependencies":[{"issue_id":"archie-core-5zh.2","depends_on_id":"archie-core-5zh","type":"parent-child","created_at":"2026-07-24T07:55:43Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":1,"comment_count":0} diff --git a/cmd/archied/main.go b/cmd/archied/main.go index f0971bc7e..5a43dba85 100644 --- a/cmd/archied/main.go +++ b/cmd/archied/main.go @@ -14,6 +14,8 @@ import ( "path/filepath" "syscall" + natsio "github.com/nats-io/nats.go" + "github.com/moby/moby/client" "github.com/samcharles93/archie-core/internal/agentexec" "github.com/samcharles93/archie-core/internal/config" @@ -21,16 +23,19 @@ import ( "github.com/samcharles93/archie-core/internal/daemon" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/forge" + "github.com/samcharles93/archie-core/internal/forgerpc" "github.com/samcharles93/archie-core/internal/nats" "github.com/samcharles93/archie-core/internal/plugin" "github.com/samcharles93/archie-core/internal/plugin/pluginextract" "github.com/samcharles93/archie-core/internal/storage" "github.com/samcharles93/archie-core/internal/store" + "github.com/samcharles93/archie-core/internal/storerpc" "github.com/samcharles93/archie-core/internal/webui" "github.com/samcharles93/archie-core/internal/workflow/skillbuild" "github.com/samcharles93/archie-core/internal/workflow/wfeval" "github.com/samcharles93/archie-core/internal/worktree" + "github.com/samcharles93/archie-core/internal/worktreerpc" ) func main() { @@ -215,18 +220,32 @@ func run() int { } } + trees := &worktree.Manager{ + WorkDir: cfg.WorkDir, + Token: token, + BotUser: cfg.BotUser, + BotEmail: cfg.BotEmail, + BaseURL: cfg.Forge.Host, + } + + // Let archie-agent containers (which hold no DB connection, forge + // token, or push credential) proxy Store/Forge/worktree operations + // back to archied over NATS. + if natsClient != nil { + unsubscribe, err := registerTaskRPCServers(natsClient.Conn(), st, forgeClient, trees, log) + if err != nil { + log.Error("task RPC server registration failed", "err", err) + return 1 + } + defer unsubscribe() + } + d := &daemon.Daemon{ - Cfg: cfg, - Store: st, - Bus: bus, - Forge: forgeClient, - Trees: &worktree.Manager{ - WorkDir: cfg.WorkDir, - Token: token, - BotUser: cfg.BotUser, - BotEmail: cfg.BotEmail, - BaseURL: cfg.Forge.Host, - }, + Cfg: cfg, + Store: st, + Bus: bus, + Forge: forgeClient, + Trees: trees, Runtime: llm, Agent: agentRunner, Workflows: registry, @@ -263,6 +282,39 @@ func executionProviders(cfg config.Config) map[string]agentexec.Provider { return providers } +// registerTaskRPCServers subscribes the storerpc/forgerpc/worktreerpc +// handlers on nc so an archie-agent container (which holds no DB +// connection, forge token, or push credential) can proxy those operations +// back to archied. The returned func unsubscribes all three. +func registerTaskRPCServers(nc *natsio.Conn, st *store.Store, forgeClient forge.Forge, trees *worktree.Manager, log *slog.Logger) (unsubscribe func(), err error) { + storeServer := &storerpc.Server{Store: st, Log: log} + unsubStore, err := storeServer.Register(nc) + if err != nil { + return nil, fmt.Errorf("register storerpc: %w", err) + } + + forgeServer := &forgerpc.Server{Forge: forgeClient, Log: log} + unsubForge, err := forgeServer.Register(nc) + if err != nil { + unsubStore() + return nil, fmt.Errorf("register forgerpc: %w", err) + } + + treesServer := &worktreerpc.Server{Trees: trees, Log: log} + unsubTrees, err := treesServer.Register(nc) + if err != nil { + unsubStore() + unsubForge() + return nil, fmt.Errorf("register worktreerpc: %w", err) + } + + return func() { + unsubStore() + unsubForge() + unsubTrees() + }, nil +} + func configuredNATSToken(cfg config.NATSConfig, getenv func(string) string) (string, error) { if cfg.TokenEnv == "" { return "", nil diff --git a/cmd/archied/main_test.go b/cmd/archied/main_test.go index 38124f98c..7cab72043 100644 --- a/cmd/archied/main_test.go +++ b/cmd/archied/main_test.go @@ -1,12 +1,115 @@ package main import ( + "context" + "log/slog" "strings" "testing" + "time" + + "github.com/nats-io/nats-server/v2/server" + natssrv "github.com/nats-io/nats-server/v2/test" + natsio "github.com/nats-io/nats.go" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/forge" + "github.com/samcharles93/archie-core/internal/forgerpc" + "github.com/samcharles93/archie-core/internal/store" + "github.com/samcharles93/archie-core/internal/storerpc" + "github.com/samcharles93/archie-core/internal/worktree" + "github.com/samcharles93/archie-core/internal/worktreerpc" ) +// stubForge implements forge.Forge with no-op successes for the four +// methods registerTaskRPCServers needs to prove reachable; every other +// method panics if called. +type stubForge struct{} + +func (stubForge) Comment(context.Context, string, string, int, string) (int64, error) { return 1, nil } +func (stubForge) CloseIssue(context.Context, string, string, int, string) error { return nil } +func (stubForge) CreatePR(context.Context, string, string, string, string, string, string) (int, error) { + return 1, nil +} +func (stubForge) SetStateLabel(context.Context, string, string, int, string, []string) {} +func (stubForge) AcceptInvitations(context.Context) error { panic("unexpected call") } +func (stubForge) AssignedIssues(context.Context, string, string, string) ([]forge.Issue, error) { + panic("unexpected call") +} +func (stubForge) IssuesWithLabel(context.Context, string, string, string) ([]forge.Issue, error) { + panic("unexpected call") +} +func (stubForge) RepliesAfter(context.Context, string, string, int, int64, string) ([]forge.Reply, error) { + panic("unexpected call") +} +func (stubForge) PRState(context.Context, string, string, int) (string, error) { + panic("unexpected call") +} +func (stubForge) CreateIssue(context.Context, string, string, string, string, []string) (int, error) { + panic("unexpected call") +} +func (stubForge) React(context.Context, string, string, int, string) error { panic("unexpected call") } +func (stubForge) VerifyPush(context.Context, string, string) error { panic("unexpected call") } + +func startEmbeddedNATS(t *testing.T) *server.Server { + t.Helper() + srv := natssrv.RunRandClientPortServer() + t.Cleanup(srv.Shutdown) + return srv +} + +func TestRegisterTaskRPCServersReachableFromClient(t *testing.T) { + srv := startEmbeddedNATS(t) + url := srv.ClientURL() + + serverConn, err := natsio.Connect(url) + if err != nil { + t.Fatalf("nats connect: %v", err) + } + t.Cleanup(serverConn.Close) + + st := store.OpenTest(t) + trees := &worktree.Manager{WorkDir: t.TempDir()} + log := slog.New(slog.DiscardHandler) + + unsubscribe, err := registerTaskRPCServers(serverConn, st, stubForge{}, trees, log) + if err != nil { + t.Fatalf("registerTaskRPCServers: %v", err) + } + t.Cleanup(unsubscribe) + + clientConn, err := natsio.Connect(url) + if err != nil { + t.Fatalf("nats connect: %v", err) + } + t.Cleanup(clientConn.Close) + + ctx := context.Background() + + storeClient := &storerpc.Client{Conn: clientConn, Timeout: 2 * time.Second} + if _, err := st.EnqueueIssue(ctx, "sam", "archie", 1, "t", "b", ""); err != nil { + t.Fatalf("enqueue: %v", err) + } + task, err := st.ClaimNext(ctx) + if err != nil || task == nil { + t.Fatalf("claim: (%v, %v)", task, err) + } + if err := storeClient.Transition(ctx, task.ID, store.StatusRunning, store.StatusPROpen, "opened"); err != nil { + t.Fatalf("storerpc Transition unreachable: %v", err) + } + + forgeClient := &forgerpc.Client{Conn: clientConn, Timeout: 2 * time.Second} + if _, err := forgeClient.Comment(ctx, "sam", "archie", 1, "hi"); err != nil { + t.Fatalf("forgerpc Comment unreachable: %v", err) + } + + treesClient := &worktreerpc.Client{Conn: clientConn, Timeout: 2 * time.Second} + if _, _, err := treesClient.Prepare(ctx, "sam", "archie", "main", 1, "feat: x", "", ""); err == nil { + t.Fatal("expected Prepare to fail against a non-git remote, proving the RPC round-tripped") + } else if err.Error() == "" { + t.Fatal("expected a non-empty error from the worktreerpc round trip") + } +} + 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 {