fix(tui): key render cache by content string and return copies

Co-Authored-By: Ahmed Allam <ahmed39652003@gmail.com>
This commit is contained in:
Devin AI
2026-07-10 06:55:38 -07:00
committed by Ahmed Allam
co-authored by Ahmed Allam
parent dd29d99b85
commit 5c6cbe0884
2 changed files with 5 additions and 6 deletions
@@ -161,7 +161,7 @@ def _process_inline_formatting(line: str) -> Text:
class AgentMessageRenderer: class AgentMessageRenderer:
_cache: ClassVar[dict[int, Text]] = {} _cache: ClassVar[dict[str, Text]] = {}
@classmethod @classmethod
def render_simple(cls, content: str) -> Text: def render_simple(cls, content: str) -> Text:
@@ -170,12 +170,11 @@ class AgentMessageRenderer:
cleaned = _BLANK_LINE_RUNS.sub("\n\n", content).strip() cleaned = _BLANK_LINE_RUNS.sub("\n\n", content).strip()
if not cleaned: if not cleaned:
return Text() return Text()
cache_key = hash(cleaned) cached = cls._cache.get(cleaned)
cached = cls._cache.get(cache_key)
if cached is not None: if cached is not None:
return cached.copy() return cached.copy()
rendered = _apply_markdown_styles(cleaned) rendered = _apply_markdown_styles(cleaned)
if len(cls._cache) > 100: if len(cls._cache) > 100:
cls._cache.clear() cls._cache.clear()
cls._cache[cache_key] = rendered cls._cache[cleaned] = rendered
return rendered return rendered.copy()
@@ -71,7 +71,7 @@ def _truncate_line(line: str) -> str:
def _clean_output(output: str) -> str: def _clean_output(output: str) -> str:
cleaned = Text.from_ansi(output).plain.translate(_CONTROL_BYTES_TO_DROP) cleaned: str = Text.from_ansi(output).plain.translate(_CONTROL_BYTES_TO_DROP)
for pattern in STRIP_PATTERNS: for pattern in STRIP_PATTERNS:
cleaned = re.sub(pattern, "", cleaned, flags=re.MULTILINE) cleaned = re.sub(pattern, "", cleaned, flags=re.MULTILINE)