# Graph Actor - Test-Driven Development Workflow
# Demonstrates a multi-node graph with conditional routing and subgraphs

name: workflows/test_driven_dev
type: graph
description: Test-driven development workflow with automated testing and feedback loops
version: "1.0"

# LLM model for agent nodes
provider: openai
model: gpt-4

# Graph topology
route:
  # Define all nodes in the workflow
  nodes:
    # Entry point: planning agent
    - id: planner
      type: agent
      name: Test Planner
      description: Plans test cases based on requirements
      config:
        model: gpt-4
        prompt: |
          You are a test planning expert. Analyze the requirements and create
          a comprehensive test plan covering:
          - Unit tests for individual functions
          - Integration tests for component interactions
          - Edge cases and error conditions
        tools:
          - files/read_file
          - files/list_directory

    # Write tests first
    - id: test_writer
      type: agent
      name: Test Writer
      description: Writes test cases based on the plan
      config:
        model: gpt-4
        prompt: |
          You are a test writing expert. Write pytest tests based on the plan.
          Follow best practices:
          - Use descriptive test names
          - Include docstrings
          - Use fixtures appropriately
          - Test one thing per test
        tools:
          - files/write_file
          - files/read_file

    # Run the tests (should fail initially)
    - id: run_tests
      type: tool
      name: Test Runner
      description: Executes pytest test suite
      config:
        tool_name: testing/run_pytest
        parameters:
          verbose: true
          coverage: true

    # Check test results
    - id: check_results
      type: conditional
      name: Test Result Checker
      description: Routes based on test pass/fail status
      config:
        conditions:
          - check: "state.get('tests_passed') == True"
            route_to: code_review
          - check: "state.get('tests_passed') == False"
            route_to: implementation_writer

    # Write implementation to make tests pass
    - id: implementation_writer
      type: agent
      name: Implementation Writer
      description: Writes code to make the tests pass
      config:
        model: gpt-4
        prompt: |
          You are an implementation expert. Write clean, well-documented code
          that makes the failing tests pass. Follow SOLID principles and
          write maintainable code.
        tools:
          - files/write_file
          - files/read_file

    # Run tests again after implementation
    - id: rerun_tests
      type: tool
      name: Test Rerunner
      description: Re-executes tests after implementation
      config:
        tool_name: testing/run_pytest
        parameters:
          verbose: true
          coverage: true

    # Check if tests pass now
    - id: verify_tests
      type: conditional
      name: Test Verification
      description: Verify tests pass after implementation
      config:
        conditions:
          - check: "state.get('tests_passed') == True"
            route_to: code_review
          - check: "state.get('tests_passed') == False and state.get('retry_count', 0) < 3"
            route_to: debug_failures
          - check: "state.get('tests_passed') == False and state.get('retry_count', 0) >= 3"
            route_to: escalate

    # Debug test failures
    - id: debug_failures
      type: agent
      name: Debugger
      description: Analyzes and fixes test failures
      config:
        model: gpt-4
        prompt: |
          You are a debugging expert. Analyze the test failures and fix the
          implementation. Look for:
          - Logic errors
          - Edge cases
          - Type mismatches
          - Missing error handling
        tools:
          - files/read_file
          - files/write_file

    # Code review (subgraph)
    - id: code_review
      type: subgraph
      name: Code Reviewer
      description: Runs code review workflow
      actor_ref: local/code-reviewer

    # Escalate if tests keep failing
    - id: escalate
      type: tool
      name: Escalation Handler
      description: Escalates persistent failures to human
      config:
        tool_name: notifications/send_alert
        parameters:
          channel: engineering
          priority: high

  # Define edges (workflow transitions)
  edges:
    # Linear flow from planner to test writer
    - from_node: planner
      to_node: test_writer

    # Run tests after writing them
    - from_node: test_writer
      to_node: run_tests

    # Check results after running tests
    - from_node: run_tests
      to_node: check_results

    # Conditional routing from check_results
    # (handled by the conditional node itself)

    # Write implementation if tests fail
    - from_node: implementation_writer
      to_node: rerun_tests

    # Verify tests after rerunning
    - from_node: rerun_tests
      to_node: verify_tests

    # Debug if tests still fail
    - from_node: debug_failures
      to_node: rerun_tests

    # All paths eventually lead to code review or escalation
    # (handled by conditional nodes)

  # Entry and exit points
  entry_node: planner
  exit_nodes:
    - code_review
    - escalate

# Context settings
context_view: strategist
memory:
  enabled: true
  max_messages: 100
  max_tokens: 16000
  summarize_old: true

context:
  include_files:
    - "README.md"
    - "requirements.txt"
    - "pyproject.toml"
  include_dirs:
    - "src/"
    - "tests/"
  exclude_patterns:
    - "**/__pycache__/**"
    - "*.pyc"
    - "**/.pytest_cache/**"
    - "**/htmlcov/**"
  max_context_tokens: 32000

# Environment variables
env_vars:
  PYTEST_ARGS: --verbose --cov --cov-report=html
  MAX_RETRIES: "3"
