Skip to content

fix(recall): 固定场景导航顺序以稳定提示前缀#469

Open
Qiyuanqiii wants to merge 1 commit into
TencentCloud:mainfrom
Qiyuanqiii:fix/issue-120-deterministic-scene-navigation
Open

fix(recall): 固定场景导航顺序以稳定提示前缀#469
Qiyuanqiii wants to merge 1 commit into
TencentCloud:mainfrom
Qiyuanqiii:fix/issue-120-deterministic-scene-navigation

Conversation

@Qiyuanqiii

Copy link
Copy Markdown

关联 Issue

Related to #120

问题说明

scene-navigation 属于每轮召回时生成的稳定系统上下文,其内容会参与后续提示构建和前缀缓存。

当前实现只按场景 heat 降序排列。同热度场景的相对顺序取决于 scene_index.json 的原始顺序,而该索引重建自文件系统目录枚举,目录枚举顺序并不构成稳定契约。

因此,即使场景内容、热度和用户配置完全没有变化,索引重建后同热度场景仍可能换序,使生成的系统上下文字节发生变化,降低 prefix-matching cache 的可复用性。

修复方案

引入统一的确定性场景排序规则:

  1. heat 降序排列;
  2. heat 相同时按 filename 升序排列。

该规则同时应用于:

  • scene index 读取;
  • scene index 写入;
  • scene index 重建;
  • Scene Navigation 最终渲染。

这样,相同场景集合无论来自何种目录枚举顺序,都会产生完全相同的索引和导航文本。

测试覆盖

新增回归测试验证:

  • 相同场景的不同输入排列生成完全相同的 Scene Navigation;
  • 高热度场景仍优先排列;
  • 同热度场景使用文件名稳定排序;
  • 排序不会修改调用方传入的数组;
  • scene index 的持久化顺序和读取顺序保持一致。

范围说明

本 PR 是独立的确定性修复,不修改:

  • recall injection placement;
  • recall.injectionMode
  • recall.showInjected
  • 历史消息压缩;
  • session prompt dedupe;
  • OpenClaw hook contract。

本 PR 不声称修复 #120 后续澄清的 OpenClaw webchat 主因,只修复 TencentDB 稳定系统上下文中一个可复现的非确定性排序问题。

验证方法

针对性回归测试

验证不同输入顺序的同一组场景会生成完全相同的 Scene Navigation,同时验证索引读写顺序稳定:

npm test -- src/core/scene/scene-order.test.ts

完整测试

npm test

预期结果:

-Test Files 5 passed (5)
-Tests 69 passed (69)

完整构建

npm run build

预期结果:
Build complete

Diff whitespace 检查

git diff --check origin/main...HEAD

预期结果:无输出,退出码为 0。

##本地验证环境

  • Node.js:满足项目要求 >=22.16.0
  • Vitest:v4.1.10
  • 分支:fix/issue-120-deterministic-scene-navigation
  • Commit:dd8619c

其中针对性测试就是最小复现:测试会把相同场景按两种不同顺序传入,旧实现会让同热度场景的导航顺序随输入变化;修复后两次生成结果严格相同。这样维护者既能复现问题,也能验证修复。

验证结果

  • Targeted tests:1 file / 2 tests passed
  • Full test:5 files / 69 tests passed
  • Build:passed
  • Diff check:passed

验证截图

image

@YOMXXX YOMXXX left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved.

I verified the PR locally on the branch with Node 24.15.0:

  • COREPACK_ENABLE_AUTO_PIN=0 pnpm vitest run src/core/scene/scene-order.test.ts — 1 file / 2 tests passed
  • COREPACK_ENABLE_AUTO_PIN=0 pnpm test — 5 files / 69 tests passed
  • COREPACK_ENABLE_AUTO_PIN=0 pnpm build — passed
  • git diff --check HEAD~1..HEAD — passed

The change is focused and deterministic: scene entries are canonicalized by heat descending and filename ascending across read/write/sync plus final Scene Navigation rendering. The regression test covers the important cache-stability case where equal-heat scenes arrive in different input orders without mutating the caller's array.

@Maxwell-Code07

Copy link
Copy Markdown
Collaborator

Thank you for submitting this PR and participating in Tencent Rhino-bird Open-source Training Program!
We have successfully received your submission. The program is currently in full swing, and we will complete the Code Review for you as soon as possible. Please keep an eye on the status notifications for this PR so you can follow up promptly once the review feedback is provided.
Thanks again for your contribution and open-source spirit! 🚀

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.

3 participants