diff --git a/docs/architecture/observability.md b/docs/architecture/observability.md index 3fd8bb9ac..0a816a5c9 100644 --- a/docs/architecture/observability.md +++ b/docs/architecture/observability.md @@ -95,6 +95,15 @@ appears on the dashboard exactly like daemon output. The approved target is for this composition to move to `internal/app/archied`; that package does not yet exist, so this document does not claim the migration is complete. +The daemon is also a writer in its own right for the decisions only it makes. +Every park records its reason into that attempt's sink (`daemon.recordPark`), +on the daemon side after the guarded transition lands and in +`workflow.park` on the agent side, because "why did this park?" is the question +the log exists to answer and the container path cannot answer a run that never +reached a container. The entry is written only once the park actually +happened, so a transition another actor won does not leave a log claiming an +outcome the task board contradicts. + ### Gate output: stop discarding it `StageBaselineGate` and the equivalent TDD-stage gate runs write full diff --git a/internal/domain/workflow/store_integration_test.go b/internal/domain/workflow/store_integration_test.go index 3928e3693..37575e83d 100644 --- a/internal/domain/workflow/store_integration_test.go +++ b/internal/domain/workflow/store_integration_test.go @@ -1,9 +1,11 @@ package workflow_test import ( + "bytes" "context" "log/slog" "path/filepath" + "strings" "testing" "github.com/samcharles93/archie-core/internal/agentexec" @@ -35,6 +37,50 @@ func (r *storeIntegrationRunner) Run( return res, nil } +// TestRunRecordsAParkReasonInTheRunsOwnLog pins the agent-side half of the +// same contract: a run's own log is what an operator downloads to answer "why +// did this park?", and a parked OUTCOME (a review that could not run, a stage +// that ended parked rather than failing) used to reach the timeline as an event +// and the store as park_reason, but never the log. A workflow that ends without +// an outcome is the smallest way to reach that path. +func TestRunRecordsAParkReasonInTheRunsOwnLog(t *testing.T) { + st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "archie.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { + if err := st.Close(); err != nil { + t.Errorf("close store: %v", err) + } + }) + ctx := context.Background() + if _, err := st.EnqueueIssue(ctx, "owner", "repo", 3, "title", "body", "", ""); err != nil { + t.Fatal(err) + } + task, err := st.ClaimNext(ctx) + if err != nil { + t.Fatal(err) + } + + var logs bytes.Buffer + workflow.Run(ctx, workflow.Workflow{Name: "empty"}, &workflow.TaskContext{ + Task: task, Store: st, Repo: config.Repo{Owner: "owner", Name: "repo"}, + Log: slog.New(slog.NewJSONHandler(&logs, nil)), + }) + + const reason = "workflow ended without an outcome (definition bug)" + if !strings.Contains(logs.String(), reason) { + t.Errorf("run log = %s, want the park reason %q recorded in it", logs.String(), reason) + } + got, err := st.TaskByIssue(ctx, "owner", "repo", 3) + if err != nil { + t.Fatal(err) + } + if got.Status != workflow.StatusParked || got.ParkReason != reason { + t.Fatalf("task status/park_reason = (%q, %q), want parked with the same reason the log carries", got.Status, got.ParkReason) + } +} + func TestAgentStagePersistsReturnedNotes(t *testing.T) { st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "archie.db")) if err != nil { diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index 92cc1e92f..3cff53eb8 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -316,7 +316,6 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) { return } t.ParkReason = fmt.Sprintf("stage %s: %v", stage.Name, err) - log.Warn("stage failed -- parking", "stage", stage.Name, "err", err) park(ctx, tc, t.ParkReason) return } @@ -348,6 +347,11 @@ func park(ctx context.Context, tc *TaskContext, reason string) { _ = tc.Store.Update(ctx, t) _ = tc.Store.Transition(ctx, t.ID, StatusRunning, StatusParked, reason) tc.Emit(events.KindParked, t.Stage, reason, nil) + // The run's own log is the surface an operator downloads to answer "why did + // this park?", and the event above lands on the timeline, which the log + // never carries. This is the one place a park's reason is final, so it is + // the one place that records it -- the stage names itself in the reason. + tc.Log.Error("task parked", "stage", t.Stage, "reason", reason) } func clip(s string, n int) string {