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:
+120
-35
@@ -8,37 +8,35 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/david/knox/internal/noise"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
const (
|
||||
chromiumHistoryPath = ".config/chromium/Default/History"
|
||||
minHistoryPath = ".config/Min/BrowserHistory.db"
|
||||
)
|
||||
const minHistoryPath = ".config/Min/BrowserHistory.db"
|
||||
|
||||
// noiseURLPatterns are URLs that represent navigation noise rather than knowledge.
|
||||
var noiseURLPatterns = []string{
|
||||
"opencode.ai",
|
||||
"auth.opencode.ai",
|
||||
"github.com/login/oauth/authorize",
|
||||
"accounts.google.com",
|
||||
"google.com/accounts",
|
||||
"kagi.com",
|
||||
"chatgpt.com",
|
||||
// knox indexing its own web UI is self-referential noise
|
||||
"localhost:8924",
|
||||
"localhost:18925",
|
||||
"127.0.0.1:8924",
|
||||
"127.0.0.1:18925",
|
||||
// chromiumBases are Chromium-family browser config roots. Each may hold a
|
||||
// "Default" and/or "Profile *" user profile containing a History database.
|
||||
var chromiumBases = []string{
|
||||
".config/chromium",
|
||||
".config/google-chrome",
|
||||
".config/google-chrome-beta",
|
||||
".config/google-chrome-unstable",
|
||||
".config/google-chrome-for-testing",
|
||||
".config/brave-browser",
|
||||
".config/BraveSoftware/Brave-Browser",
|
||||
".config/microsoft-edge",
|
||||
".config/microsoft-edge-dev",
|
||||
".config/vivaldi",
|
||||
".config/vivaldi-snapshot",
|
||||
".config/net.imput.helium",
|
||||
}
|
||||
|
||||
func noiseURL(url string) bool {
|
||||
for _, p := range noiseURLPatterns {
|
||||
if strings.Contains(url, p) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
// firefoxBaseGlobs are directories whose immediate subdirectories are Firefox
|
||||
// profiles holding places.sqlite (native, flatpak, and snap layouts).
|
||||
var firefoxBaseGlobs = []string{
|
||||
".mozilla/firefox",
|
||||
"snap/firefox/common/.mozilla/firefox",
|
||||
".var/app/org.mozilla.firefox/.mozilla/firefox",
|
||||
}
|
||||
|
||||
type BrowserHistoryIngester struct {
|
||||
@@ -62,23 +60,56 @@ func (b *BrowserHistoryIngester) IngestAll() ([]*IngestResult, error) {
|
||||
home, _ := os.UserHomeDir()
|
||||
var results []*IngestResult
|
||||
|
||||
// Chromium
|
||||
chromiumDB := filepath.Join(home, chromiumHistoryPath)
|
||||
cr, err := b.ingestChromium(chromiumDB)
|
||||
if err == nil {
|
||||
results = append(results, cr...)
|
||||
// Chromium-family (chromium, chrome*, brave, edge, vivaldi, helium), across
|
||||
// every user profile.
|
||||
for _, path := range chromiumHistoryPaths(home) {
|
||||
cr, err := b.ingestChromium(path)
|
||||
if err == nil {
|
||||
results = append(results, cr...)
|
||||
}
|
||||
}
|
||||
|
||||
// Min
|
||||
minDB := filepath.Join(home, minHistoryPath)
|
||||
minResults, err := b.ingestMin(minDB)
|
||||
minResults, err := b.ingestMin(filepath.Join(home, minHistoryPath))
|
||||
if err == nil {
|
||||
results = append(results, minResults...)
|
||||
}
|
||||
|
||||
// Firefox (places.sqlite), across every profile.
|
||||
for _, path := range firefoxPlacesPaths(home) {
|
||||
ff, err := b.ingestFirefox(path)
|
||||
if err == nil {
|
||||
results = append(results, ff...)
|
||||
}
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// chromiumHistoryPaths expands each Chromium-family base into its Default and
|
||||
// Profile * History databases.
|
||||
func chromiumHistoryPaths(home string) []string {
|
||||
var paths []string
|
||||
for _, base := range chromiumBases {
|
||||
root := filepath.Join(home, base)
|
||||
for _, pat := range []string{"Default/History", "Profile */History"} {
|
||||
matches, _ := filepath.Glob(filepath.Join(root, pat))
|
||||
paths = append(paths, matches...)
|
||||
}
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
// firefoxPlacesPaths finds places.sqlite in every Firefox profile.
|
||||
func firefoxPlacesPaths(home string) []string {
|
||||
var paths []string
|
||||
for _, base := range firefoxBaseGlobs {
|
||||
matches, _ := filepath.Glob(filepath.Join(home, base, "*", "places.sqlite"))
|
||||
paths = append(paths, matches...)
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
func (b *BrowserHistoryIngester) ingestChromium(path string) ([]*IngestResult, error) {
|
||||
if !fileExists(path) {
|
||||
return nil, nil
|
||||
@@ -173,19 +204,68 @@ func (b *BrowserHistoryIngester) ingestMin(path string) ([]*IngestResult, error)
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// ingestFirefox reads a Firefox places.sqlite. Unlike Chromium, last_visit_date
|
||||
// is a Unix (PRTime) microsecond value and visits can be hidden.
|
||||
func (b *BrowserHistoryIngester) ingestFirefox(path string) ([]*IngestResult, error) {
|
||||
if !fileExists(path) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
tmp := path + ".knox_tmp"
|
||||
if err := copyFile(path, tmp); err != nil {
|
||||
return nil, fmt.Errorf("copy firefox db: %w", err)
|
||||
}
|
||||
defer os.Remove(tmp)
|
||||
|
||||
db, err := sql.Open("sqlite", tmp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open firefox copy: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
rows, err := db.Query(
|
||||
`SELECT url, COALESCE(title,''), last_visit_date, COALESCE(visit_count,0)
|
||||
FROM moz_places
|
||||
WHERE last_visit_date > 0 AND COALESCE(hidden,0) = 0
|
||||
ORDER BY last_visit_date DESC
|
||||
LIMIT 1000`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query firefox places: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var results []*IngestResult
|
||||
for rows.Next() {
|
||||
var url, title string
|
||||
var visitMicros int64
|
||||
var visitCount int
|
||||
if err := rows.Scan(&url, &title, &visitMicros, &visitCount); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
visitTime := time.UnixMicro(visitMicros)
|
||||
if result := b.makeResult(url, title, visitTime, visitCount); result != nil {
|
||||
results = append(results, result)
|
||||
}
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (b *BrowserHistoryIngester) makeResult(url, title string, visitTime time.Time, visitCount int) *IngestResult {
|
||||
if url == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Skip chrome://, about://, file://, devtools
|
||||
// Skip chrome://, about://, file://, devtools and extension pages
|
||||
if strings.HasPrefix(url, "chrome://") || strings.HasPrefix(url, "about:") ||
|
||||
strings.HasPrefix(url, "file://") || strings.HasPrefix(url, "devtools://") ||
|
||||
strings.HasPrefix(url, "chrome-extension://") {
|
||||
strings.HasPrefix(url, "chrome-extension://") || strings.HasPrefix(url, "moz-extension://") ||
|
||||
strings.HasPrefix(url, "place:") {
|
||||
return nil
|
||||
}
|
||||
|
||||
if noiseURL(url) {
|
||||
if noise.IsNoisyURL(url) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -213,6 +293,11 @@ func (b *BrowserHistoryIngester) makeResult(url, title string, visitTime time.Ti
|
||||
CreatedAt: visitTime.Format(time.RFC3339),
|
||||
Confidence: confidence,
|
||||
IngesterVersion: b.Version,
|
||||
// A URL is the same knowledge however many times it is visited, and the
|
||||
// same URL recurs across browsers with different visit times — so dedup
|
||||
// on the URL, not on the visit timestamp (which otherwise re-appends
|
||||
// every tick).
|
||||
ContentHash: fp,
|
||||
Provenance: map[string]any{
|
||||
"url": url,
|
||||
"visit_count": visitCount,
|
||||
|
||||
Reference in New Issue
Block a user