crabtalk-runtime 0.0.21

Crabtalk agent runtime — execution loop, tool dispatch, and lifecycle hooks
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
//! Tests for Runtime — agent registry, conversation management, and execution.
//!
//! Uses `Env<()>` with InMemoryStorage. Every test gets its own
//! in-memory storage — no shared global state, no filesystem I/O, no node.

use crabtalk_runtime::{Config, Runtime};
use futures_util::StreamExt;
use std::sync::Arc;
use wcore::{
    AgentConfig, AgentEvent, AgentStopReason,
    model::Model,
    testing::{
        InMemoryStorage,
        provider::{TestProvider, text_chunks},
    },
};

struct TestCfg;

impl Config for TestCfg {
    type Storage = InMemoryStorage;
    type Provider = TestProvider;
    type Env = ();
}

fn runtime(provider: TestProvider) -> Runtime<TestCfg> {
    let storage = Arc::new(InMemoryStorage::new());
    let memory = Arc::new(parking_lot::RwLock::new(memory::Memory::new()));
    Runtime::new(
        Model::new(provider),
        Arc::new(()),
        storage,
        memory,
        wcore::ToolRegistry::new(),
    )
}

// --- Agent registry ---

#[tokio::test]
async fn add_agent_and_retrieve() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));
    runtime.add_agent(AgentConfig::new("crab"));

    assert!(runtime.agent("crab").is_some());
    assert!(runtime.agent("nonexistent").is_none());
}

#[tokio::test]
async fn agents_returns_all() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));
    runtime.add_agent(AgentConfig::new("a"));
    runtime.add_agent(AgentConfig::new("b"));

    let agents = runtime.agents();
    assert_eq!(agents.len(), 2);
}

#[tokio::test]
async fn upsert_agent_replaces_existing() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));
    let mut config = AgentConfig::new("crab");
    config.description = "first".to_owned();
    runtime.upsert_agent(config);
    assert_eq!(runtime.agent("crab").unwrap().description, "first");

    let mut replacement = AgentConfig::new("crab");
    replacement.description = "second".to_owned();
    runtime.upsert_agent(replacement);
    assert_eq!(runtime.agent("crab").unwrap().description, "second");
    assert_eq!(runtime.agents().len(), 1);
}

#[tokio::test]
async fn remove_agent_returns_true_when_present() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));
    runtime.add_agent(AgentConfig::new("crab"));
    assert!(runtime.remove_agent("crab"));
    assert!(runtime.agent("crab").is_none());
    assert!(!runtime.remove_agent("crab"));
}

// --- Tool registry on Runtime ---

#[tokio::test]
async fn register_and_unregister_tool() {
    let mut runtime = runtime(TestProvider::with_chunks(vec![]));
    let tool = wcore::model::Tool {
        kind: wcore::model::ToolType::Function,
        function: wcore::model::FunctionDef {
            name: "bash".into(),
            description: Some("run commands".into()),
            parameters: None,
        },
        strict: None,
    };
    runtime.tools.insert(tool);
    assert!(runtime.tools.remove("bash"));
    assert!(!runtime.tools.remove("bash"));
}

// --- Conversation management ---

#[tokio::test]
async fn get_or_create_conversation_requires_registered_agent() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));
    let err = runtime
        .get_or_create_conversation("nonexistent", "user")
        .await
        .unwrap_err();
    assert!(err.to_string().contains("not registered"));
}

#[tokio::test]
async fn create_and_close() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));
    runtime.add_agent(AgentConfig::new("crab"));

    let id = runtime
        .get_or_create_conversation("crab", "user")
        .await
        .unwrap();
    assert!(runtime.conversation(id).await.is_some());

    assert!(runtime.close(id).await);
    assert!(runtime.conversation(id).await.is_none());
    assert!(!runtime.close(id).await);
}

#[tokio::test]
async fn conversations_lists_all() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));
    runtime.add_agent(AgentConfig::new("crab"));

    runtime
        .get_or_create_conversation("crab", "test-a")
        .await
        .unwrap();
    runtime
        .get_or_create_conversation("crab", "test-b")
        .await
        .unwrap();

    let conversations = runtime.conversations().await;
    assert_eq!(conversations.len(), 2);
}

#[tokio::test]
async fn get_or_create_conversation_returns_existing() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));
    runtime.add_agent(AgentConfig::new("crab"));

    let id1 = runtime
        .get_or_create_conversation("crab", "test-same")
        .await
        .unwrap();
    let id2 = runtime
        .get_or_create_conversation("crab", "test-same")
        .await
        .unwrap();
    // Should return same in-memory conversation
    assert_eq!(id1, id2);
}

#[tokio::test]
async fn get_or_create_conversation_rejects_unknown_agent() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));
    let err = runtime
        .get_or_create_conversation("ghost", "user")
        .await
        .unwrap_err();
    assert!(err.to_string().contains("not registered"));
}

#[tokio::test]
async fn transfer_to_moves_all() {
    let runtime1 = runtime(TestProvider::with_chunks(vec![]));
    runtime1.add_agent(AgentConfig::new("crab"));
    let id = runtime1
        .get_or_create_conversation("crab", "test-xfer")
        .await
        .unwrap();

    let mut runtime2 = runtime(TestProvider::with_chunks(vec![]));
    runtime2.add_agent(AgentConfig::new("crab"));
    runtime1.transfer_to(&mut runtime2).await;

    // Conversation should exist in runtime2
    assert!(runtime2.conversation(id).await.is_some());
}

// --- Execution ---

#[tokio::test]
async fn send_to_returns_response() {
    let provider = TestProvider::with_chunks(vec![text_chunks("hello back")]);
    let runtime = runtime(provider);
    runtime.add_agent(AgentConfig::new("crab"));

    let conversation_id = runtime
        .get_or_create_conversation("crab", "test-send")
        .await
        .unwrap();
    let response = runtime
        .send_to(conversation_id, "hi", "", None)
        .await
        .unwrap();

    assert_eq!(response.stop_reason, AgentStopReason::TextResponse);
    assert_eq!(response.final_response.as_deref(), Some("hello back"));
}

#[tokio::test]
async fn send_to_nonexistent_conversation_errors() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));
    let err = runtime.send_to(999, "hi", "", None).await.unwrap_err();
    assert!(err.to_string().contains("not found"));
}

#[tokio::test]
async fn send_to_appends_to_history() {
    let provider = TestProvider::with_chunks(vec![
        text_chunks("first reply"),
        text_chunks("second reply"),
    ]);
    let runtime = runtime(provider);
    runtime.add_agent(AgentConfig::new("crab"));

    let conversation_id = runtime
        .get_or_create_conversation("crab", "test-history")
        .await
        .unwrap();
    runtime
        .send_to(conversation_id, "hello", "", None)
        .await
        .unwrap();
    runtime
        .send_to(conversation_id, "again", "", None)
        .await
        .unwrap();

    let conversation_mutex = runtime.conversation(conversation_id).await.unwrap();
    let conversation = conversation_mutex.lock().await;
    // history: user1 + assistant1 + user2 + assistant2
    assert_eq!(conversation.history.len(), 4);
}

#[tokio::test]
async fn stream_to_yields_correct_content() {
    let provider = TestProvider::with_chunks(vec![text_chunks("streamed")]);
    let runtime = runtime(provider);
    runtime.add_agent(AgentConfig::new("crab"));

    let conversation_id = runtime
        .get_or_create_conversation("crab", "test-stream")
        .await
        .unwrap();

    let mut events = Vec::new();
    let mut stream = std::pin::pin!(runtime.stream_to(conversation_id, "hi", "", None));
    while let Some(event) = stream.next().await {
        events.push(event);
    }

    // Verify text content streamed
    let text: String = events
        .iter()
        .filter_map(|e| match e {
            AgentEvent::TextDelta(s) => Some(s.as_str()),
            _ => None,
        })
        .collect();
    assert_eq!(text, "streamed");

    // Verify Done event
    if let AgentEvent::Done(resp) = events.last().unwrap() {
        assert_eq!(resp.stop_reason, AgentStopReason::TextResponse);
        assert_eq!(resp.final_response.as_deref(), Some("streamed"));
    } else {
        panic!("last event should be Done");
    }

    // Verify history was persisted
    let conversation_mutex = runtime.conversation(conversation_id).await.unwrap();
    let conversation = conversation_mutex.lock().await;
    assert_eq!(conversation.history.len(), 2); // user + assistant
}

