From a33ab0bdbaafdda52d4bcdd53fbca36a2fe78157 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Fri, 30 Oct 2026 03:41:57 +0100 Subject: [PATCH] conversation: pin that a restored conversation's retained K/V never serves another conversation (upstream f8ff3420 check) Characterization test, no code change: A, B, A (restored, K/V retained), C (resume 0) and back to A, B, C, each restore checked cell by cell by the harness. The production serve stack binds no parking (NO_PARKING in original_serve_conversation.rs), so retain() is unreachable there; the library path clears the retained image with limit_reuse(read_from) at every request start. Co-Authored-By: Claude Sonnet 5.5 --- .../src/conversation/manager_tests.rs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/crates/leleka-runtime/src/conversation/manager_tests.rs b/crates/leleka-runtime/src/conversation/manager_tests.rs index be8eb027..0f547eef 100644 --- a/crates/leleka-runtime/src/conversation/manager_tests.rs +++ b/crates/leleka-runtime/src/conversation/manager_tests.rs @@ -644,6 +644,41 @@ fn switching_between_conversations_parks_and_restores_exactly() { ); } +#[test] +fn a_restored_conversations_retained_kv_never_serves_another_conversation() { + // Upstream f8ff3420: a retained K/V image tracked a token COUNT, so after another conversation took the session the next park + // reused the first conversation's K/V for the new one. Here the retained image is dropped as soon as a request rewrites the + // session below the cells it claims, and every restore below is checked cell by cell against the tokens it names. + let mut w = world(options(), false); + let a1 = chat(1, 20, &[10]); + let ra = w.request(&a1, &[], true, &gen_tokens(1, 6), None, 8).unwrap(); + let b1 = chat(2, 18, &[14]); + let rb = w.request(&b1, &[], true, &gen_tokens(2, 6), None, 8).unwrap(); + assert_eq!(w.mgr.cache().retained_bytes(), 0, "nothing was restored yet"); + // back to A: restored, its K/V is kept so the next park of A copies only what changed + let mut a2 = ra.consumed.clone(); + a2.extend([TURN, 1, 2, 3, 4]); + let r = w.request(&a2, &[], true, &gen_tokens(3, 4), None, 8).unwrap(); + assert!(r.start.restored.is_some()); + assert!(w.mgr.cache().retained_bytes() > 0, "the restore retained A's K/V"); + w.mgr.take_events(); + // a third conversation takes the session from A (resume 0): A is parked, and the retained image is consumed by A's own park only + let c1 = chat(3, 16, &[12]); + let rc = w.request(&c1, &[], true, &gen_tokens(4, 6), None, 8).unwrap(); + assert_eq!(rc.start.resume, 0); + assert_eq!(w.mgr.cache().retained_bytes(), 0, "C never inherits A's retained K/V"); + let events = w.mgr.take_events(); + let parked_a = events.iter().filter(|e| e.contains("parked ") && e.contains(&format!("{} tokens", r.consumed.len()))).count(); + assert_eq!(parked_a, 1, "{events:?}"); + // every conversation comes back with exactly its own cells (the harness asserts the device state after each begin_request) + for (conv, tag) in [(r.consumed.clone(), 5), (rb.consumed.clone(), 6), (rc.consumed.clone(), 7)] { + let mut next = conv.clone(); + next.extend([TURN, tag, tag, tag]); + let back = w.request(&next, &[], true, &gen_tokens(tag, 3), None, 8).unwrap(); + assert_eq!(back.start.restored.map(|(tokens, _)| tokens).unwrap_or(back.start.resume), conv.len(), "tag {tag}"); + } +} + #[test] fn a_parked_conversation_restores_through_its_checkpoint_when_the_tail_was_rewritten() { let mut w = world(options(), false);