You are working on the hyperswitch repository (Rust payment processing system).…
- User
You are working on the hyperswitch repository (Rust payment processing system).
REPOSITORY SETUP:
- Repository: juspay/hyperswitch
- Working directory: ./repos/hyperswitch_pool_8 (already cloned)
- Base commit: 776bde00efce8be35e0c63c1bdc6215921babddf
- Task ID: juspay__hyperswitch-7870
- Version: v1.113.0
TASK DESCRIPTION: Bug: Add support for caching and resolving last used lineage context
Currently, after a user logs in and switches between merchants or profiles, their context is not remembered across sessions.
Each new login resolves the default role from the DB and starts with a clean slate.This leads to:
- Poor user experience for users with multiple roles
- Repetitive DB queries for every login
- No continuity across logins for previously active merchant/profile context
Objective
Introduce a mechanism to cache the last used lineage context (
org_id,merchant_id,profile_id,role_id,tenant_id) in Redis and use it for JWT generation on subsequent logins.Expected Behavior
- During login:
- If lineage context exists in Redis → use it to validate and generate the JWT.
- If not found or invalid → fallback to default DB-based user role resolution.
- During merchant/profile switch:
- Store the latest lineage context in Redis for future use.
- Redis entries should have a TTL of 7 days to auto-expire for inactive users.
DETAILED CONTEXT & HINTS:
Type of Change
- Bugfix
- New feature
- Enhancement
- Refactoring
- Dependency updates
- Documentation
- CI/CD
Description
This PR introduces lineage context caching to enhance login and account-switch flows with better continuity and reduced DB load.
Key Changes
-
During Login:
- Attempt to fetch the last used
LineageContextfrom Redis (LINEAGE_CONTEXT_<user_id>). - If context is found:
- Validate whether a matching user role exists (using both
V1andV2role versions). - If a valid user_role is found → use this lineage context to issue the JWT token.
- If not → fallback to the default user_role-based resolution logic.
- Validate whether a matching user role exists (using both
- If context is not found in cache, fallback to the existing login flow entirely.
- Attempt to fetch the last used
-
During Account Switch:
- After switching org/merchant/profile context and issuing the new JWT token,
the new
LineageContextis saved to Redis (LINEAGE_CONTEXT_<user_id>). - This allows restoring the last used org/merchant/profile on future logins.
- After switching org/merchant/profile context and issuing the new JWT token,
the new
-
Redis Cache Expiry:
- Cached lineage context entries are stored with a TTL of 7 days.
- If the user is inactive beyond this duration, the system reverts to default logic.
Additional Changes
- This PR modifies the API contract
- This PR modifies the database schema
- This PR modifies application configuration/environment variables
Motivation and Context
- Improves user experience by remembering their last used account context across sessions.
- Reduces unnecessary DB lookups during login for returning users.
- Ensures fallback safety in case of invalid or expired lineage context.
How did you test it?
Tested locally with TTL as 1 minute, keys are getting stored in redis
[image: Screenshot 2025-04-22 at 7 41 43 PM] open
Test 1:
- Log in to the merchant account.
- Check redis using the key:
GET LINEAGE_CONTEXT_<user_id>The value should contain the last used lineage context for the user. - Switch to another org/merchant/profile.
- Recheck redis with the same key:
GET LINEAGE_CONTEXT_<user_id>. The lineage should now be changed.
Test 2:
- After the above test, log out and log in back, within the TTL for the lineage_context, which is 7 days.
- You should be logged in to exactly the same lineage which you last used.
Test 3 (optional):
- Have a note of the lineage.
- Do not use this account for 7 days.
- Try logging in now.
- You should be logged in to a random account (possibly the lineage which you have not used last)
- This proves, that the key has expired and the default fallback login is now used.
Checklist
- I formatted the code
cargo +nightly fmt --all - I addressed lints thrown by
cargo clippy - I reviewed the submitted code
- I added unit tests for my changes where possible
CRITICAL INSTRUCTIONS - WORK FAST AND EFFICIENTLY:
-
Create DENSE, ACTION-FOCUSED todos IMMEDIATELY
- Make todos specific and actionable
- Focus on HIGH-IMPACT actions only
- 5-8 todos maximum - be surgical, not exhaustive
-
Execute aggressively:
- Use bash commands to search efficiently
- Read multiple related files in quick succession
- Make ALL related changes in one edit
- Skip unnecessary exploration
-
File operations:
- All repo files: './repos/hyperswitch_pool_8/'
- Use targeted edits over whole file rewrites
-
NO TESTING UNLESS CRITICAL:
- Focus on making the actual code changes
- Only test if explicitly required
-
Maximize information density:
- Each turn should accomplish significant work
- Batch file reads when examining related code
- Use search commands to locate code quickly
EFFICIENCY IS CRITICAL. Make substantial progress every turn.
Assistant
I'll implement the lineage context caching feature for the Hyperswitch repository. Let me start by understanding the codebase structure and identifying the relevant files for login and context management.
First, I'll explore the repository structure to locate authentication and context-related code:
Assistant
Let me examine the login flow and context management more closely, particularly looking at user authentication and the areas where lineage context would need to be implemented.
Assistant
(no content)
Assistant
Now let me examine the core user authentication logic to understand how login currently works:
Assistant
Let me search for the signin function specifically to understand how login currently works:
Assistant
(no content)
Assistant
(no content)
Assistant
Let me also look at the switch functions to understand how lineage context is managed during switches:
Assistant
Let me look at the authentication structures to understand how tokens and lineage are handled:
Assistant
Let me look for lineage context definitions and Redis integration:
Assistant
Let me search for the domain structure to understand the user roles and authentication flow better:
Assistant
(no content)
Assistant
(no content)
Assistant
Let me search for existing Redis integration or similar caching patterns in the codebase:
Assistant
Let me check the cache implementation:
Assistant
Now let me look for how the Redis connection is managed:
Assistant
Let me check the settings file to understand Redis configuration:
Assistant
Let me now look at how the user flow works in detail to understand where to implement the lineage context:
Assistant
Let me search for the NextFlow implementation to understand how tokens are generated:
Assistant
(no content)
Assistant
Let me now check the switch functions to understand the current user context flow:
Assistant
Let me now create a plan for implementing the lineage context caching functionality. I'll create a detailed todo list to guide my implementation.
Assistant
I'll now implement the lineage context caching functionality. Let me first create a LineageContext struct to represent the lineage information that should be cached.
Assistant
I need to first create the directory and then create the file:
Assistant
Let me instead add this functionality directly to the user domain module. First, let me check how the user domain module is structured to see where to add this:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-4-5-20250929Compare side by side →
claude-sonnet-4-5-20250929Compare side by side →
claude-sonnet-4-5-20250929Compare side by side →
claude-sonnet-4-5-20250929Compare side by side →
claude-sonnet-4-5-20250929Compare side by side →
claude-sonnet-4-5-20250929Compare side by side →
claude-sonnet-4-5-20250929Compare side by side →
claude-sonnet-4-5-20250929Compare side by side →
claude-sonnet-4-5-20250929Compare side by side →
claude-sonnet-4-5-20250929Compare side by side →
claude-sonnet-4-5-20250929Compare side by side →