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:
1 parent
14e7512d14
commit
2c09f42ff4
39 files changed
+4264
-83
No files matched your search
@@ -96,10 +96,26 @@ func TestStateStoreConformance(t *testing.T) {
|
||||
if err := c.Update(ctx, task); err != nil {
|
||||
t.Fatalf("Update: %v", err)
|
||||
}
|
||||
eventID, err := c.InsertEvent(ctx, events.Event{Kind: "stage_finish", TaskID: task.ID, Data: map[string]any{"duration_ms": 12.0}})
|
||||
eventID, err := c.InsertEvent(ctx, events.Event{Kind: "stage_finish", TaskID: task.ID, Attempt: 2, Data: map[string]any{"duration_ms": 12.0}})
|
||||
if err != nil || eventID == 0 {
|
||||
t.Fatalf("InsertEvent: %v %v", eventID, err)
|
||||
}
|
||||
// R4's wire acceptance: the per-attempt configuration document IS an
|
||||
// event, so it has to survive the same hop an event does, including
|
||||
// its nested structure and the attempt key that attributes it to one
|
||||
// run. A payload that arrives flattened reads as a config with no
|
||||
// fields at all. The document rides under the daemon's own key
|
||||
// (internal/daemon/daemon.go), asserted by name below.
|
||||
_, err = c.InsertEvent(ctx, events.Event{
|
||||
Kind: events.KindConfigCaptured, TaskID: task.ID, Attempt: 2,
|
||||
Data: map[string]any{
|
||||
"schema": events.ConfigCapturedSchema,
|
||||
"document": map[string]any{"bot_user": "archie", "models": map[string]any{"implement": "anthropic/claude"}},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("InsertEvent config_captured: %v", err)
|
||||
}
|
||||
|
||||
// Stale transition: from no longer matches current status.
|
||||
err = c.Transition(ctx, task.ID, "queued", "merged", "")
|
||||
@@ -131,9 +147,26 @@ func TestStateStoreConformance(t *testing.T) {
|
||||
}
|
||||
|
||||
// TaskEvents / EventsSince / stats.
|
||||
if evs, err := c.TaskEvents(ctx, task.ID); err != nil || len(evs) != 1 || evs[0].Data["duration_ms"] != 12.0 {
|
||||
evs, err := c.TaskEvents(ctx, task.ID)
|
||||
if err != nil || len(evs) != 2 || evs[0].Data["duration_ms"] != 12.0 {
|
||||
t.Fatalf("TaskEvents: %+v %v", evs, err)
|
||||
}
|
||||
if evs[0].Attempt != 2 {
|
||||
t.Errorf("TaskEvents attempt = %d, want 2 (provenance must survive the wire)", evs[0].Attempt)
|
||||
}
|
||||
if evs[1].Kind != events.KindConfigCaptured || evs[1].Attempt != 2 || evs[1].Data["schema"] != events.ConfigCapturedSchema {
|
||||
t.Errorf("config_captured event = %+v, want its kind, attempt and schema preserved", evs[1])
|
||||
}
|
||||
if _, renamed := evs[1].Data["config"]; renamed {
|
||||
t.Error(`the document crossed under "config"; the producer writes it under "document"`)
|
||||
}
|
||||
if len(evs[1].Data) != 2 {
|
||||
t.Errorf("config_captured data keys = %v, want exactly schema and document", evs[1].Data)
|
||||
}
|
||||
doc, ok := evs[1].Data["document"].(map[string]any)
|
||||
if !ok || doc["bot_user"] != "archie" {
|
||||
t.Errorf("config_captured payload = %#v, want the decoded document under its own key", evs[1].Data)
|
||||
}
|
||||
if _, err := c.EventsSince(ctx, 0, 10); err != nil {
|
||||
t.Fatalf("EventsSince: %v", err)
|
||||
}
|
||||
|
||||
@@ -167,6 +167,41 @@ func TestTaskGrantScopesWorkerToItsOwnThreeRPCs(t *testing.T) {
|
||||
if _, err := workerA.InsertEvent(ctx, events.Event{TaskID: taskA.ID, Kind: "log"}); err != nil {
|
||||
t.Fatalf("task grant should authorize InsertEvent on its own task: %v", err)
|
||||
}
|
||||
// The documented residual of R4, pinned rather than hidden: a task grant
|
||||
// may insert events on its OWN task -- that is how tool_call and agent_finish
|
||||
// already work -- so a task can write its own config_captured provenance,
|
||||
// and only its own. The permission is the existing InsertEvent permission,
|
||||
// not a new surface, which is why the provenance is exactly as trustworthy
|
||||
// as the rest of that task's event stream.
|
||||
configEvent := events.Event{
|
||||
TaskID: taskA.ID, Kind: events.KindConfigCaptured, Attempt: 1,
|
||||
Data: map[string]any{"schema": events.ConfigCapturedSchema, "document": map[string]any{"bot_user": "archie"}},
|
||||
}
|
||||
if _, err := workerA.InsertEvent(ctx, configEvent); err != nil {
|
||||
t.Fatalf("task grant should authorize its own config_captured event: %v", err)
|
||||
}
|
||||
configEvent.TaskID = taskB.ID
|
||||
if _, err := workerA.InsertEvent(ctx, configEvent); err == nil {
|
||||
t.Fatal("task grant must not authorize a config_captured event on another task")
|
||||
}
|
||||
// The document rides under the producer's own key (internal/daemon/daemon.go
|
||||
// writes "document"), so the fixture above is read back and asserted by key
|
||||
// rather than trusted: a rename would otherwise leave this test green.
|
||||
stored, err := admin.TaskEvents(ctx, taskA.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskEvents: %v", err)
|
||||
}
|
||||
for _, e := range stored {
|
||||
if e.Kind != events.KindConfigCaptured {
|
||||
continue
|
||||
}
|
||||
if _, renamed := e.Data["config"]; renamed {
|
||||
t.Error(`the document crossed under "config"; the producer writes it under "document"`)
|
||||
}
|
||||
if _, ok := e.Data["document"].(map[string]any); !ok {
|
||||
t.Errorf("config_captured data = %#v, want the document under the producer's key", e.Data)
|
||||
}
|
||||
}
|
||||
if err := workerA.Update(ctx, &workflow.Task{ID: taskA.ID, Owner: "acme", Repo: "widget"}); err != nil {
|
||||
t.Fatalf("task grant should authorize Update on its own task: %v", err)
|
||||
}
|
||||
|
||||
@@ -113,8 +113,8 @@ func eventDataValue(s string) map[string]any {
|
||||
func eventProto(e events.Event) *pb.Event {
|
||||
return &pb.Event{
|
||||
Id: e.ID, At: timestamp(e.At), Kind: e.Kind, TaskId: e.TaskID, Repo: e.Repo,
|
||||
Issue: int64(e.Issue), Workflow: e.Workflow, Stage: e.Stage, Detail: e.Detail,
|
||||
DataJson: eventDataJSON(e.Data),
|
||||
Issue: int64(e.Issue), Workflow: e.Workflow, Stage: e.Stage, Attempt: int64(e.Attempt),
|
||||
Detail: e.Detail, DataJson: eventDataJSON(e.Data),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,8 +124,8 @@ func eventValue(e *pb.Event) events.Event {
|
||||
}
|
||||
return events.Event{
|
||||
ID: e.Id, At: timeValue(e.At), Kind: e.Kind, TaskID: e.TaskId, Repo: e.Repo,
|
||||
Issue: int(e.Issue), Workflow: e.Workflow, Stage: e.Stage, Detail: e.Detail,
|
||||
Data: eventDataValue(e.DataJson),
|
||||
Issue: int(e.Issue), Workflow: e.Workflow, Stage: e.Stage, Attempt: int(e.Attempt),
|
||||
Detail: e.Detail, Data: eventDataValue(e.DataJson),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -426,7 +426,7 @@ func taskLogEntryValue(e *pb.TaskLogEntry) logging.Entry {
|
||||
func taskLogRequestProto(taskID int64, attempt int, q logging.Query) *pb.ReadTaskLogRequest {
|
||||
return &pb.ReadTaskLogRequest{
|
||||
TaskId: taskID, Attempt: int64(attempt), Limit: int64(q.Limit),
|
||||
Levels: q.Levels, Component: q.Component, Contains: q.Contains,
|
||||
Levels: q.Levels, Component: q.Component, Stage: q.Stage, Contains: q.Contains,
|
||||
Since: timestamp(q.Since), Until: timestamp(q.Until),
|
||||
}
|
||||
}
|
||||
@@ -435,6 +435,7 @@ func taskLogQueryValue(r *pb.ReadTaskLogRequest) logging.Query {
|
||||
return logging.Query{
|
||||
Levels: r.Levels,
|
||||
Component: r.Component,
|
||||
Stage: r.Stage,
|
||||
Contains: r.Contains,
|
||||
Limit: int(r.Limit),
|
||||
Since: timeValue(r.Since),
|
||||
|
||||
@@ -4,8 +4,95 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
)
|
||||
|
||||
func TestEventAttemptSurvivesTheWire(t *testing.T) {
|
||||
// attempt is provenance, so it must cross the boundary or the dashboard
|
||||
// cannot attribute an event to a run. Zero must survive too: it is the
|
||||
// unattributed marker, and a mapping that turned it into an absent field
|
||||
// would make "unattributed" unrepresentable.
|
||||
for _, attempt := range []int{0, 1, 7} {
|
||||
t.Run(fmt.Sprintf("attempt=%d", attempt), func(t *testing.T) {
|
||||
original := events.Event{
|
||||
Kind: events.KindStageFinish, TaskID: 42, Stage: "implement",
|
||||
Attempt: attempt, Detail: "done",
|
||||
Data: map[string]any{"duration_ms": float64(1200)},
|
||||
}
|
||||
got := eventValue(eventProto(original))
|
||||
if got.Attempt != attempt {
|
||||
t.Fatalf("attempt round-trip = %d, want %d", got.Attempt, attempt)
|
||||
}
|
||||
if got.Kind != original.Kind || got.TaskID != original.TaskID || got.Stage != original.Stage {
|
||||
t.Fatalf("event round-trip = %+v, want %+v", got, original)
|
||||
}
|
||||
if got.Data["duration_ms"] != float64(1200) {
|
||||
t.Fatalf("data round-trip = %v", got.Data)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigCapturedPayloadSurvivesTheWire(t *testing.T) {
|
||||
// R4's document is a nested JSON object inside the event's data, so the
|
||||
// round trip has to keep its structure rather than flattening it to a
|
||||
// string: the Config tab reads fields out of it. The key is the daemon's own
|
||||
// -- internal/daemon/daemon.go writes the document under "document" -- and
|
||||
// is asserted by name here so a rename cannot ride along unnoticed.
|
||||
document := map[string]any{
|
||||
"schema": events.ConfigCapturedSchema,
|
||||
"document": map[string]any{
|
||||
"bot_user": "archie",
|
||||
"diff_cap_lines": float64(4000),
|
||||
"models": map[string]any{"implement": "anthropic/claude"},
|
||||
},
|
||||
}
|
||||
got := eventValue(eventProto(events.Event{
|
||||
Kind: events.KindConfigCaptured, TaskID: 42, Attempt: 2, Data: document,
|
||||
}))
|
||||
if got.Kind != events.KindConfigCaptured || got.Attempt != 2 {
|
||||
t.Fatalf("config event = %+v", got)
|
||||
}
|
||||
if _, renamed := got.Data["config"]; renamed {
|
||||
t.Error(`the document crossed under "config"; the producer writes it under "document"`)
|
||||
}
|
||||
if len(got.Data) != 2 {
|
||||
t.Errorf("data keys = %v, want exactly schema and document", got.Data)
|
||||
}
|
||||
doc, ok := got.Data["document"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("document payload = %#v, want a decoded object under the producer's own key", got.Data)
|
||||
}
|
||||
if doc["bot_user"] != "archie" {
|
||||
t.Errorf("document.bot_user = %v, want archie", doc["bot_user"])
|
||||
}
|
||||
models, ok := doc["models"].(map[string]any)
|
||||
if !ok || models["implement"] != "anthropic/claude" {
|
||||
t.Errorf("document.models = %#v, want the nested map preserved", doc["models"])
|
||||
}
|
||||
if got.Data["schema"] != events.ConfigCapturedSchema {
|
||||
t.Errorf("schema = %v, want %q", got.Data["schema"], events.ConfigCapturedSchema)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskLogStageFilterSurvivesTheWire(t *testing.T) {
|
||||
// The stage filter is only useful if it reaches the reader, and the wire
|
||||
// mirrors the whole logging.Query for exactly that reason: a filter that
|
||||
// silently stops working at the boundary looks like "no matches".
|
||||
query := logging.Query{
|
||||
Levels: []string{"ERROR"}, Component: "daemon", Stage: "implement", Contains: "parked", Limit: 50,
|
||||
}
|
||||
got := taskLogQueryValue(taskLogRequestProto(42, 3, query))
|
||||
if got.Stage != "implement" {
|
||||
t.Fatalf("stage round-trip = %q, want implement", got.Stage)
|
||||
}
|
||||
if got.Component != "daemon" || got.Contains != "parked" || got.Limit != 50 {
|
||||
t.Fatalf("query round-trip = %+v, want the whole filter preserved", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventDataJSONPreservesMarshalErrorMarker pins §11's conformance
|
||||
// invariant: the local store (internal/store/events.go) records a
|
||||
// {"marshal_error":%q} marker when json.Marshal of event Data fails, so the
|
||||
|
||||
@@ -16,7 +16,7 @@ func (s Store) TaskByID(ctx context.Context, id int64) (*taskactions.Task, error
|
||||
if err != nil || t == nil {
|
||||
return nil, err
|
||||
}
|
||||
return &taskactions.Task{ID: t.ID, Owner: t.Owner, Repo: t.Repo, Identity: t.Identity, Status: t.Status, Stage: t.Stage, ParkReason: t.ParkReason, IssueNumber: t.IssueNumber, RetryCount: t.RetryCount, ForgeBacked: t.IsForgeBacked()}, nil
|
||||
return &taskactions.Task{ID: t.ID, Owner: t.Owner, Repo: t.Repo, Identity: t.Identity, Status: t.Status, Stage: t.Stage, ParkReason: t.ParkReason, IssueNumber: t.IssueNumber, RetryCount: t.RetryCount, Attempt: t.Attempt, ForgeBacked: t.IsForgeBacked()}, nil
|
||||
}
|
||||
|
||||
func MaxRetries(cfg *config.Holder) func(*taskactions.Task) int {
|
||||
|
||||
Reference in new issue
Block a user