Skip to content

Commit bbe0d4e

Browse files
author
Murat Kaan Meral
committed
update entrypoint to use agent instance
1 parent b637e7e commit bbe0d4e

2 files changed

Lines changed: 43 additions & 18 deletions

File tree

src/strands/multiagent/swarm.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def __init__(
196196
self,
197197
nodes: list[Agent],
198198
*,
199-
entry_point: str | None = None,
199+
entry_point: Agent | None = None,
200200
max_handoffs: int = 20,
201201
max_iterations: int = 20,
202202
execution_timeout: float = 900.0,
@@ -208,7 +208,7 @@ def __init__(
208208
209209
Args:
210210
nodes: List of nodes (e.g. Agent) to include in the swarm
211-
entry_point: Name of the agent to start with. If None, uses the first agent (default: None)
211+
entry_point: Agent to start with. If None, uses the first agent (default: None)
212212
max_handoffs: Maximum handoffs to agents and users (default: 20)
213213
max_iterations: Maximum node executions within the swarm (default: 20)
214214
execution_timeout: Total execution timeout in seconds (default: 900.0)
@@ -280,7 +280,7 @@ async def invoke_async(
280280

281281
# Initialize swarm state with configuration
282282
if self.entry_point:
283-
initial_node = self.nodes[self.entry_point]
283+
initial_node = self.nodes[str(self.entry_point.name)]
284284
else:
285285
initial_node = next(iter(self.nodes.values())) # First SwarmNode
286286

@@ -335,17 +335,22 @@ def _setup_swarm(self, nodes: list[Agent]) -> None:
335335

336336
# Validate entry point if specified
337337
if self.entry_point is not None:
338-
if self.entry_point not in self.nodes:
339-
available_nodes = list(self.nodes.keys())
340-
raise ValueError(
341-
f"Entry point '{self.entry_point}' not found in swarm nodes. Available nodes: {available_nodes}"
342-
)
338+
entry_point_node_id = str(self.entry_point.name)
339+
if (
340+
entry_point_node_id not in self.nodes
341+
or self.nodes[entry_point_node_id].executor is not self.entry_point
342+
):
343+
available_agents = [
344+
f"{node_id} ({type(node.executor).__name__})" for node_id, node in self.nodes.items()
345+
]
346+
raise ValueError(f"Entry point agent not found in swarm nodes. Available agents: {available_agents}")
343347

344348
swarm_nodes = list(self.nodes.values())
345349
logger.debug("nodes=<%s> | initialized swarm with nodes", [node.node_id for node in swarm_nodes])
346350

347351
if self.entry_point:
348-
logger.debug("entry_point=<%s> | configured entry point", self.entry_point)
352+
entry_point_name = getattr(self.entry_point, "name", "unnamed_agent")
353+
logger.debug("entry_point=<%s> | configured entry point", entry_point_name)
349354
else:
350355
first_node = next(iter(self.nodes.keys()))
351356
logger.debug("entry_point=<%s> | using first node as entry point", first_node)

tests/strands/multiagent/test_swarm.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,10 @@ def test_swarm_configurable_entry_point():
459459
agent3 = create_mock_agent("agent3", "Agent 3 response")
460460

461461
# Create swarm with agent2 as entry point
462-
swarm = Swarm([agent1, agent2, agent3], entry_point="agent2")
462+
swarm = Swarm([agent1, agent2, agent3], entry_point=agent2)
463463

464464
# Verify entry point is set correctly
465-
assert swarm.entry_point == "agent2"
465+
assert swarm.entry_point is agent2
466466

467467
# Execute swarm
468468
result = swarm("Test task")
@@ -477,14 +477,11 @@ def test_swarm_invalid_entry_point():
477477
"""Test swarm with invalid entry point raises error."""
478478
agent1 = create_mock_agent("agent1", "Agent 1 response")
479479
agent2 = create_mock_agent("agent2", "Agent 2 response")
480+
agent3 = create_mock_agent("agent3", "Agent 3 response") # Not in swarm
480481

481-
# Try to create swarm with non-existent entry point
482-
with pytest.raises(ValueError, match="Entry point 'nonexistent' not found in swarm nodes"):
483-
Swarm([agent1, agent2], entry_point="nonexistent")
484-
485-
# Try with random string entry point
486-
with pytest.raises(ValueError, match="Entry point 'xyz123random' not found in swarm nodes"):
487-
Swarm([agent1, agent2], entry_point="xyz123random")
482+
# Try to create swarm with agent not in the swarm
483+
with pytest.raises(ValueError, match="Entry point agent not found in swarm nodes"):
484+
Swarm([agent1, agent2], entry_point=agent3)
488485

489486

490487
def test_swarm_default_entry_point():
@@ -507,6 +504,29 @@ def test_swarm_default_entry_point():
507504
assert result.node_history[0].node_id == "agent1"
508505

509506

507+
def test_swarm_duplicate_agent_names():
508+
"""Test swarm rejects agents with duplicate names."""
509+
agent1 = create_mock_agent("duplicate_name", "Agent 1 response")
510+
agent2 = create_mock_agent("duplicate_name", "Agent 2 response")
511+
512+
# Try to create swarm with duplicate names
513+
with pytest.raises(ValueError, match="Node ID 'duplicate_name' is not unique"):
514+
Swarm([agent1, agent2])
515+
516+
517+
def test_swarm_entry_point_same_name_different_object():
518+
"""Test entry point validation with same name but different object."""
519+
agent1 = create_mock_agent("agent1", "Agent 1 response")
520+
agent2 = create_mock_agent("agent2", "Agent 2 response")
521+
522+
# Create a different agent with same name as agent1
523+
different_agent_same_name = create_mock_agent("agent1", "Different agent response")
524+
525+
# Try to use the different agent as entry point
526+
with pytest.raises(ValueError, match="Entry point agent not found in swarm nodes"):
527+
Swarm([agent1, agent2], entry_point=different_agent_same_name)
528+
529+
510530
def test_swarm_validate_unsupported_features():
511531
"""Test Swarm validation for session persistence and callbacks."""
512532
# Test with normal agent (should work)

0 commit comments

Comments
 (0)