diff --git a/docs/prds/operator-pr-review.md b/docs/prds/operator-pr-review.md index b5d17e962..8002be03f 100644 --- a/docs/prds/operator-pr-review.md +++ b/docs/prds/operator-pr-review.md @@ -50,18 +50,20 @@ planner/builder. 4. **Command.** A gateway tool `review_pr` (toolset `tasks`) bound to the channel's identity at construction โ€” the model never names an identity, - matching `task_spawn`/`task_action`. The daemon supplies `ChatPRReviewer`, - a narrow gateway interface; the daemon adapter maps `workflow.ReviewReport` - into a gateway-owned result shape (gateway states what it needs, the daemon - adapts โ€” the `ChatTaskActor` pattern). + matching `task_spawn`/`task_action`. The Gateway process serves it: it + composes the worktree manager it needs to materialise the PR head, and the + composition root supplies `ChatPRReviewer`, a narrow gateway interface + whose adapter maps `workflow.ReviewReport` into a gateway-owned result + shape (gateway states what it needs, the composition root adapts โ€” the + `ChatTaskActor` pattern). 5. **Authorization.** Identity-scoped repository allow-list, the `task_spawn` rule: a chat identity may only review a repo in its `TaskProfile.Repos`. `identity == nil` denotes an authenticated dashboard operator who may review any configured repository. -6. **Determinism.** One review per (owner, repo, number) at a time: the daemon - holds an in-flight set; a concurrent request for the same PR returns +6. **Determinism.** One review per (owner, repo, number) at a time: the + Gateway holds an in-flight set; a concurrent request for the same PR returns "review already in progress" rather than launching a second reviewer and racing on the snapshot. diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 0f7b38e28..e88035d7c 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -811,6 +811,27 @@ func (b *boot) loadPlugins() error { return nil } +// buildWorktreeManager composes the process-wide worktree manager from the +// resolved primary forge credential and the loaded config. Both process roots +// own one: the daemon works tasks in it, and the Gateway materialises a pull +// request head in it when an operator asks for a review (boot.prReviewer +// refuses to build a reviewer without one, so a Gateway that skips this step +// advertises no review_pr). It reads only, so it has nothing to report. +// +// Identity managers are not built here: buildTreesAndIdentities builds one per +// identity, each in its own WorkDir, because the Gateway must not own daemon +// identity runners. +func (b *boot) buildWorktreeManager() { + cfg := b.cfg + b.trees = &worktree.Manager{ + WorkDir: cfg.WorkDir, + Token: b.token, + BotUser: cfg.BotUser, + BotEmail: cfg.BotEmail, + BaseURL: cfg.Forge.Host, + } +} + // buildTreesAndIdentities composes the worktree manager and the // multi-identity runners. Each configured identity gets its own forge // client (its own token, possibly its own forge type/host) and its own @@ -820,13 +841,7 @@ func (b *boot) loadPlugins() error { // unchanged. func (b *boot) buildTreesAndIdentities(ctx context.Context) error { cfg, log := b.cfg, b.log - b.trees = &worktree.Manager{ - WorkDir: cfg.WorkDir, - Token: b.token, - BotUser: cfg.BotUser, - BotEmail: cfg.BotEmail, - BaseURL: cfg.Forge.Host, - } + b.buildWorktreeManager() for _, idCfg := range cfg.Identities { // Same reasoning as the primary forge: one identity whose credential is diff --git a/internal/app/archied/composition_order_test.go b/internal/app/archied/composition_order_test.go index 1f19edacf..229159e75 100644 --- a/internal/app/archied/composition_order_test.go +++ b/internal/app/archied/composition_order_test.go @@ -32,3 +32,29 @@ func TestStartGatewayRuntimeSetsUpMemoryBeforeGatewayChat(t *testing.T) { t.Fatal("startGatewayRuntime wires setupGatewayChat before setupMemoryEngine; the turn runner would capture a nil memory engine") } } + +// TestStartGatewayRuntimeBuildsWorktreeManagerBeforeGatewayChat pins the +// Gateway's own worktree-manager ordering. The Gateway owns PR review, so +// startGatewayRuntime must compose the process-wide worktree manager before +// setupGatewayChat resolves boot.prReviewer: prReviewer returns nil without a +// manager, and a nil reviewer omits review_pr from every channel's tool set +// (internal/gateway/review_tools.go). The Gateway deliberately does not call +// buildTreesAndIdentities -- that also registers daemon identity runners it +// must not own -- so nothing at runtime connects the manager to this +// composition phase except the call this asserts on. +func TestStartGatewayRuntimeBuildsWorktreeManagerBeforeGatewayChat(t *testing.T) { + fileset := token.NewFileSet() + file, err := parser.ParseFile(fileset, "gateway.go", nil, 0) + if err != nil { + t.Fatal(err) + } + run := methodBody(t, file, "startGatewayRuntime") + trees := methodCallPosition(run, "buildWorktreeManager") + chat := methodCallPosition(run, "setupGatewayChat") + if trees == token.NoPos || chat == token.NoPos { + t.Fatalf("startGatewayRuntime call positions: buildWorktreeManager=%v setupGatewayChat=%v, want both present", trees, chat) + } + if chat < trees { + t.Fatal("startGatewayRuntime wires setupGatewayChat before buildWorktreeManager; the Gateway would resolve a nil reviewer and never advertise review_pr") + } +} diff --git a/internal/app/archied/gateway.go b/internal/app/archied/gateway.go index 5363e5107..7e74fb14c 100644 --- a/internal/app/archied/gateway.go +++ b/internal/app/archied/gateway.go @@ -142,6 +142,14 @@ func (b *boot) startGatewayRuntime(ctx context.Context, actor gateway.ChatTaskAc b.addCleanup(b.bus.Close) b.capabilityHost = plugin.NewHost() b.startRateLimiter(ctx, b.cfg.Chat.RateLimit) + // The Gateway owns PR review (docs/prds/operator-pr-review.md ยง4), so it + // composes the process-wide worktree manager before setupGatewayChat + // resolves boot.prReviewer: prReviewer returns nil without a manager, and + // a nil reviewer omits review_pr from every channel's tool set. The + // manager is the only part of the daemon's tree composition the Gateway + // takes -- it must not own identity runners, so it does not call + // buildTreesAndIdentities. + b.buildWorktreeManager() // setupMemoryEngine must run before setupGatewayChat: setupGatewayChat // constructs the turn runner, which captures b.memEngines at // construction time (see the identical ordering note in main.go's Run). diff --git a/internal/app/archied/worktree_manager_composition_test.go b/internal/app/archied/worktree_manager_composition_test.go new file mode 100644 index 000000000..094b3a900 --- /dev/null +++ b/internal/app/archied/worktree_manager_composition_test.go @@ -0,0 +1,100 @@ +package archied + +import ( + "log/slog" + "path/filepath" + "testing" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/forge" + "github.com/samcharles93/archie-core/internal/gateway" + "github.com/samcharles93/archie-core/internal/secret" +) + +// TestGatewayWorktreeManagerAdvertisesReviewPR composes the Gateway's own +// tree step with the reviewer and the review tool: whatever +// buildWorktreeManager produces must be enough for boot.prReviewer to yield a +// reviewer and for ReviewTools to advertise review_pr. The call-order +// assertion in composition_order_test.go proves the Gateway makes this step; +// this proves the step is sufficient, so a manager built without the forge +// credential or the configured work dir cannot pass as wired. +func TestGatewayWorktreeManagerAdvertisesReviewPR(t *testing.T) { + const token = "gateway-forge-token" + cfg := config.Config{ + WorkDir: t.TempDir(), + BotUser: "archie-bot", + BotEmail: "bot@example.test", + Forge: config.Forge{Host: "https://forge.example.test"}, + } + b := &boot{ + cfg: cfg, + cfgHolder: config.NewHolder(cfg), + token: token, + log: slog.New(slog.DiscardHandler), + forgeClient: reviewCapableForge{Forge: forge.NewNoop(slog.New(slog.DiscardHandler))}, + } + + b.buildWorktreeManager() + + if b.trees.WorkDir != cfg.WorkDir { + t.Errorf("worktree manager WorkDir = %q, want the configured %q", b.trees.WorkDir, cfg.WorkDir) + } + if b.trees.BaseURL != cfg.Forge.Host { + t.Errorf("worktree manager BaseURL = %q, want the configured forge host %q", b.trees.BaseURL, cfg.Forge.Host) + } + if b.trees.Token != token { + t.Errorf("worktree manager Token = %q, want the resolved forge token %q", b.trees.Token, token) + } + + reviewer := b.prReviewer() + if reviewer == nil { + t.Fatal("prReviewer() = nil after the Gateway's worktree-manager step; review_pr would never be advertised") + } + if !hasReviewPRTool(gateway.ReviewTools(reviewer, "archie")) { + t.Fatal("review_pr not advertised after the Gateway's worktree-manager step") + } +} + +// TestBuildTreesAndIdentitiesBuildsRootManagerAndIdentityRunners pins the +// daemon's tree composition, which the shared buildWorktreeManager must not +// change: the daemon still owns the process-wide manager *and* one runner per +// configured identity, each with its own WorkDir so concurrent identities +// never clone into the same directory. The Gateway takes only the manager, so +// this is the assertion that keeps the two roots apart. +func TestBuildTreesAndIdentitiesBuildsRootManagerAndIdentityRunners(t *testing.T) { + workDir := t.TempDir() + cfg := config.Config{ + WorkDir: workDir, + BotUser: "archie-bot", + BotEmail: "bot@example.test", + Forge: config.Forge{Host: "https://forge.example.test"}, + Identities: []config.IdentityConfig{{ + Name: "archie", + BotUser: "archie-identity", + }}, + } + b := &boot{ + cfg: cfg, + token: "daemon-forge-token", + log: slog.New(slog.DiscardHandler), + secrets: secret.NewRegistry(), + } + + if err := b.buildTreesAndIdentities(t.Context()); err != nil { + t.Fatalf("buildTreesAndIdentities = %v", err) + } + + if b.trees == nil { + t.Fatal("b.trees = nil; the daemon would work no task") + } + if b.trees.WorkDir != workDir || b.trees.Token != b.token { + t.Errorf("root worktree manager = {WorkDir:%q Token:%q}, want {%q %q}", b.trees.WorkDir, b.trees.Token, workDir, b.token) + } + if got := len(b.identityRunners); got != 1 { + t.Fatalf("identityRunners = %d, want 1", got) + } + wantIdentityDir := filepath.Join(workDir, "identity-archie") + if got := b.identityRunners[0].Trees.WorkDir; got != wantIdentityDir { + t.Errorf("identity worktree manager WorkDir = %q, want %q", got, wantIdentityDir) + } +}