Spaces:
Paused
Paused
Update graph_upgraded.py
Browse files- 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
try:
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
|
|
|
| 544 |
def governance_decider(state: AgentState):
|
|
|
|
| 545 |
gov = state.get("governanceReport", {}) or {}
|
| 546 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
return "experimenter_agent"
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
"experimenter_agent": "experimenter_agent",
|
| 553 |
"pm_agent": "pm_agent"
|
| 554 |
-
}
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
#
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 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
|
| 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
|