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.
31 lines
1014 B
Go
31 lines
1014 B
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
|
|
}
|
|
return created, linked, nil
|
|
}
|