Skip to content

fix(reader): 嵌套解释带上层语境,命中祖先时不再自循环 - #5

Merged
2019wakeup merged 2 commits into
2019wakeup:mainfrom
liujieranjerry-lgtm:fix/nested-explain
Nov 23, 2026
Merged

2019wakeup merged 2 commits into
2019wakeup:mainfrom
liujieranjerry-lgtm:fix/nested-explain

Conversation

@liujieranjerry-lgtm

Copy link
Copy Markdown
Collaborator

症状

在概念卡片里选中词条再解释时会出现「套娃」:

  • 选 A 得到卡片 A,A 的正文里出现词条 B,对 B「再解释」得到卡片 B(parentId = A);
  • 在 B 的正文里再对 A「再解释」,回来的却是 A 自己那张卡片 —— 看起来像 A 和 B 互相解释、绕成死循环。

根因(两处,都不在模型)

  1. 提示词里没有上层卡片。 explainConcept 只把 parentId 存进新概念(index.js 约 397 行),从未使用它;buildQuickExplainPrompt / buildExplainPrompt 不知道读者是从哪张卡片下来的,所以嵌套解释等于重新从零解释,和上层解释重复。
  2. 去重键是全局的词条名。 新词条按 normalizeTerm(term) 全局查重,命中就原样返回已有卡片。于是「对上层词条再解释」必然把那张上层卡片拿回来,而它和当前下钻路径无关,观感就是自循环。

修改

服务端

  • lib/explain.js:两个解释提示词新增可选 ancestors。有上层卡片时,user 段追加「上层解释(从远到近)」列表(定义截断到 120 字),system 段追加一条规则:若词条本身是上层概念或别名,不要重复其定义,改说明它在这段话里具体指什么、与上层的差别。ancestors 为空时,提示词与改动前逐字节一致(有测试断言)。
  • index.js:新增 MAX_ANCESTOR_DEPTH = 4 与 ancestorChain(concepts, startId)(visited Set 防环,缺失 id 直接停,返回从远到近)。explainConcept 在去重后、调用模型前计算祖先链;若命中的 existing 就是链上某张卡片,直接返回 { concept: existing, reused: true, ancestor: true, route: 'ancestor', chain: [...] }——不调模型、不写 store。新建概念与 full 升级两条路径都把 ancestors 传进提示词。

客户端

  • createConcept 识别 result.ancestor:仍然打开那张卡片,但带上 hint,并给出 toast 说明「这个词本链路上层已有解释,已为你打开原卡片(不重复生成)」;正常新卡片路径不变。
  • 卡片渲染在链路面包屑下面加一行 11px 说明(仅当 card.hint 存在)。
  • i18n.js 增加 label.ancestorRevisit 的中英文案。

规模:5 个产品文件 + 3 个测试文件,无新依赖、无 store schema 变更、无新路由。

二阶影响(直接后果之外)

  • 重复解释的花费消失了一半。 命中祖先时不发模型请求,嵌套越深省得越多;而正常嵌套解释现在只发一次带上下文的请求,不会再加一次「去重后重解释」。
  • 提示词变长,quick 路径的预算要跟上。 最多 4 层祖先 × 120 字定义约 +500 字,仍在 maxTokens: 900 的输入侧承受范围内(输出侧未变);有测试断言 prompt 长度 < 1200,防止祖先定义无界膨胀。
  • route 语义扩展。 之前只有 quick / full / cached,现在多一个 ancestor。客户端只按 result.ancestor 布尔分支,route 仍是给诊断和日志看的;debug.lastSent 的写入保持原样(不调模型就不记),所以 lastSent 与调用次数仍一一对应。
  • 链路不再可能成环。 parentId 只在新卡片创建时指向已存在的卡片,新建 id 唯一,本不可能成环;ancestorChain 仍带 visited 集合,迁移或手改 store 造成的环也只会截断不会挂死(有测试)。
  • 与「嵌套最多 3 层」的上限关系不变。 客户端仍拒绝第 4 层下钻;祖先链最多 4 层只是服务端防御,不改变用户可见的上限。

