Skip to main content

causa_kernel/context/
ids.rs

1use serde::{Deserialize, Serialize};
2
3/// Unique identity of a turn. Opaque string; scopes every block, sequence,
4/// and version belonging to that turn, and is checked on every model-door
5/// invocation.
6#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub struct TurnId(pub String);
8impl TurnId {
9    /// Wraps an arbitrary string as a turn id.
10    pub fn new(s: impl Into<String>) -> Self {
11        Self(s.into())
12    }
13}
14
15/// Ordinal of a model round within a turn (0-based). Enters the frame-identity
16/// and tool-call-id hash preimages, so the same logical call in different
17/// rounds never collides.
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
19pub struct RoundId(pub u32);
20
21/// Identity of one model invocation: which turn, and which round inside it.
22#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
23pub struct InvocationId {
24    /// The turn the invocation belongs to.
25    pub turn_id: TurnId,
26    /// The round within the turn.
27    pub round_id: RoundId,
28}
29
30/// Monotonic per-turn block ordinal, assigned once when the fact machine
31/// commits a block; dense and gap-free (validated on replay).
32#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
33pub struct BlockSequence(pub u64);
34
35/// Counts canonical fact commits of a turn; bumps exactly once per non-empty
36/// commit and pins the frame identity for a round.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
38pub struct ContextVersion(pub u64);
39impl ContextVersion {
40    /// Returns the successor version.
41    pub fn next(self) -> Self {
42        Self(self.0 + 1)
43    }
44}
45
46/// Unique identity of a fact block: the owning turn plus the block's dense
47/// position within that turn.
48#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
49pub struct BlockId {
50    /// The turn the block belongs to.
51    pub turn_id: TurnId,
52    /// The block's position in the turn's monotonic sequence.
53    pub sequence: BlockSequence,
54}
55
56/// Frame identity/provenance tag. `Turn` scopes a single-turn projection;
57/// `Conversation` scopes the lossless merged view (history + active turn).
58/// Adding a scope variant is additive; the scope never changes block-level
59/// operation rules.
60#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
61pub enum FrameScope {
62    /// Single-turn projection: only one turn's committed facts.
63    Turn {
64        /// The turn being projected.
65        turn_id: TurnId,
66        /// The turn's `ContextVersion` the frame was built from; part of the
67        /// frame-identity preimage.
68        source_version: ContextVersion,
69    },
70    /// Lossless merged view: conversation history plus the active turn.
71    Conversation {
72        /// The conversation being projected.
73        conversation_id: ConversationId,
74        /// The turn currently active in the conversation.
75        active_turn_id: TurnId,
76        /// The active turn's `ContextVersion`. History snapshots are
77        /// immutable; their session ordering lives in the runtime's
78        /// `HistoryEntry`. Within one round the
79        /// (conversation_id, active_turn_id, source_version) triple is
80        /// constant, so it pins the frame input.
81        source_version: ContextVersion,
82    },
83}
84
85/// Deterministic frame identity: a blake3 digest of the scope and round,
86/// truncated to 16 hex chars. Equal `(scope, round)` inputs always yield the
87/// same id; see `from_scope` for the preimage formats.
88#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
89pub struct FrameId(pub String);
90impl FrameId {
91    /// Scope-driven deterministic derivation — the canonical entry. The Turn
92    /// branch replicates the historical preimage byte-for-byte
93    /// (`turn|version|round`, colon-separated) so historical frame ids are
94    /// unchanged.
95    pub fn from_scope(scope: &FrameScope, round_id: RoundId) -> Self {
96        let input = match scope {
97            FrameScope::Turn {
98                turn_id,
99                source_version,
100            } => format!("{}:{}:{}", turn_id.0, source_version.0, round_id.0),
101            FrameScope::Conversation {
102                conversation_id,
103                active_turn_id,
104                source_version,
105            } => format!(
106                "conversation|{}|{}|{}|{}",
107                conversation_id.0, active_turn_id.0, source_version.0, round_id.0
108            ),
109        };
110        let hex = blake3::hash(input.as_bytes()).to_hex();
111        Self(hex[..16].to_string())
112    }
113
114    /// Turn-scope thin wrapper, pinned equal to `from_scope(Turn)` by test.
115    pub fn deterministic(
116        turn_id: &TurnId,
117        source_version: ContextVersion,
118        round_id: RoundId,
119    ) -> Self {
120        Self::from_scope(
121            &FrameScope::Turn {
122                turn_id: turn_id.clone(),
123                source_version,
124            },
125            round_id,
126        )
127    }
128}
129
130/// Unique identity of a conversation aggregate. Opaque string; scopes the
131/// conversation's turns, sequences, and versions.
132#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
133pub struct ConversationId(pub String);