Save extraction result for this file. ``salt`` must match what the corresponding :func:`load_cached` passes (see there); it namespaces the key, not the directory. Each semantic namespace groups entries by the first two hex digits of the cache key. result should be a dict with 'node
(path: Path, result: dict, root: Path = Path("."), kind: str = "ast",
cache_root: Path | None = None, prompt: "str | Path | None" = None,
prompt_file: "str | Path | None" = None,
salt: str | None = None)
| 1788 | |
| 1789 | |
| 1790 | def save_cached(path: Path, result: dict, root: Path = Path("."), kind: str = "ast", |
| 1791 | cache_root: Path | None = None, prompt: "str | Path | None" = None, |
| 1792 | prompt_file: "str | Path | None" = None, |
| 1793 | salt: str | None = None) -> None: |
| 1794 | """Save extraction result for this file. |
| 1795 | |
| 1796 | ``salt`` must match what the corresponding :func:`load_cached` passes |
| 1797 | (see there); it namespaces the key, not the directory. |
| 1798 | |
| 1799 | Each semantic namespace groups entries by the first two hex digits of the cache key. |
| 1800 | result should be a dict with 'nodes' and 'edges' lists. |
| 1801 | |
| 1802 | ``root`` anchors the content-hash key and source_file relativization; |
| 1803 | ``cache_root`` (when given) is where the cache directory is written, decoupled |
| 1804 | from ``root`` so the cache never lands inside the analyzed source tree (#1774). |
| 1805 | |
| 1806 | ``prompt`` (semantic kinds) is the extraction prompt that produced ``result`` |
| 1807 | — text, or a Path to the prompt file. It stamps the entry into the |
| 1808 | p{fingerprint}/ namespace so a later run under a different prompt does not |
| 1809 | replay it (#1939). Writes always land in the fingerprinted namespace when a |
| 1810 | prompt is given: an entry of known vintage is never written back into the |
| 1811 | flat unknown-vintage layout. |
| 1812 | |
| 1813 | No-ops if `path` is not a regular file. Subagent-produced semantic fragments |
| 1814 | occasionally carry a directory path in `source_file`; skipping them prevents |
| 1815 | IsADirectoryError from aborting the whole batch. |
| 1816 | """ |
| 1817 | p = Path(path) |
| 1818 | if not p.is_file(): |
| 1819 | return |
| 1820 | # Relativize source_file fields against ``root`` before write so the |
| 1821 | # cache file on disk is portable across machines and checkout |
| 1822 | # directories (#777). The cache key is content-hashed so lookup is |
| 1823 | # already path-independent; this fixes the embedded path leak. |
| 1824 | # |
| 1825 | # Serialize a relativized copy rather than mutating the caller's dict — |
| 1826 | # downstream pipeline steps (notably extract.py's AST prefix remap, which |
| 1827 | # looks up Path(source_file).resolve() in a prefix table) depend on the |
| 1828 | # source_file field's original absolute form. Mutating the input here would |
| 1829 | # silently break those remaps on the first extraction pass. |
| 1830 | # |
| 1831 | # This used to ``copy.deepcopy(result)`` and rewrite in place. deepcopy is a |
| 1832 | # heavy per-file cost on every cold-build cache write; :func:`_save_cached_on_disk` |
| 1833 | # instead builds the same portable payload copy-on-write — allocating fresh |
| 1834 | # dicts/lists only for the sub-objects whose fields are actually rewritten and |
| 1835 | # sharing the rest by reference — so ``result`` is still never mutated (the |
| 1836 | # anchoring would otherwise corrupt the caller's absolute source_file/ids) and |
| 1837 | # the on-disk bytes are unchanged. |
| 1838 | on_disk = result |
| 1839 | if isinstance(result, dict): |
| 1840 | on_disk = _save_cached_on_disk(result, p, root, kind) |
| 1841 | h = _salted_key( |
| 1842 | file_hash(p, root, cache_root=cache_root, include_frontmatter=kind == "ast"), |
| 1843 | salt, |
| 1844 | ) |
| 1845 | location = cache_root if cache_root is not None else root |
| 1846 | # Extended-length form: under a deep root the entry, and its longer temp |
| 1847 | # name, overrun Windows MAX_PATH and the write fails the whole extraction. |