Skip to content

fix(core): forward model capability probes through the session-recording and failover wrappers - #3881

Open
birdie7761 wants to merge 1 commit into
agentscope-ai:mainfrom
birdie7761:fix/toolsearch-mode-wrapper-delegation
Open

birdie7761 wants to merge 1 commit into
agentscope-ai:mainfrom
birdie7761:fix/toolsearch-mode-wrapper-delegation

Conversation

@birdie7761

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (based on main @ 0714868cc)

Description

#3680 added on-demand tool search. Models opt into a native loading protocol by overriding
Model.getToolSearchMode() — AnthropicChatModel returns DEFERRED, OpenAIResponsesChatModel
returns CLIENT; ordinary function calling remains the interface default (FUNCTION).
ToolSearchMiddleware.onModelCall selects the protocol from input.model().getToolSearchMode().

Two agent-internal model wrappers forward only a hand-picked subset of Model's capability
probes, so the rest silently fall back to the interface defaults. Three omissions accumulated
across three feature PRs: getToolSearchMode (#3680, both wrappers), supportsToolChoiceSpecific
(#2697, both wrappers), and supportsNativeStructuredOutputWithTools (failover wrapper only —
the interface default derived it from the forwarded plain flag, which is wrong for models whose
two flags diverge). This PR forwards all three, making both wrappers complete over Model's
current capability surface:

  • SessionModels.Recorded — assembled by ReActAgent.modelForCall whenever session recording
    is active (sessionLogStore configured with a non-LEGACY sessionHistoryMode);
  • the failover wrapper — the anonymous Model returned by modelForCall whenever
    fallbackModel is configured.

For getToolSearchMode the effect was user-visible: every DEFERRED/CLIENT model behind
either wrapper was silently degraded to FUNCTION, so ToolSearchMiddleware.onModelCall hid
catalog tools from the request entirely (visible-only surface) instead of exposing them with
deferLoading=true (DEFERRED) or toolSearch=true (CLIENT). Both wrappers are
unconditional in those setups — nothing warns or logs, the native protocol implementations of
the two models become dead paths while the agent keeps working in fallback semantics.

For the other two probes the exposure is the middleware SPI: ModelCallInput.model() is the
wrapper, so any third-party ON_MODEL_CALL middleware reading them gets the derived/default
value even for models that declare otherwise (in-repo consumers read the unwrapped field and
are unaffected). The delegation mirrors the neighboring capability probes
(delegate.getToolSearchMode() in Recorded, activeModel.get().supportsToolChoiceSpecific()
in the failover wrapper).

Compatibility

  • getToolSearchMode() has exactly two readers in the codebase, both in
    ToolSearchMiddleware.onModelCall (:107, :121); supportsToolChoiceSpecific() has no
    wrapper-path readers in-repo (its consumers read the unwrapped field). No other component
    observes either value, so the behavior change is confined to tool-search requests and the
    middleware SPI behind these wrappers.
  • Models that rely on the default FUNCTION / false are unaffected (forwarding a default
    yields the default).
  • After the fix, agents that were silently running in FUNCTION semantics switch to the model's
    declared native protocol — restoring the documented behavior of feat(tool): add on-demand tool search with native provider loading #3680, not changing it.
  • These are the only Model wrappers in agentscope-core/agentscope-harness main sources
    (verified by sweep); extension models are leaf implementations declaring their own mode.
  • No public signatures, serialized state, or event payloads change.

Known interaction (heterogeneous failover pairs)

The middleware reads the mode once per request from the wrapper (activeModel at call start —
the primary). If the primary fails over, the already-shaped surface is passed to the fallback
unchanged. For a DEFERRED primary (Anthropic) failing over to a CLIENT fallback
(OpenAIResponsesChatModel) without a hosted tool_search server tool configured,
ResponsesRequestMapper rejects deferLoading tools, so the fallback call errors — and since
the docs forbid combining the middleware with a hosted tool_search server tool, that escape
hatch does not exist. This pairing requires the tool-search middleware with native loading and
two different native-protocol models in one failover pair; the pre-fix code reached it only in
degraded FUNCTION semantics (no flags) — i.e. the pre-fix "working" failover was itself the
bug: the primary never used its declared protocol. Same-family (FUNCTION default) fallbacks
ignore the flag and degrade softly; DEFERRED fallbacks consume it natively. The limitation is
now documented in ToolSearchMiddleware's Javadoc and
docs/v2/{en,zh}/docs/building-blocks/tool.md (failover across mixed native protocols:
use a function-calling fallback or nativeLoading=false). Reshaping the surface per serving
model is an architectural change in the failover path and is deliberately out of scope here.

Tests

Five new cases in ToolSearchTest. The mode cases and the false-direction capability cases
were red-verified against pre-fix code — verbatim failure output:

ToolSearchTest.sessionRecordingWrapperPreservesNativeToolSearchMode:589
  Surface: [io.agentscope.core.model.ToolSchema@2a140ce5] ==> expected: <true> but was: <false>
ToolSearchTest.failoverWrapperPreservesNativeToolSearchMode:631
  Surface: [io.agentscope.core.model.ToolSchema@52dd1be2] ==> expected: <true> but was: <false>

(The surface printed is the single tool_search schema — the catalog entry was absent, i.e. the
DEFERRED model had been degraded to visible-only FUNCTION semantics.)

Test Asserts
sessionRecordingWrapperPreservesNativeToolSearchMode a DEFERRED model + sessionLogStore(EVENT_LOG) + ToolSearchMiddleware receives the catalog tool in the request with deferLoading=true (not hidden)
failoverWrapperPreservesNativeToolSearchMode the same model behind a configured fallbackModel (failover wrapper active, primary succeeds) still exposes the deferred-loading catalog entry
modelWrappersForwardSupportsToolChoiceSpecific an ON_MODEL_CALL middleware reading input.model().supportsToolChoiceSpecific() sees the declared value through both wrappers, true and false directions (a fallback-only run exercises the failover wrapper, a no-fallback run the Recorded wrapper); red-verified pre-fix (expected: <[true]> but was: <[false]>)
failoverWrapperForwardsStructuredOutputWithTools a model with divergent flags (plain=true, with-tools=false) reads false through the failover wrapper's WithTools (not the value derived from the forwarded plain flag); the true direction (plain=false, with-tools=true), the Recorded wrapper, and the plain flag are pinned as regressions; red-verified pre-fix (expected: <[false]> but was: <[true]>)
modelWrappersOverrideEveryModelDefaultProbe mechanical drift guard: reflects over Model's default methods and asserts both wrappers (captured live via ModelCallInput.model()) override every one, so a future probe added to Model without wrapper forwarding fails here instead of silently leaking the default; red-verified by removing an override (ReActAgent$2 must override Model.supportsToolChoiceSpecific())

Regression: full agentscope-core suite — 2947 tests, 0 failures, 8 pre-existing skips;
spotless:check clean.

…ing and failover wrappers

SessionModels.Recorded (assembled whenever session recording is active) and
the failover wrapper (assembled whenever fallbackModel is configured) forward
only a hand-picked subset of Model's capability probes, so the rest silently
leak the interface defaults. Three omissions accumulated across feature PRs:
getToolSearchMode (agentscope-ai#3680, both wrappers), supportsToolChoiceSpecific (agentscope-ai#2697,
both wrappers), and supportsNativeStructuredOutputWithTools (failover wrapper
only - the interface default derived it from the forwarded plain flag, which
is wrong for models whose two flags diverge).

For getToolSearchMode the effect was user-visible: DEFERRED/CLIENT models
silently degraded to FUNCTION behind either wrapper, so ToolSearchMiddleware
hid the catalog instead of exposing it with deferred loading, and the native
protocol implementations of AnthropicChatModel/OpenAIResponsesChatModel were
dead paths. The other two probes are exposed to ON_MODEL_CALL middlewares via
ModelCallInput.model(), which is the wrapper.

Forward all three probes and document the remaining heterogeneous-failover
limitation (a DEFERRED-shaped surface cannot be served by a CLIENT fallback)
in ToolSearchMiddleware's Javadoc and docs/v2 EN/ZH. Four regression tests,
red-verified pre-fix.
@mintlify

mintlify Bot commented Oct 31, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 2597a3a.

Approve preview

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

P2 重要不紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants