Compare commits

2 Commits

Author SHA1 Message Date
david f63792417a fix: dedupe compares bytes to characters, so every Unicode body appended a duplicate
build / image (push) Successful in 57s
build / test (push) Successful in 5m58s
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.
2026-11-08 14:53:24 -08:00
david 83604d8f8a feat: ingest BookOrbit highlights as chapter entries (#50)
build / test (push) Successful in 4m7s
build / image (push) Successful in 24s
feat: ingest BookOrbit highlights as chapter entries (#50)

Co-authored-by: David Gwilliam <dhgwilliam@gmail.com>
Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
2026-11-08 19:55:56 +00:00
2 changed files with 83 additions and 1 deletions
+5 -1
View File
@@ -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:
+78
View File
@@ -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)
}
}