fix(watch): keep extract()'s run-only keys out of --no-cluster graph.json

extract() returns extracted_sources (#3411) and failed_sources (#2543)
next to nodes/edges. They steer the current run and hold the absolute
paths of its inputs. The raw --no-cluster writers dumped the extraction
as graph.json, so the first `graphify update --no-cluster` and every
`graphify extract --no-cluster` wrote the checkout path and OS username
into the graph. A rebuild dropped them again, and also added a
`hyperedges` key the first build lacked, so the first build and a
rebuild of the same tree wrote different files.

Name the run-only keys once (extract.RUN_ONLY_EXTRACTION_KEYS) and leave
them out of both raw writers; the update writer always writes
`hyperedges`, as the extract writer and the clustered path already do.
Nothing reads these keys back from graph.json: build_merge and
merge_raw_extraction take them from the in-memory extraction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 76cfcef556)
This commit is contained in:
rohit-jsfreaky authored and safishamsi committed 2026-10-07 17:26:17 +01:00
1 parent 1965a00841
commit e9597313ef
4 files changed
+85 -2

No files matched your search

+8 -1
View File
@@ -4552,7 +4552,14 @@ def dispatch_command(cmd: str) -> None:
_backup(graphify_out)
_invalidate_file_manifest_for_db_graph()
from graphify.paths import write_json_atomic as _write_json_atomic
_write_json_atomic(graph_json_path, merged, indent=2)
from graphify.extract import RUN_ONLY_EXTRACTION_KEYS as _RUN_ONLY_KEYS
# merge_raw_extraction above has consumed them; graph.json must not
# carry this run's absolute input paths.
_write_json_atomic(
graph_json_path,
{k: v for k, v in merged.items() if k not in _RUN_ONLY_KEYS},
indent=2,
)
try:
# Record the scan root so a later build_merge / update runbook can
# relativize deleted-file paths correctly even for a custom --out
+8
View File
@@ -9235,6 +9235,14 @@ def extract(
}
# Keys of an extract() result that only steer the current run (#2543, #3411).
# They hold the absolute paths of this run's inputs, so a writer that dumps the
# raw extraction as graph.json (--no-cluster) must leave them out: they put the
# checkout path and OS username into the graph, and differ between the first
# build and a rebuild of the same tree.
RUN_ONLY_EXTRACTION_KEYS = frozenset({"extracted_sources", "failed_sources"})
def collect_files(target: Path, *, follow_symlinks: bool = False, root: Path | None = None) -> list[Path]:
containment_root = root if root is not None else target
from graphify.detect import _resolves_under_root
+9 -1
View File
@@ -2101,10 +2101,18 @@ def _rebuild_code(
)
raw_nodes = _dedupe_nodes(result.get("nodes", []))
_disamb_labels(raw_nodes)
from graphify.extract import RUN_ONLY_EXTRACTION_KEYS as _RUN_ONLY_KEYS
candidate_graph_data = {
**{k: v for k, v in result.items() if k not in ("edges", "nodes")},
**{
k: v for k, v in result.items()
if k not in ("edges", "nodes") and k not in _RUN_ONLY_KEYS
},
"nodes": raw_nodes,
"links": _dedupe_edges(result.get("edges", [])),
# A first build's fresh extraction may carry no hyperedges key,
# while a reconciled rebuild always does; write it either way so
# the two builds of one tree give the same file.
"hyperedges": list(result.get("hyperedges", [])),
# Inherit the existing graph's directed flag (#2342) so
# `graphify update --no-cluster` can't silently drop it -
# `result` (the raw merged extraction) never carries one.
+60
View File
@@ -0,0 +1,60 @@
"""A --no-cluster graph.json must not carry extract()'s run-only keys.
extract() returns ``extracted_sources`` (#3411) and ``failed_sources`` (#2543)
next to nodes/edges. They steer the current run and hold the absolute paths of
its inputs. The raw --no-cluster writers dumped the extraction as graph.json, so
the first ``graphify update --no-cluster`` and every ``graphify extract
--no-cluster`` wrote the checkout path and OS username into the graph, and a
first build differed from a rebuild of the same tree.
"""
from __future__ import annotations
import json
import os
import subprocess
import sys
from pathlib import Path
from graphify.extract import RUN_ONLY_EXTRACTION_KEYS
PYTHON = sys.executable
def _corpus(root: Path) -> Path:
root.mkdir(parents=True)
(root / "app.py").write_text("import os\n\n\ndef home():\n return os.getcwd()\n", encoding="utf-8")
(root / "util.py").write_text("from app import home\n\n\ndef where():\n return home()\n", encoding="utf-8")
return root
def _run(root: Path, *args: str) -> dict:
env = dict(os.environ, PYTHONHASHSEED="0", PYTHONIOENCODING="utf-8")
r = subprocess.run([PYTHON, "-m", "graphify", *args], cwd=root, env=env, capture_output=True, text=True)
assert r.returncode == 0, r.stderr[-2000:]
text = (root / "graphify-out" / "graph.json").read_text(encoding="utf-8")
assert "alice_home" not in text, "graph.json embeds the checkout path"
return json.loads(text)
def test_run_only_keys_are_the_ones_extract_returns(tmp_path: Path):
from graphify.extract import extract
root = _corpus(tmp_path / "proj")
result = extract([root / "app.py", root / "util.py"], root=root, cache_root=tmp_path / "cache")
assert RUN_ONLY_EXTRACTION_KEYS <= set(result)
def test_update_no_cluster_first_build_matches_a_rebuild(tmp_path: Path):
root = _corpus(tmp_path / "alice_home" / "proj")
first = _run(root, "update", ".", "--no-cluster")
assert not RUN_ONLY_EXTRACTION_KEYS & set(first)
rebuilt = _run(root, "update", ".", "--no-cluster", "--force")
assert set(first) == set(rebuilt)
def test_extract_no_cluster_omits_run_only_keys(tmp_path: Path):
root = _corpus(tmp_path / "alice_home" / "proj")
graph = _run(root, "extract", ".", "--code-only", "--no-cluster")
assert not RUN_ONLY_EXTRACTION_KEYS & set(graph)
graph = _run(root, "extract", ".", "--code-only", "--no-cluster", "--force")
assert not RUN_ONLY_EXTRACTION_KEYS & set(graph)