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(agent): route workspace local runs to the requested personal device (#20889)

YuTengjingf6e5eceaa0f2
diff --git a/apps/server/src/services/aiAgent/__tests__/execAgent.deviceToolPipeline.test.ts b/apps/server/src/services/aiAgent/__tests__/execAgent.deviceToolPipeline.test.ts
index c208cabdd1a..b18555c1e35 100644
--- a/apps/server/src/services/aiAgent/__tests__/execAgent.deviceToolPipeline.test.ts
+++ b/apps/server/src/services/aiAgent/__tests__/execAgent.deviceToolPipeline.test.ts
@@ -758,6 +758,57 @@ describe('AiAgentService.execAgent - device tool pipeline ()', () => {
});
});
+ it('routes a workspace local run to the requested personal desktop without an override', async () => {
+ const workspaceId = 'ws-1';
+ service = new AiAgentService(mockDb, userId, { workspaceId });
+
+ const { deviceGateway } = await import('@/server/services/deviceGateway');
+ vi.spyOn(deviceGateway, 'isConfigured', 'get').mockReturnValue(true);
+
+ // Workspace pool holds another machine; the desktop sending the run is
+ // only enrolled in the caller's personal pool.
+ mockQueryWorkspaceDevices.mockResolvedValue([
+ {
+ deviceId: 'ws-dev-1',
+ friendlyName: null,
+ hostname: 'workspace-server',
+ lastSeenAt: new Date('2026-10-13T00:00:00.000Z'),
+ platform: 'linux',
+ },
+ ]);
+ mockQueryDeviceList.mockImplementation(async (_uid: string, wsId?: string) => {
+ if (wsId)
+ return [
+ { deviceId: 'ws-dev-1', hostname: 'workspace-server', online: true, platform: 'linux' },
+ ];
+ return [
+ {
+ deviceId: 'personal-dev-1',
+ hostname: 'personal-mac',
+ online: true,
+ platform: 'darwin',
+ },
+ ];
+ });
+ mockQueryDeviceSystemInfo.mockResolvedValue(systemInfoFixture);
+
+ // Stored `local` target with no per-user override (no boundDeviceId).
+ mockGetAgentConfig.mockResolvedValue(
+ createBaseAgentConfig({ agencyConfig: { executionTarget: 'local' } }),
+ );
+
+ await service.execAgent({
+ agentId: 'agent-1',
+ deviceId: 'personal-dev-1',
+ localDeviceId: 'personal-dev-1',
+ prompt: 'Hello',
+ });
+
+ const createOpArgs = mockCreateOperation.mock.calls[0][0];
+ expect(createOpArgs.activeDeviceId).toBe('personal-dev-1');
+ expect(createOpArgs.activeDeviceScope).toBe('personal');
+ });
+
it('should not fail createOperation when system info query returns null', async () => {
const { deviceGateway } = await import('@/server/services/deviceGateway');
vi.spyOn(deviceGateway, 'isConfigured', 'get').mockReturnValue(true);
diff --git a/apps/server/src/services/aiAgent/pipeline/toolDiscovery.ts b/apps/server/src/services/aiAgent/pipeline/toolDiscovery.ts
index 9a30465edf0..1c1967c58f0 100644
--- a/apps/server/src/services/aiAgent/pipeline/toolDiscovery.ts
+++ b/apps/server/src/services/aiAgent/pipeline/toolDiscovery.ts
@@ -525,30 +525,45 @@ export const discoverTools = async (
)
).filter((d) => d.online);
- // A workspace agent whose caller pinned this desktop's personal
- // deviceId via `users.preference.agentDeviceOverrides` (
- // the `local` code path in `useSelectExecutionTarget`) needs its
- // personal device to be visible in this run's device pool β€” otherwise
- // `resolveExecutionPlan` treats the bound device as offline and the
- // run stays unrouted. The workspace pool never includes personal
- // devices by design (`getScopedOnlineDevices` enforces the strict
- // scope), so union the specific personal device in here. The device
- // is dispatchable because the gateway routes it by
+ // A workspace run that targets one of the caller's PERSONAL devices
+ // needs that device visible in this run's pool β€” otherwise
+ // `resolveExecutionPlan` treats it as offline (`bound-device-offline`)
+ // and the run stays unrouted. The workspace pool never includes
+ // personal devices by design (`getScopedOnlineDevices` enforces the
+ // strict scope), so union the specific personal device in here. The
+ // device is dispatchable because the gateway routes it by
// `(userId, deviceId)` β€” the caller owns it.
- if (deps.workspaceId && agentConfig.agencyConfig?.boundDeviceId) {
- const boundId = agentConfig.agencyConfig.boundDeviceId;
- if (!devices.some((d) => d.deviceId === boundId)) {
- const personalPool = await getScopedOnlineDevices(deps.db, deps.userId).catch(
- () => [] as DeviceAttachment[],
+ //
+ // Candidates mirror the ids `resolveExecutionPlan` routes by:
+ // - `agencyConfig.boundDeviceId`: the per-user override written by the
+ // `local` code path in `useSelectExecutionTarget`;
+ // - `requestedDeviceId`: the desktop's own id sent with a `local` run
+ // (or the topic's pinned device, folded in by `turnSetup`). Without
+ // it, an agent whose stored target is `local` but which has no
+ // override is told the user's online desktop is offline. A `fixed`
+ // selection policy ignores it, as the resolver does.
+ const isFixedSelection = agentConfig.agencyConfig?.executionTargetSelectionPolicy === 'fixed';
+ const personalCandidateIds = [
+ ...new Set(
+ [
+ agentConfig.agencyConfig?.boundDeviceId,
+ isFixedSelection ? undefined : requestedDeviceId,
+ ].filter((id): id is string => !!id && !devices.some((d) => d.deviceId === id)),
+ ),
+ ];
+ if (deps.workspaceId && personalCandidateIds.length > 0) {
+ const personalPool = await getScopedOnlineDevices(deps.db, deps.userId).catch(
+ () => [] as DeviceAttachment[],
+ );
+ const personalMatches = personalPool.filter(
+ (d) => d.online && personalCandidateIds.includes(d.deviceId),
+ );
+ if (personalMatches.length > 0) {
+ devices = [...devices, ...personalMatches];
+ log(
+ 'execAgent: augmented device pool with caller personal device(s) %o',
+ personalMatches.map((d) => d.deviceId),
);
- const personalMatch = personalPool.find((d) => d.deviceId === boundId && d.online);
- if (personalMatch) {
- devices = [...devices, personalMatch];
- log(
- 'execAgent: augmented device pool with caller personal device %s (per-user override)',
- boundId,
- );
- }
}
}
log('execAgent: found %d online device(s)', devices.length);