Files
pipelines/internal/server/session_export_live_test.go
T
alan 3bfb3d6b44 Fix decide-after-finish refusal and run size loop data race
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.
2026-11-01 18:58:14 +00:00

175 lines
6.7 KiB
Go

package server
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
// sessionExportLiveSource is a one-stage pipeline that declares the read tool so
// the stage runs as an agent (the V2 path that persists session messages) while
// the prompt tells it not to use any tool.
func sessionExportLiveSource(provider, model string) string {
return "# stage one\n\n```output\n{ summary }\n```\n\n" +
"```prompt\nReturn success with summary exactly: session export live check. Do not use any tool.\n```\n\n" +
"```tools\nread\n```\n\n" +
"```configs\nprovider=" + provider + "\nmodel=" + model + "\nretries=0\ntimeout=2m\n```\n\n" +
"```edges\nsuccess -> #success\nerror -> #failure\n```\n\n"
}
// TestSessionExportLive is an opt-in live check: it runs one real OpenCode stage
// on a temporary store and artifacts root using the configured host harness,
// then proves the real session.html is written, listed and served 200. It is
// gated so the default suite never spends inference.
func TestSessionExportLive(t *testing.T) {
if os.Getenv("PIPELINES_SESSION_EXPORT_LIVE") != "1" {
t.Skip("set PIPELINES_SESSION_EXPORT_LIVE=1 and PIPELINES_SESSION_EXPORT_MODEL to run chargeable session-export inference")
}
model := os.Getenv("PIPELINES_SESSION_EXPORT_MODEL")
provider, id, ok := strings.Cut(model, "/")
if !ok || !stage.ValidModel(provider, id) {
t.Fatal("explicit valid PIPELINES_SESSION_EXPORT_MODEL required")
}
// Isolate notifications from the operator's real ntfy service.
endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"id":"session-export-live-receipt","event":"message"}`)
}))
defer endpoint.Close()
t.Setenv("PIPELINES_NTFY_URL", endpoint.URL+"/live")
t.Setenv("PIPELINES_NTFY_TOKEN", "")
t.Setenv("PIPELINES_NTFY_TOKEN_FILE", "")
ctx, cancel := context.WithTimeout(context.Background(), 6*time.Minute)
defer cancel()
root := t.TempDir()
store, err := pipeline.Open(filepath.Join(root, "runs.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err := store.Submit(ctx, "session-export-live", sessionExportLiveSource(provider, id)); err != nil {
t.Fatal(err)
}
items, err := store.List(ctx)
if err != nil {
t.Fatal(err)
}
handler := HandlerWithOptions(ctx, Options{ArtifactsRoot: filepath.Join(root, "artifacts")}, store).(*runHandler)
defer func() { cancel(); WaitRuns(handler) }()
workspace := filepath.Join(root, "ws")
if err := os.Mkdir(workspace, 0700); err != nil {
t.Fatal(err)
}
run, err := handler.runner.start(protocol.Message{
PipelineID: items[0].ID, Version: 1, RequestKey: "session-export-live",
Workspace: workspace, Input: map[string]any{"prompt": "Return the fixed session export live check."},
})
if err != nil {
t.Fatal(err)
}
handler.runner.wg.Wait()
detail, err := store.GetRun(context.Background(), run.ID)
if err != nil {
t.Fatal(err)
}
if detail.Status != "succeeded" || len(detail.Steps) != 1 {
t.Fatalf("live run did not succeed: %+v", detail)
}
step := detail.Steps[0]
if step.SessionExport == nil || step.SessionExport.Source != "native_export" || step.SessionExport.HTML != "session.html" || step.SessionExport.HTMLBytes == 0 {
t.Fatalf("live session export evidence missing: %+v", step.SessionExport)
}
if step.SessionExport.Messages == 0 || step.SessionExport.Truncated {
t.Fatalf("live session export was empty or truncated: %+v", step.SessionExport)
}
if _, ok := artifactNamed(step.Artifacts, stage.SessionMarkdownFile); ok {
t.Fatalf("live run produced a legacy session.md: %+v", step.Artifacts)
}
srv := httptest.NewServer(handler)
defer srv.Close()
for _, name := range []string{stage.SessionHTMLFile} {
artifact, ok := artifactNamed(step.Artifacts, name)
if !ok {
t.Fatalf("%s not in live artifact listing: %+v", name, step.Artifacts)
}
resp, err := http.Get(srv.URL + artifact.URL)
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("%s served HTTP %d", name, resp.StatusCode)
}
path := filepath.Join(handler.runner.artifactsRoot, fmt.Sprint(run.ID), "1", name)
if evidenceDir := os.Getenv("PIPELINES_SESSION_EXPORT_LIVE_DIR"); evidenceDir != "" {
if err := os.MkdirAll(evidenceDir, 0700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(evidenceDir, name), body, 0600); err != nil {
t.Fatal(err)
}
}
first := string(body)
if idx := strings.IndexByte(first, '\n'); idx >= 0 {
first = first[:idx]
}
t.Logf("live export %s: path=%s size=%d messages=%d url=%s http=%d first_line=%q", name, path, len(body), step.SessionExport.Messages, artifact.URL, resp.StatusCode, first)
}
}
// TestSessionExportRecorded is the fallback evidence path: with no chargeable
// inference, it exports one real recorded session read-only through the same
// code path, clearly labelled as not produced by a fresh runner stage.
func TestSessionExportRecorded(t *testing.T) {
session := os.Getenv("PIPELINES_SESSION_EXPORT_RECORDED_ID")
if session == "" {
t.Skip("set PIPELINES_SESSION_EXPORT_RECORDED_ID and _DIR to export a recorded session read-only (no inference)")
}
dir := os.Getenv("PIPELINES_SESSION_EXPORT_RECORDED_DIR")
if dir == "" {
t.Fatal("PIPELINES_SESSION_EXPORT_RECORDED_DIR required")
}
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
result, err := stage.ExportSession(context.Background(), stage.SessionExportRequest{
SessionID: session, Backend: "v2", Directory: dir, ArtifactsDir: dir,
RunID: 0, Sequence: 0, Stage: "recorded",
})
if err != nil {
t.Fatalf("recorded session export failed: %v", err)
}
if result.HTML != stage.SessionHTMLFile || result.HTMLBytes == 0 {
t.Fatalf("recorded export wrote no html: %+v", result)
}
htmlPath := filepath.Join(dir, stage.SessionHTMLFile)
info, err := os.Stat(htmlPath)
if err != nil {
t.Fatal(err)
}
data, _ := os.ReadFile(htmlPath)
first := string(data)
if idx := strings.IndexByte(first, '\n'); idx >= 0 {
first = first[:idx]
}
t.Logf("recorded export (NOT a fresh runner stage) %s: path=%s size=%d first_line=%q", stage.SessionHTMLFile, htmlPath, info.Size(), first)
if _, err := os.Stat(filepath.Join(dir, stage.SessionMarkdownFile)); err == nil {
// A pre-existing session.md from an older run may sit here; the export
// must not have produced or rewritten it.
t.Logf("pre-existing %s left in place; the export writes no markdown", stage.SessionMarkdownFile)
}
}