[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Explain what this code does and identify the inference stage it belongs to.

File: ds4_server.c

```c
s instead of preserving a microscopic floating-point bonus.
     */
    double effective_hits = (double)e->hits;
    uint64_t used_at = e->last_used ? e->last_used : e->created_at;
    if (used_at == 0) {
        effective_hits = 0.0;
    } else if (now > used_at) {
        double elapsed = (double)(now - used_at);
        effective_hits *= exp2(-elapsed / (double)KV_CACHE_HIT_HALF_LIFE_SECONDS);
        if (effective_hits < KV_CACHE_MIN_EFFECTIVE_HITS) effective_hits = 0.0;
    }
    return (effective_hits + 1.0) * (double)e->tokens / (double)e->file_size;
}

static void kv_cache_evict(kv_disk_cache *kc, const ds4_tokens *live,
                           const char *protected_sha) {
    if (!kc->enabled || kc->budget_bytes == 0) return;
    kv_cache_refresh(kc);
    const uint64_t now = (uint64_t)time(NULL);
    uint64_t total = 0;
    for (int i = 0; i < kc->len; i++) total += kc->entry[i].file_size;
    if (protected_sha) {
        uint64_t protected_size = 0;
        for (int i = 0; i < kc->len; i++) {
            if (!strcmp(kc->entry[i].sha, protected_sha)) {
                protected_size = kc->entry[i].file_size;
                break;
            }
        }
        /* Do not preserve an entry that cannot fit in the budget by itself:
         * protecting it would first delete every older checkpoint, then delete
         * the new one anyway. */
        if (protected_size > kc->budget_bytes) protected_sha = NULL;
    }
    while (total > kc->budget_bytes && kc->len > 0) {
        int victim = 0;
        double victim_score = kv_entry_eviction_score(&kc->entry[0], live, protected_sha, now);
        for (int i = 1; i < kc->len; i++) {
            double score = kv_entry_eviction_score(&kc->entry[i], live, protected_sha, now);
            if (score < victim_score ||
                (score == victim_score && kc->entry[i].last_used < kc->entry[victim].last_used))
            {
                victim = i;
                victim_score = score;
            }
        }
        kv_entry e = kc->entry[victim];
        if (unlink(e.path) == 0) {
            server_log(DS4_LOG_KVCACHE,
                       "ds4-server: kv cache evicted reason=disk-cache-full tokens=%u hits=%u size=%.2f MiB file=%s",
                       e.tokens,
                       e.hits,
                       (double)e.file_size / (1024.0 * 1024.0),
                       e.path ? e.path : "?");
            if (total >= e.file_size) total -= e.file_size;
            else total = 0;
        } else {
            total = 0;
        }
        kv_entry_free(&e);
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Explain what this code does and identify the inference stage it belongs to.

File: ds4_server.c

```c
s instead of preserving a microscopic floating-point bonus.
     */
    double effective_hits = (double)e->hits;
    uint64_t used_at = e->last_used ? e->last_used : e->created_at;
    if (used_at == 0) {
        effective_hits = 0.0;
    } else if (now > used_at) {
        double elapsed = (double)(now - used_at);
        effective_hits *= exp2(-elapsed / (double)KV_CACHE_HIT_HALF_LIFE_SECONDS);
        if (effective_hits < KV_CACHE_MIN_EFFECTIVE_HITS) effective_hits = 0.0;
    }
    return (effective_hits + 1.0) * (double)e->tokens / (double)e->file_size;
}

static void kv_cache_evict(kv_disk_cache *kc, const ds4_tokens *live,
                           const char *protected_sha) {
    if (!kc->enabled || kc->budget_bytes == 0) return;
    kv_cache_refresh(kc);
    const uint64_t now = (uint64_t)time(NULL);
    uint64_t total = 0;
    for (int i = 0; i < kc->len; i++) total += kc->entry[i].file_size;
    if (protected_sha) {
        uint64_t protected_size = 0;
        for (int i = 0; i < kc->len; i++) {
            if (!strcmp(kc->entry[i].sha, protected_sha)) {
                protected_size = kc->entry[i].file_size;
                break;
            }
        }
        /* Do not preserve an entry that cannot fit in the budget by itself:
         * protecting it would first delete every older checkpoint, then delete
         * the new one anyway. */
        if (protected_size > kc->budget_bytes) protected_sha = NULL;
    }
    while (total > kc->budget_bytes && kc->len > 0) {
        int victim = 0;
        double victim_score = kv_entry_eviction_score(&kc->entry[0], live, protected_sha, now);
        for (int i = 1; i < kc->len; i++) {
            double score = kv_entry_eviction_score(&kc->entry[i], live, protected_sha, now);
            if (score < victim_score ||
                (score == victim_score && kc->entry[i].last_used < kc->entry[victim].last_used))
            {
                victim = i;
                victim_score = score;
            }
        }
        kv_entry e = kc->entry[victim];
        if (unlink(e.path) == 0) {
            server_log(DS4_LOG_KVCACHE,
                       "ds4-server: kv cache evicted reason=disk-cache-full tokens=%u hits=%u size=%.2f MiB file=%s",
                       e.tokens,
                       e.hits,
                       (double)e.file_size / (1024.0 * 1024.0),
                       e.path ? e.path : "?");
            if (total >= e.file_size) total -= e.file_size;
            else total = 0;
        } else {
            total = 0;
        }
        kv_entry_free(&e);
```<｜Assistant｜><think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
e.path : "?");
            if (total >= e.file_size) total -= e.file_size;
            else total = 0;
        } else {
            total = 0;
        }
        kv_entry_free(&e);
        memmove(kc->entry + victim, kc->entry + victim + 1,
                (size_t)(kc->len - victim - 1) * sizeof(kc->entry[0]));
        kc->len--;
    }
}

static bool kv_cache_open(kv_disk_cache *kc, const char *dir, uint64_t budget_mb,
                          bool reject_different_quant, kv_cache_options opt) {
    memset(kc, 0, sizeof(*kc));
    if (!dir) return false;
    if (!mkdir_p(dir)) {
        server_log(DS4_LOG_DEFAULT, "ds4-server: failed to create KV cache directory %s: %s", dir, strerror(errno));
        return false;
    }
    kc->enabled = true;
    kc->dir = xstrdup(dir);
    if (budget_mb == 0) budget_mb = KV_CACHE_DEFAULT_MB;
    kc->budget_bytes = budget_mb * 1024ull * 1024ull;
    kc->reject_different_quant = reject_different_quant;
    kc->opt = opt;
    kv_cache_evict(kc, NULL, NULL);
    server_log(DS4_LOG_KVCACHE,
               "ds4-server: KV disk cache %s (budget=%llu MiB, cross-quant=%s, min=%d, cold_max=%d, continued=%d, trim=%d, align=%d, hit_half_life=%llus)",
               kc->dir,
               (unsigned long long)(kc->budget_bytes / (1024ull * 1024ull)),
               reject_different_quant ? "reject" : "accept",
               kc->opt.min_tokens,
               kc->opt.cold_max_tokens,
               kc->opt.continued_interval_tokens,
               kc->opt.boundary_trim_tokens,
               kc->opt.boundary_align_tokens,
               (unsigned long long)KV_CACHE_HIT_HALF_LIFE_SECONDS);
    return true;
}

static void kv_cache_close(kv_disk_cache *kc) {
    kv_cache_clear(kc);
    free(kc->dir);
    memset(kc, 0, sizeof(*kc));
}

static char *render_tokens_text(ds4_engine *engine, const ds4_tokens *tokens, size_t *out_len) {
    buf b = {0};
    for (int i = 0; i < tokens->len; i++) {
        size_t len = 0;
        char *piece = ds4_token_text(engine, tokens->v[i], &len);
        buf_append(&b, piece, len);
        free(piece);
    }
    if (out_len) *out_len = b.len;
    return buf_take(&b);
}

static bool byte_prefix_match(const char *text, size_t text_len,
                              const char *prefix, size_t prefix_len) {
    return prefix_len <= text_len &&
           (prefix_len == 0 || memcmp(text, prefix, prefix_len) == 0);
}

static const char *kv_cache_key_kind(uint8_t ext_flags) {
    if (ext_flags & KV_EXT_RESPONSES_VISIBLE) return "responses-visible";
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
e.path : "?");
            if (total >= e.file_size) total -= e.file_size;
            else total = 0;
        } else {
            total = 0;
        }
        kv_entry_free(&e);
        memmove(kc->entry + victim, kc->entry + victim + 1,
                (size_t)(kc->len - victim - 1) * sizeof(kc->entry[0]));
        kc->len--;
    }
}

static bool kv_cache_open(kv_disk_cache *kc, const char *dir, uint64_t budget_mb,
                          bool reject_different_quant, kv_cache_options opt) {
    memset(kc, 0, sizeof(*kc));
    if (!dir) return false;
    if (!mkdir_p(dir)) {
        server_log(DS4_LOG_DEFAULT, "ds4-server: failed to create KV cache directory %s: %s", dir, strerror(errno));
        return false;
    }
    kc->enabled = true;
    kc->dir = xstrdup(dir);
    if (budget_mb == 0) budget_mb = KV_CACHE_DEFAULT_MB;
    kc->budget_bytes = budget_mb * 1024ull * 1024ull;
    kc->reject_different_quant = reject_different_quant;
    kc->opt = opt;
    kv_cache_evict(kc, NULL, NULL);
    server_log(DS4_LOG_KVCACHE,
               "ds4-server: KV disk cache %s (budget=%llu MiB, cross-quant=%s, min=%d, cold_max=%d, continued=%d, trim=%d, align=%d, hit_half_life=%llus)",
               kc->dir,
               (unsigned long long)(kc->budget_bytes / (1024ull * 1024ull)),
               reject_different_quant ? "reject" : "accept",
               kc->opt.min_tokens,
               kc->opt.cold_max_tokens,
               kc->opt.continued_interval_tokens,
               kc->opt.boundary_trim_tokens,
               kc->opt.boundary_align_tokens,
               (unsigned long long)KV_CACHE_HIT_HALF_LIFE_SECONDS);
    return true;
}

static void kv_cache_close(kv_disk_cache *kc) {
    kv_cache_clear(kc);
    free(kc->dir);
    memset(kc, 0, sizeof(*kc));
}

static char *render_tokens_text(ds4_engine *engine, const ds4_tokens *tokens, size_t *out_len) {
    buf b = {0};
    for (int i = 0; i < tokens->len; i++) {
        size_t len = 0;
        char *piece = ds4_token_text(engine, tokens->v[i], &len);
        buf_append(&b, piece, len);
        free(piece);
    }
    if (out_len) *out_len = b.len;
    return buf_take(&b);
}

static bool byte_prefix_match(const char *text, size_t text_len,
                              const char *prefix, size_t prefix_len) {
    return prefix_len <= text_len &&
           (prefix_len == 0 || memcmp(text, prefix, prefix_len) == 0);
}

static const char *kv_cache_key_kind(uint8_t ext_flags) {
    if (ext_flags & KV_EXT_RESPONSES_VISIBLE) return "responses-visible";
```<｜Assistant｜></think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
e.path : "?");
            if (total >= e.file_size) total -= e.file_size;
            else total = 0;
        } else {
            total = 0;
        }
        kv_entry_free(&e);
        memmove(kc->entry + victim, kc->entry + victim + 1,
                (size_t)(kc->len - victim - 1) * sizeof(kc->entry[0]));
        kc->len--;
    }
}

static bool kv_cache_open(kv_disk_cache *kc, const char *dir, uint64_t budget_mb,
                          bool reject_different_quant, kv_cache_options opt) {
    memset(kc, 0, sizeof(*kc));
    if (!dir) return false;
    if (!mkdir_p(dir)) {
        server_log(DS4_LOG_DEFAULT, "ds4-server: failed to create KV cache directory %s: %s", dir, strerror(errno));
        return false;
    }
    kc->enabled = true;
    kc->dir = xstrdup(dir);
    if (budget_mb == 0) budget_mb = KV_CACHE_DEFAULT_MB;
    kc->budget_bytes = budget_mb * 1024ull * 1024ull;
    kc->reject_different_quant = reject_different_quant;
    kc->opt = opt;
    kv_cache_evict(kc, NULL, NULL);
    server_log(DS4_LOG_KVCACHE,
               "ds4-server: KV disk cache %s (budget=%llu MiB, cross-quant=%s, min=%d, cold_max=%d, continued=%d, trim=%d, align=%d, hit_half_life=%llus)",
               kc->dir,
               (unsigned long long)(kc->budget_bytes / (1024ull * 1024ull)),
               reject_different_quant ? "reject" : "accept",
               kc->opt.min_tokens,
               kc->opt.cold_max_tokens,
               kc->opt.continued_interval_tokens,
               kc->opt.boundary_trim_tokens,
               kc->opt.boundary_align_tokens,
               (unsigned long long)KV_CACHE_HIT_HALF_LIFE_SECONDS);
    return true;
}

static void kv_cache_close(kv_disk_cache *kc) {
    kv_cache_clear(kc);
    free(kc->dir);
    memset(kc, 0, sizeof(*kc));
}

static char *render_tokens_text(ds4_engine *engine, const ds4_tokens *tokens, size_t *out_len) {
    buf b = {0};
    for (int i = 0; i < tokens->len; i++) {
        size_t len = 0;
        char *piece = ds4_token_text(engine, tokens->v[i], &len);
        buf_append(&b, piece, len);
        free(piece);
    }
    if (out_len) *out_len = b.len;
    return buf_take(&b);
}

static bool byte_prefix_match(const char *text, size_t text_len,
                              const char *prefix, size_t prefix_len) {
    return prefix_len <= text_len &&
           (prefix_len == 0 || memcmp(text, prefix, prefix_len) == 0);
}

static const char *kv_cache_key_kind(uint8_t ext_flags) {
    if (ext_flags & KV_EXT_RESPONSES_VISIBLE) return "responses-visible";
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
e.path : "?");
            if (total >= e.file_size) total -= e.file_size;
            else total = 0;
        } else {
            total = 0;
        }
        kv_entry_free(&e);
        memmove(kc->entry + victim, kc->entry + victim + 1,
                (size_t)(kc->len - victim - 1) * sizeof(kc->entry[0]));
        kc->len--;
    }
}

static bool kv_cache_open(kv_disk_cache *kc, const char *dir, uint64_t budget_mb,
                          bool reject_different_quant, kv_cache_options opt) {
    memset(kc, 0, sizeof(*kc));
    if (!dir) return false;
    if (!mkdir_p(dir)) {
        server_log(DS4_LOG_DEFAULT, "ds4-server: failed to create KV cache directory %s: %s", dir, strerror(errno));
        return false;
    }
    kc->enabled = true;
    kc->dir = xstrdup(dir);
    if (budget_mb == 0) budget_mb = KV_CACHE_DEFAULT_MB;
    kc->budget_bytes = budget_mb * 1024ull * 1024ull;
    kc->reject_different_quant = reject_different_quant;
    kc->opt = opt;
    kv_cache_evict(kc, NULL, NULL);
    server_log(DS4_LOG_KVCACHE,
               "ds4-server: KV disk cache %s (budget=%llu MiB, cross-quant=%s, min=%d, cold_max=%d, continued=%d, trim=%d, align=%d, hit_half_life=%llus)",
               kc->dir,
               (unsigned long long)(kc->budget_bytes / (1024ull * 1024ull)),
               reject_different_quant ? "reject" : "accept",
               kc->opt.min_tokens,
               kc->opt.cold_max_tokens,
               kc->opt.continued_interval_tokens,
               kc->opt.boundary_trim_tokens,
               kc->opt.boundary_align_tokens,
               (unsigned long long)KV_CACHE_HIT_HALF_LIFE_SECONDS);
    return true;
}

static void kv_cache_close(kv_disk_cache *kc) {
    kv_cache_clear(kc);
    free(kc->dir);
    memset(kc, 0, sizeof(*kc));
}

static char *render_tokens_text(ds4_engine *engine, const ds4_tokens *tokens, size_t *out_len) {
    buf b = {0};
    for (int i = 0; i < tokens->len; i++) {
        size_t len = 0;
        char *piece = ds4_token_text(engine, tokens->v[i], &len);
        buf_append(&b, piece, len);
        free(piece);
    }
    if (out_len) *out_len = b.len;
    return buf_take(&b);
}

static bool byte_prefix_match(const char *text, size_t text_len,
                              const char *prefix, size_t prefix_len) {
    return prefix_len <= text_len &&
           (prefix_len == 0 || memcmp(text, prefix, prefix_len) == 0);
}

static const char *kv_cache_key_kind(uint8_t ext_flags) {
    if (ext_flags & KV_EXT_RESPONSES_VISIBLE) return "responses-visible";
```<｜Assistant｜><think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Trova bug sottili e spiega quali invarianti dell'inferenza possono rompersi.

File: ds4_server.c

```c
0 || memcmp(text, prefix, prefix_len) == 0);
}

static const char *kv_cache_key_kind(uint8_t ext_flags) {
    if (ext_flags & KV_EXT_RESPONSES_VISIBLE) return "responses-visible";
    if (ext_flags & KV_EXT_THINKING_VISIBLE) return "thinking-visible";
    return "token-text";
}

static void tokens_copy_prefix(ds4_tokens *dst, const ds4_tokens *src, int n) {
    dst->len = 0;
    if (n > src->len) n = src->len;
    for (int i = 0; i < n; i++) ds4_tokens_push(dst, src->v[i]);
}

static void tokens_append(ds4_tokens *dst, const ds4_tokens *src) {
    if (!dst || !src) return;
    for (int i = 0; i < src->len; i++) ds4_tokens_push(dst, src->v[i]);
}

static void build_prompt_from_exact_prefix_and_text_suffix(
        ds4_engine *engine,
        const ds4_tokens *exact_prefix,
        const char *suffix_text,
        ds4_tokens *out)
{
    ds4_tokens_copy(out, exact_prefix);

    ds4_tokens suffix = {0};
    /* The suffix may start with DS4 chat markers such as <｜User｜> or
     * </think>, so use the rendered-chat tokenizer, not plain text BPE. */
    ds4_tokenize_rendered_chat(engine, suffix_text ? suffix_text : "", &suffix);
    tokens_append(out, &suffix);
    ds4_tokens_free(&suffix);
}

static int kv_cache_store_len(const kv_disk_cache *kc, int tokens) {
    const int trim = kc->opt.boundary_trim_tokens;
    const int align = kc->opt.boundary_align_tokens;
    if (tokens > kc->opt.min_tokens + trim) {
        int stable = tokens - trim;
        if (align > 0) stable -= stable % align;
        if (stable >= kc->opt.min_tokens) return stable;
    }
    return tokens;
}

static int kv_cache_chat_anchor_pos(const kv_disk_cache *kc,
                                    const ds4_tokens *prompt,
                                    int user_token_id,
                                    int assistant_token_id) {
    if (!prompt || user_token_id < 0 || assistant_token_id < 0) return -1;

    /* Cold checkpoints are meant to maximize reuse across independent agent
     * sessions.  The stable part of a rendered chat is everything before the
     * user message that asks the model to do this specific task.  Some clients
     * put stable user-role scaffolding first, for example Codex renders:
     *
     *   <｜User｜><environment_context>...</environment_context><｜User｜>task
     *
     * So use the last user marker before the first assistant marker, not the
     * first one.  Stop at the first assistant marker so multi-turn histories do
     * not turn old conversation content into low-value cold cache files; live
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Trova bug sottili e spiega quali invarianti dell'inferenza possono rompersi.

File: ds4_server.c

```c
0 || memcmp(text, prefix, prefix_len) == 0);
}

static const char *kv_cache_key_kind(uint8_t ext_flags) {
    if (ext_flags & KV_EXT_RESPONSES_VISIBLE) return "responses-visible";
    if (ext_flags & KV_EXT_THINKING_VISIBLE) return "thinking-visible";
    return "token-text";
}

static void tokens_copy_prefix(ds4_tokens *dst, const ds4_tokens *src, int n) {
    dst->len = 0;
    if (n > src->len) n = src->len;
    for (int i = 0; i < n; i++) ds4_tokens_push(dst, src->v[i]);
}

static void tokens_append(ds4_tokens *dst, const ds4_tokens *src) {
    if (!dst || !src) return;
    for (int i = 0; i < src->len; i++) ds4_tokens_push(dst, src->v[i]);
}

static void build_prompt_from_exact_prefix_and_text_suffix(
        ds4_engine *engine,
        const ds4_tokens *exact_prefix,
        const char *suffix_text,
        ds4_tokens *out)
{
    ds4_tokens_copy(out, exact_prefix);

    ds4_tokens suffix = {0};
    /* The suffix may start with DS4 chat markers such as <｜User｜> or
     * </think>, so use the rendered-chat tokenizer, not plain text BPE. */
    ds4_tokenize_rendered_chat(engine, suffix_text ? suffix_text : "", &suffix);
    tokens_append(out, &suffix);
    ds4_tokens_free(&suffix);
}

static int kv_cache_store_len(const kv_disk_cache *kc, int tokens) {
    const int trim = kc->opt.boundary_trim_tokens;
    const int align = kc->opt.boundary_align_tokens;
    if (tokens > kc->opt.min_tokens + trim) {
        int stable = tokens - trim;
        if (align > 0) stable -= stable % align;
        if (stable >= kc->opt.min_tokens) return stable;
    }
    return tokens;
}

static int kv_cache_chat_anchor_pos(const kv_disk_cache *kc,
                                    const ds4_tokens *prompt,
                                    int user_token_id,
                                    int assistant_token_id) {
    if (!prompt || user_token_id < 0 || assistant_token_id < 0) return -1;

    /* Cold checkpoints are meant to maximize reuse across independent agent
     * sessions.  The stable part of a rendered chat is everything before the
     * user message that asks the model to do this specific task.  Some clients
     * put stable user-role scaffolding first, for example Codex renders:
     *
     *   <｜User｜><environment_context>...</environment_context><｜User｜>task
     *
     * So use the last user marker before the first assistant marker, not the
     * first one.  Stop at the first assistant marker so multi-turn histories do
     * not turn old conversation content into low-value cold cache files; live
```<｜Assistant｜></think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Trova bug sottili e spiega quali invarianti dell'inferenza possono rompersi.

File: ds4_server.c

```c
0 || memcmp(text, prefix, prefix_len) == 0);
}

static const char *kv_cache_key_kind(uint8_t ext_flags) {
    if (ext_flags & KV_EXT_RESPONSES_VISIBLE) return "responses-visible";
    if (ext_flags & KV_EXT_THINKING_VISIBLE) return "thinking-visible";
    return "token-text";
}

static void tokens_copy_prefix(ds4_tokens *dst, const ds4_tokens *src, int n) {
    dst->len = 0;
    if (n > src->len) n = src->len;
    for (int i = 0; i < n; i++) ds4_tokens_push(dst, src->v[i]);
}

static void tokens_append(ds4_tokens *dst, const ds4_tokens *src) {
    if (!dst || !src) return;
    for (int i = 0; i < src->len; i++) ds4_tokens_push(dst, src->v[i]);
}

static void build_prompt_from_exact_prefix_and_text_suffix(
        ds4_engine *engine,
        const ds4_tokens *exact_prefix,
        const char *suffix_text,
        ds4_tokens *out)
{
    ds4_tokens_copy(out, exact_prefix);

    ds4_tokens suffix = {0};
    /* The suffix may start with DS4 chat markers such as <｜User｜> or
     * </think>, so use the rendered-chat tokenizer, not plain text BPE. */
    ds4_tokenize_rendered_chat(engine, suffix_text ? suffix_text : "", &suffix);
    tokens_append(out, &suffix);
    ds4_tokens_free(&suffix);
}

static int kv_cache_store_len(const kv_disk_cache *kc, int tokens) {
    const int trim = kc->opt.boundary_trim_tokens;
    const int align = kc->opt.boundary_align_tokens;
    if (tokens > kc->opt.min_tokens + trim) {
        int stable = tokens - trim;
        if (align > 0) stable -= stable % align;
        if (stable >= kc->opt.min_tokens) return stable;
    }
    return tokens;
}

static int kv_cache_chat_anchor_pos(const kv_disk_cache *kc,
                                    const ds4_tokens *prompt,
                                    int user_token_id,
                                    int assistant_token_id) {
    if (!prompt || user_token_id < 0 || assistant_token_id < 0) return -1;

    /* Cold checkpoints are meant to maximize reuse across independent agent
     * sessions.  The stable part of a rendered chat is everything before the
     * user message that asks the model to do this specific task.  Some clients
     * put stable user-role scaffolding first, for example Codex renders:
     *
     *   <｜User｜><environment_context>...</environment_context><｜User｜>task
     *
     * So use the last user marker before the first assistant marker, not the
     * first one.  Stop at the first assistant marker so multi-turn histories do
     * not turn old conversation content into low-value cold cache files; live
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Trova bug sottili e spiega quali invarianti dell'inferenza possono rompersi.

File: ds4_server.c

```c
0 || memcmp(text, prefix, prefix_len) == 0);
}

static const char *kv_cache_key_kind(uint8_t ext_flags) {
    if (ext_flags & KV_EXT_RESPONSES_VISIBLE) return "responses-visible";
    if (ext_flags & KV_EXT_THINKING_VISIBLE) return "thinking-visible";
    return "token-text";
}

static void tokens_copy_prefix(ds4_tokens *dst, const ds4_tokens *src, int n) {
    dst->len = 0;
    if (n > src->len) n = src->len;
    for (int i = 0; i < n; i++) ds4_tokens_push(dst, src->v[i]);
}

static void tokens_append(ds4_tokens *dst, const ds4_tokens *src) {
    if (!dst || !src) return;
    for (int i = 0; i < src->len; i++) ds4_tokens_push(dst, src->v[i]);
}

static void build_prompt_from_exact_prefix_and_text_suffix(
        ds4_engine *engine,
        const ds4_tokens *exact_prefix,
        const char *suffix_text,
        ds4_tokens *out)
{
    ds4_tokens_copy(out, exact_prefix);

    ds4_tokens suffix = {0};
    /* The suffix may start with DS4 chat markers such as <｜User｜> or
     * </think>, so use the rendered-chat tokenizer, not plain text BPE. */
    ds4_tokenize_rendered_chat(engine, suffix_text ? suffix_text : "", &suffix);
    tokens_append(out, &suffix);
    ds4_tokens_free(&suffix);
}

static int kv_cache_store_len(const kv_disk_cache *kc, int tokens) {
    const int trim = kc->opt.boundary_trim_tokens;
    const int align = kc->opt.boundary_align_tokens;
    if (tokens > kc->opt.min_tokens + trim) {
        int stable = tokens - trim;
        if (align > 0) stable -= stable % align;
        if (stable >= kc->opt.min_tokens) return stable;
    }
    return tokens;
}

static int kv_cache_chat_anchor_pos(const kv_disk_cache *kc,
                                    const ds4_tokens *prompt,
                                    int user_token_id,
                                    int assistant_token_id) {
    if (!prompt || user_token_id < 0 || assistant_token_id < 0) return -1;

    /* Cold checkpoints are meant to maximize reuse across independent agent
     * sessions.  The stable part of a rendered chat is everything before the
     * user message that asks the model to do this specific task.  Some clients
     * put stable user-role scaffolding first, for example Codex renders:
     *
     *   <｜User｜><environment_context>...</environment_context><｜User｜>task
     *
     * So use the last user marker before the first assistant marker, not the
     * first one.  Stop at the first assistant marker so multi-turn histories do
     * not turn old conversation content into low-value cold cache files; live
```<｜Assistant｜><think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Review this excerpt for correctness risks, memory lifetime issues, and performance bottlenecks.

File: ds4_server.c

```c
tant marker, not the
     * first one.  Stop at the first assistant marker so multi-turn histories do
     * not turn old conversation content into low-value cold cache files; live
     * and continued checkpoints cover same-session continuation instead.
     *
     * The returned value is a token prefix length and deliberately excludes the
     * selected <｜User｜> token.  This is an exact special-token boundary, so the
     * generic trim/align heuristic is not needed to avoid tokenizer merges. */
    int last_user = -1;
    for (int i = 0; i < prompt->len; i++) {
        const int token = prompt->v[i];
        if (token == assistant_token_id) break;
        if (token == user_token_id) last_user = i;
    }
    return last_user >= kc->opt.min_tokens ? last_user : -1;
}

static int kv_cache_continued_step(const kv_disk_cache *kc) {
    if (!kc->enabled || kc->opt.continued_interval_tokens <= 0) return 0;
    int step = kc->opt.continued_interval_tokens;
    const int align = kc->opt.boundary_align_tokens;
    if (align > 0) {
        step = ((step + align - 1) / align) * align;
        if (step <= 0) step = align;
    }
    return step;
}

static int kv_cache_continued_store_target(const kv_disk_cache *kc, int live_tokens) {
    const int step = kv_cache_continued_step(kc);
    if (step <= 0) return 0;
    if (live_tokens < kc->opt.min_tokens) return 0;

    /* Continued checkpoints cannot roll the live graph backward.  Save only
     * at absolute aligned frontiers, not relative to the last cold/evict file.
     * Otherwise an early cold checkpoint can shift the whole schedule and leave
     * long generations with no recent durable restart point. */
    if (live_tokens % step != 0) return 0;
    if (live_tokens <= kc->continued_last_store_tokens) return 0;
    return live_tokens;
}

/* A same-text-prefix file can be reused by a larger context, but not by a
 * smaller one: the payload was validated against the context capacity recorded
 * in the file.  If the existing file cannot be used by this server, replace it
 * so this context can still populate its own cache. */
static bool kv_cache_file_text_matches(const char *path, const char sha[41],
                                       const char *text, size_t text_len) {
    if (text_len > UINT32_MAX) return false;
    FILE *fp = fopen(path, "rb");
    if (!fp) return false;

    kv_entry hdr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes) &&
              text_bytes == (uint32_t)text_len;
    char *stored = NULL;
    if (ok) {
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Review this excerpt for correctness risks, memory lifetime issues, and performance bottlenecks.

File: ds4_server.c

```c
tant marker, not the
     * first one.  Stop at the first assistant marker so multi-turn histories do
     * not turn old conversation content into low-value cold cache files; live
     * and continued checkpoints cover same-session continuation instead.
     *
     * The returned value is a token prefix length and deliberately excludes the
     * selected <｜User｜> token.  This is an exact special-token boundary, so the
     * generic trim/align heuristic is not needed to avoid tokenizer merges. */
    int last_user = -1;
    for (int i = 0; i < prompt->len; i++) {
        const int token = prompt->v[i];
        if (token == assistant_token_id) break;
        if (token == user_token_id) last_user = i;
    }
    return last_user >= kc->opt.min_tokens ? last_user : -1;
}

static int kv_cache_continued_step(const kv_disk_cache *kc) {
    if (!kc->enabled || kc->opt.continued_interval_tokens <= 0) return 0;
    int step = kc->opt.continued_interval_tokens;
    const int align = kc->opt.boundary_align_tokens;
    if (align > 0) {
        step = ((step + align - 1) / align) * align;
        if (step <= 0) step = align;
    }
    return step;
}

static int kv_cache_continued_store_target(const kv_disk_cache *kc, int live_tokens) {
    const int step = kv_cache_continued_step(kc);
    if (step <= 0) return 0;
    if (live_tokens < kc->opt.min_tokens) return 0;

    /* Continued checkpoints cannot roll the live graph backward.  Save only
     * at absolute aligned frontiers, not relative to the last cold/evict file.
     * Otherwise an early cold checkpoint can shift the whole schedule and leave
     * long generations with no recent durable restart point. */
    if (live_tokens % step != 0) return 0;
    if (live_tokens <= kc->continued_last_store_tokens) return 0;
    return live_tokens;
}

/* A same-text-prefix file can be reused by a larger context, but not by a
 * smaller one: the payload was validated against the context capacity recorded
 * in the file.  If the existing file cannot be used by this server, replace it
 * so this context can still populate its own cache. */
static bool kv_cache_file_text_matches(const char *path, const char sha[41],
                                       const char *text, size_t text_len) {
    if (text_len > UINT32_MAX) return false;
    FILE *fp = fopen(path, "rb");
    if (!fp) return false;

    kv_entry hdr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes) &&
              text_bytes == (uint32_t)text_len;
    char *stored = NULL;
    if (ok) {
```<｜Assistant｜></think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Review this excerpt for correctness risks, memory lifetime issues, and performance bottlenecks.

File: ds4_server.c

```c
tant marker, not the
     * first one.  Stop at the first assistant marker so multi-turn histories do
     * not turn old conversation content into low-value cold cache files; live
     * and continued checkpoints cover same-session continuation instead.
     *
     * The returned value is a token prefix length and deliberately excludes the
     * selected <｜User｜> token.  This is an exact special-token boundary, so the
     * generic trim/align heuristic is not needed to avoid tokenizer merges. */
    int last_user = -1;
    for (int i = 0; i < prompt->len; i++) {
        const int token = prompt->v[i];
        if (token == assistant_token_id) break;
        if (token == user_token_id) last_user = i;
    }
    return last_user >= kc->opt.min_tokens ? last_user : -1;
}

static int kv_cache_continued_step(const kv_disk_cache *kc) {
    if (!kc->enabled || kc->opt.continued_interval_tokens <= 0) return 0;
    int step = kc->opt.continued_interval_tokens;
    const int align = kc->opt.boundary_align_tokens;
    if (align > 0) {
        step = ((step + align - 1) / align) * align;
        if (step <= 0) step = align;
    }
    return step;
}

static int kv_cache_continued_store_target(const kv_disk_cache *kc, int live_tokens) {
    const int step = kv_cache_continued_step(kc);
    if (step <= 0) return 0;
    if (live_tokens < kc->opt.min_tokens) return 0;

    /* Continued checkpoints cannot roll the live graph backward.  Save only
     * at absolute aligned frontiers, not relative to the last cold/evict file.
     * Otherwise an early cold checkpoint can shift the whole schedule and leave
     * long generations with no recent durable restart point. */
    if (live_tokens % step != 0) return 0;
    if (live_tokens <= kc->continued_last_store_tokens) return 0;
    return live_tokens;
}

/* A same-text-prefix file can be reused by a larger context, but not by a
 * smaller one: the payload was validated against the context capacity recorded
 * in the file.  If the existing file cannot be used by this server, replace it
 * so this context can still populate its own cache. */
static bool kv_cache_file_text_matches(const char *path, const char sha[41],
                                       const char *text, size_t text_len) {
    if (text_len > UINT32_MAX) return false;
    FILE *fp = fopen(path, "rb");
    if (!fp) return false;

    kv_entry hdr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes) &&
              text_bytes == (uint32_t)text_len;
    char *stored = NULL;
    if (ok) {
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Review this excerpt for correctness risks, memory lifetime issues, and performance bottlenecks.

File: ds4_server.c

```c
tant marker, not the
     * first one.  Stop at the first assistant marker so multi-turn histories do
     * not turn old conversation content into low-value cold cache files; live
     * and continued checkpoints cover same-session continuation instead.
     *
     * The returned value is a token prefix length and deliberately excludes the
     * selected <｜User｜> token.  This is an exact special-token boundary, so the
     * generic trim/align heuristic is not needed to avoid tokenizer merges. */
    int last_user = -1;
    for (int i = 0; i < prompt->len; i++) {
        const int token = prompt->v[i];
        if (token == assistant_token_id) break;
        if (token == user_token_id) last_user = i;
    }
    return last_user >= kc->opt.min_tokens ? last_user : -1;
}

static int kv_cache_continued_step(const kv_disk_cache *kc) {
    if (!kc->enabled || kc->opt.continued_interval_tokens <= 0) return 0;
    int step = kc->opt.continued_interval_tokens;
    const int align = kc->opt.boundary_align_tokens;
    if (align > 0) {
        step = ((step + align - 1) / align) * align;
        if (step <= 0) step = align;
    }
    return step;
}

static int kv_cache_continued_store_target(const kv_disk_cache *kc, int live_tokens) {
    const int step = kv_cache_continued_step(kc);
    if (step <= 0) return 0;
    if (live_tokens < kc->opt.min_tokens) return 0;

    /* Continued checkpoints cannot roll the live graph backward.  Save only
     * at absolute aligned frontiers, not relative to the last cold/evict file.
     * Otherwise an early cold checkpoint can shift the whole schedule and leave
     * long generations with no recent durable restart point. */
    if (live_tokens % step != 0) return 0;
    if (live_tokens <= kc->continued_last_store_tokens) return 0;
    return live_tokens;
}

/* A same-text-prefix file can be reused by a larger context, but not by a
 * smaller one: the payload was validated against the context capacity recorded
 * in the file.  If the existing file cannot be used by this server, replace it
 * so this context can still populate its own cache. */
static bool kv_cache_file_text_matches(const char *path, const char sha[41],
                                       const char *text, size_t text_len) {
    if (text_len > UINT32_MAX) return false;
    FILE *fp = fopen(path, "rb");
    if (!fp) return false;

    kv_entry hdr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes) &&
              text_bytes == (uint32_t)text_len;
    char *stored = NULL;
    if (ok) {
```<｜Assistant｜><think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Explain what this code does and identify the inference stage it belongs to.

File: ds4_server.c

```c
dr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes) &&
              text_bytes == (uint32_t)text_len;
    char *stored = NULL;
    if (ok) {
        stored = xmalloc((size_t)text_bytes + 1);
        ok = fread(stored, 1, text_bytes, fp) == text_bytes;
    }
    fclose(fp);
    if (!ok) {
        free(stored);
        return false;
    }

    char stored_sha[41];
    sha1_bytes_hex(stored, text_bytes, stored_sha);
    ok = !strcmp(stored_sha, sha) &&
         (text_len == 0 || memcmp(stored, text, text_len) == 0);
    free(stored);
    return ok;
}

static bool kv_cache_file_size_bytes(uint64_t text_bytes,
                                     uint64_t payload_bytes,
                                     uint64_t tool_map_bytes,
                                     uint64_t *file_bytes) {
    const uint64_t fixed = KV_CACHE_FIXED_HEADER + 4ull;
    if (UINT64_MAX - fixed < text_bytes ||
        UINT64_MAX - fixed - text_bytes < payload_bytes ||
        UINT64_MAX - fixed - text_bytes - payload_bytes < tool_map_bytes)
        return false;
    if (file_bytes) *file_bytes = fixed + text_bytes + payload_bytes + tool_map_bytes;
    return true;
}

static bool kv_cache_budget_required(uint64_t file_bytes,
                                     uint64_t *required_bytes) {
    /* The serialized size is deterministic for one snapshot, including the
     * optional tool map.  We still reserve 1% headroom so filesystem/accounting
     * surprises or a concurrently-added tool mapping cannot produce a file that
     * is immediately removed by the cache budget pass. */
    uint64_t slack = file_bytes / 100u;
    if (file_bytes % 100u) slack++;
    if (UINT64_MAX - file_bytes < slack) return false;
    if (required_bytes) *required_bytes = file_bytes + slack;
    return true;
}

static bool kv_cache_file_size_fits(const kv_disk_cache *kc,
                                    uint64_t text_bytes,
                                    uint64_t payload_bytes,
                                    uint64_t tool_map_bytes,
                                    uint64_t *file_bytes_out,
                                    uint64_t *required_bytes_out) {
    uint64_t file_bytes = 0;
    if (!kv_cache_file_size_bytes(text_bytes, payload_bytes, tool_map_bytes,
                                  &file_bytes))
        return false;
    if (file_bytes_out) *file_bytes_out = file_bytes;
    if (!kc || kc->budget_bytes == 0) return true;
    uint64_t required = 0;
    if (!kv_cache_budget_required(file_bytes, &required)) return false;
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Explain what this code does and identify the inference stage it belongs to.

File: ds4_server.c

```c
dr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes) &&
              text_bytes == (uint32_t)text_len;
    char *stored = NULL;
    if (ok) {
        stored = xmalloc((size_t)text_bytes + 1);
        ok = fread(stored, 1, text_bytes, fp) == text_bytes;
    }
    fclose(fp);
    if (!ok) {
        free(stored);
        return false;
    }

    char stored_sha[41];
    sha1_bytes_hex(stored, text_bytes, stored_sha);
    ok = !strcmp(stored_sha, sha) &&
         (text_len == 0 || memcmp(stored, text, text_len) == 0);
    free(stored);
    return ok;
}

static bool kv_cache_file_size_bytes(uint64_t text_bytes,
                                     uint64_t payload_bytes,
                                     uint64_t tool_map_bytes,
                                     uint64_t *file_bytes) {
    const uint64_t fixed = KV_CACHE_FIXED_HEADER + 4ull;
    if (UINT64_MAX - fixed < text_bytes ||
        UINT64_MAX - fixed - text_bytes < payload_bytes ||
        UINT64_MAX - fixed - text_bytes - payload_bytes < tool_map_bytes)
        return false;
    if (file_bytes) *file_bytes = fixed + text_bytes + payload_bytes + tool_map_bytes;
    return true;
}

static bool kv_cache_budget_required(uint64_t file_bytes,
                                     uint64_t *required_bytes) {
    /* The serialized size is deterministic for one snapshot, including the
     * optional tool map.  We still reserve 1% headroom so filesystem/accounting
     * surprises or a concurrently-added tool mapping cannot produce a file that
     * is immediately removed by the cache budget pass. */
    uint64_t slack = file_bytes / 100u;
    if (file_bytes % 100u) slack++;
    if (UINT64_MAX - file_bytes < slack) return false;
    if (required_bytes) *required_bytes = file_bytes + slack;
    return true;
}

static bool kv_cache_file_size_fits(const kv_disk_cache *kc,
                                    uint64_t text_bytes,
                                    uint64_t payload_bytes,
                                    uint64_t tool_map_bytes,
                                    uint64_t *file_bytes_out,
                                    uint64_t *required_bytes_out) {
    uint64_t file_bytes = 0;
    if (!kv_cache_file_size_bytes(text_bytes, payload_bytes, tool_map_bytes,
                                  &file_bytes))
        return false;
    if (file_bytes_out) *file_bytes_out = file_bytes;
    if (!kc || kc->budget_bytes == 0) return true;
    uint64_t required = 0;
    if (!kv_cache_budget_required(file_bytes, &required)) return false;
```<｜Assistant｜></think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Explain what this code does and identify the inference stage it belongs to.

File: ds4_server.c

```c
dr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes) &&
              text_bytes == (uint32_t)text_len;
    char *stored = NULL;
    if (ok) {
        stored = xmalloc((size_t)text_bytes + 1);
        ok = fread(stored, 1, text_bytes, fp) == text_bytes;
    }
    fclose(fp);
    if (!ok) {
        free(stored);
        return false;
    }

    char stored_sha[41];
    sha1_bytes_hex(stored, text_bytes, stored_sha);
    ok = !strcmp(stored_sha, sha) &&
         (text_len == 0 || memcmp(stored, text, text_len) == 0);
    free(stored);
    return ok;
}

static bool kv_cache_file_size_bytes(uint64_t text_bytes,
                                     uint64_t payload_bytes,
                                     uint64_t tool_map_bytes,
                                     uint64_t *file_bytes) {
    const uint64_t fixed = KV_CACHE_FIXED_HEADER + 4ull;
    if (UINT64_MAX - fixed < text_bytes ||
        UINT64_MAX - fixed - text_bytes < payload_bytes ||
        UINT64_MAX - fixed - text_bytes - payload_bytes < tool_map_bytes)
        return false;
    if (file_bytes) *file_bytes = fixed + text_bytes + payload_bytes + tool_map_bytes;
    return true;
}

static bool kv_cache_budget_required(uint64_t file_bytes,
                                     uint64_t *required_bytes) {
    /* The serialized size is deterministic for one snapshot, including the
     * optional tool map.  We still reserve 1% headroom so filesystem/accounting
     * surprises or a concurrently-added tool mapping cannot produce a file that
     * is immediately removed by the cache budget pass. */
    uint64_t slack = file_bytes / 100u;
    if (file_bytes % 100u) slack++;
    if (UINT64_MAX - file_bytes < slack) return false;
    if (required_bytes) *required_bytes = file_bytes + slack;
    return true;
}

static bool kv_cache_file_size_fits(const kv_disk_cache *kc,
                                    uint64_t text_bytes,
                                    uint64_t payload_bytes,
                                    uint64_t tool_map_bytes,
                                    uint64_t *file_bytes_out,
                                    uint64_t *required_bytes_out) {
    uint64_t file_bytes = 0;
    if (!kv_cache_file_size_bytes(text_bytes, payload_bytes, tool_map_bytes,
                                  &file_bytes))
        return false;
    if (file_bytes_out) *file_bytes_out = file_bytes;
    if (!kc || kc->budget_bytes == 0) return true;
    uint64_t required = 0;
    if (!kv_cache_budget_required(file_bytes, &required)) return false;
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Explain what this code does and identify the inference stage it belongs to.

File: ds4_server.c

```c
dr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes) &&
              text_bytes == (uint32_t)text_len;
    char *stored = NULL;
    if (ok) {
        stored = xmalloc((size_t)text_bytes + 1);
        ok = fread(stored, 1, text_bytes, fp) == text_bytes;
    }
    fclose(fp);
    if (!ok) {
        free(stored);
        return false;
    }

    char stored_sha[41];
    sha1_bytes_hex(stored, text_bytes, stored_sha);
    ok = !strcmp(stored_sha, sha) &&
         (text_len == 0 || memcmp(stored, text, text_len) == 0);
    free(stored);
    return ok;
}

