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(session): preserve suspended settlement state
xfgongc4a63832da7b
diff --git a/cubeloop/agent/loop.py b/cubeloop/agent/loop.pyindex 98995f7..fe36092 100644--- a/cubeloop/agent/loop.py+++ b/cubeloop/agent/loop.py@@ -1,8 +1,9 @@from __future__ import annotationsimport asyncio+import inspectimport uuid-from typing import TYPE_CHECKING, Callable+from typing import TYPE_CHECKING, Awaitable, Callable, castfrom cubeloop.agent.tools import execute_tool_callsfrom cubeloop.agent.types import (@@ -821,12 +822,12 @@ async def _stream_assistant_response(context.turn_execution_context = capturedif context.on_turn_context is not None:callback_result = context.on_turn_context(captured)- if asyncio.iscoroutine(callback_result):- await callback_result+ if inspect.isawaitable(callback_result):+ _ = await callback_resultif previous_on_model_attempt is not None:callback_result = previous_on_model_attempt(model_spec)- if asyncio.iscoroutine(callback_result):- await callback_result+ if inspect.isawaitable(callback_result):+ _ = await cast(Awaitable[object], callback_result)options = options.model_copy(update={"on_model_attempt": _capture_model_attempt})diff --git a/cubeloop/agent/tools.py b/cubeloop/agent/tools.pyindex 756985a..95c3644 100644--- a/cubeloop/agent/tools.py+++ b/cubeloop/agent/tools.py@@ -191,7 +191,7 @@ async def _resolve_tool_call(if context.on_turn_context is not None:callback_result = context.on_turn_context(extended)if inspect.isawaitable(callback_result):- await callback_result+ _ = await callback_resultreturn rewritten, True, Nonediff --git a/cubeloop/session/session.py b/cubeloop/session/session.pyindex 3d08ab7..cf338cf 100644--- a/cubeloop/session/session.py+++ b/cubeloop/session/session.py@@ -96,6 +96,7 @@ class ExecutionSession:self._checkpoint_write_failed = Falseself._turn_contexts: list[TurnExecutionContext] = []self._resume_turn_context: TurnExecutionContext | None = None+ self._suspended_turn_context: TurnExecutionContext | None = Noneself._pending_request: HitlRequest | None = None@property@@ -318,13 +319,18 @@ class ExecutionSession:self._queued_inputs.pop(input_id, None)self._resume_turn_context = Noneif isinstance(request, RespondExecutionRequest):- self._resume_turn_context = next(- (- context- for context in reversed(self._turn_contexts)- if context.run_id == request.run_id- ),- None,+ retained = self._suspended_turn_context+ self._resume_turn_context = (+ retained+ if retained is not None and retained.run_id == request.run_id+ else next(+ (+ context+ for context in reversed(self._turn_contexts)+ if context.run_id == request.run_id+ ),+ None,+ ))self._turn_contexts.clear()if self._resume_turn_context is not None:@@ -412,6 +418,12 @@ class ExecutionSession:result,delivery_errors=tuple([*result.delivery_errors, *final_errors]),)+ if (+ result.outcome == "completed"+ and self._suspended_turn_context is not None+ and self._suspended_turn_context.run_id == request.run_id+ ):+ self._suspended_turn_context = Nonereturn resultfinally:self._release_attempt()@@ -456,6 +468,14 @@ class ExecutionSession:self._accepting_cancel = Falseif isinstance(event, AgentSuspendedEvent):self._pending_request = event.pending_request.model_copy(deep=True)+ self._suspended_turn_context = next(+ (+ context+ for context in reversed(self._turn_contexts)+ if context.run_id == self._active_run_id+ ),+ None,+ )committed_input_id: str | None = Noneif isinstance(event, MessageEndEvent):input_id = event.message.metadata.get("input_id")@@ -631,18 +651,6 @@ class ExecutionSession:tool_cycle_consistent = Falsehistory_consistent = tool_cycle_consistent and not self._checkpoint_write_failed- pending = None- pending_is_durable = False- if self._agent.checkpointer is not None and self._agent.thread_id is not None:- load_pending = getattr(self._agent.checkpointer, "load_pending", None)- if load_pending is not None:- loaded = await load_pending(self._agent.thread_id)- if loaded is not None and loaded[1] == request.run_id:- pending = loaded[0].model_copy(deep=True)- pending_is_durable = True- if pending is None:- pending = self._pending_request-private_outcome = self._agent.state.last_outcomeerror: ExecutionError | None = Noneif isinstance(caught, asyncio.CancelledError):@@ -686,6 +694,22 @@ class ExecutionSession:)error = ExecutionError(kind="execution", message=message)+ pending = None+ pending_is_durable = False+ if outcome == "suspended":+ if (+ self._agent.checkpointer is not None+ and self._agent.thread_id is not None+ ):+ load_pending = getattr(self._agent.checkpointer, "load_pending", None)+ if load_pending is not None:+ loaded = await load_pending(self._agent.thread_id)+ if loaded is not None and loaded[1] == request.run_id:+ pending = loaded[0].model_copy(deep=True)+ pending_is_durable = True+ if pending is None:+ pending = self._pending_request+checkpoint_committed = self._has_durable_checkpoint() and ((outcome == "completed" and self._agent._run_aware)or (outcome == "suspended" and pending_is_durable)diff --git a/tests/agent/test_execution_session.py b/tests/agent/test_execution_session.pyindex 818bdc0..2b5d8b2 100644--- a/tests/agent/test_execution_session.py+++ b/tests/agent/test_execution_session.py@@ -72,7 +72,7 @@ async def test_completed_checkpointed_attempt_reports_committed() -> None:@pytest.mark.asyncio-async def test_settlement_failure_returns_result_and_terminal_event() -> None:+async def test_completed_attempt_skips_pending_read_and_publishes_terminal() -> None:class BrokenPendingCheckpointer(MemoryCheckpointer):async def load_pending(self, thread_id: str):del thread_id@@ -90,11 +90,9 @@ async def test_settlement_failure_returns_result_and_terminal_event() -> None:PromptExecutionRequest(run_id="run-1", attempt_id="attempt-1", message="hi"))- assert result.outcome == "failed"- assert result.error is not None- assert result.error.kind == "finalization"- assert "pending storage unavailable" in result.error.message- assert result.checkpoint_committed is False+ assert result.outcome == "completed"+ assert result.error is None+ assert result.checkpoint_committed is Truefinished = [event for event in events if event.event.type == "execution_finished"]assert len(finished) == 1assert finished[0].event.result == result@@ -111,10 +109,17 @@ async def test_cancellation_during_settlement_still_publishes_terminal_event() -await asyncio.Future()agent = Agent(- model=_provider(_answer()).model("faux-model"),+ model=_provider().model("faux-model"),checkpointer=SlowPendingCheckpointer(),thread_id="thread-1",)++ async def suspend(message, *, run_id=None):+ del message, run_id+ agent._state.last_outcome = "suspended"+ return "run-1"++ agent._execute_prompt = suspend # type: ignore[method-assign]events = []agent.session.subscribe(lambda event: events.append(event))task = asyncio.create_task(@@ -376,6 +381,13 @@ async def test_hitl_resume_uses_tool_binding_captured_before_detach() -> None:assert (await first).outcome == "suspended"tool.execute = replacement_execute+ rejected = await agent.session.execute(+ PromptExecutionRequest(+ run_id="run-1", attempt_id="attempt-rejected", message="do not replace"+ )+ )+ assert rejected.outcome == "failed"+stale = await agent.session.execute(RespondExecutionRequest(run_id="run-1",diff --git a/tests/agent/test_session_events.py b/tests/agent/test_session_events.pyindex 04b9a30..0557cf0 100644--- a/tests/agent/test_session_events.py+++ b/tests/agent/test_session_events.py@@ -227,6 +227,13 @@ async def test_terminal_event_sanitizes_error_cause_for_consumers() -> None:checkpointer=BrokenPendingCheckpointer(),thread_id="thread-1",)++ async def suspend(message, *, run_id=None):+ del message, run_id+ agent._state.last_outcome = "suspended"+ return "run-1"++ agent._execute_prompt = suspend # type: ignore[method-assign]observed_causes = []def listener(envelope):diff --git a/tests/agent/test_session_input.py b/tests/agent/test_session_input.pyindex 9e3b50e..893df94 100644--- a/tests/agent/test_session_input.py+++ b/tests/agent/test_session_input.py@@ -470,7 +470,7 @@ async def test_cancelled_attempt_discards_its_undrained_input_queue() -> None:assert agent.session.submit_input(envelope).status == "queued"first.cancel()with pytest.raises(asyncio.CancelledError):- await first+ _ = await firstsecond = asyncio.create_task(agent.prompt("second", run_id="run-2"))await asyncio.wait_for(second_entered.wait(), timeout=1)diff --git a/tests/agent/test_turn_execution_context.py b/tests/agent/test_turn_execution_context.pyindex 6e3305e..0afdceb 100644--- a/tests/agent/test_turn_execution_context.py+++ b/tests/agent/test_turn_execution_context.py@@ -8,11 +8,13 @@ from pydantic import BaseModelfrom cubeloop import Agentfrom dataclasses import FrozenInstanceError+from cubeloop.agent.loop import run_agent_loopfrom cubeloop.agent.tools import execute_tool_callsfrom cubeloop.agent.types import AgentContext, AgentTool, AgentToolResultfrom cubeloop.providers.base import (AssistantMessage,ReasoningControl,+ StreamOptions,TextContent,ToolCall,ToolResultMessage,@@ -176,6 +178,32 @@ async def test_fallback_legs_with_identical_model_specs_capture_separately() ->assert contexts[0].turn_id == contexts[1].turn_id+async def test_model_attempt_callback_awaits_future_before_provider_call() -> None:+ provider = FauxProvider(provider_id="faux")+ provider.set_responses(+ [AssistantMessage(content=[TextContent(text="done")], stop_reason="end_turn")]+ )+ gate: asyncio.Future[None] = asyncio.get_running_loop().create_future()++ task = asyncio.create_task(+ run_agent_loop(+ prompts=[UserMessage(content=[TextContent(text="hi")])],+ context=AgentContext(system_prompt="", messages=[]),+ model=provider.model("faux-model"),+ convert_to_llm=lambda messages, *, ctx: messages,+ stream_options=StreamOptions(on_model_attempt=lambda model: gate),+ emit=lambda event: None,+ )+ )+ await asyncio.sleep(0)++ assert provider.call_count == 0+ gate.set_result(None)+ _ = await task+ assert provider.call_count == 1++def test_turn_context_extension_is_immutable_and_rejects_rebinding() -> None:async def execute(tool_call_id, args, *, signal=None, on_update=None):del tool_call_id, args, signal, on_update