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(chat): normalize reconnect startTime to epoch ms (#15811)

Arvin Xu9f1ab922422f
diff --git a/src/store/chat/slices/aiChat/actions/__tests__/gateway.test.ts b/src/store/chat/slices/aiChat/actions/__tests__/gateway.test.ts
index 22811bd940e..395005dac2d 100644
--- a/src/store/chat/slices/aiChat/actions/__tests__/gateway.test.ts
+++ b/src/store/chat/slices/aiChat/actions/__tests__/gateway.test.ts
@@ -858,4 +858,93 @@ describe('GatewayActionImpl', () => {
});
});
});
+
+ describe('reconnectToGatewayOperation', () => {
+ function createReconnectTestAction(assistantMessage: any) {
+ const startOperation = vi.fn(() => ({ operationId: 'gw-op-reconnect' }));
+ const mockClient = createMockClient();
+ const state: Record<string, any> = {
+ activeAgentId: 'agent-1',
+ gatewayConnections: {},
+ messagesMap: { 'agent-1_topic-1': assistantMessage ? [assistantMessage] : [] },
+ // getTopicById reads here; an empty map yields no running op so the
+ // reconnect guards fall through to startOperation.
+ topicDataMap: {},
+ };
+ const set = vi.fn((updater: any) => {
+ if (typeof updater === 'function') Object.assign(state, updater(state));
+ else Object.assign(state, updater);
+ });
+ const get = vi.fn(() => ({
+ ...state,
+ associateMessageWithOperation: vi.fn(),
+ connectToGateway: vi.fn(),
+ internal_updateTopicLoading: vi.fn(),
+ onOperationCancel: vi.fn(),
+ startOperation,
+ })) as any;
+
+ (globalThis as any).window = {
+ global_serverConfigStore: {
+ getState: () => ({ serverConfig: { agentGatewayUrl: 'https://gateway.test.com' } }),
+ },
+ };
+
+ vi.mocked(aiAgentService.refreshGatewayToken).mockResolvedValue({
+ token: 'fresh-token',
+ } as any);
+
+ const action = new GatewayActionImpl(set as any, get, undefined);
+ action.createClient = vi.fn(() => mockClient);
+
+ return { action, startOperation };
+ }
+
+ afterEach(() => {
+ delete (globalThis as any).window;
+ });
+
+ // After a DB rehydrate (e.g. quit + relaunch), `createdAt` can arrive as an
+ // ISO string instead of epoch ms. Anchoring startTime to it raw makes
+ // `Date.now() - startTime` evaluate to NaN, which the elapsed-time label
+ // renders as "NaN:NaN". The reconnect path must normalize it to a number.
+ it('normalizes an ISO-string createdAt into an epoch-ms startTime', async () => {
+ const createdAtMs = 1_700_000_000_000;
+ const { action, startOperation } = createReconnectTestAction({
+ createdAt: new Date(createdAtMs).toISOString(),
+ id: 'ast-1',
+ });
+
+ await action.reconnectToGatewayOperation({
+ assistantMessageId: 'ast-1',
+ operationId: 'server-op-1',
+ topicId: 'topic-1',
+ });
+
+ expect(startOperation).toHaveBeenCalledWith(
+ expect.objectContaining({
+ metadata: expect.objectContaining({ startTime: createdAtMs }),
+ }),
+ );
+ });
+
+ it('omits startTime when createdAt is not a parseable date', async () => {
+ const { action, startOperation } = createReconnectTestAction({
+ createdAt: 'not-a-date',
+ id: 'ast-1',
+ });
+
+ await action.reconnectToGatewayOperation({
+ assistantMessageId: 'ast-1',
+ operationId: 'server-op-1',
+ topicId: 'topic-1',
+ });
+
+ expect(startOperation).toHaveBeenCalledWith(
+ expect.objectContaining({
+ metadata: expect.not.objectContaining({ startTime: expect.anything() }),
+ }),
+ );
+ });
+ });
});
diff --git a/src/store/chat/slices/aiChat/actions/gateway.ts b/src/store/chat/slices/aiChat/actions/gateway.ts
index e6ecc40e4d9..cd4f2b8afe1 100644
--- a/src/store/chat/slices/aiChat/actions/gateway.ts
+++ b/src/store/chat/slices/aiChat/actions/gateway.ts
@@ -604,13 +604,22 @@ export class GatewayActionImpl {
.flat()
.find((m) => m.id === assistantMessageId);
+ // `createdAt` is typed as a number but, after a DB rehydrate, it can arrive
+ // as a Date / ISO string (the message service casts rows `as unknown` without
+ // converting). Normalize to epoch ms here so the elapsed-time math stays a
+ // number — passing a string/Invalid Date straight through makes
+ // `Date.now() - startTime` resolve to NaN and renders as "NaN:NaN".
+ const startTime = assistantMessage?.createdAt
+ ? new Date(assistantMessage.createdAt).getTime()
+ : undefined;
+
// Create a local operation for UI loading state, stashing the server op id
// so intervention flows can find it after reconnect as well.
const { operationId: gatewayOpId } = this.#get().startOperation({
context,
metadata: {
serverOperationId: operationId,
- ...(assistantMessage?.createdAt ? { startTime: assistantMessage.createdAt } : {}),
+ ...(Number.isFinite(startTime) ? { startTime } : {}),
},
type: 'execServerAgentRuntime',
});