Files
archie/internal/app/agentworker/review.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

273 lines
14 KiB
Go

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
},
)
}