Files
knox/main.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

93 lines
2.5 KiB
Go

package main
import (
"fmt"
"log"
"os"
knoxcmd "github.com/david/knox/internal/cmd"
"github.com/david/knox/internal/db"
knoxserver "github.com/david/knox/internal/server"
mcpServer "github.com/mark3labs/mcp-go/server"
"github.com/spf13/cobra"
)
func newServeCmd(kdb *db.KnoxDB) *cobra.Command {
var port int
cmd := &cobra.Command{
Use: "serve",
Short: "Start web UI for the knowledge index",
RunE: func(c *cobra.Command, args []string) error {
srv := knoxserver.New(kdb)
addr := fmt.Sprintf("localhost:%d", port)
log.Printf("[knox] web UI at http://%s", addr)
return srv.Serve(addr)
},
}
cmd.Flags().IntVarP(&port, "port", "p", 8924, "HTTP port")
return cmd
}
func main() {
dbPath := os.Getenv("KNOX_DB")
if dbPath == "" {
home, err := os.UserHomeDir()
if err != nil {
log.Fatalf("cannot get home dir: %v", err)
}
dbPath = home + "/.local/share/knox/index.db"
}
kdb, err := db.Open(dbPath)
if err != nil {
log.Fatalf("cannot open db: %v", err)
}
defer kdb.Close()
root := &cobra.Command{
Use: "knox",
Short: "Knowledge index & session tracker for opencode",
Long: `Knox watches opencode session data, ingests knowledge fingerprints,
and maintains a searchable index. Use 'knox watch' for daemon mode.`,
}
root.AddCommand(knoxcmd.NewWatchCmd(kdb))
root.AddCommand(knoxcmd.NewStatusCmd(kdb))
root.AddCommand(knoxcmd.NewQueryCmd(kdb))
root.AddCommand(knoxcmd.NewRecentCmd(kdb))
root.AddCommand(knoxcmd.NewReflectCmd(kdb))
root.AddCommand(knoxcmd.NewIngestCmd(kdb))
root.AddCommand(knoxcmd.NewThreadCmd(kdb))
root.AddCommand(knoxcmd.NewBrowserCmd(kdb))
root.AddCommand(knoxcmd.NewObsidianCmd(kdb))
root.AddCommand(knoxcmd.NewTopicsCmd(kdb))
root.AddCommand(knoxcmd.NewGiteaCmd(kdb))
root.AddCommand(knoxcmd.NewKarakeepCmd(kdb))
root.AddCommand(knoxcmd.NewSkillsHealthCmd())
root.AddCommand(knoxcmd.NewGitCmd(kdb))
root.AddCommand(knoxcmd.NewSnippetsCmd(kdb))
root.AddCommand(knoxcmd.NewReconcileCmd(kdb))
root.AddCommand(knoxcmd.NewGossipCmd(kdb))
root.AddCommand(newServeCmd(kdb))
// MCP server subcommand
mcpCmd := &cobra.Command{
Use: "mcp",
Short: "Start MCP server for opencode integration",
RunE: func(c *cobra.Command, args []string) error {
s := knoxcmd.NewMCPServer(kdb)
log.Printf("[knox] starting MCP stdio server")
if err := mcpServer.ServeStdio(s); err != nil {
return err
}
return nil
},
}
root.AddCommand(mcpCmd)
if err := root.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}