Skip to content

Commit 26bff7d

Browse files
committed
refactor(agents): share incomplete-turn visibility classification
Follow up on #134980 by computing attempt visibility and silent-reply eligibility once, removing duplicated empty-turn predicates and a one-call terminal-error wrapper. Align the failover docs with the consolidated transient retry owner from #134281. Production delta: -71 lines; no behavior or configuration change.
1 parent 91067e3 commit 26bff7d

4 files changed

Lines changed: 26 additions & 97 deletions

File tree

‎docs/concepts/model-failover.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ If all profiles for a provider fail, OpenClaw moves to the next model in `agents
269269

270270
Provider-busy signals such as `ModelNotReadyException` land in the overloaded bucket and follow the same one-rotation-then-fallback policy as rate limits (see the defaults table above).
271271

272-
If the entire candidate chain is exhausted only by overload failures, the reply runner retries the chain up to 10 times in the same turn. Full-turn retry is allowed only before tool execution or assistant output starts, avoiding duplicate mutations or messages if an overload arrives after observable work. Backoff starts at 2.5 seconds and doubles to a 30-second cap. Once the turn has been waiting for 30 seconds, OpenClaw sends one transient status notice: `The AI service is temporarily overloaded. I’m still retrying; this may take a few minutes.` The retry and any fallback winner remain turn-local; ordinary transient server errors retain their separate one-retry policy.
272+
The embedded failover controller owns transient retries, including overloads and server errors. `retry.provider.maxRetries` sets the retry budget (default: 3), with jittered backoff, provider retry pacing, and a fixed 90-second retry window. Once that budget or window is exhausted, recovery proceeds to profile rotation, configured model fallback, or a visible error. Provider SDKs and the reply runner do not add separate replay loops. Retry and any fallback winner remain turn-local, and replay-unsafe attempts are not retried.
273273

274274
When a run starts from the configured default primary, a cron job primary, an agent primary with explicit fallbacks, or an auto-selected fallback override, OpenClaw can walk the matching configured fallback chain. Agent primaries without explicit fallbacks and explicit user selections (for example `/model ollama/qwen3.5:27b`, the model picker, `sessions.patch`, or one-off CLI provider/model overrides) are strict: if that provider/model is unreachable or fails before producing a reply, OpenClaw reports the failure instead of answering from an unrelated fallback.
275275

‎src/agents/embedded-agent-runner/run/incomplete-turn-classification.ts‎

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -131,69 +131,6 @@ export function isUnsignedThinkingOnlyAssistantTurn(message: unknown): boolean {
131131
return assessLastAssistantMessage(message as AgentMessage) === "incomplete-thinking";
132132
}
133133

134-
export function isEmptyResponseAssistantTurn(params: {
135-
payloadCount: number;
136-
attempt: Pick<
137-
IncompleteTurnAttempt,
138-
"assistantTexts" | "currentAttemptAssistant" | "currentAttemptCompletedAssistant"
139-
>;
140-
}): boolean {
141-
if (params.payloadCount !== 0) {
142-
return false;
143-
}
144-
if (joinAssistantTexts(params.attempt.assistantTexts).length > 0) {
145-
return false;
146-
}
147-
const assistant = resolveCurrentAttemptAssistant(params.attempt);
148-
if (!assistant) {
149-
return true;
150-
}
151-
if (assistant.stopReason === "error") {
152-
return false;
153-
}
154-
if (
155-
isIncompleteTerminalAssistantTurn({
156-
hasAssistantVisibleText: false,
157-
lastAssistant: assistant,
158-
}) ||
159-
isReasoningOnlyAssistantTurn(assistant)
160-
) {
161-
return false;
162-
}
163-
return true;
164-
}
165-
166-
export function isNonVisibleAssistantTurnEligibleForSilentReply(params: {
167-
payloadCount: number;
168-
attempt: Pick<
169-
IncompleteTurnAttempt,
170-
"assistantTexts" | "currentAttemptAssistant" | "currentAttemptCompletedAssistant"
171-
>;
172-
}): boolean {
173-
if (isEmptyResponseAssistantTurn(params)) {
174-
return true;
175-
}
176-
if (params.payloadCount !== 0) {
177-
return false;
178-
}
179-
if (joinAssistantTexts(params.attempt.assistantTexts).length > 0) {
180-
return false;
181-
}
182-
const assistant = resolveCurrentAttemptAssistant(params.attempt);
183-
if (!assistant || assistant.stopReason === "error") {
184-
return false;
185-
}
186-
if (
187-
isIncompleteTerminalAssistantTurn({
188-
hasAssistantVisibleText: false,
189-
lastAssistant: assistant,
190-
})
191-
) {
192-
return false;
193-
}
194-
return isReasoningOnlyAssistantTurn(assistant);
195-
}
196-
197134
export function shouldApplyNonVisibleTurnRetryGuard(params: {
198135
provider?: string;
199136
modelId?: string;
@@ -244,14 +181,21 @@ export function classifyAssistantTurn(params: {
244181
>;
245182
}) {
246183
const assistant = resolveCurrentAttemptAssistant(params.attempt);
184+
const visibleText = joinAssistantTexts(params.attempt.assistantTexts);
185+
const reasoningOnly = isReasoningOnlyAssistantTurn(assistant);
186+
const nonVisibleEligibleForSilentReply =
187+
params.payloadCount === 0 &&
188+
visibleText.length === 0 &&
189+
assistant?.stopReason !== "error" &&
190+
!isIncompleteTerminalAssistantTurn({
191+
hasAssistantVisibleText: false,
192+
lastAssistant: assistant,
193+
});
247194
return {
248195
assistant,
249-
visibleText: joinAssistantTexts(params.attempt.assistantTexts),
250-
onlySilentReply: hasOnlySilentAssistantReply(params.attempt.assistantTexts),
251-
reasoningOnly: isReasoningOnlyAssistantTurn(assistant),
252-
unsignedThinkingOnly: isUnsignedThinkingOnlyAssistantTurn(assistant),
253-
emptyResponse: isEmptyResponseAssistantTurn(params),
254-
nonVisibleEligibleForSilentReply: isNonVisibleAssistantTurnEligibleForSilentReply(params),
255-
hasPositiveOutputTokenUsage: hasPositiveOutputTokenUsage(assistant ?? null),
196+
visibleText,
197+
reasoningOnly,
198+
emptyResponse: nonVisibleEligibleForSilentReply && !reasoningOnly,
199+
nonVisibleEligibleForSilentReply,
256200
};
257201
}

‎src/agents/embedded-agent-runner/run/incomplete-turn-recovery.ts‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ import {
1515
resolveCurrentAttemptAssistant,
1616
} from "./attempt-terminal-evidence.js";
1717
import {
18+
classifyAssistantTurn,
1819
hasOnlySilentAssistantReply,
1920
hasPositiveOutputTokenUsage,
20-
isEmptyResponseAssistantTurn,
21-
isNonVisibleAssistantTurnEligibleForSilentReply,
2221
isOllamaIncompleteTurnProvider,
2322
isReasoningOnlyAssistantTurn,
2423
isUnsignedThinkingOnlyAssistantTurn,
@@ -144,10 +143,7 @@ export function shouldTreatEmptyAssistantReplyAsSilent(params: {
144143
if (params.onlyExplicitSilentReply || !terminalReplyOptional) {
145144
return false;
146145
}
147-
return isNonVisibleAssistantTurnEligibleForSilentReply({
148-
payloadCount: params.payloadCount,
149-
attempt: params.attempt,
150-
});
146+
return classifyAssistantTurn(params).nonVisibleEligibleForSilentReply;
151147
}
152148

153149
/**
@@ -338,10 +334,7 @@ export function resolveSettledToolTerminalContinuationInstruction(params: {
338334
attempt.itemLifecycle.completedCount === attempt.itemLifecycle.startedCount &&
339335
attempt.itemLifecycle.activeCount === 0 &&
340336
!hasAcceptedSessionSpawn(attempt.acceptedSessionSpawns) &&
341-
isEmptyResponseAssistantTurn({
342-
payloadCount: params.payloadCount,
343-
attempt,
344-
}),
337+
classifyAssistantTurn(params).emptyResponse,
345338
);
346339
if (
347340
params.payloadCount !== 0 ||
@@ -397,16 +390,12 @@ export function resolveEmptyResponseRetryInstruction(params: {
397390
return null;
398391
}
399392

400-
if (
401-
!isEmptyResponseAssistantTurn({
402-
payloadCount: params.payloadCount,
403-
attempt: params.attempt,
404-
})
405-
) {
393+
const assistantState = classifyAssistantTurn(params);
394+
if (!assistantState.emptyResponse) {
406395
return null;
407396
}
408397

409-
const assistant = resolveCurrentAttemptAssistant(params.attempt) ?? null;
398+
const assistant = assistantState.assistant ?? null;
410399
if (
411400
assistant?.stopReason === "stop" &&
412401
isOllamaIncompleteTurnProvider(params.provider) &&

‎src/llm/utils/retry.ts‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,17 @@ const REPLAY_UNSAFE_ASSISTANT_ERROR_CODES = new Set([
1616
PROVIDER_POST_DISPATCH_AMBIGUITY_ERROR_CODE,
1717
]);
1818

19-
/** True when replaying the failed assistant request could duplicate unknown provider output. */
20-
function isReplayUnsafeAssistantError(
21-
message: Pick<AssistantMessage, "errorCode"> | null | undefined,
22-
): boolean {
23-
return Boolean(message?.errorCode && REPLAY_UNSAFE_ASSISTANT_ERROR_CODES.has(message.errorCode));
24-
}
25-
2619
/**
2720
* Preserve structured terminal outcomes before text classification.
28-
* Retrying or falling back would override the provider's recorded decision.
21+
* Replay could duplicate unknown output or override the provider's refusal.
2922
*/
3023
export function isTerminalAssistantError(
3124
message: Pick<AssistantMessage, "diagnostics" | "errorCode"> | null | undefined,
3225
): boolean {
33-
return isReplayUnsafeAssistantError(message) || isProviderRefusalAssistantError(message);
26+
return (
27+
Boolean(message?.errorCode && REPLAY_UNSAFE_ASSISTANT_ERROR_CODES.has(message.errorCode)) ||
28+
isProviderRefusalAssistantError(message)
29+
);
3430
}
3531

3632
/** Classify transient provider/transport failures for outer retry policy. */

0 commit comments

Comments
 (0)