Files
knox/internal/watch/reconcile.go
T
david 00f277cf76
build / test (push) Successful in 3m28s
build / image (push) Successful in 19s
feat: replicate threads through the observation log (#45)
feat: replicate threads through the observation log (#45)

Co-authored-by: David Gwilliam <dhgwilliam@gmail.com>
Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
2026-11-05 08:47:39 +00:00

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
}