Skip to content

fix(gateway): turn-start detection uses role+content matching (ignoring metadata) - #90087

Open
JinUltimate1995 wants to merge 2 commits into
NousResearch:mainfrom
JinUltimate1995:fix/turn-start-detection-89891
Open

fix(gateway): turn-start detection uses role+content matching (ignoring metadata)#90087
JinUltimate1995 wants to merge 2 commits into
NousResearch:mainfrom
JinUltimate1995:fix/turn-start-detection-89891

Conversation

@JinUltimate1995

@JinUltimate1995 JinUltimate1995 commented Aug 19, 2026

Copy link
Copy Markdown

Summary

Fixes #89891: _response_messages_turn_start_index in api_server.py used full dict equality to match the prefix, which fails when the agent modifies messages during the conversation loop.

This caused _turn_transcript_messages to return ALL messages instead of only the current turn, bloating the run.completed SSE event.

Root Cause

The agent modifies messages during the conversation loop:

  • _repair_message_sequence merges/rewrites messages
  • _drop_trailing_empty_response_scaffolding removes trailing messages
  • Message fields are added (finish_reason, reasoning, timestamp, etc.)

After these modifications, the prefix match fails and returns 0, causing all messages to be returned instead of just the current turn.

Fix

Compare role + content (with prefix tolerance for truncated strings), ignoring metadata fields. This handles:

  • Timestamps added by the agent
  • Content truncation (prefix matching for long strings)
  • Additional metadata fields (finish_reason, reasoning, etc.)

Changes Made

  • gateway/platforms/api_server.py: Replace dict equality with _match() helper that compares role + content
  • tests/gateway/test_api_server.py: Add TestTurnStartDetection class with 8 regression tests

How to Test

  1. pytest tests/gateway/test_api_server.py::TestTurnStartDetection -v — 8 passed
  2. pytest tests/gateway/test_api_server.py -q — 117 passed
  3. Fail-on-main verified: tests fail on unmodified main and pass with the fix

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

JinUltimate and others added 2 commits August 19, 2026 03:50
…ronment

For stdio catalog entries using auth.type: api_key, the credentials
declared in auth.env were saved to .env by _prompt_env_vars() but never
wired into the generated mcp_servers.<name> configuration. Without an
env-backed reference, _build_safe_env() excluded them and the stdio child
started without its API key, causing silent authentication failures.

Now _build_server_config translates each auth.env variable into a safe
${VAR} reference for stdio transports, mirroring how HTTP api_key
manifests get a Bearer header template.

Fixes NousResearch#89316.
…atching (ignoring metadata)

The prefix-matching logic used full dict equality (==), which fails when
the agent modifies messages during the conversation loop — timestamps are
added, content may be truncated, and fields like finish_reason/reasoning
are stamped on. This caused _turn_transcript_messages to return ALL
messages instead of only the current turn, bloating the run.completed
SSE event's messages field.

Fix: compare role + content (with prefix tolerance for truncated
strings), ignoring metadata fields. Fixes NousResearch#89891.
@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery comp/cli CLI entry point, hermes_cli/, setup wizard tool/mcp MCP client and OAuth provider/openai OpenAI / Codex Responses API P2 Medium — degraded but workaround exists sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state duplicate This issue or pull request already exists labels Aug 19, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Duplicate of #70695 for the Responses turn-start detection repair. This PR also bundles a separate MCP catalog auth.env change for #89316, so that portion should be preserved independently if maintainers salvage it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists provider/openai OpenAI / Codex Responses API sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state tool/mcp MCP client and OAuth type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: _response_messages_turn_start_index prefix matching fails when agent modifies messages

2 participants