Skip to content

fix(text): 修复 YAML 块样式序列化改写非换行分隔符 - #356

Open
openjiuwen-sync-bot[bot] wants to merge 1 commit into
openJiuwen-ai:mainfrom
openjiuwenai:sync/pr-251
Open

openjiuwen-sync-bot[bot] wants to merge 1 commit into
openJiuwen-ai:mainfrom
openjiuwenai:sync/pr-251

Conversation

@openjiuwen-sync-bot

@openjiuwen-sync-bot openjiuwen-sync-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Paired: GitHub #356 ↔ GitCode !251

问题

yaml_io._str_representer 用 str.splitlines() 拆分多行字符串,而 splitlines() 除了 \n 还会按垂直制表符、换页符、文件/组分隔符、NEL(U+0085) 以及 U+2028/U+2029 切分。这些字符在进入 emitter 之前就被改写成了 \n,导致 profile 序列化后重新加载时文本内容发生变化。

修复

  • 仅按 \n 拆分,保留行内的其他分隔符。
  • 对 YAML reader 会当作换行处理的分隔符(NEL、U+2028、U+2029)改用双引号样式输出,由 emitter 转义为 \N / \L / \P;其余分隔符由 emitter 自动回退双引号并正确转义。
  • 按 \n 拆分时结尾空串会保留最终换行,因此以换行结尾的字符串不会多出空行(相对原实现的回归已修复)。
  • 新增 tests/foundation/text/test_yaml_io.py,覆盖块样式、垂直制表符、换页符、文件/组/记录分隔符、NEL(含不带换行的情况)、U+2028/U+2029、结尾换行、行尾空白与单行字符串,共 10 个用例。

验证

  • ruff check / ruff format --check 通过
  • pytest tests/foundation/text/test_yaml_io.py 10 passed
  • 与 main 逐字符对比:上述分隔符在 main 上均被改写为 \n,本 PR 修复为无损往返

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


zxy12_zxy seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@openjiuwen-collaboration-bot

Copy link
Copy Markdown
Contributor

head_sha: c4a68a15b9cf8d8d5b90fa880ede3915ca611492

变更摘要

本次 PR 修复了 chrys/foundation/text/yaml_io.py 中 _str_representer 在序列化多行字符串时错误改写非换行分隔符的问题:原先使用 str.splitlines() 拆分文本,会把换页符、垂直制表符、NEL(U+0085) 以及 U+2028/U+2029 一并当作换行处理并改写为 \n,导致 profile 序列化后重新加载时文本内容失真。修改后仅按 \n 拆分,并对 YAML reader 会识别为换行的特殊分隔符改用双引号样式输出,以 \N / \L / \P 转义实现无损往返;同时新增回归测试文件覆盖相关场景。

主要改动

  • 新增需双引号输出的字符集合 _BLOCK_STYLE_UNSAFE: 定义 "\x85\u2028\u2029" 作为不宜使用块样式的字符集合,并配注释说明 YAML reader 会把它们当作换行、块样式会静默改写为 \n。
  • _str_representer 提前分支到双引号样式: 当字符串包含上述分隔符时,调用 dumper.represent_scalar("tag:yaml.org,2002:str", data, style='"') 输出,由 emitter 转义为 \N / \L / \P,保证可无损往返。
  • 多行清理逻辑改为仅按 \n 拆分: 将 data.splitlines() 改为 data.split("\n") 后再逐行 rstrip() 拼接,避免垂直制表符、换页符、NEL 与 Unicode 分隔符在进入 emitter 前被改写为真实换行。
  • 新增 tests/foundation/text/test_yaml_io.py 回归测试: 通过 _roundtrip 辅助函数覆盖块样式保留、换页符、NEL、U+2028/U+2029、行尾空白剥离以及单行字符串等场景。

@openjiuwen-collaboration-bot

openjiuwen-collaboration-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

head_sha: c4a68a15b9cf8d8d5b90fa880ede3915ca611492

代码审查

✅ 未发现问题

@openjiuwen-collaboration-bot

Copy link
Copy Markdown
Contributor

head_sha: c4a68a15b9cf8d8d5b90fa880ede3915ca611492

任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ✅SUCCESS 点此跳转
ST测试 N/A N/A
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A
issue关联检查 N/A N/A
pr size检查 N/A N/A

The block-style representer split multi-line strings with str.splitlines(), which also breaks on form feed, vertical tab, NEL and the Unicode line/paragraph separators. Those characters were silently rewritten to \n before reaching the emitter, so reloading the profile changed its text. Split on \n alone, and emit strings containing the separators that YAML's reader treats as line breaks in double-quoted style so they round-trip faithfully.

Signed-off-by: zxy12_zxy <419500457@qq.com>
@openjiuwen-collaboration-bot

Copy link
Copy Markdown
Contributor

head_sha: ff7927aeaca9200949af4755cf3e23e3bdfa3b16

任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ✅SUCCESS 点此跳转
ST测试 N/A N/A
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A
issue关联检查 N/A N/A
pr size检查 N/A N/A

@openjiuwen-collaboration-bot

Copy link
Copy Markdown
Contributor

head_sha: ff7927aeaca9200949af4755cf3e23e3bdfa3b16

@zxy12_zxy

[P2] 多行 profile 保存时仍会丢掉行末分隔符。 split("\n") 保留了行内字符,但随后调用的 line.rstrip() 会删除行末的垂直制表符、换页符和 U+001C–U+001E。我用实际的 Agent Profile 保存和加载路径,以及 ACP 的 profile 解析路径复现了 Page 1\f\nPage 2 被改成 Page 1\nPage 2。这些字符单独成行或出现在多行文本末尾时也会丢失。建议让它们在裁剪前进入现有的双引号转义分支,并补上这几种位置的往返测试。yaml_io.py:46

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant