-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor.py
More file actions
840 lines (719 loc) · 32.3 KB
/
Copy pathsupervisor.py
File metadata and controls
840 lines (719 loc) · 32.3 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
545
546
547
548
549
550
551
552
553
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
"""Supervisor — the new 4-layer multi-agent orchestrator.
The graph is::
entry
└─► refine (Prompt Refiner, Layer 2)
└─► classify (intent classification, kept from old flow)
└─► plan (TaskPlanner, template → LLM → generic)
└─► direction_before (Direction Master, Layer 1, before)
└─► operation (Operation Master, Layer 3)
└─► execute (Layer-4 specialist)
└─► direction_after (Direction Master, after)
└─► (loop or finalize)
finalize (compose final answer)
└─► END
Key behavioural changes vs. the old supervisor:
* Every single step is gated by a *before* and *after* call to the
Direction Master, so a stuck plan / dead-loop is detected as soon
as it happens.
* The Operation Master decides which specialist (tab / click /
observe / extract / verify) should run the next unit of work —
the plan no longer hard-codes a subagent.
* Fallback dispatch is automatic: when an operation fails, the
Operation Master's ``fallback_on_fail`` is consulted and the
step is retried with the new assignee before surfacing to the
user.
"""
from __future__ import annotations
import json
import logging
from typing import Annotated, Any, Optional, TypedDict
from langgraph.graph import END, StateGraph
from langgraph.graph.message import add_messages
from agents.direction_master import DirectionMaster, DirectionVerdict
from agents.intent_router import Intent, IntentRouter
from agents.operation_master import Operation, OperationMaster
from agents.prompt_refiner import PromptRefiner, RefinedPrompt
from agents.react_master import ReactDecision, ReactMaster
from agents.subagents import build_specialists, check_step_result
from agents.task_planner import Step, TaskPlanner
from config.config import chat_model_name, xf_api_key, xf_chat_base_url
from tools._logging import log_event, setup_logging
from tools._middleware import (
wrap_llm,
wrap_node,
wrap_specialist_tools,
)
MAX_ITERATIONS = 50 # safety net
# ---------------------------------------------------------------------------
# State schema
# ---------------------------------------------------------------------------
class AgentState(TypedDict, total=False):
# Inputs
user_input: str
# Mode selector: "plan" (default) or "react" (ReAct loop, no fixed plan)
mode: str
# Layer-2 output
refined: Optional[dict]
# Layer-0/1 (kept for backward compat with the old supervisor API)
intent: Optional[dict]
# Plan (steps; ``subagent`` is now an *initial hint* and may be overridden)
plan: list[dict]
current_step_idx: int
# Per-step direction & operation decisions
direction_history: list[dict]
operation_history: list[dict]
pending_operation: Optional[dict] # set by operation_node, consumed by execute_node
# History
subagent_history: list[dict]
# ReAct state (mode == "react")
react_decision: Optional[dict]
react_history: list[dict] # every ReactDecision seen so far
react_streak: int # count of consecutive dispatches w/ no progress
# Shared scratchpad
scratchpad: dict
# Final answer
final_answer: Optional[str]
error: Optional[str]
iteration_count: int
# Fallback tracking
step_retries: dict
messages: Annotated[list, add_messages]
# ---------------------------------------------------------------------------
# LLM factory (kept from old supervisor: tenacious retry on flaky proxy)
# ---------------------------------------------------------------------------
def default_llm():
"""Return the configured LLM, lazily imported.
Wraps the base ``ChatOpenAI`` with a tenacity retry that handles
the intermittent 502s / connection refusals / 60s timeouts we
get from the 讯飞 one-api proxy.
"""
from langchain_openai import ChatOpenAI
from langchain_core.runnables import Runnable
from tenacity import (
retry, stop_after_attempt, wait_exponential,
retry_if_exception_type, before_sleep_log,
)
import logging
base = ChatOpenAI(
model=chat_model_name,
api_key=xf_api_key,
base_url=xf_chat_base_url,
temperature=0,
timeout=120,
max_retries=0,
)
log = logging.getLogger("agent.supervisor.llm_retry")
@retry(
reraise=True,
stop=stop_after_attempt(4),
wait=wait_exponential(multiplier=2, min=2, max=20),
retry=retry_if_exception_type(Exception),
before_sleep=before_sleep_log(log, logging.WARNING),
)
def _invoke_with_retry(target, payload):
return target(payload)
class _RetryingLLM(Runnable):
def __init__(self, target=None) -> None:
# ``target`` is the underlying callable used by ``invoke``.
# The root instance uses the bare ``base.invoke`` so the
# retry wrapper is a drop-in replacement for ChatOpenAI.
self._target = target or base.invoke
def invoke(self, input, config=None, **kw):
return _invoke_with_retry(self._target, input)
async def ainvoke(self, input, config=None, **kw):
return _invoke_with_retry(self._target, input)
def bind_tools(self, tools, **kw):
return _RetryingLLM(target=base.bind_tools(tools, **kw).invoke)
def with_structured_output(self, schema, **kw):
structured = base.with_structured_output(schema, **kw)
return _RetryingLLM(target=structured.invoke)
def __getattr__(self, name):
return getattr(base, name)
return _RetryingLLM()
# ---------------------------------------------------------------------------
# Browser-state helper (used by every node that needs "current state")
# ---------------------------------------------------------------------------
def _read_browser_state() -> dict:
"""Read a cheap browser snapshot; returns empty dict on failure."""
try:
from tools import BrowserSession # local import: avoid daemon boot in tests
with BrowserSession(screenshot_dim=1024) as session:
snap = session.snapshot()
return {
"url": (snap.page_info or {}).get("url", ""),
"title": (snap.page_info or {}).get("title", ""),
"scrollY": (snap.page_info or {}).get("scrollY", 0),
"viewport": (snap.page_info or {}).get("viewport", {}),
"tabs": snap.tabs or [],
"screenshot_path": snap.screenshot_path or "",
}
except Exception as exc: # pragma: no cover
logging.getLogger("agent.supervisor.error").info(
"browser snapshot failed: %s", exc
)
return {}
def _state_step(state: AgentState) -> Optional[Step]:
plan = state.get("plan") or []
idx = state.get("current_step_idx", 0)
if idx >= len(plan):
return None
return Step(**plan[idx])
# ---------------------------------------------------------------------------
# Node implementations
# ---------------------------------------------------------------------------
# Each node reads shared resources (LLM, specialists, master agents) from
# a module-level context that ``build_supervisor`` populates before
# invoking the graph. This keeps the AgentState schema small and the
# nodes themselves pure ``(state) -> dict`` functions.
_RUNTIME_CTX: dict = {}
def _ctx() -> dict:
"""Return the active build_supervisor runtime context.
Raises if no context is active so misuse is loud rather than silent.
"""
if not _RUNTIME_CTX:
raise RuntimeError(
"supervisor runtime context is empty; "
"call build_supervisor() before invoking any node."
)
return _RUNTIME_CTX
def refine_node(state: AgentState) -> dict:
"""Layer 2: 把用户原话打磨成 RefinedPrompt."""
ctx = _ctx()
refiner = ctx["refiner"]
refined = refiner.refine(state["user_input"], context=state.get("scratchpad") or {})
return {
"refined": refined.model_dump(),
"scratchpad": {
**(state.get("scratchpad") or {}),
"refined_goal": refined.refined_goal,
"domain_hint": refined.domain_hint,
"acceptance_criteria": refined.acceptance_criteria,
"constraints": refined.constraints,
"assumptions": refined.assumptions,
"ambiguities": refined.ambiguities,
"priority": refined.priority,
},
}
def classify_node(state: AgentState) -> dict:
"""Classify the refined intent (kept from the old flow)."""
ctx = _ctx()
router = ctx["router"]
refined = RefinedPrompt(**state["refined"]) if state.get("refined") else None
text = (refined.refined_goal if refined else state["user_input"])
intent = router.classify(text)
# Merge refined hints into intent.params.
if refined:
for k in ("topic", "count", "download", "must_be_published", "language"):
if k in (refined.assumptions or []) or k in (refined.constraints or []):
continue
# Surface refined.domain_hint as the router's hint.
if refined.domain_hint and intent.domain == "unknown":
intent.domain = refined.domain_hint
return {
"intent": intent.model_dump(),
"iteration_count": state.get("iteration_count", 0) + 1,
}
def plan_node(state: AgentState) -> dict:
ctx = _ctx()
intent = Intent(**state["intent"]) if state.get("intent") else Intent()
planner = ctx["planner"]
plan = planner.plan(intent)
return {
"plan": [s.model_dump() for s in plan],
"current_step_idx": 0,
"scratchpad": {
**(state.get("scratchpad") or {}),
"intent_params": intent.params,
},
}
def direction_before_node(state: AgentState) -> dict:
"""Layer 1: pre-step verdict. May terminate the loop."""
ctx = _ctx()
dm = ctx["direction"]
step = _state_step(state)
refined = RefinedPrompt(**state["refined"]) if state.get("refined") else None
goal = refined.refined_goal if refined else state["user_input"]
directive = (step.description if step else "")
history = list(state.get("subagent_history") or [])
if not step:
# No plan left — Direction Master decides final fate.
verdict = dm.evaluate(
user_goal=goal, history=history, current_state={},
pending_action=None, phase="after",
)
else:
verdict = dm.evaluate(
user_goal=goal, history=history, current_state=_read_browser_state(),
pending_action={"task": directive, "step_id": step.step_id},
phase="before",
)
dh = list(state.get("direction_history") or [])
dh.append(verdict.model_dump())
update = {"direction_history": dh}
if verdict.verdict in ("stop", "need_user"):
update["error"] = verdict.reason or verdict.verdict
if verdict.verdict == "adjust" and verdict.adjustments:
# Record the adjustment as a directive hint for the next operation.
scratch = dict(state.get("scratchpad") or {})
scratch["adjustments"] = list(scratch.get("adjustments") or []) + verdict.adjustments
update["scratchpad"] = scratch
return update
def operation_node(state: AgentState) -> dict:
"""Layer 3: produce an Operation for the current step."""
ctx = _ctx()
om = ctx["operation"]
step = _state_step(state)
refined = RefinedPrompt(**state["refined"]) if state.get("refined") else None
goal = refined.refined_goal if refined else state["user_input"]
directive = ""
if step:
directive = step.description
# Incorporate Direction Master's adjustment hint.
scratch = state.get("scratchpad") or {}
if scratch.get("adjustments"):
directive = directive + " 调整建议:" + ";".join(scratch["adjustments"][-2:])
op = om.dispatch(
directive=directive,
refined_goal=goal,
current_state=_read_browser_state(),
history=list(state.get("subagent_history") or []),
)
# Plan-level hint: the planner wrote ``step.subagent`` (e.g. "browser",
# "extractor") for the old 4-subagent world. The Operation Master's
# LLM is the *authority* on which specialist to use — the old hint
# is only consulted when the LLM had no opinion and picked
# ``observe`` as a generic default. In that case we lift the plan's
# ``subagent`` into the new vocabulary so the user still gets the
# ``extractor``/``verifier`` specialists the planner expected.
if step and step.subagent and op.assignee == "observe":
hint_map = {
"api": "extract",
"extractor": "extract",
"verifier": "verify",
# NB: "browser" is intentionally NOT mapped to "click" —
# the Operation Master can pick tab/click/observe as needed
# based on the directive. Forcing "click" here was the
# root cause of the ReAct dead-loop on the "close other
# tabs" task (the click agent has no tab-close tool).
}
if step.subagent in hint_map:
op.assignee = hint_map[step.subagent] # type: ignore[assignment]
oh = list(state.get("operation_history") or [])
oh.append(op.model_dump())
return {
"operation_history": oh,
"pending_operation": op.model_dump(),
}
def execute_node(state: AgentState) -> dict:
"""Layer 4: run the dispatched specialist."""
ctx = _ctx()
op_data = state.get("pending_operation")
if not op_data:
return {"error": "no pending operation"}
op = Operation(**op_data)
step = _state_step(state)
is_react = state.get("mode") == "react"
specialists = ctx["specialists"]
if op.assignee not in specialists:
# In ReAct mode we don't have a plan; just append an error step.
step_id = (step.step_id if step else 0) or state.get("current_step_idx", 0)
return {
"subagent_history": list(state.get("subagent_history") or []) + [{
"subagent": op.assignee, "step_id": step_id,
"status": "error", "error": f"unknown assignee: {op.assignee}",
"elapsed_ms": 0, "data": None,
}],
"error": f"unknown assignee: {op.assignee}",
"current_step_idx": state.get("current_step_idx", 0) + (0 if is_react else 1),
}
sub = specialists[op.assignee]
# Build a synthetic Step whose ``action`` carries the Operation payload.
synth = Step(
step_id=(step.step_id if step else 0) or state.get("current_step_idx", 0),
description=op.task or (step.description if step else op.rationale),
subagent=op.assignee, # type: ignore[arg-type]
action=op.rationale,
expected_output={},
)
scratchpad = dict(state.get("scratchpad") or {})
# Provide Operation hints as scratchpad inputs the specialist may read.
if op.expected_signals:
scratchpad["expected_signals"] = op.expected_signals
scratchpad["refined_goal"] = (state.get("refined") or {}).get("refined_goal", "")
result = sub.run(synth, scratchpad)
history = list(state.get("subagent_history") or [])
history.append(result)
update: dict = {
"subagent_history": history,
"iteration_count": state.get("iteration_count", 0) + 1,
}
# In plan mode, advance the step counter. In ReAct mode, the counter
# is already incremented by the react node and used for observation
# indexing; do not bump it again here.
if not is_react:
update["current_step_idx"] = state.get("current_step_idx", 0) + 1
# Lift sub-agent ``data`` into the top-level scratchpad.
scratchpad = _flatten_data(result, scratchpad)
update["scratchpad"] = scratchpad
# On error, consult fallback_on_fail and retry ONCE on the same step.
if result.get("status") == "error" and op.fallback_on_fail:
# In ReAct mode the fallback is consumed immediately; we just
# rewrite pending_operation and let the next loop iteration
# in react_node pick it up.
if is_react:
update["pending_operation"] = Operation(
assignee=op.fallback_on_fail,
rationale=f"react fallback from {op.assignee}",
task=op.task,
expected_signals=op.expected_signals,
fallback_on_fail=None,
).model_dump()
update.pop("error", None)
return update
retry_step = step
if retry_step is None:
# We are past the end of the plan; surface the error.
update["error"] = result.get("error", "specialist failed")
return update
idx = state.get("current_step_idx", 0)
retries = dict(state.get("step_retries") or {})
if retries.get(str(idx), 0) < 1:
retries[str(idx)] = retries.get(str(idx), 0) + 1
update["step_retries"] = retries
# Decrement the step index so the next loop iteration re-runs it.
update["current_step_idx"] = idx
# Override the pending operation with the fallback assignee.
fallback_op = Operation(
assignee=op.fallback_on_fail,
rationale=f"fallback from {op.assignee}",
task=op.task,
expected_signals=op.expected_signals,
fallback_on_fail=None,
)
update["pending_operation"] = fallback_op.model_dump()
update.pop("subagent_history", None) # discard the failed attempt
update.pop("error", None)
elif result.get("status") == "error":
update["error"] = result.get("error", "specialist failed")
return update
def direction_after_node(state: AgentState) -> dict:
"""Layer 1: post-step verdict."""
ctx = _ctx()
dm = ctx["direction"]
refined = RefinedPrompt(**state["refined"]) if state.get("refined") else None
goal = refined.refined_goal if refined else state["user_input"]
history = list(state.get("subagent_history") or [])
verdict: DirectionVerdict = dm.evaluate(
user_goal=goal,
history=history,
current_state=_read_browser_state(),
pending_action=None,
phase="after",
)
dh = list(state.get("direction_history") or [])
dh.append(verdict.model_dump())
update: dict = {"direction_history": dh}
if verdict.verdict in ("stop", "need_user") and not state.get("error"):
update["error"] = verdict.reason or verdict.verdict
return update
# ---------------------------------------------------------------------------
# ReAct nodes (mode == "react" only)
# ---------------------------------------------------------------------------
def react_node(state: AgentState) -> dict:
"""Layer 3.5 — single iteration of the ReAct loop.
Inputs (read from state):
* refined_goal — the polished target
* acceptance_criteria — to know when the goal is met
* subagent_history — the latest observation (just executed)
* iteration_count — to enforce the safety cap
Outputs:
* react_decision — the new ReactDecision (always set)
* react_history — appended with this decision
* pending_operation — when action == "dispatch"
* error — when action == "stop" or "ask_user"
"""
ctx = _ctx()
rm: ReactMaster = ctx["react"]
refined = RefinedPrompt(**state["refined"]) if state.get("refined") else None
goal = refined.refined_goal if refined else state["user_input"]
history = list(state.get("subagent_history") or [])
last_decision_dict = state.get("react_decision")
last_decision = ReactDecision(**last_decision_dict) if last_decision_dict else None
# Safety cap.
if state.get("iteration_count", 0) > MAX_ITERATIONS:
return {
"error": f"ReAct loop exceeded {MAX_ITERATIONS} iterations",
"react_decision": None,
}
# Build a rich observation: latest browser snapshot + last subagent
# result + last decision. The ReAct master consumes this.
current_state = _read_browser_state()
last_observation = history[-1] if history else None
if last_observation:
# Surface the subagent's data slice so the LLM can use it.
current_state["last_observation"] = {
"subagent": last_observation.get("subagent"),
"status": last_observation.get("status"),
"task": last_observation.get("task"),
"data": last_observation.get("data") or {},
"error": last_observation.get("error"),
}
decision: ReactDecision = rm.think(
refined_goal=goal,
current_state=current_state,
history=history,
acceptance_criteria=(refined.acceptance_criteria if refined else []),
ambiguities=(refined.ambiguities if refined else []),
)
rh = list(state.get("react_history") or [])
rh.append(decision.model_dump())
update: dict = {
"react_decision": decision.model_dump(),
"react_history": rh,
"iteration_count": state.get("iteration_count", 0) + 1,
}
if decision.action == "dispatch":
if not decision.assignee or not decision.task:
update["error"] = "react decision 'dispatch' missing assignee/task"
return update
op = Operation(
assignee=decision.assignee,
rationale=decision.rationale,
task=decision.task,
expected_signals=decision.expected_signals or [],
fallback_on_fail=decision.fallback_on_fail,
)
update["pending_operation"] = op.model_dump()
# In ReAct mode we don't advance a fixed step counter — we just
# keep a numeric scratch. Bump it so middleware can show progress.
update["current_step_idx"] = state.get("current_step_idx", 0) + 1
return update
if decision.action == "stop":
summary = decision.rationale or "react master says goal achieved"
update["final_answer"] = summary
update["react_streak"] = 0
return update
if decision.action == "ask_user":
update["error"] = decision.question_for_user or "react master needs user input"
update["react_streak"] = 0
return update
return update
def _route_after_react(state: AgentState) -> str:
"""ReAct mode routing — return next node name."""
if state.get("error") or state.get("final_answer"):
return "finalize"
if state.get("pending_operation"):
return "execute"
if state.get("iteration_count", 0) > MAX_ITERATIONS:
return "finalize"
return "react" # safety net; should not normally happen
def _route_after_execute_react(state: AgentState) -> str:
"""After execute in ReAct mode, loop back to react (no plan)."""
if state.get("error") and not state.get("pending_operation"):
return "finalize"
return "react"
def finalizer_node(state: AgentState) -> dict:
"""Compose a user-facing answer from the scratchpad."""
history = list(state.get("subagent_history") or [])
scratchpad = state.get("scratchpad") or {}
refined = state.get("refined") or {}
direction_history = state.get("direction_history") or []
react_history = list(state.get("react_history") or [])
if state.get("error"):
summary = (
f"Task ended: {state['error']}. "
f"Ran {len(history)} step(s). See subagent_history for detail."
)
else:
summary_text = scratchpad.get("summary")
if isinstance(summary_text, str) and summary_text.strip():
summary = summary_text.strip()
else:
papers = scratchpad.get("papers") or scratchpad.get("candidates") or []
if papers and isinstance(papers, list):
lines = ["Research papers found:"]
for p in papers[:5]:
if not isinstance(p, dict):
continue
title = p.get("title") or "(no title)"
detail = p.get("detail_url") or p.get("pdf_path") or ""
line = f" - {title}"
if detail:
line += f" / {detail}"
lines.append(line)
summary = "\n".join(lines)
else:
goal = refined.get("refined_goal") or state.get("user_input", "")
if react_history:
# ReAct mode: surface the last decision and how many
# observe→reason→act iterations we ran.
last = react_history[-1] or {}
summary = (
f"ReAct loop finished in {len(react_history)} iteration(s).\n"
f"Goal: {goal}\n"
f"Last decision: action={last.get('action')} "
f"assignee={last.get('assignee')} "
f"progress={last.get('progress_estimate')}\n"
f"Reason: {last.get('rationale', '')}"
)
else:
summary = (
f"Task completed in {len(history)} step(s).\n"
f"Goal: {goal}\n"
f"Last direction verdict: {direction_history[-1] if direction_history else 'n/a'}"
)
log_event("agent.supervisor.result", "DONE", steps=len(history))
return {"final_answer": summary}
# ---------------------------------------------------------------------------
# Routing
# ---------------------------------------------------------------------------
def _route_after_direction_before(state: AgentState) -> str:
if state.get("error"):
return "finalize"
if state.get("current_step_idx", 0) >= len(state.get("plan") or []):
return "finalize"
if state.get("iteration_count", 0) > MAX_ITERATIONS:
log_event("agent.supervisor.error", "iteration cap exceeded", max=MAX_ITERATIONS)
return "finalize"
return "operation"
def _route_after_direction_after(state: AgentState) -> str:
if state.get("error"):
return "finalize"
# If we still have the same step queued (fallback retry), go to operation.
if state.get("pending_operation") and state.get("current_step_idx", 0) < len(state.get("plan") or []):
return "operation"
if state.get("current_step_idx", 0) >= len(state.get("plan") or []):
return "finalize"
return "direction_before"
# ---------------------------------------------------------------------------
# Graph assembly
# ---------------------------------------------------------------------------
def build_supervisor(mode: str = "plan"):
"""Build (but do not invoke) the LangGraph supervisor.
Args:
mode: "plan" (default) — fixed 3-step plan + Operation Master.
"react" — plan-free ReAct loop, observe→reason→act.
Both modes share the same specialists and the same entry
(refine → classify). They differ in the inner orchestration:
plan: refine → classify → plan → direction_before → operation
⇣
execute → direction_after
⇣
direction_before / finalize
react: refine → classify → react ⇄ execute → finalize
(no plan, no direction_before/after, react thinks
independently each loop)
"""
if mode not in ("plan", "react"):
raise ValueError(f"unknown supervisor mode: {mode!r}")
setup_logging()
graph = StateGraph(AgentState)
# Wrap every node with the observability middleware so we get
# NODE_IN/NODE_OUT log lines around every LangGraph transition.
graph.add_node("refine", wrap_node("refine", refine_node))
graph.add_node("classify", wrap_node("classify", classify_node))
graph.add_node("plan", wrap_node("plan", plan_node))
graph.add_node("direction_before", wrap_node("direction_before", direction_before_node))
graph.add_node("operation", wrap_node("operation", operation_node))
graph.add_node("execute", wrap_node("execute", execute_node))
graph.add_node("direction_after", wrap_node("direction_after", direction_after_node))
graph.add_node("react", wrap_node("react", react_node))
graph.add_node("finalize", wrap_node("finalize", finalizer_node))
graph.set_entry_point("refine")
graph.add_edge("refine", "classify")
if mode == "plan":
graph.add_edge("classify", "plan")
graph.add_edge("plan", "direction_before")
graph.add_conditional_edges(
"direction_before",
_route_after_direction_before,
{"operation": "operation", "finalize": "finalize"},
)
graph.add_edge("operation", "execute")
graph.add_edge("execute", "direction_after")
graph.add_conditional_edges(
"direction_after",
_route_after_direction_after,
{"operation": "operation", "direction_before": "direction_before", "finalize": "finalize"},
)
else: # react
graph.add_edge("classify", "react")
graph.add_conditional_edges(
"react",
_route_after_react,
{"execute": "execute", "finalize": "finalize"},
)
graph.add_conditional_edges(
"execute",
_route_after_execute_react,
{"react": "react", "finalize": "finalize"},
)
graph.add_edge("finalize", END)
compiled = graph.compile()
compiled.exposed = True # marker so the debug wrapper can find it
_compiled_ref = compiled # capture for the class below
# Inject the LLM once so every node reuses the same retried client.
llm = default_llm()
# Hook middleware so every LLM call (master + specialist) is logged.
wrap_llm(llm, label="supervisor")
# Build the specialist roster and wrap their tools for logging.
specialists = build_specialists(llm)
wrap_specialist_tools(specialists)
# Populate the module-level runtime context so the node callables
# (which have to be ``(state) -> dict`` to fit LangGraph) can find
# the shared masters and specialist dicts.
_RUNTIME_CTX.clear()
_RUNTIME_CTX.update({
"llm": llm,
"refiner": PromptRefiner(llm=llm),
"router": IntentRouter(llm=llm),
"planner": TaskPlanner(llm=llm),
"direction": DirectionMaster(llm=llm),
"operation": OperationMaster(llm=llm),
"react": ReactMaster(llm=llm),
"specialists": specialists,
"mode": mode,
})
class _BoundGraph:
compiled = _compiled_ref # raw LangGraph StateGraph for streaming/debug
def __init__(self) -> None:
self.mode = mode
self.llm = llm
def invoke(self, input: dict, config=None, **kw) -> dict:
input = dict(input or {})
input.setdefault("_llm", llm)
input.setdefault("mode", mode)
return _compiled_ref.invoke(input, config=config, **kw)
return _BoundGraph()
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _flatten_data(result: dict, scratchpad: dict) -> dict:
"""Lift the sub-agent's ``data`` into the top-level scratchpad."""
scratchpad = dict(scratchpad)
scratchpad["last_result"] = result
data = result.get("data")
if isinstance(data, dict):
for k, v in data.items():
if v not in (None, "", [], {}):
scratchpad.setdefault(k, v)
return scratchpad
# ---------------------------------------------------------------------------
# Convenience entry point
# ---------------------------------------------------------------------------
def run(user_input: str, mode: str = "plan") -> dict:
"""Build the supervisor and run it once on ``user_input``.
Args:
user_input: Raw user request.
mode: "plan" (default) — fixed 3-step plan + Operation Master.
"react" — plan-free ReAct loop.
"""
app = build_supervisor(mode=mode)
final = app.invoke({"user_input": user_input, "iteration_count": 0, "mode": mode})
return final
__all__ = [
"AgentState",
"build_supervisor",
"run",
"default_llm",
]