Syncing Oracle — how it decides what to update
How the incremental RBSR index oracle works, and why it's seed-and-forward instead of reverse-edge queries.

Syncing Oracle

The problem it solves

RBSR reconciliation currently rebuilds the entire sorted blob set from SQLite on every round and every peer (loadStore → loadRBSRStore, backend/hmnet/syncing/). The maintained monoid tree replaces that rebuild with an O(log N) range-fingerprint lookup — but a tree is only useful if it's kept current. The oracle is what keeps it current: given a freshly-indexed blob, it answers which maintained scopes gain which blobs, so we patch only what changed instead of recomputing.

The key design choice: seed-and-forward, not reverse-edge queries

The oracle is the incremental inverse of collectBlobs (discovery.go:579), which builds a scope's set via a closure over many edge types. The naive inverse — hand-write a reverse query per edge — is exactly where correctness silently dies (miss one edge → a blob never propagates, no error).

Two facts make a safer design possible:


    The RBSR set is built with collectBlobs(..., includeLinksCitationsAccounts=false). That drops links, citations, comment-links and author-account profiles. The remaining set is: resource-anchored structural blobs ∪ inbound-Contact-by-subject ∪ the forward closures (change + media + capability-delegation).

    Those closures are forward walks from a seed set. So the only genuinely new logic the oracle needs is the seeding decision — which new blob enters which scope directly. The closure is then re-run from that seed using the same traversal collectBlobs already runs, so it can't drift.

So: affectedScopes(conn, blobID, scopes) resolves the blob's type + resource IRI, decides which candidate scopes it seeds (resource coverage + type filter), then runs the forward media closure from it.

What "seeds" and what doesn't


    Resource-anchored types seed: Ref, Capability, Comment, Profile, Contact carry a resource. A blob seeds scope S iff S's IRI pattern covers that resource (exact / recursive / depth-one, mirroring fillTables) and S's type filter allows it.

    Changes do not seed. Important gotcha: a Change is indexed with an empty resource (blob_change.go:378). It enters a scope only via the forward closure from the Ref that heads it. So when you edit a document, the new Ref seeds the scope and the new Changes come in through the closure.

The closure is the media closure (all link types)

The forward walk is blob_links along every link type, transitively, minus stashed blobs — identical to the "Fill media files" CTE in collectBlobs. This subsumes ref/head + change/dep, which is why a scope gains its Changes even when the type filter omits Change: the type filter constrains the seed, not the closure. (The differential test caught me getting exactly this wrong on the first pass.)

What it can't do forward — and how it stays correct

Two edges are not a forward link-walk from the blob, so the oracle reports complete=false for them and the caller must re-materialize that scope:


    Inbound Contact-by-subject — a Contact keyed on the subject account, not the contact's own resource.

    Capability delegation — a Capability pulled in by the delegate's authored blobs.

Non-structural / unanchored blobs arriving alone (a Raw/DagPB linked into a scope) are also flagged incomplete. The reported inserts are always sound (a subset of truth); complete=false just means "there may be more — re-materialize."

How we know it's right

The linchpin is a differential test (oracle_test.go): across scope shapes (exact / recursive / depth-one / child / type-filtered), the union of the oracle's per-blob decisions must equal collectBlobs exactly. collectBlobs is the ground truth; any seeding or closure error shows up as an inequality. In production the same idea becomes shadow-verify — periodically compare a maintained scope's fingerprint against a fresh collectBlobs materialization and re-materialize on mismatch. That's the backstop for any missed edge.

Files: backend/hmnet/syncing/oracle.go, oracle_test.go. Ground truth: collectBlobs in discovery.go.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime