chore: checkpoint uncommitted work before session store fix

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.
This commit is contained in:
2026-11-01 13:33:36 -08:00
parent bb852faa27
commit 0b8ac34aee
43 changed files with 6771 additions and 419 deletions
+120 -35
View File
@@ -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,