Skip to content

fix: preserve first user message in new sessions - #171

Open
SsparKluo wants to merge 1 commit into
lehhair:mainfrom
SsparKluo:fix/new-session-first-message
Open

SsparKluo wants to merge 1 commit into
lehhair:mainfrom
SsparKluo:fix/new-session-first-message

Conversation

@SsparKluo

Copy link
Copy Markdown
Contributor

Problem

In a newly created session, the first user message can disappear from the chat until the page is refreshed.

The race is between prompt_async and the initial getSessionMessages snapshot: the snapshot can be read before the user message is committed. The live message then gets overwritten by that stale snapshot.

The multi-server session-key migration also exposed a second issue: createSession() returns a raw session ID, while the message store and SSE pipeline use serverId::sessionId. The new-session send path used the raw ID for setStreaming and its fallback pull, so those writes went to a key the UI never reads.

Fix

  • Scope the newly created session immediately with makeSessionKey(paneServerId, newSession.id).
  • Use the scoped key for streaming state and fallback message/part updates.
  • During an initial load, preserve local-only messages delivered by SSE when they are not older than the loaded snapshot.
  • Keep older, out-of-window local messages discardable.

Verification

  • npx vitest run — 670 tests passed
  • npx tsc --noEmit — passed
  • Added regression coverage for the new-session key scope and stale initial snapshot race.

新建会话发送路径持有 createSession 返回的裸 id,而 UI/SSE/loadSession
一律用 serverId::sessionId 复合键:setStreaming 与 1500ms 兜底拉取全部
落到 UI 永远不读的裸键上。初始消息快照一旦读于发送提交之前
(prompt_async 立即返回、用户消息异步落库),setMessages 会把 SSE
刚推来的用户消息整表擦掉,兜底又写错键,只能靠刷新恢复。

- sendMessageNow 创建会话后立即以 paneServerId 合成复合键,
  兜底的 handleMessageUpdated/handlePartUpdated 写入同样带 scope
- mergeWithLocalStreamingMessages 在初始加载在途(loading)时也保留
  本地独有消息,且仅限不早于快照最新的消息(窗口外旧历史照常丢弃),
  不再依赖 prompt_async 响应先于快照响应到达的时序假设
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant