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>
This commit was merged in pull request #7.
This commit is contained in:
2026-11-02 01:16:50 +00:00
committed by david
parent bb852faa27
commit 8d6d73b747
44 changed files with 6868 additions and 420 deletions
+59
View File
@@ -157,6 +157,10 @@ func (g *GitIngester) repoToResult(repoPath string) *IngestResult {
createdAt := status.LastCommit
// Carry the repo's README + config source so an agent can see what the repo
// actually contains, not just its last commit.
content := repoContent(repoPath)
return &IngestResult{
Fingerprint: fp,
SourceID: g.SourceID(),
@@ -165,9 +169,13 @@ func (g *GitIngester) repoToResult(repoPath string) *IngestResult {
ContentType: "git-status",
Title: name,
Summary: summary,
Content: content,
CreatedAt: createdAt,
Confidence: 0.9,
IngesterVersion: g.Version,
// Change signal covers status AND content, so status flips (dirty,
// ahead/behind) and code changes both refresh; unchanged repos no-op.
ContentHash: Fingerprint([]byte(fmt.Sprintf("git|%s|%s|%d|%d|%d", status.Head, content, status.Dirty, status.Ahead, status.Behind))),
Provenance: map[string]any{
"type": "git_status",
"path": repoPath,
@@ -178,10 +186,61 @@ func (g *GitIngester) repoToResult(repoPath string) *IngestResult {
"behind": status.Behind,
"last_commit": status.LastCommit,
"subject": status.Subject,
"content_len": len(content),
},
}
}
// Bounded repo content limits (kept small so a 10-minute sweep over many repos
// stays cheap).
const (
maxRepoContentBytes = 64 * 1024
maxRepoFileBytes = 16 * 1024
)
// repoContent assembles a bounded digest of a repository: its README, a
// tracked-file listing, and the contents of infrastructure/config files
// (Terraform, and common homelab formats). Empty when nothing is readable.
func repoContent(repoPath string) string {
var b strings.Builder
for _, name := range []string{"README.md", "README.rst", "README.txt", "README", "readme.md"} {
if data, err := os.ReadFile(filepath.Join(repoPath, name)); err == nil {
b.WriteString("## " + name + "\n")
b.WriteString(truncate(string(data), maxRepoFileBytes))
b.WriteString("\n\n")
break
}
}
if out, err := gitOut(repoPath, "ls-files"); err == nil && out != "" {
b.WriteString("## files\n")
b.WriteString(truncate(out, 4096))
b.WriteString("\n\n")
}
// Contents of infra/config files, newest-relevant first (git order).
files, _ := gitOut(repoPath, "ls-files", "--", "*.tf", "*.tfvars", "*.hcl", "*.yaml", "*.yml", "*.toml")
for _, f := range strings.Split(files, "\n") {
f = strings.TrimSpace(f)
if f == "" {
continue
}
if b.Len() >= maxRepoContentBytes {
break
}
data, err := os.ReadFile(filepath.Join(repoPath, f))
if err != nil {
continue
}
b.WriteString("## " + f + "\n")
b.WriteString(truncate(string(data), maxRepoFileBytes))
b.WriteString("\n\n")
}
return strings.TrimSpace(b.String())
}
func gitStatus(repoPath string) GitStatus {
out, err := gitOut(repoPath, "status", "--porcelain")
if err != nil {