Skip to content

Commit 35ed708

Browse files
committed
fix(server): report Claude authentication accurately
1 parent beab688 commit 35ed708

2 files changed

Lines changed: 251 additions & 22 deletions

File tree

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as FileSystem from "effect/FileSystem";
1111
import * as Option from "effect/Option";
1212
import * as Path from "effect/Path";
1313
import * as Result from "effect/Result";
14+
import * as Schema from "effect/Schema";
1415
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
1516
import {
1617
createModelCapabilities,
@@ -29,6 +30,7 @@ import {
2930
} from "@anthropic-ai/claude-agent-sdk";
3031

3132
import {
33+
AUTH_PROBE_TIMEOUT_MS,
3234
buildBooleanOptionDescriptor,
3335
buildSelectOptionDescriptor,
3436
buildServerProvider,
@@ -55,6 +57,15 @@ const MINIMUM_CLAUDE_OPUS_5_VERSION = "2.1.219";
5557
const MINIMUM_CLAUDE_FABLE_5_VERSION = "2.1.169";
5658
const MINIMUM_CLAUDE_OPUS_4_8_VERSION = "2.1.154";
5759
const MINIMUM_CLAUDE_OPUS_4_7_VERSION = "2.1.111";
60+
const MINIMUM_CLAUDE_AUTH_STATUS_VERSION = "2.1.41";
61+
62+
const decodeClaudeAuthStatus = Schema.decodeUnknownOption(
63+
Schema.fromJsonString(
64+
Schema.Struct({
65+
loggedIn: Schema.Boolean,
66+
}),
67+
),
68+
);
5869

5970
const CURRENT_CLAUDE_MODELS = new Set(["claude-fable-5", "claude-opus-5", "claude-sonnet-5"]);
6071

@@ -347,6 +358,22 @@ function supportsClaudeOpus47(version: string | null | undefined): boolean {
347358
return version ? compareSemverVersions(version, MINIMUM_CLAUDE_OPUS_4_7_VERSION) >= 0 : false;
348359
}
349360

361+
function supportsClaudeAuthStatus(version: string | null | undefined): boolean {
362+
return version ? compareSemverVersions(version, MINIMUM_CLAUDE_AUTH_STATUS_VERSION) >= 0 : false;
363+
}
364+
365+
function classifyClaudeAuthStatus(
366+
result: { readonly stdout: string; readonly code: number } | undefined,
367+
): "authenticated" | "unauthenticated" | "unknown" {
368+
if (!result) return "unknown";
369+
370+
const decoded = decodeClaudeAuthStatus(result.stdout);
371+
const loggedIn = Option.isSome(decoded) ? decoded.value.loggedIn : undefined;
372+
if (loggedIn === false) return "unauthenticated";
373+
if (loggedIn === true) return result.code === 0 ? "authenticated" : "unknown";
374+
return result.code === 1 ? "unauthenticated" : "unknown";
375+
}
376+
350377
function getBuiltInClaudeModelsForVersion(
351378
version: string | null | undefined,
352379
): ReadonlyArray<ServerProviderModel> {
@@ -953,6 +980,43 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
953980
subscriptionType: capabilities.subscriptionType,
954981
authMethod: capabilities.tokenSource,
955982
}) ?? apiProviderAuthMetadata(capabilities.apiProvider);
983+
const hasExternalApiProvider =
984+
capabilities.apiProvider !== undefined && capabilities.apiProvider !== "firstParty";
985+
986+
if (!hasExternalApiProvider && supportsClaudeAuthStatus(parsedVersion)) {
987+
const authProbe = yield* runClaudeCommand(
988+
claudeSettings,
989+
["auth", "status"],
990+
resolvedEnvironment,
991+
).pipe(Effect.timeoutOption(AUTH_PROBE_TIMEOUT_MS), Effect.result);
992+
const authResult =
993+
Result.isSuccess(authProbe) && Option.isSome(authProbe.success)
994+
? authProbe.success.value
995+
: undefined;
996+
const authStatus = classifyClaudeAuthStatus(authResult);
997+
998+
if (authStatus !== "authenticated") {
999+
const isUnauthenticated = authStatus === "unauthenticated";
1000+
return buildServerProvider({
1001+
presentation: CLAUDE_PRESENTATION,
1002+
enabled: claudeSettings.enabled,
1003+
checkedAt,
1004+
models,
1005+
slashCommands: dedupedSlashCommands,
1006+
skills,
1007+
probe: {
1008+
installed: true,
1009+
version: parsedVersion,
1010+
status: isUnauthenticated ? "error" : "warning",
1011+
auth: { status: isUnauthenticated ? "unauthenticated" : "unknown" },
1012+
message: isUnauthenticated
1013+
? "Claude is not authenticated. Run `claude auth login` and try again."
1014+
: "Could not verify Claude authentication status.",
1015+
},
1016+
});
1017+
}
1018+
}
1019+
9561020
return buildServerProvider({
9571021
presentation: CLAUDE_PRESENTATION,
9581022
enabled: claudeSettings.enabled,

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

Lines changed: 187 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,51 @@ function mockSpawnerLayer(
182182
);
183183
}
184184

185+
function mockClaudeAuthStatusLayer(status: "authenticated" | "unauthenticated") {
186+
const loggedIn = status === "authenticated";
187+
return mockSpawnerLayer((args) => {
188+
const joined = args.join(" ");
189+
if (joined === "--version") return { stdout: "2.1.41\n", stderr: "", code: 0 };
190+
if (joined === "auth status") {
191+
return {
192+
stdout: `${JSON.stringify({
193+
loggedIn,
194+
authMethod: loggedIn ? "claude.ai" : "none",
195+
})}\n`,
196+
stderr: "",
197+
code: loggedIn ? 0 : 1,
198+
};
199+
}
200+
throw new Error(`Unexpected args: ${joined}`);
201+
});
202+
}
203+
204+
function failingClaudeAuthStatusLayer(description: string) {
205+
return Layer.succeed(
206+
ChildProcessSpawner.ChildProcessSpawner,
207+
ChildProcessSpawner.make((command) => {
208+
const cmd = command as unknown as { args: ReadonlyArray<string> };
209+
if (cmd.args.join(" ") === "--version") {
210+
return Effect.succeed(
211+
mockHandle({
212+
stdout: "2.1.41\n",
213+
stderr: "",
214+
code: 0,
215+
}),
216+
);
217+
}
218+
return Effect.fail(
219+
PlatformError.systemError({
220+
_tag: "Unknown",
221+
module: "ChildProcess",
222+
method: "spawn",
223+
description,
224+
}),
225+
);
226+
}),
227+
);
228+
}
229+
185230
function recordingMockSpawnerLayer(
186231
handler: (args: ReadonlyArray<string>) => {
187232
stdout: string;
@@ -1783,7 +1828,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
17831828
Effect.provide(
17841829
mockSpawnerLayer((args) => {
17851830
const joined = args.join(" ");
1786-
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
1831+
if (joined === "--version") return { stdout: "2.1.41\n", stderr: "", code: 0 };
17871832
if (joined === "auth status")
17881833
return {
17891834
stdout: '{"loggedIn":true,"authMethod":"claude.ai"}\n',
@@ -1796,6 +1841,136 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
17961841
),
17971842
);
17981843

1844+
it.effect(
1845+
"returns unauthenticated when initialization has no account and the CLI reports logged out",
1846+
() =>
1847+
Effect.gen(function* () {
1848+
const status = yield* checkClaudeProviderStatus(
1849+
defaultClaudeSettings,
1850+
claudeCapabilities(),
1851+
);
1852+
assert.strictEqual(status.status, "error");
1853+
assert.strictEqual(status.installed, true);
1854+
assert.strictEqual(status.auth.status, "unauthenticated");
1855+
assert.strictEqual(
1856+
status.message,
1857+
"Claude is not authenticated. Run `claude auth login` and try again.",
1858+
);
1859+
}).pipe(Effect.provide(mockClaudeAuthStatusLayer("unauthenticated"))),
1860+
);
1861+
1862+
it.effect(
1863+
"prefers the CLI logged-out result over stale initialization account metadata",
1864+
() =>
1865+
Effect.gen(function* () {
1866+
const status = yield* checkClaudeProviderStatus(
1867+
defaultClaudeSettings,
1868+
claudeCapabilities({
1869+
1870+
subscriptionType: "team",
1871+
apiProvider: "firstParty",
1872+
}),
1873+
);
1874+
assert.strictEqual(status.status, "error");
1875+
assert.strictEqual(status.auth.status, "unauthenticated");
1876+
}).pipe(Effect.provide(mockClaudeAuthStatusLayer("unauthenticated"))),
1877+
);
1878+
1879+
it.effect("uses loggedIn=false even when the auth command exits successfully", () =>
1880+
Effect.gen(function* () {
1881+
const status = yield* checkClaudeProviderStatus(
1882+
defaultClaudeSettings,
1883+
claudeCapabilities({
1884+
1885+
subscriptionType: "team",
1886+
apiProvider: "firstParty",
1887+
}),
1888+
);
1889+
assert.strictEqual(status.status, "error");
1890+
assert.strictEqual(status.auth.status, "unauthenticated");
1891+
}).pipe(
1892+
Effect.provide(
1893+
mockSpawnerLayer((args) => {
1894+
const joined = args.join(" ");
1895+
if (joined === "--version") {
1896+
return { stdout: "2.1.41\n", stderr: "", code: 0 };
1897+
}
1898+
if (joined === "auth status") {
1899+
return {
1900+
stdout: '{"loggedIn":false,"authMethod":"none"}\n',
1901+
stderr: "",
1902+
code: 0,
1903+
};
1904+
}
1905+
throw new Error(`Unexpected args: ${joined}`);
1906+
}),
1907+
),
1908+
),
1909+
);
1910+
1911+
it.effect("preserves initialization auth on Claude versions without auth status", () =>
1912+
Effect.gen(function* () {
1913+
const status = yield* checkClaudeProviderStatus(
1914+
defaultClaudeSettings,
1915+
claudeCapabilities({
1916+
1917+
subscriptionType: "team",
1918+
apiProvider: "firstParty",
1919+
}),
1920+
);
1921+
assert.strictEqual(status.status, "ready");
1922+
assert.strictEqual(status.auth.status, "authenticated");
1923+
}).pipe(
1924+
Effect.provide(
1925+
mockSpawnerLayer((args) => {
1926+
if (args.join(" ") === "--version") {
1927+
return { stdout: "2.1.40\n", stderr: "", code: 0 };
1928+
}
1929+
throw new Error(`Unexpected args: ${args.join(" ")}`);
1930+
}),
1931+
),
1932+
),
1933+
);
1934+
1935+
it.effect("returns unknown when the auth command exits unexpectedly", () =>
1936+
Effect.gen(function* () {
1937+
const status = yield* checkClaudeProviderStatus(
1938+
defaultClaudeSettings,
1939+
claudeCapabilities(),
1940+
);
1941+
assert.strictEqual(status.status, "warning");
1942+
assert.strictEqual(status.installed, true);
1943+
assert.strictEqual(status.auth.status, "unknown");
1944+
assert.strictEqual(status.message, "Could not verify Claude authentication status.");
1945+
}).pipe(
1946+
Effect.provide(
1947+
mockSpawnerLayer((args) => {
1948+
const joined = args.join(" ");
1949+
if (joined === "--version") {
1950+
return { stdout: "2.1.41\n", stderr: "", code: 0 };
1951+
}
1952+
if (joined === "auth status") {
1953+
return { stdout: "", stderr: "unexpected failure", code: 2 };
1954+
}
1955+
throw new Error(`Unexpected args: ${joined}`);
1956+
}),
1957+
),
1958+
),
1959+
);
1960+
1961+
it.effect("returns unknown when the auth command cannot be started", () =>
1962+
Effect.gen(function* () {
1963+
const status = yield* checkClaudeProviderStatus(
1964+
defaultClaudeSettings,
1965+
claudeCapabilities(),
1966+
);
1967+
assert.strictEqual(status.status, "warning");
1968+
assert.strictEqual(status.installed, true);
1969+
assert.strictEqual(status.auth.status, "unknown");
1970+
assert.strictEqual(status.message, "Could not verify Claude authentication status.");
1971+
}).pipe(Effect.provide(failingClaudeAuthStatusLayer("spawn auth status failed"))),
1972+
);
1973+
17991974
it.effect("returns ready and labels Bedrock-backed Claude as authenticated", () =>
18001975
Effect.gen(function* () {
18011976
// Bedrock authenticates via external AWS credentials, so the SDK init
@@ -2044,15 +2219,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
20442219
assert.strictEqual(status.auth.status, "authenticated");
20452220
assert.strictEqual(status.auth.type, "Claude Max Subscription");
20462221
assert.strictEqual(status.auth.label, "Claude Max Subscription");
2047-
}).pipe(
2048-
Effect.provide(
2049-
mockSpawnerLayer((args) => {
2050-
const joined = args.join(" ");
2051-
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
2052-
throw new Error(`Unexpected args: ${joined}`);
2053-
}),
2054-
),
2055-
),
2222+
}).pipe(Effect.provide(mockClaudeAuthStatusLayer("authenticated"))),
20562223
);
20572224

20582225
it.effect("does not duplicate Claude in provider-prefixed subscription names", () =>
@@ -2066,15 +2233,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
20662233
assert.strictEqual(status.auth.status, "authenticated");
20672234
assert.strictEqual(status.auth.type, "Claude Max");
20682235
assert.strictEqual(status.auth.label, "Claude Max Subscription");
2069-
}).pipe(
2070-
Effect.provide(
2071-
mockSpawnerLayer((args) => {
2072-
const joined = args.join(" ");
2073-
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
2074-
throw new Error(`Unexpected args: ${joined}`);
2075-
}),
2076-
),
2077-
),
2236+
}).pipe(Effect.provide(mockClaudeAuthStatusLayer("authenticated"))),
20782237
);
20792238

