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:
version: u16(must be1)repo_id: RepoId(canonical ASCII repository identifier, e.g.,"aien-spark/aien-sovereign-core")base_git_oid: GitOid(20-byte Git commit OID on which the proposal is based)parent_changesets: Vec<ChangesetId>(sorted 32-byte parent changeset digests for stacked proposals)author_did: String(did:key:z6Mk...encoding the author's Ed25519 public key)idempotency_key: IdempotencyKey(32-byte client deduplication token)created_at_ms: u64intent_summary: String(bounded UTF-8 description of the change)mutations: Vec<PathMutation>(strictly sorted by normalized repository-relative path, with no duplicate paths, no leading/, no.or..segments, and no.gitpath components):path: Stringop: MutationOp:Upsert { mode: FileMode, blob_digest: BlobDigest, size_bytes: u64 }(FileMode:Regular644,Executable755,Symlink120000)Delete
evidence_receipts: Vec<EvidenceAttachment>(bounded machine test/verification digests attached at submission)signature: [u8; 64](Ed25519 signature byauthor_didover the canonical unsigned envelope bytes prefixed with"SYNAPSE_CHANGESET_SIGN_V1\0")
1.1 Domain-Separated Digests
BlobDigest:SHA-256("SYNAPSE_BLOB_V1\0" || u64_le(blob.len()) || blob_bytes)ChangesetId:SHA-256("SYNAPSE_CHANGESET_V1\0" || canonical_unsigned_envelope_bytes || signature_bytes)
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:
| Offset | Size | Field | Description |
|---|---|---|---|
0..4 | 4 | magic | Exact ASCII bytes b"SWAL" |
4..6 | 2 | version | 1u16 (little-endian) |
6 | 1 | record_kind | 1 = ChangesetIngest, 2 = EvaluationAttached, 3 = ReviewRecorded, 4 = GitProjected, 5 = CanonicalPromoted, 6 = CheckpointMarker |
7 | 1 | flags | Record codec: 0 = v1 JSON, 1 = binary canonical codec v2 (ADR 0003 A1); any other value is refused (WalFlagsNonZero) |
8..16 | 8 | seq | Monotonic WalSeq (u64 little-endian, starting at 1) |
16..48 | 32 | prev_frame_sha256 | [0u8; 32] for seq == 1 (or checkpoint anchor digest), else frame_sha256 of seq - 1 |
48..52 | 4 | payload_len | Byte length of payload (u32 little-endian) |
52..56 | 4 | payload_crc32c | CRC32C checksum over payload bytes |
56..88 | 32 | frame_sha256 | SHA-256("SYNAPSE_WAL_FRAME_V1\0" || header[0..56] || payload) |
88..88+N | N | payload | Canonical serialized record payload |
(Total header size is 88 bytes.)
2.1 Durability and FsyncPolicy
FsyncPolicy::EveryCommit(default):- Write each new blob to
blobs/<aa>/<digest>.tmp(O_CREAT | O_EXCL | O_WRONLY),fsynctemp file, atomicrenametoblobs/<aa>/<digest>,fsyncshard directory. - Append the 88-byte WAL header + payload to
wal.log,fsyncwal.log. - Advance the in-memory
committed_seqatomic watermark so readers can observeseq.
- Write each new blob to
FsyncPolicy::BatchWindow { max_records }/NoSyncForTesting: Explicitly labeled as process-crash durable only (survivesSIGKILLof the user-space process once kernel write buffers complete, but does not claim physical power-loss durability untilflush_sync()completes).
3. Consistency Model, Replay Ordering, and Idempotency
- 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, ...). - 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 optionalmin_visible_seq: Option<WalSeq>andsnapshot_seq: Option<WalSeq>:- If
min_visible_seq > current_committed_seq, the query refuses withRefusalCode::VisibilitySeqAhead(never serving stale state silently). - If
snapshot_seqis specified, the query evaluates deterministically against the exact state as ofsnapshot_seq.
- If
- Deterministic Replay:
Replaying a checkpoint at
checkpoint_seqfollowed by WAL framescheckpoint_seq + 1 ..= committed_seqproduces the exact samestate_root_digestacross restarts. Any gap, duplicate sequence, or hash-chain mismatch refuses recovery withWalSequenceGaporWalChainMismatch. - Idempotent Ingest:
Each
ChangesetEnvelopeincludes a 32-byteIdempotencyKey.- If
(repo_id, idempotency_key)is already committed with the sameChangesetId,submitsucceeds idempotently withIngestStatus::IdempotentDuplicate { wal_seq, changeset_id }without appending a second WAL frame. - If
(repo_id, idempotency_key)is already committed with a differentChangesetId,submitfails closed withRefusalCode::IdempotencyKeyConflict.
- If
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:
- Canonical Root: The changeset is part of a verified
RadicleCanonicalHeadlineage. - Human Review Root: The changeset is referenced by an open
GitHubPullRequestProjection. - Active Lease Root: The changeset holds an unexpired evaluation or integration queue lease (
lease_expires_at_ms > now_ms). - 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):
- Compute the transitive closure of reachable
ChangesetIds andBlobDigests from all activeGarbageRoots. - Write
checkpoint.v1.tmpcontaining the committedcheckpoint_seq,last_frame_sha256, the retained changeset manifests, idempotency table, projections, and canonical heads;fsyncand atomically rename tocheckpoint.v1. - Compact
wal.logso that new frames chain cleanly from(checkpoint_seq, last_frame_sha256). - Unlink any blob file in
blobs/<aa>/<digest>that is not in the retainedBlobDigestclosure and has no in-flight writer reservation.
5. Deterministic Failure and Refusal Codes (RefusalCode)
Every error path returns a typed RefusalCode:
- Storage & Integrity:
BlobDigestMismatch,BlobImmutableConflict,WalMagicInvalid,WalVersionUnsupported,WalCrcMismatch,WalChainMismatch,WalSequenceGap,WalTornFrame,WalRecordMalformed(a binary v2 record that fails canonical decoding),WireFrameMalformed(a binary wire frame that fails canonical decoding, step G1),WireProtocolViolation(a well-formed wire frame out of have-want order, step G1),WireRingViolation(a shared-memory ring peer broke the ring contract: index out of range, regressed or misaligned, a length over the cap, a torn record or a wrong sequence number, step G3),CheckpointCorrupt,IoFailure - Capacity & Backpressure:
BackpressureOverCapacity,QuotaPayloadTooLarge,QuotaTooManyPaths,QuotaRateLimitExceeded,PaginationLimitExceeded - Schema & Path Validation:
EnvelopeVersionUnsupported,PathInvalid,PathDuplicate,MutationsUnsorted,BaseTreeNotFound,ParentChangesetNotFound,ChangesetNotFound,VisibilitySeqAhead,IdempotencyKeyConflict - Auth & Security:
AuthSignatureInvalid,AuthGrantSignatureInvalid,AuthGrantExpired,AuthCapabilityDenied,AuthCrossRepoDenied,AuthReplayDetected,AuthSelfReviewRefused - Git & Radicle Canonicality:
GitObjectCorrupt,GitObjectNotRepresentable,GitRefNonFastForward,RadicleIdentityInvalid,RadicleSignatureInvalid,RadicleQuorumInsufficient,StaleBaseHead,SplitBrainDivergence
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).