Returns the cache directory for ``kind`` - creates it if needed. kind is "ast", "semantic", or a mode-namespaced semantic kind such as "semantic-deep" (#1894). Separate subdirectories prevent semantic cache entries from overwriting AST cache entries for the same source_file (#582).
(root: Path = Path("."), kind: str = "ast",
prompt_fp: str | None = None)
| 1417 | |
| 1418 | |
| 1419 | def cache_dir(root: Path = Path("."), kind: str = "ast", |
| 1420 | prompt_fp: str | None = None) -> Path: |
| 1421 | """Returns the cache directory for ``kind`` - creates it if needed. |
| 1422 | |
| 1423 | kind is "ast", "semantic", or a mode-namespaced semantic kind such as |
| 1424 | "semantic-deep" (#1894). Separate subdirectories prevent semantic cache |
| 1425 | entries from overwriting AST cache entries for the same source_file (#582). |
| 1426 | |
| 1427 | AST entries live in graphify-out/cache/ast/v{version}-s{schema}-g{grammars}/, namespaced |
| 1428 | by graphify version, cache-key schema, and tree-sitter grammar set because they depend on extractor |
| 1429 | code and key semantics, not just file contents. Semantic entries are still |
| 1430 | NOT version-namespaced (re-extraction |
| 1431 | costs LLM calls, #1252): they live in graphify-out/cache/semantic/, with |
| 1432 | deep-mode entries beside them in graphify-out/cache/semantic-deep/. |
| 1433 | |
| 1434 | ``prompt_fp`` (semantic kinds only) adds a p{fingerprint}/ subdirectory so |
| 1435 | entries are attributed to the extraction prompt that produced them (#1939). |
| 1436 | Omitting it yields the historical flat layout, where entries of unknown |
| 1437 | vintage live. |
| 1438 | """ |
| 1439 | try: |
| 1440 | cwd = os.getcwd() |
| 1441 | except OSError: |
| 1442 | cwd = "" |
| 1443 | if kind == "ast": |
| 1444 | namespace = f"v{_EXTRACTOR_VERSION}-s{_AST_CACHE_SCHEMA}-g{_grammar_fingerprint()}" |
| 1445 | else: |
| 1446 | namespace = f"p{prompt_fp}" if prompt_fp else "" |
| 1447 | key = (cwd, _GRAPHIFY_OUT, str(root), kind, namespace) |
| 1448 | got = _CACHE_DIR_MEMO.get(key) |
| 1449 | if got is not None: |
| 1450 | # A caller may have deleted graphify-out between rebuilds; recreating |
| 1451 | # the directory is a stat on the common path, instead of the |
| 1452 | # Path.resolve() + mkdir this used to do per file. |
| 1453 | if not got.is_dir(): |
| 1454 | got.mkdir(parents=True, exist_ok=True) |
| 1455 | return got |
| 1456 | _out = Path(_GRAPHIFY_OUT) |
| 1457 | base = _out if _out.is_absolute() else Path(root).resolve() / _out |
| 1458 | d = base / "cache" / kind |
| 1459 | if namespace: |
| 1460 | d = d / namespace |
| 1461 | if kind == "ast": |
| 1462 | _cleanup_stale_ast_entries(d.parent, d) |
| 1463 | d.mkdir(parents=True, exist_ok=True) |
| 1464 | _CACHE_DIR_MEMO[key] = d |
| 1465 | return d |
| 1466 | |
| 1467 | |
| 1468 | def _salted_key(h: str, salt: str | None) -> str: |