Files
archie/internal/domain/workflow/changes_test.go
T
sam c29cdf6f79 feat(workflow): post line-anchored review comments with suggested fixes (#891)
* feat(workflow): post line-anchored review comments with suggested fixes

Bead archie-core-q9au (docs/prds/inline-review.md). A review's findings reached
the operator only as a flat list in the PR body, so the author had to map every
finding back to its line by hand and apply mechanical fixes by hand. Now the
line-anchored findings are also posted as inline review comments, carrying a
one-click suggestion when the reviewer could state the fix.

Domain (internal/domain/workflow):
- ReviewFinding gains Suggestion; Validate rejects one on anything but a
  confirmed finding, so the agent's record_finding call is refused with
  something it can act on. Validate splits into validateAnchor /
  validateClassification / validateDisposition, which also brings it back under
  the cyclop limit the gate enforces.
- StagePostReviewComments runs after StageOpenPR in the implement workflow. It
  posts only line-anchored findings (Line > 0); a whole-file finding has no line
  to attach to and stays in the body list, which remains the complete record.
  It is best-effort: the PR is open and the body already lists every finding, so
  a failed comment is logged, never a park -- the same reasoning as OpenPR's
  existing best-effort LinkBranch call.
- The suggestion fence is gated on Verdict == confirmed where the fence is
  written, because it is a one-click apply: a wrong suggestion breaks the code
  and the author has to notice.

Forge (internal/forge): ReviewCommentWriter + GitHub/Gitea implementations,
type-asserted like PullRequestReader so a forge that cannot is skipped rather
than faked (NoopForge is pinned as not satisfying it). GitHub loops one
CreateComment per comment and joins the failures, so one refused anchor cannot
drop the rest; Gitea carries them on a single COMMENT-state review.

Two deliberate departures from the PRD's sketch, both recorded in it:
- The interface is batch (CreateReviewComments), not one call per comment:
  Gitea's only inline shape is one submitted review, and the set then crosses
  NATS once instead of once per finding.
- The head SHA is resolved inside each forge implementation rather than by the
  caller. The stage runs in archie-agent, which holds no forge credentials and
  has no PullRequestReader; the daemon-side forgerpc handler is the only place
  that does.

Transport (internal/forgerpc): workflow.Forger gains the method, so the agent
reaches the daemon over the new create_review_comments subject; the server
type-asserts the writer and answers an incapable forge with an error the stage
logs. This is the one proxied method whose absence on an image skew does not
park a task.

The reviewer prompt and record_finding schema ask for a suggestion on a
confirmed, mechanically fixable defect only.

Regenerating the wfextract symbol table -- the documented way a new exported
workflow symbol reaches interpreted stage plugins -- also picked up symbols that
were missing from the committed table (the Status* and Source* constants,
ReviewCheck, Store, Task, and the _Store wrapper). Nil-guards were re-applied to
every wrapper afterwards, as that file's header requires.

Evidence: task check exit 0 (incl. 229 UI tests). Three mutations of the new
stage logic each fail a test that names the behaviour -- dropping the
confirmed-only gate, dropping the Line > 0 filter, and letting a posting failure
escape -- so the tests discriminate rather than passing vacuously.

* fix(build): drop the Dockerfile pin assertion

The guard fetched golangci-lint's go.mod at image build time to fail the build
when GOLANGCI_LINT_VERSION and GOFUMPT_VERSION disagreed. It only ever tripped
on self-inflicted pin drift, but it added a network fetch and a new failure
mode to the release build, which is not where that belongs.

The pins stay correct (golangci-lint 2.13.2, whose vendored gofumpt v0.11.0
equals GOFUMPT_VERSION) and the comment above them still states the invariant,
so the reasoning survives without the build-time machinery. The pin change
itself reaches the agent image through the normal release path: CI rebuilds
and pushes archie-agent on an archie/vX.Y.Z release, and a gateway-only
release deliberately does not.

* feat(workflow): add the remediate workflow for PR review remediation

Implements step 5 of docs/prds/pr-review-remediation.md: a remediate
workflow that resumes the task's existing worktree onto its PR branch
(worktree.Manager.Resume, not Prepare's refresh), runs a builder agent
against the review's actionable comments, commits and pushes to the
same branch, and replies via the forge's ReplyToReview. Task.RetryCount
doubles as the remediation round counter, capped by the repo's
max_retries; the round-cap stage parks the task and posts one comment
when it's hit.

Task gains a ReviewPayload field (JSON-encoded review unit) with full
store/proto/staterpc plumbing. workflow.Forger gains Comment and
ReplyToReview, proxied to archie-agent via a new forgerpc RPC. Resume
and its Dir lookup are added as an unexported optional Trees capability
rather than widening the exported interface, which is already at
golangci-lint's interfacebloat cap and is projected into wfextract's
Yaegi symbol table for repository-authored stage plugins.

The daemon-side reaction consumer that dispatches into this workflow
(subscribing ARCHIE_REACTIONS, grouping comments by review, enforcing
per-task serialization) is tracked separately in archie-core-8li9.4.

* feat(messaging): extract standalone messaging service binary and process composition

* feat(messaging): adapt channel frontends to consume Gateway ChatContract

* feat(messaging): remove in-process channels and Router from daemon bootstrap

Telegram, email and webhook now run only in the Messaging Service, which
reaches this process through the Gateway's ChatContract. The daemon builds
no channel and no gateway.Router, so the wiring that existed only to serve
them goes with it: setupGateways, setupEmailGateway, setupWebhookGateway,
setupTelegramGateway, buildTelegramRouter, makeTelegramReload and the
release-announcement path helper.

The seams those paths owned move to where they can still be honoured:

- Channel ValidateConfig runs in archiemessaging.compose, which now returns
  an error, so a configured-but-invalid front-end fails the service rather
  than being dropped silently.
- The inbound rate limiter moves to the Gateway process, which owns the only
  Router and therefore the only place a per-sender budget applies to every
  channel's turns.
- The daemon's gateway readiness probe becomes a ContractProbe against the
  Gateway. It cannot observe a channel lifecycle it no longer runs, and a
  channel-count probe would have reported every daemon degraded.
- telegramSetup is now chatSetup (in chat_setup.go), trimmed to the fields a
  chat turn runner actually reads.

Telegram operator features that were wired only in the daemon (/update,
release announcements, version reporting, SIGHUP reload) are not yet wired
in the Messaging Service: archie-core-8cda.6.6. Channel lifecycle reporting:
archie-core-8cda.6.8. The Gateway process builds no worktree manager, so
review_pr is unadvertised on every channel: archie-core-8cda.6.7.

* feat(messaging): re-wire the Telegram operator surface in the Messaging Service

setupTelegramGateway satisfied eight telegram.Gateway seams from the daemon's
own process. Each is resolved by where the fact it needs is owned, not by
where it used to be built; the decision table is in
docs/architecture/migration-decisions.md.

Wired here: /update (built from this service's own [chat.telegram] check and
install commands), the update-report relay (same sha256(bot_user)[:8] path the
daemon wrote, so a pending report is found rather than written afresh),
show_tool_calls, and /restart's config reload, which re-resolves the token and
allowlist from this service's own file and refuses a reload that resolves to
an empty token.

/version is sourced from the Gateway: ChatSnapshot gains a Version field
carrying the Gateway's rendered build block, read per call and bounded by the
dependency timeout. cmd/archie-messaging deliberately takes no version
ldflags, so it cannot report a partial upgrade as a matched one.

RunningVersions and ReleaseAnnouncements stay nil. Both need a component's
real running version, and the only versions this process can obtain are a
different binary's stamps -- reporting them would manufacture exactly the
false success the update check exists to catch, and the announcer without
parseable versions is a no-op that reads as configured. archie-core-8cda.6.9
carries the contract that unblocks them. Dangerous stays nil: no composition
has ever supplied a command authority.

Enrich is dropped from the update service: componentInstallTypeEnricher read
[nats], which this service must not decode, so the check command's own
reported install type stands.

* fix(workflow): guard inline review comments against head drift

Two findings from the review of q9au.

A comment's line numbers describe exactly one revision of the branch, but the
forge resolved the pull request's head at posting time -- after the review had
already read the worktree. A push in that window (a collaborator, a retry) meant
the findings were anchored to whatever now occupied those lines, mislabelling
instead of failing. The revision the review measured now travels with the
comment set: OpenPR records it from the same worktree read it already performs
(TaskContext.ReviewedHeadSHA), StagePostReviewComments threads it through
Forger to forgerpc, and the forge refuses the whole set once the head it reads
has moved off it -- which the best-effort stage logs, the PR body remaining the
record. An unmeasured revision posts unverified rather than dropping every
finding, so a worktree that cannot report one degrades instead of regressing.

Suggestion was documented single-line only, because GitHub applies a suggestion
fence up to its first line and a multi-line replacement would silently corrupt
the file. Only the verdict was validated, so a multi-line suggestion reached the
renderer and posted as that truncated fence. Validate now rejects an embedded
newline, which makes record_finding refuse it with feedback the model can act on
instead of the reviewer discovering the corruption later.
2026-09-19 23:37:23 +10:00

625 lines
23 KiB
Go

package workflow
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/logging"
)
// recordingStore is the narrow Store the engine calls mid-run, keeping the
// events it was asked to persist. A capture is durable provenance, so the
// assertions below read this and not the lossy bus.
type recordingStore struct {
events []events.Event
transitions []transition
insertEr error
}
// transition is one status change the engine asked the store for. Parking is
// observable only through these, which is what makes "a reporting failure
// never parks the task" a test rather than a reading of the code.
type transition struct{ from, to, detail string }
func (s *recordingStore) Update(context.Context, *Task) error { return nil }
func (s *recordingStore) Transition(_ context.Context, _ int64, from, to, detail string) error {
s.transitions = append(s.transitions, transition{from: from, to: to, detail: detail})
return nil
}
func (s *recordingStore) InsertEvent(_ context.Context, e events.Event) (int64, error) {
if s.insertEr != nil {
return 0, s.insertEr
}
s.events = append(s.events, e)
return int64(len(s.events)), nil
}
func (s *recordingStore) ofKind(kind string) []events.Event {
var out []events.Event
for _, e := range s.events {
if e.Kind == kind {
out = append(out, e)
}
}
return out
}
// capturingTrees is a Trees implementation that can also report what changed.
// fakeTrees deliberately stays without the capability, so every existing test
// that drives a stage through it exercises the degrade path.
type capturingTrees struct {
fakeTrees
stats task.ChangeStats
err error
calls int
dir string
base string
}
func (c *capturingTrees) ChangedFileStats(_ context.Context, dir, base string) (task.ChangeStats, error) {
c.calls++
c.dir, c.base = dir, base
return c.stats, c.err
}
var (
_ Trees = (*fakeTrees)(nil)
_ Trees = (*capturingTrees)(nil)
_ changeStatsReader = (*capturingTrees)(nil)
_ task.Store = (*recordingStore)(nil)
)
// capturePayload mirrors the frozen persisted shape of a changes_captured
// event. Decoding the event's data through it is what proves the field names
// a reader depends on.
type capturePayload struct {
Schema string `json:"schema"`
Owner string `json:"owner"`
Repo string `json:"repo"`
Base string `json:"base"`
Branch string `json:"branch"`
HeadSHA string `json:"head_sha"`
BaseSHA string `json:"base_sha"`
PRNumber int `json:"pr_number"`
CapturedAfter string `json:"captured_after"`
Files []task.FileChange `json:"files"`
Totals task.ChangeTotals `json:"totals"`
Truncated bool `json:"truncated"`
}
func decodeCapture(t *testing.T, e events.Event) capturePayload {
t.Helper()
raw, err := json.Marshal(e.Data)
if err != nil {
t.Fatalf("marshal capture data: %v", err)
}
var payload capturePayload
if err := json.Unmarshal(raw, &payload); err != nil {
t.Fatalf("decode capture data: %v", err)
}
return payload
}
func sampleStats() task.ChangeStats {
return task.ChangeStats{
BaseSHA: strings.Repeat("a", 40),
HeadSHA: strings.Repeat("b", 40),
Files: []task.FileChange{
{Path: "internal/x.go", Status: task.ChangeModified, Additions: 12, Deletions: 3},
{Path: "docs/y.md", OldPath: "docs/z.md", Status: task.ChangeRenamed},
},
Totals: task.ChangeTotals{Files: 2, Additions: 12, Deletions: 3},
}
}
func captureTaskContext(t *testing.T, trees Trees, store Store) *TaskContext {
t.Helper()
return &TaskContext{
Task: &Task{
ID: 11, Attempt: 2, Owner: "acme", Repo: "widget", IssueNumber: 7,
Stage: "commit-push", Branch: "feat/7-widget",
},
Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"},
Trees: trees,
Store: store,
Dir: "/worktree",
Branch: "feat/7-widget",
Log: slog.New(slog.DiscardHandler),
}
}
// TestRunStampsTheAttemptOnEveryEventItEmits is R2's producer half. The
// attempt is read from the task record the run holds, so an event
// is attributable to one attempt without segmenting the stream by stage order,
// and nothing here guesses an attempt it does not have.
func TestRunStampsTheAttemptOnEveryEventItEmits(t *testing.T) {
bus := events.NewBus()
sub := bus.Subscribe(32)
t.Cleanup(sub.Close)
tc := &TaskContext{
Task: &Task{ID: 4, Attempt: 3, Owner: "o", Repo: "r", IssueNumber: 1, Status: StatusRunning},
Repo: config.Repo{Owner: "o", Name: "r"},
Store: &recordingStore{},
Bus: bus,
Log: slog.New(slog.DiscardHandler),
}
Run(context.Background(), Workflow{Name: "w", Stages: []Stage{{Name: "build", Run: func(context.Context, *TaskContext) error {
return fmt.Errorf("boom")
}}}}, tc)
var emitted []events.Event
for {
select {
case e := <-sub.C:
emitted = append(emitted, e)
default:
if len(emitted) == 0 {
t.Fatal("run emitted no events at all; the assertion below would be vacuous")
}
for _, e := range emitted {
if e.Attempt != 3 {
t.Errorf("event %s attempt = %d, want 3", e.Kind, e.Attempt)
}
}
return
}
}
}
// TestRunBindsTheStageOntoTheTaskLoggerOnlyForTheStage is R5's producer half:
// a line a stage's own code writes must be selectable by stage, and a line
// written outside any stage must not be attributed to one.
//
// It asserts against the logging entry the reader actually filters on
// (internal/logging.Query.Stage matches Entry.Fields["stage"]) rather than
// against a rendered line, so a change in the handler cannot make this pass
// while the filter stays broken.
func TestRunBindsTheStageOntoTheTaskLoggerOnlyForTheStage(t *testing.T) {
feed := logging.NewFeed(32)
outer := slog.New(logging.NewFeedHandler(slog.NewJSONHandler(discardSink{}, nil), feed))
tc := &TaskContext{
Task: &Task{ID: 4, Attempt: 1, Owner: "o", Repo: "r", IssueNumber: 1, Status: StatusRunning},
Repo: config.Repo{Owner: "o", Name: "r"},
Store: &recordingStore{},
Log: outer,
}
Run(context.Background(), Workflow{Name: "w", Stages: []Stage{{Name: "build", Run: func(_ context.Context, tc *TaskContext) error {
tc.Log.Info("inside the stage")
return nil
}}}}, tc)
stages := map[string]string{}
for _, entry := range feed.Snapshot() {
stage, _ := entry.Fields["stage"].(string)
stages[entry.Message] = stage
}
starting, ok := stages["stage starting"]
if !ok {
t.Fatalf("no stage-start line was logged at all: %v", stages)
}
if starting != "build" {
t.Errorf("stage starting carries stage %q, want build", starting)
}
if got := stages["inside the stage"]; got != "build" {
t.Errorf("a line logged by the stage's own code carries stage %q, want build", got)
}
// Restored afterwards: a line the engine writes outside a stage is not
// attributable to one, and claiming it were would make a stage filter lie.
if tc.Log != outer {
t.Error("tc.Log was not restored after the stage; the next stage's lines would inherit a stale stage")
}
outer.Info("after the run")
entries := feed.Snapshot()
last := entries[len(entries)-1]
if last.Message != "after the run" {
t.Fatalf("last entry = %q, want the line logged after the run", last.Message)
}
if stage, ok := last.Fields["stage"]; ok {
t.Errorf("a line logged after the run carries stage %v, want none", stage)
}
}
// discardSink absorbs what the wrapped JSON handler renders, so this test's
// assertions read the feed and never a terminal. It cannot be io.Discard or
// slog.DiscardHandler: FeedHandler.Enabled delegates to the wrapped handler,
// and slog.DiscardHandler reports every record as disabled, which leaves the
// feed empty and the assertions below vacuous.
type discardSink struct{}
func (discardSink) Write(p []byte) (int, error) { return len(p), nil }
func TestStageCommitCapturesTheChangedFiles(t *testing.T) {
trees := &capturingTrees{commitAllChanged: true, stats: sampleStats()}
store := &recordingStore{}
tc := captureTaskContext(t, trees, store)
tc.Task.Stage = "commit-repro"
if err := StageCommit("commit-repro", func(*TaskContext) string { return "msg" }).Run(context.Background(), tc); err != nil {
t.Fatalf("StageCommit.Run() = %v, want nil", err)
}
captures := store.ofKind(events.KindChangesCaptured)
if len(captures) != 1 {
t.Fatalf("changes_captured events = %d, want exactly 1", len(captures))
}
got := captures[0]
if got.Attempt != 2 {
t.Errorf("capture attempt = %d, want the task's own attempt 2", got.Attempt)
}
if got.Stage != "commit-repro" {
t.Errorf("capture stage = %q, want the capturing stage", got.Stage)
}
if got.TaskID != tc.Task.ID || got.Repo != "acme/widget" || got.Workflow != tc.Task.Workflow {
t.Errorf("capture identity = (%d, %q, %q), want the task's", got.TaskID, got.Repo, got.Workflow)
}
if trees.dir != tc.Dir || trees.base != "main" {
t.Errorf("diffstat read (%q, %q), want the worktree and the repo's base branch", trees.dir, trees.base)
}
payload := decodeCapture(t, got)
if payload.Schema != events.ChangesCapturedSchema {
t.Errorf("payload schema = %q, want %q: a reader refuses a capture it cannot name",
payload.Schema, events.ChangesCapturedSchema)
}
if payload.Owner != "acme" || payload.Repo != "widget" || payload.Base != "main" || payload.Branch != "feat/7-widget" {
t.Errorf("payload coordinates = %+v", payload)
}
if payload.CapturedAfter != capturedAfterCommit {
t.Errorf("captured_after = %q, want %q", payload.CapturedAfter, capturedAfterCommit)
}
if payload.HeadSHA != strings.Repeat("b", 40) || payload.BaseSHA != strings.Repeat("a", 40) {
t.Errorf("payload SHAs = (%q, %q), want the measured pair", payload.BaseSHA, payload.HeadSHA)
}
if len(payload.Files) != 2 || payload.Files[0].Path != "internal/x.go" || payload.Files[1].OldPath != "docs/z.md" {
t.Errorf("payload files = %+v", payload.Files)
}
if payload.Totals != (task.ChangeTotals{Files: 2, Additions: 12, Deletions: 3}) {
t.Errorf("payload totals = %+v", payload.Totals)
}
}
// TestStageCommitPushCapturesOnlyAfterThePush pins the second capture point:
// a push that failed is not a change that landed, and a capture claiming one
// would be worse than no capture.
func TestStageCommitPushCapturesOnlyAfterThePush(t *testing.T) {
tests := []struct {
name string
pushShares bool
}{
{name: "after a successful push", pushShares: true},
{name: "not after a failed push", pushShares: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
trees := &capturingTrees{commitAllChanged: true, stats: sampleStats()}
if !tt.pushShares {
trees.pushErr = fmt.Errorf("push refused")
}
store := &recordingStore{}
tc := captureTaskContext(t, trees, store)
err := StageCommitPush(func(*TaskContext) string { return "msg" }).Run(context.Background(), tc)
captures := store.ofKind(events.KindChangesCaptured)
if !tt.pushShares {
if err == nil {
t.Fatal("StageCommitPush.Run() = nil on a failed push, want the push error")
}
if len(captures) != 0 {
t.Fatalf("captures = %d after a failed push, want none", len(captures))
}
return
}
if err != nil {
t.Fatalf("StageCommitPush.Run() = %v, want nil", err)
}
if len(captures) != 1 {
t.Fatalf("changes_captured events = %d, want exactly 1", len(captures))
}
if got := decodeCapture(t, captures[0]).CapturedAfter; got != capturedAfterCommitPush {
t.Errorf("captured_after = %q, want %q", got, capturedAfterCommitPush)
}
})
}
}
// TestChangeCaptureDegradesWithoutTheCapability: the capture is an optional
// capability, so a Trees implementation that cannot report a diffstat must
// lose the record and nothing else. fakeTrees is exactly that implementation.
func TestChangeCaptureDegradesWithoutTheCapability(t *testing.T) {
trees := &fakeTrees{commitAllChanged: true}
store := &recordingStore{}
tc := captureTaskContext(t, trees, store)
if err := StageCommit("commit", func(*TaskContext) string { return "msg" }).Run(context.Background(), tc); err != nil {
t.Fatalf("StageCommit.Run() = %v, want the stage to succeed without a capture", err)
}
if got := len(store.ofKind(events.KindChangesCaptured)); got != 0 {
t.Errorf("changes_captured events = %d, want none", got)
}
}
// TestChangeCaptureFailureNeverFailsTheStage: reporting is not the work. A
// worktree that cannot be measured, and a store that rejects the write, must
// both leave the stage's own result untouched.
func TestChangeCaptureFailureNeverFailsTheStage(t *testing.T) {
tests := []struct {
name string
trees Trees
store Store
reason string
}{
{
name: "the diffstat could not be read",
trees: &capturingTrees{commitAllChanged: true, err: fmt.Errorf("worktree vanished")},
store: &recordingStore{},
reason: "a worktree the daemon already deleted",
},
{
name: "the capture could not be persisted",
trees: &capturingTrees{commitAllChanged: true, stats: sampleStats()},
store: &recordingStore{insertEr: fmt.Errorf("store unavailable")},
reason: "a store that refused the row",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := captureTaskContext(t, tt.trees, tt.store)
if err := StageCommit("commit", func(*TaskContext) string { return "msg" }).Run(context.Background(), tc); err != nil {
t.Fatalf("StageCommit.Run() = %v, want nil despite %s", err, tt.reason)
}
})
}
}
// TestOpenPRCapturesThePullRequestNumber is R3's link requirement at the
// producer. The captures taken at commit and push time run before the PR
// exists, so their pr_number is necessarily 0; OpenPR captures once more as
// soon as the number is recorded, which is what lets the changed-files view
// link the pull request.
func TestOpenPRCapturesThePullRequestNumber(t *testing.T) {
trees := &capturingTrees{stats: sampleStats()}
store := &recordingStore{}
forgeClient := &fakeForge{}
tc := captureTaskContext(t, trees, store)
tc.Forge = forgeClient
tc.Task.Stage = "open-pr"
tc.Task.Title = "widget fails"
if err := OpenPR(context.Background(), tc, "body"); err != nil {
t.Fatalf("OpenPR() = %v, want nil", err)
}
if tc.Task.PRNumber == 0 {
t.Fatal("OpenPR recorded no PR number; the assertion below would be vacuous")
}
captures := store.ofKind(events.KindChangesCaptured)
if len(captures) != 1 {
t.Fatalf("changes_captured events = %d, want the capture OpenPR records once the PR number exists", len(captures))
}
payload := decodeCapture(t, captures[0])
if payload.CapturedAfter != capturedAfterOpenPR {
t.Errorf("captured_after = %q, want %q", payload.CapturedAfter, capturedAfterOpenPR)
}
if payload.PRNumber != tc.Task.PRNumber {
t.Errorf("pr_number = %d, want the number OpenPR recorded (%d)", payload.PRNumber, tc.Task.PRNumber)
}
if captures[0].Stage != "open-pr" {
t.Errorf("capture stage = %q, want the capturing stage", captures[0].Stage)
}
}
// 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.
func TestChangeCaptureTruncatesTheFileListButNotTheTotals(t *testing.T) {
files := make([]task.FileChange, 0, task.MaxCapturedFiles+5)
for i := range task.MaxCapturedFiles + 5 {
files = append(files, task.FileChange{
Path: fmt.Sprintf("file-%03d.go", i), Status: task.ChangeAdded, Additions: 1,
})
}
stats := task.ChangeStats{
Files: files,
Totals: task.ChangeTotals{Files: len(files), Additions: len(files)},
}
trees := &capturingTrees{commitAllChanged: true, stats: stats}
store := &recordingStore{}
tc := captureTaskContext(t, trees, store)
if err := StageCommit("commit", func(*TaskContext) string { return "msg" }).Run(context.Background(), tc); err != nil {
t.Fatal(err)
}
captures := store.ofKind(events.KindChangesCaptured)
if len(captures) != 1 {
t.Fatalf("changes_captured events = %d, want 1", len(captures))
}
payload := decodeCapture(t, captures[0])
if len(payload.Files) != task.MaxCapturedFiles {
t.Errorf("payload carries %d files, want the cap of %d", len(payload.Files), task.MaxCapturedFiles)
}
if !payload.Truncated {
t.Error("truncated = false, want true when entries were dropped")
}
if payload.Totals != (task.ChangeTotals{Files: len(files), Additions: len(files)}) {
t.Errorf("totals = %+v, want the full set %d", payload.Totals, len(files))
}
if payload.Totals.Files == len(payload.Files) {
t.Error("totals were recomputed over the truncated list; a reader would understate the change")
}
}
// TestBaselineFixCapturesItsOwnCommit pins the third capture point: the
// baseline repair is a real, gate-verified commit of its own, and it is the
// only record of that intermediate state once the later push capture folds it
// into one bigger change.
func TestBaselineFixCapturesItsOwnCommit(t *testing.T) {
dir := t.TempDir()
script := filepath.Join(dir, "gate.sh")
if err := os.WriteFile(script, []byte("#!/bin/sh\nexit 1\n"), 0o755); err != nil {
t.Fatal(err)
}
runner := agentRunnerFunc(func(context.Context, string, agentexec.Request, agentexec.ToolCallReporter) (agentexec.Result, error) {
return agentexec.Result{Version: agentexec.ProtocolVersion, Status: agentexec.StatusPassed}, nil
})
tests := []struct {
name string
changed bool
want int
}{
{name: "a real fix is captured", changed: true, want: 1},
{name: "a commit that produced nothing is not", changed: false, want: 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
trees := &capturingTrees{commitAllChanged: tt.changed, stats: sampleStats()}
store := &recordingStore{}
tc := &TaskContext{
Task: &Task{ID: 9, Attempt: 1, Owner: "o", Repo: "r", Status: StatusRunning},
Repo: config.Repo{Owner: "o", Name: "r", Gate: [][]string{{"sh", script}}},
Cfg: config.Config{Models: map[string]string{"builder": "provider/model"}},
Agent: runner,
Trees: trees,
Store: store,
Dir: dir,
Branch: "feat/9-x",
Log: slog.New(slog.DiscardHandler),
}
if err := StageBaselineGate().Run(context.Background(), tc); err != nil {
t.Fatalf("StageBaselineGate.Run() = %v", err)
}
captures := store.ofKind(events.KindChangesCaptured)
if len(captures) != tt.want {
t.Fatalf("changes_captured events = %d, want %d", len(captures), tt.want)
}
if tt.want == 0 {
return
}
if got := decodeCapture(t, captures[0]).CapturedAfter; got != capturedAfterBaselineFix {
t.Errorf("captured_after = %q, want %q", got, capturedAfterBaselineFix)
}
if !tc.BaselineFixed {
t.Error("BaselineFixed = false after a captured baseline commit")
}
})
}
}
// TestCaptureIsNeverCallableFromTrees makes the constraint explicit in code,
// next to the type that must not grow past it: the capture is an optional,
// unexported capability, and Trees -- the surface interpreted stage code is
// handed -- must not name it. wfextract's reachability test proves the same
// thing from the interpreted side.
func TestCaptureIsNeverCallableFromTrees(t *testing.T) {
if _, ok := any((*fakeTrees)(nil)).(changeStatsReader); ok {
t.Error("fakeTrees satisfies the capture capability; the degrade test above would be vacuous")
}
if _, ok := any(Trees(nil)).(changeStatsReader); ok {
t.Error("Trees names the capture capability; interpreted stage code could call it")
}
}
// TestAFailedCaptureNeverParksTheTask runs the whole engine, not just the
// step: the only way a capture failure could reach the operator as a parked
// task is through Run's own error path, so the assertion has to be made
// against Run's transitions.
func TestAFailedCaptureNeverParksTheTask(t *testing.T) {
tests := []struct {
name string
trees Trees
}{
{
name: "the diffstat could not be read",
trees: &capturingTrees{commitAllChanged: true, err: fmt.Errorf("worktree vanished")},
},
{
name: "the capture could not be persisted",
trees: &capturingTrees{commitAllChanged: true, stats: sampleStats()},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &recordingStore{}
if tt.name == "the capture could not be persisted" {
// Only the capture's own insert fails; every other write the
// engine makes still has to succeed for the run to finish.
store = &recordingStore{insertEr: fmt.Errorf("store unavailable")}
}
tc := captureTaskContext(t, tt.trees, store)
tc.Task.Status = StatusRunning
Run(context.Background(), Workflow{Name: "implement", Stages: []Stage{
StageCommitPush(func(*TaskContext) string { return "msg" }),
{Name: "open-pr", Run: func(_ context.Context, tc *TaskContext) error {
tc.Outcome = Outcome{Status: StatusPROpen, Detail: "PR #1"}
return nil
}},
}}, tc)
for _, tr := range store.transitions {
if tr.to == StatusParked {
t.Fatalf("task parked (%s -> %s: %s); a change capture must never park a run that succeeded",
tr.from, tr.to, tr.detail)
}
}
if tc.Outcome.Status != StatusPROpen {
t.Errorf("outcome = %q, want the stage's own %q", tc.Outcome.Status, StatusPROpen)
}
})
}
}