Skip to content

Build and run agents you can see, understand and trust.

fix(sop): honor a step that marks its own run failed (#3096)

XuRuuuy4e9861f521b8
diff --git a/src/agentscope/sop/_engine.py b/src/agentscope/sop/_engine.py
index 0725ea00a..8ecb507f6 100644
--- a/src/agentscope/sop/_engine.py
+++ b/src/agentscope/sop/_engine.py
@@ -142,9 +142,9 @@ class SOPEngine:
if interrupting:
# The parked reply was closed; nothing is retried.
return
- if record.phase is SOPPhase.AWAITING:
- # Let go of the stream rather than hold a coroutine
- # open; the caller comes back with an answer.
+ if record.phase in (SOPPhase.AWAITING, SOPPhase.FAILED):
+ # Parked for an answer, or given up by the step itself;
+ # either way, let go of the stream rather than retry.
return
if record.phase is SOPPhase.COMPLETED:
break
diff --git a/tests/sop_engine_test.py b/tests/sop_engine_test.py
index 9952b52ed..7d11cfc8e 100644
--- a/tests/sop_engine_test.py
+++ b/tests/sop_engine_test.py
@@ -19,6 +19,7 @@ from agentscope.event import (
)
from agentscope.message import (
AssistantMsg,
+ Msg,
TextBlock,
ToolCallBlock,
UserMsg,
@@ -97,6 +98,20 @@ class _Noting(SOPStepBase):
self.record(state, True)
+class _Abandoning(SOPStepBase):
+ """A step that gives up on the run without filing a verdict."""
+
+ async def reply_stream( # pylint: disable=invalid-overridden-method
+ self,
+ inputs: Any,
+ state: Any,
+ ) -> AsyncGenerator[Any, None]:
+ """Mark the step failed, failing loudly if restarted after that."""
+ assert state.phase is not SOPPhase.FAILED, "restarted a failed step"
+ yield _finished("abandoning", {})
+ state.phase = SOPPhase.FAILED
+
+
class _Scripted:
"""An ``AgentLike`` that replays one scripted reply per call."""
@@ -604,3 +619,23 @@ class SOPEngineTest(IsolatedAsyncioTestCase):
("SOP_STEP_ENDED", {"step": "A", "phase": "pending"}),
],
)
+
+ async def test_a_step_that_fails_itself_ends_the_run(self) -> None:
+ """A step that marks itself failed is not restarted."""
+ sop = SOP(name="demo", description="d", steps=[_Abandoning("A", "a")])
+ engine = SOPEngine(sop)
+
+ events = await self._drive(engine, UserMsg(name="user", content="go"))
+
+ self.assertEqual(engine.phase, SOPPhase.FAILED)
+ self.assertListEqual(
+ [
+ (_.name, _.value) if isinstance(_, CustomEvent) else type(_)
+ for _ in events
+ ],
+ [
+ ("SOP_STEP_STARTED", {"step": "A", "attempt": 1}),
+ Msg,
+ ("SOP_STEP_ENDED", {"step": "A", "phase": "failed"}),
+ ],
+ )