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: gateway sync (#17118)

Rdmclin26d305917480e
diff --git a/apps/server/src/services/gateway/MessageGatewayClient.ts b/apps/server/src/services/gateway/MessageGatewayClient.ts
index b6c23594ab9..92a64d95c40 100644
--- a/apps/server/src/services/gateway/MessageGatewayClient.ts
+++ b/apps/server/src/services/gateway/MessageGatewayClient.ts
@@ -80,10 +80,20 @@ export class MessageGatewayClient {
// ─── Connection Management ───
- async connect(config: MessageGatewayConnectionConfig): Promise<{ status: string }> {
+ /**
+ * `ensure: true` marks a reconcile-driven connect (periodic sync): when the
+ * gateway DO already holds an identical config it preserves its park/backoff
+ * state instead of resetting it β€” a parked connection answers 409. Omit for
+ * user-driven connects (credential saves, manual reconnect), which must
+ * always get a full fresh lifecycle.
+ */
+ async connect(
+ config: MessageGatewayConnectionConfig,
+ options?: { ensure?: boolean },
+ ): Promise<{ status: string }> {
log('Connecting %s:%s (platform=%s)', config.connectionId, config.userId, config.platform);
- const res = await this.post('/api/connections', { config });
+ const res = await this.post('/api/connections', { config, ensure: options?.ensure });
if (!res.ok) {
const error = await res.text();
diff --git a/apps/server/src/services/gateway/__tests__/GatewayService.test.ts b/apps/server/src/services/gateway/__tests__/GatewayService.test.ts
index 3a684b2cd82..e663b45d9be 100644
--- a/apps/server/src/services/gateway/__tests__/GatewayService.test.ts
+++ b/apps/server/src/services/gateway/__tests__/GatewayService.test.ts
@@ -335,6 +335,7 @@ describe('GatewayService', () => {
connectionId: 'prov-1',
platform: 'discord',
}),
+ { ensure: true },
);
expect(mockUpdateBotRuntimeStatus).toHaveBeenCalledWith(
expect.objectContaining({ status: 'starting' }),
@@ -766,6 +767,7 @@ describe('GatewayService', () => {
expect(mockGatewayClient.getStatus).not.toHaveBeenCalled();
expect(mockGatewayClient.connect).toHaveBeenCalledWith(
expect.objectContaining({ connectionId: 'prov-1', platform: 'discord' }),
+ { ensure: true },
);
});
diff --git a/apps/server/src/services/gateway/index.ts b/apps/server/src/services/gateway/index.ts
index 29f6e88fd4c..ace7d967908 100644
--- a/apps/server/src/services/gateway/index.ts
+++ b/apps/server/src/services/gateway/index.ts
@@ -232,15 +232,22 @@ export class GatewayService {
}
const webhookPath = `/api/agent/webhooks/${platform}/${provider.applicationId}`;
- const result = await client.connect({
- applicationId: provider.applicationId,
- connectionId: provider.id,
- connectionMode,
- credentials: provider.credentials,
- platform,
- userId: provider.userId,
- webhookPath,
- });
+ // `ensure` marks this as a reconcile connect: the gateway preserves
+ // its park/backoff state when the config is unchanged (a parked
+ // connection answers 409 β†’ counted as failed below), instead of
+ // letting every sync round reset stuck connections to fast retry.
+ const result = await client.connect(
+ {
+ applicationId: provider.applicationId,
+ connectionId: provider.id,
+ connectionMode,
+ credentials: provider.credentials,
+ platform,
+ userId: provider.userId,
+ webhookPath,
+ },
+ { ensure: true },
+ );
// Gateway returns "connecting" for async persistent connections
// (e.g. Discord WebSocket), "connected" for sync webhook-mode.