Skip to content

Add cross-relationship QueryFilter: filter nodes by a related node's own properties #3137

Closed

Description

@malibio

Overview

QueryNode's structured filters (QueryFilter, packages/core/src/services/query_service/mod.rs:172) can only filter a node by its own properties, or test bare membership in one of four hardcoded relationship shapes (parent/children/mentions/mentioned_by, packages/core/src/services/query_service/mod.rs:151-157). There is no way to filter a node by a related node's properties — the common, natural query "show me tasks that belong to projects with project status active," or a Jira-style "issues in the active sprint," cannot be expressed today.

Surfaced during design of #3135 (seeding default Playbook views): a Jira Sprint board filtering sprint.tasks by the sprint's own sprint_status == active is exactly this shape, and is currently unbuildable as a QueryFilter.

Problem Statement

build_relationship_condition (packages/core/src/services/query_service/mod.rs:845) only supports a bare "is this node connected to one specific other node id" test — relationshipType is a closed enum over two built-in edge kinds (has_child, mentions), and nodeId is a single, literal target id. It cannot express "connected via relationship X to any node matching property filter Y," and has no awareness of schema-declared relationships (cycle.tasks, project.tasks, anything a Playbook defines) at all — confirmed via direct inspection that query_service never calls resolve_relationship_name (packages/core/src/ops/rel_ops.rs:190), the function that already resolves forward/reverse/extends-inherited relationship names correctly elsewhere in the codebase (Play trigger matching, graph_resolver.rs).

