fix(compressor): guard against bare-string items in multimodal content list

raw_content from message["content"] can be a list that contains bare
strings, not only dicts.  The previous `p.get("text", "")` call raised
AttributeError on string items, crashing context compression for any
session that had a message with mixed content.

Guard with isinstance checks: dict → .get("text"), str → len(p),
fallback → len(str(p)).  Adds a regression test covering the bare-string
case that would have AttributeError'd on the pre-fix code.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
briandevans
2026-04-26 09:09:03 -07:00
committed by Teknium
parent cfc8befe65
commit 943465235e
2 changed files with 23 additions and 1 deletions

View File

@@ -1084,7 +1084,14 @@ The user has requested that this compaction PRIORITISE preserving all informatio
msg = messages[i]
raw_content = msg.get("content") or ""
content_len = (
sum(len(p.get("text", "")) for p in raw_content)
sum(
len(p.get("text", ""))
if isinstance(p, dict)
else len(p)
if isinstance(p, str)
else len(str(p))
for p in raw_content
)
if isinstance(raw_content, list)
else len(raw_content)
)