pi-web icon indicating copy to clipboard operation
pi-web copied to clipboard

Bug: User message duplicated when resuming existing session from pi-cli

Open IA20201 opened this issue 6 months ago • 1 comments

Bug: User message duplicated when resuming existing session from pi-cli

Description

When continuing a conversation that was started in pi-cli inside pi-web, the user message appears twice in the chat UI. New sessions created entirely in pi-web do NOT have this issue.

Root Cause

pi-coding-agent emits message_start / message_end events for all messages, including user messages (see agent-session.ts lines 375-392 and 983-984).

pi-web's EventSource handler adds messages to the UI on message_end:

case "message_end": {
    let t = e.message;
    t && j(e => [...e, ej(t)]);  // adds ANY message, including user messages
}

The race condition depends on when the EventSource is opened relative to the prompt:

New session (no duplicate):

  1. User sends message → optimistic add to UI
  2. POST /api/agent/new → creates session
  3. eL(sessionId) → EventSource opens AFTER POST
  4. Agent emits message_start for user message → but EventSource not yet connected, event missed
  5. Result: user message appears once (from optimistic add)

Existing session from pi-cli (duplicate):

  1. User sends message → optimistic add to UI
  2. eL(sessionId) → EventSource opens BEFORE POST
  3. POST /api/agent/{id} → sends prompt to existing session
  4. Agent emits message_start for user message → EventSource receives it
  5. message_end handler adds user message again → duplicate!
  6. Result: user message appears twice

Steps to Reproduce

  1. Start a conversation in pi-cli: pi
  2. Send a message in pi-cli
  3. Open pi-web: npx @agegr/pi-web@latest
  4. Select the same session from the sidebar
  5. Send a new message in pi-web
  6. Observe: the user message appears twice

Expected Behavior

User message should appear exactly once, regardless of whether the session was started in pi-cli or pi-web.

Suggested Fix

In the message_end event handler, skip user messages (they are already added optimistically):

case "message_end": {
    let t = e.message;
    // Skip user messages - they are already added optimistically in the send handler
    if (t && t.role !== "user") {
        j(e => [...e, ej(t)]);
    }
    C({type:"reset"});
    ei({kind:"waiting_model"});
    break;
}

Environment

  • pi-web: 0.6.12
  • pi-coding-agent: 0.75.5
  • Provider: xiaomi-token-plan-cn / mimo-v2.5
  • OS: Windows

Additional Context

  • This is a UI-only issue — the session JSONL file is correct
  • New sessions created entirely in pi-web are NOT affected
  • Only sessions started in pi-cli and continued in pi-web exhibit the duplicate
  • The root cause is the EventSource opening order difference between new and existing sessions
  • Related: pi-coding-agent emits message_start/message_end for user messages (lines 375-392, 983-984 in agent-session.ts)

IA20201 avatar May 27 '26 19:05 IA20201

fixed

agegr avatar Jun 04 '26 14:06 agegr