fix(example): Fix auto-memory wrapper user ID lookup#1948
Conversation
Signed-off-by: Yuchen Zhang <yuchenz@nvidia.com>
WalkthroughThe PR refactors ChangesUser ID Retrieval Priority and Defensive Extraction
🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/nvidia_nat_langchain/src/nat/plugins/langchain/agent/auto_memory_wrapper/agent.py (1)
84-91: ⚡ Quick winConsider catching
AttributeErrorinstead of broadException.The PR description indicates the original bug was an
AttributeErrorwhenContext.user_managerwas absent. Catching the specific exception type makes the intent clearer and avoids masking unexpected errors.♻️ Proposed fix
# Priority 1: Get user_id from the runtime context. try: user_id = self._context.user_id if user_id: logger.debug(f"Using user_id from context: {user_id}") return user_id - except Exception as e: + except AttributeError as e: logger.debug(f"Failed to get user_id from context: {e}")🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/nvidia_nat_langchain/src/nat/plugins/langchain/agent/auto_memory_wrapper/agent.py` around lines 84 - 91, Replace the broad Exception catch in the block that reads self._context.user_id with a specific AttributeError handler: in the method where user_id = self._context.user_id is accessed (inside the auto_memory_wrapper/agent.py code), catch AttributeError instead of Exception so missing Context.user_manager/user_id raises are handled explicitly and other unexpected errors are not masked; keep the same logger.debug message for the AttributeError case.packages/nvidia_nat_langchain/tests/agent/test_auto_memory_wrapper.py (1)
148-154: 💤 Low valueTest covers the happy path for Priority 1 well.
Consider adding a test that verifies priority order by setting both
user_idanduser_manager, then asserting thatuser_idwins anduser_manager.get_id()is never called. This would strengthen confidence in the fallback hierarchy.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/nvidia_nat_langchain/tests/agent/test_auto_memory_wrapper.py` around lines 148 - 154, Add a new test that sets both mock_context.user_id and mock_context.user_manager (with a mock get_id) and patches Context.get to return mock_context, then call wrapper_graph._get_user_id_from_context() and assert it returns mock_context.user_id, and also assert mock_context.user_manager.get_id was never called; reference the existing test_get_user_id_from_context pattern and the _get_user_id_from_context method to locate where to add this check.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In
`@packages/nvidia_nat_langchain/src/nat/plugins/langchain/agent/auto_memory_wrapper/agent.py`:
- Around line 84-91: Replace the broad Exception catch in the block that reads
self._context.user_id with a specific AttributeError handler: in the method
where user_id = self._context.user_id is accessed (inside the
auto_memory_wrapper/agent.py code), catch AttributeError instead of Exception so
missing Context.user_manager/user_id raises are handled explicitly and other
unexpected errors are not masked; keep the same logger.debug message for the
AttributeError case.
In `@packages/nvidia_nat_langchain/tests/agent/test_auto_memory_wrapper.py`:
- Around line 148-154: Add a new test that sets both mock_context.user_id and
mock_context.user_manager (with a mock get_id) and patches Context.get to return
mock_context, then call wrapper_graph._get_user_id_from_context() and assert it
returns mock_context.user_id, and also assert mock_context.user_manager.get_id
was never called; reference the existing test_get_user_id_from_context pattern
and the _get_user_id_from_context method to locate where to add this check.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: e29edca3-1a02-4738-aefe-52511588dbd6
📒 Files selected for processing (2)
packages/nvidia_nat_langchain/src/nat/plugins/langchain/agent/auto_memory_wrapper/agent.pypackages/nvidia_nat_langchain/tests/agent/test_auto_memory_wrapper.py
Description
Updating the auto-memory wrapper to resolve the runtime user ID from
Context.user_idbefore falling back to legacy/customuser_manager,X-User-ID, anddefault_user.The previous implementation directly dereferenced
Context.user_manager, which is not present in the currentnat runcontext and caused the auto-memory wrapper examples to fail with:AttributeError: 'Context' object has no attribute 'user_manager'By Submitting this PR I confirm:
Summary by CodeRabbit
Release Notes