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