fix: sort tools deterministically to preserve prompt-cache prefix - #4
Open
sumleo wants to merge 1 commit into
Open
fix: sort tools deterministically to preserve prompt-cache prefix#4sumleo wants to merge 1 commit into
sumleo wants to merge 1 commit into
Conversation
Author
|
Hi @idoubi, gentle nudge on this when you have a moment. It's a small, self-contained prompt-caching fix, and I'm happy to rebase or tweak anything if that would make review easier. Thanks for the project and your time! |
Author
|
Hi @idoubi — gentle follow-up on this one (open since mid-June). It sorts tools deterministically so the prompt-cache prefix stays byte-stable across requests. CI is green and it's a small, self-contained change. Would you have a moment to take a look, or let me know if you'd like anything adjusted? Happy to rebase. Thanks for your time and for the project! |
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.
Problem
Registry.All()andRegistry.Names()range over the internalmap[string]types.Tool, so they return tools in Go's randomized map-iteration order. That order changes from one run to the next.The agent loop builds the request
tools[]directly fromtoolRegistry.All()(agent/loop.go), and the tools array sits ahead of the system block that carriescache_control. Because the bytes before the cache breakpoint change whenever the tool order shuffles, the cacheable prefix differs across otherwise-identical requests, which defeats prompt caching and causes avoidable cache misses.Fix
Sort the returned slice by tool
Name()inAll(), and sortNames()withsort.Strings. This makes the order stable and deterministic. It is dependency-free (standard librarysort) and does not change any public signatures.Test
Adds
tools/registry_test.go(the package had none for the registry). It registers tools in a non-sorted insertion order and asserts thatAll()andNames()come back sorted by name and stay identical across repeated calls.go test ./...andgo vet ./...pass locally.