Why: Linear transcripts cannot fork, and compaction rewrote history, so resume and branch navigation had nothing to point at. Evidence: go test -count=1 ./runtime/session/ Impact: Load still returns the model-visible path, including old one-message-per-line files. Compaction appends a summary entry instead of truncating the file. Refs: feature/pi-headless N1 Co-authored-by: Cursor <cursoragent@cursor.com>
129 lines
3.4 KiB
Go
129 lines
3.4 KiB
Go
package session
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"agent-native/runtime/llm"
|
|
)
|
|
|
|
func TestTreeForkCloneAndLeaf(t *testing.T) {
|
|
dir := t.TempDir()
|
|
s, err := Open(filepath.Join(dir, "a.jsonl"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Append(llm.Message{Role: llm.RoleUser, Content: "one"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Append(llm.Message{Role: llm.RoleAssistant, Content: "two"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entries, err := s.Entries()
|
|
if err != nil || len(entries) != 2 || entries[1].ParentID != entries[0].ID {
|
|
t.Fatalf("tree links: %+v %v", entries, err)
|
|
}
|
|
fork, err := s.Fork(filepath.Join(dir, "fork.jsonl"), entries[0].ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := fork.Append(llm.Message{Role: llm.RoleUser, Content: "branch"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
msgs, err := fork.Load()
|
|
if err != nil || len(msgs) != 2 || msgs[1].Content != "branch" {
|
|
t.Fatalf("fork path: %+v %v", msgs, err)
|
|
}
|
|
orig, _ := s.Load()
|
|
if len(orig) != 2 || orig[1].Content != "two" {
|
|
t.Fatalf("source branch changed: %+v", orig)
|
|
}
|
|
clone, err := s.Clone(filepath.Join(dir, "clone.jsonl"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cmsgs, _ := clone.Load()
|
|
if len(cmsgs) != 2 || cmsgs[1].Content != "two" {
|
|
t.Fatalf("clone: %+v", cmsgs)
|
|
}
|
|
if err := s.SetLeaf(entries[0].ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
switched, _ := s.Load()
|
|
if len(switched) != 1 || switched[0].Content != "one" {
|
|
t.Fatalf("set leaf: %+v", switched)
|
|
}
|
|
}
|
|
|
|
func TestLegacyTranscriptStillLoads(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "old.jsonl")
|
|
body := "{\"role\":\"system\",\"content\":\"sys\"}\n{\"role\":\"user\",\"content\":\"hi\"}\n"
|
|
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err := Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
msgs, err := s.Load()
|
|
if err != nil || len(msgs) != 2 || msgs[1].Content != "hi" {
|
|
t.Fatalf("legacy: %+v %v", msgs, err)
|
|
}
|
|
if err := s.Append(llm.Message{Role: llm.RoleAssistant, Content: "next"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
msgs, err = s.Load()
|
|
if err != nil || len(msgs) != 3 || msgs[2].Content != "next" {
|
|
t.Fatalf("legacy append: %+v %v", msgs, err)
|
|
}
|
|
}
|
|
|
|
func TestCatalogContinueResumeAndHTML(t *testing.T) {
|
|
dir := t.TempDir()
|
|
s, err := Open(filepath.Join(dir, "sess.jsonl"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.SetCWD("/work"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.SetName("notes"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Append(llm.Message{Role: llm.RoleUser, Content: "hello"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, ok, err := Continue(dir, "/work")
|
|
if err != nil || !ok || got.Path() != s.Path() {
|
|
t.Fatalf("continue: %v %v %v", got, ok, err)
|
|
}
|
|
resumed, err := Resume(dir, "notes")
|
|
if err != nil || resumed.Name() != "notes" {
|
|
t.Fatalf("resume: %v %v", resumed, err)
|
|
}
|
|
htmlPath := filepath.Join(dir, "out.html")
|
|
if err := s.ExportHTML(htmlPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, _ := os.ReadFile(htmlPath)
|
|
if !strings.Contains(string(raw), "hello") || !strings.Contains(string(raw), "notes") {
|
|
t.Fatalf("html: %s", raw)
|
|
}
|
|
if _, err := s.AppendEntry(Entry{Type: TypeLabel, Label: "checkpoint"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entries, _ := s.Entries()
|
|
var labels int
|
|
for _, e := range entries {
|
|
if e.Type == TypeLabel && e.Label == "checkpoint" {
|
|
labels++
|
|
}
|
|
}
|
|
if labels != 1 {
|
|
t.Fatalf("label missing: %+v", entries)
|
|
}
|
|
}
|