| 9623 | } |
| 9624 | pthread_mutex_unlock(&s->kv_mu); |
| 9625 | slot->continued_last_store_tokens = 0; |
| 9626 | pthread_mutex_lock(&s->inference_mu); |
| 9627 | ds4_session_invalidate(slot->session); |
| 9628 | pthread_mutex_unlock(&s->inference_mu); |
| 9629 | } |
| 9630 | |
| 9631 | static void kv_cache_maybe_store_continued(server *s, server_slot *slot) { |
| 9632 | if (!s || !slot) return; |
| 9633 | kv_disk_cache *kc = &s->kv; |
| 9634 | const ds4_tokens *tokens = ds4_session_tokens(slot->session); |
| 9635 | if (!tokens) return; |
| 9636 | const int target = kv_cache_slot_continued_target(s, slot, tokens->len); |
| 9637 | if (target == 0) return; |
| 9638 | if (kv_cache_store_live_prefix(s, slot, tokens, target, "continued")) { |
| 9639 | (void)kc; |
| 9640 | kv_cache_slot_note_store(slot, target); |
| 9641 | } |
| 9642 | } |
| 9643 | |
| 9644 | #ifdef DS4_SERVER_TEST |
| 9645 | static int kv_cache_find_text_prefix(kv_disk_cache *kc, const char *prompt_text, |
| 9646 | int quant_bits, int ctx_size) { |
| 9647 | return ds4_kvstore_find_text_prefix(kc, prompt_text, 0, quant_bits, ctx_size); |
| 9648 | } |
| 9649 | #endif |
| 9650 | |
| 9651 | static int kv_cache_try_load_text(server *s, server_slot *slot, |
| 9652 | const char *prompt_text, |
| 9653 | ds4_tokens *effective_prompt, |
| 9654 | char **loaded_path_out, |
| 9655 | uint8_t *loaded_ext_flags_out, |
| 9656 | bool responses_protocol) { |
| 9657 | if (!s || !slot) return 0; |
| 9658 | if (loaded_path_out) *loaded_path_out = NULL; |
| 9659 | if (loaded_ext_flags_out) *loaded_ext_flags_out = 0; |
| 9660 | ds4_kvstore_load_result lr = {0}; |
| 9661 | ds4_kvstore_trailer_hooks hooks = kv_cache_tool_map_hooks(s, NULL); |
| 9662 | pthread_mutex_lock(&s->inference_mu); |
| 9663 | pthread_mutex_lock(&s->kv_mu); |
| 9664 | int loaded = ds4_kvstore_try_load_text(&s->kv, s->engine, slot->session, |
| 9665 | prompt_text, effective_prompt, &lr, |
| 9666 | &hooks, responses_protocol); |
| 9667 | pthread_mutex_unlock(&s->kv_mu); |
| 9668 | pthread_mutex_unlock(&s->inference_mu); |
| 9669 | if (loaded > 0) { |
| 9670 | if (loaded_path_out && lr.path) *loaded_path_out = xstrdup(lr.path); |
| 9671 | if (loaded_ext_flags_out) *loaded_ext_flags_out = lr.ext_flags; |
| 9672 | } |
| 9673 | ds4_kvstore_load_result_free(&lr); |
| 9674 | return loaded; |
| 9675 | } |
| 9676 | |
| 9677 | static int kv_cache_try_load(server *s, server_slot *slot, const request *req, |
| 9678 | ds4_tokens *effective_prompt, |
| 9679 | char **loaded_path_out, |
| 9680 | uint8_t *loaded_ext_flags_out) { |
| 9681 | return kv_cache_try_load_text(s, slot, req ? req->prompt_text : NULL, |
| 9682 | effective_prompt, |
nothing calls this directly
no test coverage detected