You want to modify the existing function so that when the target is "code_generation", it prefers "TestCodeGenerator" as the node, and if it is not available, falls back to "ScenarioGenerator". Also, the order of nodes in langgraph_states is known:
[DependencyAnalyser->Clarifier->ScenarioGenerator->TestCodeGenerator->CodeRunner]
Currently, your mapping is:
target_nodes = {
"verified_io_types": "DependencyAnalyser",
"clarification_completed": "Clarifier",
"code_generation": "ScenarioGenerator",
"run_pytest_code": "TestCodeGenerator",
"self_correction": "CodeRunner",
}
The main logic is:
- For
"code_generation", try "TestCodeGenerator" first.
- If not found, then try
"ScenarioGenerator".
So, your function needs to check if the step is "code_generation", and then look for TestCodeGenerator first, else ScenarioGenerator.
Here is the modified function:
def find_langgraph_state_for_step(app_manager, step):
"""Find langgraph state node matching the step target node.
Special handling for 'code_generation' target:
- prefer 'TestCodeGenerator' node
- fallback to 'ScenarioGenerator' if not found
"""
# Mapping of other targets
target_nodes = {
"verified_io_types": "DependencyAnalyser",
"clarification_completed": "Clarifier",
"run_pytest_code": "TestCodeGenerator",
"self_correction": "CodeRunner",
}
# For 'code_generation' we have special handling
if step == "code_generation":
preferred_nodes = ["TestCodeGenerator", "ScenarioGenerator"]
else:
node = target_nodes.get(step)
if not node:
return None, None
preferred_nodes = [node]
# We will iterate over langgraph_states from end to start since you pop from the end,
# but to keep the original list intact, we iterate directly without popping.
# Alternatively, to maintain your current logic which pops items, store states popped and restore them later.
# A safe way is to just iterate without popping and return first available matching node.
for node_state in reversed(app_manager.langgraph_states):
node, state = node_state
if node in preferred_nodes:
return node, state
return None, None
Explanation:
- For other steps, it looks up one clearly defined node.
- For
"code_generation", it tries "TestCodeGenerator" first, then "ScenarioGenerator".
- We do not remove nodes from
langgraph_states (no pop). Instead, just iterate backwards since in your original code you were popping from the end.
- Return the first matching node.
If you want to keep the exact popping logic that removes nodes until it finds one, you can do that, but it's better not to modify langgraph_states.
If you want to keep your pop()-based scanning (destructive), here is an adjusted version with the required priority:
def find_langgraph_state_for_step(app_manager, step):
"""Find langgraph state node matching the step target node.
Special handling for 'code_generation' target:
- prefer 'TestCodeGenerator' node
- fallback to 'ScenarioGenerator' if not found
"""
target_nodes = {
"verified_io_types": "DependencyAnalyser",
"clarification_completed": "Clarifier",
"run_pytest_code": "TestCodeGenerator",
"self_correction": "CodeRunner",
}
if step == "code_generation":
preferred_nodes = ["TestCodeGenerator", "ScenarioGenerator"]
else:
node = target_nodes.get(step)
if not node:
return None, None
preferred_nodes = [node]
buffer = []
found = None
while app_manager.langgraph_states:
node, state = app_manager.langgraph_states.pop()
if node in preferred_nodes:
found = (node, state)
break
else:
buffer.append((node, state)) # keep temporarily
# Put back all other nodes to restore list
while buffer:
app_manager.langgraph_states.append(buffer.pop())
if found:
# Re-append found node so state list remains consistent
app_manager.langgraph_states.append(found)
return found
return None, None
This version destructively pops from the end until it finds a node in the preferred list. It stores nodes popped but not a match in a buffer and then puts them back.
It also reinserts the found node to maintain the original list.
Let me know if you want me to clarify or adapt it further.