2026-10-10 17:03:43 +08:00
import { readClientPolicyBlock } from "./GeminiClientSettings.js" ;
import {
effectiveClientConfiguration ,
readClientConfigurationFiles ,
} from "./ClientConfigurationFiles.js" ;
2026-10-06 21:58:20 -07:00
import {
2026-10-09 08:51:12 -07:00
clientServerForcedEnabled ,
clientServerListedDisabled ,
2026-10-06 21:58:20 -07:00
effectiveClientServer ,
2026-10-13 12:46:05 +08:00
type ClientConfigurationDocument ,
2026-10-06 21:58:20 -07:00
} from "./ClientConfigurationDocument.js" ;
2026-10-10 17:03:43 +08:00
import { access } from "node:fs/promises" ;
2026-07-14 01:12:05 +00:00
import { resolve } from "node:path" ;
import { z } from "zod" ;
2026-10-14 08:25:53 +08:00
import { valid } from "semver" ;
2026-07-14 01:12:05 +00:00
import { PRODUCT_IDENTITY } from "../identity.js" ;
2026-07-26 15:02:58 +00:00
import { MCP_STARTUP_POLICY } from "../mcpStartupPolicy.js" ;
2026-10-08 22:11:54 +08:00
import {
2026-10-14 08:25:53 +08:00
clientRegistrationPackageReference ,
2026-10-08 22:11:54 +08:00
isOwnedClientRegistrationCommand ,
2026-10-11 07:17:15 -04:00
packageRunnerRegistrationCommand ,
2026-10-08 22:11:54 +08:00
} from "./ClientRegistrationIdentity.js" ;
2026-10-09 06:08:29 +08:00
import {
2026-10-10 17:03:43 +08:00
clientEvidencePaths ,
2026-10-09 06:08:29 +08:00
manualRegistrationRemediation ,
supportedClients ,
} from "./SupportedClients.js" ;
2026-10-05 23:06:19 +08:00
import type { SetupClient } from "./SupportedClients.js" ;
2026-10-11 10:10:01 -07:00
import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js" ;
2026-10-12 05:06:26 +02:00
import { dshDocument } from "./DshCordisPatch.js" ;
2026-10-12 05:56:26 +08:00
import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js" ;
2026-07-14 01:12:05 +00:00
2026-08-10 14:57:44 +08:00
interface ClientRegistrationStatusBase {
2026-07-14 01:12:05 +00:00
readonly client : string ;
readonly config_path : string ;
2026-10-14 08:25:53 +08:00
/** Exact package reference requested by npx/bunx, when registration uses one. */
readonly package_reference? : string ;
2026-07-14 01:12:05 +00:00
}
2026-08-10 14:57:44 +08:00
type RegistrationCommand = readonly [ string , ... string []];
export type ClientRegistrationStatus = ClientRegistrationStatusBase &
(
| {
readonly command : RegistrationCommand ;
readonly state : "aligned" ;
readonly remediation : null ;
}
| {
readonly command : RegistrationCommand ;
readonly state : "stale" ;
readonly remediation : string ;
}
| {
readonly command : readonly [];
2026-10-09 06:08:29 +08:00
readonly state : "missing" | "invalid" | "manual" ;
2026-08-10 14:57:44 +08:00
readonly remediation : string ;
}
);
export type UnhealthyClientRegistrationStatus = Exclude <
ClientRegistrationStatus ,
{ readonly state : "aligned" }
> ;
2026-07-14 01:12:05 +00:00
const registrationSchema = z
2026-07-20 17:11:06 +08:00
. object ({
command : z.string (). min ( 1 ),
args : z.array ( z . string ()). default ([]),
startup_timeout_sec : z.number (). positive (). optional (),
2026-10-13 23:36:54 +08:00
startupTimeoutMs : z.number (). positive (). optional (),
toolTimeoutMs : z.number (). positive (). optional (),
2026-10-05 23:06:19 +08:00
type : z . string (). optional (),
2026-10-06 13:15:59 +07:00
transport : z.string (). optional (),
2026-10-12 05:06:26 +02:00
serverName : z.string (). optional (),
2026-10-05 23:06:19 +08:00
tools : z.array ( z . string ()). optional (),
disabled : z.boolean (). optional (),
enabled : z.boolean (). optional (),
2026-07-20 17:11:06 +08:00
})
2026-07-14 01:12:05 +00:00
. passthrough ();
2026-10-13 12:46:05 +08:00
interface ClientRegistrationReadOptions {
readonly platform? : NodeJS.Platform ;
readonly environment? : NodeJS.ProcessEnv ;
readonly resources? : ArtifactResourceScope ;
}
/** Registration facts shared by diagnostics and update maintenance. */
export interface ClientRegistrationInspection {
readonly status : ClientRegistrationStatus ;
readonly activation : "enabled" | "disabled" | "unknown" ;
}
/** Project diagnostics from the same inspection used to select maintenance. */
2026-07-14 01:12:05 +00:00
export const readClientRegistrationStatuses = async (
home : string ,
currentCommandPath : string = resolve ( process . argv [ 1 ] ?? "unknown" ),
2026-10-13 12:46:05 +08:00
options : ClientRegistrationReadOptions = {},
) : Promise < readonly ClientRegistrationStatus [] > =>
(
await readClientRegistrationInspections ( home , currentCommandPath , options )
). map (({ status }) => status );
/** Parse effective registrations and their client-specific activation once. */
export const readClientRegistrationInspections = async (
home : string ,
currentCommandPath : string = resolve ( process . argv [ 1 ] ?? "unknown" ),
options : ClientRegistrationReadOptions = {},
) : Promise < readonly ClientRegistrationInspection [] > => {
const inspections : ClientRegistrationInspection [] = [];
const record = (
status : ClientRegistrationStatus ,
activation : ClientRegistrationInspection [ "activation" ] = "unknown" ,
) : void => {
inspections . push ({ status , activation });
};
2026-10-06 00:50:38 +08:00
for ( const client of supportedClients (
home ,
options . platform ,
2026-10-10 01:03:25 -07:00
options . environment ,
2026-10-06 00:50:38 +08:00
)) {
2026-10-10 01:10:39 -07:00
if ( client . configPathError !== undefined ) {
2026-10-13 12:46:05 +08:00
record ({
2026-10-10 01:10:39 -07:00
client : client.name ,
config_path : client.configPath ,
command : [],
state : "invalid" ,
remediation : client.configPathError ,
});
continue ;
}
2026-10-10 17:03:43 +08:00
if ( ! ( await clientEvidencePresent ( client ))) continue ;
2026-10-09 06:08:29 +08:00
const manualRemediation = manualRegistrationRemediation ( client . name );
if ( client . format === "unsupported" ) {
if ( manualRemediation !== undefined )
2026-10-13 12:46:05 +08:00
record ({
2026-10-09 06:08:29 +08:00
client : client.name ,
config_path : client.markerPath ?? client . configPath ,
command : [],
state : "manual" ,
remediation : manualRemediation ,
});
continue ;
}
2026-07-14 01:12:05 +00:00
try {
2026-10-12 05:56:26 +08:00
const files = await readClientConfigurationFiles (
client ,
options . resources ,
);
2026-10-10 17:03:43 +08:00
if ( ! files . ok ) {
2026-10-13 12:46:05 +08:00
record ({
2026-10-10 17:03:43 +08:00
client : client.name ,
config_path : client.configPath ,
command : [],
state : "invalid" ,
remediation : files.error.detail ,
});
continue ;
}
const parsed = effectiveClientConfiguration ( files . value );
if ( parsed === undefined ) {
2026-10-13 11:04:22 -03:00
record ( unavailableStatus ( client , "missing" ));
2026-10-10 17:03:43 +08:00
continue ;
}
2026-10-12 05:56:26 +08:00
const policy = await readClientPolicyBlock (
client ,
parsed ,
undefined ,
options . resources ,
);
2026-10-10 17:03:43 +08:00
if ( ! policy . ok ) {
2026-10-13 12:46:05 +08:00
record ({
2026-10-10 17:03:43 +08:00
client : client.name ,
config_path : client.configPath ,
command : [],
state : "invalid" ,
remediation : policy.error.detail ,
});
continue ;
}
const policyBlock = policy . value ;
2026-10-09 06:08:29 +08:00
const raw = effectiveClientServer ( parsed , PRODUCT_IDENTITY . mcpServerKey );
2026-07-14 01:12:05 +00:00
if ( raw === undefined ) {
2026-10-12 11:58:49 +08:00
const editabilityError =
parsed . dialect === "dsh"
? dshDocument ( parsed . document ). editabilityError
: undefined ;
if ( editabilityError !== undefined ) {
2026-10-13 12:46:05 +08:00
record ({
2026-10-12 11:58:49 +08:00
client : client.name ,
config_path : client.configPath ,
command : [],
state : "invalid" ,
remediation : editabilityError.message ,
});
continue ;
}
2026-10-13 11:04:22 -03:00
record ( unavailableStatus ( client , "missing" ));
2026-07-14 01:12:05 +00:00
continue ;
}
2026-10-05 23:06:19 +08:00
const registration = parseRegistration ( raw , client );
2026-08-10 14:57:44 +08:00
const command : RegistrationCommand = [
registration . command ,
... registration . args ,
];
2026-10-14 08:25:53 +08:00
const packageReference = clientRegistrationPackageReference ( command );
2026-10-13 12:46:05 +08:00
const activation = registrationActivation ( registration , client , parsed );
2026-10-14 08:25:53 +08:00
const packageVersion = exactRegisteredPackageVersion ( packageReference );
const packageVersionDrift =
packageVersion !== undefined &&
packageVersion !== PRODUCT_IDENTITY . packageVersion ;
2026-10-13 12:46:05 +08:00
record (
2026-08-10 14:57:44 +08:00
configuredStatus (
2026-10-13 11:04:22 -03:00
client ,
2026-07-14 01:12:05 +00:00
command ,
2026-10-13 12:46:05 +08:00
activation === "enabled" &&
2026-10-14 08:25:53 +08:00
! packageVersionDrift &&
2026-10-13 12:46:05 +08:00
registrationAligned (
registration ,
client ,
currentCommandPath ,
options . platform ?? process . platform ,
2026-10-10 17:03:43 +08:00
) &&
2026-10-12 05:06:26 +02:00
( parsed . dialect !== "dsh" || dshDocument ( parsed . document ). ready ) &&
2026-10-10 17:03:43 +08:00
policyBlock === undefined
2026-07-14 01:12:05 +00:00
? "aligned"
: "stale" ,
2026-10-14 08:25:53 +08:00
policyBlock ??
( packageVersionDrift
? packageVersionRemediation ( packageReference ?? "" , client )
: undefined ),
packageReference ,
2026-07-14 01:12:05 +00:00
),
2026-10-13 12:46:05 +08:00
activation ,
2026-07-14 01:12:05 +00:00
);
} catch ( cause : unknown ) {
2026-10-13 12:46:05 +08:00
record (
2026-10-13 11:04:22 -03:00
unavailableStatus ( client , isMissing ( cause ) ? "missing" : "invalid" ),
2026-07-14 01:12:05 +00:00
);
}
}
2026-10-13 12:46:05 +08:00
return inspections . sort (( left , right ) =>
compareUnicodeCodePoints ( left . status . client , right . status . client ),
2026-07-14 01:12:05 +00:00
);
};
2026-10-13 12:46:05 +08:00
const registrationActivation = (
registration : z.output < typeof registrationSchema >,
client : SetupClient ,
parsed : ClientConfigurationDocument ,
) : "enabled" | "disabled" =>
( client . format !== "grok" && registration . disabled === true ) ||
( registration . enabled === false &&
! clientServerForcedEnabled ( parsed , PRODUCT_IDENTITY . mcpServerKey )) ||
( client . format === "zcode" && registration . enable === false ) ||
clientServerListedDisabled ( parsed , PRODUCT_IDENTITY . mcpServerKey )
? "disabled"
: "enabled" ;
2026-07-14 01:12:05 +00:00
const registrationAligned = (
2026-07-20 17:11:06 +08:00
registration : z.output < typeof registrationSchema >,
2026-10-05 23:06:19 +08:00
client : SetupClient ,
2026-07-14 01:12:05 +00:00
currentCommandPath : string ,
2026-10-08 22:11:54 +08:00
platform : NodeJS.Platform ,
2026-07-14 01:12:05 +00:00
) : boolean => {
2026-07-20 17:11:06 +08:00
const command = [ registration . command , ... registration . args ];
2026-10-05 23:06:19 +08:00
if ( ! isOwnedClientRegistrationCommand ( command , currentCommandPath ))
return false ;
if (
2026-10-09 06:08:29 +08:00
( client . name === "codex" || client . name === "grok_build" ) &&
2026-07-26 15:02:58 +00:00
registration . startup_timeout_sec !==
MCP_STARTUP_POLICY . codexStartupTimeoutSeconds
)
2026-07-20 17:11:06 +08:00
return false ;
2026-10-12 05:06:26 +02:00
if (
2026-10-13 23:36:54 +08:00
client . name === "kimi" &&
( registration . startupTimeoutMs !==
MCP_STARTUP_POLICY . kimiStartupTimeoutMs ||
registration . toolTimeoutMs !== MCP_STARTUP_POLICY . kimiToolTimeoutMs )
2026-10-12 05:06:26 +02:00
)
return false ;
2026-10-09 08:51:12 -07:00
if (
2026-10-13 23:36:54 +08:00
( client . format === "kimi" ||
client . format === "omp" ||
client . format === "pi" ) &&
2026-10-09 08:51:12 -07:00
registration . type !== undefined &&
registration . type !== "stdio"
)
return false ;
2026-10-13 23:36:54 +08:00
if ( client . format === "vscode" && registration . type !== "stdio" ) return false ;
// A Harness registration only connects as the rea stdio server; another
// server name or transport leaves the session without REA tools.
if (
client . format === "dsh" &&
( registration . serverName !== "rea" || registration . transport !== "stdio" )
)
return false ;
2026-10-05 23:06:19 +08:00
if (
client . format === "copilot_cli" &&
( registration . type !== "stdio" ||
JSON . stringify ( registration . tools ) !== JSON . stringify ([ "*" ]))
)
return false ;
2026-10-06 13:15:59 +07:00
if ( client . format === "commandcode" && registration . transport !== "stdio" )
return false ;
2026-10-07 10:33:10 +08:00
if (
command . length === 3 &&
command [ 2 ] === "mcp" &&
resolve ( command [ 0 ] ?? "" ) === resolve ( process . execPath ) &&
resolve ( command [ 1 ] ?? "" ) === currentCommandPath
)
return true ;
2026-07-14 01:12:05 +00:00
if (
2026-10-11 07:17:15 -04:00
([ "npx" , "bunx" ] as const ). some (( runner ) => {
const runnerCommand = packageRunnerRegistrationCommand ( runner , platform );
return (
command . length === runnerCommand . length &&
command . every (( argument , index ) => argument === runnerCommand [ index ])
);
})
2026-07-14 01:12:05 +00:00
)
return true ;
return (
command . length === 2 &&
command [ 1 ] === "mcp" &&
resolve ( command [ 0 ] ?? "" ) === currentCommandPath
);
};
2026-10-05 23:06:19 +08:00
const parseRegistration = (
value : unknown ,
client : SetupClient ,
) : z . output < typeof registrationSchema > => {
2026-10-12 05:06:26 +02:00
if ( client . format === "dsh" )
return registrationSchema
. pick ({ command : true , args : true , serverName : true , transport : true })
. strip ()
. parse ( value );
2026-10-05 23:06:19 +08:00
if ( client . format === "opencode" ) {
const entry = z
. object ({
type : z . literal ( "local" ),
command : z.array ( z . string ()). min ( 1 ),
environment : z.record ( z . string (), z . string ()). optional (),
})
. passthrough ()
. parse ( value );
const [ command = "" , ... args ] = entry . command ;
return registrationSchema . parse ({
... entry ,
command ,
args ,
});
}
const registration = registrationSchema . parse ( value );
2026-10-13 23:36:54 +08:00
if (
client . format === "kimi" &&
registration . transport !== undefined &&
registration . transport !== "stdio"
)
throw new TypeError ( "Expected an stdio registration" );
2026-10-05 23:06:19 +08:00
if (
( client . format === "vscode" || client . format === "copilot_cli" ) &&
registration . type !== "stdio"
)
throw new TypeError ( "Expected an stdio registration" );
2026-10-06 13:15:59 +07:00
if ( client . format === "commandcode" && registration . transport !== "stdio" )
throw new TypeError ( "Expected an stdio registration" );
2026-10-09 08:51:12 -07:00
if (
2026-10-13 23:36:54 +08:00
( client . format === "kimi" ||
client . format === "omp" ||
client . format === "pi" ) &&
2026-10-09 08:51:12 -07:00
registration . type !== undefined &&
registration . type !== "stdio"
)
throw new TypeError ( "Expected an stdio registration" );
2026-10-05 23:06:19 +08:00
return registration ;
};
2026-10-13 11:04:22 -03:00
/** A client that rewrites its file from memory must be quit before setup. */
const remediation = ( client : SetupClient ) : string => {
if ( client . quitBeforeSetup !== true )
return "Run rea setup to refresh this registration, then restart the client." ;
const name = client . displayName ?? client . name ;
return `Quit ${ name } fully, run rea setup to refresh this registration, then start ${ name } again. A running ${ name } can rewrite its configuration from memory and drop the registration.` ;
};
2026-08-10 14:57:44 +08:00
const configuredStatus = (
2026-10-13 11:04:22 -03:00
client : SetupClient ,
2026-08-10 14:57:44 +08:00
command : RegistrationCommand ,
state : "aligned" | "stale" ,
2026-10-10 17:03:43 +08:00
policyRemediation? : string ,
2026-10-14 08:25:53 +08:00
packageReference? : string ,
2026-08-10 14:57:44 +08:00
) : ClientRegistrationStatus =>
state === "aligned"
2026-10-13 11:04:22 -03:00
? {
client : client.name ,
config_path : client.configPath ,
command ,
2026-10-14 08:25:53 +08:00
...( packageReference === undefined
? {}
: { package_reference : packageReference }),
2026-10-13 11:04:22 -03:00
state ,
remediation : null ,
}
2026-10-10 17:03:43 +08:00
: {
2026-10-13 11:04:22 -03:00
client : client.name ,
config_path : client.configPath ,
2026-10-10 17:03:43 +08:00
command ,
2026-10-14 08:25:53 +08:00
...( packageReference === undefined
? {}
: { package_reference : packageReference }),
2026-10-10 17:03:43 +08:00
state ,
2026-10-13 11:04:22 -03:00
remediation : policyRemediation ?? remediation ( client ),
2026-10-10 17:03:43 +08:00
};
2026-08-10 14:57:44 +08:00
2026-10-14 08:25:53 +08:00
const exactRegisteredPackageVersion = (
packageReference : string | undefined ,
) : string | undefined => {
if ( packageReference === undefined ) return undefined ;
const prefix = ` ${ PRODUCT_IDENTITY . packageName } @` ;
if ( ! packageReference . startsWith ( prefix )) return undefined ;
const version = packageReference . slice ( prefix . length );
return valid ( version ) === version ? version : undefined ;
};
const packageVersionRemediation = (
packageReference : string ,
client : SetupClient ,
) : string =>
`This registration requests ${ packageReference } , while the current REA CLI package is ${ PRODUCT_IDENTITY . packageVersion } . REA does not resolve package-runner requests during doctor; update the registration with rea setup --client ${ client . name } , then restart the client.` ;
2026-08-10 14:57:44 +08:00
const unavailableStatus = (
2026-10-13 11:04:22 -03:00
client : SetupClient ,
2026-08-10 14:57:44 +08:00
state : "missing" | "invalid" ,
2026-07-14 01:12:05 +00:00
) : ClientRegistrationStatus => ({
2026-10-13 11:04:22 -03:00
client : client.name ,
config_path : client.configPath ,
2026-08-10 14:57:44 +08:00
command : [],
2026-07-14 01:12:05 +00:00
state ,
2026-10-13 11:04:22 -03:00
remediation : remediation ( client ),
2026-07-14 01:12:05 +00:00
});
2026-10-10 17:03:43 +08:00
const clientEvidencePresent = async ( client : SetupClient ) : Promise < boolean > => {
for ( const path of clientEvidencePaths ( client ))
if ( await exists ( path )) return true ;
return false ;
};
2026-07-14 01:12:05 +00:00
const exists = async ( path : string | undefined ) : Promise < boolean > => {
if ( path === undefined ) return false ;
try {
await access ( path );
return true ;
2026-10-06 05:58:46 +08:00
} catch ( cause : unknown ) {
// best-effort cleanup: optional registration probing; absence means unregistered.
void cause ;
2026-07-14 01:12:05 +00:00
return false ;
}
};
const isMissing = ( cause : unknown ) : boolean =>
cause instanceof Error && "code" in cause && cause . code === "ENOENT" ;