static bool kv_cache_file_size_bytes(uint64_t text_bytes,
                                     uint64_t payload_bytes,
                                     uint64_t tool_map_bytes,
                                     uint64_t *file_bytes) {
    const uint64_t fixed = KV_CACHE_FIXED_HEADER + 4ull;
    if (UINT64_MAX - fixed < text_bytes ||
        UINT64_MAX - fixed - text_bytes < payload_bytes ||
        UINT64_MAX - fixed - text_bytes - payload_bytes < tool_map_bytes)
        return false;
    if (file_bytes) *file_bytes = fixed + text_bytes + payload_bytes + tool_map_bytes;
    return true;
}

static bool kv_cache_budget_required(uint64_t file_bytes,
                                     uint64_t *required_bytes) {
    /* The serialized size is deterministic for one snapshot, including the
     * optional tool map.  We still reserve 1% headroom so filesystem/accounting
     * surprises or a concurrently-added tool mapping cannot produce a file that
     * is immediately removed by the cache budget pass. */
    uint64_t slack = file_bytes / 100u;
    if (file_bytes % 100u) slack++;
    if (UINT64_MAX - file_bytes < slack) return false;
    if (required_bytes) *required_bytes = file_bytes + slack;
    return true;
}

static bool kv_cache_file_size_fits(const kv_disk_cache *kc,
                                    uint64_t text_bytes,
                                    uint64_t payload_bytes,
                                    uint64_t tool_map_bytes,
                                    uint64_t *file_bytes_out,
                                    uint64_t *required_bytes_out) {
    uint64_t file_bytes = 0;
    if (!kv_cache_file_size_bytes(text_bytes, payload_bytes, tool_map_bytes,
                                  &file_bytes))
        return false;
    if (file_bytes_out) *file_bytes_out = file_bytes;
    if (!kc || kc->budget_bytes == 0) return true;
    uint64_t required = 0;
    if (!kv_cache_budget_required(file_bytes, &required)) return false;
```<｜Assistant｜><think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
*file_bytes_out = file_bytes;
    if (!kc || kc->budget_bytes == 0) return true;
    uint64_t required = 0;
    if (!kv_cache_budget_required(file_bytes, &required)) return false;
    if (required_bytes_out) *required_bytes_out = required;
    return required <= kc->budget_bytes;
}

static bool kv_cache_existing_compatible(kv_disk_cache *kc, const char *path,
                                         const char sha[41],
                                         const char *text, size_t text_len,
                                         int quant_bits, int ctx_size) {
    if (access(path, F_OK) != 0) return false;
    kv_entry e = {0};
    if (!kv_read_entry_file(path, sha, &e)) return false;
    bool compatible = (!kc->reject_different_quant || e.quant_bits == (uint8_t)quant_bits) &&
                      e.ctx_size <= (uint32_t)ctx_size &&
                      kv_cache_file_text_matches(path, sha, text, text_len);
    kv_entry_free(&e);
    if (!compatible) {
        if (unlink(path) == 0) {
            server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache replaced incompatible file %s", path);
        }
        return false;
    }
    return true;
}

static void kv_cache_rewrite_tool_map(server *s, const char *path, const char *text) {
    if (!s || !path || !text || tool_memory_count_dsml_in_text(s, text) == 0) return;
    FILE *fp = fopen(path, "r+b");
    if (!fp) return;
    kv_entry hdr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes);
    uint64_t end = KV_CACHE_FIXED_HEADER + 4ull + (uint64_t)text_bytes + hdr.payload_bytes;
    if (ok && end <= (uint64_t)INT64_MAX &&
        fseeko(fp, (off_t)end, SEEK_SET) == 0 &&
        ftruncate(fileno(fp), (off_t)end) == 0)
    {
        uint64_t ignored = 0;
        ok = kv_tool_map_write(s, fp, text, &ignored) && fflush(fp) == 0;
        if (ok && ignored > 0) {
            uint8_t h[KV_CACHE_FIXED_HEADER];
            uint64_t now = (uint64_t)time(NULL);
            kv_fill_header(h, hdr.quant_bits, hdr.reason,
                           (uint8_t)(hdr.ext_flags | KV_EXT_TOOL_MAP),
                           hdr.tokens, hdr.hits, hdr.ctx_size,
                           hdr.created_at, now, hdr.payload_bytes);
            ok = fseeko(fp, 0, SEEK_SET) == 0 &&
                 fwrite(h, 1, sizeof(h), fp) == sizeof(h) &&
                 fflush(fp) == 0;
        }
    }
    fclose(fp);
    (void)ok;
}

static bool kv_cache_store_live_prefix_text(server *s, const ds4_tokens *tokens,
                                            int store_len, const char *reason,
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
*file_bytes_out = file_bytes;
    if (!kc || kc->budget_bytes == 0) return true;
    uint64_t required = 0;
    if (!kv_cache_budget_required(file_bytes, &required)) return false;
    if (required_bytes_out) *required_bytes_out = required;
    return required <= kc->budget_bytes;
}

static bool kv_cache_existing_compatible(kv_disk_cache *kc, const char *path,
                                         const char sha[41],
                                         const char *text, size_t text_len,
                                         int quant_bits, int ctx_size) {
    if (access(path, F_OK) != 0) return false;
    kv_entry e = {0};
    if (!kv_read_entry_file(path, sha, &e)) return false;
    bool compatible = (!kc->reject_different_quant || e.quant_bits == (uint8_t)quant_bits) &&
                      e.ctx_size <= (uint32_t)ctx_size &&
                      kv_cache_file_text_matches(path, sha, text, text_len);
    kv_entry_free(&e);
    if (!compatible) {
        if (unlink(path) == 0) {
            server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache replaced incompatible file %s", path);
        }
        return false;
    }
    return true;
}

static void kv_cache_rewrite_tool_map(server *s, const char *path, const char *text) {
    if (!s || !path || !text || tool_memory_count_dsml_in_text(s, text) == 0) return;
    FILE *fp = fopen(path, "r+b");
    if (!fp) return;
    kv_entry hdr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes);
    uint64_t end = KV_CACHE_FIXED_HEADER + 4ull + (uint64_t)text_bytes + hdr.payload_bytes;
    if (ok && end <= (uint64_t)INT64_MAX &&
        fseeko(fp, (off_t)end, SEEK_SET) == 0 &&
        ftruncate(fileno(fp), (off_t)end) == 0)
    {
        uint64_t ignored = 0;
        ok = kv_tool_map_write(s, fp, text, &ignored) && fflush(fp) == 0;
        if (ok && ignored > 0) {
            uint8_t h[KV_CACHE_FIXED_HEADER];
            uint64_t now = (uint64_t)time(NULL);
            kv_fill_header(h, hdr.quant_bits, hdr.reason,
                           (uint8_t)(hdr.ext_flags | KV_EXT_TOOL_MAP),
                           hdr.tokens, hdr.hits, hdr.ctx_size,
                           hdr.created_at, now, hdr.payload_bytes);
            ok = fseeko(fp, 0, SEEK_SET) == 0 &&
                 fwrite(h, 1, sizeof(h), fp) == sizeof(h) &&
                 fflush(fp) == 0;
        }
    }
    fclose(fp);
    (void)ok;
}

static bool kv_cache_store_live_prefix_text(server *s, const ds4_tokens *tokens,
                                            int store_len, const char *reason,
```<｜Assistant｜></think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
*file_bytes_out = file_bytes;
    if (!kc || kc->budget_bytes == 0) return true;
    uint64_t required = 0;
    if (!kv_cache_budget_required(file_bytes, &required)) return false;
    if (required_bytes_out) *required_bytes_out = required;
    return required <= kc->budget_bytes;
}

