Files
knox/internal/noise/noise.go
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

146 lines
3.9 KiB
Go

// Package noise holds the shared denylist used to filter low-signal browser
// navigation and to deny/refocus knowledge threads seeded by it. The same URL
// patterns drive both, so a webapp that is not a knowledge signal also cannot
// spawn or pollute a thread.
package noise
import (
"os"
"strings"
)
// defaultPatterns are substrings of URLs that represent webapp/account
// navigation rather than knowledge signals — logins, mail/calendar/docs apps,
// password managers, OAuth flows, and knox's own UI.
var defaultPatterns = []string{
"opencode.ai",
"auth.opencode.ai",
// auth / OAuth / identity
"github.com/login/oauth/authorize",
"accounts.google.com",
"google.com/accounts",
"myaccount.google.com",
"login.microsoftonline.com",
"login.live.com",
"appleid.apple.com",
"okta.com",
"auth0.com",
"duosecurity.com",
"lastpass.com",
"1password.com",
"bitwarden.com",
"dashlane.com",
// Google workspace webapps
"mail.google.com",
"inbox.google.com",
"calendar.google.com",
"drive.google.com",
"docs.google.com",
"sheets.google.com",
"slides.google.com",
"meet.google.com",
// finance / account webapps
"privacy.com",
"secure.bankofamerica.com",
"bankofamerica.com",
"moneydesktop.com",
"beta-bridge.simplefin.org",
// assistant/chat tools
"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",
}
// genericLabels are second-level labels too common/legitimate to deny as a
// thread topic (e.g. "google" would suppress real Google/Gemini work).
var genericLabels = map[string]bool{
"google": true, "live": true, "apple": true, "opencode": true,
"github": true, "localhost": true, "googleusercontent": true,
}
var patterns = BuildPatterns()
// BuildPatterns resolves the defaults plus KNOX_BROWSER_DENY (comma-separated),
// lowercased. Exposed for tests.
func BuildPatterns() []string {
out := append([]string{}, defaultPatterns...)
for _, p := range strings.Split(os.Getenv("KNOX_BROWSER_DENY"), ",") {
if p = strings.ToLower(strings.TrimSpace(p)); p != "" {
out = append(out, p)
}
}
return out
}
// IsNoisyURL reports whether a URL is webapp/account navigation rather than a
// knowledge signal. Matching is case-insensitive substring.
func IsNoisyURL(url string) bool {
u := strings.ToLower(url)
for _, p := range patterns {
if strings.Contains(u, p) {
return true
}
}
return false
}
// DenyKeywords derives thread-topic deny keywords from the URL patterns (e.g.
// "privacy.com" -> "privacy", "host.simplefin.org" -> "simplefin"), skipping
// paths/ports/IPs and generic labels. KNOX_THREAD_DENY adds explicit extras.
func DenyKeywords() map[string]bool {
out := make(map[string]bool)
for _, p := range patterns {
p = strings.ToLower(strings.TrimSpace(p))
if p == "" || strings.ContainsAny(p, "/:") {
continue
}
labels := strings.Split(p, ".")
label := labels[0]
if len(labels) >= 2 {
label = labels[len(labels)-2] // strip the TLD
}
label = strings.Trim(label, "-")
if len(label) < 3 || genericLabels[label] || isNumeric(label) {
continue
}
out[label] = true
}
for _, e := range strings.Split(os.Getenv("KNOX_THREAD_DENY"), ",") {
if e = strings.ToLower(strings.TrimSpace(e)); e != "" {
out[e] = true
}
}
return out
}
// IsDeniedTopic reports whether a thread title/motivation/tags reference a
// denylisted webapp topic.
func IsDeniedTopic(text string) bool {
return MatchDeniedTopic(text) != ""
}
// MatchDeniedTopic returns the denylisted keyword a text references, or "" if
// none. Useful for explaining why a thread is flagged.
func MatchDeniedTopic(text string) string {
t := strings.ToLower(text)
for k := range DenyKeywords() {
if strings.Contains(t, k) {
return k
}
}
return ""
}
func isNumeric(s string) bool {
for _, r := range s {
if r < '0' || r > '9' {
return false
}
}
return len(s) > 0
}