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

+64 -7
View File
@@ -981,6 +981,14 @@ func (d *Daemon) reconcilePRs(ctx context.Context) {
d.Log.Warn("PR state check failed", "pr", t.PRNumber, "err", err)
continue
}
// OpenPRs returns a deliberately narrow projection of a pr_open task
// (id/owner/repo/issue/pr/status/source/identity) that does not carry
// the attempt, so the outcome event below reads it from the task's own
// row. A failed lookup costs attribution and never the merge handling:
// the status transition and the worktree cleanup still run. OpenPRs
// carries attempt, so these events are attributed without a second
// read per open PR on every reconcile tick.
attempt := t.Attempt
switch state {
case "merged":
_ = d.Store.Transition(ctx, t.ID, workflow.StatusPROpen, workflow.StatusMerged, "")
@@ -997,7 +1005,7 @@ func (d *Daemon) reconcilePRs(ctx context.Context) {
))
d.Log.Info("PR merged", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber)
d.emit(events.Event{
Kind: events.KindPRMerged, TaskID: t.ID,
Kind: events.KindPRMerged, TaskID: t.ID, Attempt: attempt,
Repo: t.Owner + "/" + t.Repo, Issue: t.IssueNumber,
Data: map[string]any{"pr": t.PRNumber},
})
@@ -1006,7 +1014,7 @@ func (d *Daemon) reconcilePRs(ctx context.Context) {
_ = trees.Cleanup(t.Owner, t.Repo, t.IssueNumber)
d.Log.Info("PR rejected", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber)
d.emit(events.Event{
Kind: events.KindPRRejected, TaskID: t.ID,
Kind: events.KindPRRejected, TaskID: t.ID, Attempt: attempt,
Repo: t.Owner + "/" + t.Repo, Issue: t.IssueNumber,
Data: map[string]any{"pr": t.PRNumber},
})
@@ -1040,7 +1048,7 @@ func (d *Daemon) process(ctx context.Context, task *workflow.Task) {
}
d.recordPark(ctx, task.ID, reason)
d.emit(events.Event{
Kind: events.KindParked, TaskID: task.ID,
Kind: events.KindParked, TaskID: task.ID, Attempt: task.Attempt,
Repo: repo.FullName(), Issue: task.IssueNumber, Detail: reason,
})
return
@@ -1054,7 +1062,7 @@ func (d *Daemon) process(ctx context.Context, task *workflow.Task) {
}
d.recordPark(ctx, task.ID, reason)
d.emit(events.Event{
Kind: events.KindParked, TaskID: task.ID,
Kind: events.KindParked, TaskID: task.ID, Attempt: task.Attempt,
Repo: repo.FullName(), Issue: task.IssueNumber, Detail: reason,
})
return
@@ -1318,12 +1326,15 @@ func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo conf
return
}
defer revoke()
cfg := d.configFor(task)
taskCfg := cfg.ForTask()
d.captureAttemptConfig(ctx, task, taskCfg)
req := taskrun.Request{
Task: task,
Repo: repo,
Cfg: d.configFor(task).ForTask(),
Providers: agentexec.ProvidersFromConfig(d.configFor(task).Providers),
MCPServers: d.configFor(task).Tools.MCPServers,
Cfg: taskCfg,
Providers: agentexec.ProvidersFromConfig(cfg.Providers),
MCPServers: cfg.Tools.MCPServers,
WorktreeGrant: grant,
KindWorkflows: d.KindWorkflows,
LabelWorkflows: d.LabelWorkflows,
@@ -1486,6 +1497,52 @@ func (d *Daemon) configFor(task *workflow.Task) config.Config {
return d.Cfg.Get()
}
// captureAttemptConfig persists the effective task configuration this attempt
// runs under, as a durable event on the attempt's own key.
//
// It is written where the configuration is materialised for the dispatch, so
// the document is exactly what the run received. The published configuration
// snapshot cannot answer this: it is the CURRENT configuration, replaced on
// every publish, so a run that finished last week has no record anywhere else.
//
// Fail-open by construction: a configuration that could not be recorded is
// worth a log line and nothing more. Parking a task over a reporting failure
// would trade the run for the receipt.
func (d *Daemon) captureAttemptConfig(ctx context.Context, task *workflow.Task, cfg config.TaskConfig) {
if d.Store == nil {
return
}
document, err := json.Marshal(cfg)
if err != nil {
d.Log.Warn("attempt configuration not captured", "task", task.ID, "attempt", task.Attempt, "err", err)
return
}
event := events.Event{
At: time.Now().UTC(),
Kind: events.KindConfigCaptured,
TaskID: task.ID,
Repo: task.Owner + "/" + task.Repo,
Issue: task.IssueNumber,
Workflow: task.Workflow,
Attempt: task.Attempt,
Data: map[string]any{
"schema": events.ConfigCapturedSchema,
// Raw so the document is embedded once, as it was encoded, rather
// than as a string a reader would have to decode again.
"document": json.RawMessage(document),
},
}
id, err := d.Store.InsertEvent(ctx, event)
if err != nil {
d.Log.Warn("attempt configuration not captured", "task", task.ID, "attempt", task.Attempt, "err", err)
return
}
// The assigned ID tells the event sink to broadcast without inserting a
// duplicate row.
event.ID = id
d.emit(event)
}
func configForIdentity(root config.Config, identity config.IdentityConfig) config.Config {
root.BotUser = identity.BotUser
root.BotEmail = identity.BotEmail