Skip to content

fix(query): key recursive cycle detection on kind and id for multi-kind walks - #792

Open
pdlug wants to merge 1 commit into
mainfrom
fix/recursive-kind-qualified-cycle
Open

pdlug wants to merge 1 commit into
mainfrom
fix/recursive-kind-qualified-cycle

Conversation

@pdlug

@pdlug pdlug commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Node rows are keyed by (kind, id), so two nodes of different kinds may share an id. The recursive traversal compiler used the bare node id as its per-step cycle-detection and path token unless identity expansion was on. Any recursive walk able to visit more than one kind therefore read a node whose id matched an earlier node of another kind on the same path as a revisit, and silently omitted it and everything beyond it. For example, from("Show").traverse("linked").recursive().to("Clip") returned nothing when the Show and the Clip shared an id.

The token shape now has one owner, recursivePathTokenShape in src/query/compiler/recursive-path-token.ts. It is kind-qualified (kind || U+001F || id) whenever the walk's source kinds plus step kinds contain more than one kind, or identity members are expanded, and the bare id otherwise. The recursive compiler and the path materializer (transformPathColumns) both call it with the same inputs: the materializer resolves a traversal's source kinds through the same getNodeKindsForAlias the compiler uses, including for later composed recursive stages, so it strips composite tokens exactly when the compiler emitted them. The worktable kind column and the column-pruning requirement go through the same owner. Single-kind walks and identity-expanded walks compile to byte-identical SQL. path output stays an array of bare node ids, and qualified paths are unchanged.

Two existing tests asserted the old behavior: a non-identity Person→Company control walk that stopped short at a same-id peer now reaches it, and a SQL-shape test for a Person→Organization walk now expects the kind-qualified token.

store.subgraph has a broader form of the same conflation (its whole walk and result map are id-keyed), which needs a result-shape decision and is tracked in #791.

The new cross-backend cases in tests/backends/integration/recursive-shared-ids.ts fail on main and pass with the fix. Mutating the owner back to the identity-only rule fails them, and forcing the materializer to always strip fails the separator-bearing-id case.

…nd walks

Node rows are keyed by (kind, id), but a recursive traversal that could visit more than one kind used the bare node id as its cycle-detection and path token unless identity expansion was on. Reaching a node whose id matched an earlier node of a different kind on the same path read as a revisit, so that node and everything beyond it were silently omitted.

The token shape is now decided by one owner, recursivePathTokenShape (src/query/compiler/recursive-path-token.ts): kind-qualified whenever the walk's start kinds plus step kinds hold more than one kind, or identity members are expanded; bare id otherwise. The recursive compiler and the path materializer both call it from the same inputs, so the materializer strips composite tokens exactly when the compiler emitted them. Single-kind walks compile to byte-identical SQL, and path output stays an array of bare node ids.
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