Skip to main content

faucet_core/
idempotency.rs

1//! Exactly-once / idempotent delivery primitives.
2//!
3//! The pipeline issues a monotonic **commit token** for every page that carries
4//! a bookmark. The token is persisted in the [`StateStore`](crate::state::StateStore)
5//! value next to the bookmark and committed inside the sink's own transaction,
6//! so a crash between "sink durably wrote" and "state persisted" is resolved on
7//! resume by skipping pages the sink already committed. See
8//! `docs/superpowers/specs/2026-06-09-exactly-once-delivery-design.md`.
9
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12
13/// Delivery guarantee for a pipeline run.
14#[derive(
15    Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema,
16)]
17#[serde(rename_all = "snake_case")]
18pub enum DeliveryMode {
19    /// Today's behaviour: a page may be re-delivered after a crash between the
20    /// sink write and the bookmark persist. Downstream must tolerate duplicates.
21    #[default]
22    AtLeastOnce,
23    /// The sink durably records a per-page commit token atomically with the
24    /// data; on resume the pipeline skips already-committed pages. Requires a
25    /// state store, an idempotent sink, and a deterministic-replay source.
26    ExactlyOnce,
27}
28
29/// How faithfully a [`Source`](crate::Source) **replays** its record stream
30/// when resumed from a bookmark.
31///
32/// This is the source-side capability the effectively-once *atomic-watermark*
33/// mechanism depends on: after a crash the pipeline re-anchors the source at a
34/// persisted position, and correctness requires that nothing before that
35/// position is re-emitted and nothing after it is skipped.
36#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
37#[serde(rename_all = "snake_case")]
38pub enum ReplayGuarantee {
39    /// Resuming from a bookmark may replay a *different* record stream
40    /// (query-based sources whose upstream can mutate, sources without
41    /// per-page bookmarks). The default.
42    #[default]
43    NonDeterministic,
44    /// The source emits a complete resume position (bookmark) on **every**
45    /// page, and resuming from any such bookmark continues the record stream
46    /// at exactly that position — no record before the bookmark is re-emitted
47    /// and none after it is skipped (immutable-log sources: CDC WAL/binlog/
48    /// change streams, Kafka partitions).
49    Deterministic,
50}
51
52/// The strongest delivery guarantee a [`Sink`](crate::Sink) can uphold.
53#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
54#[serde(rename_all = "snake_case")]
55pub enum SinkGuarantee {
56    /// Plain writes: a replayed page is written again. The default.
57    #[default]
58    AtLeastOnce,
59    /// The sink can dedup by key (`write_mode: upsert` with a configured
60    /// `key`): re-applying a record with the same key converges instead of
61    /// duplicating.
62    KeyedUpsert,
63    /// The sink can commit a page's rows **and** a commit token in one atomic
64    /// transaction ([`Sink::write_batch_idempotent`](crate::Sink)).
65    AtomicWatermark,
66}
67
68/// The mechanism through which a pipeline achieves effectively-once delivery.
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
70#[serde(rename_all = "snake_case")]
71pub enum EffectivelyOnceMechanism {
72    /// Deterministic-replay source + sink that commits data and a per-page
73    /// commit token atomically; on resume already-committed pages are skipped
74    /// (or the stream is re-anchored at the sink's recorded position).
75    AtomicWatermark,
76    /// The sink dedups by key (`write_mode: upsert`); replayed records
77    /// converge on the same keyed row. Works with any source.
78    KeyedUpsert,
79}
80
81/// The end-to-end guarantee a *pipeline* provides for a given
82/// source × sink × config combination.
83///
84/// Deliberately no `ExactlyOnce` variant — distributed-consensus exactly-once
85/// is not achievable here; effectively-once (idempotent at-least-once: each
86/// record is *observably applied* once) is the ceiling, and
87/// `delivery: exactly_once` in config is precisely documented as requesting it.
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
89#[serde(rename_all = "snake_case", tag = "guarantee", content = "via")]
90pub enum DeliveryGuarantee {
91    /// A crash between the sink write and the bookmark persist may re-deliver
92    /// a page. Downstream must tolerate duplicates.
93    AtLeastOnce,
94    /// Idempotent at-least-once: each record is observably applied once.
95    EffectivelyOnce(EffectivelyOnceMechanism),
96}
97
98impl std::fmt::Display for DeliveryGuarantee {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        match self {
101            Self::AtLeastOnce => write!(f, "at-least-once"),
102            Self::EffectivelyOnce(EffectivelyOnceMechanism::AtomicWatermark) => {
103                write!(f, "effectively-once (atomic watermark)")
104            }
105            Self::EffectivelyOnce(EffectivelyOnceMechanism::KeyedUpsert) => {
106                write!(f, "effectively-once (keyed upsert)")
107            }
108        }
109    }
110}
111
112/// Inputs to [`derive_delivery_guarantee`] — the facts about a concrete
113/// source × sink × config combination the derivation keys off.
114#[derive(Debug, Clone, Copy, Default)]
115pub struct GuaranteeInputs {
116    /// The source's replay capability.
117    pub replay: ReplayGuarantee,
118    /// Whether the sink commits data + token atomically
119    /// (`Sink::supports_idempotent_writes`).
120    pub sink_atomic: bool,
121    /// Whether the sink is *configured* to dedup by key — `write_mode: upsert`
122    /// (or `delete`) with a non-empty `key` (`Sink::dedups_by_key`).
123    pub keyed_upsert_configured: bool,
124    /// Whether a durable (non-memory) state store is configured. The
125    /// atomic-watermark mechanism persists its cross-restart sequence here.
126    pub durable_state: bool,
127    /// Whether a DLQ is configured (incompatible with the atomic-watermark
128    /// mechanism in this version).
129    pub dlq: bool,
130}
131
132/// Derive the end-to-end [`DeliveryGuarantee`] a pipeline actually provides.
133///
134/// Preference order: the atomic-watermark mechanism (strongest bookkeeping,
135/// no keyed-schema requirement) when the topology supports it, then keyed
136/// upsert, then at-least-once. A sink that is both atomic and keyed reports
137/// atomic-watermark when the source replays deterministically, and falls back
138/// to keyed upsert otherwise.
139pub fn derive_delivery_guarantee(i: &GuaranteeInputs) -> DeliveryGuarantee {
140    if i.sink_atomic && i.replay == ReplayGuarantee::Deterministic && i.durable_state && !i.dlq {
141        return DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::AtomicWatermark);
142    }
143    if i.keyed_upsert_configured {
144        return DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::KeyedUpsert);
145    }
146    DeliveryGuarantee::AtLeastOnce
147}
148
149/// Reserved key marking the exactly-once state wrapper object.
150const EO_MARKER: &str = "__faucet_eo";
151const EO_BOOKMARK: &str = "bookmark";
152const EO_SEQ: &str = "seq";
153
154/// Width of the zero-padded decimal token. `u64::MAX` is 20 digits, so 20 makes
155/// lexicographic order match numeric order for the full `u64` range.
156const TOKEN_WIDTH: usize = 20;
157
158/// Separator between the numeric sequence and the embedded resume bookmark in
159/// a commit token. The prefix before it is always the fixed-width sequence.
160const TOKEN_BOOKMARK_SEP: char = '#';
161
162/// Render a page sequence as a fixed-width, lexicographically-ordered token.
163pub fn format_token(seq: u64) -> String {
164    format!("{seq:0TOKEN_WIDTH$}")
165}
166
167/// Render a commit token that carries the page's **resume bookmark** alongside
168/// the sequence: `"{seq:020}#{bookmark-json}"`.
169///
170/// Sinks store the token opaquely, so the committed watermark doubles as a
171/// durable record of *where the stream stood* when the page committed. On
172/// resume the pipeline recovers that position from the sink
173/// ([`parse_token_parts`]) and re-anchors the source there — closing the
174/// crash window between "sink durably committed" and "state store persisted"
175/// without requiring the source to replay identical page boundaries.
176pub fn format_token_with_bookmark(seq: u64, bookmark: Option<&Value>) -> String {
177    match bookmark {
178        Some(bm) => format!("{seq:0TOKEN_WIDTH$}{TOKEN_BOOKMARK_SEP}{bm}"),
179        None => format_token(seq),
180    }
181}
182
183/// Parse the numeric sequence from a token produced by [`format_token`] or
184/// [`format_token_with_bookmark`]. Returns `None` on garbage.
185pub fn parse_token(s: &str) -> Option<u64> {
186    let seq = match s.split_once(TOKEN_BOOKMARK_SEP) {
187        Some((prefix, _)) => prefix,
188        None => s,
189    };
190    seq.trim().parse::<u64>().ok()
191}
192
193/// Parse a stored commit token into `(seq, embedded_bookmark)`.
194///
195/// Tokens written before bookmarks were embedded (bare `format_token` output)
196/// parse with `bookmark = None`. A bookmark suffix that is not valid JSON also
197/// yields `None` for the bookmark — the sequence alone still drives the
198/// skip-on-resume path.
199pub fn parse_token_parts(s: &str) -> Option<(u64, Option<Value>)> {
200    match s.split_once(TOKEN_BOOKMARK_SEP) {
201        Some((prefix, suffix)) => {
202            let seq = prefix.trim().parse::<u64>().ok()?;
203            Some((seq, serde_json::from_str(suffix).ok()))
204        }
205        None => Some((s.trim().parse::<u64>().ok()?, None)),
206    }
207}
208
209/// Wrap a bookmark + sequence into the exactly-once state value.
210pub fn wrap_state(bookmark: Option<&Value>, seq: u64) -> Value {
211    serde_json::json!({
212        EO_MARKER: 1,
213        EO_BOOKMARK: bookmark.cloned().unwrap_or(Value::Null),
214        EO_SEQ: seq,
215    })
216}
217
218/// Whether `value` is the exactly-once wrapper (`{"__faucet_eo": 1, …}`).
219pub fn is_eo_envelope(value: &Value) -> bool {
220    value.get(EO_MARKER).and_then(Value::as_u64) == Some(1)
221}
222
223/// Unwrap a stored state value into `(bookmark, seq)`. A versioned envelope
224/// (#736) is looked through first.
225///
226/// A value that is the exactly-once wrapper object unwraps to its inner
227/// bookmark + seq. Anything else is treated as a legacy/at-least-once **bare
228/// bookmark** with `seq = 0` — so switching an existing pipeline to
229/// `exactly_once` resumes cleanly (the sink's own watermark is authoritative).
230pub fn unwrap_state(value: &Value) -> (Option<Value>, u64) {
231    let peeled = crate::state_version::peel_versioned(value);
232    let value = &peeled;
233    if let Value::Object(map) = value
234        && map.get(EO_MARKER).and_then(Value::as_u64) == Some(1)
235    {
236        let bookmark = match map.get(EO_BOOKMARK) {
237            None | Some(Value::Null) => None,
238            Some(v) => Some(v.clone()),
239        };
240        let seq = map.get(EO_SEQ).and_then(Value::as_u64).unwrap_or(0);
241        return (bookmark, seq);
242    }
243    // Legacy bare bookmark.
244    let bookmark = if value.is_null() {
245        None
246    } else {
247        Some(value.clone())
248    };
249    (bookmark, 0)
250}
251
252/// Canonical watermark table the SQL sinks UPSERT the commit token into.
253pub const COMMIT_TOKEN_TABLE: &str = "_faucet_commit_token";
254/// Watermark column holding the pipeline state-key (`{name}::{row_id}`).
255pub const COMMIT_TOKEN_SCOPE_COL: &str = "scope";
256/// Watermark column holding the latest committed token.
257pub const COMMIT_TOKEN_TOKEN_COL: &str = "token";
258
259/// Iceberg snapshot summary property names.
260pub const ICEBERG_SCOPE_PROP: &str = "faucet.commit-scope";
261pub const ICEBERG_TOKEN_PROP: &str = "faucet.commit-token";
262
263/// Suffix appended to a destination's name to form the staging relation an
264/// in-flight `write_mode: overwrite` run writes into.
265///
266/// Reserved: the overwrite lifecycle creates, truncates, and **drops** whatever
267/// carries this suffix, so a real destination named `<x>__faucet_ovw` would be
268/// destroyed by `abort_overwrite`. Every sink derives its staging name from this
269/// one constant so the reserved set stays enumerable by a collision check or an
270/// orphan sweep (#654 M12).
271pub const OVERWRITE_STAGING_SUFFIX: &str = "__faucet_ovw";
272/// Suffix of the transient relation the *outgoing* destination is renamed to
273/// mid-swap, on backends whose only atomic publish is a rename (MySQL, whose
274/// DDL auto-commits so a transaction cannot span the swap).
275///
276/// Extends [`OVERWRITE_STAGING_SUFFIX`], so a sweep matching the base suffix as
277/// a prefix catches this one too.
278pub const OVERWRITE_STAGING_OLD_SUFFIX: &str = "__faucet_ovw_old";
279
280/// Length of the `__h:` + 64 hex-digit SHA-256 suffix appended to a shortened
281/// scope.
282const SCOPE_DIGEST_LEN: usize = 4 + 64;
283
284/// Fit a watermark scope into a length-capped, indexable key column.
285///
286/// The scope is the pipeline state key — `{name}::{row}` for a root, plus
287/// `::{parent_record_key}` for a child — and a child's key comes from *record
288/// data*, so it has no length bound. The SQL sinks store it as a PRIMARY KEY, and
289/// a key column cannot be unbounded (MySQL's index limit, SQL Server's 900-byte
290/// key budget), so an over-long scope either errors or — under a non-strict MySQL
291/// `sql_mode` — is **truncated**, silently collapsing two distinct rows onto one
292/// watermark so one row's committed token suppresses the other's pages (#456 L1).
293///
294/// Scopes at or under `max` are returned verbatim, so every watermark written
295/// before this existed still resolves. A longer one is replaced by a
296/// deterministic, collision-resistant digest form (`__h:<64-hex>`), which is
297/// stable across restarts — the only property the watermark needs.
298pub fn scope_key(scope: &str, max: usize) -> String {
299    if scope.len() <= max {
300        return scope.to_owned();
301    }
302    use sha2::Digest;
303    let digest = sha2::Sha256::digest(scope.as_bytes());
304    let h: String = digest.iter().map(|b| format!("{b:02x}")).collect();
305    // Keep as much of the readable head as fits, so an operator inspecting the
306    // watermark table can still tell which pipeline a row belongs to. Truncate on
307    // a char boundary — a scope may hold non-ASCII.
308    let room = max.saturating_sub(SCOPE_DIGEST_LEN);
309    let mut head = 0usize;
310    for (i, _) in scope.char_indices() {
311        if i > room {
312            break;
313        }
314        head = i;
315    }
316    let shortened = format!("{}__h:{h}", &scope[..head]);
317    tracing::debug!(
318        scope_len = scope.len(),
319        max,
320        "exactly-once scope exceeds the sink's key-column width; shortening with a digest"
321    );
322    shortened
323}
324
325/// The shortened form [`scope_key`] produced before faucet-core 1.13 (an
326/// FNV-1a digest), for a sink to read and retire a watermark written then
327/// (#862 M-017). `None` when `scope` fits in `max`: that form never changed.
328pub fn legacy_scope_key(scope: &str, max: usize) -> Option<String> {
329    const LEGACY_DIGEST_LEN: usize = 4 + 16;
330    if scope.len() <= max {
331        return None;
332    }
333    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
334    for b in scope.as_bytes() {
335        h ^= u64::from(*b);
336        h = h.wrapping_mul(0x1000_0000_01b3);
337    }
338    let room = max.saturating_sub(LEGACY_DIGEST_LEN);
339    let mut head = 0usize;
340    for (i, _) in scope.char_indices() {
341        if i > room {
342            break;
343        }
344        head = i;
345    }
346    Some(format!("{}__h:{h:016x}", &scope[..head]))
347}
348
349#[cfg(test)]
350mod tests {
351    use super::*;
352    use serde_json::json;
353
354    #[test]
355    fn token_round_trips_and_orders_lexicographically() {
356        assert_eq!(format_token(42).len(), TOKEN_WIDTH);
357        assert_eq!(parse_token(&format_token(42)), Some(42));
358        assert_eq!(parse_token(&format_token(0)), Some(0));
359        assert_eq!(parse_token(&format_token(u64::MAX)), Some(u64::MAX));
360        assert!(format_token(9) < format_token(10));
361        assert!(format_token(2) < format_token(1000));
362    }
363
364    #[test]
365    fn parse_token_rejects_garbage() {
366        assert_eq!(parse_token("abc"), None);
367        assert_eq!(parse_token(""), None);
368    }
369
370    #[test]
371    fn wrap_then_unwrap_preserves_bookmark_and_seq() {
372        let bm = json!({"lsn": "0/16B2D58"});
373        let wrapped = wrap_state(Some(&bm), 7);
374        let (got_bm, got_seq) = unwrap_state(&wrapped);
375        assert_eq!(got_bm, Some(bm));
376        assert_eq!(got_seq, 7);
377    }
378
379    #[test]
380    fn wrap_none_bookmark_unwraps_to_none() {
381        let wrapped = wrap_state(None, 3);
382        let (got_bm, got_seq) = unwrap_state(&wrapped);
383        assert_eq!(got_bm, None);
384        assert_eq!(got_seq, 3);
385    }
386
387    #[test]
388    fn legacy_bare_bookmark_unwraps_with_seq_zero() {
389        let (bm, seq) = unwrap_state(&json!("2024-12-01"));
390        assert_eq!(bm, Some(json!("2024-12-01")));
391        assert_eq!(seq, 0);
392        let (bm2, seq2) = unwrap_state(&json!({"updated_at": "2024-12-01"}));
393        assert_eq!(bm2, Some(json!({"updated_at": "2024-12-01"})));
394        assert_eq!(seq2, 0);
395    }
396
397    #[test]
398    fn object_with_non_sentinel_marker_is_treated_as_bare_bookmark() {
399        // A legacy/user object that merely contains the key must NOT be misread
400        // as an EO wrapper — only the typed sentinel `1` counts.
401        let v = json!({"__faucet_eo": null, "offset": 500});
402        let (bm, seq) = unwrap_state(&v);
403        assert_eq!(bm, Some(v));
404        assert_eq!(seq, 0);
405    }
406
407    #[test]
408    fn null_value_unwraps_to_none_seq_zero() {
409        let (bm, seq) = unwrap_state(&json!(null));
410        assert_eq!(bm, None);
411        assert_eq!(seq, 0);
412    }
413
414    #[test]
415    fn token_with_bookmark_round_trips() {
416        let bm = json!({"partition_offsets": [{"topic": "t", "partition": 0, "offset": 42}]});
417        let token = format_token_with_bookmark(7, Some(&bm));
418        assert!(token.starts_with(&format_token(7)));
419        assert_eq!(parse_token(&token), Some(7));
420        let (seq, parsed_bm) = parse_token_parts(&token).unwrap();
421        assert_eq!(seq, 7);
422        assert_eq!(parsed_bm, Some(bm));
423    }
424
425    #[test]
426    fn token_with_no_bookmark_is_bare_and_back_compatible() {
427        assert_eq!(format_token_with_bookmark(3, None), format_token(3));
428        let (seq, bm) = parse_token_parts(&format_token(3)).unwrap();
429        assert_eq!((seq, bm), (3, None));
430    }
431
432    #[test]
433    fn token_with_bookmark_orders_lexicographically_on_prefix() {
434        // The fixed-width numeric prefix keeps lexicographic order meaningful
435        // even with an embedded bookmark (kafka side-topic folding compares
436        // parsed sequences, but SQL MAX() naturally works too).
437        let a = format_token_with_bookmark(9, Some(&json!({"o": 1})));
438        let b = format_token_with_bookmark(10, Some(&json!({"o": 2})));
439        assert!(a < b);
440    }
441
442    #[test]
443    fn parse_token_parts_tolerates_garbage() {
444        assert_eq!(parse_token_parts("abc"), None);
445        assert_eq!(parse_token_parts(""), None);
446        // Bad JSON suffix: sequence survives, bookmark is dropped.
447        let (seq, bm) = parse_token_parts("00000000000000000005#{not json").unwrap();
448        assert_eq!((seq, bm), (5, None));
449        // parse_token ignores the suffix entirely.
450        assert_eq!(parse_token("00000000000000000005#{not json"), Some(5));
451    }
452
453    #[test]
454    fn derive_guarantee_prefers_atomic_then_keyed_then_at_least_once() {
455        use ReplayGuarantee::*;
456        let base = GuaranteeInputs {
457            replay: Deterministic,
458            sink_atomic: true,
459            keyed_upsert_configured: false,
460            durable_state: true,
461            dlq: false,
462        };
463        assert_eq!(
464            derive_delivery_guarantee(&base),
465            DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::AtomicWatermark)
466        );
467        // Atomic path degrades without deterministic replay…
468        let non_det = GuaranteeInputs {
469            replay: NonDeterministic,
470            ..base
471        };
472        assert_eq!(
473            derive_delivery_guarantee(&non_det),
474            DeliveryGuarantee::AtLeastOnce
475        );
476        // …but keyed upsert rescues it, source-independent.
477        let keyed = GuaranteeInputs {
478            keyed_upsert_configured: true,
479            ..non_det
480        };
481        assert_eq!(
482            derive_delivery_guarantee(&keyed),
483            DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::KeyedUpsert)
484        );
485        // A DLQ or missing durable state disables atomic; keyed still applies.
486        let dlq = GuaranteeInputs {
487            dlq: true,
488            keyed_upsert_configured: true,
489            ..base
490        };
491        assert_eq!(
492            derive_delivery_guarantee(&dlq),
493            DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::KeyedUpsert)
494        );
495        let mem_state = GuaranteeInputs {
496            durable_state: false,
497            ..base
498        };
499        assert_eq!(
500            derive_delivery_guarantee(&mem_state),
501            DeliveryGuarantee::AtLeastOnce
502        );
503    }
504
505    #[test]
506    fn guarantee_display_is_human_readable() {
507        assert_eq!(DeliveryGuarantee::AtLeastOnce.to_string(), "at-least-once");
508        assert_eq!(
509            DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::AtomicWatermark)
510                .to_string(),
511            "effectively-once (atomic watermark)"
512        );
513        assert_eq!(
514            DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::KeyedUpsert).to_string(),
515            "effectively-once (keyed upsert)"
516        );
517    }
518
519    #[test]
520    fn capability_enums_default_to_weakest() {
521        assert_eq!(
522            ReplayGuarantee::default(),
523            ReplayGuarantee::NonDeterministic
524        );
525        assert_eq!(SinkGuarantee::default(), SinkGuarantee::AtLeastOnce);
526    }
527
528    #[test]
529    fn delivery_mode_serde_is_snake_case_and_defaults_at_least_once() {
530        assert_eq!(DeliveryMode::default(), DeliveryMode::AtLeastOnce);
531        assert_eq!(
532            serde_json::to_string(&DeliveryMode::ExactlyOnce).unwrap(),
533            "\"exactly_once\""
534        );
535        let m: DeliveryMode = serde_json::from_str("\"at_least_once\"").unwrap();
536        assert_eq!(m, DeliveryMode::AtLeastOnce);
537    }
538
539    #[test]
540    fn overwrite_staging_suffixes_are_pinned() {
541        // These are on-the-wire relation names: a live destination staged under
542        // the old spelling would be stranded (and the new spelling's relation
543        // dropped) if either value ever moved.
544        assert_eq!(OVERWRITE_STAGING_SUFFIX, "__faucet_ovw");
545        assert_eq!(OVERWRITE_STAGING_OLD_SUFFIX, "__faucet_ovw_old");
546        assert_ne!(OVERWRITE_STAGING_SUFFIX, OVERWRITE_STAGING_OLD_SUFFIX);
547        // An orphan sweep matching the base suffix as a prefix must also catch
548        // the mid-swap relation.
549        assert!(OVERWRITE_STAGING_OLD_SUFFIX.starts_with(OVERWRITE_STAGING_SUFFIX));
550    }
551}
552
553#[cfg(test)]
554mod scope_key_tests {
555    use super::*;
556
557    /// #456 L1: the SQL sinks store the scope as a length-capped PRIMARY KEY, so
558    /// a long child scope (its key comes from record data and has no bound) either
559    /// errored or — under a non-strict MySQL sql_mode — truncated, collapsing two
560    /// rows onto one watermark.
561    #[test]
562    fn scope_key_passes_short_scopes_through_and_shortens_long_ones() {
563        // Backwards compatible: anything that fit before is returned verbatim, so
564        // watermarks written before this existed still resolve.
565        assert_eq!(scope_key("pipe::row", 255), "pipe::row");
566        let exactly = "x".repeat(255);
567        assert_eq!(scope_key(&exactly, 255), exactly);
568
569        // Over the cap: shortened, and within the cap.
570        let long = format!("pipe::row::{}", "k".repeat(400));
571        let key = scope_key(&long, 255);
572        assert!(key.len() <= 255, "len {}", key.len());
573        assert_ne!(key, long);
574        // Keeps a readable head so the row is still attributable.
575        assert!(key.starts_with("pipe::row::"), "{key}");
576        assert!(key.contains("__h:"), "{key}");
577    }
578
579    #[test]
580    fn scope_key_is_deterministic_and_distinguishes_scopes() {
581        let a = format!("pipe::row::{}", "a".repeat(400));
582        let b = format!("pipe::row::{}", "b".repeat(400));
583        // Stable across calls — the watermark must resolve after a restart.
584        assert_eq!(scope_key(&a, 255), scope_key(&a, 255));
585        // Two distinct scopes must not collide onto one watermark. Under plain
586        // truncation both of these would become the same 255-char prefix.
587        assert_ne!(scope_key(&a, 255), scope_key(&b, 255));
588        assert_eq!(&a[..255], &format!("pipe::row::{}", "a".repeat(400))[..255]);
589    }
590
591    #[test]
592    fn scope_key_digest_is_sha256() {
593        let long = format!("pipe::row::{}", "k".repeat(400));
594        let key = scope_key(&long, 255);
595        let (_, digest) = key.rsplit_once("__h:").unwrap();
596        assert_eq!(digest.len(), 64);
597        assert_eq!(
598            &digest[..16],
599            "4c5c3b297497a0a0",
600            "pinned: a changed digest strands every shortened watermark"
601        );
602    }
603
604    #[test]
605    fn legacy_scope_key_reproduces_the_fnv_form() {
606        assert_eq!(legacy_scope_key("short", 64), None);
607        let long = "p::row::".to_string() + &"x".repeat(100);
608        let legacy = legacy_scope_key(&long, 64).unwrap();
609        assert!(legacy.len() <= 64, "{legacy}");
610        assert!(legacy.starts_with("p::row::"), "{legacy}");
611        let digest = legacy.rsplit_once("__h:").unwrap().1;
612        assert_eq!(digest.len(), 16);
613        assert_ne!(legacy, scope_key(&long, 64));
614        assert_eq!(
615            legacy_scope_key("é".repeat(40).as_str(), 30).unwrap().len(),
616            4 + 16 + 10
617        );
618    }
619
620    #[test]
621    fn scope_key_truncates_on_a_char_boundary() {
622        // A multi-byte head must not be split mid-character (that would panic).
623        let long = format!("pipé::{}", "é".repeat(400));
624        let key = scope_key(&long, 255);
625        assert!(key.len() <= 255);
626        assert!(key.contains("__h:"), "{key}");
627    }
628}