static bool kv_cache_existing_compatible(kv_disk_cache *kc, const char *path,
                                         const char sha[41],
                                         const char *text, size_t text_len,
                                         int quant_bits, int ctx_size) {
    if (access(path, F_OK) != 0) return false;
    kv_entry e = {0};
    if (!kv_read_entry_file(path, sha, &e)) return false;
    bool compatible = (!kc->reject_different_quant || e.quant_bits == (uint8_t)quant_bits) &&
                      e.ctx_size <= (uint32_t)ctx_size &&
                      kv_cache_file_text_matches(path, sha, text, text_len);
    kv_entry_free(&e);
    if (!compatible) {
        if (unlink(path) == 0) {
            server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache replaced incompatible file %s", path);
        }
        return false;
    }
    return true;
}

static void kv_cache_rewrite_tool_map(server *s, const char *path, const char *text) {
    if (!s || !path || !text || tool_memory_count_dsml_in_text(s, text) == 0) return;
    FILE *fp = fopen(path, "r+b");
    if (!fp) return;
    kv_entry hdr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes);
    uint64_t end = KV_CACHE_FIXED_HEADER + 4ull + (uint64_t)text_bytes + hdr.payload_bytes;
    if (ok && end <= (uint64_t)INT64_MAX &&
        fseeko(fp, (off_t)end, SEEK_SET) == 0 &&
        ftruncate(fileno(fp), (off_t)end) == 0)
    {
        uint64_t ignored = 0;
        ok = kv_tool_map_write(s, fp, text, &ignored) && fflush(fp) == 0;
        if (ok && ignored > 0) {
            uint8_t h[KV_CACHE_FIXED_HEADER];
            uint64_t now = (uint64_t)time(NULL);
            kv_fill_header(h, hdr.quant_bits, hdr.reason,
                           (uint8_t)(hdr.ext_flags | KV_EXT_TOOL_MAP),
                           hdr.tokens, hdr.hits, hdr.ctx_size,
                           hdr.created_at, now, hdr.payload_bytes);
            ok = fseeko(fp, 0, SEEK_SET) == 0 &&
                 fwrite(h, 1, sizeof(h), fp) == sizeof(h) &&
                 fflush(fp) == 0;
        }
    }
    fclose(fp);
    (void)ok;
}

static bool kv_cache_store_live_prefix_text(server *s, const ds4_tokens *tokens,
                                            int store_len, const char *reason,
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
*file_bytes_out = file_bytes;
    if (!kc || kc->budget_bytes == 0) return true;
    uint64_t required = 0;
    if (!kv_cache_budget_required(file_bytes, &required)) return false;
    if (required_bytes_out) *required_bytes_out = required;
    return required <= kc->budget_bytes;
}

static bool kv_cache_existing_compatible(kv_disk_cache *kc, const char *path,
                                         const char sha[41],
                                         const char *text, size_t text_len,
                                         int quant_bits, int ctx_size) {
    if (access(path, F_OK) != 0) return false;
    kv_entry e = {0};
    if (!kv_read_entry_file(path, sha, &e)) return false;
    bool compatible = (!kc->reject_different_quant || e.quant_bits == (uint8_t)quant_bits) &&
                      e.ctx_size <= (uint32_t)ctx_size &&
                      kv_cache_file_text_matches(path, sha, text, text_len);
    kv_entry_free(&e);
    if (!compatible) {
        if (unlink(path) == 0) {
            server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache replaced incompatible file %s", path);
        }
        return false;
    }
    return true;
}

static void kv_cache_rewrite_tool_map(server *s, const char *path, const char *text) {
    if (!s || !path || !text || tool_memory_count_dsml_in_text(s, text) == 0) return;
    FILE *fp = fopen(path, "r+b");
    if (!fp) return;
    kv_entry hdr = {0};
    uint32_t text_bytes = 0;
    bool ok = kv_read_header(fp, &hdr, &text_bytes);
    uint64_t end = KV_CACHE_FIXED_HEADER + 4ull + (uint64_t)text_bytes + hdr.payload_bytes;
    if (ok && end <= (uint64_t)INT64_MAX &&
        fseeko(fp, (off_t)end, SEEK_SET) == 0 &&
        ftruncate(fileno(fp), (off_t)end) == 0)
    {
        uint64_t ignored = 0;
        ok = kv_tool_map_write(s, fp, text, &ignored) && fflush(fp) == 0;
        if (ok && ignored > 0) {
            uint8_t h[KV_CACHE_FIXED_HEADER];
            uint64_t now = (uint64_t)time(NULL);
            kv_fill_header(h, hdr.quant_bits, hdr.reason,
                           (uint8_t)(hdr.ext_flags | KV_EXT_TOOL_MAP),
                           hdr.tokens, hdr.hits, hdr.ctx_size,
                           hdr.created_at, now, hdr.payload_bytes);
            ok = fseeko(fp, 0, SEEK_SET) == 0 &&
                 fwrite(h, 1, sizeof(h), fp) == sizeof(h) &&
                 fflush(fp) == 0;
        }
    }
    fclose(fp);
    (void)ok;
}

static bool kv_cache_store_live_prefix_text(server *s, const ds4_tokens *tokens,
                                            int store_len, const char *reason,
```<｜Assistant｜><think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Trova bug sottili e spiega quali invarianti dell'inferenza possono rompersi.

File: ds4_server.c

```c
p);
    (void)ok;
}

