Repository navigation
fix(reader): 嵌套解释带上层语境,命中祖先时不再自循环 - #5
Merged
2019wakeup merged 2 commits intoNov 23, 2026
Merged
Conversation
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
approved these changes
Nov 23, 2026
2019wakeup
left a comment
Owner
There was a problem hiding this comment.
代码方向正确,验证通过。注意:原分支基于 #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 说明为什么回到原卡片。返回原卡片而不是当前语境差异说明,作者已明确列为后续功能,不阻塞合并。
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.
症状
在概念卡片里选中词条再解释时会出现「套娃」:
parentId = A);根因(两处,都不在模型)
explainConcept只把parentId存进新概念(index.js约 397 行),从未使用它;buildQuickExplainPrompt/buildExplainPrompt不知道读者是从哪张卡片下来的,所以嵌套解释等于重新从零解释,和上层解释重复。normalizeTerm(term)全局查重,命中就原样返回已有卡片。于是「对上层词条再解释」必然把那张上层卡片拿回来,而它和当前下钻路径无关,观感就是自循环。修改
服务端
lib/explain.js:两个解释提示词新增可选ancestors。有上层卡片时,user 段追加「上层解释(从远到近)」列表(定义截断到 120 字),system 段追加一条规则:若词条本身是上层概念或别名,不要重复其定义,改说明它在这段话里具体指什么、与上层的差别。ancestors为空时,提示词与改动前逐字节一致(有测试断言)。index.js:新增MAX_ANCESTOR_DEPTH = 4与ancestorChain(concepts, startId)(visitedSet防环,缺失 id 直接停,返回从远到近)。explainConcept在去重后、调用模型前计算祖先链;若命中的existing就是链上某张卡片,直接返回{ concept: existing, reused: true, ancestor: true, route: 'ancestor', chain: [...] }——不调模型、不写 store。新建概念与 full 升级两条路径都把ancestors传进提示词。客户端
createConcept识别result.ancestor:仍然打开那张卡片,但带上hint,并给出 toast 说明「这个词本链路上层已有解释,已为你打开原卡片(不重复生成)」;正常新卡片路径不变。card.hint存在)。i18n.js增加label.ancestorRevisit的中英文案。规模:5 个产品文件 + 3 个测试文件,无新依赖、无 store schema 变更、无新路由。
二阶影响(直接后果之外)
maxTokens: 900的输入侧承受范围内(输出侧未变);有测试断言 prompt 长度 < 1200,防止祖先定义无界膨胀。route语义扩展。 之前只有quick/full/cached,现在多一个ancestor。客户端只按result.ancestor布尔分支,route仍是给诊断和日志看的;debug.lastSent的写入保持原样(不调模型就不记),所以lastSent与调用次数仍一一对应。parentId只在新卡片创建时指向已存在的卡片,新建 id 唯一,本不可能成环;ancestorChain仍带 visited 集合,迁移或手改 store 造成的环也只会截断不会挂死(有测试)。三阶影响(模块 / 用户行为 / 复杂度 / 维护成本)
概念栏更干净 —— 卡片总量少,去重压力小,导出与检索的噪声也随之下降。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/254node eval/run.mjs→ 13/13node eval/layout-host.mjs→ 6/6node eval/visual.mjs→ 118/119(唯一失败项是既有的markdown relative images load through the file route,与本改动无关,在纯净 main 上也失败)node scripts/build-client.mjs幂等,构建后工作区只剩既有的本地package.json改动(未提交)test/host.test.mjs3 例(嵌套解释把上层卡片写进提示词 / 祖先命中不发模型请求且不写 store / 成环 parentId 不挂死)、test/explain.test.mjs4 例(两个提示词的上层段、空ancestors逐字节不变、长度上限)、test/ancestor-explain.test.mjs1 例 jsdom 端到端(选词解释 → 服务端返回 ancestor → 卡片带原因说明、不显示骨架屏)