Skip to content

Commit b1a7386

Browse files
author
Julius Marminge
committed
Fix Claude auth probe fallback
1 parent 31dfe35 commit b1a7386

2 files changed

Lines changed: 142 additions & 5 deletions

File tree

‎apps/server/src/provider/Layers/ClaudeProvider.ts‎

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import {
33
type ModelCapabilities,
44
type ModelSelection,
55
ProviderDriverKind,
6+
type ServerProviderAuth,
67
type ServerProviderModel,
78
type ServerProviderSlashCommand,
9+
type ServerProviderState,
810
} from "@t3tools/contracts";
911
import * as DateTime from "effect/DateTime";
1012
import * as Effect from "effect/Effect";
@@ -30,11 +32,14 @@ import {
3032
buildBooleanOptionDescriptor,
3133
buildSelectOptionDescriptor,
3234
buildServerProvider,
35+
AUTH_PROBE_TIMEOUT_MS,
3336
DEFAULT_TIMEOUT_MS,
37+
extractAuthBoolean,
3438
isCommandMissingCause,
3539
parseGenericCliVersion,
3640
providerModelsFromSettings,
3741
spawnAndCollect,
42+
type CommandResult,
3843
type ServerProviderDraft,
3944
} from "../providerSnapshot.ts";
4045
import { makeClaudeEnvironment } from "../Drivers/ClaudeHome.ts";
@@ -444,6 +449,81 @@ function claudeAuthMetadata(input: {
444449
return undefined;
445450
}
446451

452+
function parseClaudeAuthStatusFromOutput(result: CommandResult): {
453+
readonly status: Exclude<ServerProviderState, "disabled">;
454+
readonly auth: Pick<ServerProviderAuth, "status">;
455+
readonly message?: string;
456+
} {
457+
const lowerOutput = `${result.stdout}\n${result.stderr}`.toLowerCase();
458+
459+
if (
460+
lowerOutput.includes("unknown command") ||
461+
lowerOutput.includes("unrecognized command") ||
462+
lowerOutput.includes("unexpected argument")
463+
) {
464+
return {
465+
status: "warning",
466+
auth: { status: "unknown" },
467+
message:
468+
"Claude Agent authentication status command is unavailable in this version of Claude.",
469+
};
470+
}
471+
472+
if (
473+
lowerOutput.includes("not logged in") ||
474+
lowerOutput.includes("login required") ||
475+
lowerOutput.includes("authentication required") ||
476+
lowerOutput.includes("run `claude login`") ||
477+
lowerOutput.includes("run claude login")
478+
) {
479+
return {
480+
status: "error",
481+
auth: { status: "unauthenticated" },
482+
message: "Claude is not authenticated. Run `claude auth login` and try again.",
483+
};
484+
}
485+
486+
const trimmedStdout = result.stdout.trim();
487+
let parsedAuth: boolean | undefined;
488+
let parsedJson = false;
489+
if (trimmedStdout.startsWith("{") || trimmedStdout.startsWith("[")) {
490+
try {
491+
parsedAuth = extractAuthBoolean(JSON.parse(trimmedStdout) as unknown);
492+
parsedJson = true;
493+
} catch {
494+
// Fall through to the command exit status for non-JSON output.
495+
}
496+
}
497+
498+
if (parsedAuth === true) {
499+
return { status: "ready", auth: { status: "authenticated" } };
500+
}
501+
if (parsedAuth === false) {
502+
return {
503+
status: "error",
504+
auth: { status: "unauthenticated" },
505+
message: "Claude is not authenticated. Run `claude auth login` and try again.",
506+
};
507+
}
508+
if (parsedJson) {
509+
return {
510+
status: "warning",
511+
auth: { status: "unknown" },
512+
message:
513+
"Could not verify Claude authentication status from JSON output (missing auth marker).",
514+
};
515+
}
516+
if (result.code === 0) {
517+
return { status: "ready", auth: { status: "authenticated" } };
518+
}
519+
520+
return {
521+
status: "warning",
522+
auth: { status: "unknown" },
523+
message: "Could not verify Claude authentication status.",
524+
};
525+
}
526+
447527
// ── SDK capability probe ────────────────────────────────────────────
448528

449529
const CAPABILITIES_PROBE_TIMEOUT_MS = 8_000;
@@ -741,6 +821,34 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
741821
const dedupedSlashCommands = dedupeSlashCommands(slashCommands);
742822

743823
if (!capabilities) {
824+
const authProbe = yield* runClaudeCommand(
825+
claudeSettings,
826+
["auth", "status"],
827+
resolvedEnvironment,
828+
).pipe(Effect.timeoutOption(AUTH_PROBE_TIMEOUT_MS), Effect.result);
829+
830+
if (Result.isSuccess(authProbe) && Option.isSome(authProbe.success)) {
831+
const parsedAuth = parseClaudeAuthStatusFromOutput(authProbe.success.value);
832+
return buildServerProvider({
833+
presentation: CLAUDE_PRESENTATION,
834+
enabled: claudeSettings.enabled,
835+
checkedAt,
836+
models,
837+
slashCommands: dedupedSlashCommands,
838+
probe: {
839+
installed: true,
840+
version: parsedVersion,
841+
status: parsedAuth.status,
842+
auth: parsedAuth.auth,
843+
...(parsedAuth.message
844+
? { message: parsedAuth.message }
845+
: versionUpgradeMessage
846+
? { message: versionUpgradeMessage }
847+
: {}),
848+
},
849+
});
850+
}
851+
744852
return buildServerProvider({
745853
presentation: CLAUDE_PRESENTATION,
746854
enabled: claudeSettings.enabled,
@@ -752,7 +860,10 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
752860
version: parsedVersion,
753861
status: "warning",
754862
auth: { status: "unknown" },
755-
message: "Could not verify Claude authentication status from initialization result.",
863+
message:
864+
Result.isSuccess(authProbe) && Option.isNone(authProbe.success)
865+
? "Could not verify Claude authentication status. Timed out while running command."
866+
: "Could not verify Claude authentication status.",
756867
},
757868
});
758869
}

