|
| 1 | +"""Normalized Not Verified Bug Subgraph |
| 2 | +
|
| 3 | +This module implements a simplified enhanced issue not verified bug subgraph |
| 4 | +with patch normalization and deduplication, using standard final patch selection. |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +import threading |
| 9 | +from typing import Optional, Sequence, Mapping |
| 10 | + |
| 11 | +import neo4j |
| 12 | +from langchain_core.language_models import BaseChatModel |
| 13 | +from langgraph.graph import StateGraph, END |
| 14 | + |
| 15 | +from prometheus.knowledge_graph.knowledge_graph import KnowledgeGraph |
| 16 | +from prometheus.lang_graph.graphs.issue_state import IssueNotVerifiedBugState |
| 17 | +from prometheus.lang_graph.nodes.context_retrieval_subgraph_node import ContextRetrievalSubgraphNode |
| 18 | +from prometheus.lang_graph.nodes.edit_message_node import EditMessageNode |
| 19 | +from prometheus.lang_graph.nodes.edit_node import EditNode |
| 20 | +from prometheus.lang_graph.nodes.git_diff_node import GitDiffNode |
| 21 | +from prometheus.lang_graph.nodes.git_reset_node import GitResetNode |
| 22 | +from prometheus.lang_graph.nodes.issue_bug_analyzer_message_node import IssueBugAnalyzerMessageNode |
| 23 | +from prometheus.lang_graph.nodes.issue_bug_analyzer_node import IssueBugAnalyzerNode |
| 24 | +from prometheus.lang_graph.nodes.issue_bug_context_message_node import IssueBugContextMessageNode |
| 25 | +from prometheus.lang_graph.nodes.patch_normalization_node import PatchNormalizationNode |
| 26 | +from prometheus.lang_graph.nodes.final_patch_selection_node import FinalPatchSelectionNode |
| 27 | +from prometheus.lang_graph.nodes.reset_messages_node import ResetMessagesNode |
| 28 | +from prometheus.repository.git_repository import GitRepository |
| 29 | +from prometheus.container.base_container import BaseContainer |
| 30 | + |
| 31 | + |
| 32 | +class NormalizedNotVerifiedBugSubgraph: |
| 33 | + """Simplified Enhanced Issue Not Verified Bug Subgraph |
| 34 | + |
| 35 | + Simplified workflow with patch normalization and deduplication: |
| 36 | + 1. Original context retrieval and bug analysis |
| 37 | + 2. Patch generation and diff |
| 38 | + 3. Patch normalization and deduplication |
| 39 | + 4. Standard final patch selection |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + advanced_model: BaseChatModel, |
| 45 | + base_model: BaseChatModel, |
| 46 | + kg: KnowledgeGraph, |
| 47 | + git_repo: GitRepository, |
| 48 | + neo4j_driver: neo4j.Driver, |
| 49 | + max_token_per_neo4j_result: int, |
| 50 | + container: Optional[BaseContainer] = None, |
| 51 | + ): |
| 52 | + self._logger = logging.getLogger( |
| 53 | + f"thread-{threading.get_ident()}.prometheus.lang_graph.subgraphs.normalized_not_verified_bug_subgraph" |
| 54 | + ) |
| 55 | + |
| 56 | + # === Initialize Nodes === |
| 57 | + # Context retrieval subgraph node |
| 58 | + context_retrieval_subgraph_node = ContextRetrievalSubgraphNode( |
| 59 | + advanced_model=advanced_model, |
| 60 | + base_model=base_model, |
| 61 | + kg=kg, |
| 62 | + git_repo=git_repo, |
| 63 | + neo4j_driver=neo4j_driver, |
| 64 | + max_token_per_neo4j_result=max_token_per_neo4j_result, |
| 65 | + container=container, |
| 66 | + ) |
| 67 | + |
| 68 | + # Issue bug context message node |
| 69 | + issue_bug_context_message_node = IssueBugContextMessageNode( |
| 70 | + advanced_model=advanced_model, |
| 71 | + base_model=base_model, |
| 72 | + ) |
| 73 | + |
| 74 | + # Issue bug analyzer message node |
| 75 | + issue_bug_analyzer_message_node = IssueBugAnalyzerMessageNode( |
| 76 | + advanced_model=advanced_model, |
| 77 | + base_model=base_model, |
| 78 | + ) |
| 79 | + |
| 80 | + # Issue bug analyzer node |
| 81 | + issue_bug_analyzer_node = IssueBugAnalyzerNode( |
| 82 | + advanced_model=advanced_model, |
| 83 | + base_model=base_model, |
| 84 | + ) |
| 85 | + |
| 86 | + # Edit message node |
| 87 | + edit_message_node = EditMessageNode( |
| 88 | + advanced_model=advanced_model, |
| 89 | + base_model=base_model, |
| 90 | + ) |
| 91 | + |
| 92 | + # Edit node |
| 93 | + edit_node = EditNode( |
| 94 | + advanced_model=advanced_model, |
| 95 | + base_model=base_model, |
| 96 | + ) |
| 97 | + |
| 98 | + # Git diff node |
| 99 | + git_diff_node = GitDiffNode( |
| 100 | + git_repo=git_repo, |
| 101 | + ) |
| 102 | + |
| 103 | + # Git reset node |
| 104 | + git_reset_node = GitResetNode( |
| 105 | + git_repo=git_repo, |
| 106 | + ) |
| 107 | + |
| 108 | + # Reset messages nodes |
| 109 | + reset_issue_bug_analyzer_messages_node = ResetMessagesNode( |
| 110 | + message_key="issue_bug_analyzer_messages" |
| 111 | + ) |
| 112 | + reset_edit_messages_node = ResetMessagesNode( |
| 113 | + message_key="edit_messages" |
| 114 | + ) |
| 115 | + |
| 116 | + # Patch normalization node (only deduplication) |
| 117 | + patch_normalization_node = PatchNormalizationNode() |
| 118 | + |
| 119 | + # Final patch selection node (intelligent selection) |
| 120 | + final_patch_selection_node = FinalPatchSelectionNode( |
| 121 | + model=advanced_model, |
| 122 | + max_retries=2 |
| 123 | + ) |
| 124 | + |
| 125 | + # === Build Workflow Graph === |
| 126 | + workflow = StateGraph(IssueNotVerifiedBugState) |
| 127 | + |
| 128 | + # Add nodes |
| 129 | + workflow.add_node("context_retrieval_subgraph_node", context_retrieval_subgraph_node) |
| 130 | + workflow.add_node("issue_bug_context_message_node", issue_bug_context_message_node) |
| 131 | + workflow.add_node("issue_bug_analyzer_message_node", issue_bug_analyzer_message_node) |
| 132 | + workflow.add_node("issue_bug_analyzer_node", issue_bug_analyzer_node) |
| 133 | + workflow.add_node("edit_message_node", edit_message_node) |
| 134 | + workflow.add_node("edit_node", edit_node) |
| 135 | + workflow.add_node("git_diff_node", git_diff_node) |
| 136 | + workflow.add_node("git_reset_node", git_reset_node) |
| 137 | + workflow.add_node("reset_issue_bug_analyzer_messages_node", reset_issue_bug_analyzer_messages_node) |
| 138 | + workflow.add_node("reset_edit_messages_node", reset_edit_messages_node) |
| 139 | + workflow.add_node("patch_normalization_node", patch_normalization_node) |
| 140 | + workflow.add_node("final_patch_selection_node", final_patch_selection_node) |
| 141 | + |
| 142 | + # === Build Workflow Edges === |
| 143 | + # Start with context retrieval |
| 144 | + workflow.add_edge("context_retrieval_subgraph_node", "issue_bug_context_message_node") |
| 145 | + workflow.add_edge("issue_bug_context_message_node", "issue_bug_analyzer_message_node") |
| 146 | + workflow.add_edge("issue_bug_analyzer_message_node", "issue_bug_analyzer_node") |
| 147 | + workflow.add_edge("issue_bug_analyzer_node", "edit_message_node") |
| 148 | + workflow.add_edge("edit_message_node", "edit_node") |
| 149 | + workflow.add_edge("edit_node", "git_diff_node") |
| 150 | + |
| 151 | + # === Decision Point: Continue Generation or Process Patches === |
| 152 | + workflow.add_conditional_edges( |
| 153 | + "git_diff_node", |
| 154 | + self._routing_logic, |
| 155 | + { |
| 156 | + "continue_generation": "git_reset_node", # Continue generating more patches |
| 157 | + "process_patches": "patch_normalization_node", # Process patches with normalization |
| 158 | + } |
| 159 | + ) |
| 160 | + |
| 161 | + # Continue generating patches - original flow |
| 162 | + workflow.add_edge("git_reset_node", "reset_issue_bug_analyzer_messages_node") |
| 163 | + workflow.add_edge("reset_issue_bug_analyzer_messages_node", "reset_edit_messages_node") |
| 164 | + workflow.add_edge("reset_edit_messages_node", "issue_bug_analyzer_message_node") |
| 165 | + |
| 166 | + # === Patch Processing Flow === |
| 167 | + # Flow: normalization -> final selection -> END |
| 168 | + workflow.add_edge("patch_normalization_node", "final_patch_selection_node") |
| 169 | + workflow.add_edge("final_patch_selection_node", END) |
| 170 | + |
| 171 | + self.subgraph = workflow.compile() |
| 172 | + |
| 173 | + def _routing_logic(self, state: IssueNotVerifiedBugState) -> str: |
| 174 | + """Routing logic to decide whether to continue generation or process patches""" |
| 175 | + patches = state.get("edit_patches", []) |
| 176 | + target_patch_count = state.get("number_of_candidate_patch", 1) |
| 177 | + current_patch_count = len(patches) |
| 178 | + |
| 179 | + if current_patch_count < target_patch_count: |
| 180 | + return "continue_generation" |
| 181 | + |
| 182 | + return "process_patches" |
| 183 | + |
| 184 | + def invoke( |
| 185 | + self, |
| 186 | + issue_title: str, |
| 187 | + issue_body: str, |
| 188 | + issue_comments: Sequence[Mapping[str, str]], |
| 189 | + number_of_candidate_patch: int, |
| 190 | + recursion_limit: int = 500, |
| 191 | + ): |
| 192 | + """Invoke the subgraph with issue information""" |
| 193 | + # Prepare initial state |
| 194 | + initial_state = { |
| 195 | + "issue_title": issue_title, |
| 196 | + "issue_body": issue_body, |
| 197 | + "issue_comments": issue_comments, |
| 198 | + "number_of_candidate_patch": number_of_candidate_patch, |
| 199 | + "edit_patches": [], |
| 200 | + "issue_bug_analyzer_messages": [], |
| 201 | + "edit_messages": [], |
| 202 | + } |
| 203 | + |
| 204 | + # Execute the workflow |
| 205 | + output_state = self.subgraph.invoke( |
| 206 | + initial_state, |
| 207 | + config={"recursion_limit": recursion_limit} |
| 208 | + ) |
| 209 | + |
| 210 | + # Extract results |
| 211 | + result = { |
| 212 | + "final_patch": output_state.get("final_patch", ""), |
| 213 | + } |
| 214 | + |
| 215 | + # Add patch statistics if available |
| 216 | + if "unique_patch_count" in output_state: |
| 217 | + result["patch_statistics"] = { |
| 218 | + "original_patch_count": output_state.get("original_patch_count", 0), |
| 219 | + "unique_patch_count": output_state.get("unique_patch_count", 0), |
| 220 | + "deduplication_ratio": output_state.get("unique_patch_count", 0) / max(output_state.get("original_patch_count", 1), 1) |
| 221 | + } |
| 222 | + |
| 223 | + return result |
0 commit comments