JatinAutonomousLabs commited on
Commit
fceda21
Β·
verified Β·
1 Parent(s): 1ce1870

Update graph_upgraded.py

Browse files
Files changed (1) hide show
  1. graph_upgraded.py +126 -117
graph_upgraded.py CHANGED
@@ -505,129 +505,138 @@ def run_knowledge_curator_agent(state: AgentState) -> Dict[str, Any]:
505
  # --- Wiring / injection into existing main_workflow ---
506
 
507
  def apply_upgrades():
508
- log.info("Applying graph upgrades (flexible pragmatist/governance)")
 
 
 
 
 
509
  try:
510
- mw = getattr(base_graph, "main_workflow", None)
511
- if mw is None:
512
- raise RuntimeError("base_graph.main_workflow not found. Ensure graph.py created main_workflow before applying upgrades.")
513
-
514
- node_map = {
515
- "pragmatist_agent": run_pragmatist_agent,
516
- "governance_agent": run_governance_agent,
517
- "compliance_agent": run_compliance_agent,
518
- "observer_agent": run_observer_agent,
519
- "knowledge_curator_agent": run_knowledge_curator_agent
520
- }
521
-
522
- # Add nodes defensively
523
- for nm, fn in node_map.items():
524
- try:
525
- mw.add_node(nm, fn)
526
- log.info("Added node: %s", nm)
527
- except Exception as e:
528
- log.debug("Could not add node %s (may already exist): %s", nm, e)
529
-
530
- # Wire edges: pm -> pragmatist -> governance
531
- try:
532
- mw.add_edge("pm_agent", "pragmatist_agent")
533
- log.info("Edge: pm_agent -> pragmatist_agent")
534
- except Exception as e:
535
- log.debug("pm_agent -> pragmatist_agent: %s", e)
536
-
537
- try:
538
- mw.add_edge("pragmatist_agent", "governance_agent")
539
- log.info("Edge: pragmatist_agent -> governance_agent")
540
- except Exception as e:
541
- log.debug("pragmatist_agent -> governance_agent: %s", e)
542
-
543
- # Conditional edges from governance
 
544
  def governance_decider(state: AgentState):
 
545
  gov = state.get("governanceReport", {}) or {}
546
- if gov.get("approved_for_experiment", True) is True:
 
 
 
 
 
547
  return "experimenter_agent"
548
- return "pm_agent"
549
-
550
- try:
551
- mw.add_conditional_edges("governance_agent", governance_decider, {
 
 
 
 
552
  "experimenter_agent": "experimenter_agent",
553
  "pm_agent": "pm_agent"
554
- })
555
- log.info("Added conditional edges for governance_agent")
556
- except Exception as e:
557
- log.debug("Could not add conditional edges for governance_agent: %s", e)
558
-
559
- # experimenter -> compliance -> synthesis
560
- try:
561
- mw.add_edge("experimenter_agent", "compliance_agent")
562
- log.info("Edge: experimenter_agent -> compliance_agent")
563
- except Exception as e:
564
- log.debug("experimenter_agent -> compliance_agent: %s", e)
565
-
566
- try:
567
- mw.add_edge("compliance_agent", "synthesis_agent")
568
- log.info("Edge: compliance_agent -> synthesis_agent")
569
- except Exception as e:
570
- log.debug("compliance_agent -> synthesis_agent: %s", e)
571
-
572
- # synthesis -> qa -> observer -> archivist -> knowledge_curator -> END
573
- try:
574
- mw.add_edge("synthesis_agent", "qa_agent")
575
- log.info("Edge: synthesis_agent -> qa_agent ensured/added")
576
- except Exception as e:
577
- log.debug("synthesis_agent -> qa_agent: %s", e)
578
-
579
- try:
580
- mw.add_edge("qa_agent", "observer_agent")
581
- log.info("Edge: qa_agent -> observer_agent")
582
- except Exception as e:
583
- log.debug("qa_agent -> observer_agent: %s", e)
584
-
585
- try:
586
- mw.add_edge("observer_agent", "archivist_agent")
587
- log.info("Edge: observer_agent -> archivist_agent")
588
- except Exception as e:
589
- log.debug("observer_agent -> archivist_agent: %s", e)
590
-
591
- try:
592
- mw.add_edge("archivist_agent", "knowledge_curator_agent")
593
- log.info("Edge: archivist_agent -> knowledge_curator_agent")
594
- except Exception as e:
595
- log.debug("archivist_agent -> knowledge_curator_agent: %s", e)
596
-
597
- try:
598
- mw.add_edge("knowledge_curator_agent", base_graph.END)
599
- log.info("Edge: knowledge_curator_agent -> END")
600
- except Exception as e:
601
- log.debug("knowledge_curator_agent -> END: %s", e)
602
-
603
- # keep QA -> disclaimer if present
604
- try:
605
- mw.add_edge("qa_agent", "disclaimer_agent")
606
- log.info("Edge: qa_agent -> disclaimer_agent ensured/added")
607
- except Exception as e:
608
- log.debug("qa_agent -> disclaimer_agent: %s", e)
609
-
610
- # Feedback agent wiring (best effort)
611
- try:
612
- from feedback_agent import run_feedback_agent
613
- mw.add_node("feedback_agent", run_feedback_agent)
614
- mw.add_edge("synthesis_agent", "feedback_agent")
615
- mw.add_edge("archivist_agent", "feedback_agent")
616
- mw.add_edge("feedback_agent", "archivist_agent")
617
- log.info("Feedback agent wired.")
618
- except Exception:
619
- log.debug("Feedback agent wiring skipped or failed.")
620
-
621
- # Recompile main_app defensively
622
- try:
623
- base_graph.main_app = mw.compile()
624
- log.info("Recompiled main_workflow -> main_app")
625
- except Exception as e:
626
- log.warning("Could not recompile main_workflow: %s", e)
627
-
628
- log.info("Graph upgrades applied successfully.")
629
  return True
