Files
knox/internal/skillsrepo/health.go
T
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

122 lines
3.0 KiB
Go

// Package skillsrepo checks the health of the skills git repository
// (~/.skills). The skills MCP auto-commits and auto-pushes, but it swallows
// git errors, so a corrupt repo (e.g. from disk exhaustion mid-write) can rot
// silently. This package surfaces that state so knox can warn about it.
package skillsrepo
import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
)
// Health is a snapshot of the skills repo's git + disk state.
type Health struct {
Dir string
IsRepo bool
Head string
Branch string
Ahead int
Behind int
Dirty int
FsckOK bool
FreeBytes uint64
Problems []string
}
// Healthy reports whether the repo is in a state the MCP can commit/push from.
func (h Health) Healthy() bool {
return h.IsRepo && h.FsckOK && h.Head != "" && len(h.Problems) == 0
}
// Default checks ~/.skills.
func Default() Health {
home, _ := os.UserHomeDir()
return Check(filepath.Join(home, ".skills"))
}
// Check inspects the git repo at dir. It never mutates anything.
func Check(dir string) Health {
h := Health{Dir: dir, FsckOK: true}
if fi, err := os.Stat(filepath.Join(dir, ".git")); err != nil || !fi.IsDir() {
h.IsRepo = false
h.Problems = append(h.Problems, "no .git directory")
h.FreeBytes = freeBytes(dir)
return h
}
h.IsRepo = true
// HEAD must be readable — the tell-tale of object corruption.
if out, err := git(dir, "rev-parse", "HEAD"); err != nil {
h.Problems = append(h.Problems, "HEAD unreadable ("+firstLine(err.Error())+")")
} else {
h.Head = out
}
if out, err := git(dir, "rev-parse", "--abbrev-ref", "HEAD"); err == nil {
h.Branch = out
}
// Object integrity.
if _, err := git(dir, "fsck", "--connectivity-only", "--no-progress"); err != nil {
h.FsckOK = false
h.Problems = append(h.Problems, "git fsck failed")
}
// Working tree.
if out, err := git(dir, "status", "--porcelain"); err == nil {
h.Dirty = countLines(out)
}
// Ahead/behind vs upstream.
if out, err := git(dir, "rev-list", "--left-right", "--count", "HEAD...@{upstream}"); err == nil {
f := strings.Fields(out)
if len(f) == 2 {
h.Ahead, _ = strconv.Atoi(f[0])
h.Behind, _ = strconv.Atoi(f[1])
}
}
h.FreeBytes = freeBytes(dir)
if h.FreeBytes > 0 && h.FreeBytes < 1<<30 { // < 1 GiB free
h.Problems = append(h.Problems, "low disk space (<1 GiB)")
}
return h
}
func git(dir string, args ...string) (string, error) {
cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1")
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
func freeBytes(path string) uint64 {
var st syscall.Statfs_t
if err := syscall.Statfs(path, &st); err != nil {
return 0
}
return st.Bavail * uint64(st.Bsize)
}
func countLines(s string) int {
s = strings.TrimSpace(s)
if s == "" {
return 0
}
return len(strings.Split(s, "\n"))
}
func firstLine(s string) string {
if i := strings.IndexByte(s, '\n'); i >= 0 {
return s[:i]
}
return s
}