feat: replicate threads through the observation log #45

Merged
david merged 2 commits from feat/thread-replication into main 2026-11-05 08:47:50 +00:00
Owner

Closes #41 (partially — links are a follow-up, see below).

The replica on dhg.lol had 8 threads against the laptop's 38. Not staleness: CreateThread writes no observation, so an authored thread existed on exactly one node, forever. 21 of the 38 were authored, 11 of them with no cluster_key at all.

Identity

threads gains uid (opaque, node-generated), hcl (logical clock) and superseded_by (schema only in this PR). Not the integer id — local, and the determinism audit already flagged autoincrement as non-portable. Not cluster_key — that covers auto-created clusters only.

Transport

One observation per write, riding the existing log:

source_id    "thread"
fingerprint  sha256("thread:" + uid)     # stable across edits
content      canonical JSON document
hcl          local clock

Emitted from CreateThreadCluster, UpdateThread, CloseThread. No second replication path, no new endpoint.

Merge

Reconcile projects the newest document per uid after the auto-threader, so authored titles beat derived ones. Identity resolution falls back to cluster_key for the case where two nodes derived the same cluster before hearing about each other, guarded by a last-write-wins comparison on hcl.

Two bugs the tests caught

  1. Local threads had hcl=0 — they never mirrored the clock, so every incoming document won, including older ones. Exactly backwards.
  2. Projection cleared cluster_key when a document omitted it, which broke the fallback that prevents a cluster arriving twice — producing a duplicate row.

Backfill

knox thread emit-all — assigns a uid and emits one observation per existing thread; idempotent.

Verification

  • go test ./... green, including: create emits exactly one observation; projection restores authored state after the table is wiped; older documents lose; a peer's document with a different uid but the same cluster_key merges instead of duplicating
  • dry run on a copy of the live index: 38 threads emitted, projected back after wiping the table

Not in this PR

thread_observations (membership) still doesn't replicate — a peer will receive threads without their members, which is arguably worse than nothing for knox_thread_text. That needs link events keyed by entry fingerprint rather than local observation id. Follow-up.

Known interaction with #42

A full wipe-and-rebuild yields 55 threads, not 38: re-derived entries change cluster_keys, so fresh auto-threads no longer join the projected ones. That is #42's non-determinism, now visible; projection itself restores exactly the 38.

Closes #41 (partially — links are a follow-up, see below). The replica on dhg.lol had **8** threads against the laptop's **38**. Not staleness: `CreateThread` writes no observation, so an authored thread existed on exactly one node, forever. 21 of the 38 were authored, 11 of them with no `cluster_key` at all. ## Identity `threads` gains `uid` (opaque, node-generated), `hcl` (logical clock) and `superseded_by` (schema only in this PR). Not the integer `id` — local, and the determinism audit already flagged autoincrement as non-portable. Not `cluster_key` — that covers auto-created clusters only. ## Transport One observation per write, riding the existing log: ``` source_id "thread" fingerprint sha256("thread:" + uid) # stable across edits content canonical JSON document hcl local clock ``` Emitted from `CreateThreadCluster`, `UpdateThread`, `CloseThread`. No second replication path, no new endpoint. ## Merge Reconcile projects the newest document per uid **after** the auto-threader, so authored titles beat derived ones. Identity resolution falls back to `cluster_key` for the case where two nodes derived the same cluster before hearing about each other, guarded by a last-write-wins comparison on `hcl`. ## Two bugs the tests caught 1. **Local threads had `hcl=0`** — they never mirrored the clock, so *every* incoming document won, including older ones. Exactly backwards. 2. **Projection cleared `cluster_key`** when a document omitted it, which broke the fallback that prevents a cluster arriving twice — producing a duplicate row. ## Backfill `knox thread emit-all` — assigns a uid and emits one observation per existing thread; idempotent. ## Verification - `go test ./...` green, including: create emits exactly one observation; projection restores authored state after the table is wiped; older documents lose; a peer's document with a different uid but the same `cluster_key` merges instead of duplicating - dry run on a copy of the live index: **38 threads emitted**, projected back after wiping the table ## Not in this PR `thread_observations` (membership) still doesn't replicate — a peer will receive threads *without* their members, which is arguably worse than nothing for `knox_thread_text`. That needs link events keyed by entry fingerprint rather than local observation id. Follow-up. ## Known interaction with #42 A full wipe-and-rebuild yields 55 threads, not 38: re-derived entries change `cluster_key`s, so fresh auto-threads no longer join the projected ones. That is #42's non-determinism, now visible; projection itself restores exactly the 38.
david added 2 commits 2026-11-05 08:47:28 +00:00
fix: keep the dates in log digests (#39)
build / test (push) Successful in 5m25s
build / image (push) Successful in 45s
01b2cafa7b
normalizeLogLine stripped timestamp= first, so a signature could not say
when it was seen: an ENOSPC from Oct 22 that was fixed the same day and a
provider billing rejection from Nov 1 rendered as identical-looking lines.
A digest is only useful if a resolved fault can be told apart from a live
one, and the date is the one volatile field that carries that meaning.

Signatures are now held as {text, first, last} and rendered with their day
('2026-10-22 ...') or range ('2026-10-01..2026-11-01 ...'), ordered most
recently seen first so the summary's err: sample leads with what is live
now instead of whatever sorts first alphabetically.

Times of day, run ids and session/message ids still normalize away, so the
content hash stays stable within a day and moves only when a recurring
error recurs on a new day. Version bumped to log-parser/v3.
feat: replicate threads through the observation log (#41)
build / image (push) Successful in 52s
build / test (push) Successful in 6m46s
49e39aac44
Threads are authored as well as derived, so a peer could never learn about one:
the title someone typed exists nowhere in the log. The replica had 8 threads
against the laptop's 38, and 21 of those were authored.

Identity is a node-generated uid, not the integer id (local, collides across
nodes) and not cluster_key (auto-created clusters only; 11 authored threads had
none). Every write emits one observation keyed by sha256('thread:'+uid), so
threads ride the existing log instead of adding a second replication mechanism.

Reconcile projects the newest document per uid, after the auto-threader, because
authored titles must win over derived ones.

Two bugs the tests caught:
- locally created threads never set threads.hcl, so the LWW guard let any
  incoming document win, including older ones;
- projection cleared cluster_key when a document carried none, which broke the
  fallback that stops the same cluster arriving twice (producing duplicates).

Adds 'knox thread emit-all' to backfill existing threads.

Links (thread membership) are not replicated yet: peers receive threads without
their members. Follow-up.
david merged commit 00f277cf76 into main 2026-11-05 08:47:50 +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#45