static bool kv_cache_store_live_prefix_text(server *s, const ds4_tokens *tokens,
                                            int store_len, const char *reason,
                                            const char *cache_text_override,
                                            uint8_t cache_text_ext,
                                            const char *cache_text_key) {
    kv_disk_cache *kc = &s->kv;
    if (!kc->enabled) return false;
    if (!tokens || store_len < kc->opt.min_tokens) return false;
    const int original_len = tokens->len;

    ds4_tokens store_tokens = {0};
    tokens_copy_prefix(&store_tokens, tokens, store_len);

    const int quant_bits = ds4_engine_routed_quant_bits(s->engine);
    if (quant_bits != 2 && quant_bits != 4) {
        ds4_tokens_free(&store_tokens);
        return false;
    }
    char err[160] = {0};
    /* Disk cache persistence must observe the graph exactly as-is.  If callers
     * want a shorter prefix, they first prefill to that prefix and only then call
     * this function.  This keeps cache population from doing hidden inference. */
    const ds4_tokens *live_tokens = ds4_session_tokens(s->session);
    if (!live_tokens ||
        live_tokens->len != store_tokens.len ||
        !ds4_tokens_starts_with(live_tokens, &store_tokens))
    {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because live checkpoint is at %d",
                   store_tokens.len,
                   reason,
                   live_tokens ? live_tokens->len : -1);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    uint64_t payload_bytes = ds4_session_payload_bytes(s->session);
    if (payload_bytes == 0) {
        ds4_tokens_free(&store_tokens);
        return false;
    }

    size_t text_len = 0;
    char *text = NULL;
    const bool text_override = cache_text_override && cache_text_override[0];
    if (text_override) {
        text = xstrdup(cache_text_override);
        text_len = strlen(text);
    } else {
        text = render_tokens_text(s->engine, &store_tokens, &text_len);
    }
    if (text_len > UINT32_MAX) {
        server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache skipped tokens=%d because rendered text is too large", store_tokens.len);
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }
    uint64_t tool_map_est_bytes = 0;
    if (!kv_tool_map_serialized_size(s, text, &tool_map_est_bytes)) {
        server_log(DS4_LOG_KVCACHE,
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Trova bug sottili e spiega quali invarianti dell'inferenza possono rompersi.

File: ds4_server.c

```c
p);
    (void)ok;
}

static bool kv_cache_store_live_prefix_text(server *s, const ds4_tokens *tokens,
                                            int store_len, const char *reason,
                                            const char *cache_text_override,
                                            uint8_t cache_text_ext,
                                            const char *cache_text_key) {
    kv_disk_cache *kc = &s->kv;
    if (!kc->enabled) return false;
    if (!tokens || store_len < kc->opt.min_tokens) return false;
    const int original_len = tokens->len;

    ds4_tokens store_tokens = {0};
    tokens_copy_prefix(&store_tokens, tokens, store_len);

    const int quant_bits = ds4_engine_routed_quant_bits(s->engine);
    if (quant_bits != 2 && quant_bits != 4) {
        ds4_tokens_free(&store_tokens);
        return false;
    }
    char err[160] = {0};
    /* Disk cache persistence must observe the graph exactly as-is.  If callers
     * want a shorter prefix, they first prefill to that prefix and only then call
     * this function.  This keeps cache population from doing hidden inference. */
    const ds4_tokens *live_tokens = ds4_session_tokens(s->session);
    if (!live_tokens ||
        live_tokens->len != store_tokens.len ||
        !ds4_tokens_starts_with(live_tokens, &store_tokens))
    {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because live checkpoint is at %d",
                   store_tokens.len,
                   reason,
                   live_tokens ? live_tokens->len : -1);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    uint64_t payload_bytes = ds4_session_payload_bytes(s->session);
    if (payload_bytes == 0) {
        ds4_tokens_free(&store_tokens);
        return false;
    }

    size_t text_len = 0;
    char *text = NULL;
    const bool text_override = cache_text_override && cache_text_override[0];
    if (text_override) {
        text = xstrdup(cache_text_override);
        text_len = strlen(text);
    } else {
        text = render_tokens_text(s->engine, &store_tokens, &text_len);
    }
    if (text_len > UINT32_MAX) {
        server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache skipped tokens=%d because rendered text is too large", store_tokens.len);
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }
    uint64_t tool_map_est_bytes = 0;
    if (!kv_tool_map_serialized_size(s, text, &tool_map_est_bytes)) {
        server_log(DS4_LOG_KVCACHE,
```<｜Assistant｜></think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Trova bug sottili e spiega quali invarianti dell'inferenza possono rompersi.

File: ds4_server.c

```c
p);
    (void)ok;
}

static bool kv_cache_store_live_prefix_text(server *s, const ds4_tokens *tokens,
                                            int store_len, const char *reason,
                                            const char *cache_text_override,
                                            uint8_t cache_text_ext,
                                            const char *cache_text_key) {
    kv_disk_cache *kc = &s->kv;
    if (!kc->enabled) return false;
    if (!tokens || store_len < kc->opt.min_tokens) return false;
    const int original_len = tokens->len;

    ds4_tokens store_tokens = {0};
    tokens_copy_prefix(&store_tokens, tokens, store_len);

    const int quant_bits = ds4_engine_routed_quant_bits(s->engine);
    if (quant_bits != 2 && quant_bits != 4) {
        ds4_tokens_free(&store_tokens);
        return false;
    }
    char err[160] = {0};
    /* Disk cache persistence must observe the graph exactly as-is.  If callers
     * want a shorter prefix, they first prefill to that prefix and only then call
     * this function.  This keeps cache population from doing hidden inference. */
    const ds4_tokens *live_tokens = ds4_session_tokens(s->session);
    if (!live_tokens ||
        live_tokens->len != store_tokens.len ||
        !ds4_tokens_starts_with(live_tokens, &store_tokens))
    {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because live checkpoint is at %d",
                   store_tokens.len,
                   reason,
                   live_tokens ? live_tokens->len : -1);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    uint64_t payload_bytes = ds4_session_payload_bytes(s->session);
    if (payload_bytes == 0) {
        ds4_tokens_free(&store_tokens);
        return false;
    }

    size_t text_len = 0;
    char *text = NULL;
    const bool text_override = cache_text_override && cache_text_override[0];
    if (text_override) {
        text = xstrdup(cache_text_override);
        text_len = strlen(text);
    } else {
        text = render_tokens_text(s->engine, &store_tokens, &text_len);
    }
    if (text_len > UINT32_MAX) {
        server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache skipped tokens=%d because rendered text is too large", store_tokens.len);
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }
    uint64_t tool_map_est_bytes = 0;
    if (!kv_tool_map_serialized_size(s, text, &tool_map_est_bytes)) {
        server_log(DS4_LOG_KVCACHE,
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Trova bug sottili e spiega quali invarianti dell'inferenza possono rompersi.

File: ds4_server.c

```c
p);
    (void)ok;
}

static bool kv_cache_store_live_prefix_text(server *s, const ds4_tokens *tokens,
                                            int store_len, const char *reason,
                                            const char *cache_text_override,
                                            uint8_t cache_text_ext,
                                            const char *cache_text_key) {
    kv_disk_cache *kc = &s->kv;
    if (!kc->enabled) return false;
    if (!tokens || store_len < kc->opt.min_tokens) return false;
    const int original_len = tokens->len;

    ds4_tokens store_tokens = {0};
    tokens_copy_prefix(&store_tokens, tokens, store_len);

    const int quant_bits = ds4_engine_routed_quant_bits(s->engine);
    if (quant_bits != 2 && quant_bits != 4) {
        ds4_tokens_free(&store_tokens);
        return false;
    }
    char err[160] = {0};
    /* Disk cache persistence must observe the graph exactly as-is.  If callers
     * want a shorter prefix, they first prefill to that prefix and only then call
     * this function.  This keeps cache population from doing hidden inference. */
    const ds4_tokens *live_tokens = ds4_session_tokens(s->session);
    if (!live_tokens ||
        live_tokens->len != store_tokens.len ||
        !ds4_tokens_starts_with(live_tokens, &store_tokens))
    {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because live checkpoint is at %d",
                   store_tokens.len,
                   reason,
                   live_tokens ? live_tokens->len : -1);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    uint64_t payload_bytes = ds4_session_payload_bytes(s->session);
    if (payload_bytes == 0) {
        ds4_tokens_free(&store_tokens);
        return false;
    }

    size_t text_len = 0;
    char *text = NULL;
    const bool text_override = cache_text_override && cache_text_override[0];
    if (text_override) {
        text = xstrdup(cache_text_override);
        text_len = strlen(text);
    } else {
        text = render_tokens_text(s->engine, &store_tokens, &text_len);
    }
    if (text_len > UINT32_MAX) {
        server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache skipped tokens=%d because rendered text is too large", store_tokens.len);
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }
    uint64_t tool_map_est_bytes = 0;
    if (!kv_tool_map_serialized_size(s, text, &tool_map_est_bytes)) {
        server_log(DS4_LOG_KVCACHE,
```<｜Assistant｜><think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
done;
        json_ws(&p);
        if (*p != ':') {
            free(key);
            goto done;
        }
        p++;
        if (!strcmp(key, "type")) {
            free(type);
            if (!json_string(&p, &type)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "description")) {
            free(description);
            if (!json_string(&p, &description)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "parameters")) {
            free(parameters);
            if (!json_raw_value(&p, &parameters)) {
                free(key);
                goto done;
            }
        } else if (!json_skip_value(&p)) {
            free(key);
            goto done;
        }
        free(key);
        json_ws(&p);
        if (*p == ',') p++;
        json_ws(&p);
    }

    if (type && !strcmp(type, "tool_search")) {
        buf b = {0};
        buf_puts(&b, "{\"name\":\"tool_search\",\"description\":");
        json_escape(&b, description ? description : "Search available tools.");
        buf_puts(&b, ",\"parameters\":");
        buf_puts(&b, parameters ? parameters :
                 "{\"type\":\"object\",\"properties\":{}}");
        buf_putc(&b, '}');
        out = buf_take(&b);
    }

done:
    free(type);
    free(description);
    free(parameters);
    return out;
}

static char *responses_namespace_function_schema_from_tool(const char *raw,
                                                           const char *namespace,
                                                           char **wire_name) {
    const char *p = raw;
    json_ws(&p);
    if (*p != '{') return NULL;
    p++;

    char *type = NULL;
    char *name = NULL;
    char *description = NULL;
    char *parameters = NULL;
    char *out = NULL;

    json_ws(&p);
    while (*p && *p != '}') {
        char *key = NULL;
        if (!json_string(&p, &key)) goto done;
        json_ws(&p);
        if (*p != ':') {
            free(key);
            goto done;
        }
        p++;
        if (!strcmp(key, "type")) {
            free(type);
            if (!json_string(&p, &type)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "name")) {
            free(name);
            if (!json_string(&p, &name)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "description")) {
            free(description);
            if (!json_string(&p, &description)) {
                free(key);
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
done;
        json_ws(&p);
        if (*p != ':') {
            free(key);
            goto done;
        }
        p++;
        if (!strcmp(key, "type")) {
            free(type);
            if (!json_string(&p, &type)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "description")) {
            free(description);
            if (!json_string(&p, &description)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "parameters")) {
            free(parameters);
            if (!json_raw_value(&p, &parameters)) {
                free(key);
                goto done;
            }
        } else if (!json_skip_value(&p)) {
            free(key);
            goto done;
        }
        free(key);
        json_ws(&p);
        if (*p == ',') p++;
        json_ws(&p);
    }

    if (type && !strcmp(type, "tool_search")) {
        buf b = {0};
        buf_puts(&b, "{\"name\":\"tool_search\",\"description\":");
        json_escape(&b, description ? description : "Search available tools.");
        buf_puts(&b, ",\"parameters\":");
        buf_puts(&b, parameters ? parameters :
                 "{\"type\":\"object\",\"properties\":{}}");
        buf_putc(&b, '}');
        out = buf_take(&b);
    }

done:
    free(type);
    free(description);
    free(parameters);
    return out;
}

static char *responses_namespace_function_schema_from_tool(const char *raw,
                                                           const char *namespace,
                                                           char **wire_name) {
    const char *p = raw;
    json_ws(&p);
    if (*p != '{') return NULL;
    p++;

    char *type = NULL;
    char *name = NULL;
    char *description = NULL;
    char *parameters = NULL;
    char *out = NULL;

    json_ws(&p);
    while (*p && *p != '}') {
        char *key = NULL;
        if (!json_string(&p, &key)) goto done;
        json_ws(&p);
        if (*p != ':') {
            free(key);
            goto done;
        }
        p++;
        if (!strcmp(key, "type")) {
            free(type);
            if (!json_string(&p, &type)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "name")) {
            free(name);
            if (!json_string(&p, &name)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "description")) {
            free(description);
            if (!json_string(&p, &description)) {
                free(key);
```<｜Assistant｜></think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
done;
        json_ws(&p);
        if (*p != ':') {
            free(key);
            goto done;
        }
        p++;
        if (!strcmp(key, "type")) {
            free(type);
            if (!json_string(&p, &type)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "description")) {
            free(description);
            if (!json_string(&p, &description)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "parameters")) {
            free(parameters);
            if (!json_raw_value(&p, &parameters)) {
                free(key);
                goto done;
            }
        } else if (!json_skip_value(&p)) {
            free(key);
            goto done;
        }
        free(key);
        json_ws(&p);
        if (*p == ',') p++;
        json_ws(&p);
    }

    if (type && !strcmp(type, "tool_search")) {
        buf b = {0};
        buf_puts(&b, "{\"name\":\"tool_search\",\"description\":");
        json_escape(&b, description ? description : "Search available tools.");
        buf_puts(&b, ",\"parameters\":");
        buf_puts(&b, parameters ? parameters :
                 "{\"type\":\"object\",\"properties\":{}}");
        buf_putc(&b, '}');
        out = buf_take(&b);
    }

done:
    free(type);
    free(description);
    free(parameters);
    return out;
}

static char *responses_namespace_function_schema_from_tool(const char *raw,
                                                           const char *namespace,
                                                           char **wire_name) {
    const char *p = raw;
    json_ws(&p);
    if (*p != '{') return NULL;
    p++;

    char *type = NULL;
    char *name = NULL;
    char *description = NULL;
    char *parameters = NULL;
    char *out = NULL;

    json_ws(&p);
    while (*p && *p != '}') {
        char *key = NULL;
        if (!json_string(&p, &key)) goto done;
        json_ws(&p);
        if (*p != ':') {
            free(key);
            goto done;
        }
        p++;
        if (!strcmp(key, "type")) {
            free(type);
            if (!json_string(&p, &type)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "name")) {
            free(name);
            if (!json_string(&p, &name)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "description")) {
            free(description);
            if (!json_string(&p, &description)) {
                free(key);
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Suggest a minimal correctness-preserving optimization for this excerpt.

File: ds4_server.c

```c
done;
        json_ws(&p);
        if (*p != ':') {
            free(key);
            goto done;
        }
        p++;
        if (!strcmp(key, "type")) {
            free(type);
            if (!json_string(&p, &type)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "description")) {
            free(description);
            if (!json_string(&p, &description)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "parameters")) {
            free(parameters);
            if (!json_raw_value(&p, &parameters)) {
                free(key);
                goto done;
            }
        } else if (!json_skip_value(&p)) {
            free(key);
            goto done;
        }
        free(key);
        json_ws(&p);
        if (*p == ',') p++;
        json_ws(&p);
    }

    if (type && !strcmp(type, "tool_search")) {
        buf b = {0};
        buf_puts(&b, "{\"name\":\"tool_search\",\"description\":");
        json_escape(&b, description ? description : "Search available tools.");
        buf_puts(&b, ",\"parameters\":");
        buf_puts(&b, parameters ? parameters :
                 "{\"type\":\"object\",\"properties\":{}}");
        buf_putc(&b, '}');
        out = buf_take(&b);
    }

done:
    free(type);
    free(description);
    free(parameters);
    return out;
}

static char *responses_namespace_function_schema_from_tool(const char *raw,
                                                           const char *namespace,
                                                           char **wire_name) {
    const char *p = raw;
    json_ws(&p);
    if (*p != '{') return NULL;
    p++;

    char *type = NULL;
    char *name = NULL;
    char *description = NULL;
    char *parameters = NULL;
    char *out = NULL;

    json_ws(&p);
    while (*p && *p != '}') {
        char *key = NULL;
        if (!json_string(&p, &key)) goto done;
        json_ws(&p);
        if (*p != ':') {
            free(key);
            goto done;
        }
        p++;
        if (!strcmp(key, "type")) {
            free(type);
            if (!json_string(&p, &type)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "name")) {
            free(name);
            if (!json_string(&p, &name)) {
                free(key);
                goto done;
            }
        } else if (!strcmp(key, "description")) {
            free(description);
            if (!json_string(&p, &description)) {
                free(key);
```<｜Assistant｜><think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Review this excerpt for correctness risks, memory lifetime issues, and performance bottlenecks.

File: ds4_server.c

```c
_tokens);
        return false;
    }
    uint64_t tool_map_est_bytes = 0;
    if (!kv_tool_map_serialized_size(s, text, &tool_map_est_bytes)) {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because tool map size overflowed",
                   store_tokens.len, reason);
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }
    uint64_t est_file_bytes = 0, est_required_bytes = 0;
    if (!kv_cache_file_size_fits(kc, (uint64_t)text_len, payload_bytes,
                                 tool_map_est_bytes,
                                 &est_file_bytes, &est_required_bytes)) {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because estimated file size %.2f MiB (%.2f MiB with safety) exceeds budget %.2f MiB",
                   store_tokens.len,
                   reason,
                   (double)est_file_bytes / (1024.0 * 1024.0),
                   (double)est_required_bytes / (1024.0 * 1024.0),
                   (double)kc->budget_bytes / (1024.0 * 1024.0));
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    char sha[41];
    sha1_bytes_hex(text, text_len, sha);
    char *path = kv_path_for_sha(kc, sha);

    if (kv_cache_existing_compatible(kc, path, sha, text, text_len,
                                     quant_bits, ds4_session_ctx(s->session))) {
        kv_cache_rewrite_tool_map(s, path, text);
        free(text);
        free(path);
        ds4_tokens_free(&store_tokens);
        return true;
    }

    buf tmpb = {0};
    buf_printf(&tmpb, "%s.tmp.%ld", path, (long)getpid());
    char *tmp = buf_take(&tmpb);
    const double save_t0 = now_sec();
    FILE *fp = fopen(tmp, "wb");
    if (!fp) {
        server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache failed to create %s: %s save=%.1f ms",
                   tmp, strerror(errno), (now_sec() - save_t0) * 1000.0);
        free(tmp);
        free(text);
        free(path);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    const uint64_t now = (uint64_t)time(NULL);
    uint8_t h[KV_CACHE_FIXED_HEADER];
    uint8_t ext_flags = tool_map_est_bytes > 0 ? KV_EXT_TOOL_MAP : 0;
    if (text_override) ext_flags |= cache_text_ext;
    kv_fill_header(h, (uint8_t)quant_bits, kv_reason_code(reason), ext_flags,
                   (uint32_t)store_tokens.len, 0,
                   (uint32_t)ds4_session_ctx(s->session), now, now, payload_bytes);
    uint8_t tb[4];
    le_put32(tb, (uint32_t)text_len);
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Review this excerpt for correctness risks, memory lifetime issues, and performance bottlenecks.

File: ds4_server.c

```c
_tokens);
        return false;
    }
    uint64_t tool_map_est_bytes = 0;
    if (!kv_tool_map_serialized_size(s, text, &tool_map_est_bytes)) {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because tool map size overflowed",
                   store_tokens.len, reason);
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }
    uint64_t est_file_bytes = 0, est_required_bytes = 0;
    if (!kv_cache_file_size_fits(kc, (uint64_t)text_len, payload_bytes,
                                 tool_map_est_bytes,
                                 &est_file_bytes, &est_required_bytes)) {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because estimated file size %.2f MiB (%.2f MiB with safety) exceeds budget %.2f MiB",
                   store_tokens.len,
                   reason,
                   (double)est_file_bytes / (1024.0 * 1024.0),
                   (double)est_required_bytes / (1024.0 * 1024.0),
                   (double)kc->budget_bytes / (1024.0 * 1024.0));
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    char sha[41];
    sha1_bytes_hex(text, text_len, sha);
    char *path = kv_path_for_sha(kc, sha);

    if (kv_cache_existing_compatible(kc, path, sha, text, text_len,
                                     quant_bits, ds4_session_ctx(s->session))) {
        kv_cache_rewrite_tool_map(s, path, text);
        free(text);
        free(path);
        ds4_tokens_free(&store_tokens);
        return true;
    }

    buf tmpb = {0};
    buf_printf(&tmpb, "%s.tmp.%ld", path, (long)getpid());
    char *tmp = buf_take(&tmpb);
    const double save_t0 = now_sec();
    FILE *fp = fopen(tmp, "wb");
    if (!fp) {
        server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache failed to create %s: %s save=%.1f ms",
                   tmp, strerror(errno), (now_sec() - save_t0) * 1000.0);
        free(tmp);
        free(text);
        free(path);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    const uint64_t now = (uint64_t)time(NULL);
    uint8_t h[KV_CACHE_FIXED_HEADER];
    uint8_t ext_flags = tool_map_est_bytes > 0 ? KV_EXT_TOOL_MAP : 0;
    if (text_override) ext_flags |= cache_text_ext;
    kv_fill_header(h, (uint8_t)quant_bits, kv_reason_code(reason), ext_flags,
                   (uint32_t)store_tokens.len, 0,
                   (uint32_t)ds4_session_ctx(s->session), now, now, payload_bytes);
    uint8_t tb[4];
    le_put32(tb, (uint32_t)text_len);
```<｜Assistant｜></think>


[System] You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.

[User]
Review this excerpt for correctness risks, memory lifetime issues, and performance bottlenecks.

File: ds4_server.c

```c
_tokens);
        return false;
    }
    uint64_t tool_map_est_bytes = 0;
    if (!kv_tool_map_serialized_size(s, text, &tool_map_est_bytes)) {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because tool map size overflowed",
                   store_tokens.len, reason);
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }
    uint64_t est_file_bytes = 0, est_required_bytes = 0;
    if (!kv_cache_file_size_fits(kc, (uint64_t)text_len, payload_bytes,
                                 tool_map_est_bytes,
                                 &est_file_bytes, &est_required_bytes)) {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because estimated file size %.2f MiB (%.2f MiB with safety) exceeds budget %.2f MiB",
                   store_tokens.len,
                   reason,
                   (double)est_file_bytes / (1024.0 * 1024.0),
                   (double)est_required_bytes / (1024.0 * 1024.0),
                   (double)kc->budget_bytes / (1024.0 * 1024.0));
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    char sha[41];
    sha1_bytes_hex(text, text_len, sha);
    char *path = kv_path_for_sha(kc, sha);

    if (kv_cache_existing_compatible(kc, path, sha, text, text_len,
                                     quant_bits, ds4_session_ctx(s->session))) {
        kv_cache_rewrite_tool_map(s, path, text);
        free(text);
        free(path);
        ds4_tokens_free(&store_tokens);
        return true;
    }

    buf tmpb = {0};
    buf_printf(&tmpb, "%s.tmp.%ld", path, (long)getpid());
    char *tmp = buf_take(&tmpb);
    const double save_t0 = now_sec();
    FILE *fp = fopen(tmp, "wb");
    if (!fp) {
        server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache failed to create %s: %s save=%.1f ms",
                   tmp, strerror(errno), (now_sec() - save_t0) * 1000.0);
        free(tmp);
        free(text);
        free(path);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    const uint64_t now = (uint64_t)time(NULL);
    uint8_t h[KV_CACHE_FIXED_HEADER];
    uint8_t ext_flags = tool_map_est_bytes > 0 ? KV_EXT_TOOL_MAP : 0;
    if (text_override) ext_flags |= cache_text_ext;
    kv_fill_header(h, (uint8_t)quant_bits, kv_reason_code(reason), ext_flags,
                   (uint32_t)store_tokens.len, 0,
                   (uint32_t)ds4_session_ctx(s->session), now, now, payload_bytes);
    uint8_t tb[4];
    le_put32(tb, (uint32_t)text_len);
