feat(workflow): wire pr-review into archie's own PRs before opening

StagePRReviewAndOpenPR runs the pr-review pipeline's decision phases
(1-8) against the task's own uncommitted change, using a new
localPRSource that reads Diff/Snapshot straight from Trees instead of
fetching an external PR -- there is no PR number yet to fetch one by,
which is why this trigger needed its own PRSource rather than the
forge-backed one afbk.7 already built for external PRs.

An unchallenged blocking finding parks the task with the findings
instead of opening a PR; a pipeline stage failure parks it the same
way any stage failure does. Once the PR opens, advisory findings post
against it -- best-effort, matching the deleted StagePostReviewComments'
convention: a posting failure is logged, not reverted into the outcome
the PR's existence already earned.

Implement() now uses this in place of the plain StageOpenPR. Bootstrap
and TDD are untouched -- the PRD names "the implement workflow"
specifically; extending to the others is a separate call.

Resolves this bead's first blocker (confirmed via read: CompilePinnedWorkflow
returns the same workflow.Workflow type and runs through the same
workflow.Run either compilation path, so a stage setting Outcome ends
the run identically regardless -- stagePROutput's posting logic had to
move inside StageOpenPR's own Run body, not follow it as a separate
Stage, which OpenPR's own doc comment already anticipated).

Still open on this bead: watched-repositories and operator triggers,
both blocked on the credential path for PRSource's real forge-backed
implementation reaching the sandboxed archie-agent process -- see the
bead comment for the concrete design (egress credential-capability
grant, narrow forge diff/archive HTTP methods) not yet built.
This commit is contained in:
sam committed 2026-09-28 13:06:42 +10:00
1 parent d73e0748c8
commit 148a764521
6 files changed
+365 -18

No files matched your search

