Skip to main content

syncular_client/
client.rs

1//! The Syncular v2 Rust client core (SPEC.md client-behavior contract):
2//! rusqlite local storage, §3.2/§3.3 effective-scope persistence + purge,
3//! §4 pull/cursor/bootstrap (§4.7 resume, §5.6 segment application), §6
4//! push with outbox order, §7 optimistic apply / rollback / replay-on-top,
5//! §2.3 clientCommitId idempotency, §8 realtime client rules, §10 errors.
6//!
7//! Built from `SPEC.md` and the committed `ssp2` codec alone — no
8//! reference to the v1 Rust tree or the v2 TypeScript client.
9
10use crate::{ProgressObserver, ProgressPhase, ProgressState};
11use ssp2::encode_message;
12use std::borrow::Cow;
13#[cfg(test)]
14use std::cell::Cell;
15use std::cell::RefCell;
16use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
17use std::sync::Arc;
18
19use rusqlite::types::{ToSqlOutput, Value as SqlValue, ValueRef};
20use rusqlite::{Connection, OpenFlags, OptionalExtension};
21use serde::{Deserialize, Serialize};
22use serde_json::{json, Map, Value};
23use sha2::{Digest, Sha256};
24use ssp2::decode::WIRE_VERSION;
25use ssp2::model::{
26    FencedRecord, Frame, LiveStatus, MediaType, Message, MsgKind, Op, OpResult, PushStatus,
27    SubStatus,
28};
29use ssp2::primitives::RawJson;
30use ssp2::registration::canonical_scope_json;
31use ssp2::segment::{decode_rows_segment, Column, ColumnType, ColumnValue, Row, RowsSegment};
32use ssp2::{decode_message, encode_presence_publish, parse_control, ControlMessage, PresenceKind};
33
34use crate::api::{
35    AppliedServerCommit, AppliedServerCommitSource, ClientChangeBatch, ClientDiagnosticsHost,
36    ClientDiagnosticsLease, ClientDiagnosticsReplica, ClientDiagnosticsSchema,
37    ClientDiagnosticsSnapshot, ClientDiagnosticsStorage, ClientError, ClientLimits, CommandEffects,
38    CommitDelivery, CommitOperation, CommitOperationOutcome, CommitOutcome, CommitOutcomeQuery,
39    CommitOutcomeResolution, CommitOutcomeStatus, ConflictRecord, CoverageSnapshot,
40    CurrentBaseProof, CurrentBaseProofReason, DiagnosticLastChange, DiagnosticLastRound,
41    DiagnosticQueryFailure, DiagnosticRoundCounters, DiagnosticUnits, EchoRecord, FetchedBlob,
42    LeaseState, LocalDataPurgeInput, LocalDataPurgeResult, LocalDataPurgeTarget,
43    LocalDataRebootstrapInput, LocalDataRebootstrapResult, Mutation, PresencePeer,
44    PreviousVersionStatus, QueryOwner, QueryReadFailure, QueryRow, QuerySnapshot, QueryValue,
45    RejectionDetails, RejectionRecord, ResolveCommitOutcomeInput, RetainedCommitRow,
46    RetainedUniqueConflict, RowState, SchemaFloor, SnapshotRead, SyncIntent, SyncOutcome,
47    SyncReport, SyncStatusSnapshot, TableChange, UnitCatchup, UnitRecord, UnitScopes, UnitStatus,
48    WindowBase, WindowChange, WindowCoverage, WindowState, WindowUnitRef,
49    CLIENT_DIAGNOSTICS_VERSION, MAX_DIAGNOSTIC_QUERY_FAILURES,
50};
51use crate::api::{RealtimePolicy, RealtimeState, REALTIME_UNAVAILABLE_CODE};
52#[cfg(feature = "bench-internals")]
53use crate::bench::{Phase, Recorder};
54use crate::previous_version::{
55    capture_previous_version_from_replica, first_incompatibility,
56    reconcile_previous_version_at_boot, set_local_schema_descriptor,
57    sweep_previous_version_container, PendingCommitAudit, PreviousVersionContextConfig,
58    PreviousVersionLifecycle, PreviousVersionReadSpec, PreviousVersionSnapshot,
59};
60use crate::schema::{parse_schema_json, ClientSchema, FtsIndexSchema, TableSchema};
61use crate::transport::{BlobDownload, SegmentRequest, Transport, TransportError};
62use crate::values::{
63    bytes_to_hex, column_value_to_json, decode_row_bytes, encode_sparse_row_json, full_row_values,
64    is_canonical_bytes_envelope, json_to_column_value, json_to_scope_map, normalize_values_casing,
65    render_row_id_json, scope_map_to_json, SealedRow,
66};
67
68/// §4.2 default: rows and SQLite images, both applied in committed chunks.
69/// Bit 3 (signed URLs, §5.4) is
70/// added per transport capability at request-build time.
71const DEFAULT_ACCEPT: u8 = 0b0111;
72const ACCEPT_INLINE_ROWS: u8 = 1 << 0;
73const ACCEPT_EXTERNAL_ROWS: u8 = 1 << 1;
74const ACCEPT_SQLITE: u8 = 1 << 2;
75const ACCEPT_SIGNED_URLS: u8 = 1 << 3;
76const MAX_DIAGNOSTIC_DOMAINS: usize = 256;
77
78/// §10.1 `recommendedAction` carried by a server HTTP error body.
79fn recommended_action_of(details: &Option<Value>) -> Option<String> {
80    details
81        .as_ref()?
82        .get("recommendedAction")?
83        .as_str()
84        .map(str::to_owned)
85}
86
87/// §7.4.1 persisted local schema-version marker (`_syncular_meta` key).
88pub(crate) const LOCAL_SCHEMA_VERSION_KEY: &str = "localSchemaVersion";
89const LOCAL_REVISION_KEY: &str = "localRevision";
90/// §4.5 replica cursor; absent means `-1`.
91const REPLICA_CURSOR_KEY: &str = "replicaCursor";
92/// Hex of the last pull `LIVE_END.echoDigest` (§4.2); absent means empty.
93const ECHO_DIGEST_KEY: &str = "echoDigest";
94/// Hex of the registration digest of the list the server stores (§4.1).
95const REGISTERED_DIGEST_KEY: &str = "registrationDigest";
96/// §7.4.3: table name → generation, persisted with the declaration evidence.
97const TABLE_GENERATIONS_KEY: &str = "localTableGenerations";
98/// §7.1: the identity of the outbox sequence.
99const OUTBOX_ID_KEY: &str = "outboxId";
100const CLIENT_ID_KEY: &str = "clientId";
101const LEASE_STATE_KEY: &str = "leaseState";
102const SCHEMA_FLOOR_KEY: &str = "schemaFloor";
103pub(crate) const LOG_EPOCH_KEY: &str = "logEpoch";
104/// Persisted fail-closed quarantine marker: present while a security preflight
105/// is pending and `activateSecurity` has yet to run. Storing it in the replica
106/// keeps the gate with the data it protects, so a rebuilt host handle reopening
107/// the same file returns in preflight even though its in-memory state is fresh.
108const SECURITY_PREFLIGHT_PENDING_KEY: &str = "securityPreflightPending";
109const LOCAL_REBOOTSTRAP_RECEIPT_VERSION: u8 = 2;
110const MAX_JS_SAFE_INTEGER: u64 = 9_007_199_254_740_991;
111/// §7.4.4 client-local code: a pending outbox commit cannot re-encode under
112/// the new schema after a bump. Never a wire code (§10.3).
113const OUTBOX_INCOMPATIBLE_CODE: &str = "sync.outbox_incompatible";
114/// §5.11 client-local code: an encode at the push seam resolved no usable key
115/// id or named an unknown key. Never a wire code (§10.3).
116const ENCRYPT_FAILED_CODE: &str = "client.encrypt_failed";
117const INVALID_REQUEST_CODE: &str = "sync.invalid_request:";
118/// §7.4.4: static text for a persisted commit the current codec refuses.
119const LEGACY_VALUE_REFUSAL_MESSAGE: &str =
120    "the persisted commit carries values the current codec refuses";
121/// §7.4.4: bounded reason on every intrinsic value drop.
122const INVALID_STORED_VALUES_REASON: &str = "invalid_stored_values";
123
124#[derive(Debug, Serialize, Deserialize)]
125#[serde(rename_all = "camelCase", deny_unknown_fields)]
126struct PersistedLocalDataRebootstrapReceipt {
127    version: u8,
128    retained_commits: u64,
129    reset_subscriptions: u64,
130}
131
132fn invalid_local_rebootstrap_receipt() -> String {
133    "sync.local_corrupt: persisted local rebootstrap receipt is invalid".to_owned()
134}
135
136fn encode_local_rebootstrap_receipt(
137    retained_commits: usize,
138    reset_subscriptions: usize,
139) -> Result<String, String> {
140    let retained_commits =
141        u64::try_from(retained_commits).map_err(|_| invalid_local_rebootstrap_receipt())?;
142    let reset_subscriptions =
143        u64::try_from(reset_subscriptions).map_err(|_| invalid_local_rebootstrap_receipt())?;
144    if retained_commits > MAX_JS_SAFE_INTEGER || reset_subscriptions > MAX_JS_SAFE_INTEGER {
145        return Err(invalid_local_rebootstrap_receipt());
146    }
147    serde_json::to_string(&PersistedLocalDataRebootstrapReceipt {
148        version: LOCAL_REBOOTSTRAP_RECEIPT_VERSION,
149        retained_commits,
150        reset_subscriptions,
151    })
152    .map_err(|_| invalid_local_rebootstrap_receipt())
153}
154
155fn decode_local_rebootstrap_receipt(value: &str) -> Result<(usize, usize), String> {
156    // Pre-0.15.36 markers proved application but did not retain the receipt.
157    if value == "v1" {
158        return Ok((0, 0));
159    }
160    let receipt: PersistedLocalDataRebootstrapReceipt =
161        serde_json::from_str(value).map_err(|_| invalid_local_rebootstrap_receipt())?;
162    if receipt.version != LOCAL_REBOOTSTRAP_RECEIPT_VERSION
163        || receipt.retained_commits > MAX_JS_SAFE_INTEGER
164        || receipt.reset_subscriptions > MAX_JS_SAFE_INTEGER
165    {
166        return Err(invalid_local_rebootstrap_receipt());
167    }
168    Ok((
169        usize::try_from(receipt.retained_commits)
170            .map_err(|_| invalid_local_rebootstrap_receipt())?,
171        usize::try_from(receipt.reset_subscriptions)
172            .map_err(|_| invalid_local_rebootstrap_receipt())?,
173    ))
174}
175
176#[cfg(test)]
177mod observation_tests;
178
179/// Internal state for hosts that perform network I/O outside the client owner.
180#[doc(hidden)]
181pub struct CurrentBasePreparation {
182    client_commit_id: String,
183    sequence: i64,
184    units: Vec<UnitRecord>,
185    transport_epoch: u64,
186    registration_epoch: u64,
187    security_epoch: u64,
188}
189
190#[cfg(test)]
191mod units_tests;
192
193#[derive(Debug, Clone)]
194struct OutboxOp {
195    upsert: bool,
196    table: String,
197    row_id: String,
198    base_version: Option<i64>,
199    /// Schema-agnostic local form (§0): driver JSON values, encoded with
200    /// the current codec at send time. A key's presence IS the §6.1 sparse
201    /// presence set — both the overlay and the wire payload derive from it.
202    values: Option<Map<String, Value>>,
203    /// §7.1 insert intent: `mutate` recorded this upsert while the row was
204    /// absent from the visible replica. Replay and send-time encoding present
205    /// every current-schema column; a column `values` lacks is NULL.
206    insert: bool,
207}
208
209impl OutboxOp {
210    /// The values this upsert presents under the current `table` (§7.1). An
211    /// insert-intent operation presents every column, a missing one as NULL;
212    /// `None` when a missing column is non-nullable, which leaves the commit
213    /// unencodable (§7.4.4), or when the operation carries no values.
214    fn presented_values(&self, table: &TableSchema) -> Option<Cow<'_, Map<String, Value>>> {
215        let values = self.values.as_ref()?;
216        if !self.insert || table.columns.iter().all(|c| values.contains_key(&c.name)) {
217            return Some(Cow::Borrowed(values));
218        }
219        full_row_values(table, values.clone()).ok().map(Cow::Owned)
220    }
221}
222
223impl From<CommitOperation> for OutboxOp {
224    fn from(operation: CommitOperation) -> Self {
225        Self {
226            upsert: operation.op == "upsert",
227            table: operation.table,
228            row_id: operation.row_id,
229            base_version: operation.base_version,
230            values: operation.values,
231            insert: operation.insert,
232        }
233    }
234}
235
236/// §7.5 static authoring refusal; the table and column travel in details.
237fn invalid_authoring(message: &str, details: Value) -> ClientError {
238    ClientError {
239        code: "sync.invalid_request".to_owned(),
240        message: message.to_owned(),
241        details: Some(details),
242        retryable: false,
243    }
244}
245
246/// §7.1 chained base: `later` repeats `earlier`'s positive base on the same
247/// row and presents only columns `earlier` presents.
248fn chains_on(earlier: &OutboxOp, later: &OutboxOp) -> bool {
249    // Only an upsert carries values, and it always carries its primary key.
250    earlier.row_id == later.row_id
251        && earlier.table == later.table
252        && later.base_version.is_some_and(|base| base > 0)
253        && earlier.base_version == later.base_version
254        && later.values.as_ref().is_some_and(|values| {
255            values.keys().all(|column| {
256                earlier
257                    .values
258                    .as_ref()
259                    .is_some_and(|earlier| earlier.contains_key(column))
260            })
261        })
262}
263
264impl From<&OutboxOp> for CommitOperation {
265    fn from(operation: &OutboxOp) -> Self {
266        Self {
267            table: operation.table.clone(),
268            row_id: operation.row_id.clone(),
269            op: if operation.upsert { "upsert" } else { "delete" }.to_owned(),
270            base_version: operation.base_version,
271            values: operation.values.clone(),
272            insert: operation.insert,
273        }
274    }
275}
276
277#[derive(Debug, Clone)]
278struct OutboxCommit {
279    client_commit_id: String,
280    ops: Vec<OutboxOp>,
281}
282
283/// One commit's `(table, value keys, insert intent)` per upsert operation — the classification
284/// shape the §7.4.4 send-time drop and the RFC 0005 D6 audit share, so the two
285/// can never disagree about which commits cannot re-encode.
286fn commit_audit_operations(commit: &OutboxCommit) -> Vec<(&str, Vec<&str>, bool)> {
287    commit
288        .ops
289        .iter()
290        .filter(|operation| operation.upsert)
291        .map(|operation| {
292            (
293                operation.table.as_str(),
294                operation
295                    .values
296                    .as_ref()
297                    .map(|values| values.keys().map(String::as_str).collect())
298                    .unwrap_or_default(),
299                operation.insert,
300            )
301        })
302        .collect()
303}
304
305type FailedCommit = (OutboxCommit, Map<String, Value>);
306
307#[derive(Debug, Clone)]
308struct CompiledLocalDataPurgeTarget {
309    table: String,
310    selectors: Vec<(String, Vec<String>)>,
311}
312
313struct StoredCommitOutcomeRow {
314    sequence: i64,
315    client_commit_id: String,
316    status: String,
317    recorded_at_ms: i64,
318    results_json: String,
319    operations_json: Option<String>,
320    resolution: String,
321    resolved_at_ms: Option<i64>,
322    replacement_client_commit_id: Option<String>,
323}
324
325impl StoredCommitOutcomeRow {
326    fn from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<Self> {
327        Ok(Self {
328            sequence: row.get(0)?,
329            client_commit_id: row.get(1)?,
330            status: row.get(2)?,
331            recorded_at_ms: row.get(3)?,
332            results_json: row.get(4)?,
333            operations_json: row.get(5)?,
334            resolution: row.get(6)?,
335            resolved_at_ms: row.get(7)?,
336            replacement_client_commit_id: row.get(8)?,
337        })
338    }
339}
340
341/// Section outcome distinguishing the §5.6 unit-local fail-closed
342/// path from a round-aborting failure (§1.4 rule 5).
343/// §4.4 a unit section's `SUB_START`.
344struct SectionStart {
345    id: String,
346    status: SubStatus,
347    reason_code: String,
348    bootstrap: bool,
349    fenced: Vec<FencedRecord>,
350}
351
352enum SectionError {
353    FailClosed,
354    /// §5.5: the server refused a segment with `sync.forbidden`.
355    Refused,
356    /// §5.3 client memory: the round deferred this image's body to a later
357    /// pull of the same resume token.
358    Deferred,
359    Abort(String, String),
360    Transport(TransportError),
361}
362
363#[derive(Debug)]
364pub(crate) struct RequestMeta {
365    pushed_ids: Vec<String>,
366    /// Hex of the registration digest this pull carried; the server stores
367    /// that list once the round completes (§4.1).
368    registration_digest: String,
369    pub(crate) accept: u8,
370    /// §6.1 splitBatch: outbox commits held back from THIS request because
371    /// the running operation count reached the push cap — the next round
372    /// pushes them (`sync_needed` stays set while any remain).
373    deferred_commits: usize,
374    /// §5.6 applied-segment record per unit: segment id → rows applied at
375    /// the unit's current cursor and token.
376    pub(crate) applied: BTreeMap<String, BTreeMap<String, i64>>,
377    /// §5.3 client memory: (unit, segment id) of the first image whose body
378    /// the round did not fetch because the batch already holds two images.
379    /// That unit's section stops there without persisting `SUB_END`.
380    pub(crate) deferred: BTreeSet<(String, String)>,
381}
382
383#[derive(Default)]
384struct ChangeAccumulator {
385    /// None means table-wide; Some is the exact scoped domain.
386    tables: BTreeMap<String, Option<BTreeSet<String>>>,
387    windows: BTreeMap<String, BTreeSet<String>>,
388    status: bool,
389    conflicts: bool,
390    rejections: bool,
391    outcomes: bool,
392}
393
394impl ChangeAccumulator {
395    fn table(&mut self, table: &str) {
396        self.tables.insert(table.to_owned(), None);
397    }
398
399    fn scope(&mut self, table: &str, key: String) {
400        match self.tables.get_mut(table) {
401            Some(None) => {}
402            Some(Some(keys)) => {
403                keys.insert(key);
404            }
405            None => {
406                self.tables
407                    .insert(table.to_owned(), Some(BTreeSet::from([key])));
408            }
409        }
410    }
411
412    fn window(&mut self, base_key: &str, value: &str) {
413        self.windows
414            .entry(base_key.to_owned())
415            .or_default()
416            .insert(value.to_owned());
417    }
418
419    fn touched(&self) -> bool {
420        !self.tables.is_empty()
421            || !self.windows.is_empty()
422            || self.status
423            || self.conflicts
424            || self.rejections
425            || self.outcomes
426    }
427}
428
429/// §6.1: the server caps total operations per request (reference default
430/// 500) and rejects the whole batch with `sync.too_many_operations`; the
431/// client "splits and retries". Splitting happens at build time: commits are
432/// included IN ORDER until the operation budget is spent, the rest wait for
433/// the next round.
434const PUSH_OPS_PER_REQUEST: usize = 500;
435const MAX_LOCAL_PURGE_TARGETS: usize = 64;
436const MAX_LOCAL_PURGE_SELECTORS: usize = 8;
437const MAX_LOCAL_PURGE_VALUES: usize = 128;
438const MAX_LOCAL_PURGE_VALUE_LENGTH: usize = 256;
439pub const SECURITY_PREFLIGHT_REQUIRED_CODE: &str = "client.security_preflight_required";
440
441/// Dirty visible tables; `None` means a whole-replica recovery/reset.
442struct OverlayDirty(RefCell<Option<BTreeSet<String>>>);
443
444impl OverlayDirty {
445    fn clean() -> Self {
446        Self(RefCell::new(Some(BTreeSet::new())))
447    }
448    fn get(&self) -> bool {
449        self.0
450            .borrow()
451            .as_ref()
452            .is_none_or(|tables| !tables.is_empty())
453    }
454    fn set(&self, dirty: bool) {
455        *self.0.borrow_mut() = if dirty { None } else { Some(BTreeSet::new()) };
456    }
457    fn table(&self, table: &str) {
458        if let Some(tables) = self.0.borrow_mut().as_mut() {
459            tables.insert(table.to_owned());
460        }
461    }
462    fn snapshot(&self) -> Option<BTreeSet<String>> {
463        self.0.borrow().clone()
464    }
465    fn restore(&self, tables: Option<BTreeSet<String>>) {
466        *self.0.borrow_mut() = tables;
467    }
468}
469
470/// A copy-on-write in-memory mirror: a [`Rollback`] shares it, and the first
471/// write after a capture copies it once.
472#[derive(Debug, Clone, Default)]
473struct Shared<T>(Arc<T>);
474
475impl<T> std::ops::Deref for Shared<T> {
476    type Target = T;
477    fn deref(&self) -> &T {
478        &self.0
479    }
480}
481
482impl<T: Clone> std::ops::DerefMut for Shared<T> {
483    fn deref_mut(&mut self) -> &mut T {
484        Arc::make_mut(&mut self.0)
485    }
486}
487
488impl<T> From<T> for Shared<T> {
489    fn from(value: T) -> Self {
490        Self(Arc::new(value))
491    }
492}
493
494/// The in-memory mirrors of durable state, captured before a local
495/// transaction and restored when it rolls back. Queues only grow inside a
496/// transaction, so their lengths suffice.
497#[derive(Debug, Clone)]
498struct Rollback {
499    outbox: Shared<Vec<Arc<OutboxCommit>>>,
500    failed_commits: Shared<Vec<FailedCommit>>,
501    conflicts: Shared<Vec<ConflictRecord>>,
502    rejections: Shared<Vec<RejectionRecord>>,
503    schema_floor: Option<SchemaFloor>,
504    lease_state: Option<LeaseState>,
505    stopped: bool,
506    upgrading: bool,
507    sync_needed: bool,
508    active_round: Option<uuid::Uuid>,
509    overlay_dirty: Option<BTreeSet<String>>,
510    change_queue: usize,
511    sync_intent_queue: usize,
512    last_change: Option<DiagnosticLastChange>,
513}
514
515pub struct SyncClient {
516    #[cfg(feature = "bench-internals")]
517    benchmark_phases: Recorder,
518    conn: Connection,
519    schema: ClientSchema,
520    client_id: String,
521    limits: ClientLimits,
522    outbox: Shared<Vec<Arc<OutboxCommit>>>,
523    failed_commits: Shared<Vec<FailedCommit>>,
524    retain_failed_commits: bool,
525    conflicts: Shared<Vec<ConflictRecord>>,
526    rejections: Shared<Vec<RejectionRecord>>,
527    schema_floor: Option<SchemaFloor>,
528    /// §7.3.5: the opaque auth-lease state (from LEASE frames + lease errors).
529    lease_state: Option<LeaseState>,
530    /// §1.6: the schema-floor response stops syncing until an upgrade.
531    stopped: bool,
532    /// §7.4.5: true while a schema-bump reset + first re-bootstrap is in flight.
533    upgrading: bool,
534    /// §8.4 coalesced sync-needed signal.
535    sync_needed: bool,
536    /// §8.8: how sync rounds treat the realtime binding, and its exact
537    /// availability state with the evidence behind a lost or refused one.
538    realtime_policy: RealtimePolicy,
539    realtime_state: RealtimeState,
540    realtime_reason_code: Option<String>,
541    realtime_retry_delay_ms: Option<u64>,
542    /// §8.6 presence: scopeKey → (`actorId clientId` peer key → peer).
543    presence: HashMap<String, HashMap<String, PresencePeer>>,
544    /// Client clock (epoch ms) for the §5.4 `urlExpiresAtMs` check and the
545    /// §7.8 drive deadline; the host may pin it and move the pinned value
546    /// (conformance runs on a virtual clock).
547    now_ms: Option<std::sync::Arc<std::sync::atomic::AtomicI64>>,
548    /// §5.11 client-side encryption keys (`keyId → key bytes`). Empty ⇒ E2EE
549    /// off. The encrypt/decrypt seam (`values.rs`) is compiled only under the
550    /// `e2ee` feature; without it, a schema with encrypted columns fails loud.
551    encryption: crate::values::EncryptionConfig,
552    /// Fail-closed host bootstrap gate. While set, command hosts permit only
553    /// status/lifecycle inspection and an exact authorized local purge.
554    security_preflight: bool,
555    security_epoch: u64,
556    transport_enabled: bool,
557    transport_epoch: u64,
558    /// Raised by every registration change; a current-base proof spanning
559    /// one reports missing coverage.
560    registration_epoch: u64,
561    /// RFC 0005 D2/D8: the previous-version capture config. `None` (default)
562    /// means the feature is off; the descriptor write and the container orphan
563    /// sweep still happen.
564    previous_version: Option<PreviousVersionContextConfig>,
565    /// Per-table primary-key upsert SQL, built once per (full table name) —
566    /// the row write path runs per row during bootstrap (§5.6), so the SQL
567    /// string (and, via `prepare_cached`, its compiled statement) is reused
568    /// instead of being rebuilt and re-prepared per row. Cleared on a §7.4.3
569    /// schema reset (the column lists may have changed).
570    insert_sql: RefCell<HashMap<String, String>>,
571    /// §7.1: reconcile only tables whose base/outbox changed. Whole-replica
572    /// reset and restart explicitly invalidate every table.
573    overlay_dirty: OverlayDirty,
574    // First SQLite failure of the current operation, before savepoint cleanup.
575    storage_failure: RefCell<Option<QueryReadFailure>>,
576    /// Test-only structural performance signal: response processing must not
577    /// turn a batch of acknowledgements into one full overlay rebuild each.
578    #[cfg(test)]
579    overlay_rebuild_count: Cell<usize>,
580    #[cfg(test)]
581    acknowledged_replay_count: Cell<usize>,
582    /// Test-only structural performance signal: outcome retention is enforced
583    /// once per response rather than once per acknowledgement.
584    #[cfg(test)]
585    outcome_prune_count: Cell<usize>,
586    /// Test-only pinned duration of every §5.3 image chunk, in ms.
587    #[cfg(test)]
588    image_chunk_ms: Cell<Option<f64>>,
589    /// Exact observer-transaction output drained by command/FFI hosts.
590    progress: ProgressObserver,
591    change_queue: VecDeque<ClientChangeBatch>,
592    sync_intent_queue: VecDeque<SyncIntent>,
593    /// Explicit exponential retry policy for transient transport failures.
594    retry_delay_ms: u64,
595    /// §7.7: the background retry the current round scheduled, if any.
596    round_retry_delay_ms: Option<u64>,
597    /// §5.3: a table's next image chunk size, carried from one range image
598    /// of the table to the next.
599    image_chunk_rows: HashMap<String, u32>,
600    /// A round whose response never completed (§1.5): the client keeps its
601    /// code and reports it on every following request until a round receives a
602    /// complete, decodable response. The server already recorded the round, so
603    /// nothing else can tell it the round failed on this side.
604    round_failure: Option<String>,
605    active_round: Option<uuid::Uuid>,
606    last_round: Option<DiagnosticLastRound>,
607    last_change: Option<DiagnosticLastChange>,
608    /// §7.6: latest failed owned snapshot read per owner id, oldest first.
609    query_failures: Vec<DiagnosticQueryFailure>,
610    /// SPEC §1.4 rule 4: the open coalesced apply transaction of a response.
611    coalesced: Option<CoalescedApply>,
612    /// A coalesced apply transaction of the current response failed; every
613    /// later observation of that response fails until it ends.
614    coalesced_failure: Option<String>,
615}
616
617/// SPEC §1.4 rule 4: the frames and blocks of one response share one local
618/// write transaction of at most `COALESCED_APPLY_ROWS` rows. The in-memory
619/// state captured at its `BEGIN` is restored when its `COMMIT` fails.
620struct CoalescedApply {
621    rows: usize,
622    rollback: Rollback,
623}
624
625const COALESCED_APPLY_ROWS: usize = 1024;
626
627pub(crate) fn quote_ident(name: &str) -> String {
628    format!("\"{}\"", name.replace('"', "\"\""))
629}
630/// §7.4.1 `_syncular_meta` helpers. Free functions so RFC 0005's
631/// previous-version module reads and writes the SAME records as the client
632/// instead of growing a parallel meta layer.
633pub(crate) fn meta_get(conn: &Connection, key: &str) -> rusqlite::Result<Option<String>> {
634    conn.query_row(
635        "SELECT value FROM _syncular_meta WHERE key = ?1",
636        rusqlite::params![key],
637        |row| row.get::<_, String>(0),
638    )
639    .optional()
640}
641
642pub(crate) fn meta_set(conn: &Connection, key: &str, value: &str) -> rusqlite::Result<()> {
643    conn.execute(
644        "INSERT OR REPLACE INTO _syncular_meta (key, value) VALUES (?1, ?2)",
645        rusqlite::params![key, value],
646    )
647    .map(|_| ())
648}
649
650pub(crate) fn meta_delete(conn: &Connection, key: &str) -> rusqlite::Result<()> {
651    conn.execute(
652        "DELETE FROM _syncular_meta WHERE key = ?1",
653        rusqlite::params![key],
654    )
655    .map(|_| ())
656}
657
658/// Seek by the existing key while retaining the exact legacy text match.
659fn row_id_predicate(table: &TableSchema) -> String {
660    let key = quote_ident(&table.primary_key);
661    let text_match = format!("CAST({key} AS TEXT) = ?1");
662    match table.columns[table.pk_index].ty {
663        ColumnType::String | ColumnType::Json => {
664            format!("{key} = ?1 AND {text_match}")
665        }
666        // Native columns have no declared SQLite affinity. Match the stored
667        // integer bind type explicitly. Unary + removes expression affinity so
668        // SQLite can seek the typeless key; the text check preserves exact IDs.
669        ColumnType::Integer | ColumnType::Boolean => {
670            format!("{key} = +CAST(?1 AS INTEGER) AND {text_match}")
671        }
672        // REAL-to-text conversion can round distinct values to the same text.
673        // Narrowing those matches by numeric equality would change behavior.
674        _ => text_match,
675    }
676}
677
678/// §3.3/§4.8/§5.6 row ownership: rows whose local scope columns match
679/// `effective` and that no entry of `owners` matches. `owners` holds the
680/// pair scopes of every owner of the table (§3.3 row ownership); a scoped delete never removes a row one of them still covers.
681/// Scope values bind by the column's declared type, as `row_id_predicate`
682/// does. `Ok(None)` matches nothing (an empty map or value list); `Err(())`
683/// is a missing local scope-column mapping (the fail-closed case).
684fn local_scope_predicate(
685    table: &TableSchema,
686    effective: &[(String, Vec<String>)],
687    owners: &[Vec<(String, Vec<String>)>],
688) -> Result<Option<(String, Vec<SqlValue>)>, ()> {
689    if effective.is_empty() || effective.iter().any(|(_, values)| values.is_empty()) {
690        return Ok(None);
691    }
692    let column = |variable: &str| {
693        let name = table.scope_column(variable).ok_or(())?;
694        let column = table
695            .columns
696            .iter()
697            .find(|column| column.name == name)
698            .ok_or(())?;
699        Ok((quote_ident(name), column.ty == ColumnType::Integer))
700    };
701    let mut clauses = Vec::new();
702    let mut params = Vec::new();
703    for (variable, values) in effective {
704        let (key, integer) = column(variable)?;
705        let holes = vec!["?"; values.len()].join(", ");
706        clauses.push(if integer {
707            params.extend(values.iter().cloned().map(SqlValue::Text));
708            let seek = vec!["+CAST(? AS INTEGER)"; values.len()].join(", ");
709            format!("{key} IN ({seek}) AND CAST({key} AS TEXT) IN ({holes})")
710        } else {
711            format!("{key} IN ({holes})")
712        });
713        params.extend(values.iter().cloned().map(SqlValue::Text));
714    }
715    // Owners bind as one JSON parameter per variable set, so the statement
716    // keeps its size at any owner count; owners of one variable bind the
717    // union of their values. An owner holding other values of a variable
718    // `effective` names owns none of its rows and drops out. Values compare
719    // as text, the exact semantics of the effective clauses.
720    let mut groups: BTreeMap<Vec<&str>, Vec<Vec<&[String]>>> = BTreeMap::new();
721    for owner in owners {
722        if owner.is_empty()
723            || owner.iter().any(|(variable, values)| {
724                values.is_empty()
725                    || effective.iter().any(|(name, wanted)| {
726                        name == variable && wanted.iter().all(|value| !values.contains(value))
727                    })
728            })
729        {
730            continue;
731        }
732        let mut owner: Vec<_> = owner.iter().collect();
733        owner.sort_by(|a, b| a.0.cmp(&b.0));
734        groups
735            .entry(
736                owner
737                    .iter()
738                    .map(|(variable, _)| variable.as_str())
739                    .collect(),
740            )
741            .or_default()
742            .push(owner.iter().map(|(_, values)| values.as_slice()).collect());
743    }
744    let within = |variable: &str, list: &str| {
745        let (key, integer) = column(variable)?;
746        let key = if integer {
747            format!("CAST({key} AS TEXT)")
748        } else {
749            key
750        };
751        Ok(format!("{key} IN (SELECT value FROM json_each({list}))"))
752    };
753    for (variables, tuples) in groups {
754        if let [variable] = variables[..] {
755            let values: BTreeSet<&String> = tuples.iter().flatten().copied().flatten().collect();
756            params.push(SqlValue::Text(
757                Value::from_iter(values.into_iter().cloned()).to_string(),
758            ));
759            clauses.push(format!("NOT {}", within(variable, "?")?));
760        } else {
761            params.push(SqlValue::Text(
762                serde_json::to_string(&tuples).expect("owner tuples serialize"),
763            ));
764            let matches = variables
765                .iter()
766                .enumerate()
767                .map(|(index, variable)| within(variable, &format!("o.value,'$[{index}]'")))
768                .collect::<Result<Vec<_>, ()>>()?;
769            clauses.push(format!(
770                "NOT EXISTS (SELECT 1 FROM json_each(?) o WHERE {})",
771                matches.join(" AND ")
772            ));
773        }
774    }
775    Ok(Some((clauses.join(" AND "), params)))
776}
777
778/// [`local_scope_predicate`] over decoded row values (retained rows).
779fn owns_local_row(
780    table: &TableSchema,
781    effective: &[(String, Vec<String>)],
782    owners: &[Vec<(String, Vec<String>)>],
783    values: &Map<String, Value>,
784) -> bool {
785    let matches = |scopes: &[(String, Vec<String>)]| {
786        !scopes.is_empty()
787            && scopes.iter().all(|(variable, allowed)| {
788                table
789                    .scope_column(variable)
790                    .and_then(|column| values.get(column))
791                    .filter(|value| !value.is_null())
792                    .is_some_and(|value| {
793                        allowed.contains(
794                            &value
795                                .as_str()
796                                .map_or_else(|| value.to_string(), str::to_owned),
797                        )
798                    })
799            })
800    };
801    matches(effective) && !owners.iter().any(|owner| matches(owner))
802}
803
804fn is_local_operation_code_like(value: &str) -> bool {
805    let bytes = value.as_bytes();
806    bytes.first().is_some_and(u8::is_ascii_alphanumeric)
807        && bytes
808            .iter()
809            .all(|byte| byte.is_ascii_alphanumeric() || matches!(*byte, b'.' | b'_' | b':' | b'-'))
810}
811
812fn base_table(name: &str) -> String {
813    quote_ident(&format!("_syncular_base_{name}"))
814}
815
816/// §4.8 deferred eviction record: (unit id, table, effective scope map).
817type PendingEvict = (String, String, Vec<(String, Vec<String>)>);
818
819/// §4.8: a stable key for a window base: variable + canonical fixed scopes.
820fn window_base_key(base: &WindowBase) -> String {
821    format!(
822        "{}\0{}",
823        base.variable,
824        ssp2::registration::canonical_unit_json(&scope_pairs(&base.fixed_scopes))
825    )
826}
827
828/// The unit of one window value: the fixed scopes plus the value.
829fn window_unit_scopes(base: &WindowBase, value: &str) -> UnitScopes {
830    let mut scopes = base.fixed_scopes.clone();
831    scopes.insert(base.variable.clone(), value.to_owned());
832    scopes
833}
834
835/// A unit's scope map as the codec and the digests take it.
836fn scope_pairs(scopes: &UnitScopes) -> Vec<(String, String)> {
837    let mut pairs: Vec<(String, String)> = scopes
838        .iter()
839        .map(|(variable, value)| (variable.clone(), value.clone()))
840        .collect();
841    pairs.sort_by(|a, b| a.0.encode_utf16().cmp(b.0.encode_utf16()));
842    pairs
843}
844
845/// §4.1: the unit covers every table declaring all of its variables.
846fn covers(table: &TableSchema, scopes: &UnitScopes) -> bool {
847    scopes
848        .keys()
849        .all(|variable| table.scope_column(variable).is_some())
850}
851
852/// §4.4: the effective scopes of the (unit, table) pair under `echo`: the
853/// scope map as one-element lists plus the fenced lists. `None` when the
854/// fence revoked the pair.
855fn pair_scopes(
856    scopes: &UnitScopes,
857    table: &str,
858    echo: Option<&[EchoRecord]>,
859) -> Option<Vec<(String, Vec<String>)>> {
860    let fenced = echo.and_then(|echo| echo.iter().find(|record| record.table == table));
861    if fenced.is_some_and(|record| record.status == "revoked") {
862        return None;
863    }
864    let mut effective: BTreeMap<String, Vec<String>> = fenced
865        .map(|record| record.scopes.clone())
866        .unwrap_or_default();
867    for (variable, value) in scopes {
868        effective.insert(variable.clone(), vec![value.clone()]);
869    }
870    Some(effective.into_iter().collect())
871}
872
873/// §3.3 row ownership: the pair scopes of every owner of `table` except
874/// `except_id`. An owner is active with a persisted echo and live or
875/// post-bootstrap, or bootstrapping with a segment of the table in its
876/// current bootstrap.
877fn unit_owners(
878    units: &[UnitRecord],
879    table: &TableSchema,
880    except_id: &str,
881) -> Vec<Vec<(String, Vec<String>)>> {
882    units
883        .iter()
884        .filter(|unit| {
885            unit.id != except_id
886                && unit.status == UnitStatus::Active
887                && unit.echo.is_some()
888                && (!unit.bootstrapping() || unit.seeded.contains(&table.name))
889                && covers(table, &unit.scopes)
890        })
891        .filter_map(|unit| pair_scopes(&unit.scopes, &table.name, unit.echo.as_deref()))
892        .collect()
893}
894
895/// §5.5 / §5.6: the unit's scope map, each value a one-element list.
896fn unit_scope_lists(scopes: &UnitScopes) -> Vec<(String, Vec<String>)> {
897    scopes
898        .iter()
899        .map(|(variable, value)| (variable.clone(), vec![value.clone()]))
900        .collect()
901}
902
903/// §7.1 acknowledged intent of the rows `?1` names (`[[table, id], ...]`).
904/// It joins through the (tbl, id) index: a scan per acknowledgement would
905/// make draining a batch quadratic.
906const ACKED_ROWS_BY_ROW: &str = "SELECT a.op_json,a.intent_json FROM json_each(?1) k JOIN _syncular_acked_rows a ON a.tbl=json_extract(k.value,'$[0]') AND a.id=json_extract(k.value,'$[1]') WHERE ?2 IS NULL OR a.tbl IN(SELECT value FROM json_each(?2)) ORDER BY a.commit_seq,a.idx";
907
908const UNIT_COLUMNS: &str =
909    "id, scopes, explicit, status, reason_code, cursor, bootstrap_state, seeded, echo";
910
911/// A unit row; a field that does not parse is a conversion failure, which
912/// both read paths classify as `sync.local_corrupt`.
913fn unit_from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<UnitRecord> {
914    fn json<T: serde::de::DeserializeOwned>(index: usize, raw: &str) -> rusqlite::Result<T> {
915        serde_json::from_str(raw).map_err(|error| {
916            rusqlite::Error::FromSqlConversionFailure(
917                index,
918                rusqlite::types::Type::Text,
919                Box::new(error),
920            )
921        })
922    }
923    Ok(UnitRecord {
924        id: row.get(0)?,
925        scopes: json(1, &row.get::<_, String>(1)?)?,
926        explicit: row.get::<_, i64>(2)? == 1,
927        status: json(
928            3,
929            &serde_json::to_string(&row.get::<_, String>(3)?).expect("text"),
930        )?,
931        reason_code: row.get(4)?,
932        cursor: row.get(5)?,
933        bootstrap_state: row.get(6)?,
934        seeded: json(7, &row.get::<_, String>(7)?)?,
935        echo: row
936            .get::<_, Option<String>>(8)?
937            .map(|echo| json::<Vec<EchoRecord>>(8, &echo))
938            .transpose()?
939            .map(|echo| {
940                if echo
941                    .iter()
942                    .all(|record| record.status == "active" || record.status == "revoked")
943                {
944                    Ok(echo)
945                } else {
946                    Err(rusqlite::Error::FromSqlConversionFailure(
947                        8,
948                        rusqlite::types::Type::Text,
949                        "echo status is neither active nor revoked".into(),
950                    ))
951                }
952            })
953            .transpose()?,
954    })
955}
956
957/// Every unit in registration order (§4.1).
958fn load_units(conn: &Connection) -> rusqlite::Result<Vec<UnitRecord>> {
959    conn.prepare_cached(&format!(
960        "SELECT {UNIT_COLUMNS} FROM _syncular_units ORDER BY rowid"
961    ))?
962    .query_map([], unit_from_row)?
963    .collect()
964}
965
966fn get_unit(conn: &Connection, id: &str) -> rusqlite::Result<Option<UnitRecord>> {
967    conn.query_row(
968        &format!("SELECT {UNIT_COLUMNS} FROM _syncular_units WHERE id = ?1"),
969        [id],
970        unit_from_row,
971    )
972    .optional()
973}
974
975/// Insert or update in place: an update keeps the registration order. The
976/// §5.6 applied-segment record ends when the cursor or the token changes.
977fn save_unit(conn: &Connection, unit: &UnitRecord) -> rusqlite::Result<()> {
978    conn.prepare_cached(&format!(
979        "INSERT INTO _syncular_units({UNIT_COLUMNS}) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9)
980         ON CONFLICT(id) DO UPDATE SET explicit=excluded.explicit, status=excluded.status,
981           reason_code=excluded.reason_code, cursor=excluded.cursor,
982           bootstrap_state=excluded.bootstrap_state, seeded=excluded.seeded, echo=excluded.echo,
983           applied=CASE WHEN cursor IS excluded.cursor
984             AND bootstrap_state IS excluded.bootstrap_state THEN applied END"
985    ))?
986    .execute(rusqlite::params![
987        unit.id,
988        serde_json::to_string(&unit.scopes).expect("scope map serializes"),
989        i64::from(unit.explicit),
990        unit.status.name(),
991        unit.reason_code,
992        unit.cursor,
993        unit.bootstrap_state,
994        serde_json::to_string(&unit.seeded).expect("table list serializes"),
995        unit.echo
996            .as_ref()
997            .map(|echo| serde_json::to_string(echo).expect("echo serializes")),
998    ])
999    .map(|_| ())
1000}
1001
1002/// §5.6 applied-segment record of unit `id`: segment id → rows applied at
1003/// its current cursor and token.
1004fn applied_rows(conn: &Connection, id: &str) -> rusqlite::Result<BTreeMap<String, i64>> {
1005    conn.query_row(
1006        "SELECT applied FROM _syncular_units WHERE id = ?1",
1007        [id],
1008        |row| row.get::<_, Option<String>>(0),
1009    )
1010    .optional()?
1011    .flatten()
1012    .map_or(Ok(BTreeMap::new()), |raw| {
1013        serde_json::from_str(&raw).map_err(|error| {
1014            rusqlite::Error::FromSqlConversionFailure(
1015                0,
1016                rusqlite::types::Type::Text,
1017                Box::new(error),
1018            )
1019        })
1020    })
1021}
1022
1023/// §5.6: `rows` rows of `segment_id` applied at unit `id`'s position. Runs
1024/// in the transaction that commits them.
1025fn record_applied_rows(
1026    conn: &Connection,
1027    id: &str,
1028    segment_id: &str,
1029    rows: i64,
1030) -> rusqlite::Result<()> {
1031    conn.prepare_cached(
1032        "UPDATE _syncular_units SET applied=json_patch(coalesce(applied,'{}'),?1) WHERE id=?2",
1033    )?
1034    .execute(rusqlite::params![
1035        serde_json::json!({ segment_id: rows }).to_string(),
1036        id
1037    ])
1038    .map(|_| ())
1039}
1040
1041/// §4.1 references: every window registry entry `(base, value)` holding a unit.
1042fn window_refs(conn: &Connection, unit_id: &str) -> rusqlite::Result<Vec<(String, String)>> {
1043    conn.prepare_cached("SELECT base, unit FROM _syncular_windows WHERE sub_id = ?1")?
1044        .query_map([unit_id], |row| Ok((row.get(0)?, row.get(1)?)))?
1045        .collect()
1046}
1047
1048/// §7.4.3 reset, §2.1 log reset and local rebootstrap: keep the unit
1049/// registrations and discard the synced state of every unit outside `keep`,
1050/// so the next round sends the list and fresh-bootstraps those units. The
1051/// replica cursor survives only while a kept unit is live. The caller owns
1052/// the transaction.
1053fn reset_units(conn: &Connection, keep: &BTreeSet<String>) -> rusqlite::Result<()> {
1054    conn.execute(
1055        "UPDATE _syncular_units SET status='active', reason_code=NULL, cursor=-1,
1056           bootstrap_state=NULL, seeded='[]', echo=NULL, applied=NULL
1057         WHERE id NOT IN (SELECT value FROM json_each(?1))",
1058        [serde_json::to_string(keep).expect("unit ids serialize")],
1059    )?;
1060    conn.execute_batch(
1061        "DELETE FROM _syncular_meta WHERE key IN ('echoDigest','registrationDigest');
1062         DELETE FROM _syncular_meta WHERE key='replicaCursor'
1063           AND NOT EXISTS (SELECT 1 FROM _syncular_units WHERE cursor IS NULL);",
1064    )
1065}
1066
1067/// §4.5: the replica cursor, `-1` while no unit is live.
1068fn replica_cursor(conn: &Connection) -> rusqlite::Result<i64> {
1069    meta_get(conn, REPLICA_CURSOR_KEY)?.map_or(Ok(-1), |raw| {
1070        raw.parse().map_err(|error| {
1071            rusqlite::Error::FromSqlConversionFailure(
1072                0,
1073                rusqlite::types::Type::Text,
1074                Box::new(error),
1075            )
1076        })
1077    })
1078}
1079
1080fn visible_table(name: &str) -> String {
1081    quote_ident(name)
1082}
1083
1084const FTS_SOURCE_ID_COLUMN: &str = "_syncular_source_id";
1085
1086/// §7.4.3: is a `sqlite_master` table name a synced table (visible or base),
1087/// i.e. NOT one of the durable bookkeeping tables the reset preserves?
1088fn is_synced_table_name(name: &str) -> bool {
1089    if name.starts_with("sqlite_") {
1090        return false;
1091    }
1092    if name.starts_with("_syncular_base_") {
1093        return true; // the base half of a synced table pair
1094    }
1095    // Bookkeeping: outbox, units, meta, blob cache/uploads.
1096    !name.starts_with("_syncular_")
1097}
1098
1099/// `"sha256:" + hex` of the bytes — the content address (§5.9.1).
1100fn blob_id_for(bytes: &[u8]) -> String {
1101    let digest = Sha256::digest(bytes);
1102    format!("sha256:{}", bytes_to_hex(&digest))
1103}
1104
1105/// One [`SyncClient::write_row`] bind parameter, borrowing the row-codec
1106/// value it wraps — strings/JSON/bytes bind as borrowed TEXT/BLOB (no copy
1107/// per row on the §5.6 bootstrap path), scalars bind owned.
1108enum RowParam<'a> {
1109    Cell(&'a Option<ColumnValue>),
1110    Version(i64),
1111    Text(Option<&'a str>),
1112}
1113
1114impl rusqlite::ToSql for RowParam<'_> {
1115    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
1116        Ok(match self {
1117            RowParam::Version(v) => ToSqlOutput::Owned(SqlValue::Integer(*v)),
1118            RowParam::Text(text) => match text {
1119                Some(text) => ToSqlOutput::Borrowed(ValueRef::Text(text.as_bytes())),
1120                None => ToSqlOutput::Owned(SqlValue::Null),
1121            },
1122            RowParam::Cell(cell) => match cell {
1123                None => ToSqlOutput::Owned(SqlValue::Null),
1124                Some(ColumnValue::String(s)) => ToSqlOutput::Borrowed(ValueRef::Text(s.as_bytes())),
1125                Some(ColumnValue::Integer(i)) => ToSqlOutput::Owned(SqlValue::Integer(*i)),
1126                Some(ColumnValue::Float(f)) => ToSqlOutput::Owned(SqlValue::Real(*f)),
1127                Some(ColumnValue::Boolean(b)) => {
1128                    ToSqlOutput::Owned(SqlValue::Integer(i64::from(*b)))
1129                }
1130                Some(ColumnValue::Json(raw)) | Some(ColumnValue::BlobRef(raw)) => {
1131                    ToSqlOutput::Borrowed(ValueRef::Text(raw.0.as_bytes()))
1132                }
1133                // §5.10: crdt bytes store as BLOB, like bytes.
1134                Some(ColumnValue::Bytes(b)) | Some(ColumnValue::Crdt(b)) => {
1135                    ToSqlOutput::Borrowed(ValueRef::Blob(b))
1136                }
1137            },
1138        })
1139    }
1140}
1141
1142// Retain a typed primary key across the current import chunk without copying row payloads.
1143fn owned_sql_value(param: impl rusqlite::ToSql) -> Result<SqlValue, String> {
1144    match param.to_sql().map_err(|error| error.to_string())? {
1145        ToSqlOutput::Owned(value) => Ok(value),
1146        ToSqlOutput::Borrowed(value) => {
1147            SqlValue::try_from(value).map_err(|error| error.to_string())
1148        }
1149        _ => Err("sync.invalid_request: unsupported SQLite key parameter".into()),
1150    }
1151}
1152
1153/// §5.3 image cell → bind parameter, strict per the declared column type
1154/// (`boolean` from INTEGER 0/1, `json` from its raw TEXT, NULL only when
1155/// nullable). Mismatches are image-producer violations. Returns the cell
1156/// borrowed when the stored representation already matches what the row
1157/// codec would write, or the normalized scalar (boolean → 0/1, float from
1158/// INTEGER → REAL) otherwise — the local write is byte-identical to the
1159/// old convert-then-insert path without allocating per cell.
1160fn image_cell_param<'a>(column: &Column, value: ValueRef<'a>) -> Result<ToSqlOutput<'a>, String> {
1161    use ssp2::segment::ColumnType;
1162    let mismatch = || {
1163        Err(format!(
1164            "image column {:?} holds a value of the wrong type",
1165            column.name
1166        ))
1167    };
1168    match value {
1169        ValueRef::Null => {
1170            if !column.nullable {
1171                return Err(format!(
1172                    "image column {:?} is NULL but not nullable",
1173                    column.name
1174                ));
1175            }
1176            Ok(ToSqlOutput::Owned(SqlValue::Null))
1177        }
1178        ValueRef::Integer(i) => match column.ty {
1179            ColumnType::Integer => Ok(ToSqlOutput::Borrowed(value)),
1180            ColumnType::Boolean => Ok(ToSqlOutput::Owned(SqlValue::Integer(i64::from(i != 0)))),
1181            ColumnType::Float => Ok(ToSqlOutput::Owned(SqlValue::Real(i as f64))),
1182            _ => mismatch(),
1183        },
1184        ValueRef::Real(_) => match column.ty {
1185            ColumnType::Float => Ok(ToSqlOutput::Borrowed(value)),
1186            _ => mismatch(),
1187        },
1188        ValueRef::Text(t) => {
1189            std::str::from_utf8(t)
1190                .map_err(|_| format!("image column {:?} is not UTF-8", column.name))?;
1191            match column.ty {
1192                ColumnType::String | ColumnType::Json | ColumnType::BlobRef => {
1193                    Ok(ToSqlOutput::Borrowed(value))
1194                }
1195                _ => mismatch(),
1196            }
1197        }
1198        ValueRef::Blob(_) => match column.ty {
1199            // §5.10: a crdt column stores its opaque bytes as BLOB, like bytes.
1200            ColumnType::Bytes | ColumnType::Crdt => Ok(ToSqlOutput::Borrowed(value)),
1201            _ => mismatch(),
1202        },
1203    }
1204}
1205
1206pub(crate) fn sql_ref_to_json(column: &Column, value: rusqlite::types::ValueRef<'_>) -> Value {
1207    use rusqlite::types::ValueRef;
1208    match value {
1209        ValueRef::Null => Value::Null,
1210        ValueRef::Integer(i) => match column.ty {
1211            ssp2::segment::ColumnType::Boolean => Value::Bool(i != 0),
1212            ssp2::segment::ColumnType::Float => {
1213                serde_json::Number::from_f64(i as f64).map_or(Value::Null, Value::Number)
1214            }
1215            _ => Value::from(i),
1216        },
1217        ValueRef::Real(f) => serde_json::Number::from_f64(f).map_or(Value::Null, Value::Number),
1218        ValueRef::Text(t) => Value::from(String::from_utf8_lossy(t).into_owned()),
1219        ValueRef::Blob(b) => {
1220            let mut map = Map::new();
1221            map.insert("$bytes".to_owned(), Value::from(bytes_to_hex(b)));
1222            Value::Object(map)
1223        }
1224    }
1225}
1226
1227/// Bind a driver JSON value form as a rusqlite parameter for [`SyncClient::query`].
1228/// Objects are accepted in lossless `{"$bytes": hex}` and
1229/// `{"$bigint": decimal}` envelope forms.
1230fn json_param_to_sql(value: &Value) -> Result<SqlValue, String> {
1231    Ok(match value {
1232        Value::Null => SqlValue::Null,
1233        Value::Bool(b) => SqlValue::Integer(i64::from(*b)),
1234        Value::Number(n) => {
1235            if let Some(i) = n.as_i64() {
1236                SqlValue::Integer(i)
1237            } else if let Some(f) = n.as_f64() {
1238                SqlValue::Real(f)
1239            } else {
1240                return Err(format!("query param number {n} is out of range"));
1241            }
1242        }
1243        Value::String(s) => SqlValue::Text(s.clone()),
1244        Value::Object(_) => {
1245            if let Some(hex) = value.get("$bytes").and_then(Value::as_str) {
1246                SqlValue::Blob(crate::values::hex_to_bytes(hex)?)
1247            } else if let Some(decimal) = value.get("$bigint").and_then(Value::as_str) {
1248                SqlValue::Integer(decimal.parse::<i64>().map_err(|_| {
1249                    format!("query bigint param {decimal:?} is outside SQLite's i64 range")
1250                })?)
1251            } else {
1252                return Err(
1253                    "query object param must be a {$bytes: hex} or {$bigint: decimal} value"
1254                        .to_owned(),
1255                );
1256            }
1257        }
1258        Value::Array(_) => return Err("query array params are not supported".to_owned()),
1259    })
1260}
1261
1262/// Map a rusqlite value with no schema column to consult (arbitrary query
1263/// output): integers/reals/text pass through by stored affinity, blobs ride
1264/// as `{"$bytes": hex}`. Distinct from [`sql_ref_to_json`], which uses the
1265/// schema column type to recover booleans/floats/json.
1266fn sql_ref_to_json_dynamic(value: rusqlite::types::ValueRef<'_>) -> Value {
1267    use rusqlite::types::ValueRef;
1268    match value {
1269        ValueRef::Null => Value::Null,
1270        ValueRef::Integer(i) => {
1271            // JSON/Tauri IPC cannot represent every SQLite i64 exactly. Keep
1272            // ordinary UI-sized integers ergonomic and envelope only values
1273            // beyond JavaScript's safe range.
1274            const MAX_SAFE_INTEGER: i64 = 9_007_199_254_740_991;
1275            if (-MAX_SAFE_INTEGER..=MAX_SAFE_INTEGER).contains(&i) {
1276                Value::from(i)
1277            } else {
1278                let mut map = Map::new();
1279                map.insert("$bigint".to_owned(), Value::from(i.to_string()));
1280                Value::Object(map)
1281            }
1282        }
1283        ValueRef::Real(f) => serde_json::Number::from_f64(f).map_or(Value::Null, Value::Number),
1284        ValueRef::Text(t) => Value::from(String::from_utf8_lossy(t).into_owned()),
1285        ValueRef::Blob(b) => {
1286            let mut map = Map::new();
1287            map.insert("$bytes".to_owned(), Value::from(bytes_to_hex(b)));
1288            Value::Object(map)
1289        }
1290    }
1291}
1292
1293/// §7.5: SQLite failures classify by result code into [`QueryReadFailure`].
1294fn query_connection(
1295    conn: &Connection,
1296    sql: &str,
1297    params: &[QueryValue],
1298) -> Result<Vec<QueryRow>, QueryReadFailure> {
1299    let lowered_sql = crate::query_guard::lower_public_query_sql(sql);
1300    let mut stmt = crate::query_guard::prepare_read_only(conn, &lowered_sql)?;
1301    let bound: Vec<SqlValue> = params
1302        .iter()
1303        .map(json_param_to_sql)
1304        .collect::<Result<_, _>>()?;
1305    // §2.4: an unaliased physical version column stays engine-internal, so a
1306    // `SELECT *` row feeds straight back into mutation values. An explicit
1307    // alias (`_sync_version AS v`) returns it as ordinary query data. The
1308    // §5.11 sealed columns follow the same rule.
1309    let columns: Vec<(usize, String)> = stmt
1310        .column_names()
1311        .enumerate()
1312        .filter(|(_, name)| {
1313            !matches!(
1314                *name,
1315                "_syncular_version" | "_sync_sealed_key" | "_sync_sealed"
1316            )
1317        })
1318        .map(|(i, name)| (i, name.to_owned()))
1319        .collect();
1320    let bound_refs: Vec<&dyn rusqlite::ToSql> =
1321        bound.iter().map(|v| v as &dyn rusqlite::ToSql).collect();
1322    let mut sql_rows = stmt.query(bound_refs.as_slice())?;
1323    let mut out = Vec::new();
1324    while let Some(row) = sql_rows.next()? {
1325        let mut record = Map::new();
1326        for (i, name) in &columns {
1327            let value = row.get_ref(*i)?;
1328            record.insert(name.clone(), sql_ref_to_json_dynamic(value));
1329        }
1330        out.push(record);
1331    }
1332    Ok(out)
1333}
1334
1335fn persisted_window_state(
1336    conn: &Connection,
1337    base: &WindowBase,
1338) -> Result<WindowState, QueryReadFailure> {
1339    let mut stmt = conn.prepare(
1340        "SELECT windows.unit, units.status, units.cursor, units.bootstrap_state
1341           FROM _syncular_windows AS windows
1342           LEFT JOIN _syncular_units AS units ON units.id = windows.sub_id
1343          WHERE windows.base = ?1
1344          ORDER BY windows.unit ASC",
1345    )?;
1346    let rows = stmt.query_map(rusqlite::params![window_base_key(base)], |row| {
1347        Ok((
1348            row.get::<_, String>(0)?,
1349            row.get::<_, Option<String>>(1)?,
1350            row.get::<_, Option<i64>>(2)?,
1351            row.get::<_, Option<String>>(3)?,
1352        ))
1353    })?;
1354    let mut units = Vec::new();
1355    let mut pending = Vec::new();
1356    for row in rows {
1357        let (unit, status, cursor, bootstrap_state) = row?;
1358        // §4.8: complete iff registered, active and not bootstrapping.
1359        if status.as_deref() != Some("active")
1360            || cursor.is_some_and(|cursor| cursor < 0 || bootstrap_state.is_some())
1361        {
1362            pending.push(unit.clone());
1363        }
1364        units.push(unit);
1365    }
1366    Ok(WindowState { units, pending })
1367}
1368
1369/// One read-only statement in a §7.5 snapshot read.
1370#[derive(Debug, Clone, Copy)]
1371pub struct SnapshotStatement<'a> {
1372    pub sql: &'a str,
1373    pub params: &'a [Value],
1374}
1375
1376/// A §7.5 atomic read request: read-only statements, window coverage, unit
1377/// catch-up states, and commit deliveries, all resolved from one SQLite read
1378/// transaction and one revision.
1379#[derive(Debug, Clone, Default)]
1380pub struct SnapshotReadRequest<'a> {
1381    pub statements: Vec<SnapshotStatement<'a>>,
1382    pub coverage: &'a [WindowCoverage],
1383    pub units: Vec<String>,
1384    pub commit_ids: Vec<String>,
1385}
1386
1387#[cfg(test)]
1388thread_local! {
1389    /// Test-only rendezvous that runs between the statements of one snapshot
1390    /// read. A test uses it to commit on another connection mid-read and prove
1391    /// the read transaction's snapshot and revision stay stable.
1392    static SNAPSHOT_READ_HOOK: std::cell::RefCell<Option<Box<dyn FnOnce()>>> =
1393        std::cell::RefCell::new(None);
1394}
1395
1396#[cfg(test)]
1397fn run_snapshot_read_hook() {
1398    SNAPSHOT_READ_HOOK.with(|slot| {
1399        if let Some(hook) = slot.borrow_mut().take() {
1400            hook();
1401        }
1402    });
1403}
1404
1405/// §7.5: resolve a request over one SQLite read transaction. The revision,
1406/// every statement's rows, coverage, catch-up states, and deliveries are
1407/// captured under one `SAVEPOINT`; any statement's failure rolls the whole
1408/// read back and releases the connection.
1409fn snapshot_read_connection(
1410    conn: &Connection,
1411    request: &SnapshotReadRequest<'_>,
1412) -> Result<SnapshotRead, QueryReadFailure> {
1413    conn.execute_batch("SAVEPOINT syncular_snapshot_read")?;
1414    let corrupt = |message: &str| QueryReadFailure {
1415        code: Some("sync.local_corrupt"),
1416        sqlite_code: None,
1417        sqlite_message: None,
1418        rollback_failure: None,
1419        message: message.to_owned(),
1420    };
1421    let stored_failure = |error: rusqlite::Error, message: &str| match error {
1422        rusqlite::Error::Utf8Error(..)
1423        | rusqlite::Error::InvalidColumnType(..)
1424        | rusqlite::Error::FromSqlConversionFailure(..)
1425        | rusqlite::Error::IntegralValueOutOfRange(..) => corrupt(message),
1426        _ => QueryReadFailure::from(error),
1427    };
1428    let result: Result<SnapshotRead, QueryReadFailure> = (|| {
1429        // The durable revision as a canonical decimal `u64`; a missing marker
1430        // is the legacy zero, and any other non-canonical value is corrupt.
1431        let revision: Option<String> = conn
1432            .query_row(
1433                "SELECT value FROM _syncular_meta WHERE key = ?1",
1434                rusqlite::params![LOCAL_REVISION_KEY],
1435                |row| row.get(0),
1436            )
1437            .optional()
1438            .map_err(|error| stored_failure(error, "persisted local revision is invalid"))?;
1439        let revision = match revision {
1440            None => "0".to_owned(),
1441            Some(value)
1442                if value == "0"
1443                    || (matches!(value.as_bytes().first(), Some(b'1'..=b'9'))
1444                        && value.bytes().all(|b| b.is_ascii_digit())
1445                        && value.parse::<u64>().is_ok()) =>
1446            {
1447                value
1448            }
1449            Some(_) => return Err(corrupt("persisted local revision is invalid")),
1450        };
1451        let mut queries = Vec::with_capacity(request.statements.len());
1452        for statement in &request.statements {
1453            queries.push(query_connection(conn, statement.sql, statement.params)?);
1454            #[cfg(test)]
1455            run_snapshot_read_hook();
1456        }
1457        let mut pending = Vec::new();
1458        let mut missing = Vec::new();
1459        for requested in request.coverage {
1460            let base_key = window_base_key(&requested.base);
1461            let state = persisted_window_state(conn, &requested.base)?;
1462            for unit in BTreeSet::from_iter(requested.units.iter().cloned()) {
1463                let reference = WindowUnitRef {
1464                    base_key: base_key.clone(),
1465                    unit: unit.clone(),
1466                };
1467                if !state.units.iter().any(|held| held == &unit) {
1468                    missing.push(reference);
1469                } else if state.pending.iter().any(|held| held == &unit) {
1470                    pending.push(reference);
1471                }
1472            }
1473        }
1474        let mut units = Vec::with_capacity(request.units.len());
1475        for id in &request.units {
1476            let exists: bool = conn.query_row(
1477                "SELECT EXISTS(SELECT 1 FROM _syncular_units WHERE id = ?1)",
1478                [id],
1479                |row| row.get(0),
1480            )?;
1481            if !exists {
1482                units.push(UnitCatchup::Unknown { id: id.clone() });
1483                continue;
1484            }
1485            // Every persisted field is required to be well typed; a corrupt
1486            // record fails typed instead of inventing a state.
1487            let unit = get_unit(conn, id)
1488                .map_err(|error| stored_failure(error, "persisted unit state is invalid"))?
1489                .ok_or_else(|| corrupt("persisted unit state is invalid"))?;
1490            let cursor = match unit.cursor {
1491                Some(cursor) => cursor,
1492                None => replica_cursor(conn)
1493                    .map_err(|error| stored_failure(error, "persisted unit state is invalid"))?,
1494            };
1495            if cursor.unsigned_abs() > MAX_JS_SAFE_INTEGER {
1496                return Err(corrupt("persisted unit state is invalid"));
1497            }
1498            let active = unit.status == UnitStatus::Active;
1499            let pending = unit.bootstrapping();
1500            units.push(UnitCatchup::Known {
1501                id: id.clone(),
1502                status: unit.status,
1503                live: unit.cursor.is_none(),
1504                cursor,
1505                has_resume_token: unit.bootstrap_state.is_some(),
1506                bootstrap_complete: active && !pending,
1507                known_pending_pages: active && pending,
1508                reason_code: unit.reason_code,
1509            });
1510        }
1511        let mut deliveries = Vec::with_capacity(request.commit_ids.len());
1512        for id in &request.commit_ids {
1513            let pending: bool = conn.query_row(
1514                "SELECT EXISTS(SELECT 1 FROM _syncular_outbox WHERE commit_id = ?1)",
1515                rusqlite::params![id],
1516                |row| row.get(0),
1517            )?;
1518            if pending {
1519                deliveries.push(CommitDelivery::Pending {
1520                    client_commit_id: id.clone(),
1521                });
1522                continue;
1523            }
1524            let row = conn
1525                .query_row(
1526                    "SELECT seq, client_commit_id, status, recorded_at_ms, results_json, operations_json,
1527                            resolution, resolved_at_ms, replacement_client_commit_id
1528                       FROM _syncular_commit_outcomes WHERE client_commit_id = ?1",
1529                    rusqlite::params![id],
1530                    StoredCommitOutcomeRow::from_row,
1531                )
1532                .optional()
1533                .map_err(|error| stored_failure(error, "persisted commit outcome is invalid"))?;
1534            deliveries.push(match row {
1535                Some(row) => CommitDelivery::Known {
1536                    client_commit_id: id.clone(),
1537                    outcome: SyncClient::outcome_from_row(row)
1538                        .map_err(|_| corrupt("persisted commit outcome is invalid"))?,
1539                },
1540                None => CommitDelivery::Unknown {
1541                    client_commit_id: id.clone(),
1542                },
1543            });
1544        }
1545        Ok(SnapshotRead {
1546            revision,
1547            queries,
1548            coverage: CoverageSnapshot {
1549                complete: pending.is_empty() && missing.is_empty(),
1550                pending,
1551                missing,
1552            },
1553            units,
1554            deliveries,
1555        })
1556    })();
1557    match result {
1558        Ok(read) => {
1559            conn.execute_batch("RELEASE syncular_snapshot_read")?;
1560            Ok(read)
1561        }
1562        Err(mut error) => {
1563            if let Err(rollback) = conn
1564                .execute_batch("ROLLBACK TO syncular_snapshot_read; RELEASE syncular_snapshot_read")
1565            {
1566                error.rollback_failure = Some(Box::new(QueryReadFailure::from(rollback)));
1567            }
1568            Err(error)
1569        }
1570    }
1571}
1572
1573fn snapshot_connection(
1574    conn: &Connection,
1575    sql: &str,
1576    params: &[Value],
1577    coverage: &[WindowCoverage],
1578) -> Result<QuerySnapshot, QueryReadFailure> {
1579    let read = snapshot_read_connection(
1580        conn,
1581        &SnapshotReadRequest {
1582            statements: vec![SnapshotStatement { sql, params }],
1583            coverage,
1584            units: Vec::new(),
1585            commit_ids: Vec::new(),
1586        },
1587    )?;
1588    Ok(QuerySnapshot {
1589        revision: read.revision,
1590        rows: read
1591            .queries
1592            .into_iter()
1593            .next()
1594            .expect("single-query snapshot returns one result"),
1595        coverage: read.coverage,
1596    })
1597}
1598
1599/// A long-lived read-only SQLite sidecar for latency-critical native views.
1600/// Network rounds stay serialized on the mutable core owner, while atomic
1601/// query snapshots use this independent connection and therefore never queue
1602/// behind HTTP/WebSocket latency.
1603pub struct FileQuerySnapshotReader {
1604    path: String,
1605    conn: Option<Connection>,
1606}
1607
1608impl FileQuerySnapshotReader {
1609    #[must_use]
1610    pub fn new(path: impl Into<String>) -> Self {
1611        Self {
1612            path: path.into(),
1613            conn: None,
1614        }
1615    }
1616
1617    fn connection(&mut self) -> Result<&Connection, QueryReadFailure> {
1618        if self.conn.is_none() {
1619            let conn = Connection::open_with_flags(
1620                &self.path,
1621                OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
1622            )
1623            .map_err(QueryReadFailure::from)?;
1624            conn.busy_timeout(std::time::Duration::from_millis(250))
1625                .map_err(QueryReadFailure::from)?;
1626            self.conn = Some(conn);
1627        }
1628        self.conn
1629            .as_ref()
1630            .ok_or_else(|| QueryReadFailure::from("read sidecar connection missing".to_owned()))
1631    }
1632
1633    /// One atomic read over the shared primitive: every statement, coverage,
1634    /// catch-up, and delivery under one `SAVEPOINT` and one revision.
1635    pub fn snapshot_read(
1636        &mut self,
1637        request: &SnapshotReadRequest<'_>,
1638    ) -> Result<SnapshotRead, QueryReadFailure> {
1639        snapshot_read_connection(self.connection()?, request)
1640    }
1641
1642    /// The current durable local revision. A host can skip a read when it is
1643    /// unchanged; a corrupt or unreadable marker fails typed.
1644    pub fn local_revision(&mut self) -> Result<String, QueryReadFailure> {
1645        Ok(self
1646            .snapshot_read(&SnapshotReadRequest::default())?
1647            .revision)
1648    }
1649
1650    /// One unit's persisted catch-up state (see the §7.5 contract).
1651    pub fn unit_catchup(&mut self, id: &str) -> Result<UnitCatchup, QueryReadFailure> {
1652        let read = self.snapshot_read(&SnapshotReadRequest {
1653            units: vec![id.to_owned()],
1654            ..Default::default()
1655        })?;
1656        Ok(read
1657            .units
1658            .into_iter()
1659            .next()
1660            .expect("one requested catch-up"))
1661    }
1662
1663    /// One client commit's delivery status: pending, its retained outcome, or
1664    /// an explicit unknown (never held, or pruned by retention).
1665    pub fn commit_delivery(&mut self, id: &str) -> Result<CommitDelivery, QueryReadFailure> {
1666        let read = self.snapshot_read(&SnapshotReadRequest {
1667            commit_ids: vec![id.to_owned()],
1668            ..Default::default()
1669        })?;
1670        Ok(read
1671            .deliveries
1672            .into_iter()
1673            .next()
1674            .expect("one requested delivery"))
1675    }
1676
1677    /// Hosts report an owned failure, and the first success after it, to the
1678    /// owning core through [`SyncClient::record_query_read`] (§7.6).
1679    pub fn query_snapshot(
1680        &mut self,
1681        sql: &str,
1682        params: &[Value],
1683        coverage: &[WindowCoverage],
1684    ) -> Result<QuerySnapshot, QueryReadFailure> {
1685        snapshot_connection(self.connection()?, sql, params, coverage)
1686    }
1687}
1688
1689fn validate_authority_reads(
1690    schema: &ClientSchema,
1691    declarations: &[crate::api::AuthorityReadDeclaration],
1692) -> Result<(), String> {
1693    let mut seen = BTreeSet::new();
1694    for read in declarations {
1695        let table = schema
1696            .table(&read.table)
1697            .ok_or("client.authority_read_forbidden: authority table is not declared")?;
1698        let plain = |name: &str| {
1699            table.columns.iter().enumerate().any(|(i, c)| {
1700                c.name == name
1701                    && name != "authority_version"
1702                    && !name.starts_with("_sync")
1703                    && !table.encrypted_columns.iter().any(|e| e.index == i)
1704                    && matches!(
1705                        c.ty,
1706                        ssp2::segment::ColumnType::String
1707                            | ssp2::segment::ColumnType::Integer
1708                            | ssp2::segment::ColumnType::Float
1709                            | ssp2::segment::ColumnType::Boolean
1710                            | ssp2::segment::ColumnType::Json
1711                    )
1712            })
1713        };
1714        if !seen.insert(&read.table)
1715            || read.columns.is_empty()
1716            || read.columns.iter().collect::<BTreeSet<_>>().len() != read.columns.len()
1717            || !read.columns.contains(&table.primary_key)
1718            || !read.columns.iter().all(|c| plain(c))
1719            || read.scopes.is_empty()
1720            || !read.scopes.iter().all(|(v, values)| {
1721                table
1722                    .scope_column(v)
1723                    .is_some_and(|c| read.columns.iter().any(|column| column == c) && plain(c))
1724                    && !values.is_empty()
1725                    && values.iter().all(|v| !v.is_empty() && v != "*")
1726            })
1727        {
1728            return Err("client.authority_read_forbidden: authority reads require plain columns, a primary key and nonempty plain scope selectors".into());
1729        }
1730    }
1731    Ok(())
1732}
1733
1734// §7.4.2: every open and recreation checks before any persistent writes.
1735/// A marker read that hit a recognized SQLite storage failure keeps that
1736/// classification (BUSY/LOCKED stay retryable); only an unclassified read
1737/// failure is the invalid-marker `sync.local_corrupt`.
1738fn schema_marker_read_failure(error: rusqlite::Error) -> String {
1739    let failure = QueryReadFailure::from(error);
1740    if failure.code.is_some() {
1741        failure.to_string()
1742    } else {
1743        "sync.local_corrupt: persisted local schema marker is unreadable or invalid".to_owned()
1744    }
1745}
1746
1747/// §7.1: reject an invalid configured client limit before any storage is
1748/// opened or written. Zero would wedge the push queue and an out-of-range
1749/// value would differ per target; the TS core enforces the same bounds.
1750fn validate_client_limits(limits: &crate::api::ClientLimits) -> Result<(), String> {
1751    if limits.outcome_retention_max_entries == Some(0) {
1752        return Err("sync.invalid_request: outcomeRetentionMaxEntries must be positive".to_owned());
1753    }
1754    if [
1755        limits.max_push_commits_per_request,
1756        limits.max_push_operations_per_request,
1757        limits.max_push_request_bytes,
1758    ]
1759    .into_iter()
1760    .flatten()
1761    .any(|value| value == 0 || value > u32::MAX as usize)
1762    {
1763        return Err(
1764            "sync.invalid_request: push limit must be an integer in 1..=4294967295".to_owned(),
1765        );
1766    }
1767    Ok(())
1768}
1769
1770fn read_local_schema_version(conn: &Connection, requested: i32) -> Result<Option<i32>, String> {
1771    let corrupt =
1772        || "sync.local_corrupt: persisted local schema marker is unreadable or invalid".to_owned();
1773    let table_type: Option<String> = conn
1774        .query_row(
1775            "SELECT type FROM sqlite_master WHERE name = '_syncular_meta'",
1776            [],
1777            |row| row.get(0),
1778        )
1779        .optional()
1780        .map_err(schema_marker_read_failure)?;
1781    let Some(table_type) = table_type else {
1782        return Ok(None);
1783    };
1784    if table_type != "table" {
1785        return Err(corrupt());
1786    }
1787    // §7.4.2: a duplicated marker cannot be resolved. `query_row` would take
1788    // whichever row SQLite returns first, and an older one would drive a
1789    // destructive reset over a newer replica.
1790    let mut statement = conn
1791        .prepare("SELECT value FROM _syncular_meta WHERE key = ?1")
1792        .map_err(schema_marker_read_failure)?;
1793    let mut rows = statement
1794        .query([LOCAL_SCHEMA_VERSION_KEY])
1795        .map_err(schema_marker_read_failure)?;
1796    let value: Option<String> = rows
1797        .next()
1798        .map_err(schema_marker_read_failure)?
1799        .map(|row| row.get(0))
1800        .transpose()
1801        .map_err(schema_marker_read_failure)?;
1802    if rows.next().map_err(schema_marker_read_failure)?.is_some() {
1803        return Err(corrupt());
1804    }
1805    let Some(value) = value else {
1806        let has_descriptor = conn
1807            .query_row(
1808                "SELECT 1 FROM _syncular_meta WHERE key = ?1 LIMIT 1",
1809                [crate::previous_version::LOCAL_SCHEMA_DESCRIPTOR_KEY],
1810                |_| Ok(()),
1811            )
1812            .optional()
1813            .map_err(schema_marker_read_failure)?
1814            .is_some();
1815        if has_descriptor {
1816            return Err(corrupt());
1817        }
1818        return Ok(None);
1819    };
1820    if !matches!(value.as_bytes().first(), Some(b'1'..=b'9'))
1821        || !value.bytes().all(|b| b.is_ascii_digit())
1822    {
1823        return Err(corrupt());
1824    }
1825    let version = value.parse::<i32>().map_err(|_| corrupt())?;
1826    if version > requested {
1827        return Err(
1828            "client.schema_downgrade: persisted local schema is newer than the requested schema"
1829                .to_owned(),
1830        );
1831    }
1832    Ok(Some(version))
1833}
1834
1835/// Open a replica file. On a browser worker the file must recover a hot
1836/// journal before any statement reads it: `wasm_vfs` replaces the installed
1837/// OPFS VFS's reserved lock callback before the connection opens its file. A
1838/// new browser replica uses 64 KiB pages (§5.3), because every OPFS call costs
1839/// the same whatever its size; a database with tables keeps its page size.
1840fn open_replica(path: &str) -> Result<Connection, String> {
1841    let browser = cfg!(all(target_arch = "wasm32", target_os = "unknown"))
1842        && !path.is_empty()
1843        && path != ":memory:";
1844    #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
1845    if browser {
1846        crate::wasm_vfs::disable_foreign_lock_check()?;
1847    }
1848    let conn = Connection::open(path).map_err(|e| format!("open db {path:?}: {e}"))?;
1849    if browser {
1850        conn.execute_batch("PRAGMA page_size = 65536")
1851            .map_err(|e| format!("open db {path:?}: {e}"))?;
1852    }
1853    Ok(conn)
1854}
1855
1856impl SyncClient {
1857    pub fn new_with_identity(
1858        client_id: Option<String>,
1859        schema_json: &Value,
1860        limits: ClientLimits,
1861    ) -> Result<Self, String> {
1862        let resolved = client_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
1863        let conn = Connection::open_in_memory().map_err(|e| e.to_string())?;
1864        Self::with_connection(resolved, schema_json, limits, conn)
1865    }
1866
1867    pub fn new(
1868        client_id: String,
1869        schema_json: &Value,
1870        limits: ClientLimits,
1871    ) -> Result<Self, String> {
1872        let conn = Connection::open_in_memory().map_err(|e| e.to_string())?;
1873        Self::with_connection(client_id, schema_json, limits, conn)
1874    }
1875
1876    /// Build a client backed by an on-disk SQLite database at `path` — the
1877    /// seam a native host (Tauri plugin, FFI file-DB variant) uses to persist
1878    /// across process restarts. `create_tables` runs `IF NOT EXISTS`, so
1879    /// re-opening the same file reuses the persisted rows. Keeps rusqlite out
1880    /// of the command router's dependency set (the router only holds a path).
1881    pub fn open_path(
1882        client_id: String,
1883        schema_json: &Value,
1884        limits: ClientLimits,
1885        path: &str,
1886    ) -> Result<Self, String> {
1887        validate_client_limits(&limits)?;
1888        parse_schema_json(schema_json)?;
1889        let conn = open_replica(path)?;
1890        Self::with_connection(client_id, schema_json, limits, conn)
1891    }
1892
1893    /// Open a file replica and resolve its client id. Native targets require
1894    /// WAL for the independent snapshot reader; `wasm32-unknown-unknown`
1895    /// accepts the sqlite-wasm worker journal instead.
1896    pub fn open_path_with_identity(
1897        client_id: Option<String>,
1898        schema_json: &Value,
1899        limits: ClientLimits,
1900        path: &str,
1901    ) -> Result<Self, String> {
1902        Self::open_path_with_identity_journal(
1903            client_id,
1904            schema_json,
1905            limits,
1906            path,
1907            !cfg!(all(target_arch = "wasm32", target_os = "unknown")),
1908        )
1909    }
1910
1911    fn open_path_with_identity_journal(
1912        client_id: Option<String>,
1913        schema_json: &Value,
1914        limits: ClientLimits,
1915        path: &str,
1916        require_wal: bool,
1917    ) -> Result<Self, String> {
1918        validate_client_limits(&limits)?;
1919        // §7.4.2: an invalid requested version is refused before the replica is
1920        // even created, so a rejected constructor leaves no file behind.
1921        parse_schema_json(schema_json)?;
1922        let conn = open_replica(path)?;
1923        // A file-backed native client uses an independent read connection for
1924        // latency-critical snapshots. WAL is SQLite's intended reader/writer
1925        // concurrency mode: a view read never holds a rollback-journal lock
1926        // that delays the client's next commit. The busy timeout must precede
1927        // this replica's own startup transaction; WAL is applied only after
1928        // startup succeeded, because a refused open must leave the replica's
1929        // journal mode and contents untouched (§7.4.2).
1930        conn.busy_timeout(std::time::Duration::from_millis(250))
1931            .map_err(|error| format!("configure db {path:?} busy timeout: {error}"))?;
1932        // Durability is the caller's contract here, not the VFS default: a
1933        // rollback-journal commit needs FULL before it writes the database
1934        // pages, or a killed worker can leave a half-written image behind.
1935        // WAL below inherits the same setting.
1936        conn.pragma_update(None, "synchronous", "FULL")
1937            .map_err(|error| format!("configure db {path:?} synchronous: {error}"))?;
1938        let client = Self::with_connection_identity(client_id, schema_json, limits, conn)?;
1939        // A browser worker's sqlite-wasm VFS has no WAL mode, and the wasm
1940        // host has no independent snapshot reader. The WAL requirement exists
1941        // for native file replicas, where it is the reader/writer boundary.
1942        if require_wal {
1943            let journal_mode: String = client
1944                .conn
1945                .pragma_update_and_check(None, "journal_mode", "WAL", |row| row.get(0))
1946                .map_err(|error| Self::sqlite_failure(&client.storage_failure, error))?;
1947            if journal_mode != "wal" {
1948                return Err(
1949                    "sync.invalid_request: file-backed client requires WAL journal mode".to_owned(),
1950                );
1951            }
1952        }
1953        Ok(client)
1954    }
1955
1956    /// Delete a file replica at `path`, including its `-journal`, `-wal`, and
1957    /// `-shm` sidecars, through the VFS that owns it. Use this after
1958    /// `client.storage_corrupt`: an OPFS SAH pool stores those files under
1959    /// opaque names, so deleting the pool directory behind SQLite bypasses the
1960    /// pool's slot accounting.
1961    ///
1962    /// Close the client and every [`FileQuerySnapshotReader`] on `path` first;
1963    /// a VFS cannot delete a file another handle still owns. A file that does
1964    /// not exist counts as deleted, so a cleanly closed replica without
1965    /// sidecars resets. The next open
1966    /// creates a fresh replica (new client id) and re-bootstraps from the
1967    /// server. A memory replica has nothing to delete and fails with
1968    /// `sync.invalid_request`.
1969    pub fn reset_replica(path: &str) -> Result<(), String> {
1970        if path.is_empty() || path == ":memory:" {
1971            return Err("sync.invalid_request: a memory replica has nothing to reset".to_owned());
1972        }
1973        // SAFETY: a null VFS name asks SQLite for its process-wide default VFS,
1974        // the same one `Connection::open(path)` above selects. `xDelete` is
1975        // that VFS's own function pointer, and `c_name` outlives the call.
1976        let vfs = unsafe { rusqlite::ffi::sqlite3_vfs_find(std::ptr::null()) };
1977        if vfs.is_null() {
1978            return Err("client.storage_io: no SQLite VFS is registered".to_owned());
1979        }
1980        let delete = unsafe { (*vfs).xDelete }
1981            .ok_or_else(|| "client.storage_io: the SQLite VFS cannot delete files".to_owned())?;
1982        for suffix in ["-wal", "-shm", "-journal", ""] {
1983            let file_name = format!("{path}{suffix}");
1984            let c_name = std::ffi::CString::new(file_name.as_str()).map_err(|_| {
1985                format!("sync.invalid_request: replica path {file_name:?} contains a NUL byte")
1986            })?;
1987            // SAFETY: `vfs` and its `xDelete` pointer come from the live
1988            // SQLite library; `c_name` is a valid NUL-terminated path.
1989            let code = unsafe { delete(vfs, c_name.as_ptr(), 1) };
1990            // A cleanly closed WAL replica has no sidecars; a missing file is
1991            // already deleted.
1992            if code != rusqlite::ffi::SQLITE_OK && code != rusqlite::ffi::SQLITE_IOERR_DELETE_NOENT
1993            {
1994                return Err(format!(
1995                    "client.storage_io: deleting replica file {file_name:?} failed with SQLite code {code}"
1996                ));
1997            }
1998        }
1999        Ok(())
2000    }
2001
2002    #[must_use]
2003    pub fn client_id(&self) -> &str {
2004        &self.client_id
2005    }
2006
2007    /// Rotate the caller's client id (§1.5) and return the new one. Use it
2008    /// after a `sync.invalid_client_id` refusal (`recommendedAction:
2009    /// resetClientId`): the next round carries the new id. A live realtime
2010    /// socket is closed so the next connection registers with the new id.
2011    /// The replica and its outbox stay in place; wipe them yourself when the
2012    /// stored data belongs to the account that signed out.
2013    pub fn reset_client_id(&mut self, transport: &mut dyn Transport) -> Result<String, String> {
2014        self.disconnect_realtime(transport);
2015        let client_id = uuid::Uuid::new_v4().to_string();
2016        self.set_meta(CLIENT_ID_KEY, &client_id)?;
2017        self.client_id.clone_from(&client_id);
2018        self.round_failure = None;
2019        Ok(client_id)
2020    }
2021
2022    /// Build a client over a caller-supplied rusqlite connection — the seam a
2023    /// native host (Tauri plugin, FFI file-DB variant) uses to back the core
2024    /// with an on-disk database (`Connection::open(path)`) rather than the
2025    /// default `:memory:`. The connection MUST be fresh (no pre-existing
2026    /// syncular tables); `create_tables` runs `IF NOT EXISTS`, so re-opening
2027    /// the same file across process restarts reuses the persisted rows.
2028    pub fn with_connection(
2029        client_id: String,
2030        schema_json: &Value,
2031        limits: ClientLimits,
2032        conn: Connection,
2033    ) -> Result<Self, String> {
2034        Self::with_connection_identity(Some(client_id), schema_json, limits, conn)
2035    }
2036
2037    /// [`Self::with_connection`] for `open_path_with_identity`: no requested id
2038    /// means a durable replica's persisted `clientId` wins over a fresh one.
2039    fn with_connection_identity(
2040        client_id: Option<String>,
2041        schema_json: &Value,
2042        limits: ClientLimits,
2043        conn: Connection,
2044    ) -> Result<Self, String> {
2045        validate_client_limits(&limits)?;
2046        let schema = parse_schema_json(schema_json)?;
2047        validate_authority_reads(&schema, &limits.authority_reads)?;
2048        // RFC 0005 D8: resolve the previous-version config before the opening
2049        // reset runs — the capture happens inside it. Bad bounds fail loud.
2050        let previous_version = match limits.previous_version_context {
2051            Some(config) => {
2052                config.validate()?;
2053                Some(config)
2054            }
2055            None => None,
2056        };
2057        let mut client = SyncClient {
2058            #[cfg(feature = "bench-internals")]
2059            benchmark_phases: Recorder::default(),
2060            conn,
2061            schema,
2062            client_id: client_id
2063                .clone()
2064                .unwrap_or_else(|| uuid::Uuid::new_v4().to_string()),
2065            limits,
2066            outbox: Shared::default(),
2067            failed_commits: Shared::default(),
2068            retain_failed_commits: false,
2069            conflicts: Shared::default(),
2070            rejections: Shared::default(),
2071            schema_floor: None,
2072            lease_state: None,
2073            stopped: false,
2074            upgrading: false,
2075            sync_needed: false,
2076            realtime_policy: RealtimePolicy::Optional,
2077            realtime_state: RealtimeState::Disconnected,
2078            realtime_reason_code: None,
2079            realtime_retry_delay_ms: None,
2080            presence: HashMap::new(),
2081            now_ms: None,
2082            encryption: crate::values::EncryptionConfig::default(),
2083            security_preflight: false,
2084            security_epoch: 0,
2085            transport_enabled: true,
2086            transport_epoch: 0,
2087            registration_epoch: 0,
2088            previous_version,
2089            insert_sql: RefCell::new(HashMap::new()),
2090            overlay_dirty: OverlayDirty::clean(),
2091            storage_failure: RefCell::new(None),
2092            #[cfg(test)]
2093            overlay_rebuild_count: Cell::new(0),
2094            #[cfg(test)]
2095            acknowledged_replay_count: Cell::new(0),
2096            #[cfg(test)]
2097            outcome_prune_count: Cell::new(0),
2098            #[cfg(test)]
2099            image_chunk_ms: Cell::new(None),
2100            progress: ProgressObserver::default(),
2101            change_queue: VecDeque::new(),
2102            sync_intent_queue: VecDeque::new(),
2103            retry_delay_ms: 250,
2104            round_retry_delay_ms: None,
2105            image_chunk_rows: HashMap::new(),
2106            round_failure: None,
2107            active_round: None,
2108            last_round: None,
2109            last_change: None,
2110            query_failures: Vec::new(),
2111            coalesced: None,
2112            coalesced_failure: None,
2113        };
2114        // The row write path leans on the prepared-statement cache (two
2115        // insert statements per synced table, plus the bookkeeping
2116        // statements); size it so a multi-table schema never thrashes.
2117        client
2118            .conn
2119            .set_prepared_statement_cache_capacity(64.max(client.schema.tables.len() * 4));
2120        // §7.4.2: one write transaction (SQLite `BEGIN IMMEDIATE`, a reserved
2121        // writer) covers the marker read, the identity check, and every
2122        // bookkeeping/schema write, so a concurrent open that upgraded the
2123        // replica cannot interleave between the guard and the writes, and a
2124        // refusal leaves the replica unchanged.
2125        client
2126            .conn
2127            .execute_batch("BEGIN IMMEDIATE")
2128            .map_err(|error| Self::sqlite_failure(&client.storage_failure, error))?;
2129        let startup = (|| -> Result<(), String> {
2130            // The marker guard runs inside the write transaction, so the version
2131            // it validates is the version every write below commits under.
2132            let marker = read_local_schema_version(&client.conn, client.schema.version)?;
2133            // New app indexes may reference columns that only exist after a
2134            // version-bump reset.
2135            client.create_bookkeeping_tables()?;
2136            match client.get_meta(CLIENT_ID_KEY)? {
2137                Some(existing) => match client_id {
2138                    Some(requested) if existing != requested => {
2139                        return Err(format!(
2140                            "client.identity_mismatch: this database belongs to {existing:?}; refusing to rebind it to {requested:?}"
2141                        ))
2142                    }
2143                    _ => client.client_id = existing,
2144                },
2145                None => client.set_meta(CLIENT_ID_KEY, &client.client_id)?,
2146            }
2147            client.restore_persisted_state()?;
2148            client.failed_commits = client.load_failed_commits()?.into();
2149            match marker {
2150                None => {
2151                    client.create_synced_tables()?;
2152                    client.persist_schema_version()?;
2153                    client.save_unit_scope_schema()?;
2154                }
2155                Some(version) if version == client.schema.version => {
2156                    client.create_synced_tables()?;
2157                    // RFC 0005 D1: the same-version open backfills the descriptor
2158                    // for a database first opened by an unaware binary, so the
2159                    // NEXT bump can capture without needing a schema bump first.
2160                    set_local_schema_descriptor(&client.conn, &client.schema)
2161                        .map_err(|error| Self::sqlite_failure(&client.storage_failure, error))?;
2162                    client.save_unit_scope_schema()?;
2163                }
2164                Some(_) => client.run_schema_reset()?,
2165            }
2166            // RFC 0005 D9: the reset sweep cannot see a container whose capture
2167            // committed in its OWN file while the replica's savepoint never
2168            // released — a crash at that seam leaves a container beside a matching
2169            // OLD marker, and the next open at that version runs no reset. Discard
2170            // it before anything can read it. No-op when no container exists.
2171            client.reconcile_previous_version_at_boot()?;
2172            client.clear_satisfied_persisted_schema_floor()?;
2173            client.prune_unknown_units(false)?;
2174            // §7.1/§7.4.4: recover intrinsically codec-invalid persisted values at
2175            // the full startup/reset reconciliation boundary, inside this open
2176            // transaction and after the schema and identity guards, so a commit
2177            // an earlier version accepted is dropped with durable rejection
2178            // evidence before any replay. A table the current schema removed
2179            // keeps the §7.4.4 send-time classification, and an unresolved
2180            // encryption key stays with the send seam because hosts configure
2181            // keys after open.
2182            client.drop_unencodable_outbox(false, None)?;
2183            if marker == Some(client.schema.version)
2184                && (!client.outbox.is_empty()
2185                    || !client.failed_commits.is_empty()
2186                    || client.has_acknowledged_rows()?)
2187            {
2188                // Reconstruct the visible optimistic overlay from the durable
2189                // base plus outbox instead of trusting a process-interrupted
2190                // mirror.
2191                client.overlay_dirty.set(true);
2192                client.rebuild_overlay()?;
2193            }
2194            // Every persisted active unit needs one catch-up pull on
2195            // open: realtime only covers changes after connection, while an
2196            // idempotent setWindow correctly creates no fresh command effect.
2197            // Pending outbox work has the same restart requirement. The core
2198            // owns this intent so native hosts never poll or require an
2199            // application-issued sync().
2200            client.enqueue_startup_sync_if_needed()?;
2201            Ok(())
2202        })();
2203        match startup {
2204            Ok(()) => match client.conn.execute_batch("COMMIT") {
2205                // §4.8: an eviction a crash interrupted finishes before any
2206                // read, with or without a transport; a replica reopened in
2207                // preflight finishes it at activation.
2208                Ok(()) => {
2209                    if !client.security_preflight {
2210                        client.drain_pending_evictions()?;
2211                    }
2212                    Ok(client)
2213                }
2214                Err(error) => {
2215                    let failure = Self::sqlite_failure(&client.storage_failure, error);
2216                    if let Err(rollback) = client.conn.execute_batch("ROLLBACK") {
2217                        // Keep a failed rollback's typed storage classification.
2218                        let _ = Self::sqlite_failure(&client.storage_failure, rollback);
2219                    }
2220                    Err(failure)
2221                }
2222            },
2223            Err(error) => {
2224                if let Err(rollback) = client.conn.execute_batch("ROLLBACK") {
2225                    // Keep a failed rollback's typed storage classification.
2226                    let _ = Self::sqlite_failure(&client.storage_failure, rollback);
2227                }
2228                Err(error)
2229            }
2230        }
2231    }
2232
2233    /// Pin the client clock (epoch ms) — the §5.4 expiry check runs
2234    /// against this instead of system time (conformance virtual clock).
2235    pub fn set_now_ms(&mut self, now_ms: i64) {
2236        self.now_ms = Some(std::sync::Arc::new(now_ms.into()));
2237    }
2238
2239    /// The pinned clock, shared: a host that stores into it moves the client
2240    /// clock while a call runs (a virtual clock that advances per request).
2241    pub fn pinned_clock(&self) -> Option<std::sync::Arc<std::sync::atomic::AtomicI64>> {
2242        self.now_ms.clone()
2243    }
2244
2245    /// §5.11: install the client-side encryption keys (`keyId → key bytes`).
2246    /// The command router parses these from the `create` command's
2247    /// `encryption` config (keys as `{$bytes: hex}`).
2248    pub fn set_encryption(&mut self, encryption: crate::values::EncryptionConfig) {
2249        self.encryption = encryption;
2250    }
2251
2252    #[must_use]
2253    pub fn security_lifecycle(&self) -> &'static str {
2254        if self.security_preflight {
2255            "preflight"
2256        } else {
2257            "active"
2258        }
2259    }
2260
2261    #[must_use]
2262    pub fn security_preflight(&self) -> bool {
2263        self.security_preflight
2264    }
2265
2266    /// Host-owned, non-persisted network gate. Local operations remain available.
2267    pub fn set_transport_enabled(&mut self, transport: &mut dyn Transport, enabled: bool) {
2268        if self.transport_enabled != enabled {
2269            self.transport_enabled = enabled;
2270            self.transport_epoch = self.transport_epoch.wrapping_add(1);
2271            self.sync_intent_queue.clear();
2272            self.sync_intent_queue
2273                .push_back(if enabled && !self.security_preflight {
2274                    SyncIntent::Interactive
2275                } else {
2276                    SyncIntent::None
2277                });
2278        }
2279        // A captured reply keeps its normal apply/revocation semantics.
2280        if !enabled && self.active_round.is_none() {
2281            self.disconnect_realtime(transport);
2282        }
2283    }
2284
2285    pub fn transport_enabled(&self) -> bool {
2286        self.transport_enabled
2287    }
2288
2289    /// Quarantine this replica: enter the fail-closed gate, release all
2290    /// core-owned key material, and record the gate in the database so
2291    /// reopening re-enters preflight until `activate_security` clears it.
2292    ///
2293    /// Use [`SyncClient::seal_security_on_teardown`] for the shutdown barrier,
2294    /// which must NOT leave that durable mark.
2295    pub fn begin_security_preflight(&mut self) -> Result<(), String> {
2296        let entering = !self.security_preflight;
2297        self.seal_security_on_teardown();
2298        // Persist the quarantine so it survives handle teardown and restart:
2299        // reopening this replica re-enters preflight until activation clears it.
2300        self.set_meta(SECURITY_PREFLIGHT_PENDING_KEY, "1")?;
2301        if entering {
2302            self.emit_status_batch()?;
2303        }
2304        Ok(())
2305    }
2306
2307    /// One change batch carrying the status snapshot (§5.11 lifecycle).
2308    fn emit_status_batch(&mut self) -> Result<(), String> {
2309        self.begin_observation("syncular_status")?;
2310        let batch = ChangeAccumulator {
2311            status: true,
2312            ..ChangeAccumulator::default()
2313        };
2314        self.finish_observation("syncular_status", batch)
2315            .inspect_err(|_| self.rollback_observation("syncular_status"))
2316    }
2317
2318    /// Release core-owned key material as a host tears the client down,
2319    /// WITHOUT recording a quarantine.
2320    ///
2321    /// Shutting an activated client down is not a quarantine event. Persisting
2322    /// the gate here marks every cleanly closed replica as pending, and the
2323    /// next plain `create` is then refused permanently: the reopen path
2324    /// restores the flag from the marker and the create guard rejects it. The
2325    /// in-memory flag still closes the gate for anything still holding this
2326    /// instance, which is all a teardown barrier needs.
2327    pub fn seal_security_on_teardown(&mut self) {
2328        self.cancel_sync_round();
2329        self.security_preflight = true;
2330        self.security_epoch = self.security_epoch.wrapping_add(1);
2331        self.encryption = crate::values::EncryptionConfig::default();
2332        self.sync_intent_queue.clear();
2333    }
2334
2335    /// Install the post-authentication keyring and release the host loop.
2336    pub fn activate_security(
2337        &mut self,
2338        encryption: crate::values::EncryptionConfig,
2339    ) -> Result<(), String> {
2340        if !self.security_preflight {
2341            return Err(
2342                "sync.invalid_request: activateSecurity requires security preflight".to_owned(),
2343            );
2344        }
2345        // §4.8: an interrupted eviction finishes before protected reads open.
2346        self.drain_pending_evictions()?;
2347        self.enqueue_startup_sync_if_needed()?;
2348        self.delete_meta(SECURITY_PREFLIGHT_PENDING_KEY)?;
2349        self.encryption = encryption;
2350        self.security_preflight = false;
2351        // §5.11: rows sealed under a key this keyring now holds open at once.
2352        self.reveal_sealed_rows()
2353            .map_err(|error| format!("{}: {}", error.code, error.message))?;
2354        self.emit_status_batch()
2355    }
2356
2357    fn clock_now_ms(&self) -> i64 {
2358        self.now_ms
2359            .as_ref()
2360            .map_or_else(crate::wall_clock_ms, |clock| {
2361                clock.load(std::sync::atomic::Ordering::Relaxed)
2362            })
2363    }
2364
2365    fn create_bookkeeping_tables(&self) -> Result<(), String> {
2366        let exists = |name: &str| {
2367            self.conn
2368                .query_row(
2369                    "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?1)",
2370                    [name],
2371                    |row| row.get::<_, bool>(0),
2372                )
2373                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
2374        };
2375        let outbox_existed = exists("_syncular_outbox")?;
2376        // Durable client bookkeeping (outbox + units + meta).
2377        self.conn
2378            .execute_batch(
2379                "CREATE TABLE IF NOT EXISTS _syncular_outbox (
2380                   seq INTEGER PRIMARY KEY AUTOINCREMENT,
2381                   commit_id TEXT NOT NULL UNIQUE, ops_json TEXT NOT NULL);
2382                 CREATE TABLE IF NOT EXISTS _syncular_row_deliveries (
2383                   tbl TEXT NOT NULL, id TEXT NOT NULL, commit_seq INTEGER NOT NULL, PRIMARY KEY(tbl,id));
2384                 CREATE TABLE IF NOT EXISTS _syncular_acked_rows (
2385                   commit_id TEXT NOT NULL, idx INTEGER NOT NULL, tbl TEXT NOT NULL, id TEXT NOT NULL,
2386                   commit_seq INTEGER NOT NULL, op_json TEXT NOT NULL, intent_json TEXT,
2387                   PRIMARY KEY(commit_id, idx));
2388                 CREATE INDEX IF NOT EXISTS _syncular_acked_rows_row
2389                   ON _syncular_acked_rows(tbl, id);
2390                 CREATE TABLE IF NOT EXISTS _syncular_failed_commits (
2391                   seq INTEGER PRIMARY KEY AUTOINCREMENT, client_commit_id TEXT NOT NULL UNIQUE,
2392                   operations_json TEXT NOT NULL, initial_json TEXT NOT NULL);
2393                 CREATE TABLE IF NOT EXISTS _syncular_commit_outcomes (
2394                   seq INTEGER PRIMARY KEY AUTOINCREMENT,
2395                   client_commit_id TEXT NOT NULL UNIQUE,
2396                   status TEXT NOT NULL CHECK(status IN ('applied', 'cached', 'conflict', 'rejected')),
2397                   recorded_at_ms INTEGER NOT NULL,
2398                   results_json TEXT NOT NULL,
2399                   operations_json TEXT,
2400                   resolution TEXT NOT NULL DEFAULT 'active'
2401                     CHECK(resolution IN ('active', 'resolved_keep_server', 'superseded', 'dismissed')),
2402                   resolved_at_ms INTEGER,
2403                   replacement_client_commit_id TEXT);
2404                 CREATE INDEX IF NOT EXISTS _syncular_commit_outcomes_resolution_seq
2405                   ON _syncular_commit_outcomes(resolution, seq);
2406                 CREATE TABLE IF NOT EXISTS _syncular_units (
2407                   id TEXT PRIMARY KEY, scopes TEXT NOT NULL,
2408                   explicit INTEGER NOT NULL DEFAULT 0,
2409                   status TEXT NOT NULL DEFAULT 'active', reason_code TEXT,
2410                   cursor INTEGER DEFAULT -1, bootstrap_state TEXT,
2411                   seeded TEXT NOT NULL DEFAULT '[]', echo TEXT, applied TEXT);
2412                 CREATE TABLE IF NOT EXISTS _syncular_meta (
2413                   key TEXT PRIMARY KEY, value TEXT NOT NULL);
2414                 CREATE TABLE IF NOT EXISTS _syncular_windows (
2415                   base TEXT NOT NULL, unit TEXT NOT NULL, sub_id TEXT NOT NULL,
2416                   PRIMARY KEY (base, unit));
2417                 CREATE INDEX IF NOT EXISTS _syncular_windows_unit ON _syncular_windows(sub_id);
2418                 CREATE TABLE IF NOT EXISTS _syncular_pending_evictions (
2419                   unit_id TEXT NOT NULL, tbl TEXT NOT NULL,
2420                   effective_scopes TEXT NOT NULL, PRIMARY KEY (unit_id, tbl));
2421                 CREATE TABLE IF NOT EXISTS _syncular_blob_commit_refs(
2422                   commit_id TEXT NOT NULL, blob_id TEXT NOT NULL,
2423                   PRIMARY KEY(commit_id, blob_id));
2424                 CREATE INDEX IF NOT EXISTS _syncular_blob_commit_refs_body
2425                   ON _syncular_blob_commit_refs(blob_id);",
2426            )
2427            .map_err(|e| e.to_string())?;
2428        // §5.6 applied-segment record, added after the units table shipped.
2429        if !self
2430            .conn
2431            .prepare("SELECT 1 FROM pragma_table_info('_syncular_units') WHERE name = 'applied'")
2432            .and_then(|mut stmt| stmt.exists([]))
2433            .map_err(|e| e.to_string())?
2434        {
2435            self.conn
2436                .execute_batch("ALTER TABLE _syncular_units ADD COLUMN applied TEXT")
2437                .map_err(|e| e.to_string())?;
2438        }
2439        // Migrate an outcome journal created before failed aggregate
2440        // envelopes were retained. Historical rows intentionally stay NULL.
2441        let _ = self
2442            .conn
2443            .execute_batch("ALTER TABLE _syncular_commit_outcomes ADD COLUMN operations_json TEXT");
2444        if self.get_meta(LOCAL_REVISION_KEY)?.is_none() {
2445            self.set_meta(LOCAL_REVISION_KEY, "0")?;
2446        }
2447        // §7.1: `outboxId` names the outbox sequence. A new outbox table starts
2448        // a new sequence, so it replaces the id in the same transaction.
2449        if !outbox_existed || self.get_meta(OUTBOX_ID_KEY)?.is_none() {
2450            self.set_meta(OUTBOX_ID_KEY, &uuid::Uuid::new_v4().to_string())?;
2451        }
2452        // §9: a replica written under wire version 3 keeps its outbox, journal,
2453        // rows and pending evictions and registers its units fresh. Its
2454        // subscriptions, cursors, echoes, resume tokens and window entries go;
2455        // units left from an earlier version 4 life restart.
2456        if exists("_syncular_window_pending_evict")? {
2457            let invalid: bool = self
2458                .conn
2459                .query_row(
2460                    "SELECT EXISTS(SELECT 1 FROM _syncular_window_pending_evict
2461                       WHERE NOT json_valid(effective_scopes)
2462                          OR json_type(effective_scopes) <> 'object'
2463                          OR EXISTS(SELECT 1 FROM json_each(effective_scopes)
2464                               WHERE type <> 'array'
2465                                  OR EXISTS(SELECT 1 FROM json_each(value) AS v
2466                                       WHERE v.type <> 'text')))",
2467                    [],
2468                    |row| row.get(0),
2469                )
2470                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
2471            if invalid {
2472                return Err(
2473                    "sync.local_corrupt: a version 3 pending eviction cannot be upgraded"
2474                        .to_owned(),
2475                );
2476            }
2477            self.conn
2478                .execute_batch(
2479                    "INSERT OR REPLACE INTO _syncular_pending_evictions
2480                       SELECT sub_id, tbl, effective_scopes FROM _syncular_window_pending_evict;
2481                     DROP TABLE _syncular_window_pending_evict;
2482                     DROP TABLE IF EXISTS _syncular_subscriptions;
2483                     DELETE FROM _syncular_windows;",
2484                )
2485                .and_then(|()| reset_units(&self.conn, &BTreeSet::new()))
2486                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
2487        }
2488        // §5.9.7 blob cache. Mutable upload state stays off the body row so
2489        // completing a large upload does not rewrite its BLOB record.
2490        if self.schema_has_blobs() {
2491            if exists("_syncular_blobs")? {
2492                let mut statement = self
2493                    .conn
2494                    .prepare("PRAGMA table_info(_syncular_blobs)")
2495                    .map_err(|e| e.to_string())?;
2496                let mut columns = statement
2497                    .query_map([], |row| row.get::<_, String>(1))
2498                    .map_err(|e| e.to_string())?
2499                    .collect::<Result<Vec<_>, _>>()
2500                    .map_err(|e| e.to_string())?;
2501                columns.sort();
2502                let mut expected = vec![
2503                    "blob_id",
2504                    "byte_length",
2505                    "bytes",
2506                    "created_at_ms",
2507                    "media_type",
2508                ];
2509                expected.sort_unstable();
2510                if columns != expected {
2511                    return Err(
2512                        "sync.schema_mismatch: Local blob schema is incompatible".to_owned()
2513                    );
2514                }
2515            } else {
2516                self.conn
2517                    .execute_batch(
2518                        "CREATE TABLE _syncular_blobs(
2519                           blob_id TEXT PRIMARY KEY,
2520                           bytes BLOB NOT NULL,
2521                           byte_length INTEGER NOT NULL,
2522                           media_type TEXT,
2523                           created_at_ms INTEGER NOT NULL);",
2524                    )
2525                    .map_err(|e| e.to_string())?;
2526            }
2527            self.conn
2528                .execute_batch(
2529                    "CREATE TABLE IF NOT EXISTS _syncular_blob_uploads(
2530                       blob_id TEXT PRIMARY KEY,
2531                       media_type TEXT,
2532                       created_at_ms INTEGER NOT NULL);",
2533                )
2534                .map_err(|e| e.to_string())?;
2535        }
2536        Ok(())
2537    }
2538
2539    /// True iff any synced table declares a `blob_ref` column (§5.9).
2540    fn schema_has_blobs(&self) -> bool {
2541        self.schema
2542            .tables
2543            .iter()
2544            .any(|t| t.columns.iter().any(|c| c.ty == ColumnType::BlobRef))
2545    }
2546
2547    // -- meta (§7.4.1 marker, bookkeeping) ------------------------------------
2548
2549    fn get_meta(&self, key: &str) -> Result<Option<String>, String> {
2550        meta_get(&self.conn, key)
2551            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
2552    }
2553
2554    // The caller holds the startup/reset transaction. Either both schema
2555    // records persist or the enclosing operation fails and rolls back.
2556    fn persist_schema_version(&self) -> Result<(), String> {
2557        self.conn
2558            .execute(
2559                "INSERT OR REPLACE INTO _syncular_meta(key,value) VALUES (?1,?2)",
2560                rusqlite::params![LOCAL_SCHEMA_VERSION_KEY, self.schema.version.to_string()],
2561            )
2562            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
2563        set_local_schema_descriptor(&self.conn, &self.schema)
2564            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
2565    }
2566
2567    pub(crate) fn set_meta(&self, key: &str, value: &str) -> Result<(), String> {
2568        meta_set(&self.conn, key, value)
2569            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
2570    }
2571
2572    fn delete_meta(&self, key: &str) -> Result<(), String> {
2573        meta_delete(&self.conn, key)
2574            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
2575    }
2576
2577    /// §7.1: the durable outbox in FIFO order, in schema-agnostic local form.
2578    fn load_outbox(&self) -> Result<Vec<Arc<OutboxCommit>>, String> {
2579        let mut stmt = self
2580            .conn
2581            .prepare("SELECT commit_id, ops_json FROM _syncular_outbox ORDER BY seq ASC")
2582            .map_err(|error| error.to_string())?;
2583        let rows = stmt
2584            .query_map([], |row| {
2585                Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
2586            })
2587            .map_err(|error| error.to_string())?;
2588        let mut commits = Vec::new();
2589        for row in rows {
2590            let (client_commit_id, raw) = row.map_err(|error| error.to_string())?;
2591            let entries: Vec<Value> = serde_json::from_str(&raw).map_err(|error| {
2592                format!("invalid persisted outbox {client_commit_id:?}: {error}")
2593            })?;
2594            let mut ops = Vec::with_capacity(entries.len());
2595            for entry in entries {
2596                let op = entry.get("op").and_then(Value::as_str).unwrap_or("delete");
2597                ops.push(OutboxOp {
2598                    upsert: op == "upsert",
2599                    table: entry
2600                        .get("table")
2601                        .and_then(Value::as_str)
2602                        .ok_or_else(|| "persisted outbox operation missing table".to_owned())?
2603                        .to_owned(),
2604                    row_id: entry
2605                        .get("rowId")
2606                        .and_then(Value::as_str)
2607                        .ok_or_else(|| "persisted outbox operation missing rowId".to_owned())?
2608                        .to_owned(),
2609                    base_version: entry.get("baseVersion").and_then(Value::as_i64),
2610                    // A legacy entry's `changedFields` key is ignored:
2611                    // the values map is the presence set (§6.1).
2612                    values: entry.get("values").and_then(Value::as_object).cloned(),
2613                    insert: entry.get("insert") == Some(&Value::Bool(true)),
2614                });
2615            }
2616            commits.push(Arc::new(OutboxCommit {
2617                client_commit_id,
2618                ops,
2619            }));
2620        }
2621        Ok(commits)
2622    }
2623
2624    fn restore_persisted_state(&mut self) -> Result<(), String> {
2625        self.outbox = self.load_outbox()?.into();
2626
2627        self.prune_commit_outcomes()?;
2628        let active = self.commit_outcomes(CommitOutcomeQuery {
2629            active_only: true,
2630            ..CommitOutcomeQuery::default()
2631        })?;
2632        self.conflicts = active
2633            .iter()
2634            .flat_map(|outcome| outcome.results.iter())
2635            .filter_map(|result| match result {
2636                CommitOperationOutcome::Conflict { conflict } => Some(conflict.clone()),
2637                _ => None,
2638            })
2639            .collect::<Vec<_>>()
2640            .into();
2641        self.rejections = active
2642            .iter()
2643            .flat_map(|outcome| outcome.results.iter())
2644            .filter_map(|result| match result {
2645                CommitOperationOutcome::Error { rejection } => Some(rejection.clone()),
2646                _ => None,
2647            })
2648            .collect::<Vec<_>>()
2649            .into();
2650
2651        self.lease_state = self
2652            .get_meta(LEASE_STATE_KEY)?
2653            .map(|raw| serde_json::from_str(&raw))
2654            .transpose()
2655            .map_err(|error| format!("invalid persisted lease state: {error}"))?;
2656        self.schema_floor = self
2657            .get_meta(SCHEMA_FLOOR_KEY)?
2658            .map(|raw| serde_json::from_str(&raw))
2659            .transpose()
2660            .map_err(|error| format!("invalid persisted schema floor: {error}"))?;
2661        self.stopped = self.schema_floor.is_some();
2662        // A replica left in unactivated preflight reopens gated: the quarantine
2663        // decision persists with the data across handle teardown and restart.
2664        if self.get_meta(SECURITY_PREFLIGHT_PENDING_KEY)?.as_deref() == Some("1") {
2665            self.security_preflight = true;
2666        }
2667        Ok(())
2668    }
2669
2670    fn save_unit_scope_schema(&self) -> Result<(), String> {
2671        let declarations: BTreeMap<_, BTreeMap<_, _>> = self
2672            .schema
2673            .tables
2674            .iter()
2675            .map(|table| {
2676                (
2677                    table.name.clone(),
2678                    table
2679                        .scope_variables
2680                        .iter()
2681                        .map(|scope| {
2682                            (
2683                                scope.variable.clone(),
2684                                [scope.prefix.clone(), scope.column.clone()],
2685                            )
2686                        })
2687                        .collect(),
2688                )
2689            })
2690            .collect();
2691        self.set_meta(
2692            "subscriptionScopeSchema",
2693            &serde_json::to_string(&declarations).expect("scope schema serialization"),
2694        )?;
2695        let generations: BTreeMap<_, _> = self
2696            .schema
2697            .tables
2698            .iter()
2699            .filter_map(|table| Some((&table.name, table.generation.as_ref()?)))
2700            .collect();
2701        self.set_meta(
2702            TABLE_GENERATIONS_KEY,
2703            &serde_json::to_string(&generations).expect("generations serialize"),
2704        )
2705    }
2706
2707    /// Drop units that cover no table of the running schema, with their window
2708    /// registry rows and pending evictions; never translate scopes. At a bump
2709    /// (§7.4.3) a unit also needs every variable unchanged in prefix and column
2710    /// on every table it covered before.
2711    fn prune_unknown_units(&mut self, bump: bool) -> Result<(), String> {
2712        let previous: Option<BTreeMap<String, BTreeMap<String, [String; 2]>>> = if bump {
2713            self.get_meta("subscriptionScopeSchema")?
2714                .map(|raw| serde_json::from_str(&raw))
2715                .transpose()
2716                .map_err(|_| "sync.local_corrupt: invalid persisted scope schema".to_owned())?
2717        } else {
2718            None
2719        };
2720        // A table keeps the variables' meaning when each has the same prefix
2721        // and column as before. An old binary stored no declaration evidence;
2722        // matching names cannot prove unchanged meaning, so the app registers
2723        // again.
2724        let unchanged = |name: &str, variables: &[&String]| {
2725            let (Some(old), Some(table)) = (
2726                previous.as_ref().and_then(|previous| previous.get(name)),
2727                self.schema.table(name),
2728            ) else {
2729                return false;
2730            };
2731            variables.iter().all(|variable| {
2732                table
2733                    .scope_variables
2734                    .iter()
2735                    .find(|scope| &scope.variable == *variable)
2736                    .is_some_and(|scope| {
2737                        old.get(*variable) == Some(&[scope.prefix.clone(), scope.column.clone()])
2738                    })
2739            })
2740        };
2741        let compatible = |scopes: &UnitScopes| {
2742            if !self.schema.tables.iter().any(|table| covers(table, scopes)) {
2743                return false;
2744            }
2745            if !bump {
2746                return true;
2747            }
2748            let Some(previous) = &previous else {
2749                return false;
2750            };
2751            previous.iter().all(|(name, old)| {
2752                !scopes.keys().all(|variable| old.contains_key(variable))
2753                    || unchanged(name, &scopes.keys().collect::<Vec<_>>())
2754            })
2755        };
2756        let mut stale: BTreeSet<String> = self
2757            .stored(load_units(&self.conn))?
2758            .into_iter()
2759            .filter(|unit| !compatible(&unit.scopes))
2760            .map(|unit| unit.id)
2761            .collect();
2762        // A shrunk window's deferred eviction outlives its unit row and keeps
2763        // its per-table scopes. A table the schema no longer has cannot be
2764        // evicted, and at a bump a table whose scope declarations changed must
2765        // not be evicted by the outdated rule.
2766        for (id, table, effective) in self.load_pending_evictions()? {
2767            if self.schema.table(&table).is_none()
2768                || (bump
2769                    && !unchanged(
2770                        &table,
2771                        &effective
2772                            .iter()
2773                            .map(|(variable, _)| variable)
2774                            .collect::<Vec<_>>(),
2775                    ))
2776            {
2777                stale.insert(id);
2778            }
2779        }
2780        for id in &stale {
2781            for sql in [
2782                "DELETE FROM _syncular_windows WHERE sub_id = ?1",
2783                "DELETE FROM _syncular_pending_evictions WHERE unit_id = ?1",
2784                "DELETE FROM _syncular_units WHERE id = ?1",
2785            ] {
2786                self.conn
2787                    .execute(sql, rusqlite::params![id])
2788                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
2789            }
2790        }
2791        Ok(())
2792    }
2793
2794    pub fn local_revision(&self) -> Result<u64, String> {
2795        Ok(self
2796            .get_meta(LOCAL_REVISION_KEY)?
2797            .and_then(|value| value.parse().ok())
2798            .unwrap_or(0))
2799    }
2800
2801    /// §7.5: resolve a snapshot read on the mutable owner connection through
2802    /// the same primitive the read-only sidecar uses, so the command surface
2803    /// and the file sidecar return one revision, rows, coverage, catch-up, and
2804    /// delivery shape. The structured [`QueryReadFailure`] is preserved; an
2805    /// `owner` records its failure (and its next success) in diagnostics.
2806    pub fn snapshot_read(
2807        &mut self,
2808        request: &SnapshotReadRequest<'_>,
2809        owner: Option<QueryOwner<'_>>,
2810    ) -> Result<SnapshotRead, QueryReadFailure> {
2811        let result = snapshot_read_connection(&self.conn, request);
2812        if let Some(owner) = owner {
2813            self.record_query_read(owner, result.as_ref().err());
2814        }
2815        result
2816    }
2817
2818    #[must_use]
2819    pub fn status_snapshot(&self) -> SyncStatusSnapshot {
2820        SyncStatusSnapshot {
2821            current_schema_version: self.schema.version,
2822            outbox: self.outbox.len(),
2823            upgrading: self.upgrading,
2824            lease_state: self.lease_state.clone(),
2825            schema_floor: self.schema_floor.clone(),
2826            sync_needed: self.sync_needed,
2827            previous_version_context: self.previous_version_status(),
2828            security_lifecycle: self.security_lifecycle(),
2829        }
2830    }
2831
2832    /// RFC 0005 A2: container presence for a host that must act on it (Diego's
2833    /// rollback precondition), read without creating the file.
2834    fn previous_version_status(&self) -> PreviousVersionStatus {
2835        let Some(replica_path) = self.previous_version_replica_path() else {
2836            return PreviousVersionStatus {
2837                present: false,
2838                created_at_ms: None,
2839            };
2840        };
2841        match crate::previous_version::read_previous_version_record(&replica_path) {
2842            Ok(Some(record)) => PreviousVersionStatus {
2843                present: true,
2844                created_at_ms: Some(record.created_at_ms),
2845            },
2846            _ => PreviousVersionStatus {
2847                present: std::path::Path::new(
2848                    &crate::previous_version::previous_version_container_path(&replica_path),
2849                )
2850                .exists(),
2851                created_at_ms: None,
2852            },
2853        }
2854    }
2855
2856    /// RFC 0005 D7: read captured rows from the previous local schema. `state`
2857    /// is always `previousVersion`; the container never makes
2858    /// `query_snapshot()` coverage complete. Lease stop/expiry, scope
2859    /// revocation, coverage completion and the TTL DISCARD the container and
2860    /// close the read with their own reason.
2861    pub fn previous_version_snapshot(
2862        &self,
2863        spec: &PreviousVersionReadSpec,
2864    ) -> Result<PreviousVersionSnapshot, String> {
2865        let now_ms = self.clock_now_ms();
2866        crate::previous_version::previous_version_snapshot(
2867            &self.conn,
2868            self.previous_version_replica_path().as_deref(),
2869            self.previous_version.as_ref(),
2870            PreviousVersionLifecycle {
2871                lease_inactive: self.previous_version_lease_inactive(now_ms),
2872                scope_revoked: self
2873                    .stored(load_units(&self.conn))?
2874                    .iter()
2875                    .any(|unit| unit.status == UnitStatus::Revoked),
2876                coverage_complete: self.previous_version_coverage_complete()?,
2877            },
2878            now_ms,
2879            self.schema.version,
2880            spec,
2881        )
2882    }
2883
2884    /// RFC 0005 D6: the advisory pre-reset compatibility audit.
2885    pub fn previous_version_audit(
2886        &self,
2887    ) -> Result<Option<crate::previous_version::PreviousVersionAudit>, String> {
2888        crate::previous_version::stored_previous_version_audit(&self.conn)
2889            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
2890    }
2891
2892    /// RFC 0005 A2/D9: the executable downgrade step. Drop the container file
2893    /// and both metadata records, and report whether anything was present.
2894    /// Idempotent: absence is a successful no-op.
2895    pub fn previous_version_discard(
2896        &self,
2897    ) -> Result<crate::previous_version::PreviousVersionDiscardOutcome, String> {
2898        self.drop_previous_version()
2899    }
2900
2901    fn drop_previous_version(
2902        &self,
2903    ) -> Result<crate::previous_version::PreviousVersionDiscardOutcome, String> {
2904        let Some(replica_path) = self.previous_version_replica_path() else {
2905            return Ok(crate::previous_version::PreviousVersionDiscardOutcome {
2906                present: false,
2907                discarded: false,
2908            });
2909        };
2910        crate::previous_version::discard_previous_version(&self.conn, &replica_path)
2911    }
2912
2913    /// RFC 0005 D7: `leaseState.errorCode` or an expired lease.
2914    fn previous_version_lease_inactive(&self, now_ms: i64) -> bool {
2915        let Some(lease) = &self.lease_state else {
2916            return false;
2917        };
2918        if lease.error_code.is_some() {
2919            return true;
2920        }
2921        lease.expires_at_ms.is_some_and(|expires| expires <= now_ms)
2922    }
2923
2924    /// RFC 0005 D7: §7.4.5 replacement coverage — no active unit is
2925    /// bootstrapping. An empty active set is not completion (mirrors the TS
2926    /// predicate exactly).
2927    fn previous_version_coverage_complete(&self) -> Result<bool, String> {
2928        let units = self.stored(load_units(&self.conn))?;
2929        let mut active = units
2930            .iter()
2931            .filter(|unit| unit.status == UnitStatus::Active)
2932            .peekable();
2933        Ok(active.peek().is_some() && !active.any(UnitRecord::bootstrapping))
2934    }
2935
2936    /// RFC 0005 D7: lifetime triggers evaluated once per sync round, after the
2937    /// apply. A container that no longer has a reason to exist is discarded so
2938    /// the rows do not outlive the window they were kept for.
2939    fn previous_version_lifetime_check(&self) {
2940        let Some(config) = self.previous_version.filter(|config| config.enabled) else {
2941            return;
2942        };
2943        let now_ms = self.clock_now_ms();
2944        if self.previous_version_lease_inactive(now_ms) {
2945            let _ = self.drop_previous_version();
2946            return;
2947        }
2948        let Some(replica_path) = self.previous_version_replica_path() else {
2949            return;
2950        };
2951        let Ok(Some(record)) = crate::previous_version::read_previous_version_record(&replica_path)
2952        else {
2953            return;
2954        };
2955        let Ok(units) = load_units(&self.conn) else {
2956            return;
2957        };
2958        if units.iter().any(|unit| unit.status == UnitStatus::Revoked) {
2959            let _ = self.drop_previous_version();
2960            return;
2961        }
2962        if now_ms - record.created_at_ms > config.max_age_ms {
2963            let _ = self.drop_previous_version();
2964            return;
2965        }
2966        if self.previous_version_coverage_complete() == Ok(true) {
2967            let _ = self.drop_previous_version();
2968        }
2969    }
2970
2971    pub fn diagnostics_snapshot(&self) -> Result<ClientDiagnosticsSnapshot, String> {
2972        let registered = self.stored(load_units(&self.conn))?;
2973        // Both meta values in one statement: the FFI host recomputes
2974        // diagnostics after every command.
2975        let (local_revision, replica_cursor): (Option<String>, Option<String>) = self.stored(
2976            self.conn
2977                .prepare_cached(
2978                    "SELECT (SELECT value FROM _syncular_meta WHERE key = ?1),
2979                            (SELECT value FROM _syncular_meta WHERE key = ?2)",
2980                )
2981                .and_then(|mut statement| {
2982                    statement.query_row([LOCAL_REVISION_KEY, REPLICA_CURSOR_KEY], |row| {
2983                        Ok((row.get(0)?, row.get(1)?))
2984                    })
2985                }),
2986        )?;
2987        let mut units = DiagnosticUnits::default();
2988        for unit in &registered {
2989            *match unit.status {
2990                UnitStatus::Revoked => &mut units.revoked,
2991                UnitStatus::Failed => &mut units.failed,
2992                UnitStatus::Active if !unit.bootstrapping() => &mut units.complete,
2993                UnitStatus::Active
2994                    if unit.reason_code.as_deref() == Some("sync.cursor_expired") =>
2995                {
2996                    &mut units.reset
2997                }
2998                UnitStatus::Active => &mut units.bootstrapping,
2999            } += 1;
3000        }
3001        units.reason_codes = registered
3002            .iter()
3003            .filter_map(|unit| unit.reason_code.as_deref().map(Self::diagnostic_code))
3004            .collect::<BTreeSet<_>>()
3005            .into_iter()
3006            .collect();
3007        let captured_at_ms = self.clock_now_ms();
3008        let lease = if let Some(error_code) = self
3009            .lease_state
3010            .as_ref()
3011            .and_then(|state| state.error_code.clone())
3012        {
3013            ClientDiagnosticsLease {
3014                state: "stopped".to_owned(),
3015                expires_at_ms: self
3016                    .lease_state
3017                    .as_ref()
3018                    .and_then(|state| state.expires_at_ms),
3019                error_code: Some(Self::diagnostic_code(&error_code)),
3020            }
3021        } else if let Some(expires_at_ms) = self
3022            .lease_state
3023            .as_ref()
3024            .and_then(|state| state.expires_at_ms)
3025        {
3026            ClientDiagnosticsLease {
3027                state: if expires_at_ms <= captured_at_ms {
3028                    "expired".to_owned()
3029                } else {
3030                    "active".to_owned()
3031                },
3032                expires_at_ms: Some(expires_at_ms),
3033                error_code: None,
3034            }
3035        } else {
3036            ClientDiagnosticsLease {
3037                state: "none".to_owned(),
3038                expires_at_ms: None,
3039                error_code: None,
3040            }
3041        };
3042        let connectivity = match self.last_round.as_ref() {
3043            _ if !self.transport_enabled && self.active_round.is_none() => "offline",
3044            Some(round) if round.status == "succeeded" => "online",
3045            Some(round)
3046                if round.status == "failed"
3047                    && matches!(
3048                        round.cause_kind,
3049                        Some(
3050                            crate::TransportCauseKind::Network | crate::TransportCauseKind::Connect
3051                        )
3052                    ) =>
3053            {
3054                "offline"
3055            }
3056            Some(round) if round.status == "failed" && round.http_status.is_some() => "online",
3057            _ => "unknown",
3058        };
3059        Ok(ClientDiagnosticsSnapshot {
3060            version: CLIENT_DIAGNOSTICS_VERSION,
3061            captured_at_ms,
3062            host: ClientDiagnosticsHost {
3063                kind: "direct".to_owned(),
3064                role: "single".to_owned(),
3065                connectivity: connectivity.to_owned(),
3066                realtime: self.realtime_state,
3067                realtime_policy: self.realtime_policy,
3068                realtime_reason_code: self.realtime_reason_code.clone(),
3069                realtime_retry_delay_ms: self.realtime_retry_delay_ms,
3070            },
3071            security_lifecycle: self.security_lifecycle().to_owned(),
3072            schema: ClientDiagnosticsSchema {
3073                current_version: self.schema.version,
3074                upgrading: self.upgrading,
3075                required_version: self
3076                    .schema_floor
3077                    .as_ref()
3078                    .and_then(|floor| floor.required_schema_version),
3079                latest_version: self
3080                    .schema_floor
3081                    .as_ref()
3082                    .and_then(|floor| floor.latest_schema_version),
3083            },
3084            replica: ClientDiagnosticsReplica {
3085                local_revision: local_revision
3086                    .and_then(|value| value.parse::<u64>().ok())
3087                    .unwrap_or(0)
3088                    .to_string(),
3089                sync_needed: self.sync_needed,
3090                pending_outbox: self.outbox.len(),
3091            },
3092            lease,
3093            active_units: registered
3094                .iter()
3095                .filter(|unit| unit.status == UnitStatus::Active)
3096                .count(),
3097            units,
3098            replica_cursor: replica_cursor.map_or(Ok(-1), |raw| {
3099                raw.parse()
3100                    .map_err(|_| "sync.local_corrupt: persisted unit state is invalid".to_owned())
3101            })?,
3102            last_round: self.last_round.clone(),
3103            last_change: self.last_change.clone(),
3104            storage: self.diagnostics_storage(),
3105            query_failures: self.query_failures.clone(),
3106        })
3107    }
3108
3109    fn diagnostics_storage(&self) -> ClientDiagnosticsStorage {
3110        let read = || -> Result<ClientDiagnosticsStorage, rusqlite::Error> {
3111            let page_count: i64 = self
3112                .conn
3113                .query_row("PRAGMA page_count", [], |row| row.get(0))?;
3114            let page_size: i64 = self
3115                .conn
3116                .query_row("PRAGMA page_size", [], |row| row.get(0))?;
3117            // One cached aggregate, since the FFI host recomputes diagnostics
3118            // after every command; every invocation still reads current storage.
3119            let (outbox_bytes, outcome_entries, outcome_bytes): (i64, i64, i64) = self
3120                .conn
3121                .prepare_cached(
3122                    "SELECT (SELECT COALESCE(SUM(LENGTH(ops_json)), 0) FROM _syncular_outbox),
3123                            COUNT(*), COALESCE(SUM(LENGTH(results_json) + COALESCE(LENGTH(operations_json), 0)), 0)
3124                       FROM _syncular_commit_outcomes",
3125                )?
3126                .query_row([], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)))?;
3127            let blob_bytes = if self.schema_has_blobs() {
3128                self.conn
3129                    .prepare_cached("SELECT COALESCE(SUM(byte_length), 0) FROM _syncular_blobs")?
3130                    .query_row([], |row| row.get(0))?
3131            } else {
3132                0
3133            };
3134            let pressure = self
3135                .limits
3136                .blob_cache_max_bytes
3137                .is_some_and(|limit| blob_bytes > limit);
3138            Ok(ClientDiagnosticsStorage {
3139                status: if pressure { "pressure" } else { "healthy" }.to_owned(),
3140                database_bytes_approx: Some(page_count.saturating_mul(page_size).max(0)),
3141                pending_outbox_bytes_approx: Some(outbox_bytes.max(0)),
3142                retained_outcome_bytes_approx: Some(outcome_bytes.max(0)),
3143                retained_outcome_entries: Some(outcome_entries.max(0)),
3144                blob_cache_bytes_approx: Some(blob_bytes.max(0)),
3145                pressure_reason_code: pressure.then(|| "client.blob_cache_over_limit".to_owned()),
3146            })
3147        };
3148        read().unwrap_or_else(|_| ClientDiagnosticsStorage {
3149            status: "unreadable".to_owned(),
3150            database_bytes_approx: None,
3151            pending_outbox_bytes_approx: None,
3152            retained_outcome_bytes_approx: None,
3153            retained_outcome_entries: None,
3154            blob_cache_bytes_approx: None,
3155            pressure_reason_code: None,
3156        })
3157    }
3158
3159    pub fn drain_change_batches(&mut self) -> Vec<ClientChangeBatch> {
3160        self.change_queue.drain(..).collect()
3161    }
3162
3163    pub fn drain_sync_intents(&mut self) -> Vec<SyncIntent> {
3164        let intents = self.sync_intent_queue.drain(..).collect();
3165        if self.transport_enabled {
3166            intents
3167        } else {
3168            vec![SyncIntent::None]
3169        }
3170    }
3171
3172    fn schedule_background_retry(&mut self) -> u64 {
3173        let delay_ms = self.retry_delay_ms;
3174        self.sync_intent_queue
3175            .push_back(SyncIntent::Background { delay_ms });
3176        self.retry_delay_ms = (self.retry_delay_ms * 2).min(30_000);
3177        self.round_retry_delay_ms = Some(delay_ms);
3178        delay_ms
3179    }
3180
3181    fn reset_background_retry(&mut self) {
3182        self.retry_delay_ms = 250;
3183    }
3184
3185    fn retryable_transport_code(code: &str) -> bool {
3186        code == "transport.failed"
3187            || code == "transport.timeout"
3188            || code == "transport.unavailable"
3189            || code == "sync.transport_failed"
3190    }
3191
3192    /// §1.8: the transport reported a refused or unreachable endpoint. The
3193    /// native transport says `connect` (DNS failure, connection refused,
3194    /// reset, or aborted); a browser fetch transport says `network`. Repeating
3195    /// the request on a backoff repairs neither, so no retry is scheduled.
3196    fn unreachable_transport(details: &Option<serde_json::Value>) -> bool {
3197        matches!(
3198            details
3199                .as_ref()
3200                .and_then(|details| details.get("causeKind"))
3201                .and_then(serde_json::Value::as_str),
3202            Some("connect" | "network")
3203        )
3204    }
3205
3206    /// §1.5: a segment fetch that fails inside an otherwise complete round is
3207    /// invisible to the server (the round response arrived whole), so its code
3208    /// rides the following requests like a socket-round failure. A code the
3209    /// frame grammar rejects is not reportable and the previous code stays.
3210    fn segment_fetch_failed(&mut self, error: TransportError) -> SectionError {
3211        if ssp2::model::valid_round_failure_code(&error.code) {
3212            self.round_failure = Some(error.code.clone());
3213        }
3214        SectionError::Transport(error)
3215    }
3216
3217    /// A failed round worth a background retry: a transport failure, or a
3218    /// §10.2 code the catalog marks retryable (clients may hardcode the
3219    /// catalog metadata, §10.1). Matches the TypeScript core, which retries
3220    /// on the server's `retryable` flag.
3221    fn retryable_failure_code(code: &str) -> bool {
3222        Self::retryable_transport_code(code)
3223            || matches!(
3224                code,
3225                "sync.auth_required"
3226                    | "sync.auth_lease_required"
3227                    | "sync.auth_lease_revoked"
3228                    | "sync.internal_error"
3229                    | "sync.idempotency_cache_miss"
3230                    | "sync.schema_not_ready"
3231                    | "sync.segment_expired"
3232                    | "sync.rate_limited"
3233                    | "sync.websocket_connection_limit"
3234                    | "sync.idle_timeout"
3235            )
3236    }
3237
3238    fn diagnostic_code(code: &str) -> String {
3239        let valid = !code.is_empty()
3240            && code.len() <= 96
3241            && code.contains('.')
3242            && code
3243                .bytes()
3244                .next()
3245                .is_some_and(|byte| byte.is_ascii_lowercase())
3246            && code.bytes().all(|byte| {
3247                byte.is_ascii_lowercase()
3248                    || byte.is_ascii_digit()
3249                    || matches!(byte, b'.' | b'_' | b'-')
3250            });
3251        if valid {
3252            code.to_owned()
3253        } else {
3254            "client.unknown_failure".to_owned()
3255        }
3256    }
3257
3258    fn set_sync_needed(&mut self, value: bool, interactive: bool) {
3259        if self.sync_needed != value {
3260            if self.begin_observation("syncular_status").is_ok() {
3261                self.sync_needed = value;
3262                let batch = ChangeAccumulator {
3263                    status: true,
3264                    ..ChangeAccumulator::default()
3265                };
3266                if self.finish_observation("syncular_status", batch).is_err() {
3267                    self.rollback_observation("syncular_status");
3268                }
3269            } else {
3270                self.sync_needed = value;
3271            }
3272        }
3273        if value && interactive {
3274            self.sync_intent_queue.push_back(SyncIntent::Interactive);
3275        }
3276    }
3277
3278    /// A unit-store result: a row that does not parse is `sync.local_corrupt`;
3279    /// every other SQLite error keeps its storage classification.
3280    fn stored<T>(&self, result: rusqlite::Result<T>) -> Result<T, String> {
3281        result.map_err(|error| match error {
3282            rusqlite::Error::FromSqlConversionFailure(..) => {
3283                "sync.local_corrupt: persisted unit state is invalid".to_owned()
3284            }
3285            error => Self::sqlite_failure(&self.storage_failure, error),
3286        })
3287    }
3288
3289    fn sqlite_failure(
3290        storage_failure: &RefCell<Option<QueryReadFailure>>,
3291        error: rusqlite::Error,
3292    ) -> String {
3293        let message = error.to_string();
3294        let failure = QueryReadFailure::from(error);
3295        let code = failure.code;
3296        if code.is_some() && storage_failure.borrow().is_none() {
3297            *storage_failure.borrow_mut() = Some(failure);
3298        }
3299        match code {
3300            Some(code) => format!("{code}: {message}"),
3301            None => message,
3302        }
3303    }
3304
3305    fn begin_observation(&self, name: &str) -> Result<(), String> {
3306        if let Some(failure) = &self.coalesced_failure {
3307            return Err(failure.clone());
3308        }
3309        // SQLite rolls a whole transaction back on some I/O and capacity
3310        // errors; later frames must not commit on their own after that.
3311        if self.coalesced.is_some() && self.conn.is_autocommit() {
3312            return Err("the coalesced apply transaction was rolled back".to_owned());
3313        }
3314        self.conn
3315            .execute_batch(&format!("SAVEPOINT {name}"))
3316            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
3317    }
3318
3319    /// SPEC §1.4 rule 4: open a coalesced apply transaction unless a caller
3320    /// already owns one.
3321    fn begin_coalesced_apply(&mut self) -> Result<(), String> {
3322        if self.coalesced.is_some() || !self.conn.is_autocommit() {
3323            return Ok(());
3324        }
3325        self.conn
3326            .execute_batch("BEGIN")
3327            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
3328        self.coalesced = Some(CoalescedApply {
3329            rows: 0,
3330            rollback: self.capture(),
3331        });
3332        Ok(())
3333    }
3334
3335    fn capture(&self) -> Rollback {
3336        Rollback {
3337            outbox: self.outbox.clone(),
3338            failed_commits: self.failed_commits.clone(),
3339            conflicts: self.conflicts.clone(),
3340            rejections: self.rejections.clone(),
3341            schema_floor: self.schema_floor.clone(),
3342            lease_state: self.lease_state.clone(),
3343            stopped: self.stopped,
3344            upgrading: self.upgrading,
3345            sync_needed: self.sync_needed,
3346            active_round: self.active_round,
3347            overlay_dirty: self.overlay_dirty.snapshot(),
3348            change_queue: self.change_queue.len(),
3349            sync_intent_queue: self.sync_intent_queue.len(),
3350            last_change: self.last_change.clone(),
3351        }
3352    }
3353
3354    fn restore(&mut self, rollback: Rollback) {
3355        self.outbox = rollback.outbox;
3356        self.failed_commits = rollback.failed_commits;
3357        self.conflicts = rollback.conflicts;
3358        self.rejections = rollback.rejections;
3359        self.schema_floor = rollback.schema_floor;
3360        self.lease_state = rollback.lease_state;
3361        self.stopped = rollback.stopped;
3362        self.upgrading = rollback.upgrading;
3363        self.sync_needed = rollback.sync_needed;
3364        self.active_round = rollback.active_round;
3365        self.overlay_dirty.restore(rollback.overlay_dirty);
3366        self.change_queue.truncate(rollback.change_queue);
3367        self.sync_intent_queue.truncate(rollback.sync_intent_queue);
3368        self.last_change = rollback.last_change;
3369    }
3370
3371    /// Account the rows of the next frame or block. A unit that would take the
3372    /// coalesced transaction past the bound commits the earlier units first.
3373    fn coalesce_unit(&mut self, rows: usize) -> Result<(), String> {
3374        let Some(open) = &self.coalesced else {
3375            return Ok(());
3376        };
3377        if open.rows > 0 && open.rows + rows > COALESCED_APPLY_ROWS {
3378            self.commit_coalesced_apply()?;
3379            self.begin_coalesced_apply()?;
3380        }
3381        if let Some(open) = &mut self.coalesced {
3382            open.rows += rows;
3383        }
3384        Ok(())
3385    }
3386
3387    /// COMMIT the coalesced transaction. A failed COMMIT rolls every coalesced
3388    /// frame back, restores the in-memory state of its `BEGIN`, drops the
3389    /// change batches and sync intents queued since, and fails the rest of the
3390    /// response.
3391    fn commit_coalesced_apply(&mut self) -> Result<(), String> {
3392        let Some(open) = self.coalesced.take() else {
3393            return Ok(());
3394        };
3395        let committed = if self.conn.is_autocommit() {
3396            Err("the coalesced apply transaction was rolled back".to_owned())
3397        } else {
3398            self.conn
3399                .execute_batch("COMMIT")
3400                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
3401        };
3402        let Err(error) = committed else {
3403            return Ok(());
3404        };
3405        if !self.conn.is_autocommit() {
3406            if let Err(rollback) = self.conn.execute_batch("ROLLBACK") {
3407                let _ = Self::sqlite_failure(&self.storage_failure, rollback);
3408            }
3409        }
3410        self.restore(open.rollback);
3411        self.coalesced_failure = Some(error.clone());
3412        Err(error)
3413    }
3414
3415    /// End the response's coalescing: COMMIT what is open and clear a failure.
3416    fn finish_coalesced_apply(&mut self) -> Result<(), String> {
3417        let committed = self.commit_coalesced_apply();
3418        match self.coalesced_failure.take() {
3419            Some(failure) => Err(failure),
3420            None => committed,
3421        }
3422    }
3423
3424    fn rollback_observation(&self, name: &str) {
3425        if let Err(error) = self
3426            .conn
3427            .execute_batch(&format!("ROLLBACK TO {name}; RELEASE {name}"))
3428        {
3429            if let Some(failure) = self.storage_failure.borrow_mut().as_mut() {
3430                if failure.rollback_failure.is_none() {
3431                    failure.rollback_failure = Some(Box::new(QueryReadFailure::from(error)));
3432                }
3433            }
3434        }
3435    }
3436
3437    fn finish_observation(
3438        &mut self,
3439        name: &str,
3440        mut batch: ChangeAccumulator,
3441    ) -> Result<(), String> {
3442        #[cfg(feature = "bench-internals")]
3443        let _phase = self.benchmark_phases.start(Phase::ObservationCommit);
3444        if self.failed_commits.iter().any(|(commit, _)| {
3445            commit
3446                .ops
3447                .iter()
3448                .any(|operation| batch.tables.contains_key(&operation.table))
3449        }) {
3450            batch.outcomes = true;
3451        }
3452        if !batch.touched() {
3453            self.conn
3454                .execute_batch(&format!("RELEASE {name}"))
3455                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
3456            return Ok(());
3457        }
3458        let revision = self
3459            .local_revision()?
3460            .checked_add(1)
3461            .ok_or_else(|| "local revision exhausted u64".to_owned())?;
3462        self.conn
3463            .execute(
3464                "INSERT OR REPLACE INTO _syncular_meta(key, value) VALUES (?1, ?2)",
3465                rusqlite::params![LOCAL_REVISION_KEY, revision.to_string()],
3466            )
3467            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
3468        let status = batch.status.then(|| self.status_snapshot());
3469        let event = ClientChangeBatch {
3470            revision: revision.to_string(),
3471            tables: batch
3472                .tables
3473                .into_iter()
3474                .map(|(table, scope_keys)| TableChange {
3475                    table,
3476                    scope_keys: scope_keys.map(|keys| keys.into_iter().collect()),
3477                })
3478                .collect(),
3479            windows: batch
3480                .windows
3481                .into_iter()
3482                .map(|(base_key, units)| WindowChange {
3483                    base_key,
3484                    units: units.into_iter().collect(),
3485                })
3486                .collect(),
3487            status,
3488            conflicts_changed: batch.conflicts,
3489            rejections_changed: batch.rejections,
3490            outcomes_changed: batch.outcomes,
3491            applied_commits: Vec::new(),
3492        };
3493        self.conn
3494            .execute_batch(&format!("RELEASE {name}"))
3495            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
3496        let mut diagnostic_tables = event
3497            .tables
3498            .iter()
3499            .map(|entry| entry.table.clone())
3500            .collect::<BTreeSet<_>>()
3501            .into_iter()
3502            .collect::<Vec<_>>();
3503        // §7.6: the selected variables of the changed window bases.
3504        let mut diagnostic_windows = event
3505            .windows
3506            .iter()
3507            .map(|entry| {
3508                entry
3509                    .base_key
3510                    .split('\0')
3511                    .next()
3512                    .unwrap_or_default()
3513                    .to_owned()
3514            })
3515            .collect::<BTreeSet<_>>()
3516            .into_iter()
3517            .collect::<Vec<_>>();
3518        let domains_truncated = diagnostic_tables.len() > MAX_DIAGNOSTIC_DOMAINS
3519            || diagnostic_windows.len() > MAX_DIAGNOSTIC_DOMAINS;
3520        diagnostic_tables.truncate(MAX_DIAGNOSTIC_DOMAINS);
3521        diagnostic_windows.truncate(MAX_DIAGNOSTIC_DOMAINS);
3522        self.last_change = Some(DiagnosticLastChange {
3523            revision: event.revision.clone(),
3524            recorded_at_ms: self.clock_now_ms(),
3525            tables: diagnostic_tables,
3526            windows: diagnostic_windows,
3527            domains_truncated,
3528            status_changed: event.status.is_some(),
3529            conflicts_changed: event.conflicts_changed,
3530            rejections_changed: event.rejections_changed,
3531            outcomes_changed: event.outcomes_changed,
3532        });
3533        self.change_queue.push_back(event);
3534        Ok(())
3535    }
3536
3537    fn record_scope_map(
3538        &self,
3539        batch: &mut ChangeAccumulator,
3540        table_name: &str,
3541        scopes: &[(String, Vec<String>)],
3542    ) {
3543        let Some(table) = self.schema.table(table_name) else {
3544            return;
3545        };
3546        // A bulk scoped delete whose map leaves a declared variable unnamed
3547        // cannot list the deleted rows' keys for it: the change is table-wide.
3548        if table.scope_variables.iter().any(|scope| {
3549            !scopes
3550                .iter()
3551                .any(|(variable, _)| *variable == scope.variable)
3552        }) {
3553            batch.table(table_name);
3554            return;
3555        }
3556        for (variable, values) in scopes {
3557            let Some(scope) = table
3558                .scope_variables
3559                .iter()
3560                .find(|scope| &scope.variable == variable)
3561            else {
3562                continue;
3563            };
3564            for value in values {
3565                batch.scope(table_name, format!("{}:{value}", scope.prefix));
3566            }
3567        }
3568    }
3569
3570    /// Record a row's current scope keys from the base or visible table.
3571    fn record_row_scopes(
3572        &self,
3573        batch: &mut ChangeAccumulator,
3574        table_name: &str,
3575        row_id: &str,
3576        base: bool,
3577    ) -> bool {
3578        let Some(table) = self.schema.table(table_name) else {
3579            return false;
3580        };
3581        if table.scope_variables.is_empty() {
3582            return false;
3583        }
3584        let columns = table
3585            .scope_variables
3586            .iter()
3587            .map(|scope| quote_ident(&scope.column))
3588            .collect::<Vec<_>>()
3589            .join(", ");
3590        let full_table = if base {
3591            base_table(table_name)
3592        } else {
3593            visible_table(table_name)
3594        };
3595        let sql = format!(
3596            "SELECT {columns} FROM {full_table} WHERE {} LIMIT 1",
3597            row_id_predicate(table)
3598        );
3599        let Ok(mut stmt) = self.conn.prepare_cached(&sql) else {
3600            return false;
3601        };
3602        let values = stmt.query_row(rusqlite::params![row_id], |row| {
3603            let mut values = Vec::with_capacity(table.scope_variables.len());
3604            for index in 0..table.scope_variables.len() {
3605                values.push(row.get::<_, Option<String>>(index)?);
3606            }
3607            Ok(values)
3608        });
3609        let Ok(values) = values else {
3610            return false;
3611        };
3612        let mut recorded = false;
3613        for (scope, value) in table.scope_variables.iter().zip(values) {
3614            if let Some(value) = value {
3615                batch.scope(table_name, format!("{}:{value}", scope.prefix));
3616                recorded = true;
3617            }
3618        }
3619        recorded
3620    }
3621
3622    fn record_commit_changes(
3623        &self,
3624        batch: &mut ChangeAccumulator,
3625        tables: &[String],
3626        changes: &[ssp2::model::Change],
3627    ) {
3628        #[cfg(feature = "bench-internals")]
3629        let _phase = self.benchmark_phases.start(Phase::ObservationPrepare);
3630        for change in changes {
3631            let Some(table_name) = tables.get(change.table_index as usize) else {
3632                continue;
3633            };
3634            let mut precise = self.record_row_scopes(batch, table_name, &change.row_id, true);
3635            if let Some(table) = self.schema.table(table_name) {
3636                for (variable, value) in &change.scopes {
3637                    if let Some(scope) = table
3638                        .scope_variables
3639                        .iter()
3640                        .find(|scope| &scope.variable == variable)
3641                    {
3642                        batch.scope(table_name, format!("{}:{value}", scope.prefix));
3643                        precise = true;
3644                    }
3645                }
3646            }
3647            if !precise {
3648                batch.table(table_name);
3649            }
3650        }
3651    }
3652
3653    /// Whether a base or visible row matches the scope predicate; `false`
3654    /// without a local scope-column mapping. A SQLite failure is an error.
3655    fn scoped_rows_exist(
3656        &self,
3657        table_name: &str,
3658        effective: &[(String, Vec<String>)],
3659        owners: &[Vec<(String, Vec<String>)>],
3660    ) -> Result<bool, String> {
3661        let Some(table) = self.schema.table(table_name) else {
3662            return Ok(false);
3663        };
3664        let Ok(Some((predicate, params))) = local_scope_predicate(table, effective, owners) else {
3665            return Ok(false);
3666        };
3667        for source in [base_table(table_name), visible_table(table_name)] {
3668            if self
3669                .conn
3670                .query_row(
3671                    &format!("SELECT EXISTS(SELECT 1 FROM {source} WHERE {predicate})"),
3672                    rusqlite::params_from_iter(&params),
3673                    |row| row.get::<_, bool>(0),
3674                )
3675                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
3676            {
3677                return Ok(true);
3678            }
3679        }
3680        Ok(false)
3681    }
3682
3683    /// §5.6 first-page clear of `unit_id`'s pair: delete its rows except
3684    /// those another owner covers (§3.3), failing closed without a local
3685    /// scope-column mapping. Owners are read only when a row matches, inside
3686    /// the caller's transaction. Returns whether rows matched.
3687    fn clear_scope(
3688        &mut self,
3689        table: &TableSchema,
3690        effective: &[(String, Vec<String>)],
3691        unit_id: &str,
3692    ) -> Result<bool, SectionError> {
3693        let storage = |message| SectionError::Abort("storage.failed".to_owned(), message);
3694        let matched = self
3695            .scoped_rows_exist(&table.name, effective, &[])
3696            .map_err(storage)?;
3697        let owners = if matched {
3698            unit_owners(
3699                &self.stored(load_units(&self.conn)).map_err(storage)?,
3700                table,
3701                unit_id,
3702            )
3703        } else {
3704            Vec::new()
3705        };
3706        let changed = matched
3707            && (owners.is_empty()
3708                || self
3709                    .scoped_rows_exist(&table.name, effective, &owners)
3710                    .map_err(storage)?);
3711        if !self
3712            .purge_scope_rows(&table.name, effective, &owners)
3713            .map_err(storage)?
3714        {
3715            return Err(SectionError::FailClosed);
3716        }
3717        Ok(changed)
3718    }
3719
3720    /// §7.4.5: true while a schema-bump reset + first re-bootstrap runs.
3721    pub fn upgrading(&self) -> bool {
3722        self.upgrading
3723    }
3724
3725    /// §7.4.2 "app ships new code": swap to a NEW generated schema while
3726    /// keeping this client's local database (identity, outbox, tables). The
3727    /// §7.4.1 marker check then fires the wipe/re-bootstrap flow when the
3728    /// version increases. Recreation runs the same marker guard as opening
3729    /// a durable replica, holds the guard and every write it authorizes under
3730    /// one write transaction, and preserves the current client (compiled
3731    /// schema, outbox, tables) on refusal.
3732    pub fn recreate_with_schema(&mut self, schema_json: &Value) -> Result<(), String> {
3733        let new_schema = parse_schema_json(schema_json)?;
3734        validate_authority_reads(&new_schema, &self.limits.authority_reads)?;
3735        self.conn
3736            .execute_batch("BEGIN IMMEDIATE")
3737            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
3738        let prior_schema = std::mem::replace(&mut self.schema, new_schema);
3739        // §7.4.3/§7.4.4: a reset inside this transaction mutates in-memory
3740        // state (dropped outbox commits, rejections, upgrade and floor flags,
3741        // overlay dirtiness) and appends change/intent batches. A COMMIT that
3742        // fails after the reset succeeded rolls the database back, so every
3743        // one of those fields is captured here and restored with it.
3744        let rollback = self.capture();
3745        let guarded = (|| -> Result<(), String> {
3746            // §7.4.2: the marker is re-read under the write lock the caller
3747            // holds, so a replica another process upgraded meanwhile refuses
3748            // this recreation instead of having its marker and tables
3749            // downgraded.
3750            let marker = read_local_schema_version(&self.conn, self.schema.version)?;
3751            self.create_bookkeeping_tables()?;
3752            match marker {
3753                Some(version) if version < self.schema.version => self.run_schema_reset()?,
3754                None => {
3755                    self.create_synced_tables()?;
3756                    self.persist_schema_version()?;
3757                }
3758                _ => {}
3759            }
3760            // §7.4.2: recreation IS the boot for the in-memory core, so reload the
3761            // durable outbox the same-version path is about to replay. Only the
3762            // outbox is reloaded: the recovery below reads it, and the caller's
3763            // rollback snapshot already covers this field.
3764            self.outbox = self.load_outbox()?.into();
3765            // §7.1/§7.4.4: recover intrinsically codec-invalid persisted values at
3766            // this guarded boundary once the schema and marker agree. A bump went
3767            // through `run_schema_reset`, which performs the same recovery after
3768            // it creates the new physical tables and before its final replay.
3769            self.drop_unencodable_outbox(false, None)?;
3770            if marker == Some(self.schema.version)
3771                && (!self.outbox.is_empty()
3772                    || !self.failed_commits.is_empty()
3773                    || self.has_acknowledged_rows()?)
3774            {
3775                self.overlay_dirty.set(true);
3776                self.rebuild_overlay()?;
3777            }
3778            self.prune_unknown_units(false)?;
3779            self.save_unit_scope_schema()?;
3780            // The conformance recreate is the in-memory equivalent of reopening
3781            // a durable client. Apply the same startup catch-up contract even
3782            // when the schema itself did not change.
3783            self.enqueue_startup_sync_if_needed()?;
3784            Ok(())
3785        })();
3786        let outcome = match guarded {
3787            Ok(()) => self
3788                .conn
3789                .execute_batch("COMMIT")
3790                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error)),
3791            Err(error) => Err(error),
3792        };
3793        if let Err(error) = outcome {
3794            if let Err(rollback) = self.conn.execute_batch("ROLLBACK") {
3795                // Keep a failed rollback's typed storage classification.
3796                let _ = Self::sqlite_failure(&self.storage_failure, rollback);
3797            }
3798            self.schema = prior_schema;
3799            self.restore(rollback);
3800            // Prepared insert statements were derived from the reverted schema.
3801            self.insert_sql.borrow_mut().clear();
3802            return Err(error);
3803        }
3804        if !self.security_preflight {
3805            self.drain_pending_evictions()?;
3806        }
3807        Ok(())
3808    }
3809
3810    fn enqueue_startup_sync_if_needed(&mut self) -> Result<(), String> {
3811        let startup_work = !self.stopped
3812            && (!self.outbox.is_empty()
3813                || self.has_acknowledged_rows()?
3814                || !self.load_pending_evictions()?.is_empty()
3815                || self
3816                    .stored(load_units(&self.conn))?
3817                    .iter()
3818                    .any(|unit| unit.status == UnitStatus::Active));
3819        if startup_work {
3820            self.sync_needed = true;
3821            self.sync_intent_queue.push_back(SyncIntent::Interactive);
3822        }
3823        Ok(())
3824    }
3825
3826    /// A native client persists schema-floor stops across process restarts.
3827    /// Once the running app already satisfies that floor, the persisted stop
3828    /// is only stale evidence from an older server round (for example, the
3829    /// server was restarted after catching up to an app that was ahead).
3830    /// Clear it and let the normal startup pull re-negotiate. If the server is
3831    /// still incompatible it will return the floor again in that first round.
3832    fn clear_satisfied_persisted_schema_floor(&mut self) -> Result<(), String> {
3833        let satisfied = self
3834            .schema_floor
3835            .as_ref()
3836            .and_then(|floor| floor.required_schema_version)
3837            .is_some_and(|required| self.schema.version >= required);
3838        if !satisfied {
3839            return Ok(());
3840        }
3841        self.delete_meta(SCHEMA_FLOOR_KEY)?;
3842        self.schema_floor = None;
3843        self.stopped = false;
3844        Ok(())
3845    }
3846
3847    /// RFC 0005 D1: discard an orphan or stale container at boot. The reset
3848    /// path already swept it; this covers a same-version or fresh-install open,
3849    /// which runs no reset and therefore no sweep — plus the D7 TTL, which only
3850    /// an aware binary can apply.
3851    fn reconcile_previous_version_at_boot(&self) -> Result<(), String> {
3852        let Some(replica_path) = self.previous_version_replica_path() else {
3853            return Ok(());
3854        };
3855        let max_age_ms = self
3856            .previous_version
3857            .filter(|config| config.enabled)
3858            .map(|config| config.max_age_ms);
3859        reconcile_previous_version_at_boot(
3860            &self.conn,
3861            &replica_path,
3862            self.schema.version,
3863            max_age_ms,
3864            self.clock_now_ms(),
3865        )
3866    }
3867
3868    /// RFC 0005 D3: the sibling container path, or `None` when the replica has
3869    /// no file — an in-memory replica returns an empty path, and a `file:` URI
3870    /// has no plain sibling path — so the feature resolves as off there.
3871    fn previous_version_replica_path(&self) -> Option<String> {
3872        let path = self.conn.path()?;
3873        if path.is_empty() || path.starts_with("file:") {
3874            return None;
3875        }
3876        Some(path.to_owned())
3877    }
3878
3879    /// §7.4.3 reset: whole-database local reset EXCEPT the outbox, clientId,
3880    /// and leaseState. Drops/recreates every synced table from the new
3881    /// schema, resets unit sync state (keeping registrations), clears
3882    /// the schema-floor stop state, rewrites the marker, drops outbox commits
3883    /// that cannot re-encode (§7.4.4), and replays the survivors on top.
3884    fn run_schema_reset(&mut self) -> Result<(), String> {
3885        self.begin_observation("syncular_schema_reset")?;
3886        let rollback = self.capture();
3887        let mut batch = ChangeAccumulator::default();
3888        let result = self
3889            .run_schema_reset_observed(&mut batch, true)
3890            .and_then(|()| self.finish_observation("syncular_schema_reset", batch));
3891        if let Err(error) = result {
3892            self.rollback_observation("syncular_schema_reset");
3893            self.restore(rollback);
3894            return Err(error);
3895        }
3896        Ok(())
3897    }
3898
3899    fn run_log_epoch_reset(&mut self, log_epoch: &str) -> Result<Vec<String>, String> {
3900        if self.get_meta(LOG_EPOCH_KEY)?.is_none() {
3901            self.begin_observation("syncular_epoch_acquisition")?;
3902            let rollback = self.capture();
3903            if let Err(error) = self.conn.execute(
3904                "INSERT OR REPLACE INTO _syncular_meta(key,value) VALUES (?1,?2)",
3905                rusqlite::params![LOG_EPOCH_KEY, log_epoch],
3906            ) {
3907                self.rollback_observation("syncular_epoch_acquisition");
3908                return Err(Self::sqlite_failure(&self.storage_failure, error));
3909            }
3910            self.sync_needed = true;
3911            let batch = ChangeAccumulator {
3912                status: true,
3913                ..ChangeAccumulator::default()
3914            };
3915            if let Err(error) = self.finish_observation("syncular_epoch_acquisition", batch) {
3916                self.rollback_observation("syncular_epoch_acquisition");
3917                self.restore(rollback);
3918                return Err(error);
3919            }
3920            self.sync_intent_queue.push_back(SyncIntent::Interactive);
3921            return Ok(Vec::new());
3922        }
3923        let resets = self
3924            .stored(load_units(&self.conn))?
3925            .into_iter()
3926            .map(|unit| unit.id)
3927            .collect();
3928        self.begin_observation("syncular_log_epoch_reset")?;
3929        let rollback = self.capture();
3930        let mut batch = ChangeAccumulator::default();
3931        let result = self
3932            .run_schema_reset_observed(&mut batch, false)
3933            .and_then(|()| {
3934                self.conn
3935                    .execute(
3936                        "INSERT OR REPLACE INTO _syncular_meta(key,value) VALUES (?1,?2)",
3937                        rusqlite::params![LOG_EPOCH_KEY, log_epoch],
3938                    )
3939                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
3940                self.sync_needed = true;
3941                batch.status = true;
3942                self.finish_observation("syncular_log_epoch_reset", batch)
3943            });
3944        if let Err(error) = result {
3945            self.rollback_observation("syncular_log_epoch_reset");
3946            self.restore(rollback);
3947            return Err(error);
3948        }
3949        self.sync_intent_queue.push_back(SyncIntent::Interactive);
3950        Ok(resets)
3951    }
3952
3953    fn run_schema_reset_observed(
3954        &mut self,
3955        batch: &mut ChangeAccumulator,
3956        capture: bool,
3957    ) -> Result<(), String> {
3958        // §7.4.2: the reset is the only path that wipes local tables for a
3959        // version bump, and it rewrites the marker last. Re-read the marker
3960        // under this reset's transaction: a replica another process upgraded
3961        // meanwhile refuses here instead of being reset to this client's
3962        // older schema.
3963        read_local_schema_version(&self.conn, self.schema.version)?;
3964        // §7.4.3: a schema bump keeps every table whose generation equals the
3965        // persisted one; the log-epoch reset keeps nothing. The previous
3966        // declaration evidence decides which units cover only kept tables.
3967        let mut kept = BTreeSet::new();
3968        let mut previous_declarations: Option<BTreeMap<String, BTreeMap<String, Value>>> = None;
3969        if capture {
3970            let persisted: BTreeMap<String, String> = self
3971                .get_meta(TABLE_GENERATIONS_KEY)?
3972                .map(|raw| serde_json::from_str(&raw))
3973                .transpose()
3974                .map_err(|_| "sync.local_corrupt: invalid persisted table generations".to_owned())?
3975                .unwrap_or_default();
3976            kept = self
3977                .schema
3978                .tables
3979                .iter()
3980                .filter(|table| {
3981                    table.generation.is_some()
3982                        && table.generation.as_ref() == persisted.get(&table.name)
3983                })
3984                .map(|table| table.name.clone())
3985                .collect();
3986            previous_declarations = self
3987                .get_meta("subscriptionScopeSchema")?
3988                .map(|raw| serde_json::from_str(&raw))
3989                .transpose()
3990                .map_err(|_| "sync.local_corrupt: invalid persisted scope schema".to_owned())?;
3991        }
3992        // Kept tables keep both halves, their FTS projections with the FTS5
3993        // shadow tables and id mapping, and their row-level bookkeeping.
3994        let mut kept_objects = BTreeSet::new();
3995        for table in self
3996            .schema
3997            .tables
3998            .iter()
3999            .filter(|table| kept.contains(&table.name))
4000        {
4001            kept_objects.insert(table.name.clone());
4002            kept_objects.insert(format!("_syncular_base_{}", table.name));
4003            for index in &table.fts_indexes {
4004                kept_objects.insert(index.name.clone());
4005                for shadow in ["data", "idx", "content", "docsize", "config"] {
4006                    kept_objects.insert(format!("{}_{shadow}", index.name));
4007                }
4008            }
4009        }
4010        let kept_json = serde_json::to_string(&kept).expect("table names serialize");
4011        for sql in [
4012            "DELETE FROM _syncular_acked_rows WHERE tbl NOT IN (SELECT value FROM json_each(?1))",
4013            "DELETE FROM _syncular_row_deliveries WHERE tbl NOT IN (SELECT value FROM json_each(?1))",
4014        ] {
4015            self.conn
4016                .execute(sql, [&kept_json])
4017                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4018        }
4019        self.cancel_sync_round();
4020        self.upgrading = true;
4021        batch.status = true;
4022        // RFC 0005 D6: classify the pending outbox against the NEW compiled
4023        // schema BEFORE the wipe, and record the advisory audit. This pass
4024        // drops nothing, so the audit sees the original pending intent; the
4025        // §7.4.4 send-time drop and the intrinsic value recovery later in this
4026        // reset are the paths that remove a commit. The classification is
4027        // shared with the send-time drop so the two cannot disagree about what
4028        // is incompatible.
4029        if self.previous_version.is_some_and(|config| config.enabled) {
4030            let previous_version = self
4031                .get_meta(LOCAL_SCHEMA_VERSION_KEY)?
4032                .and_then(|value| value.parse::<i32>().ok())
4033                .unwrap_or(self.schema.version);
4034            let pending = self
4035                .outbox
4036                .iter()
4037                .map(|commit| PendingCommitAudit {
4038                    commit_id: &commit.client_commit_id,
4039                    operations: commit_audit_operations(commit),
4040                })
4041                .collect::<Vec<_>>();
4042            crate::previous_version::write_previous_version_audit(
4043                &self.conn,
4044                &crate::previous_version::build_previous_version_audit(
4045                    &self.schema,
4046                    &pending,
4047                    previous_version,
4048                    self.schema.version,
4049                    self.clock_now_ms(),
4050                ),
4051            )?;
4052        }
4053        // RFC 0005 D5: the container lives in its own FILE, so the reset can
4054        // never reach it. Sweep first (unconditional — also on the log-epoch
4055        // reset, which captures nothing), then capture BEFORE the wipe drops
4056        // any row. A refused capture records its reason and leaves no file.
4057        if let Some(replica_path) = self.previous_version_replica_path() {
4058            sweep_previous_version_container(&self.conn, &replica_path)?;
4059            if capture {
4060                if let Some(config) = self.previous_version.filter(|config| config.enabled) {
4061                    let previous_version = self
4062                        .get_meta(LOCAL_SCHEMA_VERSION_KEY)?
4063                        .and_then(|value| value.parse::<i32>().ok())
4064                        .unwrap_or(self.schema.version);
4065                    capture_previous_version_from_replica(
4066                        &self.conn,
4067                        &replica_path,
4068                        previous_version,
4069                        self.schema.version,
4070                        &config,
4071                        self.clock_now_ms(),
4072                    )?;
4073                }
4074            }
4075        }
4076        for table in self
4077            .schema
4078            .tables
4079            .iter()
4080            .filter(|table| !kept.contains(&table.name))
4081        {
4082            batch.table(&table.name);
4083        }
4084        for (base_key, value) in self.load_window_registry()? {
4085            batch.window(&base_key, &value);
4086        }
4087        if capture {
4088            self.prune_unknown_units(true)?;
4089        }
4090        // The per-table insert SQL is derived from the OLD column lists.
4091        self.insert_sql.borrow_mut().clear();
4092        self.overlay_dirty.set(true);
4093        // Drop every synced table (base + visible) that currently exists —
4094        // discovered from sqlite_master so a bump that adds/removes tables is
4095        // handled. Bookkeeping tables (`_syncular_outbox/_units/_meta`
4096        // and the blob cache) are preserved; base tables are `_syncular_base_*`
4097        // so they are matched explicitly, not by the bookkeeping filter.
4098        // Drop virtual tables first so SQLite removes every FTS shadow table
4099        // atomically instead of the generic discovery tearing it apart.
4100        let virtual_tables: Vec<String> = {
4101            let mut stmt = self
4102                .conn
4103                .prepare(
4104                    "SELECT name FROM sqlite_master WHERE type = 'table' AND sql LIKE 'CREATE VIRTUAL TABLE%'",
4105                )
4106                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4107            let rows = stmt
4108                .query_map([], |row| row.get::<_, String>(0))
4109                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4110            rows.filter_map(Result::ok)
4111                .filter(|name| is_synced_table_name(name) && !kept_objects.contains(name))
4112                .collect()
4113        };
4114        for name in virtual_tables {
4115            self.conn
4116                .execute_batch(&format!(
4117                    "DROP TABLE IF EXISTS {}",
4118                    quote_ident(&format!("_syncular_fts_{name}"))
4119                ))
4120                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4121            self.conn
4122                .execute(&format!("DROP TABLE IF EXISTS {}", quote_ident(&name)), [])
4123                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4124        }
4125        let existing: Vec<String> = {
4126            let mut stmt = self
4127                .conn
4128                .prepare("SELECT name FROM sqlite_master WHERE type = 'table'")
4129                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4130            let rows = stmt
4131                .query_map([], |row| row.get::<_, String>(0))
4132                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4133            rows.filter_map(Result::ok)
4134                .filter(|name| is_synced_table_name(name) && !kept_objects.contains(name))
4135                .collect()
4136        };
4137        for name in &existing {
4138            if let Some(table) = name.strip_prefix("_syncular_base_") {
4139                batch.table(table);
4140            } else if !name.starts_with("_syncular_") {
4141                batch.table(name);
4142            }
4143        }
4144        for name in existing {
4145            self.conn
4146                .execute(&format!("DROP TABLE IF EXISTS {}", quote_ident(&name)), [])
4147                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4148        }
4149        // Recreate the synced tables from the NEW schema.
4150        self.create_synced_tables()?;
4151        // Reset the sync state of every unit that covers a table the reset
4152        // did not keep, under the previous declarations or the new schema,
4153        // keeping the registration.
4154        let keep_units = match &previous_declarations {
4155            Some(previous) if !kept.is_empty() => self
4156                .stored(load_units(&self.conn))?
4157                .into_iter()
4158                .filter(|unit| {
4159                    previous.iter().all(|(name, old)| {
4160                        kept.contains(name) || !unit.scopes.keys().all(|v| old.contains_key(v))
4161                    }) && self
4162                        .schema
4163                        .tables
4164                        .iter()
4165                        .all(|table| kept.contains(&table.name) || !covers(table, &unit.scopes))
4166                })
4167                .map(|unit| unit.id)
4168                .collect(),
4169            _ => BTreeSet::new(),
4170        };
4171        reset_units(&self.conn, &keep_units)
4172            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4173        // The stop state is over: this client now ships a servable schema.
4174        self.stopped = false;
4175        self.schema_floor = None;
4176        self.delete_meta(SCHEMA_FLOOR_KEY)?;
4177        // Rewrite the marker LAST so a crash mid-reset re-runs the reset.
4178        self.persist_schema_version()?;
4179        self.save_unit_scope_schema()?;
4180        // §7.1/§7.4.4: the previous-version capture and audit above saw the
4181        // original pending intent; the synced tables now carry the new schema, so
4182        // recover intrinsically codec-invalid persisted values here, before the
4183        // final replay. A pending upsert that names a column the new schema
4184        // removed stays pending for the send-time `drop_incompatible_outbox`
4185        // prepass. The reset wrapper snapshots and restores the outbox,
4186        // rejections, and change queues, so a failed commit rolls this recovery
4187        // back with the rest of the reset.
4188        // §7.4.4: the enclosing reset observation owns the publication, so the
4189        // recovery contributes its rejection, outcome, and table domains to it.
4190        self.drop_unencodable_outbox(false, Some(batch))?;
4191        // Re-apply the surviving outbox optimistically over the empty tables.
4192        self.rebuild_overlay()?;
4193        Ok(())
4194    }
4195
4196    /// §7.4.4 send-time prepass: a persisted upsert whose values reference a
4197    /// column the current schema lacks (or a removed table) cannot be encoded
4198    /// (`commit_audit_operations` inspects upserts, so a value-free delete stays
4199    /// encodable and is validated by the server), nor can an insert-intent
4200    /// upsert that lacks a non-nullable column (§7.1, the audit's
4201    /// `missing-required-column`). Drop the commit, undo
4202    /// its optimistic projection, and raise a client-local
4203    /// `sync.outbox_incompatible` rejection.
4204    fn drop_incompatible_outbox(&mut self) -> Result<bool, String> {
4205        let incompatible = self
4206            .outbox
4207            .iter()
4208            .filter(|commit| {
4209                first_incompatibility(&self.schema, &commit_audit_operations(commit)).is_some()
4210            })
4211            .cloned()
4212            .collect::<Vec<_>>();
4213        if incompatible.is_empty() {
4214            return Ok(false);
4215        }
4216        self.drop_outbox_commits(
4217            &incompatible,
4218            OUTBOX_INCOMPATIBLE_CODE,
4219            "the persisted commit cannot encode under the current schema",
4220        )?;
4221        Ok(true)
4222    }
4223
4224    /// §7.4.4 incompatible-commit path, also taken by a refused blob upload
4225    /// (§5.9.7) with the server's code: reject every operation of `commits`,
4226    /// remove them from the outbox, and undo their optimistic rows.
4227    fn drop_outbox_commits(
4228        &mut self,
4229        incompatible: &[Arc<OutboxCommit>],
4230        code: &str,
4231        message: &str,
4232    ) -> Result<(), String> {
4233        // §7.4.4: one observation covers the durable outcome writes, the outbox
4234        // removal, the in-memory drop, and the overlay rebuild, so a storage
4235        // fault leaves the replica and the client unchanged and publishes no
4236        // change; a successful drop publishes exactly one batch carrying the
4237        // rejections, outcomes, status, and the tables whose projection changed.
4238        let rollback = self.capture();
4239        let owns_transaction = self.conn.is_autocommit();
4240        self.begin_observation("syncular_outbox_drop")?;
4241        let mut batch = ChangeAccumulator::default();
4242        let dropped = (|| -> Result<(), String> {
4243            let mut rejections = Vec::new();
4244            for commit in incompatible {
4245                let results = commit
4246                    .ops
4247                    .iter()
4248                    .enumerate()
4249                    .map(|(op_index, operation)| {
4250                        let rejection = RejectionRecord {
4251                            client_commit_id: commit.client_commit_id.clone(),
4252                            op_index: op_index as i32,
4253                            code: code.to_owned(),
4254                            message: message.to_owned(),
4255                            retryable: false,
4256                            details: None,
4257                            operation: Some(CommitOperation::from(operation)),
4258                        };
4259                        rejections.push(rejection.clone());
4260                        CommitOperationOutcome::Error { rejection }
4261                    })
4262                    .collect::<Vec<_>>();
4263                self.persist_commit_outcome(
4264                    &commit.client_commit_id,
4265                    CommitOutcomeStatus::Rejected,
4266                    &results,
4267                    Some(&commit.ops),
4268                )?;
4269                self.delete_outbox_persisted(&commit.client_commit_id)?;
4270                // A removed table has no local projection to invalidate; every
4271                // surviving table the dropped commit touched does.
4272                for operation in &commit.ops {
4273                    if self.schema.table(&operation.table).is_some() {
4274                        batch.table(&operation.table);
4275                    }
4276                }
4277            }
4278            self.prune_commit_outcomes()?;
4279            let incompatible_ids = incompatible
4280                .iter()
4281                .map(|commit| commit.client_commit_id.as_str())
4282                .collect::<BTreeSet<_>>();
4283            self.outbox
4284                .retain(|commit| !incompatible_ids.contains(commit.client_commit_id.as_str()));
4285            self.rejections.extend(rejections);
4286            // The dropped commits' purely-optimistic rows are undone: the
4287            // visible projection is re-derived from the surviving base + outbox.
4288            self.overlay_dirty.set(true);
4289            self.rebuild_overlay()?;
4290            batch.status = true;
4291            batch.rejections = true;
4292            batch.outcomes = true;
4293            Ok(())
4294        })();
4295        let result = dropped.and_then(|()| self.finish_observation("syncular_outbox_drop", batch));
4296        if let Err(error) = result {
4297            // An outer RELEASE is the COMMIT. Roll back that transaction
4298            // directly: retrying RELEASE after ROLLBACK TO still needs the
4299            // reader's lock. A nested observation only rolls back its savepoint.
4300            if owns_transaction {
4301                if let Err(rollback) = self.conn.execute_batch("ROLLBACK") {
4302                    let mut failure = self.storage_failure.borrow_mut();
4303                    let failure =
4304                        failure.get_or_insert_with(|| QueryReadFailure::from(error.clone()));
4305                    failure.rollback_failure = Some(Box::new(QueryReadFailure::from(rollback)));
4306                }
4307            } else {
4308                self.rollback_observation("syncular_outbox_drop");
4309            }
4310            self.restore(rollback);
4311            return Err(error);
4312        }
4313        Ok(())
4314    }
4315
4316    /// §7.4.3: (re)create the base + visible table pair for every synced
4317    /// table in the CURRENT schema (idempotent — `IF NOT EXISTS`).
4318    fn create_synced_tables(&self) -> Result<(), String> {
4319        for table in &self.schema.tables {
4320            // The base half + the visible half form the synced-table pair. An
4321            // index name is global in SQLite, so the base half's indexes are
4322            // name-prefixed (`_syncular_base_<index>`) to stay distinct.
4323            for (full, index_prefix) in [
4324                (base_table(&table.name), "_syncular_base_"),
4325                (visible_table(&table.name), ""),
4326            ] {
4327                let mut cols: Vec<String> =
4328                    table.columns.iter().map(|c| quote_ident(&c.name)).collect();
4329                cols.push("\"_syncular_version\" INTEGER NOT NULL".to_owned());
4330                cols.extend(
4331                    table
4332                        .sealed_columns()
4333                        .iter()
4334                        .map(|name| format!("{} TEXT", quote_ident(name))),
4335                );
4336                let sql = format!(
4337                    "CREATE TABLE IF NOT EXISTS {full} ({} , PRIMARY KEY ({}))",
4338                    cols.join(", "),
4339                    quote_ident(&table.primary_key)
4340                );
4341                self.conn
4342                    .execute(&sql, [])
4343                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4344                // §5.11: a replica created before sealed apply gains the
4345                // reserved columns in place, on both halves in the same order,
4346                // so data, indexes and the FTS projection stay.
4347                for name in table.sealed_columns() {
4348                    let present: bool = self
4349                        .conn
4350                        .query_row(
4351                            "SELECT EXISTS(SELECT 1 FROM pragma_table_info(?1) WHERE name = ?2)",
4352                            rusqlite::params![format!("{index_prefix}{}", table.name), name],
4353                            |row| row.get(0),
4354                        )
4355                        .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4356                    if !present {
4357                        self.conn
4358                            .execute(
4359                                &format!(
4360                                    "ALTER TABLE {full} ADD COLUMN {} TEXT",
4361                                    quote_ident(name)
4362                                ),
4363                                [],
4364                            )
4365                            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4366                    }
4367                }
4368                // The reveal at every round start reads the sealed rows of the
4369                // base half through this partial index.
4370                if table.has_encrypted_columns() && !index_prefix.is_empty() {
4371                    self.conn
4372                        .execute(
4373                            &format!(
4374                                "CREATE INDEX IF NOT EXISTS {} ON {full} (\"_sync_sealed_key\") WHERE \"_sync_sealed_key\" IS NOT NULL",
4375                                quote_ident(&format!("_syncular_sealed_{}", table.name))
4376                            ),
4377                            [],
4378                        )
4379                        .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4380                }
4381                // Local secondary indexes (CREATE INDEX subset). Created on
4382                // both halves so mirror reads hit an index on either. Runs on
4383                // both the initial create and the §7.4.3 reset recreate path.
4384                for index in &table.indexes {
4385                    let unique = if index.unique { "UNIQUE " } else { "" };
4386                    let index_name = quote_ident(&format!("{index_prefix}{}", index.name));
4387                    let cols_sql = index
4388                        .columns
4389                        .iter()
4390                        .map(|c| quote_ident(c))
4391                        .collect::<Vec<_>>()
4392                        .join(", ");
4393                    let index_sql = format!(
4394                        "CREATE {unique}INDEX IF NOT EXISTS {index_name} ON {full} ({cols_sql})"
4395                    );
4396                    self.conn
4397                        .execute(&index_sql, [])
4398                        .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4399                }
4400            }
4401        }
4402        // FTS exists only on the visible half. It is a local search
4403        // projection, never a synced/base table or a wire-schema member.
4404        for table in &self.schema.tables {
4405            for index in &table.fts_indexes {
4406                self.create_fts_projection(table, index)?;
4407            }
4408        }
4409        Ok(())
4410    }
4411
4412    fn fts_projection_exists(&self, index: &FtsIndexSchema) -> Result<bool, String> {
4413        let count: i64 = self
4414            .conn
4415            .query_row(
4416                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?1",
4417                rusqlite::params![index.name],
4418                |row| row.get(0),
4419            )
4420            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4421        Ok(count > 0)
4422    }
4423
4424    fn drop_fts_triggers(&self, index: &FtsIndexSchema) -> Result<(), String> {
4425        for suffix in ["ai", "ad", "au"] {
4426            self.conn
4427                .execute(
4428                    &format!(
4429                        "DROP TRIGGER IF EXISTS {}",
4430                        quote_ident(&format!("{}_{suffix}", index.name))
4431                    ),
4432                    [],
4433                )
4434                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4435        }
4436        Ok(())
4437    }
4438
4439    fn create_fts_triggers(
4440        &self,
4441        table: &TableSchema,
4442        index: &FtsIndexSchema,
4443    ) -> Result<(), String> {
4444        let fts = quote_ident(&index.name);
4445        let source = visible_table(&table.name);
4446        let source_id = quote_ident(FTS_SOURCE_ID_COLUMN);
4447        let pk = quote_ident(&table.primary_key);
4448        let projection_columns = std::iter::once(source_id.clone())
4449            .chain(index.columns.iter().map(|column| quote_ident(column)))
4450            .collect::<Vec<_>>()
4451            .join(", ");
4452        let new_values = std::iter::once(format!("CAST(new.{pk} AS TEXT)"))
4453            .chain(
4454                index
4455                    .columns
4456                    .iter()
4457                    .map(|column| format!("new.{}", quote_ident(column))),
4458            )
4459            .collect::<Vec<_>>()
4460            .join(", ");
4461        let mapping = quote_ident(&format!("_syncular_fts_{}", index.name));
4462        let delete_new = format!("DELETE FROM {fts} WHERE rowid = (SELECT id FROM {mapping} WHERE source_id = CAST(new.{pk} AS TEXT)); DELETE FROM {mapping} WHERE source_id = CAST(new.{pk} AS TEXT)");
4463        let delete_old = format!("DELETE FROM {fts} WHERE rowid = (SELECT id FROM {mapping} WHERE source_id = CAST(old.{pk} AS TEXT)); DELETE FROM {mapping} WHERE source_id = CAST(old.{pk} AS TEXT)");
4464        let insert_new = format!("INSERT INTO {mapping}(source_id) VALUES (CAST(new.{pk} AS TEXT)); INSERT INTO {fts} (rowid, {projection_columns}) VALUES ((SELECT id FROM {mapping} WHERE source_id = CAST(new.{pk} AS TEXT)), {new_values})");
4465        let bi = quote_ident(&format!("{}_bi", index.name));
4466        let ai = quote_ident(&format!("{}_ai", index.name));
4467        let ad = quote_ident(&format!("{}_ad", index.name));
4468        let au = quote_ident(&format!("{}_au", index.name));
4469        let replacement_exists = format!("EXISTS (SELECT 1 FROM {source} WHERE {pk} = new.{pk})");
4470        let sql = format!(
4471            "DROP TRIGGER IF EXISTS {bi};
4472             DROP TRIGGER IF EXISTS {ai};
4473             DROP TRIGGER IF EXISTS {ad};
4474             DROP TRIGGER IF EXISTS {au};
4475             CREATE TRIGGER {bi} BEFORE INSERT ON {source} WHEN {replacement_exists} BEGIN {delete_new}; END;
4476             CREATE TRIGGER {ai} AFTER INSERT ON {source} BEGIN {insert_new}; END;
4477             CREATE TRIGGER {ad} AFTER DELETE ON {source} BEGIN {delete_old}; END;
4478             CREATE TRIGGER {au} AFTER UPDATE ON {source} BEGIN {delete_old}; {delete_new}; {insert_new}; END;",
4479        );
4480        self.conn
4481            .execute_batch(&sql)
4482            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
4483    }
4484
4485    fn rebuild_fts_projection(
4486        &self,
4487        table: &TableSchema,
4488        index: &FtsIndexSchema,
4489    ) -> Result<(), String> {
4490        let fts = quote_ident(&index.name);
4491        let source_id = quote_ident(FTS_SOURCE_ID_COLUMN);
4492        let pk = quote_ident(&table.primary_key);
4493        let indexed_columns = index
4494            .columns
4495            .iter()
4496            .map(|column| quote_ident(column))
4497            .collect::<Vec<_>>();
4498        let projection_columns = std::iter::once(source_id.clone())
4499            .chain(indexed_columns.iter().cloned())
4500            .collect::<Vec<_>>()
4501            .join(", ");
4502        let mapping = quote_ident(&format!("_syncular_fts_{}", index.name));
4503        let sql = format!(
4504            "DELETE FROM {fts}; DELETE FROM {mapping}; INSERT INTO {fts} ({projection_columns}) SELECT CAST({pk} AS TEXT), {columns} FROM {source}; INSERT INTO {mapping}(id, source_id) SELECT rowid, {source_id} FROM {fts};",
4505            columns = indexed_columns.join(", "),
4506            source = visible_table(&table.name),
4507        );
4508        self.conn
4509            .execute_batch(&sql)
4510            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
4511    }
4512
4513    fn create_fts_projection(
4514        &self,
4515        table: &TableSchema,
4516        index: &FtsIndexSchema,
4517    ) -> Result<(), String> {
4518        self.begin_observation("syncular_fts_schema")?;
4519        let result = (|| {
4520            let existed = self.fts_projection_exists(index)?;
4521            let tokenizer = index.tokenize.replace('\'', "''");
4522            let columns = index
4523                .columns
4524                .iter()
4525                .map(|column| quote_ident(column))
4526                .collect::<Vec<_>>()
4527                .join(", ");
4528            let sql = format!(
4529            "CREATE VIRTUAL TABLE IF NOT EXISTS {fts} USING fts5({source_id} UNINDEXED, {columns}, tokenize='{tokenizer}')",
4530            fts = quote_ident(&index.name),
4531            source_id = quote_ident(FTS_SOURCE_ID_COLUMN),
4532        );
4533            self.conn.execute(&sql, []).map_err(|error| {
4534                format!(
4535                    "cannot create local FTS5 projection {:?}: {error}",
4536                    index.name
4537                )
4538            })?;
4539            let mapping = quote_ident(&format!("_syncular_fts_{}", index.name));
4540            let mapped: bool = self
4541                .conn
4542                .query_row(
4543                    "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name=?1)",
4544                    [format!("_syncular_fts_{}", index.name)],
4545                    |row| row.get(0),
4546                )
4547                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4548            self.conn.execute_batch(&format!("CREATE TABLE IF NOT EXISTS {mapping}(id INTEGER PRIMARY KEY, source_id TEXT NOT NULL UNIQUE)")).map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4549            if !mapped && existed {
4550                self.conn
4551                    .execute_batch(&format!(
4552                        "INSERT INTO {mapping}(id, source_id) SELECT rowid, {} FROM {}",
4553                        quote_ident(FTS_SOURCE_ID_COLUMN),
4554                        quote_ident(&index.name)
4555                    ))
4556                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4557            }
4558            self.create_fts_triggers(table, index)?;
4559            // Every visible row has exactly one mapping row, so a count
4560            // mismatch marks a projection an earlier release damaged; the
4561            // open rebuilds it.
4562            let stale: bool = !existed
4563                || self
4564                    .conn
4565                    .query_row(
4566                        &format!(
4567                            "SELECT (SELECT count(*) FROM {mapping}) != (SELECT count(*) FROM {})",
4568                            visible_table(&table.name)
4569                        ),
4570                        [],
4571                        |row| row.get(0),
4572                    )
4573                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4574            if stale {
4575                self.rebuild_fts_projection(table, index)?;
4576            }
4577            Ok(())
4578        })();
4579        if let Err(error) = result {
4580            self.rollback_observation("syncular_fts_schema");
4581            return Err(error);
4582        }
4583        if let Err(error) = self.conn.execute_batch("RELEASE syncular_fts_schema") {
4584            self.rollback_observation("syncular_fts_schema");
4585            return Err(error.to_string());
4586        }
4587        Ok(())
4588    }
4589
4590    // -- persistence write-through --------------------------------------------
4591
4592    fn persist_outbox_insert(&self, commit: &OutboxCommit) -> Result<(), String> {
4593        let ops: Vec<Value> = commit
4594            .ops
4595            .iter()
4596            .map(|op| {
4597                let mut entry = serde_json::json!({
4598                    "op": if op.upsert { "upsert" } else { "delete" },
4599                    "table": op.table,
4600                    "rowId": op.row_id,
4601                    "baseVersion": op.base_version,
4602                    "values": op.values.clone().map(Value::Object),
4603                });
4604                if op.insert {
4605                    entry["insert"] = Value::Bool(true);
4606                }
4607                entry
4608            })
4609            .collect();
4610        self.conn
4611            .execute(
4612                "INSERT OR REPLACE INTO _syncular_outbox (commit_id, ops_json) VALUES (?1, ?2)",
4613                rusqlite::params![commit.client_commit_id, Value::Array(ops).to_string()],
4614            )
4615            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4616        for operation in &commit.ops {
4617            let Some(values) = &operation.values else {
4618                continue;
4619            };
4620            let Some(table) = self.schema.table(&operation.table) else {
4621                continue;
4622            };
4623            for column in table
4624                .columns
4625                .iter()
4626                .filter(|column| column.ty == ColumnType::BlobRef)
4627            {
4628                let Some(Value::String(raw)) = values.get(&column.name) else {
4629                    continue;
4630                };
4631                let Ok(blob_ref) = serde_json::from_str::<Value>(raw) else {
4632                    continue;
4633                };
4634                let Some(blob_id) = blob_ref.get("blobId").and_then(Value::as_str) else {
4635                    continue;
4636                };
4637                self.conn
4638                    .execute(
4639                        "INSERT OR IGNORE INTO _syncular_blob_commit_refs(commit_id, blob_id)
4640                         SELECT ?1, ?2 WHERE EXISTS (SELECT 1 FROM _syncular_blobs WHERE blob_id = ?2)",
4641                        rusqlite::params![commit.client_commit_id, blob_id],
4642                    )
4643                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4644            }
4645        }
4646        Ok(())
4647    }
4648
4649    fn delete_outbox_persisted(&self, client_commit_id: &str) -> Result<(), String> {
4650        self.conn
4651            .execute(
4652                "DELETE FROM _syncular_blob_commit_refs WHERE commit_id = ?1",
4653                rusqlite::params![client_commit_id],
4654            )
4655            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4656        self.conn
4657            .execute(
4658                "DELETE FROM _syncular_outbox WHERE commit_id = ?1",
4659                rusqlite::params![client_commit_id],
4660            )
4661            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4662        self.conn.execute("DELETE FROM _syncular_row_deliveries WHERE NOT EXISTS(SELECT 1 FROM _syncular_outbox,json_each(ops_json) WHERE json_extract(json_each.value,'$.table')=tbl AND json_extract(json_each.value,'$.rowId')=id)", [])
4663            .map(|_| ())
4664            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
4665    }
4666
4667    fn outcome_status_name(status: CommitOutcomeStatus) -> &'static str {
4668        match status {
4669            CommitOutcomeStatus::Applied => "applied",
4670            CommitOutcomeStatus::Cached => "cached",
4671            CommitOutcomeStatus::Conflict => "conflict",
4672            CommitOutcomeStatus::Rejected => "rejected",
4673        }
4674    }
4675
4676    fn outcome_resolution_name(resolution: CommitOutcomeResolution) -> &'static str {
4677        match resolution {
4678            CommitOutcomeResolution::Active => "active",
4679            CommitOutcomeResolution::ResolvedKeepServer => "resolved_keep_server",
4680            CommitOutcomeResolution::Superseded => "superseded",
4681            CommitOutcomeResolution::Dismissed => "dismissed",
4682        }
4683    }
4684
4685    fn parse_outcome_status(value: &str) -> Result<CommitOutcomeStatus, String> {
4686        match value {
4687            "applied" => Ok(CommitOutcomeStatus::Applied),
4688            "cached" => Ok(CommitOutcomeStatus::Cached),
4689            "conflict" => Ok(CommitOutcomeStatus::Conflict),
4690            "rejected" => Ok(CommitOutcomeStatus::Rejected),
4691            _ => Err(format!("invalid persisted commit outcome status {value:?}")),
4692        }
4693    }
4694
4695    fn parse_outcome_resolution(value: &str) -> Result<CommitOutcomeResolution, String> {
4696        match value {
4697            "active" => Ok(CommitOutcomeResolution::Active),
4698            "resolved_keep_server" => Ok(CommitOutcomeResolution::ResolvedKeepServer),
4699            "superseded" => Ok(CommitOutcomeResolution::Superseded),
4700            "dismissed" => Ok(CommitOutcomeResolution::Dismissed),
4701            _ => Err(format!(
4702                "invalid persisted commit outcome resolution {value:?}"
4703            )),
4704        }
4705    }
4706
4707    fn persist_commit_outcome(
4708        &self,
4709        client_commit_id: &str,
4710        status: CommitOutcomeStatus,
4711        results: &[CommitOperationOutcome],
4712        operations: Option<&[OutboxOp]>,
4713    ) -> Result<(), String> {
4714        let results_json = serde_json::to_string(results).map_err(|error| error.to_string())?;
4715        let operations_json = operations
4716            .map(|items| {
4717                serde_json::to_string(&items.iter().map(CommitOperation::from).collect::<Vec<_>>())
4718            })
4719            .transpose()
4720            .map_err(|error| error.to_string())?;
4721        self.conn
4722            .execute(
4723                "INSERT INTO _syncular_commit_outcomes (
4724                   client_commit_id, status, recorded_at_ms, results_json,
4725                   operations_json, resolution
4726                 ) VALUES (?1, ?2, ?3, ?4, ?5, 'active')",
4727                rusqlite::params![
4728                    client_commit_id,
4729                    Self::outcome_status_name(status),
4730                    self.clock_now_ms(),
4731                    results_json,
4732                    operations_json
4733                ],
4734            )
4735            .map(|_| ())
4736            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
4737    }
4738
4739    /// §7.2: the journal holds a stored cell only in its canonical shape. A cell
4740    /// the byte-envelope rule refuses cannot be journaled verbatim.
4741    fn stored_cell_is_representable(cell: &Value) -> bool {
4742        cell.is_null()
4743            || cell.is_string()
4744            || cell.is_boolean()
4745            || cell.is_number()
4746            || is_canonical_bytes_envelope(cell)
4747    }
4748
4749    fn stored_row_is_representable(value: &Value) -> bool {
4750        value
4751            .as_object()
4752            .is_some_and(|row| row.values().all(Self::stored_cell_is_representable))
4753    }
4754
4755    fn stored_operation_is_representable(value: &Value) -> bool {
4756        value.get("table").is_some_and(Value::is_string)
4757            && value.get("rowId").is_some_and(Value::is_string)
4758            && value.get("baseVersion").is_none_or(|v| {
4759                v.as_i64()
4760                    .is_some_and(|n| n.unsigned_abs() <= MAX_JS_SAFE_INTEGER)
4761            })
4762            && match value.get("op").and_then(Value::as_str) {
4763                Some("upsert") => {
4764                    value
4765                        .get("values")
4766                        .is_some_and(Self::stored_row_is_representable)
4767                        && value
4768                            .get("insert")
4769                            .is_none_or(|insert| *insert == Value::Bool(true))
4770                }
4771                Some("delete") => value.get("values").is_none() && value.get("insert").is_none(),
4772                _ => false,
4773            }
4774    }
4775
4776    /// §7.2/§7.4.4: a dropped commit keeps its operation envelope in the journal
4777    /// only when the journal reader accepts the serialized operation, which is the
4778    /// rule `outcome_from_row` applies to this same row later.
4779    fn outbox_op_is_journalable(operation: &OutboxOp) -> bool {
4780        serde_json::to_value(CommitOperation::from(operation))
4781            .is_ok_and(|value| Self::stored_operation_is_representable(&value))
4782    }
4783
4784    fn outcome_from_row(row: StoredCommitOutcomeRow) -> Result<CommitOutcome, String> {
4785        let StoredCommitOutcomeRow {
4786            sequence,
4787            client_commit_id,
4788            status,
4789            recorded_at_ms,
4790            results_json,
4791            operations_json,
4792            resolution,
4793            resolved_at_ms,
4794            replacement_client_commit_id,
4795        } = row;
4796        if sequence.unsigned_abs() > MAX_JS_SAFE_INTEGER
4797            || recorded_at_ms.unsigned_abs() > MAX_JS_SAFE_INTEGER
4798            || resolved_at_ms.is_some_and(|value| value.unsigned_abs() > MAX_JS_SAFE_INTEGER)
4799        {
4800            return Err("sync.local_corrupt: persisted commit outcome is invalid".to_owned());
4801        }
4802        let invalid = || "sync.local_corrupt: persisted commit outcome is invalid".to_owned();
4803        let stored: Vec<Value> = serde_json::from_str(&results_json).map_err(|_| invalid())?;
4804        for result in &stored {
4805            let nested = match result.get("status").and_then(Value::as_str) {
4806                Some("conflict") => result.get("conflict"),
4807                Some("error") => result.get("rejection"),
4808                _ => None,
4809            };
4810            if nested
4811                .and_then(|record| record.get("operation"))
4812                .is_some_and(|op| !Self::stored_operation_is_representable(op))
4813                || (result.get("status").and_then(Value::as_str) == Some("conflict")
4814                    && nested
4815                        .and_then(|record| record.get("serverRow"))
4816                        .is_some_and(|row| !Self::stored_row_is_representable(row)))
4817            {
4818                return Err(invalid());
4819            }
4820        }
4821        let results: Vec<CommitOperationOutcome> =
4822            serde_json::from_value(Value::Array(stored)).map_err(|_| invalid())?;
4823        let has_conflict = results
4824            .iter()
4825            .any(|result| matches!(result, CommitOperationOutcome::Conflict { .. }));
4826        let has_error = results
4827            .iter()
4828            .any(|result| matches!(result, CommitOperationOutcome::Error { .. }));
4829        let status = Self::parse_outcome_status(&status).map_err(|_| invalid())?;
4830        let redacted = results.is_empty() && operations_json.is_none();
4831        if match status {
4832            CommitOutcomeStatus::Applied | CommitOutcomeStatus::Cached => has_conflict || has_error,
4833            CommitOutcomeStatus::Conflict => !(has_conflict || redacted),
4834            CommitOutcomeStatus::Rejected => has_conflict || !(has_error || redacted),
4835        } {
4836            return Err(invalid());
4837        }
4838        let operations: Option<Vec<CommitOperation>> = operations_json
4839            .map(|raw| {
4840                let stored: Vec<Value> = serde_json::from_str(&raw).map_err(|_| invalid())?;
4841                if !stored.iter().all(Self::stored_operation_is_representable) {
4842                    return Err(invalid());
4843                }
4844                serde_json::from_value(Value::Array(stored)).map_err(|_| invalid())
4845            })
4846            .transpose()?;
4847        for result in &results {
4848            match result {
4849                CommitOperationOutcome::Conflict { conflict }
4850                    if conflict.server_version.unsigned_abs() > MAX_JS_SAFE_INTEGER =>
4851                {
4852                    return Err("sync.local_corrupt: persisted commit outcome is invalid".to_owned())
4853                }
4854                CommitOperationOutcome::Error { rejection } => {
4855                    if let Some(details) = &rejection.details {
4856                        let encoded = serde_json::to_string(details).map_err(|_| {
4857                            "sync.local_corrupt: persisted commit outcome is invalid".to_owned()
4858                        })?;
4859                        RejectionDetails::parse(&encoded).map_err(|_| {
4860                            "sync.local_corrupt: persisted commit outcome is invalid".to_owned()
4861                        })?;
4862                    }
4863                }
4864                _ => {}
4865            }
4866        }
4867        Ok(CommitOutcome {
4868            sequence,
4869            client_commit_id,
4870            status,
4871            recorded_at_ms,
4872            results,
4873            operations,
4874            retained_rows: None,
4875            resolution: Self::parse_outcome_resolution(&resolution)?,
4876            resolved_at_ms,
4877            replacement_client_commit_id,
4878        })
4879    }
4880
4881    /// Resolve declared unique keys through SQLite, including affinity and NULL semantics.
4882    fn unique_conflicts(
4883        &self,
4884        table: &TableSchema,
4885        values: &Map<String, Value>,
4886        base: bool,
4887    ) -> Result<Vec<RetainedUniqueConflict>, String> {
4888        let source = if base {
4889            base_table(&table.name)
4890        } else {
4891            visible_table(&table.name)
4892        };
4893        let mut conflicts = Vec::new();
4894        for index in table.indexes.iter().filter(|index| index.unique) {
4895            let mut params = vec![json_param_to_sql(
4896                values.get(&table.primary_key).ok_or("sync.local_corrupt")?,
4897            )?];
4898            for name in &index.columns {
4899                let column = table
4900                    .columns
4901                    .iter()
4902                    .find(|column| column.name == *name)
4903                    .ok_or("sync.local_corrupt")?;
4904                let cell = json_to_column_value(column, values.get(name))?;
4905                params.push(owned_sql_value(RowParam::Cell(&cell))?);
4906            }
4907            let sql = format!(
4908                "SELECT * FROM {source} WHERE {} != ? AND {}",
4909                quote_ident(&table.primary_key),
4910                index
4911                    .columns
4912                    .iter()
4913                    .map(|column| format!("{} = ?", quote_ident(column)))
4914                    .collect::<Vec<_>>()
4915                    .join(" AND ")
4916            );
4917            let mut statement = self
4918                .conn
4919                .prepare_cached(&sql)
4920                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4921            let records = statement
4922                .query_map(rusqlite::params_from_iter(params), |record| {
4923                    let values = table
4924                        .columns
4925                        .iter()
4926                        .enumerate()
4927                        .map(|(index, column)| {
4928                            let value = sql_ref_to_json(column, record.get_ref(index)?);
4929                            json_to_column_value(column, Some(&value)).map_err(|message| {
4930                                rusqlite::Error::ToSqlConversionFailure(message.into())
4931                            })
4932                        })
4933                        .collect::<Result<Row, _>>()?;
4934                    Ok((values, record.get::<_, i64>(table.columns.len())?))
4935                })
4936                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4937            for record in records {
4938                let (row, version) =
4939                    record.map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
4940                let row_id = render_row_id_json(Some(&column_value_to_json(&row[table.pk_index])))?;
4941                conflicts.push(RetainedUniqueConflict {
4942                    index: index.name.clone(),
4943                    columns: index.columns.clone(),
4944                    row_id,
4945                    server_row: table
4946                        .columns
4947                        .iter()
4948                        .zip(&row)
4949                        .map(|(column, value)| (column.name.clone(), column_value_to_json(value)))
4950                        .collect(),
4951                    server_version: version,
4952                });
4953            }
4954        }
4955        Ok(conflicts)
4956    }
4957
4958    fn attach_retained_rows(&self, mut outcome: CommitOutcome) -> Result<CommitOutcome, String> {
4959        if let Some((commit, initial)) = self
4960            .failed_commits
4961            .iter()
4962            .find(|(commit, _)| commit.client_commit_id == outcome.client_commit_id)
4963        {
4964            let mut rows = Vec::new();
4965            for operation in &commit.ops {
4966                // A table the current schema removed has no rows to retain.
4967                let Some(table) = self.schema.table(&operation.table) else {
4968                    continue;
4969                };
4970                let key =
4971                    serde_json::to_string(&(operation.table.clone(), operation.row_id.clone()))
4972                        .map_err(|_| "sync.local_corrupt".to_owned())?;
4973                let base = self.stored_row(table, &operation.row_id, true)?;
4974                rows.push(RetainedCommitRow {
4975                    table: operation.table.clone(),
4976                    row_id: operation.row_id.clone(),
4977                    unique_conflicts: if operation.upsert {
4978                        self.unique_conflicts(
4979                            table,
4980                            initial
4981                                .get(&key)
4982                                .and_then(Value::as_object)
4983                                .or(operation.values.as_ref())
4984                                .ok_or("sync.local_corrupt")?,
4985                            true,
4986                        )?
4987                    } else {
4988                        Vec::new()
4989                    },
4990                    local_row: if operation.upsert {
4991                        initial
4992                            .get(&key)
4993                            .and_then(Value::as_object)
4994                            .cloned()
4995                            .or_else(|| operation.values.clone())
4996                    } else {
4997                        None
4998                    },
4999                    server_row: base.as_ref().map(|(values, _)| {
5000                        table
5001                            .columns
5002                            .iter()
5003                            .zip(values)
5004                            .map(|(column, value)| {
5005                                (column.name.clone(), column_value_to_json(value))
5006                            })
5007                            .collect()
5008                    }),
5009                    server_version: base.map(|(_, version)| version),
5010                });
5011            }
5012            outcome.retained_rows = Some(rows);
5013        }
5014        Ok(outcome)
5015    }
5016
5017    pub fn commit_outcome(&self, client_commit_id: &str) -> Result<Option<CommitOutcome>, String> {
5018        let row = self
5019            .conn
5020            .query_row(
5021                "SELECT seq, client_commit_id, status, recorded_at_ms, results_json, operations_json,
5022                        resolution, resolved_at_ms, replacement_client_commit_id
5023                   FROM _syncular_commit_outcomes WHERE client_commit_id = ?1",
5024                rusqlite::params![client_commit_id],
5025                |row| {
5026                    Ok(StoredCommitOutcomeRow {
5027                        sequence: row.get(0)?,
5028                        client_commit_id: row.get(1)?,
5029                        status: row.get(2)?,
5030                        recorded_at_ms: row.get(3)?,
5031                        results_json: row.get(4)?,
5032                        operations_json: row.get(5)?,
5033                        resolution: row.get(6)?,
5034                        resolved_at_ms: row.get(7)?,
5035                        replacement_client_commit_id: row.get(8)?,
5036                    })
5037                },
5038            )
5039            .optional()
5040            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5041        row.map(|row| self.attach_retained_rows(Self::outcome_from_row(row)?))
5042            .transpose()
5043    }
5044
5045    /// Internal host step. The host must run its idle drive before finishing.
5046    #[doc(hidden)]
5047    pub fn prepare_current_base(
5048        &self,
5049        client_commit_id: &str,
5050        sequence: i64,
5051    ) -> Result<CurrentBasePreparation, CurrentBaseProof> {
5052        let outcome = self
5053            .commit_outcome(client_commit_id)
5054            .map_err(|_| CurrentBaseProof::failure(CurrentBaseProofReason::SnapshotFailed))?;
5055        let valid = outcome.as_ref().is_some_and(|outcome| {
5056            outcome.sequence == sequence
5057                && outcome.resolution == CommitOutcomeResolution::Active
5058                && matches!(
5059                    outcome.status,
5060                    CommitOutcomeStatus::Conflict | CommitOutcomeStatus::Rejected
5061                )
5062                && outcome
5063                    .operations
5064                    .as_ref()
5065                    .zip(outcome.retained_rows.as_ref())
5066                    .is_some_and(|(operations, rows)| {
5067                        !rows.is_empty() && operations.len() == rows.len()
5068                    })
5069        });
5070        if !valid {
5071            return Err(CurrentBaseProof::failure(
5072                CurrentBaseProofReason::OutcomeUnavailable,
5073            ));
5074        }
5075        if !self.transport_enabled {
5076            return Err(CurrentBaseProof::failure(CurrentBaseProofReason::Offline));
5077        }
5078        Ok(CurrentBasePreparation {
5079            client_commit_id: client_commit_id.to_owned(),
5080            sequence,
5081            units: load_units(&self.conn)
5082                .map_err(|_| CurrentBaseProof::failure(CurrentBaseProofReason::SnapshotFailed))?,
5083            transport_epoch: self.transport_epoch,
5084            registration_epoch: self.registration_epoch,
5085            security_epoch: self.security_epoch,
5086        })
5087    }
5088
5089    /// Internal host step after the completed post-rejection idle drive.
5090    #[doc(hidden)]
5091    pub fn finish_current_base(
5092        &mut self,
5093        prepared: CurrentBasePreparation,
5094        drive: SyncOutcome,
5095    ) -> CurrentBaseProof {
5096        if !self.transport_enabled || self.transport_epoch != prepared.transport_epoch {
5097            return CurrentBaseProof::failure(CurrentBaseProofReason::Offline);
5098        }
5099        if self.registration_epoch != prepared.registration_epoch {
5100            return CurrentBaseProof::failure(CurrentBaseProofReason::MissingCoverage);
5101        }
5102        if self.security_preflight || self.security_epoch != prepared.security_epoch {
5103            return CurrentBaseProof::failure(CurrentBaseProofReason::SyncFailed);
5104        }
5105        let report = match drive {
5106            SyncOutcome::Ok(report) => report,
5107            SyncOutcome::BudgetExhausted(_) => {
5108                return CurrentBaseProof::failure(CurrentBaseProofReason::BudgetExhausted)
5109            }
5110            _ => return CurrentBaseProof::failure(CurrentBaseProofReason::SyncFailed),
5111        };
5112        if report.schema_floor.is_some() {
5113            return CurrentBaseProof::failure(CurrentBaseProofReason::SchemaFloor);
5114        }
5115        if self
5116            .conn
5117            .execute_batch("SAVEPOINT syncular_base_proof")
5118            .is_err()
5119        {
5120            return CurrentBaseProof::failure(CurrentBaseProofReason::SnapshotFailed);
5121        }
5122        let proof = (|| {
5123            let snapshot = snapshot_read_connection(
5124                &self.conn,
5125                &SnapshotReadRequest {
5126                    units: prepared.units.iter().map(|unit| unit.id.clone()).collect(),
5127                    commit_ids: vec![prepared.client_commit_id.clone()],
5128                    ..Default::default()
5129                },
5130            )
5131            .map_err(|_| CurrentBaseProofReason::SnapshotFailed)?;
5132            let current = self
5133                .commit_outcome(&prepared.client_commit_id)
5134                .map_err(|_| CurrentBaseProofReason::SnapshotFailed)?;
5135            let unavailable = if report.revoked.is_empty() {
5136                CurrentBaseProofReason::OutcomeUnavailable
5137            } else {
5138                CurrentBaseProofReason::Revoked
5139            };
5140            let Some(outcome) = current else {
5141                return Err(unavailable);
5142            };
5143            if !matches!(
5144                snapshot.deliveries.first(),
5145                Some(CommitDelivery::Known { .. })
5146            ) || outcome.sequence != prepared.sequence
5147                || outcome.resolution != CommitOutcomeResolution::Active
5148                || !matches!(
5149                    outcome.status,
5150                    CommitOutcomeStatus::Conflict | CommitOutcomeStatus::Rejected
5151                )
5152            {
5153                return Err(unavailable);
5154            }
5155            let (Some(operations), Some(rows)) = (outcome.operations, outcome.retained_rows) else {
5156                return Err(unavailable);
5157            };
5158            if rows.is_empty() || operations.len() != rows.len() {
5159                return Err(unavailable);
5160            }
5161            let after =
5162                load_units(&self.conn).map_err(|_| CurrentBaseProofReason::SnapshotFailed)?;
5163            let complete: HashSet<&str> = snapshot
5164                .units
5165                .iter()
5166                .filter_map(|unit| match unit {
5167                    UnitCatchup::Known {
5168                        id,
5169                        bootstrap_complete: true,
5170                        ..
5171                    } => Some(id.as_str()),
5172                    _ => None,
5173                })
5174                .collect();
5175            // A unit id names one scope map (§4.1), so the same id before and
5176            // after the drive is the same registration.
5177            let candidates: Vec<&UnitRecord> = after
5178                .iter()
5179                .filter(|unit| {
5180                    complete.contains(unit.id.as_str())
5181                        && prepared.units.iter().any(|before| before.id == unit.id)
5182                })
5183                .collect();
5184            let covered = |table_name: &str, row: &Map<String, Value>| {
5185                let Some(table) = self.schema.table(table_name) else {
5186                    return false;
5187                };
5188                candidates.iter().any(|unit| {
5189                    covers(table, &unit.scopes)
5190                        && pair_scopes(&unit.scopes, table_name, unit.echo.as_deref()).is_some_and(
5191                            |scopes| {
5192                                scopes.iter().all(|(variable, allowed)| {
5193                                    table
5194                                        .scope_column(variable)
5195                                        .and_then(|column| match row.get(column) {
5196                                            Some(Value::String(value)) => Some(value.clone()),
5197                                            Some(Value::Bool(value)) => Some(value.to_string()),
5198                                            Some(Value::Number(value)) => {
5199                                                value.as_f64().map(|number| {
5200                                                    ryu_js::Buffer::new()
5201                                                        .format_finite(number)
5202                                                        .to_owned()
5203                                                })
5204                                            }
5205                                            _ => None,
5206                                        })
5207                                        .is_some_and(|value| {
5208                                            !value.is_empty() && allowed.contains(&value)
5209                                        })
5210                                })
5211                            },
5212                        )
5213                })
5214            };
5215            for row in &rows {
5216                let Some(base) = row.server_row.as_ref().or(row.local_row.as_ref()) else {
5217                    return Err(if report.revoked.is_empty() {
5218                        CurrentBaseProofReason::MissingCoverage
5219                    } else {
5220                        CurrentBaseProofReason::Revoked
5221                    });
5222                };
5223                if !covered(&row.table, base)
5224                    || row
5225                        .unique_conflicts
5226                        .iter()
5227                        .any(|conflict| !covered(&row.table, &conflict.server_row))
5228                {
5229                    return Err(if report.revoked.is_empty() {
5230                        CurrentBaseProofReason::MissingCoverage
5231                    } else {
5232                        CurrentBaseProofReason::Revoked
5233                    });
5234                }
5235            }
5236            Ok(rows)
5237        })();
5238        if self
5239            .conn
5240            .execute_batch("RELEASE syncular_base_proof")
5241            .is_err()
5242        {
5243            return CurrentBaseProof::failure(CurrentBaseProofReason::SnapshotFailed);
5244        }
5245        match proof {
5246            Ok(rows) => CurrentBaseProof {
5247                proven: true,
5248                rows: Some(rows),
5249                reason: None,
5250            },
5251            Err(reason) => CurrentBaseProof::failure(reason),
5252        }
5253    }
5254
5255    pub fn prove_current_base(
5256        &mut self,
5257        transport: &mut dyn Transport,
5258        client_commit_id: &str,
5259        sequence: i64,
5260        max_rounds: Option<u32>,
5261    ) -> CurrentBaseProof {
5262        let prepared = match self.prepare_current_base(client_commit_id, sequence) {
5263            Ok(prepared) => prepared,
5264            Err(proof) => return proof,
5265        };
5266        let drive = self.sync_until_idle(transport, max_rounds);
5267        self.finish_current_base(prepared, drive)
5268    }
5269
5270    pub fn commit_outcomes(&self, query: CommitOutcomeQuery) -> Result<Vec<CommitOutcome>, String> {
5271        if query.limit == Some(0) {
5272            return Err("sync.invalid_request: commit outcome limit must be positive".to_owned());
5273        }
5274        let mut sql = String::from(
5275            "SELECT seq, client_commit_id, status, recorded_at_ms, results_json, operations_json,
5276                    resolution, resolved_at_ms, replacement_client_commit_id
5277               FROM _syncular_commit_outcomes",
5278        );
5279        if query.active_only {
5280            sql.push_str(" WHERE resolution = 'active' AND status IN ('conflict', 'rejected')");
5281        }
5282        sql.push_str(" ORDER BY seq DESC");
5283        if let Some(limit) = query.limit {
5284            sql.push_str(&format!(" LIMIT {limit}"));
5285        }
5286        let mut stmt = self
5287            .conn
5288            .prepare(&sql)
5289            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5290        let rows = stmt
5291            .query_map([], |row| {
5292                Ok(StoredCommitOutcomeRow {
5293                    sequence: row.get(0)?,
5294                    client_commit_id: row.get(1)?,
5295                    status: row.get(2)?,
5296                    recorded_at_ms: row.get(3)?,
5297                    results_json: row.get(4)?,
5298                    operations_json: row.get(5)?,
5299                    resolution: row.get(6)?,
5300                    resolved_at_ms: row.get(7)?,
5301                    replacement_client_commit_id: row.get(8)?,
5302                })
5303            })
5304            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5305        let mut outcomes = Vec::new();
5306        for row in rows {
5307            outcomes.push(self.attach_retained_rows(Self::outcome_from_row(
5308                row.map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?,
5309            )?)?);
5310        }
5311        Ok(outcomes)
5312    }
5313
5314    fn prune_commit_outcomes(&self) -> Result<(), String> {
5315        #[cfg(test)]
5316        self.outcome_prune_count
5317            .set(self.outcome_prune_count.get() + 1);
5318        let max_entries = self.limits.outcome_retention_max_entries.unwrap_or(1_000);
5319        let count = self
5320            .conn
5321            .query_row(
5322                "SELECT COUNT(*) FROM _syncular_commit_outcomes",
5323                [],
5324                |row| row.get::<_, i64>(0),
5325            )
5326            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
5327            as usize;
5328        let excess = count.saturating_sub(max_entries);
5329        if excess == 0 {
5330            return Ok(());
5331        }
5332        let mut stmt = self
5333            .conn
5334            .prepare(
5335                "SELECT seq FROM _syncular_commit_outcomes
5336                  WHERE status IN ('applied', 'cached') OR resolution != 'active'
5337                  ORDER BY seq ASC LIMIT ?1",
5338            )
5339            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5340        let rows = stmt
5341            .query_map(rusqlite::params![excess as i64], |row| row.get::<_, i64>(0))
5342            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5343        let sequences = rows
5344            .collect::<Result<Vec<_>, _>>()
5345            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5346        drop(stmt);
5347        for sequence in sequences {
5348            self.conn
5349                .execute(
5350                    "DELETE FROM _syncular_commit_outcomes WHERE seq = ?1",
5351                    rusqlite::params![sequence],
5352                )
5353                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5354        }
5355        Ok(())
5356    }
5357
5358    // -- units (§4.1) ------------------------------------------------------------
5359
5360    /// Every registered unit in registration order (§4.1).
5361    pub fn units(&self) -> Result<Vec<UnitRecord>, String> {
5362        self.stored(load_units(&self.conn))
5363    }
5364
5365    pub fn unit(&self, id: &str) -> Result<Option<UnitRecord>, String> {
5366        self.stored(get_unit(&self.conn, id))
5367    }
5368
5369    /// Register one unit (§4.1) and return its id.
5370    pub fn subscribe(&mut self, scopes: UnitScopes) -> Result<String, String> {
5371        Ok(self.subscribe_many(vec![scopes])?.remove(0))
5372    }
5373
5374    /// Register several units in one local transaction and return their ids:
5375    /// one commit for all of them (on OPFS a commit costs milliseconds, so a
5376    /// cold start with hundreds of units pays it once). An error rolls back
5377    /// every registration of the call. Registering an active unit again
5378    /// changes nothing; a revoked unit re-arms (§3.3). A call that registers or
5379    /// re-arms a unit raises sync-needed with an interactive intent.
5380    pub fn subscribe_many(&mut self, units: Vec<UnitScopes>) -> Result<Vec<String>, String> {
5381        self.begin_observation("syncular_subscribe")?;
5382        let mut changed = false;
5383        let registered = (|| {
5384            let mut ids = Vec::with_capacity(units.len());
5385            for scopes in units {
5386                let (id, held) = self.hold_unit(scopes, true)?;
5387                changed |= held;
5388                ids.push(id);
5389            }
5390            self.conn
5391                .execute_batch("RELEASE syncular_subscribe")
5392                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5393            Ok(ids)
5394        })();
5395        let ids = match registered {
5396            Ok(ids) => ids,
5397            Err(error) => {
5398                self.rollback_observation("syncular_subscribe");
5399                return Err(error);
5400            }
5401        };
5402        if changed {
5403            self.registration_epoch = self.registration_epoch.wrapping_add(1);
5404            self.set_sync_needed(true, true);
5405        }
5406        Ok(ids)
5407    }
5408
5409    /// Release the explicit reference to a unit. The unit leaves the
5410    /// registration list when no window base holds it (§4.1); its rows stay.
5411    pub fn unsubscribe(&mut self, id: &str) -> Result<(), String> {
5412        let Some(unit) = self.stored(get_unit(&self.conn, id))? else {
5413            return Ok(());
5414        };
5415        if !unit.explicit {
5416            return Ok(());
5417        }
5418        if !self.stored(window_refs(&self.conn, id))?.is_empty() {
5419            return self.stored(save_unit(
5420                &self.conn,
5421                &UnitRecord {
5422                    explicit: false,
5423                    ..unit
5424                },
5425            ));
5426        }
5427        self.conn
5428            .execute("DELETE FROM _syncular_units WHERE id = ?1", [id])
5429            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5430        self.registration_epoch = self.registration_epoch.wrapping_add(1);
5431        if unit.status == UnitStatus::Active {
5432            self.set_sync_needed(true, true);
5433        }
5434        Ok(())
5435    }
5436
5437    /// Take one reference on a unit (§4.1): validate the scope map, register a
5438    /// new unit catching up with cursor `-1`, re-arm a revoked one (§3.3).
5439    /// Returns the id and whether the registration list changed.
5440    fn hold_unit(&mut self, scopes: UnitScopes, explicit: bool) -> Result<(String, bool), String> {
5441        if scopes.is_empty()
5442            || scopes.values().any(|value| value == "*")
5443            || !self
5444                .schema
5445                .tables
5446                .iter()
5447                .any(|table| covers(table, &scopes))
5448        {
5449            return Err("sync.invalid_subscription: a unit names one non-wildcard value per variable and covers a table".to_owned());
5450        }
5451        let id = ssp2::registration::unit_id(&scope_pairs(&scopes));
5452        let existing = self.stored(get_unit(&self.conn, &id))?;
5453        if let Some(existing) = existing
5454            .as_ref()
5455            .filter(|unit| unit.status != UnitStatus::Revoked)
5456        {
5457            if explicit && !existing.explicit {
5458                self.stored(save_unit(
5459                    &self.conn,
5460                    &UnitRecord {
5461                        explicit,
5462                        ..existing.clone()
5463                    },
5464                ))?;
5465            }
5466            return Ok((id, false));
5467        }
5468        self.delete_pending_evict(&id)?;
5469        self.stored(save_unit(
5470            &self.conn,
5471            &UnitRecord {
5472                id: id.clone(),
5473                scopes,
5474                explicit: explicit || existing.is_some_and(|unit| unit.explicit),
5475                status: UnitStatus::Active,
5476                reason_code: None,
5477                cursor: Some(-1),
5478                bootstrap_state: None,
5479                seeded: Vec::new(),
5480                echo: None,
5481            },
5482        ))?;
5483        Ok((id, true))
5484    }
5485
5486    // -- windows (§4.8) -----------------------------------------------------------
5487
5488    /// §4.8: set the windowed-in values of a base, one unit per value. Added
5489    /// values take a reference on their unit (a new unit bootstraps on the
5490    /// next sync); removed values release theirs, and a unit no reference
5491    /// holds leaves the registration list fused with its first eviction chunk
5492    /// (E1–E4). Idempotent; re-entry cancels a deferred eviction.
5493    pub fn set_window(
5494        &mut self,
5495        base: &WindowBase,
5496        units: &[String],
5497    ) -> Result<CommandEffects, String> {
5498        let base_key = window_base_key(base);
5499        let live = self.load_window_units(&base_key)?;
5500        let mut changed = false;
5501        for value in units {
5502            if live.iter().any(|(held, _)| held == value) {
5503                continue;
5504            }
5505            let rollback = self.capture();
5506            self.begin_observation("syncular_window")?;
5507            let widened = (|| {
5508                let (id, registered) = self.hold_unit(window_unit_scopes(base, value), false)?;
5509                self.sync_needed |= registered;
5510                self.conn
5511                    .execute(
5512                        "INSERT OR REPLACE INTO _syncular_windows(base, unit, sub_id) VALUES (?1, ?2, ?3)",
5513                        rusqlite::params![base_key, value, id],
5514                    )
5515                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5516                let mut batch = ChangeAccumulator::default();
5517                batch.window(&base_key, value);
5518                batch.status = true;
5519                self.finish_observation("syncular_window", batch)
5520            })();
5521            if let Err(error) = widened {
5522                self.rollback_observation("syncular_window");
5523                self.restore(rollback);
5524                return Err(error);
5525            }
5526            changed = true;
5527            self.registration_epoch = self.registration_epoch.wrapping_add(1);
5528        }
5529        for (value, id) in live {
5530            if units.contains(&value) {
5531                continue;
5532            }
5533            self.rebuild_overlay_if_dirty()?;
5534            let rollback = self.capture();
5535            self.begin_observation("syncular_window")?;
5536            let remaining = self.release_window_unit(&base_key, &value, &id);
5537            let remaining = match remaining {
5538                Ok(remaining) => remaining,
5539                Err(error) => {
5540                    self.rollback_observation("syncular_window");
5541                    self.restore(rollback);
5542                    return Err(error);
5543                }
5544            };
5545            changed = true;
5546            self.registration_epoch = self.registration_epoch.wrapping_add(1);
5547            if remaining {
5548                self.drain_pending_evictions()?;
5549            }
5550        }
5551        Ok(if changed && self.transport_enabled {
5552            CommandEffects::interactive()
5553        } else {
5554            CommandEffects::none()
5555        })
5556    }
5557
5558    /// §4.8 completeness oracle: the windowed-in values of a base and those
5559    /// whose unit is not complete. A value is complete once its unit is active
5560    /// and no longer bootstrapping; registration alone is not completeness.
5561    pub fn window_state(&self, base: &WindowBase) -> Result<WindowState, String> {
5562        persisted_window_state(&self.conn, base).map_err(|failure| failure.message)
5563    }
5564
5565    /// Registry rows for a base, ordered by value: (value, unit id).
5566    fn load_window_units(&self, base_key: &str) -> Result<Vec<(String, String)>, String> {
5567        let mut statement = self
5568            .conn
5569            .prepare_cached(
5570                "SELECT unit, sub_id FROM _syncular_windows WHERE base = ?1 ORDER BY unit ASC",
5571            )
5572            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5573        let rows = statement
5574            .query_map([base_key], |row| Ok((row.get(0)?, row.get(1)?)))
5575            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5576        rows.collect::<Result<_, _>>()
5577            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
5578    }
5579
5580    /// Every window registry row: (base key, value).
5581    fn load_window_registry(&self) -> Result<Vec<(String, String)>, String> {
5582        let mut statement = self
5583            .conn
5584            .prepare_cached("SELECT base, unit FROM _syncular_windows ORDER BY base, unit")
5585            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5586        let rows = statement
5587            .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))
5588            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5589        rows.collect::<Result<_, _>>()
5590            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
5591    }
5592
5593    /// §4.8: release a window value's reference inside the caller's
5594    /// observation. When no reference holds the unit, unregister it fused with
5595    /// its first eviction chunk (E3): delete the rows of every covered table
5596    /// matching the persisted echo, except rows pinned by the outbox (E1) or
5597    /// owned by another unit (§3.3), and park the per-table scopes of what
5598    /// remains in the pending-eviction registry. Fails closed without a local
5599    /// scope-column mapping. Returns whether unpinned rows remain.
5600    fn release_window_unit(
5601        &mut self,
5602        base_key: &str,
5603        value: &str,
5604        id: &str,
5605    ) -> Result<bool, String> {
5606        self.conn
5607            .execute(
5608                "DELETE FROM _syncular_windows WHERE base = ?1 AND unit = ?2",
5609                rusqlite::params![base_key, value],
5610            )
5611            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5612        let mut batch = ChangeAccumulator::default();
5613        batch.window(base_key, value);
5614        batch.status = true;
5615        let unit = self.stored(get_unit(&self.conn, id))?;
5616        let mut remaining = false;
5617        if let Some(unit) = unit.filter(|unit| !unit.explicit) {
5618            if self.stored(window_refs(&self.conn, id))?.is_empty() {
5619                let units = self.stored(load_units(&self.conn))?;
5620                self.conn
5621                    .execute("DELETE FROM _syncular_units WHERE id = ?1", [id])
5622                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5623                self.sync_needed = true;
5624                for table in self.schema.tables.clone() {
5625                    let Some(effective) = unit
5626                        .echo
5627                        .as_deref()
5628                        .filter(|_| covers(&table, &unit.scopes))
5629                        .and_then(|echo| pair_scopes(&unit.scopes, &table.name, Some(echo)))
5630                    else {
5631                        continue;
5632                    };
5633                    let pinned = self.pinned_row_ids(&table.name);
5634                    let owners = unit_owners(&units, &table, id);
5635                    let (more, deferred) =
5636                        self.evict_scope_rows(&table.name, &effective, &owners, &pinned)?;
5637                    remaining |= more;
5638                    if deferred {
5639                        self.save_pending_evict(id, &table.name, &effective)?;
5640                    }
5641                    self.record_scope_map(&mut batch, &table.name, &effective);
5642                }
5643            }
5644        }
5645        self.finish_observation("syncular_window", batch)?;
5646        Ok(remaining)
5647    }
5648
5649    /// §4.8 E1: delete base rows owned by the effective scopes
5650    /// ([`local_scope_predicate`]) EXCEPT pinned primary keys. Returns (more
5651    /// unpinned work, any scoped rows remain). A missing scope-column mapping
5652    /// fails without deleting rows.
5653    fn evict_scope_rows(
5654        &self,
5655        table_name: &str,
5656        effective: &[(String, Vec<String>)],
5657        owners: &[Vec<(String, Vec<String>)>],
5658        pinned: &std::collections::HashSet<String>,
5659    ) -> Result<(bool, bool), String> {
5660        let table = self.schema.table(table_name).ok_or("sync.unknown_table")?;
5661        let Some((scope, mut params)) = local_scope_predicate(table, effective, owners)
5662            .map_err(|()| "sync.scope_revoked".to_owned())?
5663        else {
5664            return Ok((false, false));
5665        };
5666        let scope_params = params.len();
5667        let pk = quote_ident(&table.primary_key);
5668        // Pinned ids are rowId text; the text form matches every key type.
5669        let predicate = if pinned.is_empty() {
5670            scope.clone()
5671        } else {
5672            params.extend(pinned.iter().cloned().map(SqlValue::Text));
5673            let holes = vec!["?"; pinned.len()].join(", ");
5674            format!("{scope} AND CAST({pk} AS TEXT) NOT IN ({holes})")
5675        };
5676        let base = base_table(table_name);
5677        let visible = visible_table(table_name);
5678        let mut ids: Vec<SqlValue> = Vec::new();
5679        for source in [&base, &visible] {
5680            let mut stmt = self
5681                .conn
5682                .prepare_cached(&format!(
5683                    "SELECT {pk} FROM {source} WHERE {predicate} LIMIT 1024"
5684                ))
5685                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5686            ids = stmt
5687                .query_map(rusqlite::params_from_iter(&params), |row| row.get(0))
5688                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
5689                .collect::<Result<_, _>>()
5690                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5691            if !ids.is_empty() {
5692                break;
5693            }
5694        }
5695        if !ids.is_empty() {
5696            for id in &ids {
5697                self.conn
5698                    .execute(
5699                        "DELETE FROM _syncular_acked_rows WHERE tbl=?1 AND id=?2",
5700                        rusqlite::params![table_name, id],
5701                    )
5702                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5703            }
5704            let holes = vec!["?"; ids.len()].join(", ");
5705            for source in [&base, &visible] {
5706                self.conn
5707                    .execute(
5708                        &format!("DELETE FROM {source} WHERE {pk} IN ({holes})"),
5709                        rusqlite::params_from_iter(&ids),
5710                    )
5711                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5712            }
5713        }
5714        let mut remaining = false;
5715        let mut deferred = false;
5716        for source in [&base, &visible] {
5717            remaining |= self
5718                .conn
5719                .query_row(
5720                    &format!("SELECT EXISTS(SELECT 1 FROM {source} WHERE {predicate})"),
5721                    rusqlite::params_from_iter(&params),
5722                    |row| row.get::<_, bool>(0),
5723                )
5724                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5725            deferred |= self
5726                .conn
5727                .query_row(
5728                    &format!("SELECT EXISTS(SELECT 1 FROM {source} WHERE {scope})"),
5729                    rusqlite::params_from_iter(&params[..scope_params]),
5730                    |row| row.get::<_, bool>(0),
5731                )
5732                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5733        }
5734        Ok((remaining, deferred))
5735    }
5736
5737    /// §4.8 E1: continue deferred evictions after the outbox drains or between
5738    /// chunks, retaining outbox pins and their durable marker. A unit that
5739    /// re-entered the window has no pending entry left.
5740    fn drain_pending_evictions(&mut self) -> Result<(), String> {
5741        let pending = self.load_pending_evictions()?;
5742        if pending.is_empty() {
5743            return Ok(());
5744        }
5745        self.rebuild_overlay_if_dirty()?;
5746        for (unit_id, table_name, effective) in pending {
5747            let Some(table) = self.schema.table(&table_name).cloned() else {
5748                self.delete_pending_evict_table(&unit_id, &table_name)?;
5749                continue;
5750            };
5751            loop {
5752                std::thread::yield_now();
5753                self.begin_observation("syncular_evict")?;
5754                let chunk = (|| {
5755                    let pinned = self.pinned_row_ids(&table_name);
5756                    let owners =
5757                        unit_owners(&self.stored(load_units(&self.conn))?, &table, &unit_id);
5758                    let (remaining, deferred) =
5759                        self.evict_scope_rows(&table_name, &effective, &owners, &pinned)?;
5760                    if !deferred {
5761                        self.delete_pending_evict_table(&unit_id, &table_name)?;
5762                    }
5763                    let mut batch = ChangeAccumulator::default();
5764                    self.record_scope_map(&mut batch, &table_name, &effective);
5765                    self.finish_observation("syncular_evict", batch)?;
5766                    Ok(remaining)
5767                })();
5768                match chunk {
5769                    Ok(true) => {}
5770                    Ok(false) => break,
5771                    Err(error) => {
5772                        self.rollback_observation("syncular_evict");
5773                        return Err(error);
5774                    }
5775                }
5776            }
5777        }
5778        Ok(())
5779    }
5780
5781    /// §4.8 E1: primary keys of `table` referenced by a pending outbox
5782    /// commit — rows that MUST NOT be evicted until the commit drains.
5783    fn pinned_row_ids(&self, table: &str) -> std::collections::HashSet<String> {
5784        let mut pinned = std::collections::HashSet::new();
5785        for commit in self
5786            .outbox
5787            .iter()
5788            .map(|commit| &**commit)
5789            .chain(self.failed_commits.iter().map(|(commit, _)| commit))
5790        {
5791            for op in &commit.ops {
5792                if op.table == table {
5793                    pinned.insert(op.row_id.clone());
5794                }
5795            }
5796        }
5797        pinned
5798    }
5799
5800    fn save_pending_evict(
5801        &self,
5802        unit_id: &str,
5803        table: &str,
5804        effective: &[(String, Vec<String>)],
5805    ) -> Result<(), String> {
5806        self.conn.execute(
5807            "INSERT OR REPLACE INTO _syncular_pending_evictions(unit_id, tbl, effective_scopes) VALUES (?1, ?2, ?3)",
5808            rusqlite::params![unit_id, table, scope_map_to_json(effective).to_string()],
5809        ).map(|_| ()).map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
5810    }
5811
5812    /// Re-entry cancels every pending eviction of the unit.
5813    fn delete_pending_evict(&self, unit_id: &str) -> Result<(), String> {
5814        self.conn
5815            .execute(
5816                "DELETE FROM _syncular_pending_evictions WHERE unit_id = ?1",
5817                [unit_id],
5818            )
5819            .map(|_| ())
5820            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
5821    }
5822
5823    fn delete_pending_evict_table(&self, unit_id: &str, table: &str) -> Result<(), String> {
5824        self.conn
5825            .execute(
5826                "DELETE FROM _syncular_pending_evictions WHERE unit_id = ?1 AND tbl = ?2",
5827                [unit_id, table],
5828            )
5829            .map(|_| ())
5830            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
5831    }
5832
5833    fn load_pending_evictions(&self) -> Result<Vec<PendingEvict>, String> {
5834        let mut stmt = self
5835            .conn
5836            .prepare("SELECT unit_id, tbl, effective_scopes FROM _syncular_pending_evictions")
5837            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5838        let rows = stmt
5839            .query_map([], |row| {
5840                Ok((
5841                    row.get::<_, String>(0)?,
5842                    row.get::<_, String>(1)?,
5843                    row.get::<_, String>(2)?,
5844                ))
5845            })
5846            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5847        rows.map(|row| {
5848            let (unit_id, table, json) =
5849                row.map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5850            let value: Value = serde_json::from_str(&json)
5851                .map_err(|_| "sync.local_corrupt: invalid pending eviction scopes".to_owned())?;
5852            let effective = json_to_scope_map(&value)
5853                .map_err(|_| "sync.local_corrupt: invalid pending eviction scopes".to_owned())?;
5854            Ok((unit_id, table, effective))
5855        })
5856        .collect()
5857    }
5858
5859    /// Select failed-intent retention; reload durable retained aggregates.
5860    pub fn set_retain_failed_commits(&mut self, enabled: bool) -> Result<(), String> {
5861        self.retain_failed_commits = enabled;
5862        for (commit, _) in self.failed_commits.iter() {
5863            for operation in &commit.ops {
5864                self.overlay_dirty.table(&operation.table);
5865            }
5866        }
5867        self.failed_commits = self.load_failed_commits()?.into();
5868        for (commit, _) in self.failed_commits.iter() {
5869            for operation in &commit.ops {
5870                self.overlay_dirty.table(&operation.table);
5871            }
5872        }
5873        Ok(())
5874    }
5875
5876    fn load_failed_commits(&self) -> Result<Vec<FailedCommit>, String> {
5877        let mut statement = self.conn.prepare("SELECT client_commit_id, operations_json, initial_json FROM _syncular_failed_commits ORDER BY seq")
5878            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5879        let rows = statement
5880            .query_map([], |row| {
5881                Ok((
5882                    row.get::<_, String>(0)?,
5883                    row.get::<_, String>(1)?,
5884                    row.get::<_, String>(2)?,
5885                ))
5886            })
5887            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5888        rows.map(|row| {
5889            let (id, operations, initial) =
5890                row.map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5891            let operations: Vec<CommitOperation> =
5892                serde_json::from_str(&operations).map_err(|_| "sync.local_corrupt".to_owned())?;
5893            let initial =
5894                serde_json::from_str(&initial).map_err(|_| "sync.local_corrupt".to_owned())?;
5895            Ok((
5896                OutboxCommit {
5897                    client_commit_id: id,
5898                    ops: operations
5899                        .into_iter()
5900                        .map(|op| OutboxOp {
5901                            upsert: op.op == "upsert",
5902                            table: op.table,
5903                            row_id: op.row_id,
5904                            base_version: op.base_version,
5905                            values: op.values,
5906                            insert: op.insert,
5907                        })
5908                        .collect(),
5909                },
5910                initial,
5911            ))
5912        })
5913        .collect()
5914    }
5915
5916    fn retain_failed_commit(&mut self, id: &str, operations: &[OutboxOp]) -> Result<(), String> {
5917        let mut initial = Map::new();
5918        for operation in operations {
5919            // A table the current schema removed has no rows to retain.
5920            let Some(table) = self.schema.table(&operation.table) else {
5921                continue;
5922            };
5923            let initial_row = match self.stored_row(table, &operation.row_id, true)? {
5924                Some(row) => Some(row),
5925                None => self.visible_row(table, &operation.row_id)?,
5926            };
5927            if let Some((values, _)) = initial_row {
5928                let mut intended: Map<String, Value> = table
5929                    .columns
5930                    .iter()
5931                    .zip(values.iter())
5932                    .map(|(column, value)| (column.name.clone(), column_value_to_json(value)))
5933                    .collect();
5934                if let Some(patch) = &operation.values {
5935                    intended.extend(patch.clone());
5936                }
5937                initial.insert(
5938                    serde_json::to_string(&(operation.table.clone(), operation.row_id.clone()))
5939                        .map_err(|_| "sync.local_corrupt".to_owned())?,
5940                    Value::Object(intended),
5941                );
5942            }
5943        }
5944        self.conn.execute("INSERT INTO _syncular_failed_commits(client_commit_id, operations_json, initial_json) VALUES (?1, ?2, ?3)", rusqlite::params![id,
5945            serde_json::to_string(&operations.iter().map(CommitOperation::from).collect::<Vec<_>>()).map_err(|_| "sync.local_corrupt".to_owned())?,
5946            serde_json::to_string(&initial).map_err(|_| "sync.local_corrupt".to_owned())?])
5947            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5948        self.failed_commits.push((
5949            OutboxCommit {
5950                client_commit_id: id.to_owned(),
5951                ops: operations.to_vec(),
5952            },
5953            initial,
5954        ));
5955        Ok(())
5956    }
5957
5958    fn drop_failed_commits(
5959        &mut self,
5960        matches: impl Fn(&str, &Map<String, Value>) -> bool,
5961    ) -> Result<Vec<OutboxCommit>, String> {
5962        let mut doomed = self.drop_acknowledged_rows(&matches)?;
5963        for (commit, initial) in self.failed_commits.iter() {
5964            for operation in &commit.ops {
5965                let key =
5966                    serde_json::to_string(&(operation.table.clone(), operation.row_id.clone()))
5967                        .map_err(|_| "sync.local_corrupt".to_owned())?;
5968                if initial
5969                    .get(&key)
5970                    .and_then(Value::as_object)
5971                    .is_some_and(|values| matches(&operation.table, values))
5972                {
5973                    doomed.push(commit.clone());
5974                    break;
5975                }
5976            }
5977        }
5978        for commit in &doomed {
5979            self.conn
5980                .execute(
5981                    "DELETE FROM _syncular_failed_commits WHERE client_commit_id = ?1",
5982                    rusqlite::params![commit.client_commit_id],
5983                )
5984                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5985        }
5986        for commit in &doomed {
5987            self.conn.execute("UPDATE _syncular_commit_outcomes SET operations_json = NULL, results_json = '[]' WHERE client_commit_id = ?1", rusqlite::params![commit.client_commit_id]).map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
5988        }
5989        self.conflicts.retain(|record| {
5990            !doomed
5991                .iter()
5992                .any(|commit| commit.client_commit_id == record.client_commit_id)
5993        });
5994        self.rejections.retain(|record| {
5995            !doomed
5996                .iter()
5997                .any(|commit| commit.client_commit_id == record.client_commit_id)
5998        });
5999        self.failed_commits.retain(|(commit, _)| {
6000            !doomed
6001                .iter()
6002                .any(|dropped| dropped.client_commit_id == commit.client_commit_id)
6003        });
6004        for commit in &doomed {
6005            for operation in &commit.ops {
6006                self.overlay_dirty.table(&operation.table);
6007            }
6008        }
6009        Ok(doomed)
6010    }
6011
6012    /// Record one commit of local mutations. The returned commit ID is the
6013    /// §2.3 idempotency key. Failures are structured so callers read
6014    /// `code`/`message`/`details` instead of parsing a string prefix.
6015    pub fn mutate(&mut self, mutations: Vec<Mutation>) -> Result<String, ClientError> {
6016        self.mutate_with_commit_id(mutations, None)
6017    }
6018
6019    /// [`Self::mutate`] under a caller-supplied `clientCommitId` (§7.1), so an
6020    /// app that loses the reply knows its commit. The ID is non-empty (§6.1);
6021    /// one this replica still holds fails with `client.commit_id_reused`.
6022    pub fn mutate_with_commit_id(
6023        &mut self,
6024        mutations: Vec<Mutation>,
6025        client_commit_id: Option<String>,
6026    ) -> Result<String, ClientError> {
6027        let prior = self.storage_failure.borrow_mut().take();
6028        if let Some(id) = &client_commit_id {
6029            let held = if id.is_empty() {
6030                Err("sync.invalid_request: clientCommitId is empty".to_owned())
6031            } else {
6032                self.conn
6033                    .query_row(
6034                        "SELECT EXISTS(SELECT 1 FROM _syncular_outbox WHERE commit_id=?1 UNION ALL SELECT 1 FROM _syncular_commit_outcomes WHERE client_commit_id=?1)",
6035                        [id],
6036                        |row| row.get(0),
6037                    )
6038                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
6039            };
6040            match held {
6041                Ok(false) => {}
6042                Ok(true) => {
6043                    return self.finish_authoring(
6044                        prior,
6045                        Err(ClientError {
6046                            code: "client.commit_id_reused".to_owned(),
6047                            message: "clientCommitId is held".to_owned(),
6048                            details: Some(json!({ "clientCommitId": id })),
6049                            retryable: false,
6050                        }),
6051                    )
6052                }
6053                Err(message) => return self.finish_authoring(prior, Err(message.into())),
6054            }
6055        }
6056        let outcome = (|| -> Result<String, ClientError> {
6057            if mutations.is_empty() {
6058                return Err(
6059                    "sync.invalid_request: a commit must contain at least one operation (§6.1)"
6060                        .to_owned()
6061                        .into(),
6062                );
6063            }
6064            let mut ops = Vec::with_capacity(mutations.len());
6065            for mutation in mutations {
6066                match mutation {
6067                    Mutation::Patch {
6068                        table,
6069                        values,
6070                        base_version,
6071                    } => {
6072                        let schema_table = self
6073                            .schema
6074                            .table(&table)
6075                            .ok_or_else(|| "sync.unknown_table".to_owned())?;
6076                        let values = normalize_values_casing(schema_table, values)?;
6077                        let row_id = render_row_id_json(values.get(&schema_table.primary_key))?;
6078                        ops.push(self.prepare_patch(&table, &row_id, values, base_version)?);
6079                    }
6080                    Mutation::Upsert {
6081                        table,
6082                        values,
6083                        base_version,
6084                    } => {
6085                        let schema_table = self.schema.table(&table).ok_or_else(|| {
6086                            format!("sync.unknown_table: unknown table {table:?}")
6087                        })?;
6088                        // §5: value keys are accepted in snake_case AND the
6089                        // generated row types' camelCase; normalize to SQL truth
6090                        // before the pk lookup / codec see them.
6091                        let values = normalize_values_casing(schema_table, values)?;
6092                        let row_id = render_row_id_json(values.get(&schema_table.primary_key))?;
6093                        // §6.7 `mutate` marks every column present: fill missing
6094                        // nullable columns with NULL before the payload is built.
6095                        let values = full_row_values(schema_table, values)?;
6096                        self.refuse_sealed_row(schema_table, &row_id)?;
6097                        // §5.11: validate the payload encodes with the current
6098                        // codec. A key-selection or unknown-key failure is a
6099                        // durable rejection at the push seam (§10.3), never an
6100                        // author-time error. Full-row `mutate` presents every
6101                        // column, so no stored fallback is needed.
6102                        self.validate_author_encode(schema_table, &row_id, &values)?;
6103                        // §7.1 insert intent: the row is absent from the
6104                        // visible replica at call time, before this batch
6105                        // applies anything.
6106                        let insert = !self.visible_row_exists(schema_table, &row_id)?;
6107                        ops.push(OutboxOp {
6108                            upsert: true,
6109                            table,
6110                            row_id,
6111                            base_version,
6112                            values: Some(values),
6113                            insert,
6114                        });
6115                    }
6116                    Mutation::Delete {
6117                        table,
6118                        row_id,
6119                        base_version,
6120                    } => {
6121                        let schema_table = self.schema.table(&table).ok_or_else(|| {
6122                            format!("sync.unknown_table: unknown table {table:?}")
6123                        })?;
6124                        self.refuse_sealed_row(schema_table, &row_id)?;
6125                        // §2.4: the server rejects a rowId holding U+0000.
6126                        if row_id.contains('\0') {
6127                            return Err("sync.invalid_request: rowId holds U+0000 (§2.4)"
6128                                .to_owned()
6129                                .into());
6130                        }
6131                        ops.push(OutboxOp {
6132                            upsert: false,
6133                            table,
6134                            row_id,
6135                            base_version,
6136                            values: None,
6137                            insert: false,
6138                        });
6139                    }
6140                }
6141            }
6142            Ok(self.record_outbox_commit(ops, client_commit_id)?)
6143        })();
6144        self.finish_authoring(prior, outcome)
6145    }
6146
6147    /// Scope the retained `storage_failure` diagnostic to this call so an
6148    /// earlier operation's failure cannot classify a new one, and surface the
6149    /// call's own classified failure with its details. A fresh classified
6150    /// failure replaces the retained diagnostic; otherwise the earlier one is
6151    /// kept for the next sync.
6152    fn finish_authoring(
6153        &self,
6154        prior: Option<QueryReadFailure>,
6155        outcome: Result<String, ClientError>,
6156    ) -> Result<String, ClientError> {
6157        let fresh = self.storage_failure.borrow_mut().take();
6158        *self.storage_failure.borrow_mut() = fresh.clone().or(prior);
6159        outcome.map_err(|error| fresh.map(ClientError::from).unwrap_or(error))
6160    }
6161
6162    fn record_outbox_commit(
6163        &mut self,
6164        ops: Vec<OutboxOp>,
6165        client_commit_id: Option<String>,
6166    ) -> Result<String, String> {
6167        let commit = OutboxCommit {
6168            client_commit_id: client_commit_id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string()),
6169            ops,
6170        };
6171        self.begin_observation("syncular_mutation")?;
6172        let mut batch = ChangeAccumulator::default();
6173        for op in &commit.ops {
6174            let mut precise = self.record_row_scopes(&mut batch, &op.table, &op.row_id, false);
6175            if let Some(values) = &op.values {
6176                if let Some(table) = self.schema.table(&op.table) {
6177                    for scope in &table.scope_variables {
6178                        if let Some(Value::String(value)) = values.get(&scope.column) {
6179                            batch.scope(&op.table, format!("{}:{value}", scope.prefix));
6180                            precise = true;
6181                        }
6182                    }
6183                }
6184            }
6185            if !precise {
6186                batch.table(&op.table);
6187            }
6188        }
6189        if let Err(error) = self.persist_outbox_insert(&commit) {
6190            self.rollback_observation("syncular_mutation");
6191            return Err(error);
6192        }
6193        // A clean overlay contains the FIFO fold of every previous commit, so
6194        // the new commit applies over it. §7.1: an operation that does not
6195        // apply (a secondary unique collision) fails the whole commit; the
6196        // savepoint rollback removes the outbox entry and every visible write.
6197        // The append undoes itself: a rollback capture would copy the outbox.
6198        let was_dirty = self.overlay_dirty.snapshot();
6199        let id = commit.client_commit_id.clone();
6200        let appended = self.rebuild_overlay_if_dirty().and_then(|()| {
6201            commit
6202                .ops
6203                .iter()
6204                .try_for_each(|op| self.apply_outbox_op(op))?;
6205            self.outbox.push(Arc::new(commit));
6206            batch.status = true;
6207            self.finish_observation("syncular_mutation", batch)
6208                .inspect_err(|_| drop(self.outbox.pop()))
6209        });
6210        if let Err(error) = appended {
6211            self.rollback_observation("syncular_mutation");
6212            self.overlay_dirty.restore(was_dirty);
6213            return Err(error);
6214        }
6215        Ok(id)
6216    }
6217
6218    /// Record one §6.1 sparse upsert: the primary key plus the supplied
6219    /// non-scope columns are present; every other column is absent and stays
6220    /// untouched on the server and in the local overlay. An absent local base
6221    /// refuses the whole batch with sync.row_missing before enqueueing (§7.1).
6222    /// Failures are structured so callers read `code`/`message`/`details`.
6223    pub fn patch(
6224        &mut self,
6225        table: &str,
6226        row_id: &str,
6227        partial: Map<String, Value>,
6228        base_version: Option<i64>,
6229    ) -> Result<String, ClientError> {
6230        let prior = self.storage_failure.borrow_mut().take();
6231        let outcome = self.perform_patch(table, row_id, partial, base_version);
6232        self.finish_authoring(prior, outcome)
6233    }
6234
6235    /// The unscoped patch body. Public authoring entry points scope
6236    /// `storage_failure` around the whole operation, including any pre-read.
6237    fn perform_patch(
6238        &mut self,
6239        table: &str,
6240        row_id: &str,
6241        partial: Map<String, Value>,
6242        base_version: Option<i64>,
6243    ) -> Result<String, ClientError> {
6244        let operation = self.prepare_patch(table, row_id, partial, base_version)?;
6245        Ok(self.record_outbox_commit(vec![operation], None)?)
6246    }
6247
6248    fn prepare_patch(
6249        &self,
6250        table: &str,
6251        row_id: &str,
6252        partial: Map<String, Value>,
6253        base_version: Option<i64>,
6254    ) -> Result<OutboxOp, ClientError> {
6255        let schema_table = self
6256            .schema
6257            .table(table)
6258            .ok_or_else(|| format!("sync.unknown_table: unknown table {table:?}"))?;
6259        let partial = normalize_values_casing(schema_table, partial)?;
6260        if let Some(pk) = partial.get(&schema_table.primary_key) {
6261            if render_row_id_json(Some(pk)).ok().as_deref() != Some(row_id) {
6262                return Err(invalid_authoring(
6263                    "patch cannot change the primary key",
6264                    json!({ "table": table }),
6265                ));
6266            }
6267        }
6268        if !self.visible_row_exists(schema_table, row_id)? {
6269            return Err("sync.row_missing: a sparse patch requires a local row"
6270                .to_owned()
6271                .into());
6272        }
6273        self.refuse_sealed_row(schema_table, row_id)?;
6274        // §3.4 rule 5 / §6.2: scope columns are immutable on update. The
6275        // server accepts a present scope column whose value equals the
6276        // stored row and applies it as a no-op, so a decoded envelope
6277        // round-trips. The local row is the only value the client can prove
6278        // equality against; when the row is absent locally the client cannot
6279        // prove equality against the server's stored row and fails closed.
6280        // A primary key that is also a scope column needs no stored-row
6281        // read: the primary key in a sparse payload is by construction the
6282        // row id being patched (§6.1), so its value is proven equal already.
6283        for scope in &schema_table.scope_variables {
6284            if !partial.contains_key(&scope.column) || scope.column == schema_table.primary_key {
6285                continue;
6286            }
6287            let Some(column) = schema_table
6288                .columns
6289                .iter()
6290                .find(|column| column.name == scope.column)
6291            else {
6292                continue;
6293            };
6294            let stored = self
6295                .conn
6296                .query_row(
6297                    &format!(
6298                        "SELECT {} FROM {} WHERE {}",
6299                        quote_ident(&scope.column),
6300                        visible_table(&schema_table.name),
6301                        row_id_predicate(schema_table)
6302                    ),
6303                    rusqlite::params![row_id],
6304                    |row| Ok(sql_ref_to_json(column, row.get_ref(0)?)),
6305                )
6306                .optional()
6307                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
6308            // Decode the supplied value through the same column-type path the
6309            // rest of the patch encoding uses, so a value that only equals
6310            // the stored one in another number representation still compares
6311            // equal.
6312            let incoming = json_to_column_value(column, partial.get(&scope.column))
6313                .ok()
6314                .map(|value| column_value_to_json(&value));
6315            // A stored blob never equals a patch value, matching the server's
6316            // scope-column comparison (§3.4): bytes are unstorable as scope
6317            // values.
6318            let equal = stored.as_ref().is_some_and(|stored| {
6319                stored.get("$bytes").is_none() && incoming.as_ref() == Some(stored)
6320            });
6321            if !equal {
6322                return Err(invalid_authoring(
6323                    "patch cannot write a scope column (§3.4)",
6324                    json!({ "table": table, "column": scope.column }),
6325                ));
6326            }
6327        }
6328        let mut values = partial;
6329        // Presence-set semantics: a proven-equal scope column is dropped, so
6330        // the patch leaves the stored value untouched (§6.2).
6331        for scope in &schema_table.scope_variables {
6332            values.remove(&scope.column);
6333        }
6334        let pk_value = match schema_table.columns[schema_table.pk_index].ty {
6335            // §2.4: the rowId of an integer key is its canonical decimal form.
6336            ColumnType::Integer => match row_id.parse::<i64>() {
6337                Ok(v)
6338                    if v.to_string() == row_id
6339                        && (-ssp2::primitives::I64_SAFE_MAX..=ssp2::primitives::I64_SAFE_MAX)
6340                            .contains(&v) =>
6341                {
6342                    Value::from(v)
6343                }
6344                _ => {
6345                    return Err(format!(
6346                    "sync.invalid_request: table {table:?}: invalid integer primary key {row_id:?}"
6347                )
6348                    .into())
6349                }
6350            },
6351            ColumnType::Boolean => match row_id {
6352                "true" => Value::Bool(true),
6353                "false" => Value::Bool(false),
6354                _ => {
6355                    return Err(format!(
6356                    "sync.invalid_request: table {table:?}: invalid boolean primary key {row_id:?}"
6357                )
6358                    .into())
6359                }
6360            },
6361            _ => Value::from(row_id.to_owned()),
6362        };
6363        values.insert(schema_table.primary_key.clone(), pk_value);
6364        // §6.1: a patch cannot null a non-nullable column.
6365        if let Some(column) = schema_table
6366            .columns
6367            .iter()
6368            .find(|column| !column.nullable && values.get(&column.name) == Some(&Value::Null))
6369        {
6370            return Err(invalid_authoring(
6371                "a non-nullable column is null (§6.1)",
6372                json!({ "table": table, "column": column.name }),
6373            ));
6374        }
6375        // §5.11: structural validation only; an absent key-id selector is
6376        // resolved at the push seam from the stored local row, and a
6377        // key-selection or unknown-key failure becomes a durable
6378        // `client.encrypt_failed` rejection there (§10.3), never an
6379        // author-time error.
6380        self.validate_author_encode(schema_table, row_id, &values)?;
6381        Ok(OutboxOp {
6382            upsert: true,
6383            table: table.to_owned(),
6384            row_id: row_id.to_owned(),
6385            base_version,
6386            values: Some(values),
6387            insert: false,
6388        })
6389    }
6390
6391    /// §5.11 key-id fallback: the stored local row's selector value as a
6392    /// positional row (only the selector slot filled). Returns None when no
6393    /// selector is configured, the row is locally absent, or the stored
6394    /// value is not a usable key id. A failed storage read is an error.
6395    fn stored_key_fallback(
6396        &self,
6397        table: &TableSchema,
6398        row_id: &str,
6399    ) -> Result<Option<Row>, String> {
6400        let Some(selector) = self.encryption.key_id_columns.get(&table.name) else {
6401            return Ok(None);
6402        };
6403        let Some(index) = table
6404            .columns
6405            .iter()
6406            .position(|column| &column.name == selector)
6407        else {
6408            return Ok(None);
6409        };
6410        let sql = format!(
6411            "SELECT {} FROM {} WHERE {}",
6412            quote_ident(selector),
6413            visible_table(&table.name),
6414            row_id_predicate(table)
6415        );
6416        let key = self
6417            .conn
6418            .query_row(&sql, rusqlite::params![row_id], |row| {
6419                row.get::<_, Option<String>>(0)
6420            })
6421            .optional()
6422            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
6423            .flatten()
6424            .filter(|key| !key.is_empty());
6425        let Some(key) = key else {
6426            return Ok(None);
6427        };
6428        let mut fallback: Row = vec![None; table.columns.len()];
6429        fallback[index] = Some(ColumnValue::String(key));
6430        Ok(Some(fallback))
6431    }
6432
6433    /// §5.11/§10.3 author-time encode check: a structural failure (a
6434    /// non-nullable NULL, a value the codec rejects) is an author-time
6435    /// error; a key-selection or unknown-key failure is deferred to the push
6436    /// seam, where it becomes a durable `client.encrypt_failed` rejection.
6437    fn validate_author_encode(
6438        &self,
6439        table: &TableSchema,
6440        row_id: &str,
6441        values: &Map<String, Value>,
6442    ) -> Result<(), String> {
6443        match encode_sparse_row_json(table, row_id, values, &self.encryption, None) {
6444            Err(error) if error.starts_with(ENCRYPT_FAILED_CODE) => Ok(()),
6445            result => result.map(|_| ()),
6446        }
6447    }
6448
6449    /// §5.11/§10.3: drop every pending commit whose push encode fails with
6450    /// `client.encrypt_failed` (an unresolvable key id or an unknown key) and
6451    /// raise one durable rejection per commit. `sync()` never aborts for an
6452    /// encode failure. Mirrors §7.4.4's `drop_incompatible_outbox`, including
6453    /// undoing the dropped commit's purely-optimistic rows.
6454    /// §7.1/§7.4.4: drop pending commits whose values the current codec refuses
6455    /// (an envelope or a type an earlier version accepted). `with_encryption`
6456    /// additionally drops commits whose §5.11 encryption cannot resolve a key;
6457    /// the startup boundary leaves those to the send seam, where hosts configure
6458    /// keys after open.
6459    /// Recovery runs as one observation. An enclosing observation (the guarded
6460    /// schema reset) passes its `batch`, so the rejection, outcome, and table
6461    /// domains join that change event instead of publishing a second one.
6462    fn drop_unencodable_outbox(
6463        &mut self,
6464        with_encryption: bool,
6465        batch: Option<&mut ChangeAccumulator>,
6466    ) -> Result<bool, String> {
6467        let mut failures = Vec::new();
6468        for commit in self.outbox.iter() {
6469            for op in &commit.ops {
6470                let Some(table) = self.schema.table(&op.table).filter(|_| op.upsert) else {
6471                    continue;
6472                };
6473                // An insert lacking a non-nullable column is left to
6474                // `drop_incompatible_outbox` (§7.4.4).
6475                let Some(values) = op.presented_values(table) else {
6476                    continue;
6477                };
6478                let fallback = self.stored_key_fallback(table, &op.row_id)?;
6479                let failure = match encode_sparse_row_json(
6480                    table,
6481                    &op.row_id,
6482                    &values,
6483                    &self.encryption,
6484                    fallback.as_ref(),
6485                ) {
6486                    Err(error) if error.starts_with(ENCRYPT_FAILED_CODE) => {
6487                        with_encryption.then_some((error, ENCRYPT_FAILED_CODE))
6488                    }
6489                    // §7.1/§7.4.4: a value the current codec refuses was
6490                    // persisted by a version that accepted it; it leaves
6491                    // through the incompatible-outbox path, so the FIFO
6492                    // queue never wedges.
6493                    Err(error) if error.starts_with(INVALID_REQUEST_CODE) => Some((
6494                        LEGACY_VALUE_REFUSAL_MESSAGE.to_owned(),
6495                        OUTBOX_INCOMPATIBLE_CODE,
6496                    )),
6497                    _ => None,
6498                };
6499                if let Some((error, code)) = failure {
6500                    failures.push((commit.clone(), error, code));
6501                    break;
6502                }
6503            }
6504        }
6505        if failures.is_empty() {
6506            return Ok(false);
6507        }
6508        // §7.4.4: one observation covers the durable outcome writes, the outbox
6509        // removal, the in-memory drop, and the overlay rebuild, so a storage fault
6510        // leaves the replica and the client unchanged, a successful recovery
6511        // publishes one batch with the rejection, outcome, and table domains, and
6512        // an ordinary open with nothing to recover publishes nothing.
6513        let rollback = self.capture();
6514        let owns_observation = batch.is_none();
6515        let owns_transaction = self.conn.is_autocommit();
6516        let mut domains = ChangeAccumulator::default();
6517        if owns_observation {
6518            self.begin_observation("syncular_value_recovery")?;
6519        }
6520        let dropped = (|| -> Result<(), String> {
6521            for (commit, message, code) in failures {
6522                if self.retain_failed_commits && code == ENCRYPT_FAILED_CODE {
6523                    self.retain_failed_commit(&commit.client_commit_id, &commit.ops)?;
6524                }
6525                let representable = commit.ops.iter().all(Self::outbox_op_is_journalable);
6526                let rejection = RejectionRecord {
6527                    client_commit_id: commit.client_commit_id.clone(),
6528                    op_index: 0,
6529                    code: code.to_owned(),
6530                    message,
6531                    retryable: false,
6532                    details: (code == OUTBOX_INCOMPATIBLE_CODE).then(|| RejectionDetails {
6533                        field_paths: None,
6534                        reason: Some(INVALID_STORED_VALUES_REASON.to_owned()),
6535                        required_action: None,
6536                        references: None,
6537                    }),
6538                    operation: representable
6539                        .then(|| commit.ops.first())
6540                        .flatten()
6541                        .map(CommitOperation::from),
6542                };
6543                self.persist_commit_outcome(
6544                    &commit.client_commit_id,
6545                    CommitOutcomeStatus::Rejected,
6546                    &[CommitOperationOutcome::Error {
6547                        rejection: rejection.clone(),
6548                    }],
6549                    representable.then_some(commit.ops.as_slice()),
6550                )?;
6551                self.delete_outbox_persisted(&commit.client_commit_id)?;
6552                self.outbox
6553                    .retain(|candidate| candidate.client_commit_id != commit.client_commit_id);
6554                self.rejections.push(rejection);
6555                for operation in &commit.ops {
6556                    domains.table(operation.table.as_str());
6557                }
6558            }
6559            domains.status = true;
6560            domains.rejections = true;
6561            domains.outcomes = true;
6562            self.prune_commit_outcomes()?;
6563            // The visible projection is re-derived from the surviving base + outbox.
6564            self.overlay_dirty.set(true);
6565            self.rebuild_overlay()?;
6566            Ok(())
6567        })();
6568        let result = match batch {
6569            Some(batch) => {
6570                if dropped.is_ok() {
6571                    for table in domains.tables.keys() {
6572                        batch.table(table.as_str());
6573                    }
6574                    batch.status |= domains.status;
6575                    batch.rejections |= domains.rejections;
6576                    batch.outcomes |= domains.outcomes;
6577                }
6578                dropped
6579            }
6580            None => {
6581                dropped.and_then(|()| self.finish_observation("syncular_value_recovery", domains))
6582            }
6583        };
6584        if let Err(error) = result {
6585            if owns_observation {
6586                if owns_transaction {
6587                    if let Err(rollback) = self.conn.execute_batch("ROLLBACK") {
6588                        let mut failure = self.storage_failure.borrow_mut();
6589                        let failure =
6590                            failure.get_or_insert_with(|| QueryReadFailure::from(error.clone()));
6591                        failure.rollback_failure = Some(Box::new(QueryReadFailure::from(rollback)));
6592                    }
6593                } else {
6594                    self.rollback_observation("syncular_value_recovery");
6595                }
6596            }
6597            self.restore(rollback);
6598            return Err(error);
6599        }
6600        Ok(true)
6601    }
6602
6603    pub fn pending_commit_ids(&self) -> Vec<String> {
6604        self.outbox
6605            .iter()
6606            .map(|c| c.client_commit_id.clone())
6607            .collect()
6608    }
6609
6610    /// §6.1: the sparse push payload bytes of every pending upsert operation,
6611    /// FIFO — exactly what the next push round would put on the wire. Used by
6612    /// conformance B.21(h) to pin cross-core byte identity.
6613    pub fn pending_payloads(&self) -> Result<Vec<Vec<u8>>, String> {
6614        let mut payloads = Vec::new();
6615        for commit in self.outbox.iter() {
6616            for op in &commit.ops {
6617                let Some(table) = self.schema.table(&op.table).filter(|_| op.upsert) else {
6618                    continue;
6619                };
6620                let Some(values) = op.presented_values(table) else {
6621                    continue;
6622                };
6623                let fallback = self.stored_key_fallback(table, &op.row_id)?;
6624                if let Ok(payload) = encode_sparse_row_json(
6625                    table,
6626                    &op.row_id,
6627                    &values,
6628                    &self.encryption,
6629                    fallback.as_ref(),
6630                ) {
6631                    payloads.push(payload);
6632                }
6633            }
6634        }
6635        Ok(payloads)
6636    }
6637
6638    pub fn conflicts(&self) -> &[ConflictRecord] {
6639        &self.conflicts
6640    }
6641
6642    pub fn rejections(&self) -> &[RejectionRecord] {
6643        &self.rejections
6644    }
6645
6646    pub fn resolve_commit_outcome(
6647        &mut self,
6648        input: ResolveCommitOutcomeInput,
6649    ) -> Result<CommitOutcome, String> {
6650        let current = self
6651            .commit_outcome(&input.client_commit_id)?
6652            .ok_or_else(|| {
6653                format!(
6654                    "sync.outcome_not_found: no durable outcome exists for {:?}",
6655                    input.client_commit_id
6656                )
6657            })?;
6658        if current.resolution != CommitOutcomeResolution::Active {
6659            return Ok(current);
6660        }
6661        if input.resolution == CommitOutcomeResolution::Active {
6662            return Err("sync.invalid_request: resolution must leave active state".to_owned());
6663        }
6664        match input.resolution {
6665            CommitOutcomeResolution::Superseded => {
6666                let replacement = input
6667                    .replacement_client_commit_id
6668                    .as_deref()
6669                    .filter(|value| !value.is_empty() && *value != input.client_commit_id)
6670                    .ok_or_else(|| {
6671                        "sync.invalid_request: superseded outcomes require a distinct replacementClientCommitId"
6672                            .to_owned()
6673                    })?;
6674                let _ = replacement;
6675            }
6676            _ if input.replacement_client_commit_id.is_some() => {
6677                return Err(
6678                    "sync.invalid_request: replacementClientCommitId is valid only for superseded outcomes"
6679                        .to_owned(),
6680                );
6681            }
6682            _ => {}
6683        }
6684        let allowed = match current.status {
6685            CommitOutcomeStatus::Conflict => matches!(
6686                input.resolution,
6687                CommitOutcomeResolution::ResolvedKeepServer | CommitOutcomeResolution::Superseded
6688            ),
6689            CommitOutcomeStatus::Rejected => {
6690                matches!(
6691                    input.resolution,
6692                    CommitOutcomeResolution::Superseded
6693                        | CommitOutcomeResolution::ResolvedKeepServer
6694                )
6695            }
6696            CommitOutcomeStatus::Applied | CommitOutcomeStatus::Cached => {
6697                input.resolution == CommitOutcomeResolution::Dismissed
6698            }
6699        };
6700        if !allowed {
6701            return Err(format!(
6702                "sync.invalid_request: resolution {:?} is invalid for {:?} outcome",
6703                input.resolution, current.status
6704            ));
6705        }
6706
6707        let rollback = self.capture();
6708        self.begin_observation("syncular_outcome_resolution")?;
6709        let result = (|| {
6710            self.conn
6711                .execute(
6712                    "UPDATE _syncular_commit_outcomes
6713                        SET resolution = ?1, resolved_at_ms = ?2,
6714                            replacement_client_commit_id = ?3
6715                      WHERE client_commit_id = ?4 AND resolution = 'active'",
6716                    rusqlite::params![
6717                        Self::outcome_resolution_name(input.resolution),
6718                        self.clock_now_ms(),
6719                        input.replacement_client_commit_id,
6720                        input.client_commit_id
6721                    ],
6722                )
6723                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
6724            let resolved = self
6725                .commit_outcome(&current.client_commit_id)?
6726                .ok_or_else(|| "sync.outcome_not_found: outcome disappeared".to_owned())?;
6727            self.conn
6728                .execute(
6729                    "DELETE FROM _syncular_failed_commits WHERE client_commit_id = ?1",
6730                    rusqlite::params![input.client_commit_id],
6731                )
6732                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
6733            for (commit, _) in self.failed_commits.iter() {
6734                if commit.client_commit_id == input.client_commit_id {
6735                    for operation in &commit.ops {
6736                        self.overlay_dirty.table(&operation.table);
6737                    }
6738                }
6739            }
6740            self.failed_commits
6741                .retain(|(commit, _)| commit.client_commit_id != input.client_commit_id);
6742            self.prune_commit_outcomes()?;
6743            Ok(resolved)
6744        })();
6745        let resolved = match result {
6746            Ok(outcome) => outcome,
6747            Err(error) => {
6748                self.rollback_observation("syncular_outcome_resolution");
6749                self.restore(rollback);
6750                return Err(error);
6751            }
6752        };
6753        self.conflicts
6754            .retain(|record| record.client_commit_id != current.client_commit_id);
6755        self.rejections
6756            .retain(|record| record.client_commit_id != current.client_commit_id);
6757        let mut batch = ChangeAccumulator {
6758            conflicts: current.status == CommitOutcomeStatus::Conflict,
6759            rejections: current.status == CommitOutcomeStatus::Rejected,
6760            outcomes: true,
6761            ..ChangeAccumulator::default()
6762        };
6763        if let Some(operations) = &current.operations {
6764            for operation in operations {
6765                batch.table(&operation.table);
6766            }
6767        }
6768        if let Err(error) = self
6769            .rebuild_overlay_if_dirty()
6770            .and_then(|()| self.finish_observation("syncular_outcome_resolution", batch))
6771        {
6772            self.rollback_observation("syncular_outcome_resolution");
6773            self.restore(rollback);
6774            return Err(error);
6775        }
6776        Ok(resolved)
6777    }
6778
6779    pub fn schema_floor(&self) -> Option<&SchemaFloor> {
6780        self.schema_floor.as_ref()
6781    }
6782
6783    /// §7.3.5: the client's opaque auth-lease state, if any.
6784    pub fn lease_state(&self) -> Option<&LeaseState> {
6785        self.lease_state.as_ref()
6786    }
6787
6788    /// §7.3.5: record a request-level lease error (stop-and-surface). Only
6789    /// the two lease codes set it; other errors leave leaseState untouched.
6790    fn record_lease_error(&mut self, code: &str) -> Result<(), String> {
6791        if code != "sync.auth_lease_required" && code != "sync.auth_lease_revoked" {
6792            return Ok(());
6793        }
6794        let mut next = self.lease_state.clone().unwrap_or_default();
6795        next.error_code = Some(code.to_owned());
6796        self.set_lease_state(Some(next))
6797    }
6798
6799    fn set_lease_state(&mut self, next: Option<LeaseState>) -> Result<(), String> {
6800        if self.lease_state == next {
6801            return Ok(());
6802        }
6803        self.begin_observation("syncular_lease")?;
6804        let rollback = self.capture();
6805        self.lease_state = next;
6806        let persisted = match &self.lease_state {
6807            Some(lease) => serde_json::to_string(lease)
6808                .map_err(|error| error.to_string())
6809                .and_then(|json| self.set_meta(LEASE_STATE_KEY, &json)),
6810            None => self.delete_meta(LEASE_STATE_KEY),
6811        };
6812        let batch = ChangeAccumulator {
6813            status: true,
6814            ..ChangeAccumulator::default()
6815        };
6816        if let Err(error) =
6817            persisted.and_then(|()| self.finish_observation("syncular_lease", batch))
6818        {
6819            self.rollback_observation("syncular_lease");
6820            self.restore(rollback);
6821            return Err(error);
6822        }
6823        Ok(())
6824    }
6825
6826    fn set_schema_floor(&mut self, next: Option<SchemaFloor>) -> Result<(), String> {
6827        if self.schema_floor == next {
6828            return Ok(());
6829        }
6830        self.begin_observation("syncular_schema_floor")?;
6831        let rollback = self.capture();
6832        self.stopped = next.is_some();
6833        self.schema_floor = next;
6834        let persisted = match &self.schema_floor {
6835            Some(floor) => serde_json::to_string(floor)
6836                .map_err(|error| error.to_string())
6837                .and_then(|json| self.set_meta(SCHEMA_FLOOR_KEY, &json)),
6838            None => self.delete_meta(SCHEMA_FLOOR_KEY),
6839        };
6840        let batch = ChangeAccumulator {
6841            status: true,
6842            ..ChangeAccumulator::default()
6843        };
6844        if let Err(error) =
6845            persisted.and_then(|()| self.finish_observation("syncular_schema_floor", batch))
6846        {
6847            self.rollback_observation("syncular_schema_floor");
6848            self.restore(rollback);
6849            return Err(error);
6850        }
6851        Ok(())
6852    }
6853
6854    fn set_upgrading(&mut self, value: bool) {
6855        if self.upgrading == value {
6856            return;
6857        }
6858        if self.begin_observation("syncular_upgrading").is_err() {
6859            return;
6860        }
6861        self.upgrading = value;
6862        let batch = ChangeAccumulator {
6863            status: true,
6864            ..ChangeAccumulator::default()
6865        };
6866        if self
6867            .finish_observation("syncular_upgrading", batch)
6868            .is_err()
6869        {
6870            self.rollback_observation("syncular_upgrading");
6871        }
6872    }
6873
6874    pub fn sync_needed(&self) -> bool {
6875        self.sync_needed
6876    }
6877
6878    pub fn read_rows(&self, table: &str) -> Result<Vec<RowState>, String> {
6879        let schema_table = self
6880            .schema
6881            .table(table)
6882            .ok_or_else(|| format!("unknown table {table:?}"))?;
6883        let sql = format!(
6884            "SELECT * FROM {} ORDER BY {} ASC",
6885            visible_table(table),
6886            quote_ident(&schema_table.primary_key)
6887        );
6888        let mut stmt = self
6889            .conn
6890            .prepare(&sql)
6891            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
6892        let mut rows = stmt
6893            .query([])
6894            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
6895        let mut out = Vec::new();
6896        while let Some(row) = rows
6897            .next()
6898            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
6899        {
6900            let mut values = Map::new();
6901            for (i, column) in schema_table.columns.iter().enumerate() {
6902                let value = row
6903                    .get_ref(i)
6904                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
6905                values.insert(column.name.clone(), sql_ref_to_json(column, value));
6906            }
6907            let version: i64 = row
6908                .get(schema_table.columns.len())
6909                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
6910            let row_id = match values.get(&schema_table.primary_key) {
6911                Some(Value::String(s)) => s.clone(),
6912                Some(Value::Number(n)) => n.to_string(),
6913                Some(Value::Bool(b)) => b.to_string(),
6914                other => format!("{}", other.cloned().unwrap_or(Value::Null)),
6915            };
6916            let sealed_key = if schema_table.has_encrypted_columns() {
6917                row.get(schema_table.columns.len() + 1)
6918                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
6919            } else {
6920                None
6921            };
6922            out.push(RowState {
6923                row_id,
6924                version,
6925                values,
6926                sealed_key,
6927            });
6928        }
6929        Ok(out)
6930    }
6931
6932    // -- §5.10.5 native CRDT (the `crdt-yjs` feature) --------------------------
6933    //
6934    // The Rust face of the §5.10.4 client model: a local crdt edit loads the
6935    // current stored (server-merged ⊕ pending-overlay) column bytes, applies
6936    // the op with `yrs`, re-encodes the whole doc state, and records a
6937    // baseVersion-less sparse upsert carrying only the primary key and the
6938    // crdt column (§5.10.3, §6.6). No local merge — merging is server-side;
6939    // the overlay re-materializes the edit immediately (optimistic apply,
6940    // §7.1) and the server-merged bytes arrive on the next pull, idempotently.
6941    // Byte-compatible with `@syncular/crdt-yjs`.
6942
6943    /// The current stored value of a `crdt` column for one row — the visible
6944    /// (optimistic) bytes, or `None` when the row is absent or the column is
6945    /// NULL (the empty document, §5.10.1). Errors if the column is not a
6946    /// `crdt` column (guards the app against a typo'd column name).
6947    #[cfg(feature = "crdt-yjs")]
6948    fn crdt_column_bytes(
6949        &self,
6950        table: &str,
6951        row_id: &str,
6952        column: &str,
6953    ) -> Result<Option<Vec<u8>>, String> {
6954        let schema_table = self
6955            .schema
6956            .table(table)
6957            .ok_or_else(|| format!("unknown table {table:?}"))?;
6958        let col = schema_table
6959            .columns
6960            .iter()
6961            .find(|c| c.name == column)
6962            .ok_or_else(|| format!("table {table:?} has no column {column:?}"))?;
6963        if col.ty != ColumnType::Crdt {
6964            return Err(format!("column {column:?} is not a crdt column (§5.10.1)"));
6965        }
6966        let sql = format!(
6967            "SELECT {} FROM {} WHERE {}",
6968            quote_ident(column),
6969            visible_table(table),
6970            row_id_predicate(schema_table)
6971        );
6972        let bytes: Option<Vec<u8>> = self
6973            .conn
6974            .prepare_cached(&sql)
6975            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
6976            .query_row(rusqlite::params![row_id], |row| {
6977                row.get::<_, Option<Vec<u8>>>(0)
6978            })
6979            .map_err(|e| match e {
6980                rusqlite::Error::QueryReturnedNoRows => "no such row".to_owned(),
6981                other => Self::sqlite_failure(&self.storage_failure, other),
6982            })?;
6983        Ok(bytes)
6984    }
6985
6986    /// §5.10.4 materialize: the collaborative text of a `crdt` column, decoded
6987    /// from the stored bytes with `yrs` — `YjsColumn.text(name).toString()`.
6988    /// An absent row / NULL column is the empty document (empty string).
6989    #[cfg(feature = "crdt-yjs")]
6990    pub fn crdt_text(
6991        &self,
6992        table: &str,
6993        row_id: &str,
6994        column: &str,
6995        name: &str,
6996    ) -> Result<String, String> {
6997        let bytes = self
6998            .crdt_column_bytes(table, row_id, column)?
6999            .unwrap_or_default();
7000        crate::crdt::text(&bytes, name)
7001    }
7002
7003    /// §5.10.4 push-an-update: apply a text insert to a `crdt` column and
7004    /// record the resulting crdt-only sparse operation. Returns the enqueued
7005    /// `clientCommitId`.
7006    #[cfg(feature = "crdt-yjs")]
7007    pub fn crdt_insert_text(
7008        &mut self,
7009        table: &str,
7010        row_id: &str,
7011        column: &str,
7012        name: &str,
7013        index: u32,
7014        value: &str,
7015    ) -> Result<String, ClientError> {
7016        let prior = self.storage_failure.borrow_mut().take();
7017        let outcome = (|| -> Result<String, ClientError> {
7018            let current = self
7019                .crdt_column_bytes(table, row_id, column)?
7020                .unwrap_or_default();
7021            let update = crate::crdt::insert_text(&current, name, index, value)?;
7022            self.perform_crdt_push_update(table, row_id, column, &update)
7023        })();
7024        self.finish_authoring(prior, outcome)
7025    }
7026
7027    /// §5.10.4 push-an-update: apply a text delete to a `crdt` column and
7028    /// record the resulting crdt-only sparse operation. Returns the enqueued
7029    /// `clientCommitId`.
7030    #[cfg(feature = "crdt-yjs")]
7031    pub fn crdt_delete_text(
7032        &mut self,
7033        table: &str,
7034        row_id: &str,
7035        column: &str,
7036        name: &str,
7037        index: u32,
7038        len: u32,
7039    ) -> Result<String, ClientError> {
7040        let prior = self.storage_failure.borrow_mut().take();
7041        let outcome = (|| -> Result<String, ClientError> {
7042            let current = self
7043                .crdt_column_bytes(table, row_id, column)?
7044                .unwrap_or_default();
7045            let update = crate::crdt::delete_text(&current, name, index, len)?;
7046            self.perform_crdt_push_update(table, row_id, column, &update)
7047        })();
7048        self.finish_authoring(prior, outcome)
7049    }
7050
7051    /// §5.10.4 generic escape hatch: apply an arbitrary Yjs update onto a
7052    /// `crdt` column's current state and record the resulting crdt-only sparse
7053    /// operation. The app authored the update with its own `yrs` model.
7054    /// Returns the enqueued `clientCommitId`.
7055    #[cfg(feature = "crdt-yjs")]
7056    pub fn crdt_apply_update(
7057        &mut self,
7058        table: &str,
7059        row_id: &str,
7060        column: &str,
7061        update: &[u8],
7062    ) -> Result<String, ClientError> {
7063        let prior = self.storage_failure.borrow_mut().take();
7064        let outcome = (|| -> Result<String, ClientError> {
7065            let current = self
7066                .crdt_column_bytes(table, row_id, column)?
7067                .unwrap_or_default();
7068            let next = crate::crdt::apply_update(&current, update)?;
7069            self.perform_crdt_push_update(table, row_id, column, &next)
7070        })();
7071        self.finish_authoring(prior, outcome)
7072    }
7073
7074    /// Shared tail of the crdt edit methods: record a §6.1 sparse crdt-only
7075    /// upsert — the primary key plus the crdt column, no `baseVersion` — and
7076    /// enqueue it. The server merges the crdt column and leaves every other
7077    /// column untouched (§5.10.3), so a baseVersion-less crdt edit cannot
7078    /// clobber a concurrent edit to another column. A locally absent row
7079    /// records the same partial operation; the server answers per §6.2.
7080    #[cfg(feature = "crdt-yjs")]
7081    fn perform_crdt_push_update(
7082        &mut self,
7083        table: &str,
7084        row_id: &str,
7085        column: &str,
7086        crdt_bytes: &[u8],
7087    ) -> Result<String, ClientError> {
7088        let mut bytes_obj = Map::new();
7089        bytes_obj.insert("$bytes".to_owned(), Value::from(bytes_to_hex(crdt_bytes)));
7090        let mut partial = Map::new();
7091        partial.insert(column.to_owned(), Value::Object(bytes_obj));
7092        self.perform_patch(table, row_id, partial, None)
7093    }
7094
7095    /// Run an arbitrary read-only SQL query against the local database and
7096    /// return each row as a `column-name → JSON value` map. This is the seam
7097    /// the React `useSyncQuery` live-query API needs (it takes app-authored
7098    /// SQL over the visible tables/views, not a fixed table read like
7099    /// [`read_rows`]).
7100    ///
7101    /// Bound `params` are the driver value forms: JSON strings/numbers/bools/
7102    /// null bind directly; a `{"$bytes": hex}` object binds as a BLOB — the
7103    /// same envelope the command surface uses everywhere else. Output BLOB
7104    /// columns come back as `{"$bytes": hex}` to round-trip cleanly.
7105    ///
7106    /// The result column typing is dynamic (SQLite's stored affinity), because
7107    /// arbitrary SQL can alias, join, and compute — there is no schema column
7108    /// to consult per output cell, unlike [`read_rows`].
7109    pub fn query(&self, sql: &str, params: &[QueryValue]) -> Result<Vec<QueryRow>, String> {
7110        query_connection(&self.conn, sql, params).map_err(|failure| failure.to_string())
7111    }
7112
7113    /// Repository benchmark and conformance access, present only with `bench-internals`.
7114    #[cfg(feature = "bench-internals")]
7115    #[doc(hidden)]
7116    pub fn benchmark_connection(&mut self) -> &mut Connection {
7117        &mut self.conn
7118    }
7119
7120    /// Private benchmark intervals; absent from ordinary builds and diagnostics.
7121    #[cfg(feature = "bench-internals")]
7122    #[doc(hidden)]
7123    pub fn benchmark_phases(
7124        &self,
7125        enabled: Option<bool>,
7126        reset: bool,
7127    ) -> Result<Option<Value>, String> {
7128        self.benchmark_phases.configure(enabled, reset)?;
7129        Ok(self.benchmark_phases.snapshot())
7130    }
7131
7132    /// One serialized, read-only authority observation, including durable coverage.
7133    pub fn authority_snapshot(&self) -> Result<Value, String> {
7134        validate_authority_reads(&self.schema, &self.limits.authority_reads)?;
7135        if self.limits.authority_reads.is_empty() {
7136            return Err("client.authority_read_forbidden: no authority reads were declared at client creation".into());
7137        }
7138        self.conn
7139            .execute_batch("SAVEPOINT authority_snapshot")
7140            .map_err(|e| e.to_string())?;
7141        let result = (|| {
7142            let revision = self
7143                .get_meta(LOCAL_REVISION_KEY)?
7144                .ok_or("sync.local_corrupt: authority revision is missing")?;
7145            let parsed_revision = revision
7146                .parse::<u64>()
7147                .map_err(|_| "sync.local_corrupt: authority revision is invalid")?;
7148            if parsed_revision.to_string() != revision {
7149                return Err("sync.local_corrupt: authority revision is invalid".into());
7150            }
7151            let mut tables = Vec::new();
7152            for read in &self.limits.authority_reads {
7153                let table = self
7154                    .schema
7155                    .table(&read.table)
7156                    .ok_or("client.authority_read_forbidden: authority table is not declared")?;
7157                let scalar = |column: &str, value: &Value| -> Result<String, String> {
7158                    let ty = table
7159                        .columns
7160                        .iter()
7161                        .find(|c| c.name == column)
7162                        .expect("validated authority column")
7163                        .ty;
7164                    if ty == ColumnType::Boolean {
7165                        if let Some(value) = value.as_bool() {
7166                            return Ok(value.to_string());
7167                        }
7168                        return match value.as_i64() {
7169                            Some(0) => Ok("false".into()),
7170                            Some(1) => Ok("true".into()),
7171                            _ => Err("sync.local_corrupt: authority boolean is invalid".into()),
7172                        };
7173                    }
7174                    match value {
7175                        Value::String(s) => Ok(s.clone()),
7176                        Value::Number(n) => Ok(n.to_string()),
7177                        Value::Object(o) if ty == ColumnType::Integer && o.len() == 1 => o
7178                            .get("$bigint")
7179                            .and_then(Value::as_str)
7180                            .map(str::to_owned)
7181                            .ok_or("sync.local_corrupt: authority integer is invalid".into()),
7182                        _ => {
7183                            Err("sync.local_corrupt: authority scalar is missing or invalid".into())
7184                        }
7185                    }
7186                };
7187                let matches = |row: &QueryRow| -> Result<bool, String> {
7188                    for (variable, values) in &read.scopes {
7189                        let column = table
7190                            .scope_column(variable)
7191                            .expect("validated authority scope");
7192                        let value = row
7193                            .get(column)
7194                            .ok_or("sync.local_corrupt: authority scope column is missing")?;
7195                        if value.is_null() || !values.contains(&scalar(column, value)?) {
7196                            return Ok(false);
7197                        }
7198                    }
7199                    Ok(true)
7200                };
7201                let selected = read
7202                    .columns
7203                    .iter()
7204                    .map(|c| quote_ident(c))
7205                    .collect::<Vec<_>>()
7206                    .join(",");
7207                let mut intent = BTreeSet::new();
7208                let mut relevant = BTreeSet::new();
7209                for row in query_connection(&self.conn,
7210                    "SELECT id,values_json FROM (SELECT json_extract(value,'$.table') AS tbl,json_extract(value,'$.rowId') AS id,json_extract(value,'$.values') AS values_json FROM _syncular_outbox,json_each(ops_json) UNION ALL SELECT json_extract(value,'$.table'),json_extract(value,'$.rowId'),json_extract(value,'$.values') FROM _syncular_failed_commits,json_each(operations_json) UNION ALL SELECT tbl,id,intent_json FROM _syncular_acked_rows) WHERE tbl=?",
7211                    &[Value::String(read.table.clone())]).map_err(|e| e.to_string())? {
7212                    let id = row.get("id").and_then(Value::as_str).ok_or("sync.local_corrupt: authority intent id is invalid")?.to_owned();
7213                    intent.insert(id.clone());
7214                    if let Some(raw) = row.get("values_json").and_then(Value::as_str) {
7215                        let values: QueryRow = serde_json::from_str(raw).map_err(|_| "sync.local_corrupt: authority intent values are invalid")?;
7216                        if read.scopes.keys().all(|v| values.contains_key(table.scope_column(v).expect("validated scope"))) && matches(&values)? {relevant.insert(id);}
7217                    }
7218                }
7219                let mut rows = BTreeMap::new();
7220                for full in [base_table(&read.table), visible_table(&read.table)] {
7221                    for mut values in query_connection(
7222                        &self.conn,
7223                        &format!(
7224                            "SELECT {selected},_syncular_version AS authority_version FROM {full}"
7225                        ),
7226                        &[],
7227                    )
7228                    .map_err(|e| e.to_string())?
7229                    {
7230                        if !matches(&values)? {
7231                            continue;
7232                        }
7233                        let id = scalar(
7234                            &table.primary_key,
7235                            values
7236                                .get(&table.primary_key)
7237                                .expect("selected primary key"),
7238                        )?;
7239                        relevant.insert(id.clone());
7240                        if full == base_table(&read.table) {
7241                            let version = values
7242                                .remove("authority_version")
7243                                .expect("selected version");
7244                            let version = version
7245                                .as_i64()
7246                                .ok_or("sync.local_corrupt: authority server version is invalid")?;
7247                            if version >= 0 {
7248                                rows.insert(id.clone(), json!({"values": values, "version": version, "hasLocalIntent": intent.contains(&id)}));
7249                            }
7250                        }
7251                    }
7252                }
7253                intent.retain(|id| relevant.contains(id));
7254                let rows = rows.into_values().collect::<Vec<_>>();
7255                // Every unit covering the table, as its (unit, table) pair (§4.4).
7256                let replica = self.stored(replica_cursor(&self.conn))?;
7257                let persisted = self
7258                    .stored(load_units(&self.conn))?
7259                    .into_iter()
7260                    .filter(|unit| covers(table, &unit.scopes))
7261                    .map(|unit| {
7262                        let effective = unit.echo.as_deref().and_then(|echo| {
7263                            pair_scopes(&unit.scopes, &read.table, Some(echo))
7264                        });
7265                        let requested = read
7266                            .scopes
7267                            .keys()
7268                            .map(|v| (v.clone(), json!(unit.scopes.get(v).into_iter().collect::<Vec<_>>())))
7269                            .collect::<Map<_, _>>();
7270                        let complete = effective
7271                            .as_ref()
7272                            .is_some_and(|scopes| scopes.len() == read.scopes.len())
7273                            && unit.status == UnitStatus::Active
7274                            && !unit.bootstrapping();
7275                        let exposed = effective.map(|scopes| {
7276                            read.scopes
7277                                .keys()
7278                                .map(|v| {
7279                                    let held = scopes.iter().find(|(variable, _)| variable == v);
7280                                    (v.clone(), json!(held.map_or(&Vec::new(), |(_, values)| values)))
7281                                })
7282                                .collect::<Map<_, _>>()
7283                        });
7284                        json!({"requestedScopes": requested, "status": unit.status, "cursor": unit.cursor.unwrap_or(replica), "effectiveScopes": exposed, "complete": complete})
7285                    })
7286                    .collect::<Vec<_>>();
7287                fn covered(selectors: &[(&String, &Vec<String>)], candidates: &[&Value]) -> bool {
7288                    match selectors.split_first() {
7289                        None => !candidates.is_empty(),
7290                        Some(((variable, values), rest)) => values.iter().all(|value| {
7291                            covered(
7292                                rest,
7293                                &candidates
7294                                    .iter()
7295                                    .copied()
7296                                    .filter(|p| {
7297                                        p["effectiveScopes"][variable.as_str()]
7298                                            .as_array()
7299                                            .is_some_and(|held| {
7300                                                held.contains(&Value::String(value.clone()))
7301                                                    || held.contains(&Value::String("*".into()))
7302                                            })
7303                                    })
7304                                    .collect::<Vec<_>>(),
7305                            )
7306                        }),
7307                    }
7308                }
7309                let coverage = if covered(
7310                    &read.scopes.iter().collect::<Vec<_>>(),
7311                    &persisted
7312                        .iter()
7313                        .filter(|p| p["complete"] == true)
7314                        .collect::<Vec<_>>(),
7315                ) {
7316                    "complete"
7317                } else if persisted.iter().any(|p| {
7318                    p["status"] == "active"
7319                        && read.scopes.iter().all(|(v, values)| {
7320                            values.iter().any(|value| {
7321                                p["requestedScopes"][v].as_array().is_some_and(|held| {
7322                                    held.contains(&Value::String(value.clone()))
7323                                })
7324                            })
7325                        })
7326                }) {
7327                    "pending"
7328                } else {
7329                    "missing"
7330                };
7331                tables.push(json!({"table": read.table, "rows": rows, "localIntentRowIds": intent, "scopes": read.scopes, "coverage": coverage, "persisted": persisted}));
7332            }
7333            Ok(
7334                json!({"revision": revision, "complete": tables.iter().all(|t| t["coverage"] == "complete"), "tables": tables}),
7335            )
7336        })();
7337        match result {
7338            Ok(snapshot) => {
7339                self.conn
7340                    .execute_batch("RELEASE authority_snapshot")
7341                    .map_err(|e| e.to_string())?;
7342                Ok(snapshot)
7343            }
7344            Err(error) => {
7345                self.conn
7346                    .execute_batch("ROLLBACK TO authority_snapshot; RELEASE authority_snapshot")
7347                    .map_err(|e| format!("{error}; rollback failed: {e}"))?;
7348                Err(error)
7349            }
7350        }
7351    }
7352
7353    /// Rows, coverage, and local revision from one SQLite read snapshot.
7354    /// §7.5 atomic snapshot read. An `owner` records a failure in
7355    /// `diagnostics_snapshot().query_failures` until its next successful read.
7356    pub fn query_snapshot(
7357        &mut self,
7358        sql: &str,
7359        params: &[QueryValue],
7360        coverage: &[WindowCoverage],
7361        owner: Option<QueryOwner<'_>>,
7362    ) -> Result<QuerySnapshot, String> {
7363        let result = snapshot_connection(&self.conn, sql, params, coverage);
7364        if let Some(owner) = owner {
7365            self.record_query_read(owner, result.as_ref().err());
7366        }
7367        result.map_err(|failure| failure.to_string())
7368    }
7369
7370    /// §7.6: record the outcome of one owned snapshot read, including reads a
7371    /// host ran on a separate connection. A success removes the owner's entry;
7372    /// an identical failure leaves it unchanged; a changed failure moves last.
7373    pub fn record_query_read(&mut self, owner: QueryOwner<'_>, failure: Option<&QueryReadFailure>) {
7374        let position = self
7375            .query_failures
7376            .iter()
7377            .position(|entry| entry.id == owner.id);
7378        let Some(failure) = failure else {
7379            if let Some(index) = position {
7380                self.query_failures.remove(index);
7381            }
7382            return;
7383        };
7384        let tables: Vec<String> = BTreeSet::from_iter(owner.tables.iter().copied())
7385            .into_iter()
7386            .map(str::to_owned)
7387            .collect();
7388        let code = failure.code.unwrap_or("client.query_failed").to_owned();
7389        if position.is_some_and(|index| {
7390            let entry = &self.query_failures[index];
7391            entry.tables == tables && entry.code == code && entry.sqlite_code == failure.sqlite_code
7392        }) {
7393            return;
7394        }
7395        if let Some(index) = position {
7396            self.query_failures.remove(index);
7397        }
7398        self.query_failures.push(DiagnosticQueryFailure {
7399            id: owner.id.to_owned(),
7400            tables,
7401            code,
7402            sqlite_code: failure.sqlite_code,
7403            at_ms: self.clock_now_ms(),
7404        });
7405        if self.query_failures.len() > MAX_DIAGNOSTIC_QUERY_FAILURES {
7406            self.query_failures.remove(0);
7407        }
7408    }
7409
7410    // -- request building ---------------------------------------------------------
7411
7412    fn build_request(&self, url_capable: bool) -> Result<(Message, RequestMeta), Box<SyncOutcome>> {
7413        #[cfg(feature = "bench-internals")]
7414        let _phase = self.benchmark_phases.start(Phase::RequestPrepare);
7415        let log_epoch = self.get_meta(LOG_EPOCH_KEY).map_err(|message| {
7416            Box::new(SyncOutcome::Failed {
7417                error_code: "storage.failed".into(),
7418                message,
7419                details: None,
7420                recommended_action: None,
7421            })
7422        })?;
7423        let max_commits = self.limits.max_push_commits_per_request;
7424        let max_ops = self
7425            .limits
7426            .max_push_operations_per_request
7427            .unwrap_or(PUSH_OPS_PER_REQUEST);
7428        let byte_cap = self.limits.max_push_request_bytes;
7429        // §4.2/§5.4: bit 3 is advertised iff the transport can fetch a
7430        // bare URL — capability negotiation, decided per transport.
7431        let accept = self.limits.accept.unwrap_or(if url_capable {
7432            DEFAULT_ACCEPT | ACCEPT_SIGNED_URLS
7433        } else {
7434            DEFAULT_ACCEPT
7435        });
7436        let storage_failed = |message: String| {
7437            Box::new(SyncOutcome::Failed {
7438                error_code: "storage.failed".into(),
7439                message,
7440                details: None,
7441                recommended_action: None,
7442            })
7443        };
7444        let header = Frame::ReqHeader {
7445            client_id: self.client_id.clone(),
7446            schema_version: self.schema.version,
7447            log_epoch: log_epoch.clone(),
7448            outbox_id: self.get_meta(OUTBOX_ID_KEY).map_err(storage_failed)?,
7449        };
7450        // §1.5: the lead frames are REQ_HEADER plus the optional ROUND_FAILURE,
7451        // which stays ahead of every push, pull, unit and progress frame; the
7452        // byte-capacity probes below measure this exact sequence.
7453        let mut lead = vec![header];
7454        lead.extend(
7455            self.round_failure
7456                .iter()
7457                .map(|code| Frame::RoundFailure { code: code.clone() }),
7458        );
7459        let registered: Vec<UnitRecord> = self
7460            .stored(load_units(&self.conn))
7461            .map_err(storage_failed)?
7462            .into_iter()
7463            .filter(|unit| unit.status == UnitStatus::Active)
7464            .collect();
7465        let registration_digest = ssp2::registration::registration_digest(
7466            registered
7467                .iter()
7468                .map(|unit| scope_pairs(&unit.scopes))
7469                .collect::<Vec<_>>()
7470                .iter()
7471                .map(Vec::as_slice),
7472        );
7473        let echo_digest = self
7474            .get_meta(ECHO_DIGEST_KEY)
7475            .map_err(storage_failed)?
7476            .filter(|hex| !hex.is_empty())
7477            .map(|hex| {
7478                crate::values::hex_to_bytes(&hex)
7479                    .ok()
7480                    .and_then(|bytes| <[u8; 32]>::try_from(bytes).ok())
7481                    .ok_or_else(|| {
7482                        "sync.local_corrupt: persisted echo digest is invalid".to_owned()
7483                    })
7484            })
7485            .transpose()
7486            .map_err(storage_failed)?;
7487        let pull = Frame::PullHeader {
7488            limit_commits: self.limits.limit_commits.unwrap_or(0),
7489            limit_snapshot_rows: self.limits.limit_snapshot_rows.unwrap_or(0),
7490            max_snapshot_pages: self.limits.max_snapshot_pages.unwrap_or(0),
7491            accept,
7492            cursor: self
7493                .stored(replica_cursor(&self.conn))
7494                .map_err(storage_failed)?,
7495            registration_digest,
7496            echo_digest,
7497        };
7498        let registration_digest = bytes_to_hex(&registration_digest);
7499        // §4.1: the list travels when it changed since the server stored it,
7500        // after `unregistered` and after a reset (both clear the stored digest).
7501        let travels = self
7502            .get_meta(REGISTERED_DIGEST_KEY)
7503            .map_err(storage_failed)?
7504            .as_deref()
7505            != Some(registration_digest.as_str());
7506        let mut sub_frames: Vec<Frame> = Vec::new();
7507        if travels {
7508            sub_frames.extend(registered.iter().map(|unit| Frame::Unit {
7509                scopes: scope_pairs(&unit.scopes),
7510            }));
7511        }
7512        // §4.3: one PROGRESS per catching-up unit, in registration order.
7513        sub_frames.extend(registered.iter().filter_map(|unit| {
7514            Some(Frame::Progress {
7515                id: unit.id.clone(),
7516                cursor: unit.cursor?,
7517                bootstrap_state: unit.bootstrap_state.clone().map(RawJson),
7518            })
7519        }));
7520        // The fixed frames (order-independent size) and the header+pull probe
7521        // both encode canonically, so the byte budget is the exact wire size.
7522        let fixed_bytes = match byte_cap {
7523            Some(_) => {
7524                let mut frames = lead.clone();
7525                frames.push(pull.clone());
7526                frames.extend(sub_frames.iter().cloned());
7527                encode_message(&Message {
7528                    wire_version: WIRE_VERSION,
7529                    msg_kind: MsgKind::Request,
7530                    frames,
7531                })
7532                .len()
7533            }
7534            None => 0,
7535        };
7536        if let Some(cap) = byte_cap {
7537            if fixed_bytes > cap {
7538                return Err(Self::push_capacity_outcome("bytes", cap, fixed_bytes, None));
7539            }
7540        }
7541        let base_bytes = match byte_cap {
7542            Some(_) => {
7543                let mut frames = lead.clone();
7544                frames.push(pull.clone());
7545                encode_message(&Message {
7546                    wire_version: WIRE_VERSION,
7547                    msg_kind: MsgKind::Request,
7548                    frames,
7549                })
7550                .len()
7551            }
7552            None => 0,
7553        };
7554        let total_outbox = if log_epoch.is_some() {
7555            self.outbox.len()
7556        } else {
7557            0
7558        };
7559        let mut push_frames: Vec<Frame> = Vec::new();
7560        let mut pushed_ids = Vec::new();
7561        let mut ops_in_request = 0usize;
7562        let mut bytes_in_request = 0usize;
7563        let mut deferred_commits = 0usize;
7564        #[cfg(feature = "bench-internals")]
7565        let outbox_phase = self.benchmark_phases.start(Phase::OutboxEncode);
7566        for (index, commit) in self.outbox.iter().take(total_outbox).enumerate() {
7567            // §6.1/§7.1: the commit-count and operation caps are hard. A
7568            // first commit over the operation cap is a typed capacity error
7569            // and stays queued; a later one defers with the complete suffix.
7570            if max_commits.is_some_and(|cap| push_frames.len() >= cap) {
7571                deferred_commits = total_outbox - index;
7572                break;
7573            }
7574            if ops_in_request + commit.ops.len() > max_ops {
7575                if push_frames.is_empty() {
7576                    return Err(Self::push_capacity_outcome(
7577                        "operations",
7578                        max_ops,
7579                        commit.ops.len(),
7580                        Some(commit.client_commit_id.as_str()),
7581                    ));
7582                }
7583                deferred_commits = total_outbox - index;
7584                break;
7585            }
7586            // §7.1 chained base: the commit waits for the change of the commit
7587            // it chains on, which supplies its rebased version.
7588            if self.outbox[..index]
7589                .iter()
7590                .flat_map(|earlier| &earlier.ops)
7591                .any(|earlier| commit.ops.iter().any(|op| chains_on(earlier, op)))
7592            {
7593                deferred_commits = total_outbox - index;
7594                break;
7595            }
7596            let operations = commit
7597                .ops
7598                .iter()
7599                .map(|op| {
7600                    let payload = match self
7601                        .schema
7602                        .table(&op.table)
7603                        .and_then(|table| Some((table, op.presented_values(table)?)))
7604                    {
7605                        Some((table, values)) => {
7606                            // §0: outbox entries encode at send time with the
7607                            // current codec (validated at mutate()). §6.1: the
7608                            // values map's keys are the sparse presence set; an
7609                            // insert-intent operation presents every column (§7.1).
7610                            // §5.11: encrypted columns are encrypted here, with
7611                            // an absent key-id selector read from the stored row.
7612                            // `sync_inner` runs `drop_unencodable_outbox` first,
7613                            // so an encrypt failure cannot reach this encode; the
7614                            // author seam validated every other failure.
7615                            let fallback =
7616                                self.stored_key_fallback(table, &op.row_id)
7617                                    .map_err(|message| {
7618                                        Box::new(SyncOutcome::Failed {
7619                                            error_code: "storage.failed".into(),
7620                                            message,
7621                                            details: None,
7622                                            recommended_action: None,
7623                                        })
7624                                    })?;
7625                            encode_sparse_row_json(
7626                                table,
7627                                &op.row_id,
7628                                &values,
7629                                &self.encryption,
7630                                fallback.as_ref(),
7631                            )
7632                            .ok()
7633                        }
7634                        None => None,
7635                    };
7636                    Ok(ssp2::model::Operation {
7637                        table: op.table.clone(),
7638                        row_id: op.row_id.clone(),
7639                        op: if op.upsert { Op::Upsert } else { Op::Delete },
7640                        base_version: op.base_version,
7641                        payload,
7642                    })
7643                })
7644                .collect::<Result<Vec<_>, Box<SyncOutcome>>>()?;
7645            // §6.3.2, §7.1: the commit's outbox sequence.
7646            let client_seq = self
7647                .conn
7648                .prepare_cached("SELECT seq FROM _syncular_outbox WHERE commit_id = ?1")
7649                .and_then(|mut statement| {
7650                    statement.query_row([&commit.client_commit_id], |row| row.get(0))
7651                })
7652                .map_err(|error| {
7653                    storage_failed(Self::sqlite_failure(&self.storage_failure, error))
7654                })?;
7655            let frame = Frame::PushCommit {
7656                client_commit_id: commit.client_commit_id.clone(),
7657                client_seq,
7658                operations,
7659            };
7660            let frame_bytes = match byte_cap {
7661                Some(_) => {
7662                    let mut frames = lead.clone();
7663                    frames.push(frame.clone());
7664                    frames.push(pull.clone());
7665                    encode_message(&Message {
7666                        wire_version: WIRE_VERSION,
7667                        msg_kind: MsgKind::Request,
7668                        frames,
7669                    })
7670                    .len()
7671                        - base_bytes
7672                }
7673                None => 0,
7674            };
7675            // The head is always encoded so the caller can report the full
7676            // projected request size; a later commit defers once it no longer
7677            // fits.
7678            if let Some(cap) = byte_cap {
7679                if !push_frames.is_empty() && fixed_bytes + bytes_in_request + frame_bytes > cap {
7680                    deferred_commits = total_outbox - index;
7681                    break;
7682                }
7683            }
7684            push_frames.push(frame);
7685            pushed_ids.push(commit.client_commit_id.clone());
7686            ops_in_request += commit.ops.len();
7687            bytes_in_request += frame_bytes;
7688        }
7689        #[cfg(feature = "bench-internals")]
7690        drop(outbox_phase);
7691        if let Some(cap) = byte_cap {
7692            if fixed_bytes + bytes_in_request > cap {
7693                // The head alone does not fit this budget; the commit stays
7694                // queued and no transport round is attempted.
7695                let blocked = if push_frames.is_empty() {
7696                    None
7697                } else {
7698                    self.outbox.first().map(|c| c.client_commit_id.as_str())
7699                };
7700                return Err(Self::push_capacity_outcome(
7701                    "bytes",
7702                    cap,
7703                    fixed_bytes + bytes_in_request,
7704                    blocked,
7705                ));
7706            }
7707        }
7708        let mut frames = lead;
7709        frames.extend(push_frames);
7710        frames.push(pull);
7711        frames.extend(sub_frames);
7712        let message = Message {
7713            wire_version: WIRE_VERSION,
7714            msg_kind: MsgKind::Request,
7715            frames,
7716        };
7717        Ok((
7718            message,
7719            RequestMeta {
7720                pushed_ids,
7721                registration_digest,
7722                accept,
7723                deferred_commits,
7724                applied: BTreeMap::new(),
7725                deferred: BTreeSet::new(),
7726            },
7727        ))
7728    }
7729
7730    /// §7.1: a configured push capacity limit cannot carry this request.
7731    fn push_capacity_outcome(
7732        kind: &str,
7733        limit: usize,
7734        size: usize,
7735        client_commit_id: Option<&str>,
7736    ) -> Box<SyncOutcome> {
7737        let mut details = Map::new();
7738        details.insert("kind".to_owned(), Value::from(kind));
7739        details.insert("limit".to_owned(), Value::from(limit as u64));
7740        details.insert("size".to_owned(), Value::from(size as u64));
7741        if let Some(id) = client_commit_id {
7742            details.insert("clientCommitId".to_owned(), Value::from(id));
7743        }
7744        Box::new(SyncOutcome::Failed {
7745            error_code: "client.push_request_too_large".into(),
7746            message: "the push request exceeds a configured capacity limit".into(),
7747            details: Some(Value::Object(details)),
7748            recommended_action: None,
7749        })
7750    }
7751
7752    // -- sync -------------------------------------------------------------------
7753
7754    /// Cloneable observation handle; no client lock is needed to read it.
7755    pub fn progress(&self) -> ProgressObserver {
7756        self.progress.clone()
7757    }
7758
7759    pub fn sync(&mut self, transport: &mut dyn Transport) -> SyncOutcome {
7760        self.sync_bounded(transport, None)
7761    }
7762
7763    /// One round; `idle_bound` is the idle drive's round bound (§7.8).
7764    fn sync_bounded(
7765        &mut self,
7766        transport: &mut dyn Transport,
7767        idle_bound: Option<(u64, u32)>,
7768    ) -> SyncOutcome {
7769        let mut prepared = match self.prepare_sync_round(transport.supports_url_fetch()) {
7770            Ok(prepared) => prepared,
7771            Err(outcome) => return *outcome,
7772        };
7773        prepared.idle_bound = idle_bound;
7774        // The round's network deadline rides `PreparedSyncRound` and is scoped
7775        // to each exchange, so uploads, continuations, the main request, and
7776        // every segment fetch share one monotonic anchor without leaving
7777        // deadline state on a reused transport.
7778        let mut next = prepared;
7779        loop {
7780            match self.apply_sync_round(next.exchange(transport)) {
7781                crate::AppliedSyncRound::Continue(prepared) => next = prepared,
7782                crate::AppliedSyncRound::Complete {
7783                    outcome, controls, ..
7784                } => {
7785                    if self.transport_enabled {
7786                        for text in controls {
7787                            let _ = transport.realtime_send(&text);
7788                        }
7789                    } else {
7790                        self.disconnect_realtime(transport);
7791                    }
7792                    return outcome;
7793                }
7794            }
7795        }
7796    }
7797
7798    fn finish_sync_round(&mut self, started_at_ms: i64, mut outcome: SyncOutcome) -> SyncOutcome {
7799        if let SyncOutcome::Failed {
7800            error_code,
7801            message,
7802            details,
7803            ..
7804        } = &mut outcome
7805        {
7806            if let Some((failure, code)) = self
7807                .storage_failure
7808                .borrow()
7809                .as_ref()
7810                .and_then(|failure| failure.code.map(|code| (failure, code)))
7811            {
7812                *error_code = code.to_owned();
7813                *message = match code {
7814                    "client.storage_busy" => "local SQLite storage is busy",
7815                    "client.storage_full" => "local SQLite storage is full",
7816                    "client.storage_io" => "local SQLite storage I/O failed",
7817                    "client.storage_corrupt" => "local SQLite storage is corrupt",
7818                    _ => unreachable!("unrecognized classified storage failure"),
7819                }
7820                .to_owned();
7821                *details = failure.details();
7822            }
7823        }
7824        let details = match &outcome {
7825            SyncOutcome::Failed { details, .. } => details.as_ref(),
7826            _ => None,
7827        };
7828        let cause_kind = details
7829            .and_then(|details| details.get("causeKind"))
7830            .and_then(Value::as_str)
7831            .and_then(|cause| match cause {
7832                "network" => Some(crate::TransportCauseKind::Network),
7833                "connect" => Some(crate::TransportCauseKind::Connect),
7834                "tls" => Some(crate::TransportCauseKind::Tls),
7835                "io" => Some(crate::TransportCauseKind::Io),
7836                "request" => Some(crate::TransportCauseKind::Request),
7837                "protocol" => Some(crate::TransportCauseKind::Protocol),
7838                "timeout" => Some(crate::TransportCauseKind::Timeout),
7839                "round_deadline" => Some(crate::TransportCauseKind::RoundDeadline),
7840                "aborted" => Some(crate::TransportCauseKind::Aborted),
7841                "body" => Some(crate::TransportCauseKind::Body),
7842                "status" => Some(crate::TransportCauseKind::Status),
7843                "redirect" => Some(crate::TransportCauseKind::Redirect),
7844                "unknown" => Some(crate::TransportCauseKind::Unknown),
7845                _ => None,
7846            });
7847        let http_status = if matches!(
7848            cause_kind,
7849            Some(
7850                crate::TransportCauseKind::Status
7851                    | crate::TransportCauseKind::Body
7852                    | crate::TransportCauseKind::Redirect
7853            )
7854        ) {
7855            details
7856                .and_then(|details| details.get("httpStatus"))
7857                .and_then(Value::as_f64)
7858                .filter(|status| status.fract() == 0.0 && (100.0..=599.0).contains(status))
7859                .map(|status| status as u16)
7860        } else {
7861            None
7862        };
7863        let retry_delay_ms = self.round_retry_delay_ms;
7864        self.progress.update(|p| match &outcome {
7865            SyncOutcome::Ok(report) | SyncOutcome::BudgetExhausted(report)
7866                if report.failed.is_empty() =>
7867            {
7868                p.state = ProgressState::Complete
7869            }
7870            SyncOutcome::Ok(_) | SyncOutcome::BudgetExhausted(_) => {
7871                p.state = ProgressState::Failed;
7872                p.error_code = Some("sync.scope_revoked".into());
7873            }
7874            SyncOutcome::Failed { error_code, .. } => {
7875                p.state = ProgressState::Failed;
7876                p.error_code = Some(Self::diagnostic_code(error_code));
7877                p.cause_kind = cause_kind;
7878                p.http_status = http_status;
7879                p.retry_delay_ms = retry_delay_ms;
7880            }
7881            SyncOutcome::RealtimeUnavailable { .. } => {
7882                p.state = ProgressState::Failed;
7883                p.error_code = Some(REALTIME_UNAVAILABLE_CODE.to_owned());
7884                p.retry_delay_ms = retry_delay_ms;
7885            }
7886        });
7887        let completed_at_ms = self.clock_now_ms();
7888        self.last_round = Some(match &outcome {
7889            SyncOutcome::Ok(report) | SyncOutcome::BudgetExhausted(report) => DiagnosticLastRound {
7890                status: "succeeded".to_owned(),
7891                started_at_ms,
7892                completed_at_ms,
7893                duration_ms: completed_at_ms.saturating_sub(started_at_ms).max(0),
7894                counters: Some(DiagnosticRoundCounters {
7895                    pushed: report.pushed,
7896                    applied: report.applied.len(),
7897                    rejected: report.rejected.len(),
7898                    retryable: report.retryable.len(),
7899                    conflicts: report.conflicts,
7900                    commits_applied: report.commits_applied,
7901                    segment_rows_applied: report.segment_rows_applied,
7902                    bootstrapping: report.bootstrapping.len(),
7903                    resets: report.resets.len(),
7904                    revoked: report.revoked.len(),
7905                    failed: report.failed.len(),
7906                    deferred_commits: report.deferred_commits,
7907                }),
7908                error_code: None,
7909                cause_kind: None,
7910                http_status: None,
7911            },
7912            SyncOutcome::Failed { error_code, .. } => DiagnosticLastRound {
7913                status: "failed".to_owned(),
7914                started_at_ms,
7915                completed_at_ms,
7916                duration_ms: completed_at_ms.saturating_sub(started_at_ms).max(0),
7917                counters: None,
7918                error_code: Some(Self::diagnostic_code(error_code)),
7919                cause_kind,
7920                http_status,
7921            },
7922            SyncOutcome::RealtimeUnavailable { .. } => DiagnosticLastRound {
7923                status: "failed".to_owned(),
7924                started_at_ms,
7925                completed_at_ms,
7926                duration_ms: completed_at_ms.saturating_sub(started_at_ms).max(0),
7927                counters: None,
7928                error_code: Some(REALTIME_UNAVAILABLE_CODE.to_owned()),
7929                cause_kind: None,
7930                http_status: None,
7931            },
7932        });
7933        outcome
7934    }
7935
7936    /// Capture one request on the client owner. Mutations made after this
7937    /// call remain in the outbox for a later round; no network I/O occurs here.
7938    pub fn prepare_sync_round(
7939        &mut self,
7940        url_capable: bool,
7941    ) -> Result<crate::PreparedSyncRound, Box<SyncOutcome>> {
7942        if !self.transport_enabled {
7943            return Err(Box::new(SyncOutcome::Failed {
7944                error_code: "sync.offline".into(),
7945                message: "transport gate is closed".into(),
7946                details: None,
7947                recommended_action: None,
7948            }));
7949        }
7950        if self.active_round.is_some() {
7951            return Err(Box::new(SyncOutcome::Failed {
7952                error_code: "client.round_in_progress".into(),
7953                message: "a sync round is already in flight".into(),
7954                details: None,
7955                recommended_action: None,
7956            }));
7957        }
7958        self.storage_failure.borrow_mut().take();
7959        self.progress.start();
7960        self.round_retry_delay_ms = None;
7961        let started_at_ms = self.clock_now_ms();
7962        let prepared = (|| {
7963            if self.stopped {
7964                return Err(Box::new(SyncOutcome::Ok(SyncReport {
7965                    schema_floor: self.schema_floor.clone(),
7966                    ..SyncReport::default()
7967                })));
7968            }
7969            if self.security_preflight {
7970                return Err(Box::new(SyncOutcome::Failed {
7971                    error_code: SECURITY_PREFLIGHT_REQUIRED_CODE.into(),
7972                    message: "security preflight must complete before syncing".into(),
7973                    details: None,
7974                    recommended_action: None,
7975                }));
7976            }
7977            self.set_sync_needed(false, false);
7978            self.drain_pending_evictions()
7979                .and_then(|_| self.drop_incompatible_outbox())
7980                .and_then(|_| self.drop_unencodable_outbox(true, None))
7981                .map_err(|message| {
7982                    Box::new(SyncOutcome::Failed {
7983                        error_code: "storage.failed".into(),
7984                        message,
7985                        details: None,
7986                        recommended_action: None,
7987                    })
7988                })?;
7989            // §5.11: reveal rows sealed under a key the keyring gained since.
7990            self.reveal_sealed_rows().map_err(|error| {
7991                Box::new(SyncOutcome::Failed {
7992                    error_code: error.code,
7993                    message: error.message,
7994                    details: error.details,
7995                    recommended_action: None,
7996                })
7997            })?;
7998            let has_log_epoch = self.get_meta(LOG_EPOCH_KEY).map_err(|message| {
7999                Box::new(SyncOutcome::Failed {
8000                    error_code: "storage.failed".into(),
8001                    message,
8002                    details: None,
8003                    recommended_action: None,
8004                })
8005            })?;
8006            let uploads = if has_log_epoch.is_some() && self.schema_has_blobs() {
8007                self.pending_blob_uploads().map_err(|e| {
8008                    Box::new(SyncOutcome::Failed {
8009                        error_code: e.code,
8010                        message: e.message,
8011                        details: e.details,
8012                        recommended_action: None,
8013                    })
8014                })?
8015            } else {
8016                Vec::new()
8017            };
8018            if self.realtime_state != RealtimeState::Connected
8019                && self.realtime_policy == RealtimePolicy::Required
8020            {
8021                let retry_delay_ms = self.schedule_background_retry();
8022                self.realtime_retry_delay_ms = Some(retry_delay_ms);
8023                return Err(Box::new(SyncOutcome::RealtimeUnavailable {
8024                    state: self.realtime_state,
8025                    reason_code: self.realtime_reason_code.clone(),
8026                    retry_delay_ms,
8027                }));
8028            }
8029            let (message, mut meta) = self.build_request(url_capable)?;
8030            let units = self.stored(load_units(&self.conn)).map_err(|message| {
8031                Box::new(SyncOutcome::Failed {
8032                    error_code: "storage.failed".into(),
8033                    message,
8034                    details: None,
8035                    recommended_action: None,
8036                })
8037            })?;
8038            for unit in &units {
8039                let applied =
8040                    self.stored(applied_rows(&self.conn, &unit.id))
8041                        .map_err(|message| {
8042                            Box::new(SyncOutcome::Failed {
8043                                error_code: "storage.failed".into(),
8044                                message,
8045                                details: None,
8046                                recommended_action: None,
8047                            })
8048                        })?;
8049                if !applied.is_empty() {
8050                    meta.applied.insert(unit.id.clone(), applied);
8051                }
8052            }
8053            let id = uuid::Uuid::new_v4();
8054            self.active_round = Some(id);
8055            let phase = if uploads.is_empty() {
8056                crate::round::RoundPhase::Sync
8057            } else {
8058                crate::round::RoundPhase::UploadGrant
8059            };
8060            Ok(crate::PreparedSyncRound {
8061                id,
8062                started_at_ms,
8063                message,
8064                meta,
8065                uploads: uploads.into(),
8066                phase,
8067                encoded: None,
8068                continuation: None,
8069                completed: None,
8070                realtime: self.realtime_state == RealtimeState::Connected,
8071                // §5.5: the owning unit's scope map, each value a one-element list.
8072                scopes: units
8073                    .into_iter()
8074                    .map(|unit| {
8075                        (
8076                            unit.id,
8077                            canonical_scope_json(&unit_scope_lists(&unit.scopes)),
8078                        )
8079                    })
8080                    .collect(),
8081                #[cfg(feature = "bench-internals")]
8082                benchmark_phases: self.benchmark_phases.clone(),
8083                progress: self.progress.clone(),
8084                fixed_now: self
8085                    .now_ms
8086                    .as_ref()
8087                    .map(|clock| clock.load(std::sync::atomic::Ordering::Relaxed)),
8088                round_deadline_at: None,
8089                idle_bound: None,
8090            })
8091        })();
8092        prepared.map_err(|outcome| Box::new(self.finish_sync_round(started_at_ms, *outcome)))
8093    }
8094
8095    /// Discard the reply of an old authorization or registration context. Its
8096    /// submitted commits stay durable and can be retried with the same ids.
8097    pub fn cancel_sync_round(&mut self) {
8098        self.active_round = None;
8099    }
8100
8101    /// Apply captured network results on the owner, preserving all normal
8102    /// version, schema, content-address and cursor checks. No network I/O.
8103    pub fn apply_sync_round(
8104        &mut self,
8105        completed: crate::CompletedSyncRound,
8106    ) -> crate::AppliedSyncRound {
8107        let mut prepared = completed.prepared;
8108        if self.active_round != Some(prepared.id) || self.security_preflight {
8109            return crate::AppliedSyncRound::Complete {
8110                outcome: SyncOutcome::Failed {
8111                    error_code: "client.round_cancelled".into(),
8112                    message: "sync round belongs to a released client context".into(),
8113                    details: None,
8114                    recommended_action: None,
8115                },
8116                controls: Vec::new(),
8117                more: false,
8118                bootstrap_advanced: false,
8119            };
8120        }
8121        self.storage_failure.borrow_mut().take();
8122        let (response, mut downloads, transport_failed) = match completed.exchange {
8123            crate::round::ExchangeResult::Upload { id, result } => {
8124                let result = match result {
8125                    // §5.9.7: a non-retryable server refusal (`blob.too_large`)
8126                    // is the body's final outcome. The commits naming it take
8127                    // the incompatible-commit path with the server's code and
8128                    // leave this round's request; the round continues.
8129                    Err(error)
8130                        if error
8131                            .details
8132                            .as_ref()
8133                            .and_then(|details| details.get("retryable"))
8134                            == Some(&Value::Bool(false)) =>
8135                    {
8136                        self.conn
8137                            .prepare(
8138                                "SELECT commit_id FROM _syncular_blob_commit_refs WHERE blob_id = ?",
8139                            )
8140                            .and_then(|mut statement| {
8141                                statement
8142                                    .query_map(rusqlite::params![id], |row| row.get::<_, String>(0))?
8143                                    .collect::<Result<HashSet<_>, _>>()
8144                            })
8145                            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
8146                            .and_then(|refused| {
8147                                let commits = self
8148                                    .outbox
8149                                    .iter()
8150                                    .filter(|commit| refused.contains(&commit.client_commit_id))
8151                                    .cloned()
8152                                    .collect::<Vec<_>>();
8153                                if !commits.is_empty() {
8154                                    self.drop_outbox_commits(&commits, &error.code, &error.message)?;
8155                                }
8156                                prepared.message.frames.retain(|frame| {
8157                                    !matches!(frame, Frame::PushCommit { client_commit_id, .. }
8158                                        if refused.contains(client_commit_id))
8159                                });
8160                                prepared.meta.pushed_ids.retain(|id| !refused.contains(id));
8161                                prepared.continuation = Some(crate::round::RoundPhase::UploadGrant);
8162                                Ok(())
8163                            })
8164                            .map_err(|message| TransportError::new("client.failed", message))
8165                    }
8166                    result => result,
8167                };
8168                let result = result.and_then(|()| {
8169                    self.conn
8170                        .execute(
8171                            "DELETE FROM _syncular_blob_uploads WHERE blob_id = ?",
8172                            rusqlite::params![id],
8173                        )
8174                        .map(|_| ())
8175                        .map_err(|error| {
8176                            TransportError::new(
8177                                "client.failed",
8178                                Self::sqlite_failure(&self.storage_failure, error),
8179                            )
8180                        })
8181                });
8182                match result {
8183                    Ok(()) => return crate::AppliedSyncRound::Continue(prepared),
8184                    Err(TransportError {
8185                        code,
8186                        message,
8187                        details,
8188                    }) => {
8189                        self.active_round = None;
8190                        if Self::unreachable_transport(&details) {
8191                            // The round's work is still owed; the host's
8192                            // connectivity resume runs it.
8193                            self.set_sync_needed(true, false);
8194                        } else if Self::retryable_failure_code(&code) {
8195                            self.schedule_background_retry();
8196                        }
8197                        let recommended_action = recommended_action_of(&details);
8198                        let outcome = self.finish_sync_round(
8199                            prepared.started_at_ms,
8200                            SyncOutcome::Failed {
8201                                error_code: code,
8202                                message,
8203                                details,
8204                                recommended_action,
8205                            },
8206                        );
8207                        return crate::AppliedSyncRound::Complete {
8208                            outcome,
8209                            controls: Vec::new(),
8210                            more: false,
8211                            bootstrap_advanced: false,
8212                        };
8213                    }
8214                }
8215            }
8216            crate::round::ExchangeResult::Reply {
8217                response,
8218                downloads,
8219                transport_failed,
8220            } => (response, downloads, transport_failed),
8221        };
8222        self.active_round = None;
8223        let before = self.resume_tokens();
8224        let outcome = (|| {
8225            let response = match response {
8226                Ok(response) => {
8227                    // §1.5: a complete, decodable response ends the report, an
8228                    // in-band ERROR included (the server already recorded it).
8229                    self.round_failure = None;
8230                    response
8231                }
8232                Err(TransportError {
8233                    code,
8234                    message,
8235                    details,
8236                }) => {
8237                    // §1.5: the server cannot see a round whose transport
8238                    // failed or whose response never decoded, so its code rides
8239                    // the following requests. A code the frame grammar rejects
8240                    // is not reportable, and the previous code stays; the
8241                    // client-local drive timeout never goes on the wire (§7.8).
8242                    if code != "sync.idle_timeout" && ssp2::model::valid_round_failure_code(&code) {
8243                        self.round_failure = Some(code.clone());
8244                    }
8245                    if transport_failed {
8246                        if prepared.realtime {
8247                            self.set_realtime_state(RealtimeState::Lost, Some(&code));
8248                        }
8249                        if let Err(message) = self.record_lease_error(&code) {
8250                            return SyncOutcome::Failed {
8251                                error_code: "storage.failed".to_owned(),
8252                                message,
8253                                details: None,
8254                                recommended_action: None,
8255                            };
8256                        }
8257                    }
8258                    if Self::retryable_failure_code(&code) && !Self::unreachable_transport(&details)
8259                    {
8260                        let delay = self.schedule_background_retry();
8261                        if prepared.realtime && transport_failed {
8262                            self.realtime_retry_delay_ms = Some(delay);
8263                        }
8264                    }
8265                    let recommended_action = recommended_action_of(&details);
8266                    return SyncOutcome::Failed {
8267                        error_code: code,
8268                        message,
8269                        details,
8270                        recommended_action,
8271                    };
8272                }
8273            };
8274            let mut outcome = self.process_response(&mut downloads, response, &prepared.meta);
8275            if let SyncOutcome::Ok(report) = &mut outcome {
8276                report.deferred_commits = prepared.meta.deferred_commits;
8277            }
8278            match &outcome {
8279                SyncOutcome::Ok(_) => self.reset_background_retry(),
8280                SyncOutcome::Failed {
8281                    error_code,
8282                    details,
8283                    ..
8284                } if Self::retryable_failure_code(error_code)
8285                    && !Self::unreachable_transport(details) =>
8286                {
8287                    self.schedule_background_retry();
8288                }
8289                _ => {}
8290            }
8291            if prepared.meta.deferred_commits > 0 {
8292                self.set_sync_needed(true, true);
8293            }
8294            self.previous_version_lifetime_check();
8295            outcome
8296        })();
8297        match &outcome {
8298            // §7.1: a unit still catching up needs another round, also when the
8299            // drive that started it ends early.
8300            SyncOutcome::Ok(report) if !report.bootstrapping.is_empty() => {
8301                self.set_sync_needed(true, false)
8302            }
8303            // The round's work is still owed; the host's connectivity resume
8304            // runs it.
8305            SyncOutcome::Failed { details, .. } if Self::unreachable_transport(details) => {
8306                self.set_sync_needed(true, false)
8307            }
8308            _ => {}
8309        }
8310        let more = self.transport_enabled
8311            && matches!(&outcome, SyncOutcome::Ok(report)
8312                if report.schema_floor.is_none()
8313                    && (!report.bootstrapping.is_empty()
8314                        || report.commits_applied > 0
8315                        || report.segment_rows_applied > 0
8316                        || !report.resets.is_empty()
8317                        || report.deferred_commits > 0
8318                        || self.sync_needed));
8319        let bootstrap_advanced = matches!(&outcome, SyncOutcome::Ok(report) if report.segment_rows_applied > 0 && !report.bootstrapping.is_empty())
8320            && before != self.resume_tokens();
8321        crate::AppliedSyncRound::Complete {
8322            outcome: self.finish_sync_round(prepared.started_at_ms, outcome),
8323            controls: if self.transport_enabled {
8324                downloads.controls
8325            } else {
8326                Vec::new()
8327            },
8328            more,
8329            bootstrap_advanced,
8330        }
8331    }
8332
8333    /// Every unit's resume token and applied-segment record: a bootstrap
8334    /// that advanced changes one (§4.7), also when it applied images of a
8335    /// section it deferred (§5.3, §5.6).
8336    #[allow(clippy::type_complexity)]
8337    fn resume_tokens(&self) -> Option<Vec<(String, Option<String>, Option<String>)>> {
8338        self.conn
8339            .prepare_cached(
8340                "SELECT id, bootstrap_state, applied FROM _syncular_units ORDER BY rowid",
8341            )
8342            .and_then(|mut stmt| {
8343                stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)))?
8344                    .collect()
8345            })
8346            .ok()
8347    }
8348
8349    /// §7.8: one idle drive's default total deadline, the TS client's value.
8350    pub const SYNC_IDLE_TIMEOUT_MS: u64 = 60_000;
8351
8352    pub fn sync_until_idle(
8353        &mut self,
8354        transport: &mut dyn Transport,
8355        max_rounds: Option<u32>,
8356    ) -> SyncOutcome {
8357        self.sync_until_idle_with_deadline(transport, max_rounds, Some(Self::SYNC_IDLE_TIMEOUT_MS))
8358    }
8359
8360    /// As [`SyncClient::sync_until_idle`], with an explicit total deadline in
8361    /// milliseconds (`None` disables it). The deadline is checked against the
8362    /// client clock between rounds: a drive past it after a completed round
8363    /// ends as `BudgetExhausted`. Each round is bounded by `timeout_ms` on its
8364    /// own, or the transport's tighter whole-round deadline (§1.8); a round
8365    /// that reaches the drive bound fails with `sync.idle_timeout`.
8366    pub fn sync_until_idle_with_deadline(
8367        &mut self,
8368        transport: &mut dyn Transport,
8369        max_rounds: Option<u32>,
8370        timeout_ms: Option<u64>,
8371    ) -> SyncOutcome {
8372        // §7.8: an explicit budget is a strict positive round count. Zero is a
8373        // caller error, not an exhaustion.
8374        if max_rounds == Some(0) {
8375            return SyncOutcome::Failed {
8376                details: None,
8377                error_code: "sync.invalid_request".into(),
8378                message: "maxRounds must be a positive integer".into(),
8379                recommended_action: None,
8380            };
8381        }
8382        if timeout_ms.is_some_and(|ms| ms == 0 || i64::try_from(ms).is_err()) {
8383            return SyncOutcome::Failed {
8384                details: None,
8385                error_code: "sync.invalid_request".into(),
8386                message: "timeoutMs must be a positive integer".into(),
8387                recommended_action: None,
8388            };
8389        }
8390        let started_at_ms = self.clock_now_ms();
8391        let rounds = max_rounds.unwrap_or(20);
8392        let mut aggregate = SyncReport::default();
8393        let mut spent = 0;
8394        let mut completed = false;
8395        while spent < rounds {
8396            // §7.8: a deadline that elapsed across completed rounds ends the
8397            // drive like an exhausted round budget; the next drive resumes.
8398            if completed
8399                && timeout_ms.is_some_and(|ms| {
8400                    self.clock_now_ms() >= started_at_ms.saturating_add(ms as i64)
8401                })
8402            {
8403                return SyncOutcome::BudgetExhausted(aggregate);
8404            }
8405            let idle_bound = timeout_ms.map(|ms| (ms, spent));
8406            spent += 1;
8407            let before = self.resume_tokens();
8408            match self.sync_bounded(transport, idle_bound) {
8409                SyncOutcome::Failed {
8410                    error_code,
8411                    message,
8412                    details,
8413                    recommended_action,
8414                } => {
8415                    return SyncOutcome::Failed {
8416                        details,
8417                        error_code,
8418                        message,
8419                        recommended_action,
8420                    };
8421                }
8422                outcome @ SyncOutcome::RealtimeUnavailable { .. } => return outcome,
8423                SyncOutcome::BudgetExhausted(_) => {
8424                    unreachable!("sync() never exhausts a round budget")
8425                }
8426                SyncOutcome::Ok(report) => {
8427                    completed = true;
8428                    if max_rounds.is_none()
8429                        && report.segment_rows_applied > 0
8430                        && !report.bootstrapping.is_empty()
8431                        && before != self.resume_tokens()
8432                    {
8433                        spent = 0;
8434                    }
8435                    // TS parity: a disabled transport or a schema floor ends
8436                    // the loop. The latest round still merges first.
8437                    let stop = !self.transport_enabled() || report.schema_floor.is_some();
8438                    aggregate.merge(&report);
8439                    if stop {
8440                        return SyncOutcome::Ok(aggregate);
8441                    }
8442                    // §4.5: pull again whenever the response contained
8443                    // commits or segments; resets re-bootstrap; a pending
8444                    // resume token continues paging (§4.7); deferred outbox
8445                    // commits and a raised sync-needed signal push on the next
8446                    // round.
8447                    let more = !report.bootstrapping.is_empty()
8448                        || report.commits_applied > 0
8449                        || report.segment_rows_applied > 0
8450                        || !report.resets.is_empty()
8451                        || report.deferred_commits > 0
8452                        || self.sync_needed;
8453                    if !more {
8454                        return SyncOutcome::Ok(aggregate);
8455                    }
8456                }
8457            }
8458        }
8459        // §7.8: partial success. The aggregate report is retained and the run
8460        // is explicitly not idle.
8461        SyncOutcome::BudgetExhausted(aggregate)
8462    }
8463
8464    fn process_response(
8465        &mut self,
8466        transport: &mut dyn Transport,
8467        response: Message,
8468        meta: &RequestMeta,
8469    ) -> SyncOutcome {
8470        #[cfg(feature = "bench-internals")]
8471        let _phase = self.benchmark_phases.start(Phase::ResponseApply);
8472        let mut report = SyncReport {
8473            pushed: meta.pushed_ids.len() as u32,
8474            ..SyncReport::default()
8475        };
8476        let mut rejection_details_by_commit: HashMap<String, BTreeMap<i32, RejectionDetails>> =
8477            HashMap::new();
8478        let pushed_ids = meta
8479            .pushed_ids
8480            .iter()
8481            .map(String::as_str)
8482            .collect::<HashSet<_>>();
8483        let mut last_final_push_result_id: Option<String> = None;
8484        for frame in &response.frames {
8485            match frame {
8486                Frame::PushResultDetails {
8487                    client_commit_id,
8488                    entries,
8489                } => {
8490                    let details = rejection_details_by_commit
8491                        .entry(client_commit_id.clone())
8492                        .or_default();
8493                    for entry in entries {
8494                        let parsed = match RejectionDetails::parse(&entry.details.0) {
8495                            Ok(value) => value,
8496                            Err(message) => {
8497                                return SyncOutcome::Failed {
8498                                    details: None,
8499                                    error_code: "sync.invalid_request".to_owned(),
8500                                    message,
8501                                    recommended_action: None,
8502                                };
8503                            }
8504                        };
8505                        details.insert(entry.op_index, parsed);
8506                    }
8507                }
8508                Frame::PushResult {
8509                    client_commit_id,
8510                    status,
8511                    results,
8512                    ..
8513                } if pushed_ids.contains(client_commit_id.as_str())
8514                    && Self::push_result_is_final(*status, results) =>
8515                {
8516                    last_final_push_result_id = Some(client_commit_id.clone());
8517                }
8518                _ => {}
8519            }
8520        }
8521        let mut frames = response.frames.into_iter().peekable();
8522        match frames.next() {
8523            Some(Frame::RespHeader {
8524                required_schema_version,
8525                latest_schema_version,
8526                log_epoch,
8527                reset_required,
8528            }) => {
8529                if let Some(required) = required_schema_version {
8530                    // §1.6 schema-floor response: nothing else is processed;
8531                    // stop syncing and surface the upgrade requirement.
8532                    let floor = SchemaFloor {
8533                        required_schema_version: Some(required),
8534                        latest_schema_version,
8535                    };
8536                    if let Err(message) = self.set_schema_floor(Some(floor.clone())) {
8537                        return SyncOutcome::Failed {
8538                            details: None,
8539                            error_code: "storage.failed".to_owned(),
8540                            message,
8541                            recommended_action: None,
8542                        };
8543                    }
8544                    report.schema_floor = Some(floor);
8545                    return SyncOutcome::Ok(report);
8546                }
8547                if reset_required {
8548                    if frames.next().is_some() {
8549                        return SyncOutcome::Failed {
8550                            details: None,
8551                            error_code: "client.invalid_host_response".to_owned(),
8552                            message: "log-epoch reset response contains body frames".to_owned(),
8553                            recommended_action: None,
8554                        };
8555                    }
8556                    match self.run_log_epoch_reset(&log_epoch) {
8557                        Ok(resets) => {
8558                            report.resets = resets;
8559                            return SyncOutcome::Ok(report);
8560                        }
8561                        Err(message) => {
8562                            return SyncOutcome::Failed {
8563                                details: None,
8564                                error_code: "sync.local_corrupt".to_owned(),
8565                                message,
8566                                recommended_action: None,
8567                            };
8568                        }
8569                    }
8570                }
8571                let stored_epoch = match self.get_meta(LOG_EPOCH_KEY) {
8572                    Ok(stored) => stored,
8573                    Err(message) => {
8574                        return SyncOutcome::Failed {
8575                            details: None,
8576                            error_code: "storage.failed".to_owned(),
8577                            message,
8578                            recommended_action: None,
8579                        };
8580                    }
8581                };
8582                if stored_epoch.as_deref() != Some(log_epoch.as_str()) {
8583                    return SyncOutcome::Failed {
8584                        details: None,
8585                        error_code: "client.invalid_host_response".to_owned(),
8586                        message: "server changed logEpoch without requiring a reset".to_owned(),
8587                        recommended_action: None,
8588                    };
8589                }
8590            }
8591            _ => {
8592                return SyncOutcome::Failed {
8593                    details: None,
8594                    error_code: "sync.invalid_request".to_owned(),
8595                    message: "response does not start with RESP_HEADER".to_owned(),
8596                    recommended_action: None,
8597                };
8598            }
8599        }
8600
8601        if let Err(message) = self.begin_coalesced_apply() {
8602            return SyncOutcome::Failed {
8603                details: None,
8604                error_code: "storage.failed".into(),
8605                message,
8606                recommended_action: None,
8607            };
8608        }
8609        let mut failure: Option<(String, String, Option<String>, Option<Value>)> = None;
8610        // §4.5 client application: the units whose persisted echo a live
8611        // change must match, fixed at LIVE_START after every SUB_END persisted.
8612        let mut live: Option<(LiveStatus, String, Vec<UnitRecord>)> = None;
8613        let mut live_ended = false;
8614        while let Some(frame) = frames.next() {
8615            match frame {
8616                frame @ Frame::PushResult { .. } => {
8617                    let successful = matches!(
8618                        &frame,
8619                        Frame::PushResult {
8620                            status: PushStatus::Applied | PushStatus::Cached,
8621                            ..
8622                        }
8623                    );
8624                    let mut results = vec![frame];
8625                    if successful {
8626                        while matches!(
8627                            frames.peek(),
8628                            Some(Frame::PushResult {
8629                                status: PushStatus::Applied | PushStatus::Cached,
8630                                ..
8631                            })
8632                        ) {
8633                            if let Some(next) = frames.next() {
8634                                results.push(next);
8635                            }
8636                        }
8637                    }
8638                    if self
8639                        .handle_push_results(
8640                            &results,
8641                            &pushed_ids,
8642                            &rejection_details_by_commit,
8643                            &mut report,
8644                            last_final_push_result_id.as_deref(),
8645                        )
8646                        .is_err()
8647                    {
8648                        failure = Some((
8649                            "client.outcome_persistence_failed".to_owned(),
8650                            "local commit outcome could not be persisted".to_owned(),
8651                            None,
8652                            None,
8653                        ));
8654                        break;
8655                    }
8656                }
8657                Frame::PushResultDetails { .. } => {}
8658                Frame::SubStart {
8659                    id,
8660                    status,
8661                    reason_code,
8662                    bootstrap,
8663                    fenced,
8664                } => {
8665                    // §1.4 rule 5: an ERROR closes the section; its body still
8666                    // applied, its SUB_END values are never persisted.
8667                    let mut body = Vec::new();
8668                    let mut sub_end: Option<(i64, Option<String>)> = None;
8669                    let mut error: Option<Frame> = None;
8670                    for inner in frames.by_ref() {
8671                        match inner {
8672                            Frame::SubEnd {
8673                                next_cursor,
8674                                bootstrap_state,
8675                            } => {
8676                                sub_end = Some((next_cursor, bootstrap_state.map(|r| r.0)));
8677                                break;
8678                            }
8679                            inner @ Frame::Error { .. } => {
8680                                error = Some(inner);
8681                                break;
8682                            }
8683                            Frame::Unknown { .. } => {}
8684                            other => body.push(other),
8685                        }
8686                    }
8687                    let ended = sub_end.is_some();
8688                    let section = SectionStart {
8689                        id,
8690                        status,
8691                        reason_code,
8692                        bootstrap,
8693                        fenced,
8694                    };
8695                    match self.process_section(
8696                        transport,
8697                        &section,
8698                        body,
8699                        sub_end,
8700                        meta,
8701                        &mut report,
8702                    ) {
8703                        Err(SectionError::Abort(code, message)) => {
8704                            failure = Some((code, message, None, None));
8705                            break;
8706                        }
8707                        Err(SectionError::Transport(error)) => {
8708                            failure = Some((error.code, error.message, None, error.details));
8709                            break;
8710                        }
8711                        _ => {}
8712                    }
8713                    if ended {
8714                        // The counters stay: `complete` and `failed` report them.
8715                        self.progress.update(|p| {
8716                            p.unit_id = None;
8717                            p.table = None;
8718                            p.segment_id = None;
8719                        });
8720                    }
8721                    match error {
8722                        Some(Frame::Error {
8723                            code,
8724                            message,
8725                            recommended_action,
8726                            details,
8727                            ..
8728                        }) => {
8729                            failure = Some(Self::error_frame_failure(
8730                                code,
8731                                message,
8732                                recommended_action,
8733                                details,
8734                            ));
8735                            break;
8736                        }
8737                        Some(_) => unreachable!("only an ERROR closes a section early"),
8738                        None if !ended => {
8739                            failure = Some((
8740                                "sync.invalid_request".to_owned(),
8741                                "unit section without SUB_END".to_owned(),
8742                                None,
8743                                None,
8744                            ));
8745                            break;
8746                        }
8747                        None => {}
8748                    }
8749                }
8750                Frame::LiveStart {
8751                    status,
8752                    reason_code,
8753                } => {
8754                    let receivers = match self.stored(load_units(&self.conn)) {
8755                        Ok(units) => units
8756                            .into_iter()
8757                            .filter(|unit| {
8758                                unit.status == UnitStatus::Active
8759                                    && unit.echo.is_some()
8760                                    && !unit.bootstrapping()
8761                            })
8762                            .collect(),
8763                        Err(message) => {
8764                            failure = Some(("storage.failed".to_owned(), message, None, None));
8765                            break;
8766                        }
8767                    };
8768                    live = Some((status, reason_code, receivers));
8769                }
8770                Frame::Commit {
8771                    commit_seq,
8772                    tables,
8773                    changes,
8774                    ..
8775                } => {
8776                    let Some((LiveStatus::Active, _, receivers)) = &live else {
8777                        continue;
8778                    };
8779                    let receivers = receivers.clone();
8780                    match self.apply_live_commit(
8781                        &receivers,
8782                        &tables,
8783                        changes,
8784                        commit_seq,
8785                        AppliedServerCommitSource::Pull,
8786                    ) {
8787                        Ok(applied) => report.commits_applied += u32::from(applied),
8788                        Err(error) => {
8789                            failure = Some((error.code, error.message, None, error.details));
8790                            break;
8791                        }
8792                    }
8793                }
8794                Frame::LiveEnd {
8795                    through_seq,
8796                    echo_digest,
8797                } => {
8798                    let Some((status, reason_code, _)) = live.take() else {
8799                        continue;
8800                    };
8801                    live_ended = true;
8802                    live = Some((status, String::new(), Vec::new()));
8803                    if let Err(message) = self.finish_live_section(
8804                        status,
8805                        &reason_code,
8806                        through_seq,
8807                        echo_digest,
8808                        &meta.registration_digest,
8809                        &mut report,
8810                    ) {
8811                        failure = Some(("storage.failed".to_owned(), message, None, None));
8812                        break;
8813                    }
8814                }
8815                Frame::Lease {
8816                    lease_id,
8817                    expires_at_ms,
8818                } => {
8819                    // §7.3.5: persist the opaque lease; a fresh lease clears
8820                    // any prior lease error (the outage/revocation is over).
8821                    if let Err(message) = self.set_lease_state(Some(LeaseState {
8822                        lease_id: Some(lease_id),
8823                        expires_at_ms: Some(expires_at_ms),
8824                        error_code: None,
8825                    })) {
8826                        failure = Some(("storage.failed".to_owned(), message, None, None));
8827                        break;
8828                    }
8829                }
8830                Frame::Error {
8831                    code,
8832                    message,
8833                    recommended_action,
8834                    details,
8835                    ..
8836                } => {
8837                    // §1.6: the whole request failed; already-completed
8838                    // units keep their applied data and cursors.
8839                    failure = Some(Self::error_frame_failure(
8840                        code,
8841                        message,
8842                        recommended_action,
8843                        details,
8844                    ));
8845                    break;
8846                }
8847                Frame::Unknown { .. } => {}
8848                _ => {
8849                    failure = Some((
8850                        "sync.invalid_request".to_owned(),
8851                        "unexpected frame in response".to_owned(),
8852                        None,
8853                        None,
8854                    ));
8855                    break;
8856                }
8857            }
8858        }
8859
8860        let applied = match failure {
8861            Some(failure) => Err(failure),
8862            // Local apply boundaries already reconciled their affected overlay.
8863            // Refuse round success if any remaining dirty work fails to
8864            // reconcile. §4.8 E1: the push half may have drained commits that
8865            // pinned rows of a shrunk window unit, so retry deferred evictions.
8866            None => self
8867                .rebuild_overlay_if_dirty()
8868                .and_then(|()| self.drain_pending_evictions())
8869                .map_err(|message| ("storage.failed".to_owned(), message, None, None)),
8870        };
8871        // §1.4 rule 4: frames completed before a failure stay committed; a
8872        // failed COMMIT of the coalesced transaction fails the round.
8873        if let Err(message) = self.finish_coalesced_apply() {
8874            return SyncOutcome::Failed {
8875                details: None,
8876                error_code: "storage.failed".into(),
8877                message,
8878                recommended_action: None,
8879            };
8880        }
8881        if let Err((error_code, message, recommended_action, details)) = applied {
8882            return SyncOutcome::Failed {
8883                details,
8884                error_code,
8885                message,
8886                recommended_action,
8887            };
8888        }
8889        let catching_up = match self.stored(load_units(&self.conn)) {
8890            Ok(units) => units
8891                .into_iter()
8892                .filter(|unit| unit.status == UnitStatus::Active && unit.cursor.is_some())
8893                .map(|unit| unit.id)
8894                .collect::<Vec<_>>(),
8895            Err(message) => {
8896                return SyncOutcome::Failed {
8897                    details: None,
8898                    error_code: "storage.failed".into(),
8899                    message,
8900                    recommended_action: None,
8901                }
8902            }
8903        };
8904        // §7.1: a response without LIVE_END, one answered `unregistered` or
8905        // `reset`, or a unit still catching up leaves sync-needed raised;
8906        // after `unregistered` the next round starts at once.
8907        let status = live.map(|(status, _, _)| status);
8908        if !live_ended || status != Some(LiveStatus::Active) || !catching_up.is_empty() {
8909            self.set_sync_needed(true, status == Some(LiveStatus::Unregistered));
8910        }
8911        self.ack_after_pull(transport);
8912        // §7.4.5: the reset is over once the first post-reset pull round
8913        // leaves no unit catching up: the tables are rebuilt and current.
8914        if self.upgrading && catching_up.is_empty() {
8915            self.set_upgrading(false);
8916        }
8917        report.bootstrapping = catching_up;
8918        SyncOutcome::Ok(report)
8919    }
8920
8921    /// §1.6 / §10.1: the failure an in-band `ERROR` frame reports.
8922    fn error_frame_failure(
8923        code: String,
8924        message: String,
8925        recommended_action: String,
8926        details: Option<RawJson>,
8927    ) -> (String, String, Option<String>, Option<Value>) {
8928        let recommended_action = (!recommended_action.is_empty()).then_some(recommended_action);
8929        let details = details
8930            .and_then(|details| serde_json::from_str::<Value>(&details.0).ok())
8931            .filter(Value::is_object);
8932        (code, message, recommended_action, details)
8933    }
8934
8935    /// §4.5 at `LIVE_END` of a pull: join rule J for an `active` section, the
8936    /// §4.6 replica reset for `reset`, nothing for `unregistered`; then the
8937    /// replica cursor, the echo digest and the digest of the list the server
8938    /// now stores.
8939    fn finish_live_section(
8940        &mut self,
8941        status: LiveStatus,
8942        reason_code: &str,
8943        through_seq: i64,
8944        echo_digest: Option<[u8; 32]>,
8945        registration_digest: &str,
8946        report: &mut SyncReport,
8947    ) -> Result<(), String> {
8948        let rollback = self.capture();
8949        let resets = report.resets.len();
8950        self.begin_observation("syncular_live_end")?;
8951        let finished = (|| {
8952            let mut batch = ChangeAccumulator::default();
8953            let before = self.stored(replica_cursor(&self.conn))?;
8954            let mut cursor = if status == LiveStatus::Unregistered {
8955                before
8956            } else {
8957                -1
8958            };
8959            let joined = before.max(through_seq);
8960            for unit in self.stored(load_units(&self.conn))? {
8961                if unit.status != UnitStatus::Active || unit.bootstrapping() {
8962                    continue;
8963                }
8964                match status {
8965                    // §4.6 replica reset: every live unit bootstraps again;
8966                    // post-bootstrap units keep their cursors.
8967                    LiveStatus::Reset if unit.cursor.is_none() => {
8968                        for (base_key, value) in self.stored(window_refs(&self.conn, &unit.id))? {
8969                            batch.window(&base_key, &value);
8970                        }
8971                        report.resets.push(unit.id.clone());
8972                        self.stored(save_unit(
8973                            &self.conn,
8974                            &UnitRecord {
8975                                reason_code: Some(reason_code.to_owned()),
8976                                cursor: Some(-1),
8977                                seeded: Vec::new(),
8978                                ..unit
8979                            },
8980                        ))?;
8981                    }
8982                    // §4.5 join rule J.
8983                    LiveStatus::Active => {
8984                        let next = unit.cursor.unwrap_or(joined).max(through_seq);
8985                        if next >= joined {
8986                            cursor = joined;
8987                        }
8988                        if unit.cursor.is_some() {
8989                            self.stored(save_unit(
8990                                &self.conn,
8991                                &UnitRecord {
8992                                    cursor: (next < joined).then_some(next),
8993                                    ..unit
8994                                },
8995                            ))?;
8996                        }
8997                    }
8998                    _ => {}
8999                }
9000            }
9001            self.set_meta(REPLICA_CURSOR_KEY, &cursor.to_string())?;
9002            self.set_meta(
9003                ECHO_DIGEST_KEY,
9004                &echo_digest
9005                    .map(|digest| bytes_to_hex(&digest))
9006                    .unwrap_or_default(),
9007            )?;
9008            if status == LiveStatus::Unregistered {
9009                self.delete_meta(REGISTERED_DIGEST_KEY)?;
9010            } else {
9011                self.set_meta(REGISTERED_DIGEST_KEY, registration_digest)?;
9012            }
9013            self.finish_observation("syncular_live_end", batch)
9014        })();
9015        if finished.is_err() {
9016            self.rollback_observation("syncular_live_end");
9017            self.restore(rollback);
9018            report.resets.truncate(resets);
9019        }
9020        finished
9021    }
9022
9023    // -- push results (§6.3, §7.2) ------------------------------------------------
9024
9025    fn handle_push_results(
9026        &mut self,
9027        frames: &[Frame],
9028        pushed_ids: &HashSet<&str>,
9029        rejection_details_by_commit: &HashMap<String, BTreeMap<i32, RejectionDetails>>,
9030        report: &mut SyncReport,
9031        last_final_push_result_id: Option<&str>,
9032    ) -> Result<(), String> {
9033        self.begin_observation("syncular_push_results")?;
9034        let rollback = self.capture();
9035        let conflict_count = self.conflicts.len();
9036        let applied_count = report.applied.len();
9037        let rejected_count = report.rejected.len();
9038        let retryable_count = report.retryable.len();
9039        let mut drained = Vec::new();
9040        let mut batch = ChangeAccumulator::default();
9041        let persisted = (|| {
9042            for frame in frames {
9043                let Frame::PushResult {
9044                    client_commit_id,
9045                    status,
9046                    results,
9047                    commit_seq,
9048                } = frame
9049                else {
9050                    return Err("unexpected acknowledgement frame".to_owned());
9051                };
9052                if !pushed_ids.contains(client_commit_id.as_str()) {
9053                    continue;
9054                }
9055                let Some(index) = self
9056                    .outbox
9057                    .iter()
9058                    .position(|commit| commit.client_commit_id == *client_commit_id)
9059                else {
9060                    continue;
9061                };
9062                let status = *status;
9063                let rejection_details = rejection_details_by_commit.get(client_commit_id);
9064                let operations = self.outbox[index].ops.clone();
9065                match status {
9066                    PushStatus::Applied | PushStatus::Cached => {
9067                        // §7.2: a lost ack replays as `cached` — proceed as if the
9068                        // ack had arrived.
9069                        let sequence =
9070                            commit_seq.ok_or_else(|| "ACK lacks a commit sequence".to_owned())?;
9071                        let affected: HashSet<_> = operations
9072                            .iter()
9073                            .map(|op| (op.table.as_str(), op.row_id.as_str()))
9074                            .collect();
9075                        let prior = self.acknowledged_operations(Some(&affected), None)?;
9076                        for (op_index, operation) in operations.iter().enumerate() {
9077                            let delivered: bool = self.conn.query_row("SELECT EXISTS(SELECT 1 FROM _syncular_row_deliveries WHERE tbl=?1 AND id=?2 AND commit_seq>=?3)", rusqlite::params![operation.table, operation.row_id, sequence], |row| row.get(0))
9078                                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
9079                            if delivered {
9080                                continue;
9081                            }
9082                            let table = self
9083                                .schema
9084                                .table(&operation.table)
9085                                .ok_or_else(|| "sync.unknown_table".to_owned())?;
9086                            let mut intended: Map<String, Value> = self
9087                                .stored_row(table, &operation.row_id, true)?
9088                                .map(|(row, _)| {
9089                                    table
9090                                        .columns
9091                                        .iter()
9092                                        .zip(&row)
9093                                        .map(|(column, value)| {
9094                                            (column.name.clone(), column_value_to_json(value))
9095                                        })
9096                                        .collect()
9097                                })
9098                                .unwrap_or_default();
9099                            let mut scope_image = intended.clone();
9100                            for op in prior
9101                                .iter()
9102                                .chain(
9103                                    self.failed_commits
9104                                        .iter()
9105                                        .flat_map(|(commit, _)| &commit.ops),
9106                                )
9107                                .chain(
9108                                    self.outbox
9109                                        .iter()
9110                                        .take(index)
9111                                        .flat_map(|commit| &commit.ops),
9112                                )
9113                                .chain(operations.iter().take(op_index + 1))
9114                            {
9115                                if op.table != operation.table || op.row_id != operation.row_id {
9116                                    continue;
9117                                }
9118                                if let Some(values) = &op.values {
9119                                    intended.extend(values.clone());
9120                                } else {
9121                                    scope_image = intended.clone();
9122                                    intended.clear();
9123                                }
9124                            }
9125                            self.conn.execute("INSERT INTO _syncular_acked_rows(commit_id,idx,tbl,id,commit_seq,op_json,intent_json) VALUES (?1,?2,?3,?4,?5,?6,?7)",
9126                                rusqlite::params![client_commit_id, op_index as i64, operation.table, operation.row_id, sequence,
9127                                    serde_json::to_string(&CommitOperation::from(operation)).map_err(|_| "sync.local_corrupt".to_owned())?,
9128                                    serde_json::to_string(if operation.upsert { &intended } else { &scope_image }).map_err(|_| "sync.local_corrupt".to_owned())?])
9129                                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
9130                        }
9131                        let journal_results = results
9132                            .iter()
9133                            .map(|result| {
9134                                let op_index = match result {
9135                                    OpResult::Applied { op_index }
9136                                    | OpResult::Conflict { op_index, .. }
9137                                    | OpResult::Error { op_index, .. } => *op_index,
9138                                };
9139                                CommitOperationOutcome::Applied { op_index }
9140                            })
9141                            .collect::<Vec<_>>();
9142                        let outcome_status = if status == PushStatus::Applied {
9143                            CommitOutcomeStatus::Applied
9144                        } else {
9145                            CommitOutcomeStatus::Cached
9146                        };
9147                        let persisted = self
9148                            .persist_commit_outcome(
9149                                client_commit_id,
9150                                outcome_status,
9151                                &journal_results,
9152                                None,
9153                            )
9154                            .and_then(|()| self.delete_outbox_persisted(client_commit_id));
9155                        persisted?;
9156                        self.sync_needed = true;
9157                        batch.status = true;
9158                        batch.outcomes = true;
9159                    }
9160                    PushStatus::Rejected => {
9161                        if results.iter().any(|result| {
9162                            matches!(
9163                                result,
9164                                OpResult::Error {
9165                                    code,
9166                                    retryable: true,
9167                                    ..
9168                                } if code == "sync.idempotency_cache_miss"
9169                            )
9170                        }) {
9171                            // §6.3/§7.2: a serving failure, not an outcome — keep the
9172                            // exact commit queued for an identical retry.
9173                            report.retryable.push(client_commit_id.to_owned());
9174                            continue;
9175                        }
9176
9177                        let mut journal_results = Vec::with_capacity(results.len());
9178                        let mut conflicts = Vec::new();
9179                        let mut rejections = Vec::new();
9180                        for result in results {
9181                            match result {
9182                                OpResult::Applied { op_index } => {
9183                                    journal_results.push(CommitOperationOutcome::Applied {
9184                                        op_index: *op_index,
9185                                    });
9186                                }
9187                                OpResult::Conflict {
9188                                    op_index,
9189                                    code,
9190                                    message,
9191                                    server_version,
9192                                    server_row,
9193                                    conflict_columns: conflict_columns_bitmap,
9194                                } => {
9195                                    let operation = operations
9196                                        .get(*op_index as usize)
9197                                        .map(CommitOperation::from);
9198                                    let (table, row_id) = operation
9199                                        .as_ref()
9200                                        .map(|op| (op.table.clone(), op.row_id.clone()))
9201                                        .unwrap_or_default();
9202                                    let server_row_json = self
9203                                        .schema
9204                                        .table(&table)
9205                                        .and_then(|t| {
9206                                            decode_row_bytes(t, server_row, &self.encryption)
9207                                                .ok()
9208                                                .map(|(row, _)| (t, row))
9209                                        })
9210                                        .map(|(t, row)| {
9211                                            let mut map = Map::new();
9212                                            for (i, column) in t.columns.iter().enumerate() {
9213                                                map.insert(
9214                                                    column.name.clone(),
9215                                                    column_value_to_json(
9216                                                        row.get(i).unwrap_or(&None),
9217                                                    ),
9218                                                );
9219                                            }
9220                                            map
9221                                        })
9222                                        .unwrap_or_default();
9223                                    // §6.3: decode the presence-layout bitmap
9224                                    // into the marked column names.
9225                                    let conflict_columns = self
9226                                        .schema
9227                                        .table(&table)
9228                                        .map(|t| {
9229                                            t.columns
9230                                                .iter()
9231                                                .enumerate()
9232                                                .filter(|(index, _)| {
9233                                                    conflict_columns_bitmap
9234                                                        .get(index / 8)
9235                                                        .is_some_and(|byte| {
9236                                                            byte & (1 << (index % 8)) != 0
9237                                                        })
9238                                                })
9239                                                .map(|(_, column)| column.name.clone())
9240                                                .collect::<Vec<_>>()
9241                                        })
9242                                        .unwrap_or_default();
9243                                    let conflict = ConflictRecord {
9244                                        client_commit_id: client_commit_id.to_owned(),
9245                                        op_index: *op_index,
9246                                        table,
9247                                        row_id,
9248                                        code: code.clone(),
9249                                        message: message.clone(),
9250                                        server_version: *server_version,
9251                                        server_row: server_row_json,
9252                                        conflict_columns,
9253                                        operation,
9254                                    };
9255                                    journal_results.push(CommitOperationOutcome::Conflict {
9256                                        conflict: conflict.clone(),
9257                                    });
9258                                    conflicts.push(conflict);
9259                                }
9260                                OpResult::Error {
9261                                    op_index,
9262                                    code,
9263                                    message,
9264                                    retryable,
9265                                } => {
9266                                    let rejection = RejectionRecord {
9267                                        client_commit_id: client_commit_id.to_owned(),
9268                                        op_index: *op_index,
9269                                        code: code.clone(),
9270                                        message: message.clone(),
9271                                        retryable: *retryable,
9272                                        details: rejection_details
9273                                            .and_then(|details| details.get(op_index))
9274                                            .cloned(),
9275                                        operation: operations
9276                                            .get(*op_index as usize)
9277                                            .map(CommitOperation::from),
9278                                    };
9279                                    journal_results.push(CommitOperationOutcome::Error {
9280                                        rejection: rejection.clone(),
9281                                    });
9282                                    rejections.push(rejection);
9283                                }
9284                            }
9285                        }
9286                        if self.retain_failed_commits {
9287                            self.retain_failed_commit(client_commit_id, &operations)?;
9288                        }
9289                        let outcome_status = if conflicts.is_empty() {
9290                            CommitOutcomeStatus::Rejected
9291                        } else {
9292                            CommitOutcomeStatus::Conflict
9293                        };
9294                        let persisted = self
9295                            .persist_commit_outcome(
9296                                client_commit_id,
9297                                outcome_status,
9298                                &journal_results,
9299                                Some(&operations),
9300                            )
9301                            .and_then(|()| self.delete_outbox_persisted(client_commit_id));
9302                        persisted?;
9303                        batch.conflicts = !conflicts.is_empty();
9304                        batch.rejections = !rejections.is_empty();
9305                        batch.status = true;
9306                        batch.outcomes = true;
9307                        self.conflicts.extend(conflicts);
9308                        self.rejections.extend(rejections);
9309                    }
9310                }
9311                drained.push(self.outbox.remove(index));
9312                if batch.status {
9313                    for operation in &operations {
9314                        if !self.record_row_scopes(
9315                            &mut batch,
9316                            &operation.table,
9317                            &operation.row_id,
9318                            false,
9319                        ) {
9320                            batch.table(&operation.table);
9321                        }
9322                    }
9323                }
9324                match status {
9325                    PushStatus::Applied | PushStatus::Cached => {
9326                        report.applied.push(client_commit_id.to_owned())
9327                    }
9328                    PushStatus::Rejected => report.rejected.push(client_commit_id.to_owned()),
9329                }
9330            }
9331            if batch.outcomes && frames.iter().any(|frame| matches!(frame, Frame::PushResult { client_commit_id, .. } if Some(client_commit_id.as_str()) == last_final_push_result_id)) {
9332                self.prune_commit_outcomes()?;
9333            }
9334            self.reconcile_rows(&drained)?;
9335            self.rebuild_overlay_if_dirty()?;
9336            self.finish_observation("syncular_push_results", batch)
9337        })();
9338        if let Err(error) = persisted {
9339            self.rollback_observation("syncular_push_results");
9340            self.restore(rollback);
9341            report.applied.truncate(applied_count);
9342            report.rejected.truncate(rejected_count);
9343            report.retryable.truncate(retryable_count);
9344            return Err(error);
9345        }
9346        if !rollback.sync_needed && self.sync_needed {
9347            self.sync_intent_queue.push_back(SyncIntent::Interactive);
9348        }
9349        report.conflicts += (self.conflicts.len() - conflict_count) as u32;
9350        Ok(())
9351    }
9352
9353    fn push_result_is_final(status: PushStatus, results: &[OpResult]) -> bool {
9354        status != PushStatus::Rejected
9355            || !results.iter().any(|result| {
9356                matches!(
9357                    result,
9358                    OpResult::Error {
9359                        code,
9360                        retryable: true,
9361                        ..
9362                    } if code == "sync.idempotency_cache_miss"
9363                )
9364            })
9365    }
9366
9367    // -- unit sections and the live section ------------------------------------------
9368
9369    /// One unit section (§4.4): apply its segments, then persist its
9370    /// `SUB_END` (§4.6 reset, §3.3 revocation and purge, the echo, cursor and
9371    /// resume token). A section of a unit the app released while the round was
9372    /// in flight is skipped; a fail-closed first page fails the unit and skips
9373    /// the rest of its section (§5.6).
9374    fn process_section(
9375        &mut self,
9376        transport: &mut dyn Transport,
9377        start: &SectionStart,
9378        body: Vec<Frame>,
9379        sub_end: Option<(i64, Option<String>)>,
9380        meta: &RequestMeta,
9381        report: &mut SyncReport,
9382    ) -> Result<(), SectionError> {
9383        let storage = |message| SectionError::Abort("storage.failed".to_owned(), message);
9384        let unit = self
9385            .stored(get_unit(&self.conn, &start.id))
9386            .map_err(storage)?
9387            .filter(|unit| unit.status == UnitStatus::Active);
9388        // §7.7: a section boundary ends the previous payload.
9389        self.progress.update(|p| {
9390            p.phase = ProgressPhase::Import;
9391            p.unit_id = unit.as_ref().map(|unit| unit.id.clone());
9392            p.table = None;
9393            p.segment_id = None;
9394            p.bytes_received = 0;
9395            p.bytes_total = None;
9396            p.rows_processed = 0;
9397            p.rows_total = None;
9398            p.import_write_ms = None;
9399            p.import_commit_ms = None;
9400        });
9401        let Some(unit) = unit else {
9402            return Ok(());
9403        };
9404        let echo: Vec<EchoRecord> = start
9405            .fenced
9406            .iter()
9407            .map(|record| EchoRecord {
9408                table: record.table.clone(),
9409                status: record.status.name().to_owned(),
9410                scopes: record.scopes.iter().cloned().collect(),
9411            })
9412            .collect();
9413        // §5.6: rows earlier attempts at this position applied.
9414        let applied_record = self
9415            .stored(applied_rows(&self.conn, &unit.id))
9416            .map_err(storage)?;
9417        // §5.6: a resumed bootstrap keeps the tables it already seeded.
9418        let mut seeded: BTreeSet<String> = if unit.bootstrap_state.is_none() {
9419            BTreeSet::new()
9420        } else {
9421            unit.seeded.iter().cloned().collect()
9422        };
9423        let applied = (|| {
9424            for frame in body {
9425                match frame {
9426                    Frame::SegmentInline { payload } => {
9427                        let segment = decode_rows_segment(&payload).map_err(|e| {
9428                            SectionError::Abort(e.code.as_str().to_owned(), e.detail)
9429                        })?;
9430                        self.progress.update(|p| {
9431                            p.phase = ProgressPhase::Import;
9432                            p.unit_id = Some(unit.id.clone());
9433                            p.table = Some(segment.table.clone());
9434                            p.segment_id = None;
9435                            p.bytes_received = payload.len() as u64;
9436                            p.bytes_total = Some(payload.len() as u64);
9437                            p.rows_processed = 0;
9438                            p.rows_total =
9439                                Some(segment.blocks.iter().map(|block| block.len() as u64).sum());
9440                            p.import_write_ms = None;
9441                            p.import_commit_ms = None;
9442                        });
9443                        let (pair, clear, effective) =
9444                            self.segment_pair(&unit, &echo, &seeded, &segment.table)?;
9445                        report.segment_rows_applied +=
9446                            self.apply_segment(&pair, &segment, clear, &effective, &unit.id)?;
9447                        seeded.insert(segment.table);
9448                    }
9449                    Frame::SegmentRef {
9450                        segment_id,
9451                        byte_length,
9452                        media_type,
9453                        table,
9454                        row_count,
9455                        as_of_commit_seq,
9456                        scope_digest,
9457                        row_cursor,
9458                        next_row_cursor,
9459                        url,
9460                        url_expires_at_ms,
9461                        ..
9462                    } => {
9463                        // §4.2: reject a descriptor whose mediaType was not
9464                        // advertised — never skip or guess.
9465                        let advertised = match media_type {
9466                            MediaType::Rows => {
9467                                meta.accept & ACCEPT_EXTERNAL_ROWS != 0
9468                                    || meta.accept & ACCEPT_INLINE_ROWS != 0
9469                            }
9470                            MediaType::Sqlite => meta.accept & ACCEPT_SQLITE != 0,
9471                        };
9472                        if !advertised {
9473                            return Err(SectionError::Abort(
9474                                "sync.invalid_request".to_owned(),
9475                                format!(
9476                                    "SEGMENT_REF mediaType {} was not advertised in accept (§4.2)",
9477                                    media_type.name()
9478                                ),
9479                            ));
9480                        }
9481                        // §5.6 applied-segment rule: a partly applied image
9482                        // already ran its table's first-page clear.
9483                        let resumed = applied_record.get(&segment_id).copied();
9484                        if resumed.is_some() {
9485                            seeded.insert(table.clone());
9486                        }
9487                        if resumed == Some(row_count) {
9488                            continue;
9489                        }
9490                        if meta
9491                            .deferred
9492                            .contains(&(unit.id.clone(), segment_id.clone()))
9493                        {
9494                            return Err(SectionError::Deferred);
9495                        }
9496                        self.progress.update(|p| {
9497                            p.phase = ProgressPhase::Download;
9498                            p.unit_id = Some(unit.id.clone());
9499                            p.table = Some(table.clone());
9500                            p.segment_id = Some(segment_id.clone());
9501                            p.bytes_received = 0;
9502                            p.bytes_total = u64::try_from(byte_length).ok();
9503                            p.rows_processed = 0;
9504                            p.rows_total = u64::try_from(row_count).ok();
9505                            p.import_write_ms = None;
9506                            p.import_commit_ms = None;
9507                        });
9508                        let observer = self.progress.clone();
9509                        let mut on_progress = |bytes| observer.update(|p| p.bytes_received = bytes);
9510                        let bytes = if let Some(url) = url {
9511                            // §5.4: a url-carrying descriptor MUST be fetched
9512                            // from that URL; failure invalidates the whole
9513                            // descriptor (no fall-through to §5.5 — re-pull
9514                            // recovers, §1.4 rule 5).
9515                            if meta.accept & ACCEPT_SIGNED_URLS == 0 {
9516                                return Err(SectionError::Abort(
9517                                    "sync.invalid_request".to_owned(),
9518                                    "SEGMENT_REF carries a url but accept bit 3 was not advertised (§5.4)"
9519                                        .to_owned(),
9520                                ));
9521                            }
9522                            // §5.4: MUST NOT start a fetch at/past expiry.
9523                            if url_expires_at_ms.is_some_and(|exp| {
9524                                exp <= transport
9525                                    .segment_fetch_started_at(&url)
9526                                    .unwrap_or_else(|| self.clock_now_ms())
9527                            }) {
9528                                return Err(SectionError::Abort(
9529                                    "sync.segment_expired".to_owned(),
9530                                    format!(
9531                                        "signed URL for segment {segment_id} expired before fetch — re-pull mints fresh descriptors (§5.4)"
9532                                    ),
9533                                ));
9534                            }
9535                            transport
9536                                .fetch_url(
9537                                    &url,
9538                                    u64::try_from(byte_length).unwrap_or(0),
9539                                    &mut on_progress,
9540                                )
9541                                .map_err(|e| self.segment_fetch_failed(e))?
9542                        } else {
9543                            // §5.5: the owning unit's scope map, each value a one-element list.
9544                            let requested_scopes_json =
9545                                canonical_scope_json(&unit_scope_lists(&unit.scopes));
9546                            transport
9547                                .download_segment(
9548                                    &SegmentRequest {
9549                                        segment_id: segment_id.clone(),
9550                                        table: table.clone(),
9551                                        requested_scopes_json,
9552                                        byte_length: u64::try_from(byte_length).unwrap_or(0),
9553                                    },
9554                                    &mut on_progress,
9555                                )
9556                                .map_err(|e| {
9557                                    // §5.5: access changed since the response;
9558                                    // the next round's SUB_START settles the unit.
9559                                    if e.code == "sync.forbidden" {
9560                                        SectionError::Refused
9561                                    } else {
9562                                        self.segment_fetch_failed(e)
9563                                    }
9564                                })?
9565                        };
9566                        // §5.4: the descriptor's byteLength caps the body before
9567                        // §5.1 verifies the content address; the client discards
9568                        // the segment and re-pulls on either failure.
9569                        if bytes.len() as i64 > byte_length {
9570                            self.schedule_background_retry();
9571                            return Err(SectionError::Abort(
9572                                "sync.invalid_request".to_owned(),
9573                                "segment exceeds its byteLength".to_owned(),
9574                            ));
9575                        }
9576                        on_progress(bytes.len() as u64);
9577                        let digest = Sha256::digest(&bytes);
9578                        let expected = segment_id
9579                            .strip_prefix("sha256:")
9580                            .unwrap_or(segment_id.as_str());
9581                        if bytes_to_hex(&digest) != expected {
9582                            // §5.1: the client discards the segment and re-pulls.
9583                            self.schedule_background_retry();
9584                            return Err(SectionError::Abort(
9585                                "sync.invalid_request".to_owned(),
9586                                "segment bytes do not match the content address (§5.1)".to_owned(),
9587                            ));
9588                        }
9589                        self.progress.update(|p| p.phase = ProgressPhase::Import);
9590                        if media_type == MediaType::Sqlite {
9591                            // §5.3: a range image keeps its position in the resume
9592                            // token; a descriptor row cursor is invalid.
9593                            if row_cursor.is_some() || next_row_cursor.is_some() {
9594                                return Err(SectionError::Abort(
9595                                    "sync.invalid_request".to_owned(),
9596                                    "sqlite segments are whole-table: rowCursor/nextRowCursor must be absent (§5.3)"
9597                                        .to_owned(),
9598                                ));
9599                            }
9600                            let (pair, clear, effective) =
9601                                self.segment_pair(&unit, &echo, &seeded, &table)?;
9602                            report.segment_rows_applied += self.apply_sqlite_segment(
9603                                &pair,
9604                                &bytes,
9605                                clear,
9606                                &effective,
9607                                &unit.id,
9608                                &segment_id,
9609                                resumed.unwrap_or(0),
9610                                row_count,
9611                                as_of_commit_seq,
9612                                &scope_digest,
9613                            )?;
9614                            seeded.insert(table);
9615                        } else {
9616                            let segment = decode_rows_segment(&bytes).map_err(|e| {
9617                                SectionError::Abort(e.code.as_str().to_owned(), e.detail)
9618                            })?;
9619                            let (pair, clear, effective) =
9620                                self.segment_pair(&unit, &echo, &seeded, &segment.table)?;
9621                            report.segment_rows_applied +=
9622                                self.apply_segment(&pair, &segment, clear, &effective, &unit.id)?;
9623                            seeded.insert(segment.table);
9624                            // An image records its rows with each chunk.
9625                            self.stored(record_applied_rows(
9626                                &self.conn,
9627                                &unit.id,
9628                                &segment_id,
9629                                row_count,
9630                            ))
9631                            .map_err(storage)?;
9632                        }
9633                    }
9634                    _ => {
9635                        return Err(SectionError::Abort(
9636                            "sync.invalid_request".to_owned(),
9637                            "unexpected frame inside a unit section".to_owned(),
9638                        ));
9639                    }
9640                }
9641            }
9642            Ok(())
9643        })();
9644        match applied {
9645            Ok(()) => {}
9646            // The unit keeps catching up with its cursor unpersisted.
9647            Err(SectionError::Refused | SectionError::Deferred) => return Ok(()),
9648            Err(SectionError::FailClosed) => {
9649                // §5.6: the failure is unit-local; the rest of the response
9650                // still applies and the failed unit leaves the list.
9651                self.begin_observation("syncular_section_failure")
9652                    .map_err(storage)?;
9653                let failed = (|| {
9654                    let mut batch = ChangeAccumulator::default();
9655                    for (base_key, value) in self.stored(window_refs(&self.conn, &unit.id))? {
9656                        batch.window(&base_key, &value);
9657                    }
9658                    batch.status = true;
9659                    self.stored(save_unit(
9660                        &self.conn,
9661                        &UnitRecord {
9662                            status: UnitStatus::Failed,
9663                            reason_code: Some("sync.scope_revoked".to_owned()),
9664                            ..unit.clone()
9665                        },
9666                    ))?;
9667                    self.finish_observation("syncular_section_failure", batch)
9668                })();
9669                if let Err(message) = failed {
9670                    self.rollback_observation("syncular_section_failure");
9671                    return Err(storage(message));
9672                }
9673                report.failed.push(unit.id);
9674                return Ok(());
9675            }
9676            Err(error) => return Err(error),
9677        }
9678        let Some((next_cursor, bootstrap_state)) = sub_end else {
9679            return Ok(());
9680        };
9681        let rollback = self.capture();
9682        let counts = (
9683            report.resets.len(),
9684            report.revoked.len(),
9685            report.failed.len(),
9686        );
9687        self.begin_observation("syncular_sub_end")
9688            .map_err(storage)?;
9689        let finished = self
9690            .finish_section(start, echo, next_cursor, bootstrap_state, seeded, report)
9691            .and_then(|batch| {
9692                self.rebuild_overlay_if_dirty()?;
9693                self.finish_observation("syncular_sub_end", batch)
9694            });
9695        if let Err(message) = finished {
9696            self.rollback_observation("syncular_sub_end");
9697            self.restore(rollback);
9698            report.resets.truncate(counts.0);
9699            report.revoked.truncate(counts.1);
9700            report.failed.truncate(counts.2);
9701            return Err(storage(message));
9702        }
9703        Ok(())
9704    }
9705
9706    /// §5.6: the table, first-page flag and effective scopes of one segment
9707    /// of `unit`'s section.
9708    #[allow(clippy::type_complexity)]
9709    fn segment_pair(
9710        &self,
9711        unit: &UnitRecord,
9712        echo: &[EchoRecord],
9713        seeded: &BTreeSet<String>,
9714        name: &str,
9715    ) -> Result<(TableSchema, bool, Vec<(String, Vec<String>)>), SectionError> {
9716        let table = self.schema.table(name).cloned().ok_or_else(|| {
9717            SectionError::Abort(
9718                "sync.schema_mismatch".to_owned(),
9719                format!("segment table {name:?} is not in the client schema"),
9720            )
9721        })?;
9722        let effective = covers(&table, &unit.scopes)
9723            .then(|| pair_scopes(&unit.scopes, name, Some(echo)))
9724            .flatten()
9725            .ok_or_else(|| {
9726                SectionError::Abort(
9727                    "sync.invalid_request".to_owned(),
9728                    "a segment names no active pair of its unit".to_owned(),
9729                )
9730            })?;
9731        Ok((table, !seeded.contains(name), effective))
9732    }
9733
9734    /// §4.4 `SUB_END` of an active, revoked or reset section, inside the
9735    /// caller's observation: a reset discards cursor, resume token and
9736    /// first-page record and keeps the echo; a revoked pair purges by its last
9737    /// echo (§3.3); otherwise the echo replaces the persisted one, a revoked
9738    /// fenced record that turns active again re-bootstraps the unit, and a
9739    /// bootstrap section persists its pin, resume token and seeded tables.
9740    fn finish_section(
9741        &mut self,
9742        start: &SectionStart,
9743        echo: Vec<EchoRecord>,
9744        next_cursor: i64,
9745        bootstrap_state: Option<String>,
9746        seeded: BTreeSet<String>,
9747        report: &mut SyncReport,
9748    ) -> Result<ChangeAccumulator, String> {
9749        #[cfg(feature = "bench-internals")]
9750        let _phase = self.benchmark_phases.start(Phase::CursorPersist);
9751        let mut batch = ChangeAccumulator::default();
9752        // §4.4: SUB_END writes only into a unit that is still registered.
9753        let Some(unit) = self
9754            .stored(get_unit(&self.conn, &start.id))?
9755            .filter(|unit| unit.status == UnitStatus::Active)
9756        else {
9757            return Ok(batch);
9758        };
9759        for (base_key, value) in self.stored(window_refs(&self.conn, &unit.id))? {
9760            batch.window(&base_key, &value);
9761        }
9762        if start.status == SubStatus::Reset {
9763            // §4.6: staleness, never a purge; the rows stay until the
9764            // bootstrap replaces them.
9765            report.resets.push(unit.id.clone());
9766            self.stored(save_unit(
9767                &self.conn,
9768                &UnitRecord {
9769                    reason_code: Some(start.reason_code.clone()),
9770                    cursor: Some(-1),
9771                    bootstrap_state: None,
9772                    seeded: Vec::new(),
9773                    ..unit
9774                },
9775            ))?;
9776            return Ok(batch);
9777        }
9778        let revoked = start.status == SubStatus::Revoked;
9779        let mut units = None;
9780        let mut failed = false;
9781        let mut rearmed = false;
9782        for table in self.schema.tables.clone() {
9783            if !covers(&table, &unit.scopes) {
9784                continue;
9785            }
9786            let last = unit
9787                .echo
9788                .as_deref()
9789                .and_then(|last| pair_scopes(&unit.scopes, &table.name, Some(last)));
9790            let next = (!revoked)
9791                .then(|| pair_scopes(&unit.scopes, &table.name, Some(&echo)))
9792                .flatten();
9793            // §3.3: a revoked fenced record that turns active again re-bootstraps.
9794            rearmed |= unit.echo.is_some() && last.is_none() && next.is_some();
9795            // §3.3: purge a pair the response revokes, keyed on the echo the
9796            // client persisted while the pair was active.
9797            if let (Some(last), None) = (&last, &next) {
9798                let units = match units {
9799                    Some(ref units) => units,
9800                    None => units.insert(self.stored(load_units(&self.conn))?),
9801                };
9802                let owners = unit_owners(units, &table, &unit.id);
9803                failed |= !self.purge_pair(&mut batch, &table, last, &owners)?;
9804            }
9805        }
9806        if revoked || failed {
9807            if revoked {
9808                report.revoked.push(unit.id.clone());
9809            }
9810            if failed {
9811                report.failed.push(unit.id.clone());
9812            }
9813            batch.status = true;
9814            self.stored(save_unit(
9815                &self.conn,
9816                &UnitRecord {
9817                    status: if failed {
9818                        UnitStatus::Failed
9819                    } else {
9820                        UnitStatus::Revoked
9821                    },
9822                    reason_code: Some(if failed {
9823                        "sync.scope_revoked".to_owned()
9824                    } else {
9825                        start.reason_code.clone()
9826                    }),
9827                    cursor: Some(-1),
9828                    bootstrap_state: None,
9829                    seeded: Vec::new(),
9830                    echo: None,
9831                    ..unit
9832                },
9833            ))?;
9834            return Ok(batch);
9835        }
9836        let completed = start.bootstrap && !rearmed && bootstrap_state.is_none();
9837        let (cursor, bootstrap_state, seeded) = if rearmed {
9838            (Some(-1), None, Vec::new())
9839        } else if start.bootstrap {
9840            let seeded = if bootstrap_state.is_none() {
9841                Vec::new()
9842            } else {
9843                seeded.into_iter().collect()
9844            };
9845            (Some(next_cursor), bootstrap_state, seeded)
9846        } else {
9847            (
9848                unit.cursor,
9849                unit.bootstrap_state.clone(),
9850                unit.seeded.clone(),
9851            )
9852        };
9853        self.stored(save_unit(
9854            &self.conn,
9855            &UnitRecord {
9856                reason_code: None,
9857                echo: Some(echo.clone()),
9858                cursor,
9859                bootstrap_state,
9860                seeded,
9861                ..unit.clone()
9862            },
9863        ))?;
9864        if !completed {
9865            return Ok(batch);
9866        }
9867        // §7.1: a completed bootstrap covers acknowledged intent of its pairs
9868        // at or below its pin.
9869        for table in self.schema.tables.clone() {
9870            let Some(effective) = covers(&table, &unit.scopes)
9871                .then(|| pair_scopes(&unit.scopes, &table.name, Some(&echo)))
9872                .flatten()
9873            else {
9874                continue;
9875            };
9876            let mut statement = self
9877                .conn
9878                .prepare("SELECT id,intent_json FROM _syncular_acked_rows WHERE tbl=?1 AND commit_seq<=?2")
9879                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
9880            let rows = statement
9881                .query_map(rusqlite::params![table.name, next_cursor], |row| {
9882                    Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
9883                })
9884                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
9885            let mut covered = HashSet::new();
9886            for row in rows {
9887                let (id, intent) =
9888                    row.map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
9889                let values: Map<String, Value> =
9890                    serde_json::from_str(&intent).map_err(|_| "sync.local_corrupt".to_owned())?;
9891                if owns_local_row(&table, &effective, &[], &values) {
9892                    covered.insert(id);
9893                }
9894            }
9895            drop(statement);
9896            for id in covered {
9897                self.conn
9898                    .execute(
9899                        "DELETE FROM _syncular_acked_rows WHERE tbl=?1 AND id=?2 AND commit_seq<=?3",
9900                        rusqlite::params![table.name, id, next_cursor],
9901                    )
9902                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
9903                self.overlay_dirty.table(&table.name);
9904                batch.table(&table.name);
9905            }
9906        }
9907        Ok(batch)
9908    }
9909
9910    /// §3.3: purge one revoked pair inside the caller's observation: delete
9911    /// the rows matching its last effective scopes that no owner holds, drop
9912    /// the retained intent and every pending commit that provably writes into
9913    /// them, and delete now-unauthorized blob bodies. `Ok(false)` when the
9914    /// table has no local scope-column mapping (fail closed: nothing deleted).
9915    fn purge_pair(
9916        &mut self,
9917        batch: &mut ChangeAccumulator,
9918        table: &TableSchema,
9919        last: &[(String, Vec<String>)],
9920        owners: &[Vec<(String, Vec<String>)>],
9921    ) -> Result<bool, String> {
9922        if local_scope_predicate(table, last, owners).is_err() {
9923            return Ok(false);
9924        }
9925        for commit in self.drop_failed_commits(|name, values| {
9926            name == table.name && owns_local_row(table, last, owners, values)
9927        })? {
9928            batch.conflicts = true;
9929            batch.rejections = true;
9930            batch.outcomes = true;
9931            for operation in commit.ops {
9932                batch.table(&operation.table);
9933            }
9934        }
9935        if !self.purge_scope_rows(&table.name, last, owners)? {
9936            return Err("storage.failed: the revoked scope purge failed".to_owned());
9937        }
9938        self.record_scope_map(batch, &table.name, last);
9939        if self.drop_doomed_outbox(table, last, owners)? {
9940            batch.status = true;
9941            batch.rejections = true;
9942            batch.outcomes = true;
9943        }
9944        // §5.9.7 B2: revocation deletes now-unauthorized blob bodies.
9945        self.delete_unreferenced_cached_blobs()?;
9946        Ok(true)
9947    }
9948
9949    /// §4.5 client application: apply one live-section or delta `COMMIT`
9950    /// with only the changes that match the persisted echo of a receiving
9951    /// unit; every applied change then goes through the version guard.
9952    /// Returns whether any change of the frame applied.
9953    fn apply_live_commit(
9954        &mut self,
9955        receivers: &[UnitRecord],
9956        tables: &[String],
9957        changes: Vec<ssp2::model::Change>,
9958        commit_seq: i64,
9959        source: AppliedServerCommitSource,
9960    ) -> Result<bool, TransportError> {
9961        let changes: Vec<ssp2::model::Change> = changes
9962            .into_iter()
9963            .filter(|change| {
9964                // An unknown table fails loud in the apply below.
9965                let Some(table) = tables
9966                    .get(change.table_index as usize)
9967                    .and_then(|name| self.schema.table(name))
9968                else {
9969                    return true;
9970                };
9971                receivers.iter().any(|unit| {
9972                    covers(table, &unit.scopes)
9973                        && pair_scopes(&unit.scopes, &table.name, unit.echo.as_deref()).is_some_and(
9974                            |scopes| {
9975                                scopes.iter().all(|(variable, values)| {
9976                                    change
9977                                        .scopes
9978                                        .iter()
9979                                        .find(|(key, _)| key == variable)
9980                                        .is_some_and(|(_, value)| values.contains(value))
9981                                })
9982                            },
9983                        )
9984                })
9985            })
9986            .collect();
9987        if changes.is_empty() {
9988            return Ok(false);
9989        }
9990        let affected_tables = changes
9991            .iter()
9992            .filter_map(|change| tables.get(change.table_index as usize))
9993            .cloned()
9994            .collect::<BTreeSet<_>>();
9995        let previous_batches = self.change_queue.len();
9996        self.apply_commit_frame(tables, &changes, commit_seq)?;
9997        if self.change_queue.len() > previous_batches {
9998            if let Some(batch) = self.change_queue.back_mut() {
9999                batch
10000                    .applied_commits
10001                    .extend(
10002                        affected_tables
10003                            .into_iter()
10004                            .map(|table| AppliedServerCommit {
10005                                table,
10006                                commit_seq,
10007                                source,
10008                            }),
10009                    );
10010            }
10011        }
10012        Ok(true)
10013    }
10014
10015    // Both pull and realtime frames use the same durable observation boundary.
10016    fn apply_commit_frame(
10017        &mut self,
10018        tables: &[String],
10019        changes: &[ssp2::model::Change],
10020        sequence: i64,
10021    ) -> Result<(), TransportError> {
10022        #[cfg(feature = "bench-internals")]
10023        let _phase = self.benchmark_phases.start(Phase::CommitApply);
10024        let rollback = self.capture();
10025        self.coalesce_unit(changes.len())
10026            .and_then(|()| self.begin_observation("syncular_commit"))
10027            .map_err(|message| TransportError::new("storage.failed", message))?;
10028        let applied = (|| {
10029            let mut batch = ChangeAccumulator::default();
10030            self.record_commit_changes(&mut batch, tables, changes);
10031            // §7.1 chained base: a change names the version an acknowledged own
10032            // commit wrote; pending upserts chained on it move to that version.
10033            for change in changes {
10034                let Some(table) = tables.get(change.table_index as usize) else {
10035                    continue;
10036                };
10037                let acknowledged = self
10038                    .conn
10039                    .prepare_cached("SELECT op_json FROM _syncular_acked_rows WHERE tbl=?1 AND id=?2 AND commit_seq=?3")
10040                    .and_then(|mut statement| {
10041                        statement
10042                            .query_map(rusqlite::params![table, change.row_id, sequence], |row| row.get::<_, String>(0))?
10043                            .collect::<Result<Vec<_>, _>>()
10044                    })
10045                    .map_err(|error| TransportError::new("storage.failed", Self::sqlite_failure(&self.storage_failure, error)))?
10046                    .iter()
10047                    .map(|op| serde_json::from_str::<CommitOperation>(op).map(OutboxOp::from))
10048                    .collect::<Result<Vec<_>, _>>()
10049                    .map_err(|_| TransportError::new("storage.failed", "sync.local_corrupt: invalid acknowledged operation".to_owned()))?;
10050                for index in 0..self.outbox.len() {
10051                    if !self.outbox[index]
10052                        .ops
10053                        .iter()
10054                        .any(|op| acknowledged.iter().any(|earlier| chains_on(earlier, op)))
10055                    {
10056                        continue;
10057                    }
10058                    let commit = Arc::make_mut(&mut self.outbox[index]);
10059                    for op in &mut commit.ops {
10060                        if acknowledged.iter().any(|earlier| chains_on(earlier, op)) {
10061                            op.base_version = Some(change.row_version);
10062                        }
10063                    }
10064                    let ops = serde_json::to_string(
10065                        &commit
10066                            .ops
10067                            .iter()
10068                            .map(CommitOperation::from)
10069                            .collect::<Vec<_>>(),
10070                    )
10071                    .map_err(|_| {
10072                        TransportError::new(
10073                            "storage.failed",
10074                            "sync.local_corrupt: unencodable outbox".to_owned(),
10075                        )
10076                    })?;
10077                    let id = commit.client_commit_id.clone();
10078                    self.conn
10079                        .execute(
10080                            "UPDATE _syncular_outbox SET ops_json=?1 WHERE commit_id=?2",
10081                            rusqlite::params![ops, id],
10082                        )
10083                        .map_err(|error| {
10084                            TransportError::new(
10085                                "storage.failed",
10086                                Self::sqlite_failure(&self.storage_failure, error),
10087                            )
10088                        })?;
10089                }
10090            }
10091            {
10092                let storage_failed = |error| {
10093                    TransportError::new(
10094                        "storage.failed",
10095                        Self::sqlite_failure(&self.storage_failure, error),
10096                    )
10097                };
10098                let pinned: HashSet<(&str, &str)> = self
10099                    .outbox
10100                    .iter()
10101                    .flat_map(|commit| &commit.ops)
10102                    .map(|op| (op.table.as_str(), op.row_id.as_str()))
10103                    .collect();
10104                for change in changes {
10105                    let table = tables.get(change.table_index as usize).ok_or_else(|| {
10106                        TransportError::new(
10107                            "sync.invalid_request",
10108                            "change tableIndex out of range".to_owned(),
10109                        )
10110                    })?;
10111                    if pinned.contains(&(table.as_str(), change.row_id.as_str())) {
10112                        self.conn.prepare_cached("INSERT INTO _syncular_row_deliveries(tbl,id,commit_seq) VALUES (?1,?2,?3) ON CONFLICT(tbl,id) DO UPDATE SET commit_seq=MAX(commit_seq,excluded.commit_seq)")
10113                            .and_then(|mut statement| statement.execute(rusqlite::params![table, change.row_id, sequence]))
10114                            .map_err(storage_failed)?;
10115                    }
10116                    self.conn.prepare_cached("DELETE FROM _syncular_acked_rows WHERE tbl=?1 AND id=?2 AND commit_seq<=?3")
10117                        .and_then(|mut statement| statement.execute(rusqlite::params![table, change.row_id, sequence]))
10118                        .map_err(storage_failed)?;
10119                }
10120            }
10121            self.apply_commit_changes(tables, changes)?;
10122            self.rebuild_overlay_if_dirty()
10123                .map_err(|message| TransportError::new("storage.failed", message))?;
10124            self.finish_observation("syncular_commit", batch)
10125                .map_err(|message| TransportError::new("storage.failed", message))
10126        })();
10127        if applied.is_err() {
10128            self.rollback_observation("syncular_commit");
10129            self.restore(rollback);
10130        }
10131        applied
10132    }
10133
10134    fn apply_commit_changes(
10135        &mut self,
10136        tables: &[String],
10137        changes: &[ssp2::model::Change],
10138    ) -> Result<(), TransportError> {
10139        // A clean overlay can reconcile changed primary keys independently
10140        // when their tables have no secondary unique constraints. Such a
10141        // constraint can change whether another row's write succeeds, so a
10142        // table with one and any local intent takes the table rebuild.
10143        let changed_tables: BTreeSet<&str> = changes
10144            .iter()
10145            .filter_map(|change| tables.get(change.table_index as usize))
10146            .map(String::as_str)
10147            .collect();
10148        let mut mirror_visible = !self.overlay_dirty.get();
10149        for name in &changed_tables {
10150            if mirror_visible
10151                && self
10152                    .schema
10153                    .table(name)
10154                    .is_some_and(|table| table.indexes.iter().any(|index| index.unique))
10155            {
10156                let (pending, protected) = self
10157                    .table_intent(name)
10158                    .map_err(|message| TransportError::new("storage.failed", message))?;
10159                mirror_visible = !pending && !protected;
10160            }
10161        }
10162        for change in changes {
10163            let table_name = tables.get(change.table_index as usize).ok_or_else(|| {
10164                TransportError::new(
10165                    "sync.invalid_request",
10166                    "change tableIndex out of range".to_owned(),
10167                )
10168            })?;
10169            let table = self.schema.table(table_name).ok_or_else(|| {
10170                TransportError::new(
10171                    "sync.schema_mismatch",
10172                    format!("change targets unknown table {table_name:?}"),
10173                )
10174            })?;
10175            let landed = match change.op {
10176                Op::Upsert => {
10177                    let payload = change.row.as_ref().ok_or_else(|| {
10178                        TransportError::new(
10179                            "sync.invalid_request",
10180                            "upsert change without row payload".to_owned(),
10181                        )
10182                    })?;
10183                    // §5.11: decrypt encrypted columns on apply, or seal the row.
10184                    let (row, sealed) = {
10185                        #[cfg(feature = "bench-internals")]
10186                        let _phase = self.benchmark_phases.start(Phase::RowDecode);
10187                        // §5.11: a decrypt failure keeps its own identity.
10188                        decode_row_bytes(table, payload, &self.encryption)?
10189                    };
10190                    let table_name = table.name.clone();
10191                    // §4.5 version guard: only a newer upsert lands.
10192                    let landed = self
10193                        .write_base_row(&table_name, &row, change.row_version, sealed.as_ref())
10194                        .map_err(|m| TransportError::new("sync.invalid_request", m))?;
10195                    if mirror_visible && landed {
10196                        self.write_row(
10197                            &visible_table(&table_name),
10198                            &table_name,
10199                            &row,
10200                            change.row_version,
10201                            sealed.as_ref(),
10202                        )
10203                        .map_err(|m| TransportError::new("sync.invalid_request", m))?;
10204                    }
10205                    landed
10206                }
10207                // §4.5 version guard: a delete at or below the local version
10208                // is skipped, a delete of an absent row is a no-op.
10209                Op::Delete => self
10210                    .delete_base_row(table_name, &change.row_id, change.row_version)
10211                    .map_err(|m| TransportError::new("sync.invalid_request", m))?,
10212            };
10213            // The change dropped the row's acknowledged intent either way
10214            // (§7.1), so the overlay of a skipped change rebuilds from the
10215            // unchanged base.
10216            self.overlay_dirty.table(table_name);
10217            if mirror_visible && !(landed && change.op == Op::Upsert) {
10218                let predicate = row_id_predicate(table);
10219                let visible = visible_table(table_name);
10220                let mut statements = vec![format!("DELETE FROM {visible} WHERE {predicate}")];
10221                if !landed {
10222                    statements.push(format!(
10223                        "INSERT INTO {visible} SELECT * FROM {} WHERE {predicate}",
10224                        base_table(table_name)
10225                    ));
10226                }
10227                for statement in &statements {
10228                    self.conn
10229                        .prepare_cached(statement)
10230                        .and_then(|mut statement| {
10231                            statement.execute(rusqlite::params![change.row_id])
10232                        })
10233                        .map_err(|error| {
10234                            TransportError::new("sync.invalid_request", error.to_string())
10235                        })?;
10236                }
10237            }
10238        }
10239        if mirror_visible {
10240            let changed_rows: HashSet<(&str, &str)> = changes
10241                .iter()
10242                .map(|change| {
10243                    (
10244                        tables[change.table_index as usize].as_str(),
10245                        change.row_id.as_str(),
10246                    )
10247                })
10248                .collect();
10249            self.replay_overlay(&changed_tables, Some(&changed_rows))
10250                .map_err(|message| TransportError::new("storage.failed", message))?;
10251            self.overlay_dirty.set(false);
10252        }
10253        Ok(())
10254    }
10255
10256    /// §5.6 segment application: validate against the generated schema,
10257    /// clear the pair's scope on the table's first page (fail closed
10258    /// without a mapping), then upsert each row with its segment-carried
10259    /// server version under the §4.5 version guard (§5.2).
10260    fn apply_segment(
10261        &mut self,
10262        table: &TableSchema,
10263        segment: &RowsSegment,
10264        first_page: bool,
10265        effective: &[(String, Vec<String>)],
10266        unit_id: &str,
10267    ) -> Result<u32, SectionError> {
10268        // §5.2: the column table validates against the generated schema —
10269        // order, names, types, nullability; mismatch is fatal. §5.11: the
10270        // server sends the WIRE types (bytes for an encrypted column), so
10271        // validate against wire_columns.
10272        let column = if segment.columns.len() != table.wire_columns.len() {
10273            Some(Value::from("count"))
10274        } else {
10275            segment
10276                .columns
10277                .iter()
10278                .zip(table.wire_columns.iter())
10279                .position(|(a, b)| a.name != b.name || a.ty != b.ty || a.nullable != b.nullable)
10280                .map(Value::from)
10281        };
10282        if segment.table != table.name
10283            || segment.schema_version != self.schema.version
10284            || column.is_some()
10285        {
10286            let mut details = Map::new();
10287            details.insert("table".to_owned(), Value::from(table.name.as_str()));
10288            if let Some(column) = column {
10289                details.insert("column".to_owned(), column);
10290            }
10291            return Err(SectionError::Transport(TransportError {
10292                code: "sync.schema_mismatch".to_owned(),
10293                message: "rows segment columns do not match the generated schema (§5.2)".to_owned(),
10294                details: Some(Value::Object(details)),
10295            }));
10296        }
10297        let mut applied = 0u32;
10298        let empty = Vec::new();
10299        let blocks = if segment.blocks.is_empty() {
10300            std::slice::from_ref(&empty)
10301        } else {
10302            &segment.blocks
10303        };
10304        for (index, block) in blocks.iter().enumerate() {
10305            let clear = first_page && index == 0;
10306            if !clear && block.is_empty() {
10307                continue;
10308            }
10309            let rollback = self.capture();
10310            let (pending, protected) = self
10311                .table_intent(&table.name)
10312                .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10313            let reconcile_pending =
10314                !protected && pending && table.indexes.iter().any(|index| index.unique);
10315            let mirror_visible = !protected && !reconcile_pending;
10316            self.coalesce_unit(block.len())
10317                .and_then(|()| self.begin_observation("syncular_segment_block"))
10318                .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10319            let outcome = (|| {
10320                self.rebuild_overlay_if_dirty()
10321                    .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10322                let mut changed_keys = Vec::new();
10323                let mut batch = ChangeAccumulator::default();
10324                if clear && self.clear_scope(table, effective, unit_id)? || !block.is_empty() {
10325                    batch.table(&table.name);
10326                }
10327                for (row_index, row) in block.iter().enumerate() {
10328                    let decrypted;
10329                    let mut sealed = None;
10330                    let values = if table.has_encrypted_columns() {
10331                        let mut values = row.values.clone();
10332                        sealed = crate::values::decrypt_segment_row(
10333                            table,
10334                            &mut values,
10335                            &self.encryption,
10336                        )
10337                        .map_err(SectionError::Transport)?;
10338                        decrypted = values;
10339                        &decrypted
10340                    } else {
10341                        &row.values
10342                    };
10343                    if !self
10344                        .write_base_row(&table.name, values, row.server_version, sealed.as_ref())
10345                        .map_err(|message| {
10346                            SectionError::Abort("sync.invalid_request".into(), message)
10347                        })?
10348                    {
10349                        continue;
10350                    }
10351                    if reconcile_pending {
10352                        changed_keys.push(
10353                            owned_sql_value(RowParam::Cell(&values[table.pk_index])).map_err(
10354                                |message| SectionError::Abort("storage.failed".into(), message),
10355                            )?,
10356                        );
10357                    }
10358                    if mirror_visible {
10359                        self.write_row(
10360                            &visible_table(&table.name),
10361                            &table.name,
10362                            values,
10363                            row.server_version,
10364                            sealed.as_ref(),
10365                        )
10366                        .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10367                    }
10368                    let processed = u64::from(applied) + row_index as u64 + 1;
10369                    if processed.is_multiple_of(1024) {
10370                        self.progress.update(|p| p.rows_processed = processed);
10371                    }
10372                }
10373                self.reconcile_import(table, &mut changed_keys, protected, reconcile_pending)
10374                    .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10375                self.finish_observation("syncular_segment_block", batch)
10376                    .map_err(|message| SectionError::Abort("storage.failed".into(), message))
10377            })();
10378            if let Err(error) = outcome {
10379                self.rollback_observation("syncular_segment_block");
10380                self.restore(rollback);
10381                return Err(error);
10382            }
10383            applied += block.len() as u32;
10384            self.progress
10385                .update(|p| p.rows_processed = u64::from(applied));
10386            std::thread::yield_now();
10387        }
10388        self.progress
10389            .update(|p| p.rows_processed = u64::from(applied));
10390        Ok(applied)
10391    }
10392
10393    /// §5.3 sqlite-image application: validate the in-file metadata
10394    /// against the descriptor, validate column names/order against the
10395    /// generated schema, run the §5.6 first-page clear when fresh, then
10396    /// replace-or-upsert every image row with its `_syncular_version`.
10397    /// Mechanics: the image lands in a temp file read through a second
10398    /// rusqlite connection, equivalent to ATTACH + INSERT…SELECT.
10399    /// Each image chunk commits its writes and revision before readers continue.
10400    #[allow(clippy::too_many_arguments)]
10401    fn apply_sqlite_segment(
10402        &mut self,
10403        table: &TableSchema,
10404        bytes: &[u8],
10405        first_page: bool,
10406        effective: &[(String, Vec<String>)],
10407        unit_id: &str,
10408        segment_id: &str,
10409        resumed: i64,
10410        row_count: i64,
10411        as_of_commit_seq: i64,
10412        scope_digest: &str,
10413    ) -> Result<u32, SectionError> {
10414        let invalid = |detail: &str| {
10415            SectionError::Abort(
10416                "sync.invalid_request".to_owned(),
10417                format!("sqlite segment rejected: {detail} (§5.3)"),
10418            )
10419        };
10420        // §5.11: an image bypasses the decrypt seam, so a server cannot serve
10421        // one for a table with encrypted columns.
10422        if table.has_encrypted_columns() {
10423            return Err(invalid("table has encrypted columns"));
10424        }
10425
10426        // The image lands beside the replica so it shares the replica's
10427        // directory permissions and volume; an in-memory replica has no
10428        // directory and uses the system temp dir. The file is owner-only.
10429        let directory = self
10430            .previous_version_replica_path()
10431            .and_then(|replica| std::path::Path::new(&replica).parent().map(Into::into))
10432            .unwrap_or_else(std::env::temp_dir);
10433        let path = directory.join(format!(".syncular-image-{}.tmp", uuid::Uuid::new_v4()));
10434        let outcome = (|| {
10435            let mut options = std::fs::OpenOptions::new();
10436            options.write(true).create_new(true);
10437            #[cfg(unix)]
10438            std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
10439            std::io::Write::write_all(
10440                &mut options
10441                    .open(&path)
10442                    .map_err(|_| invalid("image temp file write failed"))?,
10443                bytes,
10444            )
10445            .map_err(|_| invalid("image temp file write failed"))?;
10446            let img = rusqlite::Connection::open_with_flags(
10447                &path,
10448                rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
10449            )
10450            .map_err(|_| invalid("bytes do not open as a SQLite database"))?;
10451            self.apply_sqlite_image(
10452                &img,
10453                table,
10454                first_page,
10455                effective,
10456                unit_id,
10457                segment_id,
10458                resumed,
10459                row_count,
10460                as_of_commit_seq,
10461                scope_digest,
10462            )
10463        })();
10464        let _ = std::fs::remove_file(&path);
10465        outcome
10466    }
10467
10468    #[allow(clippy::too_many_arguments)]
10469    fn apply_sqlite_image(
10470        &mut self,
10471        img: &rusqlite::Connection,
10472        table: &crate::schema::TableSchema,
10473        first_fresh_page: bool,
10474        effective: &[(String, Vec<String>)],
10475        unit_id: &str,
10476        segment_id: &str,
10477        resumed: i64,
10478        row_count: i64,
10479        as_of_commit_seq: i64,
10480        scope_digest: &str,
10481    ) -> Result<u32, SectionError> {
10482        let invalid = |detail: String| {
10483            SectionError::Abort(
10484                "sync.invalid_request".to_owned(),
10485                format!("sqlite segment rejected: {detail} (§5.3)"),
10486            )
10487        };
10488
10489        // 1. Metadata vs descriptor + client state (§5.3 rule 2; exactly
10490        //    one row).
10491        type MetaRow = (i64, String, i64, i64, String, i64, i64);
10492        let meta: MetaRow = img
10493            .query_row(
10494                "SELECT format, \"table\", \"schemaVersion\", \"asOfCommitSeq\",
10495                        \"scopeDigest\", \"rowCount\",
10496                        (SELECT count(*) FROM _syncular_segment)
10497                 FROM _syncular_segment",
10498                [],
10499                |row| {
10500                    Ok((
10501                        row.get(0)?,
10502                        row.get(1)?,
10503                        row.get(2)?,
10504                        row.get(3)?,
10505                        row.get(4)?,
10506                        row.get(5)?,
10507                        row.get(6)?,
10508                    ))
10509                },
10510            )
10511            .map_err(|_| invalid("missing or unreadable _syncular_segment metadata".to_owned()))?;
10512        let (format, meta_table, schema_version, pin, digest, meta_rows, meta_count) = meta;
10513        if meta_count != 1 {
10514            return Err(invalid(format!(
10515                "_syncular_segment must contain exactly one row, found {meta_count}"
10516            )));
10517        }
10518        if format != 1 {
10519            return Err(invalid(format!("format {format}")));
10520        }
10521        if meta_table != table.name {
10522            return Err(invalid(format!("image table {meta_table:?}")));
10523        }
10524        if schema_version != i64::from(self.schema.version) {
10525            return Err(invalid(format!("schemaVersion {schema_version}")));
10526        }
10527        if pin != as_of_commit_seq {
10528            return Err(invalid(format!("asOfCommitSeq {pin}")));
10529        }
10530        if digest != scope_digest {
10531            return Err(invalid("scopeDigest mismatch".to_owned()));
10532        }
10533        if meta_rows != row_count {
10534            return Err(invalid(format!("rowCount {meta_rows}")));
10535        }
10536
10537        // 2. Column names and order vs the generated schema (§5.3 rule 3).
10538        let mut names: Vec<String> = Vec::new();
10539        let mut primary_keys: Vec<String> = Vec::new();
10540        {
10541            let mut stmt = img
10542                .prepare(&format!("PRAGMA table_info({})", quote_ident(&table.name)))
10543                .map_err(|_| invalid("image data table missing".to_owned()))?;
10544            let mut rows = stmt
10545                .query([])
10546                .map_err(|_| invalid("image data table unreadable".to_owned()))?;
10547            while let Some(row) = rows
10548                .next()
10549                .map_err(|_| invalid("image data table unreadable".to_owned()))?
10550            {
10551                if row
10552                    .get::<_, i64>(5)
10553                    .map_err(|error| invalid(Self::sqlite_failure(&self.storage_failure, error)))?
10554                    > 0
10555                {
10556                    primary_keys.push(row.get(1).map_err(|error| {
10557                        invalid(Self::sqlite_failure(&self.storage_failure, error))
10558                    })?);
10559                }
10560                names.push(
10561                    row.get::<_, String>(1)
10562                        .map_err(|_| invalid("image data table unreadable".to_owned()))?,
10563                );
10564            }
10565        }
10566        let mut expected: Vec<&str> = table.columns.iter().map(|c| c.name.as_str()).collect();
10567        expected.push("_syncular_version");
10568        if names.len() != expected.len() || names.iter().zip(expected.iter()).any(|(a, b)| a != b) {
10569            return Err(SectionError::Transport(TransportError {
10570                code: "sync.schema_mismatch".to_owned(),
10571                message: "sqlite segment columns do not match the generated schema (§5.3)"
10572                    .to_owned(),
10573                details: Some(serde_json::json!({ "table": table.name })),
10574            }));
10575        }
10576
10577        if primary_keys != vec![table.primary_key.clone()] {
10578            return Err(invalid(
10579                "image primary key does not match generated schema".into(),
10580            ));
10581        }
10582        let actual_count: i64 = img
10583            .query_row(
10584                &format!("SELECT count(*) FROM {}", quote_ident(&table.name)),
10585                [],
10586                |row| row.get(0),
10587            )
10588            .map_err(|error| invalid(Self::sqlite_failure(&self.storage_failure, error)))?;
10589        if actual_count != row_count {
10590            return Err(invalid("image row count does not match descriptor".into()));
10591        }
10592        let insert = self.insert_row_sql(&base_table(&table.name), table);
10593        let visible_insert = self.insert_row_sql(&visible_table(&table.name), table);
10594        let column_list = names
10595            .iter()
10596            .map(|n| quote_ident(n))
10597            .collect::<Vec<_>>()
10598            .join(", ");
10599        // §5.6: an earlier attempt at this position committed the first
10600        // `resumed` rows in primary-key order; the content address pins them.
10601        let mut stmt = img
10602            .prepare(&format!(
10603                "SELECT {column_list} FROM {} ORDER BY {} LIMIT -1 OFFSET ?1",
10604                quote_ident(&table.name),
10605                quote_ident(&table.primary_key)
10606            ))
10607            .map_err(|error| invalid(Self::sqlite_failure(&self.storage_failure, error)))?;
10608        let mut rows = stmt
10609            .query([resumed])
10610            .map_err(|error| invalid(Self::sqlite_failure(&self.storage_failure, error)))?;
10611        let resumed = u32::try_from(resumed)
10612            .ok()
10613            .filter(|rows| i64::from(*rows) <= actual_count)
10614            .ok_or_else(|| invalid("applied-segment record exceeds the image".into()))?;
10615        let mut applied = resumed;
10616        let mut limit = if first_fresh_page {
10617            1024
10618        } else {
10619            self.image_chunk_rows
10620                .get(&table.name)
10621                .copied()
10622                .unwrap_or(1024)
10623        };
10624        let (mut write, mut commit) = (std::time::Duration::ZERO, std::time::Duration::ZERO);
10625        loop {
10626            let clear = applied == 0 && first_fresh_page;
10627            let rollback = self.capture();
10628            let (pending, protected) = self
10629                .table_intent(&table.name)
10630                .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10631            let reconcile_pending =
10632                !protected && pending && table.indexes.iter().any(|index| index.unique);
10633            let mirror_visible = !protected && !reconcile_pending;
10634            // §5.3: a table's first chunk holds 1,024 rows; a later range image
10635            // starts at the size its previous range reached. A later chunk holds at
10636            // least 1,024 rows, at most twice the rows of the one before, at
10637            // most 65,536, and at most the rows the one before applied per
10638            // 250 ms from its start to its COMMIT; a chunk that replays local
10639            // writes holds at most 1,024.
10640            let chunk = if pending || protected {
10641                limit.min(1024)
10642            } else {
10643                limit
10644            };
10645            self.coalesce_unit(chunk as usize)
10646                .and_then(|()| self.begin_observation("syncular_image_chunk"))
10647                .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10648            let started = std::time::Instant::now();
10649            let result = (|| {
10650                self.rebuild_overlay_if_dirty()
10651                    .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10652                let mut changed_keys = Vec::new();
10653                let mut batch = ChangeAccumulator::default();
10654                if clear && self.clear_scope(table, effective, unit_id)? {
10655                    batch.table(&table.name);
10656                }
10657                let mut processed = 0u32;
10658                {
10659                    let mut ins = self.conn.prepare_cached(&insert).map_err(|error| {
10660                        invalid(Self::sqlite_failure(&self.storage_failure, error))
10661                    })?;
10662                    let mut mirror = if mirror_visible {
10663                        Some(self.conn.prepare_cached(&visible_insert).map_err(|error| {
10664                            invalid(Self::sqlite_failure(&self.storage_failure, error))
10665                        })?)
10666                    } else {
10667                        None
10668                    };
10669                    for _ in 0..chunk {
10670                        let Some(row) = rows.next().map_err(|error| {
10671                            invalid(Self::sqlite_failure(&self.storage_failure, error))
10672                        })?
10673                        else {
10674                            break;
10675                        };
10676                        for (i, column) in table.columns.iter().enumerate() {
10677                            let cell = row.get_ref(i).map_err(|error| {
10678                                invalid(Self::sqlite_failure(&self.storage_failure, error))
10679                            })?;
10680                            let param = image_cell_param(column, cell).map_err(invalid)?;
10681                            ins.raw_bind_parameter(i + 1, &param).map_err(|error| {
10682                                invalid(Self::sqlite_failure(&self.storage_failure, error))
10683                            })?;
10684                            if let Some(mirror) = &mut mirror {
10685                                mirror.raw_bind_parameter(i + 1, &param).map_err(|error| {
10686                                    invalid(Self::sqlite_failure(&self.storage_failure, error))
10687                                })?;
10688                            }
10689                        }
10690                        let version_index = table.columns.len();
10691                        let version: i64 = row.get(version_index).map_err(|error| {
10692                            invalid(Self::sqlite_failure(&self.storage_failure, error))
10693                        })?;
10694                        if version < 1 {
10695                            return Err(invalid("image row version must be positive".into()));
10696                        }
10697                        ins.raw_bind_parameter(version_index + 1, version)
10698                            .map_err(|error| {
10699                                invalid(Self::sqlite_failure(&self.storage_failure, error))
10700                            })?;
10701                        // §4.5 version guard: the base upsert decides.
10702                        let landed = ins.raw_execute().map_err(|error| {
10703                            invalid(Self::sqlite_failure(&self.storage_failure, error))
10704                        })? > 0;
10705                        processed += 1;
10706                        if processed.is_multiple_of(1024) && processed < chunk {
10707                            self.progress
10708                                .update(|p| p.rows_processed = u64::from(applied + processed));
10709                        }
10710                        if !landed {
10711                            continue;
10712                        }
10713                        if let Some(mirror) = &mut mirror {
10714                            mirror
10715                                .raw_bind_parameter(version_index + 1, version)
10716                                .map_err(|error| {
10717                                    invalid(Self::sqlite_failure(&self.storage_failure, error))
10718                                })?;
10719                            mirror.raw_execute().map_err(|error| {
10720                                invalid(Self::sqlite_failure(&self.storage_failure, error))
10721                            })?;
10722                        }
10723                        if reconcile_pending {
10724                            changed_keys.push(row.get::<_, SqlValue>(table.pk_index).map_err(
10725                                |error| invalid(Self::sqlite_failure(&self.storage_failure, error)),
10726                            )?);
10727                        }
10728                    }
10729                }
10730                if processed > 0 {
10731                    batch.table(&table.name);
10732                    self.overlay_dirty.table(&table.name);
10733                }
10734                self.stored(record_applied_rows(
10735                    &self.conn,
10736                    unit_id,
10737                    segment_id,
10738                    i64::from(applied + processed),
10739                ))
10740                .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10741                self.reconcile_import(table, &mut changed_keys, protected, reconcile_pending)
10742                    .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10743                let written = std::time::Instant::now();
10744                self.finish_observation("syncular_image_chunk", batch)
10745                    .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10746                Ok((processed, written))
10747            })();
10748            let (processed, written) = match result {
10749                Ok(done) => done,
10750                Err(error) => {
10751                    self.rollback_observation("syncular_image_chunk");
10752                    self.restore(rollback);
10753                    return Err(error);
10754                }
10755            };
10756            // The chunk commits before the yield, also inside a coalesced apply.
10757            let coalesced = self.coalesced.is_some();
10758            if coalesced {
10759                self.commit_coalesced_apply()
10760                    .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10761            }
10762            write += written - started;
10763            commit += written.elapsed();
10764            applied += processed;
10765            let last = processed < chunk || i64::from(applied) == actual_count;
10766            // A file replica's WAL keeps its high-water size until truncated.
10767            if last && self.conn.is_autocommit() {
10768                self.conn
10769                    .query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |_| Ok(()))
10770                    .map_err(|error| invalid(Self::sqlite_failure(&self.storage_failure, error)))?;
10771            }
10772            if coalesced {
10773                self.begin_coalesced_apply()
10774                    .map_err(|message| SectionError::Abort("storage.failed".into(), message))?;
10775            }
10776            self.progress.update(|p| {
10777                p.rows_processed = u64::from(applied);
10778                p.import_write_ms = u64::try_from(write.as_millis()).ok();
10779                p.import_commit_ms = u64::try_from(commit.as_millis()).ok();
10780            });
10781            let duration = started.elapsed().as_secs_f64() * 1000.0;
10782            #[cfg(test)]
10783            let duration = self.image_chunk_ms.get().unwrap_or(duration);
10784            std::thread::yield_now();
10785            if last {
10786                break;
10787            }
10788            // A zero duration saturates the cast and leaves the doubling.
10789            limit = (processed * 2)
10790                .min(65_536)
10791                .min((f64::from(processed) * 250.0 / duration) as u32)
10792                .max(1024);
10793            self.image_chunk_rows.insert(table.name.clone(), limit);
10794        }
10795        if i64::from(applied) != actual_count {
10796            return Err(invalid(format!(
10797                "image yielded {applied} rows, expected {actual_count}"
10798            )));
10799        }
10800        Ok(applied - resumed)
10801    }
10802
10803    /// §7.1 keyed reconciliation of the rows `drained` commits touch: restore
10804    /// them from the base and replay their remaining intent. A table with a
10805    /// secondary unique index restores and replays every row with intent,
10806    /// since one row's value can admit or defer another row's write.
10807    fn reconcile_rows(&self, drained: &[Arc<OutboxCommit>]) -> Result<(), String> {
10808        let names: BTreeSet<&str> = drained
10809            .iter()
10810            .flat_map(|commit| &commit.ops)
10811            .map(|op| op.table.as_str())
10812            .collect();
10813        for name in names {
10814            let Some(table) = self.schema.table(name) else {
10815                continue;
10816            };
10817            let ops = drained.iter().flat_map(|commit| &commit.ops);
10818            let tables = BTreeSet::from([name]);
10819            let rows: HashSet<_> = ops
10820                .clone()
10821                .map(|op| (op.table.as_str(), op.row_id.as_str()))
10822                .collect();
10823            let unique = table.indexes.iter().any(|index| index.unique);
10824            let acknowledged = if unique {
10825                self.acknowledged_operations(None, Some(&tables))?
10826            } else {
10827                Vec::new()
10828            };
10829            let peers = acknowledged
10830                .iter()
10831                .chain(
10832                    self.failed_commits
10833                        .iter()
10834                        .flat_map(|(commit, _)| &commit.ops),
10835                )
10836                .chain(self.outbox.iter().flat_map(|commit| &commit.ops))
10837                .filter(|_| unique);
10838            self.restore_base_rows(table, &mut Vec::new(), peers.chain(ops.clone()))?;
10839            self.replay_overlay(&tables, (!unique).then_some(&rows))?;
10840        }
10841        Ok(())
10842    }
10843
10844    /// Whether `table` has pending outbox intent, and whether it has protected
10845    /// (acknowledged or retained failed) intent.
10846    fn table_intent(&self, table: &str) -> Result<(bool, bool), String> {
10847        let touches = |op: &OutboxOp| op.table == table;
10848        Ok((
10849            self.outbox
10850                .iter()
10851                .flat_map(|commit| &commit.ops)
10852                .any(touches),
10853            self.failed_commits
10854                .iter()
10855                .flat_map(|(commit, _)| &commit.ops)
10856                .any(touches)
10857                || self
10858                    .conn
10859                    .query_row(
10860                        "SELECT EXISTS(SELECT 1 FROM _syncular_acked_rows WHERE tbl=?1)",
10861                        [table],
10862                        |row| row.get(0),
10863                    )
10864                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?,
10865        ))
10866    }
10867
10868    /// §7.1 reconciliation after an import block or chunk. Keyed replay covers
10869    /// only pending intent, so a table with protected intent rebuilds; a
10870    /// `restore` restores the imported `keys` and every pending row first.
10871    fn reconcile_import(
10872        &mut self,
10873        table: &TableSchema,
10874        keys: &mut Vec<SqlValue>,
10875        protected: bool,
10876        restore: bool,
10877    ) -> Result<(), String> {
10878        if protected {
10879            return self.rebuild_overlay_if_dirty();
10880        }
10881        let pending = self.outbox.iter().flat_map(|commit| &commit.ops);
10882        if restore {
10883            self.restore_base_rows(table, keys, pending.clone())?;
10884        }
10885        self.apply_outbox_ops(pending.filter(|op| op.table == table.name))?;
10886        self.overlay_dirty.set(false);
10887        Ok(())
10888    }
10889
10890    /// Restore the visible rows of `keys` and of the rows `ops` touch in
10891    /// `table` from the base before the caller replays their intent. The
10892    /// caller owns the transaction and publishes its revision after replay.
10893    fn restore_base_rows<'a>(
10894        &self,
10895        table: &TableSchema,
10896        keys: &mut Vec<SqlValue>,
10897        ops: impl IntoIterator<Item = &'a OutboxOp>,
10898    ) -> Result<(), String> {
10899        let visible = visible_table(&table.name);
10900        let base = base_table(&table.name);
10901        let pk = quote_ident(&table.primary_key);
10902        let predicate = row_id_predicate(table);
10903        let mut lookup = self.conn.prepare_cached(&format!(
10904            "SELECT {pk} FROM {base} WHERE {predicate} UNION SELECT {pk} FROM {visible} WHERE {predicate}"
10905        )).map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
10906        let mut deleted = HashSet::new();
10907        for op in ops.into_iter().filter(|op| op.table == table.name) {
10908            if op.upsert {
10909                if let Some(values) = &op.values {
10910                    let key = json_to_column_value(
10911                        &table.columns[table.pk_index],
10912                        values.get(&table.primary_key),
10913                    )?;
10914                    keys.push(owned_sql_value(RowParam::Cell(&key))?);
10915                }
10916            } else if deleted.insert(&op.row_id) {
10917                keys.extend(
10918                    lookup
10919                        .query_map([&op.row_id], |row| row.get::<_, SqlValue>(0))
10920                        .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
10921                        .collect::<Result<Vec<_>, _>>()
10922                        .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?,
10923                );
10924            }
10925        }
10926        // Delete the complete affected set before restoring any base value, so
10927        // old optimistic unique values cannot collide with the new server rows.
10928        for chunk in keys.chunks(400) {
10929            let holes = vec!["?"; chunk.len()].join(",");
10930            self.conn
10931                .prepare_cached(&format!("DELETE FROM {visible} WHERE {pk} IN ({holes})"))
10932                .and_then(|mut statement| statement.execute(rusqlite::params_from_iter(chunk)))
10933                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
10934        }
10935        let updates = table
10936            .columns
10937            .iter()
10938            .map(|column| column.name.as_str())
10939            .chain(std::iter::once("_syncular_version"))
10940            .chain(table.sealed_columns().iter().copied())
10941            .filter(|name| *name != table.primary_key)
10942            .map(|name| {
10943                let name = quote_ident(name);
10944                format!("{name}=excluded.{name}")
10945            })
10946            .collect::<Vec<_>>()
10947            .join(",");
10948        for chunk in keys.chunks(400) {
10949            let holes = vec!["?"; chunk.len()].join(",");
10950            self.conn.prepare_cached(&format!("INSERT INTO {visible} SELECT * FROM {base} WHERE {pk} IN ({holes}) ON CONFLICT ({pk}) DO UPDATE SET {updates}"))
10951                .and_then(|mut statement| statement.execute(rusqlite::params_from_iter(chunk)))
10952                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
10953        }
10954        Ok(())
10955    }
10956
10957    // -- application-authorized local purge ---------------------------------------
10958
10959    fn compile_local_data_purge(
10960        &self,
10961        input: &LocalDataPurgeInput,
10962    ) -> Result<(Vec<CompiledLocalDataPurgeTarget>, String), String> {
10963        let invalid = |message: String| format!("sync.invalid_request: {message}");
10964        if input.purge_id.is_empty()
10965            || input.purge_id.len() > 128
10966            || !is_local_operation_code_like(&input.purge_id)
10967        {
10968            return Err(invalid(
10969                "local purge purgeId must be a 1–128 character code-like identifier".to_owned(),
10970            ));
10971        }
10972        if input.targets.is_empty() || input.targets.len() > MAX_LOCAL_PURGE_TARGETS {
10973            return Err(invalid(format!(
10974                "local purge needs between 1 and {MAX_LOCAL_PURGE_TARGETS} targets"
10975            )));
10976        }
10977
10978        let mut deduplicated: BTreeMap<
10979            String,
10980            (CompiledLocalDataPurgeTarget, LocalDataPurgeTarget),
10981        > = BTreeMap::new();
10982        for target in &input.targets {
10983            let table = self.schema.table(&target.table).ok_or_else(|| {
10984                invalid(format!(
10985                    "local purge names unknown table {:?}",
10986                    target.table
10987                ))
10988            })?;
10989            if target.selectors.is_empty() || target.selectors.len() > MAX_LOCAL_PURGE_SELECTORS {
10990                return Err(invalid(format!(
10991                    "local purge target {:?} needs between 1 and {MAX_LOCAL_PURGE_SELECTORS} selectors",
10992                    target.table
10993                )));
10994            }
10995            let mut selectors = Vec::with_capacity(target.selectors.len());
10996            let mut canonical_selectors = BTreeMap::new();
10997            for (column_name, raw_values) in &target.selectors {
10998                let Some((column_index, column)) = table
10999                    .columns
11000                    .iter()
11001                    .enumerate()
11002                    .find(|(_, column)| column.name == *column_name)
11003                else {
11004                    return Err(invalid(format!(
11005                        "local purge target {:?} names unknown column {:?}",
11006                        target.table, column_name
11007                    )));
11008                };
11009                let encrypted = table
11010                    .encrypted_columns
11011                    .iter()
11012                    .any(|candidate| candidate.index == column_index);
11013                if column.ty != ColumnType::String || encrypted {
11014                    return Err(invalid(format!(
11015                        "local purge selector {:?}.{:?} must be a plaintext string column",
11016                        target.table, column_name
11017                    )));
11018                }
11019                if raw_values.is_empty() || raw_values.len() > MAX_LOCAL_PURGE_VALUES {
11020                    return Err(invalid(format!(
11021                        "local purge selector {:?}.{:?} needs between 1 and {MAX_LOCAL_PURGE_VALUES} values",
11022                        target.table, column_name
11023                    )));
11024                }
11025                let mut values = raw_values.clone();
11026                values.sort();
11027                values.dedup();
11028                if values.iter().any(|value| {
11029                    value.is_empty()
11030                        || value.len() > MAX_LOCAL_PURGE_VALUE_LENGTH
11031                        || !is_local_operation_code_like(value)
11032                }) {
11033                    return Err(invalid(format!(
11034                        "local purge selector values must be 1–{MAX_LOCAL_PURGE_VALUE_LENGTH} character code-like identifiers"
11035                    )));
11036                }
11037                selectors.push((column_name.clone(), values.clone()));
11038                canonical_selectors.insert(column_name.clone(), values);
11039            }
11040            let canonical = LocalDataPurgeTarget {
11041                table: target.table.clone(),
11042                selectors: canonical_selectors,
11043            };
11044            let key = serde_json::to_string(&canonical).map_err(|error| error.to_string())?;
11045            deduplicated.insert(
11046                key,
11047                (
11048                    CompiledLocalDataPurgeTarget {
11049                        table: target.table.clone(),
11050                        selectors,
11051                    },
11052                    canonical,
11053                ),
11054            );
11055        }
11056        let targets = deduplicated
11057            .values()
11058            .map(|(compiled, _)| compiled.clone())
11059            .collect::<Vec<_>>();
11060        let canonical = deduplicated
11061            .values()
11062            .map(|(_, target)| target.clone())
11063            .collect::<Vec<_>>();
11064        let canonical_plan =
11065            serde_json::to_string(&canonical).map_err(|error| error.to_string())?;
11066        Ok((targets, canonical_plan))
11067    }
11068
11069    fn local_purge_base_row_ids(
11070        &self,
11071        targets: &[CompiledLocalDataPurgeTarget],
11072    ) -> Result<BTreeMap<String, BTreeSet<String>>, String> {
11073        let mut by_table: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
11074        for target in targets {
11075            let table = self
11076                .schema
11077                .table(&target.table)
11078                .ok_or_else(|| format!("sync.invalid_request: unknown table {:?}", target.table))?;
11079            let mut clauses = Vec::with_capacity(target.selectors.len());
11080            let mut params = Vec::new();
11081            for (column, values) in &target.selectors {
11082                clauses.push(format!(
11083                    "{} IN ({})",
11084                    quote_ident(column),
11085                    values.iter().map(|_| "?").collect::<Vec<_>>().join(", ")
11086                ));
11087                params.extend(values.iter().cloned().map(SqlValue::Text));
11088            }
11089            let sql = format!(
11090                "SELECT CAST({} AS TEXT) FROM {} WHERE {}",
11091                quote_ident(&table.primary_key),
11092                base_table(&target.table),
11093                clauses.join(" AND ")
11094            );
11095            let mut statement = self
11096                .conn
11097                .prepare(&sql)
11098                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11099            let rows = statement
11100                .query_map(rusqlite::params_from_iter(params), |row| {
11101                    let value = sql_ref_to_json(&table.columns[table.pk_index], row.get_ref(0)?);
11102                    render_row_id_json(Some(&value))
11103                        .map_err(|message| rusqlite::Error::ToSqlConversionFailure(message.into()))
11104                })
11105                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11106            let ids = by_table.entry(target.table.clone()).or_default();
11107            for row in rows {
11108                ids.insert(
11109                    row.map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?,
11110                );
11111            }
11112        }
11113        Ok(by_table)
11114    }
11115
11116    fn local_purge_values_match(
11117        target: &CompiledLocalDataPurgeTarget,
11118        values: &Map<String, Value>,
11119    ) -> bool {
11120        target.selectors.iter().all(|(column, allowed)| {
11121            values
11122                .get(column)
11123                .and_then(Value::as_str)
11124                .is_some_and(|value| allowed.iter().any(|candidate| candidate == value))
11125        })
11126    }
11127
11128    /// Apply one host-authorized local security purge. The host MUST gate the
11129    /// corresponding subscriptions first; this primitive owns local SQLite
11130    /// cleanup and never grants/revokes server authority by itself.
11131    pub fn purge_local_data(
11132        &mut self,
11133        input: &LocalDataPurgeInput,
11134    ) -> Result<LocalDataPurgeResult, String> {
11135        let (targets, canonical_plan) = self.compile_local_data_purge(input)?;
11136        let meta_key = format!("localPurge:{}", input.purge_id);
11137        let applied_plan = self
11138            .conn
11139            .query_row(
11140                "SELECT value FROM _syncular_meta WHERE key = ?1",
11141                rusqlite::params![meta_key],
11142                |row| row.get::<_, String>(0),
11143            )
11144            .optional()
11145            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11146        if let Some(applied_plan) = applied_plan {
11147            if applied_plan != canonical_plan {
11148                return Err(format!(
11149                    "sync.invalid_request: local purge id {:?} was already used with a different plan",
11150                    input.purge_id
11151                ));
11152            }
11153            // A retry after a failed rewrite finishes it.
11154            self.erase_purged_pages()?;
11155            return Ok(LocalDataPurgeResult {
11156                already_applied: true,
11157                purged_rows: 0,
11158                dropped_commits: 0,
11159            });
11160        }
11161
11162        // RFC 0005 D7: the container file cannot be a purge target (its name is
11163        // not in the running schema), so purge drops it as a fixed step, before
11164        // any mirror row is touched and unconditionally.
11165        let _ = self.drop_previous_version();
11166
11167        let rollback = self.capture();
11168        self.begin_observation("syncular_local_purge")?;
11169        let applied = (|| -> Result<(ChangeAccumulator, LocalDataPurgeResult), String> {
11170            let row_ids = self.local_purge_base_row_ids(&targets)?;
11171            let retained_dropped = self.drop_failed_commits(|table, values| {
11172                targets.iter().any(|target| {
11173                    target.table == table && Self::local_purge_values_match(target, values)
11174                })
11175            })?;
11176            let doomed = self
11177                .outbox
11178                .iter()
11179                .filter(|commit| {
11180                    commit.ops.iter().any(|operation| {
11181                        let matching_targets = targets
11182                            .iter()
11183                            .filter(|target| target.table == operation.table)
11184                            .collect::<Vec<_>>();
11185                        if matching_targets.is_empty() {
11186                            return false;
11187                        }
11188                        if row_ids
11189                            .get(&operation.table)
11190                            .is_some_and(|ids| ids.contains(&operation.row_id))
11191                        {
11192                            return true;
11193                        }
11194                        operation.values.as_ref().is_some_and(|values| {
11195                            matching_targets
11196                                .iter()
11197                                .any(|target| Self::local_purge_values_match(target, values))
11198                        })
11199                    })
11200                })
11201                .cloned()
11202                .collect::<Vec<_>>();
11203            let doomed_ids = doomed
11204                .iter()
11205                .map(|commit| commit.client_commit_id.clone())
11206                .collect::<BTreeSet<_>>();
11207            let mut batch = ChangeAccumulator::default();
11208            for commit in &retained_dropped {
11209                batch.conflicts = true;
11210                batch.rejections = true;
11211                batch.outcomes = true;
11212                for operation in &commit.ops {
11213                    batch.table(&operation.table);
11214                }
11215            }
11216            let mut rejections = Vec::new();
11217            // §5.11 purge erasure: the journal keeps a dropped commit's identity and
11218            // codes, never its operations, so no purged value survives in it.
11219            for commit in &doomed {
11220                for operation in &commit.ops {
11221                    batch.table(&operation.table);
11222                    self.overlay_dirty.table(&operation.table);
11223                }
11224                let results = (0..commit.ops.len())
11225                    .map(|op_index| {
11226                        let rejection = RejectionRecord {
11227                            client_commit_id: commit.client_commit_id.clone(),
11228                            op_index: op_index as i32,
11229                            code: "client.local_data_purged".to_owned(),
11230                            message: "the commit was dropped by an application-authorized local data purge".to_owned(),
11231                            retryable: false,
11232                            details: None,
11233                            operation: None,
11234                        };
11235                        rejections.push(rejection.clone());
11236                        CommitOperationOutcome::Error { rejection }
11237                    })
11238                    .collect::<Vec<_>>();
11239                self.persist_commit_outcome(
11240                    &commit.client_commit_id,
11241                    CommitOutcomeStatus::Rejected,
11242                    &results,
11243                    None,
11244                )?;
11245                self.delete_outbox_persisted(&commit.client_commit_id)?;
11246            }
11247            if !doomed.is_empty() {
11248                self.outbox
11249                    .retain(|commit| !doomed_ids.contains(&commit.client_commit_id));
11250                self.rejections.extend(rejections);
11251                self.prune_commit_outcomes()?;
11252                batch.status = true;
11253                batch.rejections = true;
11254                batch.outcomes = true;
11255            }
11256
11257            let mut purged_rows = 0usize;
11258            for (table, ids) in &row_ids {
11259                if ids.is_empty() {
11260                    continue;
11261                }
11262                batch.table(table);
11263                self.overlay_dirty.table(table);
11264                for row_id in ids {
11265                    self.delete_base_row(table, row_id, i64::MAX)?;
11266                    purged_rows += 1;
11267                }
11268            }
11269            // §5.11 purge erasure: a journaled outcome with an operation or
11270            // conflict row the purge reaches keeps its identity, status, ids
11271            // and codes only.
11272            let touches = |table: &str, row_id: &str, values: Option<&Map<String, Value>>| {
11273                targets.iter().any(|target| {
11274                    target.table == table
11275                        && (row_ids.get(table).is_some_and(|ids| ids.contains(row_id))
11276                            || values.is_some_and(|values| {
11277                                Self::local_purge_values_match(target, values)
11278                            }))
11279                })
11280            };
11281            let entries = self
11282                .conn
11283                .prepare("SELECT client_commit_id, results_json, operations_json FROM _syncular_commit_outcomes")
11284                .and_then(|mut statement| {
11285                    statement
11286                        .query_map([], |row| {
11287                            Ok((
11288                                row.get::<_, String>(0)?,
11289                                row.get::<_, String>(1)?,
11290                                row.get::<_, Option<String>>(2)?,
11291                            ))
11292                        })?
11293                        .collect::<Result<Vec<_>, _>>()
11294                })
11295                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11296            for (client_commit_id, results_json, operations_json) in entries {
11297                let corrupt = |_| "sync.local_corrupt: persisted commit outcome is invalid".to_owned();
11298                let mut results: Vec<CommitOperationOutcome> =
11299                    serde_json::from_str(&results_json).map_err(corrupt)?;
11300                let operations: Vec<CommitOperation> = operations_json
11301                    .map(|json| serde_json::from_str(&json))
11302                    .transpose()
11303                    .map_err(corrupt)?
11304                    .unwrap_or_default();
11305                let named = operations
11306                    .iter()
11307                    .chain(results.iter().filter_map(|result| match result {
11308                        CommitOperationOutcome::Conflict { conflict } => conflict.operation.as_ref(),
11309                        CommitOperationOutcome::Error { rejection } => rejection.operation.as_ref(),
11310                        CommitOperationOutcome::Applied { .. } => None,
11311                    }))
11312                    .any(|operation| touches(&operation.table, &operation.row_id, operation.values.as_ref()))
11313                    || results.iter().any(|result| {
11314                        matches!(result, CommitOperationOutcome::Conflict { conflict }
11315                            if touches(&conflict.table, &conflict.row_id, Some(&conflict.server_row)))
11316                    });
11317                if !named {
11318                    continue;
11319                }
11320                for result in &mut results {
11321                    match result {
11322                        CommitOperationOutcome::Conflict { conflict } => {
11323                            conflict.server_row = Map::new();
11324                            conflict.conflict_columns.clear();
11325                            conflict.operation = None;
11326                        }
11327                        CommitOperationOutcome::Error { rejection } => {
11328                            rejection.details = None;
11329                            rejection.operation = None;
11330                        }
11331                        CommitOperationOutcome::Applied { .. } => {}
11332                    }
11333                }
11334                let results_json = serde_json::to_string(&results).map_err(|error| error.to_string())?;
11335                self.conn
11336                    .execute(
11337                        "UPDATE _syncular_commit_outcomes SET results_json = ?1, operations_json = NULL WHERE client_commit_id = ?2",
11338                        rusqlite::params![results_json, client_commit_id],
11339                    )
11340                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11341                batch.outcomes = true;
11342            }
11343            self.rebuild_overlay_if_dirty()?;
11344            self.delete_unreferenced_cached_blobs()?;
11345            self.conn
11346                .execute(
11347                    "INSERT INTO _syncular_meta(key, value) VALUES (?1, ?2)",
11348                    rusqlite::params![meta_key, canonical_plan],
11349                )
11350                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11351            Ok((
11352                batch,
11353                LocalDataPurgeResult {
11354                    already_applied: false,
11355                    purged_rows,
11356                    dropped_commits: doomed.len(),
11357                },
11358            ))
11359        })()
11360        .and_then(|(batch, result)| {
11361            self.finish_observation("syncular_local_purge", batch)
11362                .map(|()| result)
11363        });
11364        let result = match applied {
11365            Ok(result) => result,
11366            Err(error) => {
11367                self.rollback_observation("syncular_local_purge");
11368                self.restore(rollback);
11369                return Err(error);
11370            }
11371        };
11372        self.cancel_sync_round();
11373        self.erase_purged_pages()?;
11374        Ok(result)
11375    }
11376
11377    /// §5.11 purge erasure: SQLite leaves a deleted value in the freed pages of the
11378    /// database file, and the WAL keeps the frames written before the purge,
11379    /// until either is rewritten. VACUUM rebuilds the file from the remaining
11380    /// rows, and the checkpoint truncates the WAL.
11381    fn erase_purged_pages(&self) -> Result<(), String> {
11382        let erased = self
11383            .conn
11384            .execute_batch("PRAGMA secure_delete = ON; VACUUM;")
11385            .and_then(|()| {
11386                self.conn
11387                    .query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |_| Ok(()))
11388            });
11389        let reset = self.conn.execute_batch("PRAGMA secure_delete = OFF");
11390        erased
11391            .and(reset)
11392            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
11393    }
11394
11395    /// Application-authorized recovery of the replicated projection. Unlike a
11396    /// local security purge, this retains the entire outbox, device identity,
11397    /// lease, outcomes, unit registrations, and protected bookkeeping.
11398    /// The projection reset, unit rewind, optimistic replay, and
11399    /// idempotency marker share one savepoint for interruption safety.
11400    pub fn rebootstrap_local_data(
11401        &mut self,
11402        input: &LocalDataRebootstrapInput,
11403    ) -> Result<LocalDataRebootstrapResult, String> {
11404        if input.rebootstrap_id.is_empty()
11405            || input.rebootstrap_id.len() > 128
11406            || !is_local_operation_code_like(&input.rebootstrap_id)
11407        {
11408            return Err(
11409                "sync.invalid_request: local rebootstrap rebootstrapId must be a 1–128 character code-like identifier"
11410                    .to_owned(),
11411            );
11412        }
11413        let meta_key = format!("localRebootstrap:{}", input.rebootstrap_id);
11414        if let Some(persisted) = meta_get(&self.conn, &meta_key).map_err(|_| {
11415            "sync.local_corrupt: persisted local rebootstrap receipt is unreadable".to_owned()
11416        })? {
11417            let (retained_commits, reset_subscriptions) =
11418                decode_local_rebootstrap_receipt(&persisted)?;
11419            return Ok(LocalDataRebootstrapResult {
11420                already_applied: true,
11421                retained_commits,
11422                reset_subscriptions,
11423            });
11424        }
11425        if self.stopped || self.schema_floor.is_some() {
11426            return Err(
11427                "sync.invalid_request: local rebootstrap cannot bypass an active schema-floor stop; update the application first"
11428                    .to_owned(),
11429            );
11430        }
11431
11432        let retained_commits = self.outbox.len();
11433        let reset_subscriptions = self.stored(load_units(&self.conn))?.len();
11434        let receipt = encode_local_rebootstrap_receipt(retained_commits, reset_subscriptions)?;
11435        let rollback = self.capture();
11436        self.begin_observation("syncular_local_rebootstrap")?;
11437        let mut batch = ChangeAccumulator::default();
11438        let applied = (|| -> Result<(), String> {
11439            self.run_schema_reset_observed(&mut batch, false)?;
11440            self.conn
11441                .execute(
11442                    "INSERT INTO _syncular_meta(key, value) VALUES (?1, ?2)",
11443                    rusqlite::params![meta_key, receipt],
11444                )
11445                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11446            self.sync_needed = true;
11447            self.sync_intent_queue.push_back(SyncIntent::Interactive);
11448            batch.status = true;
11449            Ok(())
11450        })()
11451        .and_then(|()| self.finish_observation("syncular_local_rebootstrap", batch));
11452        if let Err(error) = applied {
11453            self.rollback_observation("syncular_local_rebootstrap");
11454            self.restore(rollback);
11455            return Err(error);
11456        }
11457
11458        Ok(LocalDataRebootstrapResult {
11459            already_applied: false,
11460            retained_commits,
11461            reset_subscriptions,
11462        })
11463    }
11464
11465    // -- scope purge + doomed outbox (§3.3) ----------------------------------------
11466
11467    /// Delete base and visible rows owned by the effective scopes
11468    /// ([`local_scope_predicate`]); `Ok(false)` = no local scope-column
11469    /// mapping for a key (the fail-closed case). A SQLite failure is an error.
11470    fn purge_scope_rows(
11471        &mut self,
11472        table_name: &str,
11473        effective: &[(String, Vec<String>)],
11474        owners: &[Vec<(String, Vec<String>)>],
11475    ) -> Result<bool, String> {
11476        let Some(table) = self.schema.table(table_name) else {
11477            return Ok(false);
11478        };
11479        let Ok(predicate) = local_scope_predicate(table, effective, owners) else {
11480            return Ok(false);
11481        };
11482        let Some((predicate, params)) = predicate else {
11483            return Ok(true);
11484        };
11485        self.overlay_dirty.table(table_name);
11486        for source in [base_table(table_name), visible_table(table_name)] {
11487            self.conn
11488                .execute(
11489                    &format!("DELETE FROM {source} WHERE {predicate}"),
11490                    rusqlite::params_from_iter(&params),
11491                )
11492                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11493        }
11494        Ok(true)
11495    }
11496
11497    /// §3.3: drop pending commits whose upserts provably land in the
11498    /// revoked effective scopes and no other owner covers (row ownership)
11499    /// — whole-commit, never per-operation.
11500    fn drop_doomed_outbox(
11501        &mut self,
11502        table: &TableSchema,
11503        effective: &[(String, Vec<String>)],
11504        owners: &[Vec<(String, Vec<String>)>],
11505    ) -> Result<bool, String> {
11506        let doomed: Vec<_> =
11507            self.outbox
11508                .iter()
11509                .filter(|commit| {
11510                    commit.ops.iter().any(|op| {
11511                        op.upsert
11512                            && op.table == table.name
11513                            && op.values.as_ref().is_some_and(|values| {
11514                                owns_local_row(table, effective, owners, values)
11515                            })
11516                    })
11517                })
11518                .cloned()
11519                .collect();
11520        if doomed.is_empty() {
11521            return Ok(false);
11522        }
11523        let mut rejections = Vec::new();
11524        for commit in &doomed {
11525            let results = commit
11526                .ops
11527                .iter()
11528                .enumerate()
11529                .map(|(op_index, operation)| {
11530                    self.overlay_dirty.table(&operation.table);
11531                    let rejection = RejectionRecord {
11532                        client_commit_id: commit.client_commit_id.clone(),
11533                        op_index: op_index as i32,
11534                        code: "sync.scope_revoked".to_owned(),
11535                        message: "the commit was dropped because its effective scope was revoked"
11536                            .to_owned(),
11537                        retryable: false,
11538                        details: None,
11539                        operation: Some(CommitOperation::from(operation)),
11540                    };
11541                    rejections.push(rejection.clone());
11542                    CommitOperationOutcome::Error { rejection }
11543                })
11544                .collect::<Vec<_>>();
11545            self.persist_commit_outcome(
11546                &commit.client_commit_id,
11547                CommitOutcomeStatus::Rejected,
11548                &results,
11549                Some(&commit.ops),
11550            )?;
11551            self.delete_outbox_persisted(&commit.client_commit_id)?;
11552        }
11553        self.prune_commit_outcomes()?;
11554        let doomed_ids = doomed
11555            .iter()
11556            .map(|commit| commit.client_commit_id.as_str())
11557            .collect::<BTreeSet<_>>();
11558        self.outbox
11559            .retain(|commit| !doomed_ids.contains(commit.client_commit_id.as_str()));
11560        self.rejections.extend(rejections);
11561        Ok(true)
11562    }
11563
11564    // -- blobs (§5.9) ----------------------------------------------------------------
11565
11566    /// §5.9.7: hash bytes into the content address, cache them, queue the
11567    /// upload (flushed before the next push, B4). Returns the canonical
11568    /// BlobRef JSON `{blobId, byteLength, mediaType?}` for a `blob_ref`
11569    /// column value.
11570    pub fn upload_blob(
11571        &mut self,
11572        bytes: &[u8],
11573        media_type: Option<String>,
11574        name: Option<String>,
11575    ) -> Result<Value, String> {
11576        let blob_id = blob_id_for(bytes);
11577        let now = self.clock_now_ms();
11578        let tx = self
11579            .conn
11580            .transaction()
11581            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11582        tx.execute(
11583            "INSERT INTO _syncular_blobs(blob_id, bytes, byte_length, media_type, created_at_ms) VALUES (?,?,?,?,?)
11584             ON CONFLICT(blob_id) DO NOTHING",
11585            rusqlite::params![blob_id, bytes, bytes.len() as i64, media_type, now],
11586        )
11587        .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11588        tx.execute(
11589            "INSERT OR IGNORE INTO _syncular_blob_uploads(blob_id, media_type, created_at_ms) VALUES (?,?,?)",
11590            rusqlite::params![blob_id, media_type, now],
11591        )
11592        .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11593        tx.commit()
11594            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11595        self.enforce_blob_cache_cap();
11596        let mut obj = Map::new();
11597        obj.insert("blobId".to_owned(), Value::from(blob_id));
11598        obj.insert("byteLength".to_owned(), Value::from(bytes.len() as i64));
11599        if let Some(mt) = media_type {
11600            obj.insert("mediaType".to_owned(), Value::from(mt));
11601        }
11602        if let Some(n) = name {
11603            obj.insert("name".to_owned(), Value::from(n));
11604        }
11605        Ok(Value::Object(obj))
11606    }
11607
11608    /// §5.9.7: resolve an owned blob body without driver encoding. A
11609    /// content-addressed cache hit serves with no fetch (B1); a miss downloads
11610    /// (§5.9.5), verifies the address, caches the bytes, and returns them.
11611    pub fn fetch_blob_bytes(
11612        &mut self,
11613        transport: &mut dyn Transport,
11614        blob_id_or_ref: &str,
11615    ) -> Result<FetchedBlob, (String, String)> {
11616        let simple = |m: String| ("client.failed".to_owned(), m);
11617        // A BlobRef declares the size that caps the download (§5.9.5); a bare
11618        // blobId declares none.
11619        let (blob_id, byte_length) = if blob_id_or_ref.starts_with("sha256:") {
11620            (blob_id_or_ref.to_owned(), None)
11621        } else {
11622            let value: Value = serde_json::from_str(blob_id_or_ref)
11623                .map_err(|_| simple("blob ref is not JSON".to_owned()))?;
11624            (
11625                value
11626                    .get("blobId")
11627                    .and_then(Value::as_str)
11628                    .ok_or_else(|| simple("blob ref has no blobId".to_owned()))?
11629                    .to_owned(),
11630                value.get("byteLength").and_then(Value::as_u64),
11631            )
11632        };
11633        if let Some(cached) = self.get_cached_blob_bytes(&blob_id).map_err(simple)? {
11634            return Ok(cached);
11635        }
11636        if !self.transport_enabled {
11637            return Err(("sync.offline".into(), "transport gate is closed".into()));
11638        }
11639        // §5.9.5: propagate the server's blob.* code (blob.forbidden /
11640        // blob.not_found) verbatim so the harness can assert on it. The
11641        // authorized endpoint serves bytes inline OR (always-issue, presign
11642        // configured) a signed url the client fetches directly — no host auth,
11643        // no fall-through: failure => re-request (the caller's next fetch_blob_bytes).
11644        #[cfg(feature = "bench-internals")]
11645        let download_phase = self.benchmark_phases.start(Phase::BlobDownload);
11646        let bytes = match transport
11647            .blob_download(&blob_id, byte_length)
11648            .map_err(|e| (e.code, e.message))?
11649        {
11650            BlobDownload::Bytes(bytes) => bytes,
11651            BlobDownload::Url {
11652                url,
11653                url_expires_at_ms,
11654            } => {
11655                // §5.9.5: MUST NOT start a fetch at/past expiry.
11656                if url_expires_at_ms.is_some_and(|exp| exp <= self.clock_now_ms()) {
11657                    return Err((
11658                        "sync.segment_expired".to_owned(),
11659                        format!(
11660                            "blob url for {blob_id} expired before fetch — re-request mints a fresh url (§5.9.5)"
11661                        ),
11662                    ));
11663                }
11664                transport
11665                    .fetch_blob_url(&url, byte_length)
11666                    .map_err(|e| (e.code, e.message))?
11667            }
11668        };
11669        #[cfg(feature = "bench-internals")]
11670        drop(download_phase);
11671        #[cfg(feature = "bench-internals")]
11672        let validate_phase = self.benchmark_phases.start(Phase::BlobValidate);
11673        // §5.9.5 inherits §5.1: the declared size caps the body before the
11674        // content address is verified.
11675        if byte_length.is_some_and(|cap| bytes.len() as u64 > cap) {
11676            return Err((
11677                "sync.invalid_request".to_owned(),
11678                "blob exceeds its byteLength".to_owned(),
11679            ));
11680        }
11681        if blob_id_for(&bytes) != blob_id {
11682            return Err(simple(format!(
11683                "blob content address mismatch for {blob_id}"
11684            )));
11685        }
11686        #[cfg(feature = "bench-internals")]
11687        drop(validate_phase);
11688        #[cfg(feature = "bench-internals")]
11689        let insert_phase = self.benchmark_phases.start(Phase::BlobCacheInsert);
11690        let now = self.clock_now_ms();
11691        self.conn
11692            .execute(
11693                "INSERT OR IGNORE INTO _syncular_blobs(blob_id, bytes, byte_length, media_type, created_at_ms) VALUES (?,?,?,NULL,?)",
11694                rusqlite::params![blob_id, bytes, bytes.len() as i64, now],
11695            )
11696            .map_err(|e| simple(e.to_string()))?;
11697        self.conn
11698            .query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |_| Ok(()))
11699            .map_err(|e| simple(e.to_string()))?;
11700        self.enforce_blob_cache_cap();
11701        #[cfg(feature = "bench-internals")]
11702        drop(insert_phase);
11703        self.get_cached_blob_bytes(&blob_id)
11704            .map_err(simple)?
11705            .ok_or_else(|| simple("blob cache write failed".to_owned()))
11706    }
11707
11708    fn get_cached_blob_bytes(&self, blob_id: &str) -> Result<Option<FetchedBlob>, String> {
11709        #[cfg(feature = "bench-internals")]
11710        let _phase = self.benchmark_phases.start(Phase::BlobCacheRead);
11711        let mut stmt = self
11712            .conn
11713            .prepare("SELECT bytes, byte_length, media_type FROM _syncular_blobs WHERE blob_id = ?")
11714            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11715        let mut rows = stmt
11716            .query(rusqlite::params![blob_id])
11717            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11718        if let Some(row) = rows
11719            .next()
11720            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
11721        {
11722            let bytes: Vec<u8> = row
11723                .get(0)
11724                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11725            let byte_length: i64 = row
11726                .get(1)
11727                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11728            let media_type: Option<String> = row
11729                .get(2)
11730                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11731            return Ok(Some(FetchedBlob {
11732                blob_id: blob_id.to_owned(),
11733                byte_length,
11734                bytes,
11735                media_type,
11736            }));
11737        }
11738        Ok(None)
11739    }
11740
11741    /// §5.9.7 B4: upload every queued blob before push.
11742    fn pending_blob_uploads(&self) -> Result<Vec<crate::round::PendingUpload>, TransportError> {
11743        let pending: Vec<(String, Option<String>)> = {
11744            let mut stmt = self
11745                .conn
11746                .prepare(
11747                    "SELECT blob_id, media_type FROM (
11748                      SELECT blob_id, media_type, created_at_ms FROM _syncular_blob_uploads
11749                      UNION ALL
11750                      SELECT DISTINCT r.blob_id, b.media_type, 9223372036854775807 AS created_at_ms
11751                      FROM _syncular_blob_commit_refs r LEFT JOIN _syncular_blobs b ON b.blob_id = r.blob_id
11752                      WHERE NOT EXISTS (SELECT 1 FROM _syncular_blob_uploads u WHERE u.blob_id = r.blob_id)
11753                    ) ORDER BY created_at_ms, blob_id",
11754                )
11755                .map_err(|e| TransportError::new("client.failed", e.to_string()))?;
11756            let rows = stmt
11757                .query_map([], |row| {
11758                    Ok((row.get::<_, String>(0)?, row.get::<_, Option<String>>(1)?))
11759                })
11760                .map_err(|e| TransportError::new("client.failed", e.to_string()))?;
11761            rows.collect::<Result<_, _>>()
11762                .map_err(|error| match error {
11763                    rusqlite::Error::InvalidColumnType(..)
11764                    | rusqlite::Error::FromSqlConversionFailure(..) => TransportError::new(
11765                        "sync.local_corrupt",
11766                        "Pending blob upload metadata is invalid",
11767                    ),
11768                    _ => TransportError::new("client.failed", error.to_string()),
11769                })?
11770        };
11771        let mut uploads = Vec::new();
11772        for (blob_id, media_type) in pending {
11773            let bytes = (|| {
11774                let stored: Option<(SqlValue, SqlValue)> = self
11775                    .conn
11776                    .query_row(
11777                        "SELECT bytes, byte_length FROM _syncular_blobs WHERE blob_id = ?",
11778                        rusqlite::params![blob_id],
11779                        |row| Ok((row.get(0)?, row.get(1)?)),
11780                    )
11781                    .optional()
11782                    .map_err(|e| TransportError::new("client.failed", e.to_string()))?;
11783                let Some((SqlValue::Blob(bytes), SqlValue::Integer(byte_length))) = stored else {
11784                    return Err(TransportError::new(
11785                        "sync.local_corrupt",
11786                        "Pending blob upload body is missing or corrupt",
11787                    ));
11788                };
11789                if byte_length != bytes.len() as i64 || blob_id_for(&bytes) != blob_id {
11790                    return Err(TransportError::new(
11791                        "sync.local_corrupt",
11792                        "Pending blob upload body is missing or corrupt",
11793                    ));
11794                }
11795                Ok(bytes)
11796            })();
11797            let failed = bytes.is_err();
11798            uploads.push(crate::round::PendingUpload {
11799                id: blob_id,
11800                bytes,
11801                media_type,
11802            });
11803            if failed {
11804                break;
11805            }
11806        }
11807        Ok(uploads)
11808    }
11809
11810    fn visible_blob_ids_sql(&self) -> String {
11811        let mut selects = Vec::new();
11812        for table in &self.schema.tables {
11813            for column in table
11814                .columns
11815                .iter()
11816                .filter(|column| column.ty == ColumnType::BlobRef)
11817            {
11818                let identifier = quote_ident(&column.name);
11819                selects.push(format!(
11820                    "SELECT json_extract({identifier}, '$.blobId') AS blob_id FROM {} \
11821                     WHERE typeof({identifier}) = 'text' AND json_valid({identifier}) \
11822                     AND json_type({identifier}, '$.blobId') = 'text'",
11823                    quote_ident(&table.name),
11824                ));
11825            }
11826        }
11827        if selects.is_empty() {
11828            "SELECT NULL AS blob_id WHERE 0".to_owned()
11829        } else {
11830            selects.join(" UNION ")
11831        }
11832    }
11833
11834    /// §5.9.7 B1 size cap. Visible rows, pending commits, and
11835    /// staged uploads pin bodies directly; no derived refcount is stored.
11836    fn enforce_blob_cache_cap(&self) {
11837        let Some(max_bytes) = self.limits.blob_cache_max_bytes else {
11838            return;
11839        };
11840        let mut total: i64 = self
11841            .conn
11842            .query_row(
11843                "SELECT COALESCE(SUM(byte_length), 0) FROM _syncular_blobs",
11844                [],
11845                |row| row.get(0),
11846            )
11847            .unwrap_or(0);
11848        if total <= max_bytes {
11849            return;
11850        }
11851        let query = format!(
11852            "SELECT blob_id, byte_length FROM _syncular_blobs
11853             WHERE blob_id NOT IN (SELECT blob_id FROM _syncular_blob_uploads)
11854               AND blob_id NOT IN (SELECT blob_id FROM _syncular_blob_commit_refs)
11855               AND blob_id NOT IN ({})
11856             ORDER BY created_at_ms ASC, blob_id ASC",
11857            self.visible_blob_ids_sql(),
11858        );
11859        let candidates: Vec<(String, i64)> = {
11860            let Ok(mut stmt) = self.conn.prepare(&query) else {
11861                return;
11862            };
11863            let Ok(rows) = stmt.query_map([], |row| {
11864                Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
11865            }) else {
11866                return;
11867            };
11868            rows.filter_map(Result::ok).collect()
11869        };
11870        for (blob_id, byte_length) in candidates {
11871            if total <= max_bytes {
11872                break;
11873            }
11874            let _ = self.conn.execute(
11875                "DELETE FROM _syncular_blobs WHERE blob_id = ?",
11876                rusqlite::params![blob_id],
11877            );
11878            total -= byte_length;
11879        }
11880    }
11881
11882    fn delete_unreferenced_cached_blobs(&mut self) -> Result<(), String> {
11883        #[cfg(feature = "bench-internals")]
11884        let _phase = self.benchmark_phases.start(Phase::BlobRetention);
11885        if !self.schema_has_blobs() {
11886            return Ok(());
11887        }
11888        self.rebuild_overlay_if_dirty()?;
11889        self.conn
11890            .execute(
11891                &format!(
11892                    "DELETE FROM _syncular_blobs
11893                 WHERE blob_id NOT IN (SELECT blob_id FROM _syncular_blob_uploads)
11894                   AND blob_id NOT IN (SELECT blob_id FROM _syncular_blob_commit_refs)
11895                   AND blob_id NOT IN ({})",
11896                    self.visible_blob_ids_sql(),
11897                ),
11898                [],
11899            )
11900            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11901        Ok(())
11902    }
11903
11904    // -- local row storage ----------------------------------------------------------
11905
11906    /// The cached per-table primary-key upsert SQL for `full_table` (see
11907    /// the `insert_sql` field: built once, reused per row).
11908    fn insert_row_sql(&self, full_table: &str, table: &crate::schema::TableSchema) -> String {
11909        if let Some(sql) = self.insert_sql.borrow().get(full_table) {
11910            return sql.clone();
11911        }
11912        let mut columns: Vec<String> = table.columns.iter().map(|c| quote_ident(&c.name)).collect();
11913        columns.push(quote_ident("_syncular_version"));
11914        columns.extend(table.sealed_columns().iter().map(|name| quote_ident(name)));
11915        let placeholders: Vec<&str> = columns.iter().map(|_| "?").collect();
11916        let primary_key = quote_ident(&table.primary_key);
11917        let updates = columns
11918            .iter()
11919            .filter(|column| **column != primary_key)
11920            .map(|column| format!("{column}=excluded.{column}"))
11921            .collect::<Vec<_>>()
11922            .join(", ");
11923        // §4.5 version guard: server state lands in the base table only above
11924        // the stored version; the visible overlay takes every write.
11925        let guard = if full_table == base_table(&table.name) {
11926            format!(" WHERE excluded._syncular_version > {full_table}._syncular_version")
11927        } else {
11928            String::new()
11929        };
11930        let sql = format!(
11931            "INSERT INTO {full_table} ({}) VALUES ({}) ON CONFLICT ({primary_key}) DO UPDATE SET {updates}{guard}",
11932            columns.join(", "),
11933            placeholders.join(", ")
11934        );
11935        self.insert_sql
11936            .borrow_mut()
11937            .insert(full_table.to_owned(), sql.clone());
11938        sql
11939    }
11940
11941    /// §4.5: write a server row under the version guard; returns whether it
11942    /// landed. `sealed` is its §5.11 sealed state, `None` for an unsealed row.
11943    fn write_base_row(
11944        &self,
11945        table_name: &str,
11946        row: &Row,
11947        version: i64,
11948        sealed: Option<&SealedRow>,
11949    ) -> Result<bool, String> {
11950        let landed = self.write_row(&base_table(table_name), table_name, row, version, sealed)?;
11951        if landed {
11952            self.overlay_dirty.table(table_name);
11953        }
11954        Ok(landed)
11955    }
11956
11957    fn write_row(
11958        &self,
11959        full_table: &str,
11960        table_name: &str,
11961        row: &Row,
11962        version: i64,
11963        sealed: Option<&SealedRow>,
11964    ) -> Result<bool, String> {
11965        #[cfg(feature = "bench-internals")]
11966        let _phase = self.benchmark_phases.start(Phase::RowWrite);
11967        let table = self
11968            .schema
11969            .table(table_name)
11970            .ok_or_else(|| format!("unknown table {table_name:?}"))?;
11971        let sql = self.insert_row_sql(full_table, table);
11972        let mut stmt = self
11973            .conn
11974            .prepare_cached(&sql)
11975            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
11976        let params = row
11977            .iter()
11978            .map(RowParam::Cell)
11979            .chain(std::iter::once(RowParam::Version(version)))
11980            .chain(
11981                [
11982                    sealed.map(|sealed| sealed.key_id.as_str()),
11983                    sealed.map(|sealed| sealed.envelopes.as_str()),
11984                ]
11985                .into_iter()
11986                .take(table.sealed_columns().len())
11987                .map(RowParam::Text),
11988            );
11989        stmt.execute(rusqlite::params_from_iter(params))
11990            .map(|changed| changed > 0)
11991            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
11992    }
11993
11994    /// §4.5: delete a server row whose stored version lies below `version`;
11995    /// returns whether a row left.
11996    fn delete_base_row(
11997        &self,
11998        table_name: &str,
11999        row_id: &str,
12000        version: i64,
12001    ) -> Result<bool, String> {
12002        let table = self
12003            .schema
12004            .table(table_name)
12005            .ok_or_else(|| format!("unknown table {table_name:?}"))?;
12006        let sql = format!(
12007            "DELETE FROM {} WHERE {} AND _syncular_version < ?2",
12008            base_table(table_name),
12009            row_id_predicate(table)
12010        );
12011        let mut stmt = self
12012            .conn
12013            .prepare_cached(&sql)
12014            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12015        let landed = stmt
12016            .execute(rusqlite::params![row_id, version])
12017            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
12018            > 0;
12019        if landed {
12020            self.overlay_dirty.table(table_name);
12021        }
12022        Ok(landed)
12023    }
12024
12025    fn has_acknowledged_rows(&self) -> Result<bool, String> {
12026        self.conn
12027            .query_row(
12028                "SELECT EXISTS(SELECT 1 FROM _syncular_acked_rows)",
12029                [],
12030                |row| row.get(0),
12031            )
12032            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
12033    }
12034
12035    fn acknowledged_operations(
12036        &self,
12037        rows: Option<&HashSet<(&str, &str)>>,
12038        tables: Option<&BTreeSet<&str>>,
12039    ) -> Result<Vec<OutboxOp>, String> {
12040        let keys = rows.map(|rows| serde_json::to_string(&rows).expect("row keys"));
12041        let mut statement = self
12042            .conn
12043            .prepare_cached(if keys.is_some() {
12044                ACKED_ROWS_BY_ROW
12045            } else {
12046                "SELECT op_json,intent_json FROM _syncular_acked_rows WHERE ?1 IS NULL AND (?2 IS NULL OR tbl IN(SELECT value FROM json_each(?2))) ORDER BY commit_seq,idx"
12047            })
12048            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12049        let rows = statement
12050            .query_map(
12051                rusqlite::params![
12052                    keys,
12053                    tables.map(|tables| serde_json::to_string(tables).expect("table keys"))
12054                ],
12055                |row| {
12056                    #[cfg(test)]
12057                    self.acknowledged_replay_count
12058                        .set(self.acknowledged_replay_count.get() + 1);
12059                    Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
12060                },
12061            )
12062            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12063        rows.map(|row| {
12064            let (op, intent) =
12065                row.map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12066            let op: CommitOperation =
12067                serde_json::from_str(&op).map_err(|_| "sync.local_corrupt".to_owned())?;
12068            let table = self
12069                .schema
12070                .table(&op.table)
12071                .ok_or_else(|| "sync.unknown_table".to_owned())?;
12072            let values = if op.op == "upsert" && self.visible_row(table, &op.row_id)?.is_none() {
12073                Some(serde_json::from_str(&intent).map_err(|_| "sync.local_corrupt".to_owned())?)
12074            } else {
12075                op.values.clone()
12076            };
12077            Ok(OutboxOp {
12078                values,
12079                ..op.into()
12080            })
12081        })
12082        .collect()
12083    }
12084
12085    fn drop_acknowledged_rows(
12086        &self,
12087        matches: impl Fn(&str, &Map<String, Value>) -> bool,
12088    ) -> Result<Vec<OutboxCommit>, String> {
12089        let mut statement = self
12090            .conn
12091            .prepare("SELECT commit_id,tbl,intent_json,id FROM _syncular_acked_rows")
12092            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12093        let rows = statement
12094            .query_map([], |row| {
12095                Ok((
12096                    row.get::<_, String>(0)?,
12097                    row.get::<_, String>(1)?,
12098                    row.get::<_, String>(2)?,
12099                    row.get::<_, String>(3)?,
12100                ))
12101            })
12102            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12103        let mut doomed = HashSet::new();
12104        for row in rows {
12105            let (id, table, intent, row_id) =
12106                row.map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12107            let values =
12108                serde_json::from_str(&intent).map_err(|_| "sync.local_corrupt".to_owned())?;
12109            let base_matches = self
12110                .schema
12111                .table(&table)
12112                .map(|schema| self.stored_row(schema, &row_id, true))
12113                .transpose()?
12114                .flatten()
12115                .is_some_and(|(base, _)| {
12116                    let schema = self.schema.table(&table).expect("acknowledged table");
12117                    let values = schema
12118                        .columns
12119                        .iter()
12120                        .zip(base.iter())
12121                        .map(|(column, value)| (column.name.clone(), column_value_to_json(value)))
12122                        .collect();
12123                    matches(&table, &values)
12124                });
12125            if matches(&table, &values) || base_matches {
12126                doomed.insert(id);
12127            }
12128        }
12129        let mut dropped = Vec::new();
12130        for id in doomed {
12131            let mut statement = self
12132                .conn
12133                .prepare("SELECT op_json FROM _syncular_acked_rows WHERE commit_id=?1 ORDER BY idx")
12134                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12135            let operations = statement
12136                .query_map([&id], |row| row.get::<_, String>(0))
12137                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12138            let mut ops = Vec::new();
12139            for operation in operations {
12140                let json = operation
12141                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12142                let op: CommitOperation =
12143                    serde_json::from_str(&json).map_err(|_| "sync.local_corrupt".to_owned())?;
12144                self.overlay_dirty.table(&op.table);
12145                ops.push(OutboxOp {
12146                    table: op.table,
12147                    row_id: op.row_id,
12148                    upsert: op.op == "upsert",
12149                    base_version: op.base_version,
12150                    values: op.values,
12151                    insert: op.insert,
12152                });
12153            }
12154            dropped.push(OutboxCommit {
12155                client_commit_id: id.clone(),
12156                ops,
12157            });
12158            self.conn
12159                .execute("DELETE FROM _syncular_acked_rows WHERE commit_id=?1", [id])
12160                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12161        }
12162        Ok(dropped)
12163    }
12164
12165    /// [`Self::rebuild_overlay`], skipped when neither the base tables nor
12166    /// the outbox changed since the last rebuild (the no-op sync round).
12167    fn rebuild_overlay_if_dirty(&mut self) -> Result<(), String> {
12168        if self.overlay_dirty.get() {
12169            self.rebuild_overlay()?;
12170        }
12171        Ok(())
12172    }
12173
12174    /// §7.1: local reads see outbox state applied optimistically — rebuild
12175    /// every visible table as (base server state) + (pending outbox replay
12176    /// on top). Optimistic rows carry version `-1`.
12177    fn rebuild_overlay(&mut self) -> Result<(), String> {
12178        #[cfg(feature = "bench-internals")]
12179        let _phase = self.benchmark_phases.start(Phase::OverlayRebuild);
12180        #[cfg(test)]
12181        self.overlay_rebuild_count
12182            .set(self.overlay_rebuild_count.get() + 1);
12183        let dirty = self.overlay_dirty.snapshot();
12184        let tables: Vec<_> = self
12185            .schema
12186            .tables
12187            .iter()
12188            .filter(|table| {
12189                dirty
12190                    .as_ref()
12191                    .is_none_or(|names| names.is_empty() || names.contains(&table.name))
12192            })
12193            .cloned()
12194            .collect();
12195        let names: BTreeSet<_> = tables.iter().map(|table| table.name.as_str()).collect();
12196        self.begin_observation("syncular_overlay")?;
12197        let rebuilt = (|| {
12198            for table in &tables {
12199                for index in &table.fts_indexes {
12200                    self.drop_fts_triggers(index)?;
12201                }
12202                let visible = visible_table(&table.name);
12203                let base = base_table(&table.name);
12204                self.conn
12205                    .execute_batch(&format!(
12206                        "DELETE FROM {visible}; INSERT INTO {visible} SELECT * FROM {base}"
12207                    ))
12208                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12209            }
12210            self.replay_overlay(&names, None)?;
12211            for table in &tables {
12212                for index in &table.fts_indexes {
12213                    self.rebuild_fts_projection(table, index)?;
12214                    self.create_fts_triggers(table, index)?;
12215                }
12216            }
12217            self.conn
12218                .execute_batch("RELEASE syncular_overlay")
12219                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12220            Ok(())
12221        })();
12222        if rebuilt.is_err() {
12223            self.conn
12224                .execute_batch("ROLLBACK TO syncular_overlay; RELEASE syncular_overlay")
12225                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12226        } else {
12227            self.overlay_dirty.set(false);
12228        }
12229        rebuilt
12230    }
12231
12232    /// §7.1 replay of acknowledged, retained failed and pending intent, in
12233    /// that order, for the operations of `tables` on `rows` (every row for
12234    /// `None`) over their current visible state.
12235    fn replay_overlay(
12236        &self,
12237        tables: &BTreeSet<&str>,
12238        rows: Option<&HashSet<(&str, &str)>>,
12239    ) -> Result<(), String> {
12240        let replays = |op: &&OutboxOp| {
12241            tables.contains(op.table.as_str())
12242                && rows.is_none_or(|rows| rows.contains(&(op.table.as_str(), op.row_id.as_str())))
12243        };
12244        self.apply_outbox_ops(&self.acknowledged_operations(rows, Some(tables))?)?;
12245        for (commit, initial) in self.failed_commits.iter() {
12246            for operation in commit.ops.iter().filter(replays) {
12247                let Some(table) = self.schema.table(&operation.table) else {
12248                    continue;
12249                };
12250                if operation.upsert
12251                    && self.visible_row(table, &operation.row_id)?.is_none()
12252                    && operation.presented_values(table).is_some_and(|values| {
12253                        table
12254                            .columns
12255                            .iter()
12256                            .all(|column| values.contains_key(&column.name))
12257                    })
12258                {
12259                    let key = serde_json::to_string(&(&operation.table, &operation.row_id))
12260                        .expect("row key serialization");
12261                    if let Some(Value::Object(values)) = initial.get(&key) {
12262                        self.apply_outbox_ops(&[OutboxOp {
12263                            values: Some(values.clone()),
12264                            ..operation.clone()
12265                        }])?;
12266                    }
12267                }
12268            }
12269            self.apply_outbox_ops(commit.ops.iter().filter(replays))?;
12270        }
12271        self.apply_outbox_ops(
12272            self.outbox
12273                .iter()
12274                .flat_map(|commit| &commit.ops)
12275                .filter(replays),
12276        )
12277    }
12278
12279    /// §7.1 replay: every operation is applied over the current visible
12280    /// state. An operation that no longer applies (a server row now holds its
12281    /// unique value) stays pending and invisible until a later replay admits
12282    /// it or the server answers its push.
12283    fn apply_outbox_ops<'a>(
12284        &self,
12285        ops: impl IntoIterator<Item = &'a OutboxOp>,
12286    ) -> Result<(), String> {
12287        #[cfg(feature = "bench-internals")]
12288        let mut phase = self.benchmark_phases.start(Phase::PendingReplay);
12289        for op in ops {
12290            #[cfg(feature = "bench-internals")]
12291            if let Some(phase) = &mut phase {
12292                phase.unit();
12293            }
12294            if op.upsert {
12295                if let Some((table, values)) = self
12296                    .schema
12297                    .table(&op.table)
12298                    .filter(|table| table.indexes.iter().any(|index| index.unique))
12299                    .and_then(|table| Some((table, op.presented_values(table)?)))
12300                {
12301                    let mut intended: Map<String, Value> = self
12302                        .visible_row(table, &op.row_id)?
12303                        .map(|(row, _)| {
12304                            table
12305                                .columns
12306                                .iter()
12307                                .zip(&row)
12308                                .map(|(column, value)| {
12309                                    (column.name.clone(), column_value_to_json(value))
12310                                })
12311                                .collect()
12312                        })
12313                        .unwrap_or_default();
12314                    intended.extend(values.into_owned());
12315                    if !self.unique_conflicts(table, &intended, false)?.is_empty() {
12316                        continue;
12317                    }
12318                }
12319            }
12320            self.apply_outbox_op(op)?;
12321        }
12322        Ok(())
12323    }
12324
12325    fn apply_outbox_op(&self, op: &OutboxOp) -> Result<(), String> {
12326        let Some(table) = self.schema.table(&op.table) else {
12327            return Ok(());
12328        };
12329        let visible = visible_table(&table.name);
12330        if !op.upsert {
12331            let sql = format!("DELETE FROM {visible} WHERE {}", row_id_predicate(table));
12332            self.conn
12333                .prepare_cached(&sql)
12334                .and_then(|mut statement| statement.execute(rusqlite::params![op.row_id]))
12335                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12336            return Ok(());
12337        }
12338        // §7.1: an insert-intent operation that lacks a non-nullable column
12339        // cannot encode; it stays invisible until the send-time prepass drops
12340        // it as sync.outbox_incompatible (§7.4.4).
12341        let Some(values) = op.presented_values(table) else {
12342            return Ok(());
12343        };
12344        // §7.1: the overlay applies the operation's PRESENT columns
12345        // over the current local row. A partial operation over an
12346        // absent local row leaves it absent — the server answers it
12347        // with sync.row_deleted or sync.row_missing (§6.2). An
12348        // insert-intent operation presents every column.
12349        let full = table
12350            .columns
12351            .iter()
12352            .all(|column| values.contains_key(&column.name));
12353        let local = self.visible_row(table, &op.row_id)?;
12354        if local.is_none() && !full {
12355            return Ok(());
12356        }
12357        let mut row: Row = Vec::with_capacity(table.columns.len());
12358        for (index, column) in table.columns.iter().enumerate() {
12359            let incoming = match values.get(&column.name) {
12360                Some(raw) => json_to_column_value(
12361                    if raw.get("$bytes").is_some()
12362                        && table
12363                            .encrypted_columns
12364                            .iter()
12365                            .any(|encrypted| encrypted.index == index)
12366                    {
12367                        &table.wire_columns[index]
12368                    } else {
12369                        column
12370                    },
12371                    Some(raw),
12372                ),
12373                None => Ok(local
12374                    .as_ref()
12375                    .and_then(|(values, _)| values.get(index).cloned())
12376                    .flatten()),
12377            };
12378            row.push(incoming.map_err(|_| {
12379                "sync.local_corrupt: persisted optimistic row value is invalid".to_owned()
12380            })?);
12381        }
12382        let version = local.as_ref().map_or(-1, |(_, version)| *version);
12383        self.write_row(&visible, &table.name, &row, version, None)
12384            .map(|_| ())
12385            .map_err(|error| {
12386                if error.starts_with("UNIQUE constraint failed:") {
12387                    "sync.constraint_violation: local write violates a unique constraint".to_owned()
12388                } else {
12389                    error
12390                }
12391            })
12392    }
12393
12394    /// Whether the row is present in the overlay. Existence reads only the
12395    /// key: locked encrypted values stay opaque, and decoding the complete row
12396    /// would reject a valid plain write.
12397    fn visible_row_exists(&self, table: &TableSchema, row_id: &str) -> Result<bool, String> {
12398        self.conn
12399            .query_row(
12400                &format!(
12401                    "SELECT EXISTS(SELECT 1 FROM {} WHERE {})",
12402                    visible_table(&table.name),
12403                    row_id_predicate(table)
12404                ),
12405                rusqlite::params![row_id],
12406                |row| row.get(0),
12407            )
12408            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))
12409    }
12410
12411    /// §5.11: a sealed row is read-only; a write could otherwise send NULL
12412    /// over ciphertext this device cannot see.
12413    fn refuse_sealed_row(&self, table: &TableSchema, row_id: &str) -> Result<(), ClientError> {
12414        if !table.has_encrypted_columns() {
12415            return Ok(());
12416        }
12417        let key_id: Option<String> = self
12418            .conn
12419            .query_row(
12420                &format!(
12421                    "SELECT \"_sync_sealed_key\" FROM {} WHERE {}",
12422                    visible_table(&table.name),
12423                    row_id_predicate(table)
12424                ),
12425                rusqlite::params![row_id],
12426                |row| row.get(0),
12427            )
12428            .optional()
12429            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
12430            .flatten();
12431        match key_id {
12432            None => Ok(()),
12433            Some(key_id) => Err(ClientError {
12434                code: "client.encryption_key_missing".to_owned(),
12435                message: "the row is sealed under a key this device does not hold".to_owned(),
12436                details: Some(json!({ "table": table.name, "keyId": key_id })),
12437                retryable: false,
12438            }),
12439        }
12440    }
12441
12442    /// §5.11 reveal: decrypt every sealed base row whose envelope keys the
12443    /// keyring now holds into its columns and clear both reserved columns,
12444    /// one change batch per table. `client.decrypt_failed` fails the caller.
12445    fn reveal_sealed_rows(&mut self) -> Result<(), TransportError> {
12446        let storage = |message: String| TransportError::new("storage.failed", message);
12447        let tables: Vec<TableSchema> = self
12448            .schema
12449            .tables
12450            .iter()
12451            .filter(|table| table.has_encrypted_columns())
12452            .cloned()
12453            .collect();
12454        for table in &tables {
12455            let base = base_table(&table.name);
12456            let pk = quote_ident(&table.primary_key);
12457            let sealed: Vec<(SqlValue, i64, String)> = self
12458                .conn
12459                .prepare_cached(&format!(
12460                    "SELECT {pk}, \"_syncular_version\", \"_sync_sealed\" FROM {base} WHERE \"_sync_sealed_key\" IS NOT NULL"
12461                ))
12462                .and_then(|mut statement| {
12463                    statement
12464                        .query_map([], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)))?
12465                        .collect()
12466                })
12467                .map_err(|error| storage(Self::sqlite_failure(&self.storage_failure, error)))?;
12468            let mut revealed = Vec::new();
12469            for (key, version, envelopes) in sealed {
12470                if let Some(row) =
12471                    crate::values::reveal_sealed_row(table, &envelopes, &self.encryption)?
12472                {
12473                    revealed.push((key, version, row));
12474                }
12475            }
12476            if revealed.is_empty() {
12477                continue;
12478            }
12479            self.begin_observation("syncular_reveal").map_err(storage)?;
12480            let outcome = (|| {
12481                let assignments = table
12482                    .encrypted_columns
12483                    .iter()
12484                    .map(|enc| format!("{}=?", quote_ident(&table.columns[enc.index].name)))
12485                    .collect::<Vec<_>>()
12486                    .join(",");
12487                let mut statement = self
12488                    .conn
12489                    .prepare_cached(&format!(
12490                        "UPDATE {base} SET {assignments},\"_sync_sealed_key\"=NULL,\"_sync_sealed\"=NULL WHERE {pk}=? AND \"_syncular_version\"=?"
12491                    ))
12492                    .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12493                for (key, version, row) in &revealed {
12494                    let mut params = table
12495                        .encrypted_columns
12496                        .iter()
12497                        .map(|enc| owned_sql_value(RowParam::Cell(&row[enc.index])))
12498                        .collect::<Result<Vec<_>, _>>()?;
12499                    params.push(key.clone());
12500                    params.push(SqlValue::Integer(*version));
12501                    statement
12502                        .execute(rusqlite::params_from_iter(params))
12503                        .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12504                }
12505                drop(statement);
12506                self.overlay_dirty.table(&table.name);
12507                self.rebuild_overlay()?;
12508                let mut batch = ChangeAccumulator::default();
12509                batch.table(&table.name);
12510                self.finish_observation("syncular_reveal", batch)
12511            })();
12512            if outcome.is_err() {
12513                self.rollback_observation("syncular_reveal");
12514            }
12515            outcome.map_err(storage)?;
12516        }
12517        Ok(())
12518    }
12519
12520    /// The visible (optimistic) row's values plus version, or `None` when the
12521    /// row is absent from the overlay.
12522    fn visible_row(&self, table: &TableSchema, row_id: &str) -> Result<Option<(Row, i64)>, String> {
12523        self.stored_row(table, row_id, false)
12524    }
12525
12526    fn stored_row(
12527        &self,
12528        table: &TableSchema,
12529        row_id: &str,
12530        base: bool,
12531    ) -> Result<Option<(Row, i64)>, String> {
12532        let sql = format!(
12533            "SELECT * FROM {} WHERE {}",
12534            if base {
12535                base_table(&table.name)
12536            } else {
12537                visible_table(&table.name)
12538            },
12539            row_id_predicate(table)
12540        );
12541        let mut stmt = self
12542            .conn
12543            .prepare_cached(&sql)
12544            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12545        let mut rows = stmt
12546            .query(rusqlite::params![row_id])
12547            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12548        let Some(row) = rows
12549            .next()
12550            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?
12551        else {
12552            return Ok(None);
12553        };
12554        let mut values = Vec::with_capacity(table.columns.len());
12555        for (index, column) in table.columns.iter().enumerate() {
12556            let raw = row
12557                .get_ref(index)
12558                .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12559            if let rusqlite::types::ValueRef::Blob(bytes) = raw {
12560                if table
12561                    .encrypted_columns
12562                    .iter()
12563                    .any(|encrypted| encrypted.index == index)
12564                {
12565                    values.push(Some(ColumnValue::Bytes(bytes.to_vec())));
12566                    continue;
12567                }
12568            }
12569            let json = sql_ref_to_json(column, raw);
12570            values.push(json_to_column_value(column, Some(&json)).map_err(|_| {
12571                "sync.local_corrupt: persisted local row value is invalid".to_owned()
12572            })?);
12573        }
12574        let version: i64 = row
12575            .get(table.columns.len())
12576            .map_err(|error| Self::sqlite_failure(&self.storage_failure, error))?;
12577        Ok(Some((values, version)))
12578    }
12579
12580    // -- realtime (§8) ---------------------------------------------------------------
12581
12582    /// §8.8: the configured realtime policy.
12583    #[must_use]
12584    pub fn realtime_policy(&self) -> RealtimePolicy {
12585        self.realtime_policy
12586    }
12587
12588    /// §8.8: the explicit realtime availability state.
12589    #[must_use]
12590    pub fn realtime_state(&self) -> RealtimeState {
12591        self.realtime_state
12592    }
12593
12594    /// §8.8: set the realtime policy. Refused while the socket is connected:
12595    /// a policy change never silently re-routes an ownership state.
12596    pub fn set_realtime_policy(&mut self, policy: RealtimePolicy) -> Result<(), String> {
12597        if self.realtime_state == RealtimeState::Connected && policy != self.realtime_policy {
12598            return Err(
12599                "sync.invalid_request: disconnect realtime before changing the realtime policy"
12600                    .to_owned(),
12601            );
12602        }
12603        self.realtime_policy = policy;
12604        match policy {
12605            RealtimePolicy::Off => self.set_realtime_state(RealtimeState::Disabled, None),
12606            RealtimePolicy::Required | RealtimePolicy::Optional
12607                if self.realtime_state == RealtimeState::Disabled =>
12608            {
12609                self.set_realtime_state(RealtimeState::Disconnected, None);
12610            }
12611            RealtimePolicy::Required | RealtimePolicy::Optional => {}
12612        }
12613        Ok(())
12614    }
12615
12616    /// Record an availability transition, keeping the reason code bounded and
12617    /// code-like (§7.6) and clearing it where it cannot apply.
12618    fn set_realtime_state(&mut self, state: RealtimeState, reason_code: Option<&str>) {
12619        self.realtime_state = state;
12620        self.realtime_reason_code = reason_code.map(Self::diagnostic_code);
12621        if matches!(state, RealtimeState::Connected | RealtimeState::Disabled) {
12622            self.realtime_reason_code = None;
12623            self.realtime_retry_delay_ms = None;
12624        }
12625    }
12626
12627    #[must_use]
12628    fn realtime_connected(&self) -> bool {
12629        self.realtime_state == RealtimeState::Connected
12630    }
12631
12632    pub fn connect_realtime(&mut self, transport: &mut dyn Transport) -> Result<(), String> {
12633        if !self.transport_enabled {
12634            return Err("sync.offline: transport gate is closed".into());
12635        }
12636        if self.realtime_policy == RealtimePolicy::Off {
12637            return Err(
12638                "sync.invalid_request: realtime is off; realtime connect is refused".to_owned(),
12639            );
12640        }
12641        if self.realtime_connected() {
12642            return Ok(());
12643        }
12644        self.realtime_state = RealtimeState::Connecting;
12645        match transport.realtime_connect_for_client(&self.client_id) {
12646            Ok(()) => {
12647                self.set_realtime_state(RealtimeState::Connected, None);
12648                Ok(())
12649            }
12650            Err(error) => {
12651                self.set_realtime_state(RealtimeState::Refused, Some(&error.code));
12652                Err(format!("{}: {}", error.code, error.message))
12653            }
12654        }
12655    }
12656
12657    pub fn disconnect_realtime(&mut self, transport: &mut dyn Transport) {
12658        self.cancel_sync_round();
12659        // A deliberately disconnected or policy-disabled client has nothing
12660        // to release; a `lost` socket still does (the host owns the handle).
12661        if self.realtime_state == RealtimeState::Disconnected
12662            || self.realtime_state == RealtimeState::Disabled
12663        {
12664            return;
12665        }
12666        let _ = transport.realtime_close();
12667        self.set_realtime_state(
12668            if self.realtime_policy == RealtimePolicy::Off {
12669                RealtimeState::Disabled
12670            } else {
12671                RealtimeState::Disconnected
12672            },
12673            None,
12674        );
12675        self.presence.clear(); // §8.6.1: presence is per-connection
12676    }
12677
12678    /// §8.8: the host observed the socket end without disconnecting it (a
12679    /// peer close, an I/O error, a failed send; the native transport reports
12680    /// these as `Inbound::Disconnected`). Releases the transport, moves a
12681    /// `Connected` state to `Lost` with the transport's code as the reason, and schedules
12682    /// one background retry intent so the host reconnects and catches up.
12683    pub fn realtime_lost(&mut self, transport: &mut dyn Transport, error: &TransportError) {
12684        if self.realtime_state != RealtimeState::Connected {
12685            return;
12686        }
12687        self.cancel_sync_round();
12688        let _ = transport.realtime_close();
12689        self.set_realtime_state(RealtimeState::Lost, Some(&error.code));
12690        self.realtime_retry_delay_ms = Some(self.schedule_background_retry());
12691        self.presence.clear();
12692    }
12693
12694    /// §8.6.2: publish (or clear, `doc: None`) this client's presence
12695    /// document for `scope_key`. Requires a live socket; the document is
12696    /// ephemeral (lost on disconnect). Authorization is the connection's
12697    /// registration (§8.6.3) — an unheld key is rejected loudly by the
12698    /// server with `presence.forbidden`.
12699    pub fn set_presence(
12700        &mut self,
12701        transport: &mut dyn Transport,
12702        scope_key: &str,
12703        doc: Option<&Value>,
12704    ) -> Result<(), String> {
12705        if !self.transport_enabled {
12706            return Err("sync.offline: transport gate is closed".into());
12707        }
12708        if !self.realtime_connected() {
12709            return Err("setPresence requires a connected realtime socket (§8.6)".to_string());
12710        }
12711        let text = encode_presence_publish(scope_key, doc);
12712        transport
12713            .realtime_send(&text)
12714            .map_err(|e| format!("{}: {}", e.code, e.message))
12715    }
12716
12717    /// §8.6: the peers currently present on a scope key (ephemeral).
12718    pub fn presence(&self, scope_key: &str) -> Vec<PresencePeer> {
12719        self.presence
12720            .get(scope_key)
12721            .map(|peers| peers.values().cloned().collect())
12722            .unwrap_or_default()
12723    }
12724
12725    /// §8.6 apply an inbound presence fanout to the local map.
12726    fn apply_presence(
12727        &mut self,
12728        scope_key: String,
12729        kind: Option<PresenceKind>,
12730        actor_id: Option<String>,
12731        client_id: Option<String>,
12732        doc: Option<Value>,
12733        error: Option<String>,
12734    ) {
12735        // A refused publish (§8.6.2) changes no peer; the native host surfaces
12736        // it as a `presence` event carrying the server's `error` code.
12737        if error.is_some() {
12738            return;
12739        }
12740        let (Some(kind), Some(actor_id), Some(client_id)) = (kind, actor_id, client_id) else {
12741            return;
12742        };
12743        let peer_key = format!("{actor_id} {client_id}");
12744        match kind {
12745            PresenceKind::Leave => {
12746                if let Some(peers) = self.presence.get_mut(&scope_key) {
12747                    peers.remove(&peer_key);
12748                    if peers.is_empty() {
12749                        self.presence.remove(&scope_key);
12750                    }
12751                }
12752            }
12753            _ => {
12754                let doc = match doc {
12755                    Some(Value::Object(_)) => doc.unwrap(),
12756                    _ => return,
12757                };
12758                self.presence.entry(scope_key).or_default().insert(
12759                    peer_key,
12760                    PresencePeer {
12761                        actor_id,
12762                        client_id,
12763                        doc,
12764                    },
12765                );
12766            }
12767        }
12768    }
12769
12770    /// Inbound JSON control message (§8.1). Unknown events are tolerated.
12771    pub fn on_realtime_text(&mut self, text: &str) {
12772        if self.stopped || !self.transport_enabled || self.security_preflight {
12773            return;
12774        }
12775        match parse_control(text) {
12776            Ok(ControlMessage::Hello { requires_sync, .. }) => {
12777                if requires_sync {
12778                    // §8.1: pull before trusting the socket for continuity.
12779                    self.set_sync_needed(true, true);
12780                }
12781            }
12782            Ok(ControlMessage::Presence {
12783                scope_key,
12784                kind,
12785                actor_id,
12786                client_id,
12787                doc,
12788                error,
12789                ..
12790            }) => {
12791                self.apply_presence(scope_key, kind, actor_id, client_id, doc, error);
12792            }
12793            Ok(ControlMessage::Wake { .. }) => {
12794                // §8.3: any wake-up means "run a pull soon", never data.
12795                self.set_sync_needed(true, true);
12796            }
12797            _ => {}
12798        }
12799    }
12800
12801    /// Inbound binary delta: a complete SSP2 response (§8.2), applied like
12802    /// a pull response per section; an unapplied delta is a wake-up.
12803    /// Mailbox hosts send durable acknowledgements on their I/O executor.
12804    pub fn on_realtime_binary_queued(&mut self, bytes: &[u8]) -> Vec<String> {
12805        let mut transport = crate::round::DownloadResults::default();
12806        self.on_realtime_binary(&mut transport, bytes);
12807        transport.controls
12808    }
12809
12810    /// §8.2: a delta applies exactly like a live section and sets the replica
12811    /// cursor to its `throughSeq`; one at or below the replica cursor is a
12812    /// duplicate. A delta the client does not apply (no live unit, a status
12813    /// other than `active`, a decode or apply failure) is a drop and raises
12814    /// sync-needed.
12815    pub fn on_realtime_binary(&mut self, transport: &mut dyn Transport, bytes: &[u8]) {
12816        if self.stopped || !self.transport_enabled || self.security_preflight {
12817            return;
12818        }
12819        #[cfg(feature = "bench-internals")]
12820        let decode_phase = self.benchmark_phases.start(Phase::ResponseDecode);
12821        let message = match decode_message(bytes) {
12822            Ok(m) if m.msg_kind == MsgKind::Response => m,
12823            _ => {
12824                self.set_sync_needed(true, true);
12825                return;
12826            }
12827        };
12828        #[cfg(feature = "bench-internals")]
12829        drop(decode_phase);
12830        // §8.2: a delta's header means what a pull response's header means. A
12831        // schema floor (§1.6) is recorded, stops syncing and applies nothing.
12832        if let Some(Frame::RespHeader {
12833            required_schema_version: Some(required),
12834            latest_schema_version,
12835            ..
12836        }) = message.frames.first()
12837        {
12838            let floor = SchemaFloor {
12839                required_schema_version: Some(*required),
12840                latest_schema_version: *latest_schema_version,
12841            };
12842            if self.set_schema_floor(Some(floor)).is_err() {
12843                self.set_sync_needed(true, true);
12844            }
12845            return;
12846        }
12847        let through = message.frames.iter().find_map(|frame| match frame {
12848            Frame::LiveEnd { through_seq, .. } => Some(*through_seq),
12849            _ => None,
12850        });
12851        let epoch = self.get_meta(LOG_EPOCH_KEY).ok().flatten();
12852        let mut dropped = !matches!(message.frames.first(), Some(Frame::RespHeader {
12853                reset_required: false,
12854                log_epoch,
12855                ..
12856            }) if Some(log_epoch) == epoch.as_ref());
12857        let mut applied: Option<i64> = None;
12858        dropped |= self.begin_coalesced_apply().is_err();
12859        let mut receivers: Option<Vec<UnitRecord>> = None;
12860        for frame in message.frames {
12861            if dropped {
12862                break;
12863            }
12864            match frame {
12865                Frame::LiveStart { status, .. } => {
12866                    let Ok(cursor) = self.stored(replica_cursor(&self.conn)) else {
12867                        dropped = true;
12868                        break;
12869                    };
12870                    if status != LiveStatus::Active || cursor < 0 {
12871                        dropped = true;
12872                    } else if !through.is_some_and(|through| through <= cursor) {
12873                        // A catching-up unit receives no delta (§8.2).
12874                        receivers = self.stored(load_units(&self.conn)).ok().map(|units| {
12875                            units
12876                                .into_iter()
12877                                .filter(|unit| {
12878                                    unit.status == UnitStatus::Active
12879                                        && unit.echo.is_some()
12880                                        && unit.cursor.is_none()
12881                                })
12882                                .collect()
12883                        });
12884                        dropped = receivers.is_none();
12885                    }
12886                }
12887                Frame::Commit {
12888                    commit_seq,
12889                    tables,
12890                    changes,
12891                    ..
12892                } => {
12893                    let Some(receivers) = receivers.clone() else {
12894                        continue;
12895                    };
12896                    dropped = self
12897                        .apply_live_commit(
12898                            &receivers,
12899                            &tables,
12900                            changes,
12901                            commit_seq,
12902                            AppliedServerCommitSource::Delta,
12903                        )
12904                        .is_err();
12905                }
12906                Frame::LiveEnd { through_seq, .. } if receivers.is_some() => {
12907                    dropped = self
12908                        .set_meta(REPLICA_CURSOR_KEY, &through_seq.to_string())
12909                        .is_err();
12910                    applied = Some(through_seq);
12911                }
12912                Frame::Error { .. } => dropped = true,
12913                _ => {}
12914            }
12915        }
12916        // §1.4 rule 4: the ack follows the COMMIT. A failed COMMIT restored
12917        // the replica cursor, so the delta counts as a drop.
12918        if self.finish_coalesced_apply().is_err() || dropped {
12919            applied = None;
12920            dropped = true;
12921        }
12922        if let Some(cursor) = applied {
12923            // §8.2 ack point: the delta's throughSeq.
12924            let ack = format!("{{\"type\":\"ack\",\"cursor\":{cursor}}}");
12925            let _ = transport.realtime_send(&ack);
12926        }
12927        if dropped {
12928            self.set_sync_needed(true, true);
12929        }
12930    }
12931
12932    /// §8.2 ack point after a pull round on a live connection: the replica
12933    /// cursor while it is at least `0`.
12934    fn ack_after_pull(&mut self, transport: &mut dyn Transport) {
12935        if !self.realtime_connected() {
12936            return;
12937        }
12938        if let Ok(cursor @ 0..) = self.stored(replica_cursor(&self.conn)) {
12939            let ack = format!("{{\"type\":\"ack\",\"cursor\":{cursor}}}");
12940            let _ = transport.realtime_send(&ack);
12941        }
12942    }
12943}
12944
12945#[cfg(test)]
12946mod previous_version_wiring_tests;
12947
12948#[cfg(test)]
12949mod identity_tests;
12950
12951#[cfg(test)]
12952mod insert_intent_tests;
12953
12954#[cfg(test)]
12955mod robustness_tests;
12956
12957#[cfg(test)]
12958mod range_image_tests;