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.
217 lines
5.4 KiB
Go
217 lines
5.4 KiB
Go
package events
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestBusFanOutAndDrop(t *testing.T) {
|
|
b := NewBus()
|
|
defer b.Close()
|
|
|
|
fast := b.Subscribe(8)
|
|
slow := b.Subscribe(2) // will overflow
|
|
|
|
for i := range 5 {
|
|
b.Publish(Event{Kind: KindLog, Issue: i})
|
|
}
|
|
|
|
if got := len(fast.C); got != 5 {
|
|
t.Fatalf("fast subscriber buffered %d, want 5", got)
|
|
}
|
|
if got := len(slow.C); got != 2 {
|
|
t.Fatalf("slow subscriber buffered %d, want 2", got)
|
|
}
|
|
if d := slow.Dropped(); d != 3 {
|
|
t.Fatalf("slow.Dropped() = %d, want 3", d)
|
|
}
|
|
|
|
e := <-fast.C
|
|
if e.At.IsZero() {
|
|
t.Fatal("Publish must stamp At")
|
|
}
|
|
|
|
fast.Close()
|
|
b.Publish(Event{Kind: KindLog}) // must not panic on closed subscriber
|
|
if got := len(slow.C); got != 2 {
|
|
t.Fatalf("closed subscriber removal broke fan-out: slow has %d", got)
|
|
}
|
|
}
|
|
|
|
func TestBusCloseStopsDelivery(t *testing.T) {
|
|
b := NewBus()
|
|
s := b.Subscribe(4)
|
|
b.Close()
|
|
b.Publish(Event{Kind: KindLog}) // no-op, no panic
|
|
if _, open := <-s.C; open {
|
|
t.Fatal("subscriber channel must be closed after bus Close")
|
|
}
|
|
}
|
|
|
|
// ── Regression tests ───────────────────────────────────────────────
|
|
|
|
func TestSubscribeZeroBufferDefaults(t *testing.T) {
|
|
b := NewBus()
|
|
defer b.Close()
|
|
s := b.Subscribe(0)
|
|
// Send 64 events (default buffer size) — none should be dropped.
|
|
for i := range 64 {
|
|
b.Publish(Event{Kind: KindLog, Issue: i})
|
|
}
|
|
if d := s.Dropped(); d != 0 {
|
|
t.Errorf("0-buffer subscriber should default to 64, but dropped %d", d)
|
|
}
|
|
}
|
|
|
|
func TestSubscribeNegativeBufferDefaults(t *testing.T) {
|
|
b := NewBus()
|
|
defer b.Close()
|
|
s := b.Subscribe(-1)
|
|
for i := range 64 {
|
|
b.Publish(Event{Kind: KindLog, Issue: i})
|
|
}
|
|
if d := s.Dropped(); d != 0 {
|
|
t.Errorf("negative-buffer subscriber should default to 64, but dropped %d", d)
|
|
}
|
|
}
|
|
|
|
func TestSubscribeAfterCloseReturnsClosedChannel(t *testing.T) {
|
|
b := NewBus()
|
|
b.Close()
|
|
s := b.Subscribe(10)
|
|
if _, open := <-s.C; open {
|
|
t.Error("subscribe after close should return closed channel")
|
|
}
|
|
}
|
|
|
|
func TestDoubleCloseIsSafe(t *testing.T) {
|
|
b := NewBus()
|
|
b.Close()
|
|
// Must not panic.
|
|
b.Close()
|
|
}
|
|
|
|
func TestPublishDuringCloseConcurrent(t *testing.T) {
|
|
b := NewBus()
|
|
s := b.Subscribe(100)
|
|
done := make(chan struct{})
|
|
go func() {
|
|
for range 50 {
|
|
b.Publish(Event{Kind: KindLog})
|
|
}
|
|
close(done)
|
|
}()
|
|
b.Close()
|
|
<-done
|
|
// Drain channel — all published events plus eventual close.
|
|
for range s.C {
|
|
}
|
|
// Must not panic.
|
|
}
|
|
|
|
func TestSlowSubscriberDoesNotBlockOthers(t *testing.T) {
|
|
b := NewBus()
|
|
defer b.Close()
|
|
|
|
slow := b.Subscribe(1) // tiny buffer
|
|
fast := b.Subscribe(100) // large buffer
|
|
|
|
// Publish enough to overflow slow subscriber.
|
|
for i := range 50 {
|
|
b.Publish(Event{Kind: KindLog, Issue: i})
|
|
}
|
|
|
|
// Fast subscriber should have received all 50 (or at least most).
|
|
// The key assertion: fast subscriber was not blocked by slow.
|
|
if len(fast.C) < 40 {
|
|
t.Errorf("fast subscriber only got %d events; slow should not block fast", len(fast.C))
|
|
}
|
|
if slow.Dropped() == 0 {
|
|
t.Error("slow subscriber with buffer 1 should have dropped events")
|
|
}
|
|
}
|
|
|
|
func TestMultiSubscriberOrdering(t *testing.T) {
|
|
b := NewBus()
|
|
defer b.Close()
|
|
|
|
a := b.Subscribe(10)
|
|
bSub := b.Subscribe(10)
|
|
|
|
b.Publish(Event{Kind: KindLog, Issue: 1})
|
|
b.Publish(Event{Kind: KindLog, Issue: 2})
|
|
b.Publish(Event{Kind: KindLog, Issue: 3})
|
|
|
|
// Both subscribers should receive events in order.
|
|
for _, sub := range []*Sub{a, bSub} {
|
|
if e := <-sub.C; e.Issue != 1 {
|
|
t.Errorf("expected issue 1, got %d", e.Issue)
|
|
}
|
|
if e := <-sub.C; e.Issue != 2 {
|
|
t.Errorf("expected issue 2, got %d", e.Issue)
|
|
}
|
|
if e := <-sub.C; e.Issue != 3 {
|
|
t.Errorf("expected issue 3, got %d", e.Issue)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSubscriberCloseRemovesFromBus(t *testing.T) {
|
|
b := NewBus()
|
|
defer b.Close()
|
|
|
|
s := b.Subscribe(10)
|
|
s.Close()
|
|
|
|
// Publish after close — must not block or panic.
|
|
b.Publish(Event{Kind: KindLog})
|
|
// Channel should be closed.
|
|
if _, open := <-s.C; open {
|
|
t.Error("channel should be closed after Sub.Close()")
|
|
}
|
|
}
|
|
|
|
func TestPublishStampsAt(t *testing.T) {
|
|
b := NewBus()
|
|
defer b.Close()
|
|
s := b.Subscribe(1)
|
|
|
|
before := time.Now().UTC()
|
|
b.Publish(Event{Kind: KindLog})
|
|
after := time.Now().UTC()
|
|
|
|
e := <-s.C
|
|
if e.At.Before(before) || e.At.After(after) {
|
|
t.Errorf("At = %v, expected between %v and %v", e.At, before, after)
|
|
}
|
|
}
|
|
|
|
func TestPublishDoesNotOverwritePreSetAt(t *testing.T) {
|
|
b := NewBus()
|
|
defer b.Close()
|
|
s := b.Subscribe(1)
|
|
|
|
preset := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
b.Publish(Event{Kind: KindLog, At: preset})
|
|
|
|
e := <-s.C
|
|
if !e.At.Equal(preset) {
|
|
t.Errorf("At = %v, want %v (preset should not be overwritten)", e.At, preset)
|
|
}
|
|
}
|
|
|
|
// TestCapturedSchemasAreSpelledAsTheDashboardExpects pins the two schema names
|
|
// as literals. They are wire vocabulary: the dashboard mirrors them
|
|
// (ui/src/tasks/attempt-config.jsx, and api_tasks_changes.go for the changes
|
|
// one), so changing a name here without changing the reader turns every
|
|
// capture into "recorded but could not be read". The literal assertion is what
|
|
// makes that a failing test rather than a silent one.
|
|
func TestCapturedSchemasAreSpelledAsTheDashboardExpects(t *testing.T) {
|
|
if ConfigCapturedSchema != "archie/task-config@1" {
|
|
t.Errorf("ConfigCapturedSchema = %q, want archie/task-config@1", ConfigCapturedSchema)
|
|
}
|
|
if ChangesCapturedSchema != "archie/task-changes@1" {
|
|
t.Errorf("ChangesCapturedSchema = %q, want archie/task-changes@1", ChangesCapturedSchema)
|
|
}
|
|
}
|