Skip to content

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

fix(hitl): thread cur_ctx through middleware chain + document loop silent catch design

xf gong5dcd38bdd4a4
diff --git a/cubepi/agent/loop.py b/cubepi/agent/loop.py
index cc09a64..1dd78b3 100644
--- a/cubepi/agent/loop.py
+++ b/cubepi/agent/loop.py
@@ -180,9 +180,14 @@ async def _run_loop(
emit=emit,
)
except (HitlDetached, HitlAborted):
- # Caller (Agent.detach / Agent.abort_pending) emitted the event
- # already. Loop exits silently — assistant message and pending
- # state remain intact for the next respond() call.
+ # Per the spec design, HITL terminal events (AgentSuspendedEvent /
+ # AgentAbortedEvent) are emitted by the Agent layer (Agent.detach()
+ # / Agent.abort_pending()) BEFORE these exceptions are raised — they
+ # are mutually exclusive with AgentEndEvent. The loop intentionally
+ # exits silently here: emitting AgentEndEvent would double-signal
+ # termination to event-stream consumers who already saw the HITL
+ # event. Assistant message and pending state remain intact for the
+ # next respond() call.
return
diff --git a/cubepi/middleware/base.py b/cubepi/middleware/base.py
index 013715e..9f33bd6 100644
--- a/cubepi/middleware/base.py
+++ b/cubepi/middleware/base.py
@@ -102,7 +102,7 @@ def compose_middleware(middlewares: list[Middleware]) -> dict[str, Callable]:
cur_ctx = ctx
for mw in before_chain:
if edited_args is not None:
- cur_ctx = _rebuild_ctx_with_args(ctx, edited_args)
+ cur_ctx = _rebuild_ctx_with_args(cur_ctx, edited_args)
result = await mw.before_tool_call(cur_ctx, signal=signal)
if result is None:
continue