agent-framework-redis 0.1.1

Redis-backed chat message store and context provider
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Live integration tests against a real Redis server.
//!
//! Every test resolves an endpoint independently via [`test_server`], which
//! implements both options documented for this work package:
//!
//! 1. If `REDIS_URL` is set, use it as-is. The server is assumed to be
//!    externally managed (e.g. CI service container); this test binary
//!    neither spawns nor tears it down.
//! 2. Otherwise, if a `redis-server` binary is on `PATH` (checked with
//!    `redis-server --version`), spawn a private instance on an ephemeral
//!    localhost port, wait for it to accept connections, and kill it (via a
//!    `Drop` guard) when the test's `ServerGuard` goes out of scope —
//!    including on panic/assertion failure, since unwinding still runs
//!    `Drop`.
//! 3. Otherwise, print a message to stderr and return early. The test still
//!    reports as passed (a "runtime skip", per the work package's second
//!    gating option) rather than failing a CI environment that lacks Redis.
//!
//! Every test also uses a UUID-derived key prefix / thread id so that
//! multiple tests sharing one `REDIS_URL` server (option 1) can't collide.
//!
//! # RediSearch (`FT.*`) coverage
//!
//! [`RedisContextProvider`]'s `FT.SEARCH`-backed retrieval path (see the
//! crate's `context_provider` module docs) only activates against a **Redis
//! Stack** server — plain `redis-server` (the only kind this file ever
//! spawns itself) has no RediSearch module loaded. So:
//!
//! - The existing tests below exercise the SCAN-fallback path exactly as
//!   before (this is what a self-spawned plain server gives you), proving
//!   fallback correctness end-to-end regardless of RediSearch availability.
//! - The `context_provider_redisearch_*` tests additionally probe the
//!   connected server for RediSearch via [`redisearch_available`] and
//!   `return` early (a graceful runtime skip, printed to stderr) unless it's
//!   present — which in practice means they only actually assert anything
//!   when `REDIS_URL` is pointed at a Redis Stack server. They are still
//!   compiled and run (as no-ops) in any environment lacking Stack, so
//!   nothing about this suite requires Stack to be installed.

use std::net::TcpListener;
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::time::Duration;

use agent_framework_core::memory::{ContextProvider, SessionContext};
use agent_framework_core::types::Message;
use agent_framework_redis::{RedisChatMessageStore, RedisContextProvider};
use uuid::Uuid;

/// Kills the spawned `redis-server` child (if any) when dropped.
enum ServerGuard {
    Spawned(Child),
    External,
}

impl Drop for ServerGuard {
    fn drop(&mut self) {
        if let ServerGuard::Spawned(child) = self {
            let _ = child.kill();
            let _ = child.wait();
        }
    }
}

