Spaces:
Sleeping
Profile Session Restoration Fix
Problem Summary
When loading the webapp, users encountered the error:
Error: Profile session not found. Please log in again.
Root Cause
The Flask session cache (session["profile_session_id"]) would expire, but the user's profile session still existed in the backend API. The webapp had no fallback mechanism to restore the session from the backend.
Solution
Implemented a session restoration mechanism that automatically queries the backend API when the Flask session cache expires.
Files Changed
webapp/src/services/session_helper.py(new file)ensure_profile_session(user_id): Queries backend API for existing profile session or creates new oneget_or_restore_profile_session(user_id, cached_session_id): Returns cached ID or restores from backend
webapp/src/routes/contacts.py(2 locations updated)- Line ~414: In
send_message()- replaced simple cache check with restoration logic - Line ~537: In
add_fact()- replaced simple cache check with restoration logic
- Line ~414: In
How It Works
Before (would fail):
profile_session_id = session.get("profile_session_id")
if not profile_session_id:
flash("Error: Profile session not found. Please log in again.", "warning")
return redirect(...)
After (auto-restores):
try:
profile_session_id = get_or_restore_profile_session(
user_id=user_id,
cached_session_id=session.get("profile_session_id")
)
# Update Flask session cache
session["profile_session_id"] = profile_session_id
except Exception as e:
flash("Error: Unable to initialize profile session. Please log in again.", "warning")
logger.error(f"Failed to restore profile_session_id for user {user_id}: {e}")
return redirect(...)
Restoration Flow
Cache Hit: If
profile_session_idexists in Flask session β return immediately (fast path)Cache Miss: If Flask session expired:
- Call
backend_api.list_sessions(user_id=user_id) - Search for session with
contact_id="user-profile" - If found β return existing
session_idand update Flask cache - If not found β create new profile session and return
session_id
- Call
Benefits
- Seamless UX: Users no longer need to log out/in when Flask session expires
- Resilient: Handles backend session persistence correctly
- Performant: Only queries backend on cache miss (lazy restoration)
- Logged: All restoration attempts are logged for monitoring
Testing
Manual Test Steps
Start the backend API:
cd /Users/christian.kniep/src/gitlab.com/qnib-memverge/streamlit/prepmate docker compose up -d apiStart the webapp:
cd webapp # Set required environment variables export FLASK_SECRET_KEY="your-secret-key" export HUGGINGFACE_CLIENT_ID="your-client-id" export HUGGINGFACE_CLIENT_SECRET="your-client-secret" export BACKEND_API_URL="http://localhost:4004" # Run webapp python -m flask run --port 5001Test restoration scenario:
- Log in via OAuth (creates profile session)
- Create a contact
- Clear Flask session cache (delete cookies or
session.pop("profile_session_id")) - Send a message to the contact
- Expected: Message sends successfully (profile session restored automatically)
- Check logs: Should see
[SESSION_RESTORE]or[SESSION_CREATE]log entries
Expected Log Output
Cache Miss β Existing Session Found:
[SESSION_RESTORE] Found existing profile session for user test-user: session_id=abc-123
Cache Miss β New Session Created:
[SESSION_CREATE] Creating new profile session for user test-user
[SESSION_CREATE] Created new profile session for user test-user: session_id=def-456
Verify Fix
- Check that messages/facts can be sent even after Flask session expires
- Check logs for restoration attempts
- Verify no more "Profile session not found" errors in normal operation
- Confirm profile session is reused (not creating duplicates)
Related Files
- Backend API: api/internal/services/session_manager.go - GetSession method (line 194-341)
- Profile Handler: api/internal/handlers/profile.go - GetProfile endpoint
- Auth Route: webapp/src/routes/auth.py - OAuth callback creates initial profile session (line 112)
Notes
- The backend API's
GetSessionmethod only retrieves sessions, it does not auto-create - Profile sessions are identified by
contact_id="user-profile"in the backend - The fix maintains backward compatibility - existing profile sessions are reused
- Flask session cache is updated after successful restoration for performance