fix(agents): keep compaction on live session model (#95713)

Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
NikandPeter Steinberger authored and GitHub committed 2026-07-21 00:22:58 -07:00
1 parent 19fb605fc4
commit eeb7205fa2
3 files changed
+114 -4

No files matched your search

@@ -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,
@@ -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",
});
});
});
@@ -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",
};
}