fn redis_server_available() -> bool {
    Command::new("redis-server")
        .arg("--version")
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

fn free_ephemeral_port() -> u16 {
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind ephemeral port");
    listener.local_addr().expect("local_addr").port()
}

async fn wait_until_ready(url: &str) -> bool {
    for _ in 0..50 {
        if let Ok(store) = RedisChatMessageStore::new(url, None) {
            if store.ping().await {
                return true;
            }
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
    false
}

/// Resolve a Redis endpoint for this test. Returns `None` (meaning: skip)
/// if neither `REDIS_URL` nor a `redis-server` binary is available.
async fn test_server() -> Option<(String, ServerGuard)> {
    if let Ok(url) = std::env::var("REDIS_URL") {
        return Some((url, ServerGuard::External));
    }

    if !redis_server_available() {
        eprintln!(
            "skipping live Redis test: REDIS_URL is not set and no `redis-server` binary was found on PATH"
        );
        return None;
    }

    let port = free_ephemeral_port();
    let mut dir: PathBuf = std::env::temp_dir();
    dir.push(format!("agent-framework-redis-it-{}", Uuid::new_v4()));
    std::fs::create_dir_all(&dir).expect("create redis-server working dir");

    let mut child = Command::new("redis-server")
        .args([
            "--port",
            &port.to_string(),
            "--bind",
            "127.0.0.1",
            "--save",
            "",
            "--appendonly",
            "no",
            "--daemonize",
            "no",
            "--dir",
        ])
        .arg(&dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("spawn redis-server");

    let url = format!("redis://127.0.0.1:{port}/0");
    if !wait_until_ready(&url).await {
        eprintln!("skipping live Redis test: spawned redis-server did not become ready in time");
        // Don't leave a zombie/orphaned process behind on the skip path.
        let _ = child.kill();
        let _ = child.wait();
        return None;
    }

    Some((url, ServerGuard::Spawned(child)))
}

fn unique(prefix: &str) -> String {
    format!("{prefix}-{}", Uuid::new_v4())
}

/// Probe `url` directly (independent of [`RedisContextProvider`]'s own,
/// private capability cache) for a loaded RediSearch module via `FT._LIST`.
/// Used to gate the `context_provider_redisearch_*` tests below: they only
/// assert anything when this returns `true`, which in practice means
/// `REDIS_URL` was pointed at a Redis Stack server — the plain
/// `redis-server` this file spawns itself never has RediSearch loaded.
async fn redisearch_available(url: &str) -> bool {
    let Ok(client) = redis::Client::open(url) else {
        return false;
    };
    let Ok(mut conn) = client.get_multiplexed_async_connection().await else {
        return false;
    };
    redis::cmd("FT._LIST")
        .query_async::<Vec<String>>(&mut conn)
        .await
        .is_ok()
}

#[tokio::test]
async fn chat_message_store_add_list_clear_round_trip() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    let store = RedisChatMessageStore::new(&url, Some(unique("thread"))).unwrap();

    assert!(store.list_messages().await.unwrap().is_empty());

    store
        .add_messages(vec![
            Message::user("Hello"),
            Message::assistant("Hi there!"),
        ])
        .await
        .unwrap();

    let messages = store.list_messages().await.unwrap();
    assert_eq!(messages.len(), 2);
    assert_eq!(messages[0].text(), "Hello");
    assert_eq!(messages[1].text(), "Hi there!");

    store
        .add_messages(vec![Message::user("How are you?")])
        .await
        .unwrap();
    let messages = store.list_messages().await.unwrap();
    assert_eq!(messages.len(), 3);
    assert_eq!(messages[2].text(), "How are you?");

    store.clear().await.unwrap();
    assert!(store.list_messages().await.unwrap().is_empty());
}

#[tokio::test]
async fn chat_message_store_trims_to_max_messages() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    let store = RedisChatMessageStore::new(&url, Some(unique("thread")))
        .unwrap()
        .with_max_messages(3);

    for i in 0..5 {
        store
            .add_messages(vec![Message::user(format!("message {i}"))])
            .await
            .unwrap();
    }

    let messages = store.list_messages().await.unwrap();
    assert_eq!(messages.len(), 3);
    // LTRIM keeps the most recent 3: messages 2, 3, 4.
    assert_eq!(messages[0].text(), "message 2");
    assert_eq!(messages[1].text(), "message 3");
    assert_eq!(messages[2].text(), "message 4");
}

#[tokio::test]
async fn chat_message_store_auto_generated_thread_ids_are_isolated() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    let key_prefix = unique("iso");
    let store_a = RedisChatMessageStore::new(&url, None)
        .unwrap()
        .with_key_prefix(&key_prefix);
    let store_b = RedisChatMessageStore::new(&url, None)
        .unwrap()
        .with_key_prefix(&key_prefix);
    assert_ne!(store_a.session_id(), store_b.session_id());

    store_a
        .add_messages(vec![Message::user("only in A")])
        .await
        .unwrap();

    assert_eq!(store_a.list_messages().await.unwrap().len(), 1);
    assert!(store_b.list_messages().await.unwrap().is_empty());
}

#[tokio::test]
async fn chat_message_store_survives_to_dict_round_trip_against_live_server() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    let thread_id = unique("thread");
    let store = RedisChatMessageStore::new(&url, Some(thread_id.clone())).unwrap();
    store
        .add_messages(vec![Message::user("persisted")])
        .await
        .unwrap();

    let state = store.to_dict();
    let restored = RedisChatMessageStore::from_dict(&state).unwrap();
    let messages = restored.list_messages().await.unwrap();
    assert_eq!(messages.len(), 1);
    assert_eq!(messages[0].text(), "persisted");
}

#[tokio::test]
async fn chat_message_store_before_run_prepends_history_and_after_run_records_it() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    let store = RedisChatMessageStore::new(&url, Some(unique("thread"))).unwrap();

    // Nothing stored yet: before_run leaves the run's own messages untouched.
    let mut ctx = SessionContext::new(vec![Message::user("first turn")]);
    store.before_run(&mut ctx).await.unwrap();
    assert!(ctx.messages.is_empty());

    // A successful run records the request + response messages.
    store
        .after_run(
            &[Message::user("first turn")],
            &[Message::assistant("first reply")],
            None,
        )
        .await
        .unwrap();

    // The next run sees the recorded turn prepended ahead of its own input.
    let mut ctx2 = SessionContext::new(vec![Message::user("second turn")]);
    store.before_run(&mut ctx2).await.unwrap();
    let texts: Vec<String> = ctx2.messages.iter().map(|m| m.text()).collect();
    assert_eq!(
        texts,
        vec!["first turn".to_string(), "first reply".to_string()]
    );

    // A failed run must not record anything.
    store
        .after_run(
            &[Message::user("second turn")],
            &[],
            Some(&agent_framework_core::error::Error::service("boom")),
        )
        .await
        .unwrap();
    let messages = store.list_messages().await.unwrap();
    assert_eq!(messages.len(), 2);

    assert!(store.is_history_provider());
}

