Skip to content

repair_message_sequence aggressively merges separate user messages (v0.16.0+) #45560

Open

Description

@jackebox-alt

hermes-message-alternation-bug-report.md

The model sees a single blob of text and picks the earliest question to answer, completely unaware the user has moved on. The user must wait for each agent reply before sending the next message, or the agent will keep chasing old questions forever. This is especially painful on instant messaging platforms like QQ where users naturally send quick follow-ups.

Activity

  1. added
    type/bugSomething isn't working
    comp/agentCore agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint
    P2Medium — degraded but workaround exists
    on Jun 13, 2026
  2. AIalliAI commented on Jun 13, 2026

    @AIalliAI

    This looks genuine. repair_message_sequence (agent/agent_runtime_helpers.py) joins consecutive user messages with \n\n into a single turn. That's needed to satisfy strict role-alternation providers (Anthropic/OpenAI reject back-to-back user turns), but it erases the boundary between semantically distinct messages — so when a user fires several follow-ups (common on IM platforms before the agent replies), the model receives one blob and tends to answer the earliest question, exactly as you describe.

    The text content is preserved (the merge is honest about that), but the turn structure isn't, and it appears amplified by the context-compaction interactions in #44837 and #43062 (both closed, same "user messages merged into single turn" symptom) and the open #43066. A fix probably wants to keep the original message objects in session history and only collapse for the wire copy at API-call time (or use an explicit boundary marker), so alternation is satisfied without losing per-message semantics. PR-able.

  3. kairosys commented on Jun 28, 2026

    @kairosys

    Hi everyone, I recently ran into this exact same issue while deploying Hermes Agent via the Telegram adapter connected to vLLM (Gemma 4). I want to share my debugging findings and a working hotfix that resolves both the message boundary destruction and the DB cursor desync.

    The Context & Root Cause:
    When users interact via IM platforms (like Telegram or Discord), they frequently send multiple short, consecutive messages (e.g., Msg 1: "Is the server up?", Msg 2: "Check the cron jobs").

    In agent/agent_runtime_helpers.py, the repair_message_sequence function attempts to enforce strict role alternation (to prevent 400 Bad Request from strict providers like Anthropic or vLLM). In Pass 2, it merges consecutive user messages using string concatenation.
    While this satisfies the API provider, it destroys the semantic boundaries of the user's distinct prompts. Furthermore, because the array length shrinks during this merge, it breaks the database flush cursor (which relates to Issue #43066), causing subsequent assistant replies to be dropped from the session history.

    The Workaround (Hotfix):
    Instead of aggressively merging the user messages and shrinking the array, we can safely bypass the provider's strict alternation check by injecting a "dummy" empty assistant message between consecutive user messages.

    Here is the modification I made in agent/agent_runtime_helpers.py (inside Pass 2):

        # Pass 2: Merge consecutive user messages -> CHANGED: Inject dummy assistant message
        merged: List[Dict] = []
        for msg in filtered:
            if (
                merged
                and isinstance(msg, dict)
                and msg.get("role") == "user"
                and isinstance(merged[-1], dict)
                and merged[-1].get("role") == "user"
            ):
                # Inject a dummy assistant turn to satisfy strict alternation without merging
                merged.append({"role": "assistant", "content": "\u200B"})
                merged.append(msg)
                repairs += 1
            else:
                merged.append(msg)
  4. GottZ commented on Jul 28, 2026

    @GottZ

    This was generated by AI during triage.

    Summary

    Four PRs were compared, but only #46459 and #63298 directly address #45560. #46459 adds textual boundary markers while still collapsing canonical turns; #63298 preserves separate prompts through FIFO queueing, canonical history, and SessionDB, then intentionally strips source metadata and merges adjacent users only in the transient provider copy with a boundary marker, while #31496 and #66027 fix different gateway merge defects.

    Related pull requests

    Duplicates

    #46459 and #63298 overlap on #45560 and the provider-visible boundary-marker concept, but #63298 is the comprehensive superseding implementation; #31496 and #66027 are related only through gateway message merging and are not duplicates.

    Suggested consolidation

    Merge #63298 after final maintainer review: it addresses both destructive canonical user-turn merging and the TUI busy-queue source-boundary loss, while preserving strict-provider compatibility through a wire-only merge with a textual marker—not source identity in provider serialization. Close #46459 as superseded by #63298 despite its keep_open review because its diff leaves canonical turns collapsed and omits the TUI queue root cause; keep #31496 and #66027 triaged separately rather than closing them as duplicates.

    Complex graph

    flowchart LR
        classDef open fill:#dbeafe,stroke:#1d4ed8,color:#1e3a8a
        classDef merged fill:#dcfce7,stroke:#15803d,color:#14532d
        classDef closed fill:#e5e7eb,stroke:#6b7280,color:#1f2937
        classDef unverified fill:#f3f4f6,stroke:#9ca3af,color:#374151
        classDef best stroke-width:3px,stroke:#b45309
        classDef target stroke-width:3px,stroke:#4338ca
        I45560(["issue #45560 (open)"])
        subgraph Dup46459 ["PRs duplicating each other"]
            P46459["PR #46459 (open)"]
            P63298["PR #63298 (open)"]
        end
        P46459 -.->|partial| I45560
        P63298 -->|best fix| I45560
        class I45560 open
        class I45560 target
        class P46459 open
        class P63298 open
        class P63298 best
        click I45560 "https://github.com/NousResearch/hermes-agent/issues/45560"
        click P46459 "https://github.com/NousResearch/hermes-agent/pull/46459"
        click P63298 "https://github.com/NousResearch/hermes-agent/pull/63298"
    
    Loading

    Graph: solid arrow = fixes / best fix, dashed arrow = partial or unverified (see edge label); boxed group = PRs duplicating each other; amber border = best fix; indigo border = target; gray node = closed or no verify verdict yet (state tag in the node label).

    Cross-PR triage: Reviewed 4 pull requests and 1 issue in this complex. Each diff was read against this issue; Assessment working set: 168 kB of PR diffs, 10 kB of issue/PR text, 14 kB of discussion (18 comments), 6 verify verdicts. verdicts reflect diff content, not PR titles. Part of an automated triage batch.

  5. added
    needs-decisionAwaiting maintainer decision before any implementation
    sweeper:risk-session-stateSweeper risk: may lose/corrupt/mis-associate session or context state
    area/sessionsSession lifecycle, resume, persistence, history
    on Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existsarea/sessionsSession lifecycle, resume, persistence, historycomp/agentCore agent runtime: loop, agent_init, prompt builder, context-compression, responses endpointneeds-decisionAwaiting maintainer decision before any implementationsweeper:risk-session-stateSweeper risk: may lose/corrupt/mis-associate session or context statetype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions