from __future__ import annotations

import gc
import inspect
import os
import time
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional
from unittest.mock import MagicMock, patch

import dill as pickle
import networkx as nx
import pytest
from inference.core.workflows.errors import StepExecutionError, WorkflowError
from inference.core.workflows.execution_engine.profiling.core import \
    NullWorkflowsProfiler
from inference.core.workflows.execution_engine.v1.compiler.entities import \
    CompiledWorkflow
from inference.core.workflows.execution_engine.v1.executor.core import \
    _run_workflow
from networkx import DiGraph


def codeflash_wrap(codeflash_wrapped, codeflash_test_module_name, codeflash_test_class_name, codeflash_test_name, codeflash_function_name, codeflash_line_id, codeflash_loop_index, *args, **kwargs):
    test_id = f'{codeflash_test_module_name}:{codeflash_test_class_name}:{codeflash_test_name}:{codeflash_line_id}:{codeflash_loop_index}'
    if not hasattr(codeflash_wrap, 'index'):
        codeflash_wrap.index = {}
    if test_id in codeflash_wrap.index:
        codeflash_wrap.index[test_id] += 1
    else:
        codeflash_wrap.index[test_id] = 0
    codeflash_test_index = codeflash_wrap.index[test_id]
    invocation_id = f'{codeflash_line_id}_{codeflash_test_index}'
    test_stdout_tag = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_loop_index}:{invocation_id}"
    print(f'!$######{test_stdout_tag}######$!')
    exception = None
    gc.disable()
    try:
        counter = time.perf_counter_ns()
        return_value = codeflash_wrapped(*args, **kwargs)
        codeflash_duration = time.perf_counter_ns() - counter
    except Exception as e:
        codeflash_duration = time.perf_counter_ns() - counter
        exception = e
    gc.enable()
    print(f'!######{test_stdout_tag}######!')
    iteration = os.environ['CODEFLASH_TEST_ITERATION']
    with Path('{codeflash_run_tmp_dir_client_side}', f'test_return_values_{iteration}.bin').open('ab') as f:
        pickled_values = pickle.dumps((args, kwargs, exception)) if exception else pickle.dumps((args, kwargs, return_value))
        _test_name = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_line_id}".encode('ascii')
        f.write(len(_test_name).to_bytes(4, byteorder='big'))
        f.write(_test_name)
        f.write(codeflash_duration.to_bytes(8, byteorder='big'))
        f.write(len(pickled_values).to_bytes(4, byteorder='big'))
        f.write(pickled_values)
        f.write(codeflash_loop_index.to_bytes(8, byteorder='big'))
        f.write(len(invocation_id).to_bytes(4, byteorder='big'))
        f.write(invocation_id.encode('ascii'))
    if exception:
        raise exception
    return return_value

def test_run_workflow_returns_list_of_dicts():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test that _run_workflow returns a list of dictionaries.\n    This is the primary contract of the function.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    runtime_parameters = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters=runtime_parameters, max_concurrent_steps=1, kinds_serializers=None, serialize_results=False)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_returns_list_of_dicts', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for item in result:
        pass

def test_run_workflow_with_empty_workflow():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with minimal workflow configuration.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_empty_workflow', '_run_workflow', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_runtime_parameters():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test that runtime_parameters are properly passed through.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    runtime_parameters = {'param1': 'value1', 'param2': 42, 'param3': [1, 2, 3]}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters=runtime_parameters, max_concurrent_steps=1, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_runtime_parameters', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_serialize_results_false():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with serialize_results=False.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=None, serialize_results=False)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_serialize_results_false', '_run_workflow', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_serialize_results_true():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with serialize_results=True.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    kinds_serializers = {'default': lambda x: x}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=kinds_serializers, serialize_results=True)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_serialize_results_true', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_null_profiler():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow explicitly with NullWorkflowsProfiler.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    profiler = NullWorkflowsProfiler.init()
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=None, profiler=profiler)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_null_profiler', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_explicit_executor():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with an explicitly provided ThreadPoolExecutor.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    with ThreadPoolExecutor(max_workers=2) as executor:
        _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=2, kinds_serializers=None, executor=executor)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_explicit_executor', '_run_workflow', '6_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_run_workflow_with_step_error_handler():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with a custom step error handler.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    error_handler_called = []

    def error_handler(exc: Exception) -> None:
        error_handler_called.append(exc)
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=None, step_error_handler=error_handler)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_step_error_handler', '_run_workflow', '8', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_max_concurrent_steps_various_values():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with different max_concurrent_steps values.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    for max_workers in [1, 2, 4, 8]:
        _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=max_workers, kinds_serializers=None)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_max_concurrent_steps_various_values', '_run_workflow', '6_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_run_workflow_with_none_kinds_serializers():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with kinds_serializers=None (default case).\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=None, serialize_results=False)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_none_kinds_serializers', '_run_workflow', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_empty_kinds_serializers():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with empty kinds_serializers dictionary.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers={}, serialize_results=False)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_empty_kinds_serializers', '_run_workflow', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_none_profiler():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with profiler=None (default).\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=None, profiler=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_none_profiler', '_run_workflow', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_none_executor():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with executor=None (creates internal executor).\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=None, executor=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_none_executor', '_run_workflow', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_none_step_error_handler():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with step_error_handler=None (default).\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=None, step_error_handler=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_none_step_error_handler', '_run_workflow', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_complex_runtime_parameters():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with complex nested runtime parameters.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    runtime_parameters = {'nested': {'level1': {'level2': [1, 2, 3]}}, 'lists': [[1, 2], [3, 4], [5, 6]], 'mixed': [1, 'string', 3.14, None, True]}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters=runtime_parameters, max_concurrent_steps=1, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_complex_runtime_parameters', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_multiple_kinds_serializers():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with multiple kinds_serializers entries.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    kinds_serializers = {'image': lambda x: f'image_{x}', 'detection': lambda x: f'detection_{x}', 'segmentation': lambda x: f'segmentation_{x}'}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=kinds_serializers, serialize_results=False)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_multiple_kinds_serializers', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_max_concurrent_steps_one():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with max_concurrent_steps=1 (sequential execution).\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_max_concurrent_steps_one', '_run_workflow', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_high_max_concurrent_steps():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with very high max_concurrent_steps value.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=256, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_high_max_concurrent_steps', '_run_workflow', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_special_characters_in_parameters():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with special characters in runtime parameters.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    runtime_parameters = {'special_chars': '!@#$%^&*()', 'unicode': 'こんにちは世界', 'symbols': '™®©', 'quotes': 'He said "hello"', 'newlines': 'line1\nline2\nline3'}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters=runtime_parameters, max_concurrent_steps=1, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_special_characters_in_parameters', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_callable_error_handler():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with a callable step_error_handler.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    handler_calls = []

    def custom_error_handler(exc: Exception) -> None:
        handler_calls.append({'exception': exc, 'type': type(exc).__name__})
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=1, kinds_serializers=None, step_error_handler=custom_error_handler)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_callable_error_handler', '_run_workflow', '8', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_large_runtime_parameters():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with a large number of runtime parameters.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    runtime_parameters = {f'param_{i}': i for i in range(1000)}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters=runtime_parameters, max_concurrent_steps=4, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_large_runtime_parameters', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_large_list_parameters():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with large list values in runtime parameters.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    runtime_parameters = {'large_list': list(range(1000)), 'nested_lists': [list(range(100)) for _ in range(10)]}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters=runtime_parameters, max_concurrent_steps=4, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_large_list_parameters', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_many_kinds_serializers():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with a large number of kinds_serializers.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    kinds_serializers = {f'kind_{i}': lambda x, i=i: f'serialized_{i}_{x}' for i in range(500)}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=4, kinds_serializers=kinds_serializers, serialize_results=True)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_many_kinds_serializers', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_large_execution_graph():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with a large execution graph (1000 nodes).\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    graph = nx.DiGraph()
    for i in range(1000):
        graph.add_node(f'node_{i}')
    for i in range(999):
        graph.add_edge(f'node_{i}', f'node_{i + 1}')
    workflow.execution_graph = graph
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=4, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_large_execution_graph', '_run_workflow', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_multiple_iterations():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow called multiple times in sequence.\n    '
    for iteration in range(100):
        workflow = MagicMock(spec=CompiledWorkflow)
        workflow.execution_graph = nx.DiGraph()
        workflow.workflow_definition = MagicMock()
        workflow.workflow_definition.outputs = []
        workflow.steps = {}
        _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={'iteration': iteration}, max_concurrent_steps=2, kinds_serializers=None)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_multiple_iterations', '_run_workflow', '1_5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_run_workflow_with_large_nested_structures():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with deeply nested data structures.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    nested = {'level': 0}
    current = nested
    for i in range(1, 100):
        current['child'] = {'level': i}
        current = current['child']
    runtime_parameters = {'deep_nested': nested}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters=runtime_parameters, max_concurrent_steps=4, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_large_nested_structures', '_run_workflow', '10', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_run_workflow_with_varying_concurrency_levels():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Test _run_workflow with various concurrency levels in a loop.\n    '
    for max_concurrent in range(1, 11):
        workflow = MagicMock(spec=CompiledWorkflow)
        workflow.execution_graph = nx.DiGraph()
        workflow.workflow_definition = MagicMock()
        workflow.workflow_definition.outputs = []
        workflow.steps = {}
        _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters={}, max_concurrent_steps=max_concurrent, kinds_serializers=None)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_with_varying_concurrency_levels', '_run_workflow', '1_5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_run_workflow_stress_test_parameters():
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    '\n    Stress test with 500 parameters each containing large data.\n    '
    workflow = MagicMock(spec=CompiledWorkflow)
    workflow.execution_graph = nx.DiGraph()
    workflow.workflow_definition = MagicMock()
    workflow.workflow_definition.outputs = []
    workflow.steps = {}
    runtime_parameters = {f'param_{i}': {'data': list(range(100)), 'nested': {'value': i * 100}, 'strings': [f'str_{j}' for j in range(50)]} for i in range(500)}
    _call__bound__arguments = inspect.signature(_run_workflow).bind(workflow=workflow, runtime_parameters=runtime_parameters, max_concurrent_steps=8, kinds_serializers=None)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(_run_workflow, 'tests.inference.unit_tests.test__run_workflow__unit_test_1', None, 'test_run_workflow_stress_test_parameters', '_run_workflow', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output