use std::cell::Cell;
use std::env;
use std::fmt;
use std::marker::PhantomData;
use chrono::{DateTime, Utc};
use uuid::{Builder, Timestamp, Uuid};
pub const RANDOMNESS_INVENTORY_ROWS_CONTENT_HASH: &str =
"blake3-ish:51a8854727a5768008ba8269596e8666cc9ffdd88e8ac3f13101ad36434a3bfc";
const ROOT_SCOPE: &str = "root";
const UUID_COUNTER_BITS: u8 = 74;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Seed(u64);
impl Seed {
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
#[must_use]
pub const fn as_u64(self) -> u64 {
self.0
}
#[must_use]
pub fn from_bytes(domain: &str, bytes: impl AsRef<[u8]>) -> Self {
let mut hasher = blake3::Hasher::new();
hasher.update(b"ee.determinism.seed.v1");
hasher.update(domain.as_bytes());
hasher.update(&[0]);
hasher.update(bytes.as_ref());
let digest = hasher.finalize();
let mut seed_bytes = [0_u8; 8];
seed_bytes.copy_from_slice(&digest.as_bytes()[..8]);
Self(u64::from_be_bytes(seed_bytes))
}
}
impl From<u64> for Seed {
fn from(value: u64) -> Self {
Self::new(value)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum SeedSource {
Explicit,
PersistentWorkspace,
TimestampSecond,
Env,
Child,
}
impl SeedSource {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Explicit => "explicit",
Self::PersistentWorkspace => "persistent_workspace",
Self::TimestampSecond => "timestamp_second",
Self::Env => "env",
Self::Child => "child",
}
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum DeterminismError {
MissingEnv { name: String },
InvalidSeed { value: String },
InvalidTimestamp { value: String, message: String },
}
impl fmt::Display for DeterminismError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MissingEnv { name } => {
write!(
formatter,
"determinism seed environment variable `{name}` is missing"
)
}
Self::InvalidSeed { value } => {
write!(formatter, "determinism seed `{value}` is not a u64")
}
Self::InvalidTimestamp { value, message } => write!(
formatter,
"determinism timestamp `{value}` is not valid RFC 3339: {message}"
),
}
}
}
impl std::error::Error for DeterminismError {}
#[derive(Debug)]
pub struct Deterministic<S = Seed> {
seed: Seed,
source: SeedSource,
scope: String,
counter: u64,
_scope: PhantomData<fn() -> S>,
_not_sync: PhantomData<Cell<()>>,
}
impl Deterministic<Seed> {
#[must_use]
pub fn from_seed(seed: u64) -> Self {
Self::from_parts(Seed::new(seed), SeedSource::Explicit, ROOT_SCOPE.to_owned())
}
#[must_use]
pub fn from_persistent_seed(bytes: impl AsRef<[u8]>) -> Self {
Self::from_parts(
Seed::from_bytes("persistent_workspace", bytes),
SeedSource::PersistentWorkspace,
ROOT_SCOPE.to_owned(),
)
}
pub fn from_timestamp_second(value: &str) -> Result<Self, DeterminismError> {
let parsed = DateTime::parse_from_rfc3339(value).map_err(|error| {
DeterminismError::InvalidTimestamp {
value: value.to_owned(),
message: error.to_string(),
}
})?;
let seconds = parsed.with_timezone(&Utc).timestamp();
Ok(Self::from_parts(
Seed::from_bytes("timestamp_second", seconds.to_be_bytes()),
SeedSource::TimestampSecond,
ROOT_SCOPE.to_owned(),
))
}
pub fn from_env(name: &str) -> Result<Self, DeterminismError> {
let value = env::var(name).map_err(|_| DeterminismError::MissingEnv {
name: name.to_owned(),
})?;
Self::from_env_value(&value)
}
pub fn from_env_value(value: &str) -> Result<Self, DeterminismError> {
let seed = value
.parse::<u64>()
.map_err(|_| DeterminismError::InvalidSeed {
value: value.to_owned(),
})?;
Ok(Self::from_parts(
Seed::new(seed),
SeedSource::Env,
ROOT_SCOPE.to_owned(),
))
}
}
impl<S> Deterministic<S> {
fn from_parts(seed: Seed, source: SeedSource, scope: String) -> Self {
Self {
seed,
source,
scope,
counter: 0,
_scope: PhantomData,
_not_sync: PhantomData,
}
}
#[must_use]
pub const fn seed(&self) -> Seed {
self.seed
}
#[must_use]
pub const fn source(&self) -> SeedSource {
self.source
}
#[must_use]
pub fn scope(&self) -> &str {
&self.scope
}
#[must_use]
pub fn seed_hash_prefix(&self) -> String {
let mut hasher = blake3::Hasher::new();
hasher.update(b"ee.determinism.seed_hash_prefix.v1");
hasher.update(&self.seed.as_u64().to_be_bytes());
hasher.update(self.scope.as_bytes());
let digest = hasher.finalize();
hex_prefix(digest.as_bytes(), 12)
}
#[must_use]
pub fn child(&mut self, label: &str) -> Deterministic<Seed> {
let ordinal = self.next_counter();
let mut hasher = blake3::Hasher::new();
hasher.update(b"ee.determinism.child.v1");
hasher.update(&self.seed.as_u64().to_be_bytes());
hasher.update(self.scope.as_bytes());
hasher.update(&[0]);
hasher.update(label.as_bytes());
hasher.update(&ordinal.to_be_bytes());
let digest = hasher.finalize();
let mut bytes = [0_u8; 8];
bytes.copy_from_slice(&digest.as_bytes()[..8]);
let child_seed = Seed::new(u64::from_be_bytes(bytes));
let scope_label = escape_scope_label(label);
Deterministic::from_parts(
child_seed,
SeedSource::Child,
format!("{}::{scope_label}#{ordinal}", self.scope),
)
}
#[must_use]
pub fn shared_child(&self, label: &str) -> Deterministic<Seed> {
let mut hasher = blake3::Hasher::new();
hasher.update(b"ee.determinism.shared_child.v1");
hasher.update(&self.seed.as_u64().to_be_bytes());
hasher.update(self.scope.as_bytes());
hasher.update(&[0]);
hasher.update(label.as_bytes());
let digest = hasher.finalize();
let mut bytes = [0_u8; 8];
bytes.copy_from_slice(&digest.as_bytes()[..8]);
let scope_label = escape_scope_label(label);
Deterministic::from_parts(
Seed::new(u64::from_be_bytes(bytes)),
SeedSource::Child,
format!("{}::{scope_label}", self.scope),
)
}
pub fn clock(&mut self) -> DeterministicClock<'_, S> {
DeterministicClock { token: self }
}
pub fn rng(&mut self) -> DeterministicRng<'_, S> {
DeterministicRng { token: self }
}
pub fn order(&mut self) -> DeterministicOrder<'_, S> {
DeterministicOrder { _token: self }
}
fn next_counter(&mut self) -> u64 {
let current = self.counter;
self.counter = match self.counter.checked_add(1) {
Some(next) => next,
None => panic!("deterministic token counter exhausted"),
};
current
}
fn next_word(&mut self, domain: &[u8]) -> u64 {
let ordinal = self.next_counter();
let mut hasher = blake3::Hasher::new();
hasher.update(b"ee.determinism.rng_word.v2");
hasher.update(&self.seed.as_u64().to_be_bytes());
hasher.update(&[0]);
hasher.update(self.scope.as_bytes());
hasher.update(&[0]);
hasher.update(domain);
hasher.update(&ordinal.to_be_bytes());
let digest = hasher.finalize();
let mut bytes = [0_u8; 8];
bytes.copy_from_slice(&digest.as_bytes()[..8]);
u64::from_be_bytes(bytes)
}
}
pub trait RandomnessConsumer {
fn consumer_kind(&self) -> &'static str;
}
pub struct DeterministicClock<'a, S = Seed> {
token: &'a mut Deterministic<S>,
}
impl<S> DeterministicClock<'_, S> {
#[must_use]
pub fn advance(&mut self) -> Timestamp {
let ordinal = self.token.next_counter();
let millis = self.token.seed.as_u64().saturating_add(ordinal);
let seconds = millis / 1_000;
let subsec_nanos = ((millis % 1_000) as u32).saturating_mul(1_000_000);
Timestamp::from_unix_time(seconds, subsec_nanos, ordinal as u128, UUID_COUNTER_BITS)
}
#[must_use]
pub fn next_uuid_v7(&mut self) -> Uuid {
let ordinal = self.token.counter;
let (seconds, nanos) = self.advance().to_unix();
let millis = seconds
.saturating_mul(1_000)
.saturating_add(u64::from(nanos) / 1_000_000);
let mut payload = [0_u8; 10];
payload[1] = (ordinal >> 62) as u8;
payload[2..].copy_from_slice(&ordinal.to_be_bytes());
Builder::from_unix_timestamp_millis(millis, &payload).into_uuid()
}
}
impl<S> RandomnessConsumer for DeterministicClock<'_, S> {
fn consumer_kind(&self) -> &'static str {
"deterministic_clock"
}
}
pub struct DeterministicRng<'a, S = Seed> {
token: &'a mut Deterministic<S>,
}
impl<S> DeterministicRng<'_, S> {
#[must_use]
pub fn next_u64(&mut self) -> u64 {
self.token.next_word(b"rng_u64")
}
pub fn fill_bytes(&mut self, output: &mut [u8]) {
for chunk in output.chunks_mut(8) {
let word = self.next_u64().to_be_bytes();
chunk.copy_from_slice(&word[..chunk.len()]);
}
}
}
impl<S> RandomnessConsumer for DeterministicRng<'_, S> {
fn consumer_kind(&self) -> &'static str {
"deterministic_rng"
}
}
pub struct DeterministicOrder<'a, S = Seed> {
_token: &'a mut Deterministic<S>,
}
impl<S> DeterministicOrder<'_, S> {
pub fn sort_by_key<T, K: Ord>(&mut self, values: &mut [T], mut key: impl FnMut(&T) -> K) {
values.sort_by_key(|value| key(value));
}
}
impl<S> RandomnessConsumer for DeterministicOrder<'_, S> {
fn consumer_kind(&self) -> &'static str {
"deterministic_order"
}
}
fn escape_scope_label(label: &str) -> String {
if !label
.as_bytes()
.iter()
.any(|byte| matches!(byte, b'%' | b':' | b'#'))
{
return label.to_owned();
}
let mut escaped = String::with_capacity(label.len());
for character in label.chars() {
match character {
'%' => escaped.push_str("%25"),
':' => escaped.push_str("%3A"),
'#' => escaped.push_str("%23"),
other => escaped.push(other),
}
}
escaped
}
fn hex_prefix(bytes: &[u8], chars: usize) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut output = String::with_capacity(chars);
for byte in bytes {
if output.len() >= chars {
break;
}
output.push(HEX[(byte >> 4) as usize] as char);
if output.len() >= chars {
break;
}
output.push(HEX[(byte & 0x0F) as usize] as char);
}
output
}
#[cfg(test)]
mod tests {
use super::{Deterministic, SeedSource, escape_scope_label};
type TestResult = Result<(), String>;
fn ensure_equal<T>(actual: &T, expected: &T, context: &str) -> TestResult
where
T: std::fmt::Debug + PartialEq,
{
if actual == expected {
Ok(())
} else {
Err(format!("{context}: expected {expected:?}, got {actual:?}"))
}
}
fn ensure_not_equal<T>(left: &T, right: &T, context: &str) -> TestResult
where
T: std::fmt::Debug + PartialEq,
{
if left != right {
Ok(())
} else {
Err(format!("{context}: both sides were {left:?}"))
}
}
#[test]
fn ordinary_child_scope_labels_remain_unchanged() -> TestResult {
let mut token = Deterministic::from_seed(7);
let child = token.child("retrieval");
let shared = token.shared_child("pack");
ensure_equal(&child.scope(), &"root::retrieval#0", "child scope")?;
ensure_equal(&shared.scope(), &"root::pack", "shared child scope")?;
ensure_equal(&child.source(), &SeedSource::Child, "child seed source")
}
#[test]
fn child_scope_labels_escape_path_delimiters() -> TestResult {
let mut direct_root = Deterministic::from_seed(7);
let direct = direct_root.child("a#0::b");
let mut nested_root = Deterministic::from_seed(7);
let mut parent = nested_root.child("a");
let nested = parent.child("b");
ensure_equal(
&direct.scope(),
&"root::a%230%3A%3Ab#0",
"escaped direct child scope",
)?;
ensure_equal(&nested.scope(), &"root::a#0::b#0", "nested child scope")?;
ensure_not_equal(&direct.scope(), &nested.scope(), "scopes must not collide")
}
#[test]
fn shared_child_scope_labels_escape_path_delimiters() -> TestResult {
let token = Deterministic::from_seed(7);
let shared = token.shared_child("a#0::b%tail");
ensure_equal(
&shared.scope(),
&"root::a%230%3A%3Ab%25tail",
"escaped shared child scope",
)
}
#[test]
fn scope_label_escape_is_stable_for_mixed_delimiters() -> TestResult {
ensure_equal(
&escape_scope_label("scope:%#tail"),
&"scope%3A%25%23tail".to_owned(),
"escaped mixed delimiter label",
)
}
#[test]
fn rng_words_domain_separate_seed_from_counter() -> TestResult {
let mut seed_one = Deterministic::from_seed(1);
let seed_one_first = seed_one.rng().next_u64();
let mut seed_zero = Deterministic::from_seed(0);
let _seed_zero_first = seed_zero.rng().next_u64();
let seed_zero_second = seed_zero.rng().next_u64();
ensure_not_equal(
&seed_one_first,
&seed_zero_second,
"seed and ordinal must not collapse to the same RNG stream position",
)
}
#[test]
fn uuid_clock_does_not_collapse_seed_and_counter() -> TestResult {
let mut seed_one = Deterministic::from_seed(1);
let first_at_one = seed_one.clock().next_uuid_v7();
let mut seed_zero = Deterministic::from_seed(0);
let _first_at_zero = seed_zero.clock().next_uuid_v7();
let second_at_zero = seed_zero.clock().next_uuid_v7();
ensure_not_equal(
&first_at_one,
&second_at_zero,
"distinct seed and ordinal pairs sharing a timestamp must retain their counters",
)
}
#[test]
fn uuid_clock_remains_unique_and_replayable_when_millis_saturate() -> TestResult {
let mut token = Deterministic::from_seed(u64::MAX);
let mut replay = Deterministic::from_seed(u64::MAX);
let mut previous = None;
for expected in [
"ffffffff-ffff-7000-8000-000000000000",
"ffffffff-ffff-7000-8000-000000000001",
"ffffffff-ffff-7000-8000-000000000002",
] {
let actual = token.clock().next_uuid_v7();
ensure_equal(
&actual.to_string(),
&expected.to_owned(),
"saturated timestamp must preserve the full counter",
)?;
ensure_equal(
&actual,
&replay.clock().next_uuid_v7(),
"saturated UUID sequence must replay exactly",
)?;
if previous.is_some_and(|previous| previous >= actual) {
return Err("saturated UUID sequence must remain strictly increasing".to_owned());
}
previous = Some(actual);
}
Ok(())
}
#[test]
fn uuid_clock_preserves_counter_bits_across_the_variant() -> TestResult {
for (ordinal, expected) in [
((1_u64 << 62) - 1, "ffffffff-ffff-7000-bfff-ffffffffffff"),
(1_u64 << 62, "ffffffff-ffff-7001-8000-000000000000"),
((1_u64 << 62) + 1, "ffffffff-ffff-7001-8000-000000000001"),
((1_u64 << 63) - 1, "ffffffff-ffff-7001-bfff-ffffffffffff"),
(1_u64 << 63, "ffffffff-ffff-7002-8000-000000000000"),
(u64::MAX - 1, "ffffffff-ffff-7003-bfff-fffffffffffe"),
] {
let mut token = Deterministic::from_seed(u64::MAX);
token.counter = ordinal;
ensure_equal(
&token.clock().next_uuid_v7().to_string(),
&expected.to_owned(),
"counter bits must survive UUID version and variant insertion",
)?;
ensure_equal(
&token.counter,
&(ordinal + 1),
"UUID generation must consume exactly one ordinal",
)?;
}
Ok(())
}
#[test]
#[should_panic(expected = "deterministic token counter exhausted")]
fn uuid_clock_exhaustion_panics_instead_of_reusing_scope_ordinals() {
let mut token = Deterministic::from_seed(7);
token.counter = u64::MAX;
let _ = token.clock().next_uuid_v7();
}
#[test]
fn counter_boundary_advances_without_silent_saturation() -> TestResult {
let mut token = Deterministic::from_seed(7);
token.counter = u64::MAX - 1;
let child = token.child("last");
let expected_last_scope = format!("root::last#{}", u64::MAX - 1);
ensure_equal(
&child.scope(),
&expected_last_scope.as_str(),
"last representable child scope before exhaustion",
)?;
ensure_equal(
&token.counter,
&u64::MAX,
"parent counter reaches terminal sentinel",
)
}
#[test]
#[should_panic(expected = "deterministic token counter exhausted")]
fn counter_exhaustion_panics_instead_of_reusing_scope_ordinals() {
let mut token = Deterministic::from_seed(7);
token.counter = u64::MAX;
let _ = token.child("collision");
}
}