Save semantic extraction results to cache, keyed by source_file. Groups nodes and edges by source_file, then saves one cache entry per file under cache/semantic/ (separate from AST entries in cache/ast/) to prevent hash-key collisions (#582). ``mode`` selects the cache namespace, m
(
nodes: list[dict],
edges: list[dict],
hyperedges: list[dict] | None = None,
root: Path = Path("."),
merge_existing: bool = False,
allowed_source_files: Iterable[str | Path] | None = None,
mode: str | None = None,
prompt: "str | Path | None" = None,
prompt_file: "str | Path | None" = None,
partial_source_files: Iterable[str | Path] | None = None,
cache_root: "Path | None" = None,
)
| 2104 | |
| 2105 | |
| 2106 | def save_semantic_cache( |
| 2107 | nodes: list[dict], |
| 2108 | edges: list[dict], |
| 2109 | hyperedges: list[dict] | None = None, |
| 2110 | root: Path = Path("."), |
| 2111 | merge_existing: bool = False, |
| 2112 | allowed_source_files: Iterable[str | Path] | None = None, |
| 2113 | mode: str | None = None, |
| 2114 | prompt: "str | Path | None" = None, |
| 2115 | prompt_file: "str | Path | None" = None, |
| 2116 | partial_source_files: Iterable[str | Path] | None = None, |
| 2117 | cache_root: "Path | None" = None, |
| 2118 | ) -> int: |
| 2119 | """Save semantic extraction results to cache, keyed by source_file. |
| 2120 | |
| 2121 | Groups nodes and edges by source_file, then saves one cache entry per file |
| 2122 | under cache/semantic/ (separate from AST entries in cache/ast/) to prevent |
| 2123 | hash-key collisions (#582). |
| 2124 | |
| 2125 | ``mode`` selects the cache namespace, mirroring |
| 2126 | :func:`check_semantic_cache`: ``None`` (the default) writes |
| 2127 | ``cache/semantic/`` — byte-identical to the historical behavior for |
| 2128 | existing callers that omit it — while a non-None mode (e.g. ``"deep"``) |
| 2129 | writes ``cache/semantic-{mode}/`` so richer deep-mode results never |
| 2130 | overwrite standard-mode entries and vice versa (#1894). |
| 2131 | |
| 2132 | When ``merge_existing`` is True, any already-cached entry for a file is |
| 2133 | unioned with the new results before saving instead of being overwritten. |
| 2134 | This lets callers checkpoint incrementally (e.g. once per chunk) without |
| 2135 | dropping a prior slice of a large file that was split across chunks. |
| 2136 | |
| 2137 | When ``allowed_source_files`` is provided, only those files may be used as |
| 2138 | cache-write keys. Semantic nodes can legitimately mention another corpus |
| 2139 | file, but a model must not be able to replace that file's complete cache |
| 2140 | entry unless the file was part of the current extraction batch (#1757). |
| 2141 | |
| 2142 | When ``partial_source_files`` is provided, entries for those files are |
| 2143 | stamped ``partial: True`` — the extraction was truncated, so the entry is |
| 2144 | incomplete and :func:`load_cached` must treat it as a miss. Partial-ness is |
| 2145 | ALSO detected intrinsically from a ``_partial`` marker on any grouped item, |
| 2146 | so the flag survives even when a caller (e.g. cli.py's final save) does not |
| 2147 | pass ``partial_source_files``. |
| 2148 | |
| 2149 | ``prompt`` is the extraction prompt that produced these results — text, or |
| 2150 | a Path to the prompt file. It stamps entries into the p{fingerprint}/ |
| 2151 | namespace so a later run under a different prompt re-extracts rather than |
| 2152 | replaying them (#1939). Pass the same prompt here as to |
| 2153 | :func:`check_semantic_cache`, or the write lands in a namespace the next |
| 2154 | read won't consult. |
| 2155 | |
| 2156 | ``cache_root`` decouples *where* the cache directory is written from the |
| 2157 | source-key anchor ``root`` — mirroring the same split that :func:`load_cached` |
| 2158 | and :func:`save_cached` already expose (#1774). When given, cache files land |
| 2159 | under ``cache_root`` while ``source_file`` paths are still resolved and |
| 2160 | relativized against ``root``. When omitted, ``root`` is used for both |
| 2161 | purposes (unchanged behaviour for existing callers). This fixes checkpoints |
| 2162 | and the final save going to the corpus tree instead of ``--out`` (#1990, |
| 2163 | #1991). |