Repository navigation
Description
Is your feature request related to a problem? Please describe.
When ReActAgent falls back from a failed primary model to fallbackModel, the switch is not surfaced on any framework contract. The original error is consumed inside the anonymous Model wrapper built by ReActAgent#modelForCall(): its switchOnFirst block logs a single WARN and discards the Throwable. No surface that downstream code can build on reports either the switch or its cause:
ModelCallEndEventcarries onlyreplyIdandusage— neither the model that served the call nor any error;- hooks receive the serving model's name per chunk (
fireReasoningChunk) but never the error; - middlewares observe the
AgentEventstream, where the error never appears — it is swallowed insidemci.model().stream(...), below the event conversion.
What can be observed today are implicit side channels, each losing part of the fact:
- wrapping the primary
ModelwithdoOnErrorsees the error, but cannot tell whether a fallback actually took over or the error propagated to the caller; - the wrapper's
getModelName()flips to the fallback after the switch — attribution-grade, but it loses theThrowable, so a 429 cannot be told from a 500 or a quota error.
Consumers that need the switch itself — degraded-mode alerting, cooldown bookkeeping, per-provider accounting of which model served which call — are left with the log line: rate limiting, quota exhaustion, and provider outages are masked as silent success.
Describe the solution you'd like
A callback invoked at the switch site, with the original error — e.g.:
public interface FailoverPolicy {
/** Called when the primary model fails and the fallback takes over. */
default void onFailover(Model primary, Throwable error) {}
}Required semantics, whatever the exact shape:
- invoked synchronously at the switch site with the original
Throwable; - implementations must be non-blocking (reactor thread) and thread-safe (one policy shared across the agent's concurrent runs);
- an exception thrown by the implementation must not affect the switch (log-and-continue);
- no policy configured (the default) behaves exactly as today.
Mounting naturally sits alongside fallbackModel (ModelConfig + builder setters); whether this lands as a new interface or on an existing observer surface (the hook dispatcher) is open for discussion.
Describe alternatives you've considered
- Decorating the primary
Model(delegate.stream(...).doOnError(...)): zero framework changes, and sufficient for cooldown-style bookkeeping on the primary's failures. Not a contract for the switch itself: it cannot distinguish "running degraded on the fallback" from "no fallback configured, error propagated", and every downstream reimplements it by hand. - A new
AgentEventtype: the switch happens in theModellayer (Flux<ChatResponse>), below the conversion intoAgentEvents; surfacing it as an event crosses that layer boundary and forces every event consumer — and the serialization side — to handle a new type. A callback keeps the signal at the mechanism layer without touching the event model.
Additional context
Verified on current main (commit 7d321e2b): ReActAgent#modelForCall() (switchOnFirst block), ModelCallEndEvent fields, per-chunk model-name attribution in fireReasoningChunk.
Activity
[-][Feature]: No way to observe fallback model switching — the primary model's failure is swallowed inside ReActAgent[/-][+][Feature]: Surface fallback model switching to application code — the switch and its cause currently reach only the log[/+]oss-maintainer commentedon Sep 14, 2026
Issue Evaluation
Category:
enhancement| Area:area/core/agent| Status: Evaluated — feasible, needs a design decisionThe problem statement checks out. Re-verified against
main@7d321e2b, every claim in the report is accurate:ReActAgent#modelForCall()(ReActAgent.java:4065-4090) wraps the primary inswitchOnFirst; onsignal.isOnError()it captures theThrowable, flipsactiveModel, emits a singlelog.warn(...)and returnsfallbackModel.stream(...). The error never leaves that lambda.ModelCallEndEvent(ModelCallEndEvent.java:27-28) carries onlyreplyIdandusage— no serving-model identity and no error.fireReasoningChunk(...)against the wrapper'sgetModelName(), but never the cause, so a 429 is indistinguishable from a 500 or a quota error.AgentEventstream, which is downstream of the recovery point — the switch is invisible there too.Feasibility: moderate, and additive.
fallbackModelalready lives on theModelConfigrecord plusReActAgent.Builder#fallbackModel(...)(ReActAgent.java:4989,:5001), so a policy mounted alongside it follows an existing shape. Default (no policy configured) is byte-for-byte today's behaviour, and there is no change to the event model or its serialization — which is the right call, and rules out the new-AgentEvent-type alternative for the reasons given in the report.One compatibility constraint to respect:
ModelConfigis arecord (int maxRetries, Model fallbackModel). Adding a third component breaks binary compatibility for any externalnew ModelConfig(...)caller. Keep the existing two-arg constructor delegating with a null/no-op policy.Design decision needed before implementation — the notification surface:
FailoverPolicycallback invoked at the switch site: the contract is explicit and non-blocking-thread-friendly, but adds a new public core type plus plumbing throughModelConfig, the builder, andfromAgent(...)copy paths.Throwable, which touches every hook implementation.Option 1 matches the stated required semantics most directly (synchronous at the switch site, original
Throwable, log-and-continue on implementation error). Could a maintainer confirm the shape and naming (FailoverPolicy.onFailover(Model primary, Throwable error)vs. a method on the hook/Middleware surface) before this is implemented?Two semantics worth pinning down while deciding:
ModelConfigjavadoc says the fallback shares the primary'smaxRetriesbudget. Should the callback fire once per switch (recommended), or per primary attempt that failed?fromAgent(...)copies model-resilience config today, so the policy must be carried there as well ortoBuilderround-trips will silently drop it.Test coverage today:
ReActAgentTest#testFallbackModel,ReActAgentSummarizingTest#testSummaryHooksUseFallbackModelName,ReActAgentNewLoopBuilderTest#fromAgentCopiesModelResilienceConfig— these pin that the switch happens; none asserts that the switch is observable. An implementation would add: callback fires once with the original cause, a throwing implementation does not break the switch, and the no-policy default is unchanged.Not generating an automated fix proposal for this one — a new public core interface in
agentscope-corecascades to harness/distribution/extensions and needs maintainer sign-off on the API shape first.Automated evaluation by github-manager-bot