-
Notifications
You must be signed in to change notification settings - Fork 49.5k
fix(gateway): suppress silent placeholder replies in live chats #9956
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
luoxiao6645
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
luoxiao6645:fix/gateway-silent-placeholder-replies-9840
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from typing import Optional | ||
|
|
||
| LIVE_GATEWAY_SILENT_MARKERS = frozenset( | ||
| { | ||
| "[silent]", | ||
| "silent", | ||
| "no message", | ||
| "no reply", | ||
| "no response", | ||
| "no response generated", | ||
| "empty", | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def _unwrap_live_gateway_response_text(text: str) -> str: | ||
| normalized = text | ||
| for _ in range(6): | ||
| updated = normalized.strip() | ||
| changed = False | ||
|
|
||
| for wrapper in ("**", "__", "~~", "`"): | ||
| if updated.startswith(wrapper) and updated.endswith(wrapper): | ||
| inner = updated[len(wrapper) : -len(wrapper)].strip() | ||
| if inner: | ||
| normalized = inner | ||
| changed = True | ||
| break | ||
| if changed: | ||
| continue | ||
|
|
||
| for left, right in (("(", ")"), ("[", "]"), ("{", "}"), ('"', '"'), ("'", "'")): | ||
| if updated.startswith(left) and updated.endswith(right): | ||
| inner = updated[len(left) : -len(right)].strip() | ||
| if inner: | ||
| normalized = inner | ||
| changed = True | ||
| break | ||
|
|
||
| if not changed: | ||
| normalized = updated | ||
| break | ||
|
|
||
| return normalized | ||
|
|
||
|
|
||
| def _canonicalize_live_gateway_response(text: str) -> str: | ||
| normalized = _unwrap_live_gateway_response_text(text) | ||
| return re.sub(r"[\s\-_]+", " ", normalized).strip(" .!?:;").casefold() | ||
|
|
||
|
|
||
| def normalize_live_gateway_response( | ||
| response: Optional[str], *, failed: bool = False | ||
| ) -> str: | ||
| """Suppress placeholder silence markers before live message delivery.""" | ||
| if response is None: | ||
| return "" | ||
|
|
||
| text = str(response).strip() | ||
| if not text or failed: | ||
| return text | ||
|
|
||
| if _canonicalize_live_gateway_response(text) in LIVE_GATEWAY_SILENT_MARKERS: | ||
| return "" | ||
|
|
||
| return text |
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
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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| from types import SimpleNamespace | ||
| from unittest.mock import AsyncMock, MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| from gateway.config import Platform | ||
| from gateway.platforms.base import MessageEvent, MessageType | ||
| from gateway.response_filters import normalize_live_gateway_response | ||
| from gateway.run import GatewayRunner | ||
| from gateway.session import SessionSource | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("raw_text", "expected"), | ||
| [ | ||
| ("(No message)", ""), | ||
| ("[SILENT]", ""), | ||
| ("`(No reply)`", ""), | ||
| ("**(No response generated)**", ""), | ||
| ("(empty)", ""), | ||
| ("[SILENT] means stay quiet", "[SILENT] means stay quiet"), | ||
| ("No message received from Discord", "No message received from Discord"), | ||
| ], | ||
| ) | ||
| def test_normalize_live_gateway_response(raw_text, expected): | ||
| assert normalize_live_gateway_response(raw_text) == expected | ||
|
|
||
|
|
||
| def test_normalize_live_gateway_response_preserves_failed_output(): | ||
| assert normalize_live_gateway_response("[SILENT]", failed=True) == "[SILENT]" | ||
|
|
||
|
|
||
| def _make_runner(): | ||
| runner = GatewayRunner.__new__(GatewayRunner) | ||
| runner.config = MagicMock() | ||
| runner.session_store = MagicMock() | ||
| runner.hooks = SimpleNamespace(emit=AsyncMock()) | ||
| runner.adapters = {} | ||
| runner._show_reasoning = False | ||
| runner._session_db = None | ||
| runner._set_session_env = MagicMock(return_value=[]) | ||
| runner._clear_session_env = MagicMock() | ||
| runner._should_send_voice_reply = MagicMock(return_value=False) | ||
| runner._deliver_media_from_response = AsyncMock() | ||
| return runner | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_handle_message_with_agent_suppresses_placeholder(monkeypatch): | ||
| runner = _make_runner() | ||
|
|
||
| session_entry = SimpleNamespace( | ||
| session_id="sess-1", | ||
| session_key="key-1", | ||
| created_at=1, | ||
| updated_at=2, | ||
| was_auto_reset=False, | ||
| last_prompt_tokens=0, | ||
| ) | ||
| history = [{"role": "assistant", "content": "Earlier reply"}] | ||
|
|
||
| runner.session_store.get_or_create_session.return_value = session_entry | ||
| runner.session_store.load_transcript.return_value = history | ||
| runner.session_store.has_any_sessions.return_value = True | ||
| runner.session_store.append_to_transcript = MagicMock() | ||
| runner.session_store.update_session = MagicMock() | ||
|
|
||
| runner._run_agent = AsyncMock( | ||
| return_value={ | ||
| "final_response": "(No message)", | ||
| "messages": history, | ||
| "api_calls": 1, | ||
| "last_prompt_tokens": 0, | ||
| } | ||
| ) | ||
|
|
||
| monkeypatch.setattr("gateway.run.build_session_context", lambda *_a, **_kw: {}) | ||
| monkeypatch.setattr("gateway.run.build_session_context_prompt", lambda *_a, **_kw: "") | ||
|
|
||
| source = SessionSource( | ||
| platform=Platform.LOCAL, | ||
| chat_id="chat-1", | ||
| user_id="user-1", | ||
| user_name="tester", | ||
| ) | ||
| event = MessageEvent(text="test", message_type=MessageType.TEXT, source=source) | ||
|
|
||
| result = await runner._handle_message_with_agent(event, source, "key-1") | ||
|
|
||
| assert result == "" | ||
| appended = [call.args[1] for call in runner.session_store.append_to_transcript.call_args_list] | ||
| assert any(entry["role"] == "user" for entry in appended) | ||
| assert not any(entry.get("content") == "(No message)" for entry in appended) | ||
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.
(empty)is not an intentional-silence token on current main:gateway/run.py:11565-11575converts it to a visible exhausted-retry/model-failure explanation. Please remove this expectation so a real failure is not silently dropped.