Skip to content

Commit 0eb6ccd

Browse files
committed
fixing planner
1 parent 2ac99b4 commit 0eb6ccd

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

src/backend/kernel_agents/planner_agent.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ async def _create_structured_plan(self, input_task: InputTask) -> Tuple[Plan, Li
268268
# Generate the instruction for the LLM
269269
instruction = self._generate_instruction(input_task.description)
270270

271+
# Log the input task for debugging
272+
logging.info(f"Creating plan for task: '{input_task.description}'")
273+
logging.info(f"Using available agents: {self._available_agents}")
274+
271275
# Use the Azure AI Agent instead of direct function invocation
272276
if self._azure_ai_agent is None:
273277
# Initialize the agent if it's not already done
@@ -299,6 +303,8 @@ async def _create_structured_plan(self, input_task: InputTask) -> Tuple[Plan, Li
299303
# Debug the response
300304
logging.info(f"Response content length: {len(response_content)}")
301305
logging.debug(f"Response content first 500 chars: {response_content[:500]}")
306+
# Log more of the response for debugging
307+
logging.info(f"Full response: {response_content}")
302308

303309
# Check if response is empty or whitespace
304310
if not response_content or response_content.isspace():
@@ -346,12 +352,16 @@ async def _create_structured_plan(self, input_task: InputTask) -> Tuple[Plan, Li
346352
logging.info("Using fallback plan creation from text response")
347353
return await self._create_fallback_plan_from_text(input_task, response_content)
348354

349-
# Extract plan details
355+
# Extract plan details and log for debugging
350356
initial_goal = parsed_result.initial_goal
351357
steps_data = parsed_result.steps
352358
summary = parsed_result.summary_plan_and_steps
353359
human_clarification_request = parsed_result.human_clarification_request
354360

361+
# Log the steps and agent assignments for debugging
362+
for i, step in enumerate(steps_data):
363+
logging.info(f"Step {i+1} - Agent: {step.agent}, Action: {step.action}")
364+
355365
# Create the Plan instance
356366
plan = Plan(
357367
id=str(uuid.uuid4()),
@@ -385,6 +395,10 @@ async def _create_structured_plan(self, input_task: InputTask) -> Tuple[Plan, Li
385395
action = step_data.action
386396
agent_name = step_data.agent
387397

398+
# Log any unusual agent assignments for debugging
399+
if "onboard" in input_task.description.lower() and agent_name != "HrAgent":
400+
logging.warning(f"UNUSUAL AGENT ASSIGNMENT: Task contains 'onboard' but assigned to {agent_name} instead of HrAgent")
401+
388402
# Validate agent name
389403
if agent_name not in self._available_agents:
390404
logging.warning(f"Invalid agent name: {agent_name}, defaulting to GenericAgent")

src/backend/tests/test_planner_agent_integration.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,102 @@ async def test_create_structured_plan(self):
350350
print(f"\nCreated technical webinar plan with {len(steps)} steps")
351351
print(f"Steps assigned to: {', '.join(set(step.agent for step in steps))}")
352352

353+
async def test_hr_agent_selection(self):
354+
"""Test that the planner correctly assigns employee onboarding tasks to the HR agent."""
355+
# Initialize components
356+
await self.initialize_planner_agent()
357+
358+
# Create an onboarding task
359+
input_task = InputTask(
360+
session_id=self.session_id,
361+
user_id=self.user_id,
362+
description="Onboard a new employee, Jessica Smith."
363+
)
364+
365+
print("\n\n==== TESTING HR AGENT SELECTION FOR ONBOARDING ====")
366+
print(f"Task: '{input_task.description}'")
367+
368+
# Call handle_input_task
369+
args = KernelArguments(input_task_json=input_task.json())
370+
result = await self.planner_agent.handle_input_task(args)
371+
372+
# Check that result contains a success message
373+
self.assertIn("created successfully", result)
374+
375+
# Verify plan was created in memory store
376+
plan = await self.memory_store.get_plan_by_session(self.session_id)
377+
self.assertIsNotNone(plan)
378+
379+
# Verify steps were created
380+
steps = await self.memory_store.get_steps_for_plan(plan.id, self.session_id)
381+
self.assertGreater(len(steps), 0)
382+
383+
# Log plan details
384+
print(f"\n📋 Created onboarding plan with ID: {plan.id}")
385+
print(f"🎯 Goal: {plan.initial_goal}")
386+
print(f"📝 Summary: {plan.summary}")
387+
388+
print("\n📝 Steps:")
389+
for i, step in enumerate(steps):
390+
print(f" {i+1}. 👤 Agent: {step.agent}, 🔧 Action: {step.action}")
391+
392+
# Count agents used in the plan
393+
agent_counts = {}
394+
for step in steps:
395+
agent_counts[step.agent] = agent_counts.get(step.agent, 0) + 1
396+
397+
print("\n📊 Agent Distribution:")
398+
for agent, count in agent_counts.items():
399+
print(f" {agent}: {count} step(s)")
400+
401+
# The critical test: verify that at least one step is assigned to HrAgent
402+
hr_steps = [step for step in steps if step.agent == "HrAgent"]
403+
has_hr_steps = len(hr_steps) > 0
404+
self.assertTrue(has_hr_steps, "No steps assigned to HrAgent for an onboarding task")
405+
406+
if has_hr_steps:
407+
print("\n✅ TEST PASSED: HrAgent is used for onboarding task")
408+
else:
409+
print("\n❌ TEST FAILED: HrAgent is not used for onboarding task")
410+
411+
# Verify that no steps are incorrectly assigned to MarketingAgent
412+
marketing_steps = [step for step in steps if step.agent == "MarketingAgent"]
413+
no_marketing_steps = len(marketing_steps) == 0
414+
self.assertEqual(len(marketing_steps), 0,
415+
f"Found {len(marketing_steps)} steps incorrectly assigned to MarketingAgent for an onboarding task")
416+
417+
if no_marketing_steps:
418+
print("✅ TEST PASSED: No MarketingAgent steps for onboarding task")
419+
else:
420+
print(f"❌ TEST FAILED: Found {len(marketing_steps)} steps incorrectly assigned to MarketingAgent")
421+
422+
# Verify that the first step or a step containing "onboard" is assigned to HrAgent
423+
first_agent = steps[0].agent if steps else None
424+
onboarding_steps = [step for step in steps if "onboard" in step.action.lower()]
425+
426+
if onboarding_steps:
427+
onboard_correct = onboarding_steps[0].agent == "HrAgent"
428+
self.assertEqual(onboarding_steps[0].agent, "HrAgent",
429+
"The step containing 'onboard' was not assigned to HrAgent")
430+
if onboard_correct:
431+
print("✅ TEST PASSED: Steps containing 'onboard' are assigned to HrAgent")
432+
else:
433+
print(f"❌ TEST FAILED: Step containing 'onboard' assigned to {onboarding_steps[0].agent}, not HrAgent")
434+
435+
# If no specific "onboard" step but we have steps, the first should likely be HrAgent
436+
elif steps and "hr" not in first_agent.lower():
437+
first_step_correct = first_agent == "HrAgent"
438+
self.assertEqual(first_agent, "HrAgent",
439+
f"The first step was assigned to {first_agent}, not HrAgent")
440+
if first_step_correct:
441+
print("✅ TEST PASSED: First step is assigned to HrAgent")
442+
else:
443+
print(f"❌ TEST FAILED: First step assigned to {first_agent}, not HrAgent")
444+
445+
print("\n==== END HR AGENT SELECTION TEST ====\n")
446+
447+
return plan, steps
448+
353449
async def run_all_tests(self):
354450
"""Run all tests in sequence."""
355451
# Call setUp explicitly to ensure environment is properly initialized
@@ -372,6 +468,10 @@ async def run_all_tests(self):
372468
print("\n===== Testing _create_structured_plan directly =====")
373469
await self.test_create_structured_plan()
374470

471+
# Test 5: Verify HR agent selection for onboarding tasks
472+
print("\n===== Testing HR agent selection =====")
473+
await self.test_hr_agent_selection()
474+
375475
print("\nAll tests completed successfully!")
376476

377477
except Exception as e:

0 commit comments

Comments
 (0)