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
//! Session storage trait for pluggable persistence backends.
use std::io;
use std::sync::Once;
use swink_agent::{AgentMessage, CustomMessageRegistry};
use crate::entry::SessionEntry;
use crate::interrupt::InterruptState;
use crate::load_options::LoadOptions;
use crate::meta::SessionMeta;
use crate::search::{SessionHit, SessionSearchOptions};
/// Pluggable session persistence.
///
/// All save/load methods use [`AgentMessage`] as the canonical message type,
/// preserving both LLM and custom messages without silent data loss.
///
/// The default implementation ([`JsonlSessionStore`](crate::JsonlSessionStore))
/// uses JSONL files, but alternative backends (`SQLite`, S3, etc.) can
/// implement this trait directly.
pub trait SessionStore: Send + Sync {
/// Persist a session, including both LLM and custom messages.
///
/// Overwrites any existing session with the same ID. Custom messages that
/// cannot be serialized are logged and skipped — callers should ensure
/// custom types implement `CustomMessage::to_json` and
/// `CustomMessage::type_name`.
fn save(&self, id: &str, meta: &SessionMeta, messages: &[AgentMessage]) -> io::Result<()>;
/// Persist a session transcript plus its state snapshot.
///
/// Stores with optimistic-concurrency metadata should return the metadata
/// as persisted on disk so callers can keep their local sequence in sync.
/// Backends must implement this explicitly if they support atomic
/// transcript+state persistence; the default returns
/// [`io::ErrorKind::Unsupported`] so callers do not accidentally rely on a
/// non-atomic fallback.
fn save_full(
&self,
id: &str,
meta: &SessionMeta,
messages: &[AgentMessage],
state: &serde_json::Value,
) -> io::Result<SessionMeta> {
let _ = (id, meta, messages, state);
Err(io::Error::new(
io::ErrorKind::Unsupported,
"SessionStore::save_full requires an explicit atomic backend implementation",
))
}
/// Append messages to an existing session without rewriting the entire file.
fn append(&self, id: &str, messages: &[AgentMessage]) -> io::Result<()>;
/// Load a session by ID.
///
/// If `registry` is `Some`, custom messages are deserialized using the
/// provided registry. If `None`, custom messages are skipped.
fn load(
&self,
id: &str,
registry: Option<&CustomMessageRegistry>,
) -> io::Result<(SessionMeta, Vec<AgentMessage>)>;
/// Load a session transcript plus its state snapshot from one consistent
/// read boundary.
///
/// Backends must implement this explicitly if they support atomic
/// transcript+state restore; the default returns
/// [`io::ErrorKind::Unsupported`] so callers do not silently mix
/// transcript and state revisions via separate reads.
fn load_full(
&self,
id: &str,
registry: Option<&CustomMessageRegistry>,
) -> io::Result<(SessionMeta, Vec<AgentMessage>, Option<serde_json::Value>)> {
let _ = (id, registry);
Err(io::Error::new(
io::ErrorKind::Unsupported,
"SessionStore::load_full requires an explicit atomic backend implementation",
))
}
/// List all saved sessions, sorted by last updated (newest first).
fn list(&self) -> io::Result<Vec<SessionMeta>>;
/// Delete a session by ID.
fn delete(&self, id: &str) -> io::Result<()>;
/// Save session state snapshot.
///
/// The default implementation is a no-op that **discards the state**:
/// the snapshot is dropped and a later [`load_state`](Self::load_state)
/// returns `None`. It exists only for backward compatibility with
/// pre-034 store implementations (spec 034 FR-018). The first time the
/// default runs in a process it emits a `tracing::warn!` so the data
/// loss is visible; subsequent calls are silent.
///
/// **Planned breaking change:** `save_state` will become a required
/// (non-defaulted) trait method in the next major version bump. Custom
/// `SessionStore` implementations should override it (together with
/// [`load_state`](Self::load_state)) now.
fn save_state(&self, id: &str, state: &serde_json::Value) -> io::Result<()> {
static WARN_ONCE: Once = Once::new();
WARN_ONCE.call_once(|| {
tracing::warn!(
session_id = %id,
"SessionStore::save_state default no-op invoked: this store does not \
implement state persistence, so session state will be lost. Override \
save_state and load_state; they become required methods in the next \
major version. This warning is emitted once per process."
);
});
let _ = state;
Ok(())
}
/// Load session state snapshot.
///
/// The default implementation always returns `Ok(None)` (empty state),
/// even if a matching [`save_state`](Self::save_state) call appeared to
/// succeed. It exists only for backward compatibility with pre-034 store
/// implementations (spec 034 FR-018). The first time the default runs in
/// a process it emits a `tracing::warn!` so the silent fallback is
/// visible; subsequent calls are silent.
///
/// **Planned breaking change:** `load_state` will become a required
/// (non-defaulted) trait method in the next major version bump. Custom
/// `SessionStore` implementations should override it (together with
/// [`save_state`](Self::save_state)) now.
fn load_state(&self, id: &str) -> io::Result<Option<serde_json::Value>> {
static WARN_ONCE: Once = Once::new();
WARN_ONCE.call_once(|| {
tracing::warn!(
session_id = %id,
"SessionStore::load_state default no-op invoked: this store does not \
implement state persistence, so loads always return empty state. \
Override save_state and load_state; they become required methods in \
the next major version. This warning is emitted once per process."
);
});
Ok(None)
}
/// Persist interrupt state for a session.
///
/// Stores the interrupt as `{session_id}.interrupt.json`. Overwrites any
/// existing interrupt for the same session. Default: no-op.
fn save_interrupt(&self, id: &str, state: &InterruptState) -> io::Result<()> {
let _ = (id, state);
Ok(())
}
/// Load interrupt state for a session.
///
/// Returns `Some` if an interrupt file exists, `None` otherwise.
/// Returns an error if the file exists but is corrupted. Default: `None`.
fn load_interrupt(&self, id: &str) -> io::Result<Option<InterruptState>> {
let _ = id;
Ok(None)
}
/// Clear interrupt state for a session.
///
/// Deletes the `{session_id}.interrupt.json` file. Idempotent — safe to
/// call if no interrupt exists. Default: no-op.
fn clear_interrupt(&self, id: &str) -> io::Result<()> {
let _ = id;
Ok(())
}
/// Load a session with filtering options.
///
/// Returns metadata and a filtered subset of session entries based on the
/// provided [`LoadOptions`]. Default options return the full session.
fn load_with_options(
&self,
id: &str,
options: &LoadOptions,
) -> io::Result<(SessionMeta, Vec<SessionEntry>)>;
/// Search across persisted sessions.
///
/// Backends that do not support search can rely on the default empty
/// result for backward compatibility.
fn search(&self, query: &str, options: &SessionSearchOptions) -> io::Result<Vec<SessionHit>> {
let _ = (query, options);
Ok(Vec::new())
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use chrono::Utc;
use serde_json::json;
use super::*;
struct CountingStore {
saves: Arc<AtomicUsize>,
state_saves: Arc<AtomicUsize>,
loads: Arc<AtomicUsize>,
state_loads: Arc<AtomicUsize>,
}
impl SessionStore for CountingStore {
fn save(
&self,
_id: &str,
_meta: &SessionMeta,
_messages: &[AgentMessage],
) -> io::Result<()> {
self.saves.fetch_add(1, Ordering::Relaxed);
Ok(())
}
fn append(&self, _id: &str, _messages: &[AgentMessage]) -> io::Result<()> {
Ok(())
}
fn load(
&self,
_id: &str,
_registry: Option<&CustomMessageRegistry>,
) -> io::Result<(SessionMeta, Vec<AgentMessage>)> {
self.loads.fetch_add(1, Ordering::Relaxed);
Ok((sample_meta(), Vec::new()))
}
fn list(&self) -> io::Result<Vec<SessionMeta>> {
Ok(Vec::new())
}
fn delete(&self, _id: &str) -> io::Result<()> {
Ok(())
}
fn save_state(&self, _id: &str, _state: &serde_json::Value) -> io::Result<()> {
self.state_saves.fetch_add(1, Ordering::Relaxed);
Ok(())
}
fn load_state(&self, _id: &str) -> io::Result<Option<serde_json::Value>> {
self.state_loads.fetch_add(1, Ordering::Relaxed);
Ok(None)
}
fn load_with_options(
&self,
_id: &str,
_options: &LoadOptions,
) -> io::Result<(SessionMeta, Vec<SessionEntry>)> {
Ok((sample_meta(), Vec::new()))
}
}
fn sample_meta() -> SessionMeta {
SessionMeta {
id: "session-1".to_string(),
title: "Session 1".to_string(),
created_at: Utc::now(),
updated_at: Utc::now(),
version: 1,
sequence: 0,
}
}
/// Store that relies on the trait defaults for `save_state`/`load_state`,
/// simulating a pre-034 custom `SessionStore` implementation.
struct DefaultStateStore;
impl SessionStore for DefaultStateStore {
fn save(
&self,
_id: &str,
_meta: &SessionMeta,
_messages: &[AgentMessage],
) -> io::Result<()> {
Ok(())
}
fn append(&self, _id: &str, _messages: &[AgentMessage]) -> io::Result<()> {
Ok(())
}
fn load(
&self,
_id: &str,
_registry: Option<&CustomMessageRegistry>,
) -> io::Result<(SessionMeta, Vec<AgentMessage>)> {
Ok((sample_meta(), Vec::new()))
}
fn list(&self) -> io::Result<Vec<SessionMeta>> {
Ok(Vec::new())
}
fn delete(&self, _id: &str) -> io::Result<()> {
Ok(())
}
fn load_with_options(
&self,
_id: &str,
_options: &LoadOptions,
) -> io::Result<(SessionMeta, Vec<SessionEntry>)> {
Ok((sample_meta(), Vec::new()))
}
}
/// Captures tracing output into a shared buffer for assertions.
#[derive(Clone, Default)]
struct CaptureWriter(Arc<Mutex<Vec<u8>>>);
impl CaptureWriter {
fn contents(&self) -> String {
String::from_utf8(self.0.lock().unwrap().clone()).unwrap()
}
}
impl std::io::Write for CaptureWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.lock().unwrap().extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for CaptureWriter {
type Writer = Self;
fn make_writer(&'a self) -> Self::Writer {
self.clone()
}
}
/// The default `save_state`/`load_state` must stay behavioral no-ops
/// (spec 034 FR-018 / SC-006) while warning exactly once per process
/// per method that state persistence is not implemented.
///
/// This is the only test in this binary that exercises the defaults
/// (all other tests use stores that override the state methods), so the
/// process-wide `Once` observed here is deterministic.
#[test]
fn default_state_methods_are_noops_and_warn_once_per_process() {
let store = DefaultStateStore;
let writer = CaptureWriter::default();
let subscriber = tracing_subscriber::fmt()
.with_writer(writer.clone())
.with_ansi(false)
.finish();
tracing::subscriber::with_default(subscriber, || {
// No-op contract preserved: save succeeds, load returns None.
store
.save_state("session-1", &json!({"scroll": 1}))
.unwrap();
assert_eq!(store.load_state("session-1").unwrap(), None);
// Second round trips must not warn again.
store
.save_state("session-1", &json!({"scroll": 2}))
.unwrap();
assert_eq!(store.load_state("session-1").unwrap(), None);
});
let output = writer.contents();
assert!(
output.contains("WARN"),
"expected WARN level output: {output}"
);
assert_eq!(
output
.matches("SessionStore::save_state default no-op")
.count(),
1,
"save_state warning must fire exactly once: {output}"
);
assert_eq!(
output
.matches("SessionStore::load_state default no-op")
.count(),
1,
"load_state warning must fire exactly once: {output}"
);
}
#[test]
fn default_save_full_rejects_non_atomic_fallback_without_writing() {
let save_calls = Arc::new(AtomicUsize::new(0));
let save_state_calls = Arc::new(AtomicUsize::new(0));
let store = CountingStore {
saves: Arc::clone(&save_calls),
state_saves: Arc::clone(&save_state_calls),
loads: Arc::new(AtomicUsize::new(0)),
state_loads: Arc::new(AtomicUsize::new(0)),
};
let error = store
.save_full("session-1", &sample_meta(), &[], &json!({"draft": true}))
.unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::Unsupported);
assert_eq!(save_calls.load(Ordering::Relaxed), 0);
assert_eq!(save_state_calls.load(Ordering::Relaxed), 0);
}
#[test]
fn default_load_full_rejects_non_atomic_fallback_without_reading() {
let load_calls = Arc::new(AtomicUsize::new(0));
let load_state_calls = Arc::new(AtomicUsize::new(0));
let store = CountingStore {
saves: Arc::new(AtomicUsize::new(0)),
state_saves: Arc::new(AtomicUsize::new(0)),
loads: Arc::clone(&load_calls),
state_loads: Arc::clone(&load_state_calls),
};
let error = store.load_full("session-1", None).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::Unsupported);
assert_eq!(load_calls.load(Ordering::Relaxed), 0);
assert_eq!(load_state_calls.load(Ordering::Relaxed), 0);
}
}