58c6b9b7c6
Every OpenCode-harness model stage now writes its real session into its own step artifacts directory as session.md and session.html, so the existing bounded artifact listing, the /artifacts/<run>/<step>/<name> serving path and the artifact scan find and serve them with no special casing, while the stage keeps its bounded preview in the run record unchanged. - internal/opencode/export.go adds the one shared native export invocation and the exact session-ID/messages validation used by both Connection.Messages and the new ExportSession, so the readback path does not diverge. The backend's published subcommand is used (v1 `export <id>`, v2 `session export <id>`), the source read is capped at 8 MiB, and a larger session is read as a prefix and reported truncated instead of failing. boundedOutput.ReadFrom keeps the cap effective so overflow and the truncation path are real. - internal/stage/session_export.go renders markdown and html with a metadata header, every role and every part (unknown part types as raw JSON, every session text escaped in html), atomic temp+rename writes that never overwrite an existing file, a visible truncation banner at both ends when the source or the 2 MiB rendered cap cuts it, and an honest 0-message header for an empty session rather than fabricated text. - protocol.SessionExport and RunStep.SessionExport are stored in a new step_session_exports table (CREATE TABLE IF NOT EXISTS, so an old database gains an empty table with no migration) loaded through the existing main SELECT alongside the other per-step evidence and deleted by run retention. - internal/server/runner.go exports after the post-stage filediff capture and before scanArtifacts, with context.WithTimeout(context.WithoutCancel(ctx), 10s); it never waits for session idleness and a failure or a Pi stage (no native export path) records a sanitized diagnostic and continues, never changing the stage status, outcome, target, timing or result validation. Reflected where it belongs: the packaged skill references (internal/author/skill/references/operations.md and delivery.md) carry the new Session export rules and the run-artifacts bullet, and docs/stage-artifacts.md documents the names, the <run>/<step> paths, the served URL, the caps, the truncation labels, failure isolation and the Pi limitation. The packaged templates (examples/*.md) only tell a stage to save its own artifacts and say nothing about the server-written session export, so none needed updating; no new reference file was added, so install.go's managed inventory is unchanged. Shared documentation: README.md's per-step artifacts sentence is now stale - it should name the exported session alongside images, screenshots, reports and logs. It is deliberately not committed here; the shared documents are folded by the single documentation writer. Checks: gofmt -l on the touched packages produced no output; go vet ./... passed; go test -race -count=1 ./... passed.
169 lines
6.3 KiB
Go
169 lines
6.3 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.md/session.html are 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 := Handler(ctx, store).(*runHandler)
|
|
handler.runner.artifactsRoot = filepath.Join(root, "artifacts")
|
|
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.MarkdownBytes == 0 || 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)
|
|
}
|
|
srv := httptest.NewServer(handler)
|
|
defer srv.Close()
|
|
for _, name := range []string{"session.md", "session.html"} {
|
|
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)
|
|
}
|
|
for _, name := range []string{stage.SessionMarkdownFile, stage.SessionHTMLFile} {
|
|
info, err := os.Stat(filepath.Join(dir, name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, _ := os.ReadFile(filepath.Join(dir, name))
|
|
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", name, filepath.Join(dir, name), info.Size(), first)
|
|
}
|
|
if result.MarkdownBytes == 0 || result.HTMLBytes == 0 {
|
|
t.Fatalf("recorded export wrote no files: %+v", result)
|
|
}
|
|
}
|