Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
perf: reduce repeated plan scans and home session projections (#5799)
DavidShenXD1551413ef637
diff --git a/apps/desktop/src/renderer/__tests__/buildRenderItemsKeyStability.test.ts b/apps/desktop/src/renderer/__tests__/buildRenderItemsKeyStability.test.tsindex a7bfe7b3f0..e65f6c7105 100644--- a/apps/desktop/src/renderer/__tests__/buildRenderItemsKeyStability.test.ts+++ b/apps/desktop/src/renderer/__tests__/buildRenderItemsKeyStability.test.ts@@ -1177,6 +1177,56 @@ describe('buildRenderItems — key stability', () => {expect(segment.toolCalls.map((tool) => tool.clientId)).toEqual(['t1', 't2']);});+ it.each(['none', 'TodoWrite', 'update_plan'])(+ 'keeps ordinary history and %s plans unchanged in an incomplete window', (source) => {+ const messages: ChatMessage[] = [mkUser('u1')];+ for (let index = 0; index < 80; index += 1) {+ messages.push(mkTool(`read-${index}`, 'Read'), mkResult(`result-${index}`, `tu-read-${index}`));+ }+ if (source === 'TodoWrite') {+ messages.push(mkTool('plan', source, { todos: [+ { content: 'Inspect', status: 'completed' },+ { content: 'Verify', status: 'pending' },+ ] }));+ } else if (source === 'update_plan') {+ messages.push(mkTool('plan', source, { plan: [+ { step: 'Inspect', status: 'completed' },+ { step: 'Verify', status: 'pending' },+ ] }));+ }+ messages.push(mkAssistant('answer', 'Done'));+ const complete = buildRenderItems(messages);+ const partial = buildRenderItems(messages, undefined, undefined, { historyWindowIncomplete: true });+ expect(partial).toEqual(complete);+ expect(partial.items.filter((item) => item.type === 'agent_plan')).toHaveLength(source === 'none' ? 0 : 1);+ },+ );++ it('keeps independent Task plans with delayed results across user turns', () => {+ const messages: ChatMessage[] = [];+ const lateResults: ChatMessage[] = [];+ for (let turn = 0; turn < 6; turn += 1) {+ messages.push(mkUser(`user-${turn}`), mkTool(`plan-${turn}`, 'TaskList'));+ lateResults.push(mkResult(`result-${turn}`, `tu-plan-${turn}`, JSON.stringify({ tasks: [+ { id: `${turn}-1`, subject: `Inspect ${turn}`, status: 'in_progress' },+ { id: `${turn}-2`, subject: `Verify ${turn}`, status: 'pending' },+ ] })));+ messages.push(mkAssistant(`answer-${turn}`, 'Progress'));+ }+ messages.push(...lateResults.reverse());+ const plans = buildRenderItems(messages, undefined, undefined, { historyWindowIncomplete: true })+ .items.filter((item) => item.type === 'agent_plan');+ expect(plans.map((plan) => ({ key: plan.key, todos: plan.todos }))).toEqual(+ Array.from({ length: 6 }, (_, turn) => ({+ key: `todo-plan-${turn}`,+ todos: [+ { content: `Inspect ${turn}`, status: 'in_progress' },+ { content: `Verify ${turn}`, status: 'pending' },+ ],+ })),+ );+ });+it('waits for older Task history before showing a partial inline plan', () => {const first = mkTool('tc2', 'TaskCreate', { subject: 'Fix renderer' });const second = mkTool('tc3', 'TaskCreate', { subject: 'Run tests' });@@ -1254,6 +1304,71 @@ describe('buildRenderItems — key stability', () => {});});+ it('keeps late Task results attached after a long history prefix', () => {+ const messages: ChatMessage[] = [+ mkUser('u1', 'Show the current tasks'),+ mkTool('task-list', 'TaskList'),+ ];+ for (let index = 0; index < 200; index += 1) {+ messages.push(mkAssistant(`history-${index}`, `history ${index}`));+ }+ messages.push(mkResult(+ 'r-task-list-late',+ 'tu-task-list',+ JSON.stringify({+ tasks: [+ { id: '1', subject: 'Inspect renderer', status: 'in_progress' },+ { id: '2', subject: 'Run tests', status: 'pending' },+ ],+ }),+ ));++ const plans = buildRenderItems(messages, undefined, undefined, {+ historyWindowIncomplete: true,+ }).items.filter(+ (item): item is Extract<RenderItem, { type: 'agent_plan' }> => item.type === 'agent_plan',+ );++ expect(plans).toHaveLength(1);+ expect(plans[0]?.todos).toEqual([+ { content: 'Inspect renderer', status: 'in_progress' },+ { content: 'Run tests', status: 'pending' },+ ]);+ });++ it('black-box: repeated history-window renders keep one stable plan result', () => {+ const messages: ChatMessage[] = [+ mkUser('u-blackbox', 'Review the task list'),+ mkTool('task-list', 'TaskList'),+ ];+ for (let index = 0; index < 200; index += 1) {+ messages.push(mkAssistant(`blackbox-history-${index}`, `history ${index}`));+ }+ messages.push(mkResult(+ 'r-task-list-blackbox',+ 'tu-task-list',+ JSON.stringify({+ tasks: [+ { id: 'blackbox-task-1', subject: 'Keep the task visible', status: 'in_progress' },+ { id: 'blackbox-task-2', subject: 'Keep the second task visible', status: 'pending' },+ ],+ }),+ ));++ const render = () => buildRenderItems(messages, undefined, undefined, {+ historyWindowIncomplete: true,+ }).items;+ const first = render();+ const second = render();+ const summarize = (items: RenderItem[]) => items.map((item) => {+ if (item.type !== 'agent_plan') return item.type;+ return { type: item.type, key: item.key, todos: item.todos };+ });++ expect(summarize(second)).toEqual(summarize(first));+ expect(second.filter((item) => item.type === 'agent_plan')).toHaveLength(1);+ });+it('renders a new Task plan after a real user turn despite an older orphan update', () => {const messages = [mkTool('old-update', 'TaskUpdate', { taskId: 'old', status: 'in_progress' }),diff --git a/apps/desktop/src/renderer/components/chat/MessageStream.tsx b/apps/desktop/src/renderer/components/chat/MessageStream.tsxindex ed8bd494fb..806fa441cc 100644--- a/apps/desktop/src/renderer/components/chat/MessageStream.tsx+++ b/apps/desktop/src/renderer/components/chat/MessageStream.tsx@@ -1608,30 +1608,66 @@ export function buildRenderItems(// Task session。不能只检查整窗「最新」计划:后面若已有可解析的新 session,// 较早的半截 Task 卡仍会漏出来。逐个 Task insertion 用其前缀复核,等 prepend// 补齐标题/早期 TaskCreate 后再由同一 stable key 插回。+ let needsTaskHistoryValidation = false;if (opts?.historyWindowIncomplete === true) {+ for (const insertion of planInsertAt.values()) {+ if (insertion.source === 'task') {+ needsTaskHistoryValidation = true;+ break;+ }+ }+ }+ // Ordinary tool history and TodoWrite/update_plan need no Task prefix validation.+ if (needsTaskHistoryValidation) {+ // Build the indexes once. The old validation path rescanned the whole+ // prefix and filtered the whole suffix for every Task insertion. A paged+ // history window can contain many task rows, so keep message order while+ // looking up only plan ids and their late results.+ const planToolUseCountAtInsertion = new Map<number, number>();+ const toolResultsByUseId = new Map<+ string,+ Array<{ index: number; message: ChatMessage }>+ >();+ const planToolUseIds: string[] = [];+ for (let messageIndex = 0; messageIndex < messages.length; messageIndex += 1) {+ const message = messages[messageIndex];+ if (message.role === 'tool_result' && typeof message.toolUseId === 'string') {+ const results = toolResultsByUseId.get(message.toolUseId) ?? [];+ results.push({ index: messageIndex, message });+ toolResultsByUseId.set(message.toolUseId, results);+ }+ if (+ isAgentPlanToolName(message.toolName)+ && typeof message.toolUseId === 'string'+ && message.toolUseId.length > 0+ ) {+ planToolUseIds.push(message.toolUseId);+ }+ if (planInsertAt.has(messageIndex)) {+ planToolUseCountAtInsertion.set(messageIndex, planToolUseIds.length);+ }+ }+for (const [index, insertion] of planInsertAt) {if (insertion.source !== 'task') continue;const prefix = messages.slice(0, index + 1);- const prefixPlanToolUseIds = new Set(- prefix- .filter((message) => isAgentPlanToolName(message.toolName))- .map((message) => message.toolUseId)- .filter((toolUseId): toolUseId is string => Boolean(toolUseId)),- );+ const prefixPlanToolUseIds = new Set<string>();+ const planToolUseCount = planToolUseCountAtInsertion.get(index) ?? 0;+ for (let planIndex = 0; planIndex < planToolUseCount; planIndex += 1) {+ prefixPlanToolUseIds.add(planToolUseIds[planIndex]);+ }// TaskList / TaskGet 的权威内容在后续 tool_result 行里。只切到工具行会把// 已完整的 snapshot 重新判成半截;保留当前前缀中计划调用的匹配结果,但// 不把后续计划事件带进来,避免改变「正在复核哪张 insertion」的语义。- const validationMessages = [- ...prefix,- ...messages- .slice(index + 1)- .filter(- (message) =>- message.role === 'tool_result' &&- typeof message.toolUseId === 'string' &&- prefixPlanToolUseIds.has(message.toolUseId),- ),- ];+ const lateResults: Array<{ index: number; message: ChatMessage }> = [];+ for (const toolUseId of prefixPlanToolUseIds) {+ for (const result of toolResultsByUseId.get(toolUseId) ?? []) {+ if (result.index > index) lateResults.push(result);+ }+ }+ if (lateResults.length > 1) lateResults.sort((left, right) => left.index - right.index);+ const validationMessages = prefix;+ for (const result of lateResults) validationMessages.push(result.message);const stateAtInsertion = getLatestMessageTodoState(validationMessages, {taskHistoryMayBeIncomplete: true,});diff --git a/apps/mobile/src/__tests__/remoteSessionStore.test.ts b/apps/mobile/src/__tests__/remoteSessionStore.test.tsindex d27c5fe989..fb9a0360da 100644--- a/apps/mobile/src/__tests__/remoteSessionStore.test.ts+++ b/apps/mobile/src/__tests__/remoteSessionStore.test.ts@@ -419,6 +419,100 @@ describe('remoteSessionStore', () => {});});+ it('keeps the cached home projection stable across usage-only refreshes', () => {+ remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);+ const before = remoteSessionStore.getHomeSessions();+ const beforeRow = before[0];+ expect(beforeRow).toBeDefined();+ expect(beforeRow).not.toHaveProperty('totalCostUsd');++ remoteSessionStore.applyRemotePush('dev-1', 'usage:session-spend-changed', {+ sessionId: 's1',+ totalCostUsd: 2.5,+ });+ expect(remoteSessionStore.getHomeSessions()).toBe(before);+ expect(remoteSessionStore.getHomeSessions()[0]).toBe(beforeRow);++ // A later ordinary recompute must still reuse the projection because usage+ // fields are intentionally absent from Home.+ remoteSessionStore.setDeviceIdentity([{ deviceId: 'dev-1', name: 'Mac' }]);+ expect(remoteSessionStore.getHomeSessions()).toBe(before);+ expect(remoteSessionStore.getHomeSessions()[0]).toBe(beforeRow);+ expect(remoteSessionStore.getHomeSessions()[0]).not.toHaveProperty('totalCostUsd');+ });++ it('black-box: repeated device refreshes keep the visible home snapshot stable', () => {+ remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);+ const before = remoteSessionStore.getHomeSessions();+ const beforeRows = [...before];++ for (let index = 0; index < 100; index += 1) {+ remoteSessionStore.setDeviceIdentity([+ { deviceId: 'dev-1', name: 'Mac' },+ { deviceId: `unrelated-${index}`, name: `Other ${index}` },+ ]);+ }++ const after = remoteSessionStore.getHomeSessions();+ expect(after).toBe(before);+ expect(after).toEqual(beforeRows);+ expect(after.every((row) => !Object.prototype.hasOwnProperty.call(row, 'totalCostUsd'))).toBe(true);+ expect(after.every((row) => !Object.prototype.hasOwnProperty.call(row, 'totalMoney'))).toBe(true);+ expect(after.every((row) => !Object.prototype.hasOwnProperty.call(row, 'totalTokenUsage'))).toBe(true);+ });++ it('refreshes Home metadata after unchanged device refreshes and usage-only pushes', () => {+ remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);+ const before = remoteSessionStore.getHomeSessions();+ const homeChanged = vi.fn();+ const unsubscribe = remoteSessionStore.subscribeHomeStatus(homeChanged);+ try {+ remoteSessionStore.applyRemotePush('dev-1', 'usage:session-tokens-changed', { sessionId: 's1', totalTokens: 500 });+ for (let index = 0; index < 10; index += 1) {+ remoteSessionStore.setDeviceIdentity([+ { deviceId: 'dev-1', name: 'Mac' },+ { deviceId: `other-${index}`, name: 'Other' },+ ]);+ }+ expect(remoteSessionStore.getHomeSessions()).toBe(before);+ expect(homeChanged).not.toHaveBeenCalled();+ expect(remoteSessionStore.getSessions()[0].totalTokenUsage).toBe(500);+ expect(remoteSessionStore.getDeviceIdentity()[1].deviceId).toBe('other-9');++ remoteSessionStore.applySessionPatch('dev-1', 's1', { title: 'Renamed' });+ const renamed = remoteSessionStore.getHomeSessions();+ expect(renamed).not.toBe(before);+ expect(renamed[0]).toMatchObject({ id: 's1', title: 'Renamed' });+ expect(renamed[0]).not.toHaveProperty('totalTokenUsage');+ expect(renamed[1]).toBe(before[1]);+ expect(homeChanged).toHaveBeenCalledTimes(1);++ remoteSessionStore.renameDevice('dev-1', 'New Mac');+ expect(remoteSessionStore.getHomeSessions().every((row) => row.deviceLinkDeviceName === 'New Mac')).toBe(true);+ expect(homeChanged).toHaveBeenCalledTimes(2);+ } finally {+ unsubscribe();+ }+ });++ it.each(['remove', 'clear'] as const)('rebuilds Home after %s and reusing a session ID', (action) => {+ remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);+ const before = remoteSessionStore.getHomeSessions();+ remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s2'), session('s1')]);+ expect(remoteSessionStore.getHomeSessions()).toEqual([before[1], before[0]]);+ expect(remoteSessionStore.getHomeSessions()[0]).toBe(before[1]);++ if (action === 'clear') remoteSessionStore.clear();+ else remoteSessionStore.setDeviceSessions('dev-1', 'Mac', []);+ expect(remoteSessionStore.getHomeSessions()).toEqual([]);+ remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);+ expect(remoteSessionStore.getHomeSessions()).toHaveLength(1);+ expect(remoteSessionStore.getHomeSessions()[0]).not.toBe(before[0]);+ expect(remoteSessionStore.getHomeSessions()[0]).toMatchObject({ id: 's1', title: 's1' });+ remoteSessionStore.applySessionPatch('dev-1', 's1', { title: 'Restored title' });+ expect(remoteSessionStore.getHomeSessions()[0].title).toBe('Restored title');+ });+it('removes archived sessions from the active mirror', () => {remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);remoteSessionStore.applySessionPatch('dev-1', 's1', { status: 'archived' });diff --git a/apps/mobile/src/session/remoteSessionStore.ts b/apps/mobile/src/session/remoteSessionStore.tsindex f660214f06..9ecbc0609d 100644--- a/apps/mobile/src/session/remoteSessionStore.ts+++ b/apps/mobile/src/session/remoteSessionStore.ts@@ -1281,17 +1281,31 @@ const mergedSessionById = new Map<string, RemoteSession>();// live usage. Usage pushes must not rebuild Home's grouping/localization tree.let homeSessions: RemoteSession[] = [];const SESSION_USAGE_FIELDS = new Set(['totalMoney', 'totalCostUsd', 'totalTokenUsage']);+const homeSessionProjectionCache = new Map<string, {+ source: RemoteSession;+ projected: RemoteSession;+}>();function reconcileHomeSessions(): void {- const previous = new Map(homeSessions.map((session) => [session.id, session]));+ const activeSessionIds = new Set<string>();const next = mergedSessions.map((session) => {+ activeSessionIds.add(session.id);+ const cached = homeSessionProjectionCache.get(session.id);+ if (cached?.source === session) return cached.projected;+const projected = { ...session };delete projected.totalMoney;delete projected.totalCostUsd;delete projected.totalTokenUsage;- const old = previous.get(session.id);- return old && remoteSessionEqual(old, projected) ? old : projected;+ const stableProjected = cached && remoteSessionEqual(cached.projected, projected)+ ? cached.projected+ : projected;+ homeSessionProjectionCache.set(session.id, { source: session, projected: stableProjected });+ return stableProjected;});+ for (const sessionId of homeSessionProjectionCache.keys()) {+ if (!activeSessionIds.has(sessionId)) homeSessionProjectionCache.delete(sessionId);+ }if (sameElementRefs(homeSessions, next)) return;homeSessions = next;bumpHomeStatusVersion();@@ -1544,8 +1558,9 @@ function recomputeSessions(): void {for (const { session, physicalDeviceId } of byId.values()) {const prev = prevById.get(session.id);const projected = applyPendingTitlePreview(session);- if (!prev || !remoteSessionEqual(prev, projected)) pendingMessagePreviewSessionIds.add(session.id);- next.push(prev && remoteSessionEqual(prev, projected) ? prev : projected);+ const same = prev !== undefined && remoteSessionEqual(prev, projected);+ if (!same) pendingMessagePreviewSessionIds.add(session.id);+ next.push(same ? prev : projected);sessionDeviceIndex.set(session.id, physicalDeviceId);}for (const sessionId of [...sessionLiveActivity.keys()]) {@@ -1555,10 +1570,13 @@ function recomputeSessions(): void {// 权威标题落地、明确失败撤回、归档/删除、设备移除、clear() 才会丢掉。// 数组级同样调和:全部元素引用与序都未变时保留旧数组引用——useRemoteSessions 的// useSyncExternalStore 快照经 Object.is 即可短路,消费屏对无关 emit 零重渲染。- mergedSessions = sameElementRefs(mergedSessions, next) ? mergedSessions : next;+ const sessionsChanged = !sameElementRefs(mergedSessions, next);+ if (sessionsChanged) mergedSessions = next;mergedSessionById.clear();for (const session of mergedSessions) mergedSessionById.set(session.id, session);- reconcileHomeSessions();+ // Usage-only patches bypass this projection and cannot change Home fields.+ // Reuse the list comparison above to skip its scan and allocations entirely.+ if (sessionsChanged) reconcileHomeSessions();let liveRowsReanchored = false;for (const session of mergedSessions) {bindPendingHostAnchorSendAt(session.id, session.userSendAt);@@ -5211,6 +5229,7 @@ export const remoteSessionStore = {pendingTitlePreview.clear();mergedSessions = [];mergedSessionById.clear();+ homeSessionProjectionCache.clear();if (homeSessions.length > 0) {homeSessions = [];bumpHomeStatusVersion();