Skip to content

feat: add DAG-based flow model with topological execution#116

Merged
dgenio merged 4 commits intomainfrom
feat/10-dag-based-flow-model
Apr 14, 2026
Merged

feat: add DAG-based flow model with topological execution#116
dgenio merged 4 commits intomainfrom
feat/10-dag-based-flow-model

Conversation

@dgenio
Copy link
Copy Markdown
Owner

@dgenio dgenio commented Apr 13, 2026

Summary

Implements Issue #10: Design and implement a DAG-based flow model with topological execution.

DAGFlow and DAGFlowStep are added as first-class data models alongside the existing linear Flow. The executor gains a unified dispatch path — isinstance(flow, DAGFlow) routes to a new level-by-level runner while the linear path is completely unchanged.


What changed

File Change
chainweaver/flow.py DAGFlowStep (extends FlowStep with step_id, depends_on, step_type, capability_id), DAGFlow, validate_dag_topology()
chainweaver/exceptions.py DAGDefinitionError(flow_name, reason, detail) — reason ∈ {cycle, duplicate_step_id, unknown_dependency}
chainweaver/registry.py AnyFlow = Flow | DAGFlow; topology validated at registration time via validate_dag_topology
chainweaver/executor.py _execute_dag_flow() runs steps level-by-level (topological sort via graphlib.TopologicalSorter); sibling key-conflict detection guards output determinism; _execute_step reused via FlowStep proxy
chainweaver/__init__.py Export DAGFlow, DAGFlowStep, DAGDefinitionError
tests/test_flow_execution.py 9 new DAG test classes: simple chain, single node, diamond, mixed depth, sibling conflict, level schemas, linear backward compat, missing tool, step_type
tests/test_registry.py TestDAGFlowRegistration — 9 tests covering valid registration, coexistence with linear flows, duplicate ID, unknown dep, cycle, self-loop, overwrite, get, match-by-intent
README.md Added DAGDefinitionError to error table; updated v0.2 roadmap checkboxes
AGENTS.md Updated repo map; added DAGFlow/DAGFlowStep row to common-tasks table
docs/agent-context/architecture.md Updated module-boundaries table and decision-context table (DAG execution, step_type/capability_id rationale, registration-time validation)

Why

DAG execution is ChainWeaver's core identity per the weaver-spec ("Deterministic DAG/flow orchestration"). The existing Flow model was linear-only with three # TODO (Phase 2) placeholders in flow.py.

The step_type + capability_id fields are zero-cost forward-compat slots for weaver-spec Invariant I-07 (kernel delegation). Only "tool" is executed today; "capability" is reserved for the future KernelBackedExecutor.


Testing performed

ruff check chainweaver/ tests/ examples/      → All checks passed
ruff format --check chainweaver/ tests/ examples/ → 17 files already formatted
python -m mypy chainweaver/                   → Success: no issues found in 8 source files
python -m pytest tests/ -v                   → 112 passed in 0.33s

Executor invariants preserved

  • No LLM calls in executor.py
  • No network I/O in executor.py
  • No randomness in executor.py

Docs and AI-agent instruction updates

  • README.md — error table + roadmap updated
  • AGENTS.md — repo map + common-tasks table updated
  • docs/agent-context/architecture.md — module boundaries and decision context updated
  • .github/copilot-instructions.md — no change needed (routes to AGENTS.md/docs)
  • .github/instructions/chainweaver.instructions.md — no change needed
  • .github/instructions/tests.instructions.md — no change needed

Checklist

  • All four validation commands pass locally
  • Both success and error paths are tested
  • __init__.py __all__ updated for all new public symbols
  • Linear Flow execution path completely unaffected
  • No new contradictions introduced between docs
  • AGENTS.md updated (architecture changed)
  • One logical change in this PR

Implements Issue #10. Adds DAGFlow and DAGFlowStep as first-class
data models alongside the existing linear Flow, with topological
level-grouped execution and registration-time cycle detection.

Changes:
- flow.py: DAGFlowStep (extends FlowStep with step_id, depends_on,
  step_type, capability_id), DAGFlow, validate_dag_topology()
- exceptions.py: DAGDefinitionError with reason/detail attributes
- registry.py: AnyFlow = Flow | DAGFlow; topology validated on register
- executor.py: dispatch on isinstance(flow, DAGFlow); _execute_dag_flow
  runs levels in order; sibling key-conflict detection guards determinism
- __init__.py: export DAGFlow, DAGFlowStep, DAGDefinitionError
- tests: 9 new DAG test classes (112 total), all pass
- docs: README error table, AGENTS.md repo map, architecture.md updated

step_type and capability_id are forward-compat slots for weaver-spec
Invariant I-07 (kernel delegation); only 'tool' is executed today.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-class DAG support to ChainWeaver flows by introducing new DAG data models, validating DAG topology, and executing DAGs level-by-level while preserving existing linear Flow behavior.

Changes:

  • Introduces DAGFlow / DAGFlowStep models plus validate_dag_topology() for cycle/ID/dep validation.
  • Extends FlowRegistry and FlowExecutor to accept and execute DAG flows (with sibling output key conflict detection).
  • Adds extensive DAG-focused test coverage and updates public docs/API exports.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
chainweaver/flow.py Adds DAG models and topology validation helper.
chainweaver/executor.py Adds DAG dispatch + level-by-level DAG execution path.
chainweaver/registry.py Accepts Flow | DAGFlow and validates DAG topology at registration.
chainweaver/exceptions.py Adds DAGDefinitionError.
chainweaver/__init__.py Exports new DAG symbols and error type.
tests/test_flow_execution.py Adds DAG execution tests and keeps linear regression coverage.
tests/test_registry.py Adds DAG registration/validation tests.
README.md Documents new error and updates roadmap items.
AGENTS.md Updates repo map and task guidance for new DAG fields.
docs/agent-context/architecture.md Updates module boundaries/decision context for DAG design.

Comment thread chainweaver/executor.py
Comment thread chainweaver/executor.py
Comment thread chainweaver/flow.py Outdated
Comment thread chainweaver/flow.py
Comment thread docs/agent-context/architecture.md Outdated
Comment thread chainweaver/executor.py Outdated
dgenio added 3 commits April 14, 2026 06:38
- Fix _compute_dag_levels() to use TopologicalSorter order, preventing
  KeyError when steps are listed in non-dependency order in flow.steps
- Add step_type guard in _execute_dag_flow() that rejects non-tool steps
  with a structured FlowExecutionError (capability reserved for
  KernelBackedExecutor)
- Add Pydantic model_validator on DAGFlowStep: step_type='tool' must have
  capability_id=None (capability steps left unconstrained for future spec)
- Fix DAGFlow docstring: DAGDefinitionError is raised when
  validate_dag_topology() is invoked, not at model-validation time
- Fix _compute_dag_levels() docstring: acknowledge belt-and-suspenders
  validate_dag_topology() call can raise DAGDefinitionError
- Reconcile parallel execution version reference: v0.4 -> v0.2 in
  architecture.md and flow.py DAGFlow docstring (aligned with README)
- Add tests: reverse-ordered steps, capability step rejection,
  tool-step-with-capability_id rejection, capability-without-id acceptance
- Export validate_dag_topology in __init__.py __all__ (invariant 5)
- Update __init__.py docstring to include DAG types in API example
- Update executor TODO: replace completed DAG item with parallel/async
  level execution which is still pending
- Restore two removed flow.py TODOs for conditional branching and
  determinism scoring (still unimplemented)
- Add DAG output schema failure test (TestDAGFlowLevelSchemas)
- Remove dead code: unused _Doubled/_Tripled/_Sum/_Formatted classes
  and unused _make_dag_executor stub in tests
README.md: keep both DAGDefinitionError and FlowBuilderError rows in
the error table (both added independently on each branch).

chainweaver/__init__.py: merge DAG types and validate_dag_topology
(this branch) with FlowBuilder/FlowBuilderError (main); update docstring
example to show both additions.

tests/test_flow_execution.py: use version-portable ZeroDivisionError
assertion from main (detail set matching Python <= 3.13 and 3.14+)
instead of simple string match.
@dgenio dgenio merged commit 1253d23 into main Apr 14, 2026
4 checks passed
@dgenio dgenio deleted the feat/10-dag-based-flow-model branch April 14, 2026 05:55
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.

2 participants