memstead_base/provenance.rs
1//! Backend-neutral provenance record.
2//!
3//! Two persistence shapes exist today — commit-message trailer
4//! (git-branch backend) and JSONL line (folder backend's
5//! `.memstead/changes.jsonl`) — and historically each backend modelled
6//! its mutation log with its own type. After the workspace-store
7//! rebuild both adapters construct (and are read into) this single
8//! [`Provenance`] record so `memstead_changes_since` returns
9//! identically-shaped values regardless of which backend serves the
10//! queried mem.
11//!
12//! This module ships the **shape**; the read/write wiring on each
13//! backend lands as that backend gains a [`crate::backend::MemBackend`]
14//! implementation. The existing `crate::filesystem::changelog`
15//! `ChangeEntry` / `MutationKind` pair stays as the folder backend's
16//! on-disk encoder until that wiring lands; the two are kept in
17//! lockstep by deliberate field correspondence (timestamp, kind,
18//! entity, actor, client, note).
19
20use std::time::SystemTime;
21
22use crate::vcs::{Actor, ClientId};
23
24/// Mutation kind written to provenance. The string forms produced by
25/// [`Self::as_str`] are the wire shape — readers and external tools
26/// (jq, grep) branch on them.
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum ProvenanceKind {
29 Create,
30 Update,
31 Delete,
32 Relate,
33 Rename,
34 Batch,
35}
36
37impl ProvenanceKind {
38 /// Stable kebab-case wire form.
39 pub fn as_str(&self) -> &'static str {
40 match self {
41 ProvenanceKind::Create => "create",
42 ProvenanceKind::Update => "update",
43 ProvenanceKind::Delete => "delete",
44 ProvenanceKind::Relate => "relate",
45 ProvenanceKind::Rename => "rename",
46 ProvenanceKind::Batch => "batch",
47 }
48 }
49
50 /// Inverse of [`Self::as_str`]. Returns `None` for any unknown
51 /// string so backend readers can treat unrecognised kinds as a
52 /// forward-compat extension rather than misclassify.
53 pub fn parse(s: &str) -> Option<Self> {
54 match s {
55 "create" => Some(ProvenanceKind::Create),
56 "update" => Some(ProvenanceKind::Update),
57 "delete" => Some(ProvenanceKind::Delete),
58 "relate" => Some(ProvenanceKind::Relate),
59 "rename" => Some(ProvenanceKind::Rename),
60 "batch" => Some(ProvenanceKind::Batch),
61 _ => None,
62 }
63 }
64}
65
66/// One mutation event in a mem's provenance log.
67///
68/// Constructed at the engine boundary (one per MCP mutating tool, one
69/// per CLI mutation, one per drift-flush) and handed to the backend
70/// via [`crate::backend::MemBackend::append_provenance`]. Read back
71/// out via [`crate::backend::MemBackend::read_provenance`] for
72/// `memstead_changes_since`.
73///
74/// The folder backend persists this as a JSONL line under
75/// `.memstead/changes.jsonl`; the git-branch backend persists it as part
76/// of the commit-message trailer block (timestamp / kind / entity ride
77/// the commit metadata). The persistence form differs per backend, the
78/// in-memory record does not.
79#[derive(Debug, Clone)]
80pub struct Provenance {
81 pub timestamp: SystemTime,
82 pub kind: ProvenanceKind,
83 /// Mem-relative entity id (`mem:slug`), or `None` for batch
84 /// mutations that touch multiple entities.
85 pub entity: Option<String>,
86 pub actor: Actor,
87 pub client: Option<ClientId>,
88 /// Agent-authored one-sentence provenance note. Whitespace-only
89 /// values are normalised to `None` at construction; callers that
90 /// want an empty note pass `None`.
91 pub note: Option<String>,
92 /// Correlation id that ties every commit produced by a single
93 /// logical operation (notably a multi-mem `memstead_rename`) to one
94 /// another. `Some(id)` on every commit a single logical call
95 /// produced; `None` on legacy or single-call mutations that don't
96 /// participate in correlation. Consumers that don't know the
97 /// field continue working — it's purely additive. Single-mem
98 /// mutations may carry an id too (a logical-op with one commit),
99 /// or `None` — both are valid wire shapes.
100 pub logical_operation_id: Option<String>,
101 /// The caller-declared role (agent-trust plan 13).
102 /// `Unspecified` records as absence on both backends (no trailer,
103 /// no ledger field) — old records read back as `Unspecified`.
104 pub role: crate::vcs::Role,
105}
106
107impl Provenance {
108 /// Build a record, normalising a whitespace-only `note` to `None`.
109 /// Callers that already have a normalised `Option<String>` may set
110 /// the field directly. `logical_operation_id` defaults to `None`;
111 /// callers that need to tag a multi-commit logical operation use
112 /// [`Self::with_logical_operation_id`].
113 pub fn new(
114 timestamp: SystemTime,
115 kind: ProvenanceKind,
116 entity: Option<String>,
117 actor: Actor,
118 client: Option<ClientId>,
119 note: Option<String>,
120 ) -> Self {
121 let note = note
122 .as_deref()
123 .map(str::trim)
124 .filter(|n| !n.is_empty())
125 .map(|s| s.to_string());
126 Self {
127 timestamp,
128 kind,
129 entity,
130 actor,
131 client,
132 note,
133 logical_operation_id: None,
134 role: crate::vcs::Role::Unspecified,
135 }
136 }
137
138 /// Builder: attach the caller-declared role (agent-trust plan 13).
139 pub fn with_role(mut self, role: crate::vcs::Role) -> Self {
140 self.role = role;
141 self
142 }
143
144 /// Builder: attach a correlation id so multiple commits produced
145 /// by a single logical operation can be linked at read time.
146 pub fn with_logical_operation_id(mut self, id: String) -> Self {
147 self.logical_operation_id = Some(id);
148 self
149 }
150}
151
152/// Mint a fresh `logical_operation_id`. Combines a nanosecond-
153/// precision timestamp with a process-monotonic counter so two ids
154/// produced in the same nanosecond are still distinct, and the
155/// timestamp prefix gives consumers a rough ordering hint without
156/// a dedicated comparator. Mirrors the shape of
157/// `make_commit_id` in the filesystem backend.
158pub fn mint_logical_operation_id() -> String {
159 use std::sync::atomic::{AtomicU64, Ordering};
160 static LOGICAL_OP_COUNTER: AtomicU64 = AtomicU64::new(0);
161 let nanos = SystemTime::now()
162 .duration_since(std::time::UNIX_EPOCH)
163 .map(|d| d.as_nanos())
164 .unwrap_or(0);
165 let counter = LOGICAL_OP_COUNTER.fetch_add(1, Ordering::Relaxed);
166 format!("logop-{nanos:032x}{counter:016x}")
167}
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172
173 #[test]
174 fn kind_wire_strings_are_stable() {
175 // Locks the wire shape — readers (jq, external tools) key on
176 // these exact strings.
177 assert_eq!(ProvenanceKind::Create.as_str(), "create");
178 assert_eq!(ProvenanceKind::Update.as_str(), "update");
179 assert_eq!(ProvenanceKind::Delete.as_str(), "delete");
180 assert_eq!(ProvenanceKind::Relate.as_str(), "relate");
181 assert_eq!(ProvenanceKind::Rename.as_str(), "rename");
182 assert_eq!(ProvenanceKind::Batch.as_str(), "batch");
183 }
184
185 #[test]
186 fn new_normalises_whitespace_only_note_to_none() {
187 let r = Provenance::new(
188 SystemTime::UNIX_EPOCH,
189 ProvenanceKind::Create,
190 Some("v:e".into()),
191 Actor::Cli,
192 None,
193 Some(" \t ".into()),
194 );
195 assert!(r.note.is_none());
196 }
197
198 #[test]
199 fn new_preserves_non_blank_note() {
200 let r = Provenance::new(
201 SystemTime::UNIX_EPOCH,
202 ProvenanceKind::Create,
203 Some("v:e".into()),
204 Actor::Cli,
205 None,
206 Some(" first draft ".into()),
207 );
208 assert_eq!(r.note.as_deref(), Some("first draft"));
209 }
210}