Files
archie/internal/app/archied/pr_review.go
T
sam 372f80628a fix(archied): stop advertising review_pr when no reviewer can run
review_pr was registered on every channel and failed on every call with
"worktree manager is not configured". Two independent causes, one contract:

The daemon ran setupGateways before buildTreesAndIdentities, so setupGateways
reached prReviewer() while b.trees was still nil and captured it; the memoised
reviewer then held that nil for the process lifetime. Build the worktree
manager before the gateways, which is the ordering the reviewer depends on.

Ordering alone cannot fix the standalone Gateway, which calls prReviewer() and
never builds trees at all. Have prReviewer() return nil when b.trees is nil, so
a reviewer that can never run is not produced. That is what
gateway.ReviewTools' existing nil-reviewer guard always intended -- "a tool that
cannot run must not be advertised" -- but the guard never fired because the
daemon never passed nil.

TestPrReviewerIsMemoized asserted a trees-less boot yields a non-nil cached
reviewer, encoding the broken precondition as expected behaviour, which is why
a green suite hid this since review_pr shipped. It now gives the boot a real
worktree manager so its actual subject -- one shared in-flight set across
channels -- stays pinned, and two tests are added: one for the nil-trees
invariant, and one that composes the real wiring (prReviewer -> ReviewTools)
and asserts review_pr is registered only when the reviewer can run.

Found by an adversarial package sweep (temp tracker: app-archied-5,
.local/issue-tracker/).
2026-09-15 02:08:46 +10:00

260 lines
8.0 KiB
Go

