File size: 9,153 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# Context Cache Usage Report
## Executive Summary
This report analyzes how agents and components access interaction, session, and user contexts. The analysis confirms that agents use cache-only context, but identifies one area where session context generation queries the database directly (which is acceptable since it only runs at session end).
## Context Access Flow
### 1. Context Retrieval Pattern
**Orchestrator β Context Manager β Cache β Agents**
```
orchestrator.process_request()
ββ> _get_or_create_context()
ββ> context_manager.manage_context()
ββ> Check session_cache (cache-first)
ββ> Check user_cache (cache-first)
ββ> Only queries DB if cache miss
ββ> Returns cached context to orchestrator
ββ> Passes to agents
```
### 2. Cache-Only Access Verification
All agents receive context from orchestrator, which gets it from cache:
#### Intent Agent (`src/agents/intent_agent.py`)
- **Context Source**: From orchestrator (line 201-204)
- **Context Access**:
- Uses `combined_context` (pre-formatted from cache)
- Falls back to `interaction_contexts` and `user_context` from cache
- **Status**: β
Cache-only
#### Skills Identification Agent (`src/agents/skills_identification_agent.py`)
- **Context Source**: From orchestrator (line 218-221)
- **Context Access**:
- Uses `user_context` from cache (line 230)
- Uses `interaction_contexts` from cache (line 231)
- **Status**: β
Cache-only
#### Synthesis Agent (`src/agents/synthesis_agent.py`)
- **Context Source**: From orchestrator (line 283-287)
- **Context Access**:
- Uses `interaction_contexts` from cache (lines 299, 358, 550)
- Uses `user_context` from cache (implied in context dict)
- **Status**: β
Cache-only
#### Safety Agent (`src/agents/safety_agent.py`)
- **Context Source**: From orchestrator (line 314-317)
- **Context Access**:
- Uses `user_context` from cache (line 158)
- Uses `interaction_contexts` from cache (line 163)
- **Status**: β
Cache-only
## Context Manager Cache Behavior
### Session Context (Interaction Contexts)
**Location**: `src/context_manager.py` - `manage_context()` (lines 235-289)
**Cache Strategy**:
1. **Check cache first** (line 247): `session_context = self._get_from_memory_cache(session_cache_key)`
2. **Query database only if cache miss** (line 260-262): Only when `not session_context`
3. **Cache immediately after DB query** (line 265): Warm cache with fresh data
4. **Update cache synchronously** (line 444): When interaction context generated, cache updated immediately via `_update_cache_with_interaction_context()`
**Result**: β
**Cache-first, DB only on miss**
### User Context
**Location**: `src/context_manager.py` - `manage_context()` (lines 267-281)
**Cache Strategy**:
1. **Check cache first** (line 258): `user_context = self._get_from_memory_cache(user_cache_key)`
2. **Query database only if not cached** (line 269): `if not user_context or not user_context.get("user_context_loaded")`
3. **Cache after first load** (line 277): `self._warm_memory_cache(user_cache_key, user_context)`
4. **Never queries DB again** (line 279-281): Uses cached user context for all subsequent requests
**Result**: β
**Load once, cache forever (no DB queries after initial load)**
### Interaction Contexts
**Location**: `src/context_manager.py` - `_update_cache_with_interaction_context()` (lines 770-809)
**Cache Strategy**:
1. **Updated synchronously with database** (line 444): Called immediately after DB insert
2. **Adds to cached list** (line 788): Updates in-memory cache without DB query
3. **Maintains most recent 20** (line 789): Matches database query limit
**Result**: β
**Cache updated when DB updated, no DB query needed**
## Session Context Analysis
### Session Context Generation
**Location**: `src/context_manager.py` - `generate_session_context()` (lines 463-532)
**Purpose**: Generate 100-token summary of entire session for long-term user persona building
**When Called**: Only at session end via `end_session()` (line 540)
**Database Access**:
- β
**Queries database directly** (lines 469-479) to get all interaction contexts for session
- **Why This Is Acceptable**:
1. Only called once per session (at end)
2. Not used by agents during conversation
3. Used for generating user persona summary (long-term context)
4. Cache doesn't need to maintain full session summary during conversation
**Result**: β
**Acceptable - Only runs at session end, not during conversation**
### Session Context Usage
**Finding**: Session context is **NOT used by agents during conversation**.
**Evidence**:
- No agent accesses `session_context` or `session_summary` fields
- Session context is only used for generating user persona (`get_user_context()`)
- User persona is generated from session contexts across all sessions (line 326)
**Current Usage**:
1. Generated at session end (`generate_session_context()`)
2. Stored in `session_contexts` table
3. Retrieved when generating user persona (`get_user_context()` line 326)
4. User persona is cached after first load (lines 277-281)
**Recommendation**: β
**Current usage is correct - session context should not be accessed during conversation**
## Cache Update Synchronization
### Interaction Context Updates
**Location**: `src/context_manager.py` - `generate_interaction_context()` (lines 422-447)
**Flow**:
1. Generate interaction summary via LLM
2. Store in database (line 427-438)
3. **Immediately update cache** (line 444): `_update_cache_with_interaction_context()`
4. Cache contains new interaction context before next request
**Result**: β
**Cache and database synchronized at write time**
## Verification Summary
### β
All Agents Use Cache-Only Context
| Agent | Context Source | Access Method | Cache-Only? |
|-------|---------------|---------------|-------------|
| Intent Agent | Orchestrator | `combined_context`, `interaction_contexts`, `user_context` | β
Yes |
| Skills Agent | Orchestrator | `user_context`, `interaction_contexts` | β
Yes |
| Synthesis Agent | Orchestrator | `interaction_contexts`, `user_context` | β
Yes |
| Safety Agent | Orchestrator | `user_context`, `interaction_contexts` | β
Yes |
### β
Context Manager Cache Strategy
| Context Type | Cache Strategy | DB Queries |
|-------------|----------------|------------|
| Interaction Contexts | Cache-first, updated on write | Only on cache miss |
| User Context | Load once, cache forever | Only on first load |
| Session Context | Generated at session end | Only at session end (acceptable) |
### β
Cache Synchronization
| Operation | Cache Update | Timing |
|-----------|--------------|--------|
| Interaction Context Generated | β
Immediate | Same time as DB write |
| Session Context Generated | β
Not needed | Only at session end |
| User Context Loaded | β
Immediate | After first DB query |
## Findings
### β
Correct Behaviors
1. **Agents receive context from cache**: All agents get context from orchestrator, which retrieves from cache
2. **Cache-first retrieval**: Context manager checks cache before querying database
3. **User context cached forever**: Loaded once, never queries database again
4. **Interaction contexts updated synchronously**: Cache updated when database is updated
5. **Session context properly scoped**: Only generated at session end, not used during conversation
### β οΈ Session Context Notes
1. **Session context generation queries database**: `generate_session_context()` directly queries `interaction_contexts` table (lines 473-479)
- **Status**: β
Acceptable - Only called at session end
- **Impact**: None - Not used during conversation flow
- **Recommendation**: No change needed
2. **Session context not in cache during conversation**: Session summaries are not cached during conversation
- **Status**: β
Correct behavior
- **Reason**: Session summaries are 100-token summaries of entire session, not needed during conversation
- **Usage**: Only used for generating user persona (long-term context)
## Recommendations
### β
No Changes Needed
All agents and components correctly use cache-only context during conversation. The only database query during conversation flow is:
- Initial cache miss (acceptable - only happens once per session or on cache expiration)
- User context first load (acceptable - only happens once per user)
Session context generation queries database, but this is acceptable because:
1. Only runs at session end
2. Not used by agents during conversation
3. Used for long-term user persona building
## Conclusion
**Status**: β
**All agents and components correctly use cache-only context**
The system follows a cache-first strategy where:
- Context is retrieved from cache
- Database is only queried on cache miss (first request or cache expiration)
- Cache is updated immediately when database is updated
- User context is loaded once and cached forever
- Session context generation is properly scoped (session end only)
No changes required - system is working as designed.
|