Skip to content

Commit 6bd8356

Browse files
authored
fix(mxc): agent tools fail in Windows workspace subdirectories (#115399)
* fix(mxc): validate agent workdirs Co-authored-by: Copilot <[email protected]> Copilot-Session: fc20c06c-e8c0-4649-9efd-ad87fac71a4f * fix(mxc): treat non-directory parents as unavailable workdirs validateWorkdir declares that unusable workdirs return null, but only ENOENT was classified as missing. A workdir nested under a file raises ENOTDIR on Linux, which escaped as a raw filesystem error instead of the normal unavailable-workdir result. The same ENOENT-only classification appeared at three sibling sites in this file, so the shared isMissingPathError guard fixes the whole class rather than the single reported path. Co-authored-by: Copilot <[email protected]> Copilot-Session: d7253887-71c9-4083-a591-91979c981e39 * fix(mxc): sandbox provisioning crashes reading containerWorkdir MxcFsBridge initialized three fields from this.sandbox, but sandbox is a constructor parameter property. Plugin sources load through jiti, which evaluates field initializers before assigning parameter properties, so this.sandbox was still undefined and every MXC sandbox provisioning failed with SandboxProvisioningError: Cannot read properties of undefined (reading 'containerWorkdir'). Assign the derived fields in the constructor body from the sandbox parameter instead. The repo tsconfig sets useDefineForClassFields=false, so tsgo and Vitest (esbuild) both apply legacy ordering and could not observe the failure; only the jiti-loaded runtime path did. Co-authored-by: Copilot <[email protected]> Copilot-Session: d7253887-71c9-4083-a591-91979c981e39 --------- Copilot-Session: fc20c06c-e8c0-4649-9efd-ad87fac71a4f Copilot-Session: d7253887-71c9-4083-a591-91979c981e39
1 parent b5d5ec3 commit 6bd8356

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

‎extensions/mxc/src/fs-bridge.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,26 @@ export function createMxcFsBridge(params: { sandbox: MxcFsBridgeContext }): Sand
3535
}
3636

