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
test(session): cover lifecycle edge cases
xfgong9f9050e00702
diff --git a/cubeloop/session/session.py b/cubeloop/session/session.pyindex 8e7df36..9bcf758 100644--- a/cubeloop/session/session.py+++ b/cubeloop/session/session.py@@ -228,7 +228,7 @@ class ExecutionSession:raise TypeError(f"unsupported execution request: {type(request)!r}")except asyncio.CancelledError as exc:caught = exc- except BaseException as exc:+ except Exception as exc:caught = exctry:diff --git a/tests/agent/test_execution_session.py b/tests/agent/test_execution_session.pyindex 53f8b67..0c66484 100644--- a/tests/agent/test_execution_session.py+++ b/tests/agent/test_execution_session.py@@ -120,6 +120,64 @@ async def test_load_checkpoint_failure_does_not_partially_replace_live_state() -assert len(agent.state.messages) == 1+async def test_load_checkpoint_handles_unconfigured_and_empty_storage() -> None:+ unconfigured = Agent(model=_provider(_answer()).model("faux-model"))+ assert await unconfigured.session.load_checkpoint() is None++ empty = Agent(+ model=_provider(_answer()).model("faux-model"),+ checkpointer=MemoryCheckpointer(),+ thread_id="thread-1",+ )+ assert await empty.session.load_checkpoint() is None+++async def test_load_checkpoint_rejects_while_execution_is_active() -> None:+ entered = asyncio.Event()+ release = asyncio.Event()++ async def slow_response(messages, model):+ del messages, model+ entered.set()+ await release.wait()+ return _answer()++ provider = _provider()+ provider.set_responses([slow_response])+ agent = Agent(model=provider.model("faux-model"))+ task = asyncio.create_task(agent.prompt("hi", run_id="run-1"))+ await asyncio.wait_for(entered.wait(), timeout=1)++ with pytest.raises(ExecutionBusy, match="during execution"):+ await agent.session.load_checkpoint()++ release.set()+ await task+++async def test_subscribe_validates_limits_and_unsubscribe_stops_delivery() -> None:+ agent = Agent(model=_provider(_answer()).model("faux-model"))++ with pytest.raises(ValueError, match="capacity"):+ agent.session.subscribe(lambda event: None, capacity=0)+ with pytest.raises(ValueError, match="delivery_timeout"):+ agent.session.subscribe(lambda event: None, delivery_timeout=0)++ events = []+ unsubscribe = agent.session.subscribe(lambda event: events.append(event))+ await agent.prompt("first", run_id="run-1")+ assert events++ unsubscribe()+ provider = _provider(_answer())+ agent.model = provider.model("faux-model")+ await agent.prompt("second", run_id="run-2")+ assert {event.run_id for event in events} == {"run-1"}++@pytest.mark.asyncioasync def test_cancelled_attempt_returns_cancelled_result() -> None:entered = asyncio.Event()diff --git a/tests/agent/test_session_input.py b/tests/agent/test_session_input.pyindex e42de47..04da686 100644--- a/tests/agent/test_session_input.py+++ b/tests/agent/test_session_input.py@@ -130,3 +130,52 @@ def test_input_to_idle_session_is_closed() -> None:)assert receipt.status == "closed"+++async def test_follow_up_receipt_becomes_committed_in_memory() -> None:+ entered = asyncio.Event()+ release = asyncio.Event()++ async def first_response(messages, model):+ del messages, model+ entered.set()+ await release.wait()+ return AssistantMessage(+ content=[TextContent(text="first")], stop_reason="end_turn"+ )++ provider = FauxProvider(provider_id="faux")+ provider.set_responses(+ [+ first_response,+ AssistantMessage(+ content=[TextContent(text="done")], stop_reason="end_turn"+ ),+ ]+ )+ agent = Agent(model=provider.model("faux-model"))+ task = asyncio.create_task(+ agent.session.execute(+ PromptExecutionRequest(+ run_id="run-1", attempt_id="attempt-1", message="start"+ )+ )+ )+ await asyncio.wait_for(entered.wait(), timeout=1)++ receipt = agent.session.submit_input(+ InputEnvelope(+ input_id="follow-up-1",+ message=UserMessage(content=[TextContent(text="one more thing")]),+ mode="follow_up",+ )+ )+ assert receipt.status == "queued"+ assert agent.session.cancel_input("unknown").status == "closed"++ release.set()+ await task+ committed = agent.session.cancel_input("follow-up-1")+ assert committed.status == "committed"+ assert committed.durability == "memory"diff --git a/tests/agent/test_turn_execution_context.py b/tests/agent/test_turn_execution_context.pyindex 084efb8..96f31a2 100644--- a/tests/agent/test_turn_execution_context.py+++ b/tests/agent/test_turn_execution_context.py@@ -7,12 +7,14 @@ from cubeloop import Agentfrom cubeloop.agent.types import AgentTool, AgentToolResultfrom cubeloop.providers.base import (AssistantMessage,+ ReasoningControl,TextContent,ToolCall,UserMessage,)from cubeloop.providers.faux import FauxProviderfrom cubeloop.providers.fallback import FallbackBoundModel+from cubeloop.session import TurnExecutionContextclass _Args(BaseModel):@@ -53,6 +55,8 @@ async def test_context_captures_transformed_request_as_immutable_view() -> None:with pytest.raises(TypeError):transformed.metadata["nested"] = {"v": 2} # type: ignore[index]assert agent.state.messages[0].metadata == {}+ with pytest.raises(AttributeError, match="missing"):+ _ = transformed.content[0].missing@pytest.mark.asyncio@@ -128,3 +132,34 @@ async def test_retry_reuses_context_and_fallback_captures_new_model_view() -> No("fallback", "fallback-model"),]assert contexts[0].turn_id == contexts[1].turn_id+++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+ return AgentToolResult(content=[])++ first = AgentTool(+ name="work", description="work", parameters=_Args, execute=execute+ )+ replacement = AgentTool(+ name="work", description="replacement", parameters=_Args, execute=execute+ )+ provider = FauxProvider(provider_id="faux")+ context = TurnExecutionContext.capture(+ turn_id="turn-1",+ run_id="run-1",+ attempt_id="attempt-1",+ model=provider.model("faux-model").spec,+ reasoning=ReasoningControl(),+ system_prompt="",+ messages=[],+ tools=[],+ )++ extended = context.extend(first)+ assert context.tools == ()+ assert extended.binding_for("work") is not None+ assert extended.extend(first) is extended+ with pytest.raises(ValueError, match="already binds"):+ extended.extend(replacement)