Skip to content

Commit 187f113

Browse files
morlutoqorvrhemv-857
committed
fix(identity)!: preserve invocation uncertainty and advertised catalogs
Capture the current entrypoint once without inventing a path. Preserve unknown registration ownership through setup and doctor, refuse unsafe registration plans, and retain exact pinned-launcher readiness checks. Hash the same full or compact schema projection advertised by each MCP server and use that identity in doctor comparisons. BREAKING CHANGE: server.command_path may be null when the invocation path is unavailable. Registration and setup statuses, and doctor classifications, may report unknown. Reviewed-PR: #3230, #3015 Co-authored-by: qorvr <[email protected]> Co-authored-by: Hemang Varshney <[email protected]>
1 parent dd1c879 commit 187f113

32 files changed

Lines changed: 753 additions & 178 deletions

‎src/application/ClientRegistrationIdentity.ts‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { basename, isAbsolute, resolve } from "node:path";
1+
import { isAbsolute, resolve } from "node:path";
22
import { valid } from "semver";
33

44
import { PRODUCT_IDENTITY } from "../identity.js";
@@ -112,13 +112,56 @@ const isOwnedPackageReference = (packageReference: string): boolean => {
112112
};
113113

114114
/** Check that one parsed command points to REA's MCP entry point. */
115+
export type ClientRegistrationCommandIdentity = "owned" | "foreign" | "unknown";
116+
117+
/** Classify only what the command and current executable identity establish. */
118+
export const classifyClientRegistrationCommand = (
119+
command: readonly string[],
120+
currentCommandPath: string | undefined,
121+
): ClientRegistrationCommandIdentity => {
122+
const packageReference = clientRegistrationPackageReference(command);
123+
if (packageReference !== undefined)
124+
return isOwnedClientRegistrationCommand(command, currentCommandPath)
125+
? "owned"
126+
: "foreign";
127+
const currentPathAvailable =
128+
currentCommandPath !== undefined && currentCommandPath.length > 0;
129+
const nodeScript = command.length === 3 && command[2] === "mcp";
130+
if (nodeScript) {
131+
if (
132+
!isAbsolute(command[0] ?? "") ||
133+
!isAbsolute(command[1] ?? "") ||
134+
!currentPathAvailable
135+
)
136+
return "unknown";
137+
return isOwnedClientRegistrationCommand(command, currentCommandPath)
138+
? "owned"
139+
: "foreign";
140+
}
141+
if (command.length === 2 && command[1] === "mcp") {
142+
const executable = command[0] ?? "";
143+
if (executable === PRODUCT_IDENTITY.cliBinary) return "owned";
144+
const relativePathLike =
145+
!isAbsolute(executable) && /[./\\]/u.test(executable);
146+
if (relativePathLike || (isAbsolute(executable) && !currentPathAvailable))
147+
return "unknown";
148+
}
149+
if (isOwnedClientRegistrationCommand(command, currentCommandPath))
150+
return "owned";
151+
return "foreign";
152+
};
153+
115154
export const isOwnedClientRegistrationCommand = (
116155
command: readonly string[],
117-
currentCommandPath: string = resolve(process.argv[1] ?? "unknown"),
156+
currentCommandPath: string | undefined,
118157
): boolean => {
119158
if (
159+
currentCommandPath !== undefined &&
160+
currentCommandPath.length > 0 &&
120161
command.length === 3 &&
121162
command[2] === "mcp" &&
163+
isAbsolute(command[0] ?? "") &&
164+
isAbsolute(command[1] ?? "") &&
122165
resolve(command[0] ?? "") === resolve(process.execPath) &&
123166
resolve(command[1] ?? "") === currentCommandPath
124167
)
@@ -129,8 +172,9 @@ export const isOwnedClientRegistrationCommand = (
129172
if (
130173
executable === PRODUCT_IDENTITY.cliBinary ||
131174
(isAbsolute(executable) &&
132-
basename(executable) === PRODUCT_IDENTITY.cliBinary) ||
133-
resolve(executable) === currentCommandPath
175+
currentCommandPath !== undefined &&
176+
currentCommandPath.length > 0 &&
177+
resolve(executable) === currentCommandPath)
134178
)
135179
return true;
136180
}

‎src/application/ClientRegistrationStatus.ts‎

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import {
77
type ClientConfigurationDocument,
88
} from "./ClientConfigurationDocument.js";
99
import { access } from "node:fs/promises";
10-
import { resolve } from "node:path";
10+
import { isAbsolute, resolve } from "node:path";
1111