Correction to an earlier framing of this issue: an initial draft stated NodeSpace's relationship model has no edge properties at all. That was wrong — EdgeField/SchemaRelationship.edge_fields (packages/nodespace-types/src/schema.rs:226,308) is a real, implemented, validated feature: edge property values are stored per-instance-edge, validated at write time against the declared edge_fields (validate_edge_data_against_fields, two call sites at packages/core/src/services/node_service/relationship.rs:1137,2019), and fetched alongside relationship reads as (node, edge_properties) pairs (packages/core/src/db/sqlite_store/relationships.rs:1818). This issue is scoped to filtering by a related node's properties, which is the gap that motivated it (#3135's Sprint-board example) — filtering by an edge's own properties (e.g. an assigned_at date on a person --owner--> project edge) is a real, closely related, and currently equally unsupported capability, named explicitly below rather than dismissed as nonexistent.

Proposed Solution

Filter shape: recursive from day one, depth-limited to 1 for now

Add a new FilterType::Related variant. A related-node filter carries:

  • relationshipName: String — resolved via resolve_relationship_name (see below), not restricted to the current four-member enum
  • filter: Box<QueryFilter> — a nested filter, evaluated against the related node(s), matching this same QueryFilter type recursively

This makes the shape itself unbounded in nesting depth from the type definition alone (task → project → owner.department is representable by construction — the inner filter is just another QueryFilter, which can itself be a FilterType::Related). Enforce a max depth of 1 at validation time for this issue (to_query_definition/equivalent rejects a Related filter whose own nested filter is itself Related) — every named use case so far (tasks/projects, issues/sprints) is one hop, and unbounded depth is unvalidated scope with no consuming case yet. Raising the cap later is a one-line validator change, not a shape change, which is the entire point of choosing a recursive type now rather than a flat relatedProperty/relatedValue pair that would need a breaking shape change to extend.

Relationship-name resolution: reuse resolve_relationship_name, not a re-implementation

resolve_relationship_name (packages/core/src/ops/rel_ops.rs:190) already resolves a relationship name correctly across every shape that matters here: built-in (has_child/mentions), schema-declared forward, schema-declared reverse (a name that's a reverse_name on the target schema), and — critically — a relationship declared only on an ancestor schema and inherited via extends (ADR-078) without being redeclared on the descendant. This last case is not hypothetical: a Jira-style story/epic filtering through a task-declared relationship must resolve correctly, and only this resolver's resolve_relationships call (chain-aware) gets that right — a direct schema lookup would not.

This resolver is async; today's filter-building path (to_query_definition, packages/core/src/ops/query_ops.rs) is sync. This issue makes that path async to call the real resolver, rather than requiring a literal, unresolved relationship-type string (rejected as an option: would silently fail on a reverse-only name or an inherited-via-extends relationship, the same class of correctness gap already fixed elsewhere this session for reverse traversal, #2641, and extends-awareness).

Execution: SQL subquery, modeled on the existing relationship-membership pattern

build_relationship_condition already compiles a bare relationship-membership test to id_field IN (SELECT ... FROM relationship WHERE ...). A related-node filter extends this shape: resolve the relationship's actual persisted relationship_type and direction via the resolver above, then compile to id_field IN (SELECT <out_node|in_node> FROM relationship JOIN node ON node.id = relationship.<in_node|out_node> WHERE relationship.relationship_type = ? AND <nested filter's own compiled condition, evaluated against the joined node's properties>). The nested filter's own compilation is a recursive call into the same filter-building logic already used for a top-level filter — no new condition-building logic, just a new entry point that nests.

If the relationship's cardinality is "many" (e.g. sprint.tasks), a related-node filter naturally becomes an existence test ("connected to at least one project matching..."), consistent with how SQL IN (SELECT ...) already behaves — no special-casing needed for many vs. one.

Architecture Impact

No ADR governs QueryFilter/QueryDefinition today — confirmed via search of ../nodespace-docs/decisions/ for query-filter/relationship-filter terms, none found specific to this mechanism. This issue does not reverse or extend an existing decision; it extends an undocumented mechanism using an already-Accepted resolver (resolve_relationship_name, underpinning ADR-078's extends design and #2641's reverse-traversal work) rather than building new resolution logic. No companion ADR required — this is additive capability on an existing, undocumented internal mechanism, not an architectural decision reversal.

Acceptance Criteria

  • FilterType::Related added; QueryFilter gains relationship_name: Option<String> and filter: Option<Box<QueryFilter>> fields (or equivalent — implementer's call on exact field naming/shape, consistent with existing QueryFilter conventions)
  • to_query_definition/the filter-building path becomes async, resolves relationship_name via resolve_relationship_name, correctly handling: built-in relationships, schema-declared forward names, schema-declared reverse names, and relationships inherited via an extends chain without being redeclared on the descendant (test for each)
  • A Related filter whose nested filter is itself Related (depth > 1) is rejected at validation time with a clear error — not silently truncated or silently executed incorrectly (test)
  • Query execution: "tasks belonging to a project with status active" returns correct results (integration test, mirroring the issue's own motivating example)
  • Query execution against a many-cardinality relationship (e.g. a Jira-style sprint.tasks) correctly behaves as an existence test — "sprints containing at least one task with severity critical" (test)
  • Query execution against a relationship inherited via extends (e.g. a story/epic using a task-declared relationship) resolves and filters correctly (test, direct consumer of ADR-078 correctness)
  • No regression to existing parent/children/mentions/mentioned_by bare-membership filters — confirmed still working exactly as today (regression test)
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • packages/core/src/services/query_service/mod.rs — QueryFilter/FilterType/RelationshipType (~lines 56-130), build_relationship_filter/build_relationship_condition (~lines 652, 784) — the pattern this issue extends
  • packages/core/src/ops/rel_ops.rs — resolve_relationship_name (~line 185), ResolvedRelName, the extends-chain-aware resolver this issue's async filter-building path must call
  • packages/core/src/ops/query_ops.rs — to_query_definition (~line 281) — the sync path becoming async
  • packages/core/src/db/sqlite_store/relationships.rs — MENTION_CONTAINERS_QUERY (~line 170), the collection-subtree WITH RECURSIVE query (~line 977) — precedent for compiling a graph-traversal condition into a single SQL query, relevant if the executor benefits from a similar single-query join rather than nested subqueries (evaluate during implementation)
  • packages/nodespace-types/src/schema.rs:226,308 — EdgeField, SchemaRelationship.edge_fields — the real, implemented edge-property mechanism this issue does NOT filter by (see Non-Goals); confirms the concept exists so a future edge-property-filter issue is not mistaken for novel scope
  • packages/core/src/services/node_service/relationship.rs:2122, db/sqlite_store/relationships.rs:1818 — (node, edge_properties) pair fetch shape, the precedent an edge-property filter follow-on would extend

Dependencies

None — resolve_relationship_name and the extends/ADR-078 machinery it depends on are already implemented (#2598, closed).

Related Issues

Non-Goals

  • Edge/relationship property filters (filtering by data stored on the edge itself, e.g. assigned_at on a person --owner--> project edge, rather than on the related node) — a real, already-implemented data feature (EdgeField/edge_fields, see Reference Files), just not one this issue's filter shape covers. A FilterType::EdgeProperty variant (or folding edge-property comparisons into this issue's Related filter's own condition alongside the node-property one) is a natural, closely-scoped follow-on — flagged here explicitly rather than treated as unneeded, since the underlying storage already exists and this issue's design should not make it awkward to add later.
  • Depth > 1 nesting — the shape supports it structurally (recursive QueryFilter), but this issue validates and enforces exactly one hop; raising the cap is a follow-on, not part of this issue's scope
  • Frontend query-builder UI for constructing a related-node filter visually — this issue is data-layer/query-engine only, matching this repo's standing "schema/query authoring is CLI/agent-driven" convention

Activity

  1. malibio commented on Sep 27, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 281 test files passed, 5441 tests passed (74.45s).

  2. self-assigned this
    on Sep 27, 2026
  3. added 3 commits that reference this issue on Sep 27, 2026
    e0bafce
    641512a
    9b634c7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions