The agent-side half of the same gap. A stage failure already logged "stage failed -- parking" before parking, but a run whose OUTCOME was parked (a review that could not run, a workflow that ended with no outcome) reached the timeline as a KindParked event and the store as park_reason and never the log an operator downloads. park is the one place a park's reason is final, so it is now the one place that logs it; the stage-failure branch no longer logs its own near-copy, and the stage names itself inside the reason. The entry carries the stage as a field so nothing is lost by dropping the duplicate line. observability.md's "Implemented design" section documented the container's system-log path as though it were the only source of task-log content; it now records the daemon's and the workflow's own park writers, including why the entry follows the transition rather than preceding it.
154 lines
5.0 KiB
Go
154 lines
5.0 KiB
Go
package workflow_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"log/slog"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"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/store"
|
|
)
|
|
|
|
// storeIntegrationRunner is a minimal agentexec.Runner stub, duplicated
|
|
// here (rather than reused from the in-package tests) because this file
|
|
// lives in the external workflow_test package: it needs the real
|
|
// *store.Store as a workflow.Store, and the package under test now owns
|
|
// workflow.Task/Status/Source, which would otherwise cycle back through
|
|
// internal/store's dependency on internal/domain/workflow.
|
|
type storeIntegrationRunner struct {
|
|
result agentexec.Result
|
|
}
|
|
|
|
func (r *storeIntegrationRunner) Run(
|
|
_ context.Context, _ string, req agentexec.Request, _ agentexec.ToolCallReporter,
|
|
) (agentexec.Result, error) {
|
|
res := r.result
|
|
if res.Version == 0 {
|
|
res.Version = agentexec.ProtocolVersion
|
|
}
|
|
res.TaskID = req.TaskID
|
|
res.Attempt = req.Attempt
|
|
res.Stage = req.Stage
|
|
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 {
|
|
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", 1, "title", "body", "", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task, err := st.ClaimNext(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runner := &storeIntegrationRunner{result: agentexec.Result{
|
|
Version: agentexec.ProtocolVersion, Status: agentexec.StatusPassed,
|
|
AppendedNotes: []string{"checked with go test"},
|
|
}}
|
|
stage := workflow.AgentStage{
|
|
Name: "build", Role: "builder", Mission: func(*workflow.TaskContext) string { return "build" },
|
|
}.Stage()
|
|
tc := &workflow.TaskContext{
|
|
Task: task, Store: st, Agent: runner, Log: slog.New(slog.DiscardHandler),
|
|
Cfg: config.Config{Models: map[string]string{"builder": "provider/model"}},
|
|
}
|
|
if err := stage.Run(ctx, tc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if task.Notes != "- checked with go test\n" {
|
|
t.Fatalf("task notes = %q", task.Notes)
|
|
}
|
|
}
|
|
|
|
func TestRunLeavesInterruptedTaskForCrashRecovery(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)
|
|
}
|
|
})
|
|
if _, err := st.EnqueueIssue(context.Background(), "owner", "repo", 2, "title", "body", "", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task, err := st.ClaimNext(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
workflow.Run(ctx, workflow.Workflow{Name: "test", Stages: []workflow.Stage{{
|
|
Name: "agent", Run: func(ctx context.Context, _ *workflow.TaskContext) error { return ctx.Err() },
|
|
}}}, &workflow.TaskContext{
|
|
Task: task, Store: st, Repo: config.Repo{Owner: "owner", Name: "repo"},
|
|
Log: slog.New(slog.DiscardHandler),
|
|
})
|
|
got, err := st.TaskByIssue(context.Background(), "owner", "repo", 2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Status != workflow.StatusRunning || got.ParkReason != "" {
|
|
t.Fatalf("interrupted task status=%q park_reason=%q, want running with no park reason", got.Status, got.ParkReason)
|
|
}
|
|
}
|