@devries-cameron20 / t3code Mirror · read-only / commits / 5b818b3e69
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 12:15 UTC

perf(server): resolve model aliases in one memoized traversal (#31197)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Hieu Le committed Nov 15, 2026 at 02:28 UTC 5b818b3e69690d266474f765f0dc9ce8c866305a
2 files changed +31 -16
apps/server/src/usage/usageAggregation.test.ts
+13 -9
@@ -292,20 +292,24 @@ describe("UsageAggregator", () => {
292
293 describe("resolveModelAliases", () => {
294 it("follows chains to the final model and drops chains that enter a loop", () => {
295 - expect(
296 - resolveModelAliases({
295 + expect([
296 + ...resolveModelAliases({
297 "preview[1m]": "preview",
298 preview: "example-model",
299 + shared: "preview",
300 + intoLoop: "loop",
301 loop: "back",
302 back: "loop",
301 - intoLoop: "loop",
303 + laterIntoLoop: "back",
304 + deepIntoLoop: "intoLoop",
305 self: "self",
306 + validAfterLoops: "shared",
307 }),
304 - ).toEqual(
305 - new Map([
306 - ["preview[1m]", "example-model"],
307 - ["preview", "example-model"],
308 - ]),
309 - );
308 + ]).toEqual([
309 + ["preview[1m]", "example-model"],
310 + ["preview", "example-model"],
311 + ["shared", "example-model"],
312 + ["validAfterLoops", "example-model"],
313 + ]);
314 });
315 });
apps/server/src/usage/usageAggregation.ts
+18 -7
@@ -79,15 +79,26 @@ export interface AggregateOptions {
79 export function resolveModelAliases(
80 aliases: Readonly<Record<string, string>>,
81 ): ReadonlyMap<string, string> {
82 - const resolved = new Map<string, string>();
83 - for (const model of Object.keys(aliases)) {
84 - const seen = new Set([model]);
85 - let target = aliases[model]!;
86 - while (Object.hasOwn(aliases, target) && !seen.has(target)) {
87 - seen.add(target);
82 + const models = Object.keys(aliases);
83 + const memo = new Map<string, string | null>();
84 + for (const model of models) {
85 + if (memo.has(model)) continue;
86 + const path: string[] = [];
87 + let target = model;
88 + while (Object.hasOwn(aliases, target) && !memo.has(target)) {
89 + path.push(target);
90 + // Null marks both the active walk and paths that end in a cycle.
91 + memo.set(target, null);
92 target = aliases[target]!;
93 }
90 - if (!Object.hasOwn(aliases, target)) resolved.set(model, target);
94 + const terminal = Object.hasOwn(aliases, target) ? (memo.get(target) ?? null) : target;
95 + for (const alias of path) memo.set(alias, terminal);
96 + }
97 +
98 + const resolved = new Map<string, string>();
99 + for (const model of models) {
100 + const target = memo.get(model);
101 + if (target !== null && target !== undefined) resolved.set(model, target);
102 }
103 return resolved;
104 }