Fix: Stop the client dropping chat messages that begin with e

CChatManager::OnReceive opened with the same heartbeat guard the other
managers use, `if (szBuffer[0] == TOKEN_HEARTBEAT) return;`, before it
looked at the length. TOKEN_HEARTBEAT is 101, which is also the letter e,
and this channel carries free text rather than command bytes, so every
message starting with e -- "echo", "error", anything -- was silently
discarded. The sender never learned: the server's chat dialog had already
written the text into its own log, so the two sides disagreed about what
was said.

The guard is dead code here. Heartbeats travel on the main connection
only, as a multi-byte [101][Heartbeat] packet consumed by the main
dispatcher; the server never sends one on a chat sub-connection. Delete
it and keep the length check that follows.

The server keeps prepending a space when the GBK payload starts with
101, because already-deployed clients still carry the guard and losing
the message outright is worse than an invisible leading space. Note the
same guard, harmless today, still sits in the machine, file, proxy and
screen-hide managers.

Verified with the Debug instance and a local Debug client: with the
server's space temporarily disabled, chat_send "echo test" and
"second line" both showed up verbatim in the client dialog (read back
with WM_GETTEXT); with the space restored the first arrives as
" echo test". Details in docs/Mcp_Chat_Design.md section 5.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 14:57:47 +01:00
1 parent cc3e11d30d
commit 1c5fab1645
3 files changed
+24 -10

No files matched your search

