From b75f70bc04427a60e89347ecd1a904c77adbbd69 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 27 May 2026 06:15:12 +0100 Subject: [PATCH] perf(gateway): avoid cloning live switch store reads --- src/agents/live-model-switch.test.ts | 26 ++++++++++++++++---------- src/agents/live-model-switch.ts | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/agents/live-model-switch.test.ts b/src/agents/live-model-switch.test.ts index 32e23acfc9d..ad0d96d3acc 100644 --- a/src/agents/live-model-switch.test.ts +++ b/src/agents/live-model-switch.test.ts @@ -398,12 +398,13 @@ describe("live model switch", () => { describe("shouldSwitchToLiveModel", () => { it("returns the persisted selection when liveModelSwitchPending is true and model differs", async () => { + const sessionEntry = { + liveModelSwitchPending: true, + providerOverride: "openai", + modelOverride: "gpt-5.4", + }; state.loadSessionStoreMock.mockReturnValue({ - main: { - liveModelSwitchPending: true, - providerOverride: "openai", - modelOverride: "gpt-5.4", - }, + main: sessionEntry, }); const { shouldSwitchToLiveModel } = await loadModule(); @@ -431,15 +432,20 @@ describe("live model switch", () => { const result = shouldSwitchToLiveModel(makeShouldSwitchParams()); expect(result).toBeUndefined(); + expect(state.loadSessionStoreMock).toHaveBeenCalledWith("/tmp/session-store.json", { + skipCache: true, + clone: false, + }); }); it("returns undefined when liveModelSwitchPending is true but models match", async () => { + const sessionEntry = { + liveModelSwitchPending: true, + providerOverride: "anthropic", + modelOverride: "claude-opus-4-6", + }; state.loadSessionStoreMock.mockReturnValue({ - main: { - liveModelSwitchPending: true, - providerOverride: "anthropic", - modelOverride: "claude-opus-4-6", - }, + main: sessionEntry, }); const { shouldSwitchToLiveModel } = await loadModule(); diff --git a/src/agents/live-model-switch.ts b/src/agents/live-model-switch.ts index ee4e38a4b4a..1062ddacd4e 100644 --- a/src/agents/live-model-switch.ts +++ b/src/agents/live-model-switch.ts @@ -159,7 +159,7 @@ export function shouldSwitchToLiveModel(params: { const storePath = resolveStorePath(cfg.session?.store, { agentId: params.agentId?.trim(), }); - const entry = loadSessionStore(storePath, { skipCache: true })[sessionKey]; + const entry = loadSessionStore(storePath, { skipCache: true, clone: false })[sessionKey]; if (!entry?.liveModelSwitchPending) { return undefined; }