20802239
it.effect("returns claude auth email from initialization result", () =>
@@ -2107,7 +2266,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
21072266
const claudeConfigDir = "/tmp/t3code-claude-home";
21082267
const recorded = recordingMockSpawnerLayer((args) => {
21092268
const joined = args.join(" ");
2110-
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
2269+
if (joined === "--version") return { stdout: "2.1.41\n", stderr: "", code: 0 };
21112270
if (joined === "auth status")
21122271
return {
21132272
stdout: '{"loggedIn":true,"authMethod":"claude.ai"}\n',
@@ -2127,8 +2286,14 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
21272286
);
21282287
assert.strictEqual(status.status, "ready");
21292288
assert.deepStrictEqual(
2130-
recorded.commands.map((command) => command.env?.CLAUDE_CONFIG_DIR),
2131-
[claudeConfigDir],
2289+
recorded.commands.map((command) => ({
2290+
args: command.args,
2291+
claudeConfigDir: command.env?.CLAUDE_CONFIG_DIR,
2292+
})),
2293+
[
2294+
{ args: ["--version"], claudeConfigDir },
2295+
{ args: ["auth", "status"], claudeConfigDir },
2296+
],
21322297
);
21332298
}).pipe(Effect.provide(recorded.layer));
21342299
});

0 commit comments

Comments
 (0)