1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Async CRUD storage for namespaced JSON configuration documents.
use async_trait::async_trait;
use serde_json::Value;
use super::storage::StorageError;
/// Async CRUD store for namespaced JSON configuration documents.
#[async_trait]
pub trait ConfigStore: Send + Sync {
/// Get a single entry by namespace and ID.
async fn get(&self, namespace: &str, id: &str) -> Result<Option<Value>, StorageError>;
/// List entries in a namespace ordered by ID.
async fn list(
&self,
namespace: &str,
offset: usize,
limit: usize,
) -> Result<Vec<(String, Value)>, StorageError>;
/// Create or overwrite an entry.
async fn put(&self, namespace: &str, id: &str, value: &Value) -> Result<(), StorageError>;
/// Delete an entry. Missing entries are not an error.
async fn delete(&self, namespace: &str, id: &str) -> Result<(), StorageError>;
/// Create an entry only when it does not already exist.
///
/// Production stores should override this with an atomic implementation.
/// The default implementation is best-effort and exists for lightweight
/// test stores.
async fn put_if_absent(
&self,
namespace: &str,
id: &str,
value: &Value,
) -> Result<(), StorageError> {
if self.exists(namespace, id).await? {
return Err(StorageError::AlreadyExists(format!("{namespace}/{id}")));
}
self.put(namespace, id, value).await
}
/// Check whether an entry exists.
async fn exists(&self, namespace: &str, id: &str) -> Result<bool, StorageError> {
Ok(self.get(namespace, id).await?.is_some())
}
/// Atomic compare-and-set on the record's `meta.revision`.
///
/// Reads the existing record at `(namespace, id)`, extracts the integer
/// at JSON path `meta.revision` (defaulting to 0 if absent), and:
///
/// - if it equals `expected_revision`, replaces the record with `value`
/// and returns `Ok(())`.
/// - if it differs, returns
/// `Err(StorageError::VersionConflict { expected, actual })`.
/// - if no existing record AND `expected_revision == 0`, creates the
/// record (treats absence as revision 0).
///
/// The new revision is the caller's responsibility: write `value` with
/// `meta.revision = expected_revision + 1` before calling this method.
/// The store does not modify the value it stores; it only enforces the
/// CAS predicate.
///
/// **Implementations MUST guarantee atomicity** with respect to
/// concurrent `put` / `put_if_revision` / `delete` against the same
/// `(namespace, id)`. The default implementation here is best-effort
/// (read-then-put, racy across replicas) and is provided so existing
/// `ConfigStore` impls compile without modification — production-grade
/// stores must override.
async fn put_if_revision(
&self,
namespace: &str,
id: &str,
value: &Value,
expected_revision: u64,
) -> Result<(), StorageError> {
let actual = self
.get(namespace, id)
.await?
.as_ref()
.and_then(extract_meta_revision)
.unwrap_or(0);
if actual != expected_revision {
return Err(StorageError::VersionConflict {
expected: expected_revision,
actual,
});
}
self.put(namespace, id, value).await
}
/// Delete only when the current record's `meta.revision` matches
/// `expected_revision`.
///
/// Production stores must make this atomic with concurrent writes to the
/// same `(namespace, id)`. The default implementation is best-effort for
/// simple test stores.
async fn delete_if_revision(
&self,
namespace: &str,
id: &str,
expected_revision: u64,
) -> Result<(), StorageError> {
let actual = self
.get(namespace, id)
.await?
.as_ref()
.and_then(extract_meta_revision)
.unwrap_or(0);
if actual != expected_revision {
return Err(StorageError::VersionConflict {
expected: expected_revision,
actual,
});
}
self.delete(namespace, id).await
}
}
/// Extract the `meta.revision` integer from a stored config document.
///
/// Returns `None` if the path does not exist or is not an integer (e.g. legacy
/// bare-spec documents that predate the envelope format).
pub fn extract_meta_revision(value: &Value) -> Option<u64> {
value
.get("meta")
.and_then(|m| m.get("revision"))
.and_then(|r| r.as_u64())
}
/// Type of config mutation that was published by a store notification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConfigChangeKind {
Put,
Delete,
}
/// A config change notification emitted by a store implementation.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ConfigChangeEvent {
pub namespace: String,
pub id: String,
pub kind: ConfigChangeKind,
}
/// Blocking/streaming receiver for store-native config change notifications.
#[async_trait]
pub trait ConfigChangeSubscriber: Send {
async fn next(&mut self) -> Result<ConfigChangeEvent, StorageError>;
}
/// Optional native notification capability for a [`ConfigStore`].
///
/// Stores that can push change events (for example PostgreSQL LISTEN/NOTIFY)
/// should implement this in addition to [`ConfigStore`]. Callers should still
/// keep a polling fallback because notifications may be delayed or unavailable.
#[async_trait]
pub trait ConfigChangeNotifier: Send + Sync {
async fn subscribe(&self) -> Result<Box<dyn ConfigChangeSubscriber>, StorageError>;
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use super::*;
// ── extract_meta_revision ────────────────────────────────────────
#[test]
fn extract_meta_revision_returns_some_for_valid_path() {
let value = serde_json::json!({"meta": {"revision": 42}});
assert_eq!(extract_meta_revision(&value), Some(42));
}
#[test]
fn extract_meta_revision_returns_none_for_missing_meta() {
let value = serde_json::json!({"spec": {"id": "foo"}});
assert_eq!(extract_meta_revision(&value), None);
}
#[test]
fn extract_meta_revision_returns_none_for_missing_revision_key() {
let value = serde_json::json!({"meta": {"source": {"kind": "user"}}});
assert_eq!(extract_meta_revision(&value), None);
}
#[test]
fn extract_meta_revision_returns_none_for_wrong_type() {
let value = serde_json::json!({"meta": {"revision": "not-a-number"}});
assert_eq!(extract_meta_revision(&value), None);
}
#[test]
fn extract_meta_revision_returns_none_for_bare_spec() {
// Legacy bare-spec shape (no envelope).
let value = serde_json::json!({"id": "agent-1", "name": "Alice"});
assert_eq!(extract_meta_revision(&value), None);
}
#[derive(Debug, Default)]
struct MemoryConfigStore {
data: RwLock<HashMap<String, HashMap<String, Value>>>,
}
#[async_trait]
impl ConfigStore for MemoryConfigStore {
async fn get(&self, namespace: &str, id: &str) -> Result<Option<Value>, StorageError> {
let data = self.data.read().await;
Ok(data.get(namespace).and_then(|ns| ns.get(id)).cloned())
}
async fn list(
&self,
namespace: &str,
offset: usize,
limit: usize,
) -> Result<Vec<(String, Value)>, StorageError> {
let data = self.data.read().await;
let Some(namespace_data) = data.get(namespace) else {
return Ok(Vec::new());
};
let mut items: Vec<_> = namespace_data
.iter()
.map(|(id, value)| (id.clone(), value.clone()))
.collect();
items.sort_by(|left, right| left.0.cmp(&right.0));
Ok(items.into_iter().skip(offset).take(limit).collect())
}
async fn put(&self, namespace: &str, id: &str, value: &Value) -> Result<(), StorageError> {
let mut data = self.data.write().await;
data.entry(namespace.to_string())
.or_default()
.insert(id.to_string(), value.clone());
Ok(())
}
async fn delete(&self, namespace: &str, id: &str) -> Result<(), StorageError> {
let mut data = self.data.write().await;
if let Some(namespace_data) = data.get_mut(namespace) {
namespace_data.remove(id);
}
Ok(())
}
}
#[tokio::test]
async fn config_store_round_trip() {
let store: Arc<dyn ConfigStore> = Arc::new(MemoryConfigStore::default());
let value = serde_json::json!({"id": "alpha", "label": "first"});
store.put("tests", "alpha", &value).await.unwrap();
assert_eq!(store.get("tests", "alpha").await.unwrap(), Some(value));
}
#[tokio::test]
async fn config_store_lists_sorted_entries() {
let store: Arc<dyn ConfigStore> = Arc::new(MemoryConfigStore::default());
store
.put(
"tests",
"bravo",
&serde_json::json!({"id": "bravo", "label": "second"}),
)
.await
.unwrap();
store
.put(
"tests",
"alpha",
&serde_json::json!({"id": "alpha", "label": "first"}),
)
.await
.unwrap();
let items = store.list("tests", 0, 10).await.unwrap();
assert_eq!(items[0].0, "alpha");
assert_eq!(items[1].0, "bravo");
}
}