Skip to content

Commit b39308c

Browse files
committed
fix: prevent context condensing on settings save when provider/model unchanged
Fixes #4430 The issue was that saving settings during an active task would unconditionally rebuild the API handler in both upsertProviderProfile and activateProviderProfile, even when the provider and model hadn't changed. This caused transient changes to model metadata (contextWindow, maxTokens) which triggered the allowedTokens fast-path in context condensing logic, causing immediate condensing regardless of the configured threshold. Changes: - Added guard in upsertProviderProfile to only rebuild API handler when provider OR model changes - Added guard in activateProviderProfile to only rebuild API handler when provider OR model changes - Added comprehensive test suite to verify handler is not rebuilt unnecessarily This prevents the "condense on save" behavior reported in issue #4430 while still rebuilding the handler when genuinely needed (provider or model change).
1 parent fc2147a commit b39308c

File tree

2 files changed

+543
-2
lines changed

2 files changed

+543
-2
lines changed

src/core/webview/ClineProvider.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
ORGANIZATION_ALLOW_ALL,
4343
DEFAULT_MODES,
4444
DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
45+
getModelId,
4546
} from "@roo-code/types"
4647
import { TelemetryService } from "@roo-code/telemetry"
4748
import { CloudService, BridgeOrchestrator, getRooCodeApiUrl } from "@roo-code/cloud"
@@ -1344,7 +1345,19 @@ export class ClineProvider
13441345
// TODO: We should rename `buildApiHandler` for clarity (e.g. `getProviderClient`).
13451346
const task = this.getCurrentTask()
13461347

1347-
if (task) {
1348+
if (task && task.apiConfiguration) {
1349+
// Only rebuild API handler if provider or model actually changed
1350+
// to avoid triggering unnecessary context condensing
1351+
const currentProvider = task.apiConfiguration.apiProvider
1352+
const newProvider = providerSettings.apiProvider
1353+
const currentModelId = getModelId(task.apiConfiguration)
1354+
const newModelId = getModelId(providerSettings)
1355+
1356+
if (currentProvider !== newProvider || currentModelId !== newModelId) {
1357+
task.api = buildApiHandler(providerSettings)
1358+
}
1359+
} else if (task) {
1360+
// Fallback: rebuild if apiConfiguration is not available
13481361
task.api = buildApiHandler(providerSettings)
13491362
}
13501363
} else {
@@ -1405,7 +1418,19 @@ export class ClineProvider
14051418
// Change the provider for the current task.
14061419
const task = this.getCurrentTask()
14071420

1408-
if (task) {
1421+
if (task && task.apiConfiguration) {
1422+
// Only rebuild API handler if provider or model actually changed
1423+
// to avoid triggering unnecessary context condensing
1424+
const currentProvider = task.apiConfiguration.apiProvider
1425+
const newProvider = providerSettings.apiProvider
1426+
const currentModelId = getModelId(task.apiConfiguration)
1427+
const newModelId = getModelId(providerSettings)
1428+
1429+
if (currentProvider !== newProvider || currentModelId !== newModelId) {
1430+
task.api = buildApiHandler(providerSettings)
1431+
}
1432+
} else if (task) {
1433+
// Fallback: rebuild if apiConfiguration is not available
14091434
task.api = buildApiHandler(providerSettings)
14101435
}
14111436

0 commit comments

Comments
 (0)