Home / Architecture / Part 01
14 min read 2821 words Part 01 Formal Correctness
Normative Invariants:

Aequora Sync — Part 01

The executable registry now also includes Part 08 invariants through : live-hint loss safety, no direct replica/cursor mutation, authorized scope isolation, bounded slow-consumer memory, and leader-handoff catch-up without durable hint replay. Part 09 adds through : deterministic import identity, checkpoint-after-commit safety, verified cutover, baseline history safety, post-activation journal visibility, and durable quarantine accounting. Part 10 adds through : cursor/activation atomicity, one-boundary manifests, staging isolation, chunk retry idempotency, pending-intent preservation, and snapshot-plus-delta convergence. Part 12 adds through : equivalent captured inputs produce an equivalent plan, every nondeterministic input is explicit, replay cannot execute side effects, committed inputs cannot drift, and handler plus policy/config versions fail closed. Part 13 adds through : required audit atomicity, retry deduplication, truthful actor attribution, append-only correction, sensitive-value policy, explanation authorization, chain continuity, visible tamper failure, and accurate field provenance. Part 14 adds through : tombstone and journal-floor safety, legal-hold precedence, required-evidence preservation, all-surface completion, offboarding write fencing, restore reconciliation, resurrection prevention, and governed-copy coverage.

Formal Correctness, Invariants, Model Checking, and Deterministic Simulation Architecture

1. Purpose

Aequora is a distributed state machine operating across:

client storage
network
server runtime
authoritative storage
network
client reconciliation

Traditional unit and integration tests can demonstrate expected examples, but they cannot efficiently explore the enormous number of possible retries, crashes, duplicate deliveries, timeouts, transaction races, process restarts, and conflicting writes.

Aequora therefore needs a dedicated correctness verification architecture.

The objective is not to mathematically prove every line of implementation. It is to make the most important synchronization invariants:

explicit
machine-checkable
continuously tested
reproducible
adapter-independent

2. Verification Layers

Use five complementary layers:

L1 — Rust type-system invariants
L2 — deterministic unit/property tests
L3 — concurrent implementation tests
L4 — abstract distributed model checking
L5 — end-to-end fault simulation

No one layer replaces another.


3. Layer 1 — Type-System Invariants

Use type states to stop invalid transitions:

Incoming<O>
Authenticated<O>
Authorized<O>
Validated<O>
ConflictChecked<O>
Executable<O>
Committed<O>

Only Executable<O> may reach the executor.

Use newtypes for:

OperationId
EntityVersion
Cursor
Sequence
ScopeId
DeviceId
AuthorityEpoch

Avoid using raw primitive values where semantic confusion would be dangerous.


4. Durable State Types

Transient and durable states should be represented explicitly.

Example:

PendingOperation
InFlightOperation
CommittedOperation
RejectedOperation
ConflictOperation

Do not model durable state transitions as arbitrary strings.


5. Normative Invariant Registry

Maintain a stable registry of invariants.

Each invariant has:

stable ID
human description
model assertion
property-test reference
adapter-test reference
production metric/diagnostic where applicable

Example:


One OperationId produces at most one authoritative logical effect.

6. Minimum Core Invariants

— Idempotent Authority

same OperationId
→ at most one authoritative logical effect

— Local Intent Atomicity

committed local synchronizable mutation
↔ durable outbox intent

— Authoritative Publication Atomicity

accepted authoritative mutation
↔ journal event
↔ operation ledger result

— Cursor Safety

cursor N
⇒ every required event through N is durably applied locally

— Version Monotonicity

authoritative entity version never decreases

— No Unauthorized Commit

an unauthorized operation cannot become authoritative

— Retry Preservation

retry cannot create an additional logical effect

— Reconciliation Idempotency

reapplying the same authoritative event does not duplicate its logical effect

— Tombstone Safety

a stale update cannot silently resurrect a deleted entity

— Timeline Safety

a cursor from authority epoch A cannot be interpreted as valid in incompatible epoch B

7. Abstract State-Machine Model

Define a model independent of Axum, SQLx, Stoolap, PostgreSQL, and HTTP.

struct Model {
    server: ServerModel,
    clients: BTreeMap<ClientId, ClientModel>,
    network: NetworkModel,
}

The model represents semantic state rather than production implementation details.


8. Client Model

struct ClientModel {
    authoritative_state: CanonicalState,
    optimistic_state: CanonicalState,
    outbox: VecDeque<ModelOperation>,
    cursor: Cursor,
    applied_events: BTreeSet<EventId>,
    online: bool,
}

