Files
archie/internal/webui/api_tasks_debug_test.go
T
sam 2c09f42ff4 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

99 lines
3.5 KiB
Go

package webui
import (
"encoding/json"
"net/http"
"strings"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/events"
)
// TestHandleTaskDebugReturnsTheWholeRecordAndEveryEvent pins R7's read: the
// stored task record verbatim, and EVERY event of the task rather than only the
// selected attempt's -- each event carries its own attempt, so a filtered list
// would hide the very provenance the view exists to show.
func TestHandleTaskDebugReturnsTheWholeRecordAndEveryEvent(t *testing.T) {
srv := newTestServer(t)
current := twoAttemptTask(t, srv)
insertTaskEvent(t, srv, events.Event{
Kind: events.KindStageStart, TaskID: current.ID, Attempt: 1, Stage: "prepare", At: railBase,
})
insertTaskEvent(t, srv, events.Event{
Kind: events.KindAgentFinish, TaskID: current.ID, Attempt: 0, At: railBase.Add(time.Second),
Data: map[string]any{"status": "ok"},
})
insertTaskEvent(t, srv, events.Event{
Kind: events.KindStageStart, TaskID: current.ID, Attempt: 2, Stage: "implement",
At: railBase.Add(2 * time.Second),
})
w := getTaskAPI(t, srv, taskRoute(current.ID, "/debug?attempt=1"))
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body %s", w.Code, w.Body)
}
raw := w.Body.String()
if strings.Contains(raw, `"entries"`) {
t.Errorf("body carries a log payload: %s", raw)
}
var body struct {
TaskID int64 `json:"task_id"`
Attempt int `json:"attempt"`
Task workflow.Task `json:"task"`
Events []struct {
Kind string `json:"kind"`
Attempt int `json:"attempt"`
} `json:"events"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v (%s)", err, w.Body)
}
if body.TaskID != current.ID || body.Attempt != 1 {
t.Errorf("envelope = task %d attempt %d, want task %d attempt 1 (the selected attempt)",
body.TaskID, body.Attempt, current.ID)
}
if body.Task.ID != current.ID || body.Task.Status != workflow.StatusRunning || body.Task.Attempt != 2 {
t.Errorf("task record = id %d status %q attempt %d, want the stored record (%d, running, 2)",
body.Task.ID, body.Task.Status, body.Task.Attempt, current.ID)
}
if len(body.Events) != 3 {
t.Fatalf("events = %d, want all 3 including the one recorded against a different attempt (%s)",
len(body.Events), w.Body)
}
wantAttempts := []int{1, 0, 2}
for i, want := range wantAttempts {
if body.Events[i].Attempt != want {
t.Errorf("event %d attempt = %d, want %d (order and attribution preserved)", i, body.Events[i].Attempt, want)
}
}
if !strings.Contains(raw, `"attempt":0`) {
t.Errorf("body = %s, want attempt carried on every event so an unattributed one stays distinguishable", raw)
}
}
// TestHandleTaskDebugResolvesTheCurrentAttemptWithoutAParameter pins that the
// envelope reports the attempt the page selected, defaulting to the task's
// current attempt exactly as the log read does.
func TestHandleTaskDebugResolvesTheCurrentAttemptWithoutAParameter(t *testing.T) {
srv := newTestServer(t)
current := twoAttemptTask(t, srv)
w := getTaskAPI(t, srv, taskRoute(current.ID, "/debug"))
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body %s", w.Code, w.Body)
}
var body struct {
Attempt int `json:"attempt"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("decode response: %v (%s)", err, w.Body)
}
if body.Attempt != current.Attempt {
t.Errorf("attempt = %d, want the task's current attempt %d", body.Attempt, current.Attempt)
}
}