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(desktop): preserve operation tokens for gateway agent runs (#21812)

AmAzing-12360f696543
diff --git a/apps/desktop/src/main/controllers/GatewayConnectionCtr.ts b/apps/desktop/src/main/controllers/GatewayConnectionCtr.ts
index 5acc9497c53..ef21227bf5a 100644
--- a/apps/desktop/src/main/controllers/GatewayConnectionCtr.ts
+++ b/apps/desktop/src/main/controllers/GatewayConnectionCtr.ts
@@ -214,7 +214,6 @@ export default class GatewayConnectionCtr extends ControllerModule {
// Wire up agent run handler
srv.setAgentRunHandler((request) =>
executeGatewayAgentRun(request, {
- getAccessToken: () => this.remoteServerConfigCtr.getAccessToken(),
getServerUrl: () => this.remoteServerConfigCtr.getRemoteServerUrl(),
heterogeneousAgentCtr: this.heterogeneousAgentCtr,
platformTasks: this.platformTasks,
diff --git a/apps/desktop/src/main/controllers/__tests__/GatewayConnectionCtr.test.ts b/apps/desktop/src/main/controllers/__tests__/GatewayConnectionCtr.test.ts
index 5cf6fc99f76..ac70ef40709 100644
--- a/apps/desktop/src/main/controllers/__tests__/GatewayConnectionCtr.test.ts
+++ b/apps/desktop/src/main/controllers/__tests__/GatewayConnectionCtr.test.ts
@@ -1711,9 +1711,7 @@ describe('GatewayConnectionCtr', () => {
expect(mockHeterogeneousAgentCtr.spawnLhHeteroExec).toHaveBeenCalledWith(
expect.objectContaining({
agentType: 'openclaw',
- // Reuses the device's own session token as the run identity, not the
- // dispatched operation jwt.
- jwt: 'mock-access-token',
+ jwt: 'mock-jwt',
operationId: 'op-xyz',
prompt: 'hello',
serverUrl: 'https://server.example.com',
@@ -1722,33 +1720,30 @@ describe('GatewayConnectionCtr', () => {
);
});
- it('reuses the device access token as the run jwt instead of request.jwt', async () => {
- const client = await connectAndOpen();
- client.simulateAgentRunRequest('claude-code', 'op-auth', 'hi', 'dispatched-operation-jwt');
- await vi.advanceTimersByTimeAsync(0);
-
- expect(mockHeterogeneousAgentCtr.spawnLhHeteroExec).toHaveBeenCalledWith(
- expect.objectContaining({ jwt: 'mock-access-token' }),
- );
- });
-
- it('falls back to request.jwt when the device has no access token', async () => {
- const client = await connectAndOpen();
- // Set after connect so the auto-connect getAccessToken call isn't the one
- // that returns null β€” only the executeAgentRun lookup should see no token.
- vi.mocked(mockRemoteServerConfigCtr.getAccessToken).mockResolvedValueOnce(null);
- client.simulateAgentRunRequest(
- 'claude-code',
- 'op-fallback',
- 'hi',
- 'dispatched-operation-jwt',
- );
- await vi.advanceTimersByTimeAsync(0);
-
- expect(mockHeterogeneousAgentCtr.spawnLhHeteroExec).toHaveBeenCalledWith(
- expect.objectContaining({ jwt: 'dispatched-operation-jwt' }),
- );
- });
+ describe.each(['pi', 'codex', 'claude-code', 'openclaw', 'hermes'])(
+ '%s run identity',
+ (agentType) => {
+ it.each(['same-account-login', 'another-account-login', null])(
+ 'preserves the operation token with device login %s',
+ async (accessToken) => {
+ const client = await connectAndOpen();
+ const getAccessToken = vi.mocked(mockRemoteServerConfigCtr.getAccessToken);
+ getAccessToken.mockClear().mockResolvedValue(accessToken);
+ client.simulateAgentRunRequest(agentType, 'op-auth', 'hi', 'dispatched-operation-jwt');
+ await vi.advanceTimersByTimeAsync(0);
+
+ expect(client.sendAgentRunAck).toHaveBeenCalledWith({
+ operationId: 'op-auth',
+ status: 'accepted',
+ });
+ expect(mockHeterogeneousAgentCtr.spawnLhHeteroExec).toHaveBeenCalledWith(
+ expect.objectContaining({ jwt: 'dispatched-operation-jwt' }),
+ );
+ expect(getAccessToken).not.toHaveBeenCalled();
+ },
+ );
+ },
+ );
it('sends rejected ack when remote server URL is not configured', async () => {
const client = await connectAndOpen();
diff --git a/apps/desktop/src/main/controllers/__tests__/gatewayAgentRun.test.ts b/apps/desktop/src/main/controllers/__tests__/gatewayAgentRun.test.ts
index 8cb6ef6af37..e6dbb78dd23 100644
--- a/apps/desktop/src/main/controllers/__tests__/gatewayAgentRun.test.ts
+++ b/apps/desktop/src/main/controllers/__tests__/gatewayAgentRun.test.ts
@@ -4,7 +4,6 @@ import { executeGatewayAgentRun, type GatewayAgentRunDeps } from '../gatewayAgen
const createDeps = (ctr: Partial<GatewayAgentRunDeps['heterogeneousAgentCtr']> = {}) =>
({
- getAccessToken: vi.fn().mockResolvedValue('access-token'),
getServerUrl: vi.fn().mockResolvedValue('https://app.lobehub.com'),
heterogeneousAgentCtr: {
getClaudeCodeInheritedRoutingEnv: vi.fn().mockResolvedValue({}),
@@ -27,17 +26,30 @@ const request = (extra: Record<string, unknown> = {}) =>
...extra,
}) as Parameters<typeof executeGatewayAgentRun>[0];
-describe('executeGatewayAgentRun Claude Code account routing', () => {
+describe('executeGatewayAgentRun', () => {
it('spawns an unrouted run without reading the local logins', async () => {
const deps = createDeps();
expect(await executeGatewayAgentRun(request(), deps)).toEqual({ status: 'accepted' });
expect(deps.heterogeneousAgentCtr.listClaudeCodeAccounts).not.toHaveBeenCalled();
expect(deps.heterogeneousAgentCtr.spawnLhHeteroExec).toHaveBeenCalledWith(
- expect.objectContaining({ accountEnv: {}, jwt: 'access-token' }),
+ expect.objectContaining({ accountEnv: {}, jwt: 'dispatch-jwt' }),
);
});
+ it.each([undefined, '', ' '])(
+ 'rejects a missing/blank dispatch token (%j) without launching',
+ async (jwt) => {
+ const deps = createDeps();
+
+ expect(await executeGatewayAgentRun(request({ jwt }), deps)).toEqual({
+ reason: 'Missing operation token',
+ status: 'rejected',
+ });
+ expect(deps.heterogeneousAgentCtr.spawnLhHeteroExec).not.toHaveBeenCalled();
+ },
+ );
+
it('spawns a routed run on the profile of its account', async () => {
const deps = createDeps({
listClaudeCodeAccounts: vi.fn().mockResolvedValue([
diff --git a/apps/desktop/src/main/controllers/gatewayAgentRun.ts b/apps/desktop/src/main/controllers/gatewayAgentRun.ts
index 7cf4726e9bb..af456165ffa 100644
--- a/apps/desktop/src/main/controllers/gatewayAgentRun.ts
+++ b/apps/desktop/src/main/controllers/gatewayAgentRun.ts
@@ -20,7 +20,6 @@ export interface PlatformTaskEntry {
}
export interface GatewayAgentRunDeps {
- getAccessToken: () => Promise<string | null | undefined>;
getServerUrl: () => Promise<string | undefined>;
heterogeneousAgentCtr: Pick<
HeterogeneousAgentCtr,
@@ -69,19 +68,13 @@ export const executeGatewayAgentRun = async (
return { reason: 'Remote server URL not configured', status: 'rejected' };
}
- // Reuse this device's own logged-in session as the run identity. The
- // access token is a full user OIDC token (7-day TTL, longer than any run),
- // which heteroIngest/heteroFinish now accept (ownership-gated), AND which
- // gives the spawned Claude Code's nested `lh` calls a real login state β€”
- // unlike the narrow `hetero-operation` token, which only works for the
- // ingest endpoints. We deliberately do NOT pass the refresh token to the
- // CLI: the device stays the single refresher (refresh tokens rotate), and
- // the 7-day access token outlives the run so no mid-run refresh is needed.
- //
- // Fall back to the dispatched `request.jwt` when the device has no access
- // token (e.g. not logged in), preserving the prior behavior gracefully.
- const accessToken = await deps.getAccessToken();
- const jwt = accessToken || request.jwt;
+ // Keep the dispatched operation identity: channel history and other
+ // operation-scoped APIs require it. The device's login authenticates the
+ // gateway connection, not the run, and may belong to a different user.
+ // An empty token would let the CLI fall back to its stored login instead.
+ if (!request.jwt?.trim()) {
+ return { reason: 'Missing operation token', status: 'rejected' };
+ }
const account = await resolveClaudeAccountEnv(heterogeneousAgentCtr, request);
if (account.kind === 'reject') return { reason: account.reason, status: 'rejected' };
@@ -98,7 +91,7 @@ export const executeGatewayAgentRun = async (
args: request.args,
cwd: request.cwd,
imageList: request.imageList,
- jwt,
+ jwt: request.jwt,
operationId: request.operationId,
prompt: request.prompt,
resumeFallbackSystemContext: request.resumeFallbackSystemContext,