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(compression): use the topic-pinned model for manual compaction (#22697)

Lobehub Coder8fba8e3d25f4
diff --git a/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/compression.test.ts b/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/compression.test.ts
index 17a05aa2bf3..64279396b55 100644
--- a/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/compression.test.ts
+++ b/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/compression.test.ts
@@ -4,7 +4,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { chatService } from '@/services/chat';
import { messageService } from '@/services/message';
-import { agentSelectors } from '@/store/agent/selectors';
+import { agentByIdSelectors, agentSelectors } from '@/store/agent/selectors';
+import { type ChatStore } from '@/store/chat/store';
import { messageMapKey } from '@/store/chat/utils/messageMapKey';
import { useChatStore } from '../../../../../store';
@@ -41,6 +42,7 @@ beforeEach(() => {
useChatStore.setState({
activeAgentId: agentId,
activeTopicId: topicId,
+ topicDataMap: {},
dbMessagesMap: { [key]: originalMessages },
messagesMap: { [key]: originalMessages },
});
@@ -176,4 +178,87 @@ describe('executeCompression', () => {
expect(messageService.cancelCompression).not.toHaveBeenCalled();
expect(getMessageIds()).toEqual(['group-1']);
});
+
+ describe('summary model', () => {
+ const runWithSummary = async (options?: Parameters<ChatStore['executeCompression']>[2]) => {
+ vi.spyOn(messageService, 'finalizeCompression').mockResolvedValue({ messages: [] });
+ const fetchSpy = vi
+ .spyOn(chatService, 'fetchPresetTaskResult')
+ .mockImplementation(async ({ onMessageHandle }) => {
+ onMessageHandle?.({ text: 'summary', type: 'text' });
+ });
+ await act(async () => {
+ await useChatStore.getState().executeCompression(context, '', options);
+ });
+ return fetchSpy.mock.calls[0][0].params;
+ };
+
+ const pinTopicModel = () =>
+ act(() => {
+ useChatStore.setState({
+ topicDataMap: {
+ [agentId]: {
+ items: [{ id: topicId, model: 'claude-opus-4-8', provider: 'anthropic' }],
+ } as any,
+ },
+ });
+ });
+
+ it('uses the model pinned to the topic over the agent default', async () => {
+ pinTopicModel();
+
+ const params = await runWithSummary();
+
+ expect(params).toMatchObject({ model: 'claude-opus-4-8', provider: 'anthropic' });
+ });
+
+ it('uses the pinned model for the exceeded-context compact-and-retry', async () => {
+ pinTopicModel();
+
+ // Same options the ExceededContextWindowError card passes.
+ const params = await runWithSummary({ beforeMessageId: 'assistant-1', onRetry: vi.fn() });
+
+ expect(params).toMatchObject({ model: 'claude-opus-4-8', provider: 'anthropic' });
+ });
+
+ it('falls back to the agent default when the topic has no pinned model', async () => {
+ act(() => {
+ useChatStore.setState({
+ topicDataMap: { [agentId]: { items: [{ id: topicId }] } as any },
+ });
+ });
+
+ const params = await runWithSummary();
+
+ expect(params).toMatchObject({ model: 'gpt-4o-mini', provider: 'openai' });
+ });
+
+ it('ignores a heterogeneous runtime pin, which the chat endpoint cannot serve', async () => {
+ vi.spyOn(agentByIdSelectors, 'isAgentHeterogeneousById').mockImplementation(() => () => true);
+ // snapshotAgentModel pins the CLI runtime type as the topic provider.
+ act(() => {
+ useChatStore.setState({
+ topicDataMap: {
+ [agentId]: { items: [{ id: topicId, model: 'default', provider: 'cursor' }] } as any,
+ },
+ });
+ });
+
+ const params = await runWithSummary();
+
+ expect(params).toMatchObject({ model: 'gpt-4o-mini', provider: 'openai' });
+ });
+
+ it('falls back to the agent default when the topic pin has no provider', async () => {
+ act(() => {
+ useChatStore.setState({
+ topicDataMap: { [agentId]: { items: [{ id: topicId, model: 'gpt-5' }] } as any },
+ });
+ });
+
+ const params = await runWithSummary();
+
+ expect(params).toMatchObject({ model: 'gpt-4o-mini', provider: 'openai' });
+ });
+ });
});
diff --git a/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/compression.ts b/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/compression.ts
index 296eeeb7905..2dd3bdd04c0 100644
--- a/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/compression.ts
+++ b/src/store/chat/slices/agentRun/actions/entries/conversationLifecycle/compression.ts
@@ -7,8 +7,8 @@ import { t } from 'i18next';
import { chatService } from '@/services/chat';
import { messageService } from '@/services/message';
import { getAgentStoreState } from '@/store/agent';
-import { agentSelectors } from '@/store/agent/selectors';
-import { dbMessageSelectors } from '@/store/chat/selectors';
+import { agentByIdSelectors, agentSelectors } from '@/store/agent/selectors';
+import { dbMessageSelectors, topicSelectors } from '@/store/chat/selectors';
import { type ChatStore } from '@/store/chat/store';
import {
createPendingCompressedGroup,
@@ -98,8 +98,18 @@ export const executeContextCompression = async (
get().replaceMessages(serverMessages, { context: context as any });
get().associateMessageWithOperation(messageGroupId, operationId);
- // 2. Generate summary via LLM
- const { model, provider } = agentSelectors.getAgentConfigById(agentId)(getAgentStoreState());
+ // 2. Generate summary via LLM. Match the runtime's auto-compression model resolution: a model
+ // pinned to the topic wins over the agent default (see streamingExecutor).
+ // The summary goes through the regular chat endpoint, so only an API pin is usable:
+ // heterogeneous topics pin the CLI runtime (e.g. `cursor/default`), and a model-only legacy
+ // pin has no provider to route to. Both keep the agent default.
+ const agentState = getAgentStoreState();
+ const topicModel = agentByIdSelectors.isAgentHeterogeneousById(agentId)(agentState)
+ ? undefined
+ : topicSelectors.getTopicModelById(topicId)(get());
+ const { model, provider } = topicModel?.provider
+ ? topicModel
+ : agentSelectors.getAgentConfigById(agentId)(agentState);
const compressionPayload = chainCompressContext(messagesToSummarize);
let summaryContent = '';
let summaryError: Error | undefined;