fix: read opencode sessions from session_v2/session_message
opencode 2.0.24 migrated its session store: the session, message and part tables stopped being written, and sessions now live in session_v2 with messages in session_message. The ingest kept querying the abandoned tables, so every sweep after the migration logged "0 new" and no error. Session titles are nullable in session_v2, so keep the COALESCE and the "Session <short id>" fallback. Transcripts are rebuilt from both message shapes the new store uses: user turns carry text at $.text, assistant turns carry a typed part list at $.content of which only "text" parts are conversation (reasoning and tool parts are not indexed). Rows are ordered by seq, then part index, so a session reads in order. Harness reminders arrive as synthetic messages now; drop <system-reminder> text so it does not consume the 4000-byte summary budget. Also stop skipping an entry purely on created_at: resuming a session appends messages without touching the creation time, so the summary is compared too and the entry refreshes. Bump the ingester to opencode-db/v2 and cover both message shapes with a test that builds the new schema in a temp database. Fixes #6
This commit is contained in:
@@ -1,10 +1,88 @@
|
||||
package ingest
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// opencode 2.0.24 moved sessions to session_v2/session_message and stopped
|
||||
// writing the v1 session/message/part tables. The adapter must read the new
|
||||
// store and pull conversation out of both message shapes: user text at
|
||||
// $.text, assistant text parts under $.content.
|
||||
func TestOpenCodeDBIngestSessionV2(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "opencode.db")
|
||||
db, 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 := db.Exec(stmt); err != nil {
|
||||
t.Fatalf("schema: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
msgs := []struct {
|
||||
typ string
|
||||
data string
|
||||
}{
|
||||
{"user", `{"text":"check the kitchen light"}`},
|
||||
{"assistant", `{"content":[{"type":"reasoning","text":"thinking hard"},` +
|
||||
`{"type":"text","text":"it is excluded from the kelvin path"},{"type":"tool","id":"call_1"}]}`},
|
||||
{"synthetic", `{"text":"<system-reminder>plan mode</system-reminder>"}`},
|
||||
}
|
||||
for i, m := range msgs {
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO session_message VALUES (?,?,?,?,0,0,?)`,
|
||||
fmt.Sprintf("msg_%d", i), "ses_upgrade", m.typ, i+1, m.data)
|
||||
if err != nil {
|
||||
t.Fatalf("insert message: %v", err)
|
||||
}
|
||||
}
|
||||
if _, err := db.Exec(
|
||||
`INSERT INTO session_v2 VALUES ('ses_upgrade','Kitchen light','/home/david/assistant',1700000000000,1700000001000)`,
|
||||
); err != nil {
|
||||
t.Fatalf("insert session: %v", err)
|
||||
}
|
||||
if err := db.Close(); err != nil {
|
||||
t.Fatalf("close: %v", err)
|
||||
}
|
||||
|
||||
results, err := (&OpenCodeDBIngester{Path: path}).IngestAll()
|
||||
if err != nil {
|
||||
t.Fatalf("IngestAll: %v", err)
|
||||
}
|
||||
if len(results) != 1 {
|
||||
t.Fatalf("results=%d, want 1", len(results))
|
||||
}
|
||||
|
||||
r := results[0]
|
||||
if r.Title != "Kitchen light" || r.Project != "assistant" {
|
||||
t.Fatalf("title=%q project=%q", r.Title, r.Project)
|
||||
}
|
||||
if !strings.Contains(r.Summary, "check the kitchen light") ||
|
||||
!strings.Contains(r.Summary, "excluded from the kelvin path") {
|
||||
t.Fatalf("summary missing conversation: %q", r.Summary)
|
||||
}
|
||||
for _, unwanted := range []string{"thinking hard", "system-reminder", "call_1"} {
|
||||
if strings.Contains(r.Content, unwanted) {
|
||||
t.Fatalf("content leaked %q: %q", unwanted, r.Content)
|
||||
}
|
||||
}
|
||||
if got := len(strings.Split(strings.TrimSpace(r.Content), "\n")); got != 2 {
|
||||
t.Fatalf("content lines=%d, want 2 (%q)", got, r.Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildSummary(t *testing.T) {
|
||||
if got := buildSummary(nil); got != "" {
|
||||
t.Fatalf("empty summary=%q", got)
|
||||
|
||||
Reference in New Issue
Block a user