Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/synapse/blob/main/docs/SCHEMA_AND_CONSISTENCY.md # synapse A component of the AienOS ecosystem. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `synapse / docs / SCHEMA_AND_CONSISTENCY.md` 106 lines ยท 9675 bytes Raw Blame History Rendered 1 # Synapse Changeset Schema, WAL Format, Consistency, and Garbage Collection 2 3 Tracking Issue: [aien-spark/aien-sovereign-core#515](https://github.com/aien-spark/aien-sovereign-core/issues/515) 4 5 ## 1. Canonical Changeset Envelope (`SYNAPSE_ENVELOPE_VERSION = 1`) 6 7 A `ChangesetEnvelope` represents one atomic, immutable agent proposal: 8 9 * `version: u16` (must be `1`) 10 * `repo_id: RepoId` (canonical ASCII repository identifier, e.g., `"aien-spark/aien-sovereign-core"`) 11 * `base_git_oid: GitOid` (20-byte Git commit OID on which the proposal is based) 12 * `parent_changesets: Vec` (sorted 32-byte parent changeset digests for stacked proposals) 13 * `author_did: String` (`did:key:z6Mk...` encoding the author's Ed25519 public key) 14 * `idempotency_key: IdempotencyKey` (32-byte client deduplication token) 15 * `created_at_ms: u64` 16 * `intent_summary: String` (bounded UTF-8 description of the change) 17 * `mutations: Vec` (strictly sorted by normalized repository-relative path, with no duplicate paths, no leading `/`, no `.` or `..` segments, and no `.git` path components): 18 * `path: String` 19 * `op: MutationOp`: 20 * `Upsert { mode: FileMode, blob_digest: BlobDigest, size_bytes: u64 }` (`FileMode`: `Regular644`, `Executable755`, `Symlink120000`) 21 * `Delete` 22 * `evidence_receipts: Vec` (bounded machine test/verification digests attached at submission) 23 * `signature: [u8; 64]` (Ed25519 signature by `author_did` over the canonical unsigned envelope bytes prefixed with `"SYNAPSE_CHANGESET_SIGN_V1\0"`) 24 25 ### 1.1 Domain-Separated Digests 26 27 * `BlobDigest`: `SHA-256("SYNAPSE_BLOB_V1\0" || u64_le(blob.len()) || blob_bytes)` 28 * `ChangesetId`: `SHA-256("SYNAPSE_CHANGESET_V1\0" || canonical_unsigned_envelope_bytes || signature_bytes)` 29 30 ## 2. Single-Writer WAL Format (`SYNAPSE_WAL_VERSION = 1`) 31 32 Each record in `wal.log` is framed with a fixed 84-byte little-endian header followed by `payload_len` bytes: 33 34 | Offset | Size | Field | Description | 35 | :--- | :--- | :--- | :--- | 36 | `0..4` | `4` | `magic` | Exact ASCII bytes `b"SWAL"` | 37 | `4..6` | `2` | `version` | `1u16` (little-endian) | 38 | `6` | `1` | `record_kind` | `1 = ChangesetIngest`, `2 = EvaluationAttached`, `3 = ReviewRecorded`, `4 = GitProjected`, `5 = CanonicalPromoted`, `6 = CheckpointMarker` | 39 | `7` | `1` | `flags` | Record codec: `0` = v1 JSON, `1` = binary canonical codec v2 (ADR 0003 A1); any other value is refused (`WalFlagsNonZero`) | 40 | `8..16` | `8` | `seq` | Monotonic `WalSeq` (`u64` little-endian, starting at `1`) | 41 | `16..48` | `32` | `prev_frame_sha256` | `[0u8; 32]` for `seq == 1` (or checkpoint anchor digest), else `frame_sha256` of `seq - 1` | 42 | `48..52` | `4` | `payload_len` | Byte length of payload (`u32` little-endian) | 43 | `52..56` | `4` | `payload_crc32c` | CRC32C checksum over `payload` bytes | 44 | `56..88` | `32` | `frame_sha256` | `SHA-256("SYNAPSE_WAL_FRAME_V1\0" || header[0..56] || payload)` | 45 | `88..88+N` | `N` | `payload` | Canonical serialized record payload | 46 47 *(Total header size is 88 bytes.)* 48 49 ### 2.1 Durability and `FsyncPolicy` 50 51 * `FsyncPolicy::EveryCommit` (default): 52 1. Write each new blob to `blobs//.tmp` (`O_CREAT | O_EXCL | O_WRONLY`), `fsync` temp file, atomic `rename` to `blobs//`, `fsync` shard directory. 53 2. Append the 88-byte WAL header + payload to `wal.log`, `fsync` `wal.log`. 54 3. Advance the in-memory `committed_seq` atomic watermark so readers can observe `seq`. 55 * `FsyncPolicy::BatchWindow { max_records }` / `NoSyncForTesting`: 56 Explicitly labeled as process-crash durable only (survives `SIGKILL` of the user-space process once kernel write buffers complete, but does not claim physical power-loss durability until `flush_sync()` completes). 57 58 ## 3. Consistency Model, Replay Ordering, and Idempotency 59 60 1. **Single-Writer Total Order**: 61 All state-mutating operations within a repository are serialized through the single writer and assigned a contiguous `WalSeq` (`1, 2, 3, ...`). 62 2. **Read-Your-Writes and Snapshot Isolation**: 63 Every mutation response returns `committed_seq: WalSeq`. Any read/query call (`get`, `tree`, `diff`, `history`, `conflicts`, `status`) accepts an optional `min_visible_seq: Option` and `snapshot_seq: Option`: 64 * If `min_visible_seq > current_committed_seq`, the query refuses with `RefusalCode::VisibilitySeqAhead` (never serving stale state silently). 65 * If `snapshot_seq` is specified, the query evaluates deterministically against the exact state as of `snapshot_seq`. 66 3. **Deterministic Replay**: 67 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`. 68 4. **Idempotent Ingest**: 69 Each `ChangesetEnvelope` includes a 32-byte `IdempotencyKey`. 70 * 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. 71 * If `(repo_id, idempotency_key)` is already committed with a **different** `ChangesetId`, `submit` fails closed with `RefusalCode::IdempotencyKeyConflict`. 72 73 ## 4. Retention, Garbage Collection, and Garbage-Root Ownership 74 75 Speculative agent changesets are high-volume and ephemeral unless promoted or pinned. 76 77 ### 4.1 Garbage Roots (`GarbageRoot`) 78 79 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: 80 1. **Canonical Root**: The changeset is part of a verified `RadicleCanonicalHead` lineage. 81 2. **Human Review Root**: The changeset is referenced by an open `GitHubPullRequestProjection`. 82 3. **Active Lease Root**: The changeset holds an unexpired evaluation or integration queue lease (`lease_expires_at_ms > now_ms`). 83 4. **Retention Window Root**: `now_ms.saturating_sub(changeset.created_at_ms) <= retention_window_ms`. 84 85 ### 4.2 Compaction and Sweep Procedure 86 87 During `checkpoint_and_compact(now_ms)`: 88 1. Compute the transitive closure of reachable `ChangesetId`s and `BlobDigest`s from all active `GarbageRoot`s. 89 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`. 90 3. Compact `wal.log` so that new frames chain cleanly from `(checkpoint_seq, last_frame_sha256)`. 91 4. Unlink any blob file in `blobs//` that is not in the retained `BlobDigest` closure and has no in-flight writer reservation. 92 93 ## 5. Deterministic Failure and Refusal Codes (`RefusalCode`) 94 95 Every error path returns a typed `RefusalCode`: 96 * 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` 97 * Capacity & Backpressure: `BackpressureOverCapacity`, `QuotaPayloadTooLarge`, `QuotaTooManyPaths`, `QuotaRateLimitExceeded`, `PaginationLimitExceeded` 98 * Schema & Path Validation: `EnvelopeVersionUnsupported`, `PathInvalid`, `PathDuplicate`, `MutationsUnsorted`, `BaseTreeNotFound`, `ParentChangesetNotFound`, `ChangesetNotFound`, `VisibilitySeqAhead`, `IdempotencyKeyConflict` 99 * Auth & Security: `AuthSignatureInvalid`, `AuthGrantSignatureInvalid`, `AuthGrantExpired`, `AuthCapabilityDenied`, `AuthCrossRepoDenied`, `AuthReplayDetected`, `AuthSelfReviewRefused` 100 * Git & Radicle Canonicality: `GitObjectCorrupt`, `GitObjectNotRepresentable`, `GitRefNonFastForward`, `RadicleIdentityInvalid`, `RadicleSignatureInvalid`, `RadicleQuorumInsufficient`, `StaleBaseHead`, `SplitBrainDivergence` 101 102 ## 6. Schema and Storage Upgrades 103 104 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. 105 106 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`).