Cortex icon indicating copy to clipboard operation
Cortex copied to clipboard

Medium: switching GGUF model directories can reuse stale metadata and file paths from the prior root

Open dovvnloading opened this issue 3 weeks ago • 0 comments

Impact

Medium / incorrect model metadata — switching the configured GGUF directory can reuse stale cached metadata and a stale absolute file path from the previous directory when both roots contain a same-named file with the same size and preserved mtime. The inventory then describes file A even though inference resolves and loads file B.

Evidence

  • model_directory.py:212–218: one long-lived GGUFModelDirectory instance holds _cache across scans.
  • model_directory.py:234–252: its cache key is only the relative filename, and its fingerprint is only file size and mtime_ns. Changing root without changing either reuses the previous InstalledModel.
  • model_directory.py:334–377: InstalledModel retains path=str(path), architecture, context length, and quantization metadata from whichever root produced it.
  • app_factory.py:115–143: the directory is a dynamic settings getter, but the same gguf_model_directory instance remains in use across changes.
  • Existing test_scan_cache_is_invalidated_by_mtime_and_size tests changes within one directory; it doesn't test changing the directory while both files have the same stat signature.

Deterministic reproduction

  1. Create two folders A and B, each containing model.gguf. Give them different valid GGUF metadata/contents but the same byte count. Copy or set mtime_ns to exactly the same value (common after shutil.copy2, backup restore, or metadata-preserving moves).
  2. Construct GGUFModelDirectory(lambda: selected_root), where selected_root=A, and scan. Metadata and path refer to A.
  3. Change selected_root=B, scan again through the same directory object.
  4. Actual: the (relative,size,mtime_ns) cache hit returns A's InstalledModel, including path=A/model.gguf and its context/quantization, while resolve_path points to B.
  5. Expected: a settings-root switch always invalidates the cache or uses the canonical root/file identity as part of its key.

Fix / regression

Track the resolved directory root and clear cached entries on change, or include it in the key; consider per-file identity for conservative replacement detection. Add two-root fixture with equal names/sizes/mtimes but different GGUF metadata, assert the second scan reports B's actual path, architecture, and context length.

Reviewed revision: e824fe2dcba0d1be127c9fff165f776186dc37e9. Source-level deterministic cache-key proof; two-root test not executed.

dovvnloading avatar Nov 09 '26 15:11 dovvnloading