Repository navigation
feat(agent): 对 Agent 最终答案做确定性 Markdown 规范化 - #275
Merged
mateaix merged 2 commits intoJun 7, 2026
Merged
Conversation
LLM 原始输出常含机械排版缺陷(ATX 标题缺空格、`---` 与内容粘连、 表格管道符未对齐、标题与表格粘连),提示词约束无法可靠预防。新增 确定性、零 token 的正则后处理 MarkdownNormalizer,在 FinalAnswerNode 收敛路径上落库/发渠道前修复这些缺陷。代码块感知(``` / ~~~ 围栏内原样 保留)、幂等。RETURN_DIRECT(逐字工具输出)与 AWAITING_APPROVAL 早返回 路径不受影响。 Closes mateaix#274
原 MarkdownNormalizer 只在行首拆分 `---`,但模型输出中 `---` 常粘在 正文之后再接标题,如 `*来源…2026-06-01*---### 二、…`、 `…逾 **90%**---### 综合判断🔍| 维度 |…`(HR+标题+表格同一行)。 新增 HR_MID_HEADING 正则,把 `前缀 --- #标题` 拆成独立行并对两侧递归, 四重粘连可层层拆开。仅在 `---` 紧跟 ATX 标题时才拆,避免误伤散文里 em-dash 风格的 `---`。粗体伪标题(`**📌Title**` 粘连正文)属语义判断, 正则无法安全自动化,仍保留在提示词约束。 Refs mateaix#274
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
在 AGENTS.md 中写明了 Markdown 输出格式规则(
#后空格、---独占行、表格管道符对齐、分隔行| --- |),但 Agent 返回的内容经常不规范(---#标题、##二、美股、表格与段落粘连、|cell|无空格)。排查确认:MateClaw 输出管道没有破坏 Markdown(流式 delta 原样累加、
scrubFakeUrls只删无效引用、渠道渲染仅折叠\n{3,}→\n\n)。畸形 Markdown 是模型自身原始输出——同一篇里时对时错正是 LLM 未严格遵守提示词规则的特征。靠提示词让 LLM 精确遵守机械排版规则本质不可靠。方案
新增确定性(纯正则、零 token、不调用 LLM)后处理
MarkdownNormalizer,在FinalAnswerNode收敛路径上、落库/发渠道前修复机械排版缺陷:##二→## 二,保护#5/#1引用)---与后续内容粘连时拆行(含行首---#标题与行内正文---#标题两种)|---|→| --- |,保留对齐冒号)安全约束:代码块感知(
```/~~~围栏内原样保留)、幂等(normalize(normalize(x)) == normalize(x))、保守(散文中散落管道符、行内#、em-dash 风格---不动)。覆盖范围:
FinalAnswer经persistOnly发出,覆盖 DB 持久化、历史重载、复制、TTS、所有 IM 渠道。RETURN_DIRECT(逐字工具输出)与AWAITING_APPROVAL早返回路径不受影响。改动
common/text/MarkdownNormalizer.java---拆分HR_MID_HEADINGagent/graph/node/FinalAnswerNode.javacommon/text/MarkdownNormalizerTest.java提交
feat(agent): deterministic Markdown normalization for final answer— 基础规范化(标题/表格/行首---)feat(agent): 扩展 Markdown 规范化覆盖 --- 行内(mid-line)粘连— 补充正文---#标题(HR+标题+表格四重粘连)拆分,仅在---紧跟标题时才拆,避免误伤散文 em-dash测试
---拆分、四重粘连链、代码块保护、幂等性、误伤防护(em-dash / 合规输入保持不变)。不在范围内(保留提示词约束)
粗体伪标题粘连正文(
2026**📌Title**正文…)、Emoji 位置、代码块语言标注属语义判断,正则无法安全自动化。Closes #274