CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Python agent-frameworkagent-harnessagent-runtimeagentic-aiai-agentsai-frameworkanthropicharnessharness-engineeringharness-frameworklanggraphlanggraph-alternativellmmcpopenaipi-agentpi-agent-corepythonpython-agent
fix(loop): drain steering at turn boundary (#117)
Chris8e9e4b90b2de
diff --git a/cubepi/agent/loop.py b/cubepi/agent/loop.pyindex a7076a9..ddef5d2 100644--- a/cubepi/agent/loop.py+++ b/cubepi/agent/loop.py@@ -327,6 +327,21 @@ async def _run_loop(current_context.messages.append(msg)new_messages.append(msg)+ # The in-loop drain only fires when more tool calls remain, so a steer+ # that arrives while the model is finishing a tool-less turn would be+ # dropped. Drain it here and re-invoke the model so "steer anytime"+ # works during a final text turn too.+ if get_steering_messages:+ steering = await get_steering_messages() or []+ if steering:+ for msg in steering:+ await emit_event(emit, MessageStartEvent(message=msg))+ await emit_event(emit, MessageEndEvent(message=msg))+ current_context.messages.append(msg)+ new_messages.append(msg)+ first_turn = False+ continue+# After inner loop completes, check for follow-up messagesif get_follow_up_messages:follow_ups = await get_follow_up_messages() or []diff --git a/tests/agent/test_loop.py b/tests/agent/test_loop.pyindex 6c46571..49c739e 100644--- a/tests/agent/test_loop.py+++ b/tests/agent/test_loop.py@@ -407,6 +407,40 @@ class TestAgentLoop:assert message_ends[1].message.role == "user" # steering messageassert message_ends[2].message.role == "assistant" # response+ async def test_steering_drained_at_turn_boundary_when_no_more_tools(self):+ # Pre-loop poll returns nothing; the steer arrives at the turn boundary+ # of a tool-less turn. Without the boundary drain it would be dropped.+ provider = FauxProvider()+ provider.set_responses(+ [+ faux_assistant_message("first answer"),+ faux_assistant_message("acknowledged steer"),+ ]+ )+ context = AgentContext(system_prompt="", messages=[], tools=[])+ call_count = 0++ async def get_steering():+ nonlocal call_count+ call_count += 1+ if call_count == 2: # first call = pre-loop (empty); second = turn boundary+ return [make_user_message("steer msg")]+ return []++ events: list[AgentEvent] = []+ messages = await run_agent_loop(+ prompts=[make_user_message("start")],+ context=context,+ provider=provider,+ model=make_model(),+ convert_to_llm=identity_converter,+ get_steering_messages=get_steering,+ emit=lambda e: events.append(e),+ )++ roles = [m.role for m in messages]+ assert roles == ["user", "assistant", "user", "assistant"]+async def test_error_stop_reason_ends_loop(self):provider = FauxProvider()provider.set_responses(