Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions pkgs/mcpx/internal/daemon/completion_rank_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package daemon

import (
"reflect"
"testing"

"github.com/dezren39/mcpx/internal/mcpclient"
)

func promptsOf(names ...string) []mcpclient.Prompt {
out := make([]mcpclient.Prompt, len(names))
for i, n := range names {
out[i] = mcpclient.Prompt{Name: n}
}
return out
}

func resourcesOf(uris ...string) []mcpclient.Resource {
out := make([]mcpclient.Resource, len(uris))
for i, u := range uris {
out[i] = mcpclient.Resource{URI: u}
}
return out
}

func completeValues(t *testing.T, got map[string]any) []string {
t.Helper()
vals, ok := got["values"].([]string)
if !ok {
t.Fatalf("values = %T %v", got["values"], got["values"])
}
return vals
}

// The three tiers in order, for prompts: exact, then prefix (shorter
// first), then substring (by first-match position, then alphabetical).
// Candidates are listed interleaved alphabetically so alphabetical
// order cannot masquerade as tier order.
func TestLocalCompletionTiersPrompts(t *testing.T) {
req := completeReq{}
req.Ref.Type = "ref/prompt"
req.Argument.Value = "alp"
got := localCompletion(promptsOf(
"zzalpzz", // substring, position 2
"alpine", // prefix, 6 runes
"xalp", // substring, position 1
"alp", // exact
"beta", // no match
"alps", // prefix, 4 runes
"balp", // substring, position 1, alphabetically before xalp
), nil, req, 0)
want := []string{"alp", "alps", "alpine", "balp", "xalp", "zzalpzz"}
if vals := completeValues(t, got); !reflect.DeepEqual(vals, want) {
t.Fatalf("values = %v, want %v", vals, want)
}
if got["total"] != 6 || got["hasMore"] != false {
t.Fatalf("total/hasMore = %v/%v, want 6/false", got["total"], got["hasMore"])
}
}

// The same tiers for resource template URIs, including a mid-name
// substring hit that the old prefix-only fallback returned nothing for.
func TestLocalCompletionTiersResources(t *testing.T) {
req := completeReq{}
req.Ref.Type = "ref/resource"
req.Argument.Value = "temp"
got := localCompletion(nil, resourcesOf(
"file:///var/temp", // substring, position 10
"template", // prefix, 8 runes
"temp", // exact
"other", // no match
"temp2", // prefix, 5 runes
"atemp", // substring, position 1
), req, 0)
want := []string{"temp", "temp2", "template", "atemp", "file:///var/temp"}
if vals := completeValues(t, got); !reflect.DeepEqual(vals, want) {
t.Fatalf("values = %v, want %v", vals, want)
}
}

// A mid-name substring hit: "pin" appears inside "alpine" but is not a
// prefix, so the pre-#2388 fallback found nothing.
func TestLocalCompletionSubstringHit(t *testing.T) {
req := completeReq{}
req.Ref.Type = "ref/prompt"
req.Argument.Value = "pin"
got := localCompletion(promptsOf("alpine", "beta"), nil, req, 0)
if vals := completeValues(t, got); !reflect.DeepEqual(vals, []string{"alpine"}) {
t.Fatalf("values = %v, want [alpine]", vals)
}
}

// Matching is case-folded: an upper-case value still hits the exact
// tier, and exact beats prefix regardless of case.
func TestLocalCompletionCaseFolded(t *testing.T) {
req := completeReq{}
req.Ref.Type = "ref/prompt"
req.Argument.Value = "ALP"
got := localCompletion(promptsOf("alpine", "Alp"), nil, req, 0)
if vals := completeValues(t, got); !reflect.DeepEqual(vals, []string{"Alp", "alpine"}) {
t.Fatalf("values = %v, want [Alp alpine]", vals)
}
}

// The cap applies AFTER ranking: the alphabetically-first candidate
// (a substring match) must not survive truncation ahead of the exact
// match, and total/hasMore report the full ranked match count.
func TestLocalCompletionCapAfterRanking(t *testing.T) {
req := completeReq{}
req.Ref.Type = "ref/prompt"
req.Argument.Value = "alp"
got := localCompletion(promptsOf("aalp", "alpine", "alp"), nil, req, 1)
if vals := completeValues(t, got); !reflect.DeepEqual(vals, []string{"alp"}) {
t.Fatalf("values = %v, want [alp]", vals)
}
if got["total"] != 3 || got["hasMore"] != true {
t.Fatalf("total/hasMore = %v/%v, want 3/true", got["total"], got["hasMore"])
}
}

// An empty value puts every candidate in the prefix tier: shorter
// names first, then alphabetical (triage settlement, task 3557).
func TestLocalCompletionEmptyValue(t *testing.T) {
req := completeReq{}
req.Ref.Type = "ref/prompt"
req.Argument.Value = ""
got := localCompletion(promptsOf("bbbb", "a", "cc", "dd"), nil, req, 0)
want := []string{"a", "cc", "dd", "bbbb"}
if vals := completeValues(t, got); !reflect.DeepEqual(vals, want) {
t.Fatalf("values = %v, want %v", vals, want)
}
if got["total"] != 4 || got["hasMore"] != false {
t.Fatalf("total/hasMore = %v/%v, want 4/false", got["total"], got["hasMore"])
}
}

// Duplicate names in the cache are preserved, as before.
func TestLocalCompletionDuplicatesPreserved(t *testing.T) {
req := completeReq{}
req.Ref.Type = "ref/prompt"
req.Argument.Value = "alp"
got := localCompletion(promptsOf("alp", "alp", "alps"), nil, req, 0)
if vals := completeValues(t, got); !reflect.DeepEqual(vals, []string{"alp", "alp", "alps"}) {
t.Fatalf("values = %v, want [alp alp alps]", vals)
}
}

// No matches: empty values, zero total, hasMore false.
func TestLocalCompletionNoMatch(t *testing.T) {
req := completeReq{}
req.Ref.Type = "ref/prompt"
req.Argument.Value = "zzz"
got := localCompletion(promptsOf("alpine"), nil, req, 0)
if vals := completeValues(t, got); len(vals) != 0 {
t.Fatalf("values = %v, want empty", vals)
}
if got["total"] != 0 || got["hasMore"] != false {
t.Fatalf("total/hasMore = %v/%v, want 0/false", got["total"], got["hasMore"])
}
}
69 changes: 62 additions & 7 deletions pkgs/mcpx/internal/daemon/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"net/url"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"

"github.com/dezren39/mcpx/internal/api"
"github.com/dezren39/mcpx/internal/config"
Expand Down Expand Up @@ -404,21 +406,18 @@ func (s *Server) handleComplete(w http.ResponseWriter, r *http.Request) {
// server's own completion knows the *values* an argument may take, which
// mcpx cannot guess, so this is a floor rather than a substitute.
func localCompletion(prompts []mcpclient.Prompt, templates []mcpclient.Resource, req completeReq, max int) map[string]any {
var values []string
var candidates []string
switch req.Ref.Type {
case "ref/prompt":
for _, p := range prompts {
if hasPrefixFold(p.Name, req.Argument.Value) {
values = append(values, p.Name)
}
candidates = append(candidates, p.Name)
}
case "ref/resource":
for _, t := range templates {
if hasPrefixFold(t.URI, req.Argument.Value) {
values = append(values, t.URI)
}
candidates = append(candidates, t.URI)
}
}
values := rankCompletions(candidates, req.Argument.Value)
if values == nil {
values = []string{}
}
Expand All @@ -431,6 +430,62 @@ func localCompletion(prompts []mcpclient.Prompt, templates []mcpclient.Resource,
return map[string]any{"values": values, "total": total, "hasMore": total > len(values)}
}

// rankCompletions orders the candidates that match value by relevance
// (issue #2388, Phase 2 of design #2386): tier 1 is an exact
// case-folded match, tier 2 a prefix match (shorter names first, then
// folded alphabetical, then the raw string), tier 3 a substring
// (non-prefix) match (by first-match position, then folded
// alphabetical, then raw). Matching and ordering are case-folded with
// strings.ToLower, consistent with hasPrefixFold. An empty value puts
// every candidate in the prefix tier. The sort is stable and duplicate
// candidates are preserved. Non-matches are dropped.
func rankCompletions(candidates []string, value string) []string {
q := strings.ToLower(value)
type scored struct {
raw string
folded string
tier int
pos int // tier 3: first-match index in the folded candidate
}
var hits []scored
for _, c := range candidates {
fc := strings.ToLower(c)
switch {
case q != "" && fc == q:
hits = append(hits, scored{c, fc, 1, 0})
case strings.HasPrefix(fc, q):
hits = append(hits, scored{c, fc, 2, 0})
case q != "" && strings.Index(fc, q) >= 0:
hits = append(hits, scored{c, fc, 3, strings.Index(fc, q)})
}
}
sort.SliceStable(hits, func(i, j int) bool {
a, b := hits[i], hits[j]
if a.tier != b.tier {
return a.tier < b.tier
}
switch a.tier {
case 2:
if ra, rb := utf8.RuneCountInString(a.raw), utf8.RuneCountInString(b.raw); ra != rb {
return ra < rb
}
case 3:
if a.pos != b.pos {
return a.pos < b.pos
}
}
if a.folded != b.folded {
return a.folded < b.folded
}
return a.raw < b.raw
})
values := make([]string, 0, len(hits))
for _, h := range hits {
values = append(values, h.raw)
}
return values
}

// ---- tasks ----

// taskStore is the daemon's own, created on first use so a daemon that never
Expand Down
Loading