package archied
import (
"context"
"fmt"
"log/slog"
"os"
"strconv"
"strings"
"sync"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/app/agentworker"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/forge"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/worktree"
)
// prReviewer implements gateway.ChatPRReviewer for the daemon. It fetches
// an existing PR's metadata, materialises its head in a fresh clone, and runs
// the same adversarial reviewer the workflow stage uses, all synchronously in
// the requesting turn. Authorization is identity-scoped (the task_spawn
// repository allow-list) and one review per PR runs at a time.
type prReviewer struct {
forge forge.PullRequestReader // nil = forge cannot read PRs
trees *worktree.Manager
models map[string]string
providers map[string]agentexec.Provider
maxSteps int
// allowed maps a chat identity to the "owner/name" repos it may review.
// A nil identity is an authenticated dashboard operator, who may review
// any configured repository.
allowed map[string]map[string]bool
log *slog.Logger
mu sync.Mutex
inflight map[string]struct{}
}
var _ gateway.ChatPRReviewer = (*prReviewer)(nil)
// ReviewPR fetches the PR, runs the isolated reviewer against its head, and
// returns the structured findings. It never half-reviews: a missing forge
// capability, an unresolvable head/base, or an absent reviewer model returns
// an error rather than a fabricated result.
func (r *prReviewer) ReviewPR(ctx context.Context, identity *string, repo string, number int) (gateway.PRReviewResult, error) {
owner, name, err := splitOwnerRepo(repo)
if err != nil {
return gateway.PRReviewResult{}, err
}
if err := r.authorize(identity, owner, name); err != nil {
return gateway.PRReviewResult{}, err
}
key := owner + "/" + name + "#" + strconv.Itoa(number)
if !r.begin(key) {
return gateway.PRReviewResult{}, fmt.Errorf("review already in progress for %s", key)
}
defer r.end(key)
if r.forge == nil {
return gateway.PRReviewResult{}, fmt.Errorf("forge does not support pull request review")
}
pr, err := r.forge.GetPullRequest(ctx, owner, name, number)
if err != nil {
return gateway.PRReviewResult{}, err
}
model := agentworker.ReviewerModel(r.models)
if model == "" {
return gateway.PRReviewResult{}, fmt.Errorf("reviewer model is not configured (set models.reviewer, or models.builder as a fallback)")
}
reviewer := agentworker.NewOperatorReviewer(r.models, r.providers)
if reviewer == nil {
return gateway.PRReviewResult{}, fmt.Errorf("no provider runtime configured for the reviewer model %q", model)
}
if r.trees == nil {
return gateway.PRReviewResult{}, fmt.Errorf("worktree manager is not configured")
}
dir, cleanup, err := r.trees.CheckoutPR(ctx, owner, name, pr.HeadRef, pr.BaseRef)
if err != nil {
return gateway.PRReviewResult{}, err
}
defer cleanup()
diff, err := r.trees.Diff(ctx, dir, pr.BaseRef)
if err != nil {
return gateway.PRReviewResult{}, fmt.Errorf("compute PR diff: %w", err)
}
snapshotDir, err := os.MkdirTemp("", "pr-review-snapshot-*")
if err != nil {
return gateway.PRReviewResult{}, fmt.Errorf("create review snapshot directory: %w", err)
}
defer os.RemoveAll(snapshotDir)
if err := r.trees.Snapshot(ctx, dir, snapshotDir); err != nil {
return gateway.PRReviewResult{}, fmt.Errorf("snapshot PR head: %w", err)
}
report := reviewer.Review(ctx, workflow.ReviewRequest{
SnapshotDir: snapshotDir,
Diff: diff,
IssueText: prIssueText(pr),
MaxSteps: r.maxSteps,
})
r.logReview(key, model, report)
return mapReviewResult(repo, number, model, report), nil
}
// authorize enforces the identity-scoped repository allow-list. A nil
// identity (dashboard operator) may review any configured repository.
func (r *prReviewer) authorize(identity *string, owner, repo string) error {
full := owner + "/" + repo
if identity == nil {
for _, set := range r.allowed {
if set[full] {
return nil
}
}
return fmt.Errorf("repository %q is not configured", full)
}
set, ok := r.allowed[*identity]
if !ok {
return fmt.Errorf("identity %q is not configured for PR review", *identity)
}
if !set[full] {
return fmt.Errorf("repository %q is not configured for this identity", full)
}
return nil
}
func (r *prReviewer) begin(key string) bool {
r.mu.Lock()
defer r.mu.Unlock()
if r.inflight == nil {
r.inflight = make(map[string]struct{})
}
if _, ok := r.inflight[key]; ok {
return false
}
r.inflight[key] = struct{}{}
return true
}
func (r *prReviewer) end(key string) {
r.mu.Lock()
defer r.mu.Unlock()
delete(r.inflight, key)
}
func (r *prReviewer) logReview(key, model string, report workflow.ReviewReport) {
if r.log == nil {
return
}
blocking := 0
for _, f := range report.Findings {
if f.Blocking() {
blocking++
}
}
r.log.Info("operator PR review complete",
"pr", key, "model", model, "status", string(report.Status),
"findings", len(report.Findings), "blocking", blocking)
}
// splitOwnerRepo splits "owner/name" with the same validation the gateway's
// task_spawn allow-list uses.
func splitOwnerRepo(s string) (owner, repo string, err error) {
owner, repo, ok := strings.Cut(s, "/")
if !ok || owner == "" || repo == "" {
return "", "", fmt.Errorf("repository %q must be owner/name", s)
}
return owner, repo, nil
}
// prIssueText renders the PR title/body as the review's issue text.
func prIssueText(pr forge.PullRequest) string {
if pr.Body == "" {
return pr.Title
}
return pr.Title + "\n\n" + pr.Body
}
// mapReviewResult adapts a workflow.ReviewReport into the gateway's
// channel-neutral result shape.
func mapReviewResult(repo string, number int, model string, report workflow.ReviewReport) gateway.PRReviewResult {
out := gateway.PRReviewResult{
Repo: repo,
Number: number,
Model: model,
Status: string(report.Status),
Summary: report.Summary,
Reason: report.SkipReason,
Findings: make([]gateway.PRReviewFinding, 0, len(report.Findings)),
}
for _, f := range report.Findings {
out.Findings = append(out.Findings, gateway.PRReviewFinding{
File: f.File,
Line: f.Line,
Defect: f.Defect,
FailureScenario: f.FailureScenario,
Verdict: string(f.Verdict),
Level: string(f.Level),
Category: string(f.Category),
Blocking: f.Blocking(),
})
}
return out
}
// buildReviewAllowlist derives the identity-scoped review allow-list from the
// same profiles task_spawn uses, so a chat identity and the review command
// agree on which repositories the identity may touch.
func buildReviewAllowlist(cfg config.Config) map[string]map[string]bool {
profiles, _ := chatTaskProfiles(cfg)
out := make(map[string]map[string]bool, len(profiles))
for _, p := range profiles {
set := make(map[string]bool, len(p.Repos))
for _, repo := range p.Repos {
set[repo] = true
}
out[p.Identity] = set
}
return out
}
// prReviewer assembles the operator-triggered review capability, or nil when
// the forge cannot read PRs (the noop forge, or a forge with no PR read
// support). A review tool that can never run must not be advertised, matching
// the TaskTools nil-backend rule.
//
// The instance is built once and memoized on the boot struct so every channel
// shares one in-flight set: a review of the same PR requested concurrently
// from the Web UI and Telegram deduplicates instead of running twice.
func (b *boot) prReviewer() gateway.ChatPRReviewer {
if b.chatPRReviewer != nil {
return b.chatPRReviewer
}
if b.trees == nil {
return nil
}
forgeReader, _ := b.forgeClient.(forge.PullRequestReader)
if forgeReader == nil {
return nil
}
b.chatPRReviewer = &prReviewer{
forge: forgeReader,
trees: b.trees,
models: b.cfg.Models,
providers: executionProviders(b.cfg),
maxSteps: b.cfg.Budgets.MaxSteps,
allowed: buildReviewAllowlist(b.cfg),
log: b.log,
}
return b.chatPRReviewer
}