From c8be545655218d5edf62b651b35547bdeae0fad6 Mon Sep 17 00:00:00 2001 From: SMKRV Date: Thu, 12 Mar 2026 02:24:52 +0300 Subject: [PATCH] fix: Prevent shallow copy mutation in async_get_history with include_metadata When include_metadata=True, the metadata dict was added directly to the original history entry objects (shallow copy of list, same dict refs). Now creates per-entry dict copies before enriching with metadata. --- custom_components/ha_text_ai/history.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/custom_components/ha_text_ai/history.py b/custom_components/ha_text_ai/history.py index 51f55fa..6c34afe 100644 --- a/custom_components/ha_text_ai/history.py +++ b/custom_components/ha_text_ai/history.py @@ -407,14 +407,18 @@ class HistoryManager: history = history[:limit] if include_metadata: + enriched = [] for entry in history: - entry["metadata"] = { + enriched_entry = dict(entry) + enriched_entry["metadata"] = { "entry_size": len(str(entry)), "question_length": len(entry.get("question", "")), "response_length": len(entry.get("response", "")), "model_used": entry.get("model", default_model), "instance": self.instance_name, } + enriched.append(enriched_entry) + return enriched return history except Exception as e: