Files
archie/internal/app/agentworker/task_execution_test.go
T
sam e0b6aa6105 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

909 lines
32 KiB
Go

package agentworker
import (
"context"
"encoding/json"
"log/slog"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
"github.com/go-git/go-git/v6"
"github.com/nats-io/nats-server/v2/server"
natssrv "github.com/nats-io/nats-server/v2/test"
natsio "github.com/nats-io/nats.go"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/domain/workintake"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/forge"
"github.com/samcharles93/archie-core/internal/forgerpc"
agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/installtype"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/taskrun"
"github.com/samcharles93/archie-core/internal/worktree"
"github.com/samcharles93/archie-core/internal/worktreerpc"
)
// ── regression: archie-core#520 -- worktree ownership must be
// reconciled before push, since the agent commits as root inside the
// container and the daemon reads those same loose objects as its own
// non-root host user ──────────────────────────────────────────────────
type fakePushTrees struct {
pushed bool
pushErr error
}
func (f *fakePushTrees) Push(context.Context) error {
f.pushed = true
return f.pushErr
}
func TestWorktreeOwnerIDParsesEnvValue(t *testing.T) {
cases := []struct {
name string
env string
want int
}{
{"unset", "", -1},
{"zero is a valid uid (root)", "0", 0},
{"typical non-root uid", "1000", 1000},
{"garbage falls back to unset", "not-a-number", -1},
{"negative falls back to unset", "-1", -1},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := worktreeOwnerID(c.env); got != c.want {
t.Errorf("worktreeOwnerID(%q) = %d, want %d", c.env, got, c.want)
}
})
}
}
// TestHybridTreesPushReconcilesOwnershipWhenConfigured chowns to the test
// process's own uid/gid -- a permission no-op that succeeds without root --
// so this proves the walk runs and still reaches the underlying push,
// without needing privilege to chown to an arbitrary owner.
func TestHybridTreesPushReconcilesOwnershipWhenConfigured(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "file.txt"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(dir, "sub"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "sub", "nested.txt"), []byte("y"), 0o644); err != nil {
t.Fatal(err)
}
fake := &fakePushTrees{}
trees := &hybridTrees{
push: fake,
localDir: dir,
worktreeUID: os.Getuid(),
worktreeGID: os.Getgid(),
}
if err := trees.Push(t.Context(), dir, "branch"); err != nil {
t.Fatalf("Push: %v", err)
}
if !fake.pushed {
t.Fatal("Push did not reach the underlying remote push")
}
}
// TestHybridTreesPushReconcilesOwnershipWithDanglingSymlink: a repo can
// legitimately track a symlink whose target doesn't exist inside the
// container (e.g. a relative link to something outside the checkout, or to
// a tool the image doesn't ship). Chowning must not follow it -- a
// following chown fails on a dangling target, which would abort a push
// that would otherwise have succeeded, a regression worse than the bug
// being fixed here.
func TestHybridTreesPushReconcilesOwnershipWithDanglingSymlink(t *testing.T) {
dir := t.TempDir()
if err := os.Symlink(filepath.Join(dir, "does-not-exist"), filepath.Join(dir, "broken-link")); err != nil {
t.Fatal(err)
}
fake := &fakePushTrees{}
trees := &hybridTrees{
push: fake,
localDir: dir,
worktreeUID: os.Getuid(),
worktreeGID: os.Getgid(),
}
if err := trees.Push(t.Context(), dir, "branch"); err != nil {
t.Fatalf("Push: %v", err)
}
if !fake.pushed {
t.Fatal("Push did not reach the underlying remote push")
}
}
// TestHybridTreesPushSkipsReconciliationWhenNotConfigured: an unset
// WORKTREE_UID/GID (archie-core's non-container and test callers) must not
// attempt any chown at all -- a nonexistent dir would otherwise fail the
// walk, so this proves the walk is skipped entirely, not just tolerant of
// this one input.
func TestHybridTreesPushSkipsReconciliationWhenNotConfigured(t *testing.T) {
fake := &fakePushTrees{}
trees := &hybridTrees{
push: fake,
localDir: filepath.Join(t.TempDir(), "does-not-exist"),
worktreeUID: -1,
worktreeGID: -1,
}
if err := trees.Push(t.Context(), trees.localDir, "branch"); err != nil {
t.Fatalf("Push: %v", err)
}
if !fake.pushed {
t.Fatal("Push did not reach the underlying remote push")
}
}
func TestHybridTreesCommitReconcilesOwnershipEvenWhenCommitFails(t *testing.T) {
missing := filepath.Join(t.TempDir(), "does-not-exist")
trees := &hybridTrees{
local: &worktree.Manager{},
localDir: missing,
worktreeUID: os.Getuid(),
worktreeGID: os.Getgid(),
}
_, err := trees.CommitAll(t.Context(), missing, "test commit")
if err == nil {
t.Fatal("CommitAll error = nil, want commit and ownership reconciliation failure")
}
if !strings.Contains(err.Error(), "reconcile worktree ownership after commit") {
t.Fatalf("CommitAll error = %q, want ownership reconciliation context", err)
}
}
// TestHybridTreesPushSurfacesOwnershipReconciliationFailure: a configured
// but broken reconciliation (the dir push is asked to fix vanished) must
// fail loudly rather than forward to a push that will only fail later with
// a much more confusing git-internal permission error.
func TestHybridTreesPushSurfacesOwnershipReconciliationFailure(t *testing.T) {
fake := &fakePushTrees{}
missing := filepath.Join(t.TempDir(), "does-not-exist")
trees := &hybridTrees{
push: fake,
localDir: missing,
worktreeUID: os.Getuid(),
worktreeGID: os.Getgid(),
}
err := trees.Push(t.Context(), missing, "branch")
if err == nil {
t.Fatal("expected an error reconciling ownership of a missing directory")
}
if fake.pushed {
t.Fatal("Push must not reach the underlying remote push when reconciliation fails")
}
}
// TestApplyToolLimitsWiresConfiguredPolicyIntoLoopRunner guards against
// regressing archie-core's tool-result cap and spill dir back to inert
// zero values on the worker path (agentexec-1 / tools-core-11): a
// *LoopRunner built for a task must carry the daemon's configured
// ToolPolicy, not the zero value NewLoopRunner leaves it at.
func TestApplyToolLimitsWiresConfiguredPolicyIntoLoopRunner(t *testing.T) {
runner := agentexec.NewLoopRunner(agentexec.NewRuntime(nil), slog.New(slog.DiscardHandler))
policy := config.ToolPolicy{MaxResultChars: 12345, SpillDir: "/var/tmp/archie-spill"}
applyToolLimits(runner, policy)
want := agentexec.ToolLimits{MaxResultChars: 12345, SpillDir: "/var/tmp/archie-spill"}
if runner.Limits != want {
t.Fatalf("runner.Limits = %+v, want %+v", runner.Limits, want)
}
}
// TestApplyToolLimitsIgnoresNonLoopRunner confirms a test-fake runner (which
// is not a *agentexec.LoopRunner) is left untouched rather than panicking.
func TestApplyToolLimitsIgnoresNonLoopRunner(t *testing.T) {
applyToolLimits(panicRunner{t}, config.ToolPolicy{MaxResultChars: 1})
}
type panicRunner struct{ t *testing.T }
func (r panicRunner) Run(context.Context, string, agentexec.Request, agentexec.ToolCallReporter) (agentexec.Result, error) {
r.t.Fatal("bootstrap workflow must not invoke an LLM runner")
return agentexec.Result{}, nil
}
type prCapturingForge struct {
prs []struct{ owner, repo, title, head, base, body string }
}
func (f *prCapturingForge) Comment(context.Context, string, string, int, string) (int64, error) {
return 1, nil
}
func (f *prCapturingForge) CloseIssue(context.Context, string, string, int, string) error { return nil }
func (f *prCapturingForge) CreatePR(_ context.Context, owner, repo, title, head, base, body string) (int, error) {
f.prs = append(f.prs, struct{ owner, repo, title, head, base, body string }{owner, repo, title, head, base, body})
return 5, nil
}
func (f *prCapturingForge) SetStateLabel(context.Context, string, string, int, string, []string) {}
func (f *prCapturingForge) AcceptInvitations(context.Context) error { panic("unexpected call") }
func (f *prCapturingForge) AssignedIssues(context.Context, string, string, string) ([]forge.Issue, error) {
panic("unexpected call")
}
func (f *prCapturingForge) IssuesWithLabel(context.Context, string, string, string) ([]forge.Issue, error) {
panic("unexpected call")
}
func (f *prCapturingForge) PRState(context.Context, string, string, int) (string, error) {
panic("unexpected call")
}
func (f *prCapturingForge) CreateIssue(context.Context, string, string, string, string, []string) (int, error) {
panic("unexpected call")
}
func (f *prCapturingForge) React(context.Context, string, string, int, string) error {
panic("unexpected call")
}
func (f *prCapturingForge) VerifyPush(context.Context, string, string) error {
panic("unexpected call")
}
func (f *prCapturingForge) LinkBranch(context.Context, string, string, int, string) error { return nil }
func (f *prCapturingForge) CreateReviewComments(context.Context, string, string, int, string, []workflow.ReviewComment) error {
return nil
}
func (f *prCapturingForge) ReplyToReview(context.Context, string, string, int, int64, string) error {
return nil
}
type remoteManager struct {
manager *worktree.Manager
dir string
branch string
}
func (r *remoteManager) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string) (string, string, error) {
dir, branch, err := r.manager.Prepare(ctx, owner, repo, base, issue, title, body, labels)
if err == nil {
r.dir = dir
r.branch = branch
}
return dir, branch, err
}
func (r *remoteManager) Push(ctx context.Context) error {
return r.manager.Push(ctx, r.dir, r.branch)
}
func newLocalRemote(t *testing.T, owner, repo string) string {
t.Helper()
host := t.TempDir()
bare := filepath.Join(host, owner, repo+".git")
seed := filepath.Join(t.TempDir(), "seed")
run := func(dir string, args ...string) {
t.Helper()
cmd := exec.CommandContext(t.Context(), "git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
if err := os.MkdirAll(bare, 0o755); err != nil {
t.Fatal(err)
}
run(host, "init", "--bare", "-b", "main", bare)
if err := os.MkdirAll(seed, 0o755); err != nil {
t.Fatal(err)
}
run(seed, "init", "-b", "main")
run(seed, "config", "user.name", "seeder")
run(seed, "config", "user.email", "seed@example.com")
if err := os.WriteFile(filepath.Join(seed, "README.md"), []byte("seed\n"), 0o644); err != nil {
t.Fatal(err)
}
run(seed, "add", "-A")
run(seed, "commit", "-m", "seed")
run(seed, "push", "file://"+bare, "main")
return host
}
func startEmbeddedTaskRPCServer(t *testing.T) *server.Server {
t.Helper()
srv := natssrv.RunRandClientPortServer()
t.Cleanup(srv.Shutdown)
if err := srv.EnableJetStream(&server.JetStreamConfig{StoreDir: t.TempDir()}); err != nil {
t.Fatal(err)
}
return srv
}
func connectTaskRPC(t *testing.T, url string) *natsio.Conn {
t.Helper()
connection, err := natsio.Connect(url)
if err != nil {
t.Fatal(err)
}
t.Cleanup(connection.Close)
return connection
}
// startStateStoreGRPC serves the State Store gRPC contract over a loopback
// TCP listener fronting local, returning the dial address. The agent is fully
// on gRPC for its store calls (docs/prds/state-store-contract.md §12 step 4),
// so an agentworker test that drives executeTaskRequest needs a real State
// Store server rather than the deleted NATS storerpc path.
func startStateStoreGRPC(t *testing.T, local *store.Store) string {
t.Helper()
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
server := grpc.NewServer()
staterpc.RegisterServer(server, staterpc.Deps{
Tasks: local, Captures: local, Mappings: local, Bindings: local,
BindingDispatcher: local, BindingTaskCreator: local,
})
go func() { _ = server.Serve(listener) }()
t.Cleanup(func() { server.Stop(); _ = listener.Close() })
return listener.Addr().String()
}
func TestRunTaskExecutesBootstrapWorkflowEndToEnd(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not installed")
}
ctx := t.Context()
host := newLocalRemote(t, "acme", "widget")
st := store.OpenTest(t)
if _, err := st.EnqueueIssue(ctx, "acme", "widget", 1, "feat: bootstrap test", "", "bootstrap", ""); err != nil {
t.Fatalf("enqueue: %v", err)
}
task, err := st.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("claim: (%v, %v)", task, err)
}
fg := &prCapturingForge{}
manager := &worktree.Manager{WorkDir: t.TempDir(), Token: "unused", BotUser: "archie-bot", BotEmail: "archie-bot@example.com", BaseURL: "file://" + host}
remote := &remoteManager{manager: manager}
hostDir, _, err := remote.Prepare(ctx, "acme", "widget", "main", 1, task.Title, "", "bootstrap")
if err != nil {
t.Fatalf("prepare: %v", err)
}
req := taskrun.Request{
Task: task,
Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"},
Cfg: config.Config{BotUser: "archie-bot", BotEmail: "archie-bot@example.com"}.ForTask(),
}
fakeRunner := runnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner { return panicRunner{t} })
response, err := runTask(ctx, req, taskDependencies{forge: fg, store: st, trees: remote}, fakeRunner, hostDir, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatalf("runTask: %v", err)
}
if response.Status != workflow.StatusPROpen || len(fg.prs) != 1 {
t.Fatalf("response status/PRs = (%q, %d), want (%q, 1)", response.Status, len(fg.prs), workflow.StatusPROpen)
}
stored, err := st.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil || stored == nil || stored.Status != workflow.StatusPROpen || stored.PRNumber != 5 {
t.Fatalf("stored task = (%+v, %v), want PR-open task #5", stored, err)
}
if _, err := os.Stat(filepath.Join(hostDir, ".archie", "bootstrap.md")); err != nil {
t.Fatalf("expected bootstrap marker file: %v", err)
}
repo, err := git.PlainOpen(hostDir)
if err != nil {
t.Fatal(err)
}
head, err := repo.Head()
if err != nil {
t.Fatal(err)
}
commit, err := repo.CommitObject(head.Hash())
if err != nil {
t.Fatal(err)
}
if commit.Author.Name != "archie-bot" || commit.Author.Email != "archie-bot@example.com" ||
commit.Committer.Name != "archie-bot" || commit.Committer.Email != "archie-bot@example.com" {
t.Fatalf("commit identity = author %q <%s>, committer %q <%s>",
commit.Author.Name, commit.Author.Email, commit.Committer.Name, commit.Committer.Email)
}
// The change the run produced is captured at the push, through the
// container's hybridTrees, and persisted as durable provenance attributed
// to the attempt -- and captured once more by OpenPR, the first moment a
// capture can carry the pull-request number the view links. This is the
// only end-to-end path that matters: every production workflow.Run executes
// here.
timeline, err := st.TaskEvents(ctx, task.ID)
if err != nil {
t.Fatalf("TaskEvents: %v", err)
}
var captured []events.Event
for _, e := range timeline {
if e.Kind == events.KindChangesCaptured {
captured = append(captured, e)
}
}
if len(captured) != 2 {
t.Fatalf("changes_captured events = %d, want the push capture and the one OpenPR takes (%d)", len(captured), stored.PRNumber)
}
for i, e := range captured {
if e.Attempt != task.Attempt || task.Attempt == 0 {
t.Errorf("capture %d attempt = %d, want the task's own attempt %d", i, e.Attempt, task.Attempt)
}
}
if captured[0].Stage != "commit-push" {
t.Errorf("capture stage = %q, want the capturing stage", captured[0].Stage)
}
data, err := json.Marshal(captured[0].Data)
if err != nil {
t.Fatal(err)
}
var payload struct {
CapturedAfter string `json:"captured_after"`
PRNumber int `json:"pr_number"`
Files []struct {
Path string `json:"path"`
Status string `json:"status"`
} `json:"files"`
}
if err := json.Unmarshal(data, &payload); err != nil {
t.Fatalf("decode capture: %v", err)
}
if payload.CapturedAfter != "commit-push" {
t.Errorf("captured_after = %q, want commit-push", payload.CapturedAfter)
}
if payload.PRNumber != 0 {
t.Errorf("the push capture carries pr_number %d, want 0: no PR existed when it was taken", payload.PRNumber)
}
if len(payload.Files) != 1 || payload.Files[0].Path != ".archie/bootstrap.md" || payload.Files[0].Status != "added" {
t.Errorf("captured files = %+v, want the bootstrap marker as an added file", payload.Files)
}
finalData, err := json.Marshal(captured[1].Data)
if err != nil {
t.Fatal(err)
}
var final struct {
CapturedAfter string `json:"captured_after"`
PRNumber int `json:"pr_number"`
}
if err := json.Unmarshal(finalData, &final); err != nil {
t.Fatalf("decode the final capture: %v", err)
}
if final.CapturedAfter != "open-pr" || final.PRNumber != stored.PRNumber || final.PRNumber == 0 {
t.Errorf("final capture = %q pr %d, want open-pr carrying the stored PR number %d, without which the changed-files view has nothing to link",
final.CapturedAfter, final.PRNumber, stored.PRNumber)
}
}
// TestRunTaskRestoresWorktreeOwnershipOnEveryExit pins the half of the
// worktree-ownership contract that archie-core#520 left open. CommitAll and
// Push hand the worktree back to the daemon's own UID on the SUCCESS path;
// every other way a container run can end -- a gate failure that parks the
// task, a stage error, an early return -- used to leave the worktree owned by
// root, because the agent runs as root inside the container. The daemon then
// could not clean or reset those paths on the next attempt (a root-owned
// directory cannot be unlinked from by its non-root owner), so a retry failed
// before it could start no matter what the worktree refresh did.
//
// The run below exits early, which is the point: the restore has to be
// deferred over the whole run rather than attached to the paths that happen to
// commit. It is observed through a supplementary group -- a non-root process
// may move a file it owns into a group it is a member of, so this proves the
// chown ran without needing root.
func TestRunTaskRestoresWorktreeOwnershipOnEveryExit(t *testing.T) {
probeGroup := supplementaryGroup(t)
workDir := t.TempDir()
probe := filepath.Join(workDir, "probe.txt")
if err := os.WriteFile(probe, []byte("x\n"), 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("WORKTREE_UID", strconv.Itoa(os.Getuid()))
t.Setenv("WORKTREE_GID", strconv.Itoa(probeGroup))
// A runner factory that yields nothing fails the run after the trees (and
// the ownership defer) are already in place.
_, err := runTask(t.Context(), taskrun.Request{
Task: &workflow.Task{ID: 77, Branch: "feat/77-probe", Workflow: "implement"},
Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"},
}, taskDependencies{}, runnerFactory(func(map[string]agentexec.Provider, *slog.Logger) agentexec.Runner {
return nil
}), workDir, slog.New(slog.DiscardHandler))
if err == nil {
t.Fatal("runTask error = nil, want the missing-runner failure this exit is built around")
}
info, statErr := os.Stat(probe)
if statErr != nil {
t.Fatal(statErr)
}
gid, ok := info.Sys().(*syscall.Stat_t)
if !ok {
t.Fatal("no syscall.Stat_t for the ownership probe")
}
if int(gid.Gid) != probeGroup {
t.Errorf("probe.txt gid = %d, want %d -- a run that never reached CommitAll or Push left the worktree owned by the container's own user", gid.Gid, probeGroup)
}
}
// supplementaryGroup returns a group this process is a member of but that is
// not its primary gid, so a test can observe a chown a non-root process is
// allowed to make.
func supplementaryGroup(t *testing.T) int {
t.Helper()
groups, err := os.Getgroups()
if err != nil {
t.Skipf("os.Getgroups: %v", err)
}
for _, gid := range groups {
if gid != os.Getgid() {
return gid
}
}
t.Skip("process belongs to no supplementary group to chown into")
return 0
}
// TestExecuteTaskRequestUsesInfrastructureRPCDependencies preserves the
// application-to-infrastructure proof: the application asks Transport for its
// forge/store/tree contracts, and those concrete clients cross embedded NATS
// to daemon-side RPC servers before the bootstrap workflow can complete.
func TestExecuteTaskRequestUsesInfrastructureRPCDependencies(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not installed")
}
ctx := t.Context()
host := newLocalRemote(t, "acme", "rpc-widget")
st := store.OpenTest(t)
if _, err := st.EnqueueIssue(ctx, "acme", "rpc-widget", 2, "feat: RPC bootstrap test", "", "bootstrap", ""); err != nil {
t.Fatal(err)
}
task, err := st.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("claim = (%+v, %v)", task, err)
}
forge := &prCapturingForge{}
daemonTrees := &worktree.Manager{
WorkDir: t.TempDir(),
Token: "unused-for-file-remotes",
BotUser: "archie-bot",
BotEmail: "archie-bot@example.com",
BaseURL: "file://" + host,
}
hostDir, branch, err := daemonTrees.Prepare(ctx, "acme", "rpc-widget", "main", 2, task.Title, "", "bootstrap")
if err != nil {
t.Fatal(err)
}
task.Branch = branch
grants := worktreerpc.NewGrants()
grant, revokeGrant, err := grants.Issue(task)
if err != nil {
t.Fatal(err)
}
t.Cleanup(revokeGrant)
srv := startEmbeddedTaskRPCServer(t)
serverConn := connectTaskRPC(t, srv.ClientURL())
unsubForge, err := (&forgerpc.Server{Forge: forge, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
if err != nil {
t.Fatal(err)
}
t.Cleanup(unsubForge)
unsubTrees, err := (&worktreerpc.Server{Trees: daemonTrees, Grants: grants, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
if err != nil {
t.Fatal(err)
}
t.Cleanup(unsubTrees)
transport, err := agentnats.Connect(ctx, agentnats.Config{URL: srv.ClientURL(), StateStoreURL: startStateStoreGRPC(t, st)}, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatal(err)
}
t.Cleanup(transport.Close)
request := taskrun.Request{
Task: task,
Repo: config.Repo{Owner: "acme", Name: "rpc-widget", Base: "main"},
Cfg: config.Config{}.ForTask(),
WorktreeGrant: grant,
}
response, err := executeTaskRequest(ctx, request, transport, hostDir, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatal(err)
}
if response.Status != workflow.StatusPROpen || len(forge.prs) != 1 {
t.Fatalf("response status/PRs = (%q, %d), want (%q, 1)", response.Status, len(forge.prs), workflow.StatusPROpen)
}
// archie-core-1786751948782-3-837a8fd0 point 4: every response reports
// what this worker actually is, since it's the only channel the daemon
// has to learn what's really running (see daemon.AgentStatus).
if response.AgentVersion != Version() {
t.Errorf("response.AgentVersion = %q, want Version() = %q", response.AgentVersion, Version())
}
if response.AgentInstallType != installtype.Type() {
t.Errorf("response.AgentInstallType = %q, want installtype.Type() = %q", response.AgentInstallType, installtype.Type())
}
stored, err := st.TaskByIssue(ctx, "acme", "rpc-widget", 2)
if err != nil || stored == nil || stored.Status != workflow.StatusPROpen || stored.PRNumber != 5 {
t.Fatalf("stored task = (%+v, %v), want RPC-persisted PR-open task #5", stored, err)
}
if _, err := os.Stat(filepath.Join(hostDir, ".archie", "bootstrap.md")); err != nil {
t.Fatalf("expected bootstrap marker file: %v", err)
}
}
// TestExecuteTaskRequestForwardsWorkflowEventsOverNATS is the regression test
// for the "No events yet" dashboard bug (archie-core-518): a workflow run
// through the NATS/container path publishes its stage/outcome events to an
// in-process *events.Bus nothing on the daemon side could see, because
// runTask never wired TaskContext.Bus. This proves the fix end to end -- a
// bystander subscribed directly to SubjectForEvents(task.ID) on the same
// embedded NATS server executeTaskRequest uses receives real events the
// bootstrap workflow emits, exactly as the daemon's own subscriber would.
func TestExecuteTaskRequestForwardsWorkflowEventsOverNATS(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not installed")
}
ctx := t.Context()
host := newLocalRemote(t, "acme", "events-widget")
st := store.OpenTest(t)
if _, err := st.EnqueueIssue(ctx, "acme", "events-widget", 3, "feat: events bootstrap test", "", "bootstrap", ""); err != nil {
t.Fatal(err)
}
task, err := st.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("claim = (%+v, %v)", task, err)
}
forge := &prCapturingForge{}
daemonTrees := &worktree.Manager{
WorkDir: t.TempDir(),
Token: "unused-for-file-remotes",
BotUser: "archie-bot",
BotEmail: "archie-bot@example.com",
BaseURL: "file://" + host,
}
hostDir, branch, err := daemonTrees.Prepare(ctx, "acme", "events-widget", "main", 3, task.Title, "", "bootstrap")
if err != nil {
t.Fatal(err)
}
task.Branch = branch
grants := worktreerpc.NewGrants()
grant, revokeGrant, err := grants.Issue(task)
if err != nil {
t.Fatal(err)
}
t.Cleanup(revokeGrant)
srv := startEmbeddedTaskRPCServer(t)
serverConn := connectTaskRPC(t, srv.ClientURL())
unsubForge, err := (&forgerpc.Server{Forge: forge, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
if err != nil {
t.Fatal(err)
}
t.Cleanup(unsubForge)
unsubTrees, err := (&worktreerpc.Server{Trees: daemonTrees, Grants: grants, Log: slog.New(slog.DiscardHandler)}).Register(serverConn)
if err != nil {
t.Fatal(err)
}
t.Cleanup(unsubTrees)
// Stand in for the daemon's own SubjectEventsWildcard subscriber: bind
// to the exact per-task subject before the workflow runs, so nothing
// published during the run can be missed.
var (
mu sync.Mutex
received []events.Event
)
bystander := connectTaskRPC(t, srv.ClientURL())
eventSub, err := bystander.Subscribe(agentexec.SubjectForEvents(task.ID), func(msg *natsio.Msg) {
var e events.Event
if err := json.Unmarshal(msg.Data, &e); err != nil {
t.Errorf("undecodable event message: %v", err)
return
}
mu.Lock()
received = append(received, e)
mu.Unlock()
})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = eventSub.Unsubscribe() })
if err := bystander.Flush(); err != nil {
t.Fatal(err)
}
transport, err := agentnats.Connect(ctx, agentnats.Config{URL: srv.ClientURL(), StateStoreURL: startStateStoreGRPC(t, st)}, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatal(err)
}
t.Cleanup(transport.Close)
request := taskrun.Request{
Task: task,
Repo: config.Repo{Owner: "acme", Name: "events-widget", Base: "main"},
Cfg: config.Config{}.ForTask(),
WorktreeGrant: grant,
}
response, err := executeTaskRequest(ctx, request, transport, hostDir, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatal(err)
}
if response.Status != workflow.StatusPROpen {
t.Fatalf("response status = %q, want %q", response.Status, workflow.StatusPROpen)
}
deadline := time.After(2 * time.Second)
for {
mu.Lock()
sawOutcome := false
for _, e := range received {
if e.Kind == events.KindOutcome {
sawOutcome = true
break
}
}
mu.Unlock()
if sawOutcome {
break
}
select {
case <-deadline:
t.Fatal("no \"outcome\" event arrived on SubjectForEvents(task.ID) within 2s")
case <-time.After(10 * time.Millisecond):
}
}
mu.Lock()
defer mu.Unlock()
sawOutcome := false
for _, e := range received {
if e.TaskID != task.ID {
t.Errorf("event %+v carries TaskID %d, want %d", e, e.TaskID, task.ID)
}
if e.Kind == events.KindOutcome {
sawOutcome = true
}
}
if !sawOutcome {
t.Errorf("received %d events %+v, want at least one %q", len(received), received, events.KindOutcome)
}
}
// TestRouteTaskUsesRequestKindOverride is the regression for
// workflow-engine-1: a kind -> workflow override loaded by the daemon is
// carried in the taskrun request and applied by the worker process, so
// workflow.Route honours it instead of silently falling back to the
// built-in default (bug -> tdd).
func TestRouteTaskUsesRequestKindOverride(t *testing.T) {
t.Cleanup(func() {
workflow.SetKindWorkflows(nil)
workflow.SetLabelWorkflows(nil)
})
registry := workflow.Registry{
"tdd": workflow.Workflow{Name: "tdd"},
"bootstrap": workflow.Workflow{Name: "bootstrap"},
"implement": workflow.Workflow{Name: "implement"},
}
req := taskrun.Request{
Task: &workflow.Task{Labels: "bug"},
KindWorkflows: workflow.KindWorkflows{workintake.KindBug: "bootstrap"},
}
wf := routeTask(req, registry)
if wf.Name != "bootstrap" {
t.Fatalf("routeTask() = %q, want %q (request kind override not applied)", wf.Name, "bootstrap")
}
}
// TestRouteTaskUsesRequestLabelOverride is the label counterpart: an
// arbitrary-label binding carried in the request also reaches the worker's
// routing decision.
func TestRouteTaskUsesRequestLabelOverride(t *testing.T) {
t.Cleanup(func() {
workflow.SetKindWorkflows(nil)
workflow.SetLabelWorkflows(nil)
})
registry := workflow.Registry{
"implement": workflow.Workflow{Name: "implement"},
"security-review": workflow.Workflow{Name: "security-review"},
}
req := taskrun.Request{
Task: &workflow.Task{Labels: "security"},
LabelWorkflows: workflow.LabelWorkflows{"security": "security-review"},
}
wf := routeTask(req, registry)
if wf.Name != "security-review" {
t.Fatalf("routeTask() = %q, want %q (request label override not applied)", wf.Name, "security-review")
}
}
// changeStatsReader mirrors the unexported optional interface package workflow
// type-asserts on TaskContext.Trees to capture what an attempt changed. It is
// restated here because an unexported interface cannot be named from this
// package; the assertion below is therefore the only way this side can pin
// that the in-container Trees is capable at all.
type changeStatsReader interface {
ChangedFileStats(ctx context.Context, dir, base string) (task.ChangeStats, error)
}
// TestHybridTreesCanReportWhatChanged is the in-container half of the change
// capture. Every production workflow.Run executes in archie-agent against a
// hybridTrees, so a Trees without this capability would make the capture a
// silent no-op in exactly the deployment that produces all the real data --
// the daemon-side manager being capable would prove nothing.
func TestHybridTreesCanReportWhatChanged(t *testing.T) {
var trees any = &hybridTrees{local: &worktree.Manager{}}
if _, ok := trees.(changeStatsReader); !ok {
t.Fatal("hybridTrees cannot report a diffstat; every capture inside a container would silently no-op")
}
}
// TestHybridTreesForwardsChangedFileStatsToItsLocalManager drives the
// capability against a real worktree, so satisfaction of the interface is not
// mistaken for a working forwarder.
func TestHybridTreesForwardsChangedFileStatsToItsLocalManager(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not installed")
}
ctx := t.Context()
host := newLocalRemote(t, "acme", "diffstat")
manager := &worktree.Manager{
WorkDir: t.TempDir(), Token: "unused",
BotUser: "archie-bot", BotEmail: "archie-bot@example.com", BaseURL: "file://" + host,
}
dir, branch, err := manager.Prepare(ctx, "acme", "diffstat", "main", 4, "feat: widget", "", "")
if err != nil {
t.Fatalf("prepare: %v", err)
}
if err := os.WriteFile(filepath.Join(dir, "widget.txt"), []byte("one\ntwo\n"), 0o644); err != nil {
t.Fatal(err)
}
if _, err := manager.CommitAll(ctx, dir, "feat: widget"); err != nil {
t.Fatalf("commit: %v", err)
}
trees := &hybridTrees{local: manager, localDir: dir, branch: branch}
stats, err := trees.ChangedFileStats(ctx, dir, "main")
if err != nil {
t.Fatalf("ChangedFileStats: %v", err)
}
if len(stats.Files) != 1 || stats.Files[0].Path != "widget.txt" {
t.Fatalf("files = %+v, want the one committed file", stats.Files)
}
if stats.Totals != (task.ChangeTotals{Files: 1, Additions: 2}) {
t.Errorf("totals = %+v, want the file's two added lines", stats.Totals)
}
if stats.HeadSHA == "" || stats.BaseSHA == "" {
t.Errorf("SHAs = (%q, %q), want both resolved", stats.BaseSHA, stats.HeadSHA)
}
}