package archied import ( "context" "fmt" "log/slog" "os" "strconv" "strings" "sync" "time" "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/infrastructure/artifactsync" "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 // artifacts publishes the completed review to the collaborative editor. // nil = publishing is disabled (no [artifacts] base_url configured). artifacts artifactsync.Sender // currentMaxSteps lets the composition root apply live control-plane // versions to reviews that have not started yet. Tests and direct // constructions retain maxSteps as the fallback. currentMaxSteps func() 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) } maxSteps := r.maxSteps if r.currentMaxSteps != nil { maxSteps = r.currentMaxSteps() } report := reviewer.Review(ctx, workflow.ReviewRequest{ SnapshotDir: snapshotDir, Diff: diff, IssueText: prIssueText(pr), MaxSteps: maxSteps, }) r.logReview(key, model, report) result := mapReviewResult(repo, number, model, report) return r.attachArtifact(ctx, result, identity, owner, name, number, 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) } // attachArtifact publishes the report when the sender is configured and // records the editor deep link on the result. Publishing never fails the // review: a failure degrades to an empty URL. func (r *prReviewer) attachArtifact(ctx context.Context, result gateway.PRReviewResult, identity *string, owner, name string, number int, report workflow.ReviewReport) gateway.PRReviewResult { result.ArtifactURL = r.publishArtifact(ctx, reviewActor(identity), owner, name, number, report) return result } // reviewActor resolves the attribution: the requesting chat identity when one // exists, else the daemon's operator-reviewer actor. func reviewActor(identity *string) string { if identity != nil && *identity != "" { return *identity } return "system:pr-reviewer" } // publishArtifact renders the report and hands it to the collaborative // editor when artifact publishing is configured. It returns the editor deep // link, or "" when publishing is disabled or failed: a publish failure is a // logged degradation, never an error that fails the producing review. func (r *prReviewer) publishArtifact(ctx context.Context, actor, owner, name string, number int, report workflow.ReviewReport) string { if r.artifacts == nil { return "" } title := fmt.Sprintf("Review of %s/%s#%d", owner, name, number) artifact := artifactsync.Artifact{ Title: title, Source: report.RenderMarkdown(title), Meta: artifactsync.Meta{ Format: "markdown", Category: "pr-review", Visibility: "account-only", Egress: "none", Actor: actor, Origin: "pr-review", RequestID: "artifact:pr-review:" + owner + "/" + name + "#" + strconv.Itoa(number), Version: "r1", SentAt: time.Now().UTC(), Task: &artifactsync.Task{Owner: owner, Repo: name, Number: number}, }, } url, err := r.artifacts.Publish(ctx, artifact) if err != nil { if r.log != nil { r.log.Warn("artifact publish failed; the review result is unaffected", "artifact", artifact.RequestID(), "err", err) } return "" } if r.log != nil { r.log.Info("artifact published", "artifact", artifact.RequestID(), "url", url) } return url } // 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 } maxSteps := b.cfg.Budgets.MaxSteps if b.cfgHolder != nil { maxSteps = b.cfgHolder.Get().Budgets.MaxSteps } review := &prReviewer{ forge: forgeReader, trees: b.trees, models: b.cfg.Models, providers: executionProviders(b.cfg), maxSteps: maxSteps, artifacts: b.artifactSender(), allowed: buildReviewAllowlist(b.cfg), log: b.log, } if b.cfgHolder != nil { review.currentMaxSteps = func() int { return b.cfgHolder.Get().Budgets.MaxSteps } } b.chatPRReviewer = review return b.chatPRReviewer } // artifactSender builds the collaborative-editor sender when the [artifacts] // section configures one. An empty base_url disables publishing entirely; a // base_url with an unresolvable token is a missing credential, which disables // the capability with a loud warning rather than stopping the daemon // (docs/architecture/configuration.md, "A missing credential disables a // capability"). func (b *boot) artifactSender() artifactsync.Sender { cfg := b.cfg.Artifacts if cfg.BaseURL == "" { return nil } token, err := b.secrets.Resolve(cfg.Token) if err != nil { b.log.Warn("artifact publishing disabled: the service credential could not be resolved", "base_url", cfg.BaseURL, "token_engine", cfg.Token.Engine, "err", err) return nil } if token == "" { b.log.Warn("artifact publishing disabled: the service token resolved empty", "base_url", cfg.BaseURL, "token_engine", cfg.Token.Engine) return nil } return artifactsync.NewClient(cfg.BaseURL, token) }