Repository navigation
fix(networkx): skip re-initialization when singleton adapter already exists - #694
DevChiniwala wants to merge 1 commit into
Conversation
…exists Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
tonydzi
left a comment
There was a problem hiding this comment.
Read the whole networkx_adapter.py at the PR head, not just the diff. The guard stops the original handle from being retargeted, but it does not give the behavior #493 lists under "Expected Behavior", and it changes who gets the misdirected writes rather than removing them.
1. networkx_adapter.py:60-65 (the guard) + __new__ at :33-50: a second database now silently becomes the first one.
__new__ still returns the one process-wide _instance whatever database_name is passed, and the guard makes __init__ return early. So b = NetworkXAdapter(None, None, None, ".../beta") is the same object as a, with b.filename == ".../alpha.pkl". Every add_node / add_edge through b calls save_graph_to_file(self.filename) and writes beta's data into alpha.pkl.
I checked the constructor semantics in isolation (a stub with the same __new__ / guard, not the full adapter, and without cognee installed): a is b is True and b.filename is alpha.pkl. In the repro from #493 that means "same object for different files: True" and "original handle sees beta edge: True" stay as they are, and "later alpha write in beta file" just flips to "beta write in alpha file". The issue's expected result (alpha handle keeps alpha.pkl, nothing from alpha lands in beta.pkl) is not reached, and the data mixing is now harder to notice because nothing changes on the first handle.
2. Suggested shape: key the singleton by the resolved database name instead of a single _instance.
_instances: dict[str, "NetworkXAdapter"] = {}
def __new__(cls, graph_database_url, graph_database_username, graph_database_password, database_name, **kwargs):
key = database_name if database_name else "cognee_graph"
inst = cls._instances.get(key)
if inst is None:
inst = super().__new__(cls)
inst._initialized = False
cls._instances[key] = inst
return inst
def __init__(self, ..., database_name="", **kwargs):
if self._initialized:
return
... # existing body
self._initialized = TrueSame database name -> same handle, in-memory graph kept (which also covers the "same-store reset" half of #493). Different name -> separate handle, graph and file. If you would rather keep a true single instance, raising (or at least logging a warning) when a different database_name arrives is better than silently reusing the first store.
3. Minor. graph = None is a class attribute (:31), so hasattr(self, "graph") is always true and self.graph is not None carries the whole check. An explicit _initialized flag is clearer and does not depend on nothing ever assigning None to graph later.
4. Tests. packages/graph/networkx/tests/unit/ has no test for this. A regression test with two tmp_path database names would pin the contract: after constructing alpha and beta, a.filename != b.filename, a write through a shows up in alpha.pkl only, and re-constructing alpha keeps its in-memory edge. It should fail on current main and on this PR's guard alone, which is a good check that it tests the right thing.
Happy to be corrected if the singleton-per-process behavior is intentional for cognee's engine cache; in that case the thing to decide is what a second, different database_name should do (error vs. reuse), because the current guard picks "reuse and write into the wrong file" by default.
Closes #493
Summary
NetworkXAdapter.__init__reinitializesdatabase_name,filename, andgraphon every construction, even though__new__returns a singleton instance__init__to skip initialization when the instance already has agraphattributeTest plan
NetworkXAdapterwith different parameters no longer mutates the first handle🤖 Generated with Claude Code