From 56d2490db987631de58aba6446f0b251c454526e Mon Sep 17 00:00:00 2001 From: adityachaudhary99 Date: Sun, 14 Jun 2026 20:46:04 +0530 Subject: [PATCH 1/5] feat: add compactModel config option to use a different model for compaction When set and different from mainLoopModel, the forked-agent prompt-cache-sharing path is bypassed (guaranteed cache miss with different models) and the streaming fallback uses compactModel for the API call instead. Closes #1445 --- src/services/compact/compact.ts | 14 +++++++++++--- src/utils/config.ts | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/services/compact/compact.ts b/src/services/compact/compact.ts index 144456a956..64756798dd 100644 --- a/src/services/compact/compact.ts +++ b/src/services/compact/compact.ts @@ -41,7 +41,7 @@ import { getDeferredToolsDeltaAttachment, getMcpInstructionsDeltaAttachment, } from '../../utils/attachments.js' -import { getMemoryPath } from '../../utils/config.js' +import { getGlobalConfig, getMemoryPath } from '../../utils/config.js' import { COMPACT_MAX_OUTPUT_TOKENS } from '../../utils/context.js' import { createChildAbortController } from '../../utils/abortController.js' import { @@ -459,7 +459,11 @@ export async function compactConversation( // The GB flag is kept as a kill-switch. // streamCompactSummary() (below) follows the same gate; see also the // provider-gate tests in src/services/compact/compact.test.ts. + const compactModel = getGlobalConfig().compactModel + const modelChangesForCompaction = + compactModel !== undefined && compactModel !== context.options.mainLoopModel const promptCacheSharingEnabled = + !modelChangesForCompaction && isCompactionCacheSharingCompatible(context.options.mainLoopModel) && getFeatureValue_CACHED_MAY_BE_STALE( 'tengu_compact_cache_prefix', @@ -1186,10 +1190,14 @@ async function streamCompactSummary({ // prompt cache; other 3P providers are incompatible and would send // Anthropic-only params that they reject. The shared predicate makes this // safe to call from 3P provider paths. + const compactModel = getGlobalConfig().compactModel + const modelChangesForCompaction = + compactModel !== undefined && compactModel !== context.options.mainLoopModel const cacheSharingAvailable = isCompactionCacheSharingCompatible( context.options.mainLoopModel, ) const promptCacheSharingEnabled = + !modelChangesForCompaction && cacheSharingAvailable && getFeatureValue_CACHED_MAY_BE_STALE( 'tengu_compact_cache_prefix', @@ -1402,13 +1410,13 @@ async function streamCompactSummary({ const appState = context.getAppState() return appState.toolPermissionContext }, - model: context.options.mainLoopModel, + model: compactModel ?? context.options.mainLoopModel, toolChoice: undefined, isNonInteractiveSession: context.options.isNonInteractiveSession, hasAppendSystemPrompt: !!context.options.appendSystemPrompt, maxOutputTokensOverride: Math.min( COMPACT_MAX_OUTPUT_TOKENS, - getMaxOutputTokensForModel(context.options.mainLoopModel), + getMaxOutputTokensForModel(compactModel ?? context.options.mainLoopModel), ), querySource: 'compact', agents: context.options.agentDefinitions.activeAgents, diff --git a/src/utils/config.ts b/src/utils/config.ts index 062e2f5f0d..15ccbfa93d 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -670,6 +670,10 @@ export type GlobalConfig = { // When enabled, triggers forced compaction if the message count exceeds the // chosen threshold, regardless of token usage. maxMessagesCompactionThreshold?: MaxMessagesCompactionThreshold + + // Use a different (e.g. cheaper/faster) model for compaction. + // Defaults to mainLoopModel when unset. + compactModel?: string } /** @@ -774,6 +778,7 @@ export const GLOBAL_CONFIG_KEYS = [ 'knowledgeGraphEnabled', 'logoColor', 'maxMessagesCompactionThreshold', + 'compactModel', ] as const export type GlobalConfigKey = (typeof GLOBAL_CONFIG_KEYS)[number] From d741990b9d0bf6892d9b77cf8e468436fb583f7b Mon Sep 17 00:00:00 2001 From: adityachaudhary99 Date: Sun, 14 Jun 2026 23:03:40 +0530 Subject: [PATCH 2/5] feat(config): expose compactModel in /config TUI and add compactModel test coverage Addresses review feedback on #1629: - Surface compactModel as a managedEnum setting in the /config screen, following the teammateDefaultModel pattern (submenu + ModelPicker). - Add a compact.test.ts case covering the compactModel !== mainLoopModel path: cache-sharing is skipped and the streaming compaction fallback routes model/maxOutputTokensOverride to compactModel. --- src/components/Settings/Config.tsx | 54 ++++++++++++++++- src/services/compact/compact.test.ts | 86 ++++++++++++++++++++++++---- 2 files changed, 126 insertions(+), 14 deletions(-) diff --git a/src/components/Settings/Config.tsx b/src/components/Settings/Config.tsx index 73b5c816d7..85378b80f6 100644 --- a/src/components/Settings/Config.tsx +++ b/src/components/Settings/Config.tsx @@ -82,7 +82,7 @@ type Setting = (SettingBase & { onChange(value: string): void; type: 'managedEnum'; }); -type SubMenu = 'Theme' | 'Model' | 'TeammateModel' | 'ExternalIncludes' | 'OutputStyle' | 'ChannelDowngrade' | 'Language' | 'EnableAutoUpdates'; +type SubMenu = 'Theme' | 'Model' | 'TeammateModel' | 'CompactModel' | 'ExternalIncludes' | 'OutputStyle' | 'ChannelDowngrade' | 'Language' | 'EnableAutoUpdates'; export function Config({ onClose, context, @@ -888,6 +888,12 @@ export function Config({ value: mainLoopModel === null ? 'Default (recommended)' : mainLoopModel, type: 'managedEnum' as const, onChange: onChangeMainModelConfig + }, { + id: 'compactModel', + label: 'Compaction model', + value: compactModelDisplayString(globalConfig.compactModel), + type: 'managedEnum' as const, + onChange() {} }, ...(isConnectedToIde ? [{ id: 'diffTool', label: 'Diff tool', @@ -1378,7 +1384,7 @@ export function Config({ } return; } - if (setting_0.id === 'theme' || setting_0.id === 'model' || setting_0.id === 'teammateDefaultModel' || setting_0.id === 'showExternalIncludesDialog' || setting_0.id === 'outputStyle' || setting_0.id === 'language') { + if (setting_0.id === 'theme' || setting_0.id === 'model' || setting_0.id === 'compactModel' || setting_0.id === 'teammateDefaultModel' || setting_0.id === 'showExternalIncludesDialog' || setting_0.id === 'outputStyle' || setting_0.id === 'language') { // managedEnum items open a submenu — isDirty is set by the submenu's // completion callback, not here (submenu may be cancelled). switch (setting_0.id) { @@ -1390,6 +1396,10 @@ export function Config({ setShowSubmenu('Model'); setTabsHidden(true); return; + case 'compactModel': + setShowSubmenu('CompactModel'); + setTabsHidden(true); + return; case 'teammateDefaultModel': setShowSubmenu('TeammateModel'); setTabsHidden(true); @@ -1592,6 +1602,42 @@ export function Config({ }} onCancel={() => { setShowSubmenu(null); setTabsHidden(false); + }} /> + + + + + + + : showSubmenu === 'CompactModel' ? <> + { + setShowSubmenu(null); + setTabsHidden(false); + isDirty.current = true; + saveGlobalConfig(current_24 => { + if (model_2 === null) { + const { + compactModel, + ...rest + } = current_24; + return rest; + } + return { + ...current_24, + compactModel: model_2 + }; + }); + setGlobalConfig(getGlobalConfig()); + setChanges(prev_26 => ({ + ...prev_26, + compactModel: compactModelDisplayString(model_2 ?? undefined) + })); + logEvent('tengu_compact_model_changed', { + model: model_2 as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS + }); + }} onCancel={() => { + setShowSubmenu(null); + setTabsHidden(false); }} /> @@ -1823,6 +1869,10 @@ function teammateModelDisplayString(value: string | null | undefined): string { if (value === null) return "Default (leader's model)"; return modelDisplayString(value); } +function compactModelDisplayString(value: string | undefined): string { + if (value === undefined) return 'Default (main model)'; + return modelDisplayString(value); +} const THEME_LABELS: Record = { auto: 'Auto (match terminal)', dark: 'Dark mode', diff --git a/src/services/compact/compact.test.ts b/src/services/compact/compact.test.ts index 8f8aab9003..129aa8d936 100644 --- a/src/services/compact/compact.test.ts +++ b/src/services/compact/compact.test.ts @@ -200,6 +200,12 @@ export type CompactMockOptions = { growthBookDefault?: boolean /** Mock for executePreCompactHooks. */ executePreCompactHooks?: ReturnType + /** Override for getGlobalConfig(), e.g. to set compactModel. */ + getGlobalConfig?: ReturnType + /** Override for queryModelWithStreaming(), to inspect the model/options passed in. */ + queryModelWithStreaming?: ReturnType + /** Override for getMaxOutputTokensForModel(). */ + getMaxOutputTokensForModel?: ReturnType } /** @@ -318,18 +324,21 @@ function registerCommonCompactStubs(options: CompactMockOptions = {}) { // --- API / streaming (DEFENSIVE) --- mock.module('../api/claude.js', () => ({ - queryModelWithStreaming: mock(async function* () { - yield { - type: 'assistant' as const, - message: { - role: 'assistant' as const, - content: [{ type: 'text' as const, text: 'Streamed summary.' }], - }, - uuid: `stream-${Math.random()}`, - timestamp: new Date().toISOString(), - } - }), - getMaxOutputTokensForModel: mock(() => 8192), + queryModelWithStreaming: + options.queryModelWithStreaming ?? + mock(async function* () { + yield { + type: 'assistant' as const, + message: { + role: 'assistant' as const, + content: [{ type: 'text' as const, text: 'Streamed summary.' }], + }, + uuid: `stream-${Math.random()}`, + timestamp: new Date().toISOString(), + } + }), + getMaxOutputTokensForModel: + options.getMaxOutputTokensForModel ?? mock(() => 8192), })) mock.module('../api/errors.js', () => ({ @@ -374,6 +383,9 @@ function registerCommonCompactStubs(options: CompactMockOptions = {}) { mock.module('../../utils/config.js', () => ({ ..._realConfigModule, getMemoryPath: mock(() => '/tmp/memory'), + ...(options.getGlobalConfig + ? { getGlobalConfig: options.getGlobalConfig } + : {}), })) // --- File state cache (DEFENSIVE) --- @@ -663,3 +675,53 @@ describe('compactConversation provider gate', () => { expect(runForkedAgent).toHaveBeenCalled() }) }) + +describe('compactConversation compactModel override', () => { + test('skips cache-sharing and routes streaming compaction to compactModel when it differs from mainLoopModel', async () => { + // All provider env vars are cleared by beforeEach, so this would normally + // be eligible for forked-agent cache-sharing (Anthropic provider). Setting + // compactModel to a different model than mainLoopModel must override that. + const compactModel = 'claude-opus-4-1' + const getMaxOutputTokensForModel = mock((model: string) => + model === compactModel ? 4096 : 8192, + ) + const queryModelWithStreaming = mock(async function* () { + yield { + type: 'assistant' as const, + message: { + role: 'assistant' as const, + content: [{ type: 'text' as const, text: 'Streamed summary.' }], + }, + uuid: `stream-${Math.random()}`, + timestamp: new Date().toISOString(), + } + }) + + const { compactConversation, runForkedAgent } = await importCompact({ + getGlobalConfig: mock(() => ({ + ..._realConfigModule.getGlobalConfig(), + compactModel, + })), + getMaxOutputTokensForModel, + queryModelWithStreaming, + }) + + const messages = [userMessage('Hello'), assistantMessage('Hi there!')] + const ctx = toolUseContext() + const csp = cacheSafeParams(messages) + + await compactConversation(messages, ctx, csp, false) + + // modelChangesForCompaction forces promptCacheSharingEnabled to false, + // even though the default (Anthropic) provider would normally allow + // forked-agent cache-sharing. + expect(runForkedAgent).not.toHaveBeenCalled() + + expect(queryModelWithStreaming).toHaveBeenCalled() + const [{ options: streamOptions }] = queryModelWithStreaming.mock.calls[0] as unknown as [ + { options: { model: string; maxOutputTokensOverride: number } }, + ] + expect(streamOptions.model).toBe(compactModel) + expect(streamOptions.maxOutputTokensOverride).toBe(4096) + }) +}) From 75b4c451a56800844bece9b6ae4fcb66c08ff643 Mon Sep 17 00:00:00 2001 From: adityachaudhary99 Date: Mon, 15 Jun 2026 19:24:58 +0530 Subject: [PATCH 3/5] fix(compact): use compactModel for tool-search check and add no-op guard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses round-2 review feedback on #1629: - compact.ts: pass compactModel ?? mainLoopModel to isToolSearchEnabled so the tool-search capability check matches the model actually used for streaming compaction (not always mainLoopModel when a compact model is set) - Config.tsx: mirror the teammateDefaultModel no-op guard — return early when compactModel is unset and the picker confirms null (no-op selection) Co-Authored-By: Claude Sonnet 4.6 --- src/components/Settings/Config.tsx | 3 +++ src/services/compact/compact.ts | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/components/Settings/Config.tsx b/src/components/Settings/Config.tsx index 85378b80f6..c834d75091 100644 --- a/src/components/Settings/Config.tsx +++ b/src/components/Settings/Config.tsx @@ -1613,6 +1613,9 @@ export function Config({ { setShowSubmenu(null); setTabsHidden(false); + if (globalConfig.compactModel === undefined && model_2 === null) { + return; + } isDirty.current = true; saveGlobalConfig(current_24 => { if (model_2 === null) { diff --git a/src/services/compact/compact.ts b/src/services/compact/compact.ts index 64756798dd..0ab82e281d 100644 --- a/src/services/compact/compact.ts +++ b/src/services/compact/compact.ts @@ -1360,10 +1360,10 @@ async function streamCompactSummary({ let response: AssistantMessage | undefined context.setResponseLength?.(() => 0) - // Check if tool search is enabled using the main loop's tools list. - // context.options.tools includes MCP tools merged via useMergedTools. + // Check if tool search is enabled for the model actually used for + // compaction (compactModel when set, otherwise mainLoopModel). const useToolSearch = await isToolSearchEnabled( - context.options.mainLoopModel, + compactModel ?? context.options.mainLoopModel, context.options.tools, async () => appState.toolPermissionContext, context.options.agentDefinitions.activeAgents, From bbc6aaa49498e3a08cc58cee00e932cd4eadfd43 Mon Sep 17 00:00:00 2001 From: adityachaudhary99 Date: Tue, 16 Jun 2026 01:10:07 +0530 Subject: [PATCH 4/5] fix(compact): normalize compactModel comparison in no-op guard Compare globalConfig.compactModel ?? null against the picker's selection so re-confirming the current value (including explicit "Default"/null when a model was previously set) is treated as a no-op instead of marking settings dirty. --- src/components/Settings/Config.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Settings/Config.tsx b/src/components/Settings/Config.tsx index c834d75091..a584443a22 100644 --- a/src/components/Settings/Config.tsx +++ b/src/components/Settings/Config.tsx @@ -1613,7 +1613,7 @@ export function Config({ { setShowSubmenu(null); setTabsHidden(false); - if (globalConfig.compactModel === undefined && model_2 === null) { + if ((globalConfig.compactModel ?? null) === model_2) { return; } isDirty.current = true; From 948ed92406f38baf2f68d4d994d07c90b0d3566e Mon Sep 17 00:00:00 2001 From: adityachaudhary99 Date: Tue, 16 Jun 2026 11:05:57 +0530 Subject: [PATCH 5/5] fix: resolve compactModel alias to full model ID before API calls ModelPicker stores alias strings (e.g. 'sonnet') directly in globalConfig.compactModel. Compact reads that value and must call parseUserSpecifiedModel() to expand it to the canonical model ID before comparing against mainLoopModel or sending to the API. Two read-sites in compactConversation and streamCompactSummary are both fixed. Test updated so the 'skips cache-sharing' case uses the resolved model ID (legacy claude-opus-4-1 remaps to current default) and a new test verifies alias expansion end-to-end. Co-Authored-By: Claude Sonnet 4.6 --- src/services/compact/compact.test.ts | 48 ++++++++++++++++++++++++++-- src/services/compact/compact.ts | 13 ++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/services/compact/compact.test.ts b/src/services/compact/compact.test.ts index 129aa8d936..22cb3b0d13 100644 --- a/src/services/compact/compact.test.ts +++ b/src/services/compact/compact.test.ts @@ -677,13 +677,57 @@ describe('compactConversation provider gate', () => { }) describe('compactConversation compactModel override', () => { + test('resolves a compact model alias to full model ID before comparing and sending', async () => { + // The ModelPicker stores alias values like 'sonnet' directly; compact must + // resolve them with parseUserSpecifiedModel before comparing to mainLoopModel + // or passing to the API, so 'sonnet' → 'claude-sonnet-4-6-20251001' etc. + const compactModelAlias = 'sonnet' + const resolvedModel = _realModelModule.parseUserSpecifiedModel(compactModelAlias) + const queryModelWithStreaming = mock(async function* () { + yield { + type: 'assistant' as const, + message: { + role: 'assistant' as const, + content: [{ type: 'text' as const, text: 'Alias summary.' }], + }, + uuid: `alias-${Math.random()}`, + timestamp: new Date().toISOString(), + } + }) + + const { compactConversation } = await importCompact({ + getGlobalConfig: mock(() => ({ + ..._realConfigModule.getGlobalConfig(), + compactModel: compactModelAlias, + })), + queryModelWithStreaming, + }) + + const messages = [userMessage('Hello'), assistantMessage('Hi there!')] + const ctx = toolUseContext() + const csp = cacheSafeParams(messages) + + await compactConversation(messages, ctx, csp, false) + + expect(queryModelWithStreaming).toHaveBeenCalled() + const [{ options: streamOptions }] = queryModelWithStreaming.mock.calls[0] as unknown as [ + { options: { model: string } }, + ] + // The alias must be expanded — never sent verbatim to the API. + expect(streamOptions.model).toBe(resolvedModel) + expect(streamOptions.model).not.toBe(compactModelAlias) + }) + test('skips cache-sharing and routes streaming compaction to compactModel when it differs from mainLoopModel', async () => { // All provider env vars are cleared by beforeEach, so this would normally // be eligible for forked-agent cache-sharing (Anthropic provider). Setting // compactModel to a different model than mainLoopModel must override that. const compactModel = 'claude-opus-4-1' + // parseUserSpecifiedModel remaps legacy opus IDs to the current default — + // the resolved form is what compact.ts sends to the API. + const resolvedCompactModel = _realModelModule.parseUserSpecifiedModel(compactModel) const getMaxOutputTokensForModel = mock((model: string) => - model === compactModel ? 4096 : 8192, + model === resolvedCompactModel ? 4096 : 8192, ) const queryModelWithStreaming = mock(async function* () { yield { @@ -721,7 +765,7 @@ describe('compactConversation compactModel override', () => { const [{ options: streamOptions }] = queryModelWithStreaming.mock.calls[0] as unknown as [ { options: { model: string; maxOutputTokensOverride: number } }, ] - expect(streamOptions.model).toBe(compactModel) + expect(streamOptions.model).toBe(resolvedCompactModel) expect(streamOptions.maxOutputTokensOverride).toBe(4096) }) }) diff --git a/src/services/compact/compact.ts b/src/services/compact/compact.ts index 0ab82e281d..726f8a060e 100644 --- a/src/services/compact/compact.ts +++ b/src/services/compact/compact.ts @@ -98,6 +98,7 @@ import { } from '../../utils/toolSearch.js' import { getFeatureValue_CACHED_MAY_BE_STALE } from '../analytics/growthbook.js' import { isAnthropicProvider } from '../../utils/betas.js' +import { parseUserSpecifiedModel } from '../../utils/model/model.js' import { isGithubNativeAnthropicMode } from '../../utils/model/providers.js' import { type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, @@ -459,7 +460,11 @@ export async function compactConversation( // The GB flag is kept as a kill-switch. // streamCompactSummary() (below) follows the same gate; see also the // provider-gate tests in src/services/compact/compact.test.ts. - const compactModel = getGlobalConfig().compactModel + const rawCompactModel = getGlobalConfig().compactModel + const compactModel = + rawCompactModel !== undefined + ? parseUserSpecifiedModel(rawCompactModel) + : undefined const modelChangesForCompaction = compactModel !== undefined && compactModel !== context.options.mainLoopModel const promptCacheSharingEnabled = @@ -1190,7 +1195,11 @@ async function streamCompactSummary({ // prompt cache; other 3P providers are incompatible and would send // Anthropic-only params that they reject. The shared predicate makes this // safe to call from 3P provider paths. - const compactModel = getGlobalConfig().compactModel + const rawCompactModel = getGlobalConfig().compactModel + const compactModel = + rawCompactModel !== undefined + ? parseUserSpecifiedModel(rawCompactModel) + : undefined const modelChangesForCompaction = compactModel !== undefined && compactModel !== context.options.mainLoopModel const cacheSharingAvailable = isCompactionCacheSharingCompatible(