Files
david 8d6d73b747 fix: read opencode sessions from session_v2/session_message (#7)
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>
2026-11-02 01:16:50 +00:00

57 lines
1.6 KiB
Go

package ingest
import (
"strings"
"testing"
)
func TestHTMLToText(t *testing.T) {
in := `<div id="readability-page-1"><article><h1>Hello</h1>` +
`<script>evil()</script><style>.x{}</style>` +
`<p>A &amp; B &#39;quoted&#39;</p></article></div>`
out := htmlToText(in)
if strings.Contains(out, "evil()") || strings.Contains(out, ".x{}") {
t.Fatalf("script/style not stripped: %q", out)
}
for _, want := range []string{"Hello", "A & B", "'quoted'"} {
if !strings.Contains(out, want) {
t.Fatalf("missing %q in %q", want, out)
}
}
}
func TestKarakeepToResult(t *testing.T) {
title := "My Link"
k := &KarakeepIngester{Version: "test"}
b := kaBookmark{
ID: "bm1",
CreatedAt: "2026-01-02T03:04:05Z",
ModifiedAt: "2026-01-02T03:04:05Z",
Title: &title,
Tags: []kaTag{{Name: "zigbee"}},
Content: kaContent{Type: "link", URL: "https://example.com/a", Description: "short desc"},
}
r := k.toResult(b)
if r == nil {
t.Fatal("nil result")
}
if r.SourceID != "karakeep" || r.Title != "My Link" || r.SourcePath != "https://example.com/a" {
t.Fatalf("unexpected mapping: %+v", r)
}
// No asset and no inline text -> content falls back to description.
if r.Content != "short desc" {
t.Fatalf("content=%q, want description fallback", r.Content)
}
if r.Fingerprint == "" || r.ContentHash == "" {
t.Fatal("fingerprint/content hash must be set")
}
}
func TestKarakeepUnconfigured(t *testing.T) {
k := &KarakeepIngester{Version: "test"} // no BaseURL/APIKey
got, err := k.IngestAll()
if err != nil || got != nil {
t.Fatalf("unconfigured ingester should no-op, got %v %v", got, err)
}
}