Skip to content

feat(gateway): inject user_id alongside display name in session context - #22711

Open
ariel-ai-bot wants to merge 1 commit into
NousResearch:mainfrom
ariel-ai-bot:feat/session-user-id
Open

feat(gateway): inject user_id alongside display name in session context#22711
ariel-ai-bot wants to merge 1 commit into
NousResearch:mainfrom
ariel-ai-bot:feat/session-user-id

Conversation

@ariel-ai-bot

@ariel-ai-bot ariel-ai-bot commented May 9, 2026

Copy link
Copy Markdown

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_id into 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 their user_id remains 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 showing User ID when user_name is missing.

Before:

**User:** 🌸 𝓛𝓲𝓵𝔂 🌸

After:

**User:** 🌸 𝓛𝓲𝓵𝔂 🌸
**User ID:** <user_id>

Security Implications

  • Identity verification: Agent can now detect when a user's display name changes mid-conversation
  • Prompt injection mitigation: Makes it harder for a user to impersonate another identity since the immutable user_id is visible
  • Privacy: user_id is already visible in SessionSource for 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_id is only shown when available (non-empty). Multi-user sessions continue to use the existing Session type: note.
  • This is a non-breaking change — it only adds information, doesn't remove or change existing behavior.
  • Compatible with PII redaction: when redact_pii=True, the user_id is hashed via _hash_sender_id().

Signed-off-by: Ariel ariel_ai_bot@proton.me

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>
@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter labels May 9, 2026

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:311 and gateway/session.py:316 predates current prompt-metadata hardening. Main now uses _format_untrusted_prompt_value() for names and IDs at gateway/session.py:486 and gateway/session.py:492 (commit 09666ceb76c0c3388c03c82c5d2dc2ecd2e57b60). A salvage should preserve that formatting.
  • Please add the both-fields case to tests/gateway/test_pii_redaction.py. Its current test_user_name_not_redacted at lines 79-84 intentionally asserts the old name-wins behavior, so it must be updated along with a redact_pii=True assertion.

Automated hermes-sweeper review.

Comment thread gateway/session.py
uid = context.source.user_id
if redact_pii:
uid = _hash_sender_id(uid)
lines.append(f"**User ID:** {uid}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@teknium1 teknium1 added sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform area/sessions Session lifecycle, resume, persistence, history labels Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/sessions Session lifecycle, resume, persistence, history comp/gateway Gateway runner, session dispatch, delivery P3 Low — cosmetic, nice to have platform/telegram Telegram bot adapter sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants