From 60de294646b7a2fa2d651a78e092ffbdb82f7175 Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Mon, 30 Nov 2026 16:05:38 +0500 Subject: [PATCH] fix(process): compare Windows server paths by native realpath (#15044) * fix(process): compare Windows server paths by native realpath compareServerPaths returned mismatch for every non-equal Windows path, so case, separator, 8.3, and junction spellings of the running server were reported as registration_path_mismatch. Compare native realpaths on Windows, matching registration identity: a different existing file still mismatches and an unresolvable path stays unknown. * test(process): run the server path identity regression on Windows The injected win32 platform only selects the comparison branch; on the Linux source-test host realpathSync.native still uses POSIX rules, so Windows letter case, separators, and junction behavior were unverified. Add Windows-only spelling cases beside the portable regression and include the file in the curated Windows lane, with the lane guard asserting it stays there. * ci(windows): own the server identity regression in the package lane The Windows lane now runs serverIdentity.test.ts, so the package scope must own that path for the native-host lane ownership check. * docs(process): clarify server path comparison authority --------- Co-authored-by: Vincent Koc --- .github/workflows/ci.yml | 1 + scripts/ci/scopes.mjs | 1 + src/process/ServerIdentity.ts | 16 +++- tests/boundary/mcp/serverIdentity.test.ts | 79 +++++++++++++++++++ .../process-global/curatedWindowsLane.test.ts | 1 + 5 files changed, 96 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b24c3f62123..6bb29d7e619 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1186,6 +1186,7 @@ jobs: tests/boundary/mcp/advertisedDialect.test.ts tests/boundary/mcp/absoluteLocalPathSchema.test.ts tests/boundary/mcp/toolSchemaValidity.test.ts + tests/boundary/mcp/serverIdentity.test.ts tests/boundary/filesystem/safeOutputTree.test.ts tests/boundary/filesystem/safeOutputTreePublication.test.ts tests/boundary/providers/native/artifactProvider.archiveSafety.test.ts diff --git a/scripts/ci/scopes.mjs b/scripts/ci/scopes.mjs index 9533f6844e3..bd45ff3e66c 100644 --- a/scripts/ci/scopes.mjs +++ b/scripts/ci/scopes.mjs @@ -324,6 +324,7 @@ const PATH_SCOPES = { "tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts", "tests/boundary/mcp/rawBinaryMcp.test.ts", "tests/boundary/filesystem/windowsTargetSeparators.test.ts", + "tests/boundary/mcp/serverIdentity.test.ts", "src/contracts/advertisedDialect*.ts", "tests/boundary/mcp/advertisedDialect.test.ts", // The advertised absolute-path pattern is host-specific (#7151). diff --git a/src/process/ServerIdentity.ts b/src/process/ServerIdentity.ts index 77ceb1550ad..239ee984faf 100644 --- a/src/process/ServerIdentity.ts +++ b/src/process/ServerIdentity.ts @@ -1,4 +1,4 @@ -import { statSync } from "node:fs"; +import { realpathSync, statSync } from "node:fs"; import { dirname, basename, resolve } from "node:path"; import { HOST_PLATFORM } from "../platform/HostPlatform.js"; @@ -38,13 +38,22 @@ const compareServerPaths = ( expected: string, observed: string, runtime: JavaScriptRuntime["id"], + platform: NodeJS.Platform, ): "aligned" | "mismatch" | "unknown" => { // Select the registered script the way this runtime does: Node resolves it // lexically, while Bun follows native traversal through a symlink before `..`. const script = normalizeEntrypointPath(expected, runtime) ?? expected; if (script === observed) return "aligned"; - if (HOST_PLATFORM.platform === "win32") return "mismatch"; try { + // Windows identity comes from the native realpath, matching registration + // identity: case, separators, 8.3 names, and junctions to the same file + // canonicalize to one path, while a different file stays a mismatch. + // Like the POSIX stat below, this inspects a caller-selected filesystem + // path and preserves the shared Windows path contract. + if (platform === "win32") + return realpathSync.native(script) === realpathSync.native(observed) + ? "aligned" + : "mismatch"; const selected = statSync(script, { bigint: true }); const running = statSync(observed, { bigint: true }); return selected.dev === running.dev && selected.ino === running.ino @@ -65,6 +74,8 @@ export const createServerIdentity = (input: { readonly catalog: ReturnType; /** The runtime that selected `commandPath`; defaults to the current one. */ readonly runtime?: JavaScriptRuntime["id"]; + /** The platform whose path identity rules apply; defaults to this host. */ + readonly platform?: NodeJS.Platform; }) => { const catalog = input.catalog; const serverPath = input.commandPath || undefined; @@ -75,6 +86,7 @@ export const createServerIdentity = (input: { input.expected.server_path, serverPath, input.runtime ?? javaScriptRuntime(process.versions).id, + input.platform ?? HOST_PLATFORM.platform, ); const reasons: string[] = []; if ( diff --git a/tests/boundary/mcp/serverIdentity.test.ts b/tests/boundary/mcp/serverIdentity.test.ts index c28a0761533..32feff9c9a4 100644 --- a/tests/boundary/mcp/serverIdentity.test.ts +++ b/tests/boundary/mcp/serverIdentity.test.ts @@ -188,6 +188,85 @@ it.skipIf(process.platform === "win32")( }, ); +it("compares Windows server paths by native realpath identity", async () => { + const root = await createTestTempDirectory("server-identity-windows-path-"); + const scripts = join(root, "package", "scripts"); + const scratch = join(root, "scratch"); + await mkdir(scripts, { recursive: true }); + await mkdir(scratch); + const canonical = join(scripts, "rea.mjs"); + const other = join(root, "other.mjs"); + await writeFile(canonical, "entrypoint"); + await writeFile(other, "other entrypoint"); + await symlink(scripts, join(scratch, "link"), "junction"); + const identity = (server_path: string) => + createServerIdentity({ + catalog: DEFAULT_CATALOG_IDENTITY, + startedAt: "2026-07-13T00:00:00.000Z", + commandPath: canonical, + expected: { server_path }, + runtime: "node", + platform: "win32", + }); + + // A junction spelling of the running file is the same registration, while + // a different existing file still mismatches and a missing one is unknown. + expect(identity(join(scratch, "link", "rea.mjs")).alignment).toMatchObject({ + state: "aligned", + reasons: [], + }); + expect(identity(other).alignment).toMatchObject({ + state: "mcp_server_restart_required", + reasons: ["registration_path_mismatch"], + }); + expect(identity(join(root, "missing.mjs")).alignment).toMatchObject({ + state: "unknown", + reasons: [], + }); +}); + +it.skipIf(process.platform !== "win32")( + "compares Windows server path spellings by native identity", + async () => { + const root = await createTestTempDirectory( + "server-identity-windows-spelling-", + ); + const scripts = join(root, "package", "scripts"); + const scratch = join(root, "scratch"); + await mkdir(scripts, { recursive: true }); + await mkdir(scratch); + const canonical = join(scripts, "rea.mjs"); + const other = join(root, "other.mjs"); + await writeFile(canonical, "entrypoint"); + await writeFile(other, "other entrypoint"); + await symlink(scripts, join(scratch, "link"), "junction"); + const identity = (server_path: string) => + createServerIdentity({ + catalog: DEFAULT_CATALOG_IDENTITY, + startedAt: "2026-07-13T00:00:00.000Z", + commandPath: canonical, + expected: { server_path }, + runtime: "node", + platform: "win32", + }); + + // Only the native Windows realpath canonicalizes letter case and + // separator spellings of the running file to one identity. + expect(identity(join(scratch, "LINK", "REA.MJS")).alignment).toMatchObject({ + state: "aligned", + reasons: [], + }); + expect(identity(canonical.split("\\").join("/")).alignment).toMatchObject({ + state: "aligned", + reasons: [], + }); + expect(identity(other).alignment).toMatchObject({ + state: "mcp_server_restart_required", + reasons: ["registration_path_mismatch"], + }); + }, +); + it("reports the connected client and compares caller expectations against the live catalog", async () => { const session = createTestBinarySession(availabilityProvider()); const server = createServer({ kind: "session", session }); diff --git a/tests/process-global/curatedWindowsLane.test.ts b/tests/process-global/curatedWindowsLane.test.ts index b1c2cbc6106..ebf82e5b9b1 100644 --- a/tests/process-global/curatedWindowsLane.test.ts +++ b/tests/process-global/curatedWindowsLane.test.ts @@ -117,6 +117,7 @@ describe("curated Windows test lane", () => { "tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts", "tests/boundary/mcp/rawBinaryMcp.test.ts", "tests/boundary/filesystem/windowsTargetSeparators.test.ts", + "tests/boundary/mcp/serverIdentity.test.ts", ]), ); });