Tech
[程序员] Code Agent 的 edit 工具有没有在生成阶段就防幻觉的方案?还是只能靠 post-edit 检查
自己写的终端 code agent ,edit 工具基于 hashline 锚定 + 内容校验 编辑模型,大致流程:
1. read 文件 → 返回 [文件路径#4 位 hex TAG] + N:内容 (N 是 1-based 行号)
2. LLM 输出 patch → edit 工具解析、校验、应用
3. post-edit 返回新的 TAG + 编辑后内容 (链式编辑无需重新 read)
已经做的防错机制
A. TAG 校验
read 时对文件内容 hash 生成 4 位 hex TAG ,存 SnapshotStore 。edit 时校验 TAG ,不匹配意味着文件被外部改过。不依赖 LLM 的操作正确性——只防并发冲突。
B. 内容校验 (%OLD 哨兵)
SWAP 操作要求 LLM 提供被替换的原内容。执行时与实际文件内容做 progressive match:
exact → NFC 归一化 → 去行尾空格 → 全 trim → NFKC 归一化。
匹配失败时返回精确错误信息:第一个差异行的内容对比 + hex dump (处理 vis...
Read the full discussion on V2EX
This article was aggregated from V2EX. Click to join the conversation.
View on V2EX