aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-kv-cache / tests / truncate_sequence_tests.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! `truncate_sequence` (draft-verify lane, 2026-10-12): drops the K/V past a position so a |
| 17 | //! speculative verify block can be rolled back; the next append continues at the cut. |
| 18 | use aien_kv_cache::AienKvManager; |
| 19 | |
| 20 | const BS: usize = 16; |
| 21 | |
| 22 | #[test] |
| 23 | fn truncate_frees_trailing_blocks_and_appends_continue_at_the_cut() { |
| 24 | let mut mgr = AienKvManager::new(64, BS); |
| 25 | let prompt: Vec<u32> = (0..40).collect(); |
| 26 | mgr.allocate_sequence(1, &prompt).unwrap(); |
| 27 | let free0 = mgr.free_block_count(); |
| 28 | // Eight appends: 48 tokens, 3 blocks full. |
| 29 | let mut slots = Vec::new(); |
| 30 | for _ in 0..8 { |
| 31 | slots.push(mgr.append_token_with_slot(1).unwrap()); |
| 32 | } |
| 33 | assert_eq!(mgr.get_block_table(1).unwrap().total_tokens, 48); |
| 34 | // Roll back to 41: the third block keeps 9 tokens, nothing freed. |
| 35 | mgr.truncate_sequence(1, 41).unwrap(); |
| 36 | let t = mgr.get_block_table(1).unwrap(); |
| 37 | assert_eq!(t.total_tokens, 41); |
| 38 | assert_eq!(t.block_ids.len(), 3); |
| 39 | assert_eq!(mgr.free_block_count(), free0); |
| 40 | // The next append lands where token 41 was. |
| 41 | assert_eq!(mgr.append_token_with_slot(1).unwrap(), slots[1]); |
| 42 | // Roll back to a block boundary: the third block is freed, the next append opens a new one. |
| 43 | mgr.truncate_sequence(1, 32).unwrap(); |
| 44 | let t = mgr.get_block_table(1).unwrap(); |
| 45 | assert_eq!(t.total_tokens, 32); |
| 46 | assert_eq!(t.block_ids.len(), 2); |
| 47 | assert_eq!(mgr.free_block_count(), free0 + 1); |
| 48 | let (blk, slot) = mgr.append_token_with_slot(1).unwrap(); |
| 49 | assert_eq!(slot, 0); |
| 50 | assert_eq!(mgr.get_block_table(1).unwrap().block_ids[2], blk); |
| 51 | assert_eq!(mgr.get_block_table(1).unwrap().total_tokens, 33); |
| 52 | // Past the end and unknown sequences are refused; no-op at the current length. |
| 53 | assert!(mgr.truncate_sequence(1, 34).is_err()); |
| 54 | assert!(mgr.truncate_sequence(2, 0).is_err()); |
| 55 | mgr.truncate_sequence(1, 33).unwrap(); |
| 56 | assert_eq!(mgr.get_block_table(1).unwrap().total_tokens, 33); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn truncate_refuses_shared_blocks() { |
| 61 | let mut mgr = AienKvManager::new(64, BS); |
| 62 | let prompt: Vec<u32> = (0..40).collect(); |
| 63 | mgr.allocate_sequence(1, &prompt).unwrap(); |
| 64 | mgr.fork_sequence(1, 2).unwrap(); |
| 65 | // The forked prefix is shared: a cut inside it must be refused, a cut that only touches |
| 66 | // the child's own tail is fine once it has one. |
| 67 | assert!(mgr.truncate_sequence(2, 20).is_err()); |
| 68 | assert!(mgr.truncate_sequence(2, 40).is_ok()); |
| 69 | } |