swink-agent 0.8.0

Core scaffolding for running LLM-powered agentic loops
Documentation
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Shared codec for serializing and deserializing [`AgentMessage`] batches.
//!
//! Consolidates the message-envelope logic previously duplicated across
//! checkpoints, JSONL session storage, and blocking async adapters into a
//! single module.
//!
//! ## Provided functionality
//!
//! - [`MessageSlot`] — records the original position of each message in an
//!   interleaved LLM/custom sequence.
//! - [`SerializedMessages`] — the result of splitting a `&[AgentMessage]`
//!   into separate LLM and custom vectors with ordering metadata.
//! - [`serialize_messages`] / [`restore_messages`] — batch serialization and
//!   deserialization with interleaved ordering.
//! - [`restore_single_custom`] — restore one custom-message envelope via a
//!   registry (useful for line-oriented formats like JSONL).
//! - [`SerializedCustomMessage`] — a lightweight [`CustomMessage`](super::CustomMessage)
//!   implementation that holds pre-serialized `type_name` + `to_json` data,
//!   enabling transfer across thread or process boundaries.
//! - [`clone_messages_for_send`] — snapshot a slice of `AgentMessage` into
//!   fully `Send + Clone`-safe values for crossing `spawn_blocking` or IPC.

use serde::{Deserialize, Serialize};

use super::{
    AgentMessage, CustomMessageRegistry, LlmMessage, deserialize_custom_message,
    serialize_custom_message,
};

// ─── MessageSlot ────────────────────────────────────────────────────────────

/// Tracks the original position of each message in the sequence.
///
/// During serialization, LLM and custom messages are stored in separate
/// vectors for backward compatibility. `MessageSlot` records the original
/// ordering so that [`restore_messages`] can reconstruct the interleaved
/// sequence faithfully.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum MessageSlot {
    /// An LLM message at the given index in the `messages` vector.
    Llm { index: usize },
    /// A custom message at the given index in the `custom_messages` vector.
    Custom { index: usize },
}

// ─── SerializedMessages ─────────────────────────────────────────────────────

/// The result of splitting an `AgentMessage` slice into LLM and custom
/// vectors, plus ordering metadata.
#[derive(Debug, Clone)]
pub struct SerializedMessages {
    /// LLM messages in insertion order.
    pub llm_messages: Vec<LlmMessage>,
    /// Custom message envelopes (`{"type": "…", "data": {…}}`).
    pub custom_messages: Vec<serde_json::Value>,
    /// Records the original interleaved order of LLM and custom messages.
    pub message_order: Vec<MessageSlot>,
}

// ─── Batch serialize / restore ──────────────────────────────────────────────

/// Split a slice of [`AgentMessage`] into separate LLM and custom vectors
/// with ordering metadata.
///
/// Custom messages that do not support serialization (`type_name()` or
/// `to_json()` returns `None`) are skipped with a `tracing::warn`.
///
/// `kind` is a human-readable label used in log messages (e.g. "checkpoint",
/// "session").
pub fn serialize_messages(messages: &[AgentMessage], kind: &str) -> SerializedMessages {
    let mut llm_messages = Vec::new();
    let mut custom_messages = Vec::new();
    let mut message_order = Vec::new();

    for message in messages {
        match message {
            AgentMessage::Llm(llm) => {
                message_order.push(MessageSlot::Llm {
                    index: llm_messages.len(),
                });
                llm_messages.push(llm.clone());
            }
            AgentMessage::Custom(custom) => {
                if let Some(envelope) = serialize_custom_message(custom.as_ref()) {
                    message_order.push(MessageSlot::Custom {
                        index: custom_messages.len(),
                    });
                    custom_messages.push(envelope);
                } else {
                    tracing::warn!(
                        "skipping non-serializable CustomMessage in {kind}: {:?}",
                        custom
                    );
                }
            }
        }
    }

    SerializedMessages {
        llm_messages,
        custom_messages,
        message_order,
    }
}

