From f63792417ac83977275e4f24520f694072f81642 Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Sun, 8 Nov 2026 14:53:24 -0800 Subject: [PATCH] fix: dedupe compares bytes to characters, so every Unicode body appended a duplicate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RecordObservation decided "unchanged" by comparing len(o.Content) — bytes in Go — against SQLite's LENGTH(content), which counts characters for TEXT. Any body with an em dash or a curly quote therefore looked changed on every sweep and appended a second observation with an identical content hash. The log shows it: obsidian holds 8520 rows for 1180 distinct versions, gitea 358 for 104, karakeep 329 for 195. That is most of the duplication #43 is about, and it grows every tick. CAST the column to BLOB so SQLite counts bytes too. Regression tests cover both halves: identical multibyte content must dedupe, and a changed body must still append a version. --- internal/db/db.go | 6 +- internal/db/observation_dedupe_test.go | 78 ++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 internal/db/observation_dedupe_test.go diff --git a/internal/db/db.go b/internal/db/db.go index 0b50856..9ae1d15 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -360,9 +360,13 @@ func (k *KnoxDB) RecordObservation(o ObservationRecord) (obsID int64, isNew bool err = tx.QueryRow( `SELECT id, COALESCE(title,''), COALESCE(summary,''), COALESCE(project,''), COALESCE(created_at,''), COALESCE(line_start,0), COALESCE(line_end,0), - COALESCE(content_hash,''), COALESCE(LENGTH(content),0) + COALESCE(content_hash,''), COALESCE(LENGTH(CAST(content AS BLOB)),0) FROM observations WHERE fingerprint=? ORDER BY hcl DESC, id DESC LIMIT 1`, o.Fingerprint, ).Scan(&prevID, &pTitle, &pSummary, &pProject, &pCreated, &pStart, &pEnd, &pContent, &pContentLen) + // Byte length, not characters. len() counts bytes; LENGTH() on a TEXT value + // counts characters, so the two disagree for any body with an em dash or a + // curly quote — and every sweep then appends a duplicate of identical + // content. The CAST keeps SQLite on the same scale as Go. sameContent := int64(len(o.Content)) == pContentLen switch err { case nil: diff --git a/internal/db/observation_dedupe_test.go b/internal/db/observation_dedupe_test.go new file mode 100644 index 0000000..3f78d68 --- /dev/null +++ b/internal/db/observation_dedupe_test.go @@ -0,0 +1,78 @@ +package db_test + +import ( + "testing" + + "github.com/david/knox/internal/db" +) + +// TestRecordObservationDeduplicatesMultibyteContent pins the byte-vs-character +// bug: SQLite's LENGTH() counts characters on TEXT, while Go's len() counts +// bytes. Comparing the two made every body containing an em dash, a curly quote +// or any non-ASCII text look changed, so each sweep appended a duplicate +// observation with an identical content hash. +func TestRecordObservationDeduplicatesMultibyteContent(t *testing.T) { + kdb, raw := openBoth(t) + + rec := db.ObservationRecord{ + Fingerprint: "fp-multibyte", + SourceID: "test", + Title: "Typographic punctuation", + Summary: "em dash and curly quotes", + Content: "— “quoted” and an em dash — plus ünïcödé", + ContentHash: "hash-1", + IngesterVersion: "test/v1", + Trigger: "test", + } + + if _, isNew, err := kdb.RecordObservation(rec); err != nil || !isNew { + t.Fatalf("first record = (isNew=%v, err=%v), want a new observation", isNew, err) + } + if _, isNew, err := kdb.RecordObservation(rec); err != nil || isNew { + t.Fatalf("second record = (isNew=%v, err=%v), want a deduped no-op", isNew, err) + } + + var rows int + if err := raw.QueryRow( + `SELECT COUNT(*) FROM observations WHERE fingerprint='fp-multibyte'`, + ).Scan(&rows); err != nil { + t.Fatalf("count: %v", err) + } + if rows != 1 { + t.Fatalf("observations = %d, want 1 (identical multibyte content must not append a version)", rows) + } +} + +// TestRecordObservationAppendsOnRealChange is the other half of the contract: +// fixing the length comparison must not turn dedupe into "ignore everything". +func TestRecordObservationAppendsOnRealChange(t *testing.T) { + kdb, raw := openBoth(t) + + base := db.ObservationRecord{ + Fingerprint: "fp-change", + SourceID: "test", + Content: "first version — with a dash", + ContentHash: "hash-a", + IngesterVersion: "test/v1", + Trigger: "test", + } + if _, _, err := kdb.RecordObservation(base); err != nil { + t.Fatalf("first: %v", err) + } + + base.Content = "second version — with a longer dash" + base.ContentHash = "hash-b" + if _, isNew, err := kdb.RecordObservation(base); err != nil || isNew { + t.Fatalf("changed record = (isNew=%v, err=%v), want a new version, not a new fingerprint", isNew, err) + } + + var versions int + if err := raw.QueryRow( + `SELECT COUNT(*) FROM observations WHERE fingerprint='fp-change'`, + ).Scan(&versions); err != nil { + t.Fatalf("count: %v", err) + } + if versions != 2 { + t.Fatalf("versions = %d, want 2 (a changed body must still append)", versions) + } +}