Repository navigation
Bug: Agent loses conversation context when commands require terminal interpretation #177
Description
Problem
Agent does not maintain conversation context when user messages result in terminal command execution with interpretations but no final LLM response.
Conversation ID affected: 53feaecb-3974-4bdf-badd-517358c74cf3
Symptoms
- Agent forgets previous user messages in subsequent interactions
- LLM context (
chat:conversationRedis key) only contains 2 exchanges - UI history (
chat:sessionRedis key) correctly shows all 21 messages with 5 user messages
Missing from LLM context:
- User message Docs improvements #2: "hello" (17:05:35)
- User message Fix frontend errors and remove unused code #3: "what other devices are on 192.168.168.0/24..." (17:07:09)
- User message Dev new gui #4: "now you scanned network, what ports are open..." (17:09:55)
Root Cause
The _persist_conversation() method in src/chat_workflow_manager.py:1090 is only called when there is a final llm_response from the LLM (line 1476).
When a user message results in:
- Command approval request
- Terminal command execution
- Terminal output
- Terminal interpretation
WITHOUT a final LLM response, _persist_conversation() is never called, so:
- The user/assistant exchange is NOT saved to
chat:conversation:{session_id} - The LLM loses context for that interaction
- Future messages lack crucial conversation history
Example Flow (Broken)
User: "hello" (message #2)
→ Assistant: command_approval_request for hostname
→ Terminal: executes hostname
→ Agent_terminal: interpretation "This is MV-Stealth"
→ ❌ NO llm_response generated
→ ❌ _persist_conversation() never called
→ ❌ Exchange NOT saved to chat:conversation
Fix Applied
Added immediate persistence of terminal interpretations to conversation history in interpret_terminal_command() method.
File: src/chat_workflow_manager.py
Lines: 910-958
Changes:
- After saving terminal interpretation to
chat:session(UI history) - Retrieve the most recent user message from
chat:session - Call
_persist_conversation()to save user message + interpretation tochat:conversation - Ensures LLM maintains full conversation context
Code snippet:
# CRITICAL FIX: Persist to conversation history (chat:conversation) for LLM context
# This fixes the bug where terminal interpretations weren't being tracked in LLM context
try:
session = await self.get_or_create_session(session_id)
# Get the last user message from chat:session
if self.redis_client is not None:
session_key = f"chat:session:{session_id}"
session_data_json = await asyncio.wait_for(
self.redis_client.get(session_key),
timeout=2.0
)
if session_data_json:
session_data = json.loads(session_data_json)
messages = session_data.get("messages", [])
# Find most recent user message
last_user_message = None
for msg in reversed(messages):
if msg.get("sender") == "user":
last_user_message = msg.get("text", "")
break
if last_user_message:
# Persist the exchange to conversation history
await self._persist_conversation(
session_id=session_id,
session=session,
message=last_user_message,
llm_response=interpretation
)Testing
Backend restarted successfully with fix applied.
Next: Test with a new conversation to verify conversation context is maintained across multiple terminal interpretation exchanges.
Labels
bughigh-priorityconversation-trackingredis
Related
- Conversation ID:
53feaecb-3974-4bdf-badd-517358c74cf3 - Redis keys:
chat:conversation:{session_id},chat:session:{session_id}
Activity
mrveiss commentedon Nov 24, 2025
Fix Verification ✅
Code Review: The fix is properly implemented in
src/chat_workflow_manager.py:910-958.Implementation Details:
The
interpret_terminal_command()method now:chat:session(UI history) ✅chat:session✅_persist_conversation()to save user message + interpretation tochat:conversation(LLM context) ✅Error Handling:
Current Status:
http://172.16.168.20:8001/api/health= healthy)Testing Recommendation:
Create a new conversation with multiple terminal command interactions:
Expected behavior: Agent should recall all previous interactions, including terminal command exchanges.
Validation: Check Redis keys after conversation:
Should contain all user messages + interpretations, not just UI messages.
mrveiss commentedon Nov 25, 2025
Fix Verification Check
The fix described in this issue is already implemented in the codebase at:
src/chat_workflow_manager.pyThe code properly:
chat:session_persist_conversation()with user message + interpretationStatus: Fix is implemented, needs testing to verify it works correctly in production.
Consider closing this issue if testing confirms the fix is working.
mrveiss commentedon Nov 25, 2025
Starting work on Issue #177 - Reviewing current fix implementation and verifying it handles all terminal interpretation scenarios.
mrveiss commentedon Nov 25, 2025
Code Review Completed ✅
Reviewer: code-reviewer (Senior Code Reviewer)
Date: 2025-11-25
Status: ✅ APPROVED WITH MINOR RECOMMENDATIONS
Summary
The deduplication fix correctly addresses Issue #177 by preventing duplicate conversation entries when both terminal service and chat flow attempt to persist the same user message.
Key Findings:
Test Results:
Minor Recommendations (Optional)
Verdict
✅ READY FOR MERGE
Full review report:
reports/code-review/issue-177-deduplication-fix-review.mdcc: @mrveiss
mrveiss commentedon Nov 25, 2025
Issue #177 Fix Complete ✅
Summary
Implemented deduplication logic to prevent duplicate conversation history entries when both terminal service and chat flow attempt to persist the same user message.
Changes Made
File:
src/chat_workflow_manager.py_persist_conversationmethod (lines 1162-1217):Root Cause Analysis
interpret_terminal_command(lines 912-966) correctly persists conversation for terminal interpretationsprocess_message_streamalso calls_persist_conversationat line 1562Solution
Modified
_persist_conversationto:Testing
New Test File
tests/unit/test_persist_conversation_dedup.py:Ready for Verification
The fix is ready for live testing. Conversation context should now be maintained across terminal interpretation exchanges without duplicate entries.
mrveiss commentedon Nov 25, 2025
Issue Resolved ✅
The conversation deduplication fix has been committed and merged.
Commit: b1da86d
Branch: Dev_new_gui
Summary of Fix
_persist_conversation()methodThe agent will now maintain proper conversation context across terminal interpretation exchanges without duplicate entries.
fix(chat): Add conversation deduplication to prevent duplicate histor…
fix(chat): Add conversation deduplication to prevent duplicate histor…