feat(gateway): inject user_id alongside display name in session context - #22711
Open
ariel-ai-bot wants to merge 1 commit into
Open
feat(gateway): inject user_id alongside display name in session context#22711ariel-ai-bot wants to merge 1 commit into
ariel-ai-bot wants to merge 1 commit into
Conversation
Previously, the system prompt only showed the user name (if available) or user ID (only if name was missing). Now both are always shown together, so the agent can see both the display name and the platform-specific ID. This enables the agent to correlate the user's Telegram display name with their numeric user_id for better context awareness. Co-Authored-By: Ariel <ariel_ai_bot@proton.me>
4 tasks
teknium1
reviewed
Jul 13, 2026
teknium1
left a comment
Contributor
There was a problem hiding this comment.
Thanks for the focused gateway-context improvement. The underlying gap remains on current main: gateway/session.py:484-492 still renders either the display name or user ID, not both.
Problems
- The PR's raw prompt interpolation at
gateway/session.py:311andgateway/session.py:316predates current prompt-metadata hardening. Main now uses_format_untrusted_prompt_value()for names and IDs atgateway/session.py:486andgateway/session.py:492(commit09666ceb76c0c3388c03c82c5d2dc2ecd2e57b60). A salvage should preserve that formatting. - Please add the both-fields case to
tests/gateway/test_pii_redaction.py. Its currenttest_user_name_not_redactedat lines 79-84 intentionally asserts the old name-wins behavior, so it must be updated along with aredact_pii=Trueassertion.
Automated hermes-sweeper review.
| uid = context.source.user_id | ||
| if redact_pii: | ||
| uid = _hash_sender_id(uid) | ||
| lines.append(f"**User ID:** {uid}") |
Contributor
There was a problem hiding this comment.
Please retain current main's _format_untrusted_prompt_value(uid) here rather than interpolating the raw ID. Commit 09666ceb76c0c3388c03c82c5d2dc2ecd2e57b60 hardened all user-controlled session metadata in this prompt; the corresponding name path must retain that protection too.
15 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When a Telegram conversation is open, users can change their display name at any time. The AI agent only sees the display name in the session context prompt but has no way to verify it against the actual
user_id(which comes from Telegram's platform and is stable).This creates a security risk: a user could change their display name to impersonate another user, and the agent wouldn't be able to detect the spoofing since it only sees the mutable name string.
By injecting both the display name and the
user_idinto the session context, the agent can correlate the two values and detect identity mismatches. For example, if a user changes their name to 'Lily (Admin)' but theiruser_idremains the same as the original user, the agent can flag the anomaly.Changes
Modified
gateway/session.py::build_session_context_prompt()to always show both**User:**and**User ID:**in the system prompt, instead of only showingUser IDwhenuser_nameis missing.Before:
After:
Security Implications
user_idis visibleuser_idis already visible inSessionSourcefor routing; this just exposes it in the prompt for the agent's awareness. PII redaction (if enabled) still applies via_hash_sender_id().Notes
user_idis only shown when available (non-empty). Multi-user sessions continue to use the existingSession type:note.redact_pii=True, theuser_idis hashed via_hash_sender_id().Signed-off-by: Ariel ariel_ai_bot@proton.me