/// Reconstruct an interleaved `Vec<AgentMessage>` from separate LLM and
/// custom vectors, using [`MessageSlot`] ordering metadata.
///
/// If `message_order` is empty (legacy data created before ordering support),
/// falls back to LLM messages first, then custom messages appended.
///
/// If `registry` is `None`, custom messages are silently skipped.
/// Deserialization failures are logged as warnings.
///
/// `kind` is used in log messages (e.g. "checkpoint", "session").
pub fn restore_messages(
    llm_messages: &[LlmMessage],
    custom_messages: &[serde_json::Value],
    message_order: &[MessageSlot],
    registry: Option<&CustomMessageRegistry>,
    kind: &str,
) -> Vec<AgentMessage> {
    if !message_order.is_empty() {
        let mut result = Vec::with_capacity(message_order.len());
        for slot in message_order {
            match slot {
                MessageSlot::Llm { index } => {
                    if let Some(llm) = llm_messages.get(*index) {
                        result.push(AgentMessage::Llm(llm.clone()));
                    }
                }
                MessageSlot::Custom { index } => {
                    if let Some(reg) = registry
                        && let Some(envelope) = custom_messages.get(*index)
                    {
                        match deserialize_custom_message(reg, envelope) {
                            Ok(custom) => result.push(AgentMessage::Custom(custom)),
                            Err(error) => {
                                tracing::warn!(
                                    "failed to deserialize custom message from {kind}: {error}"
                                );
                            }
                        }
                    }
                }
            }
        }
        return result;
    }

    // Legacy fallback: LLM messages first, then custom messages appended.
    let mut result: Vec<AgentMessage> = llm_messages
        .iter()
        .cloned()
        .map(AgentMessage::Llm)
        .collect();

    if let Some(reg) = registry {
        for envelope in custom_messages {
            match deserialize_custom_message(reg, envelope) {
                Ok(custom) => result.push(AgentMessage::Custom(custom)),
                Err(error) => {
                    tracing::warn!("failed to deserialize custom message from {kind}: {error}");
                }
            }
        }
    }

    result
}

// ─── Single-envelope restore ────────────────────────────────────────────────

/// Restore a single custom-message envelope via a registry.
///
/// Returns `Ok(Some(msg))` on success, `Ok(None)` if the registry is `None`,
/// or `Err(reason)` if deserialization fails.
pub fn restore_single_custom(
    registry: Option<&CustomMessageRegistry>,
    envelope: &serde_json::Value,
) -> Result<Option<Box<dyn super::CustomMessage>>, String> {
    registry.map_or_else(
        || Ok(None),
        |reg| deserialize_custom_message(reg, envelope).map(Some),
    )
}

// ─── SerializedCustomMessage ────────────────────────────────────────────────

/// A lightweight [`CustomMessage`](super::CustomMessage) stand-in that holds pre-serialized data.
///
/// Useful for ferrying custom messages across `spawn_blocking` boundaries or
/// other contexts where the original `Box<dyn CustomMessage>` (which is
/// neither `Clone` nor necessarily transferable) must be replaced with a
/// plain-data snapshot.
///
/// Implements `CustomMessage` so it can be stored in `AgentMessage::Custom`
/// and round-trips faithfully through `serialize_custom_message` /
/// `deserialize_custom_message`.
#[derive(Debug, Clone)]
pub struct SerializedCustomMessage {
    name: String,
    json: serde_json::Value,
}

impl SerializedCustomMessage {
    /// Create a new serialized custom message from a name and JSON payload.
    #[must_use]
    pub fn new(name: impl Into<String>, json: serde_json::Value) -> Self {
        Self {
            name: name.into(),
            json,
        }
    }

    /// Attempt to create a `SerializedCustomMessage` from a `dyn CustomMessage`.
    ///
    /// Returns `None` if the custom message does not support serialization.
    #[must_use]
    pub fn from_custom(msg: &dyn super::CustomMessage) -> Option<Self> {
        Some(Self {
            name: msg.type_name()?.to_string(),
            json: msg.to_json()?,
        })
    }
}

impl super::CustomMessage for SerializedCustomMessage {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
    fn type_name(&self) -> Option<&str> {
        Some(&self.name)
    }
    fn to_json(&self) -> Option<serde_json::Value> {
        Some(self.json.clone())
    }
    fn clone_box(&self) -> Option<Box<dyn super::CustomMessage>> {
        Some(Box::new(self.clone()))
    }
}

// ─── clone_messages_for_send ────────────────────────────────────────────────

/// Snapshot a slice of [`AgentMessage`] into fully `Send + Clone`-safe values.
///
/// `Llm` variants are cloned directly. `Custom` variants are
/// snapshot-serialized into [`SerializedCustomMessage`] wrappers so they can
/// cross thread (`spawn_blocking`) or process (IPC) boundaries faithfully.
///
/// Custom messages that lack `type_name()` or `to_json()` are silently
/// dropped — matching the existing behavior of `serialize_custom_message`.
pub fn clone_messages_for_send(messages: &[AgentMessage]) -> Vec<AgentMessage> {
    messages
        .iter()
        .filter_map(|m| match m {
            AgentMessage::Llm(llm) => Some(AgentMessage::Llm(llm.clone())),
            AgentMessage::Custom(custom) => {
                let snapshot = SerializedCustomMessage::from_custom(custom.as_ref())?;
                Some(AgentMessage::Custom(Box::new(snapshot)))
            }
        })
        .collect()
}

