File size: 7,888 Bytes
93f44e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# Cache Invalidation Fix - Implementation Summary
## Overview
Implemented targeted fixes for interaction context retrieval failures by adding proper cache invalidation. All changes **only affect cache management** and do not modify application functionality.
## Issues Fixed
### Issue 1: Primary - Cache Not Invalidated After Interaction Context Generation β
**Location**: `src/orchestrator_engine.py` (lines 449-457)
**Fix**: After successfully generating and storing an interaction context, invalidate both caches to force fresh retrieval on next request.
**Changes**:
- Call `invalidate_session_cache()` after `generate_interaction_context()`
- Also clear orchestrator-level `_context_cache`
- Added logging for cache invalidation
**Impact**: Next request will query database instead of using stale cache, retrieving newly generated interaction contexts.
### Issue 2: Secondary - Orchestrator Cache Not Cleared β
**Location**: `src/orchestrator_engine.py` (lines 453-457)
**Fix**: When invalidating session cache, also clear orchestrator-level cache (`_context_cache`).
**Changes**:
- Check if `_context_cache` exists and contains entry for session
- Delete orchestrator cache entry if present
- Added debug logging
**Impact**: Prevents orchestrator cache from returning stale data even if session cache is cleared.
### Issue 3: Tertiary - Context Reference Mismatch Detection β
**Location**: `src/orchestrator_engine.py` (lines 174-195)
**Fix**: Detect when users reference previous context but cache shows 0 contexts, then force cache refresh.
**Changes**:
- Detect phrases like "based on above inputs", "previous response", etc.
- If user references previous context but has 0 interaction contexts, invalidate both caches
- Force re-retrieval of context from database
- Log warning when mismatch detected and info when refreshed
**Impact**: When users explicitly reference previous context, system will refresh cache and retrieve stored interaction contexts.
### Issue 4: Cache Invalidation Method Added β
**Location**: `src/context_manager.py` (lines 1045-1053)
**Fix**: Added dedicated method for cache invalidation.
**Changes**:
- Added `invalidate_session_cache(session_id)` method
- Method safely checks if cache entry exists before deletion
- Added logging for cache invalidation
**Impact**: Provides clean API for cache invalidation that can be reused throughout codebase.
## Code Changes Summary
### File: `src/context_manager.py`
**Added Method** (lines 1045-1053):
```python
def invalidate_session_cache(self, session_id: str):
"""
Invalidate cached context for a session to force fresh retrieval
Only affects cache management - does not change application functionality
"""
session_cache_key = f"session_{session_id}"
if session_cache_key in self.session_cache:
del self.session_cache[session_cache_key]
logger.info(f"Cache invalidated for session {session_id} to ensure fresh context retrieval")
```
### File: `src/orchestrator_engine.py`
**Change 1** - Cache invalidation after interaction context generation (lines 449-457):
```python
# After generate_interaction_context()
# Invalidate caches to ensure fresh context retrieval on next request
# Only affects cache management - does not change application functionality
self.context_manager.invalidate_session_cache(session_id)
# Also clear orchestrator-level cache
if hasattr(self, '_context_cache'):
orchestrator_cache_key = f"context_{session_id}"
if orchestrator_cache_key in self._context_cache:
del self._context_cache[orchestrator_cache_key]
logger.debug(f"Orchestrator cache invalidated for session {session_id}")
```
**Change 2** - Context reference mismatch detection (lines 174-195):
```python
# Detect context reference mismatches and force cache refresh if needed
# Only affects cache management - does not change application functionality
user_input_lower = user_input.lower()
references_previous = any(phrase in user_input_lower for phrase in [
'based on above', 'based on previous', 'above inputs', 'previous response',
'last response', 'earlier', 'before', 'mentioned above', 'as discussed',
'from above', 'from previous', 'the above', 'the previous'
])
interaction_contexts_count = len(context.get('interaction_contexts', []))
if references_previous and interaction_contexts_count == 0:
logger.warning(f"User references previous context but cache shows 0 contexts - forcing cache refresh")
# Invalidate both caches and re-retrieve
self.context_manager.invalidate_session_cache(session_id)
if hasattr(self, '_context_cache'):
orchestrator_cache_key = f"context_{session_id}"
if orchestrator_cache_key in self._context_cache:
del self._context_cache[orchestrator_cache_key]
# Force fresh context retrieval
context = await self._get_or_create_context(session_id, user_input, user_id)
interaction_contexts_count = len(context.get('interaction_contexts', []))
logger.info(f"Context refreshed after cache invalidation: {interaction_contexts_count} interaction contexts")
```
## Impact Assessment
### Application Functionality
- **NO CHANGES**: All existing functionality preserved
- Only cache management logic modified
- Database operations unchanged
- Agent execution unchanged
- Response generation unchanged
### Cache Behavior
- **IMPROVED**: Cache now invalidated at appropriate times
- Fresh data retrieved when needed
- Stale cache no longer prevents context retrieval
### Performance
- **MINIMAL IMPACT**: Cache invalidation is O(1) operation
- May result in one extra database query per request (only when cache invalidated)
- Trade-off: Better accuracy vs. minimal performance cost
## Expected Behavior After Fix
### Scenario 1: Normal Request Flow
```
Request 1: "Tell me about Excel handling"
β Generate response
β Store interaction context in DB
β Invalidate cache β
Request 2: "Create a prototype"
β Cache miss (invalidated)
β Query database
β Retrieve interaction context from DB β
β Use context for response generation
```
### Scenario 2: Context Reference Detection
```
Request 1: "Tell me about Excel handling"
β Generate response
β Store interaction context in DB
β Invalidate cache β
Request 2: "Based on above inputs, create prototype"
β Cache miss (invalidated) OR cache hit with 0 contexts
β Detect "based on above" reference β
β Force cache invalidation β
β Query database
β Retrieve interaction context from DB β
β Use context for response generation
```
## Testing Recommendations
1. **Test Normal Flow**:
- Send request 1 β Verify cache invalidated in logs
- Send request 2 β Verify interaction context retrieved (> 0)
- Verify response correctly references previous discussion
2. **Test Context Reference Detection**:
- Send request 1
- Send request 2 with "based on above inputs"
- Verify log: "forcing cache refresh"
- Verify log: "Context refreshed after cache invalidation: X interaction contexts"
- Verify response correctly references previous discussion
3. **Verify No Functionality Regression**:
- All existing features work as before
- Response quality unchanged
- Performance acceptable (may have 1 extra DB query)
## Logging Changes
New log entries to monitor:
- `"Cache invalidated for session {session_id} to ensure fresh context retrieval"`
- `"Orchestrator cache invalidated for session {session_id}"` (debug level)
- `"User references previous context but cache shows 0 contexts - forcing cache refresh"` (warning)
- `"Context refreshed after cache invalidation: X interaction contexts"`
## Related Documentation
- See `INTERACTION_CONTEXT_FAILURE_ANALYSIS.md` for detailed root cause analysis
- Changes only affect cache management as identified in analysis
|