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
22use crate::CurrentBranch;
23
24/// An opaque, cheap-to-store staleness marker (design `LC-4`). Two tokens composed from an unchanged
25/// signal set are equal; that is the only guarantee — nothing above the seam may branch on, decode, or
26/// otherwise interpret a token's contents (RFC 003 decision 3). Its `Debug` prints the digest only, on
27/// purpose: there is no structure in it worth reading.
28#[derive(Clone, Copy, PartialEq, Eq)]
29pub struct ChangeToken(u64);
30
31impl ChangeToken {
32 /// Compose a token from the RFC 003 signal set: every ref's `(name, `RefState`/pointer id)`, the
33 /// queued-patch count and its target ref (or `None` for an empty or unpublished queue), and **prikk's
34 /// current branch** (RFC 030 decision 2).
35 ///
36 /// `refs` need not be pre-sorted — this always sorts by name internally before hashing, so a
37 /// caller (or a future prikk version) returning the same refs in a different order can never look
38 /// like a repository change. This is deliberate, not incidental: relying on a sort prikk merely
39 /// happens to do today would make an upstream ordering change indistinguishable from a real one.
40 ///
41 /// **The current branch is hashed with a discriminant per state**, so prikk's unresolved text
42 /// `heads/x` can never compose the same token as the branch `heads/x`, and "not reported" never
43 /// equals either. A terminal `prikk branch switch` at ≥ 0.42 moves no ref, tag or queue — this input
44 /// is the only one that sees it.
45 #[must_use]
46 pub fn compose<'a>(
47 refs: impl IntoIterator<Item = (&'a str, &'a str)>,
48 queued_patches: u64,
49 queued_target: Option<&str>,
50 current_branch: &CurrentBranch,
51 ) -> Self {
52 let mut sorted: Vec<(&str, &str)> = refs.into_iter().collect();
53 sorted.sort_unstable_by_key(|(name, _)| *name);
54
55 let mut hasher = DefaultHasher::new();
56 sorted.len().hash(&mut hasher);
57 for (name, id) in &sorted {
58 name.hash(&mut hasher);
59 id.hash(&mut hasher);
60 }
61 queued_patches.hash(&mut hasher);
62 queued_target.hash(&mut hasher);
63 match current_branch {
64 CurrentBranch::NotReported => 0u8.hash(&mut hasher),
65 CurrentBranch::Unresolved(text) => {
66 1u8.hash(&mut hasher);
67 text.hash(&mut hasher);
68 }
69 CurrentBranch::Branch(name) => {
70 2u8.hash(&mut hasher);
71 name.as_str().hash(&mut hasher);
72 }
73 }
74 Self(hasher.finish())
75 }
76}
77
78impl std::fmt::Debug for ChangeToken {
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 write!(f, "ChangeToken({:016x})", self.0)
81 }
82}
83
84#[cfg(test)]
85mod tests;