Skip to content

fix: 重启/恢复会话后微信重复发送历史消息 - #5

Open
midmirror wants to merge 1 commit into
shenjiecode:mainfrom
midmirror:fix/duplicate-messages
Open

fix: 重启/恢复会话后微信重复发送历史消息#5
midmirror wants to merge 1 commit into
shenjiecode:mainfrom
midmirror:fix/duplicate-messages

Conversation

@midmirror

Copy link
Copy Markdown

Closes #4

问题

微信桥接在 omp/pi 会话重启或恢复(omp -c)后,向微信重复发送历史回复。两个独立根因:

  1. getUpdates 游标仅存内存src/client.ts):重启后游标丢失,空游标轮询导致服务端重放全部历史消息,agent 重新处理并重发回复。
  2. agent_end 补发逻辑src/index.ts):allReplies.slice(sentCount) 在全量 event.messages 上切片。恢复会话后 event.messages 含整个会话历史,sentCount 只计本次增量 → 每次对话都把全部历史回复补发到微信。

修复

  • src/auth.ts / src/client.ts:游标持久化到 cursor.json,初始化时恢复;已见消息 id 持久化到 seen-ids.json(游标丢失/过期时的兜底去重);按 messageId 对入站消息去重。
  • src/index.ts:移除 agent_end 补发。message_end 已逐条增量发送,补发是冗余且危险的通道。
  • 清理随之失效的 sendRepliesToWechat / extractAllAssistantReplies 及对应测试。
  • README(en/zh/ja/ko)更新发送机制描述。

验证

  • tsc --noEmit 0 错误
  • npm test 42/42 通过
  • 实测:恢复会话后重启,不再重发任何历史消息;游标跨重启生效

Two root causes caused WeChat users to receive previously-sent replies
repeatedly after restarting the agent session:

1. getUpdates cursor was in-memory only. After a restart the client
   started polling with an empty cursor, so the WeChat iLink Bot server
   replayed all buffered history messages and the agent re-processed
   them (duplicate replies). Persist the cursor (cursor.json) and restore
   it on init.

2. The agent_end catch-up sent `allReplies.slice(sentCount)` where
   allReplies was extracted from the full event.messages. When a session
   is resumed (omp -c / continue), event.messages contains the entire
   conversation history, so every historical assistant reply was treated
   as "not yet sent" and re-sent to WeChat on every turn. Remove the
   catch-up replay: message_end already delivers each assistant message
   incrementally. Also drop the now-unused sendRepliesToWechat /
   extractAllAssistantReplies and their tests.

Also persist seen message ids (seen-ids.json) as a safety net for
cursor loss/expiry.

Verified: tsc --noEmit clean, 42/42 tests pass.
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