Skip to content

[codex] fix DeepSeek stream delta dedupe - #66

Closed
RuszLi wants to merge 1 commit into
NIyueeE:mainfrom
RuszLi:codex/ds-state-dedup-delta
Closed

[codex] fix DeepSeek stream delta dedupe#66
RuszLi wants to merge 1 commit into
NIyueeE:mainfrom
RuszLi:codex/ds-state-dedup-delta

Conversation

@RuszLi

@RuszLi RuszLi commented May 12, 2026

Copy link
Copy Markdown
Contributor

Fix DeepSeek streaming replay by treating snapshot-like fragment updates as snapshots and emitting only new suffix content. Also suppresses repeated append fragments that would resend the same visible text through Feishu/OpenClaw streaming. Scope: only src/openai_adapter/response/state.rs. Validation: staged diff checked with git diff --cached --check; local Rust tests were not run because this machine is missing a usable Rust toolchain; Docker rebuild and /health check passed earlier.

@RuszLi
RuszLi marked this pull request as ready for review May 12, 2026 16:34
@RuszLi
RuszLi requested a review from NIyueeE as a code owner May 12, 2026 16:34
@NIyueeE

NIyueeE commented May 13, 2026

Copy link
Copy Markdown
Owner

感谢提交这个PR,我们做了深入调研,包括分析实际 SSE 流和 DeepSeek 前端的 JS 源码,结论是这个PR是过度设计的。

实际流验证

我们抓取了 5 个不同场景的实际流(简单问答、DEEP_SEARCH + 引用、思考中中断、输出中中断、thinking=OFF),所有 response/fragments/-1/content 的更新全都是增量追加,从未出现过全量 snapshot 替换。旧代码把 bare v 统一当作 APPEND 处理是正确的。

JS 源码验证

chat.deepseek.com 的 main.js 中找到了前端的状态更新引擎:

// o 字段的 schema 定义:仅有 3 个合法值
o: union(["SET", "BATCH", "APPEND"])

// 状态更新引擎
switch (o) {
case "SET":    target[prop] = value;           break;  // 直接替换
case "APPEND": target[prop] += value;          break;  // 纯字符串拼接
default:       unreachable;                    break;  // 其他值 = bug
}

关键发现:

  1. o 只有 3 个值:SETBATCHAPPEND,没有其他操作符
  2. APPEND 对字符串就是 +=,纯增量追加,不存在 snapshot 替换语义
  3. 前端默认分支是 unreachable,意味着 o 为其他值或不合法会被视为 bug
  4. BATCH 在更高层展开为子项,每个子项仍然走 SET/APPEND

结论

PR 引入的 looks_like_snapshotsnapshot_deltaappend_deltarepeated_fragment_delta 四个函数在防御一个不存在的场景。实际协议行为比这简单得多:内容更新永远是 APPEND(+=),没有中途的全量 snapshot 替换,也没有重复重放。

所以我们决定关闭这个PR,保持旧代码不变。

@NIyueeE

NIyueeE commented May 13, 2026

Copy link
Copy Markdown
Owner

基于实际流抓包和 DeepSeek JS 源码分析确认:协议中内容更新只有 APPEND(),不存在 snapshot 替换场景。旧代码的 bare → APPEND 处理是正确的。PR 的四个去重函数防御的是不存在的场景,属于过度设计。详见上方评论。

@NIyueeE NIyueeE closed this May 13, 2026
NIyueeE added a commit that referenced this pull request May 13, 2026
- Rewrite state.rs to match DeepSeek frontend DeltaParser algorithm
  (p/o persistence, BATCH recursive decomposition, path prepending)
- Fix INCOMPLETE status not triggering stream termination in converter
- Remove PR #66's over-engineered snapshot dedup (behavior doesn't exist in protocol)
- Add current_op tracking to DsState for correct bare v routing
- Tighten health_check to validate SSE content and detect muted accounts
- Add runtime biz_code detection for SSE streams (mute/error at request time)
- Init-failed accounts now added to pool as Invalid (visible in frontend)
  instead of silently dropped; startup no longer blocked on all-fail
- Remove 3x retry loop from init_account (failures handled by recovery task)
- Bump X-Client-Version default to 2.1.0, add vision model_type
- Align docs/deepseek-api-reference.md with actual API responses
  (PARSING status, VISION model_kind, fragment-level BATCH patterns,
   INCOMPLETE terminal state, no event:finish in practice)
- Update e2e runner to derive model list from config.toml dynamically
- Fix clippy warnings and cargo fmt issues
@RuszLi

RuszLi commented May 13, 2026 via email

Copy link
Copy Markdown
Contributor Author

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