| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // Legacy session JSONL compatibility gate for the extension-kernel work. |
| 10 | // Sessions written by older releases (messages without any of the newer |
| 11 | // fields — no reasoning signature, no images, no local_only, no event log) |
| 12 | // must load unchanged, and a save/load round-trip must preserve every |
| 13 | // message. The extension kernel migrates session paths across runtime |
| 14 | // rebuilds, so this contract is load-bearing. |
| 15 | |
| 16 | const legacySessionFixture = `{"role":"system","content":"LEGACY SYSTEM PROMPT","createdAt":1690000000000} |
| 17 | {"role":"user","content":"first question","createdAt":1690000001000} |
| 18 | {"role":"assistant","content":"first answer","createdAt":1690000002000} |
| 19 | {"role":"user","content":"run something","createdAt":1690000003000} |
| 20 | {"role":"assistant","content":"running","tool_calls":[{"id":"call_abc","name":"bash","arguments":"{\"cmd\":\"ls\"}"}],"createdAt":1690000004000} |
| 21 | {"role":"tool","content":"file.txt","tool_call_id":"call_abc","name":"bash","createdAt":1690000005000} |
| 22 | {"role":"assistant","content":"done","createdAt":1690000006000} |
| 23 | ` |
| 24 | |
| 25 | func TestLegacySessionJSONLRoundTrip(t *testing.T) { |
| 26 | dir := t.TempDir() |
| 27 | path := filepath.Join(dir, "legacy-session.jsonl") |
| 28 | if err := os.WriteFile(path, []byte(legacySessionFixture), 0o644); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | |
| 32 | s, err := LoadSession(path) |
| 33 | if err != nil { |
| 34 | t.Fatalf("LoadSession legacy fixture: %v", err) |
| 35 | } |
| 36 | msgs := s.Snapshot() |
| 37 | if len(msgs) != 7 { |
| 38 | t.Fatalf("loaded %d messages, want 7", len(msgs)) |
| 39 | } |
| 40 | if msgs[0].Role != "system" || msgs[0].Content != "LEGACY SYSTEM PROMPT" { |
| 41 | t.Fatalf("system message drifted: %+v", msgs[0]) |
| 42 | } |
| 43 | if msgs[4].ToolCalls[0].ID != "call_abc" || msgs[4].ToolCalls[0].Name != "bash" { |
| 44 | t.Fatalf("tool call drifted: %+v", msgs[4].ToolCalls) |
| 45 | } |
| 46 | if msgs[5].ToolCallID != "call_abc" || msgs[5].Name != "bash" { |
| 47 | t.Fatalf("tool result drifted: %+v", msgs[5]) |
| 48 | } |
| 49 | |
| 50 | // Save through the current writer and reload: the legacy conversation |
| 51 | // must survive a full round-trip with content intact. |
| 52 | if err := s.Save(path); err != nil { |
| 53 | t.Fatalf("Save: %v", err) |
| 54 | } |
| 55 | reloaded, err := LoadSession(path) |
| 56 | if err != nil { |
| 57 | t.Fatalf("reload: %v", err) |
| 58 | } |
| 59 | got := reloaded.Snapshot() |
| 60 | if len(got) != len(msgs) { |
| 61 | t.Fatalf("round-trip changed message count: %d -> %d", len(msgs), len(got)) |
| 62 | } |
| 63 | for i := range msgs { |
| 64 | if got[i].Role != msgs[i].Role || got[i].Content != msgs[i].Content || |
| 65 | got[i].ToolCallID != msgs[i].ToolCallID || got[i].Name != msgs[i].Name || |
| 66 | len(got[i].ToolCalls) != len(msgs[i].ToolCalls) { |
| 67 | t.Fatalf("message %d drifted across round-trip:\nbefore: %+v\nafter: %+v", i, msgs[i], got[i]) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 |