TypeScript
fix(provider-muse): Muse turns no longer fail when the project path's case differs from the disk (#22346)
Nishchay Chandra5033239dec86
diff --git a/packages/provider-muse/src/server/adapter.test.ts b/packages/provider-muse/src/server/adapter.test.tsindex 7950ef28cb9..3baeabbbe3c 100644--- a/packages/provider-muse/src/server/adapter.test.ts+++ b/packages/provider-muse/src/server/adapter.test.ts@@ -1,3 +1,6 @@+// @effect-diagnostics-next-line nodeBuiltinImport:off -- Mock native realpath to exercise macOS case correction on every platform.+import * as NodeFSP from "node:fs/promises";+import { vi } from "vite-plus/test";import { assert, describe, it } from "@effect/vitest";import * as NodeServices from "@effect/platform-node/NodeServices";import { MspError } from "@muse-code/sdk";@@ -379,6 +382,8 @@ const approval = (turnId: string) => ({subject: { kind: "shell", command: "touch result.txt" },});+vi.mock("node:fs/promises", { spy: true });+describe("MuseAdapterV2", () => {it.effect("connects the thread's MCP credential on native start and resume", () =>Effect.gen(function* () {@@ -569,6 +574,36 @@ describe("MuseAdapterV2", () => {),);+ it.effect("pins sessions and turns to native canonical case", () =>+ Effect.gen(function* () {+ using _realpath = vi.spyOn(NodeFSP, "realpath").mockResolvedValue("/workspace/eevee");+ const fake = yield* makeFakeMuse();+ const policy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({+ ...runtimePolicy,+ cwd: "/workspace/EEVEE",+ });+ const harness = yield* makeHarness(+ fake,+ INSTANCE_ID,+ undefined,+ undefined,+ undefined,+ policy,+ );+ yield* startConversation(harness, fake);+ const sessionStart = fake.calls.find((call) => call.method === "session/start");+ const turnStart = fake.calls.find((call) => call.method === "turn/start");+ assert.strictEqual(sessionStart?.params.workspaceRoot, "/workspace/eevee");+ assert.deepStrictEqual(turnStart?.params.workspaceRoots, [+ sessionStart?.params.workspaceRoot,+ ]);+ }).pipe(+ Effect.scoped,+ Effect.provide(testLayer),+ Effect.provideService(HostProcess.Platform, "darwin"),+ ),+ );+it.effect("stops at a Meta usage limit until every exhausted window resets", () =>Effect.gen(function* () {const fake = yield* makeFakeMuse();diff --git a/packages/provider-muse/src/server/adapter.ts b/packages/provider-muse/src/server/adapter.tsindex 40785df6bad..f022bf407fb 100644--- a/packages/provider-muse/src/server/adapter.ts+++ b/packages/provider-muse/src/server/adapter.ts@@ -65,6 +65,7 @@ import { isMuseUsageLimitFailure, museUsageLimitResetAt } from "./usageLimits.tsimport {createMuseSdkHostEffect,museApprovalMode,+ museCanonicalPath,musePermissionProfile,museWorkspaceRoot,type createMuseSdkHost,@@ -289,11 +290,9 @@ export const makeMuseAdapterV2 = Effect.fn("makeMuseAdapterV2")(function* (if (input.runtimePolicy.interactionMode === "plan")return yield* protocolError("Muse Code does not support dedicated Plan mode");const scope = yield* Effect.scope;- // Muse requires canonical workspace roots and approval paths; resolve symlinks once.+ // Native realpath also corrects filesystem case, which Muse validates.const requestedCwd = input.runtimePolicy.cwd ?? providerHost.paths.cwd;- const cwd = yield* fileSystem- .realPath(requestedCwd)- .pipe(Effect.orElseSucceed(() => requestedCwd));+ const cwd = yield* museCanonicalPath(requestedCwd);const workspaceRoot = yield* museWorkspaceRoot(cwd);const now = yield* DateTime.now;let session: OrchestrationV2ProviderSession = {diff --git a/packages/provider-muse/src/server/sdk.test.ts b/packages/provider-muse/src/server/sdk.test.tsindex c1a11270e35..739b8c04ff4 100644--- a/packages/provider-muse/src/server/sdk.test.ts+++ b/packages/provider-muse/src/server/sdk.test.ts@@ -1,3 +1,5 @@+// @effect-diagnostics-next-line nodeBuiltinImport:off -- Mock native realpath to exercise macOS case correction on every platform.+import * as NodeFSP from "node:fs/promises";// @effect-diagnostics-next-line nodeBuiltinImport:off -- The Muse SDK spawns with Node's child_process, so the launch test quotes arguments the same way.import * as NodeChildProcess from "node:child_process";import * as NodeServices from "@effect/platform-node/NodeServices";@@ -21,6 +23,8 @@ import {type MuseSdkHost,} from "./sdk.ts";+vi.mock("node:fs/promises", { spy: true });+vi.mock("@muse-code/sdk", () => ({ spawnMspConnection: vi.fn() }));function pending<A>() {@@ -311,13 +315,54 @@ describe("Muse launch", () => {});describe("Muse workspace root", () => {+ it.effect("uses native canonical case off Windows", () =>+ Effect.gen(function* () {+ using _realpath = vi.spyOn(NodeFSP, "realpath").mockResolvedValue("/workspace/eevee");+ expect(yield* museWorkspaceRoot("/workspace/EEVEE")).toBe("/workspace/eevee");+ }).pipe(Effect.provideService(HostProcess.Platform, "darwin")),+ );+it("writes Windows paths in their verbatim form", () => {expect(museVerbatimPath("C:\\Users\\Person\\repo")).toBe("\\\\?\\C:\\Users\\Person\\repo");expect(museVerbatimPath("\\\\server\\share\\repo")).toBe("\\\\?\\UNC\\server\\share\\repo");expect(museVerbatimPath("\\\\?\\C:\\repo")).toBe("\\\\?\\C:\\repo");});- // FileSystem.realPath keeps the case it is given; Muse rejects a root whose case differs from the disk's.+ it.effect("keeps a root it cannot resolve off Windows", () =>+ Effect.gen(function* () {+ expect(yield* museWorkspaceRoot("/workspace/repo")).toBe("/workspace/repo");+ }).pipe(Effect.provideService(HostProcess.Platform, "linux")),+ );++ it.effect("keeps a Windows root it cannot resolve, in verbatim form", () =>+ Effect.gen(function* () {+ expect(yield* museWorkspaceRoot("C:\\definitely-missing\\repo")).toBe(+ "\\\\?\\C:\\definitely-missing\\repo",+ );+ expect(yield* museWorkspaceRoot("C:/definitely-missing/repo")).toBe(+ "\\\\?\\C:\\definitely-missing\\repo",+ );+ expect(yield* museWorkspaceRoot("//server/share/missing")).toBe(+ "\\\\?\\UNC\\server\\share\\missing",+ );+ }).pipe(Effect.provideService(HostProcess.Platform, "win32")),+ );++ // FileSystem.realPath keeps the case it is given; Muse rejects a root whose case+ // differs from the disk's, and APFS is case-insensitive just like NTFS.+ it.effect("fixes the case of a macOS root natively", () =>+ Effect.gen(function* () {+ if ((yield* HostProcess.Platform) !== "darwin") return;+ const fs = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const created = yield* fs.makeTempDirectoryScoped({ prefix: "MuseRoot-" });+ // Only a case-insensitive volume resolves the miscased path; skip on a case-sensitive one.+ if (!(yield* fs.exists(created.toUpperCase()))) return;+ const root = yield* museWorkspaceRoot(created.toUpperCase());+ expect(path.basename(root)).toBe(path.basename(created));+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );+it.effect("fixes the case of a Windows root natively", () =>Effect.gen(function* () {if ((yield* HostProcess.Platform) !== "win32") return;diff --git a/packages/provider-muse/src/server/sdk.ts b/packages/provider-muse/src/server/sdk.tsindex fdd8da3b87e..17e9a017d81 100644--- a/packages/provider-muse/src/server/sdk.ts+++ b/packages/provider-muse/src/server/sdk.ts@@ -1,6 +1,6 @@// @effect-diagnostics globalTimers:off// The SDK owns process shutdown; native deadlines cover initialize before an Effect resource exists.-// @effect-diagnostics-next-line nodeBuiltinImport:off -- FileSystem.realPath is Node's JS realpath; museWorkspaceRoot needs the native one.+// @effect-diagnostics-next-line nodeBuiltinImport:off -- FileSystem.realPath is Node's JS realpath; museCanonicalPath needs the native one.import * as NodeFSP from "node:fs/promises";import {@@ -70,9 +70,16 @@ export function museVerbatimPath(path: string): string {return path.startsWith("\\\\") ? `\\\\?\\UNC\\${path.slice(2)}` : `\\\\?\\${path}`;}-/** Windows roots need native realpath for true case/long names, then verbatim form; launch cmd.exe in plain form. */+// Native realpath corrects filesystem case as well as resolving symlinks.+export const museCanonicalPath = Effect.fn("museCanonicalPath")(function* (path: string) {+ return yield* Effect.tryPromise(() => NodeFSP.realpath(path)).pipe(+ Effect.orElseSucceed(() => path),+ );+});++// Windows turn roots need verbatim form; launch cmd.exe with the plain canonical path.export const museWorkspaceRoot = Effect.fn("museWorkspaceRoot")(function* (path: string) {- if ((yield* HostProcess.Platform) !== "win32") return path;+ if ((yield* HostProcess.Platform) !== "win32") return yield* museCanonicalPath(path);const canonical = yield* Effect.tryPromise(() => NodeFSP.realpath(path)).pipe(// A verbatim path takes `/` literally, so an unresolved root needs Windows separators.Effect.orElseSucceed(() => path.replaceAll("/", "\\")),