Skip to content

fix(chat): deduplicate forward=true boundary messages to prevent infinite pagination loop#461

Open
weiconghe wants to merge 1 commit into
DingTalk-Real-AI:mainfrom
weiconghe:fix/chat-forward-pagination-loop
Open

fix(chat): deduplicate forward=true boundary messages to prevent infinite pagination loop#461
weiconghe wants to merge 1 commit into
DingTalk-Real-AI:mainfrom
weiconghe:fix/chat-forward-pagination-loop

Conversation

@weiconghe

Copy link
Copy Markdown

Issue for this PR

Closes #430

Type of change

  • Bug fix

What does this PR do?

问题描述

chat message list / list-direct 使用 --forward=true 翻页时,上游 API 返回 createTime >= T 的消息(含边界)。当用户按文档推荐使用最后一条消息的 createTime 作为下一页的 --time 参数时,会得到完全相同的消息集合,导致无限循环。

具体文件:

  • internal/compat/dynamic_commands.go — 动态命令生成器,生成 chat message list 等命令
  • skills/mono/references/products/chat.md — 文档描述翻页方式

复现步骤(v1.0.34 实测):

# 第一页
dws chat message list --group <id> --time "2026-05-22 00:00:00" --forward=true --limit 3

# 第二页 — 用上一页最后一条的 createTime
dws chat message list --group <id> --time "2026-05-22 09:26:59" --forward=true --limit 3
# ↑ 返回完全相同的消息 ID

影响分析

  • 任何使用 --forward=true 翻页的用户/AI 都会陷入无限循环
  • 文档推荐的翻页方式直接导致此问题,影响所有依赖 chat message list 的自动化流程

修复方案

  1. 新增 chat_hooks.go:参照已有的 mail_hooks.go / todo_hooks.go 模式,新建 installChatHook,在动态命令构建时安装

    • --forward=true 时,wrap RunE 捕获 JSON 输出
    • 去除 createTime--time 锚点相同的边界重复消息
    • 在 stderr 输出翻页警告,建议使用 --forward=false
    • 非 chat 产品 / 非目标 tool 完全无影响(no-op)
  2. 更新文档:在 chat.md(mono + multi 两份)中:

    • 明确推荐 --forward=false(从新往老翻页)
    • 文档标注 --forward=true 的已知限制
    • 说明返回数组始终按降序排列

How did you verify your code works?

  • 21 个单元测试全部通过,覆盖:
    • 去重逻辑(字符串/数字 createTime、嵌套 result、无重叠、空数据)
    • Hook 安装(非 chat 产品跳过、非目标 tool 跳过、链式 RunE、错误透传)
    • 边界情况(nil cmd、空 anchor、无效 JSON)
  • 全量 compat 包测试无回归(go test ./internal/compat/ PASS)
  • 环境:Windows 11, Go 1.25.11
=== RUN   TestInstallChatHook_TargetToolForwardTrue
--- PASS
=== RUN   TestDeduplicateForwardBoundary_RemovesBoundaryMessages
--- PASS
... (21/21 PASS)

Screenshots / recordings

N/A

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…nite pagination loop (DingTalk-Real-AI#430)

When --forward=true is used with chat message list / list-direct, the
upstream API returns messages where createTime >= T (inclusive boundary).
Using the last message's createTime as the next page's --time returns
identical messages, causing an infinite loop.

Changes:
- Add chat_hooks.go: wraps RunE to intercept JSON output when
  --forward=true, removes messages whose createTime matches the --time
  anchor, and emits a stderr warning recommending --forward=false
- Add chat_hooks_test.go: 21 tests covering dedup logic, hook
  installation, error passthrough, and edge cases
- Wire installChatHook in dynamic_commands.go alongside existing
  mail/todo hooks
- Update chat.md docs (mono + multi) to recommend --forward=false for
  pagination and document the forward=true boundary limitation
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.

[Bug] chat message list 文档推荐的 forward=true 翻页方式会无限循环(v1.0.34)

1 participant