Skip to content

Commit 47cef79

Browse files
fix: Add missing increment_state method and improve chat history persistence
- Fix f-string issues on lines 275, 300 (remove unnecessary f prefixes) - Fix chat history initialization to always set agent.chat_history even if empty - Fix critical data loss bug in _save_agent_chat_histories method - Now prioritizes live agent history but falls back to restored history - Prevents chat history loss when agents aren''t re-instantiated after restore Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent 16fb96c commit 47cef79

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

src/praisonai-agents/praisonaiagents/session.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ def Agent(
183183
else:
184184
# Try to restore from memory for backward compatibility
185185
restored_history = self._restore_agent_chat_history(agent_key)
186-
if restored_history:
187-
agent.chat_history = restored_history
186+
agent.chat_history = restored_history
188187

189188
# Track the agent
190189
self._agents[agent_key] = {"agent": agent, "chat_history": agent.chat_history}
@@ -272,7 +271,7 @@ def _restore_agent_chat_history(self, agent_key: str) -> List[Dict[str, Any]]:
272271

273272
# Search for agent chat history in memory
274273
results = self.memory.search_short_term(
275-
query=f"type:agent_chat_history",
274+
query="type:agent_chat_history",
276275
limit=10
277276
)
278277

@@ -297,7 +296,7 @@ def _restore_agent_chat_histories(self) -> None:
297296

298297
# Search for all agent chat histories in memory
299298
results = self.memory.search_short_term(
300-
query=f"type:agent_chat_history",
299+
query="type:agent_chat_history",
301300
limit=50 # Get many results to find all agents
302301
)
303302

@@ -324,12 +323,18 @@ def _save_agent_chat_histories(self) -> None:
324323
return
325324

326325
for agent_key, agent_data in self._agents.items():
327-
agent = agent_data["agent"]
328-
if agent is not None and hasattr(agent, 'chat_history'):
329-
# Update the tracked chat history
330-
agent_data["chat_history"] = agent.chat_history
331-
332-
# Save to memory (save even if empty to track agent existence)
326+
agent = agent_data.get("agent")
327+
chat_history = None
328+
329+
# Prioritize history from the live agent object, but fall back to restored history
330+
if agent and hasattr(agent, 'chat_history'):
331+
chat_history = agent.chat_history
332+
agent_data["chat_history"] = chat_history # Ensure tracked history is up-to-date
333+
else:
334+
chat_history = agent_data.get("chat_history")
335+
336+
if chat_history is not None:
337+
# Save to memory
333338
history_text = f"Agent chat history for {agent_key}"
334339
self.memory.store_short_term(
335340
text=history_text,
@@ -338,7 +343,7 @@ def _save_agent_chat_histories(self) -> None:
338343
"session_id": self.session_id,
339344
"user_id": self.user_id,
340345
"agent_key": agent_key,
341-
"chat_history": agent.chat_history
346+
"chat_history": chat_history
342347
}
343348
)
344349

0 commit comments

Comments
 (0)