-
Notifications
You must be signed in to change notification settings - Fork 46.7k
feat(gateway): authoritative sender attribution in all chat contexts #13939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0xyg3n
wants to merge
2
commits into
NousResearch:main
Choose a base branch
from
0xyg3n:feat/sender-attribution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -304,6 +304,12 @@ def _home_target_env_var(platform_name: str) -> str: | |
| _DOCKER_VOLUME_SPEC_RE = re.compile(r"^(?P<host>.+):(?P<container>/[^:]+?)(?::(?P<options>[^:]+))?$") | ||
| _DOCKER_MEDIA_OUTPUT_CONTAINER_PATHS = {"/output", "/outputs"} | ||
|
|
||
| # Leading "[from NAME (uid:ID)]" sender attribution header. Stripped from | ||
| # inbound text before re-prepending the canonical prefix, to prevent trivial | ||
| # impersonation by pasting a fake header into a message body. Both ``uid`` | ||
| # numbers and free-form ids are tolerated. | ||
| _SENDER_PREFIX_RE = re.compile(r"^\s*\[from\s+[^\]]*\(uid:[^)]+\)\]\s*") | ||
|
|
||
| # Bridge config.yaml values into the environment so os.getenv() picks them up. | ||
| # config.yaml is authoritative for terminal settings — overrides .env. | ||
| _config_path = _hermes_home / 'config.yaml' | ||
|
|
@@ -5420,13 +5426,45 @@ async def _prepare_inbound_message_text( | |
| # concurrently preparing multimodal turns on the same runner. | ||
| self._consume_pending_native_image_paths(session_key) | ||
|
|
||
| _is_shared_multi_user = is_shared_multi_user_session( | ||
| source, | ||
| group_sessions_per_user=_group_sessions_per_user, | ||
| thread_sessions_per_user=_thread_sessions_per_user, | ||
| ) | ||
| if _is_shared_multi_user and source.user_name: | ||
| message_text = f"[{source.user_name}] {message_text}" | ||
| # Sender attribution. | ||
| # | ||
| # When enabled (default) every inbound message is prefixed with | ||
| # ``[from NAME (uid:USER_ID)] <text>`` before it reaches the agent. | ||
| # The platform ``user_id`` is the authoritative identifier; the name | ||
| # is best-effort and resolved in this order: | ||
| # | ||
| # 1. ``HERMES_USER_NAME_<user_id>`` env var (operator override) | ||
| # 2. ``source.user_name`` (platform display name) | ||
| # 3. literal ``unknown`` | ||
| # | ||
| # Rationale: group chats, channels, and threads shared by multiple | ||
| # humans are ambiguous to the agent without attribution — two users | ||
| # with the same first name, or a rename mid-conversation, become | ||
| # indistinguishable. DMs are also attributed so the prefix format | ||
| # is invariant across contexts and downstream tooling can parse it | ||
| # unconditionally. | ||
| # | ||
| # Set ``attribute_sender: false`` in gateway config to restore the | ||
| # legacy behaviour (display-name-only prefix, shared sessions only). | ||
| if getattr(self.config, "attribute_sender", True) and source.user_id: | ||
| # Strip any user-supplied text that mimics our own prefix, to | ||
| # prevent trivial impersonation by pasting a fake ``[from … ]`` | ||
| # header into the message body. | ||
| message_text = _SENDER_PREFIX_RE.sub("", message_text, count=1) | ||
| _env_name = os.environ.get(f"HERMES_USER_NAME_{source.user_id}") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds a new user-facing non-secret |
||
| _display_name = _env_name or source.user_name or "unknown" | ||
| message_text = f"[from {_display_name} (uid:{source.user_id})] {message_text}" | ||
| else: | ||
| # Legacy fallback: best-effort attribution only when the session | ||
| # is known to be shared across multiple humans and a display name | ||
| # is available. No-op in DMs and in per-user isolated sessions. | ||
| _is_shared_multi_user = is_shared_multi_user_session( | ||
| source, | ||
| group_sessions_per_user=getattr(self.config, "group_sessions_per_user", True), | ||
| thread_sessions_per_user=getattr(self.config, "thread_sessions_per_user", False), | ||
| ) | ||
| if _is_shared_multi_user and source.user_name: | ||
| message_text = f"[{source.user_name}] {message_text}" | ||
|
|
||
| if event.media_urls: | ||
| image_paths = [] | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please resolve the same canonical participant identity used by
build_session_key(source.user_id_alt or source.user_id;gateway/session.py:935). Signal provides a stable UUID inuser_id_alt(gateway/platforms/signal.py:706-708), so this condition and emitted value otherwise fail the PR's authoritative-ID guarantee on a supported platform.