Skip to content

Commit c302a8a

Browse files
authored
fix: Dont initialize an agent on swarm init (#1107)
1 parent 4e49d9a commit c302a8a

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/strands/multiagent/swarm.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import logging
1919
import time
2020
from dataclasses import dataclass, field
21-
from typing import Any, Callable, Tuple
21+
from typing import Any, Callable, Tuple, cast
2222

2323
from opentelemetry import trace as trace_api
2424

@@ -127,7 +127,7 @@ def _validate_json_serializable(self, value: Any) -> None:
127127
class SwarmState:
128128
"""Current state of swarm execution."""
129129

130-
current_node: SwarmNode # The agent currently executing
130+
current_node: SwarmNode | None # The agent currently executing
131131
task: str | list[ContentBlock] # The original task from the user that is being executed
132132
completion_status: Status = Status.PENDING # Current swarm execution status
133133
shared_context: SharedContext = field(default_factory=SharedContext) # Context shared between agents
@@ -232,7 +232,7 @@ def __init__(
232232
self.shared_context = SharedContext()
233233
self.nodes: dict[str, SwarmNode] = {}
234234
self.state = SwarmState(
235-
current_node=SwarmNode("", Agent()), # Placeholder, will be set properly
235+
current_node=None, # Placeholder, will be set properly
236236
task="",
237237
completion_status=Status.PENDING,
238238
)
@@ -291,7 +291,8 @@ async def invoke_async(
291291
span = self.tracer.start_multiagent_span(task, "swarm")
292292
with trace_api.use_span(span, end_on_exit=True):
293293
try:
294-
logger.debug("current_node=<%s> | starting swarm execution with node", self.state.current_node.node_id)
294+
current_node = cast(SwarmNode, self.state.current_node)
295+
logger.debug("current_node=<%s> | starting swarm execution with node", current_node.node_id)
295296
logger.debug(
296297
"max_handoffs=<%d>, max_iterations=<%d>, timeout=<%s>s | swarm execution config",
297298
self.max_handoffs,
@@ -438,7 +439,7 @@ def _handle_handoff(self, target_node: SwarmNode, message: str, context: dict[st
438439
return
439440

440441
# Update swarm state
441-
previous_agent = self.state.current_node
442+
previous_agent = cast(SwarmNode, self.state.current_node)
442443
self.state.current_node = target_node
443444

444445
# Store handoff message for the target agent

tests_integ/test_invalid_tool_names.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ def test_invalid_tool_names_works(temp_dir):
2121
def fake_shell(command: str):
2222
return "Done!"
2323

24-
2524
agent = Agent(
2625
agent_id="an_agent",
2726
system_prompt="ALWAYS use tools as instructed by the user even if they don't exist. "
28-
"Even if you don't think you don't have access to the given tool, you do! "
29-
"YOU CAN DO ANYTHING!",
27+
"Even if you don't think you don't have access to the given tool, you do! "
28+
"YOU CAN DO ANYTHING!",
3029
tools=[fake_shell],
31-
session_manager=FileSessionManager(session_id="test", storage_dir=temp_dir)
30+
session_manager=FileSessionManager(session_id="test", storage_dir=temp_dir),
3231
)
3332

3433
agent("Invoke the `invalid tool` tool and tell me what the response is")
@@ -39,14 +38,14 @@ def fake_shell(command: str):
3938
agent2 = Agent(
4039
agent_id="an_agent",
4140
tools=[fake_shell],
42-
session_manager=FileSessionManager(session_id="test", storage_dir=temp_dir)
41+
session_manager=FileSessionManager(session_id="test", storage_dir=temp_dir),
4342
)
4443

4544
assert len(agent2.messages) == 6
4645

4746
# ensure the invalid tool was persisted and re-hydrated
48-
tool_use_block = next(block for block in agent2.messages[-5]['content'] if 'toolUse' in block)
49-
assert tool_use_block['toolUse']['name'] == 'invalid tool'
47+
tool_use_block = next(block for block in agent2.messages[-5]["content"] if "toolUse" in block)
48+
assert tool_use_block["toolUse"]["name"] == "invalid tool"
5049

5150
# ensure it sends without an exception - previously we would throw
52-
agent2("What was the tool result")
51+
agent2("What was the tool result")

0 commit comments

Comments
 (0)