Skip to content

Commit dd8e07c

Browse files
fix(agents): stop fallback on gateway draining (#118101)
1 parent bbc8d3b commit dd8e07c

3 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎src/agents/failover-error.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
import { describe, expect, it } from "vitest";
66
import { createAgentRunStaleLifecycleError } from "../infra/agent-lifecycle-error.js";
7+
import { GatewayDrainingError } from "../process/gateway-work-admission.js";
78
import { classifyFailoverSignal } from "./embedded-agent-helpers/errors.js";
89
import {
910
buildFailoverRemediationHint,
@@ -1428,6 +1429,20 @@ describe("failover-error", () => {
14281429
expect(isNonProviderRuntimeCoordinationError(abortWrapper)).toBe(true);
14291430
});
14301431

1432+
it("returns true for direct and nested gateway drain admission failures", () => {
1433+
const draining = new GatewayDrainingError();
1434+
const causeWrapper = new Error("session send failed", { cause: draining });
1435+
const aggregateWrapper = new AggregateError(
1436+
[new Error("cleanup failed"), { error: draining }],
1437+
"agent run failed",
1438+
);
1439+
1440+
for (const error of [draining, causeWrapper, aggregateWrapper]) {
1441+
expect(isNonProviderRuntimeCoordinationError(error)).toBe(true);
1442+
expect(resolveModelFallbackError(error)).toEqual({ kind: "coordination", error });
1443+
}
1444+
});
1445+
14311446
it("returns true when the coordination error is nested via cause", () => {
14321447
const wrapped = new Error("wrapper", { cause: makeSessionLockError() });
14331448
expect(isNonProviderRuntimeCoordinationError(wrapped)).toBe(true);

‎src/agents/failover-error.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { parseStrictNonNegativeInteger } from "@openclaw/normalization-core/number-coercion";
77
import { formatCliCommand } from "../cli/command-format.js";
88
import { isAgentRunStaleLifecycleError } from "../infra/agent-lifecycle-error.js";
9-
import { readErrorName } from "../infra/errors.js";
9+
import { collectErrorGraphCandidates, readErrorName } from "../infra/errors.js";
1010
import {
1111
classifyFailoverSignal,
1212
extractFailoverSignalDetails,
@@ -505,6 +505,13 @@ function hasStaleAgentRunLifecycleFailure(err: unknown): boolean {
505505
);
506506
}
507507

508+
function hasGatewayDrainingFailure(err: unknown): boolean {
509+
return collectErrorGraphCandidates(err, (candidate) => {
510+
const errors = candidate.errors;
511+
return [candidate.error, candidate.cause, ...(Array.isArray(errors) ? errors : [])];
512+
}).some((candidate) => readErrorName(candidate) === "GatewayDrainingError");
513+
}
514+
508515
function hasDirectProviderFailureIdentity(err: unknown): boolean {
509516
if (isFailoverError(err)) {
510517
return true;
@@ -919,6 +926,11 @@ export function resolveModelFallbackError(
919926
if (err instanceof AgentHarnessSessionSupersededError) {
920927
return { kind: "coordination", error: err };
921928
}
929+
// Gateway admission can fail before any provider turn starts. Preserve that
930+
// identity through wrappers and aggregates so fallback cannot blame a model.
931+
if (hasGatewayDrainingFailure(err)) {
932+
return { kind: "coordination", error: err };
933+
}
922934
const staleLifecycleFailure = hasStaleAgentRunLifecycleFailure(err);
923935
if (
924936
staleLifecycleFailure &&

‎src/agents/model-fallback.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { createWarnLogCapture } from "../logging/test-helpers/warn-log-capture.j
1616
import { setCurrentPluginMetadataSnapshot } from "../plugins/current-plugin-metadata-snapshot.js";
1717
import { clearCurrentPluginMetadataSnapshot } from "../plugins/current-plugin-metadata-state.js";
1818
import { loadPluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js";
19+
import { GatewayDrainingError } from "../process/gateway-work-admission.js";
1920
import { AgentRunTerminalOutcomeError } from "./agent-run-terminal-outcome.js";
2021
import { AUTH_STORE_VERSION } from "./auth-profiles/constants.js";
2122
import type { AuthProfileStore } from "./auth-profiles/types.js";
@@ -2080,6 +2081,60 @@ describe("runWithModelFallback", () => {
20802081
);
20812082
});
20822083

2084+
it.each([
2085+
["direct", () => new GatewayDrainingError()],
2086+
["cause", () => new Error("session send failed", { cause: new GatewayDrainingError() })],
2087+
[
2088+
"aggregate",
2089+
() =>
2090+
new AggregateError(
2091+
[new Error("cleanup failed"), new GatewayDrainingError()],
2092+
"agent run failed",
2093+
),
2094+
],
2095+
])("aborts fallback on %s gateway drain failures", async (_label, makeError) => {
2096+
const error = makeError();
2097+
const run = vi.fn().mockRejectedValueOnce(error).mockResolvedValueOnce("too late");
2098+
const onError = vi.fn();
2099+
const onFallbackStep = vi.fn();
2100+
2101+
await expect(
2102+
runWithModelFallback({
2103+
cfg: undefined,
2104+
provider: "openai",
2105+
model: "gpt-5.6-sol",
2106+
fallbacksOverride: ["openai/gpt-5.4-mini"],
2107+
skipAuthProfileRuntime: true,
2108+
run,
2109+
onError,
2110+
onFallbackStep,
2111+
}),
2112+
).rejects.toBe(error);
2113+
expect(run).toHaveBeenCalledTimes(1);
2114+
expect(onError).not.toHaveBeenCalled();
2115+
expect(onFallbackStep).not.toHaveBeenCalled();
2116+
});
2117+
2118+
it("still advances after a genuine provider rate limit", async () => {
2119+
const rateLimit = Object.assign(new Error("rate limit exceeded"), { status: 429 });
2120+
const run = vi.fn().mockRejectedValueOnce(rateLimit).mockResolvedValueOnce("fallback ok");
2121+
2122+
const result = await runWithModelFallback({
2123+
cfg: undefined,
2124+
provider: "openai",
2125+
model: "gpt-5.6-sol",
2126+
fallbacksOverride: ["openai/gpt-5.4-mini"],
2127+
skipAuthProfileRuntime: true,
2128+
run,
2129+
});
2130+
2131+
expect(run).toHaveBeenCalledTimes(2);
2132+
expect(result.result).toBe("fallback ok");
2133+
expect(result.provider).toBe("openai");
2134+
expect(result.model).toBe("gpt-5.4-mini");
2135+
expect(result.attempts[0]).toMatchObject({ reason: "rate_limit", status: 429 });
2136+
});
2137+
20832138
it("aborts the fallback chain on transcript continuation failures without candidate_failed attribution", async () => {
20842139
const cfg = makeCfg({
20852140
agents: {

0 commit comments

Comments
 (0)