-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
fix(export): write graph.canvas and Obsidian notes atomically #3304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
drmikecrypto
wants to merge
1
commit into
Graphify-Labs:v8
Choose a base branch
from
drmikecrypto:fix/3282-atomic-canvas-write
base: v8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+76
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """Regression: to_canvas / Obsidian vault writes must be atomic (#3282).""" | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| import networkx as nx | ||
| import pytest | ||
|
|
||
| from graphify.export import to_canvas, to_obsidian | ||
|
|
||
|
|
||
| def _tiny_graph(): | ||
| G = nx.Graph() | ||
| G.add_node("a", label="Alpha", file_type="code", source_file="a.py") | ||
| G.add_node("b", label="Beta", file_type="code", source_file="b.py") | ||
| G.add_edge("a", "b", relation="calls", confidence="EXTRACTED", weight=1.0) | ||
| return G, {0: ["a", "b"]} | ||
|
|
||
|
|
||
| def test_to_canvas_uses_atomic_replace(tmp_path, monkeypatch): | ||
| G, communities = _tiny_graph() | ||
| out = tmp_path / "graph.canvas" | ||
| out.write_text('{"nodes":[],"edges":[]}', encoding="utf-8") | ||
|
|
||
| real_replace = os.replace | ||
| calls: list[tuple[str, str]] = [] | ||
|
|
||
| def tracking_replace(src, dst): | ||
| calls.append((str(src), str(dst))) | ||
| return real_replace(src, dst) | ||
|
|
||
| monkeypatch.setattr(os, "replace", tracking_replace) | ||
| to_canvas(G, communities, str(out)) | ||
|
|
||
| assert any(Path(dst).resolve() == out.resolve() for _, dst in calls), calls | ||
| data = json.loads(out.read_text(encoding="utf-8")) | ||
| assert "nodes" in data and "edges" in data | ||
| assert len(data["nodes"]) >= 2 | ||
|
|
||
|
|
||
| def test_to_canvas_preserves_existing_when_replace_fails(tmp_path, monkeypatch): | ||
| G, communities = _tiny_graph() | ||
| out = tmp_path / "graph.canvas" | ||
| original = '{"nodes":[],"edges":[],"preserved":true}' | ||
| out.write_text(original, encoding="utf-8") | ||
|
|
||
| def boom(src, dst): | ||
| raise OSError("simulated failure") | ||
|
|
||
| monkeypatch.setattr(os, "replace", boom) | ||
| with pytest.raises(OSError): | ||
| to_canvas(G, communities, str(out)) | ||
|
|
||
| assert out.read_text(encoding="utf-8") == original | ||
|
|
||
|
|
||
| def test_to_obsidian_owned_writes_are_atomic(tmp_path, monkeypatch): | ||
| G, communities = _tiny_graph() | ||
| real_replace = os.replace | ||
| calls: list[str] = [] | ||
|
|
||
| def tracking_replace(src, dst): | ||
| calls.append(str(dst)) | ||
| return real_replace(src, dst) | ||
|
|
||
| monkeypatch.setattr(os, "replace", tracking_replace) | ||
| to_obsidian(G, communities, str(tmp_path), community_labels={0: "Core"}) | ||
|
|
||
| # At least one vault artifact should land via os.replace (notes / graph.json). | ||
| assert calls, "expected atomic replaces for Obsidian vault writes" | ||
| assert any(Path(p).name.endswith(".md") or p.endswith("graph.json") for p in calls) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to_graphml()15 callers depend on it (afferent coupling).
Grounded coupling-delta finding (deterministic), not an LLM guess.