Skip to content

Commit 8a18bbf

Browse files
MarcoDotIOclaude
andauthored
perf(client): repeated server settings keep the same settings object (#27818)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 58f8ab4 commit 8a18bbf

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

‎packages/client-runtime/src/state/serverConfigProjection.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import type { ServerConfig, ServerConfigStreamEvent } from "@t3tools/contracts";
1+
import {
2+
DEFAULT_SERVER_SETTINGS,
3+
ServerSettings,
4+
type ServerConfig,
5+
type ServerConfigStreamEvent,
6+
} from "@t3tools/contracts";
27
import * as Option from "effect/Option";
8+
import * as Schema from "effect/Schema";
39
import { describe, expect, it } from "vite-plus/test";
410

511
import { applyServerConfigProjection, reuseUnchanged } from "./serverConfigProjection.ts";
612

13+
const encodeSettings = Schema.encodeSync(ServerSettings);
14+
const decodeSettings = Schema.decodeSync(ServerSettings);
15+
716
describe("reuseUnchanged", () => {
817
const provider = (checkedAt: string, skills: ReadonlyArray<string>) => ({
918
instanceId: "claude",
@@ -53,4 +62,20 @@ describe("applyServerConfigProjection settingsUpdated", () => {
5362
expect(nextSettings.providerInstances.codex).toBe(settings.providerInstances.codex);
5463
expect(nextSettings.providerInstances.claude.config.launchArgs).toBe("--verbose");
5564
});
65+
66+
it("keeps the settings object when an update repeats the current settings", () => {
67+
const wire = encodeSettings(DEFAULT_SERVER_SETTINGS);
68+
const decode = () => decodeSettings(structuredClone(wire));
69+
const settings = decode();
70+
const current = Option.some({
71+
config: { settings } as unknown as ServerConfig,
72+
latestEvent: {} as ServerConfigStreamEvent,
73+
source: "live" as const,
74+
});
75+
const next = applyServerConfigProjection(current, {
76+
type: "settingsUpdated",
77+
payload: { settings: decode() },
78+
} as ServerConfigStreamEvent);
79+
expect(Option.getOrThrow(next).config).toBe(Option.getOrThrow(current).config);
80+
});
5681
});

‎packages/client-runtime/src/state/serverConfigProjection.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ServerConfig, ServerConfigStreamEvent } from "@t3tools/contracts";
2+
import * as Equal from "effect/Equal";
23
import * as Option from "effect/Option";
34

45
export interface ServerConfigProjection {
@@ -44,6 +45,8 @@ export function reuseUnchanged<T>(previous: unknown, next: T): T {
4445
});
4546
return (same ? previous : merged) as T;
4647
}
48+
// Decoded settings carry Durations, which are fresh instances on every push.
49+
if (Equal.isEqual(next)) return (Equal.equals(previous, next) ? previous : next) as T;
4750
if (Object.getPrototypeOf(next) !== Object.prototype) return next;
4851
const before = previous as Record<string, unknown>;
4952
const after = next as Record<string, unknown>;
@@ -104,14 +107,17 @@ export function applyServerConfigProjection(
104107
source: "live",
105108
}));
106109
case "settingsUpdated":
107-
return Option.map(current, (projection) => ({
108-
config: {
109-
...projection.config,
110-
settings: reuseUnchanged(projection.config.settings, event.payload.settings),
111-
},
112-
latestEvent: event,
113-
source: "live",
114-
}));
110+
return Option.map(current, (projection) => {
111+
const settings = reuseUnchanged(projection.config.settings, event.payload.settings);
112+
return {
113+
config:
114+
settings === projection.config.settings
115+
? projection.config
116+
: { ...projection.config, settings },
117+
latestEvent: event,
118+
source: "live",
119+
};
120+
});
115121
case "environmentThemesUpdated":
116122
return Option.map(current, (projection) => ({
117123
config: {

0 commit comments

Comments
 (0)