Additional modeled state can include:

conflicts
bootstrap generation
pending response
local crash state

9. Server Model

struct ServerModel {
    state: CanonicalState,
    applied_operations: BTreeMap<OperationId, OperationOutcome>,
    journal: Vec<ModelEvent>,
    next_sequence: u64,
    authority_epoch: u64,
}

10. Network Model

The model network must intentionally behave badly.

It may:

deliver
delay
duplicate
drop
reorder
disconnect

Do not assume FIFO ordering unless a protocol layer explicitly guarantees it.


11. Model Actions

State transitions include:

LocalMutate(client)
BuildBatch(client)
SendRequest(client)
DeliverRequest
ServerExecute
ServerCommit
DropResponse
DeliverResponse
ClientReconcile
ClientCrash
ClientRestart
ServerCrash
ServerRestart
NetworkDisconnect
NetworkReconnect

Each action must define precise preconditions and resulting state.


12. Exhaustive Interleaving Exploration

The model checker should explore sequences such as:

local mutation
send
server commit
response lost
client crash
client restart
retry

and:

client sends operation
client retries
both requests arrive
server processes concurrently

Every reachable state is checked against safety invariants.


13. Rust-Native Model Checking

Prefer a Rust-native abstract state model and model-checking implementation where practical.

The architecture should support a Stateright-style exhaustive search over small state spaces.

The model is intentionally small:

2 clients
1–2 entities
2–3 operations
bounded queue sizes
bounded sequence numbers

Small models are sufficient to uncover many protocol bugs.


14. Do Not Model the Entire Production Stack

Do not attempt to model-check:

TCP
SQL parsing
allocator internals
TLS
Axum routing

Model the semantic protocol:

operation accepted?
transaction committed?
response delivered?
cursor advanced?

15. State Canonicalization

Equivalent states should normalize to a stable representation before hashing.

Use this for:

visited-state detection
symmetry reduction
failure deduplication

16. Symmetry Reduction

Two identical clients can often be treated symmetrically.

Normalize states so equivalent permutations do not multiply the search space unnecessarily.


17. Deterministic State Hash

Each model state must generate a deterministic hash.

This supports:

visited set
failure identity
regression fixtures

18. Failure Trace Format

When an invariant fails, record:

model version
Aequora version
invariant ID
initial state
action sequence
state after each action
final violation

Example:

1. A.local_mutate(op1)
2. A.send(op1)
3. server.commit(op1)
4. network.drop(response)
5. A.retry(op1)
6. server.commit(op1)  <-- violation

19. Replayable Failure Fixtures

Store minimized traces in RON:

FailureTrace(
    model_version: 1,
    invariant: "",
    actions: [
        ...
    ],
)

Any discovered failure becomes a permanent regression test.


20. Property-Based State-Machine Testing

Model checking explores bounded exhaustive spaces.

Property testing explores larger randomized spaces.

Generate actions such as:

mutate
connect
disconnect
sync
crash
restart
duplicate
conflict
resync

Check invariants after every step.


21. Proptest Action Model

enum TestAction {
    Mutate(ClientId, ModelMutation),
    Connect(ClientId),
    Disconnect(ClientId),
    Sync(ClientId),
    CrashClient(ClientId),
    RestartClient(ClientId),
    CrashServer,
    RestartServer,
}

Proptest shrinking is valuable because it reduces a long failing scenario to the smallest reproducer.


22. Deterministic Random Seeds

Every randomized simulation must accept and report a seed.

A CI failure must be reproducible locally from that seed.


23. Loom Concurrency Tests

Use Loom for in-process concurrency primitives such as:

local coordinator lease
shared sync status
in-memory operation reservation
bounded queues
shutdown races
worker fencing

Do not use Loom as a replacement for distributed protocol model checking.


24. Real Database Concurrency Tests

Every authoritative adapter must run concurrent transaction scenarios:

same expected version
same OperationId
same unique business key
same worker lease
same snapshot generation

Expected semantic outcome:

one succeeds
others conflict/retry/observe duplicate

Never silent lost update.


25. Fault Injection

Adapters need test-only failpoints.

Examples:

before transaction begin
after local domain write
before outbox append
after outbox append
before journal append
after journal append
before operation ledger write
after operation ledger write
before commit
after commit
before cursor update
after authoritative apply

26. Failpoint Actions

A failpoint may simulate:

returned error
panic/unwind
task cancellation
connection loss
process termination

Production builds should not expose dangerous arbitrary failpoint controls.


27. Crash Semantics Matrix

Distinguish:

