Skip to content

vmcp optimizer: tool catalog is embedded in non-deterministic order across aggregations #5702

Closed

Description

@gabrielcosi

Bug description

The optimizer embeds the aggregated tool catalog in a non-deterministic order. MergeCapabilities builds the tool slice by ranging resolved.Tools (a map[string]*ResolvedTool) with no sort, and generateEmbeddings embeds in that slice order. Go randomizes map iteration, so each fresh aggregation produces a different order; the 30s capability cache only holds it steady within a window.

tools/list is not affected — the advertised list comes out sorted, so the reordering is invisible to clients and surfaces only in the embedding batch.

Same class as #3448, which fixed non-deterministic config.Backends order by sorting in the Discoverer (#3450). That covered backend order, not the tools inside the aggregation.

Steps to reproduce

  1. Run a VirtualMCPServer with the optimizer enabled and embeddingProvider: openai pointed at an embedding endpoint whose request logs you can inspect.
  2. Open several sessions spread over time, or restart the pod between them, so the capability cache re-aggregates.
  3. Inspect the /embeddings request bodies: the order of tool texts in the input array changes between aggregations.

Observed on my gateway from the embedding backend's request logs: across 23 embedding requests, 15 distinct orderings — requests inside one capability-cache window share an order, requests in different windows differ from the first element.

Expected behavior

The same set of tools produces the same embedding-batch order every time.

Actual behavior

The order varies per aggregation. An order-sensitive cache in front of the embedding endpoint sees a new request hash each window and misses.

Environment (if relevant)

  • ToolHive version: v0.33.0

Additional context

Fix mirrors #3450: sort the tool slice deterministically (resolved name, or workload+name) before embedding. Happy to PR.

Activity

  1. added
    bug
    on Jul 1, 2026
  2. added
    needs-triage
    on Jul 1, 2026
  3. added
    good-first-issue
    and removed
    needs-triage
    on Jul 1, 2026
  4. jhrozek commented on Jul 2, 2026

    @jhrozek
    Contributor

    @gabrielcosi happy to review a PR or assign this to you, seems like the issue sounds "easy enough" to openclaws otherwise so we are already getting PR spam.

  5. gabrielcosi commented on Jul 2, 2026

    @gabrielcosi
    ContributorAuthor

    @jhrozek I opened a PR, thanks!

  6. added a commit that references this issue on Jul 3, 2026
    8010cd0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

buggood-first-issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions