@@ -4,8 +4,12 @@ import {
44 type ServerProviderSlashCommand ,
55 type ServerProviderResetCredits ,
66} from "@t3tools/contracts" ;
7+ import * as Cache from "effect/Cache" ;
8+ import * as Cause from "effect/Cause" ;
79import * as DateTime from "effect/DateTime" ;
10+ import * as Duration from "effect/Duration" ;
811import * as Effect from "effect/Effect" ;
12+ import * as Exit from "effect/Exit" ;
913import * as FileSystem from "effect/FileSystem" ;
1014import * as Option from "effect/Option" ;
1115import * as Path from "effect/Path" ;
@@ -34,7 +38,7 @@ import {
3438 type ServerProviderDraft ,
3539} from "../providerSnapshot.ts" ;
3640import { resolveClaudeSdkExecutablePath } from "../Drivers/ClaudeExecutable.ts" ;
37- import { makeClaudeEnvironment } from "../Drivers/ClaudeHome.ts" ;
41+ import { makeClaudeCapabilitiesCacheKey , makeClaudeEnvironment } from "../Drivers/ClaudeHome.ts" ;
3842import { discoverClaudeSkills } from "../Drivers/ClaudeSkills.ts" ;
3943import { makeUnavailableUsageLimits } from "../providerUsageLimits.ts" ;
4044import {
@@ -168,9 +172,10 @@ function apiProviderAuthMetadata(
168172
169173// Amazon Bedrock initializes far slower than first-party auth: the SDK boots the
170174// Bedrock backend and runs the `awsAuthRefresh` credential hook before returning
171- // account info. The previous 8s budget expired mid-init, so the probe returned
172- // `undefined` and left the provider unverified and unselectable in the picker.
175+ // account info. The previous 8s budget expired mid-init, so the probe timed out
176+ // and left the provider unverified and unselectable in the picker.
173177const CAPABILITIES_PROBE_TIMEOUT_MS = 25_000 ;
178+ const CAPABILITIES_PROBE_TTL = Duration . minutes ( 5 ) ;
174179
175180/**
176181 * Keep workspace-scoped command discovery intact while isolating the periodic
@@ -237,9 +242,10 @@ type ClaudeCapabilitiesProbe = {
237242 readonly apiProvider : string | undefined ;
238243 readonly slashCommands : ReadonlyArray < ServerProviderSlashCommand > ;
239244 /**
240- * Subscription windows from the SDK's `get_usage` control request, or
241- * `undefined` when the request itself failed. Absent windows on an
242- * otherwise successful response mean the account has none (API key).
245+ * Subscription windows from the SDK's `get_usage` control request.
246+ * `undefined` when that request failed, or when the last good probe answers
247+ * for a failed one. Absent windows on an otherwise successful response mean
248+ * the account has none (API key).
243249 */
244250 readonly usage ?: Pick < SDKControlGetUsageResponse , "rate_limits_available" | "rate_limits" > ;
245251} ;
@@ -326,8 +332,8 @@ function waitForAbortSignal(signal: AbortSignal): Promise<void> {
326332 * account info and slash commands) but never starts an API request to
327333 * Anthropic. We read the init data and then abort the subprocess.
328334 *
329- * This is used as a fallback when `claude auth status` does not include
330- * subscription type information .
335+ * Fails with the timeout or SDK error. `makeClaudeCapabilitiesCache` decides
336+ * what a failed probe means for the provider snapshot .
331337 */
332338const probeClaudeCapabilities = (
333339 claudeSettings : ClaudeSettings ,
@@ -396,11 +402,58 @@ const probeClaudeCapabilities = (
396402 if ( ! abort . signal . aborted ) abort . abort ( ) ;
397403 } ) ,
398404 ) ,
399- Effect . result ,
400- Effect . map ( ( result ) => ( Result . isSuccess ( result ) ? result . success : undefined ) ) ,
405+ // Log only the tag. SDK errors can quote the CLI's stderr.
406+ Effect . tapError ( ( error ) =>
407+ Effect . logWarning ( "Claude capability probe failed." , { errorTag : error . _tag } ) ,
408+ ) ,
401409 ) ;
402410} ;
403411
412+ type ClaudeCapabilitiesProbeError = Effect . Error < ReturnType < typeof probeClaudeCapabilities > > ;
413+
414+ /**
415+ * One Claude instance's capability probe, as `checkClaudeProviderStatus` reads
416+ * it. Every answer is reused for five minutes. One stalled probe must not turn
417+ * a working Claude into an auth warning, so when a probe fails, the last good
418+ * probe answers once more, without its usage windows. Those would otherwise be
419+ * republished as current. A failure with nothing to fall back on reaches the
420+ * caller and is never cached, so the next status check probes again.
421+ */
422+ export const makeClaudeCapabilitiesCache = Effect . fn ( "makeClaudeCapabilitiesCache" ) ( function * (
423+ claudeSettings : ClaudeSettings ,
424+ environment ?: NodeJS . ProcessEnv ,
425+ cwd ?: string ,
426+ ) {
427+ // Keyed on binary + resolved HOME so account metadata never crosses instances.
428+ const key = yield * makeClaudeCapabilitiesCacheKey ( claudeSettings , cwd , environment ) ;
429+ // Emptied when it answers for a failed probe, so a second failure in a row surfaces.
430+ const lastGood = yield * Ref . make < ClaudeCapabilitiesProbe | undefined > ( undefined ) ;
431+ const cache = yield * Cache . makeWith (
432+ ( _key : string ) =>
433+ probeClaudeCapabilities ( claudeSettings , environment , cwd ) . pipe (
434+ Effect . tap ( ( capabilities ) => Ref . set ( lastGood , capabilities ) ) ,
435+ Effect . catch ( ( error ) =>
436+ Effect . flatMap ( Ref . getAndSet ( lastGood , undefined ) , ( last ) => {
437+ if ( ! last ) return Effect . fail ( error ) ;
438+ const { usage : _staleUsage , ...capabilities } = last ;
439+ return Effect . succeed < ClaudeCapabilitiesProbe > ( capabilities ) ;
440+ } ) ,
441+ ) ,
442+ ) ,
443+ {
444+ capacity : 1 ,
445+ timeToLive : Exit . match ( {
446+ onSuccess : ( ) => CAPABILITIES_PROBE_TTL ,
447+ onFailure : ( ) => Duration . zero ,
448+ } ) ,
449+ } ,
450+ ) ;
451+ return {
452+ get : Cache . get ( cache , key ) ,
453+ invalidate : Cache . invalidateAll ( cache ) ,
454+ } ;
455+ } ) ;
456+
404457const runClaudeCommand = Effect . fn ( "runClaudeCommand" ) ( function * (
405458 claudeSettings : ClaudeSettings ,
406459 args : ReadonlyArray < string > ,
@@ -419,9 +472,9 @@ const runClaudeCommand = Effect.fn("runClaudeCommand")(function* (
419472
420473export const checkClaudeProviderStatus = Effect . fn ( "checkClaudeProviderStatus" ) ( function * (
421474 claudeSettings : ClaudeSettings ,
422- resolveCapabilities ? : (
475+ resolveCapabilities : (
423476 claudeSettings : ClaudeSettings ,
424- ) => Effect . Effect < ClaudeCapabilitiesProbe | undefined > ,
477+ ) => Effect . Effect < ClaudeCapabilitiesProbe , ClaudeCapabilitiesProbeError > ,
425478 environment ?: NodeJS . ProcessEnv ,
426479 cwd ?: string ,
427480 modelCatalog : ClaudeModelCatalog = BUNDLED_CLAUDE_MODEL_CATALOG ,
@@ -533,31 +586,35 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
533586 ) ;
534587 const versionUpgradeMessage = formatClaudeVersionUpgradeMessage ( modelCatalog , parsedVersion ) ;
535588
536- const capabilities = resolveCapabilities
537- ? yield * resolveCapabilities ( claudeSettings ) . pipe ( Effect . orElseSucceed ( ( ) => undefined ) )
538- : undefined ;
589+ const probeResult = yield * resolveCapabilities ( claudeSettings ) . pipe ( Effect . result ) ;
539590 const skills = yield * discoverClaudeSkills ( claudeSettings , cwd , resolvedEnvironment ) ;
540- const slashCommands = [ COMPACT_SLASH_COMMAND , ...( capabilities ?. slashCommands ?? [ ] ) ] ;
541- const dedupedSlashCommands = dedupeSlashCommands ( slashCommands ) ;
542591
543- if ( ! capabilities ) {
592+ if ( Result . isFailure ( probeResult ) ) {
544593 return buildServerProvider ( {
545594 presentation : CLAUDE_PRESENTATION ,
546595 enabled : claudeSettings . enabled ,
547596 checkedAt,
548597 models,
549- slashCommands : dedupedSlashCommands ,
598+ slashCommands : [ COMPACT_SLASH_COMMAND ] ,
550599 skills,
551600 probe : {
552601 installed : true ,
553602 version : parsedVersion ,
554603 status : "warning" ,
555604 auth : { status : "unknown" } ,
556- message : "Could not verify Claude authentication status from initialization result." ,
605+ // A failed probe says nothing about the login, so name what failed.
606+ message : Cause . isTimeoutError ( probeResult . failure )
607+ ? `Timed out after ${ CAPABILITIES_PROBE_TIMEOUT_MS / 1_000 } s while checking Claude account status.`
608+ : "Claude Agent CLI failed while checking account status." ,
557609 } ,
558610 } ) ;
559611 }
560612
613+ const capabilities = probeResult . success ;
614+ const dedupedSlashCommands = dedupeSlashCommands ( [
615+ COMPACT_SLASH_COMMAND ,
616+ ...capabilities . slashCommands ,
617+ ] ) ;
561618 const authMetadata =
562619 claudeAuthMetadata ( {
563620 subscriptionType : capabilities . subscriptionType ,
0 commit comments