Files
knox/internal/watch/reconcile.go
T
david 9f378946b9
build / image (push) Successful in 39s
build / test (push) Successful in 5m9s
feat: replicate thread membership and record merges (#41)
Threads replicated without their members: the replica could list a thread but
not summarise it. Membership was stored two node-local ways — thread_observations
by local observation id, provenance_links by ThreadFP(int id) — so neither could
cross a node boundary.

The replicated fact is the entry-level edge (thread uid, entry fingerprint),
because the fingerprint is already stable across nodes; both local forms are
materialised from it on arrival. Unlinks replicate as tombstones, or the next
reconcile would resurrect the edge.

Merges now record superseded_by = successor uid and replicate both threads, so a
peer resolves the merge instead of restoring the absorbed thread.

One deadlock fixed along the way: MergeThreads resolved the successor uid inside
its transaction, and the pool is pinned to a single connection, so the query
waited on itself.
2026-11-05 01:09:03 -08:00

38 lines
1.3 KiB
Go

package watch
import (
"fmt"
"github.com/david/knox/internal/db"
)
// Reconcile rebuilds all derived state from the observation log: entries are
// reconstituted via deterministic SQL aggregation, then the auto-threader
// re-links/re-creates threads idempotently by cluster_key. Returns the threads
// created and observations linked.
func Reconcile(kdb *db.KnoxDB) (int, int, error) {
if _, _, err := kdb.RebuildEntriesFromObservations(); err != nil {
return 0, 0, fmt.Errorf("rebuild entries: %w", err)
}
threader := NewAutoThreader(kdb)
created, linked, err := threader.AutoThread()
if err != nil {
return 0, 0, fmt.Errorf("auto-thread: %w", err)
}
// Replicated threads are applied last: they carry authored titles and
// motivations, which must win over anything the heuristic just derived.
if applied, err := kdb.ProjectThreads(); err != nil {
return created, linked, fmt.Errorf("project threads: %w", err)
} else if applied > 0 {
created += applied
}
// Membership last: links resolve against threads, so they need both the
// derived and the replicated threads to exist first.
if applied, err := kdb.ProjectLinks(); err != nil {
return created, linked, fmt.Errorf("project links: %w", err)
} else {
linked += applied
}
return created, linked, nil
}