Expand description
§node-app-replica
Change-only replication of app data (Phase 3 of the one-stack design,
docs/superpowers/specs/2026-11-19-one-stack-phase-3-replication.md).
An app backend declares each collection once, as a Collection bound to
its pull capability (friends.people.pull), and tells it what changed —
either the whole current list (Collection::reconcile) or one row at a time
at its write sites (Collection::touch, Collection::remove). The helper
keeps a small ledger in the app’s own database (keys, digests, versions,
tombstones; never the rows) and from it answers the node’s pull with one of
three replies: nothing new, the changes since the caller’s version, or a full
copy under a new or the same epoch. The app writes no versioning code.
use node_app_replica::{Collection, Row};
use serde_json::json;
static PEOPLE: Collection = Collection::new("friends.people.pull", "friends_", &[1]);
PEOPLE.ensure(conn).map_err(|e| e.to_string())?;
PEOPLE
.reconcile(conn, &[Row::new("did:key:z6Mk…", json!({ "name": "Alice" }))])
.map_err(|e| e.to_string())?;
let reply = PEOPLE.pull(conn, request, |_conn, _keys| {
Ok(vec![Row::new("did:key:z6Mk…", json!({ "name": "Alice" }))])
})?;The crate depends on serde, serde_json and sha2 only (plus rusqlite
behind the sqlite feature) and not on node-app-sdk-rust, so an app pinned
to any SDK version can use it. Announcer is the one seam to the node’s
capability bus; implement it with the SDK’s invoke_capability.
Re-exports§
pub use store::CollectionRecord;pub use store::LedgerEntry;pub use store::LedgerStore;pub use store::LedgerTx;pub use store::MemoryLedger;pub use store::Pruned;pub use wire::Announce;pub use wire::AnnounceReply;pub use wire::HeadsData;pub use wire::PullReply;pub use wire::PullRequest;pub use wire::PushedHead;pub use wire::Row;pub use wire::SyncAnswer;pub use wire::SyncEntry;pub use wire::SyncErrorCode;pub use wire::SyncReply;pub use wire::SyncRequest;
Modules§
- digest
- Row digests (Phase 3 spec, section 5.1).
- store
- The storage seam: where the ledger lives.
- wire
- The replication contract’s wire shapes (Phase 3 spec, section 4), byte for byte.
Structs§
- Answered
- A pull’s answer and whether the pull moved the ledger’s head (a dirty reconcile, a write the app had not reported, or a rotation): when it did, the app announces, so other devices hear of it.
- Collection
- One replicated collection: its pull capability, the prefix of the ledger’s
tables, and the schema numbers it serves. Declare it as a
static. - Head
- The head a pull or an announce reports:
(epoch, version). - Horizon
- How long, and how many, tombstones are kept (section 4.7).
- System
Environment - The system clock and an 8-character random epoch.
Enums§
- Announce
Outcome - The result of an announce. An announce never fails the app’s own work, but
until one lands the node answers a device that holds the head it remembers
with “nothing new” without calling the app, so devices do not see the change.
The helper does not retry:
Collection::announceis one synchronous call, and the app retries aFailed(orRotationDeferred) announce on its own schedule (see the README’s “Announce”). - Replica
Error - Why a ledger operation or a pull failed.
Displaystarts with a stable prefix, so the text can go straight into the capability error channel.
Constants§
- ROTATION_
MIN_ INTERVAL_ MS - A rotation the app asks for (
reset(), or the pruning guard) within this long of the last one is owed, not done at once (section 4.6): a burst of requests becomes one rotation, so the devices take one full copy, not one per request. An owed rotation is never dropped. A rotation for evidence that the database went back in time is never held back. - TOMBSTONE_
MAX_ AGE_ MS - 30 days.
- TOMBSTONE_
MAX_ COUNT - Per collection.
Traits§
- Announcer
- The app’s door to the node’s capability bus, for
core.replica.announce. Implement it with the SDK’sinvoke_capability. - Environment
- Where the helper reads the time and draws new epochs.