Skip to content

Review mirror: preserve MCP tool-search prompt cache (#16539) - #296

Open
wasimysaid wants to merge 10 commits into
review-base/16539from
review/issue-16538-mcp-cache
Open

wasimysaid wants to merge 10 commits into
review-base/16539from
review/issue-16538-mcp-cache

Conversation

@wasimysaid

@wasimysaid wasimysaid commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

Fixes unslothai#16538. Keep searched MCP schemas out of the local model’s cached prompt declaration and return them in tool results. For llama.cpp, supply discovered schemas to its decoding grammar separately from the template’s tools. External providers retain normal tool declarations because they cannot separate the two.

Search admits only complete schemas within the response budget, without charging rejected matches against smaller tools. Deferred calls retain the existing schema-identifier filtering and argument coercion.

Continues Shreesh-Sree’s unslothai#16539, preserving the original commits. That PR and its source fork became unavailable during review. Includes fixes for four Codex findings from the review mirror, plus the external-provider compatibility regression.

Live validation:

  • Real ComfyMCP discovery: 23 tools plus the resource tool. Main expands declarations from 5 to 7; the local prompt declaration stays at 5 after the fix.
  • Qwen3.5-0.8B/llama.cpp b11985 controlled replay: 1,616 cached tokens versus 1,104 on main. This verifies the mechanism, not the reported 17× timing or Windows hardware.
  • Qwen3-8B-Q4_K_M/b11985: the original stable declaration repeats the search; the separated grammar emits the discovered native call (mcp__demo__ping, n=7). /apply-template confirms byte-identical rendered prompts.

MCP/controller/external-loop tests: 317 passed. The updated llama-loop regression and seven llama MCP tests pass. Pre-commit passes. Regression cases fail on their respective baselines. The broader backend run was interrupted after stalling with 415 tests passed; it is not a full-suite pass.

Final Codex review found no major issues at d9570c4ffa.

Shreesh-Sree and others added 4 commits November 3, 2026 16:11
…ation

mcp_tool_search injected the matched tool schemas into the declared
tools array mid-turn. The chat template renders tools at the top of the
prompt, so the next continuation diverged from the first tokens and
llama-server re-evaluated the whole conversation; on hybrid/recurrent
models without a checkpoint at the divergence that is a full prefill
(unslothai#16538), and a discovered-but-unused tool dropped from the next turn's
listing repeated the storm on every later search.

The search result now carries the full schemas it already budgets with
_MCP_TOOL_LOAD_SHARE, and the controller only opens its call gate for
the found catalog names, leaving the declared tools - and the cached
prompt prefix - untouched. Tools a thread runs are still recorded and
listed on its later turns.

Refs unslothai#16538
@wasimysaid

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T12:40:34.502562Z d9570c4 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 860a824061

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/core/inference/tools.py Resolved
@wasimysaid

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b7afe4c1bb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/core/inference/tools.py Resolved
@wasimysaid

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7422d5b1f8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@wasimysaid

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0d4293d2c0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@wasimysaid

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: d9570c4ffa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit to unslothai/unsloth that referenced this pull request Nov 3, 2026
Fixes #16538. Keep searched MCP schemas out of the local model’s cached
prompt declaration and return them in tool results. For llama.cpp,
supply discovered schemas to its decoding grammar separately from the
template’s tools. External providers retain normal tool declarations
because they cannot separate the two.

Search admits only complete schemas within the response budget, without
charging rejected matches against smaller tools. Deferred calls retain
the existing schema-identifier filtering and argument coercion.

Continues Shreesh-Sree’s #16539, preserving the original commits. That
PR and its source fork became unavailable during review. Includes fixes
for four Codex findings from [the review
mirror](wasimysaid#296), plus the
external-provider compatibility regression.

Live validation:
- Real ComfyMCP discovery: 23 tools plus the resource tool. Main expands
declarations from 5 to 7; the local prompt declaration stays at 5 after
the fix.
- Qwen3.5-0.8B/llama.cpp b11985 controlled replay: 1,616 cached tokens
versus 1,104 on main. This verifies the mechanism, not the reported 17×
timing or Windows hardware.
- Qwen3-8B-Q4_K_M/b11985: the original stable declaration repeats the
search; the separated grammar emits the discovered native call
(`mcp__demo__ping`, `n=7`). `/apply-template` confirms byte-identical
rendered prompts.

MCP/controller/external-loop tests: 317 passed. The updated llama-loop
regression and seven llama MCP tests pass. Pre-commit passes. Regression
cases fail on their respective baselines. The broader backend run was
interrupted after stalling with 415 tests passed; it is not a full-suite
pass.

[Final Codex
review](wasimysaid#296 (comment))
found no major issues at `d9570c4ffa`.

---------

Co-authored-by: Shreesh-Sree <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants