Skip to content

fix(gateway): prevent duplicate messages on no-message-id platforms#6853

Closed
KUSH42 wants to merge 2 commits into
NousResearch:mainfrom
KUSH42:feat/gateway-no-edit-sentinel
Closed

fix(gateway): prevent duplicate messages on no-message-id platforms#6853
KUSH42 wants to merge 2 commits into
NousResearch:mainfrom
KUSH42:feat/gateway-no-edit-sentinel

Conversation

@KUSH42

@KUSH42 KUSH42 commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

On platforms that accept a message send but return no `message_id` (Signal, GitHub webhooks with `github_comment` delivery), `GatewayStreamConsumer` was re-entering the "first send" path on every tool-call boundary (`on_delta(None)`). Each segment reset cleared `_message_id`, `_accumulated`, and `_last_sent_text`, so the next text chunk triggered a fresh `adapter.send()` — posting one platform message per tool call. This is what caused 155 PR comments under a single response.

The fix: when `_message_id == "no_edit"` (the sentinel set by `_send_or_edit` when a platform returns no ID), skip the segment-break reset. State is preserved so all continuation text accumulates and is delivered once via `_send_fallback_final` at stream end.

Platforms with real message IDs (Telegram, Discord, Slack) are unaffected — their `_message_id` is never `"no_edit"` so the reset still fires normally.

Also makes `prompt_toolkit` imports in `hermes_cli/commands.py` optional so gateway and test environments that lack the package can import `resolve_command`, `gateway_help_lines`, and `COMMAND_REGISTRY` without error.

Related Issue

N/A

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🔒 Security fix
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)
  • 🎯 New skill (bundled or hub)

Changes Made

  • `gateway/stream_consumer.py`: guard the segment-break reset with `self._message_id != "no_edit"` — one-line change to the `if got_segment_break:` condition
  • `hermes_cli/commands.py`: wrap `prompt_toolkit` imports in a `try/except ImportError` with stub fallbacks so non-CLI environments can import the module
  • `tests/gateway/test_stream_consumer.py`: add `test_no_message_id_segment_breaks_do_not_resend` — 3-segment stream on a no-ID platform asserts exactly 2 sends (initial + fallback), not 3

How to Test

  1. Configure a webhook delivery target that returns no `message_id` (e.g. `github_comment`)
  2. Send a prompt that triggers multiple tool calls in one turn
  3. Confirm only one final message is posted, not one per tool boundary

Or run the new test directly:

```
pytest tests/gateway/test_stream_consumer.py::TestSegmentBreakOnToolBoundary::test_no_message_id_segment_breaks_do_not_resend -v
```

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run `pytest tests/ -q` and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: Ubuntu 24.04

Documentation & Housekeeping

  • I've updated relevant documentation (README, `docs/`, docstrings) — or N/A
  • I've updated `cli-config.yaml.example` if I added/changed config keys — or N/A
  • I've updated `CONTRIBUTING.md` or `AGENTS.md` if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Screenshots / Logs

N/A

KUSH42 added 2 commits April 10, 2026 01:26
Platforms that don't return a message_id after the first send (Signal,
GitHub webhooks) were causing GatewayStreamConsumer to re-enter the
"first send" path on every tool boundary, posting one platform message
per tool call (observed as 155 PR comments on a single response).

Fix: treat _message_id == "__no_edit__" as a sentinel meaning "platform
accepted the send but cannot be edited". When a tool boundary arrives
in that state, skip the message_id/accumulated/last_sent_text reset so
all continuation text is delivered once via _send_fallback_final rather
than re-posted per segment.

Also make prompt_toolkit imports in hermes_cli/commands.py optional so
gateway and test environments that lack the package can still import
resolve_command, gateway_help_lines, and COMMAND_REGISTRY.
- test_background_autocompletes: pytest.importorskip("prompt_toolkit")
  so the test skips gracefully where the CLI dep is absent

- test_run_agent_progress_stays_in_originating_topic: update stale emoji
  💻 → ⚙️ to match get_tool_emoji("terminal", default="⚙️") in run.py

- test_internal_event_bypass{_authorization,_pairing}: mock
  _handle_message_with_agent to raise immediately; avoids the 300s
  run_in_executor hang that caused the tests to time out
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #7123. Both commits were cherry-picked onto current main with your authorship preserved. Great catch on the duplicate message bug — thanks!

@teknium1 teknium1 closed this Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants