Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(project): reject binding a working directory missing on its device (#22558)

Arvin Xu8a73747d95d2
diff --git a/apps/server/src/routers/lambda/__tests__/projectWorkingDirectory.bind.test.ts b/apps/server/src/routers/lambda/__tests__/projectWorkingDirectory.bind.test.ts
new file mode 100644
index 00000000000..2ae2245592c
--- /dev/null
+++ b/apps/server/src/routers/lambda/__tests__/projectWorkingDirectory.bind.test.ts
@@ -0,0 +1,98 @@
+// @vitest-environment node
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const mocks = vi.hoisted(() => ({
+ assertCanUseTopicTargets: vi.fn(),
+ bind: vi.fn(),
+ statPath: vi.fn(),
+}));
+
+vi.mock('@/database/core/db-adaptor', () => ({
+ getServerDB: vi.fn(function () {
+ return {};
+ }),
+}));
+
+vi.mock('@/business/server/trpc-middlewares/rbacPermission', () => ({
+ withScopedPermission: vi.fn(function () {
+ return (opts: any) => opts.next({ ctx: opts.ctx });
+ }),
+}));
+
+vi.mock('@/database/repositories/projectDirectory', () => ({
+ ProjectDirectoryRepository: vi.fn(function () {
+ return { bind: mocks.bind };
+ }),
+}));
+
+vi.mock('@/server/services/deviceGateway', () => ({
+ deviceGateway: { statPath: mocks.statPath },
+}));
+
+vi.mock('../_helpers/conversationResourceGuard', () => ({
+ assertCanUseConversationTargets: vi.fn(),
+ assertCanUseTopicTargets: mocks.assertCanUseTopicTargets,
+ assertCanViewTopicTargets: vi.fn(),
+}));
+
+const { projectWorkingDirectoryRouter } = await import('../projectWorkingDirectory');
+
+const caller = () =>
+ projectWorkingDirectoryRouter.createCaller({ userId: 'user-1', workspaceId: null } as never);
+const input = {
+ deviceId: 'device-m4',
+ name: 'develop-center',
+ path: '/Users/other/CodeProjects/develop-center',
+ projectId: 'project-1',
+ topicIds: ['topic-1'],
+};
+
+describe('projectWorkingDirectory.bind', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.bind.mockResolvedValue({ id: 'directory-1' });
+ });
+
+ it('rejects a path that does not exist on the device', async () => {
+ // Legacy topic metadata paired this device with another machine's path;
+ // binding it left every topic under it failing with working_directory_not_found.
+ mocks.statPath.mockResolvedValue({ exists: false, isDirectory: false });
+
+ await expect(caller().bind(input)).rejects.toThrow(
+ 'Working directory does not exist on this device',
+ );
+ expect(mocks.statPath).toHaveBeenCalledWith({
+ deviceId: 'device-m4',
+ path: input.path,
+ userId: 'user-1',
+ workspaceId: undefined,
+ });
+ expect(mocks.bind).not.toHaveBeenCalled();
+ });
+
+ it('rejects a path that is a file', async () => {
+ mocks.statPath.mockResolvedValue({ exists: true, isDirectory: false });
+
+ await expect(caller().bind(input)).rejects.toThrow(
+ 'Working directory does not exist on this device',
+ );
+ expect(mocks.bind).not.toHaveBeenCalled();
+ });
+
+ it('binds a directory that exists on the device', async () => {
+ mocks.statPath.mockResolvedValue({ exists: true, isDirectory: true });
+
+ await expect(caller().bind(input)).resolves.toEqual({
+ data: { id: 'directory-1' },
+ success: true,
+ });
+ expect(mocks.bind).toHaveBeenCalledWith(input);
+ });
+
+ it('still binds when the device cannot be reached to verify', async () => {
+ mocks.statPath.mockResolvedValue(undefined);
+
+ await expect(caller().bind(input)).resolves.toMatchObject({ success: true });
+ expect(mocks.bind).toHaveBeenCalledWith(input);
+ });
+});
diff --git a/apps/server/src/routers/lambda/projectWorkingDirectory.ts b/apps/server/src/routers/lambda/projectWorkingDirectory.ts
index eb29d21ddb6..908413a94a6 100644
--- a/apps/server/src/routers/lambda/projectWorkingDirectory.ts
+++ b/apps/server/src/routers/lambda/projectWorkingDirectory.ts
@@ -214,6 +214,22 @@ export const projectWorkingDirectoryRouter = router({
{ db: ctx.serverDB, userId: ctx.userId, workspaceId: ctx.workspaceId },
input.topicIds,
);
+ // A binding pairs a device with a path on that device. Legacy topic
+ // metadata can pair one machine's device with another machine's path, and
+ // persisting that leaves every topic under it failing with
+ // `working_directory_not_found`. An unreachable device can't be verified
+ // and stays bindable; only a definite "not there" is rejected.
+ const stat = await deviceGateway.statPath({
+ deviceId: input.deviceId,
+ path: input.path,
+ userId: ctx.userId,
+ workspaceId: ctx.workspaceId ?? undefined,
+ });
+ if (stat && (!stat.exists || !stat.isDirectory))
+ throw new TRPCError({
+ code: 'PRECONDITION_FAILED',
+ message: 'Working directory does not exist on this device',
+ });
return { data: await ctx.directoryRepo.bind(input), success: true };
}),
list: procedure