Return cached extraction for this file if hash matches, else None. Cache key: SHA256 of file contents. Semantic entries use {hash[:2]}/{hash}.json inside their namespace. Older entries directly inside that namespace are also readable. ``root`` anchors the content-hash key and sourc
(path: Path, root: Path = Path("."), kind: str = "ast",
cache_root: Path | None = None, prompt: "str | Path | None" = None,
prompt_file: "str | Path | None" = None,
allow_legacy: bool = True,
allow_partial: bool = False,
salt: str | None = None)
| 1485 | |
| 1486 | |
| 1487 | def load_cached(path: Path, root: Path = Path("."), kind: str = "ast", |
| 1488 | cache_root: Path | None = None, prompt: "str | Path | None" = None, |
| 1489 | prompt_file: "str | Path | None" = None, |
| 1490 | allow_legacy: bool = True, |
| 1491 | allow_partial: bool = False, |
| 1492 | salt: str | None = None) -> dict | None: |
| 1493 | """Return cached extraction for this file if hash matches, else None. |
| 1494 | |
| 1495 | Cache key: SHA256 of file contents. |
| 1496 | Semantic entries use {hash[:2]}/{hash}.json inside their namespace. |
| 1497 | Older entries directly inside that namespace are also readable. |
| 1498 | |
| 1499 | ``root`` anchors the content-hash key and source_file relativization (it |
| 1500 | must stay the inferred common parent so keys remain portable). ``cache_root`` |
| 1501 | decouples *where* the cache directory lives from that anchor — the cache is |
| 1502 | an output and must not land inside a read-only/analyzed source tree (#1774). |
| 1503 | When ``cache_root`` is None the location falls back to ``root`` (unchanged |
| 1504 | behavior for existing callers). |
| 1505 | |
| 1506 | AST entries written by other graphify versions — including the legacy |
| 1507 | flat cache/ layout (pre-0.5.3) and the unversioned cache/ast/ layout — |
| 1508 | are deliberately not consulted: they were produced by a different |
| 1509 | extractor and may be stale. |
| 1510 | |
| 1511 | ``prompt`` (semantic kinds) is the extraction prompt — text, or a Path to |
| 1512 | the prompt file — that the caller is about to extract with. It selects the |
| 1513 | p{fingerprint}/ namespace, so an entry produced by a different prompt is a |
| 1514 | miss rather than a silent stale hit (#1939). When it is given and the |
| 1515 | fingerprinted namespace misses, ``allow_legacy`` (default True) falls back |
| 1516 | to a flat-layout entry: those predate fingerprinting, so their vintage is |
| 1517 | unknowable — they are served rather than re-billed, and the hit is counted |
| 1518 | so :func:`check_semantic_cache` can report N to the user. Callers that must |
| 1519 | not mix vintages within one entry (see :func:`save_semantic_cache`'s |
| 1520 | ``merge_existing``) pass allow_legacy=False. |
| 1521 | ``salt`` folds extra material into the key for an entry whose validity |
| 1522 | depends on more than the file's bytes — a file the project remaps to |
| 1523 | another language (#2961) parses to a different graph from the same |
| 1524 | content, so it must not hit the entry produced under the old extractor. |
| 1525 | Returns None if no cache entry or file has changed. |
| 1526 | """ |
| 1527 | global _legacy_semantic_hits, _corrupt_cache_entries |
| 1528 | location = cache_root if cache_root is not None else root |
| 1529 | try: |
| 1530 | h = file_hash(path, root, cache_root=cache_root, include_frontmatter=kind == "ast") |
| 1531 | except OSError: |
| 1532 | return None |
| 1533 | h = _salted_key(h, salt) |
| 1534 | prompt_fp = _resolve_prompt_fp(prompt, prompt_file) |
| 1535 | # Extended-length form so a deep root does not push the entry past MAX_PATH. |
| 1536 | entry = Path(_os_long_path(_cache_entry_path(location, kind, h, prompt_fp))) |
| 1537 | legacy_hit = False |
| 1538 | if prompt_fp and not entry.exists() and allow_legacy: |
| 1539 | legacy = Path(_os_long_path(_cache_entry_path(location, kind, h))) |
| 1540 | if legacy.exists(): |
| 1541 | entry, legacy_hit = legacy, True |
| 1542 | if entry.exists(): |
| 1543 | try: |
| 1544 | result = json.loads(entry.read_text(encoding="utf-8")) |