Files
knox/internal/ingest/log_test.go
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

71 lines
2.3 KiB
Go

package ingest
import (
"os"
"path/filepath"
"testing"
)
func writeTempLog(t *testing.T, content string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "test.log")
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
return path
}
// INFO lines that merely contain the substring "timeout" must not be counted as
// errors (the old substring matcher flagged them) and must not appear in the
// content hash.
func TestLogIgnoresInfoTimeoutSubstring(t *testing.T) {
content := "" +
`timestamp=2026-10-01T00:00:00.000Z level=INFO run=aaaa message=evaluated permission=bash pattern="timeout 120 ssh foo"` + "\n" +
`timestamp=2026-10-01T00:00:01.000Z level=INFO run=bbbb message=evaluated permission=bash pattern="timeout 5 curl bar"` + "\n"
r, err := NewLogIngester().Ingest(writeTempLog(t, content))
if err != nil {
t.Fatal(err)
}
if r.Summary != "no errors" {
t.Fatalf("summary=%q, want 'no errors'", r.Summary)
}
if r.Provenance["error_count"].(int) != 0 {
t.Fatalf("error_count=%v, want 0", r.Provenance["error_count"])
}
}
// The content hash must be stable across volatile differences (timestamps, run
// ids, session/message ids, line numbers, extra INFO) and change only when the
// distinct error set changes.
func TestLogContentHashStability(t *testing.T) {
base := `[] level=ERROR run=aaaa session.id=ses_abc123 messageID=msg_zzz999 error="Not Found"` + "\n"
a := `timestamp=2026-10-01T00:00:00.000Z ` + base
// Same error, different volatile values, plus unrelated INFO noise.
b := `timestamp=2026-10-02T12:34:56.789Z ` + base +
`timestamp=2026-10-02T12:34:57.000Z level=INFO run=cccc message="resolved path" arg=timeout` + "\n"
l := NewLogIngester()
ra, err := l.Ingest(writeTempLog(t, a))
if err != nil {
t.Fatal(err)
}
rb, err := l.Ingest(writeTempLog(t, b))
if err != nil {
t.Fatal(err)
}
if ra.ContentHash != rb.ContentHash {
t.Fatalf("hash should be stable: %s != %s", ra.ContentHash, rb.ContentHash)
}
// A genuinely new distinct error changes the hash.
c := a + `timestamp=2026-10-01T00:00:05.000Z level=WARN run=dddd message="server unavailable" key=docker status=failed` + "\n"
rc, err := l.Ingest(writeTempLog(t, c))
if err != nil {
t.Fatal(err)
}
if rc.ContentHash == ra.ContentHash {
t.Fatal("hash should change when a new distinct error appears")
}
}