| 239 | } |
| 240 | |
| 241 | int main() { |
| 242 | publication_allocation_failures(); |
| 243 | pool_reclaim_allocation_failure(); |
| 244 | { |
| 245 | ConversationCache cache(1u << 20, 2); |
| 246 | const std::vector<int32_t> live = {1, 2, 3, 4, 9}; |
| 247 | check(!cache.has_reuse_for(live), "no retained K/V does not block exchange"); |
| 248 | std::vector<ConversationKv> retained(1); |
| 249 | retained[0].k.resize(64, 0x5a); |
| 250 | cache.retain(std::move(retained), 5, {}, {1, 2, 3, 4, 5}); |
| 251 | cache.limit_reuse(4); |
| 252 | const size_t before_bytes = cache.retained_bytes(); |
| 253 | check(cache.has_reuse_for(live) && cache.has_reuse_for(std::vector<int32_t>{1, 99}) && |
| 254 | !cache.has_reuse_for(std::vector<int32_t>{9, 1, 2}) && |
| 255 | !cache.has_reuse_for(std::vector<int32_t>{}), |
| 256 | "only a positive common retained prefix blocks exchange"); |
| 257 | bool attempted = false; |
| 258 | // Use the same gate as generate.cpp: the old park keeps its reusable prefix. |
| 259 | if (!cache.has_reuse_for(live)) { |
| 260 | attempted = true; |
| 261 | (void) cache.can_fit(SIZE_MAX); |
| 262 | } |
| 263 | check(!attempted && !cache.can_fit(SIZE_MAX) && cache.retained_bytes() == before_bytes, |
| 264 | "existing retained prefix bypasses exchange and survives budget refusal"); |
| 265 | auto reuse = cache.take_reuse_for(live); |
| 266 | uint8_t first = 0, last = 0; |
| 267 | check(reuse.unchanged_tokens == 4 && reuse.kv.size() == 1 && reuse.kv[0].k.size() == 64 && |
| 268 | reuse.kv[0].k.read(&first, 0, 1) && reuse.kv[0].k.read(&last, 63, 1) && first == 0x5a && last == 0x5a, |
| 269 | "old capture path still takes the original reusable prefix and bytes"); |
| 270 | cache.retain(std::move(reuse.kv), 5, {}, {1, 2, 3, 4, 5}); |
| 271 | const std::vector<int32_t> one_prefix = {1, 99}; |
| 272 | check(cache.has_reuse_for(one_prefix), "one shared token blocks exchange"); |
| 273 | reuse = cache.take_reuse_for(one_prefix); |
| 274 | check(reuse.unchanged_tokens == 1 && !reuse.kv.empty(), "one shared token is reusable by the old path"); |
| 275 | cache.retain(std::move(reuse.kv), 5, {}, {1, 2, 3, 4, 5}); |
| 276 | const std::vector<int32_t> unrelated = {9, 1, 2}; |
| 277 | check(!cache.has_reuse_for(unrelated), "unrelated retained K/V does not block exchange"); |
| 278 | reuse = cache.take_reuse_for(unrelated); |
| 279 | check(reuse.unchanged_tokens == 0 && reuse.kv.empty(), "the old path also refuses an unrelated prefix"); |
| 280 | std::vector<ConversationKv> no_identity(1); |
| 281 | no_identity[0].k.resize(64, 0x5a); |
| 282 | cache.retain(std::move(no_identity), 5); |
| 283 | check(cache.retained_tokens() > 0 && !cache.has_reuse_for(live), |
| 284 | "retained K/V without identity does not block exchange"); |
| 285 | } |
| 286 | { |
| 287 | int mib = conversation_cache_swap_default_block_mib; |
| 288 | check(mib == 0 && conversation_cache_swap_block_bytes(mib) == 0, |
| 289 | "host exchange is disabled by default"); |
| 290 | for (int value = 0; value <= 256; ++value) { |
| 291 | const std::string text = std::to_string(value); |
| 292 | check(parse_conversation_cache_swap_block_mib(text, mib) && mib == value && |
| 293 | conversation_cache_swap_block_bytes(mib) == (size_t(value) << 20), |
| 294 | "exchange block accepts every in-range decimal"); |
| 295 | } |
| 296 | for (const auto& text : {"", "257", "-0", "-1", "+1", "1x", "1,2", "1.0", " 1", "1 ", "99999999999999999999999"}) { |
| 297 | mib = 17; |
| 298 | check(!parse_conversation_cache_swap_block_mib(text, mib) && mib == 17, |
nothing calls this directly
no test coverage detected