feat(tasks): attribute events, changes and config to the attempt that produced them

The events table had no attempt column, so two attempts of the same task were
indistinguishable: a retried task's stage events interleaved into one timeline and
nothing could say which run produced a given result.

- events gains an `attempt` column through the existing presence-gated
  migrateTasks loop rather than a version bump, so user_version stays 3 and an
  existing archie.db is migrated in place. A legacy-database test builds the
  pre-change schema and proves both that existing rows survive unattributed
  (attempt = 0) and that a new value round-trips.
- Event.attempt = 11 and ReadTaskLogRequest.stage = 9 are field additions to
  existing messages: no new RPC, no new table, and no change to any canonical error
  message that crosses the state-store wire.
- Every task event the workflow engine emits now carries the attempt. Producers
  that have no attempt yet (task_queued, binding_dispatch_failure) stay
  unattributed rather than guessing one.
- Changed files are captured as a durable changes_captured event at commit and push
  time, reusing the existing three-dot diff primitive: paths from FilePatches(),
  counts from Stats(), base_sha recorded as the merge base actually diffed rather
  than the base tip, a 200-file cap with totals kept complete. Consumption goes
  through an unexported optional interface so workflow.Trees -- the yaegi capability
  surface for interpreted stage plugins -- is not widened. A capture is also taken
  once the pull request exists, so the capture can link it.
- The per-attempt configuration is captured as a durable config_captured event
  carrying the non-secret config.TaskConfig, instead of a new table plus two new
  state-store RPCs: events are already this system's per-attempt provenance stream,
  so a table would have given one feature two provenance mechanisms. Redaction is
  proven against canary secrets. Measured payload: 539 bytes typical, 2333 bytes at
  20 models.
- Captures carry a schema marker and are refused unless their key set matches the
  declared payload shape, so drift surfaces as "recorded but could not be read"
  rather than as a capture of zeroes.
- Three read endpoints serve the attempt rail, the changed-file captures and a raw
  debug view, mapping to UI-owned response types rather than dumping store types.
- The task-log read gains a stage filter, bound to the stage logger for that
  stage's own lines; agent and tool output records no stage.

A task-scoped grant may already insert events on its own task, so this provenance
is exactly as trustworthy as the rest of that task's event stream. That limit is
documented rather than papered over.
This commit is contained in:
sam committed 2026-09-18 18:32:59 +10:00
1 parent 14e7512d14
commit 2c09f42ff4
39 files changed
+4264 -83

No files matched your search

