Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:173TECH Sayn dag utils

From Leeroopedia
Knowledge Sources
Domains Graph_Theory, Algorithm
Last Updated 2026-10-17 00:00 GMT

Overview

Concrete tool for validating and querying task dependency graphs using cycle detection, topological sorting, and upstream/downstream traversal provided by the SAYN framework.

Description

The sayn/utils/dag.py module provides the core DAG operations used throughout SAYN:

  • dag_is_valid: Checks for missing parents and cycles
  • topological_sort: Returns tasks in dependency order
  • upstream: Returns all ancestors of a node
  • downstream: Returns all descendants of a node
  • query: Filters the DAG by include/exclude operations with upstream/downstream expansion

Usage

Import these functions when you need to validate or query a task dependency graph. Used internally by App.set_tasks for DAG validation and by App.setup_execution for task query resolution.

Code Reference

Source Location

  • Repository: SAYN
  • File: sayn/utils/dag.py
  • Lines: L6-14 (_has_missing_parents), L17-47 (_is_cyclic), L50-58 (dag_is_valid), L62-83 (topological_sort), L87-100 (upstream/downstream), L103-142 (query)

Signature

def dag_is_valid(dag: Dict[str, List[str]]) -> Result
def topological_sort(dag: Dict[str, List[str]]) -> Result[List[str]]
def upstream(dag: Dict[str, List[str]], node: str) -> Result[List[str]]
def downstream(dag: Dict[str, List[str]], node: str) -> Result[List[str]]
def query(dag: Dict[str, List[str]], query: List[Dict] = list()) -> Result[List[str]]

Import

from sayn.utils.dag import dag_is_valid, topological_sort, upstream, downstream, query

I/O Contract

Inputs

Name Type Required Description
dag Dict[str, List[str]] Yes Mapping of task name to list of parent names
node str Yes (for upstream/downstream) Task name to traverse from
query List[Dict] No List of include/exclude operations

Outputs

Name Type Description
topological_sort result Result[List[str]] Tasks in execution order
upstream result Result[List[str]] All ancestor task names
query result Result[List[str]] Filtered task list in topo order

Usage Examples

Validating and Sorting a DAG

from sayn.utils.dag import dag_is_valid, topological_sort

dag = {
    "load_users": [],
    "load_orders": [],
    "model_users": ["load_users"],
    "model_orders": ["load_orders", "model_users"],
}

# Validate
result = dag_is_valid(dag)
assert result.is_ok

# Get execution order
sort_result = topological_sort(dag)
if sort_result.is_ok:
    print(sort_result.value)
    # ['load_users', 'load_orders', 'model_users', 'model_orders']

Querying with Include/Exclude

from sayn.utils.dag import query

# Include only model_orders and its upstream
task_query = [
    {"operation": "include", "task": "model_orders", "upstream": True, "downstream": False}
]
result = query(dag, task_query)
# Returns: ['load_users', 'load_orders', 'model_users', 'model_orders']

# Exclude model_users and its downstream
task_query = [
    {"operation": "exclude", "task": "model_users", "upstream": False, "downstream": True}
]
result = query(dag, task_query)
# Returns: ['load_users', 'load_orders']

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment