[blade-ai] tool_compactor aborts the whole session on tool results whose "items" array contains strings (AttributeError: 'str' object has no attribute 'get')
Environment
- blade-ai
0.7.0(source at98a9ddb) - Python 3.12, langgraph / langchain-core
- Tools exposed over MCP: read-only Kubernetes, Prometheus and Jaeger queries
Summary
smart_strip_k8s_json() treats any JSON object that contains an items key as a Kubernetes list response, and calls .get() on items[0]. When a tool legitimately returns {"items": ["...", "..."]} — a plain array of strings — this raises AttributeError: 'str' object has no attribute 'get'.
Nothing along the call path catches it, so the exception propagates out of the LangGraph node and terminates the turn through the SSE stream. The session then produces no output at all.
Minimal reproduction (verified on 0.7.0)
from chaos_agent.memory.tool_compactor import smart_strip_k8s_json
payload = '{"ok": true, "service": "jaeger", "total": 2, "items": ["GET /api/cart", "POST /api/cart"]}'
smart_strip_k8s_json(payload, 1024)File "src/chaos_agent/memory/tool_compactor.py", line 311, in smart_strip_k8s_json
first_kind = _detect_item_kind(items[0]) if items else "Generic"
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/chaos_agent/memory/tool_compactor.py", line 255, in _detect_item_kind
kind = item.get("kind", "")
^^^^^^^^
AttributeError: 'str' object has no attribute 'get'A genuine K8s list ({"items": [{"kind": "Pod", ...}]}) is unaffected.
Root cause
src/chaos_agent/memory/tool_compactor.py:303-311:
if not isinstance(data, dict) or "items" not in data:
return None # only the *presence* of "items" is checked
items = data.get("items", [])
if not items:
return None
first_kind = _detect_item_kind(items[0]) if items else "Generic" # line 311_detect_item_kind() (line 253) is annotated item: dict and dereferences item.get("kind", "") on line 255 with no isinstance guard. The presence of an items key is not sufficient evidence that a payload is a Kubernetes list — it is a very common field name in ordinary paginated API responses.
Why it looks intermittent
Compaction only rewrites a tool result when all of the following hold (tool_compactor.py:429-431):
- the result exceeds its size limit —
OLD_MAX_BYTES = 1024for historical results,RECENT_MAX_BYTES = 16 * 1024for recent ones; - it is no longer among the
KEEP_RECENT_N = 3most recent tool results; - its payload is a dict with a non-empty
itemslist.
So a short session survives, and the crash only appears once the agent has made a few more tool calls and the offending result ages out of the "recent" window. This makes the failure look random even though it is fully deterministic.
Impact
In a batch of 18 long-running sessions against a read-only MCP toolset, 11 sessions aborted with exactly this traceback. All 11 died during the intent_clarification stage — before any plan was produced — so those sessions returned nothing at all.
In our case the triggering payloads came from Jaeger "list operations" and Prometheus "list label names", both of which return {"items": [<string>, ...]}. Any tool with a string array under items will do it.
Suggested fix
Leave the K8s-specific path when the elements are not objects, so the payload falls through to the generic truncation that already exists:
# smart_strip_k8s_json(), before line 311
if not isinstance(items[0], dict):
return None # not a K8s object list -> caller falls back to truncate_json_at_boundary()One caveat, which I verified on 0.7.0: guarding only inside _detect_item_kind() (returning "Generic" for non-dict items) does stop the crash, but it is not sufficient. _strip_item() then maps every string element through _extract_nested(), which yields nothing, so
{"ok": true, "items": ["GET /api/cart", "POST /api/cart"]}is silently rewritten to
{"ok": true, "items": [{}, {}], "truncated": true}and the tool output is lost without any error. The guard therefore belongs in smart_strip_k8s_json(), not (only) in _detect_item_kind().
A stricter alternative would be to require a positive K8s signal before taking this path at all — e.g. a top-level kind ending in List, or apiVersion being present — and treat everything else as generic JSON.
Happy to send a PR if that would help.
Source: chaosblade-io/chaosblade