+6 -1
View File
@@ -22,6 +22,11 @@ type Task struct {
Owner, Repo, Identity, Status, Stage, ParkReason string
IssueNumber, RetryCount int
ForgeBacked bool
// Attempt is the run the operator acted on. It is stamped onto the event
// this action records so an intervention is attributable to the run it
// changed course -- without it a retry or a stop is indistinguishable
// between attempts on the timeline.
Attempt int
}
type Store interface {
@@ -85,7 +90,7 @@ func (s Service) apply(ctx context.Context, task *Task, identity *string, action
if identity != nil {
source = "chat"
}
o := outcome{event: events.Event{TaskID: task.ID, Repo: task.Owner + "/" + task.Repo, Issue: task.IssueNumber}}
o := outcome{event: events.Event{TaskID: task.ID, Repo: task.Owner + "/" + task.Repo, Issue: task.IssueNumber, Attempt: task.Attempt}}
switch action {
case taskstate.ActionApprove:
err := s.Store.Requeue(ctx, task.ID, "waiting_human", "implement")
+588
View File
@@ -0,0 +1,588 @@
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)
}
}
// 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)
}
})
}
}
+1
View File
@@ -111,6 +111,7 @@ func StageBaselineGate() Stage {
if changed {
tc.BuildSummary = res.Summary
tc.BaselineFixed = true
tc.captureChanges(ctx, capturedAfterBaselineFix)
}
}
return nil
@@ -382,6 +382,7 @@ func TestOpenPRLinksSourceBranchBeforeCreatingPR(t *testing.T) {
Task: &Task{ID: 1, Owner: "acme", Repo: "widget", IssueNumber: 42, Title: "Fix bug"},
Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"},
Branch: "fix/42-bug",
Log: slog.New(slog.DiscardHandler),
}
if err := OpenPR(context.Background(), tc, "summary"); err != nil {
t.Fatal(err)
@@ -420,10 +421,15 @@ func TestStageCommitPushDoesNotUseSyntheticIssueForChatNoOp(t *testing.T) {
// fakeTrees implements Trees, tracking calls without touching a real
// worktree. commitAllChanged controls what CommitAll reports.
//
// It deliberately does NOT implement the unexported changeStatsReader
// capability: every stage test that drives a fake tree therefore also covers
// the path where a capture cannot be taken at all.
type fakeTrees struct {
commitAllChanged bool
pushed bool
pushBranch string
pushErr error
}
func (f *fakeTrees) Prepare(context.Context, string, string, string, int, string, string, string) (string, string, error) {
@@ -435,6 +441,9 @@ func (f *fakeTrees) CommitAll(context.Context, string, string) (bool, error) {
}
func (f *fakeTrees) Push(_ context.Context, _, branch string) error {
if f.pushErr != nil {
return f.pushErr
}
f.pushed = true
f.pushBranch = branch
return nil
@@ -464,6 +473,7 @@ func TestStageCommitPushPushesBaselineFixEvenWithNothingNewToCommit(t *testing.T
Dir: "/tmp/test",
Branch: "archie/issue-1",
Task: &Task{ID: 1, Owner: "o", Repo: "r", IssueNumber: 1},
Log: slog.New(slog.DiscardHandler),
}
if err := stage.Run(context.Background(), tc); err != nil {
@@ -488,6 +498,7 @@ func TestStageCommitPushStillErrorsOnEmptyTreeWithoutBaselineFix(t *testing.T) {
Dir: "/tmp/test",
Branch: "archie/issue-1",
Task: &Task{ID: 1, Owner: "o", Repo: "r", IssueNumber: 1},
Log: slog.New(slog.DiscardHandler),
}
if err := stage.Run(context.Background(), tc); err == nil {
@@ -595,6 +606,7 @@ func TestOpenPRSurvivesALinkBranchFailure(t *testing.T) {
Task: &Task{ID: 1, Owner: "acme", Repo: "widget", IssueNumber: 42, Title: "Fix bug"},
Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"},
Branch: "fix/42-bug",
Log: slog.New(slog.DiscardHandler),
}
if err := OpenPR(context.Background(), taskCtx, "summary"); err != nil {
+99 -1
View File
@@ -8,6 +8,8 @@ import (
"strings"
"time"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/gate"
"github.com/samcharles93/archie-core/internal/gate/gateeval"
)
@@ -15,6 +17,91 @@ import (
// Shared step library. Every workflow composes these; workflow-specific
// stages live next to their workflow definition.
// The captured_after values recorded inside a changes_captured event. They
// are part of the on-disk payload: a reader distinguishes a mid-workflow
// commit from the push that ended the run, so renaming one is a migration,
// not a rename.
const (
capturedAfterCommit = "commit"
capturedAfterCommitPush = "commit-push"
capturedAfterBaselineFix = "baseline-fix"
// capturedAfterOpenPR is the final capture of a run: OpenPR takes it the
// moment the PR number is recorded, because that is the only point at which
// a capture can carry one.
capturedAfterOpenPR = "open-pr"
)
// changeStatsReader is the optional capability through which a Trees
// implementation reports what an attempt changed. It is deliberately
// unexported, and deliberately NOT a method on Trees: Trees is projected into
// the interpreted-stage symbol table, so declaring it there would widen what
// repository-authored .archie/stages/*.go may call (pinned by
// wfextract/reachability_test.go). A Trees implementation without it degrades
// to no capture rather than failing the stage.
type changeStatsReader interface {
ChangedFileStats(ctx context.Context, dir, base string) (task.ChangeStats, error)
}
// captureChanges records what this attempt has changed, read off the worktree
// at the moment it was committed or pushed -- the only point where the
// worktree still holds the change and a commit or push is known to have
// happened. A retry resets the branch onto its base and a terminal state
// deletes the worktree, so nothing can re-derive this afterwards.
//
// Capturing is reporting, not work: every failure path here logs and returns,
// because a provenance record that could not be written must never park or
// fail a run that otherwise succeeded.
func (tc *TaskContext) captureChanges(ctx context.Context, after string) {
if tc.Dir == "" {
tc.Log.Warn("change capture skipped: no worktree to read", "captured_after", after)
return
}
reader, ok := tc.Trees.(changeStatsReader)
if !ok {
tc.Log.Warn("change capture unavailable: this worktree implementation cannot report a diffstat",
"captured_after", after)
return
}
stats, err := reader.ChangedFileStats(ctx, tc.Dir, tc.Repo.BaseBranch())
if err != nil {
tc.Log.Warn("change capture failed", "captured_after", after, "err", err)
return
}
// Durable, never bus-only: the worktree this was measured from is gone by
// the time anyone reads it, so a capture that only reached the live feed
// would be lost with the process.
if err := tc.EmitDurable(ctx, events.KindChangesCaptured, tc.Task.Stage, "", changeCaptureData(tc, after, stats)); err != nil {
tc.Log.Warn("change capture not persisted", "captured_after", after, "err", err)
}
}
// changeCaptureData builds the persisted payload for one capture. Totals are
// taken over the FULL set of changed files, so a capture truncated at
// task.MaxCapturedFiles still reports how much the attempt changed -- only the
// per-file breakdown is bounded.
func changeCaptureData(tc *TaskContext, after string, stats task.ChangeStats) map[string]any {
files := stats.Files
truncated := false
if len(files) > task.MaxCapturedFiles {
files = files[:task.MaxCapturedFiles]
truncated = true
}
return map[string]any{
"schema": events.ChangesCapturedSchema,
"owner": tc.Task.Owner,
"repo": tc.Task.Repo,
"base": tc.Repo.BaseBranch(),
"branch": tc.Branch,
"head_sha": stats.HeadSHA,
"base_sha": stats.BaseSHA,
"pr_number": tc.Task.PRNumber,
"captured_after": after,
"files": files,
"totals": stats.Totals,
"truncated": truncated,
}
}
// StagePrepareWorktree clones the repo fresh and checks out the task
// branch. Skips if the daemon already prepared the worktree (Docker
// containers require the tree before acquire).
@@ -42,6 +129,7 @@ func StageCommit(name string, message func(*TaskContext) string) Stage {
if !changed {
return fmt.Errorf("worktree has no changes to commit")
}
tc.captureChanges(ctx, capturedAfterCommit)
return nil
}}
}
@@ -69,7 +157,11 @@ func StageCommitPush(message func(*TaskContext) string) Stage {
if !changed && !tc.BaselineFixed {
return fmt.Errorf("worktree has no changes to commit")
}
return tc.Trees.Push(ctx, tc.Dir, tc.Branch)
if err := tc.Trees.Push(ctx, tc.Dir, tc.Branch); err != nil {
return err
}
tc.captureChanges(ctx, capturedAfterCommitPush)
return nil
}}
}
@@ -211,6 +303,12 @@ func OpenPR(ctx context.Context, tc *TaskContext, body string) error {
}
t.PRNumber = num
tc.Outcome = Outcome{Status: StatusPROpen, Detail: fmt.Sprintf("PR #%d", num)}
// The captures taken while committing and pushing ran before this PR
// existed, so they carry no number and the changed-files view cannot link
// it. This is the one point where the number is known and the worktree is
// still there to measure, so it is captured here rather than left for a
// read to reconstruct. Reporting only: captureChanges never fails the stage.
tc.captureChanges(ctx, capturedAfterOpenPR)
return nil
}
+67
View File
@@ -0,0 +1,67 @@
package task
// ChangeStats is the change one attempt produced, measured at the moment it
// was committed or pushed.
//
// It is a measurement, not a view: the worktree it was read from is deleted on
// merge, close and no-PR terminal states, and a retry resets the branch onto
// its base, so this is the only record of what an attempt actually changed.
// Nothing re-derives it after the fact.
//
// The value types live in this subpackage rather than in package workflow or
// internal/worktree, for two reasons that were checked rather than assumed.
// Package workflow must never import internal/worktree -- it has no such
// import today -- so a shared value cannot live there. And this subpackage is
// not projected into the interpreted-stage symbol table (the generated
// wfextract table names no type from here), whereas a new exported type in
// package workflow would be reachable from repository-authored
// `.archie/stages/*.go`. Declaring it here widens nothing and needs no
// symbol-table regeneration.
type ChangeStats struct {
BaseSHA string `json:"base_sha"`
HeadSHA string `json:"head_sha"`
Files []FileChange `json:"files"`
Totals ChangeTotals `json:"totals"`
}
// FileChange is one file's contribution to a change.
type FileChange struct {
Path string `json:"path"`
// OldPath is the pre-change path, populated only for a rename.
OldPath string `json:"old_path"`
// Status is one of the Change* values below.
Status string `json:"status"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
// Binary reports that the file produced no textual hunks. It is NOT a
// claim that the file is binary on disk: an empty or otherwise chunkless
// edit reports this too, so it must never be rendered as a file type.
Binary bool `json:"binary"`
}
// ChangeTotals summarises a change. Totals always cover the FULL set of
// changed files, even when FileChange entries were dropped by the
// MaxCapturedFiles cap, so a truncated capture still reports honest totals.
type ChangeTotals struct {
Files int `json:"files"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
}
// Change status values as they are persisted inside a changes_captured
// event's data. Like the task lifecycle statuses, these strings are part of
// the on-disk format: renaming one is a migration, not a rename.
const (
ChangeAdded = "added"
ChangeModified = "modified"
ChangeDeleted = "deleted"
ChangeRenamed = "renamed"
ChangeTypeChanged = "typechange"
)
// MaxCapturedFiles caps how many file entries one capture persists. Unlike
// Event.Detail, an event's data is not length-limited by the store, so this cap
// is what keeps one large refactor from writing an unbounded payload. A capture
// that dropped entries keeps the totals of the full set and reports itself as
// truncated.
const MaxCapturedFiles = 200
@@ -0,0 +1,29 @@
package task
import "testing"
// TestChangeStatusesAreSpelledAsPersisted pins the five status strings as
// literals. They are the on-disk vocabulary of a changes_captured payload and
// the dashboard mirrors them as its own key set
// (ui/src/tasks/changed-files.jsx FILE_STATUS), so renaming one here without
// the matching dashboard change renders raw status codes instead of words --
// with both suites still green unless each side pins the literal.
func TestChangeStatusesAreSpelledAsPersisted(t *testing.T) {
statuses := []struct {
name string
got string
want string
}{
{name: "ChangeAdded", got: ChangeAdded, want: "added"},
{name: "ChangeModified", got: ChangeModified, want: "modified"},
{name: "ChangeDeleted", got: ChangeDeleted, want: "deleted"},
{name: "ChangeRenamed", got: ChangeRenamed, want: "renamed"},
{name: "ChangeTypeChanged", got: ChangeTypeChanged, want: "typechange"},
}
for _, s := range statuses {
if s.got != s.want {
t.Errorf("%s = %q, want %q", s.name, s.got, s.want)
}
}
}
@@ -2,6 +2,7 @@ package wfextract
import (
"context"
"reflect"
"testing"
"github.com/traefik/yaegi/interp"
@@ -109,3 +110,69 @@ func TestWrapperNilGuardsPreserved(t *testing.T) {
t.Errorf("ChangedLines on nil WChangedLines = %v, want nil", err)
}
}
// TestChangeCaptureIsNotCallableFromInterpretedCode is the acceptance test for
// "the sandbox surface did not widen", in the same shape as the reachability
// test above and with the opposite assertion.
//
// An attempt's changed files are captured through an UNEXPORTED optional
// interface asserted on TaskContext.Trees, precisely so that the capability is
// not part of workflow.Trees: Trees is projected into this symbol table, so a
// method added to it would become callable by every repository-authored
// .archie/stages/*.go file. Both halves are asserted here -- the interface's
// method set, and the interpreted call that must not resolve -- because the
// second alone would be weaker: it would still pass if the symbol table lost
// Trees altogether.
func TestChangeCaptureIsNotCallableFromInterpretedCode(t *testing.T) {
trees := reflect.TypeFor[workflow.Trees]()
if _, ok := trees.MethodByName("ChangedFileStats"); ok {
t.Fatalf("workflow.Trees gained ChangedFileStats; interpreted stage code can now read a diffstat the engine only meant to capture")
}
// The control first: an interpreted call to a method Trees really has must
// resolve, so the refusal below is the missing method and not a broken
// harness.
const control = `package main
import (
"context"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
func UseTrees(t workflow.Trees) {
_, _ = t.Diff(context.Background(), "", "")
}
`
const probe = `package main
import (
"context"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
// CaptureDiffstat is an interpreted stage trying to reach the engine's
// change capture through the surface TaskContext hands it.
func CaptureDiffstat(t workflow.Trees) {
stats, err := t.ChangedFileStats(context.Background(), "", "")
_, _ = stats.HeadSHA, err
}
`
i, err := yaegiutil.New(interp.Options{}, Symbols)
if err != nil {
t.Fatalf("New: %v", err)
}
if _, err := yaegiutil.Resolve[func(workflow.Trees)](i, control, "main.UseTrees"); err != nil {
t.Fatalf("the control call Trees.Diff did not resolve (%v); this test cannot tell a missing capability from a broken symbol table", err)
}
j, err := yaegiutil.New(interp.Options{}, Symbols)
if err != nil {
t.Fatalf("New: %v", err)
}
if _, err := yaegiutil.Resolve[func(workflow.Trees)](j, probe, "main.CaptureDiffstat"); err == nil {
t.Error("interpreted stage code resolved Trees.ChangedFileStats; the capture capability leaked into the projected surface")
}
}
+23 -4
View File
@@ -134,7 +134,13 @@ type TaskContext struct {
}
// Emit publishes an observability event stamped with the task's
// identity. Safe on a nil bus.
// identity and the attempt that produced it. Safe on a nil bus.
//
// The attempt comes from the task record this context runs against, so a
// reader can attribute the event to one run without segmenting the task's
// stream by stage order. It is never guessed: a producer with no attempt
// (the deliberately task-agnostic daemon events) leaves it zero, which reads
// as unattributed rather than as a first run.
func (tc *TaskContext) Emit(kind, stage, detail string, data map[string]any) {
if tc.Bus == nil {
return
@@ -145,6 +151,7 @@ func (tc *TaskContext) Emit(kind, stage, detail string, data map[string]any) {
Repo: tc.Task.Owner + "/" + tc.Task.Repo,
Issue: tc.Task.IssueNumber,
Workflow: tc.Task.Workflow,
Attempt: tc.Task.Attempt,
Stage: stage,
Detail: detail,
Data: data,
@@ -161,7 +168,8 @@ func (tc *TaskContext) EmitDurable(ctx context.Context, kind, stage, detail stri
}
event := events.Event{
At: time.Now().UTC(), Kind: kind, TaskID: tc.Task.ID, Repo: tc.Task.Owner + "/" + tc.Task.Repo,
Issue: tc.Task.IssueNumber, Workflow: tc.Task.Workflow, Stage: stage, Detail: detail, Data: data,
Issue: tc.Task.IssueNumber, Workflow: tc.Task.Workflow, Attempt: tc.Task.Attempt,
Stage: stage, Detail: detail, Data: data,
}
id, err := tc.Store.InsertEvent(ctx, event)
if err != nil {
@@ -293,7 +301,17 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) {
for _, stage := range wf.Stages {
t.Stage = stage.Name
_ = tc.Store.Update(ctx, t)
log.Info("stage starting", "stage", stage.Name)
// The stage is bound onto the task logger for exactly the stage's own
// execution and removed afterwards, so every line a stage's code writes
// is selectable by stage (internal/logging.Query.Stage). It is restored
// rather than left in place because a line written outside any stage is
// not attributable to one. Agent and tool output is logged by the runtime
// that produced it, which never sees this logger, so it carries no stage:
// the stage filter narrows the log, it does not cover it.
previousLog := tc.Log
stageLog := previousLog.With("stage", stage.Name)
tc.Log = stageLog
stageLog.Info("stage starting")
tc.Emit(events.KindStageStart, stage.Name, "", nil)
started := time.Now()
@@ -306,13 +324,14 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) {
}
}
tc.Emit(events.KindStageFinish, stage.Name, "", data)
tc.Log = previousLog
if err != nil {
// Daemon shutdown is not a workflow failure. Leave the task running
// so Startup's existing crash recovery requeues it; parking here
// would publish a false failure and require manual intervention.
if ctx.Err() != nil {
log.Info("stage interrupted", "stage", stage.Name, "err", err)
stageLog.Info("stage interrupted", "err", err)
return
}
t.ParkReason = fmt.Sprintf("stage %s: %v", stage.Name, err)