feat(workflow): remove the superseded adversarial-self-review system

docs/prds/pr-review-agent.md's own header already declared this
superseded (docs/architecture/adversarial-review.md, docs/prds/
operator-pr-review.md, docs/prds/review-calibration.md); this executes
that removal per the maintainer's explicit decision, so no reference
to the old path remains:

- internal/domain/workflow/review.go, review_findings.go,
  review_report_md.go and their tests: Reviewer, ReviewRequest,
  StageReview, StagePostReviewComments, ReviewReport/ReviewFinding and
  friends. ReviewComment relocated into workflow.go (still used by
  Forger.CreateReviewComments and the new pr-review pipeline).
- internal/app/agentworker/review.go: the ai-sdk-backed subagentReviewer.
- internal/app/archied/pr_review.go: the synchronous operator-review
  path (prReviewer, gateway.ChatPRReviewer wiring). The review_pr chat
  tool now self-omits (nil reviewer) until the operator trigger is
  rebuilt to enqueue an async pr-review task instead of reviewing
  synchronously in the chat turn.
- config.Repo.ReviewEnabled and its dashboard exposure.
- Two composition-order tests and one dashboard test that existed only
  to pin the deleted path.

Also splits PRReview()'s Stage list into prReviewDecisionStages()
(phases 1-8) and stagePROutput() (phase 9), so a future caller can
splice the decision phases before a PR exists and defer posting until
after -- needed for the "archie's own PRs" trigger, not wired here: it
needs the same stage-loop-around-StageOpenPR semantics this pass
couldn't fully verify in the time available (does an Outcome-setting
stage like StageOpenPR end the compiled-workflow-definition engine the
same way it ends workflow.Run's literal loop?). Carries ScoredFinding.
Adversary through synthesis so that trigger's blocking-finding check
doesn't need to cross-reference back into the pre-synthesis slice.

Not built in this pass, left as a precise continuation (see the bead
comment): the watched-repositories and operator triggers, both blocked
on the same real question -- how a forge-reading credential reaches
PRSource's real implementation when the pr-review pipeline's Stage.Run
bodies execute inside the sandboxed archie-agent process, which holds
none today.
This commit is contained in:
sam committed 2026-09-28 12:55:05 +10:00
1 parent 697971bbcd
commit d73e0748c8
31 files changed
+100 -3761

No files matched your search

