Skip to content

Playbook Engine: Graph Traversal, NodeCollection, and Path Validation #1010

Closed

Description

@malibio

Overview

Implement graph traversal for the Playbook Engine's CEL evaluator and action binding resolver. This enables dot-path expressions like node.story.epic.status to walk relationships during condition evaluation and action execution. Also adds NodeCollection operators and schema-aware path validation.

Parent issue: #995 (Playbook Engine Architecture — Phases 0-7 complete)
Architecture spec: playbook-engine.md Section 3 — CEL Evaluation


Background

Issue #995 implemented the full playbook engine pipeline (Phases 0-7), but graph traversal was deferred because cel-interpreter's Program::execute() is synchronous while NodeService is async. Architectural review (by senior-architect-reviewer agent) determined:

  • cel-interpreter's Value::member() is hardcoded — no custom resolver hook exists
  • CEL Value trees must be pre-populated before evaluation, not lazily resolved
  • The recommended approach: parse CEL ASTs to extract dot-paths → resolve against graph → build nested CEL Maps → evaluate
  • Sync bridging via tokio::task::block_in_place in a dedicated GraphResolver (NOT on NodeService)
  • The path extractor is the shared foundation for three features: save-time validation, runtime resolution, and schema drift detection

Implementation Plan

Phase A: Path Extractor (foundation)

  • path_extractor.rs — Walk cel-parser AST to extract dot-path references from compiled CEL programs
    • Extract flat paths: node.story.epic.status → ["node", "story", "epic", "status"]
    • Extract sub-expression paths inside collection operators: node.tasks.any(item.status == "done") → collection path ["node", "tasks"] + item path ["item", "status"]
    • Handle nested collection paths recursively
  • Write tests: path extraction from various CEL expression patterns

Phase B: GraphResolver (runtime resolution)

  • graph_resolver.rs — Resolves dot-paths by walking the data graph via NodeService
    • GraphResolver::new(node_service: Arc<NodeService>) — created per rule evaluation
    • resolve_path(root_node, segments) -> ResolvedValue — walks segments, property-first then relationship fallback
    • Sync bridge via tokio::task::block_in_place + Handle::current().block_on() (safe: multi-threaded runtime, single RuleProcessor task)
    • Segment cache: HashMap<Vec<String>, ResolvedValue> — prevents redundant DB queries for overlapping paths across conditions in the same rule
    • ResolvedValue enum: Node(Node), Collection(Vec<Node>), Scalar(Value)
  • Enrich CEL context — build_condition_context injects resolved paths as nested Value::Map structures
  • Update evaluate_conditions in cel.rs to accept &mut GraphResolver and pre-resolve paths before evaluation
  • Update engine.rs RuleProcessor — create GraphResolver per work item, pass to CEL evaluation
  • Write tests: single-hop resolution, multi-hop chains, property vs. relationship precedence, segment cache hit/miss, missing path → false

Phase C: NodeCollection operators

  • NodeCollection — resolved as Value::List of node Maps (pre-fetched by GraphResolver)
  • Register custom CEL functions:
    • .count — property on List, returns integer
    • .any(expr) — returns bool (true if any item matches)
    • .all(expr) — returns bool (true if ALL items match; empty → false per spec)
    • .none(expr) — returns bool (true if no items match)
    • .where(expr) — returns filtered List (chainable)
  • Sub-expression resolution — paths inside .any()/.where() lambdas are extracted by path_extractor, resolved in batch for all collection items
  • Empty collection → false (deliberate departure from vacuous truth per spec)
  • Write tests: collection operators, empty collections, chained .where().any(), nested relationship paths in sub-expressions

Phase D: Action binding graph traversal

  • Update BindingContext in actions.rs — {trigger.node.story.epic.title} resolves via GraphResolver
  • Live state resolution — bindings resolve against current graph (not snapshot), so action 0's create_node is visible to action 1's binding
  • for_each collection resolution — for_each: "trigger.node.tasks" uses GraphResolver to fetch the collection
  • Write tests: multi-hop binding resolution, live state visibility across actions, for_each with graph-resolved collections

Phase E: Schema-aware path validation (save-time + drift)

  • Schema-level path validation in validation.rs — reuse path_extractor, walk the schema graph (not data graph) to verify every segment resolves to a field or relationship
    • node.status → "status" is a field on trigger's schema ✓
    • node.story → "story" is a relationship on trigger's schema ✓
    • node.story.epic → "epic" is a relationship on story's target schema ✓
    • node.story.epic.status → "status" is a field on epic's target schema ✓
  • Schema drift with path awareness — upgrade handle_schema_update from "does playbook reference this node_type?" to "do any paths still resolve after the schema change?"
    • When a schema changes: re-extract paths from all active playbooks, re-validate against updated schema graph
    • Broken path → disable playbook + log node explaining which path broke and why
  • Write tests: valid paths pass, broken intermediate relationship fails, removed field fails, schema update triggers re-validation

Phase F: get_workflow_state MCP tool

  • MCP handler — shares CEL evaluation infrastructure (condition results without action execution)
  • Returns: which conditions passed/failed for a given node against active playbook rules
  • Write tests: workflow state query, condition evaluation results

Architecture Decisions

Decision Choice Why
Sync bridging block_in_place in GraphResolver cel-interpreter is sync; multi-threaded runtime makes this safe
Sync methods on NodeService? No — sync bridge lives in GraphResolver only NodeService stays purely async; sync bridging is a playbook-engine concern
Path extraction Walk cel-parser AST Required by cel-interpreter's non-extensible Value::member()
Collection representation Value::List of node Maps Pre-fetched by GraphResolver; collection operators as custom CEL functions
Path validation target Schema graph (save-time), Data graph (runtime) Same path extractor, different resolution backends

Files to create/modify

New files:

  • packages/core/src/playbook/path_extractor.rs
  • packages/core/src/playbook/graph_resolver.rs

Modified files:

  • packages/core/src/playbook/cel.rs — evaluate_conditions gains GraphResolver; enriched context building
  • packages/core/src/playbook/engine.rs — RuleProcessor creates GraphResolver per work item
  • packages/core/src/playbook/actions.rs — BindingContext uses GraphResolver for dot-path resolution
  • packages/core/src/playbook/validation.rs — schema-level path validation
  • packages/core/src/playbook/lifecycle.rs — handle_schema_update uses path-aware drift detection
  • packages/core/src/playbook/mod.rs — add new modules
  • packages/core/src/mcp/handlers/ — new get_workflow_state handler

References

🤖 Generated with Claude Code

Activity

  1. malibio commented on Apr 2, 2026

    @malibio
    CollaboratorAuthor

    Test Baseline (Issue #1010)\n\nFrontend tests: 119 files, 3748 tests all passing\nDuration: 38.42s\nBranch: main (e9c9575)\n\nStarting implementation.

  2. self-assigned this
    on Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions