File size: 2,888 Bytes
f2c56d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Graph-Aufbau der agentischen Sanduhr (Mockup).

build_graph(checkpointer=None):
  - checkpointer=None  → für `langgraph dev` (Studio injiziert eigene Persistenz)
  - checkpointer=...   → für direkten Invoke/Tests (interrupt braucht einen Saver)
"""

from __future__ import annotations

from langgraph.graph import END, START, StateGraph

from .nodes import (
    automation_output,
    execute_with_codex,
    fan_out_research,
    hai_owner_packet,
    human_escalation,
    intake_router,
    learning_agent,
    meta_agent,
    park,
    priority_gate,
    research_subagent,
    research_with_opus,
    route_after_verifier,
    route_thought,
    verifier_mediator,
)
from .state import ThoughtState


def build_graph(checkpointer=None):
    g = StateGraph(ThoughtState)

    # Nodes
    g.add_node("intake_router", intake_router)
    g.add_node("priority_gate", priority_gate)
    g.add_node("execute_with_codex", execute_with_codex)
    g.add_node("research_with_opus", research_with_opus)
    g.add_node("research_subagent", research_subagent)
    g.add_node("learning_agent", learning_agent)
    g.add_node("meta_agent", meta_agent)
    g.add_node("park", park)
    g.add_node("verifier_mediator", verifier_mediator)
    g.add_node("human_escalation", human_escalation)
    g.add_node("hai_owner_packet", hai_owner_packet)
    g.add_node("automation_output", automation_output)

    # Intake-Pyramide
    g.add_edge(START, "intake_router")
    g.add_edge("intake_router", "priority_gate")

    # prio-Gate → Lane (A/B/C/D/E)
    g.add_conditional_edges(
        "priority_gate",
        route_thought,
        {
            "execute_with_codex": "execute_with_codex",
            "research_with_opus": "research_with_opus",
            "learning_agent": "learning_agent",
            "meta_agent": "meta_agent",
            "park": "park",
        },
    )

    # Research-Lane: Send-Fanout → Subagenten → join
    g.add_conditional_edges("research_with_opus", fan_out_research, ["research_subagent"])
    g.add_edge("research_subagent", "verifier_mediator")

    # Übrige Lanes → Verifier
    g.add_edge("execute_with_codex", "verifier_mediator")
    g.add_edge("learning_agent", "verifier_mediator")
    g.add_edge("meta_agent", "verifier_mediator")

    # park ist terminal (Anti-Fanout-Ventil)
    g.add_edge("park", END)

    # Konflikt-Lane
    g.add_conditional_edges(
        "verifier_mediator",
        route_after_verifier,
        {"human_escalation": "human_escalation", "hai_owner_packet": "hai_owner_packet"},
    )
    g.add_edge("human_escalation", "hai_owner_packet")

    # Taille → Automation-Output → Ende
    g.add_edge("hai_owner_packet", "automation_output")
    g.add_edge("automation_output", END)

    return g.compile(checkpointer=checkpointer)


# Modul-Level-Graph für LangGraph Studio (langgraph.json).
graph = build_graph()