fix(tool): deterministic tool list ordering to preserve prompt/KV cache - #2445
Merged
Merged
Conversation
Registry.List() iterated r.tools, a Go map, so the order of tools sent to the provider differed on every run. Tool definitions sit at the very front of the prompt prefix (tools -> system -> messages on Anthropic; same conceptual position for OpenAI-compatible automatic prefix caching), so a randomized tool order produced a different byte prefix on every run and invalidated the provider-side prompt/KV cache before any cache_control breakpoint (anthropic.go) could take effect. Sort List() output by tool name. This makes ToDefinitions() and ToolNames() deterministic, restoring cross-run prefix stability - reducing TTFT and input-token cost per Manus, "Context Engineering for AI Agents" (2025): KV-cache hit rate is the most cost-impactful property of an agent prompt prefix. Adds TestToolRegistry_ListDeterministicOrder verifying stable, sorted ordering across repeated ToDefinitions() calls. Co-Authored-By: ggcode <noreply@ggcode.dev>
topcheer
commented
Sep 16, 2026
topcheer
left a comment
Owner
Author
There was a problem hiding this comment.
复审通过 ✅
- 根因真实:
r.tools是 map,Go 迭代随机化——工具定义在 prompt 前缀最前部,字节序不稳直接击穿 provider KV cache(注释引用的 Manus 成本论成立) - 锁语义正确:sort.Slice 在 RLock+defer 作用域内(diff 上下文核实),且排序对象是本地拷贝切片——无 race 无死锁
- 下游零顺序依赖:ToolNames/List 消费者抽查(mcp_loader 迁移对比/info 展示)均为集合语义,无序敏感调用方
- 测试质量:20 轮迭代序稳定 + 严格递增有序断言(双保险)+ count 漂移检测
- 顺手 em-dash 修 ASCII 无害
topcheer
pushed a commit
that referenced
this pull request
Sep 16, 2026
Implements the 2026 baseline practice of Temporal Context Injection: - Session-anchored current date/time header prepended to the cacheable base system prompt (byte-stable per run to preserve KV-cache reuse, consistent with #2445 prefix-stability findings) - New current_time built-in tool with optional IANA timezone param for live wall-clock reads in long sessions (crossing midnight, DST, etc.) - Header hints the model to call current_time when freshness matters Co-Authored-By: ggcode <noreply@ggcode.dev>
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.
研究出处
问题
Registry.List()直接迭代r.tools(Go map),map 迭代顺序随机 → 每次 run 发给 provider 的工具列表顺序都不同。由于工具定义位于 prompt 前缀最前端,这导致:buildParams中已实现的cache_control断点(含 arXiv:2601.06007 的静态/动态分层设计)完全无法命中。ToolNames()顺序也随之抖动,影响依赖稳定展示的下游。修复
List()输出按工具名排序,使ToDefinitions()/ToolNames()确定化,一次排序恢复跨 run 前缀稳定性。测试
TestToolRegistry_ListDeterministicOrder:连续 20 次ToDefinitions()验证顺序稳定且按名排序。go build -tags goolm ./...通过,go vet/gofmt干净,pre-commit 钩子(fmt+vet+build)通过。Co-Authored-By: ggcode noreply@ggcode.dev