+1 -1
View File
@@ -70,7 +70,7 @@ const (
// diffRuleDetailBytes bounds how much of a park Detail the rendered findings
// occupy, matching the store's own park-reason cap headroom (see
// reviewDetailBytes in review.go).
// parkDetailBytes in prreview_own_pr.go).
const diffRuleDetailBytes = 4000
// DiffRule is one declarative rule applied to the added lines of the committed
+1 -1
View File
@@ -197,7 +197,7 @@ func Implement() Workflow {
}),
StageYaegiGate(),
StageDiffCap(),
StageOpenPR(implementPRBody),
StagePRReviewAndOpenPR(implementPRBody),
},
}
}
@@ -41,6 +41,7 @@ var outcomeSites = []outcomeSite{
{"diff_rules.go", "StatusParked", "the diff breaks one of the repo's blocking rules"},
{"feasibility.go", "StatusClosedWontDo", "feasibility decided this work should not be done"},
{"feasibility.go", "StatusWaitingHuman", "the PRD is delivered and the run waits for a go/no-go"},
{"prreview_own_pr.go", "StatusParked", "archie's own PR trigger found an unchallenged blocking finding before opening the PR"},
{"prreview_stages.go", "StatusCompleted", "the pr-review pipeline posted its findings (or found none to post)"},
{"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"},
+153
View File
@@ -0,0 +1,153 @@
package workflow
import (
"context"
"fmt"
"strings"
"github.com/samcharles93/archie-core/internal/domain/workflow/prreview"
)
// localPRSource implements PRSource against the task's own worktree instead
// of an external forge fetch: archie's own PRs trigger (docs/prds/pr-review-
// agent.md, Triggers: "the implement workflow runs the pipeline before
// opening its PR") reviews a change that has no PR number yet, so there is
// nothing for a forge-backed PRSource to fetch by. Trees already holds
// everything the pipeline's phase 1/2 need: the diff against base, and a
// snapshot of the (as yet unopened) PR's head.
type localPRSource struct {
tc *TaskContext
}
var _ PRSource = (*localPRSource)(nil)
// Metadata returns the task's own title and body as the PR's title and body
// -- the same text OpenPR uses to open the real PR moments later. Commit
// messages are left empty, matching the external PRSource implementation's
// precedent (internal/infrastructure/prsource): neither Trees nor Task
// carries them structured, and they are only one of three hallucination-check
// inputs, not load-bearing alone.
func (s *localPRSource) Metadata(context.Context, string, string, int) (PRMetadata, error) {
return PRMetadata{Title: s.tc.Task.Title, Body: s.tc.Task.Body}, nil
}
// Diff returns the task's own worktree diff against its base branch.
func (s *localPRSource) Diff(ctx context.Context, _, _ string, _ int) (string, error) {
return s.tc.Trees.Diff(ctx, s.tc.Dir, s.tc.Repo.BaseBranch())
}
// Snapshot exports the task's own worktree HEAD, no .git present, and
// reports the commit SHA it was measured on -- read through the same
// changeStatsReader capability captureChanges already uses, since Trees'
// core contract has no head-SHA method. A Trees implementation without that
// capability (a test fake, most commonly) snapshots successfully but
// reports an empty SHA; postPRReview already treats an empty headSHA as "not
// measured" rather than fails the run over it, matching captureChanges'
// same convention.
func (s *localPRSource) Snapshot(ctx context.Context, _, _ string, _ int, destDir string) (string, error) {
if err := s.tc.Trees.Snapshot(ctx, s.tc.Dir, destDir); err != nil {
return "", err
}
reader, ok := s.tc.Trees.(changeStatsReader)
if !ok {
return "", nil
}
stats, err := reader.ChangedFileStats(ctx, s.tc.Dir, s.tc.Repo.BaseBranch())
if err != nil {
return "", nil //nolint:nilerr // head SHA is best-effort provenance, not a required result
}
return stats.HeadSHA, nil
}
// parkDetailBytes bounds how much of a park Detail the rendered blocking
// findings occupy, matching the deleted adversarial-self-review's
// reviewDetailBytes (diff_rules.go's own park Detail cap headroom comment
// still points at that constant by name; it named this cap, not a specific
// symbol).
const parkDetailBytes = 4000
// unchallengedBlockingFindings returns the scored findings the merge gate
// marked blocking that the adversary did not challenge -- confirmed or never
// reviewed by the adversary both count, since only an explicit challenge
// argues the finding away (docs/prds/pr-review-agent.md, Triggers: "archie's
// own PRs").
func unchallengedBlockingFindings(scored []prreview.ScoredFinding) []prreview.ScoredFinding {
var out []prreview.ScoredFinding
for _, f := range scored {
if f.Blocking && f.Adversary != prreview.AdversaryChallenged {
out = append(out, f)
}
}
return out
}
// renderBlockingFindingsDetail formats the unchallenged blocking findings
// that parked the task, in the same shape the deleted adversarial-self-
// review's renderReviewDetail used, so a park reason here reads the way an
// operator following this repository already expects.
func renderBlockingFindingsDetail(findings []prreview.ScoredFinding) string {
var b strings.Builder
b.WriteString("pr-review found unchallenged blocking defects:\n")
for _, f := range findings {
fmt.Fprintf(&b, "- %s:%d %s (%s)\n", f.File, f.LineStart, f.Title, f.Body)
}
return clip(b.String(), parkDetailBytes)
}
// StagePRReviewAndOpenPR runs the pr-review pipeline's decision phases (1-8)
// against the task's own uncommitted change, then opens the PR body builds
// and posts the pipeline's advisory findings against it -- archie's own PRs
// trigger (docs/prds/pr-review-agent.md, Triggers). An unchallenged blocking
// finding parks the task with the findings instead of opening a PR; a
// pipeline stage failure parks the task the same way any other stage failure
// does. Once the PR is open, posting failure is logged and does not revert
// the outcome the PR's existence already earned -- the same best-effort
// convention the deleted StagePostReviewComments followed.
//
// This must run every decision stage from inside one Stage.Run body rather
// than as separate Stage entries in the workflow's list: the engine ends a
// run the instant any stage sets tc.Outcome (workflow.go's Run loop), so a
// stage that opens the PR could never be reached if the review phases were
// spliced in as their own preceding stages -- OpenPR's own doc comment names
// this exact pattern ("call this and then do so in the same stage").
func StagePRReviewAndOpenPR(body func(*TaskContext) string) Stage {
return stagePRReviewAndOpenPR(prReviewDecisionStages(), body)
}
// stagePRReviewAndOpenPR is StagePRReviewAndOpenPR's implementation, taking
// the decision-stage list as a parameter so tests can substitute a small
// fake pipeline instead of scripting all nine real phases' agent calls.
func stagePRReviewAndOpenPR(decisionStages []Stage, body func(*TaskContext) string) Stage {
return Stage{Name: "pr-review-and-open-pr", Run: func(ctx context.Context, tc *TaskContext) error {
if tc.PRSource == nil {
tc.PRSource = &localPRSource{tc: tc}
}
for _, stage := range decisionStages {
if err := stage.Run(ctx, tc); err != nil {
return fmt.Errorf("pr-review (%s): %w", stage.Name, err)
}
if tc.Outcome.Status != "" {
// A decision stage ended the run itself (operator-approval's
// waiting_human, or a decision stage's own park). Honor it
// rather than proceed to open a PR the operator has not
// cleared yet.
return nil
}
}
if blocking := unchallengedBlockingFindings(tc.prReview.scored); len(blocking) > 0 {
tc.Outcome = Outcome{Status: StatusParked, Detail: renderBlockingFindingsDetail(blocking)}
return nil
}
if err := OpenPR(ctx, tc, body(tc)); err != nil {
return err
}
if _, err := runPROutputPhase(ctx, tc); err != nil {
tc.Log.Error("pr-review findings not posted; the pull request is open regardless", "err", err,
"pr", tc.Task.PRNumber)
}
return nil
}}
}
@@ -0,0 +1,176 @@
package workflow
import (
"context"
"errors"
"log/slog"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow/prreview"
)
var errBoom = errors.New("boom")
// fakeDecisionStage is a minimal decision-stage substitute: it runs fn, so a
// test can seed tc.prReview / tc.Outcome / return an error without scripting
// a full nine-phase pipeline's agent calls.
func fakeDecisionStage(name string, fn func(tc *TaskContext) error) Stage {
return Stage{Name: name, Run: func(_ context.Context, tc *TaskContext) error { return fn(tc) }}
}
func ownPRTaskContext(t *testing.T) (*TaskContext, *fakeForge) {
t.Helper()
forge := &fakeForge{}
tc := &TaskContext{
Task: &Task{ID: 1, Owner: "acme", Repo: "widgets", Title: "add a thing", Body: "does the thing"},
Repo: configRepoWithBase(t),
Forge: forge,
Trees: &fakeTrees{diff: smallDiff},
Store: &threadSafeStore{},
Log: slog.New(slog.DiscardHandler),
}
return tc, forge
}
func TestStagePRReviewAndOpenPRParksOnUnchallengedBlockingFinding(t *testing.T) {
tc, forge := ownPRTaskContext(t)
seed := fakeDecisionStage("seed", func(tc *TaskContext) error {
tc.prReview = &prReviewState{scored: []prreview.ScoredFinding{
{Title: "sql injection", File: "a.go", LineStart: 3, Blocking: true},
}}
return nil
})
stage := stagePRReviewAndOpenPR([]Stage{seed}, func(*TaskContext) string { return "body" })
if err := stage.Run(context.Background(), tc); err != nil {
t.Fatalf("stage: %v", err)
}
if tc.Outcome.Status != StatusParked {
t.Fatalf("outcome = %q, want %q", tc.Outcome.Status, StatusParked)
}
if len(forge.calls) != 0 {
t.Errorf("forge calls = %v, want none: an unchallenged blocking finding must not open a PR", forge.calls)
}
}
func TestStagePRReviewAndOpenPRProceedsWhenBlockingFindingWasChallenged(t *testing.T) {
tc, forge := ownPRTaskContext(t)
seed := fakeDecisionStage("seed", func(tc *TaskContext) error {
tc.prReview = &prReviewState{scored: []prreview.ScoredFinding{
{Title: "false alarm", File: "a.go", LineStart: 3, Blocking: true, Adversary: prreview.AdversaryChallenged},
}}
return nil
})
stage := stagePRReviewAndOpenPR([]Stage{seed}, func(*TaskContext) string { return "body" })
if err := stage.Run(context.Background(), tc); err != nil {
t.Fatalf("stage: %v", err)
}
if tc.Outcome.Status != StatusPROpen {
t.Fatalf("outcome = %q, want %q (a challenged blocking finding must not park)", tc.Outcome.Status, StatusPROpen)
}
if forge.prNumber == 0 {
t.Error("no PR was opened")
}
}
func TestStagePRReviewAndOpenPROpensAndPostsCleanReview(t *testing.T) {
tc, forge := ownPRTaskContext(t)
seed := fakeDecisionStage("seed", func(tc *TaskContext) error {
tc.prReview = &prReviewState{scored: []prreview.ScoredFinding{
{Title: "nit", File: "a.go", LineStart: 3, Body: "tidy this up"},
}}
return nil
})
stage := stagePRReviewAndOpenPR([]Stage{seed}, func(*TaskContext) string { return "the pr body" })
if err := stage.Run(context.Background(), tc); err != nil {
t.Fatalf("stage: %v", err)
}
if tc.Outcome.Status != StatusPROpen {
t.Fatalf("outcome = %q, want %q", tc.Outcome.Status, StatusPROpen)
}
if forge.prNumber != 1 {
t.Fatalf("PR number = %d, want 1", forge.prNumber)
}
if forge.reviewCalls != 1 {
t.Fatalf("review comment calls = %d, want 1: the advisory finding must post after the PR opens", forge.reviewCalls)
}
if forge.reviewNumber != forge.prNumber {
t.Errorf("review posted against PR %d, want %d", forge.reviewNumber, forge.prNumber)
}
}
func TestStagePRReviewAndOpenPRHonorsAnEarlyOutcomeFromADecisionStage(t *testing.T) {
tc, forge := ownPRTaskContext(t)
seed := fakeDecisionStage("seed", func(tc *TaskContext) error {
tc.Outcome = Outcome{Status: StatusWaitingHuman, Detail: "awaiting operator review"}
return nil
})
stage := stagePRReviewAndOpenPR([]Stage{seed}, func(*TaskContext) string {
t.Fatal("PR body should never be built: a decision stage already ended the run")
return ""
})
if err := stage.Run(context.Background(), tc); err != nil {
t.Fatalf("stage: %v", err)
}
if tc.Outcome.Status != StatusWaitingHuman {
t.Fatalf("outcome = %q, want %q (the decision stage's own outcome must survive)", tc.Outcome.Status, StatusWaitingHuman)
}
if len(forge.calls) != 0 {
t.Errorf("forge calls = %v, want none", forge.calls)
}
}
func TestStagePRReviewAndOpenPRParksOnADecisionStageFailure(t *testing.T) {
tc, forge := ownPRTaskContext(t)
seed := fakeDecisionStage("seed", func(*TaskContext) error {
return errBoom
})
stage := stagePRReviewAndOpenPR([]Stage{seed}, func(*TaskContext) string { return "body" })
if err := stage.Run(context.Background(), tc); err == nil {
t.Fatal("want an error: a failed decision stage must fail this stage (the engine parks on a stage error)")
}
if len(forge.calls) != 0 {
t.Errorf("forge calls = %v, want none", forge.calls)
}
}
func TestLocalPRSourceReadsFromTheTasksOwnWorktree(t *testing.T) {
tc, _ := ownPRTaskContext(t)
src := &localPRSource{tc: tc}
meta, err := src.Metadata(context.Background(), "", "", 0)
if err != nil {
t.Fatalf("Metadata: %v", err)
}
if meta.Title != tc.Task.Title || meta.Body != tc.Task.Body {
t.Errorf("Metadata = %+v, want the task's own title/body", meta)
}
diff, err := src.Diff(context.Background(), "", "", 0)
if err != nil {
t.Fatalf("Diff: %v", err)
}
if diff != smallDiff {
t.Errorf("Diff = %q, want the worktree's own diff", diff)
}
headSHA, err := src.Snapshot(context.Background(), "", "", 0, t.TempDir())
if err != nil {
t.Fatalf("Snapshot: %v", err)
}
if headSHA != "" {
t.Errorf("headSHA = %q, want empty: fakeTrees does not implement changeStatsReader", headSHA)
}
}
// configRepoWithBase builds a minimal config.Repo whose BaseBranch()
// resolves without a live git call.
func configRepoWithBase(t *testing.T) config.Repo {
t.Helper()
return config.Repo{Base: "main"}
}
+33 -16
View File
@@ -628,30 +628,47 @@ func stagePRSynthesis() Stage {
// The polish pass is what a budget-exhausted run skips; posting itself
// always runs, since it is the pipeline's only externally visible act and
// must report what happened even when every other phase was skipped.
//
// Used by the standalone pr-review workflow only. Archie's own PRs trigger
// (StagePRReviewAndOpenPR, prreview_own_pr.go) needs the same polish-then-post
// work but must not overwrite the StatusPROpen outcome OpenPR already set, so
// it calls runPROutputPhase directly instead of this Stage.
func stagePROutput() Stage {
return Stage{Name: "output", Run: func(ctx context.Context, tc *TaskContext) error {
if budgetExhausted(tc, "output") {
skipPhase(tc, "output")
} else {
polished := make([]prreview.ScoredFinding, len(tc.prReview.scored))
forEachBounded(prReviewConcurrency, len(tc.prReview.scored), func(i int) {
polished[i] = polishFinding(ctx, tc, tc.prReview.scored[i])
})
tc.prReview.scored = polished
}
if err := postPRReview(ctx, tc); err != nil {
return fmt.Errorf("post review: %w", err)
}
detail := fmt.Sprintf("posted %d finding(s) as %s", len(tc.prReview.scored), prreview.ReviewEventFor(tc.prReview.scored))
if len(tc.prReview.skippedPhases) > 0 {
detail += fmt.Sprintf("; skipped: %s", strings.Join(tc.prReview.skippedPhases, ", "))
detail, err := runPROutputPhase(ctx, tc)
if err != nil {
return err
}
tc.Outcome = Outcome{Status: StatusCompleted, Detail: detail}
return nil
}}
}
// runPROutputPhase is phase 9's actual work, factored out of stagePROutput so
// a caller that must preserve its own outcome (archie's own PRs, which has
// already set StatusPROpen by the time findings are ready to post) can run it
// without stagePROutput's unconditional StatusCompleted assignment.
func runPROutputPhase(ctx context.Context, tc *TaskContext) (string, error) {
if budgetExhausted(tc, "output") {
skipPhase(tc, "output")
} else {
polished := make([]prreview.ScoredFinding, len(tc.prReview.scored))
forEachBounded(prReviewConcurrency, len(tc.prReview.scored), func(i int) {
polished[i] = polishFinding(ctx, tc, tc.prReview.scored[i])
})
tc.prReview.scored = polished
}
if err := postPRReview(ctx, tc); err != nil {
return "", fmt.Errorf("post review: %w", err)
}
detail := fmt.Sprintf("posted %d finding(s) as %s", len(tc.prReview.scored), prreview.ReviewEventFor(tc.prReview.scored))
if len(tc.prReview.skippedPhases) > 0 {
detail += fmt.Sprintf("; skipped: %s", strings.Join(tc.prReview.skippedPhases, ", "))
}
return detail, nil
}
// polishFinding runs one phase-9 polish call, tightening a finding's wording.
// It keeps the original finding unchanged on any failure, per the PRD.
func polishFinding(ctx context.Context, tc *TaskContext, f prreview.ScoredFinding) prreview.ScoredFinding {