3737
class MxcFsBridge implements SandboxFsBridge {
38-
private readonly defaultContainerRoot = path.resolve(this.sandbox.containerWorkdir);
38+
// These must be assigned in the constructor body, not as field initializers.
39+
// Plugin sources load through jiti, which evaluates field initializers before
40+
// it assigns constructor parameter properties, so `this.sandbox` would still
41+
// be undefined here and provisioning would crash reading containerWorkdir.
42+
private readonly defaultContainerRoot: string;
3943

40-
private readonly protectedSkillMounts = resolveMxcProtectedSkillMounts(this.sandbox);
44+
private readonly protectedSkillMounts: readonly MxcFsMount[];
4145

42-
private readonly workspaceMounts = resolveWorkspaceMounts(this.sandbox);
46+
private readonly workspaceMounts: readonly MxcFsMount[];
4347

4448
private readonly resolveRenameTargets = createWritableRenameTargetResolver(
4549
(target) => this.resolveTarget(target),
4650
(target, action) => this.ensureWritable(target, action),
4751
);
4852

49-
constructor(private readonly sandbox: MxcFsBridgeContext) {}
53+
constructor(private readonly sandbox: MxcFsBridgeContext) {
54+
this.defaultContainerRoot = path.resolve(sandbox.containerWorkdir);
55+
this.protectedSkillMounts = resolveMxcProtectedSkillMounts(sandbox);
56+
this.workspaceMounts = resolveWorkspaceMounts(sandbox);
57+
}
5058

5159
resolvePath(params: { filePath: string; cwd?: string }): SandboxResolvedPath {
5260
const target = this.resolveTarget(params);

‎extensions/mxc/src/mxc-backend.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function resolveWorkdirInsideWorkspace(workspaceDir: string, workdir: string): s
106106
return candidate;
107107
}
108108
} catch (err) {
109-
if (isNodeError(err) && err.code === "ENOENT") {
109+
if (isMissingPathError(err)) {
110110
throw new Error(`MXC sandbox workdir ${workdir} does not exist.`, { cause: err });
111111
}
112112
throw err;
@@ -118,7 +118,7 @@ function realpathForExistingPath(value: string, label: string): string {
118118
try {
119119
return realpathSync(path.resolve(value));
120120
} catch (err) {
121-
if (isNodeError(err) && err.code === "ENOENT") {
121+
if (isMissingPathError(err)) {
122122
throw new Error(`MXC ${label} ${value} does not exist.`, { cause: err });
123123
}
124124
throw err;
@@ -130,7 +130,7 @@ function realpathForPotentialPath(value: string): string {
130130
try {
131131
return realpathSync(resolved);
132132
} catch (err) {
133-
if (!isNodeError(err) || err.code !== "ENOENT") {
133+
if (!isMissingPathError(err)) {
134134
throw err;
135135
}
136136
const parent = path.dirname(resolved);
@@ -145,6 +145,13 @@ function isNodeError(err: unknown): err is NodeJS.ErrnoException {
145145
return err instanceof Error && "code" in err;
146146
}
147147

148+
// ENOTDIR means a parent component is a file, so the path can never resolve to a
149+
// directory. Classifying it alongside ENOENT keeps validateWorkdir's "return null
150+
// for unusable workdirs" contract intact instead of leaking a raw filesystem error.
151+
function isMissingPathError(err: unknown): boolean {
152+
return isNodeError(err) && (err.code === "ENOENT" || err.code === "ENOTDIR");
153+
}
154+
148155
function buildMxcLauncherOptions(config: MxcConfig, usePty: boolean): MxcLauncherOptions {
149156
const options: MxcLauncherOptions = {
150157
debug: config.debug ?? false,
@@ -194,6 +201,17 @@ export function createMxcSandboxBackendHandle(params: {
194201
runtimeId: params.runtimeId,
195202
runtimeLabel: params.runtimeId,
196203
workdir: params.workdir,
204+
workdirValidation: "backend",
205+
async validateWorkdir(workdir) {
206+
try {
207+
return resolveWorkdirInsideWorkspace(params.workdir, workdir);
208+
} catch (err) {
209+
if (err instanceof Error && err.message.startsWith("MXC sandbox workdir")) {
210+
return null;
211+
}
212+
throw err;
213+
}
214+
},
197215
capabilities: {},
198216

199217
async buildExecSpec({ command, workdir, env, usePty }): Promise<SandboxBackendExecSpec> {

‎extensions/mxc/test/mxc-backend.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,29 @@ describeOnWindows("createMxcSandboxBackendHandle (Windows-only MXC backend tests
299299
});
300300
});
301301

302+
test("normalizes agent-tool workdirs before execution", async () => {
303+
const childDir = path.join(baseParams.workdir, "child");
304+
mkdirSync(childDir);
305+
const handle = createMxcSandboxBackendHandle(baseParams);
306+
307+
expect(handle.workdirValidation).toBe("backend");
308+
await expect(handle.validateWorkdir?.(`${baseParams.workdir}/child`)).resolves.toBe(childDir);
309+
await expect(
310+
handle.validateWorkdir?.(path.join(baseParams.workdir, "missing")),
311+
).resolves.toBeNull();
312+
});
313+
314+
test("reports workdirs nested under a file as unavailable instead of throwing", async () => {
315+
// A file parent surfaces ENOTDIR on Linux (CI truth) and ENOENT on Windows.
316+
// Both must reach the contract's null result rather than a raw filesystem error.
317+
const filePath = path.join(baseParams.workdir, "not-a-directory.txt");
318+
writeFileSync(filePath, "content");
319+
const handle = createMxcSandboxBackendHandle(baseParams);
320+
321+
await expect(handle.validateWorkdir?.(path.join(filePath, "child"))).resolves.toBeNull();
322+
await expect(handle.validateWorkdir?.(filePath)).resolves.toBeNull();
323+
});
324+
302325
test("buildExecSpec keeps command and env payload out of process argv", async () => {
303326
await withProcessEnv({ OPENCLAW_MXC_HOST_SECRET_TEST: "host-secret" }, async () => {
304327
const handle = createMxcSandboxBackendHandle(baseParams);

0 commit comments

Comments
 (0)