Skip to content

Commit 7f4aa21

Browse files
committed
Improve error handling and logging in agent orchestration
Added print statements for better visibility of agent creation failures in magentic_agent_factory and orchestration_manager. Enhanced error handling in orchestration_manager to log and raise exceptions during agent creation and orchestration initialization. Updated human_approval_manager to ensure correct progress ledger handling when max rounds are exceeded, and removed unused imports. Temporarily disabled file search tool addition in foundry_agent for debugging.
1 parent 9883c8c commit 7f4aa21

File tree

4 files changed

+48
-35
lines changed

4 files changed

+48
-35
lines changed

src/backend/v4/magentic_agents/foundry_agent.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ async def _collect_tools(self) -> List:
6565

6666
# File Search tool (RAG)
6767
if self.search:
68-
search_tool = await self._make_file_search_tool()
69-
if search_tool:
70-
tools.append(search_tool)
71-
self.logger.info("Added File Search tool.")
68+
print("Adding File Search tool.")
69+
# search_tool = await self._make_file_search_tool()
70+
# if search_tool:
71+
# tools.append(search_tool)
72+
# self.logger.info("Added File Search tool.")
7273

7374
# Code Interpreter
7475
if self.enable_code_interpreter:

src/backend/v4/magentic_agents/magentic_agent_factory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ async def get_agents(self, user_id: str, team_config_input: TeamConfiguration) -
160160

161161
except (UnsupportedModelError, InvalidConfigurationError) as e:
162162
self.logger.warning(f"Skipped agent {agent_cfg.name}: {e}")
163+
print(f"Skipped agent {agent_cfg.name}: {e}")
163164
continue
164165
except Exception as e:
165166
self.logger.error(f"Failed to create agent {agent_cfg.name}: {e}")
167+
print(f"Failed to create agent {agent_cfg.name}: {e}")
166168
continue
167169

168170
self.logger.info(

src/backend/v4/orchestration/human_approval_manager.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from agent_framework import ChatMessage
1212
from agent_framework._workflows._magentic import (
1313
MagenticContext,
14-
MagenticProgressLedger as ProgressLedger,
15-
MagenticProgressLedgerItem as ProgressLedgerItem,
1614
StandardMagenticManager,
1715
ORCHESTRATOR_FINAL_ANSWER_PROMPT,
1816
ORCHESTRATOR_TASK_LEDGER_PLAN_PROMPT,
@@ -75,7 +73,6 @@ def __init__(self, user_id: str, *args, **kwargs):
7573
ORCHESTRATOR_TASK_LEDGER_PLAN_UPDATE_PROMPT + plan_append
7674
)
7775
kwargs["final_answer_prompt"] = ORCHESTRATOR_FINAL_ANSWER_PROMPT + final_append
78-
#kwargs["current_user_id"] = user_id # retained for downstream usage if needed
7976

8077
self.current_user_id = user_id
8178
super().__init__(*args, **kwargs)
@@ -161,11 +158,12 @@ async def replan(self, magentic_context: MagenticContext) -> Any:
161158
)
162159
return replan_message
163160

164-
async def create_progress_ledger(
165-
self, magentic_context: MagenticContext
166-
) -> ProgressLedger:
161+
async def create_progress_ledger(self, magentic_context: MagenticContext):
167162
"""
168163
Check for max rounds exceeded and send final message if so, else defer to base.
164+
165+
Returns:
166+
Progress ledger object (type depends on agent_framework version)
169167
"""
170168
if magentic_context.round_count >= orchestration_config.max_rounds:
171169
final_message = messages.FinalResultMessage(
@@ -180,22 +178,24 @@ async def create_progress_ledger(
180178
message_type=messages.WebsocketMessageType.FINAL_RESULT_MESSAGE,
181179
)
182180

183-
return ProgressLedger(
184-
is_request_satisfied=ProgressLedgerItem(
185-
reason="Maximum rounds exceeded", answer=True
186-
),
187-
is_in_loop=ProgressLedgerItem(reason="Terminating", answer=False),
188-
is_progress_being_made=ProgressLedgerItem(
189-
reason="Terminating", answer=False
190-
),
191-
next_speaker=ProgressLedgerItem(reason="Task complete", answer=""),
192-
instruction_or_question=ProgressLedgerItem(
193-
reason="Task complete",
194-
answer="Process terminated due to maximum rounds exceeded",
195-
),
196-
)
197-
198-
# Delegate to base (which creates a MagenticProgressLedger)
181+
# Call base class to get the proper ledger type, then raise to terminate
182+
ledger = await super().create_progress_ledger(magentic_context)
183+
184+
# Override key fields to signal termination
185+
ledger.is_request_satisfied.answer = True
186+
ledger.is_request_satisfied.reason = "Maximum rounds exceeded"
187+
ledger.is_in_loop.answer = False
188+
ledger.is_in_loop.reason = "Terminating"
189+
ledger.is_progress_being_made.answer = False
190+
ledger.is_progress_being_made.reason = "Terminating"
191+
ledger.next_speaker.answer = ""
192+
ledger.next_speaker.reason = "Task complete"
193+
ledger.instruction_or_question.answer = "Process terminated due to maximum rounds exceeded"
194+
ledger.instruction_or_question.reason = "Task complete"
195+
196+
return ledger
197+
198+
# Delegate to base for normal progress ledger creation
199199
return await super().create_progress_ledger(magentic_context)
200200

201201
async def _wait_for_user_approval(
@@ -302,4 +302,4 @@ def plan_to_obj(self, magentic_context: MagenticContext, ledger) -> MPlan:
302302
task=task_text,
303303
)
304304

305-
return return_plan
305+
return return_plan

src/backend/v4/orchestration/orchestration_manager.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,24 @@ async def get_current_or_new_orchestration(
205205
cls.logger.error("Error closing agent: %s", e)
206206

207207
factory = MagenticAgentFactory()
208-
agents = await factory.get_agents(
209-
user_id=user_id, team_config_input=team_config
210-
)
211-
cls.logger.info("Created %d agents for user '%s'", len(agents), user_id)
212-
213-
orchestration_config.orchestrations[user_id] = await cls.init_orchestration(
214-
agents, user_id
215-
)
208+
try:
209+
agents = await factory.get_agents(
210+
user_id=user_id, team_config_input=team_config
211+
)
212+
cls.logger.info("Created %d agents for user '%s'", len(agents), user_id)
213+
except Exception as e:
214+
cls.logger.error("Failed to create agents for user '%s': %s", user_id, e)
215+
print(f"Failed to create agents for user '{user_id}': {e}")
216+
raise
217+
try:
218+
cls.logger.info("Initializing new orchestration for user '%s'", user_id)
219+
orchestration_config.orchestrations[user_id] = await cls.init_orchestration(
220+
agents, user_id
221+
)
222+
except Exception as e:
223+
cls.logger.error("Failed to initialize orchestration for user '%s': %s", user_id, e)
224+
print(f"Failed to initialize orchestration for user '{user_id}': {e}")
225+
raise
216226
return orchestration_config.get_current_orchestration(user_id)
217227

218228
# ---------------------------

0 commit comments

Comments
 (0)