```

<｜begin▁of▁sentence｜>You are DeepSeek V4 Flash running locally. Answer accurately, preserve technical details, and use tools only when the prompt asks for tool use.<｜User｜>Review this excerpt for correctness risks, memory lifetime issues, and performance bottlenecks.

File: ds4_server.c

```c
_tokens);
        return false;
    }
    uint64_t tool_map_est_bytes = 0;
    if (!kv_tool_map_serialized_size(s, text, &tool_map_est_bytes)) {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because tool map size overflowed",
                   store_tokens.len, reason);
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }
    uint64_t est_file_bytes = 0, est_required_bytes = 0;
    if (!kv_cache_file_size_fits(kc, (uint64_t)text_len, payload_bytes,
                                 tool_map_est_bytes,
                                 &est_file_bytes, &est_required_bytes)) {
        server_log(DS4_LOG_KVCACHE,
                   "ds4-server: kv cache skipped tokens=%d reason=%s because estimated file size %.2f MiB (%.2f MiB with safety) exceeds budget %.2f MiB",
                   store_tokens.len,
                   reason,
                   (double)est_file_bytes / (1024.0 * 1024.0),
                   (double)est_required_bytes / (1024.0 * 1024.0),
                   (double)kc->budget_bytes / (1024.0 * 1024.0));
        free(text);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    char sha[41];
    sha1_bytes_hex(text, text_len, sha);
    char *path = kv_path_for_sha(kc, sha);

    if (kv_cache_existing_compatible(kc, path, sha, text, text_len,
                                     quant_bits, ds4_session_ctx(s->session))) {
        kv_cache_rewrite_tool_map(s, path, text);
        free(text);
        free(path);
        ds4_tokens_free(&store_tokens);
        return true;
    }

    buf tmpb = {0};
    buf_printf(&tmpb, "%s.tmp.%ld", path, (long)getpid());
    char *tmp = buf_take(&tmpb);
    const double save_t0 = now_sec();
    FILE *fp = fopen(tmp, "wb");
    if (!fp) {
        server_log(DS4_LOG_KVCACHE, "ds4-server: kv cache failed to create %s: %s save=%.1f ms",
                   tmp, strerror(errno), (now_sec() - save_t0) * 1000.0);
        free(tmp);
        free(text);
        free(path);
        ds4_tokens_free(&store_tokens);
        return false;
    }

    const uint64_t now = (uint64_t)time(NULL);
    uint8_t h[KV_CACHE_FIXED_HEADER];
    uint8_t ext_flags = tool_map_est_bytes > 0 ? KV_EXT_TOOL_MAP : 0;
    if (text_override) ext_flags |= cache_text_ext;
    kv_fill_header(h, (uint8_t)quant_bits, kv_reason_code(reason), ext_flags,
                   (uint32_t)store_tokens.len, 0,
                   (uint32_t)ds4_session_ctx(s->session), now, now, payload_bytes);
    uint8_t tb[4];
    le_put32(tb, (uint32_t)text_len);
```<｜Assistant｜><think>