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
//! Session key-value state store with delta tracking.
//!
//! Provides [`SessionState`] for per-session structured data that tools can
//! read/write during execution, and [`StateDelta`] for tracking mutations
//! since the last flush. State is shared via `Arc<RwLock<SessionState>>`.
#![forbid(unsafe_code)]
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
// ─── StateDelta ─────────────────────────────────────────────────────────────
/// Record of mutations since the last flush.
///
/// `Some(value)` = set/update, `None` = removed.
#[non_exhaustive]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct StateDelta {
/// Map of changed keys. `Some(v)` means the key was set to `v`;
/// `None` means the key was removed.
pub changes: HashMap<String, Option<Value>>,
}
impl StateDelta {
/// Create an empty delta.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Build a delta from a complete change map.
///
/// `Some(value)` entries mean the key was set to `value`; `None` entries
/// mean the key was removed.
#[must_use]
pub fn from_changes(changes: HashMap<String, Option<Value>>) -> Self {
Self { changes }
}
/// Record a single change, chainable builder-style.
///
/// Pass `Some(value)` to record a set/update of `key`, or `None` to
/// record its removal.
#[must_use]
pub fn with_change(mut self, key: impl Into<String>, value: Option<Value>) -> Self {
self.changes.insert(key.into(), value);
self
}
/// True if no changes recorded.
pub fn is_empty(&self) -> bool {
self.changes.is_empty()
}
/// Number of changed keys.
pub fn len(&self) -> usize {
self.changes.len()
}
}
// ─── SessionState ───────────────────────────────────────────────────────────
/// Key-value store with change tracking for session-attached structured data.
///
/// Tools receive an `Arc<RwLock<SessionState>>` during execution and can
/// read/write arbitrary typed values. Changes are tracked in a [`StateDelta`]
/// that is flushed at the end of each turn.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SessionState {
data: HashMap<String, Value>,
#[serde(skip)]
delta: StateDelta,
}
impl SessionState {
/// Create a new empty session state.
pub fn new() -> Self {
Self::default()
}
/// Create session state pre-populated with the given data.
///
/// Pre-seeded data does NOT appear in the delta (baseline semantics).
pub fn with_data(data: HashMap<String, Value>) -> Self {
Self {
data,
delta: StateDelta::default(),
}
}
/// Layer baseline entries underneath the existing data.
///
/// For each key in `baseline` that is absent from this state, the
/// baseline value is inserted. Existing entries win: keys already
/// present keep their current value and are never overridden by the
/// baseline. Inserted baseline entries do NOT appear in the delta,
/// mirroring [`Self::with_data`] baseline semantics.
pub fn apply_baseline(&mut self, baseline: &Self) {
for (key, value) in &baseline.data {
if !self.data.contains_key(key) {
self.data.insert(key.clone(), value.clone());
}
}
}
/// Get a typed value by key. Returns `None` if key is missing or
/// deserialization fails.
pub fn get<T: serde::de::DeserializeOwned>(&self, key: &str) -> Option<T> {
self.data
.get(key)
.and_then(|v| serde_json::from_value(v.clone()).ok())
}
/// Get the raw JSON value by key without deserialization.
pub fn get_raw(&self, key: &str) -> Option<&Value> {
self.data.get(key)
}
/// Set a typed value. Serializes to `Value` and records in delta.
///
/// Returns an error if the value cannot be serialized to JSON.
pub fn set<T: Serialize>(&mut self, key: &str, value: T) -> Result<(), serde_json::Error> {
let val = serde_json::to_value(value)?;
self.data.insert(key.to_string(), val.clone());
self.delta.changes.insert(key.to_string(), Some(val));
Ok(())
}
/// Remove a key. Records removal in delta. No-op if key absent.
pub fn remove(&mut self, key: &str) {
if self.data.remove(key).is_some() {
self.delta.changes.insert(key.to_string(), None);
}
}
/// Check if a key exists.
pub fn contains(&self, key: &str) -> bool {
self.data.contains_key(key)
}
/// Iterate over all keys.
pub fn keys(&self) -> impl Iterator<Item = &str> {
self.data.keys().map(String::as_str)
}
/// Number of key-value pairs.
pub fn len(&self) -> usize {
self.data.len()
}
/// True if no key-value pairs.
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
/// Remove all key-value pairs. Records all existing keys as removed in delta.
pub fn clear(&mut self) {
for key in self.data.keys() {
self.delta.changes.insert(key.clone(), None);
}
self.data.clear();
}
/// Read-only reference to pending delta.
pub const fn delta(&self) -> &StateDelta {
&self.delta
}
/// Take the pending delta and reset tracking. Returns the delta.
pub fn flush_delta(&mut self) -> StateDelta {
std::mem::take(&mut self.delta)
}
/// Snapshot the materialized data as a JSON Value (for persistence).
pub fn snapshot(&self) -> Value {
serde_json::to_value(&self.data).expect("HashMap<String, Value> is always serializable")
}
/// Restore from a JSON Value snapshot. Returns a new `SessionState` with
/// empty delta.
pub fn restore_from_snapshot(snapshot: Value) -> Result<Self, serde_json::Error> {
let data: HashMap<String, Value> = serde_json::from_value(snapshot)?;
Ok(Self {
data,
delta: StateDelta::default(),
})
}
}
// ─── Compile-time Send + Sync assertions ────────────────────────────────────
const _: () = {
const fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<SessionState>();
assert_send_sync::<StateDelta>();
assert_send_sync::<std::sync::Arc<std::sync::RwLock<SessionState>>>();
};
// ─── Tests ──────────────────────────────────────────────────────────────────
#[cfg(test)]
#[path = "state_tests.rs"]
mod tests;