| 367 | if (nlen < 4 || sname.compare(nlen - 4, 4, ".dkv") != 0) continue; |
| 368 | |
| 369 | // Read the header to get the layout_id. |
| 370 | std::string fpath = se.path().string(); |
| 371 | FILE * f = std::fopen(fpath.c_str(), "rb"); |
| 372 | if (!f) continue; |
| 373 | |
| 374 | DiskCacheHeader hdr{}; |
| 375 | if (read_header(f, hdr) && std::memcmp(hdr.magic, "DKVC", 4) == 0) { |
| 376 | std::memcpy(layout_id_.data(), hdr.layout_id, 16); |
| 377 | layout_known_ = true; |
| 378 | layout_from_disk_ = true; // unverified — must be confirmed by learn_layout() |
| 379 | layout_dir_ = subdir; |
| 380 | std::fclose(f); |
| 381 | scan_directory(); |
| 382 | return; |
| 383 | } |
| 384 | std::fclose(f); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // ─── Lookup ───────────────────────────────────────────────────────────── |
| 390 | |
| 391 | bool DiskPrefixCache::lookup(const std::vector<int32_t> & prompt_ids, int slot) { |
| 392 | if (disabled() || !layout_known_) return false; |
| 393 | |
| 394 | PrefixHash hash = hash_prefix(prompt_ids.data(), (int)prompt_ids.size()); |
| 395 | |
| 396 | std::lock_guard<std::mutex> lock(mu_); |
| 397 | int idx = find_entry(hash); |
| 398 | if (idx < 0) return false; |
| 399 | |
| 400 | auto & entry = entries_[idx]; |
| 401 | if (!read_file(entry.path, slot)) { |
| 402 | // File is corrupt or incompatible — remove it. |
| 403 | std::fprintf(stderr, "[disk-cache] lookup failed, removing %s\n", |
| 404 | entry.path.c_str()); |
| 405 | std::remove(entry.path.c_str()); |
| 406 | total_bytes_ -= entry.file_size; |
| 407 | entries_.erase(entries_.begin() + idx); |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | if (layout_from_disk_) { |
| 412 | const std::array<uint8_t, 16> disk_id = layout_id_; |
| 413 | auto ref = backend_.snapshot_ref(slot); |
| 414 | if (!ref.ctx) { |
| 415 | backend_.snapshot_free(slot); |
| 416 | return false; |
| 417 | } |
| 418 | compute_layout_id(ref.ctx); |
| 419 | if (std::memcmp(disk_id.data(), layout_id_.data(), 16) != 0) { |
| 420 | std::fprintf(stderr, |
| 421 | "[disk-cache] adopted layout mismatch: disk=%s model=%s\n", |
| 422 | hex(disk_id.data(), 16).c_str(), |
| 423 | hex(layout_id_.data(), 16).c_str()); |
nothing calls this directly
no test coverage detected