3bfb3d6b44
A run stays in r.active after finishRunState persists its terminal status until its goroutine finishes notifying, so a decision in that window was refused as "already active". decide now refuses only runs whose terminal state is not yet durable; a finished run gets ResolveDecision's already-decided / not-waiting answer, and an executing run is still refused. Tests overwrote runner.artifactsRoot after Handler/newRunner had started background goroutines (sizeLoop, session export jobs) that read it. The root is now fixed at construction through newRunnerAt, which HandlerWithOptions uses for Options.ArtifactsRoot, and tests configure it there instead of mutating the field afterwards.
190 lines
6.8 KiB
Go
190 lines
6.8 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/stage"
|
|
"git.a-wan.net/alan/pipelines/internal/summarise"
|
|
)
|
|
|
|
func TestRenderReportContract(t *testing.T) {
|
|
for _, format := range []string{"html", "markdown"} {
|
|
name, body, err := renderReport(format, map[string]any{"markdown": "# Report\n\n**Done**\n<script>alert(1)</script>"})
|
|
if err != nil || name == "" || len(body) == 0 {
|
|
t.Fatalf("%s: %s %v", format, body, err)
|
|
}
|
|
if format == "html" && (!strings.Contains(string(body), "<h1>Report</h1>") || strings.Contains(string(body), "<script>") || !strings.Contains(string(body), "Content-Security-Policy")) {
|
|
t.Fatalf("unsafe or malformed report: %s", body)
|
|
}
|
|
_, again, _ := renderReport(format, map[string]any{"markdown": "# Report\n\n**Done**\n<script>alert(1)</script>"})
|
|
if string(again) != string(body) {
|
|
t.Fatal("not deterministic")
|
|
}
|
|
}
|
|
for _, output := range []map[string]any{nil, {}, {"markdown": " \n"}, {"files": []string{"report.html"}}, {"markdown": strings.Repeat("x", reportLimit+1)}} {
|
|
if _, _, err := renderReport("html", output); err == nil {
|
|
t.Fatalf("accepted absent/oversized output: %.80v", output)
|
|
}
|
|
}
|
|
if _, _, err := renderReport("../../escape", map[string]any{"summary": "hello"}); err == nil {
|
|
t.Fatal("accepted invalid format")
|
|
}
|
|
}
|
|
|
|
func reportRunner(t *testing.T, format string) (*runner, int64) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
s, err := pipeline.Open(filepath.Join(t.TempDir(), "report.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { s.Close() })
|
|
if _, err := s.Submit(ctx, "report", pipeline.Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := s.List(ctx)
|
|
d, _ := s.Get(ctx, items[0].ID, 1)
|
|
id, _, err := s.CreateRunTitled(ctx, d, "report-key", "workspace", false, nil, map[string]any{"prompt": "task"}, pipeline.RunOverrides{OutputFormat: format}, summarise.Title{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.BeginStep(ctx, id, 1, "one", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.FinishStep(ctx, id, protocol.RunStep{Sequence: 1, Stage: "one", Status: "completed", Outcome: "success", Output: map[string]any{"markdown": "# Result"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &runner{ctx: ctx, store: s, artifactsRoot: t.TempDir(), notifyModes: map[int64]string{id: "off"}}, id
|
|
}
|
|
|
|
func TestDeliverableBeforeSuccess(t *testing.T) {
|
|
for _, format := range []string{"html", "markdown", ""} {
|
|
t.Run(format, func(t *testing.T) {
|
|
r, id := reportRunner(t, format)
|
|
r.finishRunState(id, "report", "workspace", "succeeded", "success", "", 1, 1, "one", stage.Values{"markdown": "# Result"})
|
|
d, err := r.store.GetRun(context.Background(), id)
|
|
if err != nil || d.Status != "succeeded" || d.OutputFormat != format {
|
|
t.Fatalf("%+v %v", d, err)
|
|
}
|
|
if format != "" {
|
|
if len(d.Steps[0].Artifacts) != 1 {
|
|
t.Fatalf("missing report inventory: %+v", d.Steps)
|
|
}
|
|
file := d.Steps[0].Artifacts[0]
|
|
if file.SizeBytes <= 0 || !strings.HasPrefix(file.Name, "public/report.") {
|
|
t.Fatalf("%+v", file)
|
|
}
|
|
body, err := os.ReadFile(filepath.Join(r.artifactsRoot, "1", file.Name))
|
|
if err != nil || len(body) == 0 {
|
|
t.Fatalf("missing requested report: %v", err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDeliverableMissingFailsRun(t *testing.T) {
|
|
r, id := reportRunner(t, "html")
|
|
r.finishRunState(id, "report", "workspace", "succeeded", "success", "", 1, 1, "one", nil)
|
|
d, _ := r.store.GetRun(context.Background(), id)
|
|
if d.Status != "failed" || !strings.Contains(d.Diagnostic, "deliverable contract") {
|
|
t.Fatalf("missing report succeeded: %+v", d)
|
|
}
|
|
}
|
|
|
|
func TestDeliverableRejectsExistingFilesAndSymlinks(t *testing.T) {
|
|
for _, kind := range []string{"file", "directory", "symlink", "public-symlink", "run-symlink"} {
|
|
t.Run(kind, func(t *testing.T) {
|
|
r, id := reportRunner(t, "html")
|
|
public := filepath.Join(r.artifactsRoot, "1", "public")
|
|
if err := os.MkdirAll(public, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
target := filepath.Join(public, "report.html")
|
|
outside := filepath.Join(t.TempDir(), "keep")
|
|
if err := os.WriteFile(outside, []byte("keep"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var err error
|
|
switch kind {
|
|
case "file":
|
|
err = os.WriteFile(target, []byte("keep"), 0600)
|
|
case "directory":
|
|
err = os.Mkdir(target, 0755)
|
|
case "symlink":
|
|
err = os.Symlink(outside, target)
|
|
case "public-symlink":
|
|
os.Remove(public)
|
|
err = os.Symlink(filepath.Dir(outside), public)
|
|
case "run-symlink":
|
|
os.Remove(public)
|
|
os.Remove(filepath.Dir(public))
|
|
err = os.Symlink(filepath.Dir(outside), filepath.Dir(public))
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.finalizeDeliverable(context.Background(), id, 1, map[string]any{"markdown": "new"}); err == nil {
|
|
t.Fatal("unsafe/existing target accepted")
|
|
}
|
|
body, _ := os.ReadFile(outside)
|
|
if string(body) != "keep" {
|
|
t.Fatal("outside evidence overwritten")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOutputFormatRetryInheritanceThroughRuntime(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
s, err := pipeline.Open(filepath.Join(t.TempDir(), "retry-format.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
if _, err := s.Submit(ctx, "report", pipeline.Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := s.List(ctx)
|
|
r, err := newRunnerAt(ctx, s, t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { cancel(); r.wg.Wait() }()
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
return stage.Result{}, fmt.Errorf("fixture failure")
|
|
}
|
|
original, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "original-format", Workspace: t.TempDir(), Input: map[string]any{"prompt": "task"}, OutputFormat: "html", NotifyMode: "off"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.wg.Wait()
|
|
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
|
|
return stage.Result{Outcome: "success", Output: stage.Values{"markdown": "# Recovered report"}}, nil
|
|
}
|
|
retry, err := r.retry(protocol.Message{RunID: original.ID, RequestKey: "retry-format", RetryMode: "restart"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.wg.Wait()
|
|
retry, err = s.GetRun(ctx, retry.ID)
|
|
if err != nil || retry.Status != "succeeded" || retry.OutputFormat != "html" {
|
|
t.Fatalf("contract not inherited: %+v %v", retry, err)
|
|
}
|
|
if _, ok := artifactNamed(retry.Steps[0].Artifacts, "public/report.html"); !ok {
|
|
t.Fatal("retry report missing")
|
|
}
|
|
replay, err := r.retry(protocol.Message{RunID: original.ID, RequestKey: "retry-format", RetryMode: "restart"})
|
|
if err != nil || replay.ID != retry.ID {
|
|
t.Fatalf("retry idempotency lost: %+v %v", replay, err)
|
|
}
|
|
}
|