Skip to content

feat(dynaflow): code ⇄ visual round-trip (import/export graph as code) (#292) - #356

Merged
Kaap10 merged 2 commits into
codeforstartups:developmentfrom
shivamm-gupta:issue-292-code-visual-roundtrip
Nov 23, 2026
Merged

Kaap10 merged 2 commits into
codeforstartups:developmentfrom
shivamm-gupta:issue-292-code-visual-roundtrip

Conversation

@shivamm-gupta

@shivamm-gupta shivamm-gupta commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Introduces bidirectional code ⇄ visual round-trip capabilities in dynaflow, allowing SimpleGraph instances to be exported to runnable Python SDK code and imported safely back via static AST parsing without arbitrary code execution (no eval/exec), addressing issue #292.

Related issue

Fixes #292

Changes

  • Implemented export_graph_to_code in src/dynavec/nodes/codegen.py:
    • Generates clean, runnable Python SDK code instantiating nodes, configs, retry policies, and wiring edges.
    • Adds helpful comments for custom node registration requirements.
    • Relies on execution-ordered node additions to establish execution sequence without mutating private attributes.
  • Implemented import_graph_from_code with safe static AST analysis:
    • Resolves variables defined in script assignments for configs, retry policies, and port mappings.
    • Preserves existing custom node registrations in the registry rather than overwriting with dummy stubs.
    • Supports node instances, edge mappings, and error edges.
  • Added convenience methods SimpleGraph.to_code() and SimpleGraph.from_code(code_str).
  • Added comprehensive unit tests in tests/test_nodes_roundtrip.py validating lossless round-trips, multi-node pipelines, re-execution of reconstructed graphs, variable resolution, registry preservation, and AST safety.

Testing

  • Tests pass locally (pytest tests/test_nodes_roundtrip.py tests/test_nodes.py -> 48 passed)
  • Ruff checks pass (ruff check src/dynavec/nodes/codegen.py tests/test_nodes_roundtrip.py -> All checks passed)
  • Mypy checks pass (mypy src/dynavec/nodes/codegen.py -> Success: no issues found)
  • Documentation updated, if applicable

Checklist

  • My changes are focused and relevant to this pull request.
  • I have added or updated tests where appropriate.
  • I have reviewed my changes for unrelated modifications.
  • I have updated documentation where necessary.

@Kaap10 Kaap10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for working on this! The bidirectional code ⇄ visual round-trip is well-structured, and using Python's ast module for safe parsing without eval/exec is great for security.

Before we can merge, please address the following issues:

  1. Global Registry Overwrite Side-Effect: In import_graph_from_code, when encountering @custom_node, it registers a dummy function returning {} with register=True into the global node_registry. If that node type was already registered in the application, this overwrites the real function with a dummy stub.
  2. Variable References in AST Parsing: import_graph_from_code only supports inline dictionary/call literals for config, mapping, and retry_policy. If a user writes cfg = {"temp": 0.5}; LLMNode(..., config=cfg) or defines policy = RetryPolicy(...) as a variable, parsing fails with a ValueError or drops the policy.
  3. Private Attribute Assignment: export_graph_to_code outputs graph._execution_order = [...]. Generated code should avoid directly mutating private attributes. Calling graph.add_node(...) in order already sets this naturally.
  4. Exporting Custom Nodes: Exporting a graph with custom nodes emits node_registry.get("type")(...). The generated code is not runnable on its own unless the custom node is pre-registered. Please document this requirement or add a comment in the generated code.
  5. PR Description Formatting: The PR description has escaped backslashes (e.g. \export_graph_to_code...\, \eval()\/ \exec()\) and an unprintable control character (\^Gst.parse\). Please format it according to .github/pull_request_template.md.

See the inline comments below for specific line references and suggested fixes.

Comment thread src/dynavec/nodes/codegen.py Resolved
Comment thread src/dynavec/nodes/codegen.py Resolved
Comment thread src/dynavec/nodes/codegen.py Resolved
Comment thread src/dynavec/nodes/codegen.py Resolved
Comment thread src/dynavec/nodes/codegen.py Resolved
@shivamm-gupta

Copy link
Copy Markdown
Collaborator Author

Thanks @Kaap10 for the detailed review! All comments have been resolved:

  1. Registry preservation: import_graph_from_code checks if reg.has(node_type) and reuses the existing registered class rather than overwriting it with a dummy stub.
  2. Variable resolution in AST: Supported local variable tracking during �st.Assign so variables used for config,
    etry_policy, and mapping are safely resolved without needing to be inline literals.
  3. Encapsulation: Removed direct mutation of graph._execution_order in export_graph_to_code. The execution order is naturally established by emitting graph.add_node() calls in sequence.
  4. Custom node export note: Added a generated comment above custom node definitions reminding users that custom node types must be registered before script execution.
  5. PR description format: Reformatted according to .github/pull_request_template.md without escaped backslashes.

@shivamm-gupta shivamm-gupta self-assigned this Nov 22, 2026
@shivamm-gupta
shivamm-gupta requested a review from Kaap10 November 22, 2026 13:32

@Kaap10 Kaap10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM! Thanks for addressing the feedback on AST variable resolution, registry safety, and encapsulation. Approved and ready to merge.

@Kaap10
Kaap10 merged commit 0bb69d6 into codeforstartups:development Nov 23, 2026
4 checks passed
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.

Code ⇄ visual round-trip (import/export graph as code)

2 participants