From eeb7205fa2aa41819fe200e37fd2b830ccf616aa Mon Sep 17 00:00:00 2001 From: Nik <151941421+0xghost42@users.noreply.github.com> Date: Tue, 21 Jul 2026 12:52:58 +0530 Subject: [PATCH] fix(agents): keep compaction on live session model (#95713) Co-authored-by: Peter Steinberger --- .../run/attempt-recovery.ts | 18 ++++-- .../compaction-live-model-selection.test.ts | 60 +++++++++++++++++++ .../run/compaction-live-model-selection.ts | 40 +++++++++++++ 3 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 src/agents/embedded-agent-runner/run/compaction-live-model-selection.test.ts create mode 100644 src/agents/embedded-agent-runner/run/compaction-live-model-selection.ts diff --git a/src/agents/embedded-agent-runner/run/attempt-recovery.ts b/src/agents/embedded-agent-runner/run/attempt-recovery.ts index f24f0286d81..75740c38633 100644 --- a/src/agents/embedded-agent-runner/run/attempt-recovery.ts +++ b/src/agents/embedded-agent-runner/run/attempt-recovery.ts @@ -11,6 +11,7 @@ import type { prepareAndDispatchEmbeddedRunAttempt } from "./attempt-dispatch-pr import type { normalizeEmbeddedRunAttempt } from "./attempt-normalization.js"; import { buildEmbeddedRunBlockedResult } from "./blocked-run-result.js"; import { resolveCodexAppServerRecoveryRetry } from "./codex-app-server-recovery.js"; +import { resolveCompactionLiveModelSelection } from "./compaction-live-model-selection.js"; import type { createEmbeddedRunCompactionRuntime } from "./compaction-runtime.js"; import type { createEmbeddedRunContextRecoveryState } from "./context-recovery-state.js"; import type { PreparedEmbeddedRunInput } from "./execution-context.js"; @@ -154,6 +155,15 @@ export async function recoverEmbeddedRunAttempt(input: { ); throw new LiveSessionModelSwitchError(requestedSelection); } + const compactionSelection = resolveCompactionLiveModelSelection({ + current: { + provider: preparedRuntime.provider, + model: preparedRuntime.modelId, + authProfileId: runtime.lastProfileId, + authProfileIdSource: preparedRuntime.lockedProfileId ? "user" : "auto", + }, + requested: requestedSelection, + }); const commonRecoveryInput = { runParams: params, state: input.contextRecoveryState, @@ -166,12 +176,12 @@ export async function recoverEmbeddedRunAttempt(input: { sessionAgentId: input.sessionAgentId, agentDir: runInput.agentDir, workspaceDir: runInput.workspaceDir, - provider: preparedRuntime.provider, - modelId: preparedRuntime.modelId, + provider: compactionSelection.provider, + modelId: compactionSelection.model, harnessRuntime: runtime.agentHarness.id, thinkLevel: runtime.thinkLevel, - authProfileId: runtime.lastProfileId, - authProfileIdSource: preparedRuntime.lockedProfileId ? ("user" as const) : ("auto" as const), + authProfileId: compactionSelection.authProfileId, + authProfileIdSource: compactionSelection.authProfileIdSource, resolveContextEnginePluginId: input.resolveContextEnginePluginId, buildRuntimeSettings: input.buildRuntimeSettings, ...compactionRuntime, diff --git a/src/agents/embedded-agent-runner/run/compaction-live-model-selection.test.ts b/src/agents/embedded-agent-runner/run/compaction-live-model-selection.test.ts new file mode 100644 index 00000000000..a3547065c51 --- /dev/null +++ b/src/agents/embedded-agent-runner/run/compaction-live-model-selection.test.ts @@ -0,0 +1,60 @@ +import { describe, expect, it } from "vitest"; +import { resolveCompactionLiveModelSelection } from "./compaction-live-model-selection.js"; + +const current = { + provider: "anthropic", + model: "claude-sonnet-4-6", + authProfileId: "anthropic:default", + authProfileIdSource: "user" as const, +}; + +describe("resolveCompactionLiveModelSelection", () => { + it("keeps the active routing when no live switch is pending", () => { + expect(resolveCompactionLiveModelSelection({ current })).toEqual(current); + }); + + it("uses a pending model and its pinned auth profile", () => { + expect( + resolveCompactionLiveModelSelection({ + current, + requested: { + provider: "openai", + model: "gpt-5.5", + authProfileId: "openai:p1", + authProfileIdSource: "user", + }, + }), + ).toEqual({ + provider: "openai", + model: "gpt-5.5", + authProfileId: "openai:p1", + authProfileIdSource: "user", + }); + }); + + it("drops a stale profile when the pending switch changes providers", () => { + expect( + resolveCompactionLiveModelSelection({ + current, + requested: { provider: "openai", model: "gpt-5.5" }, + }), + ).toEqual({ + provider: "openai", + model: "gpt-5.5", + authProfileIdSource: "auto", + }); + }); + + it("retains the active profile for a same-provider model switch", () => { + expect( + resolveCompactionLiveModelSelection({ + current, + requested: { provider: "Anthropic", model: "claude-opus-4-6" }, + }), + ).toEqual({ + ...current, + provider: "Anthropic", + model: "claude-opus-4-6", + }); + }); +}); diff --git a/src/agents/embedded-agent-runner/run/compaction-live-model-selection.ts b/src/agents/embedded-agent-runner/run/compaction-live-model-selection.ts new file mode 100644 index 00000000000..c754ad99078 --- /dev/null +++ b/src/agents/embedded-agent-runner/run/compaction-live-model-selection.ts @@ -0,0 +1,40 @@ +import { normalizeProviderId } from "@openclaw/model-catalog-core/provider-id"; +import type { LiveSessionModelSelection } from "../../live-model-switch.js"; + +type AuthProfileSource = "auto" | "user"; + +export function resolveCompactionLiveModelSelection(params: { + current: { + provider: string; + model: string; + authProfileId?: string; + authProfileIdSource: AuthProfileSource; + }; + requested?: LiveSessionModelSelection; +}): { + provider: string; + model: string; + authProfileId?: string; + authProfileIdSource: AuthProfileSource; +} { + const { current, requested } = params; + if (!requested) { + return current; + } + if (requested.authProfileId) { + return { + provider: requested.provider, + model: requested.model, + authProfileId: requested.authProfileId, + authProfileIdSource: requested.authProfileIdSource ?? "auto", + }; + } + if (normalizeProviderId(requested.provider) === normalizeProviderId(current.provider)) { + return { ...current, provider: requested.provider, model: requested.model }; + } + return { + provider: requested.provider, + model: requested.model, + authProfileIdSource: "auto", + }; +}