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
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/skillbuild"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/wfeval"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/installtype"
|
||||
@@ -111,6 +112,16 @@ func (h *hybridTrees) ChangedLines(ctx context.Context, dir, base string) (int,
|
||||
return h.local.ChangedLines(ctx, dir, base)
|
||||
}
|
||||
|
||||
// ChangedFileStats is the capability the workflow captures a change through.
|
||||
// It is not part of workflow.Trees: it is asserted for as the unexported
|
||||
// optional interface package workflow declares, so a Trees implementation
|
||||
// without it degrades to no capture. Without this forwarder the in-container
|
||||
// path -- which is every production run, archie-agent executing the whole
|
||||
// workflow -- would be exactly that silent no-op.
|
||||
func (h *hybridTrees) ChangedFileStats(ctx context.Context, dir, base string) (task.ChangeStats, error) {
|
||||
return h.local.ChangedFileStats(ctx, dir, base)
|
||||
}
|
||||
|
||||
var _ workflow.Trees = (*hybridTrees)(nil)
|
||||
|
||||
// chownTree recursively chowns dir to uid:gid so the daemon -- running as
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/agentexec"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
@@ -421,6 +422,74 @@ func TestRunTaskExecutesBootstrapWorkflowEndToEnd(t *testing.T) {
|
||||
t.Fatalf("commit identity = author %q <%s>, committer %q <%s>",
|
||||
commit.Author.Name, commit.Author.Email, commit.Committer.Name, commit.Committer.Email)
|
||||
}
|
||||
|
||||
// The change the run produced is captured at the push, through the
|
||||
// container's hybridTrees, and persisted as durable provenance attributed
|
||||
// to the attempt -- and captured once more by OpenPR, the first moment a
|
||||
// capture can carry the pull-request number the view links. This is the
|
||||
// only end-to-end path that matters: every production workflow.Run executes
|
||||
// here.
|
||||
timeline, err := st.TaskEvents(ctx, task.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("TaskEvents: %v", err)
|
||||
}
|
||||
var captured []events.Event
|
||||
for _, e := range timeline {
|
||||
if e.Kind == events.KindChangesCaptured {
|
||||
captured = append(captured, e)
|
||||
}
|
||||
}
|
||||
if len(captured) != 2 {
|
||||
t.Fatalf("changes_captured events = %d, want the push capture and the one OpenPR takes (%d)", len(captured), stored.PRNumber)
|
||||
}
|
||||
for i, e := range captured {
|
||||
if e.Attempt != task.Attempt || task.Attempt == 0 {
|
||||
t.Errorf("capture %d attempt = %d, want the task's own attempt %d", i, e.Attempt, task.Attempt)
|
||||
}
|
||||
}
|
||||
if captured[0].Stage != "commit-push" {
|
||||
t.Errorf("capture stage = %q, want the capturing stage", captured[0].Stage)
|
||||
}
|
||||
data, err := json.Marshal(captured[0].Data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var payload struct {
|
||||
CapturedAfter string `json:"captured_after"`
|
||||
PRNumber int `json:"pr_number"`
|
||||
Files []struct {
|
||||
Path string `json:"path"`
|
||||
Status string `json:"status"`
|
||||
} `json:"files"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
t.Fatalf("decode capture: %v", err)
|
||||
}
|
||||
if payload.CapturedAfter != "commit-push" {
|
||||
t.Errorf("captured_after = %q, want commit-push", payload.CapturedAfter)
|
||||
}
|
||||
if payload.PRNumber != 0 {
|
||||
t.Errorf("the push capture carries pr_number %d, want 0: no PR existed when it was taken", payload.PRNumber)
|
||||
}
|
||||
if len(payload.Files) != 1 || payload.Files[0].Path != ".archie/bootstrap.md" || payload.Files[0].Status != "added" {
|
||||
t.Errorf("captured files = %+v, want the bootstrap marker as an added file", payload.Files)
|
||||
}
|
||||
|
||||
finalData, err := json.Marshal(captured[1].Data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var final struct {
|
||||
CapturedAfter string `json:"captured_after"`
|
||||
PRNumber int `json:"pr_number"`
|
||||
}
|
||||
if err := json.Unmarshal(finalData, &final); err != nil {
|
||||
t.Fatalf("decode the final capture: %v", err)
|
||||
}
|
||||
if final.CapturedAfter != "open-pr" || final.PRNumber != stored.PRNumber || final.PRNumber == 0 {
|
||||
t.Errorf("final capture = %q pr %d, want open-pr carrying the stored PR number %d, without which the changed-files view has nothing to link",
|
||||
final.CapturedAfter, final.PRNumber, stored.PRNumber)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunTaskRestoresWorktreeOwnershipOnEveryExit pins the half of the
|
||||
@@ -768,3 +837,64 @@ func TestRouteTaskUsesRequestLabelOverride(t *testing.T) {
|
||||
t.Fatalf("routeTask() = %q, want %q (request label override not applied)", wf.Name, "security-review")
|
||||
}
|
||||
}
|
||||
|
||||
// changeStatsReader mirrors the unexported optional interface package workflow
|
||||
// type-asserts on TaskContext.Trees to capture what an attempt changed. It is
|
||||
// restated here because an unexported interface cannot be named from this
|
||||
// package; the assertion below is therefore the only way this side can pin
|
||||
// that the in-container Trees is capable at all.
|
||||
type changeStatsReader interface {
|
||||
ChangedFileStats(ctx context.Context, dir, base string) (task.ChangeStats, error)
|
||||
}
|
||||
|
||||
// TestHybridTreesCanReportWhatChanged is the in-container half of the change
|
||||
// capture. Every production workflow.Run executes in archie-agent against a
|
||||
// hybridTrees, so a Trees without this capability would make the capture a
|
||||
// silent no-op in exactly the deployment that produces all the real data --
|
||||
// the daemon-side manager being capable would prove nothing.
|
||||
func TestHybridTreesCanReportWhatChanged(t *testing.T) {
|
||||
var trees any = &hybridTrees{local: &worktree.Manager{}}
|
||||
if _, ok := trees.(changeStatsReader); !ok {
|
||||
t.Fatal("hybridTrees cannot report a diffstat; every capture inside a container would silently no-op")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHybridTreesForwardsChangedFileStatsToItsLocalManager drives the
|
||||
// capability against a real worktree, so satisfaction of the interface is not
|
||||
// mistaken for a working forwarder.
|
||||
func TestHybridTreesForwardsChangedFileStatsToItsLocalManager(t *testing.T) {
|
||||
if _, err := exec.LookPath("git"); err != nil {
|
||||
t.Skip("git not installed")
|
||||
}
|
||||
ctx := t.Context()
|
||||
host := newLocalRemote(t, "acme", "diffstat")
|
||||
manager := &worktree.Manager{
|
||||
WorkDir: t.TempDir(), Token: "unused",
|
||||
BotUser: "archie-bot", BotEmail: "archie-bot@example.com", BaseURL: "file://" + host,
|
||||
}
|
||||
dir, branch, err := manager.Prepare(ctx, "acme", "diffstat", "main", 4, "feat: widget", "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("prepare: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "widget.txt"), []byte("one\ntwo\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := manager.CommitAll(ctx, dir, "feat: widget"); err != nil {
|
||||
t.Fatalf("commit: %v", err)
|
||||
}
|
||||
|
||||
trees := &hybridTrees{local: manager, localDir: dir, branch: branch}
|
||||
stats, err := trees.ChangedFileStats(ctx, dir, "main")
|
||||
if err != nil {
|
||||
t.Fatalf("ChangedFileStats: %v", err)
|
||||
}
|
||||
if len(stats.Files) != 1 || stats.Files[0].Path != "widget.txt" {
|
||||
t.Fatalf("files = %+v, want the one committed file", stats.Files)
|
||||
}
|
||||
if stats.Totals != (task.ChangeTotals{Files: 1, Additions: 2}) {
|
||||
t.Errorf("totals = %+v, want the file's two added lines", stats.Totals)
|
||||
}
|
||||
if stats.HeadSHA == "" || stats.BaseSHA == "" {
|
||||
t.Errorf("SHAs = (%q, %q), want both resolved", stats.BaseSHA, stats.HeadSHA)
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,16 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/health"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
@@ -100,13 +103,16 @@ func TestUIServesDashboardAgainstRemoteContracts(t *testing.T) {
|
||||
t.Fatalf("open temp store: %v", err)
|
||||
}
|
||||
defer st.Close()
|
||||
if _, err := st.EnqueueChatTask(t.Context(), "acme", "widget", "remote summary", "body", "implement", ""); err != nil {
|
||||
seeded, err := st.EnqueueChatTask(t.Context(), "acme", "widget", "remote summary", "body", "implement", "")
|
||||
if err != nil {
|
||||
t.Fatalf("seed task: %v", err)
|
||||
}
|
||||
// The daemon publishes the configuration page's projection; this process
|
||||
// only renders it (archie-core-ymut).
|
||||
// only renders it (archie-core-ymut). The forge host is published too: it
|
||||
// is what the run-detail reads resolve their repository and pull-request
|
||||
// links from, the same projection the task list uses.
|
||||
published, err := json.Marshal(webui.ConfigView{
|
||||
Identity: webui.IdentityView{BotUser: "archie", ForgeType: "github"},
|
||||
Identity: webui.IdentityView{BotUser: "archie", ForgeType: "github", ForgeHost: "https://forge.example"},
|
||||
Editable: true,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -118,6 +124,32 @@ func TestUIServesDashboardAgainstRemoteContracts(t *testing.T) {
|
||||
t.Fatalf("publish config snapshot: %v", err)
|
||||
}
|
||||
|
||||
// The other half of the configuration page is per-attempt provenance, and
|
||||
// the three run-detail reads are backed by the same State Store contract as
|
||||
// the rest of the task surface. The statements below are what other
|
||||
// processes write in production -- the daemon's dispatch capture, the
|
||||
// worker's change capture -- attributed to one attempt, so the assertions
|
||||
// further down read them back through the real client rather than a fake.
|
||||
seedAt := time.Now().UTC()
|
||||
for _, e := range []events.Event{
|
||||
{Kind: events.KindStageStart, TaskID: seeded.ID, Attempt: 1, Stage: "prepare", At: seedAt},
|
||||
{Kind: events.KindStageFinish, TaskID: seeded.ID, Attempt: 1, Stage: "prepare", At: seedAt.Add(time.Second), Data: map[string]any{"duration_ms": 1000}},
|
||||
{Kind: events.KindConfigCaptured, TaskID: seeded.ID, Attempt: 1, At: seedAt, Data: map[string]any{
|
||||
"schema": events.ConfigCapturedSchema, "document": map[string]any{"bot_user": "archie"},
|
||||
}},
|
||||
{Kind: events.KindChangesCaptured, TaskID: seeded.ID, Attempt: 1, Stage: "open-pr", At: seedAt, Data: map[string]any{
|
||||
"schema": events.ChangesCapturedSchema, "owner": "acme", "repo": "widget",
|
||||
"base": "main", "branch": "feat/1-widget", "head_sha": "head-sha", "base_sha": "base-sha",
|
||||
"pr_number": 7, "captured_after": "open-pr", "truncated": false,
|
||||
"files": []any{map[string]any{"path": "internal/x.go", "old_path": "", "status": "modified", "additions": 12, "deletions": 3, "binary": false}},
|
||||
"totals": map[string]any{"files": 1, "additions": 12, "deletions": 3},
|
||||
}},
|
||||
} {
|
||||
if _, err := st.InsertEvent(t.Context(), e); err != nil {
|
||||
t.Fatalf("seed %s event: %v", e.Kind, err)
|
||||
}
|
||||
}
|
||||
|
||||
stateTarget, stopState := serveGRPC(t, func(r grpc.ServiceRegistrar) {
|
||||
staterpc.RegisterServer(r, staterpc.Deps{Tasks: st, Captures: st, BindingDispatcher: st, ConfigSnapshots: st, Log: slog.New(slog.DiscardHandler)})
|
||||
})
|
||||
@@ -240,6 +272,96 @@ func TestUIServesDashboardAgainstRemoteContracts(t *testing.T) {
|
||||
t.Errorf("PATCH /api/config = %d, want 503: the daemon stays the configuration writer (PRD lines 95-96)", resp.StatusCode)
|
||||
}
|
||||
|
||||
// The run-detail reads. These three routes have no fake behind them: the
|
||||
// rail, the capture and the raw event list all come back over the State
|
||||
// Store listener the UI dialled, so each assertion below is the real
|
||||
// client's answer about the attempt-attributed set seeded above.
|
||||
taskPath := "/api/tasks/" + strconv.FormatInt(seeded.ID, 10)
|
||||
code, body = get(taskPath + "/attempts")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("GET /attempts = %d (%s), want 200", code, body)
|
||||
}
|
||||
var rail struct {
|
||||
Attempts []struct {
|
||||
Attempt int `json:"attempt"`
|
||||
Status string `json:"status"`
|
||||
Stages []struct {
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
} `json:"stages"`
|
||||
} `json:"attempts"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &rail); err != nil {
|
||||
t.Fatalf("decode attempts: %v (%s)", err, body)
|
||||
}
|
||||
if len(rail.Attempts) != 1 || rail.Attempts[0].Attempt != 1 || rail.Attempts[0].Status != "ok" {
|
||||
t.Fatalf("attempts = %s, want exactly the seeded attempt 1 recorded ok", body)
|
||||
}
|
||||
if len(rail.Attempts[0].Stages) != 1 || rail.Attempts[0].Stages[0].Name != "prepare" || rail.Attempts[0].Stages[0].Status != "ok" {
|
||||
t.Fatalf("stage rail = %s, want the attempt's own prepare stage", body)
|
||||
}
|
||||
|
||||
code, body = get(taskPath + "/changes?attempt=1")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("GET /changes = %d (%s), want 200", code, body)
|
||||
}
|
||||
var changes struct {
|
||||
Found bool `json:"found"`
|
||||
Captures []struct {
|
||||
PRNumber int `json:"pr_number"`
|
||||
PRURL string `json:"pr_url"`
|
||||
Files []struct {
|
||||
Path string `json:"path"`
|
||||
} `json:"files"`
|
||||
} `json:"captures"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &changes); err != nil {
|
||||
t.Fatalf("decode changes: %v (%s)", err, body)
|
||||
}
|
||||
if !changes.Found || len(changes.Captures) != 1 {
|
||||
t.Fatalf("changes = %s, want the one capture recorded for the attempt", body)
|
||||
}
|
||||
if changes.Captures[0].PRNumber != 7 || changes.Captures[0].PRURL != "https://forge.example/acme/widget/pull/7" {
|
||||
t.Errorf("capture = pr %d %q, want #7 linked through the published forge projection (%s)",
|
||||
changes.Captures[0].PRNumber, changes.Captures[0].PRURL, body)
|
||||
}
|
||||
if len(changes.Captures[0].Files) != 1 || changes.Captures[0].Files[0].Path != "internal/x.go" {
|
||||
t.Errorf("capture files = %s, want the file the attempt changed", body)
|
||||
}
|
||||
|
||||
code, body = get(taskPath + "/debug?attempt=1")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("GET /debug = %d (%s), want 200", code, body)
|
||||
}
|
||||
var debug struct {
|
||||
Attempt int `json:"attempt"`
|
||||
Task struct {
|
||||
ID int64 `json:"id"`
|
||||
} `json:"task"`
|
||||
Events []struct {
|
||||
Kind string `json:"kind"`
|
||||
Attempt int `json:"attempt"`
|
||||
} `json:"events"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &debug); err != nil {
|
||||
t.Fatalf("decode debug: %v (%s)", err, body)
|
||||
}
|
||||
if debug.Task.ID != seeded.ID || debug.Attempt != 1 {
|
||||
t.Errorf("debug = task %d attempt %d, want the seeded task and attempt 1 (%s)", debug.Task.ID, debug.Attempt, body)
|
||||
}
|
||||
kinds := map[string]int{}
|
||||
for _, e := range debug.Events {
|
||||
kinds[e.Kind]++
|
||||
if e.Attempt != 1 {
|
||||
t.Errorf("debug event %s carries attempt %d, want 1: attribution must survive the store it was read from", e.Kind, e.Attempt)
|
||||
}
|
||||
}
|
||||
for _, kind := range []string{events.KindStageStart, events.KindStageFinish, events.KindConfigCaptured, events.KindChangesCaptured} {
|
||||
if kinds[kind] != 1 {
|
||||
t.Errorf("debug events = %s, want exactly one %s", body, kind)
|
||||
}
|
||||
}
|
||||
|
||||
// Routes with no contract behind them degrade rather than panic.
|
||||
code, body = get("/api/logs")
|
||||
if code != http.StatusOK {
|
||||
|
||||
Reference in new issue
Block a user