c559f4ab03
Snippet extraction and claim lint were still on the frozen v1 part table. opencode 2.0.24 moved tool calls into session_message.data $.content and stopped writing part at 2026-11-01, so every later session produced no snippet candidates and no lint evidence: 1,475 completed write/edit/shell calls are in v2 that v1 never saw. Both readers now prefer v2 and fall back to v1 only where v2 has nothing, so a migrated session is never counted twice. Shape differences handled: the tool name moved from $.tool to $.name, write/edit input carries 'path' as well as 'filePath', completed output is $.state.content[] rather than a flat $.state.output, and the shell tool is emitted as both 'bash' and 'shell'. v2 has no patch parts, so PatchHash stays empty for v2 candidates; supersession already derives from the last full write, so nothing depends on it.
140 lines
4.8 KiB
Go
140 lines
4.8 KiB
Go
package claims
|
|
|
|
import (
|
|
"database/sql"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// opencode 2.0.24 keeps prose and tool results inside session_message.data and
|
|
// stopped writing the v1 part table. The lint used to join the two, which found
|
|
// nothing for any session written after the migration.
|
|
func TestLoadSessionV2(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "opencode.db")
|
|
raw, err := sql.Open("sqlite", "file:"+path)
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
for _, stmt := range []string{
|
|
`CREATE TABLE session_v2 (id TEXT PRIMARY KEY, title TEXT, directory TEXT,
|
|
time_created INTEGER NOT NULL, time_updated INTEGER NOT NULL)`,
|
|
`CREATE TABLE session_message (id TEXT PRIMARY KEY, session_id TEXT NOT NULL,
|
|
type TEXT NOT NULL, seq INTEGER NOT NULL, time_created INTEGER NOT NULL,
|
|
time_updated INTEGER NOT NULL, data TEXT NOT NULL)`,
|
|
} {
|
|
if _, err := raw.Exec(stmt); err != nil {
|
|
t.Fatalf("schema: %v", err)
|
|
}
|
|
}
|
|
if _, err := raw.Exec(`INSERT INTO session_v2 VALUES ('ses_1','Costs','/tmp/p',1,2)`); err != nil {
|
|
t.Fatalf("session: %v", err)
|
|
}
|
|
msgs := []struct {
|
|
id, typ, data string
|
|
}{
|
|
{"msg_1", "user", `{"text":"what did the run cost?"}`},
|
|
{"msg_2", "assistant", `{"model":{"id":"m1"},"content":[` +
|
|
`{"type":"reasoning","text":"look at the output"},` +
|
|
`{"type":"text","text":"The change touched 4,212 lines."},` +
|
|
`{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed",` +
|
|
`"content":[{"type":"text","text":"4,212 lines changed"}]}}]}`},
|
|
// A tool that never finished proves nothing and must stay out.
|
|
{"msg_3", "assistant", `{"content":[{"type":"tool","id":"call_2","name":"shell",` +
|
|
`"state":{"status":"running","content":[{"type":"text","text":"9,999"}]}}]}`},
|
|
}
|
|
for i, m := range msgs {
|
|
if _, err := raw.Exec(
|
|
`INSERT INTO session_message VALUES (?,?,?,?,1,1,?)`, m.id, "ses_1", m.typ, i+1, m.data,
|
|
); err != nil {
|
|
t.Fatalf("message: %v", err)
|
|
}
|
|
}
|
|
if err := raw.Close(); err != nil {
|
|
t.Fatalf("close: %v", err)
|
|
}
|
|
|
|
db, err := Open(path)
|
|
if err != nil {
|
|
t.Fatalf("claims open: %v", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
s, err := LoadSession(db, "ses_1")
|
|
if err != nil {
|
|
t.Fatalf("LoadSession: %v", err)
|
|
}
|
|
if len(s.Messages) != 3 {
|
|
t.Fatalf("messages=%d, want 3", len(s.Messages))
|
|
}
|
|
if got := strings.Join(s.Messages[0].Text, " "); !strings.Contains(got, "cost") {
|
|
t.Fatalf("user prose not read from $.text: %q", got)
|
|
}
|
|
assistant := s.Messages[1]
|
|
if got := strings.Join(assistant.Text, " "); !strings.Contains(got, "4,212 lines") {
|
|
t.Fatalf("assistant prose not read from $.content: %q", got)
|
|
}
|
|
if len(assistant.Outputs) != 1 || !strings.Contains(assistant.Outputs[0], "4,212") {
|
|
t.Fatalf("tool output not read from $.state.content: %v", assistant.Outputs)
|
|
}
|
|
if len(s.Messages[2].Outputs) != 0 {
|
|
t.Fatalf("a running tool contributed evidence: %v", s.Messages[2].Outputs)
|
|
}
|
|
|
|
// The prose figure is backed by the tool output in the same turn.
|
|
runs := Lint(s)
|
|
if len(runs) == 0 || runs[0].Status != Sourced {
|
|
t.Fatalf("lint did not source the figure: %+v", runs)
|
|
}
|
|
}
|
|
|
|
// Messages that v2 predates must still be readable from the v1 part table.
|
|
func TestLoadSessionFallsBackToLegacyParts(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "opencode.db")
|
|
raw, err := sql.Open("sqlite", "file:"+path)
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
for _, stmt := range []string{
|
|
`CREATE TABLE session_v2 (id TEXT PRIMARY KEY, title TEXT, directory TEXT,
|
|
time_created INTEGER NOT NULL, time_updated INTEGER NOT NULL)`,
|
|
`CREATE TABLE session_message (id TEXT PRIMARY KEY, session_id TEXT NOT NULL,
|
|
type TEXT NOT NULL, seq INTEGER NOT NULL, time_created INTEGER NOT NULL,
|
|
time_updated INTEGER NOT NULL, data TEXT NOT NULL)`,
|
|
`CREATE TABLE part (id TEXT PRIMARY KEY, message_id TEXT, session_id TEXT,
|
|
time_created INTEGER, data TEXT NOT NULL)`,
|
|
} {
|
|
if _, err := raw.Exec(stmt); err != nil {
|
|
t.Fatalf("schema: %v", err)
|
|
}
|
|
}
|
|
if _, err := raw.Exec(`INSERT INTO session_v2 VALUES ('ses_1','Old','/tmp/p',1,2)`); err != nil {
|
|
t.Fatalf("session: %v", err)
|
|
}
|
|
if _, err := raw.Exec(
|
|
`INSERT INTO session_message VALUES ('msg_1','ses_1','assistant',1,1,1,'{}')`); err != nil {
|
|
t.Fatalf("message: %v", err)
|
|
}
|
|
if _, err := raw.Exec(
|
|
`INSERT INTO part VALUES ('prt_1','msg_1','ses_1',1,'{"type":"text","text":"legacy prose"}')`); err != nil {
|
|
t.Fatalf("part: %v", err)
|
|
}
|
|
if err := raw.Close(); err != nil {
|
|
t.Fatalf("close: %v", err)
|
|
}
|
|
|
|
db, err := Open(path)
|
|
if err != nil {
|
|
t.Fatalf("claims open: %v", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
s, err := LoadSession(db, "ses_1")
|
|
if err != nil {
|
|
t.Fatalf("LoadSession: %v", err)
|
|
}
|
|
if len(s.Messages) != 1 || len(s.Messages[0].Text) != 1 || s.Messages[0].Text[0] != "legacy prose" {
|
|
t.Fatalf("legacy part not read: %+v", s.Messages)
|
|
}
|
|
}
|