2026-07-12 06:34:27 +08:00
import { constants } from "node:fs" ;
2026-10-10 09:31:26 -04:00
import { access , readdir , stat } from "node:fs/promises" ;
2026-10-12 05:56:26 +08:00
import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js" ;
2026-10-09 20:02:52 +08:00
import { homeDirectoryFromEnvironment } from "../config/homeDirectory.js" ;
import { join } from "node:path" ;
2026-07-12 06:34:27 +08:00
2026-10-06 19:53:08 +08:00
import { analysisErrorRemediationAction } from "../domain/analysisErrorPresentation.js" ;
2026-10-10 00:16:04 +08:00
import { BinaryTargetError } from "../domain/configurationErrors.js" ;
2026-09-08 10:59:13 +08:00
import { parseBinaryTarget } from "./BinaryTargetResolver.js" ;
2026-09-29 17:36:04 +08:00
import { execFileOutput } from "../process/ExecFileOutput.js" ;
2026-07-22 20:38:26 +08:00
import type { JsonValue } from "../domain/jsonValue.js" ;
2026-10-11 07:20:00 -04:00
import {
javaScriptRuntime ,
type JavaScriptRuntime ,
} from "../domain/runtimeVersion.js" ;
2026-10-09 03:28:25 +00:00
import { installedCaskAppdir , probeHomebrew } from "./homebrew.js" ;
2026-07-12 23:42:20 +00:00
import {
2026-07-13 20:50:01 +00:00
linuxHopperBinarySupported ,
2026-07-12 23:42:20 +00:00
linuxSharedLibrariesAvailable ,
readLinuxDistribution ,
type LinuxDistribution ,
} from "./LinuxHopper.js" ;
2026-10-10 01:59:12 +08:00
import {
readInstalledSkillIdentity ,
type InstalledSkillIdentity ,
2026-10-14 16:00:00 +08:00
type SkillPathIssue ,
2026-10-10 01:59:12 +08:00
} from "./SetupSkill.js" ;
2026-07-14 01:12:05 +00:00
import { CATALOG_IDENTITY } from "../catalogIdentity.js" ;
import { PRODUCT_IDENTITY , SDK_IDENTITY } from "../identity.js" ;
import {
readClientRegistrationStatuses ,
type ClientRegistrationStatus ,
2026-08-10 14:57:44 +08:00
type UnhealthyClientRegistrationStatus ,
2026-07-14 01:12:05 +00:00
} from "./ClientRegistrationStatus.js" ;
2026-07-18 00:27:04 +00:00
import {
collectDoctorDiagnostics ,
doctorHealthy ,
} from "./DoctorDiagnostics.js" ;
2026-07-22 20:38:26 +08:00
import {
normalizeDoctorScope ,
scopeDoctorChecks ,
type DoctorScopeReport ,
} from "./DoctorScope.js" ;
2026-07-12 06:53:31 +08:00
2026-07-12 06:34:27 +08:00
/** One actionable environment diagnostic. */
export interface DoctorCheck {
readonly name : string ;
readonly ok : boolean ;
2026-07-12 23:42:20 +00:00
readonly classification :
| "healthy"
| "missing_dependency"
2026-10-14 08:25:53 +08:00
| "runtime_failure"
2026-07-12 23:42:20 +00:00
| "unsupported_host"
2026-10-06 19:53:08 +08:00
| "unsupported_target"
2026-07-12 23:42:20 +00:00
| "missing_analysis_engine"
| "config_drift" ;
2026-07-12 06:34:27 +08:00
readonly detail? : string ;
2026-07-22 20:38:26 +08:00
readonly details? : Readonly < Record < string , JsonValue >>;
2026-07-12 06:34:27 +08:00
readonly remediation? : string ;
}
2026-07-16 00:02:49 +08:00
/** One provider-owned check projected without importing its adapter types. */
export interface DoctorProviderCheck {
readonly name : string ;
readonly ok : boolean ;
readonly code : string | null ;
readonly detail : string ;
readonly remediation : string | null ;
readonly classification : Exclude < DoctorCheck [ "classification" ] , "healthy" >;
2026-10-14 08:25:53 +08:00
readonly details? : Readonly < Record < string , JsonValue >>;
2026-07-16 00:02:49 +08:00
}
/** Read-only provider inspection supplied by an outer composition adapter. */
export interface DoctorProviderInspection {
readonly id : string ;
readonly configured : boolean ;
readonly available : boolean ;
readonly providerVersion : string | null ;
2026-10-06 16:53:33 +08:00
/** Adapter-validated non-secret settings, independent of runtime readiness.
* Invalid installations publish no settings. */
2026-07-16 00:02:49 +08:00
readonly registrationEnvironment : Readonly < Record < string , string >>;
readonly checks : readonly DoctorProviderCheck [];
}
2026-07-12 08:45:35 +08:00
/** Read-only host capabilities required by diagnostics. */
2026-07-12 06:34:27 +08:00
export interface DoctorHost {
readonly platform : NodeJS.Platform ;
2026-07-17 01:15:31 +08:00
readonly architecture : NodeJS.Architecture ;
2026-10-11 07:20:00 -04:00
readonly runtime : JavaScriptRuntime ;
2026-10-09 20:02:52 +08:00
readonly homeDirectory : string ;
2026-07-12 06:34:27 +08:00
readonly configuredHopperPath? : string ;
2026-07-16 22:51:25 +08:00
readonly configuredIlspyCmdPath? : string ;
2026-07-12 06:34:27 +08:00
macosVersion () : Promise < string | undefined >;
2026-07-12 23:42:20 +00:00
linuxDistribution () : Promise < LinuxDistribution | undefined >;
2026-07-12 12:26:16 +08:00
validTarget ( path : string ) : Promise < boolean >;
2026-10-06 19:53:08 +08:00
/** Route-specific target admission details, when supplied by the host. */
inspectTarget ? ( path : string ) : Promise < DoctorCheck >;
2026-07-12 08:55:36 +08:00
executable ( path : string ) : Promise < boolean >;
2026-07-13 20:50:01 +00:00
supportedLinuxHopper ( path : string ) : Promise < boolean >;
2026-07-22 20:38:26 +08:00
linuxDemoRuntimeCheck () : Promise < DoctorCheck >;
2026-07-12 06:34:27 +08:00
brewHopperPath () : Promise < string | undefined >;
manualHopperPaths () : Promise < readonly string [] >;
2026-07-16 00:02:49 +08:00
providerInspections ? () : Promise < readonly DoctorProviderInspection [] >;
2026-10-14 10:06:46 +08:00
/** Validated provider settings preserved by setup, independent of readiness. */
providerRegistrationEnvironment ? () : Readonly < Record < string , string >>;
2026-07-14 01:12:05 +00:00
installationPaths ? () : Promise < readonly string [] >;
2026-10-10 01:59:12 +08:00
installedSkillIdentity ? (
clientIds : readonly string [],
) : Promise < InstalledSkillIdentity | undefined >;
2026-10-12 05:56:26 +08:00
close ? () : Promise < string | undefined >;
2026-07-14 01:12:05 +00:00
clientRegistrations ? () : Promise < readonly ClientRegistrationStatus [] >;
2026-07-16 22:51:25 +08:00
ilspyCmdVersion ? ( path : string ) : Promise < string | undefined >;
2026-07-12 06:34:27 +08:00
}
2026-07-18 00:27:04 +00:00
/** Structured result returned by the read-only doctor workflow. */
export interface DoctorReport {
readonly healthy : boolean ;
2026-07-22 20:38:26 +08:00
readonly environment_healthy : boolean ;
readonly scope : DoctorScopeReport ;
readonly scope_checks : readonly DoctorCheck [];
readonly informational_checks : readonly DoctorCheck [];
2026-07-18 00:27:04 +00:00
readonly hopperPath? : string ;
readonly providerInspections? : readonly DoctorProviderInspection [];
readonly checks : readonly DoctorCheck [];
readonly identity? : DoctorIdentity ;
}
2026-07-22 20:38:26 +08:00
/** Caller-selected readiness boundary; omitted values retain audit semantics. */
export interface DoctorScope {
readonly clients? : readonly string [];
2026-10-11 23:52:42 +08:00
readonly skillClients? : readonly string [];
2026-07-22 20:38:26 +08:00
readonly providers? : readonly string [];
readonly skill? : boolean ;
}
2026-07-18 00:27:04 +00:00
/** Installed-package, skill, client, and runtime identity observations. */
2026-10-14 16:00:00 +08:00
export type DoctorSkillState =
| "aligned"
| "stale"
| "missing"
| "unmanaged"
| "unknown" ;
2026-07-18 01:46:40 +00:00
interface DoctorIdentity {
2026-07-18 00:27:04 +00:00
readonly cli_package_version : string ;
readonly expected_skill_version : string ;
readonly sdk : typeof SDK_IDENTITY ;
readonly catalog : typeof CATALOG_IDENTITY ;
readonly live_server : {
readonly state : "unknown" ;
readonly remediation : string ;
};
readonly installations : {
readonly paths : readonly string [];
readonly state : "single" | "multiple" | "unknown" ;
};
readonly skill : {
readonly installed_version : string | null ;
readonly installed_tool_count : number | null ;
2026-10-14 16:00:00 +08:00
readonly state : DoctorSkillState ;
readonly issues? : readonly SkillPathIssue [];
2026-07-18 00:27:04 +00:00
readonly remediation : string | null ;
};
readonly registrations : readonly ClientRegistrationStatus [];
}
2026-07-12 08:45:35 +08:00
/**
* Check requirements and an optional target without mutating the host.
* Every failed check includes remediation suitable for either the one-shot CLI
* or an agent consuming the structured result.
*/
2026-07-12 06:34:27 +08:00
export const runDoctor = async (
target? : string ,
2026-10-12 05:56:26 +08:00
selectedHost? : DoctorHost ,
requestedScope? : DoctorScope ,
) : Promise < DoctorReport > =>
selectedHost === undefined
? runDoctorOwned ( target , systemDoctorHost (), requestedScope )
: inspectDoctor ( target , selectedHost , requestedScope );
/** Inspect with a host whose lifetime belongs to this operation. */
export const runDoctorOwned = async (
target : string | undefined ,
host : DoctorHost ,
requestedScope? : DoctorScope ,
) : Promise < DoctorReport > => {
let report : DoctorReport ;
try {
report = await inspectDoctor ( target , host , requestedScope );
} catch ( cause : unknown ) {
const cleanup = await closeDoctorHost ( host );
if ( cleanup !== undefined )
throw new AggregateError (
[ cause , new Error ( cleanup )],
"Doctor failed and its resource cleanup remains incomplete." ,
{ cause },
);
throw cause ;
}
const cleanup = await closeDoctorHost ( host );
return cleanup === undefined
? report
: doctorReportWithCleanup ( report , cleanup );
};
const inspectDoctor = async (
target : string | undefined ,
host : DoctorHost ,
2026-07-22 20:38:26 +08:00
requestedScope? : DoctorScope ,
2026-07-18 00:27:04 +00:00
) : Promise < DoctorReport > => {
2026-10-10 01:59:12 +08:00
const scope = normalizeDoctorScope ( requestedScope , target );
2026-07-18 00:27:04 +00:00
const {
checks : diagnosticChecks ,
hopperPath ,
providerInspections ,
} = await collectDoctorDiagnostics ( target , host );
2026-10-10 01:59:12 +08:00
const identity = await collectDoctorIdentity ( host , scope );
2026-07-18 00:27:04 +00:00
const checks = [... diagnosticChecks , ... identity . checks ];
2026-07-22 20:38:26 +08:00
const providers = {
hopperAvailable :
hopperPath !== undefined &&
checks
. filter (({ name }) => name . startsWith ( "hopper" ))
. every (({ ok }) => ok ),
hopperConfigured : host.configuredHopperPath !== undefined ,
providerInspections ,
};
const environmentHealthy = doctorHealthy ( checks , providers );
const scoped = scopeDoctorChecks ({
checks ,
registrations : identity.value.registrations ,
skillState : identity.value.skill.state ,
providers ,
scope ,
});
2026-07-12 06:34:27 +08:00
return {
2026-07-22 20:38:26 +08:00
healthy : scope.mode === "audit-wide" ? environmentHealthy : scoped.healthy ,
environment_healthy : environmentHealthy ,
scope ,
2026-10-05 21:15:04 +08:00
scope_checks : scope.mode === "audit-wide" ? checks : scoped.scopeChecks ,
2026-07-22 20:38:26 +08:00
informational_checks :
2026-10-05 21:15:04 +08:00
scope.mode === "audit-wide" ? [] : scoped . informationalChecks ,
2026-07-12 06:34:27 +08:00
...( hopperPath === undefined ? {} : { hopperPath }),
2026-07-16 00:02:49 +08:00
...( providerInspections === undefined ? {} : { providerInspections }),
2026-07-12 06:34:27 +08:00
checks ,
2026-07-18 00:27:04 +00:00
identity : identity.value ,
};
};
2026-10-12 05:56:26 +08:00
const closeDoctorHost = async (
host : DoctorHost ,
) : Promise < string | undefined > => {
try {
return await host . close ? .();
} catch ( cause : unknown ) {
return `Doctor host cleanup failed: ${ cause instanceof Error ? cause.message : String ( cause ) } ` ;
}
};
const doctorReportWithCleanup = (
report : DoctorReport ,
detail : string ,
) : DoctorReport => {
const check : DoctorCheck = {
name : "doctor-resource-cleanup" ,
ok : false ,
classification : "config_drift" ,
detail ,
remediation : "Resolve the reported cleanup failure, then rerun rea doctor." ,
};
return {
... report ,
healthy : false ,
environment_healthy : false ,
checks : [... report . checks , check ],
scope_checks : [... report . scope_checks , check ],
};
};
2026-07-18 00:27:04 +00:00
const collectDoctorIdentity = async (
host : DoctorHost ,
2026-10-10 01:59:12 +08:00
scope : DoctorScopeReport ,
2026-07-18 00:27:04 +00:00
) : Promise < {
readonly checks : readonly DoctorCheck [];
readonly value : DoctorIdentity ;
} > => {
const installationPaths = ( await host . installationPaths ? .()) ?? [];
const registrations = ( await host . clientRegistrations ? .()) ?? [];
2026-10-10 01:59:12 +08:00
const skillClientIds =
2026-10-11 23:52:42 +08:00
scope . skill_clients ??
( scope . mode === "audit-wide"
2026-10-10 01:59:12 +08:00
? registrations . map (({ client }) => client )
2026-10-11 23:52:42 +08:00
: scope . clients );
2026-10-10 01:59:12 +08:00
const installedSkillIdentity =
await host . installedSkillIdentity ? .( skillClientIds );
const skillAligned = skillIdentityAligned ( installedSkillIdentity );
2026-07-18 00:27:04 +00:00
return {
checks : [
...( skillAligned ? [] : [ skillIdentityCheck ( installedSkillIdentity )]),
... registrations
2026-08-10 14:57:44 +08:00
. filter (
( registration ) : registration is UnhealthyClientRegistrationStatus =>
2026-10-09 06:08:29 +08:00
registration . state !== "aligned" && registration . state !== "manual" ,
2026-08-10 14:57:44 +08:00
)
2026-07-18 00:27:04 +00:00
. map ( registrationCheck ),
],
value : {
2026-07-14 01:12:05 +00:00
cli_package_version : PRODUCT_IDENTITY.packageVersion ,
expected_skill_version : PRODUCT_IDENTITY.skillVersion ,
sdk : SDK_IDENTITY ,
catalog : CATALOG_IDENTITY ,
live_server : {
state : "unknown" ,
remediation :
2026-09-29 07:08:46 +08:00
"Call binary_session in the active client to inspect the running server identity." ,
2026-07-14 01:12:05 +00:00
},
installations : {
paths : installationPaths ,
2026-07-18 00:27:04 +00:00
state : installationState ( installationPaths ),
2026-07-14 01:12:05 +00:00
},
skill : {
2026-07-18 00:27:04 +00:00
installed_version : installedSkillIdentity?.version ?? null ,
2026-07-16 18:51:05 +08:00
installed_tool_count : installedSkillIdentity?.toolCount ?? null ,
2026-10-14 16:00:00 +08:00
state : skillIdentityState ( installedSkillIdentity ),
...( installedSkillIdentity ? . issues === undefined
? {}
: { issues : installedSkillIdentity.issues }),
2026-07-16 18:51:05 +08:00
remediation : skillAligned
? null
2026-10-14 16:00:00 +08:00
: skillIdentityRemediation ( installedSkillIdentity ),
2026-07-14 01:12:05 +00:00
},
registrations ,
},
2026-07-12 06:34:27 +08:00
};
};
2026-07-18 00:27:04 +00:00
const skillIdentityAligned = (
identity : InstalledSkillIdentity | undefined ,
) : boolean =>
identity ? . version === PRODUCT_IDENTITY . skillVersion &&
identity . toolCount === CATALOG_IDENTITY . counts . mcp_tools &&
2026-10-08 22:21:30 +08:00
identity . canonical ;
2026-07-18 00:27:04 +00:00
2026-10-14 16:00:00 +08:00
const skillIdentityState = (
identity : InstalledSkillIdentity | undefined ,
) : DoctorSkillState => {
if ( identity ? . issues ? . some (({ kind }) => kind === "unreadable" ))
return "unknown" ;
if (( identity ? . issues ? . length ?? 0 ) > 0 ) return "unmanaged" ;
return identity === undefined
? "missing"
: skillIdentityAligned ( identity )
? "aligned"
: "stale" ;
};
const skillIdentityRemediation = (
identity : InstalledSkillIdentity | undefined ,
) : string => {
const issues = identity ? . issues ;
if ( issues !== undefined && issues . length > 0 )
return ` ${ issues . map (({ path , detail } ) => ` $ { detail } ( $ { path }) `).join(" ")} Verify the reported paths and use regular skill directories, or run rea setup with --skill=false to configure the client without installing the skill.` ;
return "Run rea setup to update the installed REA skill." ;
};
2026-07-18 00:27:04 +00:00
const skillIdentityCheck = (
identity : InstalledSkillIdentity | undefined ,
2026-10-14 16:00:00 +08:00
) : DoctorCheck => {
const issues = identity ? . issues ;
return {
name : "skill:identity" ,
ok : false ,
classification : "config_drift" ,
detail :
issues !== undefined && issues . length > 0
? `Selected skill identity is ${ skillIdentityState ( identity ) } : ${ issues . map (({ path } ) => path).join(", ")}.`
: identity === undefined
? "Installed REA skill identity is missing."
: "Installed REA skill identity is stale." ,
remediation : skillIdentityRemediation ( identity ),
};
};
2026-07-18 00:27:04 +00:00
const registrationCheck = (
2026-08-10 14:57:44 +08:00
registration : UnhealthyClientRegistrationStatus ,
2026-07-18 00:27:04 +00:00
) : DoctorCheck => ({
name : `registration: ${ registration . client } ` ,
ok : false ,
classification : "config_drift" ,
detail : registration.config_path ,
2026-08-10 14:57:44 +08:00
remediation : registration.remediation ,
2026-07-18 00:27:04 +00:00
});
const installationState = (
paths : readonly string [],
) : DoctorIdentity [ "installations" ][ "state" ] =>
paths . length === 0 ? "unknown" : paths . length === 1 ? "single" : "multiple" ;
2026-07-13 21:08:33 +00:00
2026-07-16 00:02:49 +08:00
/** Optional outer-adapter diagnostics composed without reversing dependencies. */
export interface SystemDoctorHostOptions {
2026-10-06 00:50:38 +08:00
readonly platform? : NodeJS.Platform ;
readonly architecture? : NodeJS.Architecture ;
readonly environment? : NodeJS.ProcessEnv ;
readonly execFileOutput? : typeof execFileOutput ;
2026-07-16 00:02:49 +08:00
readonly providerInspections ?: () => Promise <
readonly DoctorProviderInspection []
> ;
2026-10-14 10:06:46 +08:00
readonly providerRegistrationEnvironment ?: () => Readonly <
Record < string , string >
> ;
2026-07-22 20:38:26 +08:00
readonly linuxDemoRuntimeCheck ?: () => Promise < DoctorCheck >;
2026-07-16 00:02:49 +08:00
}
2026-07-12 06:34:27 +08:00
/** Create diagnostics backed by the current process and host commands. */
2026-07-16 00:02:49 +08:00
export const systemDoctorHost = (
options : SystemDoctorHostOptions = {},
2026-10-06 00:50:38 +08:00
) : DoctorHost => {
2026-10-12 05:56:26 +08:00
const resources = new ArtifactResourceScope ();
2026-10-06 00:50:38 +08:00
const platform = options . platform ?? process . platform ;
const architecture = options . architecture ?? process . arch ;
const environment = options . environment ?? process . env ;
const hostExecFileOutput = options . execFileOutput ?? execFileOutput ;
2026-10-09 20:02:52 +08:00
const homeDirectory = homeDirectoryFromEnvironment ( environment , platform );
2026-10-06 00:50:38 +08:00
const commandEnvironment = { env : environment };
return {
platform ,
architecture ,
2026-10-11 07:20:00 -04:00
runtime : javaScriptRuntime ( process . versions ),
2026-10-06 00:50:38 +08:00
homeDirectory ,
...( environment . HOPPER_LAUNCHER_PATH === undefined
? {}
: { configuredHopperPath : environment.HOPPER_LAUNCHER_PATH }),
...( environment . REA_ILSPY_CMD_PATH === undefined
? {}
: { configuredIlspyCmdPath : environment.REA_ILSPY_CMD_PATH }),
macosVersion : () =>
readMacosVersion ( hostExecFileOutput , commandEnvironment ),
linuxDistribution : readLinuxDistribution ,
async validTarget ( path ) {
2026-10-10 00:16:04 +08:00
return (
await parseBinaryTarget ( path , {
cwd : process.cwd (),
hostArchitecture : architecture ,
})
). ok ;
2026-10-06 00:50:38 +08:00
},
2026-10-06 19:53:08 +08:00
async inspectTarget ( path ) {
2026-10-10 00:16:04 +08:00
const result = await parseBinaryTarget ( path , {
cwd : process.cwd (),
hostArchitecture : architecture ,
});
2026-10-06 19:53:08 +08:00
if ( result . ok )
return {
name : "target" ,
ok : true ,
classification : "healthy" ,
detail : path ,
};
const error = result . error ;
2026-10-10 00:16:04 +08:00
const targetError =
error instanceof BinaryTargetError ? error : undefined ;
2026-10-06 19:53:08 +08:00
return {
name : "target" ,
ok : false ,
classification :
2026-10-12 07:00:30 +08:00
targetError?.constraint !== undefined
2026-10-06 19:53:08 +08:00
? "unsupported_target"
: "config_drift" ,
detail : path ,
details : {
2026-10-10 00:16:04 +08:00
reason : targetError?.reason ?? error . message ,
...( targetError ? . constraint === undefined
2026-10-06 19:53:08 +08:00
? {}
2026-10-10 00:16:04 +08:00
: { constraint : targetError.constraint }),
2026-10-06 19:53:08 +08:00
},
remediation : analysisErrorRemediationAction ( error ),
};
},
2026-10-06 00:50:38 +08:00
executable : ( path ) =>
executableAvailable (
path ,
platform ,
hostExecFileOutput ,
commandEnvironment ,
),
linuxDemoRuntimeCheck :
options.linuxDemoRuntimeCheck ?? uncomposedLinuxDemoRuntimeCheck ,
supportedLinuxHopper : linuxHopperBinarySupported ,
async brewHopperPath() {
return probeHomebrew ( async ( command ) => {
2026-10-09 03:28:25 +00:00
let caskroom : string ;
2026-10-06 00:50:38 +08:00
try {
2026-10-09 03:28:25 +00:00
// Without a cask argument, `--caskroom` does not load Homebrew's
// cask API, so doctor writes no Homebrew cache files.
caskroom = (
2026-10-06 00:50:38 +08:00
await hostExecFileOutput (
command ,
2026-10-09 03:28:25 +00:00
[ "--caskroom" ],
2026-10-06 00:50:38 +08:00
commandEnvironment ,
)
). stdout . trim ();
2026-10-06 05:58:46 +08:00
} catch ( cause : unknown ) {
// best-effort cleanup: optional Homebrew probing; absence means uninstalled.
void cause ;
2026-10-06 00:50:38 +08:00
return undefined ;
}
2026-10-09 03:28:25 +00:00
const appdir = await installedCaskAppdir (
caskroom ,
"hopper-disassembler" ,
homeDirectory ,
);
return appdir === undefined
? undefined
: join ( appdir , "Hopper Disassembler.app/Contents/MacOS/hopper" );
2026-10-06 00:50:38 +08:00
});
},
manualHopperPaths : () => manualHopperPaths ( homeDirectory ),
...( options . providerInspections === undefined
? {}
: { providerInspections : options.providerInspections }),
2026-10-14 10:06:46 +08:00
...( options . providerRegistrationEnvironment === undefined
? {}
: {
providerRegistrationEnvironment :
options.providerRegistrationEnvironment ,
}),
2026-10-06 00:50:38 +08:00
async ilspyCmdVersion ( path ) {
2026-07-12 06:34:27 +08:00
try {
2026-10-06 00:50:38 +08:00
await access ( path , constants . X_OK );
return firstIlspyVersionLine (
(
await hostExecFileOutput ( path , [ "--version" ], {
timeout : 10_000 ,
... commandEnvironment ,
})
). stdout ,
);
2026-10-06 05:58:46 +08:00
} catch ( cause : unknown ) {
// best-effort cleanup: optional version probing; failure means unavailable.
void cause ;
2026-07-12 06:53:31 +08:00
return undefined ;
2026-07-12 06:34:27 +08:00
}
2026-10-06 00:50:38 +08:00
},
async installationPaths() {
try {
const command = platform === "win32" ? "where" : "which" ;
const arguments_ = platform === "win32" ? [ "rea" ] : [ "-a" , "rea" ];
return uniqueLines (
( await hostExecFileOutput ( command , arguments_ , commandEnvironment ))
. stdout ,
);
2026-10-06 05:58:46 +08:00
} catch ( cause : unknown ) {
// best-effort cleanup: optional path probing; failure means none found.
void cause ;
2026-10-06 00:50:38 +08:00
return [];
}
},
2026-10-10 01:59:12 +08:00
installedSkillIdentity : ( clientIds ) =>
readInstalledSkillIdentity (
homeDirectory ,
clientIds ,
2026-10-10 01:03:25 -07:00
environment ,
platform ,
2026-10-12 05:56:26 +08:00
resources ,
2026-10-10 01:59:12 +08:00
),
2026-10-12 05:56:26 +08:00
close : async () => {
try {
await resources . close ();
return undefined ;
} catch ( cause : unknown ) {
return `Doctor resource cleanup failed: ${ cause instanceof Error ? cause.message : String ( cause ) } ` ;
}
},
2026-10-06 00:50:38 +08:00
clientRegistrations : () =>
readClientRegistrationStatuses ( homeDirectory , undefined , {
platform ,
environment ,
2026-10-12 05:56:26 +08:00
resources ,
2026-10-06 00:50:38 +08:00
}),
};
};
2026-07-18 04:11:29 +00:00
2026-10-06 00:50:38 +08:00
const readMacosVersion = async (
2026-10-09 20:02:52 +08:00
run : typeof execFileOutput ,
options : { readonly env : NodeJS.ProcessEnv },
2026-10-06 00:50:38 +08:00
) : Promise < string | undefined > => {
2026-07-18 04:11:29 +00:00
try {
2026-10-06 00:50:38 +08:00
return ( await run ( "sw_vers" , [ "-productVersion" ], options )). stdout . trim ();
2026-10-06 05:58:46 +08:00
} catch ( cause : unknown ) {
// best-effort cleanup: optional OS version probing; failure means unknown.
void cause ;
2026-07-18 04:11:29 +00:00
return undefined ;
}
};
2026-10-06 00:50:38 +08:00
const executableAvailable = async (
path : string ,
2026-10-09 20:02:52 +08:00
platform : NodeJS.Platform ,
run : typeof execFileOutput ,
options : { readonly env : NodeJS.ProcessEnv },
2026-10-06 00:50:38 +08:00
) : Promise < boolean > => {
2026-07-18 04:11:29 +00:00
try {
await access ( path , constants . X_OK );
2026-10-10 09:31:26 -04:00
if ( ! ( await stat ( path )). isFile ()) return false ;
2026-10-06 00:50:38 +08:00
if ( platform !== "linux" ) return true ;
const linked = await run ( "ldd" , [ path ], options );
2026-07-18 04:11:29 +00:00
return linuxSharedLibrariesAvailable ( ` ${ linked . stdout } \ n ${ linked . stderr } ` );
2026-10-06 05:58:46 +08:00
} catch ( cause : unknown ) {
// best-effort cleanup: optional executable probing; failure means unavailable.
void cause ;
2026-07-18 04:11:29 +00:00
return false ;
}
};
2026-07-22 20:38:26 +08:00
const uncomposedLinuxDemoRuntimeCheck = () : Promise < DoctorCheck > =>
Promise . resolve ({
name : "hopper-demo-runtime" ,
ok : false ,
classification : "config_drift" ,
detail : "Linux Hopper private-display diagnostics were not composed" ,
remediation : "Run rea doctor through the production CLI adapter." ,
});
2026-07-18 04:11:29 +00:00
2026-10-09 20:02:52 +08:00
const manualHopperPaths = async ( home : string ) : Promise < readonly string [] > => {
2026-07-18 04:11:29 +00:00
const paths : string [] = [];
2026-10-06 00:50:38 +08:00
for ( const root of [ "/Applications" , join ( home , "Applications" )]) {
2026-07-14 01:12:05 +00:00
try {
2026-07-18 04:11:29 +00:00
for ( const entry of await readdir ( root , { withFileTypes : true })) {
if ( entry . isDirectory () && /^Hopper.*\.app$/i . test ( entry . name ))
paths . push ( join ( root , entry . name , "Contents/MacOS/hopper" ));
}
2026-10-06 05:58:46 +08:00
} catch ( cause : unknown ) {
2026-07-18 04:11:29 +00:00
// A missing or unreadable optional application directory is not fatal.
2026-10-06 05:58:46 +08:00
void cause ;
2026-07-14 01:12:05 +00:00
}
2026-07-18 04:11:29 +00:00
}
return paths ;
};
2026-07-14 01:12:05 +00:00
const uniqueLines = ( value : string ) : string [] => [
... new Set (
value
. split ( /\r?\n/u)
. map (( line ) => line . trim ())
. filter (( line ) => line . length > 0 ),
),
];
2026-07-16 22:51:25 +08:00
const firstIlspyVersionLine = ( value : string ) : string | undefined =>
uniqueLines ( value ). find (( line ) => line . startsWith ( "ilspycmd:" ));