95362134aa
A stage's session was exported twice, as session.md and session.html, and both files were presented. It is now one html file; the markdown export is retired. - internal/stage/session_export.go: ExportSession renders and writes exactly one file, session.html, via writeSessionFile; renderSessionMarkdown and its helpers are gone; SessionExportResult carries HTML/HTMLBytes only. The source read, the 8 MiB source / 2 MiB rendered bounds, the truncation banner, the never-overwrite rule and the honest empty-session handling are unchanged. - internal/protocol/runs.go: SessionExport drops markdown/markdown_bytes (a record written before the change still decodes; unknown fields are ignored), and adds LegacySessionMarkdownArtifact with RunStep.PresentedArtifacts(), the one Go rule that hides the retired session.md from every artifact listing. - internal/server/runner.go: evidence.Markdown/MarkdownBytes are no longer copied. - internal/tui: the artifacts panel, the recorded count and the per-visit summary derive from PresentedArtifacts(), so the terminal never lists session.md. - web/src/app.js: the spike's renderArtifacts applies the same filter. - android: the Session section is the html export alone in a WebView — SessionView, the Markdown/HTML toggle and the session markdown path are removed; presentedArtifacts(step) filters the retired session.md out of the per-visit gallery, the run gallery and its count, the verdict/selection lists and the timeline indicator; data/protocol SessionExport drops the markdown fields. - Tests pin the single html file, prove no session.md is written and that a pre-existing one is neither overwritten nor presented, and prove a legacy export record still loads and its session.md is hidden. The legacy session.md of an older run stays on disk (dated evidence); it is only no longer produced and no longer presented. Checks (macOS, Go 1.24, Android Gradle 8.11.1, 18 Oct 2026): gofmt -l . prints nothing; go build ./... && go vet ./... clean; go test ./internal/... ./cmd/... -> 19 packages ok, 0 failures; node --check web/src/app.js ok and node --test web/test -> 20/20; gradle --offline --no-daemon :app:assembleDebug :app:testDebugUnitTest -> BUILD SUCCESSFUL. Packaged references checked: internal/author/skill/references/delivery.md and operations.md both described the session.md+session.html pair, so both are updated here to describe the single session.html export and the retired, hidden session.md. README.md and internal/author/skill/SKILL.md carry no session.md reference and install.go's managed inventory is unchanged (no reference added or removed). docs/notes/session-html-only.md records the remaining stale shared documentation (docs/stage-artifacts.md, docs/app-integration-verification.md) for the docs-fold writer; shared docs are not edited by this item.
544 lines
20 KiB
Go
544 lines
20 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/summarise"
|
|
)
|
|
|
|
func TestRunListIncludesHistoryBeyondFiftyForSearchAndPipelineScope(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "runs.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "history", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
definition, err := store.Get(ctx, items[0].ID, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 55; i++ {
|
|
if _, _, err := store.CreateRun(ctx, definition, fmt.Sprintf("history-%d", i), "workspace", false, nil, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
runs, err := store.ListRuns(ctx)
|
|
if err != nil || len(runs) != 55 || runs[len(runs)-1].ID != 1 {
|
|
t.Fatal("older searchable runs missing", len(runs), err)
|
|
}
|
|
}
|
|
|
|
// TestRunTitleStorageMigrationAndFallback proves the three title contracts: an
|
|
// explicit title is kept verbatim, an absent one is derived bounded and
|
|
// non-empty, and runs created before the title columns survive the migration and
|
|
// still list.
|
|
func TestRunTitleStorageMigrationAndFallback(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "titles.db")
|
|
store, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := store.Submit(ctx, "titled", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
definition, _ := store.Get(ctx, items[0].ID, 1)
|
|
|
|
explicit, _, err := store.CreateRunTitled(ctx, definition, "explicit", "workspace", false, nil,
|
|
map[string]any{"prompt": "a prompt that must not become the title"}, RunOverrides{},
|
|
summarise.Title{Text: "Fix the flaky login test", Source: summarise.SourceModel, Model: "openai/gpt"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := store.GetRun(ctx, explicit)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Title != "Fix the flaky login test" || got.TitleSource != summarise.SourceModel || got.TitleModel != "openai/gpt" {
|
|
t.Fatalf("explicit title not kept verbatim with its provenance: %+v", got)
|
|
}
|
|
|
|
derived, _, err := store.CreateRun(ctx, definition, "derived", "workspace", false, nil,
|
|
map[string]any{"prompt": strings.Repeat("é", 500) + "\nsecond line is ignored"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err = store.GetRun(ctx, derived)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Title == "" || got.TitleSource != summarise.SourceFallback || got.TitleModel != "" {
|
|
t.Fatalf("derived title = %+v, want a non-empty fallback with no model", got)
|
|
}
|
|
if len([]rune(got.Title)) > summarise.TitleCap {
|
|
t.Fatalf("derived title has %d runes, want <= %d", len([]rune(got.Title)), summarise.TitleCap)
|
|
}
|
|
|
|
// Simulate a database created before run titles by removing the columns, then
|
|
// reopen: existing rows must survive the re-add migration and still list.
|
|
for _, column := range []string{"title", "title_source", "title_model"} {
|
|
if _, err := store.db.ExecContext(ctx, "ALTER TABLE pipeline_runs DROP COLUMN "+column); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store, err = Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
runs, err := store.ListRuns(ctx)
|
|
if err != nil {
|
|
t.Fatal("legacy runs did not survive the title migration", err)
|
|
}
|
|
ids := map[int64]bool{}
|
|
for _, run := range runs {
|
|
ids[run.ID] = true
|
|
}
|
|
if !ids[explicit] || !ids[derived] {
|
|
t.Fatalf("migrated run list lost history: %+v", runs)
|
|
}
|
|
migrated, err := store.GetRun(ctx, explicit)
|
|
if err != nil || migrated.Title != "" || migrated.TitleSource != "" || migrated.TitleModel != "" {
|
|
t.Fatalf("migrated row should keep a valid empty title: %+v %v", migrated, err)
|
|
}
|
|
}
|
|
|
|
// TestSetRunTitleRenamesWithoutTouchingInput proves an after-the-fact rename
|
|
// replaces the stored title and its provenance and leaves the run's input,
|
|
// status and steps untouched; an unknown run is reported, not silently ignored.
|
|
func TestSetRunTitleRenamesWithoutTouchingInput(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "rename.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "rename", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
definition, _ := store.Get(ctx, items[0].ID, 1)
|
|
id, _, err := store.CreateRun(ctx, definition, "rename-key", "workspace", false, nil, map[string]any{"prompt": "original derived prompt"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, err := store.GetRun(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.SetRunTitle(ctx, id, summarise.Title{Text: "Investigate the regression", Source: summarise.SourceExplicit}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
after, err := store.GetRun(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if after.Title != "Investigate the regression" || after.TitleSource != summarise.SourceExplicit || after.TitleModel != "" {
|
|
t.Fatalf("rename = %+v, want the explicit title and provenance", after)
|
|
}
|
|
if after.Status != before.Status || !reflect.DeepEqual(after.Input, before.Input) || len(after.Steps) != len(before.Steps) {
|
|
t.Fatalf("rename changed run state: before %+v after %+v", before, after)
|
|
}
|
|
if err := store.SetRunTitle(ctx, id+999, summarise.Title{Text: "ghost", Source: summarise.SourceExplicit}); err == nil {
|
|
t.Fatal("renaming an unknown run succeeded")
|
|
}
|
|
}
|
|
|
|
// TestRunModelOverrideRoundTripAndRequestKey proves the launch-time model
|
|
// overrides round-trip through the run record and the per-step provider/model,
|
|
// and that a request-key replay with different overrides is refused as a
|
|
// different run.
|
|
func TestRunModelOverrideRoundTripAndRequestKey(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "override.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "override", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
definition, _ := store.Get(ctx, items[0].ID, 1)
|
|
overrides := RunOverrides{Model: "acme/whole", StageModels: map[string]string{"one": "acme/strong"}}
|
|
id, created, err := store.CreateRunTitled(ctx, definition, "override-key", "workspace", false, nil,
|
|
map[string]any{"prompt": "task"}, overrides, summarise.Title{})
|
|
if err != nil || !created {
|
|
t.Fatalf("create override run = %d %v %v", id, created, err)
|
|
}
|
|
if err := store.BeginStep(ctx, id, 1, "one", map[string]any{"prompt": "task"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: 1, Stage: "one", Status: "completed", Outcome: "success", Output: map[string]any{}, Provider: "acme", Model: "strong"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := store.GetRun(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Model != "acme/whole" || got.StageModels["one"] != "acme/strong" {
|
|
t.Fatalf("run record did not echo overrides: %+v", got)
|
|
}
|
|
if len(got.Steps) != 1 || got.Steps[0].Provider != "acme" || got.Steps[0].Model != "strong" {
|
|
t.Fatalf("step did not record the effective model: %+v", got.Steps)
|
|
}
|
|
list, err := store.ListRuns(ctx)
|
|
if err != nil || len(list) != 1 || list[0].Model != "acme/whole" || list[0].StageModels["one"] != "acme/strong" {
|
|
t.Fatalf("run list omitted the overrides: %+v %v", list, err)
|
|
}
|
|
// An idempotent replay with the same overrides returns the same run.
|
|
again, created, err := store.CreateRunTitled(ctx, definition, "override-key", "workspace", false, nil,
|
|
map[string]any{"prompt": "task"}, overrides, summarise.Title{})
|
|
if err != nil || created || again != id {
|
|
t.Fatalf("same-override replay = %d %v %v, want the stored run", again, created, err)
|
|
}
|
|
// A different override under the same key is a different run.
|
|
if _, _, err := store.CreateRunTitled(ctx, definition, "override-key", "workspace", false, nil,
|
|
map[string]any{"prompt": "task"}, RunOverrides{StageModels: map[string]string{"one": "acme/other"}}, summarise.Title{}); err == nil || !strings.Contains(err.Error(), "request key already used") {
|
|
t.Fatalf("differing-override replay was not refused: %v", err)
|
|
}
|
|
if _, _, err := store.CreateRunTitled(ctx, definition, "override-key", "workspace", false, nil,
|
|
map[string]any{"prompt": "task"}, RunOverrides{}, summarise.Title{}); err == nil || !strings.Contains(err.Error(), "request key already used") {
|
|
t.Fatalf("no-override replay was not refused: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestRunModelOverrideMigrationKeepsHistory proves a database created before the
|
|
// override columns existed gains them idempotently on Open and still lists and
|
|
// reads its historical rows with empty overrides.
|
|
func TestRunModelOverrideMigrationKeepsHistory(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "override-migration.db")
|
|
store, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := store.Submit(ctx, "legacy", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
definition, _ := store.Get(ctx, items[0].ID, 1)
|
|
id, _, err := store.CreateRun(ctx, definition, "legacy-key", "workspace", false, nil, map[string]any{"prompt": "task"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Simulate a pre-override database by dropping the columns, then reopen.
|
|
for _, column := range []string{"model", "stage_models"} {
|
|
if _, err := store.db.ExecContext(ctx, "ALTER TABLE pipeline_runs DROP COLUMN "+column); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, column := range []string{"provider", "model"} {
|
|
if _, err := store.db.ExecContext(ctx, "ALTER TABLE run_steps DROP COLUMN "+column); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store, err = Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
runs, err := store.ListRuns(ctx)
|
|
if err != nil || len(runs) != 1 {
|
|
t.Fatalf("legacy run did not survive the override migration: %+v %v", runs, err)
|
|
}
|
|
got, err := store.GetRun(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Model != "" || len(got.StageModels) != 0 {
|
|
t.Fatalf("migrated row should keep empty overrides: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestRunRecoveryAndVersionPinning(t *testing.T) {
|
|
ctx := context.Background()
|
|
path := filepath.Join(t.TempDir(), "audit.db")
|
|
store, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
source := Template([]string{"one"}, true)
|
|
if _, err := store.Submit(ctx, "audit", source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
definition, _ := store.Get(ctx, items[0].ID, 1)
|
|
id, _, err := store.CreateRun(ctx, definition, "recover", "workspace", false, nil, map[string]any{"n": 123})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.BeginStep(ctx, id, 1, "one", map[string]any{"n": 123}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.RunSession(ctx, id, 1, "ses_recover", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store, err = Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
owners, err := store.ActiveSessionOwners(ctx)
|
|
if err != nil || len(owners) != 1 {
|
|
t.Fatal("lost active session ownership")
|
|
}
|
|
if err := store.RecoverRuns(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := store.Submit(ctx, "audit", Template([]string{"replacement", "another"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run, err := store.GetRun(ctx, id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if run.Status != "interrupted" || run.Steps[0].Status != "interrupted" || run.Steps[0].AgentActive || run.ContentHash != definition.ContentHash || run.Version != 1 {
|
|
t.Fatal("recovery silently resumed or changed version", run)
|
|
}
|
|
if !reflect.DeepEqual(run.Graph, definition.Graph) {
|
|
t.Fatal("run graph did not retain its pinned definition", run.Graph)
|
|
}
|
|
loaded, _ := store.Get(ctx, definition.ID, 1)
|
|
if loaded.Source != source {
|
|
t.Fatal("run modified immutable source")
|
|
}
|
|
}
|
|
|
|
// TestActiveAgentSessionsAreScopedToRun proves the run-scoped query returns only
|
|
// the row's own active session: a second run's active session is never returned,
|
|
// and clearing the session clears the result.
|
|
func TestActiveAgentSessionsAreScopedToRun(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "scoped.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "scoped", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
definition, err := store.Get(ctx, items[0].ID, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runA, _, err := store.CreateRun(ctx, definition, "a", "workspace-a", false, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
runB, _, err := store.CreateRun(ctx, definition, "b", "workspace-b", false, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, run := range []struct {
|
|
id int64
|
|
session string
|
|
}{{runA, "ses_a"}, {runB, "ses_b"}} {
|
|
if err := store.BeginStep(ctx, run.id, 1, "one", map[string]any{"prompt": "task"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.RunSession(ctx, run.id, 1, run.session, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
sessions, err := store.ActiveAgentSessionsForRun(ctx, runA)
|
|
if err != nil || !reflect.DeepEqual(sessions, []string{"ses_a"}) {
|
|
t.Fatalf("run-scoped query leaked another run's session: %v %v", sessions, err)
|
|
}
|
|
if err := store.RunSession(ctx, runA, 1, "ses_a", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sessions, err = store.ActiveAgentSessionsForRun(ctx, runA)
|
|
if err != nil || len(sessions) != 0 {
|
|
t.Fatalf("inactive session still reported active: %v %v", sessions, err)
|
|
}
|
|
if sessions, err = store.ActiveAgentSessionsForRun(ctx, runB); err != nil || !reflect.DeepEqual(sessions, []string{"ses_b"}) {
|
|
t.Fatalf("second run's own session was affected: %v %v", sessions, err)
|
|
}
|
|
}
|
|
|
|
// TestOwnedSessionsListsRecordedWithActiveFlag proves OwnedSessions returns every
|
|
// session the server ever recorded (active or not) in run_steps, deduped by
|
|
// session id in favour of the newest owning run, with that run's workspace
|
|
// directory and the active flag.
|
|
func TestOwnedSessionsListsRecordedWithActiveFlag(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "owned.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "owned", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, err := store.List(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
definition, err := store.Get(ctx, items[0].ID, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
record := func(key, workspace, session string, active bool) {
|
|
t.Helper()
|
|
run, _, err := store.CreateRun(ctx, definition, key, workspace, false, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.BeginStep(ctx, run, 1, "one", map[string]any{"prompt": "task"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.RunSession(ctx, run, 1, session, active, "v2", "/repo"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
record("owned-a", "/work/a", "ses_active", true)
|
|
record("owned-b", "/work/b", "ses_idle", false)
|
|
// The same session recorded by a newer run must dedupe to one entry, taking
|
|
// the newest run's workspace and active flag.
|
|
record("owned-c", "/work/c", "ses_idle", true)
|
|
|
|
owners, err := store.OwnedSessions(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(owners) != 2 {
|
|
t.Fatalf("owned sessions = %+v, want 2 deduped", owners)
|
|
}
|
|
byID := map[string]SessionOwner{}
|
|
for _, owner := range owners {
|
|
byID[owner.ID] = owner
|
|
}
|
|
if owner, ok := byID["ses_active"]; !ok || !owner.Active || owner.Directory != "/work/a" {
|
|
t.Fatalf("active owner = %+v", owner)
|
|
}
|
|
if owner, ok := byID["ses_idle"]; !ok || !owner.Active || owner.Directory != "/work/c" {
|
|
t.Fatalf("deduped owner should come from the newest run: %+v", owner)
|
|
}
|
|
if active, err := store.ActiveSessionOwners(ctx); err != nil || len(active) != 2 {
|
|
t.Fatalf("ActiveSessionOwners = %+v %v, want both now-active sessions", active, err)
|
|
}
|
|
}
|
|
|
|
// TestSessionExportRoundTripAndAbsentEvidence proves a recorded session export
|
|
// survives FinishStep/GetRun and that a step with no export row (an old database
|
|
// that gained the empty table on Open) loads with no export evidence.
|
|
func TestSessionExportRoundTripAndAbsentEvidence(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "session-exports.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "exports", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
definition, _ := store.Get(ctx, items[0].ID, 1)
|
|
withExport, _, err := store.CreateRun(ctx, definition, "with-export", "workspace", false, nil, map[string]any{"prompt": "task"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
withoutExport, _, err := store.CreateRun(ctx, definition, "without-export", "workspace", false, nil, map[string]any{"prompt": "task"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, id := range []int64{withExport, withoutExport} {
|
|
if err := store.BeginStep(ctx, id, 1, "one", map[string]any{"prompt": "task"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
want := &protocol.SessionExport{
|
|
SessionID: "ses_recorded", Backend: "v2", Source: "native_export",
|
|
HTML: "session.html", HTMLBytes: 2345, Messages: 9, Truncated: true,
|
|
Diagnostic: "kept",
|
|
}
|
|
if err := store.FinishStep(ctx, withExport, protocol.RunStep{Sequence: 1, Stage: "one", Status: "completed", Outcome: "success", Output: map[string]any{}, Transcript: strings.Repeat("x", 40<<10), SessionExport: want}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.FinishStep(ctx, withoutExport, protocol.RunStep{Sequence: 1, Stage: "one", Status: "completed", Outcome: "success", Output: map[string]any{}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := store.GetRun(ctx, withExport)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got.Steps) != 1 || got.Steps[0].SessionExport == nil || *got.Steps[0].SessionExport != *want {
|
|
t.Fatalf("session export did not round-trip: %+v", got.Steps[0].SessionExport)
|
|
}
|
|
// The export evidence must not change how the transcript is bounded.
|
|
if len(got.Steps[0].Transcript) > 32<<10 || !strings.Contains(got.Steps[0].Transcript, "truncated") {
|
|
t.Fatalf("transcript bound changed by export: %d bytes", len(got.Steps[0].Transcript))
|
|
}
|
|
plain, err := store.GetRun(ctx, withoutExport)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if plain.Steps[0].SessionExport != nil {
|
|
t.Fatalf("absent export evidence was fabricated: %+v", plain.Steps[0].SessionExport)
|
|
}
|
|
}
|
|
|
|
// TestSessionExportIgnoresLegacyMarkdownFields proves a record written before
|
|
// the HTML-only change still loads: the retired markdown fields are ignored and
|
|
// the html evidence is read, so an older database opens cleanly.
|
|
func TestSessionExportIgnoresLegacyMarkdownFields(t *testing.T) {
|
|
ctx := context.Background()
|
|
store, err := Open(filepath.Join(t.TempDir(), "legacy-session-exports.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if _, err := store.Submit(ctx, "legacy-exports", Template([]string{"one"}, true)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
items, _ := store.List(ctx)
|
|
definition, _ := store.Get(ctx, items[0].ID, 1)
|
|
runID, _, err := store.CreateRun(ctx, definition, "legacy", "workspace", false, nil, map[string]any{"prompt": "task"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.BeginStep(ctx, runID, 1, "one", map[string]any{"prompt": "task"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
legacy := `{"session_id":"ses_old","backend":"v2","source":"native_export","markdown":"session.md","html":"session.html","markdown_bytes":111,"html_bytes":222,"messages":3}`
|
|
if _, err := store.db.ExecContext(ctx, "INSERT INTO step_session_exports(run_id,sequence,export_json) VALUES(?,?,?)", runID, 1, legacy); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
detail, err := store.GetRun(ctx, runID)
|
|
if err != nil {
|
|
t.Fatalf("an older database must still open: %v", err)
|
|
}
|
|
got := detail.Steps[0].SessionExport
|
|
if got == nil || got.HTML != "session.html" || got.HTMLBytes != 222 || got.Messages != 3 || got.SessionID != "ses_old" {
|
|
t.Fatalf("legacy export record did not load: %+v", got)
|
|
}
|
|
}
|