三阶影响(模块 / 用户行为 / 复杂度 / 维护成本)

  • 用户行为: 下钻遇到已解释的上层词条时,得到的是「回到原卡片 + 一句原因」,而不是一次静默的重复。这会减少重复卡片,也让 概念 栏更干净 —— 卡片总量少,去重压力小,导出与检索的噪声也随之下降。
  • 其他模块: buildExplainPrompt 同时服务 full 升级路径,因此升级一张嵌套卡片时同样知道上下文;buildContextPrompt(同词条跨书语境)刻意不改——它回答的是「这本书怎么用它」,与层级无关,改动它会把两种语义混在一起。buildAskPrompt(追问)也不动,追问已经带着完整概念字段。
  • 系统复杂度: 新增状态只有卡片对象上瞬时的 hint,不落盘、不参与 fingerprint,因此不影响标注/概念锚点的重建逻辑,也不影响 normalizeDocs / 迁移。
  • 维护成本: 祖先规则集中在 lib/explain.js 的一个常量和 ancestorChain 里;将来若要支持「链式摘要」或「显示全部祖先」,只需扩展这两处。代价是提示词多了一个可空参数,两处调用都要记得传——已由 host 测试(断言 prompt 里出现上层列表)和 prompt 测试(断言空 ancestors 下逐字节不变)双向夹住。
  • 已知未做(有意): 祖先命中时返回的是原卡片,而不是「在这段语境里的差异说明」;后者需要新字段(例如 variants[])和新 UI,属于另一个功能,先不引入。若之后要做,route: 'ancestor' 已经是天然的挂点。

验证

  • node --test test/*.test.mjs → 254/254
  • node eval/run.mjs → 13/13
  • node eval/layout-host.mjs → 6/6
  • node eval/visual.mjs → 118/119(唯一失败项是既有的 markdown relative images load through the file route,与本改动无关,在纯净 main 上也失败)
  • node scripts/build-client.mjs 幂等,构建后工作区只剩既有的本地 package.json 改动(未提交)
  • 新增测试:test/host.test.mjs 3 例(嵌套解释把上层卡片写进提示词 / 祖先命中不发模型请求且不写 store / 成环 parentId 不挂死)、test/explain.test.mjs 4 例(两个提示词的上层段、空 ancestors 逐字节不变、长度上限)、test/ancestor-explain.test.mjs 1 例 jsdom 端到端(选词解释 → 服务端返回 ancestor → 卡片带原因说明、不显示骨架屏)

liujieranjerry-lgtm and others added 2 commits November 23, 2026 17:45
A concept the reader reached from a card elsewhere in the chain was either
re-explained from scratch (the model never saw the ancestor cards) or, because
dedupe is global by term, handed back as the very card that raised the question
— which reads as A explained by B explained by A.

Pass the ancestor chain (farthest first, capped, cycle-safe) into both explain
prompts so a nested answer is contextual, and when the requested term is itself
one of those ancestors return that stored card with `route: "ancestor"` instead
of silently re-using it. No extra model call, no store write, no schema change;
the card shows a one-line reason and the top-level prompt is byte-identical.

@2019wakeup 2019wakeup left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码方向正确,验证通过。注意:原分支基于 #4 合并前,直接合会回退标注编辑器的滚动修复并把版本降到 0.11.6;我已把当前 main 合并进分支、重建 client.js,并推回 fix/nested-explain(提交 a85e12a),现在相对 main 只有本 PR 的 7 个文件。

验证(更新后分支):node --test 258/258、node eval/run.mjs 13/13、node eval/layout-host.mjs 6/6、node eval/visual.mjs 122/122、node scripts/release-check.mjs 11/11。

设计上认可:ancestorChain 带 visited 防环、命中祖先不发模型且不写 store、空 ancestors 时提示词逐字节不变、提示词长度有上限、客户端用 hint 说明为什么回到原卡片。返回原卡片而不是当前语境差异说明,作者已明确列为后续功能,不阻塞合并。

@2019wakeup
2019wakeup merged commit 3a4ae14 into 2019wakeup:main Nov 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants