Files
archie/internal/agentexec/taskevents_test.go
T
sam 2ef962912a 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.
2026-09-18 18:32:59 +10:00

131 lines
4.5 KiB
Go

package agentexec
import (
"encoding/json"
"errors"
"log/slog"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/events"
)
var errPublishBoom = errors.New("publish boom")
// eventPublisherStub is an EventPublisher that records every call and can be
// told to fail, mirroring fakePublisher in systemlog_test.go for the same
// reason: assert both the happy path and "one failure must not stop the
// rest."
type eventPublisherStub struct {
subjects []string
payloads [][]byte
failWith error
}
func (p *eventPublisherStub) Publish(subject string, data []byte) error {
p.subjects = append(p.subjects, subject)
p.payloads = append(p.payloads, append([]byte(nil), data...))
return p.failWith
}
// TestForwardTaskEventsPublishesEachEventOnItsTaskSubject is the core
// contract: everything published on the bus while ForwardTaskEvents is
// running arrives at pub, JSON-decodable back into the same Event, on
// SubjectForEvents(taskID) -- regardless of which task's events happen to be
// flowing, since taskID is a ForwardTaskEvents parameter, not read from the
// event itself.
func TestForwardTaskEventsPublishesEachEventOnItsTaskSubject(t *testing.T) {
bus := events.NewBus()
sub := bus.Subscribe(8)
pub := &eventPublisherStub{}
done := make(chan struct{})
go func() {
ForwardTaskEvents(sub, pub, 42, slog.New(slog.DiscardHandler))
close(done)
}()
want := []events.Event{
{Kind: events.KindStageStart, TaskID: 42, Stage: "baseline", Attempt: 1},
{Kind: events.KindStageFinish, TaskID: 42, Stage: "baseline", Attempt: 1, Data: map[string]any{"duration_ms": float64(120)}},
{Kind: events.KindParked, TaskID: 42, Attempt: 1, Detail: "baseline red"},
// R4's document rides this bridge too: a worker captures its own
// effective configuration, and the daemon is a different process, so
// the payload has to survive the hop or the Config tab reads nothing.
// The daemon's own key for it is asserted below by name.
{Kind: events.KindConfigCaptured, TaskID: 42, Attempt: 1, Data: map[string]any{
"schema": events.ConfigCapturedSchema,
"document": map[string]any{"bot_user": "archie"},
}},
}
for _, e := range want {
bus.Publish(e)
}
sub.Close()
<-done
if len(pub.payloads) != len(want) {
t.Fatalf("published %d events, want %d", len(pub.payloads), len(want))
}
wantSubject := SubjectForEvents(42)
for i, payload := range pub.payloads {
if pub.subjects[i] != wantSubject {
t.Errorf("event %d subject = %q, want %q", i, pub.subjects[i], wantSubject)
}
var got events.Event
if err := json.Unmarshal(payload, &got); err != nil {
t.Fatalf("event %d undecodable: %v", i, err)
}
if got.Kind != want[i].Kind || got.TaskID != want[i].TaskID || got.Stage != want[i].Stage || got.Detail != want[i].Detail {
t.Errorf("event %d = %+v, want %+v", i, got, want[i])
}
if got.Attempt != want[i].Attempt {
t.Errorf("event %d attempt = %d, want %d (provenance must not be dropped on the hop)", i, got.Attempt, want[i].Attempt)
}
if got.Kind == events.KindConfigCaptured {
if got.Data["schema"] != events.ConfigCapturedSchema {
t.Errorf("event %d schema = %v, want %q", i, got.Data["schema"], events.ConfigCapturedSchema)
}
if _, renamed := got.Data["config"]; renamed {
t.Errorf(`event %d crossed under "config"; the producer writes the document under "document"`, i)
}
doc, ok := got.Data["document"].(map[string]any)
if !ok || doc["bot_user"] != "archie" {
t.Errorf("event %d document payload = %#v, want the decoded document under its own key", i, got.Data["document"])
}
}
if got.At.IsZero() {
t.Errorf("event %d has zero At, want the bus-stamped publish time", i)
}
}
}
// TestForwardTaskEventsSurvivesAPublishFailure pins "one missing event must
// never abort the ones after it": event shipping must never block or fail
// the workflow run it is reporting on.
func TestForwardTaskEventsSurvivesAPublishFailure(t *testing.T) {
bus := events.NewBus()
sub := bus.Subscribe(8)
pub := &eventPublisherStub{failWith: errPublishBoom}
done := make(chan struct{})
go func() {
ForwardTaskEvents(sub, pub, 7, slog.New(slog.DiscardHandler))
close(done)
}()
bus.Publish(events.Event{Kind: events.KindStageStart, TaskID: 7})
bus.Publish(events.Event{Kind: events.KindOutcome, TaskID: 7})
sub.Close()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("ForwardTaskEvents did not return after sub.Close(); a publish failure blocked the loop")
}
if len(pub.payloads) != 2 {
t.Fatalf("published %d events despite failures, want 2 (both attempted)", len(pub.payloads))
}
}