fix(tools): rebind discovery tools when cloning registries

This commit is contained in:
Anton Bogdanovich 2026-05-06 21:05:22 -07:00
parent 01280eaa53
commit 8a8fe42f60
2 changed files with 34 additions and 1 deletions

View file

@ -412,8 +412,15 @@ func (r *ToolRegistry) Clone() *ToolRegistry {
mediaStore: r.mediaStore,
}
for name, entry := range r.tools {
tool := entry.Tool
switch t := entry.Tool.(type) {
case *RegexSearchTool:
tool = NewRegexSearchTool(clone, t.ttl, t.maxSearchResults)
case *BM25SearchTool:
tool = NewBM25SearchTool(clone, t.ttl, t.maxSearchResults)
}
clone.tools[name] = &ToolEntry{
Tool: entry.Tool,
Tool: tool,
IsCore: entry.IsCore,
TTL: entry.TTL,
}

View file

@ -515,6 +515,32 @@ func TestToolRegistry_Clone_PreservesTTLValue(t *testing.T) {
}
}
func TestToolRegistry_Clone_RebindsDiscoveryToolsToClone(t *testing.T) {
parent := NewToolRegistry()
parent.RegisterHidden(newMockTool("mcp_research", "deep research report tool"))
parent.Register(NewBM25SearchTool(parent, 3, 5))
clone := parent.Clone()
searchTool, ok := clone.Get("tool_search_tool_bm25")
if !ok {
t.Fatal("expected cloned registry to expose BM25 search tool")
}
result := searchTool.Execute(context.Background(), map[string]any{
"query": "deep research",
})
if result == nil || result.IsError {
t.Fatalf("search result error: %+v", result)
}
if _, ok := clone.Get("mcp_research"); !ok {
t.Fatal("expected search in clone to promote hidden tool in clone")
}
if _, ok := parent.Get("mcp_research"); ok {
t.Fatal("expected search in clone not to promote hidden tool in parent")
}
}
func TestToolRegistry_ConcurrentAccess(t *testing.T) {
r := NewToolRegistry()
var wg sync.WaitGroup