Bug: User message duplicated when resuming existing session from pi-cli
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):
- User sends message → optimistic add to UI
POST /api/agent/new→ creates sessioneL(sessionId)→ EventSource opens AFTER POST- Agent emits
message_startfor user message → but EventSource not yet connected, event missed - Result: user message appears once (from optimistic add)
Existing session from pi-cli (duplicate):
- User sends message → optimistic add to UI
eL(sessionId)→ EventSource opens BEFORE POSTPOST /api/agent/{id}→ sends prompt to existing session- Agent emits
message_startfor user message → EventSource receives it message_endhandler adds user message again → duplicate!- Result: user message appears twice
Steps to Reproduce
- Start a conversation in pi-cli:
pi - Send a message in pi-cli
- Open pi-web:
npx @agegr/pi-web@latest - Select the same session from the sidebar
- Send a new message in pi-web
- 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_endfor user messages (lines 375-392, 983-984 inagent-session.ts)
fixed