supertag-cli icon indicating copy to clipboard operation
supertag-cli copied to clipboard

tana_graph_query always returns zero results — wrong column in tag_applications JOIN

Open evielync opened this issue 5 months ago • 2 comments

Version: 2.5.10

Description

tana_graph_query always returns zero results for all queries, regardless of supertag names or graph topology. The execution plan looks correct (it generates the right steps), but the actual traversal returns nothing.

Steps to reproduce

  1. Have nodes tagged #log with inline references to nodes tagged #person (confirmed present in the DB)
  2. Run: tana_graph_query with query "FIND log CONNECTED TO person RETURN name LIMIT 5"
  3. Observe: {"rows": [], "count": 0}

Root cause — confirmed via SQL

The graph traversal joins tag_applications using tuple_node_id instead of data_node_id.

tuple_node_id is the ID of Tana's internal compound tagging node — it never appears as a from_node in the references table. data_node_id is the actual data node ID, which is what the references table uses.

Proof:

-- Uses tuple_node_id (what graph_query does) → 0 results
SELECT COUNT(*) 
FROM references r
JOIN tag_applications ta ON r.from_node = ta.tuple_node_id
WHERE ta.tag_name = 'log' AND r.reference_type = 'inline_ref';
-- Result: 0

-- Uses data_node_id (correct) → 170 results
SELECT COUNT(*) 
FROM references r
JOIN tag_applications ta ON r.from_node = ta.data_node_id
WHERE ta.tag_name = 'log' AND r.reference_type = 'inline_ref';
-- Result: 170

The manual traversal with the correct column returns exactly the expected nodes:

SELECT n2.name 
FROM tag_applications ta1
JOIN references r ON ta1.data_node_id = r.from_node
JOIN tag_applications ta2 ON r.to_node = ta2.data_node_id
JOIN nodes n2 ON ta2.data_node_id = n2.id
WHERE ta1.tag_name = 'log' 
AND ta2.tag_name = 'person'
AND r.reference_type = 'inline_ref'
LIMIT 5;
-- Returns: Angela Raspass, Jen Stumbles, Bones Ijeoma, ...

Expected behaviour

FIND log CONNECTED TO person RETURN name should return log entries that contain inline references to person nodes.

Additional context

This affects all graph traversal — tana_related is unaffected (it uses a different code path and works correctly). The explain: true flag correctly describes the intended traversal plan; the bug is only in the execution step.

DB stats at time of testing:

  • 2,363,488 nodes
  • 5,793 inline references
  • 19,161 tag applications

The fix should be to change the JOIN in the graph traversal from tuple_node_id to data_node_id in the tag_applications lookup.

evielync avatar Jun 21 '26 03:06 evielync

Thanks for the detailed SQL. I checked this against current main (v2.5.11), and the traversal doesn't join on tuple_node_id. That column is only written by the indexer and never read by any query. Tag lookups during traversal already use data_node_id (src/query/tana-query-engine.ts).

The zero result looks like it has a different cause:

  • getRelatedNodes follows child/parent edges from the references table, but the indexer only stores inline_ref rows there. Child edges are never written, so CONNECTED TO can't reach an inline reference that sits on one of the log's child nodes, which is where most of them are.
  • The FIND step and the traversal are each capped at 100 rows before the tag filter runs, which loses more matches on large workspaces.

Planned fix: serve child/parent (and reference-field) edges when a node is traversed, plus a regression test for FIND log CONNECTED TO person. This overlaps with #100.

jcfischer avatar Oct 21 '26 12:10 jcfischer

Fixed in #121 (merged as 8c695a8). It ships in the next release.

Root cause, confirmed with a failing test on a fixture built by the real indexer:

  1. getRelatedNodes looked up child/parent rows in the references table, but the indexer only ever writes inline_ref rows there. So CONNECTED TO could never reach an inline ref on a log's child node, which is where most refs sit. Before the fix, FIND log CONNECTED TO person returned 0 rows.
  2. The FIND step took only the first LIMIT nodes, and each traversal was capped at 100 before the target-tag filter ran, so matches on older nodes were dropped.

What changed:

  • CONNECTED TO now counts inline refs from anywhere in a node's content: its untagged children and grandchildren, up to 3 levels deep.
  • Child and parent edges come from the node tree.
  • Traversal filters by target tag in SQL with no cap.
  • FIND pages through the tag until LIMIT connected rows are found.

tana_related also returns child/parent edges now.

jcfischer avatar Oct 21 '26 18:10 jcfischer