Repository navigation
node: rank remote tools by intent word overlap #832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package node | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "reflect" | ||
| "slices" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRankRowsByIntent(t *testing.T) { | ||
| tests := []struct { | ||
| name, intent string | ||
| rows []remoteToolRow | ||
| want []string | ||
| }{ | ||
| {"coverage before name matches", "review pull request", []remoteToolRow{ | ||
| {PeerID: "partial", ToolName: "mcp://review/review"}, | ||
| {PeerID: "full", ToolName: "mcp://tools/check", Description: "Review a pull request"}, | ||
| }, []string{"full", "partial"}}, | ||
| {"name breaks coverage tie", "review", []remoteToolRow{ | ||
| {PeerID: "description", ToolName: "mcp://a/check", Description: "review"}, | ||
| {PeerID: "name", ToolName: "mcp://z/review"}, | ||
| }, []string{"name", "description"}}, | ||
| {"case punctuation and distinct terms", "THE REVIEW_review, pull-request!", []remoteToolRow{ | ||
| {PeerID: "repetition", ToolName: "mcp://a/review", Description: "review review review review"}, | ||
| {PeerID: "coverage", ToolName: "mcp://z/review_pr", Description: "Review a pull request"}, | ||
| }, []string{"coverage", "repetition"}}, | ||
| {"Unicode words", "문서 요약", []remoteToolRow{ | ||
| {PeerID: "other", ToolName: "mcp://tools/edit", Description: "문서 편집"}, | ||
| {PeerID: "match", ToolName: "mcp://tools/summarize", Description: "문서 요약"}, | ||
| }, []string{"match", "other"}}, | ||
| {"retain unmatched tools and errors", "review", []remoteToolRow{ | ||
| {PeerID: "error", ToolName: "mcp://review", Error: "review unavailable"}, | ||
| {PeerID: "unmatched", ToolName: "mcp://a/list"}, | ||
| {PeerID: "match", ToolName: "mcp://z/review"}, | ||
| }, []string{"match", "unmatched", "error"}}, | ||
| {"sort ties by tool then peer", "missing", []remoteToolRow{ | ||
| {PeerID: "z", ToolName: "mcp://b/tool"}, | ||
| {PeerID: "b", ToolName: "mcp://a/tool"}, | ||
| {PeerID: "a", ToolName: "mcp://a/tool"}, | ||
| }, []string{"a", "b", "z"}}, | ||
| {"empty preserves order", "", []remoteToolRow{{PeerID: "z"}, {PeerID: "a"}}, []string{"z", "a"}}, | ||
| {"whitespace preserves order", " \t\n", []remoteToolRow{{PeerID: "z"}, {PeerID: "a"}}, []string{"z", "a"}}, | ||
| {"function words preserve order", "the and of to", []remoteToolRow{{PeerID: "z"}, {PeerID: "a"}}, []string{"z", "a"}}, | ||
| {"empty catalogue", "review", nil, nil}, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| rows := rankRowsByIntent(slices.Clone(tc.rows), tc.intent) | ||
| var got []string | ||
| for _, row := range rows { | ||
| got = append(got, row.PeerID) | ||
| } | ||
| if !slices.Equal(got, tc.want) { | ||
| t.Errorf("order = %v, want %v", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRankRowsByIntent_PreservesRows(t *testing.T) { | ||
| rows := []remoteToolRow{ | ||
| {PeerID: "one", ToolName: "mcp://tools/list", Description: "List branches", Labels: map[string]string{"region": "west"}}, | ||
| {PeerID: "two", ToolName: "mcp://tools/review", Description: "Review code", Labels: map[string]string{"region": "east"}}, | ||
| {PeerID: "three", ToolName: "mcp://unavailable", Error: "failed to connect"}, | ||
| } | ||
| want := []remoteToolRow{rows[1], rows[0], rows[2]} | ||
| got := rankRowsByIntent(rows, "review") | ||
| if !reflect.DeepEqual(got, want) { | ||
| t.Errorf("ranked rows = %+v, want %+v", got, want) | ||
| } | ||
| slices.Reverse(got) | ||
| if again := rankRowsByIntent(got, "review"); !reflect.DeepEqual(again, want) { | ||
| t.Errorf("rank depends on input order: %+v, want %+v", again, want) | ||
| } | ||
| } | ||
|
|
||
| // BenchmarkRankRowsByIntent measures local ranking of synthetic catalogue rows, | ||
| // including the input-slice copy needed because ranking reorders it in place. | ||
| // It excludes mesh discovery and MCP calls and does not measure search relevance. | ||
| func BenchmarkRankRowsByIntent(b *testing.B) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what are those results?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are synthetic catalogue sizes (100, 1,000 and 10,000 I added an explicit benchmark comment and renamed the subcases to |
||
| for _, size := range []int{100, 1000, 10000} { | ||
| b.Run(fmt.Sprintf("rows=%d", size), func(b *testing.B) { | ||
| catalogue := make([]remoteToolRow, size) | ||
| for i := range catalogue { | ||
| catalogue[i] = remoteToolRow{ | ||
| PeerID: fmt.Sprintf("peer-%d", i), | ||
| ToolName: fmt.Sprintf("mcp://code-reviewer/tool_%d", i), | ||
| Description: "Review a pull request and report code quality issues", | ||
| } | ||
| } | ||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
| for b.Loop() { | ||
| rankRowsByIntent(slices.Clone(catalogue), "review pull request") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance Optimization: Avoid Map Allocations in Search Loop
In the current implementation of
rankRowsByIntent,toolSearchTermsis called twice for every row in the catalogue (once for the tool name and once for the description). Each call totoolSearchTermsallocates a new map and a slice of strings. For a large catalogue (e.g., 1,000 or 10,000 rows), this results in tens of thousands of heap allocations, leading to high GC pressure and CPU overhead.We can optimize this by:
uint64bitmask to track matched query terms.This completely eliminates map allocations inside the loop, significantly improving performance and reducing memory usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the concrete optimization suggestion. The allocation cost is visible in
BenchmarkRankRowsByIntent -benchmem: on the current four-vCPU Linux/arm64 VM, 1,000 rows take about 826 µs / 1.20 MB / 9,005 allocations per operation, and 10,000 rows take about 7.83 ms / 11.93 MB / 90,005 allocations (including the input-slice copy).I am keeping the straightforward term-set implementation for this baseline and recording those costs for comparison. The proposed
uint64path would silently ignore meaningful query terms beyond the first 64, so adopting it would need an unbounded fallback and regression coverage for longer intents. The current implementation has no 64-term cutoff. The benchmark now explicitly identifies its row counts and makes clear that it measures local cost, not retrieval quality.