Skip to content

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

fix(deferred): race-safe pending injection + is_error semantic

xf gong2634e458411d
diff --git a/cubepi/deferred/_expand_tool.py b/cubepi/deferred/_expand_tool.py
index 525fb5e..15401f3 100644
--- a/cubepi/deferred/_expand_tool.py
+++ b/cubepi/deferred/_expand_tool.py
@@ -51,7 +51,7 @@ def _make_expand_tools(
text = json.dumps(output.model_dump(), ensure_ascii=False)
return AgentToolResult(
content=[TextContent(text=text)],
- is_error=bool(output.error),
+ is_error=not output.expanded,
)
return AgentTool(
diff --git a/cubepi/deferred/middleware.py b/cubepi/deferred/middleware.py
index 7cc643c..f2d6e7f 100644
--- a/cubepi/deferred/middleware.py
+++ b/cubepi/deferred/middleware.py
@@ -176,7 +176,7 @@ class DeferredToolsMiddleware(Middleware):
self._expanded_schemas.append((group_id, new_schemas))
# Stage newly expanded tools for injection by after_tool_call.
- self._pending_injection = newly_expanded
+ self._pending_injection.extend(newly_expanded)
# Calculate remaining count.
current_expanded = expanded_groups.get(group_id)
@@ -207,8 +207,8 @@ class DeferredToolsMiddleware(Middleware):
) -> ExpandToolsOutput:
output = await self._expand_callback(group_id, tool_names)
if output.expanded and self._pending_injection:
- newly_expanded = self._pending_injection
- self._pending_injection = []
+ newly_expanded = list(self._pending_injection)
+ self._pending_injection.clear()
if context.tools is not None:
existing_names = {t.name for t in context.tools}
for tool in newly_expanded:
@@ -233,8 +233,8 @@ class DeferredToolsMiddleware(Middleware):
return None
if self._pending_injection:
- newly_expanded = self._pending_injection
- self._pending_injection = []
+ newly_expanded = list(self._pending_injection)
+ self._pending_injection.clear()
if ctx.context.tools is not None:
existing_names = {t.name for t in ctx.context.tools}
for tool in newly_expanded: