Skip to content

fix(chat): 未发出的纯文本 assistant 历史投影为内部想法,避免旁听自持循环 - #125

Open
xueyutan wants to merge 2 commits into
lendevs:masterfrom
xueyutan:fix/unspoken-thought-projection
Open

xueyutan wants to merge 2 commits into
lendevs:masterfrom
xueyutan:fix/unspoken-thought-projection

Conversation

@xueyutan

Copy link
Copy Markdown

问题

某一轮里模型没有调用发言工具(say / react),而是直接输出了正文,这条消息会以「无 tool_calls 的 assistant 纯文本」形式留在会话历史中。

ChatContext.project_entries() 把它原样投影回下一轮上下文。模型于是看到「上一轮 assistant 已经说过话」,形成 few-shot 示范,后续回合继续用同样格式写字、不调用 say。表现出来就是:群里在聊,它一直旁听;面板回合卡片里 finish_reason=stop 且没有 say 步骤,正文全被当成内部想法丢掉。

历史里这类条目越多,示范越强,自持循环越难打破——它不会自己恢复,只能清库或改投影。

复现

  • 前置:某一轮 assistant 返回纯文本、无 tool_calls(未走 outlet)
  • 之后:每轮上下文都携带该条目,模型持续模仿「不调用工具直接写字」
  • 本地实测(0.2.7 + OpenAI 兼容端点):带 3 条此类历史的会话,改前 3/3 只吐正文、finish_reason=stop;改后 5/5 finish_reason=tool_calls,正常调用 say

改动

ChatContext.project_entries() 增加一步 _unspoken_thought:

  • role == 'assistant' 且没有 tool_calls、content 为非空字符串 → 投影为 role: 'user',正文前加一行说明:这是上一轮未发出的内部想法,不是发言,不要模仿这种写法
  • 带 tool_calls 的 assistant 条目保持原样,保证紧随其后的 tool 消息仍有对应的前置 assistant 调用可配对
  • 空正文、非字符串 content 不动

再加三个单测覆盖上面三条分支。

影响面

  • 只改上下文投影:不写库、不改配置格式、不改工具定义、不动提示词文件(所以不需要 replay 评测)
  • 存储里消息的 role 不变,只在每次投影时转换;已有历史自动生效,无需迁移
  • 如果把这类条目改标成 assistant 继续留着、只加前缀,模型仍会学到「assistant 能直接写字」,所以这里必须换成 user

验证

python -m compileall -q src/len_bot     # 通过

新增 tests/test_next_unspoken_thought.py,三个用例在打过同一改动的本地安装上 3/3 通过。

如实说明:提交环境里没有装好项目依赖(无法 uv sync),完整 pytest / ruff 没能在本地跑,需要 CI 复核;行为侧的改动前后对照是在真实 QQ 会话上做的。

未确认

  • 只在 OpenAI 兼容协议(DeepSeek)上验证过 finish_reason=stop 这条路径,没覆盖其它模型的异常终止组合
  • 另一种做法是写入时就不落这类消息。那样语义更干净,但会丢失审计和重新生成所需的信息,所以这次选在投影层处理——想听你的意见

模型某一回合没调用发言工具时,会话历史里会留下一条纯文本的
assistant 记录。project_entries() 把它原样回放,模型因此把自己
未发出的文字当成已经发出的回复,后续回合继续模仿这个形态、
不再调用 say / react,表现成只旁听不说话。

投影时把这类记录改标为只读的用户备注,模型仍能看到自己想过什么,
但不再模仿这种写法;带 tool_calls 的记录保持原样,避免 tool 结果
失去对应的 assistant 调用。
… clipping

- media/images.py: sample up to 6 evenly spaced frames from an animated image
  into a 3x2 contact sheet instead of describing only the first frame.
  Sticker collection explicitly keeps a single frame so the sticker library
  and candidate sheets are not polluted by grids.
- next/media/images.py + next/chat/tools.py: pass the frame mode through the
  subprocess worker, rename the misleading animated_first_frame_only field,
  and tell the vision model to read the grid as one scene over time.
- prompts/next_response_choice.md: when the peer sends only an image with no
  text, answer with a single sticker via react; screenshots, error dumps and
  walls of text still get a normal reply.
- next/launcher.py: add CREATE_NO_WINDOW for the Windows host child process
  so the service no longer opens a persistent console window; CTRL_BREAK
  based shutdown keeps working.
- next/platform/messages.py: clip group cards longer than 16 characters,
  fall back to the QQ number for blank cards, and skip the empty-body line.

Verified against the 0.2.7 release package: all 22 local patches apply
cleanly, compileall passes. No automated tests are included yet.

@LEN5010 LEN5010 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢排查和复现。这个 PR 现在包含两个提交,第二个(dcc87795)和标题无关,麻烦拆出去单独提。下面分开说。

提交 1:纯文本 assistant 投影

纯文本结尾是设计好的不发言方式:next_mind.md 写明普通文字是内部想法,不需要回应就直接结束本轮;next_response_choice.md 也把旁听列为完整的选择。现在的前缀 不要模仿这种写法,要开口必须调用 say 工具 会把所有正常的旁听记录变成反例,模型容易在不该说话时开口。我这边实例里的这类记录基本都是正常旁听,被直接 @ 时也几乎都有回复,目前还看不出这个改动的收益能抵消副作用。

希望这样调整:

  1. 投影后的说明改成中性描述,只说明这是上一轮没有发出的想法,不写禁止模仿和必须调用 say。文本放进 src/len_bot/prompts/ 下的模板文件,让面板的提示词设置能覆盖。
  2. 按 CONTRIBUTING 先把问题提炼成回放用例,再做改动前后对照。至少覆盖三类:被 @ 时应该回答、群友之间闲聊时应该旁听、连续几轮旁听后被点名。最好在复现问题的 DeepSeek 和另一个模型上各跑一次。
  3. 测试不直接调用私有方法 _unspoken_thought,也不断言提示词原文,改成通过 project_entries 检查投影结果的角色,以及工具调用和结果的配对。

关于写入时就不存这类消息:建议继续存,压缩回想和换角色交接都要读这些记录,在投影层处理就好。

提交 2:建议拆成独立 PR,逐项处理

  • 动图抽帧拼图:方向可以。look 处理过的图片会缓存复用,升级前缓存的动图只有第一帧,升级后也会被标成 animated_grid: true,需要处理这部分旧数据。
  • 群名片截断:_display_name 里的 card or nickname 在名片是纯空格时会选中名片,去掉空格后返回 None,有昵称也只显示 QQ 号。另外请去掉代码里的 [local patch] 标记。
  • Windows CREATE_NO_WINDOW:注释提到的桌面启动程序不在仓库里,上游的启动方式是 run.cmd / run.ps1 和计划任务。子进程拿到独立的隐藏控制台后,launcher 发出的 CTRL_BREAK_EVENT 可能传不到宿主,宿主日志也可能不再显示在终端窗口里。需要在 Windows 上实测 Ctrl+C 停止、面板重启和部署包启动之后再考虑合并。
  • 只发图就回表情的提示词:和群聊里的旁听规则冲突,群友随手发张图也可能被接一张表情。这条先不收;如果确实需要,请附上回放对照。

另外,提交信息请沿用仓库的中文惯例。

This branch has not been deployed

No deployments
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.

2 participants