@devries-cameron20 / t3code Mirror · read-only / commits / 061dc79e3b
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 22, 13:15 UTC

fix(usage): reject cyclic model mappings before saving (#23963)

Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Victor Vogel committed Oct 29, 2026 at 12:54 UTC 061dc79e3bf7a56709fda2c5dd71b6dcefe4d787
2 files changed +68 -1
apps/web/src/components/usage/usagePriceTable.test.ts
+48 -1
@@ -1,6 +1,11 @@
1 import { EnvironmentId } from "@t3tools/contracts";
2 import { describe, expect, it } from "vite-plus/test";
3 -import { usagePriceCell, usagePriceTableChanges, type UsagePriceDraft } from "./usagePriceTable";
3 +import {
4 + usagePriceCell,
5 + usagePriceTableChanges,
6 + usagePriceTableErrors,
7 + type UsagePriceDraft,
8 +} from "./usagePriceTable";
9 import type { UsagePriceTarget } from "./usagePriceTargets";
10
11 const price = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
@@ -116,3 +121,45 @@ describe("price table edits", () => {
121 ).toBe("Map to a different model.");
122 });
123 });
124 +
125 +describe("model mapping validation", () => {
126 + const mapping = (model: string, alias: string): UsagePriceDraft => ({
127 + id: `model:${model}`,
128 + model,
129 + alias,
130 + isNew: false,
131 + values: {},
132 + });
133 +
134 + it("rejects a cycle created by several mappings in one save", () => {
135 + const edits = [mapping("a", "b"), mapping("b", "c"), mapping("c", "a")];
136 + const result = usagePriceTableChanges(target("local", {}), edits);
137 + expect([...result.errors.keys()]).toEqual(["model:a", "model:b", "model:c"]);
138 + });
139 +
140 + it("checks each environment's saved chain when an edited mapping closes it", () => {
141 + const edits = [mapping("c", "a")];
142 + const acyclic = target("one", {}, { a: "b" });
143 + const cyclic = target("two", {}, { a: "b", b: "c" });
144 + expect(usagePriceTableChanges(acyclic, edits).errors.size).toBe(0);
145 + expect(usagePriceTableErrors([acyclic, cyclic], edits).get("model:c")).toBe(
146 + "Model mappings cannot form a cycle on two.",
147 + );
148 + });
149 +
150 + it("validates the final batch so a mapping can be reversed in either row order", () => {
151 + const environment = target("local", {}, { constructor: "__proto__" });
152 + const clear = mapping("constructor", "");
153 + const reverse = mapping("__proto__", "constructor");
154 + for (const edits of [
155 + [reverse, clear],
156 + [clear, reverse],
157 + ]) {
158 + const result = usagePriceTableChanges(environment, edits);
159 + expect(result.errors.size).toBe(0);
160 + expect(result.changes).toEqual(
161 + edits.map(({ model, alias }) => ({ model, alias: alias || null })),
162 + );
163 + }
164 + });
165 +});
apps/web/src/components/usage/usagePriceTable.ts
+20
@@ -115,6 +115,26 @@ export function usagePriceTableChanges(
115 if (USAGE_PRICE_FIELDS.some((field) => original[field.key] !== next[field.key]))
116 changes.push(parsed);
117 }
118 + // Check the final batch so clearing a mapping and reversing it in one save stays valid.
119 + const aliases = new Map(Object.entries(target.aliases ?? {}));
120 + for (const change of changes) {
121 + if (!("alias" in change)) continue;
122 + if (change.alias === null) aliases.delete(change.model);
123 + else aliases.set(change.model, change.alias);
124 + }
125 + for (const draft of drafts) {
126 + if (draft.removed || errors.has(draft.id)) continue;
127 + let model = draft.model.trim();
128 + const seen = new Set<string>();
129 + while (aliases.has(model)) {
130 + if (seen.has(model)) {
131 + errors.set(draft.id, `Model mappings cannot form a cycle on ${target.label}.`);
132 + break;
133 + }
134 + seen.add(model);
135 + model = aliases.get(model)!;
136 + }
137 + }
138 return { changes, errors };
139 }
140