+6 -4
View File
@@ -341,10 +341,12 @@ StateStoreGrantIssuer` (`staterpc.GrantIssuer`) registers a fresh
separate fresh-context reviewer pass on every change as a matter of course.
Red-green TDD plus `task check` is the gate; a manual adversarial pass is
opt-in per request — ask before running one. This is the rule for a human or
agent _contributor_. Separately, archied runs the same adversarial review
automatically on the PRs it opens (gated per repo by `repo.review_enabled`)
— see `docs/architecture/adversarial-review.md`. The two paths are distinct:
you run one when asked; archied runs one before it opens a PR.
agent _contributor_. Separately, archied runs its own PR review pipeline
(`docs/prds/pr-review-agent.md`) before it opens a PR of its own — see that
PRD's Triggers section ("archie's own PRs"). It supersedes the older
single-reviewer stage `docs/architecture/adversarial-review.md` described
(`repo.review_enabled` no longer exists). The two paths are distinct: you
run a manual pass when asked; archied runs its own before it opens a PR.
6. **Linter Guard:**
- When using `errorlint` fixes, ensure boolean predicates (e.g.
-3
View File
@@ -496,9 +496,6 @@ target = "127.0.0.1:9090" # REQUIRED. The State Store gRPC address arch
# # [containers].max_concurrency globally.
# # persistent_storage = true # reuse a per-repo Docker volume and Git object cache until volume_ttl
# # max_retries = 5 # per-repo override of the global max_retries above
# # review_enabled = true # run a fresh adversarial reviewer (no path to the implementer's
# # context) after gates pass and before the PR opens. Default false --
# # an uncalibrated reviewer that cries wolf is worse than no review.
# ── Tools ───────────────────────────────────────────────────────────────────
# Limits on what a tool may return. These bound how much of the context window
-272
View File
@@ -1,272 +0,0 @@
package agentworker
import (
"context"
"fmt"
"strings"
"github.com/samcharles93/ai-sdk/agent"
"github.com/samcharles93/ai-sdk/core"
"github.com/samcharles93/ai-sdk/runtime"
"github.com/samcharles93/ai-sdk/toolkit"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/taskrun"
)
// newReviewerFor builds the workflow.Reviewer for one task run, or nil
// when the repo has not opted into review (Repo.ReviewEnabled == false)
// or no providers are configured. Model resolution mirrors AgentStage's
// own role->"builder" fallback (agent.go): Models["reviewer"], falling
// back to Models["builder"].
func newReviewerFor(req taskrun.Request) workflow.Reviewer {
if !req.Repo.ReviewEnabled {
return nil
}
return NewOperatorReviewer(req.Cfg.Models, req.Providers)
}
// NewOperatorReviewer builds the workflow.Reviewer for an operator-triggered
// review, resolved from daemon-level models and providers rather than a task
// run. It shares newReviewerFor's role->"builder" model fallback but drops
// the Repo.ReviewEnabled gate: the operator asked for the review explicitly,
// so "not opted in" is not a reason to refuse. It returns nil when no model
// resolves (no reviewer/builder model configured) or no provider runtime
// builds, so the caller can report a clear "review not configured" result.
func NewOperatorReviewer(models map[string]string, providers map[string]agentexec.Provider) workflow.Reviewer {
modelRef := ReviewerModel(models)
if modelRef == "" {
return nil
}
rt := agentexec.NewRuntime(providers)
if rt == nil {
return nil
}
return newSubagentReviewer(rt, modelRef)
}
// ReviewerModel resolves the reviewer role's model with the builder fallback.
// It is the single source of truth for the role->"builder" rule, shared by
// the workflow stage (via NewOperatorReviewer/newReviewerFor) and the
// operator-triggered review's result reporting so the reported model and the
// model actually run cannot diverge.
func ReviewerModel(models map[string]string) string {
if models["reviewer"] != "" {
return models["reviewer"]
}
return models["builder"]
}
// defaultReviewMaxSteps bounds the reviewer's tool loop when neither
// ReviewRequest.MaxSteps nor the repo's configured budget set one.
const defaultReviewMaxSteps = 10
// subagentReviewer implements workflow.Reviewer using ai-sdk's
// agent.Subagent: a nested, synchronous generation in this same worker
// process, with its own model, system prompt, toolset, and step budget.
//
// Isolation is two-fold. Conversation isolation is structural: Subagent.Run
// only ever sends a prompt string, never the implementer's message
// history. Workspace isolation is built by the caller (StageReview via
// workflow.Trees.Snapshot): reviewerToolSet is rooted at that snapshot
// directory and holds only read-only tools -- never the worker's own
// toolset, which carries the worktreerpc publication grant a reviewer must
// not be able to reach.
type subagentReviewer struct {
runtime *runtime.Runtime
modelRef string
}
// newSubagentReviewer constructs a Reviewer that runs on modelRef, resolved
// through rt (the same provider runtime the worker's own agent runner
// uses). rt must be non-nil.
func newSubagentReviewer(rt *runtime.Runtime, modelRef string) *subagentReviewer {
return &subagentReviewer{runtime: rt, modelRef: modelRef}
}
var _ workflow.Reviewer = (*subagentReviewer)(nil)
// Review runs one adversarial review. It never returns a Go error: every
// failure mode (unresolvable model, a Subagent.Run error, a truncated
// run) folds into the returned ReviewReport, per
// docs/prds/adversarial-self-review.md section 4's fail-closed contract.
func (s *subagentReviewer) Review(ctx context.Context, req workflow.ReviewRequest) workflow.ReviewReport {
if s.runtime == nil {
return workflow.NewNotRunReviewReport("no provider runtime configured for the reviewer")
}
provider, model, err := s.runtime.ChatProvider(ctx, s.modelRef)
if err != nil {
return workflow.NewNotRunReviewReport(fmt.Sprintf("resolve reviewer model %q: %v", s.modelRef, err))
}
var findings []workflow.ReviewFinding
var checks []workflow.ReviewCheck
tools := reviewerToolSet(req.SnapshotDir, &findings, &checks)
maxSteps := req.MaxSteps
if maxSteps <= 0 {
maxSteps = defaultReviewMaxSteps
}
sub := agent.Subagent{
Provider: provider,
Model: model,
System: reviewerSystemPrompt,
Tools: tools,
MaxSteps: maxSteps,
}
summary, runErr := sub.Run(ctx, reviewerPrompt(req))
if runErr != nil {
// A truncated or errored run still yields whatever the reviewer
// captured before it ran out of budget -- those findings are real
// and must not be discarded just because the run's own
// conclusion never arrived (docs/prds/adversarial-self-review.md
// section 1, "findings return through a closure"). Zero captured
// findings on error means the reviewer never got anywhere, which
// fail-closed treats as not having run at all.
if len(findings) == 0 {
return workflow.NewNotRunReviewReport(runErr.Error())
}
return completedReview(findings, checks, "")
}
return completedReview(findings, checks, summary)
}
// completedReview builds a completed report carrying the captured findings
// and the properties the reviewer verified clean.
func completedReview(findings []workflow.ReviewFinding, checks []workflow.ReviewCheck, summary string) workflow.ReviewReport {
report := workflow.NewCompletedReviewReport(findings, summary)
report.Checked = checks
return report
}
// reviewerSystemPrompt instructs the reviewer per CLAUDE.md's own
// adversarial-review discipline: assume every line is wrong until proven
// correct, work the checklist, report only what a fresh reader can verify
// from what it was given.
const reviewerSystemPrompt = `You are a fresh adversarial code reviewer. You did not write this change and have no access to how or why it was written -- only what is given to you below.
Assume every line is wrong until you have verified it is correct. Read whole files with your read tool, not just diff hunks, before concluding a call site or interface is satisfied. Use grep/find to check for other call sites a change affects.
Check for: dead code, unchecked errors, hardcoded values that should be parameters, interface-satisfaction defects, nil-pointer risk, goroutine leaks, races and unsynchronized shared state.
For every defect you find, call record_finding with: the file and line, a one-sentence defect statement, a concrete failure scenario (specific inputs or state that produce the wrong output or a crash -- not a hypothetical), a verdict, a level, and a category.
A finding must describe wrong behaviour or a crash. Style, formatting, naming, and preference nits are out of contract -- do not report them; they are exactly the padding that teaches an operator to skim past a findings list.
As you clear each checklist property, call record_checked with the property and how you verified it (which files or call sites you read). Record every property you actually checked and found correct, even when you also found defects; a review that reports no findings must still record what it checked, because "I checked and it is fine" and "I did not look" must not read the same.
Verdict is "confirmed" only when you have traced the actual failure -- read the code paths involved and can state exactly how it goes wrong. Verdict is "plausible" for a real worry you have not fully traced. Only a "confirmed" finding at level "error" blocks the pull request; do not inflate a plausible worry to confirmed/error to make it count -- an unjustified block is worse than a missed one, because it teaches the operator to stop trusting your reports.
Level is "error" for a defect that blocks; "warn" for something worth knowing but not blocking.
When a confirmed defect's fix is mechanical, and only then, pass suggestion: the exact text that should replace the anchored line, so the author can apply it in one click. It replaces that line exactly, so include the whole line (indentation included) and nothing else, and never guess -- a wrong one-click fix is worse than none. Do not pass a suggestion for a plausible finding, or for a fix you cannot state as a single line.
When you have finished checking, reply with a short summary of what you checked and what you found (or that you found nothing). If you find nothing, say so plainly -- reporting zero findings on properties you actually checked is a valid and expected outcome, not a failure to find something.`
// reviewerPrompt renders the reviewer's mission: the diff and issue text.
// The snapshot itself is not embedded here -- it reaches the reviewer only
// through its read-only toolset, rooted at SnapshotDir, never as prompt
// text the reviewer could mistake for something it already checked.
func reviewerPrompt(req workflow.ReviewRequest) string {
var b strings.Builder
b.WriteString("Review the following change. The full file tree at the reviewed commit is available through your tools; ")
b.WriteString("start by reading the files the diff touches, not just the diff itself.\n\n")
b.WriteString("<issue>\n")
b.WriteString(req.IssueText)
b.WriteString("\n</issue>\n\n<diff>\n")
b.WriteString(req.Diff)
b.WriteString("\n</diff>\n")
return b.String()
}
// reviewerToolSet builds the reviewer's read-only toolset, rooted at
// snapshotDir, plus the structured capture tools that append directly to
// findings and checks. This is a distinct toolset built fresh for each
// review -- never the worker's own registry, which holds write/edit/shell
// tools and the worktreerpc publication grant.
func reviewerToolSet(snapshotDir string, findings *[]workflow.ReviewFinding, checks *[]workflow.ReviewCheck) core.ToolSet {
reg := toolkit.NewRegistry()
readOnly := []toolkit.Tool{
toolkit.NewReadTool(snapshotDir, toolkit.NewReadTracker()),
toolkit.NewGrepTool(snapshotDir),
toolkit.NewFindTool(snapshotDir),
}
for _, tool := range readOnly {
_ = reg.Register(tool) // names are fixed and distinct; Register cannot fail here
}
set := reg.CoreToolSet(toolkit.NonInteractiveBridge{})
set["record_finding"] = recordFindingTool(findings)
set["record_checked"] = recordCheckedTool(checks)
return set
}
// checkInput is the reviewer-facing shape of workflow.ReviewCheck.
type checkInput struct {
Property string `json:"property" jsonschema:"description=The property or code path you verified clean, e.g. 'nil-safety of Foo callers'."`
Evidence string `json:"evidence" jsonschema:"description=How you verified it, e.g. 'read all 4 callers; each nil-checks before use'."`
}
// recordCheckedTool builds the typed capture tool the reviewer calls once per
// property it verified clean. Without it a zero-finding review reads the same
// as a review that never looked; with it, "ran and found nothing" arrives
// backed by what was actually checked.
func recordCheckedTool(checks *[]workflow.ReviewCheck) *core.Tool {
return core.NewTypedTool(
"record_checked",
"Record one property or code path you verified clean. Call this once per property you actually checked and found correct -- a review that reports no findings must still record what it checked.",
func(_ context.Context, in checkInput) (string, error) {
check := workflow.ReviewCheck{Property: in.Property, Evidence: in.Evidence}
if err := check.Validate(); err != nil {
return "record_checked rejected: " + err.Error(), nil //nolint:nilerr // rejection feedback lets the model retry with a valid check
}
*checks = append(*checks, check)
return "check recorded", nil
},
)
}
// findingInput is the reviewer-facing shape of workflow.ReviewFinding: the
// model reports plain strings for Verdict/Level/Category, validated and
// converted before being appended.
type findingInput struct {
File string `json:"file" jsonschema:"description=Repo-relative file path where the defect is located."`
Line int `json:"line" jsonschema:"description=Line number where the defect is located."`
Defect string `json:"defect" jsonschema:"description=One-sentence statement of the defect."`
FailureScenario string `json:"failure_scenario" jsonschema:"description=Concrete inputs or state that produce the wrong output or a crash -- not a hypothetical."`
Suggestion string `json:"suggestion,omitempty" jsonschema:"description=Optional. The exact replacement for the anchored line, when the fix is mechanical and you are certain of it -- it is offered as a one-click apply, so an approximate or partial line is worse than none. Only a confirmed finding may carry one; omit it for a plausible finding. Single line only."`
Verdict string `json:"verdict" jsonschema:"description=Only 'confirmed' may block. Use 'plausible' for a real worry you have not fully traced.,enum=confirmed|plausible"`
Level string `json:"level" jsonschema:"description=error blocks when confirmed; warn is advisory only.,enum=error|warn"`
Category string `json:"category" jsonschema:"enum=dead-code|unchecked-error|hardcoded-value|interface-satisfaction|nil-risk|goroutine-leak|race|other"`
}
// recordFindingTool builds the typed capture tool the reviewer must call
// to report a defect. Rejecting an invalid finding with a plain-text
// message (rather than a Go error) lets the model see what was wrong and
// retry, the same environmental-enforcement pattern the rest of the
// agent-loop tool contract uses.
func recordFindingTool(findings *[]workflow.ReviewFinding) *core.Tool {
return core.NewTypedTool(
"record_finding",
"Report one defect found during review. Call this once per defect; do not summarize multiple defects into one call.",
func(_ context.Context, in findingInput) (string, error) {
finding := workflow.ReviewFinding{
File: in.File,
Line: in.Line,
Defect: in.Defect,
FailureScenario: in.FailureScenario,
Suggestion: in.Suggestion,
Verdict: workflow.ReviewVerdict(in.Verdict),
Level: workflow.ReviewLevel(in.Level),
Category: workflow.ReviewCategory(in.Category),
}
if err := finding.Validate(); err != nil {
return "record_finding rejected: " + err.Error(), nil //nolint:nilerr // rejection feedback lets the model retry with a valid finding
}
*findings = append(*findings, finding)
return "finding recorded", nil
},
)
}
-252
View File
@@ -1,252 +0,0 @@
package agentworker
import (
"context"
"errors"
"os"
"path/filepath"
"slices"
"testing"
"github.com/samcharles93/ai-sdk/agent"
"github.com/samcharles93/ai-sdk/chat"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
// TestReviewerToolSetIsReadOnlyAndHasNoMutationPath asserts, as a
// construction fact rather than a prompt claim, that the reviewer's
// toolset carries no tool capable of writing, editing, or running a
// shell command -- the class of tool that could reach the worktreerpc
// publication grant or otherwise mutate anything. Read/grep/find plus the
// findings-capture tool are the entire surface.
func TestReviewerToolSetIsReadOnlyAndHasNoMutationPath(t *testing.T) {
var findings []workflow.ReviewFinding
var checks []workflow.ReviewCheck
set := reviewerToolSet(t.TempDir(), &findings, &checks)
names := make([]string, 0, len(set))
for name := range set {
names = append(names, name)
}
slices.Sort(names)
want := []string{"find", "grep", "read", "record_checked", "record_finding"}
slices.Sort(want)
if !slices.Equal(names, want) {
t.Fatalf("reviewer toolset = %v, want exactly %v (no write/edit/shell)", names, want)
}
}
// TestReviewerToolSetIsRootedAtTheGivenSnapshot asserts the read tool
// cannot read outside the snapshot directory it was constructed with --
// it has no path back to the implementer's actual worktree.
func TestReviewerToolSetIsRootedAtTheGivenSnapshot(t *testing.T) {
snapshot := t.TempDir()
if err := os.WriteFile(filepath.Join(snapshot, "in-snapshot.txt"), []byte("ok\n"), 0o644); err != nil {
t.Fatal(err)
}
outside := t.TempDir()
if err := os.WriteFile(filepath.Join(outside, "secret.txt"), []byte("nope\n"), 0o644); err != nil {
t.Fatal(err)
}
var findings []workflow.ReviewFinding
var checks []workflow.ReviewCheck
set := reviewerToolSet(snapshot, &findings, &checks)
readTool := set["read"]
if readTool == nil {
t.Fatal("reviewer toolset has no read tool")
}
out, err := readTool.Execute(t.Context(), `{"path":"in-snapshot.txt"}`)
if err != nil {
t.Fatalf("read in-snapshot file: %v", err)
}
if !containsSubstr(out, "ok") {
t.Errorf("read in-snapshot file = %q, want it to contain the file's content", out)
}
// Tool failures are encoded in-band in the returned content, not as a
// Go error (toolkit.Registry.CoreToolSet's own contract) -- an escape
// attempt must reject there, not succeed.
escapeOut, err := readTool.Execute(t.Context(), `{"path":"`+filepath.Join(outside, "secret.txt")+`"}`)
if err != nil {
t.Fatalf("read outside-snapshot path: %v", err)
}
if containsSubstr(escapeOut, "nope") {
t.Errorf("read tool returned content from outside the snapshot: %q", escapeOut)
}
if !containsSubstr(escapeOut, "escapes") {
t.Errorf("read tool output = %q, want a path-confinement rejection", escapeOut)
}
}
// TestRecordFindingToolAppendsToCapturedSlice asserts the capture-tool
// contract: findings are recorded via the closure over the findings
// slice, not returned from Subagent.Run's text.
func TestRecordFindingToolAppendsToCapturedSlice(t *testing.T) {
var findings []workflow.ReviewFinding
tool := recordFindingTool(&findings)
input := `{"file":"main.go","line":10,"defect":"nil deref","failure_scenario":"calling Foo(nil) panics","verdict":"confirmed","level":"error","category":"nil-risk"}`
if _, err := tool.Execute(t.Context(), input); err != nil {
t.Fatalf("record_finding: %v", err)
}
if len(findings) != 1 {
t.Fatalf("captured %d findings, want 1", len(findings))
}
if !findings[0].Blocking() {
t.Error("captured finding is not blocking, want confirmed+error to block")
}
}
// TestRecordFindingToolRejectsInvalidVerdictWithoutError asserts an
// invalid finding is rejected in-band (so the model can retry) rather
// than surfacing as a hard tool error, and is not captured.
func TestRecordFindingToolRejectsInvalidVerdictWithoutError(t *testing.T) {
var findings []workflow.ReviewFinding
tool := recordFindingTool(&findings)
input := `{"file":"main.go","line":1,"defect":"x","failure_scenario":"y","verdict":"maybe","level":"error","category":"other"}`
out, err := tool.Execute(t.Context(), input)
if err != nil {
t.Fatalf("record_finding returned a Go error for invalid input: %v", err)
}
if !containsSubstr(out, "rejected") {
t.Errorf("record_finding output = %q, want a rejection message", out)
}
if len(findings) != 0 {
t.Errorf("captured %d findings, want 0 for an invalid finding", len(findings))
}
}
// TestRecordCheckedToolAppendsToCapturedSlice asserts the calibration capture
// contract: a cleared property is recorded through the closure, so a
// zero-finding review can report what it actually checked.
func TestRecordCheckedToolAppendsToCapturedSlice(t *testing.T) {
var checks []workflow.ReviewCheck
tool := recordCheckedTool(&checks)
input := `{"property":"nil-safety of Foo callers","evidence":"read all 4 callers; each nil-checks"}`
if _, err := tool.Execute(t.Context(), input); err != nil {
t.Fatalf("record_checked: %v", err)
}
if len(checks) != 1 || checks[0].Property != "nil-safety of Foo callers" {
t.Fatalf("captured checks = %+v, want one check for the recorded property", checks)
}
}
// TestRecordCheckedToolRejectsMissingEvidence asserts a check without evidence
// is rejected in-band and not captured, so "verified clean" cannot be recorded
// as a bare assertion.
func TestRecordCheckedToolRejectsMissingEvidence(t *testing.T) {
var checks []workflow.ReviewCheck
tool := recordCheckedTool(&checks)
out, err := tool.Execute(t.Context(), `{"property":"looks fine","evidence":""}`)
if err != nil {
t.Fatalf("record_checked returned a Go error for invalid input: %v", err)
}
if !containsSubstr(out, "rejected") {
t.Errorf("record_checked output = %q, want a rejection message", out)
}
if len(checks) != 0 {
t.Errorf("captured %d checks, want 0 for a check with no evidence", len(checks))
}
}
// TestCompletedReviewCarriesChecks asserts the report the reviewer returns
// carries the captured checks, not just the findings.
func TestCompletedReviewCarriesChecks(t *testing.T) {
checks := []workflow.ReviewCheck{{Property: "p", Evidence: "e"}}
report := completedReview(nil, checks, "summary")
if !report.Ran() {
t.Fatal("completedReview report did not run")
}
if len(report.Checked) != 1 || report.Checked[0].Property != "p" {
t.Fatalf("report.Checked = %+v, want the captured check", report.Checked)
}
}
// TestReviewSubagentConstructionNeverSendsConversationHistory asserts, at
// the exact agent.Subagent construction Review performs, that only a
// prompt string reaches the model. This exercises the same Subagent{}
// shape subagentReviewer.Review builds (provider, model, system prompt,
// reviewerToolSet, MaxSteps) directly against a fake chat.Provider, so it
// proves the construction fact without needing a full *runtime.Runtime:
// Subagent.Run populates GenerateOptions.Prompt and never Messages, so
// there is no code path for the implementer's transcript to travel
// through.
func TestReviewSubagentConstructionNeverSendsConversationHistory(t *testing.T) {
captured := &capturingChatProvider{}
var findings []workflow.ReviewFinding
var checks []workflow.ReviewCheck
req := workflow.ReviewRequest{
SnapshotDir: t.TempDir(),
Diff: "diff --git a/x.go b/x.go\n",
IssueText: "some issue text",
}
sub := agent.Subagent{
Provider: captured,
Model: "fake-model",
System: reviewerSystemPrompt,
Tools: reviewerToolSet(req.SnapshotDir, &findings, &checks),
MaxSteps: defaultReviewMaxSteps,
}
prompt := reviewerPrompt(req)
if _, err := sub.Run(t.Context(), prompt); err != nil {
t.Fatalf("Subagent.Run() error = %v", err)
}
if captured.lastRequest == nil {
t.Fatal("chat provider was never called")
}
// The only non-system message the model ever sees is the one
// Subagent.Run derives from the prompt string -- there is no code
// path here for a second, implementer-authored message to arrive
// alongside it.
var userMessages int
for _, m := range captured.lastRequest.Messages {
if m.Role == chat.RoleUser {
userMessages++
if m.Content != prompt {
t.Errorf("user message content = %q, want the review prompt %q", m.Content, prompt)
}
}
}
if userMessages != 1 {
t.Errorf("chat.Request carried %d user messages, want exactly 1 (the review prompt, no implementer history)", userMessages)
}
}
func containsSubstr(s, substr string) bool {
return len(substr) == 0 ||
(len(s) >= len(substr) && func() bool {
for i := 0; i+len(substr) <= len(s); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}())
}
// capturingChatProvider is a minimal chat.Provider fake that records the
// last chat.Request it received and returns a fixed, valid completion.
type capturingChatProvider struct {
lastRequest *chat.Request
}
func (c *capturingChatProvider) Name() string { return "fake" }
func (c *capturingChatProvider) Chat(_ context.Context, req chat.Request) (chat.Response, error) {
r := req
c.lastRequest = &r
return chat.Response{Content: "no findings", FinishReason: "stop"}, nil
}
func (c *capturingChatProvider) ChatStream(_ context.Context, req chat.Request) (chat.Stream, error) {
return nil, errors.New("not implemented")
}
+17 -19
View File
@@ -217,12 +217,11 @@ func applyToolLimits(agent agentexec.Runner, policy config.ToolPolicy, allow []s
runner.AllowTools = allow
}
// stageRunners builds the runner every agent stage uses, and the reviewer.
func stageRunners(req taskrun.Request, mcpSet *mcpProviderSet, newRunner runnerFactory, log *slog.Logger) (agentexec.Runner, workflow.Reviewer, error) {
// stageRunners builds the runner every agent stage uses.
func stageRunners(req taskrun.Request, mcpSet *mcpProviderSet, newRunner runnerFactory, log *slog.Logger) (agentexec.Runner, error) {
if req.Harness != nil {
// The built-in loop has no route to a model from a Kit container, so
// the review stage parks rather than running on it.
return agentexec.HarnessStages{Runner: agentexec.NewHarnessRunner(nil), Spec: *req.Harness}, nil, nil
// The built-in loop has no route to a model from a Kit container.
return agentexec.HarnessStages{Runner: agentexec.NewHarnessRunner(nil), Spec: *req.Harness}, nil
}
var agent agentexec.Runner
if mcpSet != nil && mcpSet.registry != nil {
@@ -231,10 +230,10 @@ func stageRunners(req taskrun.Request, mcpSet *mcpProviderSet, newRunner runnerF
agent = newRunner(req.Providers, log)
}
if agent == nil {
return nil, nil, fmt.Errorf("no agent runner configured for task %d", req.Task.ID)
return nil, fmt.Errorf("no agent runner configured for task %d", req.Task.ID)
}
applyToolLimits(agent, req.Cfg.ToolPolicy, req.Tools)
return agent, newReviewerFor(req), nil
return agent, nil
}
// routeTask remains available for routing-only callers. Production execution
@@ -286,7 +285,7 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
log.Warn("mcp providers had errors, continuing with available tools", "err", mcpErr)
}
agent, reviewer, err := stageRunners(req, mcpSet, newRunner, log)
agent, err := stageRunners(req, mcpSet, newRunner, log)
if err != nil {
return nil, err
}
@@ -310,17 +309,16 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
}
tc := &workflow.TaskContext{
Task: req.Task,
Repo: req.Repo,
Cfg: req.Cfg.ToConfig(),
Forge: dependencies.forge,
Store: dependencies.store,
Calls: dependencies.calls,
Trees: trees,
Agent: agent,
Reviewer: reviewer,
Bus: bus,
Log: log,
Task: req.Task,
Repo: req.Repo,
Cfg: req.Cfg.ToConfig(),
Forge: dependencies.forge,
Store: dependencies.store,
Calls: dependencies.calls,
Trees: trees,
Agent: agent,
Bus: bus,
Log: log,
}
if req.Repo.PersistentStorage {
memory, readErr := readProjectMemory(storage.MemoryPath)
+9 -13
View File
@@ -238,15 +238,11 @@ type boot struct {
// unrelated call.
embeddings domainembedding.Client
toolReg *tools.Registry
chatModels *chatModelManager
personas *gateway.PersonaRegistry
chatTasks gateway.TaskCreator
chatController *gateway.StoreTaskController
// chatPRReviewer is the operator-triggered PR review capability, built
// once and shared by every channel so a review in flight on one channel
// is deduplicated against the same request on another (single-flight).
chatPRReviewer gateway.ChatPRReviewer
toolReg *tools.Registry
chatModels *chatModelManager
personas *gateway.PersonaRegistry
chatTasks gateway.TaskCreator
chatController *gateway.StoreTaskController
defaultChatIdentity string
updateService *releaseupdate.Service
@@ -930,10 +926,10 @@ func (b *boot) loadPlugins() error {
// 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.
// own one: the daemon works tasks in it, and the Gateway used it to
// materialise a pull request head for the now-removed synchronous
// operator-review path (archie-core-afbk.7). 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
@@ -32,29 +32,3 @@ 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")
}
}
+8 -7
View File
@@ -156,13 +156,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.
// The Gateway composes the process-wide worktree manager before
// setupGatewayChat; nothing in this process reads it today (the old
// synchronous operator-review path that did was removed with the
// adversarial self-review system, archie-core-afbk.7 -- see that bead's
// continuation for the operator trigger's replacement). Kept for the
// manager's other consumers; it 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
+8 -1
View File
@@ -128,7 +128,14 @@ func (b *boot) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskActor
Personas: b.personas, ChatTasks: b.chatTasks,
ChatTaskLister: chatTaskListerAdapter{tasks: b.stateStore.Tasks},
ChatTaskLogs: chatTaskLogReaderAdapter{tasks: b.stateStore.TaskByID, taskLogs: b.taskLogs},
ChatTaskActor: actor, ChatPRReviewer: b.prReviewer(),
// ChatPRReviewer is nil for now: the synchronous operator-review
// implementation (prReviewer) was removed with the adversarial
// self-review system it depended on (archie-core-afbk.7). The
// review_pr chat tool self-omits when its reviewer is nil, so the
// dashboard/chat simply stops advertising the capability until the
// operator trigger is rebuilt to enqueue an async pr-review task
// instead (tracked in that bead's continuation).
ChatTaskActor: actor, ChatPRReviewer: nil,
DefaultChatIdentity: b.defaultChatIdentity,
Bus: b.bus, Log: b.log,
MemoryEngine: b.memoryStore(),
-364
View File
@@ -1,364 +0,0 @@
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)
}
-317
View File
@@ -1,317 +0,0 @@
package archied
import (
"context"
"errors"
"log/slog"
"strings"
"testing"
"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/tools"
"github.com/samcharles93/archie-core/internal/worktree"
)
func TestSplitOwnerRepo(t *testing.T) {
tests := []struct {
in string
owner string
repo string
wantErr bool
}{
{"acme/widget", "acme", "widget", false},
{"acme/widget/service", "acme", "widget/service", false},
{"acme", "", "", true},
{"/widget", "", "", true},
{"acme/", "", "", true},
{"", "", "", true},
}
for _, tt := range tests {
owner, repo, err := splitOwnerRepo(tt.in)
if tt.wantErr {
if err == nil {
t.Errorf("splitOwnerRepo(%q) error = nil, want error", tt.in)
}
continue
}
if err != nil {
t.Errorf("splitOwnerRepo(%q) error = %v", tt.in, err)
continue
}
if owner != tt.owner || repo != tt.repo {
t.Errorf("splitOwnerRepo(%q) = (%q, %q), want (%q, %q)", tt.in, owner, repo, tt.owner, tt.repo)
}
}
}
func TestPRIssueText(t *testing.T) {
tests := []struct {
name string
pr forge.PullRequest
want string
}{
{"title only", forge.PullRequest{Title: "Fix login"}, "Fix login"},
{"title and body", forge.PullRequest{Title: "Fix login", Body: "The form 500s."}, "Fix login\n\nThe form 500s."},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := prIssueText(tt.pr); got != tt.want {
t.Errorf("prIssueText = %q, want %q", got, tt.want)
}
})
}
}
func TestMapReviewResult(t *testing.T) {
report := workflow.ReviewReport{
Status: workflow.ReviewStatusCompleted,
Summary: "checked and cleared",
Findings: []workflow.ReviewFinding{
{File: "a.go", Line: 3, Defect: "nil deref", FailureScenario: "x", Verdict: workflow.ReviewVerdictConfirmed, Level: workflow.ReviewLevelError, Category: workflow.ReviewCategoryNilRisk},
{File: "b.go", Line: 0, Defect: "style", FailureScenario: "y", Verdict: workflow.ReviewVerdictPlausible, Level: workflow.ReviewLevelWarn, Category: workflow.ReviewCategoryOther},
},
}
got := mapReviewResult("acme/widget", 7, "openai/gpt-5", report)
if got.Repo != "acme/widget" || got.Number != 7 || got.Model != "openai/gpt-5" || got.Status != "completed" || got.Summary != "checked and cleared" {
t.Fatalf("result meta = %+v", got)
}
if len(got.Findings) != 2 {
t.Fatalf("findings = %d, want 2", len(got.Findings))
}
if !got.Findings[0].Blocking {
t.Error("confirmed error finding should be blocking")
}
if got.Findings[1].Blocking {
t.Error("plausible warn finding must not be blocking")
}
if got.Findings[0].Line != 3 {
t.Errorf("line = %d, want 3", got.Findings[0].Line)
}
}
func TestMapReviewResultNotRunPreservesReason(t *testing.T) {
report := workflow.NewNotRunReviewReport("no reviewer model configured")
got := mapReviewResult("acme/widget", 7, "", report)
if got.Status != "not_run" || got.Reason != "no reviewer model configured" {
t.Fatalf("not_run result = %+v, want status not_run and reason preserved", got)
}
}
func TestPrReviewerAuthorize(t *testing.T) {
r := &prReviewer{allowed: map[string]map[string]bool{
"": {"acme/widget": true, "acme/core": true},
"archie": {"acme/widget": true},
}}
archie := "archie"
other := "other"
tests := []struct {
name string
identity *string
owner string
repo string
wantErr bool
}{
{"identity own repo", &archie, "acme", "widget", false},
{"identity foreign repo", &archie, "acme", "core", true},
{"unknown identity", &other, "acme", "widget", true},
{"operator any configured", nil, "acme", "core", false},
{"operator unconfigured repo", nil, "acme", "other", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := r.authorize(tt.identity, tt.owner, tt.repo)
if tt.wantErr && err == nil {
t.Errorf("authorize(%v, %s/%s) error = nil, want error", tt.identity, tt.owner, tt.repo)
}
if !tt.wantErr && err != nil {
t.Errorf("authorize(%v, %s/%s) error = %v, want nil", tt.identity, tt.owner, tt.repo, err)
}
})
}
}
func TestPrReviewerSingleflight(t *testing.T) {
r := &prReviewer{}
key := "acme/widget#7"
if !r.begin(key) {
t.Fatal("first begin = false, want true")
}
if r.begin(key) {
t.Fatal("concurrent begin = true, want false (deterministic single-flight)")
}
r.end(key)
if !r.begin(key) {
t.Fatal("begin after end = false, want true")
}
}
func TestBuildReviewAllowlist(t *testing.T) {
cfg := config.Config{
Identities: []config.IdentityConfig{
{Name: "archie", Repos: []config.Repo{{Owner: "acme", Name: "widget"}}},
},
}
allowed := buildReviewAllowlist(cfg)
if !allowed["archie"]["acme/widget"] {
t.Errorf("allowlist missing acme/widget for identity archie: %+v", allowed)
}
if allowed["archie"]["acme/core"] {
t.Errorf("allowlist leaked acme/core to identity archie: %+v", allowed)
}
if _, ok := allowed["nobody"]; ok {
t.Errorf("allowlist contains unknown identity nobody: %+v", allowed)
}
}
// reviewCapableForge satisfies both forge.Forge (via embedding) and
// forge.PullRequestReader, so boot.prReviewer can be exercised in isolation.
type reviewCapableForge struct {
forge.Forge
}
func (reviewCapableForge) GetPullRequest(context.Context, string, string, int) (forge.PullRequest, error) {
return forge.PullRequest{}, nil
}
// TestPrReviewerIsMemoized pins the cross-channel single-flight fix: every
// gateway must share one prReviewer so a review of the same PR requested from
// two channels at once deduplicates instead of running twice. The boot must
// own a worktree manager: a reviewer without one can never run and must not be
// produced (see TestPrReviewerNilWithoutTrees).
func TestPrReviewerIsMemoized(t *testing.T) {
b := &boot{
forgeClient: reviewCapableForge{Forge: forge.NewNoop(slog.New(slog.DiscardHandler))},
trees: &worktree.Manager{},
}
first := b.prReviewer()
if first == nil {
t.Fatal("prReviewer() = nil, want a reviewer")
}
if second := b.prReviewer(); first != second {
t.Fatal("prReviewer() returned distinct instances; channels would not share the in-flight set")
}
}
// TestPrReviewerNilWithoutTrees pins the producer-side invariant: a PR-capable
// forge without a worktree manager must yield no reviewer. The standalone
// Gateway never builds trees, and the daemon used to capture b.trees before it
// was assigned, so both advertised a review_pr tool that always failed.
func TestPrReviewerNilWithoutTrees(t *testing.T) {
b := &boot{forgeClient: reviewCapableForge{Forge: forge.NewNoop(slog.New(slog.DiscardHandler))}}
if got := b.prReviewer(); got != nil {
t.Fatalf("prReviewer() with no worktree manager = %T, want nil", got)
}
}
// TestReviewPRToolRegisteredOnlyWithWorkingReviewer composes the real boot
// wiring (boot.prReviewer -> gateway.ReviewTools) and asserts review_pr is
// advertised only when the reviewer can actually run.
func TestReviewPRToolRegisteredOnlyWithWorkingReviewer(t *testing.T) {
logger := slog.New(slog.DiscardHandler)
b := &boot{
forgeClient: reviewCapableForge{Forge: forge.NewNoop(logger)},
log: logger,
}
if hasReviewPRTool(gateway.ReviewTools(b.prReviewer(), "archie")) {
t.Fatal("review_pr advertised with no worktree manager: a tool that cannot run must not be offered")
}
b.trees = &worktree.Manager{}
if !hasReviewPRTool(gateway.ReviewTools(b.prReviewer(), "archie")) {
t.Fatal("review_pr not advertised with a worktree manager")
}
}
// hasReviewPRTool reports whether review_pr is offered. Presence is asserted by
// name rather than by entry count: the contract is that this one capability is
// offered only when it can run, so adding a sibling review tool must not turn
// this red.
func hasReviewPRTool(entries []tools.ToolEntry) bool {
for _, e := range entries {
if e.Name == "review_pr" {
return true
}
}
return false
}
type fakeArtifactSender struct {
calls int
url string
err error
last artifactsync.Artifact
}
func (f *fakeArtifactSender) Publish(_ context.Context, a artifactsync.Artifact) (string, error) {
f.calls++
f.last = a
if f.err != nil {
return "", f.err
}
return f.url, nil
}
func TestPrReviewerPublishesTheCompletedReview(t *testing.T) {
fake := &fakeArtifactSender{url: "https://offloaded.dev/collab?document=a_1"}
r := &prReviewer{artifacts: fake, log: slog.New(slog.DiscardHandler)}
report := workflow.NewCompletedReviewReport([]workflow.ReviewFinding{{
File: "handler.go", Line: 42, Defect: "nil deref",
FailureScenario: "empty payload crashes", Verdict: workflow.ReviewVerdictConfirmed,
Level: workflow.ReviewLevelError,
}}, "one finding")
report.Checked = []workflow.ReviewCheck{{Property: "p", Evidence: "e"}}
url := r.publishArtifact(context.Background(), "system:pr-reviewer", "acme", "widget", 7, report)
if url != fake.url {
t.Fatalf("publishArtifact = %q, want the sender's url %q", url, fake.url)
}
if fake.calls != 1 {
t.Fatalf("Publish called %d times, want 1", fake.calls)
}
got := fake.last
if got.Meta.Format != "markdown" || got.Meta.Category != "pr-review" {
t.Fatalf("format/category = %s/%s, want markdown/pr-review", got.Meta.Format, got.Meta.Category)
}
if got.Meta.Actor != "system:pr-reviewer" || got.Meta.Origin != "pr-review" {
t.Fatalf("attribution = %s/%s", got.Meta.Actor, got.Meta.Origin)
}
if got.Meta.Visibility != "account-only" || got.Meta.Egress != "none" {
t.Fatalf("visibility/egress = %s/%s", got.Meta.Visibility, got.Meta.Egress)
}
if got.Title != "Review of acme/widget#7" {
t.Fatalf("title = %q", got.Title)
}
if got.Meta.RequestID != "artifact:pr-review:acme/widget#7" {
t.Fatalf("requestId = %q, want the deterministic artifact key", got.Meta.RequestID)
}
if got.Meta.Task == nil || got.Meta.Task.Owner != "acme" || got.Meta.Task.Number != 7 {
t.Fatalf("task coordinates = %+v", got.Meta.Task)
}
if !strings.Contains(got.Source, "## Findings") {
t.Fatalf("source is not the rendered report:\n%s", got.Source)
}
}
func TestPrReviewerPublishDegradesOnSenderFailure(t *testing.T) {
r := &prReviewer{artifacts: &fakeArtifactSender{err: errors.New("503 exhausted")}, log: slog.New(slog.DiscardHandler)}
if url := r.publishArtifact(context.Background(), "system:pr-reviewer", "acme", "widget", 7, workflow.NewCompletedReviewReport(nil, "s")); url != "" {
t.Fatalf("url = %q, want empty on a failed publish", url)
}
}
func TestPrReviewerPublishIsANoOpWithoutASender(t *testing.T) {
r := &prReviewer{}
if url := r.publishArtifact(context.Background(), "system:pr-reviewer", "acme", "widget", 7, workflow.NewCompletedReviewReport(nil, "s")); url != "" {
t.Fatalf("url = %q, want empty when the sender is disabled", url)
}
}
@@ -6,55 +6,9 @@ import (
"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
-7
View File
@@ -95,13 +95,6 @@ type Repo struct {
// won't collide (e.g. different base branches or packages per
// task). Still bounded by [containers].max_concurrency globally.
AllowConcurrent bool `toml:"allow_concurrent" json:"allow_concurrent" yaml:"allow_concurrent"`
// ReviewEnabled opts this repo into the adversarial self-review stage
// (docs/prds/adversarial-self-review.md): a fresh reviewer agent, with
// no path to the implementer's context, runs after gates pass and
// before the PR opens. Default false -- an uncalibrated reviewer that
// cries wolf is worse than no review, so a repo opts in once it
// trusts the stage.
ReviewEnabled bool `toml:"review_enabled" json:"review_enabled" yaml:"review_enabled"`
}
// Protected reports whether path matches a protected suffix.
-2
View File
@@ -253,7 +253,6 @@ func TestRepoJSONRoundTrip(t *testing.T) {
PersistentStorage: true,
MaxRetries: 4,
AllowConcurrent: true,
ReviewEnabled: true,
}
data, err := json.Marshal(repo)
@@ -273,7 +272,6 @@ func TestRepoJSONRoundTrip(t *testing.T) {
"persistent_storage",
"max_retries",
"allow_concurrent",
"review_enabled",
)
var got Repo
-36
View File
@@ -458,42 +458,6 @@ func TestOpenPRCapturesThePullRequestNumber(t *testing.T) {
}
}
// TestOpenPRRecordsTheReviewedRevision is the producer half of the q9au
// head-drift guard. Line numbers in a review finding describe one revision of
// the worktree, and OpenPR is the last point at which the worktree is still
// there to name it -- StageReview reads that same worktree and commits
// nothing, so the head OpenPR measures is the head the review read. Without
// the revision recorded here the posting stage has nothing to check the pull
// request's head against.
func TestOpenPRRecordsTheReviewedRevision(t *testing.T) {
trees := &capturingTrees{stats: sampleStats()}
tc := captureTaskContext(t, trees, &recordingStore{})
tc.Forge = &fakeForge{}
if err := OpenPR(context.Background(), tc, "body"); err != nil {
t.Fatalf("OpenPR() = %v, want nil", err)
}
if tc.ReviewedHeadSHA != trees.stats.HeadSHA {
t.Errorf("ReviewedHeadSHA = %q, want the measured worktree head %q", tc.ReviewedHeadSHA, trees.stats.HeadSHA)
}
}
// TestOpenPRLeavesTheReviewedRevisionUnsetWithoutAMeasurement pins the degrade
// path: a worktree implementation that cannot report a diffstat leaves the
// revision empty, which the forge reads as "not measured" and posts without the
// check. Inventing a revision here would refuse every posting instead.
func TestOpenPRLeavesTheReviewedRevisionUnsetWithoutAMeasurement(t *testing.T) {
tc := captureTaskContext(t, &fakeTrees{}, &recordingStore{})
tc.Forge = &fakeForge{}
if err := OpenPR(context.Background(), tc, "body"); err != nil {
t.Fatalf("OpenPR() = %v, want nil", err)
}
if tc.ReviewedHeadSHA != "" {
t.Errorf("ReviewedHeadSHA = %q, want empty when nothing measured the worktree", tc.ReviewedHeadSHA)
}
}
// TestChangeCaptureTruncatesTheFileListButNotTheTotals: one large refactor
// must not write an unbounded payload, and a capture that dropped entries must
// still report how much the attempt actually changed.
+10 -9
View File
@@ -197,9 +197,7 @@ func Implement() Workflow {
}),
StageYaegiGate(),
StageDiffCap(),
StageReview(),
StageOpenPR(implementPRBody),
StagePostReviewComments(),
},
}
}
@@ -208,13 +206,16 @@ func Implement() Workflow {
// reports the fresh-vs-cached breakdown of tc.Task.TokensUsed (see
// formatTokenUsage) so the operator isn't misled by the raw prompt-token sum
// -- most of it is typically prefix-cache hits billed at a steep discount,
// not full price. When the adversarial review ran, its findings section is
// appended so the PR states what the reviewer checked and found (h019.6).
// not full price.
//
// This used to also append the adversarial self-review's findings section
// (docs/prds/adversarial-self-review.md, now superseded by
// docs/prds/pr-review-agent.md per that PRD's own header). archie-core-afbk.7
// removed the old review stage; hooking the new pr-review pipeline in as
// this workflow's "archie's own PRs" trigger is tracked separately (see the
// bead's continuation comment) rather than guessed at here, since it needs
// the same stage-loop-around-StageOpenPR investigation that bead flagged.
func implementPRBody(tc *TaskContext) string {
summary := tc.BuildSummary
if section := renderPRReviewSection(tc.ReviewReport); section != "" {
summary += "\n\n" + section
}
return fmt.Sprintf("%s\n\n---\n*workflow: implement · %d iterations · %s*",
summary, tc.Task.Iterations, formatTokenUsage(tc.Task.TokensUsed, tc.RunUsage))
tc.BuildSummary, tc.Task.Iterations, formatTokenUsage(tc.Task.TokensUsed, tc.RunUsage))
}
@@ -45,8 +45,6 @@ var outcomeSites = []outcomeSite{
{"prreview_stages_verify.go", "StatusWaitingHuman", "review.approve_before_post is set and synthesis produced findings: the run waits for an operator decision before the merge gate"},
{"remediate.go", "StatusParked", "the remediation round cap is spent"},
{"remediate.go", "StatusPROpen", "a remediation round pushed the task back to its open PR"},
{"review.go", "StatusParked", "review is enabled but no reviewer is wired"},
{"review.go", "StatusParked", "review confirmed an error-level finding"},
{"steps.go", "StatusCompleted", "a no-change build closed the forge issue"},
{"steps.go", "StatusCompleted", "a no-change build on a task with no forge issue"},
{"steps.go", "StatusPROpen", "the PR opened"},
@@ -127,6 +127,11 @@ type ScoredFinding struct {
Score float64
Multipliers []string
Blocking bool
// Adversary carries phase 5's verdict through synthesis, so a caller
// deciding whether a blocking finding was ever challenged (the pr-review
// PRD's archie's-own-PR trigger rule) does not have to cross-reference
// back into the pre-synthesis Finding slice.
Adversary AdversaryVerdict
}
// ScoreInputs is the run context that moves every finding's score.
@@ -170,6 +175,7 @@ func Score(findings []Finding, inputs ScoreInputs) []ScoredFinding {
Score: score,
Multipliers: applied,
Blocking: finding.Blocking,
Adversary: finding.Adversary,
})
}
+24 -14
View File
@@ -149,20 +149,30 @@ const prReviewConcurrency = 8
// merge gate), both off by default and each gated by its own config flag.
func PRReview() Workflow {
return Workflow{
Name: "pr-review",
Stages: []Stage{
stagePRIntake(),
stagePRAnatomy(),
stagePRLenses(),
stagePRReview(),
stagePRPrecisionGate(),
stagePRVerification(),
stagePRCoverageConsistency(),
stagePRSynthesis(),
stagePROperatorApproval(),
stagePRMergeGate(),
stagePROutput(),
},
Name: "pr-review",
Stages: append(prReviewDecisionStages(), stagePROutput()),
}
}
// prReviewDecisionStages is phases 1 through 8: everything up to and
// including the merge gate's blocking/advisory verdict, but before phase 9
// posts anything. Split out from PRReview so the implement workflow can
// splice it in before StageOpenPR (archie-core-afbk.7's "archie's own PRs"
// trigger): the decision of whether an unchallenged blocking finding parks
// the task must happen before a PR exists to post to, while phase 9's
// posting must happen after, once a PR number exists to anchor comments to.
func prReviewDecisionStages() []Stage {
return []Stage{
stagePRIntake(),
stagePRAnatomy(),
stagePRLenses(),
stagePRReview(),
stagePRPrecisionGate(),
stagePRVerification(),
stagePRCoverageConsistency(),
stagePRSynthesis(),
stagePROperatorApproval(),
stagePRMergeGate(),
}
}
-281
View File
@@ -1,281 +0,0 @@
package workflow
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// Reviewer runs an adversarial review of a code snapshot in its own
// isolated context and reports what it found. Implementations must give
// the reviewer no path to the implementer's conversation, credentials, or
// git history -- see docs/prds/adversarial-self-review.md. A reviewer that
// errors, is truncated, or reaches no conclusion returns a ReviewReport
// with Status == ReviewStatusNotRun rather than a Go error: the stage
// always has a report to act on, and "did not run" is a first-class
// outcome, not a special case.
type Reviewer interface {
Review(ctx context.Context, req ReviewRequest) ReviewReport
}
// ReviewRequest is everything an adversarial reviewer is given: a
// .git-free snapshot of the reviewed commit's tracked files, the diff that
// produced it, and the originating issue text. Nothing else -- commit
// messages, branch name, reflog, and the implementer's own transcript are
// structurally excluded, not merely withheld by instruction.
type ReviewRequest struct {
// SnapshotDir is a filesystem directory containing HEAD's tracked
// files with no .git present.
SnapshotDir string
Diff string
IssueText string
// MaxSteps bounds the reviewer's tool-loop iterations. 0 means the
// implementation's own default.
MaxSteps int
}
// reviewDetailBytes bounds how much of a park Detail the rendered review
// findings occupy, matching the store's own park-reason cap headroom (see
// baselineParkOutputBytes in implement.go).
const reviewDetailBytes = 4000
// StageReview runs the adversarial self-review stage. It is a no-op unless
// Repo.ReviewEnabled is true. A surviving confirmed error-level finding, or
// a review that failed to run at all, parks the task (StatusParked)
// with the findings in Detail and stops the workflow before StageOpenPR;
// a clean pass, or only warn/plausible findings, leaves Outcome unset so
// the workflow proceeds.
func StageReview() Stage {
return Stage{Name: "review", Run: func(ctx context.Context, tc *TaskContext) error {
if !tc.Repo.ReviewEnabled {
return nil
}
if tc.Reviewer == nil {
tc.Outcome = Outcome{
Status: StatusParked,
Detail: "adversarial review is enabled (review_enabled) but no reviewer is configured",
}
return nil
}
report, err := runReview(ctx, tc)
if err != nil {
return fmt.Errorf("review: %w", err)
}
// Stash the report regardless of outcome: the PR-body findings
// section (h019.6) needs it when the review passes, and the parked
// task's park reason already carries it when it does not.
tc.ReviewReport = report
if !report.Passed() {
tc.Outcome = Outcome{Status: StatusParked, Detail: renderReviewDetail(report)}
}
return nil
}}
}
// runReview builds the reviewer's isolated snapshot, diff and issue text,
// invokes tc.Reviewer, and cleans up the snapshot regardless of outcome.
func runReview(ctx context.Context, tc *TaskContext) (ReviewReport, error) {
diff, err := tc.Trees.Diff(ctx, tc.Dir, tc.Repo.BaseBranch())
if err != nil {
return ReviewReport{}, fmt.Errorf("compute diff: %w", err)
}
// Beside, not inside, the task worktree: a snapshot inside tc.Dir
// would itself be an untracked, .git-adjacent directory the next
// commit or gate run could pick up.
snapshotDir, err := os.MkdirTemp(filepath.Dir(tc.Dir), "review-snapshot-*")
if err != nil {
return ReviewReport{}, fmt.Errorf("create review snapshot directory: %w", err)
}
defer os.RemoveAll(snapshotDir)
if err := tc.Trees.Snapshot(ctx, tc.Dir, snapshotDir); err != nil {
return ReviewReport{}, fmt.Errorf("snapshot worktree for review: %w", err)
}
req := ReviewRequest{
SnapshotDir: snapshotDir,
Diff: diff,
IssueText: taskPromptBlock(tc.Task),
MaxSteps: tc.Cfg.Budgets.MaxSteps,
}
// The reviewer is an agent call like any other: it records itself as a
// child of the review stage's step, so an intervention during review can
// say what the reviewer did. The reviewer runs on a report contract, not
// the agent runner's result, so the recording maps the report's own
// verdict: a review that ran is succeeded, one that never ran is failed,
// and the tokens it accounted are not carried by the contract.
stepID, _, err := tc.startChildStep(ctx, task.StepKindAgent, "review")
if err != nil {
return ReviewReport{}, err
}
report := tc.Reviewer.Review(ctx, req)
to, detail := taskstate.StepSucceeded, report.Summary
if report.Status == ReviewStatusNotRun {
to, detail = taskstate.StepFailed, report.SkipReason
}
if ferr := tc.finishChildStep(ctx, stepID, to, detail, 0); ferr != nil {
return ReviewReport{}, ferr
}
return report, nil
}
// StagePostReviewComments posts the review's line-anchored findings as inline
// comments on the pull request StageOpenPR has just opened (archie-core-q9au).
//
// Only line-anchored findings are posted. A whole-file finding has no line to
// attach to and stays in the PR-body list, which remains the complete record and
// the fallback. A blocking finding never reaches here either: StageReview parks
// the task before StageOpenPR when a confirmed error-level finding survives, so
// there is no PR to comment on.
//
// Best-effort by construction. The PR is already open and its body already
// carries every finding, so a comment that failed to post must not park a task
// whose actual work succeeded -- the same reasoning as OpenPR's best-effort
// LinkBranch call. A failure is logged, never returned. That includes the forge
// refusing the set because the pull request's head moved past ReviewedHeadSHA:
// the line numbers would then describe a revision the author is no longer
// looking at, and the PR body is the record that survives anyway.
func StagePostReviewComments() Stage {
return Stage{Name: "post-review-comments", Run: func(ctx context.Context, tc *TaskContext) error {
if !tc.ReviewReport.Ran() {
return nil
}
comments := lineAnchoredReviewComments(tc.ReviewReport)
if len(comments) == 0 {
return nil
}
// OpenPR records the number; without one there is nothing to anchor to,
// and a guess would attach the findings to unrelated work.
if tc.Task.PRNumber <= 0 {
tc.Log.Warn("review comments not posted: the task carries no pull request number",
"findings", len(comments))
return nil
}
err := tc.Forge.CreateReviewComments(ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.PRNumber, tc.ReviewedHeadSHA, comments)
if err != nil {
tc.Log.Error("review comments not posted; the pull request body still lists every finding",
"err", err, "pr", tc.Task.PRNumber, "comments", len(comments))
return nil
}
tc.Log.Info("review comments posted", "pr", tc.Task.PRNumber, "comments", len(comments))
return nil
}}
}
// ReviewComment is one line-anchored comment to post on an open pull request:
// the repo-relative path, the line in the reviewed revision, and the rendered
// body -- which may carry a fenced suggestion block. It is what the review stage
// hands the forge through Forger, so it names nothing forge-specific.
type ReviewComment struct {
Path string
Line int
Body string
}
// lineAnchoredReviewComments renders the findings that can carry an inline
// comment. It is the one place "which findings, with what body" is decided, so
// the PR body and the inline comments cannot disagree about a finding beyond the
// location the body has to state in text.
func lineAnchoredReviewComments(report ReviewReport) []ReviewComment {
comments := make([]ReviewComment, 0, len(report.Findings))
for _, f := range report.Findings {
if f.Line <= 0 {
continue
}
comments = append(comments, ReviewComment{Path: f.File, Line: f.Line, Body: renderReviewCommentBody(f)})
}
return comments
}
// renderReviewCommentBody renders one finding as the body of a comment anchored
// to its line: the anchor carries the location, so the body states the defect,
// the scenario that produces it, and -- for a confirmed defect the reviewer
// supplied a replacement for -- the replacement as a fence GitHub applies in one
// click.
//
// The fence is gated on confirmed here rather than left to the reviewer, because
// it is a one-click apply: a wrong suggestion breaks the code and the author has
// to notice. The prompt asks for a suggestion only on a confirmed, mechanically
// fixable finding, but an agent's output is input, so the rule is enforced where
// the fence is written.
func renderReviewCommentBody(f ReviewFinding) string {
var b strings.Builder
fmt.Fprintf(&b, "**%s (%s)**: %s", f.Verdict, f.Level, f.Defect)
if f.FailureScenario != "" {
fmt.Fprintf(&b, "\n\n%s", f.FailureScenario)
}
if f.Verdict == ReviewVerdictConfirmed && f.Suggestion != "" {
fmt.Fprintf(&b, "\n\n```suggestion\n%s\n```", strings.TrimRight(f.Suggestion, "\n"))
}
return b.String()
}
// renderReviewDetail formats a review report as a park Detail: why the
// task stopped, and what a human (or a retry) needs to know.
func renderReviewDetail(report ReviewReport) string {
if !report.Ran() {
reason := report.SkipReason
if reason == "" {
reason = "no reason given"
}
return clip("adversarial review did not run -- "+reason, reviewDetailBytes)
}
var b strings.Builder
b.WriteString("adversarial review found blocking defects:\n")
for _, f := range report.Findings {
if !f.Blocking() {
continue
}
fmt.Fprintf(&b, "- %s:%d %s (%s)\n", f.File, f.Line, f.Defect, f.FailureScenario)
}
return clip(b.String(), reviewDetailBytes)
}
// renderPRReviewSection renders the PR-body findings section (h019.6). It
// returns "" when the review did not run, so a review that was disabled or
// never wired does not claim one ran -- silence would be indistinguishable
// from the review running and finding nothing. When it did run, the section
// states the outcome explicitly, including the zero-findings case.
func renderPRReviewSection(report ReviewReport) string {
if !report.Ran() {
return ""
}
var b strings.Builder
b.WriteString("## Adversarial review\n\n")
if len(report.Findings) == 0 {
b.WriteString("Adversarial self-review ran on the committed diff and found no defects.\n")
} else {
b.WriteString("Adversarial self-review ran and found no blocking defects. Non-blocking notes:\n\n")
for _, f := range report.Findings {
loc := f.File
if f.Line > 0 {
loc = fmt.Sprintf("%s:%d", f.File, f.Line)
}
fmt.Fprintf(&b, "- `%s` (%s, %s): %s", loc, f.Verdict, f.Level, f.Defect)
if f.FailureScenario != "" {
fmt.Fprintf(&b, " — %s", f.FailureScenario)
}
b.WriteString("\n")
}
}
// The cleared properties are what back "ran and found nothing": without
// them a zero-finding review reads the same as one that never looked
// (h019.5 calibration).
if len(report.Checked) > 0 {
b.WriteString("\nChecked and cleared:\n\n")
for _, c := range report.Checked {
fmt.Fprintf(&b, "- **%s** — %s\n", c.Property, c.Evidence)
}
}
if report.Summary != "" {
fmt.Fprintf(&b, "\nSummary: %s\n", report.Summary)
}
return b.String()
}
@@ -1,272 +0,0 @@
package workflow
import (
"context"
"errors"
"log/slog"
"strings"
"testing"
)
// TestStagePostReviewCommentsPostsLineAnchoredFindings is the q9au stage
// contract: a line-anchored finding reaches the forge as an inline comment,
// a whole-file finding does not (it has no line to attach to and stays in the
// PR body), and only a confirmed defect carries the one-click suggestion fence.
func TestStagePostReviewCommentsPostsLineAnchoredFindings(t *testing.T) {
tests := []struct {
name string
finding ReviewFinding
wantPosted bool
wantSuggestion bool
wantBodyHas []string
wantBodyHasNot []string
}{
{
name: "a confirmed defect carries its suggestion",
finding: ReviewFinding{
File: "a.go", Line: 12, Defect: "nil deref on an empty map",
FailureScenario: "an issue with no labels panics the poller",
Verdict: ReviewVerdictConfirmed, Level: ReviewLevelWarn,
// Single-line only: a multi-line suggestion is rejected by
// ReviewFinding.Validate (Issue 2), so the posted fence replaces
// the anchored line exactly.
Suggestion: "if len(labels) == 0 { return nil }",
},
wantPosted: true,
wantSuggestion: true,
wantBodyHas: []string{"nil deref on an empty map", "an issue with no labels panics the poller", "confirmed"},
},
{
name: "a plausible worry never carries one, even when supplied",
finding: ReviewFinding{
File: "b.go", Line: 3, Defect: "possible race",
FailureScenario: "two writers", Verdict: ReviewVerdictPlausible,
Level: ReviewLevelWarn, Suggestion: "mu.Lock()",
},
wantPosted: true,
wantSuggestion: false,
wantBodyHas: []string{"possible race", "plausible"},
wantBodyHasNot: []string{"```suggestion", "mu.Lock()"},
},
{
name: "a confirmed defect with no mechanical fix has no fence",
finding: ReviewFinding{
File: "c.go", Line: 9, Defect: "wrong ordering",
FailureScenario: "retry drops the newest event",
Verdict: ReviewVerdictConfirmed, Level: ReviewLevelError,
},
wantPosted: true,
wantSuggestion: false,
wantBodyHasNot: []string{"```suggestion"},
},
{
name: "a whole-file finding is not posted inline",
finding: ReviewFinding{
File: "README.md", Line: 0, Defect: "docs describe the old topology",
Verdict: ReviewVerdictConfirmed, Level: ReviewLevelWarn,
},
wantPosted: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &fakeForge{}
tc := reviewCommentContext(f, ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{tt.finding},
})
if err := StagePostReviewComments().Run(context.Background(), tc); err != nil {
t.Fatalf("stage returned %v; a posting failure must never fail the task", err)
}
if got := len(f.reviewComments); (got > 0) != tt.wantPosted {
t.Fatalf("posted %d comments, want posted=%v", got, tt.wantPosted)
}
if !tt.wantPosted {
return
}
got := f.reviewComments[0]
if got.Path != tt.finding.File || got.Line != tt.finding.Line {
t.Errorf("anchor = %s:%d, want %s:%d", got.Path, got.Line, tt.finding.File, tt.finding.Line)
}
for _, want := range tt.wantBodyHas {
if !strings.Contains(got.Body, want) {
t.Errorf("body %q does not mention %q", got.Body, want)
}
}
for _, unwanted := range tt.wantBodyHasNot {
if strings.Contains(got.Body, unwanted) {
t.Errorf("body %q contains %q, which must not be offered", got.Body, unwanted)
}
}
hasFence := strings.Contains(got.Body, "```suggestion")
if hasFence != tt.wantSuggestion {
t.Errorf("suggestion fence present = %v, want %v (body %q)", hasFence, tt.wantSuggestion, got.Body)
}
})
}
}
// TestStagePostReviewCommentsPostsEveryAnchoredFindingInOneCall pins the batch
// shape: Gitea carries inline comments only on a submitted review, so the set
// must travel as one call rather than one call per finding.
func TestStagePostReviewCommentsPostsEveryAnchoredFindingInOneCall(t *testing.T) {
f := &fakeForge{}
tc := reviewCommentContext(f, ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{File: "a.go", Line: 1, Defect: "one", Verdict: ReviewVerdictConfirmed, Level: ReviewLevelWarn},
{File: "b.go", Line: 2, Defect: "two", Verdict: ReviewVerdictPlausible, Level: ReviewLevelWarn},
{File: "c.go", Line: 0, Defect: "file-level", Verdict: ReviewVerdictPlausible, Level: ReviewLevelWarn},
},
})
if err := StagePostReviewComments().Run(context.Background(), tc); err != nil {
t.Fatal(err)
}
if f.reviewCalls != 1 {
t.Errorf("CreateReviewComments called %d times, want 1", f.reviewCalls)
}
if len(f.reviewComments) != 2 {
t.Errorf("posted %d comments, want the 2 anchored findings", len(f.reviewComments))
}
}
// TestStagePostReviewCommentsIsANoOpWithoutAReview keeps the stage silent when
// there is nothing to post: a disabled or never-wired review must not reach the
// forge at all, and a clean review has nothing to anchor.
func TestStagePostReviewCommentsIsANoOpWithoutAReview(t *testing.T) {
tests := []struct {
name string
report ReviewReport
}{
{name: "the review never ran", report: ReviewReport{}},
{name: "the review was skipped", report: ReviewReport{Status: ReviewStatusSkipped, SkipReason: "no reviewer"}},
{name: "the review ran and found nothing", report: ReviewReport{Status: ReviewStatusCompleted}},
{
name: "the review found only a whole-file finding",
report: ReviewReport{Status: ReviewStatusCompleted, Findings: []ReviewFinding{
{File: "README.md", Line: 0, Defect: "stale", Verdict: ReviewVerdictPlausible, Level: ReviewLevelWarn},
}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &fakeForge{}
tc := reviewCommentContext(f, tt.report)
if err := StagePostReviewComments().Run(context.Background(), tc); err != nil {
t.Fatal(err)
}
if f.reviewCalls != 0 {
t.Errorf("CreateReviewComments called %d times, want 0", f.reviewCalls)
}
})
}
}
// TestStagePostReviewCommentsSurvivesAPostingFailure is the "best effort"
// clause: the PR is already open and its body already lists every finding, so a
// failed comment must be logged, not turned into a parked task.
func TestStagePostReviewCommentsSurvivesAPostingFailure(t *testing.T) {
f := &fakeForge{reviewErr: errors.New("422 line is not part of the diff")}
tc := reviewCommentContext(f, ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{File: "a.go", Line: 4, Defect: "nil deref", Verdict: ReviewVerdictConfirmed, Level: ReviewLevelWarn},
},
})
if err := StagePostReviewComments().Run(context.Background(), tc); err != nil {
t.Fatalf("stage returned %v; a posting failure must never fail the task", err)
}
if tc.Outcome.Status != "" {
t.Errorf("outcome = %q, want unset: a failed comment must not end the workflow", tc.Outcome.Status)
}
}
// TestStagePostReviewCommentsNeedsAPullRequestNumber covers the one precondition
// the stage cannot invent. A task with no recorded PR number has nothing to
// anchor to, and guessing would attach the findings to unrelated work.
func TestStagePostReviewCommentsNeedsAPullRequestNumber(t *testing.T) {
f := &fakeForge{}
tc := reviewCommentContext(f, ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{File: "a.go", Line: 4, Defect: "nil deref", Verdict: ReviewVerdictConfirmed, Level: ReviewLevelWarn},
},
})
tc.Task.PRNumber = 0
if err := StagePostReviewComments().Run(context.Background(), tc); err != nil {
t.Fatal(err)
}
if f.reviewCalls != 0 {
t.Errorf("CreateReviewComments called %d times, want 0 with no PR number", f.reviewCalls)
}
}
// TestStagePostReviewCommentsAddressesTheRecordedPullRequest pins that the
// comment lands on the task's own PR, in the task's own repo.
func TestStagePostReviewCommentsAddressesTheRecordedPullRequest(t *testing.T) {
f := &fakeForge{}
tc := reviewCommentContext(f, ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{File: "a.go", Line: 4, Defect: "nil deref", Verdict: ReviewVerdictConfirmed, Level: ReviewLevelWarn},
},
})
if err := StagePostReviewComments().Run(context.Background(), tc); err != nil {
t.Fatal(err)
}
if f.reviewOwner != "acme" || f.reviewRepo != "widget" || f.reviewNumber != 42 {
t.Errorf("anchored at %s/%s#%d, want acme/widget#42", f.reviewOwner, f.reviewRepo, f.reviewNumber)
}
}
// TestStagePostReviewCommentsThreadsTheReviewedHeadSHA is the producer half of
// the head-drift guard: the forge can only refuse a set whose line numbers
// describe a revision the pull request has left if the stage tells it which
// revision those numbers were measured on.
func TestStagePostReviewCommentsThreadsTheReviewedHeadSHA(t *testing.T) {
tests := []struct {
name string
reviewed string
}{
{name: "a measured revision reaches the forge", reviewed: "1a2b3c4d"},
{
name: "an unmeasured revision is passed through as empty so the forge posts unverified",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := &fakeForge{}
tc := reviewCommentContext(f, ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{File: "a.go", Line: 4, Defect: "nil deref", Verdict: ReviewVerdictConfirmed, Level: ReviewLevelWarn},
},
})
tc.ReviewedHeadSHA = tt.reviewed
if err := StagePostReviewComments().Run(context.Background(), tc); err != nil {
t.Fatal(err)
}
if f.reviewHeadSHA != tt.reviewed {
t.Errorf("forge was handed reviewed head %q, want %q", f.reviewHeadSHA, tt.reviewed)
}
})
}
}
func reviewCommentContext(f *fakeForge, report ReviewReport) *TaskContext {
return &TaskContext{
Task: &Task{ID: 1, Owner: "acme", Repo: "widget", IssueNumber: 7, PRNumber: 42},
Forge: f,
Log: slog.New(slog.DiscardHandler),
ReviewReport: report,
}
}
-320
View File
@@ -1,320 +0,0 @@
package workflow
import (
"errors"
"fmt"
"strings"
)
// ReviewLevel represents the severity level of a review finding.
// In house style, "error" blocks PR creation when confirmed; "warn" is advisory.
type ReviewLevel string
const (
// ReviewLevelError indicates a severe defect that blocks workflow execution when confirmed.
ReviewLevelError ReviewLevel = "error"
// ReviewLevelWarn indicates an advisory finding that is logged but never blocks.
ReviewLevelWarn ReviewLevel = "warn"
)
// ReviewVerdict distinguishes confirmed defects from plausible worries.
// The load-bearing rule is that ONLY a confirmed defect may block.
type ReviewVerdict string
const (
// ReviewVerdictConfirmed indicates a verified defect backed by a concrete failure scenario.
ReviewVerdictConfirmed ReviewVerdict = "confirmed"
// ReviewVerdictPlausible indicates a potential defect or worry that is plausible but unverified.
ReviewVerdictPlausible ReviewVerdict = "plausible"
)
// ReviewDisposition records what happened once a finding was acted on.
type ReviewDisposition string
const (
// ReviewDispositionFixed indicates the defect was addressed and resolved in code.
ReviewDispositionFixed ReviewDisposition = "fixed"
// ReviewDispositionSkipped indicates the finding was reviewed and intentionally bypassed.
ReviewDispositionSkipped ReviewDisposition = "skipped"
// ReviewDispositionNoChangeNeeded indicates the finding was investigated and judged as requiring no change.
ReviewDispositionNoChangeNeeded ReviewDisposition = "no_change_needed"
)
// ReviewCategory classifies findings against this repo's review checklist.
type ReviewCategory string
const (
// ReviewCategoryDeadCode identifies unreferenced or dead code paths.
ReviewCategoryDeadCode ReviewCategory = "dead-code"
// ReviewCategoryUncheckedError identifies ignored or unchecked error returns.
ReviewCategoryUncheckedError ReviewCategory = "unchecked-error"
// ReviewCategoryHardcodedValue identifies hardcoded values that should be parameters or constants.
ReviewCategoryHardcodedValue ReviewCategory = "hardcoded-value"
// ReviewCategoryInterfaceSatisfaction identifies interface compliance or satisfaction defects.
ReviewCategoryInterfaceSatisfaction ReviewCategory = "interface-satisfaction"
// ReviewCategoryNilRisk identifies nil-pointer dereference risks.
ReviewCategoryNilRisk ReviewCategory = "nil-risk"
// ReviewCategoryGoroutineLeak identifies leaking goroutines or unbound concurrency.
ReviewCategoryGoroutineLeak ReviewCategory = "goroutine-leak"
// ReviewCategoryRace identifies data races or unsynchronized access to shared mutable state.
ReviewCategoryRace ReviewCategory = "race"
// ReviewCategoryOther identifies defects not matching specific checklist items.
ReviewCategoryOther ReviewCategory = "other"
)
// ReviewStatus represents the execution status of an adversarial review stage.
type ReviewStatus string
const (
// ReviewStatusNotRun indicates the reviewer agent was not executed.
ReviewStatusNotRun ReviewStatus = "not_run"
// ReviewStatusCompleted indicates the reviewer agent ran to completion.
ReviewStatusCompleted ReviewStatus = "completed"
// ReviewStatusSkipped indicates the reviewer was intentionally skipped.
ReviewStatusSkipped ReviewStatus = "skipped"
)
// ReviewFinding represents one defect found during adversarial review.
// It is serialisable across the agent-execution JSON data boundary.
//
// A finding describes wrong behaviour or a crash. Style, formatting, naming,
// and preference nits are out of contract -- they are the padding that trains
// an operator to skim past a findings list.
type ReviewFinding struct {
// File is the repo-relative file path where the defect is located.
File string `json:"file"`
// Line is the line number where the defect is located.
Line int `json:"line"`
// Defect is a one-sentence statement of the defect.
Defect string `json:"defect"`
// FailureScenario describes the concrete inputs or state that produce the wrong output or crash.
FailureScenario string `json:"failure_scenario"`
// Suggestion is the replacement text for the anchored line, when the
// reviewer can state the fix mechanically. Only a confirmed finding may
// carry one: it is rendered as a one-click apply, and a wrong one-click
// fix is worse than no fix (h019.5 calibration). Empty means "no
// mechanical fix", not "no fix needed".
Suggestion string `json:"suggestion,omitempty"`
// Verdict distinguishes confirmed defects from plausible hypotheses. Only confirmed findings may block.
Verdict ReviewVerdict `json:"verdict"`
// Disposition records the action taken once the finding is addressed.
Disposition ReviewDisposition `json:"disposition,omitempty"`
// Level is "error" (blocking when confirmed) or "warn" (advisory).
Level ReviewLevel `json:"level"`
// Category classifies the defect according to the review checklist.
Category ReviewCategory `json:"category"`
}
// ReviewCheck records a property the reviewer verified clean. It is the
// symmetric counterpart of ReviewFinding: where a finding carries a failure
// scenario as its evidence, a check carries how the property was verified. A
// zero-finding review is credible only when it lists what it checked, so a
// check without evidence is an assertion, not a verification.
type ReviewCheck struct {
// Property is what was verified clean, e.g. "nil-safety of Foo callers".
Property string `json:"property"`
// Evidence is how it was verified, e.g. "read all 4 callers; each nil-checks".
Evidence string `json:"evidence"`
}
// ReviewReport represents the aggregate result of an adversarial review.
// It explicitly distinguishes "reviewer looked and found nothing" (Status == ReviewStatusCompleted, Findings empty)
// from "reviewer never ran" (Status == ReviewStatusNotRun or uninitialised).
type ReviewReport struct {
// Status explicitly records whether the review was run, not run, or skipped.
Status ReviewStatus `json:"status"`
// Findings contains all findings produced by the review.
Findings []ReviewFinding `json:"findings,omitempty"`
// Checked records the properties the reviewer verified clean. A
// zero-finding review is credible only when it says what it checked;
// this is what separates "I checked and it is fine" from "I did not look".
Checked []ReviewCheck `json:"checked,omitempty"`
// Summary is a human-readable high-level overview of the review.
Summary string `json:"summary,omitempty"`
// SkipReason explains why the review was not run or was skipped.
SkipReason string `json:"skip_reason,omitempty"`
}
// Blocking reports whether the finding is a blocking defect.
// The load-bearing rule: ONLY a finding with Verdict == ReviewVerdictConfirmed
// AND Level == ReviewLevelError blocks. A plausible finding, however severe
// its level claims to be, never blocks on its own.
func (f ReviewFinding) Blocking() bool {
return f.Verdict == ReviewVerdictConfirmed && f.Level == ReviewLevelError
}
// ReviewBlocking reports whether any finding in the slice is blocking.
// The load-bearing rule: ONLY a finding with Verdict == ReviewVerdictConfirmed
// and Level == ReviewLevelError blocks. Plausible findings and warn-level findings
// never block.
func ReviewBlocking(findings []ReviewFinding) bool {
for _, f := range findings {
if f.Blocking() {
return true
}
}
return false
}
// Ran reports whether the adversarial reviewer actually executed to completion.
// Reviews that were not run (Status == ReviewStatusNotRun or uninitialised) or skipped
// return false.
func (r ReviewReport) Ran() bool {
return r.Status == ReviewStatusCompleted
}
// Blocking reports whether any finding within the report is blocking.
func (r ReviewReport) Blocking() bool {
return ReviewBlocking(r.Findings)
}
// Passed reports whether the review ran to completion and produced no blocking findings.
// A review that did not run (or has zero-value status) returns false because "not run"
// is structurally distinct from "ran and found 0 defects".
func (r ReviewReport) Passed() bool {
return r.Ran() && !r.Blocking()
}
// Validate checks that the finding satisfies contract invariants.
func (f ReviewFinding) Validate() error {
if err := f.validateAnchor(); err != nil {
return err
}
if err := f.validateClassification(); err != nil {
return err
}
return f.validateDisposition()
}
// validateAnchor checks where the defect is and what fix, if any, accompanies
// it.
func (f ReviewFinding) validateAnchor() error {
if f.File == "" {
return errors.New("review finding file is required")
}
if f.Line < 0 {
return fmt.Errorf("review finding line cannot be negative: %d", f.Line)
}
if f.Defect == "" {
return errors.New("review finding defect statement is required")
}
if f.FailureScenario == "" {
return errors.New("review finding failure scenario is required")
}
// A suggestion is applied in one click, so only a confirmed defect may
// carry one: a wrong suggestion breaks the code and the author has to
// notice, where a plausible worry gets a sentence (h019.5 calibration).
// Enforced at the value so the agent's record_finding call is rejected with
// something the model can act on, instead of the suggestion being dropped
// somewhere later where nothing tells it.
if f.Suggestion != "" && f.Verdict != ReviewVerdictConfirmed {
return fmt.Errorf("review finding %s:%d offers a one-click suggestion with verdict %q; only %q may", f.File, f.Line, f.Verdict, ReviewVerdictConfirmed)
}
// A suggestion is a one-line replacement GitHub applies with a single click,
// so it must be single-line: an embedded newline renders a multi-line fence
// that GitHub applies only up to the first line, silently corrupting the file
// (docs/prds/inline-review.md). Enforced at the value so the agent's
// record_finding call is rejected with something the model can act on.
if f.Suggestion != "" && strings.ContainsAny(f.Suggestion, "\n\r") {
return fmt.Errorf("review finding %s:%d suggestion must be single-line; got %d line(s)", f.File, f.Line, strings.Count(f.Suggestion, "\n")+1)
}
return nil
}
// validateClassification checks the three enums that classify the finding.
func (f ReviewFinding) validateClassification() error {
switch f.Verdict {
case ReviewVerdictConfirmed, ReviewVerdictPlausible:
default:
return fmt.Errorf("invalid review verdict %q (must be %q or %q)", f.Verdict, ReviewVerdictConfirmed, ReviewVerdictPlausible)
}
switch f.Level {
case ReviewLevelError, ReviewLevelWarn:
default:
return fmt.Errorf("invalid review level %q (must be %q or %q)", f.Level, ReviewLevelError, ReviewLevelWarn)
}
switch f.Category {
case ReviewCategoryDeadCode,
ReviewCategoryUncheckedError,
ReviewCategoryHardcodedValue,
ReviewCategoryInterfaceSatisfaction,
ReviewCategoryNilRisk,
ReviewCategoryGoroutineLeak,
ReviewCategoryRace,
ReviewCategoryOther:
default:
return fmt.Errorf("invalid review category %q", f.Category)
}
return nil
}
// validateDisposition checks the optional record of what was done about the
// finding once it was acted on.
func (f ReviewFinding) validateDisposition() error {
switch f.Disposition {
case "", ReviewDispositionFixed, ReviewDispositionSkipped, ReviewDispositionNoChangeNeeded:
default:
return fmt.Errorf("invalid review disposition %q", f.Disposition)
}
return nil
}
// Validate checks that the check satisfies contract invariants. Evidence is
// required: a check without evidence is an assertion, not a verification.
func (c ReviewCheck) Validate() error {
if c.Property == "" {
return errors.New("review check property is required")
}
if c.Evidence == "" {
return errors.New("review check evidence is required")
}
return nil
}
// Validate checks that the report satisfies contract invariants.
func (r ReviewReport) Validate() error {
switch r.Status {
case ReviewStatusNotRun, ReviewStatusCompleted, ReviewStatusSkipped:
default:
return fmt.Errorf("invalid review status %q", r.Status)
}
for i, f := range r.Findings {
if err := f.Validate(); err != nil {
return fmt.Errorf("findings[%d]: %w", i, err)
}
}
for i, c := range r.Checked {
if err := c.Validate(); err != nil {
return fmt.Errorf("checked[%d]: %w", i, err)
}
}
return nil
}
// NewCompletedReviewReport constructs a completed review report.
func NewCompletedReviewReport(findings []ReviewFinding, summary string) ReviewReport {
if findings == nil {
findings = []ReviewFinding{}
}
return ReviewReport{
Status: ReviewStatusCompleted,
Findings: findings,
Summary: summary,
}
}
// NewNotRunReviewReport constructs a report representing an unexecuted review.
func NewNotRunReviewReport(reason string) ReviewReport {
return ReviewReport{
Status: ReviewStatusNotRun,
SkipReason: reason,
}
}
// NewSkippedReviewReport constructs a report representing a skipped review.
func NewSkippedReviewReport(reason string) ReviewReport {
return ReviewReport{
Status: ReviewStatusSkipped,
SkipReason: reason,
}
}
@@ -1,969 +0,0 @@
package workflow
import (
"encoding/json"
"reflect"
"testing"
)
func TestReviewFindingBlockingMatrix(t *testing.T) {
tests := []struct {
name string
verdict ReviewVerdict
level ReviewLevel
wantBlocking bool
}{
{
name: "confirmed error blocks",
verdict: ReviewVerdictConfirmed,
level: ReviewLevelError,
wantBlocking: true,
},
{
name: "confirmed warn does not block",
verdict: ReviewVerdictConfirmed,
level: ReviewLevelWarn,
wantBlocking: false,
},
{
name: "confirmed empty level does not block",
verdict: ReviewVerdictConfirmed,
level: "",
wantBlocking: false,
},
{
name: "confirmed unknown level does not block",
verdict: ReviewVerdictConfirmed,
level: "critical",
wantBlocking: false,
},
{
name: "plausible error MUST NOT block",
verdict: ReviewVerdictPlausible,
level: ReviewLevelError,
wantBlocking: false,
},
{
name: "plausible warn does not block",
verdict: ReviewVerdictPlausible,
level: ReviewLevelWarn,
wantBlocking: false,
},
{
name: "plausible empty level does not block",
verdict: ReviewVerdictPlausible,
level: "",
wantBlocking: false,
},
{
name: "empty verdict error does not block",
verdict: "",
level: ReviewLevelError,
wantBlocking: false,
},
{
name: "empty verdict warn does not block",
verdict: "",
level: ReviewLevelWarn,
wantBlocking: false,
},
{
name: "empty verdict and level does not block",
verdict: "",
level: "",
wantBlocking: false,
},
{
name: "unknown verdict with error does not block",
verdict: "likely",
level: ReviewLevelError,
wantBlocking: false,
},
{
name: "uppercase confirmed error does not block (case sensitive)",
verdict: "CONFIRMED",
level: "ERROR",
wantBlocking: false,
},
{
name: "title case confirmed error does not block (case sensitive)",
verdict: "Confirmed",
level: "Error",
wantBlocking: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := ReviewFinding{
File: "internal/domain/workflow/review_findings.go",
Line: 42,
Defect: "sample defect statement",
FailureScenario: "sample failure scenario",
Verdict: tt.verdict,
Level: tt.level,
Category: ReviewCategoryNilRisk,
}
if got := f.Blocking(); got != tt.wantBlocking {
t.Errorf("ReviewFinding.Blocking() = %v, want %v (verdict=%q, level=%q)",
got, tt.wantBlocking, tt.verdict, tt.level)
}
})
}
}
func TestReviewBlockingSlice(t *testing.T) {
tests := []struct {
name string
findings []ReviewFinding
wantBlocking bool
}{
{
name: "nil slice returns false",
findings: nil,
wantBlocking: false,
},
{
name: "empty slice returns false",
findings: []ReviewFinding{},
wantBlocking: false,
},
{
name: "single confirmed error returns true",
findings: []ReviewFinding{
{
File: "main.go",
Line: 10,
Defect: "nil pointer dereference",
FailureScenario: "running with nil config crashes",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
},
wantBlocking: true,
},
{
name: "warn-only confirmed findings do not block",
findings: []ReviewFinding{
{
File: "main.go",
Line: 10,
Defect: "unused helper function",
FailureScenario: "function is never called",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelWarn,
Category: ReviewCategoryDeadCode,
},
{
File: "util.go",
Line: 20,
Defect: "magic number in loop",
FailureScenario: "hardcoded 100 instead of constant",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelWarn,
Category: ReviewCategoryHardcodedValue,
},
},
wantBlocking: false,
},
{
name: "plausible error findings do not block",
findings: []ReviewFinding{
{
File: "server.go",
Line: 50,
Defect: "potential race condition",
FailureScenario: "concurrent writes might race if mutex is missing",
Verdict: ReviewVerdictPlausible,
Level: ReviewLevelError,
Category: ReviewCategoryRace,
},
},
wantBlocking: false,
},
{
name: "mixed findings with one confirmed error returns true",
findings: []ReviewFinding{
{
File: "doc.go",
Line: 1,
Defect: "typo in comment",
FailureScenario: "spelling mistake in doc comment",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelWarn,
Category: ReviewCategoryOther,
},
{
File: "handler.go",
Line: 45,
Defect: "unchecked error return from Write",
FailureScenario: "client disconnect causes unhandled error",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryUncheckedError,
},
{
File: "worker.go",
Line: 100,
Defect: "unbounded goroutine launch",
FailureScenario: "burst of 10k items exhausts memory",
Verdict: ReviewVerdictPlausible,
Level: ReviewLevelError,
Category: ReviewCategoryGoroutineLeak,
},
},
wantBlocking: true,
},
{
name: "confirmed error as first element returns true",
findings: []ReviewFinding{
{
File: "first.go",
Line: 1,
Defect: "blocking issue",
FailureScenario: "crashes immediately",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
{
File: "second.go",
Line: 2,
Defect: "advisory issue",
FailureScenario: "minor warning",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelWarn,
Category: ReviewCategoryOther,
},
},
wantBlocking: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ReviewBlocking(tt.findings); got != tt.wantBlocking {
t.Errorf("ReviewBlocking() = %v, want %v", got, tt.wantBlocking)
}
})
}
t.Run("large slice detects confirmed error buried among warns", func(t *testing.T) {
findings := make([]ReviewFinding, 1000)
for i := range findings {
findings[i] = ReviewFinding{
File: "file.go",
Line: i,
Defect: "warn finding",
FailureScenario: "advisory scenario",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelWarn,
Category: ReviewCategoryOther,
}
}
findings[750] = ReviewFinding{
File: "file.go",
Line: 750,
Defect: "confirmed error",
FailureScenario: "fatal crash on nil context",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
}
if !ReviewBlocking(findings) {
t.Error("ReviewBlocking() = false, want true for confirmed error at index 750")
}
})
}
func TestReviewReportStatusDistinction(t *testing.T) {
tests := []struct {
name string
report ReviewReport
wantRan bool
wantBlocking bool
wantPassed bool
}{
{
name: "ran and found zero findings passes",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{},
Summary: "adversarial review completed with 0 findings",
},
wantRan: true,
wantBlocking: false,
wantPassed: true,
},
{
name: "ran and found nil findings slice passes",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: nil,
Summary: "adversarial review completed cleanly",
},
wantRan: true,
wantBlocking: false,
wantPassed: true,
},
{
name: "ran and found only warn findings passes",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{
File: "a.go",
Line: 12,
Defect: "advisory note",
FailureScenario: "minor style difference",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelWarn,
Category: ReviewCategoryOther,
},
},
Summary: "advisories found",
},
wantRan: true,
wantBlocking: false,
wantPassed: true,
},
{
name: "ran and found plausible error findings passes",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{
File: "b.go",
Line: 34,
Defect: "hypothetical deadlock",
FailureScenario: "if channel buffers fill up",
Verdict: ReviewVerdictPlausible,
Level: ReviewLevelError,
Category: ReviewCategoryGoroutineLeak,
},
},
Summary: "plausible error found",
},
wantRan: true,
wantBlocking: false,
wantPassed: true,
},
{
name: "ran and found confirmed error blocks and does not pass",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{
File: "c.go",
Line: 56,
Defect: "unhandled error in critical section",
FailureScenario: "database error causes silent data corruption",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryUncheckedError,
},
},
Summary: "confirmed blocking error",
},
wantRan: true,
wantBlocking: true,
wantPassed: false,
},
{
name: "did not run MUST NOT be considered passed even with empty findings",
report: ReviewReport{
Status: ReviewStatusNotRun,
Findings: []ReviewFinding{},
SkipReason: "stage not reached due to prior failure",
},
wantRan: false,
wantBlocking: false,
wantPassed: false,
},
{
name: "did not run with nil findings does not pass",
report: ReviewReport{
Status: ReviewStatusNotRun,
Findings: nil,
SkipReason: "pipeline stopped early",
},
wantRan: false,
wantBlocking: false,
wantPassed: false,
},
{
name: "skipped review does not count as ran and does not pass",
report: ReviewReport{
Status: ReviewStatusSkipped,
Findings: []ReviewFinding{},
SkipReason: "documentation-only diff skipped by policy",
},
wantRan: false,
wantBlocking: false,
wantPassed: false,
},
{
name: "zero-value report does not count as ran and does not pass",
report: ReviewReport{},
wantRan: false,
wantBlocking: false,
wantPassed: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.report.Ran(); got != tt.wantRan {
t.Errorf("ReviewReport.Ran() = %v, want %v", got, tt.wantRan)
}
if got := tt.report.Blocking(); got != tt.wantBlocking {
t.Errorf("ReviewReport.Blocking() = %v, want %v", got, tt.wantBlocking)
}
if got := tt.report.Passed(); got != tt.wantPassed {
t.Errorf("ReviewReport.Passed() = %v, want %v", got, tt.wantPassed)
}
})
}
}
func TestReviewReportConstructors(t *testing.T) {
t.Run("NewCompletedReviewReport sets completed status", func(t *testing.T) {
findings := []ReviewFinding{
{
File: "pkg/foo.go",
Line: 15,
Defect: "hardcoded port",
FailureScenario: "port 8080 conflicts in container",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelWarn,
Category: ReviewCategoryHardcodedValue,
},
}
report := NewCompletedReviewReport(findings, "completed successfully")
if report.Status != ReviewStatusCompleted {
t.Errorf("Status = %q, want %q", report.Status, ReviewStatusCompleted)
}
if !report.Ran() {
t.Error("Ran() = false, want true")
}
if len(report.Findings) != 1 {
t.Errorf("len(Findings) = %d, want 1", len(report.Findings))
}
if report.Summary != "completed successfully" {
t.Errorf("Summary = %q, want %q", report.Summary, "completed successfully")
}
})
t.Run("NewCompletedReviewReport with nil findings initializes empty slice", func(t *testing.T) {
report := NewCompletedReviewReport(nil, "clean run")
if report.Findings == nil {
t.Error("Findings should be non-nil empty slice")
}
if len(report.Findings) != 0 {
t.Errorf("len(Findings) = %d, want 0", len(report.Findings))
}
if !report.Passed() {
t.Error("Passed() = false, want true for clean completed review")
}
})
t.Run("NewNotRunReviewReport sets not_run status", func(t *testing.T) {
report := NewNotRunReviewReport("prerequisite stage failed")
if report.Status != ReviewStatusNotRun {
t.Errorf("Status = %q, want %q", report.Status, ReviewStatusNotRun)
}
if report.Ran() {
t.Error("Ran() = true, want false")
}
if report.Passed() {
t.Error("Passed() = true, want false")
}
if report.SkipReason != "prerequisite stage failed" {
t.Errorf("SkipReason = %q, want %q", report.SkipReason, "prerequisite stage failed")
}
})
t.Run("NewSkippedReviewReport sets skipped status", func(t *testing.T) {
report := NewSkippedReviewReport("markdown change only")
if report.Status != ReviewStatusSkipped {
t.Errorf("Status = %q, want %q", report.Status, ReviewStatusSkipped)
}
if report.Ran() {
t.Error("Ran() = true, want false")
}
if report.Passed() {
t.Error("Passed() = true, want false")
}
if report.SkipReason != "markdown change only" {
t.Errorf("SkipReason = %q, want %q", report.SkipReason, "markdown change only")
}
})
}
func TestReviewFindingJSONRoundTrip(t *testing.T) {
original := ReviewFinding{
File: "internal/domain/workflow/steps.go",
Line: 142,
Defect: "Goroutine spawned without error channel drain causes leak",
FailureScenario: "When context is cancelled before worker exits, the goroutine remains blocked on ch <- result forever",
Verdict: ReviewVerdictConfirmed,
Disposition: ReviewDispositionFixed,
Level: ReviewLevelError,
Category: ReviewCategoryGoroutineLeak,
}
data, err := json.Marshal(original)
if err != nil {
t.Fatalf("json.Marshal(ReviewFinding) failed: %v", err)
}
var decoded ReviewFinding
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("json.Unmarshal(ReviewFinding) failed: %v", err)
}
if !reflect.DeepEqual(original, decoded) {
t.Errorf("JSON round-trip mismatch:\n got: %#v\n want: %#v", decoded, original)
}
// Explicitly assert that verdict and disposition survived the round trip
if decoded.Verdict != ReviewVerdictConfirmed {
t.Errorf("Verdict = %q, want %q", decoded.Verdict, ReviewVerdictConfirmed)
}
if decoded.Disposition != ReviewDispositionFixed {
t.Errorf("Disposition = %q, want %q", decoded.Disposition, ReviewDispositionFixed)
}
if decoded.Level != ReviewLevelError {
t.Errorf("Level = %q, want %q", decoded.Level, ReviewLevelError)
}
if decoded.Category != ReviewCategoryGoroutineLeak {
t.Errorf("Category = %q, want %q", decoded.Category, ReviewCategoryGoroutineLeak)
}
if !decoded.Blocking() {
t.Error("Decoded finding should remain blocking after round-trip")
}
}
func TestReviewReportJSONRoundTrip(t *testing.T) {
tests := []struct {
name string
report ReviewReport
}{
{
name: "completed with multiple findings",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{
File: "pkg/auth/token.go",
Line: 88,
Defect: "Missing signature verification before claims decoding",
FailureScenario: "Forged JWT with 'none' alg is accepted as valid",
Verdict: ReviewVerdictConfirmed,
Disposition: ReviewDispositionNoChangeNeeded,
Level: ReviewLevelError,
Category: ReviewCategoryInterfaceSatisfaction,
},
{
File: "pkg/auth/token.go",
Line: 120,
Defect: "Debug log prints raw token",
FailureScenario: "Verbose logging exposes bearer token in daemon logs",
Verdict: ReviewVerdictPlausible,
Disposition: ReviewDispositionSkipped,
Level: ReviewLevelWarn,
Category: ReviewCategoryOther,
},
},
Summary: "Found 1 confirmed defect and 1 advisory",
},
},
{
name: "completed with zero findings",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{},
Summary: "Clean review pass",
},
},
{
name: "zero findings with checked properties",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{},
Checked: []ReviewCheck{
{Property: "nil-safety of Foo callers", Evidence: "read all 4 callers; each nil-checks"},
{Property: "error propagation in Bar", Evidence: "traced Bar through to the caller's error return"},
},
Summary: "Checked and cleared",
},
},
{
name: "not run report",
report: ReviewReport{
Status: ReviewStatusNotRun,
SkipReason: "gate failed prior to review",
},
},
{
name: "skipped report",
report: ReviewReport{
Status: ReviewStatusSkipped,
SkipReason: "doc changes only",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.report)
if err != nil {
t.Fatalf("json.Marshal(ReviewReport) failed: %v", err)
}
var decoded ReviewReport
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("json.Unmarshal(ReviewReport) failed: %v", err)
}
// For empty slice, JSON unmarshals as nil if omitted or [] if present.
// Compare semantic state.
if decoded.Status != tt.report.Status {
t.Errorf("Status = %q, want %q", decoded.Status, tt.report.Status)
}
if decoded.Summary != tt.report.Summary {
t.Errorf("Summary = %q, want %q", decoded.Summary, tt.report.Summary)
}
if decoded.SkipReason != tt.report.SkipReason {
t.Errorf("SkipReason = %q, want %q", decoded.SkipReason, tt.report.SkipReason)
}
if decoded.Ran() != tt.report.Ran() {
t.Errorf("Ran() = %v, want %v", decoded.Ran(), tt.report.Ran())
}
if decoded.Blocking() != tt.report.Blocking() {
t.Errorf("Blocking() = %v, want %v", decoded.Blocking(), tt.report.Blocking())
}
if decoded.Passed() != tt.report.Passed() {
t.Errorf("Passed() = %v, want %v", decoded.Passed(), tt.report.Passed())
}
if len(decoded.Findings) != len(tt.report.Findings) {
t.Fatalf("len(Findings) = %d, want %d", len(decoded.Findings), len(tt.report.Findings))
}
for i := range decoded.Findings {
if !reflect.DeepEqual(decoded.Findings[i], tt.report.Findings[i]) {
t.Errorf("Findings[%d] mismatch:\n got: %#v\n want: %#v",
i, decoded.Findings[i], tt.report.Findings[i])
}
}
})
}
}
func TestReviewFindingValidation(t *testing.T) {
tests := []struct {
name string
finding ReviewFinding
wantError bool
}{
{
name: "valid full finding with confirmed verdict and error level",
finding: ReviewFinding{
File: "internal/domain/workflow/steps.go",
Line: 42,
Defect: "Nil check missing before dereferencing context",
FailureScenario: "Calling step with nil context causes panic",
Verdict: ReviewVerdictConfirmed,
Disposition: ReviewDispositionFixed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
wantError: false,
},
{
name: "valid finding without disposition (new unacted finding)",
finding: ReviewFinding{
File: "internal/domain/workflow/steps.go",
Line: 10,
Defect: "Unchecked error return",
FailureScenario: "Write failure ignored silently",
Verdict: ReviewVerdictPlausible,
Level: ReviewLevelWarn,
Category: ReviewCategoryUncheckedError,
},
wantError: false,
},
{
name: "valid finding with line 0",
finding: ReviewFinding{
File: "README.md",
Line: 0,
Defect: "Missing architecture section",
FailureScenario: "New developers cannot find component map",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelWarn,
Category: ReviewCategoryOther,
},
wantError: false,
},
{
name: "all valid checklist categories accepted",
finding: ReviewFinding{
File: "file.go",
Line: 1,
Defect: "Valid defect",
FailureScenario: "Valid scenario",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryDeadCode,
},
wantError: false,
},
{
name: "invalid empty file",
finding: ReviewFinding{
File: "",
Line: 10,
Defect: "Defect without file",
FailureScenario: "Scenario",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
wantError: true,
},
{
name: "invalid negative line",
finding: ReviewFinding{
File: "file.go",
Line: -5,
Defect: "Negative line finding",
FailureScenario: "Scenario",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
wantError: true,
},
{
name: "invalid empty defect",
finding: ReviewFinding{
File: "file.go",
Line: 10,
Defect: "",
FailureScenario: "Scenario",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
wantError: true,
},
{
name: "invalid empty failure scenario",
finding: ReviewFinding{
File: "file.go",
Line: 10,
Defect: "Defect without scenario",
FailureScenario: "",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
wantError: true,
},
{
name: "multi-line suggestion is rejected",
finding: ReviewFinding{
File: "file.go",
Line: 10,
Defect: "Defect with multi-line suggestion",
FailureScenario: "Scenario",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
Suggestion: "return nil\nmu.Lock()",
},
wantError: true,
},
{
name: "single-line suggestion on a confirmed finding is valid",
finding: ReviewFinding{
File: "file.go",
Line: 10,
Defect: "Defect with single-line suggestion",
FailureScenario: "Scenario",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
Suggestion: "mu.Lock()",
},
wantError: false,
},
{
name: "invalid verdict",
finding: ReviewFinding{
File: "file.go",
Line: 10,
Defect: "Defect with bogus verdict",
FailureScenario: "Scenario",
Verdict: "maybe",
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
wantError: true,
},
{
name: "invalid level",
finding: ReviewFinding{
File: "file.go",
Line: 10,
Defect: "Defect with bogus level",
FailureScenario: "Scenario",
Verdict: ReviewVerdictConfirmed,
Level: "critical",
Category: ReviewCategoryNilRisk,
},
wantError: true,
},
{
name: "invalid category",
finding: ReviewFinding{
File: "file.go",
Line: 10,
Defect: "Defect with bogus category",
FailureScenario: "Scenario",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: "unknown-category",
},
wantError: true,
},
{
name: "invalid disposition",
finding: ReviewFinding{
File: "file.go",
Line: 10,
Defect: "Defect with bogus disposition",
FailureScenario: "Scenario",
Verdict: ReviewVerdictConfirmed,
Disposition: "ignored",
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.finding.Validate()
if (err != nil) != tt.wantError {
t.Errorf("ReviewFinding.Validate() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
func TestReviewReportValidation(t *testing.T) {
tests := []struct {
name string
report ReviewReport
wantError bool
}{
{
name: "valid completed report with valid findings",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{
File: "file.go",
Line: 10,
Defect: "Valid defect",
FailureScenario: "Valid scenario",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
},
Summary: "valid summary",
},
wantError: false,
},
{
name: "valid not_run report",
report: ReviewReport{
Status: ReviewStatusNotRun,
SkipReason: "gate failed",
},
wantError: false,
},
{
name: "valid skipped report",
report: ReviewReport{
Status: ReviewStatusSkipped,
SkipReason: "policy skip",
},
wantError: false,
},
{
name: "invalid report status",
report: ReviewReport{
Status: "in_progress",
},
wantError: true,
},
{
name: "valid completed report with checked properties",
report: ReviewReport{
Status: ReviewStatusCompleted,
Checked: []ReviewCheck{{Property: "nil-safety of Foo callers", Evidence: "read all 4 callers"}},
},
wantError: false,
},
{
name: "checked entry without evidence is rejected",
report: ReviewReport{
Status: ReviewStatusCompleted,
Checked: []ReviewCheck{{Property: "looks fine"}},
},
wantError: true,
},
{
name: "checked entry without property is rejected",
report: ReviewReport{
Status: ReviewStatusCompleted,
Checked: []ReviewCheck{{Evidence: "read the file"}},
},
wantError: true,
},
{
name: "invalid finding inside findings slice",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{
File: "",
Line: 10,
Defect: "Missing file",
FailureScenario: "Scenario",
Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError,
Category: ReviewCategoryNilRisk,
},
},
},
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.report.Validate()
if (err != nil) != tt.wantError {
t.Errorf("ReviewReport.Validate() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
@@ -1,56 +0,0 @@
package workflow
import (
"fmt"
"strings"
)
// RenderMarkdown renders an adversarial review report as the artifact source
// the collaborative editor publishes. It is a pure function over the report:
// no I/O, no infrastructure, so the rendered form is testable in isolation.
//
// A completed review renders its summary, every finding as its own subsection
// (file, line, verdict, level, failure scenario, and the mechanical suggestion
// and disposition when present) and the verified properties as a "checked
// clean" list. A review that did not run renders only its reason, because a
// report that never executed has no findings or checks to stand behind.
func (r ReviewReport) RenderMarkdown(title string) string {
var b strings.Builder
fmt.Fprintf(&b, "# %s\n\n", title)
if r.Status == ReviewStatusNotRun || (r.SkipReason != "" && len(r.Findings) == 0 && len(r.Checked) == 0) {
fmt.Fprintf(&b, "> This review did not run: %s\n", r.SkipReason)
return strings.TrimRight(b.String(), "\n") + "\n"
}
if r.Summary != "" {
b.WriteString(r.Summary)
b.WriteString("\n\n")
}
if len(r.Findings) > 0 {
b.WriteString("## Findings\n\n")
for _, f := range r.Findings {
fmt.Fprintf(&b, "### %s:%d — %s (%s)\n\n", f.File, f.Line, f.Level, f.Verdict)
fmt.Fprintf(&b, "**Defect:** %s\n\n", f.Defect)
if f.FailureScenario != "" {
fmt.Fprintf(&b, "**Failure scenario:** %s\n\n", f.FailureScenario)
}
if f.Suggestion != "" {
fmt.Fprintf(&b, "Suggested fix: %s\n\n", f.Suggestion)
}
if f.Disposition != "" {
fmt.Fprintf(&b, "Disposition: %s\n\n", f.Disposition)
}
}
}
if len(r.Checked) > 0 {
b.WriteString("## Checked clean\n\n")
for _, c := range r.Checked {
fmt.Fprintf(&b, "- %s — %s\n", c.Property, c.Evidence)
}
}
return strings.TrimRight(b.String(), "\n") + "\n"
}
@@ -1,78 +0,0 @@
package workflow
import (
"strings"
"testing"
)
func mdFixture() ReviewReport {
disposition := ReviewDisposition("fixed")
return ReviewReport{
Status: ReviewStatusCompleted,
Summary: "The nil-deref path is reachable from a malformed payload.",
Findings: []ReviewFinding{
{
File: "handler.go", Line: 42, Defect: "nil deref on empty payload",
FailureScenario: "POST with a body lacking the field crashes the handler.",
Suggestion: "guard the field before use", Verdict: ReviewVerdictConfirmed,
Level: ReviewLevelError, Category: "correctness", Disposition: disposition,
},
{
File: "store.go", Line: 9, Defect: "query loses the filter",
FailureScenario: "paged reads return rows from other scopes",
Verdict: ReviewVerdictPlausible, Level: ReviewLevelWarn,
},
},
Checked: []ReviewCheck{
{Property: "nil-safety of Foo callers", Evidence: "read all 4 callers; each nil-checks"},
},
}
}
func TestReviewReportRendersFindingsAsMarkdown(t *testing.T) {
got := mdFixture().RenderMarkdown("Review of acme/widget#42")
for _, want := range []string{
"# Review of acme/widget#42",
"The nil-deref path is reachable",
"### handler.go:42",
"**Defect:** nil deref on empty payload",
"**Failure scenario:** POST with a body lacking the field crashes the handler.",
"error (confirmed)",
"Suggested fix: guard the field before use",
"Disposition: fixed",
"### store.go:9",
"warn (plausible)",
"## Checked clean",
"- nil-safety of Foo callers — read all 4 callers; each nil-checks",
} {
if !strings.Contains(got, want) {
t.Fatalf("rendered report missing %q:\n%s", want, got)
}
}
}
func TestReviewReportRendersNotRunStatus(t *testing.T) {
report := NewNotRunReviewReport("no reviewer model configured")
got := report.RenderMarkdown("Review of acme/widget#42")
if !strings.Contains(got, "no reviewer model configured") {
t.Fatalf("skip reason missing:\n%s", got)
}
if strings.Contains(got, "## Findings") || strings.Contains(got, "## Checked clean") {
t.Fatalf("a not-run report must not render finding or check sections:\n%s", got)
}
}
func TestReviewReportRendersACleanReview(t *testing.T) {
report := ReviewReport{
Status: ReviewStatusCompleted,
Summary: "No defects found.",
Checked: []ReviewCheck{{Property: "p", Evidence: "e"}},
}
got := report.RenderMarkdown("Review")
if strings.Contains(got, "### ") {
t.Fatalf("a zero-finding review must not render finding sections:\n%s", got)
}
if !strings.Contains(got, "No defects found.") || !strings.Contains(got, "- p — e") {
t.Fatalf("summary and checks missing:\n%s", got)
}
}
@@ -1,125 +0,0 @@
package workflow
import (
"strings"
"testing"
)
func TestRenderPRReviewSection(t *testing.T) {
tests := []struct {
name string
report ReviewReport
empty bool
want []string
}{
{
name: "zero value renders nothing",
report: ReviewReport{},
empty: true,
},
{
name: "not run renders nothing",
report: NewNotRunReviewReport("provider down"),
empty: true,
},
{
name: "zero findings is explicit",
report: NewCompletedReviewReport(nil, "checked and cleared"),
want: []string{"Adversarial self-review ran", "found no defects"},
},
{
name: "checked properties back a zero-finding review",
report: ReviewReport{
Status: ReviewStatusCompleted,
Checked: []ReviewCheck{{Property: "nil-safety of Foo callers", Evidence: "read all 4 callers"}},
},
want: []string{"found no defects", "Checked and cleared", "nil-safety of Foo callers", "read all 4 callers"},
},
{
name: "checked properties also render alongside findings",
report: ReviewReport{
Status: ReviewStatusCompleted,
Findings: []ReviewFinding{
{File: "a.go", Line: 1, Defect: "warn", FailureScenario: "x", Verdict: ReviewVerdictPlausible, Level: ReviewLevelWarn, Category: ReviewCategoryOther},
},
Checked: []ReviewCheck{{Property: "error paths", Evidence: "traced two"}},
},
want: []string{"Non-blocking notes", "Checked and cleared", "error paths"},
},
{
name: "non-blocking findings listed",
report: NewCompletedReviewReport([]ReviewFinding{
{File: "a.go", Line: 12, Defect: "style nit", FailureScenario: "x", Verdict: ReviewVerdictPlausible, Level: ReviewLevelWarn, Category: ReviewCategoryOther},
}, "one note"),
want: []string{"found no blocking defects", "a.go:12", "style nit"},
},
{
name: "whole-file finding omits line",
report: NewCompletedReviewReport([]ReviewFinding{
{File: "README.md", Defect: "typo", FailureScenario: "x", Verdict: ReviewVerdictPlausible, Level: ReviewLevelWarn, Category: ReviewCategoryOther},
}, ""),
want: []string{"README.md", "typo"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := renderPRReviewSection(tt.report)
if tt.empty {
if got != "" {
t.Fatalf("renderPRReviewSection = %q, want empty", got)
}
return
}
for _, w := range tt.want {
if !strings.Contains(got, w) {
t.Errorf("renderPRReviewSection missing %q in:\n%s", w, got)
}
}
})
}
}
func TestImplementPRBodyOmitsFindingsWhenReviewDidNotRun(t *testing.T) {
tc := &TaskContext{BuildSummary: "did the thing", Task: &Task{}}
body := implementPRBody(tc)
if strings.Contains(body, "Adversarial review") {
t.Fatalf("PR body includes a review section when no review ran:\n%s", body)
}
if !strings.Contains(body, "did the thing") {
t.Fatalf("PR body lost the build summary:\n%s", body)
}
}
func TestImplementPRBodyCarriesZeroFindingSection(t *testing.T) {
tc := &TaskContext{
BuildSummary: "did the thing",
Task: &Task{},
ReviewReport: NewCompletedReviewReport(nil, "checked and cleared"),
}
body := implementPRBody(tc)
if !strings.Contains(body, "found no defects") {
t.Fatalf("PR body missing the zero-findings review section:\n%s", body)
}
if !strings.Contains(body, "did the thing") {
t.Fatalf("PR body lost the build summary:\n%s", body)
}
}
// TestStageReviewStashesReportForPRBody pins the integration between
// StageReview and implementPRBody: a passing review must land on
// tc.ReviewReport, or the PR body silently omits the findings section
// (renderPRReviewSection returns "" for a zero-value report).
func TestStageReviewStashesReportForPRBody(t *testing.T) {
tc, _ := reviewTaskContext(t, true)
tc.Reviewer = &fakeReviewer{report: NewCompletedReviewReport(nil, "clean")}
if err := StageReview().Run(t.Context(), tc); err != nil {
t.Fatalf("StageReview().Run() error = %v", err)
}
if !tc.ReviewReport.Ran() {
t.Fatal("ReviewReport not stashed after a passing review; the PR body would omit the findings section")
}
if tc.ReviewReport.Status != ReviewStatusCompleted {
t.Errorf("ReviewReport.Status = %q, want %q", tc.ReviewReport.Status, ReviewStatusCompleted)
}
}
-203
View File
@@ -1,203 +0,0 @@
package workflow
import (
"context"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/worktree"
)
// fakeReviewer records the request it was given and returns a canned
// report, standing in for the app-layer agent.Subagent-backed Reviewer.
//
// It reads what it needs from req.SnapshotDir synchronously, inside
// Review, because the stage cleans the snapshot up as soon as Review
// returns -- a real Reviewer implementation faces the same constraint.
type fakeReviewer struct {
gotRequest ReviewRequest
snapshotContent string
snapshotHadGit bool
called bool
report ReviewReport
}
func (f *fakeReviewer) Review(_ context.Context, req ReviewRequest) ReviewReport {
f.called = true
f.gotRequest = req
if content, err := os.ReadFile(filepath.Join(req.SnapshotDir, "feature.go")); err == nil {
f.snapshotContent = string(content)
}
if _, err := os.Stat(filepath.Join(req.SnapshotDir, ".git")); err == nil {
f.snapshotHadGit = true
}
return f.report
}
// reviewTaskContext builds a TaskContext over a real git repo with a
// tracked, committed change against base, so StageReview's Diff/Snapshot
// calls have real content to work with.
func reviewTaskContext(t *testing.T, enabled bool) (*TaskContext, string) {
t.Helper()
dir := gitRepoWithOriginRef(t, "main")
if err := os.WriteFile(filepath.Join(dir, "feature.go"), []byte("package main\n"), 0o644); err != nil {
t.Fatal(err)
}
runGit(t, dir, "add", "-A")
runGit(t, dir, "commit", "-m", "feat: add feature.go")
tc := &TaskContext{
Task: &Task{ID: 1, Owner: "acme", Repo: "todo", IssueNumber: 42, Title: "Add feature", Body: "Please add it."},
Repo: config.Repo{Owner: "acme", Name: "todo", Base: "main", ReviewEnabled: enabled},
Cfg: config.Config{},
Trees: &worktree.Manager{WorkDir: t.TempDir()},
Dir: dir,
Log: slog.New(slog.DiscardHandler),
}
return tc, dir
}
func TestStageReviewIsInertUnlessEnabled(t *testing.T) {
tc, _ := reviewTaskContext(t, false)
reviewer := &fakeReviewer{}
tc.Reviewer = reviewer
if err := StageReview().Run(t.Context(), tc); err != nil {
t.Fatalf("StageReview().Run() error = %v", err)
}
if reviewer.called {
t.Error("StageReview called the reviewer despite Repo.ReviewEnabled == false")
}
if tc.Outcome.Status != "" {
t.Errorf("Outcome.Status = %q, want unset", tc.Outcome.Status)
}
}
func TestStageReviewParksWhenEnabledWithNoReviewerWired(t *testing.T) {
tc, _ := reviewTaskContext(t, true)
tc.Reviewer = nil
if err := StageReview().Run(t.Context(), tc); err != nil {
t.Fatalf("StageReview().Run() error = %v", err)
}
if tc.Outcome.Status != StatusParked {
t.Errorf("Outcome.Status = %q, want %q", tc.Outcome.Status, StatusParked)
}
}
func TestStageReviewParksFailClosedWhenReviewDidNotRun(t *testing.T) {
tc, _ := reviewTaskContext(t, true)
reviewer := &fakeReviewer{report: NewNotRunReviewReport("provider outage")}
tc.Reviewer = reviewer
if err := StageReview().Run(t.Context(), tc); err != nil {
t.Fatalf("StageReview().Run() error = %v", err)
}
if !reviewer.called {
t.Fatal("reviewer was not called")
}
if tc.Outcome.Status != StatusParked {
t.Errorf("Outcome.Status = %q, want %q", tc.Outcome.Status, StatusParked)
}
if got := tc.Outcome.Detail; !containsAll(got, "did not run", "provider outage") {
t.Errorf("Outcome.Detail = %q, want it to say the review did not run and why", got)
}
}
func TestStageReviewParksOnConfirmedErrorFinding(t *testing.T) {
tc, _ := reviewTaskContext(t, true)
blocking := ReviewFinding{
File: "feature.go", Line: 1, Defect: "nil deref",
FailureScenario: "calling Foo(nil) panics",
Verdict: ReviewVerdictConfirmed, Level: ReviewLevelError, Category: ReviewCategoryNilRisk,
}
reviewer := &fakeReviewer{report: NewCompletedReviewReport([]ReviewFinding{blocking}, "found one defect")}
tc.Reviewer = reviewer
if err := StageReview().Run(t.Context(), tc); err != nil {
t.Fatalf("StageReview().Run() error = %v", err)
}
if tc.Outcome.Status != StatusParked {
t.Errorf("Outcome.Status = %q, want %q", tc.Outcome.Status, StatusParked)
}
if got := tc.Outcome.Detail; !containsAll(got, "feature.go", "nil deref") {
t.Errorf("Outcome.Detail = %q, want the blocking finding rendered", got)
}
}
func TestStageReviewDoesNotBlockOnWarnOrPlausibleFindings(t *testing.T) {
tc, _ := reviewTaskContext(t, true)
findings := []ReviewFinding{
{
File: "feature.go", Line: 1, Defect: "could be tidier",
FailureScenario: "n/a", Verdict: ReviewVerdictConfirmed, Level: ReviewLevelWarn,
Category: ReviewCategoryOther,
},
{
File: "feature.go", Line: 2, Defect: "maybe a race",
FailureScenario: "under heavy concurrency", Verdict: ReviewVerdictPlausible, Level: ReviewLevelError,
Category: ReviewCategoryRace,
},
}
reviewer := &fakeReviewer{report: NewCompletedReviewReport(findings, "found two non-blocking findings")}
tc.Reviewer = reviewer
if err := StageReview().Run(t.Context(), tc); err != nil {
t.Fatalf("StageReview().Run() error = %v", err)
}
if tc.Outcome.Status != "" {
t.Errorf("Outcome.Status = %q, want unset (non-blocking findings must not park)", tc.Outcome.Status)
}
}
func TestStageReviewGivesReviewerAnIsolatedSnapshotDiffAndIssueText(t *testing.T) {
tc, dir := reviewTaskContext(t, true)
reviewer := &fakeReviewer{report: NewCompletedReviewReport(nil, "clean")}
tc.Reviewer = reviewer
if err := StageReview().Run(t.Context(), tc); err != nil {
t.Fatalf("StageReview().Run() error = %v", err)
}
if !reviewer.called {
t.Fatal("reviewer was not called")
}
req := reviewer.gotRequest
if req.SnapshotDir == "" {
t.Fatal("ReviewRequest.SnapshotDir is empty")
}
if req.SnapshotDir == dir {
t.Errorf("ReviewRequest.SnapshotDir = %q reuses the task worktree; want a distinct snapshot", req.SnapshotDir)
}
if reviewer.snapshotContent != "package main\n" {
t.Errorf("snapshotted feature.go = %q, want %q", reviewer.snapshotContent, "package main\n")
}
if reviewer.snapshotHadGit {
t.Error("ReviewRequest.SnapshotDir contains .git, want it stripped")
}
if req.Diff == "" {
t.Error("ReviewRequest.Diff is empty, want the feature.go diff")
}
if !containsAll(req.IssueText, "Add feature", "Please add it.") {
t.Errorf("ReviewRequest.IssueText = %q, want it to contain the task title and body", req.IssueText)
}
// The snapshot is scratch, cleaned up once the stage returns -- it
// must not leak into the task worktree's directory tree permanently.
if _, err := os.Stat(req.SnapshotDir); !os.IsNotExist(err) {
t.Errorf("ReviewRequest.SnapshotDir %q still exists after the stage returned, want it cleaned up", req.SnapshotDir)
}
}
func containsAll(s string, substrs ...string) bool {
for _, sub := range substrs {
if !strings.Contains(s, sub) {
return false
}
}
return true
}
+1 -8
View File
@@ -266,14 +266,7 @@ func OpenPR(ctx context.Context, tc *TaskContext, body string) error {
// it. This is the one point where the number is known and the worktree is
// still there to measure, so it is captured here rather than left for a
// read to reconstruct. Reporting only: captureChanges never fails the stage.
//
// The same read answers which revision this run's line numbers describe:
// nothing commits between StageReview and here, so the worktree still holds
// the head the review read, and the stage that posts line-anchored comments
// needs it to refuse a post once the PR's head has moved past it.
if stats := tc.captureChanges(ctx, capturedAfterOpenPR); stats.HeadSHA != "" {
tc.ReviewedHeadSHA = stats.HeadSHA
}
tc.captureChanges(ctx, capturedAfterOpenPR)
return nil
}
+11 -19
View File
@@ -54,6 +54,17 @@ type Forger interface {
ReplyToReview(ctx context.Context, owner, repo string, number int, commentID int64, body string) error
}
// ReviewComment is one line-anchored comment to post on an open pull
// request: the repo-relative path, the line in the reviewed revision, and
// the rendered body -- which may carry a fenced suggestion block. It names
// nothing forge-specific, so both the pr-review pipeline (prreview_stages.go)
// and the remediate workflow's callers can hand it to Forger unchanged.
type ReviewComment struct {
Path string
Line int
Body string
}
// Trees is the subset of *worktree.Manager that workflow stages call
// mid-run. *worktree.Manager (the daemon's real manager, holding the push
// token) and a hybrid RPC-backed implementation (archie-agent proxies
@@ -88,11 +99,6 @@ type TaskContext struct {
// workflow.call step: such a step in a runner with no capability
// fails the run with a named error (docs/prds/workflow-calls.md).
Calls task.Caller
// Reviewer runs the adversarial self-review stage (StageReview). Nil
// is only safe when Repo.ReviewEnabled is false; StageReview parks
// rather than silently skipping if it is enabled with no Reviewer
// wired.
Reviewer Reviewer
// PRSource fetches a pull request under review and its head snapshot for
// the pr-review workflow (see prreview_stages.go). Nil is only safe for a
// workflow that never runs pr-review's stages: they fail the run with a
@@ -172,20 +178,6 @@ type TaskContext struct {
// reported prompt-token sum was cache hits (billed at a steep discount)
// rather than fresh, full-price tokens; see formatTokenUsage.
RunUsage agentexec.Usage
// ReviewReport is the adversarial self-review stage's result, stashed
// by StageReview so StageOpenPR can render a findings section on the PR
// body (h019.6). Zero value means the review did not run (disabled or
// skipped); ReviewReport.Ran() is the test for "render the section".
ReviewReport ReviewReport
// ReviewedHeadSHA is the worktree head at the moment the pull request was
// opened, which is the revision the review's line numbers were measured on
// (StageReview runs against that same worktree and commits nothing, so no
// revision intervenes). StagePostReviewComments threads it to the forge so
// a comment whose line numbers describe a head the pull request has since
// moved past is refused instead of attaching to unrelated code. Empty when
// the revision could not be read -- posting then proceeds unverified rather
// than dropping every finding.
ReviewedHeadSHA string
}
// Emit publishes an observability event stamped with the task's
-2
View File
@@ -221,7 +221,6 @@ type RepoView struct {
PersistentStorage bool `json:"persistent_storage"`
AllowConcurrent bool `json:"allow_concurrent"`
MaxRetries int `json:"max_retries"`
ReviewEnabled bool `json:"review_enabled"`
}
// ProviderView is an LLM provider's shape, never its key. APIKeyEnv is the
@@ -517,7 +516,6 @@ func reposView(repos []config.Repo) []RepoView {
PersistentStorage: r.PersistentStorage,
AllowConcurrent: r.AllowConcurrent,
MaxRetries: r.MaxRetries,
ReviewEnabled: r.ReviewEnabled,
})
}
return out
-34
View File
@@ -349,40 +349,6 @@ func TestHandleConfigReportsLockedKeys(t *testing.T) {
}
}
// TestHandleConfigRepoViewIncludesReviewEnabled proves Repo.ReviewEnabled
// reaches the dashboard alongside AllowConcurrent and MaxRetries --
// previously it was absent from RepoView entirely, not merely unrendered
// (archie-core-b6ew.4).
func TestHandleConfigRepoViewIncludesReviewEnabled(t *testing.T) {
srv := newTestServer(t)
localView(srv, ConfigViewInput{Config: config.Config{
Repos: []config.Repo{
{Owner: "acme", Name: "widget", Base: "main", AllowConcurrent: true, MaxRetries: 3, ReviewEnabled: true},
{Owner: "acme", Name: "gadget", Base: "main"},
},
}})
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/config", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
var got ConfigView
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
if len(got.Repositories) != 2 {
t.Fatalf("Repositories = %+v, want 2", got.Repositories)
}
widget := got.Repositories[0]
if !widget.AllowConcurrent || widget.MaxRetries != 3 || !widget.ReviewEnabled {
t.Errorf("widget = %+v, want AllowConcurrent=true MaxRetries=3 ReviewEnabled=true", widget)
}
gadget := got.Repositories[1]
if gadget.AllowConcurrent || gadget.MaxRetries != 0 || gadget.ReviewEnabled {
t.Errorf("gadget = %+v, want all repo-tuning fields at their zero value", gadget)
}
}
// TestHandleConfigIncludesSchemaWithLiveValues proves the schema (archie-core-b6ew.2)
// carries the same values as the flat ConfigView fields it is built from,
// not a stale or empty catalog.