fix: dedupe compares bytes to characters, so every Unicode body appended a duplicate #51

Merged
david merged 1 commits from fix/dedupe-byte-length into main 2026-11-08 22:54:00 +00:00
Owner

Found while verifying the BookOrbit ingester: a second sweep reported 0 new and still wrote 18 rows.

Bug

RecordObservation decided "unchanged" with

sameContent := int64(len(o.Content)) == pContentLen   // bytes in Go
// ... COALESCE(LENGTH(content), 0)                    // characters in SQLite

For TEXT, SQLite's LENGTH() counts characters. Any body containing an em dash, a curly quote or any non-ASCII text therefore never matched its stored length, so the hash check was skipped and a duplicate observation was appended — with an identical content hash, on every sweep.

The BookOrbit entry hit it immediately: 6511 characters vs 6578 bytes.

Blast radius

Rows vs distinct (fingerprint, content_hash) in the live index:

source rows distinct versions
obsidian 8520 1180
opencode-session 1686 1153
gitea 358 104
karakeep 329 195
skills-catalog 543 227

This is most of what #43 is about, and it was growing every ticker interval.

Fix

LENGTH(CAST(content AS BLOB)) — SQLite counts bytes, matching Go.

Tests

  • identical multibyte content records once, second call is a no-op, one row
  • a genuinely changed body still appends a version (guarding against fixing dedupe into "ignore everything")

Not done here

No cleanup of the existing duplicates — that is a destructive pass over the log and belongs with #43, where the numbers above are now recorded.

Found while verifying the BookOrbit ingester: a second sweep reported **0 new** and still wrote 18 rows. ## Bug `RecordObservation` decided "unchanged" with ```go sameContent := int64(len(o.Content)) == pContentLen // bytes in Go // ... COALESCE(LENGTH(content), 0) // characters in SQLite ``` For TEXT, SQLite's `LENGTH()` counts characters. Any body containing an em dash, a curly quote or any non-ASCII text therefore never matched its stored length, so the hash check was skipped and a duplicate observation was appended — with an **identical content hash**, on every sweep. The BookOrbit entry hit it immediately: 6511 characters vs 6578 bytes. ## Blast radius Rows vs distinct `(fingerprint, content_hash)` in the live index: | source | rows | distinct versions | |---|---|---| | obsidian | 8520 | 1180 | | opencode-session | 1686 | 1153 | | gitea | 358 | 104 | | karakeep | 329 | 195 | | skills-catalog | 543 | 227 | This is most of what #43 is about, and it was growing every ticker interval. ## Fix `LENGTH(CAST(content AS BLOB))` — SQLite counts bytes, matching Go. ## Tests - identical multibyte content records once, second call is a no-op, one row - a genuinely changed body still appends a version (guarding against fixing dedupe into "ignore everything") ## Not done here No cleanup of the existing duplicates — that is a destructive pass over the log and belongs with #43, where the numbers above are now recorded.
david added 1 commit 2026-11-08 22:53:46 +00:00
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
f63792417a
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.
david merged commit aa8bfa61e1 into main 2026-11-08 22:54:00 +00:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: david/knox#51