Skip to content

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
acn-ericlaw merged 1 commit into
mainfrom
fix/graph-ingest-ends-by-index
Nov 27, 2026
Merged

acn-ericlaw merged 1 commit into
mainfrom
fix/graph-ingest-ends-by-index

Conversation

@acn-ericlaw

Copy link
Copy Markdown
Collaborator

What

graph.ingest finds a node by an index on its key, so the cost of a write no longer grows with the database.

  • Every node the ingest writes carries the label _Ingested beside its own.
  • One uniqueness constraint, mercury_ingested_key on (:_Ingested {key}), gives the index.
  • The node MERGE, the check that a relationship's ends exist, and the relationship MERGE all go through the label.
  • The first ingest of a process prepares the graph: when the constraint is missing, every node with a key takes 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.ingest matched each end of a relationship by key without 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 Before Now
300 nodes 67 ms -
20,000 nodes 1.1 s -
180,000 nodes, through graph.ingest 9.6 s about 30 ms

The plan before was an all-nodes scan for each end, and stayed one with an index on every label. The default tools.graph.timeout.ms is 10 s, so on a slower database the same write is a 408.

Two more things showed while reading:

  • The node MERGE had no index either: 505 ms for 150 nodes on a label of 30,000, 7 ms now.
  • The key was not unique, though the row says it is. A key ingested under a second label was written as a second node, and a relationship to that key reached both. The constraint makes it a 400.

For the reviewer

Choices taken inside the fix.

  1. The label's name, _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.
  2. A uniqueness constraint, not a plain index. It also makes a concurrent MERGE of 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.
  3. The tool prepares the graph itself, at the first ingest of a process. Labelling 180,000 nodes took 0.4 s. A process without the privilege gets a 503 naming the statement for an administrator; a labelling over the deadline a 408 naming it.
  4. graph.schema leaves the label out; a query result shows it among a node's labels.
  5. Two rows change, so two catalog files: graph_query tells the model to find a node by key through the label, graph_ingest that a key names one node. A graph that copied the earlier rows keeps working.
  6. Another dialect is not broken. Memgraph refuses a constraint command inside a transaction. There the tool labels and matches by the label, logs one warning, and the README gives the two statements for the installation.

Upgrade note - READ

  • The first ingest after the upgrade labels the nodes of an existing graph and creates the constraint. The database user needs the privilege to create a constraint once, or an administrator runs the statement the README gives.
  • A graph that holds one key on several nodes is refused with a 409 until the extra nodes are removed.
  • A node written by graph.write is an end of an ingested relationship only when it carries _Ingested and a key.
  • A query that listed a node's labels now sees _Ingested among them.

Verification

  • pytest: 921 passed, 76 skipped; tests/test_agent_tools.py 166 passed with the live round trip on (TOOLS_GRAPH_TEST_URI), five tests new.
  • Live on Neo4j 5 Community: the round trip, a node written before the label merged into itself and not written twice, a second label for a key refused, a key on two nodes refused with the 409 and accepted after the repair, the README's batched labelling (25,000 nodes) and constraint statement.
  • Live on Memgraph: the ingest twice, the node from before the label, the schema, the README's index and constraint statements, the plan through the index.
  • ruff check and ruff format --check clean.
  • Not done: a privilege refusal against a real server (Community has no roles; held by a test on the driver's error class), and the type checker, which is not installed in this environment.

Not in this pull request

  • The Node twin, raised beside this one with the same statements and rows.
  • The engines' page (docs/guides/agent-tools.md in mercury-composable), which follows the two packs.
  • The session memory, which goes to main.

Co-Authored-By: Claude Fable 5.2 noreply@anthropic.com

🤖 Generated with Claude Code

https://claude.ai/code/session_01UNwVNhbvhqbdRRLBSbSWZ3

…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
acn-ericlaw merged commit 9aae5f3 into main Nov 27, 2026
6 checks passed
@acn-ericlaw
acn-ericlaw deleted the fix/graph-ingest-ends-by-index branch November 27, 2026 21:01
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant