Skip to content

Share one RelationshipPath compiled to CTE SQL between queries and plays, and type play rules #3449

Closed

Description

@malibio

Overview

Introduce one typed RelationshipPath, compiled to CTE SQL, shared by query relationship filters, play for_each and CEL dot-paths. Let plays select nodes the way queries do. Type play rules, including per-action parameters (ADR-086 §11, §4 "Structured").

Problem Statement

  • Plays walk the graph one hop at a time. GraphResolver (packages/core/src/playbook/graph_resolver.rs) calls get_related_nodes for each hop, per node. A scheduled rule over N nodes with a k-hop path makes about N×k round trips.
  • Query has its own model. Query relationship filters (QueryFilter with RelationshipType in packages/nodespace-types/src/query.rs) cover only parent/children/mentions/mentioned_by plus related, while play also follows schema-declared relationships and reverse names.
  • Scheduled play triggers scan every node of a type and filter in CEL, rather than using query's typed filters and SQL.
  • Play rules are stringly typed and live only in core.
    • RuleDefinition, TriggerDefinition and ActionDefinition are in packages/core/src/playbook/types.rs, not nodespace-types.
    • trigger_type, on and action_type are String.
    • Action params is open JSON, so a play using parameter names the engine never reads saves without error.

Proposed Solution

  1. RelationshipPath in nodespace-types: a list of hops, each a relationship name (built-in, schema-declared or reverse) and a direction, optionally open-ended. It is resolved against the schemas when saved, so every name becomes a concrete relationship_type and direction.
  2. One store method compiles and runs a path:
    • one correlated subquery per hop (never a JOIN; the JOIN form measured about 50× slower);
    • WITH RECURSIVE for open-ended walks;
    • results keyed by (root id, path);
    • batched across all the roots of a scan.
      It serves query relationship filters, play for_each, and CEL dot-paths (resolved before CEL evaluation, as today).
  3. Delete GraphResolver's per-hop walking, keeping its root-scoped result keying.
  4. Play rules move to nodespace-types as typed wire types: a tagged Trigger enum, a tagged Action enum with typed per-action params (deny_unknown_fields), and open JSON only for the properties and edge_data being written. A play's scheduled selector is a QueryFields value or a reference to a saved query node. CEL stays for conditions.
  5. Wire it end to end: a typed PlayNode and update through the shared pipeline, with generated TypeScript.
  6. No migration: reset the database. Update the bundled plays and docs to the typed params.

Architecture Impact

Implements decisions/086-core-node-types-defined-once-typed-by-category.md §11 and §4 for play and query (nodespace-docs). Update architecture/playbook-system.md (teach the real param names), architecture/playbook-engine.md and components/query-node.md in the same PR. Keep the tests that pin the (root, path) keying and the correlated-subquery form.

Acceptance Criteria

  • RelationshipPath exists in nodespace-types; query filters, play for_each and CEL dot-paths all use it (tests)
  • A path compiles to one SQL statement (correlated subqueries per hop; WITH RECURSIVE for open-ended walks), and a scheduled scan resolves a path for all roots in one statement (tests, including a query-plan assertion)
  • Results are keyed by (root id, path); one root's answer is never returned for another (test)
  • GraphResolver no longer calls get_related_nodes per hop
  • Play rules are typed wire types; an unknown action param is rejected when the play is saved (test per action)
  • A scheduled trigger can select with QueryFields or a saved query id (tests)
  • The bundled playbooks and core plays use the typed params
  • Follows development/node-type-sequence.md; the node-types.md play and query sheets updated in the same PR
  • bun run test:changed passes; bun run quality:fix clean

Technical Specifications

Reference Files

  • packages/core/src/playbook/graph_resolver.rs, types.rs, validation.rs, cel.rs, cron_runner.rs, actions.rs, engine.rs
  • packages/nodespace-types/src/query.rs, packages/core/src/ops/query_ops.rs
  • packages/core/src/db/sqlite_store/relationships.rs, packages/core/src/db/sqlite_store/nodes.rs (existing recursive CTEs)
  • packages/core/src/playbook/core_plays.rs, packages/core/src/methodology/ (bundled plays)

Dependencies

Related Issues

Non-Goals

  • Changing CEL as the condition language

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions