Semantic extraction: nonexistent model-invented source_file bypasses #1895 graph filter
Summary
The graph-side fix for #1895 is incomplete for a source_file that does not exist on disk.
Reproduced with Graphify 0.9.26, Python 3.12, and the DeepSeek backend. Semantic extraction can return a node whose source_file was reconstructed from a referenced node ID or basename in a document rather than copied from the dispatched source path. The reconstructed path does not exist.
The semantic cache correctly refuses to persist this attribution, but the same node still reaches the live merged graph.json.
Root cause
The current out-of-scope filter effectively does:
p = _resolve_against_root(source_file)
return p.is_file() and p not in _dispatched_resolved
This drops an existing undispatched file, but a nonexistent model-invented path has p.is_file() == False and therefore passes through as a model-invented anchor.
That is unsafe for source_file: a conceptual node may be inferred, but its provenance should still point to a source actually dispatched to the extraction call.
Reproduction shape
- A dispatched Markdown document contains a node ID or filename as data, for example in query-memory metadata.
- The model reconstructs that value into a plausible repository path and returns it as
source_file. - The reconstructed path does not exist and was not dispatched.
- Cache validation rejects it, but live merge writes it to
graph.json. - A subsequent extraction may remove the old ghost and create a different one, so repeated incremental refreshes do not reliably converge.
This blocks strict downstream source-policy validation and can cause repeated paid semantic extraction attempts.
Expected behavior
For nodes, edges, and hyperedges, a non-empty source_file should exactly resolve to one of the source paths dispatched for that extraction call. Existence should not determine admission.
Suggested invariant:
resolved_source_file in dispatched_resolved
If a node is rejected, dependent edges and hyperedges should also be removed. The extraction prompt should additionally state that source_file must be copied exactly from a supplied <untrusted_source path="..."> and must never be inferred from labels, node IDs, citations, basenames, imports, or referenced entities.
Relationship to #1895
#1895 closed the graph-write gap only for real existing files outside the dispatched set. This report covers the remaining nonexistent-path branch that the current p.is_file() condition explicitly allows.
Seen on 0.9.46 (claude-cli backend), and _run_one in llm.py is unchanged in 0.9.71. A variant of the same cause: the dispatched Markdown file starts with a frontmatter path: line naming the file it was copied from (<Project>/<folder>/<file>.md). The model often returns that string as source_file instead of the path it was given. save_semantic_cache rejects the group (the #1991 "paths do not resolve" warning), so nothing is cached and every later run sends the file again. The nodes are then left out of graph.json. In one corpus, 447 of 736 such files ended up with no semantic nodes, and running again didn't change that.
Filtering stops the stray paths from spreading through the graph, but it throws the extraction away. A repair that worked for us, applied to each chunk before the checkpoint: for each node, edge or hyperedge whose source_file doesn't resolve under the root, find the chunk's dispatched files whose text contains that string. If exactly one does, rewrite source_file to that file's relative path (if none match, try the file name alone). Zero or several matches leave it alone. After two rebuilds with this wrapped around _extract_with_adaptive_retry, every affected file was cached and had semantic nodes.