π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(device): remove an online managed workspace device without an unenroll ack (#22529)
Neko682f60153009
diff --git a/apps/server/src/routers/lambda/device/remove.managedDevice.test.ts b/apps/server/src/routers/lambda/device/remove.managedDevice.test.tsindex 314b5ba5aaa..e6ccad8906d 100644--- a/apps/server/src/routers/lambda/device/remove.managedDevice.test.ts+++ b/apps/server/src/routers/lambda/device/remove.managedDevice.test.ts@@ -11,6 +11,8 @@ const mocks = vi.hoisted(() => ({findWorkspaceDeviceById: vi.fn(),hasFixedAgentBinding: vi.fn(),onManagedDeviceRemoved: vi.fn(),+ queryDeviceList: vi.fn(),+ unenrollWorkspace: vi.fn(),}));vi.mock('@/business/server/managedDevice', () => ({@@ -38,8 +40,8 @@ vi.mock('@/database/models/device', () => ({}));vi.mock('@/server/services/deviceGateway', () => ({deviceGateway: {- queryDeviceList: vi.fn().mockResolvedValue([]),- unenrollWorkspace: vi.fn().mockResolvedValue({ success: true }),+ queryDeviceList: mocks.queryDeviceList,+ unenrollWorkspace: mocks.unenrollWorkspace,},}));@@ -64,6 +66,8 @@ describe('removing a managed cloud device', () => {mocks.onManagedDeviceRemoved.mockResolvedValue(undefined);mocks.findWorkspaceDeviceById.mockResolvedValue(row('machine-id', 'workspace-1'));mocks.hasFixedAgentBinding.mockResolvedValue(false);+ mocks.queryDeviceList.mockResolvedValue([]);+ mocks.unenrollWorkspace.mockResolvedValue({ success: true });});it('reports a removed personal managed device after deleting it', async () => {@@ -155,4 +159,49 @@ describe('removing a managed cloud device', () => {expect(consoleError).toHaveBeenCalledOnce();consoleError.mockRestore();});++ describe('while the device is still connected', () => {+ beforeEach(() => {+ mocks.unenrollWorkspace.mockResolvedValue({ error: 'no handler', success: false });+ mocks.queryDeviceList.mockResolvedValue([{ deviceId: 'device-1' }]);+ });++ it('removes an online workspace managed device without an unenroll acknowledgement', async () => {+ const managed = row(MANAGED_CLOUD_DEVICE_IDENTITY_SOURCE, 'workspace-1');+ mocks.findWorkspaceDeviceById.mockResolvedValue(managed);+ mocks.deleteWorkspaceDevice.mockResolvedValue([managed]);++ await expect(+ workspaceCaller().removeWorkspaceDevice({ deviceId: 'device-1' }),+ ).resolves.toEqual({ success: true });++ expect(mocks.deleteWorkspaceDevice).toHaveBeenCalledWith('device-1');+ expect(mocks.onManagedDeviceRemoved).toHaveBeenCalledExactlyOnceWith({+ deviceId: 'device-1',+ userId: 'user-1',+ workspaceId: 'workspace-1',+ });+ });++ it('refuses to remove an online ordinary device that did not acknowledge the unenroll', async () => {+ await expect(+ workspaceCaller().removeWorkspaceDevice({ deviceId: 'device-1' }),+ ).rejects.toMatchObject({ code: 'PRECONDITION_FAILED' });++ expect(mocks.unenrollWorkspace).toHaveBeenCalledOnce();+ expect(mocks.deleteWorkspaceDevice).not.toHaveBeenCalled();+ expect(mocks.onManagedDeviceRemoved).not.toHaveBeenCalled();+ });++ it('removes an ordinary device once it is offline', async () => {+ mocks.queryDeviceList.mockResolvedValue([]);+ mocks.deleteWorkspaceDevice.mockResolvedValue([row('machine-id', 'workspace-1')]);++ await expect(+ workspaceCaller().removeWorkspaceDevice({ deviceId: 'device-1' }),+ ).resolves.toEqual({ success: true });++ expect(mocks.deleteWorkspaceDevice).toHaveBeenCalledWith('device-1');+ });+ });});diff --git a/apps/server/src/routers/lambda/device/workspace.ts b/apps/server/src/routers/lambda/device/workspace.tsindex 9363053a8a3..bd9bc76c4c0 100644--- a/apps/server/src/routers/lambda/device/workspace.ts+++ b/apps/server/src/routers/lambda/device/workspace.ts@@ -1,4 +1,4 @@-import { workingDirConfigSchema } from '@lobechat/types';+import { isManagedCloudDevice, workingDirConfigSchema } from '@lobechat/types';import { TRPCError } from '@trpc/server';import { z } from 'zod';@@ -359,30 +359,36 @@ export const workspaceRouter = router({});}await assertDeviceNotBoundToFixedAgent(model, input.deviceId);- // Tell a live device to drop its workspace connection and stop- // auto-reconnecting, so removal doesn't leave an online ghost. For most- // rows this stays best-effort β an offline device simply stops resolving.- const unenrolled = await deviceGateway.unenrollWorkspace({- deviceId: input.deviceId,- userId: ctx.userId,- workspaceId: ctx.workspaceId,- });- // But never delete the row under a still-live socket that did not- // acknowledge the unenroll (old client without the handler, RPC error):- // for a PRIVATE row that would resurface the machine to other members- // (the row is the only thing `queryWorkspaceHiddenDeviceIds` hides it- // by), and for ANY row it leaves an unregistered ghost that members can- // still dispatch to yet can no longer remove β there is no row left to- // delete. Fail the removal while the socket is demonstrably alive; a- // dead socket can't ghost, so an offline device still deletes fine.- if (!unenrolled.success) {- const online = await deviceGateway.queryDeviceList(ctx.userId, ctx.workspaceId);- if (online.some((d) => d.deviceId === input.deviceId)) {- throw new TRPCError({- code: 'PRECONDITION_FAILED',- message:- 'The device is still connected but did not acknowledge the unenroll β retry once it has disconnected or updated its client.',- });+ // A managed cloud device skips the unenroll handshake. Its agent does+ // not handle the unenroll RPC, so it would never acknowledge. Deleting+ // the row reports the removal to the deployment, which retires the+ // device and its credentials, so it cannot reconnect as a ghost.+ if (!isManagedCloudDevice(row)) {+ // Tell a live device to drop its workspace connection and stop+ // auto-reconnecting, so removal doesn't leave an online ghost. For most+ // rows this stays best-effort β an offline device simply stops resolving.+ const unenrolled = await deviceGateway.unenrollWorkspace({+ deviceId: input.deviceId,+ userId: ctx.userId,+ workspaceId: ctx.workspaceId,+ });+ // But never delete the row under a still-live socket that did not+ // acknowledge the unenroll (old client without the handler, RPC error):+ // for a PRIVATE row that would resurface the machine to other members+ // (the row is the only thing `queryWorkspaceHiddenDeviceIds` hides it+ // by), and for ANY row it leaves an unregistered ghost that members can+ // still dispatch to yet can no longer remove β there is no row left to+ // delete. Fail the removal while the socket is demonstrably alive; a+ // dead socket can't ghost, so an offline device still deletes fine.+ if (!unenrolled.success) {+ const online = await deviceGateway.queryDeviceList(ctx.userId, ctx.workspaceId);+ if (online.some((d) => d.deviceId === input.deviceId)) {+ throw new TRPCError({+ code: 'PRECONDITION_FAILED',+ message:+ 'The device is still connected but did not acknowledge the unenroll β retry once it has disconnected or updated its client.',+ });+ }}}const removed = await model.deleteWorkspaceDevice(input.deviceId);