1- from typing import TypedDict , List , Annotated , Optional
1+ from typing import TypedDict , List , Annotated ,Optional
22from operator import add
33from server .agentic .utils .llm_client import llm
44from server .agentic .utils .vector_tool import search_vector_tool
5- import time
6- import traceback
7- import logging
8-
9- logger = logging .getLogger (__name__ )
105
116class SimilarPR (TypedDict ):
127 ref_id : str
@@ -24,63 +19,14 @@ class PRState(TypedDict):
2419 code_quality_issues : Annotated [List [str ], add ]
2520 performance_issues : Annotated [List [str ], add ]
2621 test_suggestions : Annotated [List [str ], add ]
27- commit_sha : int
22+ commit_sha :int
2823 learnings : str
2924 progress_comment_id : Optional [int ]
3025 final_review : str
3126 review_complete : bool
32- agent_failed : Optional [str ]
33- agent_error : Optional [str ]
34- agent_trace : Optional [str ]
35-
36- def with_retry (agent_fn , retries = 3 , base_delay = 1 ):
37- """
38- Wraps agent nodes with retries + exponential backoff
39- """
40- def wrapper (state : PRState ):
41- attempt = 0
42- last_exc = None
43- while attempt < retries :
44- try :
45- logger .info (f"Running { agent_fn .__name__ } (Attempt { attempt + 1 } /{ retries } )" )
46- return agent_fn (state )
47- except Exception as e :
48- last_exc = e
49- delay = base_delay * (2 ** attempt )
50- logger .error (f"Error in { agent_fn .__name__ } : { e } " )
51- logger .debug (traceback .format_exc ())
52- time .sleep (delay )
53- attempt += 1
54- logger .critical (f"{ agent_fn .__name__ } FAILED after { retries } retries" )
55- return {
56- "agent_failed" : agent_fn .__name__ ,
57- "agent_error" : str (last_exc ),
58- "agent_trace" : traceback .format_exc ()
59- }
60- return wrapper
61-
62-
63- def error_handler (state : PRState ):
64- failed_node = state .get ("agent_failed" )
65- err = state .get ("agent_error" )
66-
67- logger .critical (f"Global Error Handler Triggered due to: { failed_node } " )
68-
69- return {
70- "final_review" : f"""
71- **Agent Failure Detected**
72-
73- **Failed Node:** { failed_node }
74- **Error:** { err }
75-
76- Partial review generated from available agents.
77- """ ,
78- "review_complete" : True
79- }
80-
8127
8228def fetch_context_agent (state : PRState ) -> dict :
83- logger . info ("fetch_context_agent running" )
29+ print ("fetch_context_agent running" )
8430 search_query = f"{ state .get ('pr_number' , '' )} { state .get ('repo_name' , '' )} "
8531
8632 raw_similar_pr = search_vector_tool (search_query )
@@ -95,11 +41,11 @@ def fetch_context_agent(state: PRState) -> dict:
9541
9642 learnings = "no past-learnig for now"
9743
44+ # ONLY return the keys you're updating
9845 return {"similar_prs" : similar_pr , "learnings" : str (learnings )}
9946
100-
10147def security_agent (state : PRState ) -> dict :
102- logger . info ("security_agent running" )
48+ print ("security_agent running" )
10349 learnings = state .get ("learnings" , "" )
10450 context = state .get ("similar_prs" , [])
10551
@@ -123,12 +69,12 @@ def security_agent(state: PRState) -> dict:
12369"""
12470 res = llm .invoke (prompt )
12571 issues = [line .strip () for line in res .content .splitlines () if line .strip ()]
126-
72+
73+ # ONLY return the security_issues key
12774 return {"security_issues" : issues }
12875
129-
13076def code_quality_agent (state : PRState ) -> dict :
131- logger . info ("code_quality_agent running" )
77+ print ("code_quality_agent running" )
13278 learnings = state .get ("learnings" , "" )
13379 context = state .get ("similar_prs" , [])
13480
@@ -149,12 +95,12 @@ def code_quality_agent(state: PRState) -> dict:
14995"""
15096 res = llm .invoke (prompt )
15197 issues = [line .strip () for line in res .content .splitlines () if line .strip ()]
152-
98+
99+ # ONLY return the code_quality_issues key
153100 return {"code_quality_issues" : issues }
154101
155-
156102def performance_agent (state : PRState ) -> dict :
157- logger . info ("performance_agent running" )
103+ print ("performance_agent running" )
158104 learnings = state .get ("learnings" , "" )
159105 context = state .get ("similar_prs" , [])
160106
@@ -173,12 +119,11 @@ def performance_agent(state: PRState) -> dict:
173119"""
174120 res = llm .invoke (prompt )
175121 issues = [line .strip () for line in res .content .splitlines () if line .strip ()]
176-
122+
177123 return {"performance_issues" : issues }
178124
179-
180125def test_agent (state : PRState ) -> dict :
181- logger . info ("test_agent running" )
126+ print ("test_agent running" )
182127 learnings = state .get ("learnings" , "" )
183128 context = state .get ("similar_prs" , [])
184129
@@ -200,5 +145,5 @@ def test_agent(state: PRState) -> dict:
200145"""
201146 res = llm .invoke (prompt )
202147 issues = [line .strip () for line in res .content .splitlines () if line .strip ()]
203-
148+
204149 return {"test_suggestions" : issues }
0 commit comments