8d6d73b747
Fixes #6. opencode 2.0.24 migrated its session store on 2026-11-01 21:01:20 UTC. `session`, `message` and `part` stopped being written; sessions now live in `session_v2` and messages in `session_message`. The ingest kept querying the abandoned tables, so every sweep after the migration logged `0 new` — no error, no new sessions indexed. ## Changes - **`internal/ingest/opencode_db.go`** - Read sessions from `session_v2` (title is nullable there, the existing COALESCE + fallback still apply). - Rebuild transcripts from both message shapes: user turns at `$.text`, assistant turns from `$.content[]` filtered to `type='text'` (reasoning and tool parts are not indexed). Ordered by `seq`, then part index. - Count messages from `session_message` instead of parts. - Drop `<system-reminder>` text — reminders now arrive as `synthetic` messages and would eat the 4000-byte summary budget. - Ingester version is now `opencode-db/v2`. - **`internal/watch/watch.go`** — an entry is no longer skipped on `created_at` alone. Resuming a session appends messages without touching the creation time, so the summary is compared too. - **`internal/ingest/opencode_db_test.go`** — new test builds the v2 schema in a temp SQLite database and asserts user text and assistant text parts are indexed while reasoning, tool and reminder content is not. ## Validation - `go build ./...`, `go test ./internal/ingest/ ./internal/db/ ./internal/watch/` pass. - Rebuilt the binary and restarted `knox.service` against the live store: ``` [knox] opencode.db: 1 new, 387 unchanged ``` The new entry is the session that had been invisible since the migration (`Knox OpenCode session adapter compatibility check`, observation version `opencode-db/v2`), and `knox query "knox opencode session adapter compatibility"` finds it. - 59 pre-migration sessions re-recorded because their stored transcripts contained reminder text that the new filter drops; summaries still carry the conversation. ## Notes The first commit on this branch is a checkpoint of pre-existing uncommitted work that the adapter fix sits on top of; the fix itself is the second commit (`e07ae8a`). Reviewed-on: #7 Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
121 lines
3.7 KiB
Go
121 lines
3.7 KiB
Go
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)
|
|
}
|
|
|
|
got := buildSummary([]string{"first prompt", "assistant: reply"})
|
|
if !strings.HasPrefix(got, "first prompt") || !strings.Contains(got, "reply") {
|
|
t.Fatalf("unexpected summary: %q", got)
|
|
}
|
|
|
|
// Transcript is bounded well under the cap plus the leading prompt.
|
|
long := strings.Repeat("x", transcriptCap*2)
|
|
got = buildSummary([]string{"p", long})
|
|
if len(got) > transcriptCap+300 {
|
|
t.Fatalf("summary not capped: %d bytes", len(got))
|
|
}
|
|
}
|
|
|
|
func TestToolEchoFiltered(t *testing.T) {
|
|
if !isToolEcho("Called the Read tool with the following input: {}") {
|
|
t.Fatal("tool echo not detected")
|
|
}
|
|
if !isToolEcho("<path>/tmp/x</path>") {
|
|
t.Fatal("path echo not detected")
|
|
}
|
|
if isToolEcho("review repo, start with @AGENTS.md") {
|
|
t.Fatal("real prompt wrongly filtered")
|
|
}
|
|
}
|
|
|
|
func TestShortID(t *testing.T) {
|
|
if got := shortID("ses_38703c295ffeIjbbaYM2Xh6GvK"); got != "38703c29" {
|
|
t.Fatalf("shortID=%q", got)
|
|
}
|
|
}
|