DeepTutor icon indicating copy to clipboard operation
DeepTutor copied to clipboard

[Bug]: CJK emphasis persistence repair corrupts inline code containing dollar signs

Open QuanHongnan opened this issue 1 month ago • 2 comments

Do you need to file an issue?

  • [x] I have searched the existing issues and this bug is not already filed.
  • [x] I believe this is a legitimate bug, not just a question or feature request.

Describe the bug

版本:v1.6.15;核对提交a31d82624c27d75f4e771d4fcb78dce16470b383。此问题可在无模型、无网络环境确定性复现。

最小复现

调用_repair_chinese_emphasis_for_persistence(content, "zh"),输入合成文本:

PHP 对象:`$b = $a`;PHP 数组:`$b = $a`。

预期:输入不变,行内代码必须按字面保留。

上游原始函数实际输出如下,\u0000表示真实NUL字符的JSON转义:

"PHP 对象:`$b = \u0000CJK_PROTECTED_0\u0000b = $a`。"

它既泄漏了内部占位符,也破坏了原代码与文本边界。这是答案持久化内容完整性问题,不是单纯的Markdown显示差异。

根因

函数依次遮罩围栏代码、数学片段和行内代码,使用同一占位符列表。美元符号可能被数学正则匹配;后续行内代码匹配又把已经生成的占位符包进新的保护片段。最终单次替换恢复外层后,内层占位符不会再次恢复。

固定占位符名称还需考虑输入原本包含相同标记时的冲突。

修复方向与验收

  • 用不会反复遮罩已处理内容的解析方式,明确围栏代码、行内代码与数学片段的优先级;或使用一次互斥匹配。
  • 原子恢复保护片段,并避免与用户原文中的标记冲突。
  • 不建议仅递归替换内部标记,因为原文中的类似字符串可能被误解释。
  • 测试PHP美元变量、美元金额、代码/数学混合、围栏代码、嵌套强调、字面占位符、非中文语言旁路。
  • 代码及数学原文应逐字符保留,同时正常中文强调修复仍有效。

可直接调用上述函数验证,不需要启动Web应用或访问模型接口。

上游源码

持久化修复函数

Steps to reproduce

  1. 在已安装项目依赖的开发环境中导入 deeptutor.services.session._turn_runtime_shared._repair_chinese_emphasis_for_persistence。
  2. 使用上方合成文本调用该函数,language="zh"。
  3. 比较返回字符串和输入;检查 CJK_PROTECTED_ 占位符及 NUL 字符。无需模型、凭据或网络。

Expected Behavior

原始行内代码与文本应逐字符保持不变;持久化答案不得泄漏内部保护标记。正常的中文强调修复仍应有效。

Related Module

API/Backend

Configuration Used

language="zh";合成PHP代码文本。该问题与具体模型、供应商或学习内容无关。

Logs and screenshots

固定发布提交原始函数已确定性复现:preserved=false,leaked_placeholder=true。局部修复后同一输入逐字符保留,且不再出现内部占位符。

Additional Information

DeepTutor v1.6.15 / a31d82624c27d75f4e771d4fcb78dce16470b383。另核对 dev 提交 9ce7ba9059e3d2ae0aa5fa9427b112a4148abce0:受影响的修复函数与该发布版本一致。复现采用从原源码提取的函数,而不是仅根据模型输出推断。报告只含合成文本,不包含用户会话或凭据。

QuanHongnan avatar Oct 28 '26 07:10 QuanHongnan

Fixed on current dev. _repair_chinese_emphasis_for_persistence now uses a single left-to-right _PROTECTED_SPAN_RE (fenced code | inline code | math) instead of sequential masks, so a $ inside inline code can no longer be re-masked as math. The change came in the validated integration merge 7b307357d.\n\nI ran the exact repro from the issue on 88d46c21: input PHP 对象:\$b = $a`;PHP 数组:`$b = $a`。returns byte-identical, with no NUL and noCJK_PROTECTED_` leak. This can be closed.

Antonio-Faure avatar Oct 31 '26 12:10 Antonio-Faure

@QuanHongnan — #2115 (merged 2026-10-28) explicitly says Closes #2104, and the fix is in v1.7.1.

Could you retest on v1.7.1 and close this if the symptom is gone? If it still reproduces, a short note with the current behavior would help.

Antonio-Faure avatar Nov 06 '26 10:11 Antonio-Faure