Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(sessions): align card timestamps and prevent message echo reordering

fmfsaisaifb6a3b1acc6d
diff --git a/apps/desktop/src/renderer/__tests__/makerChatStoreTextDeltaBatching.test.ts b/apps/desktop/src/renderer/__tests__/makerChatStoreTextDeltaBatching.test.ts
index 04c0adeadc..fa74f73748 100644
--- a/apps/desktop/src/renderer/__tests__/makerChatStoreTextDeltaBatching.test.ts
+++ b/apps/desktop/src/renderer/__tests__/makerChatStoreTextDeltaBatching.test.ts
@@ -2487,7 +2487,7 @@ describe('makerChatStore text delta batching', () => {
vi.advanceTimersByTime(32);
expect(notifyCount).toBe(1);
- expect(sessionsBus.emitPatch).toHaveBeenCalledTimes(1);
+ expect(sessionsBus.emitPatch).not.toHaveBeenCalled();
unsubscribe();
});
@@ -2546,7 +2546,25 @@ describe('makerChatStore text delta batching', () => {
}
});
- it('does not starve a Map-only sidebar patch behind 8 continuously active sessions', () => {
+ it.each(['user', 'assistant'] as const)(
+ 'does not invent a sorting timestamp for a persisted %s message',
+ (role) => {
+ vi.mocked(sessionsBus.emitPatch).mockClear();
+ emitDbMessageCreated({
+ clientId: 'goal-round-message',
+ role,
+ content: 'goal intermediate round',
+ createdAt: '2026-10-24T12:00:00.000Z',
+ });
+ vi.runOnlyPendingTimers();
+ expect(makerChatStore.getSnapshot(SESSION_ID).messages).toEqual([
+ expect.objectContaining({ clientId: 'goal-round-message', role }),
+ ]);
+ expect(sessionsBus.emitPatch).not.toHaveBeenCalled();
+ },
+ );
+
+ it('does not advance sidebar activity for repeated tool DB echoes during streaming', () => {
const patchOnlySessionId = MULTI_SESSION_IDS[8];
onEvent?.({
sessionId: patchOnlySessionId,
@@ -2582,7 +2600,6 @@ describe('makerChatStore text delta batching', () => {
},
});
}
- // 完全相同的 DB echo 让 setState 走 no-op,只剩 sidebar patch 待发。
emitDbMessageCreated(persistedTool, patchOnlySessionId);
vi.advanceTimersByTime(32);
@@ -2600,10 +2617,10 @@ describe('makerChatStore text delta batching', () => {
}
vi.advanceTimersByTime(32);
- expect(sessionsBus.emitPatch).toHaveBeenCalledTimes(1);
- expect(sessionsBus.emitPatch).toHaveBeenCalledWith(patchOnlySessionId, {
- updatedAt: expect.any(String),
- });
+ expect(sessionsBus.emitPatch).not.toHaveBeenCalled();
+ expect(makerChatStore.getSnapshot(patchOnlySessionId).messages).toEqual([
+ expect.objectContaining({ clientId: 'fair-tool-row', role: 'tool_use' }),
+ ]);
});
it('flushes pending text before a closed status finalizes the session', () => {
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionCard.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionCard.tsx
index bd56a94870..3b21787966 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionCard.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionCard.tsx
@@ -1,5 +1,6 @@
import { SessionTaskMenu } from './SessionTaskMenu';
import { isSharedTaskPeer } from '@cindy/device-link';
+import { getSessionListActivityAt } from '@cindy/maker-shared/session-activity';
import { TaskTagMenuSection, TaskTagEditor, TaskTagDots } from '@/features/task-tags/TaskTags';
import { Button } from '@/components/ui/button';
/**
@@ -204,7 +205,7 @@ export const SessionCard = withSidebarNavigation<SessionCardProps>(function Sess
const rightStatusKind = resolveSidebarRightStatus(sessionActivity);
const isPinned = session.pinnedAt != null;
const isEmpty = isEmptyDraftSession(session);
- const activityIso = session.updatedAt;
+ const activityIso = getSessionListActivityAt(session);
const remoteIconKind = session.deviceLinkDeviceId
? 'device-link'
: session.remoteHostId
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionInfoMeta.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionInfoMeta.tsx
index e416ef34c3..bd8ad03512 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionInfoMeta.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/SessionInfoMeta.tsx
@@ -21,8 +21,8 @@
* 无单位后缀(与费用的货币前缀天然区分);0 视为无数据不显示。
* - cost:优先 totalMoney(区域币种 $/¥),回退 legacy totalCostUsd;
* 两者都为 0 / 缺失(如订阅模式)不显示。
- * - time:与现状时间槽同一时间轴(activityIso 由调用方传入,SessionItem 用
- * session.updatedAt,SessionCard 同)。
+ * - time:与列表排序同一时间轴,调用方通过 getSessionListActivityAt 依次取
+ * lastActivityAt、userSendAt、updatedAt。
*
* 远程会话:token / 费用字段在远端 DB,device-link 投影可能缺失或为 0——按
* 「无数据不显示」自然降级,不误显示 0(设计文档 §9.6:第一期仅本机会话有值)。
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/SessionCard.visual.test.ts b/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/SessionCard.visual.test.ts
index 5364540baf..0822066aa3 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/SessionCard.visual.test.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/__tests__/SessionCard.visual.test.ts
@@ -25,6 +25,7 @@ import {
} from '@/features/device-link/remoteSessionActivityStore';
import { SessionCard } from '../SessionCard';
+import type { Session } from '@/lib/ccAgent.types';
import { SessionItem } from '../SessionItem';
import { sessionCardVisualCases } from '../__fixtures__/sessionCardVisualCases';
import { SPLIT_GROUP_SESSION_MIME } from '../../splitGroupDnd';
@@ -268,6 +269,25 @@ describe('SessionCard visual cases', () => {
}
});
+ it.each(['list', 'card'] as const)(
+ 'shows the sorting timestamp in the %s row with legacy fallback',
+ (variant) => {
+ const session: Session = {
+ ...sessionCardVisualCases[0].session,
+ updatedAt: '2026-10-24T09:00:00.000Z',
+ userSendAt: '2026-10-24T08:00:00.000Z',
+ lastActivityAt: '2026-10-24T10:00:00.000Z',
+ };
+ for (const expected of [session.lastActivityAt, session.userSendAt, session.updatedAt]) {
+ const view = renderCase(sessionCardVisualCases[0].id, { session, variant });
+ expect(view.container.querySelector('time')?.getAttribute('dateTime')).toBe(expected);
+ view.unmount();
+ if (session.lastActivityAt) delete session.lastActivityAt;
+ else session.userSendAt = null;
+ }
+ },
+ );
+
it('scrolls a text row into nearest view only when it becomes active', () => {
const props = {
session: sessionCardVisualCases[0].session,
diff --git a/apps/desktop/src/renderer/lib/makerChatStore.ts b/apps/desktop/src/renderer/lib/makerChatStore.ts
index 34f3ec21a7..2a2906bdf6 100644
--- a/apps/desktop/src/renderer/lib/makerChatStore.ts
+++ b/apps/desktop/src/renderer/lib/makerChatStore.ts
@@ -2304,7 +2304,6 @@ export function invalidateLiveIngressForDataOwnerBoundary(): void {
pendingTextDeltaBatches.clear();
clearDeferredStateNotificationTimer();
pendingDeferredStateNotifications.clear();
- pendingMessageCreatedPatches.clear();
remotePresenceOnlineByDevice.clear();
resetRemoteDataOwnerPushFence();
}
@@ -3300,11 +3299,10 @@ const HIGH_FREQUENCY_NOTIFY_INTERVAL_MS = 32;
const TYPING_PRIORITY_NOTIFY_INTERVAL_MS = 250;
const HIGH_FREQUENCY_NOTIFY_MAX_SESSIONS_PER_TICK = 8;
const pendingDeferredStateNotifications = new Set<string>();
-const pendingMessageCreatedPatches = new Map<string, string>();
let deferredStateNotificationTimer: ReturnType<typeof setTimeout> | null = null;
function hasDeferredStateWork(): boolean {
- return pendingDeferredStateNotifications.size > 0 || pendingMessageCreatedPatches.size > 0;
+ return pendingDeferredStateNotifications.size > 0;
}
function clearDeferredStateNotificationTimer(): void {
@@ -3329,18 +3327,12 @@ function emitStateNotifications(sessionId: string): void {
});
}
-function flushPendingMessageCreatedPatch(sessionId: string): void {
- const updatedAt = pendingMessageCreatedPatches.get(sessionId);
- if (!updatedAt) return;
- pendingMessageCreatedPatches.delete(sessionId);
- emitPatch(sessionId, { updatedAt });
-}
-
function flushDeferredStateNotifications(): void {
deferredStateNotificationTimer = null;
- const sessionIds = [
- ...new Set([...pendingDeferredStateNotifications, ...pendingMessageCreatedPatches.keys()]),
- ].slice(0, HIGH_FREQUENCY_NOTIFY_MAX_SESSIONS_PER_TICK);
+ const sessionIds = [...pendingDeferredStateNotifications].slice(
+ 0,
+ HIGH_FREQUENCY_NOTIFY_MAX_SESSIONS_PER_TICK,
+ );
let stateChanged = false;
for (const sessionId of sessionIds) {
if (pendingDeferredStateNotifications.delete(sessionId)) {
@@ -3353,7 +3345,6 @@ function flushDeferredStateNotifications(): void {
cb();
});
}
- for (const sessionId of sessionIds) flushPendingMessageCreatedPatch(sessionId);
if (hasDeferredStateWork()) scheduleDeferredStateNotifications();
}
@@ -3362,16 +3353,8 @@ function queueDeferredStateNotification(sessionId: string): void {
scheduleDeferredStateNotifications();
}
-function queueMessageCreatedPatch(sessionId: string, updatedAt: string): void {
- pendingMessageCreatedPatches.set(sessionId, updatedAt);
- // patch 也进入同一个 FIFO session 集合。否则 Map-only 的 no-op DB echo 会永远排在
- // 持续活跃的前 8 个 state session 之后,slice 上限会让 sidebar patch 饥饿。
- queueDeferredStateNotification(sessionId);
-}
-
function discardDeferredStateWork(sessionId: string): void {
pendingDeferredStateNotifications.delete(sessionId);
- pendingMessageCreatedPatches.delete(sessionId);
if (!hasDeferredStateWork()) clearDeferredStateNotificationTimer();
}
@@ -4524,9 +4507,6 @@ function setState(
globalListeners.forEach((cb) => {
cb();
});
- // messages:created 的侧栏时间戳与 chat state 保持原有先后:先通知消息,再 patch 列表。
- flushPendingMessageCreatedPatch(sessionId);
- if (!hasDeferredStateWork()) clearDeferredStateNotificationTimer();
}
/**
@@ -8995,10 +8975,6 @@ function initGlobalListeners(options: GlobalListenerOptions = {}): void {
},
{ deferNotification },
);
- // sidebar 排序时间轴 — 让接管路径下新消息也能 bump session 顺序。
- const updatedAt = new Date().toISOString();
- if (deferNotification) queueMessageCreatedPatch(sessionId, updatedAt);
- else emitPatch(sessionId, { updatedAt });
}
// 消息本地删除推送:本机多窗口与 device-link 控制端共用同一 reducer。
@@ -9889,7 +9865,6 @@ function __teardownGlobalListeners(): void {
remoteMessageRepairTimers.clear();
clearDeferredStateNotificationTimer();
pendingDeferredStateNotifications.clear();
- pendingMessageCreatedPatches.clear();
_pendingErrorClearOnLeave.clear();
_deferredTurnErrorPersist.clear();
_liveErrorEpoch.clear();
@@ -14392,8 +14367,7 @@ function prewarmSession(
function touchSessionUserSend(sessionId: string, workingDir: string, wasFirst: boolean): void {
const sendAt = new Date();
const userSendAtIso = sendAt.toISOString();
- // 侧栏时间轴读 sessions.updatedAt,乐观更新同步 bump 让 sidebar 立即跳到"刚发过"
- // (main 侧 touchUserSendInDb 也会写 updatedAt,广播回来的 patch 值一致、幂等)。
+ // 真人发送保留旧时间字段的乐观投影;换手时间由 main 的 sessions:patched 下发。
emitPatch(
sessionId,
wasFirst