Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(remote-agent): 沙箱用户目录对不上时用本机的,盘符按 Agent 的路径约定判断
fmfsaisaid748021b2a81
diff --git a/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts b/apps/desktop/src/main/remote-agent/controller/execServerRelay.tsindex 524f9df3ef..6991ccad9e 100644--- a/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts+++ b/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts@@ -10,6 +10,7 @@* 受邀者任务里,凭证类操作没有批准时先在本机弹确认卡,等待期间这条连接后面的消息排在它后面。*/import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';+import os from 'node:os';import path from 'node:path';import { fileURLToPath, pathToFileURL } from 'node:url';@@ -158,37 +159,50 @@ function isLocalAbsolutePath(value: string): boolean {: path.posix.isAbsolute(value) && !/^[\\/]{2}/.test(value);}-/** Agent 发来的 file URI 还原成它那边的路径写法(`file:///C:/x` → `C:/x`,UNC 带回主机名)。 */-function agentPathFromUri(value: unknown): string | null {+/** 转换沙箱用到的 Agent 一侧信息:它的路径约定按虚拟工作区(Agent 主机上的路径)判断,不按单个 URI 的长相猜。 */+interface SandboxMapping {+ workspace: ExecutorWorkspace;+ windowsAgent: boolean;+}++/** Agent 发来的 file URI 还原成它那边的路径写法(Windows Agent:`file:///C:/x` → `C:/x`;UNC 带回主机名)。 */+function agentPathFromUri(value: unknown, windowsAgent: boolean): string | null {if (typeof value !== 'string' || !value.startsWith('file://')) return null;try {const url = new URL(value);const pathname = decodeURIComponent(url.pathname);- return url.host ? `//${url.host}${pathname}` : pathname.replace(/^\/([A-Za-z]:)/, '$1');+ if (url.host) return `//${url.host}${pathname}`;+ // POSIX 上 `/C:/secret` 是根目录下的普通路径,只有 Windows Agent 才去掉盘符前的 `/`。+ return windowsAgent ? pathname.replace(/^\/([A-Za-z]:)/, '$1') : pathname;} catch {return null;}}/** 虚拟工作区内的 file URI 换成本机 file URI;Agent 主机自己的路径返回 null。 */-function localUriFromAgent(value: unknown, workspace: ExecutorWorkspace): string | null {- const agentPath = agentPathFromUri(value);+function localUriFromAgent(value: unknown, { workspace, windowsAgent }: SandboxMapping): string | null {+ const agentPath = agentPathFromUri(value, windowsAgent);const local = agentPath === null ? null : workspace.localPathFromAgent(agentPath);return local === null ? null : pathToFileURL(local).href;}-/** 沙箱上下文里解释权限条目用的目录(policyContext 里同名字段同样处理)。 */-function mapSandboxDirs(record: JsonRecord, workspace: ExecutorWorkspace): JsonRecord {+/**+ * 沙箱上下文里解释权限条目用的目录(policyContext 里同名字段同样处理)。用户目录对不上时用本机的:+ * Codex 缺了它会把 `~/…` 规则当作解析失败、一律拒绝读取。+ */+function mapSandboxDirs(record: JsonRecord, mapping: SandboxMapping): JsonRecord {const result = { ...record };- for (const key of ['cwd', 'userHomeDir']) {- if (result[key] === undefined) continue;- const local = localUriFromAgent(result[key], workspace);- if (local === null) delete result[key];- else result[key] = local;+ if (result.cwd !== undefined) {+ const local = localUriFromAgent(result.cwd, mapping);+ if (local === null) delete result.cwd;+ else result.cwd = local;+ }+ if (result.userHomeDir !== undefined) {+ result.userHomeDir = localUriFromAgent(result.userHomeDir, mapping) ?? pathToFileURL(os.homedir()).href;}for (const key of ['workspaceRoots', 'temporaryDirectories']) {const items = result[key];- if (Array.isArray(items)) result[key] = items.flatMap((item) => localUriFromAgent(item, workspace) ?? []);+ if (Array.isArray(items)) result[key] = items.flatMap((item) => localUriFromAgent(item, mapping) ?? []);}return result;}@@ -200,19 +214,19 @@ function mapSandboxDirs(record: JsonRecord, workspace: ExecutorWorkspace): JsonR* 按工作目录解释,Agent 主机的绝对路径在本机匹配不到东西,无需判断它是不是绝对路径。特殊路径(`:tmpdir`、* 项目根等)由本机 exec-server 按映射后的目录解释。*/-function mapSandboxEntry(entry: unknown, workspace: ExecutorWorkspace): unknown[] {+function mapSandboxEntry(entry: unknown, mapping: SandboxMapping): unknown[] {const record = asJsonRecord(entry);const target = asJsonRecord(record?.path);if (!record || !target) return [entry];const deny = record.access === 'deny' || record.access === 'none';if (target.type === 'path') {- const local = localUriFromAgent(target.path, workspace);+ const local = localUriFromAgent(target.path, mapping);if (local !== null) return [{ ...record, path: { ...target, path: local } }];- const agentPath = agentPathFromUri(target.path);+ const agentPath = agentPathFromUri(target.path, mapping.windowsAgent);return deny && agentPath !== null && isLocalAbsolutePath(agentPath) ? [entry] : [];}if (target.type === 'glob_pattern' && typeof target.pattern === 'string') {- const local = workspace.localGlobFromAgent(target.pattern);+ const local = mapping.workspace.localGlobFromAgent(target.pattern);if (local !== null) return [{ ...record, path: { ...target, pattern: local } }];// 读写 glob 只有明确是绝对路径(盘符加分隔符、`/` 开头、`\\server`)才是 Agent 主机的路径,丢掉;判断不准只会少放。return deny || !/^(?:[A-Za-z]:[\\/]|\/|\\\\)/.test(target.pattern) ? [entry] : [];@@ -227,14 +241,16 @@ function mapSandboxEntry(entry: unknown, workspace: ExecutorWorkspace): unknown[*/function mapExecServerSandbox(value: unknown, workspace: ExecutorWorkspace): unknown {const record = asJsonRecord(value);- if (!record || workspace.agentPathPrefixes().length === 0) return value;- const sandbox = mapSandboxDirs(record, workspace);+ const agentPrefixes = workspace.agentPathPrefixes();+ if (!record || agentPrefixes.length === 0) return value;+ const mapping: SandboxMapping = { workspace, windowsAgent: /^(?:[A-Za-z]:|[\\/]{2})/.test(agentPrefixes[0]) };+ const sandbox = mapSandboxDirs(record, mapping);const policyContext = asJsonRecord(sandbox.policyContext);- if (policyContext) sandbox.policyContext = mapSandboxDirs(policyContext, workspace);+ if (policyContext) sandbox.policyContext = mapSandboxDirs(policyContext, mapping);const permissions = asJsonRecord(sandbox.permissions);const fileSystem = asJsonRecord(permissions?.file_system);if (permissions && fileSystem && Array.isArray(fileSystem.entries)) {- const entries = fileSystem.entries.flatMap((entry) => mapSandboxEntry(entry, workspace));+ const entries = fileSystem.entries.flatMap((entry) => mapSandboxEntry(entry, mapping));sandbox.permissions = { ...permissions, file_system: { ...fileSystem, entries } };}return sandbox;diff --git a/apps/desktop/src/main/remote-agent/executor/__tests__/virtualWorkspace.test.ts b/apps/desktop/src/main/remote-agent/executor/__tests__/virtualWorkspace.test.tsindex b05f71aa66..10c2bb0189 100644--- a/apps/desktop/src/main/remote-agent/executor/__tests__/virtualWorkspace.test.ts+++ b/apps/desktop/src/main/remote-agent/executor/__tests__/virtualWorkspace.test.ts@@ -25,7 +25,7 @@ function written(local: string, virtual: string): string { return virtual.starts/** 转发给 exec-server 的 Codex 请求里与沙箱有关的字段。 */interface MappedSandboxRequest {cwd?: string;- sandbox: { cwd?: string; permissions: { file_system: { entries: unknown[] } } };+ sandbox: { cwd?: string; userHomeDir?: string; permissions: { file_system: { entries: unknown[] } } };}function setup(virtual: string) {@@ -446,6 +446,8 @@ describe('virtual workspace', () => {] } },cwd: local(project),policyContext: { cwd: local(project), workspaceRoots: [local(project)] },+ // 对不上的用户目录换成本机的:Codex 缺了它会把 `~/…` 规则当解析失败、一律拒绝读取。+ userHomeDir: local(os.homedir()),temporaryDirectories: [local(temp)],windowsSandboxLevel: 'disabled',});@@ -483,6 +485,8 @@ describe('virtual workspace', () => {{ path: { type: 'path', path: 'file:///home/agent/workspace' }, access: 'write' },{ path: { type: 'path', path: 'file:///home/agent/.codex/memories' }, access: 'write' },{ path: { type: 'path', path: 'file:///home/agent/.ssh' }, access: 'deny' },+ // POSIX Agent 的 `/C:/secret` 是根目录下的普通路径,不是 Windows 盘符。+ { path: { type: 'path', path: 'file:///C:/secret' }, access: 'deny' },// 相对 glob 不能被误认成绝对路径丢掉:以转义开头(名字就叫 `*.pem` 的文件)、文件名里带冒号。{ path: { type: 'glob_pattern', pattern: '\\*.pem' }, access: 'deny' },{ path: { type: 'glob_pattern', pattern: 'a:*.pem' }, access: 'deny' },@@ -492,18 +496,23 @@ describe('virtual workspace', () => {{ path: { type: 'glob_pattern', pattern: '/home/agent/.aws/**' }, access: 'deny' },] } },cwd: 'file:///home/agent/workspace',+ userHomeDir: 'file:///home/agent',};const params = mapExecServerParams({ path: 'file:///home/agent/workspace/a.txt', sandbox }, workspace, 'fs/readFile') as MappedSandboxRequest;expect(params.sandbox.permissions.file_system.entries).toEqual([{ path: { type: 'path', path: pathToFileURL(project).href }, access: 'write' },// 任务电脑是 Windows 时 POSIX 路径不是本机路径,禁止条目也无从对应。- ...(process.platform === 'win32' ? [] : [{ path: { type: 'path', path: 'file:///home/agent/.ssh' }, access: 'deny' }]),+ ...(process.platform === 'win32' ? [] : [+ { path: { type: 'path', path: 'file:///home/agent/.ssh' }, access: 'deny' },+ { path: { type: 'path', path: 'file:///C:/secret' }, access: 'deny' },+ ]),{ path: { type: 'glob_pattern', pattern: '\\*.pem' }, access: 'deny' },{ path: { type: 'glob_pattern', pattern: 'a:*.pem' }, access: 'deny' },{ path: { type: 'glob_pattern', pattern: 'a:*.cache' }, access: 'write' },{ path: { type: 'glob_pattern', pattern: '/home/agent/.aws/**' }, access: 'deny' },]);expect(params.sandbox.cwd).toBe(pathToFileURL(project).href);+ expect(params.sandbox.userHomeDir).toBe(pathToFileURL(os.homedir()).href);// 没有虚拟工作区(旧版对端)时沙箱原样转发,与之前一致。const plain = new ExecutorWorkspace({ workingDir: project });expect((mapExecServerParams({ sandbox }, plain) as MappedSandboxRequest).sandbox).toBe(sandbox);diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.mdindex 8a4504ac7e..e7b9bc6e56 100644--- a/docs/dev-rules/protocol-compatibility.md+++ b/docs/dev-rules/protocol-compatibility.md@@ -1057,8 +1057,9 @@ handler 无 sender 依赖;不加入共享任务访客白名单,不进入自`WireFileSystemSandboxContext`),其中目录与权限条目都是 B 上看到的 file URI,中继换成 A 的路径:虚拟工作区内的映射为本机路径(绝对 glob 里本机目录名按字面转义);不在其中的是 B 自己的目录(如它的 Codex 记忆目录),读写授权丢弃,只收不放;禁止规则不因转换失效:路径条目在 A 上也是合法绝对路径时原样保留,没映射上的禁止 glob- 一律原样保留;网络与 Windows 沙箱档位不动。原样转发时两端系统不同会被 A 的 exec-server 拒绝- (`invalid sandbox permission path URI`)。+ 一律原样保留;用户目录对不上时换成 A 本机的(Codex 缺了它会把 `~/…` 规则当解析失败、一律拒绝读取);+ `file:///C:/…` 是否为盘符按 B 的路径约定(虚拟工作区路径)判断;网络与 Windows 沙箱档位不动。原样转发时+ 两端系统不同会被 A 的 exec-server 拒绝(`invalid sandbox permission path URI`)。- **影子目录与配置同步**:`open` 载荷除项目说明文件(含 `.claude/CLAUDE.md`、`.claude/rules`、`.codex/skills`)外还带 `ancestorFiles`(项目上级目录里的`CLAUDE.md` / `CLAUDE.local.md` / `AGENTS.md` / `AGENTS.override.md`,按层级 `up`,最多 24 级)与