Skip to main content

stikk_model/
change_token.rs

1//! The repository-change token (design `LC-4`; RFC 003).
2//!
3//! stikk holds no repository authority (`INV-1`): every derived view and armed preview is valid only
4//! as long as the repository has not changed underneath it. [`ChangeToken`] is the cheap, comparable
5//! signal that answers "has anything changed since we last looked?" — composed from prikk-observable
6//! state the seam already reads (ref pointers, queue extent), never from `.prikk/` bytes stikk is
7//! forbidden to read (`CON-1`).
8//!
9//! **It is a detection primitive, not a lock** (`CT-05`/`NFR-R02`). Between reading a token and acting
10//! on it, the repository can change again — prikk's own locking is the real guard against a mutation
11//! racing another writer. What the token gives is that a preview cannot be *executed* against a
12//! repository that has demonstrably moved since the preview was computed; it does not make execution
13//! atomic, and nothing in the UI may imply that it does.
14//!
15//! **It is not an identity.** [`ChangeToken`] changes with ordinary history growth, so it cannot serve
16//! as a stable fingerprint for "is this the same repository" — that question is answered by canonical
17//! path plus `INV-5`'s re-resolution-on-load discipline (RFC 003 decision 4), not by this type.
18
19use std::collections::hash_map::DefaultHasher;
20use std::hash::{Hash, Hasher};
21
22/// An opaque, cheap-to-store staleness marker (design `LC-4`). Two tokens composed from an unchanged
23/// signal set are equal; that is the only guarantee — nothing above the seam may branch on, decode, or
24/// otherwise interpret a token's contents (RFC 003 decision 3). Its `Debug` prints the digest only, on
25/// purpose: there is no structure in it worth reading.
26#[derive(Clone, Copy, PartialEq, Eq)]
27pub struct ChangeToken(u64);
28
29impl ChangeToken {
30    /// Compose a token from the RFC 003 signal set: every ref's `(name, `RefState`/pointer id)`, plus
31    /// the queued-patch count and its target ref (or `None` for an empty or unpublished queue).
32    ///
33    /// `refs` need not be pre-sorted — this always sorts by name internally before hashing, so a
34    /// caller (or a future prikk version) returning the same refs in a different order can never look
35    /// like a repository change. This is deliberate, not incidental: relying on a sort prikk merely
36    /// happens to do today would make an upstream ordering change indistinguishable from a real one.
37    #[must_use]
38    pub fn compose<'a>(
39        refs: impl IntoIterator<Item = (&'a str, &'a str)>,
40        queued_patches: u64,
41        queued_target: Option<&str>,
42    ) -> Self {
43        let mut sorted: Vec<(&str, &str)> = refs.into_iter().collect();
44        sorted.sort_unstable_by_key(|(name, _)| *name);
45
46        let mut hasher = DefaultHasher::new();
47        sorted.len().hash(&mut hasher);
48        for (name, id) in &sorted {
49            name.hash(&mut hasher);
50            id.hash(&mut hasher);
51        }
52        queued_patches.hash(&mut hasher);
53        queued_target.hash(&mut hasher);
54        Self(hasher.finish())
55    }
56}
57
58impl std::fmt::Debug for ChangeToken {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        write!(f, "ChangeToken({:016x})", self.0)
61    }
62}
63
64#[cfg(test)]
65mod tests;