WeKnora/internal/agent
linus-liu-web abc365f919
fix(wiki): 唯一写出口 UpdatePage 拦住丢表格行的机器重写(agent 整页写入不再静默改短页面) (#3793)
* fix(wiki): 在唯一写出口 UpdatePage 拦住丢表格行的机器重写

模型被要求把整页重新吐一遍时,markdown 表格是最常出错的地方:一百行的
证书台账回来只剩三十行,finish_reason=stop、没有任何报错,页面历史里就是
一次普通编辑。守卫因此加在 UpdatePage —— 所有调用方共用的唯一写出口,
而不是某一个调用方。

- 按「每行第一个非空单元格」的身份集合比较新旧内容(去掉强调符号、折叠
  空白、统一大小写):换行、重排、改日期/金额列都不算丢行,只有一行指向
  的实体整个消失才算;
- 同一身份出现两次按多重集比较,丢掉其中一行同样是丢;
- 表头行(`| --- |` 的上一行)不参与比较,模型改列名不算丢行。

有意的缩短放行:人在页面上删行(user)、回滚到更短的旧版本(revert),
以及调用方用 types.WithWikiShrinkAllowed 明确标注的写入 —— ingest 的
retract 轮(源文档被删除,被删文档名下的行本来就该消失)与 agent 的精确
文本替换(old_text 必须逐字命中现有内容,删除是有意为之)各自在自己的
写入上打这个标记。

agent 的整页写入拿到的是错误而不是静默成功:否则模型会以为改了一页、其实
一个字节都没动;它可以把那几行重新吐出来,或改用精确替换工具。

测试:身份比较、多重集、表头跳过、散文中孤立竖线不算表格;sqlite 上的
UpdatePage 拒绝写入且版本不推进、四类放行都能写进去、正常重写照常落库。
反向验证两组:去掉豁免 → 4 个放行用例 FAIL;让身份比较失效 → 6 个用例
FAIL(含两个 UpdatePage 用例);文件逐字节还原后全 PASS。

* fix(wiki): 拒写要向调用方明说,否则 trim 会删掉没落地的文档

UpdatePage 的守卫对非 agent 来源是**静默拒写**(返回现有页面 + nil
error)—— 页面本身没坏,这个选择是对的。但对 ingest 这条调用链来说,
"静默"本身就是个洞:

  reduceSlugUpdates 拿到 err == nil
    → 返回 changed=true, err=nil
    → 调用方那句 `if reduceErr != nil { collectUnapplied(updates) }` 走不到
    → trim 阶段认为这个 slug 已经成功合并
    → 把它背后的文档删掉
    → 而这些文档的贡献从来没写进页面,下一批也不会再回来

collectUnapplied 是"这些文档别删、下一批再来"的唯一入口,全仓只有三个
调用点(lockErr / !acquired / reduceErr != nil),没有一个能覆盖静默拒写。

修法不改 UpdatePage 的契约:拒写时它返回的是**未经改动的页面**,所以
调用方比较写入前后的 Version 就能判断,然后把 updateDeferred 一路带出去,
交给 collectUnapplied —— 与 #3617 的 updateDeferred 语义一致。

补的是 #3617 里这块语义;守卫本身仍在唯一写出口(不改架构)。
本提交只补连线,没有新增单测:这条路径要 asynq + chat model 的整套夹具,
而"静默拒写返回 nil"这一前提已由 TestUpdatePageRefusesARewriteThatDropsTableRows 覆盖。

* fix(wiki): 表格行守卫按去重身份集算比例,拒写改用显式信号

- agent 整页更新仍是任一身份缺失即拒写;ingest(未显式允许缩减)改为
  丢失超过旧内容唯一身份数 20% 才拒写,user/revert/显式允许缩减不变
- wikiWriteMissingRowIdentities 改为集合比较:同身份的两行合成一行不再
  算丢失;身份只取首个非空单元格,注释与日志都写明这个边界
- UpdatePage 的拒写统一返回 ErrWikiWriteDroppedTableRows;reduce 用
  errors.Is 区分"守卫拒写"与"成功但版本未变",不再拿版本号当信号,
  只有确实未落地的贡献才保留关联文档重试

---------

Co-authored-by: linus-liu-web <11640009+linus-liu-web@users.noreply.github.com>
Co-authored-by: linus-liu-web <linus-liu-web@users.noreply.github.com>
2026-10-09 10:55:28 +08:00
..
approval refactor(agent): discover and invoke MCP tools on demand 2026-09-08 12:01:36 +08:00
compaction feat: index and recall images with multimodal embedding models (#3683) 2026-10-08 18:19:53 +08:00
skills feat(lite): install and run skills on the host (#3661) 2026-09-24 21:30:34 +08:00
token refactor(models): rebuild vendor integration on protocol + catalog + vendor layers (#3470) 2026-09-20 21:28:23 +08:00
tools fix(wiki): 唯一写出口 UpdatePage 拦住丢表格行的机器重写(agent 整页写入不再静默改短页面) (#3793) 2026-10-09 10:55:28 +08:00
act.go fix(agent): recover tool panics instead of crashing the server 2026-09-25 18:22:14 +08:00
act_order_test.go fix(agent): recover tool panics instead of crashing the server 2026-09-25 18:22:14 +08:00
calibration.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
calibration_test.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
checkpoint.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
const.go fix(models): keep Retry-After on HTTPError and honour it in agent LLM retries 2026-09-29 01:22:15 +08:00
const_test.go fix(browser): support human handoffs and resume interrupted tasks 2026-09-11 23:56:13 +08:00
context_debug.go refactor(models): rebuild vendor integration on protocol + catalog + vendor layers (#3470) 2026-09-20 21:28:23 +08:00
engine.go feat(sandbox): run unpinned Lite sessions on the host OS sandbox (#3549) 2026-09-22 19:35:03 +08:00
engine_image_test.go feat: auto-describe MCP tool result images via VLM 2026-03-25 21:38:15 +08:00
engine_memory_test.go feat(memory): add cross-session long-term memory 2026-08-13 18:00:02 +08:00
engine_test.go fix(llm): treat cut-off streams as broken and tighten LLM retries 2026-09-25 18:22:14 +08:00
finalize.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00
grounding_prompt.go fix(agent): search bound knowledge bases before answering, and pass suggested-question origins (#3400) 2026-09-18 17:00:47 +08:00
grounding_prompt_test.go fix(agent): search bound knowledge bases before answering, and pass suggested-question origins (#3400) 2026-09-18 17:00:47 +08:00
knowledge_base_profile_prompt_test.go feat(knowledge): derive knowledge base descriptions from document profiles (#3370) 2026-09-17 22:21:55 +08:00
langfuse_test.go refactor(agent): introduce modelcontext registry and fix handle-codec bugs 2026-07-27 11:05:43 +08:00
length_truncation_test.go fix(agent): end the turn when the completion cap truncates an answer (#3449) 2026-09-20 13:17:50 +08:00
mcp_exposure_test.go fix(mcp): normalize JSON-encoded argument envelopes 2026-09-09 16:11:52 +08:00
mcp_proxy_test.go fix(mcp): normalize JSON-encoded argument envelopes 2026-09-09 16:11:52 +08:00
observe.go refactor(models): rebuild vendor integration on protocol + catalog + vendor layers (#3470) 2026-09-20 21:28:23 +08:00
observe_test.go fix(agent): search bound knowledge bases before answering, and pass suggested-question origins (#3400) 2026-09-18 17:00:47 +08:00
prompt_composition_test.go fix(agent): search bound knowledge bases before answering, and pass suggested-question origins (#3400) 2026-09-18 17:00:47 +08:00
prompts.go feat(sandbox): run unpinned Lite sessions on the host OS sandbox (#3549) 2026-09-22 19:35:03 +08:00
prompts_browser.go fix(browser): align BrowserSkill 0.3.0 and repair task lifecycle 2026-09-17 11:54:12 +08:00
prompts_browser_test.go feat(browser): polish connection settings, open tasks in a new window, and streamline prompts (#3193) 2026-09-11 21:28:58 +08:00
prompts_shell_test.go feat(sandbox): run unpinned Lite sessions on the host OS sandbox (#3549) 2026-09-22 19:35:03 +08:00
prompts_wiki.go fix(wiki): 整页重写被补全预算截断时续写,续写用尽则拒绝写回(长名单 146→119) (#3582) 2026-09-23 12:17:07 +08:00
prompts_wiki_test.go refactor(agent): introduce modelcontext registry and fix handle-codec bugs 2026-07-27 11:05:43 +08:00
retry_delay_test.go fix(models): keep Retry-After on HTTPError and honour it in agent LLM retries 2026-09-29 01:22:15 +08:00
steer.go fix(chat): preserve steering continuity and composer focus 2026-09-09 20:15:23 +08:00
steer_test.go fix(chat): preserve steering continuity and composer focus 2026-09-09 20:15:23 +08:00
terminal_event_test.go fix(agent): stop emitting terminal answer events for retryable empty stops 2026-09-14 21:02:15 +08:00
think.go fix(agent): 工具自起 goroutine 的 panic 自行兜底,MCP 预热不再带走进程 (#3846) 2026-10-08 01:24:36 +08:00
tool_goroutine_panic_test.go fix(agent): 工具自起 goroutine 的 panic 自行兜底,MCP 预热不再带走进程 (#3846) 2026-10-08 01:24:36 +08:00
tool_images.go fix(browser): complete task cancellation, screenshots, and result display (#3206) 2026-09-12 09:32:26 +08:00
tool_images_test.go refactor(models): separate provider definitions, model catalogs, and runtime configuration (#3589) 2026-09-23 14:20:08 +08:00
turn_usage_test.go feat(agent): size agent history by the window and calibrate token estimates (#3413) 2026-09-18 22:51:31 +08:00