Skip to content

Commit 468054f

Browse files
MarvintheboredPeetiegonzalezobviyus
authored
fix(agents): surface Claude OAuth expiry in channels (#131345)
Classify the dependency-owned Claude CLI OAuth expiry as authentication so channel users receive the existing re-authentication steps. Preserve resumable-session recovery for other CLI providers, missing provider metadata, and real stale conversation IDs. Co-authored-by: Marvinthebored <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 1454c60 commit 468054f

4 files changed

Lines changed: 46 additions & 27 deletions

File tree

‎src/agents/failover/classification-rules.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,15 @@ export function isExactUnknownNoDetailsError(raw: string): boolean {
407407
normalizeOptionalLowercaseString(raw)?.trim() === "unknown error (no error details in response)"
408408
);
409409
}
410-
export function isClaudeCliLoggedOutError(raw: string, provider?: string): boolean {
411-
// This upstream phrase is generic prose. Provider identity must come from
412-
// the runner metadata so other providers cannot inherit Claude CLI policy.
410+
export function isClaudeCliAuthError(raw: string, provider?: string): boolean {
411+
// These upstream phrases overlap generic session/auth wording. Provider identity
412+
// must come from runner metadata so other CLIs cannot inherit Claude policy.
413413
if (normalizeOptionalLowercaseString(provider)?.trim() !== "claude-cli") {
414414
return false;
415415
}
416-
return /\bnot logged in\b\s*·\s*please run \/login\b/i.test(raw);
416+
return /\bnot logged in\b\s*·\s*please run \/login\b|\bfailed to authenticate:\s*oauth session expired and could not be refreshed\b/i.test(
417+
raw,
418+
);
417419
}
418420
export function isUnsupportedImageInputErrorMessage(raw: string | undefined): boolean {
419421
const normalized = normalizeOptionalLowercaseString(raw);

‎src/agents/failover/classify.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,15 @@ describe("Claude CLI logged-out failures", () => {
1010
expect(classifyFailoverReason(loggedOutMessage)).toBeNull();
1111
});
1212
});
13+
14+
describe("OAuth session expiry", () => {
15+
const expiredMessage = "Failed to authenticate: OAuth session expired and could not be refreshed";
16+
17+
it("classifies OAuth expiry as auth only for claude-cli", () => {
18+
expect(classifyFailoverReason(expiredMessage, { provider: "claude-cli" })).toBe("auth");
19+
expect(classifyFailoverReason(expiredMessage, { provider: "custom-cli" })).toBe(
20+
"session_expired",
21+
);
22+
expect(classifyFailoverReason(expiredMessage)).toBe("session_expired");
23+
});
24+
});

‎src/agents/failover/classify.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
classifyFailoverReasonFromCode,
2323
failoverReasonFromClassification,
2424
inferSignalStatus,
25-
isClaudeCliLoggedOutError,
25+
isClaudeCliAuthError,
2626
isExactUnknownNoDetailsError,
2727
isGenericUnknownStreamErrorMessage,
2828
isTransientHttpError,
@@ -123,6 +123,9 @@ function classifyFailoverClassificationFromMessage(
123123
if (isUnsupportedImageInputErrorMessage(raw)) {
124124
return toReasonClassification("format");
125125
}
126+
if (isClaudeCliAuthError(raw, provider)) {
127+
return toReasonClassification("auth");
128+
}
126129
if (isCliSessionExpiredErrorMessage(raw)) {
127130
return toReasonClassification("session_expired");
128131
}
@@ -192,9 +195,6 @@ function classifyFailoverClassificationFromMessage(
192195
// Auth classifiers run before the broad isJsonApiInternalServerError check so that
193196
// provider errors like {"type":"api_error","message":"invalid api key"} are
194197
// correctly classified as "auth" rather than "timeout".
195-
if (isClaudeCliLoggedOutError(raw, provider)) {
196-
return toReasonClassification("auth");
197-
}
198198
const oauthRefreshFailure = classifyOAuthRefreshFailure(raw);
199199
if (oauthRefreshFailure?.reason) {
200200
return toReasonClassification("auth_permanent");

‎src/auto-reply/reply/agent-runner-execution-auth-failures.test.ts‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "vitest";
22
import { OAuthRefreshFailureError } from "../../agents/auth-profiles/oauth-refresh-failure.js";
3+
import { createCliOutputFailoverError } from "../../agents/cli-runner/output-error.js";
34
import { FailoverError } from "../../agents/failover-error.js";
45
import { MissingProviderAuthError, ProviderAuthError } from "../../agents/model-auth.js";
56
import type { TemplateContext } from "../templating.js";
@@ -315,27 +316,31 @@ describe("executeAgentTurn: authentication failures", () => {
315316
}
316317
});
317318

318-
it("surfaces claude-cli re-auth hint over generic provider auth copy for 401 OAuth expiry", async () => {
319-
// When the claude subprocess emits a 401 "Failed to authenticate" because
320-
// its OAuth token has expired, the error is wrapped as a FailoverError with
321-
// reason:"auth" and status:401. Without the ordering fix, this would be
322-
// caught by generic provider-auth mapping before the typed refresh cause,
323-
// producing generic provider copy instead of the
324-
// targeted claude-cli re-auth command.
325-
state.runEmbeddedAgentMock.mockRejectedValueOnce(
326-
new FailoverError(
327-
"Provider claude-cli failed: Failed to authenticate. API Error: 401 Invalid authentication credentials",
328-
{
329-
reason: "auth",
330-
provider: "claude-cli",
331-
model: "claude-sonnet-4-20250514",
332-
status: 401,
333-
},
334-
),
335-
);
319+
it("surfaces Agent SDK OAuth session expiry in Discord channels", async () => {
320+
const error = createCliOutputFailoverError({
321+
output: {
322+
text: "",
323+
errorText: "Failed to authenticate: OAuth session expired and could not be refreshed",
324+
},
325+
provider: "claude-cli",
326+
model: "claude-opus-5",
327+
});
328+
if (!error) {
329+
throw new Error("expected CLI output failure");
330+
}
331+
state.runEmbeddedAgentMock.mockRejectedValueOnce(error);
336332

337333
const executeAgentTurn = await getExecuteAgentTurnForTest();
338-
const result = await executeAgentTurn(createMinimalRunAgentTurnParams());
334+
const result = await executeAgentTurn(
335+
createMinimalRunAgentTurnParams({
336+
sessionCtx: {
337+
Provider: "discord",
338+
Surface: "discord",
339+
ChatType: "channel",
340+
MessageSid: "msg",
341+
} as unknown as TemplateContext,
342+
}),
343+
);
339344

340345
expect(result.kind).toBe("final");
341346
if (result.kind === "final") {

0 commit comments

Comments
 (0)