Repository navigation
fix(agent-tools): graph.ingest finds a node by an index on its key - a write no longer reads every node of the database - #89
Merged
Conversation
…a write no longer reads every node of the database
graph.ingest matched each end of a relationship, and checked that the ends exist, by key without a label. A database
index belongs to a label, so no index could serve those matches and the database read every node for each end: on
Neo4j 5 with 180,000 nodes, 150 relationships took 9.6 s through graph.ingest, at the edge of the default 10 s
deadline, against 67 ms on 300 nodes. The node MERGE had no index either.
Every node the ingest writes now carries the label _Ingested beside its own, one uniqueness constraint
(mercury_ingested_key on (:_Ingested {key})) gives the index, and the node MERGE, the check of the ends and the
relationship MERGE go through the label: the same write takes about 30 ms whatever the size of the graph. The
constraint makes a key name one node: the same key under another label is a 400 where it was written as a second
node, and two ingests of one key at the same moment merge into one node.
The first ingest of a process prepares the graph, each step in its own transaction: when the constraint is missing,
every node with a key takes the label and the constraint is created, so a graph written before the label existed
is carried over. A key on more than one node is a 409 naming the query that lists them, a process without the
privilege a 503 naming the statement, a labelling over the deadline a 408 naming it - each before anything is
merged. graph.schema leaves the label out; the graph_query and graph_ingest rows tell the model about it. A
database that does not take Neo4j's constraint commands is labelled and matched by the label, with one warning.
Five new tests and the gated live round trip hold it; driven on Neo4j 5 Community and on Memgraph. The README and
CHANGELOG Unreleased Fixed 3 carry the rule and the upgrade note.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UNwVNhbvhqbdRRLBSbSWZ3
acn-ericlaw
added a commit
that referenced
this pull request
Nov 27, 2026
…port investigated, measured on Neo4j 5 and fixed; #89 merged Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UNwVNhbvhqbdRRLBSbSWZ3
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
graph.ingestfinds a node by an index on its key, so the cost of a write no longer grows with the database._Ingestedbeside its own.mercury_ingested_keyon(:_Ingested {key}), gives the index.MERGE, the check that a relationship's ends exist, and the relationshipMERGEall go through the label.keytakes the label and the constraint is created, each step in its own transaction. A graph written before the label existed is carried over by that ingest.Why
A field report: writes that passed on a small graph went quiet for tens of seconds once the graph had grown.
graph.ingestmatched each end of a relationship bykeywithout a label. A database index belongs to a label, so no index could serve the match and the database read every node for each end.Measured on Neo4j 5 Community, 150 relationships in one call:
graph.ingestThe plan before was an all-nodes scan for each end, and stayed one with an index on every label. The default
tools.graph.timeout.msis 10 s, so on a slower database the same write is a 408.Two more things showed while reading:
MERGEhad no index either: 505 ms for 150 nodes on a label of 30,000, 7 ms now.For the reviewer
Choices taken inside the fix.
_Ingested. It is fixed, not a configuration key, so every process agrees on it. The underscore keeps it out of the callers' names: a caller's label starts with a letter. It becomes data in every graph once released, so this is the moment to rename it.MERGEof one key safe. The cost: a graph that already holds a key on several nodes is refused with a 409 naming the query that lists them, until they are repaired. A plain index would keep such a graph working and its duplicates silent.graph.schemaleaves the label out; a query result shows it among a node's labels.graph_querytells the model to find a node by key through the label,graph_ingestthat a key names one node. A graph that copied the earlier rows keeps working.Upgrade note - READ
graph.writeis an end of an ingested relationship only when it carries_Ingestedand a key._Ingestedamong them.Verification
pytest: 921 passed, 76 skipped;tests/test_agent_tools.py166 passed with the live round trip on (TOOLS_GRAPH_TEST_URI), five tests new.ruff checkandruff format --checkclean.Not in this pull request
docs/guides/agent-tools.mdin mercury-composable), which follows the two packs.main.Co-Authored-By: Claude Fable 5.2 noreply@anthropic.com
🤖 Generated with Claude Code
https://claude.ai/code/session_01UNwVNhbvhqbdRRLBSbSWZ3