Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(remote-agent): gate Codex replace opens as writes

Lizid5eee437f1db
diff --git a/apps/desktop/src/main/remote-agent/__tests__/execServerRelayConsent.test.ts b/apps/desktop/src/main/remote-agent/__tests__/execServerRelayConsent.test.ts
index 0444775d5c..e3d7554c4f 100644
--- a/apps/desktop/src/main/remote-agent/__tests__/execServerRelayConsent.test.ts
+++ b/apps/desktop/src/main/remote-agent/__tests__/execServerRelayConsent.test.ts
@@ -91,6 +91,15 @@ describe('exec-server relay with confirmation on this computer', () => {
expect(pending).toEqual([]);
});
+ it('checks fs/open replace as a write before allowing the handle stream', async () => {
+ const { send, pending, replies } = setup();
+ send({ id: 1, method: 'fs/open', params: { handleId: 'h1', mode: 'replace', path: path.join(root, '.env') } });
+ await vi.waitFor(() => expect(pending).toHaveLength(1));
+ expect(pending[0].action).toEqual({ kind: 'write', path: path.join(root, '.env') });
+ pending[0].answer(false);
+ await vi.waitFor(() => expect(replies).toEqual([{ id: 1, error: { code: -32001, message: 'needs confirmation' } }]));
+ });
+
it('keeps later messages behind one that waits for the user, then forwards them in order', async () => {
const { send, sentIds, pending } = setup();
send({ id: 1, method: 'fs/readFile', params: { path: path.join(root, '.env') } });
diff --git a/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts b/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts
index dd9b7913cd..3ed3c8b333 100644
--- a/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts
+++ b/apps/desktop/src/main/remote-agent/__tests__/plumbing.test.ts
@@ -181,6 +181,8 @@ describe('exec-server relay gate', () => {
.toEqual([{ kind: 'read', path: skills, scope: 'tree' }]);
expect(execServerActions('fs/open', { handleId: 'h1', path: pathToFileURL(path.join(root, 'a.png')).href }, '/w'))
.toEqual([{ kind: 'read', path: path.join(root, 'a.png') }]);
+ expect(execServerActions('fs/open', { handleId: 'h2', mode: 'replace', path: pathToFileURL(path.join(root, 'a.png')).href }, '/w'))
+ .toEqual([{ kind: 'write', path: path.join(root, 'a.png') }]);
expect(execServerActions('fs/canonicalize', { path: pathToFileURL(skills).href }, '/w')).toEqual([]);
// 不认识的 fs 方法仍从严按写入。
expect(execServerActions('fs/futureMethod', { path: pathToFileURL(skills).href }, '/w'))
@@ -200,6 +202,7 @@ describe('exec-server relay gate', () => {
const plan = new ExecutorGate(new ExecutorWorkspace({ workingDir: project }), 'plan');
expect(decide(plan, 'fs/walk', { path: skills })).toEqual([true]);
expect(decide(plan, 'fs/open', { handleId: 'h1', path: agentsMd })).toEqual([true]);
+ expect(decide(plan, 'fs/open', { handleId: 'h2', mode: 'replace', path: agentsMd })).toEqual([false]);
// 任务在子目录:上级目录的单文件读取照读取规则放行,递归遍历仍要本机确认。
const sub = new ExecutorGate(new ExecutorWorkspace({ workingDir: client }), 'normal');
diff --git a/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts b/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts
index a90cae0d1b..ab092f1d57 100644
--- a/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts
+++ b/apps/desktop/src/main/remote-agent/controller/execServerRelay.ts
@@ -94,7 +94,7 @@ function fromFileUrl(value: unknown, base: string): string | null {
// 方法先从严)。`fs/walk`、`fs/canonicalize`、`fs/open` 曾落进写入:计划模式下工作区里的项目 Skill
// 加载被拒,工作区外的单文件读取也被当成区外写入拒掉(#5764)。
-/** 读单个文件或单层目录(`fs/open` 只开只读句柄,内容随后经 `fs/readBlock` 按句柄读取)。 */
+/** 读单个文件或单层目录;fs/open 的显式写入模式在下方按写入处理。 */
const READ_METHODS = new Set(['fs/readFile', 'fs/open', 'fs/readDirectory', 'fs/readDir', 'fs/listDirectory']);
/** 递归遍历:按目录级读取过闸门,根在工作区外与本机任务的搜索一样要本机确认。 */
const TREE_READ_METHODS = new Set(['fs/walk']);
@@ -114,7 +114,11 @@ export function execServerActions(method: string, params: unknown, cwd: string):
.map((key) => fromFileUrl(record[key], cwd))
.filter((value): value is string => !!value);
if (TREE_READ_METHODS.has(method)) return paths.map((target) => ({ kind: 'read', path: target, scope: 'tree' }));
- const kind = READ_METHODS.has(method) ? 'read' : 'write';
+ // fs/open also handles writes: Codex replace/write mode creates or truncates a file, while
+ // the later fs/writeBlock request carries only a handle and no path. Check the write at open.
+ // An omitted mode is the legacy read-only shape; every explicit unknown mode is fail-closed.
+ const readOnlyOpen = method !== 'fs/open' || record.mode === undefined || record.mode === 'read';
+ const kind = READ_METHODS.has(method) && readOnlyOpen ? 'read' : 'write';
return paths.map((target) => ({ kind, path: target }) as ExecutorAction);
}
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index d476a0dd5d..7809846e49 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -584,7 +584,8 @@ handler 无 sender 依赖;不加入共享任务访客白名单,不进入自
- **Codex 依赖**:B 的 Codex 通过 app-server 的实验接口 `environment/add` + `environments` 把 A 注册为
exec-server 执行环境,A 为每条连接起本机 `codex exec-server --listen stdio://`。该接口随 Codex 版本
可能变化,升级 Codex 时需回归 `remote-agent/__tests__/codexHosted.e2e.test.ts`。A 的中继按方法语义
- 过本机闸门(`controller/execServerRelay.ts`):`fs/readFile`、`fs/open` 与目录读取按读取,`fs/walk` 按
+ 过本机闸门(`controller/execServerRelay.ts`):`fs/readFile`、`fs/open` 的 read 模式与目录读取按读取,
+ `fs/open` 的 replace/write 模式按写入(后续 `fs/writeBlock` 只有句柄,权限必须在 open 时检查),`fs/walk` 按
目录级读取,`fs/getMetadata`、`fs/canonicalize` 等只看元数据的不过闸门,其余 `fs/` 方法按写入;
升级 Codex 后核对 exec-server 新增的方法,只读的要显式归类,否则会被当成写入拒掉(#5764)。
- **影子目录与配置同步**:`open` 载荷除项目说明文件(含 `.claude/CLAUDE.md`、`.claude/rules`、