Skip to content

Preserve prompt cache during deferred MCP tool search - #16546

Merged
danielhanchen merged 10 commits into
unslothai:mainfrom
wasimysaid:review/issue-16538-mcp-cache
Nov 3, 2026
Merged

danielhanchen merged 10 commits into
unslothai:mainfrom
wasimysaid:review/issue-16538-mcp-cache

Conversation

@wasimysaid

@wasimysaid wasimysaid commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

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, 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 8 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
wasimysaid marked this pull request as ready for review November 3, 2026 12:41
@danielhanchen danielhanchen self-assigned this Nov 3, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@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-03T13:34:46.720485Z d02daa8 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.

danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 3, 2026

@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: fe90003866

ℹ️ 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 on lines +12872 to +12873
if _fit_result_to_room(candidate, MCP_TOOL_SEARCH_NAME) == candidate:
fitted.append(spec)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Enable the schemas actually admitted by the budgeted search

On the GGUF path, execute_tool() sets _REQUEST_RESULT_BUDGET inside stream_tool_execution()'s copied worker context, but ToolLoopController.record_result() recomputes mcp_tool_search_specs() in the parent context, where the result room is still None. This new context-dependent check can therefore select different schemas: a large first match rejected by the worker's result cap can be accepted during recomputation and consume enough load share to exclude a smaller match that the worker actually returned. The next llama.cpp decoding grammar then lacks the tool reported as loaded, preventing its native call. Carry the admitted schemas back from execution, or recompute using the same result budget.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in d02daa8: build the decoding catalog from the schemas in the completed search result instead of recomputing under the parent context. A real worker-thread regression returns ping but previously enabled large; it fails before this change and passes after. Python documents that copied-context changes remain isolated: https://docs.python.org/3/library/contextvars.html

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 3, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: d02daa8d9d

ℹ️ 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".

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.

[Bug] Studio: Dynamic MCP loading invalidates prompt cache on hybrid/recurrent models (17x slowdown)

3 participants