#[tokio::test]
async fn switch_topic_resumes_archive_from_memory() {
    use memory::{EntryKind, Op};
    use wcore::{
        model::HistoryEntry,
        storage::{ConversationMeta, Storage},
    };

    let storage = Arc::new(InMemoryStorage::new());
    let mem = Arc::new(parking_lot::RwLock::new(memory::Memory::new()));
    mem.write()
        .apply(Op::Add {
            name: "archive-test".into(),
            content: "earlier context, compacted".into(),
            aliases: vec![],
            kind: EntryKind::Archive,
        })
        .unwrap();

    let handle = storage.create_session("crab", "tester").unwrap();
    let topic_meta = ConversationMeta {
        agent: "crab".into(),
        created_by: "tester".into(),
        created_at: chrono::Utc::now().to_rfc3339(),
        title: String::new(),
        uptime_secs: 0,
        topic: Some("auth".into()),
    };
    storage.update_session_meta(&handle, &topic_meta).unwrap();
    storage
        .append_session_messages(
            &handle,
            &[
                HistoryEntry::user("pre-compact"),
                HistoryEntry::assistant("pre-compact reply", None, None),
            ],
        )
        .unwrap();
    storage
        .append_session_compact(&handle, "archive-test")
        .unwrap();
    storage
        .append_session_messages(&handle, &[HistoryEntry::user("after-compact")])
        .unwrap();

    let runtime = Runtime::<TestCfg>::new(
        Model::new(TestProvider::with_chunks(vec![])),
        Arc::new(()),
        storage,
        mem,
        wcore::ToolRegistry::new(),
    );
    runtime.add_agent(AgentConfig::new("crab"));

    runtime
        .switch_topic("crab", "tester", "auth", None)
        .await
        .unwrap();
    let conv_id = runtime
        .get_or_create_conversation("crab", "tester")
        .await
        .unwrap();
    let conversation = runtime.conversation(conv_id).await.unwrap();
    let conv = conversation.lock().await;

    assert_eq!(conv.topic.as_deref(), Some("auth"));
    assert_eq!(conv.history.len(), 2);
    assert_eq!(conv.history[0].text(), "earlier context, compacted");
    assert_eq!(conv.history[1].text(), "after-compact");
}

#[tokio::test]
async fn switch_topic_injects_placeholder_when_archive_missing() {
    use wcore::{
        model::HistoryEntry,
        storage::{ConversationMeta, Storage},
    };

    let storage = Arc::new(InMemoryStorage::new());
    // Empty memory — the referenced archive doesn't exist.
    let mem = Arc::new(parking_lot::RwLock::new(memory::Memory::new()));

    let handle = storage.create_session("crab", "tester").unwrap();
    let topic_meta = ConversationMeta {
        agent: "crab".into(),
        created_by: "tester".into(),
        created_at: chrono::Utc::now().to_rfc3339(),
        title: String::new(),
        uptime_secs: 0,
        topic: Some("ghost".into()),
    };
    storage.update_session_meta(&handle, &topic_meta).unwrap();
    storage
        .append_session_compact(&handle, "archive-gone")
        .unwrap();
    storage
        .append_session_messages(&handle, &[HistoryEntry::user("continuing")])
        .unwrap();

    let runtime = Runtime::<TestCfg>::new(
        Model::new(TestProvider::with_chunks(vec![])),
        Arc::new(()),
        storage,
        mem,
        wcore::ToolRegistry::new(),
    );
    runtime.add_agent(AgentConfig::new("crab"));

    runtime
        .switch_topic("crab", "tester", "ghost", None)
        .await
        .unwrap();
    let conv_id = runtime
        .get_or_create_conversation("crab", "tester")
        .await
        .unwrap();
    let conversation = runtime.conversation(conv_id).await.unwrap();
    let conv = conversation.lock().await;

    assert_eq!(conv.history.len(), 2);
    assert!(conv.history[0].text().contains("archive-gone"));
    assert!(conv.history[0].text().contains("unavailable"));
    assert_eq!(conv.history[1].text(), "continuing");
}

#[tokio::test]
async fn stream_to_nonexistent_conversation_yields_error() {
    let runtime = runtime(TestProvider::with_chunks(vec![]));

    let mut events = Vec::new();
    let mut stream = std::pin::pin!(runtime.stream_to(999, "hi", "", None));
    while let Some(event) = stream.next().await {
        events.push(event);
    }

    if let AgentEvent::Done(resp) = events.last().unwrap() {
        if let AgentStopReason::Error(msg) = &resp.stop_reason {
            assert!(msg.contains("not found"));
        } else {
            panic!("expected Error stop reason");
        }
    } else {
        panic!("expected Done event");
    }
}