Skip to content

bug(gateway): a trailing <system-reminder> user message is taken as the turn's user message #2767

Done

Description

@Fengzdadi

Describe the bug

The gateway uses a request's last user-role message as the turn's user message. Its text is the prepare query, the block goes into it, and the captured turn starts there: turn_transcript records that message as user and only the items after it as steps. The replay anchor and the turn digest also cover the messages up to it. Five functions pick the message the same way: last_user_index, last_user_text, and turn_digest in gateway/crates/pc-gateway-core/src/executor.rs, message_anchor in replay_cache.rs, and turn_transcript in transcript.rs.

Kimi Code and ZCode add harness reminders as separate user messages, whose text is wrapped in <system-reminder>…</system-reminder>:

  • Kimi Code 2.1.1 puts them right after the prompt: the date and the PowerContext plugin's session-start skill on a session's first turn, and on every -p turn the notice that auto permission mode is active.
  • ZCode 0.16.9 adds them inside a turn's tool loop, after the tool results: after the third identical tool call (model_anomaly), and after ten assistant messages without TodoWrite (todo_reminder). The TodoWrite count runs across the session, so any long agentic turn reaches it.

From then on, the gateway takes the reminder as the turn's message. So neither host can leave prompt-time recall to the gateway yet (#2250, step 5).

Steps to reproduce

ZCode, with real processes and a scripted model:

  1. Build ZCode CLI 0.16.9 from zai-org/ZCode 29628c9a, the commit that zcode-acceptance.yml pins.
  2. In ZCode's legacy CLI config, add a provider with kind set to "openai", which ZCode maps to openai-responses, and with baseURL set to the gateway's /gateway/v1.
  3. Run the gateway built from gateway/ with PC_GATEWAY_INJECTION=on, because ZCode sends no conversation header the gateway recognizes.
    • Its Core is a PowerContext Server with inference protection in advisory mode, a target for the gateway profile, and one Memory that the prompt recalls. ZCode's Responses requests carry no store: false, so enforcing protection withholds them.
    • The upstream is a Responses model that calls one tool with the same input many times. We used ResponsesModelFixture from tests/e2e/host_models/responses.py, with the stream: false handling from feat(pi): leave prompt-time context to the gateway #2755, scripted to call search_memory 12 times.
  4. Run three headless turns in one session: --prompt … --output-format stream-json, then --resume <sessionId> for the next two. The second turn runs the tool loop.

Expected behavior

Every request of a turn uses the prompt as the turn's message. The prompt is the prepare query, and every request carries the block in the prompt message. The turn is captured with the prompt as user, with all of its tool steps, and without the reminders.

Actual behavior

ZCode, three turns and 15 upstream requests:

  • The first and third turns end with the prompt and work. Each request carries the block in the prompt message, and the captured user is the prompt.
  • The second turn has 13 requests, and only the first three carry the block. ZCode appends the model_anomaly reminder after the third call's result, and the todo_reminder after the ninth. In the last 10 requests the last user message is a reminder. No block is injected, and the prompt message goes upstream without the block it had in the first three.
  • That turn's Source (gw-turn-…) has the TodoWrite reminder as user. Since turn_transcript starts the turn at that message, the prompt and the tool steps before the reminder are not in the captured turn.

Kimi Code 2.1.1: on the affected turns, the gateway prepares with the reminder as the query, injects no block into the prompt, and captures the reminder as user. In Kimi's ACP mode, which has no auto-mode notice, the turns after the first end with the prompt and work.

A rule that covers both hosts: take the last user message that is not made up only of <system-reminder>…</system-reminder> blocks, in one helper that the five functions share.

ZCode's --force-mcs flag sends these reminders as system items, and the gateway then picks the prompt. It is not a workaround: the flag also moves the hook context and the skills listing to system items after the prompt, and turn_transcript captures them as the turn's steps.

Environment

  • Gateway built from master: ca935e1 for the ZCode runs and 2f4997d for the Kimi Code runs. The turn-message selection is the same at both.
  • ZCode CLI 0.16.9 (zai-org/ZCode 29628c9a) on Node 24.16.
  • Kimi Code 2.1.1.
  • macOS.
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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions