-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.py
More file actions
114 lines (90 loc) · 3.59 KB
/
Copy pathgraph.py
File metadata and controls
114 lines (90 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""LangGraph workflow definition for the review roadmap agent.
This module constructs the directed graph that orchestrates the multi-step
analysis process for generating PR review roadmaps.
"""
from langgraph.graph import StateGraph, END
from langgraph.graph.state import CompiledStateGraph
from review_roadmap.agent.state import ReviewState
from review_roadmap.agent.nodes import (
analyze_structure,
context_expansion,
draft_roadmap,
reflect_on_roadmap,
)
from review_roadmap.agent.prompts import MAX_REFLECTION_ITERATIONS
from review_roadmap.logging import get_logger
logger = get_logger(__name__)
def _should_reflect(state: ReviewState) -> str:
"""Determine whether to run reflection or skip to end.
Args:
state: Current workflow state.
Returns:
'reflect' to run reflection, 'end' to skip.
"""
if state.skip_reflection:
logger.info("reflection_skipped", reason="disabled by user")
return "end"
return "reflect"
def _after_reflection(state: ReviewState) -> str:
"""Determine whether to retry roadmap generation or finish.
Args:
state: Current workflow state with reflection results.
Returns:
'retry' to regenerate roadmap, 'end' to finish.
"""
if state.reflection_passed:
return "end"
if state.reflection_iterations >= MAX_REFLECTION_ITERATIONS:
logger.warning("max_reflection_iterations_reached",
iterations=state.reflection_iterations)
return "end"
return "retry"
def build_graph() -> CompiledStateGraph:
"""Build and compile the LangGraph workflow for review roadmap generation.
The workflow consists of four nodes with conditional routing:
1. analyze_structure: Groups changed files into logical components
2. context_expansion: Optionally fetches additional file content for context
3. draft_roadmap: Generates the final Markdown roadmap
4. reflect_on_roadmap: Self-reviews the roadmap and may trigger a retry
The reflection step can be skipped by setting skip_reflection=True in the
initial state. If reflection fails, the workflow loops back to draft_roadmap
with feedback, up to MAX_REFLECTION_ITERATIONS times.
Returns:
A compiled LangGraph that can be invoked with a ReviewState containing
the PR context.
Example:
>>> graph = build_graph()
>>> result = graph.invoke({"pr_context": pr_context})
>>> roadmap = result["roadmap"]
# Skip reflection:
>>> result = graph.invoke({"pr_context": pr_context, "skip_reflection": True})
"""
workflow = StateGraph(ReviewState)
# Add Nodes
workflow.add_node("analyze_structure", analyze_structure)
workflow.add_node("context_expansion", context_expansion)
workflow.add_node("draft_roadmap", draft_roadmap)
workflow.add_node("reflect_on_roadmap", reflect_on_roadmap)
# Define Edges
workflow.set_entry_point("analyze_structure")
workflow.add_edge("analyze_structure", "context_expansion")
workflow.add_edge("context_expansion", "draft_roadmap")
# After draft_roadmap, decide whether to reflect or skip
workflow.add_conditional_edges(
"draft_roadmap",
_should_reflect,
{
"reflect": "reflect_on_roadmap",
"end": END,
}
)
# After reflection, decide whether to retry or finish
workflow.add_conditional_edges(
"reflect_on_roadmap",
_after_reflection,
{
"retry": "draft_roadmap",
"end": END,
}
)
return workflow.compile()