‎apps/server/src/provider/Layers/ProviderRegistry.test.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,18 +1902,44 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
19021902
);
19031903
});
19041904

1905-
it.effect("returns warning when the Claude initialization result is unavailable", () =>
1905+
it.effect("falls back to CLI auth when the Claude initialization result is unavailable", () =>
19061906
Effect.gen(function* () {
19071907
const status = yield* checkClaudeProviderStatus(
19081908
defaultClaudeSettings,
19091909
noClaudeCapabilities,
19101910
);
1911-
assert.strictEqual(status.status, "warning");
1911+
assert.strictEqual(status.status, "ready");
19121912
assert.strictEqual(status.installed, true);
1913-
assert.strictEqual(status.auth.status, "unknown");
1913+
assert.strictEqual(status.auth.status, "authenticated");
1914+
}).pipe(
1915+
Effect.provide(
1916+
mockSpawnerLayer((args) => {
1917+
const joined = args.join(" ");
1918+
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
1919+
if (joined === "auth status")
1920+
return {
1921+
stdout: '{"loggedIn":true,"authMethod":"third_party","apiProvider":"bedrock"}\n',
1922+
stderr: "",
1923+
code: 0,
1924+
};
1925+
throw new Error(`Unexpected args: ${joined}`);
1926+
}),
1927+
),
1928+
),
1929+
);
1930+
1931+
it.effect("returns unauthenticated when the CLI auth fallback reports logged out", () =>
1932+
Effect.gen(function* () {
1933+
const status = yield* checkClaudeProviderStatus(
1934+
defaultClaudeSettings,
1935+
noClaudeCapabilities,
1936+
);
1937+
assert.strictEqual(status.status, "error");
1938+
assert.strictEqual(status.installed, true);
1939+
assert.strictEqual(status.auth.status, "unauthenticated");
19141940
assert.strictEqual(
19151941
status.message,
1916-
"Could not verify Claude authentication status from initialization result.",
1942+
"Claude is not authenticated. Run `claude auth login` and try again.",
19171943
);
19181944
}).pipe(
19191945
Effect.provide(

0 commit comments

Comments
 (0)