swink-agent 0.10.0

Core scaffolding for running LLM-powered agentic loops
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! 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.
#[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 {
    /// 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)]
mod tests {
    use super::*;
    use serde_json::json;

    // ── StateDelta ──

    #[test]
    fn delta_default_is_empty() {
        let d = StateDelta::default();
        assert!(d.is_empty());
        assert_eq!(d.len(), 0);
    }

    #[test]
    fn delta_serde_roundtrip() {
        let mut d = StateDelta::default();
        d.changes.insert("a".into(), Some(json!(1)));
        d.changes.insert("b".into(), None);
        let json = serde_json::to_string(&d).unwrap();
        let d2: StateDelta = serde_json::from_str(&json).unwrap();
        assert_eq!(d2.len(), 2);
        assert_eq!(d2.changes["a"], Some(json!(1)));
        assert_eq!(d2.changes["b"], None);
    }

    // ── SessionState get/set/remove ──

    #[test]
    fn set_and_get_typed() {
        let mut s = SessionState::new();
        s.set("count", 42_i64).unwrap();
        assert_eq!(s.get::<i64>("count"), Some(42));
    }

    #[test]
    fn get_raw_returns_value_ref() {
        let mut s = SessionState::new();
        s.set("key", "hello").unwrap();
        assert_eq!(s.get_raw("key"), Some(&json!("hello")));
    }

    #[test]
    fn get_missing_returns_none() {
        let s = SessionState::new();
        assert_eq!(s.get::<String>("nope"), None);
    }

    #[test]
    fn get_wrong_type_returns_none() {
        let mut s = SessionState::new();
        s.set("key", "hello").unwrap();
        // Try to get as i64 — should fail gracefully
        assert_eq!(s.get::<i64>("key"), None);
        // Original value still intact
        assert_eq!(s.get::<String>("key"), Some("hello".to_string()));
    }

    #[test]
    fn remove_existing_key() {
        let mut s = SessionState::new();
        s.set("x", 1).unwrap();
        s.remove("x");
        assert!(!s.contains("x"));
        assert!(s.is_empty());
    }

    #[test]
    fn remove_absent_key_is_noop() {
        let mut s = SessionState::new();
        s.remove("nope");
        assert!(s.delta().is_empty());
    }

    #[test]
    fn contains_keys_len_is_empty() {
        let mut s = SessionState::new();
        assert!(s.is_empty());
        s.set("a", 1).unwrap();
        s.set("b", 2).unwrap();
        assert!(s.contains("a"));
        assert!(!s.contains("c"));
        assert_eq!(s.len(), 2);
        assert!(!s.is_empty());
        let keys: Vec<&str> = s.keys().collect();
        assert!(keys.contains(&"a"));
        assert!(keys.contains(&"b"));
    }

    #[test]
    fn clear_records_all_removals() {
        let mut s = SessionState::new();
        s.set("a", 1).unwrap();
        s.set("b", 2).unwrap();
        s.flush_delta(); // reset
        s.clear();
        assert!(s.is_empty());
        assert_eq!(s.delta().len(), 2);
        assert_eq!(s.delta().changes["a"], None);
        assert_eq!(s.delta().changes["b"], None);
    }

    // ── Delta collapse ──

    #[test]
    fn delta_set_set_last_wins() {
        let mut s = SessionState::new();
        s.set("k", 1).unwrap();
        s.set("k", 2).unwrap();
        assert_eq!(s.delta().changes["k"], Some(json!(2)));
        assert_eq!(s.delta().len(), 1);
    }

    #[test]
    fn delta_set_remove_is_none() {
        let mut s = SessionState::new();
        s.set("k", 1).unwrap();
        s.remove("k");
        assert_eq!(s.delta().changes["k"], None);
    }

    #[test]
    fn delta_remove_set_is_some() {
        let mut s = SessionState::with_data(std::iter::once(("k".to_string(), json!(1))).collect());
        s.remove("k");
        s.set("k", 99).unwrap();
        assert_eq!(s.delta().changes["k"], Some(json!(99)));
    }

    // ── flush_delta ──

    #[test]
    fn flush_delta_returns_and_resets() {
        let mut s = SessionState::new();
        s.set("a", 1).unwrap();
        let d = s.flush_delta();
        assert_eq!(d.len(), 1);
        assert!(s.delta().is_empty());
    }

    #[test]
    fn flush_empty_delta_returns_empty() {
        let mut s = SessionState::new();
        let d = s.flush_delta();
        assert!(d.is_empty());
    }

    // ── with_data (baseline semantics) ──

    #[test]
    fn with_data_pre_seeds_without_delta() {
        let data: HashMap<String, Value> = std::iter::once(("x".into(), json!(42))).collect();
        let s = SessionState::with_data(data);
        assert_eq!(s.get::<i64>("x"), Some(42));
        assert!(s.delta().is_empty());
    }

    // ── apply_baseline (baseline underlies, existing wins) ──

    #[test]
    fn apply_baseline_inserts_missing_keys_without_delta() {
        let mut s = SessionState::new();
        s.set("mine", "kept").unwrap();
        s.flush_delta(); // reset so only apply_baseline effects are observed

        let baseline = SessionState::with_data(
            [
                ("mine".to_string(), json!("overridden?")),
                ("extra".to_string(), json!("from baseline")),
            ]
            .into_iter()
            .collect(),
        );
        s.apply_baseline(&baseline);

        // Missing key filled in from the baseline.
        assert_eq!(s.get::<String>("extra"), Some("from baseline".to_string()));
        // Existing entry wins over the baseline.
        assert_eq!(s.get::<String>("mine"), Some("kept".to_string()));
        // Baseline inserts are NOT recorded in the delta.
        assert!(s.delta().is_empty());
    }

    #[test]
    fn apply_baseline_into_empty_state_copies_all_keys() {
        let baseline = SessionState::with_data(
            [("a".to_string(), json!(1)), ("b".to_string(), json!("two"))]
                .into_iter()
                .collect(),
        );
        let mut s = SessionState::new();
        s.apply_baseline(&baseline);
        assert_eq!(s.get::<i64>("a"), Some(1));
        assert_eq!(s.get::<String>("b"), Some("two".to_string()));
        assert!(s.delta().is_empty());
    }

    // ── snapshot / restore ──

    #[test]
    fn snapshot_restore_roundtrip() {
        let mut s = SessionState::new();
        s.set("name", "alice").unwrap();
        s.set("age", 30).unwrap();
        let snap = s.snapshot();
        let s2 = SessionState::restore_from_snapshot(snap).unwrap();
        assert_eq!(s2.get::<String>("name"), Some("alice".to_string()));
        assert_eq!(s2.get::<i64>("age"), Some(30));
        assert!(s2.delta().is_empty());
    }

    // ── Serialize roundtrip (delta skipped) ──

    #[test]
    fn serde_roundtrip_skips_delta() {
        let mut s = SessionState::new();
        s.set("k", "v").unwrap();
        // Delta has an entry
        assert!(!s.delta().is_empty());
        let json = serde_json::to_string(&s).unwrap();
        let s2: SessionState = serde_json::from_str(&json).unwrap();
        assert_eq!(s2.get::<String>("k"), Some("v".to_string()));
        // Delta is empty after deserialization (skipped)
        assert!(s2.delta().is_empty());
    }

    // ── Serialization error handling ──

    #[test]
    fn set_returns_error_on_serialization_failure() {
        use serde::ser::{self, Serializer};

        /// A type whose `Serialize` impl always fails.
        struct Unserializable;

        impl Serialize for Unserializable {
            fn serialize<S: Serializer>(&self, _s: S) -> Result<S::Ok, S::Error> {
                Err(ser::Error::custom("intentional serialization failure"))
            }
        }

        let mut s = SessionState::new();
        let result = s.set("bad", Unserializable);
        assert!(result.is_err());
        // State must remain unchanged after a failed set.
        assert!(!s.contains("bad"));
        assert!(s.delta().is_empty());
    }

    // ── Nested JSON values ──

    #[test]
    fn nested_json_roundtrip() {
        let mut s = SessionState::new();
        let nested = json!({
            "user": {"name": "bob", "scores": [1, 2, 3]},
            "active": true
        });
        s.set("profile", nested.clone()).unwrap();
        let snap = s.snapshot();
        let s2 = SessionState::restore_from_snapshot(snap).unwrap();
        assert_eq!(s2.get_raw("profile"), Some(&nested));
    }

    #[test]
    fn restore_from_corrupt_snapshot_returns_error() {
        let err = SessionState::restore_from_snapshot(json!(["not", "an", "object"])).unwrap_err();
        assert!(err.to_string().contains("map"));
    }
}