1212
import { z } from "zod";
1313
import { valid } from "semver";
1414

1515
import { PRODUCT_IDENTITY } from "../identity.js";
16+
import { CURRENT_ENTRYPOINT_PATH } from "../process/CurrentEntrypointPath.js";
1617
import { MCP_STARTUP_POLICY } from "../mcpStartupPolicy.js";
1718
import {
1819
clientRegistrationPackageReference,
19-
isOwnedClientRegistrationCommand,
20+
classifyClientRegistrationCommand,
2021
packageRunnerRegistrationCommand,
2122
} from "./ClientRegistrationIdentity.js";
2223
import {
@@ -50,6 +51,11 @@ export type ClientRegistrationStatus = ClientRegistrationStatusBase &
5051
readonly state: "stale";
5152
readonly remediation: string;
5253
}
54+
| {
55+
readonly command: RegistrationCommand;
56+
readonly state: "unknown";
57+
readonly remediation: string;
58+
}
5359
| {
5460
readonly command: readonly [];
5561
readonly state: "missing" | "invalid" | "manual";
@@ -93,7 +99,7 @@ export interface ClientRegistrationInspection {
9399
/** Project diagnostics from the same inspection used to select maintenance. */
94100
export const readClientRegistrationStatuses = async (
95101
home: string,
96-
currentCommandPath: string = resolve(process.argv[1] ?? "unknown"),
102+
currentCommandPath: string | undefined = CURRENT_ENTRYPOINT_PATH,
97103
options: ClientRegistrationReadOptions = {},
98104
): Promise<readonly ClientRegistrationStatus[]> =>
99105
(
@@ -103,7 +109,7 @@ export const readClientRegistrationStatuses = async (
103109
/** Parse effective registrations and their client-specific activation once. */
104110
export const readClientRegistrationInspections = async (
105111
home: string,
106-
currentCommandPath: string = resolve(process.argv[1] ?? "unknown"),
112+
currentCommandPath: string | undefined = CURRENT_ENTRYPOINT_PATH,
107113
options: ClientRegistrationReadOptions = {},
108114
): Promise<readonly ClientRegistrationInspection[]> => {
109115
const inspections: ClientRegistrationInspection[] = [];
@@ -219,26 +225,32 @@ export const readClientRegistrationInspections = async (
219225
const packageVersionDrift =
220226
packageVersion !== undefined &&
221227
packageVersion !== PRODUCT_IDENTITY.packageVersion;
228+
const alignment = registrationAligned(
229+
registration,
230+
effectiveClient,
231+
currentCommandPath,
232+
options.platform ?? process.platform,
233+
);
222234
record(
223235
configuredStatus(
224236
effectiveClient,
225237
command,
226-
activation === "enabled" &&
227-
!packageVersionDrift &&
228-
registrationAligned(
229-
registration,
230-
effectiveClient,
231-
currentCommandPath,
232-
options.platform ?? process.platform,
233-
) &&
234-
(parsed.dialect !== "dsh" || dshDocument(parsed.document).ready) &&
235-
policyBlock === undefined
236-
? "aligned"
237-
: "stale",
238+
alignment === "unknown"
239+
? "unknown"
240+
: activation === "enabled" &&
241+
!packageVersionDrift &&
242+
alignment === "aligned" &&
243+
(parsed.dialect !== "dsh" ||
244+
dshDocument(parsed.document).ready) &&
245+
policyBlock === undefined
246+
? "aligned"
247+
: "stale",
238248
policyBlock ??
239249
(packageVersionDrift
240250
? packageVersionRemediation(packageReference ?? "", client)
241-
: undefined),
251+
: alignment === "unknown"
252+
? unknownEntryPointRemediation()
253+
: undefined),
242254
packageReference,
243255
),
244256
activation,
@@ -275,71 +287,85 @@ const registrationActivation = (
275287
const registrationAligned = (
276288
registration: z.output<typeof registrationSchema>,
277289
client: SetupClient,
278-
currentCommandPath: string,
290+
currentCommandPath: string | undefined,
279291
platform: NodeJS.Platform,
280-
): boolean => {
292+
): "aligned" | "stale" | "unknown" => {
281293
const command = [registration.command, ...registration.args];
282-
if (!isOwnedClientRegistrationCommand(command, currentCommandPath))
283-
return false;
294+
const identity = classifyClientRegistrationCommand(
295+
command,
296+
currentCommandPath,
297+
);
298+
if (identity === "unknown") return "unknown";
299+
if (identity === "foreign") return "stale";
284300
if (
285301
(client.name === "codex" || client.name === "grok_build") &&
286302
registration.startup_timeout_sec !==
287303
MCP_STARTUP_POLICY.codexStartupTimeoutSeconds
288304
)
289-
return false;
305+
return "stale";
290306
if (
291307
client.name === "kimi" &&
292308
(registration.startupTimeoutMs !==
293309
MCP_STARTUP_POLICY.kimiStartupTimeoutMs ||
294310
registration.toolTimeoutMs !== MCP_STARTUP_POLICY.kimiToolTimeoutMs)
295311
)
296-
return false;
312+
return "stale";
297313
if (
298314
(client.format === "kimi" ||
299315
client.format === "omp" ||
300316
client.format === "pi") &&
301317
registration.type !== undefined &&
302318
registration.type !== "stdio"
303319
)
304-
return false;
305-
if (client.format === "vscode" && registration.type !== "stdio") return false;
320+
return "stale";
321+
if (client.format === "vscode" && registration.type !== "stdio")
322+
return "stale";
306323
// A Harness registration only connects as the rea stdio server; another
307324
// server name or transport leaves the session without REA tools.
308325
if (
309326
client.format === "dsh" &&
310327
(registration.serverName !== "rea" || registration.transport !== "stdio")
311328
)
312-
return false;
329+
return "stale";
313330
if (
314331
client.format === "copilot_cli" &&
315332
(registration.type !== "stdio" ||
316333
JSON.stringify(registration.tools) !== JSON.stringify(["*"]))
317334
)
318-
return false;
335+
return "stale";
319336
if (client.format === "commandcode" && registration.transport !== "stdio")
320-
return false;
337+
return "stale";
321338
if (
339+
(["npx", "bunx"] as const).some((runner) => {
340+
const expected = packageRunnerRegistrationCommand(runner, platform);
341+
return (
342+
command.length === expected.length &&
343+
command.every((argument, index) => argument === expected[index])
344+
);
345+
})
346+
)
347+
return "aligned";
348+
if (
349+
currentCommandPath !== undefined &&
350+
currentCommandPath.length > 0 &&
322351
command.length === 3 &&
323352
command[2] === "mcp" &&
353+
isAbsolute(command[0] ?? "") &&
354+
isAbsolute(command[1] ?? "") &&
324355
resolve(command[0] ?? "") === resolve(process.execPath) &&
325356
resolve(command[1] ?? "") === currentCommandPath
326357
)
327-
return true;
358+
return "aligned";
328359
if (
329-
(["npx", "bunx"] as const).some((runner) => {
330-
const runnerCommand = packageRunnerRegistrationCommand(runner, platform);
331-
return (
332-
command.length === runnerCommand.length &&
333-
command.every((argument, index) => argument === runnerCommand[index])
334-
);
335-
})
336-
)
337-
return true;
338-
return (
360+
currentCommandPath !== undefined &&
361+
currentCommandPath.length > 0 &&
339362
command.length === 2 &&
340363
command[1] === "mcp" &&
364+
isAbsolute(command[0] ?? "") &&
341365
resolve(command[0] ?? "") === currentCommandPath
342-
);
366+
)
367+
return "aligned";
368+
return "stale";
343369
};
344370

345371
const parseRegistration = (
@@ -400,10 +426,13 @@ const remediation = (client: SetupClient): string => {
400426
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.`;
401427
};
402428

429+
const unknownEntryPointRemediation = (): string =>
430+
"The existing registration was retained because its executable identity cannot be verified. Run the installed REA CLI or a supported package runner, then rerun rea doctor; no registration was changed.";
431+
403432
const configuredStatus = (
404433
client: SetupClient,
405434
command: RegistrationCommand,
406-
state: "aligned" | "stale",
435+
state: "aligned" | "stale" | "unknown",
407436
policyRemediation?: string,
408437
packageReference?: string,
409438
): ClientRegistrationStatus =>

‎src/application/DirectAnalysisStatus.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { DirectAnalysisDependencies } from "./DirectAnalysisDependencies.js
77
import { providerCapabilitySet } from "../contracts/toolOutputSchemaPrimitives.js";
88
import { providerCapabilitySets } from "./binary/BinarySessionStatus.js";
99
import { sqliteDatabaseCapability } from "./sqlite/SqliteDatabaseCapability.js";
10+
import { CURRENT_ENTRYPOINT_PATH } from "../process/CurrentEntrypointPath.js";
1011

1112
/** Read target-free provider status and operation availability. */
1213
export const runSessionStatus = async (
@@ -31,6 +32,7 @@ export const runSessionStatus = async (
3132
],
3233
server_identity: createServerIdentity({
3334
startedAt: new Date().toISOString(),
35+
commandPath: CURRENT_ENTRYPOINT_PATH,
3436
}),
3537
};
3638
} finally {

‎src/application/Doctor.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ describe("doctor scoped readiness", () => {
330330
client: "cursor",
331331
config_path: "/home/user/.cursor/mcp.json",
332332
command: ["/old/rea", "mcp"],
333-
state: "stale",
333+
state: "unknown",
334334
remediation: "Run rea setup to refresh this registration.",
335335
},
336336
];
@@ -349,7 +349,11 @@ describe("doctor scoped readiness", () => {
349349
expect.objectContaining({ name: "registration:codex", ok: true }),
350350
);
351351
expect(scoped.informational_checks).toContainEqual(
352-
expect.objectContaining({ name: "registration:cursor", ok: false }),
352+
expect.objectContaining({
353+
name: "registration:cursor",
354+
ok: false,
355+
classification: "unknown",
356+
}),
353357
);
354358

355359
const staleSelected = await runDoctor(

‎src/application/Doctor.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface DoctorCheck {
5353
| "unsupported_host"
5454
| "unsupported_target"
5555
| "missing_analysis_engine"
56+
| "unknown"
5657
| "config_drift";
5758
readonly detail?: string;
5859
readonly details?: Readonly<Record<string, JsonValue>>;
@@ -385,7 +386,7 @@ const registrationCheck = (
385386
): DoctorCheck => ({
386387
name: `registration:${registration.client}`,
387388
ok: false,
388-
classification: "config_drift",
389+
classification: registration.state === "unknown" ? "unknown" : "config_drift",
389390
detail: registration.config_path,
390391
remediation: registration.remediation,
391392
});

‎src/application/DoctorScope.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ const registrationCheck = (
174174
): DoctorCheck => ({
175175
name: `registration:${status.client}`,
176176
ok: false,
177-
classification: "config_drift",
177+
classification: status.state === "unknown" ? "unknown" : "config_drift",
178178
detail: status.config_path,
179179
remediation: status.remediation,
180180
});

‎src/application/Setup.configuration.test.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { describe, expect, it } from "vitest";
2-
import { join, resolve } from "node:path";
2+
import { join } from "node:path";
33

44
import { FakeSetupHost, options } from "./Setup.fixture.js";
55
import { runSetup } from "./Setup.js";
66
import { setupRegistrationCommand } from "./SetupHost.js";
7+
import { CURRENT_ENTRYPOINT_PATH } from "../process/CurrentEntrypointPath.js";
78
import { discoverSetupState } from "./SetupPlan.js";
89
import type { MsixPackageView } from "./MsixPackageStorage.js";
910
import type { SetupClient } from "./SupportedClients.js";
@@ -333,13 +334,13 @@ describe("setup workflow action selection", () => {
333334

334335
expect(result.status).toBe("ready");
335336
expect(host.configuredCommands).toEqual([
336-
setupRegistrationCommand("win32", undefined),
337-
]);
338-
expect(setupRegistrationCommand("win32", undefined)).toEqual([
339-
process.execPath,
340-
resolve(process.argv[1] ?? "rea"),
341-
"mcp",
337+
setupRegistrationCommand("win32", undefined, CURRENT_ENTRYPOINT_PATH),
342338
]);
339+
expect(
340+
setupRegistrationCommand("win32", undefined, CURRENT_ENTRYPOINT_PATH),
341+
).toEqual([process.execPath, CURRENT_ENTRYPOINT_PATH, "mcp"]);
342+
expect(setupRegistrationCommand("win32", undefined, undefined)).toEqual([]);
343+
expect(setupRegistrationCommand("win32", "npx", undefined)).not.toEqual([]);
343344
});
344345

345346
it.each([

0 commit comments

Comments
 (0)