ordinary error
panic
async cancellation
client process death
server process death
DB connection death
server death after DB commit

Each has different recovery semantics and deserves explicit tests.


28. Cancellation Safety

A Tokio task can be cancelled at an .await boundary.

Verify:

uncommitted transaction rolls back
committed operation remains authoritative
client ambiguity resolves by retrying same OperationId

29. Deterministic Reference Store

TestKit should contain a slow but precise reference store.

It needs:

clear transaction semantics
complete introspection
deterministic ordering
failpoints

It becomes the semantic oracle for adapter comparison.


30. Differential Adapter Testing

Run the same scenario against:

ReferenceStore
Stoolap
SQLite
PostgreSQL
other adapters

Compare canonical outcomes.

This proves database adapters preserve Aequora semantics.


31. Linearization Point

Aequora does not claim global instantaneous consistency across offline clients.

But the authoritative linearization point for an accepted operation is:

authoritative DB commit

Tests should ensure results are compatible with that commit point.


32. Exactly-Once Terminology

Never claim exactly-once network delivery.

Correct terminology:

at-least-once delivery/retry
+
exactly-once logical authoritative effect by OperationId

Tests, docs, and telemetry must use the same semantics.


33. Convergence Property

Assuming:

no new writes
the network eventually works
the server remains available
retry eventually executes
no unresolved manual conflict remains

then all active clients should eventually converge to authoritative server state.

This is a liveness property.


34. Safety vs Liveness

Safety:

nothing bad happens

Examples:

no duplicate payment
no unauthorized commit
no premature cursor advancement

Liveness:

something good eventually happens

Examples:

pending operation eventually resolves
client eventually catches up

Keep these categories explicit.


35. Fairness Assumptions

Liveness requires assumptions.

Examples:

network eventually becomes available
server eventually responds
retry scheduler eventually runs

Document them instead of claiming impossible guarantees for permanently disconnected systems.


36. Conflict Verification

Create reusable scenario suites for:

same-field updates
different-field updates
delete vs update
append vs append
commutative operations
manual conflicts

Every built-in consistency/conflict profile should pass its own property suite.


37. Compaction Verification Hook

Future offline compaction must prove:

original operation sequence

and:

compacted operation sequence

are semantically equivalent under the compactor's documented preconditions.

This should be property-tested.


38. Snapshot Verification

Property:

install snapshot at cursor N
+
apply events after N

must equal the canonical state produced by replaying authoritative history to the same point.


39. Resync Verification

Scenario:

client has pending operations
cursor becomes invalid
client bootstraps
pending operations are preserved/rebased/retried

Invariant:

pending user intent is not silently lost

40. Authority Epoch Verification

Future authority failover needs:

epoch 5 primary
epoch 6 promoted primary

Invariant:

epoch 5 cannot make new authoritative commits after epoch 6 fencing becomes valid

This protects against split brain.


41. Security-State Invariants

Model or property-test:

tenant A cannot mutate tenant B
revoked device cannot create new accepted commits
unknown operation kind cannot execute
invalid schema cannot execute

42. Resource Invariants

Correctness also includes bounded resource behavior.

Examples:

batch operations <= configured maximum
dependency nodes <= maximum
decoded bytes <= maximum
retry queue bounded by persisted outbox

43. Protocol Fuzzing

Fuzz:

Postcard envelope
operation batches
capability negotiation
schema upcasters
dependency graph
snapshot manifests

Expected behavior:

no panic
no UB
no unbounded allocation
typed rejection

44. Fuzz Corpus

Maintain fixtures for:

current valid messages
old supported messages
truncated messages
unknown discriminants
maximum batch sizes
deep dependency graphs
snapshot manifests

45. Mutation Testing

For critical correctness modules, mutation testing can reveal weak test coverage.

Example:

If changing an equality check in OperationId deduplication does not break tests, the verification suite is insufficient.

Use this selectively on high-risk modules.


46. Invariant Coverage Matrix

Maintain a matrix:

Invariant | Model | Property | Loom | Adapter | E2E

Critical invariants should be covered by multiple independent techniques.


47. CI Verification Tiers

Pull Request CI

unit tests
small proptest suite
small model search
adapter smoke tests

Nightly

larger model bounds
large randomized simulations
fuzzing
fault injection
real DB concurrency tests

Release

full adapter compliance
migration matrix
long-running deterministic simulation
chaos suite
performance/correctness regression gates

48. Model Versioning

The abstract model is a specification artifact.

Version it.

If protocol semantics intentionally change, the model version should change too.


49. Prevent Specification Drift

Every meaningful behavior change should answer:

Does the abstract model change?
Does an invariant change?
Does protocol documentation change?
Does adapter compliance change?

This keeps architecture, implementation, and tests aligned.


50. Verification CLI

Future tooling:

aequora verify model
aequora verify adapter
aequora verify protocol
aequora verify trace <file>

This helps core maintainers and third-party adapter authors.


51. Failure Artifact Format

A failure bundle should contain:

Aequora version
model version
adapter versions
DB versions
seed
action trace
OperationIds
cursor state
authority epoch
invariant ID

No customer-sensitive payload is necessary.


52. Production Incident Replay

When a real incident happens, operators should be able to export a sanitized structural trace and replay an equivalent scenario in TestKit.

This bridges production forensics and correctness engineering.


53. Public Correctness Documentation

A universal synchronization library should publicly document:

invariants
adapter requirements
failure assumptions
verification methodology
known limitations

This makes the term "production-ready" meaningful.


crates/
├── aequora-invariants/
├── aequora-model/
├── aequora-testkit/
└── aequora-adapter-sdk/

Possible responsibilities:

aequora-invariants
    invariant IDs and normative descriptions

aequora-model
    abstract distributed model and model checker integration

aequora-testkit
    deterministic simulator, reference store, failpoints

aequora-adapter-sdk
    shared compliance suites

If crate count becomes excessive, aequora-invariants can remain a module within aequora-testkit, but the specification itself should remain explicit.


55. Completion Criteria

Part 01 is implemented when:

[x] invariant registry exists
[x] abstract client/server/network model exists
[x] idempotency model check exists
[x] cursor-safety model check exists
[x] lost-response model check exists
[x] duplicate-request model check exists
[x] two-client conflict model exists
[x] property-based state machine exists
[x] Loom covers local concurrency primitives
[x] adapter failpoints exist in test builds
[x] differential adapter tests exist
[x] failures emit replayable RON traces
[x] CI tiers execute verification automatically

Current implementation evidence

[x] invariant registry exists (`aequora-invariants`,  through )
[x] abstract client/server/network model exists (`aequora-model`)
[x] idempotency model check exists
[x] cursor-safety model check exists
[x] lost-response model check exists
[x] duplicate-request model check exists
[x] two-client conflict model exists
[x] property-based state machine exists
[x] Loom covers the client engine's single-flight synchronization reservation
[x] adapter failpoints exist in TestKit builds
[x] differential reference/Stoolap/PostgreSQL adapter outcome tests are explicit
[x] failures emit self-contained replayable RON traces
[x] pull-request and scheduled correctness CI tiers execute automatically

aequora verify model runs the bounded default search. aequora verify trace <failure.ron> validates the model version and initial-state hash, replays every action, and succeeds only when the recorded invariant violation is reproduced. The trace contains abstract structural values, not customer payloads.

The local differential test runs the same fixture through the reference and Stoolap stores and compares complete contract reports. The authority differential test runs the same commit through the reference and PostgreSQL stores and compares its logical acknowledgement; CI supplies a real PostgreSQL service. The local offline run compiles that test but does not claim its live result.

Part 01 is complete at the repository boundary. As with the named prerequisites, a skipped live database test is not current infrastructure evidence; the configured PostgreSQL CI job is the continuous real-adapter gate.

Part 11 extends the normative registry with through for operation/profile compatibility, adapter capability safety, append-only preservation, strong aggregate atomicity, derived-authority isolation, and fail-closed unknown operations. The profile crate and reusable testkit compliance gate provide their executable evidence.


56. Final Architecture

                    AEQUORA CORRECTNESS SYSTEM

               ┌─────────────────────────┐
               │ Normative Invariants    │
               └────────────┬────────────┘
                            │
          ┌─────────────────┼──────────────────┐
          ▼                 ▼                  ▼
   Abstract Model      Property Tests      Type System
          │                 │                  │
          ▼                 ▼                  ▼
   Model Checker      Random Simulation     Compile-Time
          │                 │                  │
          └────────────┬────┴────────────┬────┘
                       ▼                 ▼
                  Reference Store     Loom Tests
                       │                 │
                       └────────┬────────┘
                                ▼
                         Adapter Suites
                                │
                                ▼
                         Real DB/Network
                                │
                                ▼
                      Production Confidence

The correctness system should be treated as part of Aequora itself, not merely as a test folder.

A synchronization platform is only as trustworthy as its ability to demonstrate that difficult failure interleavings have been considered, explored, reproduced, and continuously checked.