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:
sam committed 2026-09-18 18:32:59 +10:00
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)
}
}