fix: read opencode sessions from session_v2/session_message #7

Merged
david merged 3 commits from fix/opencode-session-v2 into main 2026-11-02 01:16:51 +00:00
Owner

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).

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`).
david added 3 commits 2026-11-01 21:51:56 +00:00
Pre-existing work-in-progress in the tree: karakeep, snippets,
skills-health and redaction ingesters, the opencode.db session sweep,
gossip and metrics changes. Checkpointed as-is so the adapter fix in the
next commit stays reviewable on its own.
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
Missed by the gofmt sweep in d6d2a24; `gofmt -l .` is now clean.
david force-pushed fix/opencode-session-v2 from e07ae8aa27 to 6335670ddb 2026-11-01 21:51:56 +00:00 Compare
Author
Owner

Rebased onto main (bb852fa) — conflicts resolved, no merge commit.

Why it conflicted

#3 (gossip/HLC/watcher/MCP hardening) and #4 (gofmt) landed after this branch was cut. Both touched the same files as the checkpoint commit; the fix commit itself merged without a conflict.

How each was resolved

file resolution
internal/cmd/ingest.go main's side was whitespace + import ordering only; kept main's import block and this branch's ingester additions (karakeep, opencode.db).
internal/cmd/mcp.go kept main's requiredStringArg/optionalIntArg hardening and ported the snippet/code scope onto it. The seven remaining clampInt call sites (thread_groom, thread_text, snippet_candidates, snippet_search) now use optionalIntArg and report bad args instead of silently clamping.
internal/db/db_test.go add/add on both sides — unioned. main's 4 tests (tmpDB/record) and this branch's 17 (newTestDB/obs), no symbol collisions.
internal/watch/watch.go rebuilt on main's race-hardened version: kept fileDebouncer and the atomic.Bool run-guards, re-expressed the 30s log settle as a per-event delay into debounce.schedule, and re-added the opencode/karakeep/health/snippet tickers (now CompareAndSwap-guarded) plus their sweep functions.

Also added 6335670 — gofmt -l . flagged internal/watch/gossip_test.go (missed by #4); the tree is now gofmt-clean.

Re-validated after the rebase

  • go build ./..., go vet ./..., go test ./... — all packages pass, including main's new hlc, gossip and concurrency tests.
  • Rebuilt the daemon and restarted knox.service: first sweep logged opencode.db: 1 new, 448 unchanged, log debouncing still settles correctly, no errors or panics.
  • git merge-tree origin/main HEAD is clean, so the PR merges without conflicts.
Rebased onto `main` (`bb852fa`) — conflicts resolved, no merge commit. ## Why it conflicted `#3` (gossip/HLC/watcher/MCP hardening) and `#4` (gofmt) landed after this branch was cut. Both touched the same files as the checkpoint commit; the fix commit itself merged without a conflict. ## How each was resolved | file | resolution | |---|---| | `internal/cmd/ingest.go` | main's side was whitespace + import ordering only; kept main's import block and this branch's ingester additions (karakeep, opencode.db). | | `internal/cmd/mcp.go` | kept main's `requiredStringArg`/`optionalIntArg` hardening and ported the snippet/code scope onto it. The seven remaining `clampInt` call sites (thread_groom, thread_text, snippet_candidates, snippet_search) now use `optionalIntArg` and report bad args instead of silently clamping. | | `internal/db/db_test.go` | add/add on both sides — unioned. main's 4 tests (`tmpDB`/`record`) and this branch's 17 (`newTestDB`/`obs`), no symbol collisions. | | `internal/watch/watch.go` | rebuilt on main's race-hardened version: kept `fileDebouncer` and the `atomic.Bool` run-guards, re-expressed the 30s log settle as a per-event delay into `debounce.schedule`, and re-added the opencode/karakeep/health/snippet tickers (now `CompareAndSwap`-guarded) plus their sweep functions. | Also added `6335670` — `gofmt -l .` flagged `internal/watch/gossip_test.go` (missed by #4); the tree is now gofmt-clean. ## Re-validated after the rebase - `go build ./...`, `go vet ./...`, `go test ./...` — all packages pass, including main's new `hlc`, `gossip` and concurrency tests. - Rebuilt the daemon and restarted `knox.service`: first sweep logged `opencode.db: 1 new, 448 unchanged`, log debouncing still settles correctly, no errors or panics. - `git merge-tree origin/main HEAD` is clean, so the PR merges without conflicts.
david merged commit 8d6d73b747 into main 2026-11-02 01:16:51 +00:00
david deleted branch fix/opencode-session-v2 2026-11-02 01:16:52 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: david/knox#7