// ─── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{
        AssistantMessage, ContentBlock, Cost, CustomMessage, StopReason, Usage, UserMessage,
    };

    // ── Test helpers ────────────────────────────────────────────────────────

    #[derive(Debug)]
    struct NonSerializableCustom;

    impl CustomMessage for NonSerializableCustom {
        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }

    #[derive(Debug, Clone, PartialEq)]
    struct TaggedCustom {
        tag: String,
    }

    impl CustomMessage for TaggedCustom {
        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
        fn type_name(&self) -> Option<&str> {
            Some("TaggedCustom")
        }
        fn to_json(&self) -> Option<serde_json::Value> {
            Some(serde_json::json!({ "tag": self.tag }))
        }
    }

    fn tagged_registry() -> CustomMessageRegistry {
        let mut reg = CustomMessageRegistry::new();
        reg.register(
            "TaggedCustom",
            Box::new(|val: serde_json::Value| {
                let tag = val
                    .get("tag")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| "missing tag".to_string())?;
                Ok(Box::new(TaggedCustom {
                    tag: tag.to_string(),
                }) as Box<dyn CustomMessage>)
            }),
        );
        reg
    }

    fn user_msg(text: &str) -> AgentMessage {
        AgentMessage::Llm(LlmMessage::User(UserMessage {
            content: vec![ContentBlock::Text {
                text: text.to_string(),
            }],
            timestamp: 0,
            cache_hint: None,
        }))
    }

    fn assistant_msg(text: &str) -> AgentMessage {
        AgentMessage::Llm(LlmMessage::Assistant(AssistantMessage {
            content: vec![ContentBlock::Text {
                text: text.to_string(),
            }],
            provider: "test".to_string(),
            model_id: "m".to_string(),
            usage: Usage::default(),
            cost: Cost::default(),
            stop_reason: StopReason::Stop,
            error_message: None,
            error_kind: None,
            timestamp: 0,
            cache_hint: None,
        }))
    }

    fn custom_msg(tag: &str) -> AgentMessage {
        AgentMessage::Custom(Box::new(TaggedCustom {
            tag: tag.to_string(),
        }))
    }

    fn message_label(msg: &AgentMessage) -> String {
        match msg {
            AgentMessage::Llm(LlmMessage::User(u)) => {
                format!("user:{}", ContentBlock::extract_text(&u.content))
            }
            AgentMessage::Llm(LlmMessage::Assistant(a)) => {
                format!("assistant:{}", ContentBlock::extract_text(&a.content))
            }
            AgentMessage::Custom(c) => {
                if let Some(json) = c.to_json() {
                    format!("custom:{}", json["tag"].as_str().unwrap_or("?"))
                } else {
                    "custom:?".to_string()
                }
            }
            _ => "other".to_string(),
        }
    }

    // ── serialize_messages ──────────────────────────────────────────────────

    #[test]
    fn serialize_skips_non_serializable_custom() {
        let messages = vec![
            user_msg("hi"),
            AgentMessage::Custom(Box::new(NonSerializableCustom)),
            assistant_msg("hello"),
        ];

        let result = serialize_messages(&messages, "test");
        assert_eq!(result.llm_messages.len(), 2);
        assert!(result.custom_messages.is_empty());
        assert_eq!(result.message_order.len(), 2);
    }

    #[test]
    fn serialize_preserves_interleaved_order() {
        let messages = vec![
            user_msg("hello"),
            custom_msg("A"),
            assistant_msg("hi"),
            custom_msg("B"),
            user_msg("thanks"),
        ];

        let result = serialize_messages(&messages, "test");
        assert_eq!(result.llm_messages.len(), 3);
        assert_eq!(result.custom_messages.len(), 2);
        assert_eq!(result.message_order.len(), 5);

        // Verify envelope content
        assert_eq!(result.custom_messages[0]["type"], "TaggedCustom");
        assert_eq!(result.custom_messages[0]["data"]["tag"], "A");
        assert_eq!(result.custom_messages[1]["data"]["tag"], "B");
    }

    // ── restore_messages ───────────────────────────────────────────────────

    #[test]
    fn roundtrip_preserves_order() {
        let registry = tagged_registry();
        let messages = vec![
            user_msg("hello"),
            custom_msg("A"),
            assistant_msg("hi"),
            custom_msg("B"),
            user_msg("thanks"),
        ];

        let serialized = serialize_messages(&messages, "test");
        let restored = restore_messages(
            &serialized.llm_messages,
            &serialized.custom_messages,
            &serialized.message_order,
            Some(&registry),
            "test",
        );

        let labels: Vec<String> = restored.iter().map(message_label).collect();
        assert_eq!(
            labels,
            vec![
                "user:hello",
                "custom:A",
                "assistant:hi",
                "custom:B",
                "user:thanks",
            ]
        );
    }

    #[test]
    fn restore_without_registry_skips_custom() {
        let messages = vec![user_msg("hi"), custom_msg("skipped"), assistant_msg("ok")];

        let serialized = serialize_messages(&messages, "test");
        let restored = restore_messages(
            &serialized.llm_messages,
            &serialized.custom_messages,
            &serialized.message_order,
            None,
            "test",
        );

        assert_eq!(restored.len(), 2);
        let labels: Vec<String> = restored.iter().map(message_label).collect();
        assert_eq!(labels, vec!["user:hi", "assistant:ok"]);
    }

    #[test]
    fn legacy_fallback_no_ordering() {
        let registry = tagged_registry();
        let llm = vec![LlmMessage::User(UserMessage {
            content: vec![ContentBlock::Text {
                text: "hi".to_string(),
            }],
            timestamp: 0,
            cache_hint: None,
        })];
        let custom = vec![serde_json::json!({
            "type": "TaggedCustom",
            "data": { "tag": "legacy" }
        })];

        let restored = restore_messages(&llm, &custom, &[], Some(&registry), "test");
        assert_eq!(restored.len(), 2);
        let labels: Vec<String> = restored.iter().map(message_label).collect();
        assert_eq!(labels, vec!["user:hi", "custom:legacy"]);
    }

    // ── restore_single_custom ──────────────────────────────────────────────

    #[test]
    fn restore_single_custom_with_registry() {
        let registry = tagged_registry();
        let envelope = serde_json::json!({
            "type": "TaggedCustom",
            "data": { "tag": "single" }
        });

        let result = restore_single_custom(Some(&registry), &envelope).unwrap();
        assert!(result.is_some());
        let custom = result.unwrap();
        assert_eq!(custom.type_name(), Some("TaggedCustom"));
    }

    #[test]
    fn restore_single_custom_without_registry() {
        let envelope = serde_json::json!({ "type": "X", "data": {} });
        let result = restore_single_custom(None, &envelope).unwrap();
        assert!(result.is_none());
    }

    // ── SerializedCustomMessage ────────────────────────────────────────────

    #[test]
    fn serialized_custom_message_from_custom() {
        let original = TaggedCustom {
            tag: "hello".to_string(),
        };
        let snapshot = SerializedCustomMessage::from_custom(&original).unwrap();
        assert_eq!(snapshot.type_name(), Some("TaggedCustom"));
        assert_eq!(snapshot.to_json().unwrap()["tag"], "hello");
    }

    #[test]
    fn serialized_custom_message_from_non_serializable() {
        let bare = NonSerializableCustom;
        assert!(SerializedCustomMessage::from_custom(&bare).is_none());
    }

    // ── clone_messages_for_send ────────────────────────────────────────────

    #[test]
    fn clone_for_send_preserves_all_serializable() {
        let messages = vec![
            user_msg("hello"),
            custom_msg("kept"),
            AgentMessage::Custom(Box::new(NonSerializableCustom)),
            assistant_msg("world"),
        ];

        let cloned = clone_messages_for_send(&messages);
        assert_eq!(cloned.len(), 3); // non-serializable custom dropped
        let labels: Vec<String> = cloned.iter().map(message_label).collect();
        assert_eq!(labels, vec!["user:hello", "custom:kept", "assistant:world"]);
    }

    #[test]
    fn clone_for_send_custom_roundtrips_through_registry() {
        let registry = tagged_registry();
        let messages = vec![custom_msg("roundtrip")];
        let cloned = clone_messages_for_send(&messages);
        assert_eq!(cloned.len(), 1);

        // The cloned custom message can be serialized and restored
        let envelope =
            serialize_custom_message(cloned[0].downcast_ref::<SerializedCustomMessage>().unwrap())
                .unwrap();
        let restored = deserialize_custom_message(&registry, &envelope).unwrap();
        assert_eq!(
            restored
                .as_any()
                .downcast_ref::<TaggedCustom>()
                .unwrap()
                .tag,
            "roundtrip"
        );
    }
}