+5 -1
View File
@@ -465,7 +465,11 @@ INT_PTR CALLBACK CChatManager::ChatDialogProc(HWND hDlg, UINT uMsg, WPARAM wPara
VOID CChatManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
{
if (szBuffer[0] == TOKEN_HEARTBEAT) return;
// 这里没有心跳包:心跳只走主连接([TOKEN_HEARTBEAT][Heartbeat],且由主连接分发消费),
// 服务端从不给交谈子连接发心跳。原先照搬其它 manager 的
// if (szBuffer[0] == TOKEN_HEARTBEAT) return;
// 会在任何长度判断之前丢掉首字节为 101('e') 的包——而本通道承载的是自由文本,
// 于是「echo」「error」这类以 e 开头的消息被静默吞掉。删掉即可,保留长度判断。
if (ulLength == 1) {
switch (szBuffer[0]) {
+13 -4
View File
@@ -95,9 +95,12 @@ chat_close(id, sid)
## 5. 客户端怪癖与规避(协议不变)
1. **首字节 101 ('e') 被丢弃**:`client/ChatManager.cpp` 在任何长度判断之前执行
`if (szBuffer[0] == TOKEN_HEARTBEAT) return;`。服务端在转换后的 GBK 首字节为
101 时前置一个空格(显示上无感)。若客户端日后修复此行,可移除该规避。
1. **首字节 101 ('e') 被丢弃(客户端已修)**:`client/ChatManager.cpp` 原在任何长度
判断之前执行 `if (szBuffer[0] == TOKEN_HEARTBEAT) return;`,而该通道承载自由文本,
于是「echo」「error」这类以 e 开头的消息被静默吞掉。交谈子连接上没有心跳(心跳只在
主连接、且是 `[101][Heartbeat]` 多字节包,由主连接分发消费),这行是照搬其它 manager
的死防御,已删除。服务端**仍保留**前置空格规避:老客户端还带这个缺陷,多一个看不见的
空格好过静默丢消息。验证见本章末尾。
2. **窗口创建前的文本被丢弃**:客户端收到 `[0]` 后 `WaitForDialogOpen` 只
`Sleep(150)`,随后才在线程里 `CreateDialogParamW`;`OnReceive` 仅在 `m_hWnd`
非空时显示文本。服务端把 `readyAt` 设为绑定后 1000ms,`chat_open` 等满再返回,
@@ -138,6 +141,12 @@ chat_close(id, sid)
间切换,`Script.rc` 因此直接写英文(德语系统看到的是英文而非乱码)。协议与服务端
逻辑不变。
**首字节 'e' 修复的验证(2026-10-25,Debug 实例 + 本机 Debug 客户端)**:验证时把
`McpChat.cpp` 的前置空格临时关掉(否则测不到客户端行为),`chat_open` 后
`chat_send "echo test"` 与 `"second line"` 各一条,再从服务端读客户端窗口文本
(`EnumWindows` → `WM_GETTEXT`):两条都完整出现在聊天记录里,`echo test` 无前导空格。
恢复前置空格后复测,显示为 `" echo test"`(多一个空格但不丢),两条路径都通过。
## 6. 服务端状态机
数据结构(`McpServer.h`,受 `m_ChatMutex` 保护):
@@ -209,7 +218,7 @@ using ChatSessionPtr = std::shared_ptr<ChatSession>;
| 2 | `chat_open(id)` | VM 约 1s 内弹出「远程交谈」;返回 32 位 hex `session_id` |
| 3 | `chat_send` 中文;用户回中文;`chat_wait` | VM 显示「对方: …」;返回 UTF-8 原文 |
| 4 | 连续多轮、用户连发两条 | 下一轮批量返回,顺序正确,`dropped=0` |
| 5 | 发送 `"echo test"`(首字节 'e') | 正常显示(前置空格规避生效) |
| 5 | 发送 `"echo test"`(首字节 'e') | 新客户端原样显示;老客户端靠前置空格仍能显示(`" echo test"`) |
| 6 | open 后立刻 send,重复 10 次 | 第一条不丢(1000ms 建窗等待) |
| 7 | 4000 / 4001 字节、emoji | 边界内成功;超长与 emoji 返回 `-32602` |
| 8 | 无回复时 `chat_wait` | 到点 `timed_out:true`,会话仍活 |
+6 -5
View File
@@ -7,9 +7,9 @@
// → 双向文本 [CP936 字节][NUL],无 token 前缀
// → 服务端 [COMMAND_CHAT_CLOSE=1] 关闭窗口
//
// 客户端怪癖(见 docs/Mcp_Chat_Design.md §4):
// 1. ChatManager.cpp 在任何长度判断前丢弃首字节为 TOKEN_HEARTBEAT(101, 'e') 的包,
// 故 GBK 首字节为 101 时前置一个空格。
// 客户端怪癖(见 docs/Mcp_Chat_Design.md §5):
// 1. 老客户端 ChatManager.cpp 在任何长度判断前丢弃首字节为 TOKEN_HEARTBEAT(101, 'e')
// 的包,故 GBK 首字节为 101 时前置一个空格(新客户端已删掉那行守卫,空格只为兼容)。
// 2. 窗口由子线程 CreateDialogParamA 创建,收到 [0] 后仍有约 150ms 建窗延迟;
// 绑定后 1000ms 内不返回 chat_open,也不接受 chat_send。
//
@@ -419,8 +419,9 @@ static std::string BuildChatSend(const Json::Value& id, const Json::Value& args,
" GBK bytes (limit " + std::to_string(kMaxSendBytes) + ")");
}
// 客户端在长度判断之前丢弃首字节为 TOKEN_HEARTBEAT(101, 'e') 的包。
// 前置一个空格规避,显示上无感(2026-10 客户端 ChatManager.cpp:281)。
// 兼容老客户端:它们在任何长度判断之前丢弃首字节为 TOKEN_HEARTBEAT(101, 'e') 的包
// (ChatManager::OnReceive 里照搬来的心跳守卫)。新客户端已删掉那行,这里保留前置空格
// 是为了不区分客户端版本——多一个看不见的空格,好过老客户端静默丢消息。
if ((BYTE)gbk[0] == (BYTE)TOKEN_HEARTBEAT) gbk.insert(gbk.begin(), ' ');
std::vector<char> packet(gbk.begin(), gbk.end());