Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

fix(session): settle resumed input ownership

xfgongd0fec6d49bcd
diff --git a/cubeloop/agent/agent.py b/cubeloop/agent/agent.py
index 4aa73b7..614b11a 100644
--- a/cubeloop/agent/agent.py
+++ b/cubeloop/agent/agent.py
@@ -887,6 +887,7 @@ class Agent(Generic[TMessage]):
extra=self._extra,
run_id=self._state.active_run_id,
attempt_id=self.session.active_attempt_id,
+ turn_execution_context=self.session._resume_turn_context,
on_turn_context=self.session._capture_turn_context,
set_input_admission=self.session._set_input_admission,
)
diff --git a/cubeloop/agent/loop.py b/cubeloop/agent/loop.py
index ffba3f9..148e392 100644
--- a/cubeloop/agent/loop.py
+++ b/cubeloop/agent/loop.py
@@ -372,7 +372,17 @@ async def _run_agent_loop_resume_body( # pragma: no cover — E2E tested
current_context.messages.append(msg)
new_messages.append(msg)
+ resumed_follow_ups: list[Message] = []
+ if get_follow_up_messages:
+ resumed_follow_ups = await get_follow_up_messages() or []
+ for msg in resumed_follow_ups:
+ await emit_event(emit, MessageStartEvent(message=msg))
+ await emit_event(emit, MessageEndEvent(message=msg))
+ current_context.messages.append(msg)
+ new_messages.append(msg)
+
# Honor should_stop_after_turn (codex BLOCKING: previous draft skipped this).
+ should_stop = False
if should_stop_after_turn:
stop_ctx = ShouldStopAfterTurnContext(
message=last,
@@ -380,7 +390,8 @@ async def _run_agent_loop_resume_body( # pragma: no cover — E2E tested
context=current_context,
new_messages=new_messages,
)
- if await should_stop_after_turn(stop_ctx):
+ should_stop = await should_stop_after_turn(stop_ctx)
+ if should_stop and not resumed_follow_ups:
if current_context.set_input_admission is not None:
current_context.set_input_admission(False)
await emit_event(emit, AgentEndEvent(messages=new_messages))
@@ -389,7 +400,7 @@ async def _run_agent_loop_resume_body( # pragma: no cover — E2E tested
return new_messages
# Terminate-by-tool semantics (codex BLOCKING: previous draft ignored).
- if terminated_by_tool:
+ if terminated_by_tool and not resumed_follow_ups:
if current_context.set_input_admission is not None:
current_context.set_input_admission(False)
await emit_event(emit, AgentEndEvent(messages=new_messages))
diff --git a/cubeloop/session/session.py b/cubeloop/session/session.py
index 627899b..bc3f0b8 100644
--- a/cubeloop/session/session.py
+++ b/cubeloop/session/session.py
@@ -93,6 +93,7 @@ class ExecutionSession:
self._queued_inputs: dict[str, Message] = {}
self._delivery_errors: list[DeliveryError] = []
self._turn_contexts: list[TurnExecutionContext] = []
+ self._resume_turn_context: TurnExecutionContext | None = None
self._pending_request: HitlRequest | None = None
@property
@@ -263,6 +264,22 @@ class ExecutionSession:
self._input_durability.clear()
self._queued_inputs.clear()
+ def _reconcile_consumed_inputs(self) -> list[str]:
+ committed: list[str] = []
+ for input_id, status in tuple(self._input_status.items()):
+ if status != "queued":
+ continue
+ queued_message = self._queued_inputs.get(input_id)
+ if queued_message is None or not any(
+ message is queued_message for message in self._agent.state.messages
+ ):
+ continue
+ self._input_status[input_id] = "committed"
+ self._input_durability[input_id] = "memory"
+ self._queued_inputs.pop(input_id, None)
+ committed.append(input_id)
+ return committed
+
async def execute(self, request: ExecutionRequest) -> ExecutionResult:
"""Execute one attempt and return settled lifecycle facts."""
self._assert_idle()
@@ -275,34 +292,36 @@ class ExecutionSession:
self._accepting_input = True
self._seq = 0
self._delivery_errors = []
+ self._reconcile_consumed_inputs()
for input_id, status in tuple(self._input_status.items()):
if status != "queued":
continue
removed = self._agent._steering_queue.remove(input_id)
removed = self._agent._follow_up_queue.remove(input_id) or removed
- queued_message = self._queued_inputs.get(input_id)
- if (
- not removed
- and queued_message is not None
- and any(
- message is queued_message for message in self._agent.state.messages
- )
- ):
- self._input_status[input_id] = "committed"
- self._input_durability[input_id] = "memory"
- self._queued_inputs.pop(input_id, None)
- continue
del self._input_status[input_id]
self._input_durability.pop(input_id, None)
self._queued_inputs.pop(input_id, None)
+ self._resume_turn_context = None
+ if isinstance(request, RespondExecutionRequest):
+ self._resume_turn_context = next(
+ (
+ context
+ for context in reversed(self._turn_contexts)
+ if context.run_id == request.run_id
+ ),
+ None,
+ )
self._turn_contexts.clear()
self._pending_request = None
caught: BaseException | None = None
try:
if isinstance(request, PromptExecutionRequest):
- await self._agent._execute_prompt(
- request.message, run_id=request.run_id
- )
+ payload = request.message
+ if isinstance(payload, list):
+ payload = [message.model_copy(deep=True) for message in payload]
+ elif not isinstance(payload, str):
+ payload = payload.model_copy(deep=True)
+ await self._agent._execute_prompt(payload, run_id=request.run_id)
elif isinstance(request, RespondExecutionRequest):
await self._agent._execute_respond(
question_id=request.question_id,
@@ -324,6 +343,19 @@ class ExecutionSession:
self._accepting_input = False
self._accepting_cancel = False
+ try:
+ for input_id in self._reconcile_consumed_inputs():
+ await self._publish(
+ InputCommitted(input_id=input_id, durability="memory"),
+ terminal=False,
+ )
+ except asyncio.CancelledError as exc:
+ if caught is None:
+ caught = exc
+ except Exception as exc:
+ if caught is None:
+ caught = exc
+
try:
try:
result = await self._settle(request, caught)
@@ -366,6 +398,7 @@ class ExecutionSession:
def _release_attempt(self) -> None:
self._active_attempt_id = None
self._active_run_id = None
+ self._resume_turn_context = None
self._idle.set()
def _assert_idle(self) -> None:
diff --git a/tests/agent/test_execution_session.py b/tests/agent/test_execution_session.py
index 96a5516..a5d554d 100644
--- a/tests/agent/test_execution_session.py
+++ b/tests/agent/test_execution_session.py
@@ -5,6 +5,7 @@ import asyncio
import pytest
from cubeloop import Agent
+from cubeloop.agent.types import AfterToolCallResult, AgentToolResult
from cubeloop.checkpointer.base import CheckpointData
from cubeloop.checkpointer.memory import MemoryCheckpointer
from cubeloop.hitl.ask_user import ask_user_tool
@@ -14,6 +15,7 @@ from cubeloop.providers.base import AssistantMessage, TextContent, ToolCall, Use
from cubeloop.providers.faux import FauxProvider
from cubeloop.session import (
ExecutionBusy,
+ InputEnvelope,
PromptExecutionRequest,
RespondExecutionRequest,
)
@@ -195,6 +197,196 @@ async def test_respond_rejects_request_run_mismatch_before_resume() -> None:
assert await checkpointer.load_pending("thread-1") == (pending, "run-real")
+async def test_prompt_snapshots_mutable_payload_before_checkpoint_await() -> None:
+ entered = asyncio.Event()
+ release = asyncio.Event()
+
+ class SlowClaimCheckpointer(MemoryCheckpointer):
+ async def claim_run(self, thread_id: str, run_id: str) -> None:
+ entered.set()
+ await release.wait()
+ await super().claim_run(thread_id, run_id)
+
+ checkpointer = SlowClaimCheckpointer()
+ agent = Agent(
+ model=_provider(_answer()).model("faux-model"),
+ checkpointer=checkpointer,
+ thread_id="thread-1",
+ )
+ message = UserMessage(
+ content=[TextContent(text="original")],
+ metadata={"nested": {"value": 1}},
+ run_id="run-1",
+ )
+ payload = [message]
+ task = asyncio.create_task(
+ agent.session.execute(
+ PromptExecutionRequest(
+ run_id="run-1", attempt_id="attempt-1", message=payload
+ )
+ )
+ )
+ await asyncio.wait_for(entered.wait(), timeout=1)
+
+ message.content[0].text = "mutated"
+ message.metadata["nested"]["value"] = 2
+ message.run_id = "other-run"
+ payload.append(UserMessage(content=[TextContent(text="late")]))
+ release.set()
+ result = await task
+
+ assert result.outcome == "completed"
+ user_messages = [item for item in agent.state.messages if item.role == "user"]
+ assert len(user_messages) == 1
+ assert user_messages[0].content[0].text == "original"
+ assert user_messages[0].metadata["nested"]["value"] == 1
+ assert user_messages[0].run_id == "run-1"
+
+
+async def test_hitl_resume_uses_tool_binding_captured_before_detach() -> None:
+ checkpointer = MemoryCheckpointer()
+ channel = CheckpointedChannel(
+ checkpointer=checkpointer,
+ thread_id="thread-1",
+ run_id="run-1",
+ )
+ tool = ask_user_tool(channel)
+ provider = _provider(
+ AssistantMessage(
+ content=[
+ ToolCall(
+ id="ask-1",
+ name="ask_user",
+ arguments={"questions": [{"key": "answer", "prompt": "Continue?"}]},
+ )
+ ],
+ stop_reason="tool_use",
+ ),
+ _answer(),
+ )
+ replacement_calls = 0
+
+ async def replacement_execute(tool_call_id, args, *, signal=None, on_update=None):
+ nonlocal replacement_calls
+ del tool_call_id, args, signal, on_update
+ replacement_calls += 1
+ return AgentToolResult(content=[TextContent(text="replacement")])
+
+ agent = Agent(
+ model=provider.model("faux-model"),
+ tools=[tool],
+ channel=channel,
+ checkpointer=checkpointer,
+ thread_id="thread-1",
+ )
+ first = asyncio.create_task(
+ agent.session.execute(
+ PromptExecutionRequest(run_id="run-1", attempt_id="attempt-1", message="hi")
+ )
+ )
+ for _ in range(100):
+ if channel.pending is not None:
+ break
+ await asyncio.sleep(0.01)
+ assert channel.pending is not None
+ question_id = channel.pending.question_id
+ await agent.session.request_detach()
+ assert (await first).outcome == "suspended"
+ tool.execute = replacement_execute
+
+ resumed = await agent.session.execute(
+ RespondExecutionRequest(
+ run_id="run-1",
+ attempt_id="attempt-2",
+ question_id=question_id,
+ answer={"answer": "yes"},
+ )
+ )
+
+ assert resumed.outcome == "completed"
+ assert replacement_calls == 0
+
+
+async def test_terminating_hitl_resume_drains_accepted_follow_up() -> None:
+ checkpointer = MemoryCheckpointer()
+ channel = CheckpointedChannel(
+ checkpointer=checkpointer,
+ thread_id="thread-1",
+ run_id="run-1",
+ )
+ resume_entered = asyncio.Event()
+ release_resume = asyncio.Event()
+
+ async def terminate_after_resume(context, signal=None):
+ del context, signal
+ resume_entered.set()
+ await release_resume.wait()
+ return AfterToolCallResult(terminate=True)
+
+ provider = _provider(
+ AssistantMessage(
+ content=[
+ ToolCall(
+ id="ask-1",
+ name="ask_user",
+ arguments={"questions": [{"key": "answer", "prompt": "Continue?"}]},
+ )
+ ],
+ stop_reason="tool_use",
+ ),
+ _answer("follow-up handled"),
+ )
+ agent = Agent(
+ model=provider.model("faux-model"),
+ tools=[ask_user_tool(channel)],
+ channel=channel,
+ checkpointer=checkpointer,
+ thread_id="thread-1",
+ after_tool_call=terminate_after_resume,
+ )
+ first = asyncio.create_task(
+ agent.session.execute(
+ PromptExecutionRequest(run_id="run-1", attempt_id="attempt-1", message="hi")
+ )
+ )
+ for _ in range(100):
+ if channel.pending is not None:
+ break
+ await asyncio.sleep(0.01)
+ assert channel.pending is not None
+ question_id = channel.pending.question_id
+ await agent.session.request_detach()
+ assert (await first).outcome == "suspended"
+ resumed = asyncio.create_task(
+ agent.session.execute(
+ RespondExecutionRequest(
+ run_id="run-1",
+ attempt_id="attempt-2",
+ question_id=question_id,
+ answer={"answer": "yes"},
+ )
+ )
+ )
+ await asyncio.wait_for(resume_entered.wait(), timeout=1)
+ envelope = InputEnvelope(
+ input_id="follow-up-1",
+ message=UserMessage(content=[TextContent(text="one more thing")]),
+ mode="follow_up",
+ )
+ assert agent.session.submit_input(envelope).status == "queued"
+ release_resume.set()
+ result = await resumed
+
+ assert result.outcome == "completed"
+ receipt = agent.session.cancel_input("follow-up-1")
+ assert receipt.status == "committed"
+ assert receipt.durability == "checkpoint"
+ assert provider.call_count == 2
+
+
@pytest.mark.asyncio
async def test_agent_prompt_and_session_share_one_admission_gate() -> None:
entered = asyncio.Event()
diff --git a/tests/agent/test_session_input.py b/tests/agent/test_session_input.py
index fcf5276..933ca6c 100644
--- a/tests/agent/test_session_input.py
+++ b/tests/agent/test_session_input.py
@@ -607,6 +607,8 @@ async def test_append_failure_keeps_drained_input_deduplicated_in_memory() -> No
checkpointer=FailingInputCheckpointer(),
thread_id="thread-1",
)
+ events = []
+ agent.session.subscribe(lambda event: events.append(event))
first = asyncio.create_task(
agent.session.execute(
PromptExecutionRequest(
@@ -625,6 +627,15 @@ async def test_append_failure_keeps_drained_input_deduplicated_in_memory() -> No
first_result = await first
assert first_result.outcome == "failed"
assert first_result.error is not None
+ receipt = agent.session.cancel_input("input-1")
+ assert receipt.status == "committed"
+ assert receipt.durability == "memory"
+ assert any(
+ event.event.type == "input_committed"
+ and event.event.input_id == "input-1"
+ and event.event.durability == "memory"
+ for event in events
+ )
second = asyncio.create_task(agent.prompt("second", run_id="run-2"))
await asyncio.wait_for(second_entered.wait(), timeout=1)