Scope an extraction result in place to the files actually dispatched (#2926). Graph-side mirror of the ``allowed_source_files`` write-guard in :func:`save_semantic_cache` (#1757). A model can attribute stray nodes/edges to a corpus file that was not part of the current extraction ba
(
result: dict,
root: Path = Path("."),
allowed_source_files: "Iterable[str | Path] | None" = None,
)
| 2609 | |
| 2610 | |
| 2611 | def scope_semantic_result( |
| 2612 | result: dict, |
| 2613 | root: Path = Path("."), |
| 2614 | allowed_source_files: "Iterable[str | Path] | None" = None, |
| 2615 | ) -> tuple[set[str], int]: |
| 2616 | """Scope an extraction result in place to the files actually dispatched (#2926). |
| 2617 | |
| 2618 | Graph-side mirror of the ``allowed_source_files`` write-guard in |
| 2619 | :func:`save_semantic_cache` (#1757). A model can attribute stray |
| 2620 | nodes/edges to a corpus file that was not part of the current extraction |
| 2621 | batch; :func:`build_merge` derives its replace-set from the source_files |
| 2622 | present in the new chunks, so such a stray fragment would REPLACE that |
| 2623 | file's entire prior contribution in graph.json — while its manifest entry |
| 2624 | still says unchanged, so no later incremental run re-dispatches it and the |
| 2625 | loss is permanent until a full rebuild. |
| 2626 | |
| 2627 | Nodes and hyperedges whose ``source_file`` resolves outside |
| 2628 | ``allowed_source_files`` are dropped from ``result`` (mutated in place); |
| 2629 | items without a ``source_file`` pass through. An edge whose own |
| 2630 | ``source_file`` is outside the allowlist is kept when both endpoint ids are |
| 2631 | kept nodes (#2276). An edge or hyperedge that survives |
| 2632 | the scope filter but references a dropped node id is dropped too (#1916 |
| 2633 | mirror), unless that id is also defined by a kept node (duplicate |
| 2634 | attribution must not be over-pruned). |
| 2635 | |
| 2636 | Path matching shares :func:`_semantic_source_matcher` with |
| 2637 | :func:`save_semantic_cache` (relative against ``root``, walked-path |
| 2638 | identity). That kept edge can still be skipped by the cache write. |
| 2639 | |
| 2640 | Returns ``(dropped_source_files, dropped_item_count)`` for logging; |
| 2641 | ``dropped_source_files`` holds the normalized ``source_file`` strings of |
| 2642 | every group that had at least one item removed. |
| 2643 | """ |
| 2644 | if allowed_source_files is None: |
| 2645 | return set(), 0 |
| 2646 | |
| 2647 | source_identity, normalize_value = _semantic_source_matcher(root) |
| 2648 | |
| 2649 | def _item_identity(item: dict) -> tuple[str | None, Path | None]: |
| 2650 | """(display form, walked identity) of an item's source_file.""" |
| 2651 | src = item.get("source_file") |
| 2652 | if not src: |
| 2653 | return None, None |
| 2654 | norm = normalize_value(src) |
| 2655 | return norm, source_identity(norm) |
| 2656 | |
| 2657 | allowed_paths = {source_identity(str(path)) for path in allowed_source_files} |
| 2658 | |
| 2659 | def _hashable(value) -> bool: |
| 2660 | try: |
| 2661 | hash(value) |
| 2662 | except TypeError: |
| 2663 | return False |
| 2664 | return True |
| 2665 | |
| 2666 | dropped_files: set[str] = set() |
| 2667 | dropped_items = 0 |
| 2668 | dropped_ids: set = set() |