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(shared-task): 访客按任务记录的端点执行,SSH 发送前重新绑定任务目录

david5e63c3c6417a
diff --git a/apps/desktop/src/main/file-browser/__tests__/deviceOp.test.ts b/apps/desktop/src/main/file-browser/__tests__/deviceOp.test.ts
index d2f3b67995..1fe24e80fc 100644
--- a/apps/desktop/src/main/file-browser/__tests__/deviceOp.test.ts
+++ b/apps/desktop/src/main/file-browser/__tests__/deviceOp.test.ts
@@ -299,11 +299,27 @@ describe('file-browser device-op', () => {
'PERMISSION_DENIED',
);
expect(guardMock).not.toHaveBeenCalled();
- // The task lives on an SSH host, but this path resolves locally: never guess.
- sharedWorkdirMock.mockResolvedValueOnce({ remoteHostId: 'ssh-1' });
- await expect(asGuest(() => handleRemoteOp({ op: 'listDir', workdir }))).rejects.toThrow(
- 'PERMISSION_DENIED',
- );
+ });
+
+ it('routes by the recorded task endpoint instead of reverse-resolving the path', async () => {
+ // The path exists locally and is also recorded for an SSH session: same-account
+ // controllers still see an ambiguous endpoint, a guest of the local task does not.
+ dbRowsMock.mockReturnValue([{ remoteHostId: 'other-host' }]);
+ await expect(handleRemoteOp({ op: 'listDir', workdir })).rejects.toThrow(/ambiguous/);
+ const entries = (await asGuest(() => handleRemoteOp({ op: 'listDir', workdir }))) as Array<{
+ name: string;
+ }>;
+ expect(entries.map((e) => e.name)).toContain('src');
+ expect(sshRequestMock).not.toHaveBeenCalled();
+ // A task recorded on an SSH host goes to that host even though the path is local.
+ sharedWorkdirMock.mockImplementation(async () => ({ remoteHostId: 'ssh-1' }));
+ sshRequestMock.mockResolvedValue({ entries: [{ name: 'remote.ts' }] });
+ const remote = (await asGuest(() => handleRemoteOp({ op: 'listDir', workdir }))) as Array<{
+ name: string;
+ }>;
+ expect(remote.map((e) => e.name)).toEqual(['remote.ts']);
+ expect(sshRequestMock.mock.calls[0]?.[0]).toBe('ssh-1');
+ sshRequestMock.mockReset();
});
it('does not write when membership ends during the endpoint checks', async () => {
@@ -325,13 +341,22 @@ describe('file-browser device-op', () => {
it('does not send an SSH write when membership ends while the connection is being built', async () => {
guardMock.mockResolvedValue({ allowed: false, reason: 'not-found' });
dbRowsMock.mockReturnValue([{ remoteHostId: 'host-1' }]);
- sharedWorkdirMock.mockImplementation(async () => ({ remoteHostId: 'host-1' }));
+ let taskHost = 'host-1';
+ sharedWorkdirMock.mockImplementation(async () => {
+ if (!current) throw new Error('[PERMISSION_DENIED] Shared task access changed');
+ return { remoteHostId: taskHost };
+ });
const sent = vi.fn();
- // The manager awaits getClient, then calls beforeSend right before sending.
+ // The manager awaits getClient, then awaits beforeSend right before sending.
sshRequestMock.mockImplementation(
- async (_h: string, method: string, _p: unknown, options?: { beforeSend?: () => void }) => {
+ async (
+ _h: string,
+ method: string,
+ _p: unknown,
+ options?: { beforeSend?: () => void | Promise<void> },
+ ) => {
current = false;
- options?.beforeSend?.();
+ await options?.beforeSend?.();
sent(method);
return { size: 1, mtimeMs: 1 };
},
@@ -342,8 +367,29 @@ describe('file-browser device-op', () => {
),
).rejects.toThrow('PERMISSION_DENIED');
expect(sent).not.toHaveBeenCalled();
- // Same-account controllers keep the unchanged three-argument call.
+ // The owner moves the task to another SSH host while the connection is built.
current = true;
+ sshRequestMock.mockImplementation(
+ async (
+ _h: string,
+ method: string,
+ _p: unknown,
+ options?: { beforeSend?: () => void | Promise<void> },
+ ) => {
+ taskHost = 'host-2';
+ await options?.beforeSend?.();
+ sent(method);
+ return { size: 1, mtimeMs: 1 };
+ },
+ );
+ await expect(
+ asGuest(() =>
+ handleRemoteOp({ op: 'writeFile', workdir: '/remote/proj', relPath: 'a.ts', content: 'x' }),
+ ),
+ ).rejects.toThrow('PERMISSION_DENIED');
+ expect(sent).not.toHaveBeenCalled();
+ // Same-account controllers keep the unchanged three-argument call.
+ sshRequestMock.mockReset();
sshRequestMock.mockResolvedValue({ size: 1, mtimeMs: 1 });
await handleRemoteOp({ op: 'writeFile', workdir: '/remote/proj', relPath: 'a.ts', content: 'x' });
expect(sshRequestMock).toHaveBeenLastCalledWith('host-1', 'writeFile', {
diff --git a/apps/desktop/src/main/file-browser/device-op.ts b/apps/desktop/src/main/file-browser/device-op.ts
index 1fd8dc265b..ea1a8bb186 100644
--- a/apps/desktop/src/main/file-browser/device-op.ts
+++ b/apps/desktop/src/main/file-browser/device-op.ts
@@ -432,7 +432,13 @@ async function handleRemoteOp(args: RemoteOpArgs): Promise<unknown> {
throwIpcError(rejection.code, rejection.message);
}
- const exec = await resolveWorkdirExecution(args.workdir, guardResult);
+ // 共享访客已从任务记录拿到权威端点,直接按它选择本地或 SSH 执行;只有同账号
+ // 请求才按路径反查全部会话(同一路径在本地与其它 SSH 会话并存时不误判歧义)。
+ const exec: WorkdirExecution = sharedTaskEndpoint
+ ? sharedTaskEndpoint.remoteHostId
+ ? { kind: 'ssh', hostId: sharedTaskEndpoint.remoteHostId }
+ : { kind: 'local' }
+ : await resolveWorkdirExecution(args.workdir, guardResult);
const workdir = args.workdir;
if (exec.kind === 'unavailable') {
@@ -479,12 +485,8 @@ async function handleRemoteOp(args: RemoteOpArgs): Promise<unknown> {
// 上面的 guard / 端点判定 / 解压都是 await 边界:房主可能已撤权或移动任务目录。
// 落盘或读出前按最新任务记录重新绑定目录与端点(内含成员资格复核)。
const current = await assertSharedTaskWorkdir(sharedTask, args.workdir, sharedTaskOperation!);
- // 访客只能落到任务自己的执行端点(本地或任务所属 SSH 主机),不按路径反查到别的会话主机。
- const expected = current.remoteHostId;
- const sameEndpoint =
- expected === sharedTaskEndpoint.remoteHostId &&
- (expected ? exec.kind === 'ssh' && exec.hostId === expected : exec.kind === 'local');
- if (!sameEndpoint)
+ // exec 取自请求开始时的任务端点;任务中途换了端点则拒绝,不落到旧端点。
+ if (current.remoteHostId !== sharedTaskEndpoint.remoteHostId)
throw new Error('[PERMISSION_DENIED] Shared task working directory endpoint mismatch');
}
@@ -492,16 +494,24 @@ async function handleRemoteOp(args: RemoteOpArgs): Promise<unknown> {
if (exec.kind === 'ssh') {
const mgr = getRemoteFileBrowser();
const hostId = exec.hostId;
- // 共享访客:SSH 建链等待期间可能被撤权,尚未发出的请求在发送前复核成员资格。
- const gate = sharedTask
- ? {
- beforeSend: () => {
- if (!sharedTask.isCurrent() || !sharedTask.authorize(sharedTaskOperation!)) {
- throw new Error('[PERMISSION_DENIED] Shared task access changed');
- }
- },
- }
- : undefined;
+ // 共享访客:SSH 建链可能等待数秒,期间房主可能撤权或移动任务目录。每次真正
+ // 发送前按最新任务记录重新绑定目录与端点(内含成员资格复核)。
+ const gate =
+ sharedTask && sharedTaskEndpoint
+ ? {
+ beforeSend: async () => {
+ const latest = await assertSharedTaskWorkdir(
+ sharedTask,
+ args.workdir,
+ sharedTaskOperation!,
+ );
+ if (latest.remoteHostId !== hostId)
+ throw new Error(
+ '[PERMISSION_DENIED] Shared task working directory endpoint mismatch',
+ );
+ },
+ }
+ : undefined;
const request = <M extends keyof FsRpcMethods>(method: M, params: FsRpcMethods[M]['params']) =>
gate ? mgr.request(hostId, method, params, gate) : mgr.request(hostId, method, params);
switch (args.op) {
diff --git a/apps/desktop/src/main/file-browser/remote.ts b/apps/desktop/src/main/file-browser/remote.ts
index 41b6bcfdef..2f92bd509e 100644
--- a/apps/desktop/src/main/file-browser/remote.ts
+++ b/apps/desktop/src/main/file-browser/remote.ts
@@ -121,19 +121,22 @@ export class RemoteFileBrowserManager {
/**
* 类型安全的远程调用入口。断链自动重建一次(仅幂等读方法);其余错误原样抛
* FileServiceRpcError(caller 决定 throwIpcError 还是 {ok:false})。
- * `beforeSend` 在建链等待之后、每次真正发出请求之前同步调用,抛错即不发送
- * (共享任务访客用它在建链期间被撤权时拦下尚未发出的修改)。
+ * `beforeSend` 在建链等待之后、每次真正发出请求之前调用(可异步),抛错即不发送
+ * (共享任务访客用它在建链期间重新绑定任务目录,拦下撤权或移动后尚未发出的请求)。
*/
async request<M extends keyof FsRpcMethods>(
hostId: string,
method: M,
params: FsRpcMethods[M]['params'],
- options?: { beforeSend?: () => void },
+ options?: { beforeSend?: () => void | Promise<void> },
): Promise<FsRpcMethods[M]['result']> {
const generation = this.currentEndpointGeneration(hostId);
const client = await this.getClient(hostId, generation);
this.assertCurrentEndpointGeneration(hostId, generation);
- options?.beforeSend?.();
+ if (options?.beforeSend) {
+ await options.beforeSend();
+ this.assertCurrentEndpointGeneration(hostId, generation);
+ }
try {
const result = await client.request(method, params);
this.assertCurrentEndpointGeneration(hostId, generation);
@@ -152,7 +155,10 @@ export class RemoteFileBrowserManager {
this.log.warn('file-service channel lost, rebuilding', { hostId, method });
this.clients.delete(hostId);
const rebuilt = await this.getClient(hostId, generation);
- options?.beforeSend?.();
+ if (options?.beforeSend) {
+ await options.beforeSend();
+ this.assertCurrentEndpointGeneration(hostId, generation);
+ }
const result = await rebuilt.request(method, params);
this.assertCurrentEndpointGeneration(hostId, generation);
return result;