#[tokio::test]
async fn context_provider_after_run_then_before_run_surfaces_matching_memory() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    let provider = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(unique("ctx"))
        .with_user_id("user-it-1");

    provider
        .after_run(&[Message::user("I love hiking in the Cascades")], &[], None)
        .await
        .unwrap();

    let mut ctx = SessionContext::new(vec![Message::user("Tell me about hiking")]);
    provider.before_run(&mut ctx).await.unwrap();

    assert_eq!(ctx.messages.len(), 1);
    assert!(ctx.messages[0]
        .text()
        .contains("I love hiking in the Cascades"));
    assert!(ctx.messages[0]
        .text()
        .starts_with("## Memories\nConsider the following memories"));
}

#[tokio::test]
async fn context_provider_before_run_returns_empty_context_when_nothing_matches() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    let provider = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(unique("ctx"))
        .with_user_id("user-it-2");

    provider
        .after_run(&[Message::user("I love hiking in the Cascades")], &[], None)
        .await
        .unwrap();

    // No overlapping token with the stored memory.
    let mut ctx = SessionContext::new(vec![Message::user("What is the capital of France?")]);
    provider.before_run(&mut ctx).await.unwrap();
    assert!(ctx.messages.is_empty());
}

#[tokio::test]
async fn context_provider_scopes_memories_by_user_id() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    let key_prefix = unique("ctx");

    let provider_a = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(&key_prefix)
        .with_user_id("user-a");
    let provider_b = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(&key_prefix)
        .with_user_id("user-b");

    provider_a
        .after_run(
            &[Message::user("user-a's secret hobby is pottery")],
            &[],
            None,
        )
        .await
        .unwrap();

    let mut ctx_b = SessionContext::new(vec![Message::user("Tell me about pottery")]);
    provider_b.before_run(&mut ctx_b).await.unwrap();
    assert!(
        ctx_b.messages.is_empty(),
        "provider scoped to user-b must not see user-a's memories"
    );

    let mut ctx_a = SessionContext::new(vec![Message::user("Tell me about pottery")]);
    provider_a.before_run(&mut ctx_a).await.unwrap();
    assert_eq!(ctx_a.messages.len(), 1);
}

#[tokio::test]
async fn context_provider_session_id_conflict_is_enforced_end_to_end() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    let provider = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(unique("ctx"))
        .with_user_id("user-it-3")
        .with_scope_to_per_operation_thread_id(true);

    let mut ctx1 = SessionContext::new(vec![]);
    ctx1.session_id = Some("thread-1".to_string());
    provider.before_run(&mut ctx1).await.unwrap();

    let mut ctx2 = SessionContext::new(vec![]);
    ctx2.session_id = Some("thread-2".to_string());
    let err = provider.before_run(&mut ctx2).await.unwrap_err();
    assert!(err.to_string().contains("only be used with one thread"));
}

/// Forcing the SCAN fallback must behave identically no matter what the
/// connected server actually supports — this is the "still asserting
/// fallback correctness end-to-end" case that always runs (never gated on
/// RediSearch availability), including when `REDIS_URL` happens to point at
/// a Redis Stack server.
#[tokio::test]
async fn context_provider_force_scan_fallback_works_regardless_of_redisearch_availability() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    let provider = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(unique("ctx"))
        .with_user_id("user-force-fallback")
        .with_force_scan_fallback(true);

    provider
        .after_run(&[Message::user("I love hiking in the Cascades")], &[], None)
        .await
        .unwrap();

    let mut ctx = SessionContext::new(vec![Message::user("Tell me about hiking")]);
    provider.before_run(&mut ctx).await.unwrap();

    assert_eq!(ctx.messages.len(), 1);
    assert!(ctx.messages[0]
        .text()
        .contains("I love hiking in the Cascades"));
}

