Research_AI_Assistant / CONTEXT_CACHE_USAGE_REPORT.md
JatsTheAIGen's picture
cache key error when user id changes -fixed task 1 31_10_2025 v6
93f44e2
|
raw
history blame
9.15 kB

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.