interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / docs / SCHEMA_AND_CONSISTENCY.md

106 lines · 9675 bytes Raw Blame History Source

Synapse Changeset Schema, WAL Format, Consistency, and Garbage Collection

Tracking Issue: aien-spark/aien-sovereign-core#515

1. Canonical Changeset Envelope (SYNAPSE_ENVELOPE_VERSION = 1)

A ChangesetEnvelope represents one atomic, immutable agent proposal:

1.1 Domain-Separated Digests

2. Single-Writer WAL Format (SYNAPSE_WAL_VERSION = 1)

Each record in wal.log is framed with a fixed 84-byte little-endian header followed by payload_len bytes:

OffsetSizeFieldDescription
0..44magicExact ASCII bytes b"SWAL"
4..62version1u16 (little-endian)
61record_kind1 = ChangesetIngest, 2 = EvaluationAttached, 3 = ReviewRecorded, 4 = GitProjected, 5 = CanonicalPromoted, 6 = CheckpointMarker
71flagsRecord codec: 0 = v1 JSON, 1 = binary canonical codec v2 (ADR 0003 A1); any other value is refused (WalFlagsNonZero)
8..168seqMonotonic WalSeq (u64 little-endian, starting at 1)
16..4832prev_frame_sha256[0u8; 32] for seq == 1 (or checkpoint anchor digest), else frame_sha256 of seq - 1
48..524payload_lenByte length of payload (u32 little-endian)
52..564payload_crc32cCRC32C checksum over payload bytes
56..8832frame_sha256SHA-256("SYNAPSE_WAL_FRAME_V1\0" || header[0..56] || payload)
88..88+NNpayloadCanonical serialized record payload

(Total header size is 88 bytes.)

2.1 Durability and FsyncPolicy

3. Consistency Model, Replay Ordering, and Idempotency

  1. Single-Writer Total Order: All state-mutating operations within a repository are serialized through the single writer and assigned a contiguous WalSeq (1, 2, 3, ...).
  2. Read-Your-Writes and Snapshot Isolation: Every mutation response returns committed_seq: WalSeq. Any read/query call (get, tree, diff, history, conflicts, status) accepts an optional min_visible_seq: Option<WalSeq> and snapshot_seq: Option<WalSeq>:
    • If min_visible_seq > current_committed_seq, the query refuses with RefusalCode::VisibilitySeqAhead (never serving stale state silently).
    • If snapshot_seq is specified, the query evaluates deterministically against the exact state as of snapshot_seq.
  3. Deterministic Replay: Replaying a checkpoint at checkpoint_seq followed by WAL frames checkpoint_seq + 1 ..= committed_seq produces the exact same state_root_digest across restarts. Any gap, duplicate sequence, or hash-chain mismatch refuses recovery with WalSequenceGap or WalChainMismatch.
  4. Idempotent Ingest: Each ChangesetEnvelope includes a 32-byte IdempotencyKey.
    • If (repo_id, idempotency_key) is already committed with the same ChangesetId, submit succeeds idempotently with IngestStatus::IdempotentDuplicate { wal_seq, changeset_id } without appending a second WAL frame.
    • If (repo_id, idempotency_key) is already committed with a different ChangesetId, submit fails closed with RefusalCode::IdempotencyKeyConflict.

4. Retention, Garbage Collection, and Garbage-Root Ownership

Speculative agent changesets are high-volume and ephemeral unless promoted or pinned.

4.1 Garbage Roots (GarbageRoot)

A ChangesetId (and all its ancestor parent_changesets and referenced BlobDigest objects) is protected from garbage collection if and only if at least one of the following holds:

  1. Canonical Root: The changeset is part of a verified RadicleCanonicalHead lineage.
  2. Human Review Root: The changeset is referenced by an open GitHubPullRequestProjection.
  3. Active Lease Root: The changeset holds an unexpired evaluation or integration queue lease (lease_expires_at_ms > now_ms).
  4. Retention Window Root: now_ms.saturating_sub(changeset.created_at_ms) <= retention_window_ms.

4.2 Compaction and Sweep Procedure

During checkpoint_and_compact(now_ms):

  1. Compute the transitive closure of reachable ChangesetIds and BlobDigests from all active GarbageRoots.
  2. Write checkpoint.v1.tmp containing the committed checkpoint_seq, last_frame_sha256, the retained changeset manifests, idempotency table, projections, and canonical heads; fsync and atomically rename to checkpoint.v1.
  3. Compact wal.log so that new frames chain cleanly from (checkpoint_seq, last_frame_sha256).
  4. Unlink any blob file in blobs/<aa>/<digest> that is not in the retained BlobDigest closure and has no in-flight writer reservation.

5. Deterministic Failure and Refusal Codes (RefusalCode)

Every error path returns a typed RefusalCode:

6. Schema and Storage Upgrades

Every persisted structure (ChangesetEnvelope, WAL frame header, CheckpointManifest, AuditEntry) starts with an explicit u16 format version (1). Readers refuse any version != 1 prior to deserializing variable-length fields (WalVersionUnsupported, EnvelopeVersionUnsupported). Future schema versions (v2) will be introduced via explicit migration checkpoints that verify the v1 state digest before writing a v2 checkpoint.

Record codec v2 (ADR 0003 section 9 step A1, docs/A1_BINARY_CODEC.md): WAL record payloads and the checkpoint move from JSON to one canonical binary encoding; the envelope, its signature and every identity are unchanged. A store with v1 history keeps writing JSON until its first checkpoint.v2; that migration checkpoint records the last v1 state_root_digest and chains its own root to it, and every later checkpoint chains to the previous root. checkpoint.v1 is then replaced by a version: 2 tombstone so binaries that only know v1 refuse the store. Fresh stores write binary from the first record. Replay refuses a JSON record after a binary one and a binary record before the migration (WalVersionUnsupported). A frame whose declared payload exceeds StoreLimits::max_wal_record_bytes is refused before anything is allocated for it (QuotaPayloadTooLarge).