630
-
631
  except Exception as e:
632
- log.exception("Failed to apply graph upgrades: %s", e)
633
  return False
 
505
  # --- Wiring / injection into existing main_workflow ---
506
 
507
  def apply_upgrades():
508
+ """
509
+ Rebuild the main workflow graph with upgraded routing.
510
+ CRITICAL: Creates a NEW graph instead of modifying the compiled one.
511
+ """
512
+ log.info("Applying graph upgrades (rebuilding graph with proper routing)")
513
+
514
  try:
515
+ from langgraph.graph import StateGraph, END
516
+
517
+ # Create BRAND NEW graph
518
+ new_workflow = StateGraph(AgentState)
519
+
520
+ # Add all nodes (reusing original functions where appropriate)
521
+ new_workflow.add_node("memory_retriever", run_memory_retrieval)
522
+ new_workflow.add_node("intent_agent", run_intent_agent)
523
+ new_workflow.add_node("pm_agent", run_pm_agent)
524
+ new_workflow.add_node("pragmatist_agent", run_pragmatist_agent)
525
+ new_workflow.add_node("governance_agent", run_governance_agent)
526
+ new_workflow.add_node("experimenter_agent", run_experimenter_agent)
527
+ new_workflow.add_node("compliance_agent", run_compliance_agent)
528
+ new_workflow.add_node("synthesis_agent", run_synthesis_agent)
529
+ new_workflow.add_node("qa_agent", run_qa_agent)
530
+ new_workflow.add_node("observer_agent", run_observer_agent)
531
+ new_workflow.add_node("archivist_agent", run_archivist_agent)
532
+ new_workflow.add_node("knowledge_curator_agent", run_knowledge_curator_agent)
533
+ new_workflow.add_node("disclaimer_agent", run_disclaimer_agent)
534
+
535
+ log.info("βœ… All nodes added to new graph")
536
+
537
+ # Set entry point
538
+ new_workflow.set_entry_point("memory_retriever")
539
+
540
+ # Standard flow: Memory β†’ Intent β†’ PM
541
+ new_workflow.add_edge("memory_retriever", "intent_agent")
542
+ new_workflow.add_edge("intent_agent", "pm_agent")
543
+
544
+ # NEW ROUTING: PM β†’ Pragmatist β†’ Governance
545
+ new_workflow.add_edge("pm_agent", "pragmatist_agent")
546
+ new_workflow.add_edge("pragmatist_agent", "governance_agent")
547
+ log.info("βœ… New routing added: PM β†’ Pragmatist β†’ Governance")
548
+
549
+ # Governance conditional: approved β†’ Experimenter, rejected β†’ PM
550
  def governance_decider(state: AgentState):
551
+ """Decide next step based on governance decision"""
552
  gov = state.get("governanceReport", {}) or {}
553
+ decision = gov.get("governanceDecision", "approve")
554
+ approved = gov.get("approved_for_experiment", True)
555
+
556
+ log.info(f"Governance decision: {decision}, approved: {approved}")
557
+
558
+ if approved and decision in ("approve", "approve_with_warning"):
559
  return "experimenter_agent"
560
+ else:
561
+ # Rejected or requires escalation - loop back to PM
562
+ return "pm_agent"
563
+
564
+ new_workflow.add_conditional_edges(
565
+ "governance_agent",
566
+ governance_decider,
567
+ {
568
  "experimenter_agent": "experimenter_agent",
569
  "pm_agent": "pm_agent"
570
+ }
571
+ )
572
+ log.info("βœ… Governance conditional routing added")
573
+
574
+ # Continue standard flow: Experimenter β†’ Compliance β†’ Synthesis β†’ QA
575
+ new_workflow.add_edge("experimenter_agent", "compliance_agent")
576
+ new_workflow.add_edge("compliance_agent", "synthesis_agent")
577
+ new_workflow.add_edge("synthesis_agent", "qa_agent")
578
+
579
+ # QA conditional routing (from original graph)
580
+ def should_continue(state: AgentState):
581
+ """Decide next step after QA"""
582
+ # Budget check first
583
+ if state.get("budget_exceeded"):
584
+ return "disclaimer_agent"
585
+
586
+ # Parse rework cycles
587
+ try:
588
+ rework = int(state.get("rework_cycles", 0))
589
+ max_loops = int(state.get("max_loops", 0))
590
+ except Exception:
591
+ rework = state.get("rework_cycles", 0) or 0
592
+ max_loops = state.get("max_loops", 0) or 0
593
+
594
+ # If approved β†’ success path
595
+ if state.get("approved"):
596
+ return "observer_agent"
597
+
598
+ # If exceeded rework limit β†’ disclaimer
599
+ if rework > max_loops:
600
+ return "disclaimer_agent"
601
+
602
+ # Otherwise β†’ loop back to PM for revision
603
+ return "pm_agent"
604
+
605
+ new_workflow.add_conditional_edges(
606
+ "qa_agent",
607
+ should_continue,
608
+ {
609
+ "observer_agent": "observer_agent",
610
+ "pm_agent": "pm_agent",
611
+ "disclaimer_agent": "disclaimer_agent"
612
+ }
613
+ )
614
+ log.info("βœ… QA conditional routing added")
615
+
616
+ # Final success path: Observer β†’ Archivist β†’ Knowledge Curator β†’ END
617
+ new_workflow.add_edge("observer_agent", "archivist_agent")
618
+ new_workflow.add_edge("archivist_agent", "knowledge_curator_agent")
619
+ new_workflow.add_edge("knowledge_curator_agent", END)
620
+
621
+ # Disclaimer path (failure/limit reached)
622
+ new_workflow.add_edge("disclaimer_agent", END)
623
+
624
+ log.info("οΏ½οΏ½ Final flow edges added")
625
+
626
+ # CRITICAL: Compile NEW graph and REPLACE old one
627
+ base_graph.main_app = new_workflow.compile()
628
+ base_graph.main_workflow = new_workflow # Also update workflow reference
629
+
630
+ log.info("=" * 60)
631
+ log.info("βœ… GRAPH REBUILD SUCCESSFUL")
632
+ log.info("=" * 60)
633
+ log.info("New flow: Memory β†’ Intent β†’ PM β†’ Pragmatist β†’ Governance")
634
+ log.info(" β†’ Experimenter β†’ Compliance β†’ Synthesis β†’ QA")
635
+ log.info(" β†’ Observer β†’ Archivist β†’ Knowledge Curator β†’ END")
636
+ log.info("=" * 60)
637
+
 
 
 
 
 
 
 
638
  return True
639
+
640
  except Exception as e:
641
+ log.exception(f"❌ Failed to rebuild graph: {e}")
642
  return False