#[tokio::test]
async fn context_provider_redisearch_finds_and_excludes_memories_when_available() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    if !redisearch_available(&url).await {
        eprintln!(
            "skipping RediSearch-specific test: connected Redis server has no RediSearch \
             module loaded (point REDIS_URL at a Redis Stack server to exercise FT.SEARCH)"
        );
        return;
    }

    let provider = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(unique("ftctx"))
        .with_user_id("user-ft-1");

    provider
        .after_run(&[Message::user("I love hiking in the Cascades")], &[], None)
        .await
        .unwrap();

    let mut ctx = SessionContext::new(vec![Message::user("Tell me about hiking")]);
    provider.before_run(&mut ctx).await.unwrap();
    assert_eq!(ctx.messages.len(), 1);
    assert!(ctx.messages[0]
        .text()
        .contains("I love hiking in the Cascades"));
    assert!(ctx.messages[0]
        .text()
        .starts_with("## Memories\nConsider the following memories"));

    // No overlapping meaningful token -> FT.SEARCH finds nothing, mirroring
    // the fallback path's equivalent assertion.
    let mut ctx_empty = SessionContext::new(vec![Message::user("What is the capital of France?")]);
    provider.before_run(&mut ctx_empty).await.unwrap();
    assert!(ctx_empty.messages.is_empty());
}

#[tokio::test]
async fn context_provider_redisearch_scopes_memories_by_tag_filter_when_available() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    if !redisearch_available(&url).await {
        eprintln!("skipping RediSearch-specific test: no RediSearch module loaded");
        return;
    }

    let key_prefix = unique("ftctx");
    let provider_a = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(&key_prefix)
        .with_user_id("user-ft-a");
    let provider_b = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(&key_prefix)
        .with_user_id("user-ft-b");

    provider_a
        .after_run(
            &[Message::user("user-ft-a's secret hobby is pottery")],
            &[],
            None,
        )
        .await
        .unwrap();

    let mut ctx_b = SessionContext::new(vec![Message::user("Tell me about pottery")]);
    provider_b.before_run(&mut ctx_b).await.unwrap();
    assert!(
        ctx_b.messages.is_empty(),
        "provider scoped to user-ft-b must not see user-ft-a's memories via FT.SEARCH's TAG filter"
    );

    let mut ctx_a = SessionContext::new(vec![Message::user("Tell me about pottery")]);
    provider_a.before_run(&mut ctx_a).await.unwrap();
    assert_eq!(ctx_a.messages.len(), 1);
}

#[tokio::test]
async fn context_provider_redisearch_respects_limit_when_available() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    if !redisearch_available(&url).await {
        eprintln!("skipping RediSearch-specific test: no RediSearch module loaded");
        return;
    }

    let provider = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(unique("ftctx"))
        .with_user_id("user-ft-limit")
        .with_limit(2);

    for i in 0..5 {
        provider
            .after_run(
                &[Message::user(format!("apple fact number {i}"))],
                &[],
                None,
            )
            .await
            .unwrap();
    }

    let mut ctx = SessionContext::new(vec![Message::user("apple")]);
    provider.before_run(&mut ctx).await.unwrap();
    assert_eq!(ctx.messages.len(), 1);
    // DEFAULT_CONTEXT_PROMPT is itself two lines; every line after that is
    // one matched memory, so this counts how many FT.SEARCH actually
    // returned under `LIMIT 0 2`.
    let memory_line_count = ctx.messages[0].text().lines().skip(2).count();
    assert_eq!(memory_line_count, 2);
}

/// Entries written by the RediSearch path (`JSON.SET`) are a different
/// Redis value type than the SCAN fallback's plain `SET` strings — the
/// module docs call this out explicitly. Confirm it end-to-end: a memory
/// stored while RediSearch is in use is invisible to a *second*, otherwise
/// identically-scoped provider that has fallback forced on.
#[tokio::test]
async fn context_provider_redisearch_entries_are_not_visible_to_forced_scan_fallback() {
    let Some((url, _guard)) = test_server().await else {
        return;
    };
    if !redisearch_available(&url).await {
        eprintln!("skipping RediSearch-specific test: no RediSearch module loaded");
        return;
    }

    let key_prefix = unique("ftctx");
    let ft_provider = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(&key_prefix)
        .with_user_id("user-ft-cross");
    let fallback_provider = RedisContextProvider::new(&url)
        .unwrap()
        .with_key_prefix(&key_prefix)
        .with_user_id("user-ft-cross")
        .with_force_scan_fallback(true);

    ft_provider
        .after_run(&[Message::user("stored via JSON.SET")], &[], None)
        .await
        .unwrap();

    let mut ctx = SessionContext::new(vec![Message::user("Tell me about JSON.SET")]);
    fallback_provider.before_run(&mut ctx).await.unwrap();
    assert!(
        ctx.messages.is_empty(),
        "SCAN+MGET must not see a JSON.SET-backed entry (documented storage-encoding divergence)"
    );
}