GeezerVison is showing github.com — fix(web): keep a shared instance ID even when environments report oth… · pingdotgg/t3code@edaf9c0
File treeExpand file treeCollapse file tree Open diff view settings apps/web/src/components/settings
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/settings/scopedModelSelection.ts +16-48Lines changed: 16 additions & 48 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,52 +39,34 @@ export interface ScopedModelEnvironment { | 39 | 39 | readonly settings: Pick<ServerSettings, "textGenerators">; | 40 | 40 | } | 41 | 41 |
| 42 | | -const identity = ({ auth }: ServerProvider) => [ | 43 | | - auth.type, | 44 | | - auth.email?.toLowerCase(), | 45 | | - auth.workspaceId, | 46 | | -]; | 47 | | - | 48 | | -/** Unreported login, email, or workspace is unknown, not a conflict. */ | 49 | | -const knownConflict = (first: ServerProvider, second: ServerProvider) => | 50 | | - first.driver !== second.driver || | 51 | | - identity(first).some((value, index) => { | 52 | | - const other = identity(second)[index]; | 53 | | - return value && other && value !== other; | 54 | | - }); | 55 | | - | 56 | 42 | /** Instance IDs and names are machine-local; only a reported ChatGPT login identifies the account. */ | 57 | | -const chatGptAccount = (provider: ServerProvider) => | 58 | | - provider.driver === "codex" && | 59 | | - provider.auth.status === "authenticated" && | 60 | | - provider.auth.type === "chatgpt" && | 61 | | - provider.auth.email && | 62 | | - provider.auth.workspaceId | 63 | | - ? JSON.stringify(identity(provider)) | | 43 | +const chatGptAccount = ({ driver, auth }: ServerProvider) => | | 44 | + driver === "codex" && | | 45 | + auth.status === "authenticated" && | | 46 | + auth.type === "chatgpt" && | | 47 | + auth.email && | | 48 | + auth.workspaceId | | 49 | + ? `${auth.email.toLowerCase()}\n${auth.workspaceId}` | 64 | 50 | : null; | 65 | 51 |
| 66 | 52 | const find = (providers: readonly ServerProvider[], instanceId: ProviderInstanceId) => | 67 | 53 | providers.find((provider) => provider.instanceId === instanceId); | 68 | 54 |
| | 55 | +/** Keeps an ID the destination has; remaps an absent one only through a unique ChatGPT account. */ | 69 | 56 | export function resolveScopedModelInstanceId( | 70 | 57 | instanceId: ProviderInstanceId, | 71 | 58 | sourceEnvironment: Pick<ScopedModelEnvironment, "providers">, | 72 | 59 | destination: ScopedModelEnvironment, | 73 | | - peers: readonly ServerProvider[] = [], | 74 | 60 | key: ScopedModelSettingKey = "defaultModelSelection", | 75 | 61 | ): ProviderInstanceId | null { | 76 | | - const source = find(sourceEnvironment.providers, instanceId); | 77 | | - if (!source) return null; | 78 | 62 | if ( | 79 | 63 | key === "reviewGuideModelSelection" && | 80 | 64 | scopedTextGenerator(destination.settings, instanceId, key) | 81 | 65 | ) | 82 | 66 | return null; | 83 | | - const sameId = find(destination.providers, instanceId); | 84 | | - const compatible = (provider: ServerProvider) => | 85 | | - !peers.some((peer) => knownConflict(peer, provider)); | 86 | | - if (sameId && !knownConflict(source, sameId) && compatible(sameId)) return instanceId; | 87 | | - const account = chatGptAccount(source); | | 67 | + if (find(destination.providers, instanceId)) return instanceId; | | 68 | + const source = find(sourceEnvironment.providers, instanceId); | | 69 | + const account = source ? chatGptAccount(source) : null; | 88 | 70 | if (account === null) return null; | 89 | 71 | const matches = (providers: readonly ServerProvider[]) => | 90 | 72 | providers.filter((provider) => chatGptAccount(provider) === account); | @@ -93,7 +75,6 @@ export function resolveScopedModelInstanceId( | 93 | 75 | return match && | 94 | 76 | rest.length === 0 && | 95 | 77 | matches(sourceEnvironment.providers).length === 1 && | 96 | | - compatible(match) && | 97 | 78 | scopedTextGenerator(destination.settings, match.instanceId, key) === undefined | 98 | 79 | ? match.instanceId | 99 | 80 | : null; | @@ -117,24 +98,11 @@ export function scopedModelSelectionsEqual( | 117 | 98 | Equal.equals(first, second) | 118 | 99 | ); | 119 | 100 | } | 120 | | - const firstProvider = find(firstEnvironment.providers, first.instanceId); | 121 | | - const secondProvider = find(secondEnvironment.providers, second.instanceId); | 122 | 101 | const sameInstance = | 123 | | - first.instanceId === second.instanceId | 124 | | - ? !(firstProvider && secondProvider && knownConflict(firstProvider, secondProvider)) | 125 | | - : resolveScopedModelInstanceId( | 126 | | - first.instanceId, | 127 | | - firstEnvironment, | 128 | | - secondEnvironment, | 129 | | - [], | 130 | | - key, | 131 | | - ) === second.instanceId && | 132 | | - resolveScopedModelInstanceId( | 133 | | - second.instanceId, | 134 | | - secondEnvironment, | 135 | | - firstEnvironment, | 136 | | - [], | 137 | | - key, | 138 | | - ) === first.instanceId; | | 102 | + first.instanceId === second.instanceId || | | 103 | + (resolveScopedModelInstanceId(first.instanceId, firstEnvironment, secondEnvironment, key) === | | 104 | + second.instanceId && | | 105 | + resolveScopedModelInstanceId(second.instanceId, secondEnvironment, firstEnvironment, key) === | | 106 | + first.instanceId); | 139 | 107 | return sameInstance && Equal.equals({ ...first, instanceId: second.instanceId }, second); | 140 | 108 | } |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/settings/scopedSettings.test.ts +9-29Lines changed: 9 additions & 29 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -869,18 +869,12 @@ describe("model selections across environments' provider instances", () => { | 869 | 869 | | 870 | 870 | const team = { workspaceId: "team" }; | 871 | 871 | const apiKey = { type: "apiKey" }; | 872 | | - const none = { type: undefined, email: undefined, workspaceId: undefined }; | 873 | 872 | const nameOnly = (id: string) => ({ ...a(id, { workspaceId: undefined }), displayName: "Gmail" }); | 874 | 873 | const claude = "claudeAgent"; | 875 | | - const cursor = "cursor"; | 876 | 874 | it.each([ | 877 | 875 | ["same ID takes precedence", [a("x"), a("l")], [a("s"), a("x")], "x"], | 878 | | - ["same ID, email case differs", [a("x")], [a("x", lower)], "x"], | 879 | | - ["same ID, unknown identity", [a("x", {}, cursor)], [a("x", none, cursor)], "x"], | 880 | | - ["conflicting ID beside matching account", [a("l")], [a("l", other), a("s")], "s"], | 881 | | - ["the same ID on another account", [a("x")], [a("x", other)], null], | 882 | | - ["the same ID with another login", [a("x")], [a("x", apiKey)], null], | 883 | | - ["the same ID on another driver", [a("x")], [a("x", {}, claude)], null], | | 876 | + ["the same ID on another account", [a("x", {}, claude)], [a("x", other, claude)], "x"], | | 877 | + ["case-insensitive email", [a("l")], [a("s", lower)], "s"], | 884 | 878 | ["Claude, different IDs", [a("l", {}, claude)], [a("s", {}, claude)], null], | 885 | 879 | ["an API key account", [a("l", apiKey)], [a("s", apiKey)], null], | 886 | 880 | ["another workspace", [a("l")], [a("s", team)], null], | @@ -929,12 +923,6 @@ describe("model selections across environments' provider instances", () => { | 929 | 923 | "textGenerationModelSelection", | 930 | 924 | ), | 931 | 925 | ).toBe(true); | 932 | | - expect( | 933 | | - scopedModelSelectionsEqual(selection, generator, selection, { | 934 | | - ...generator, | 935 | | - providers: [a(id, other)], | 936 | | - }), | 937 | | - ).toBe(false); | 938 | 926 | expect( | 939 | 927 | planScopedSettingsPatch( | 940 | 928 | all, | @@ -990,23 +978,15 @@ describe("model selections across environments' provider instances", () => { | 990 | 978 | expect.soft(mixed(luna, onServer)).toEqual([false, false]); | 991 | 979 | expect.soft(mixed(luna, { ...onServer, model: "gpt-6-sol" })).toEqual([true, true]); | 992 | 980 | expect.soft(mixed(x, x, new Map())).toEqual([false, false]); | 993 | | - expect.soft(mixed(x, x, providers([a("x")], [a("x", other)]))).toEqual([true, true]); | 994 | | - const third = environment("Third"); | 995 | | - const three = [target(laptop, x), target(server, x), target(third, x)]; | 996 | | - const identities = new Map([ | 997 | | - [laptop.environmentId, [a("x", none)]], | 998 | | - [server.environmentId, [a("x")]], | 999 | | - [third.environmentId, [a("x", other)]], | 1000 | | - ]); | 1001 | | - expect.soft(scopedSettingsAreMixed(three, keys, identities)).toBe(true); | 1002 | | - const connected = [laptop, server, third]; | | 981 | + // Main writes a shared ID everywhere even when each machine reports another account. | | 982 | + expect.soft(mixed(x, x, providers([a("x")], [a("x", other)]))).toEqual([false, false]); | 1003 | 983 | expect( | 1004 | 984 | planScopedSettingsPatch( | 1005 | | - resolveSettingsScope({}, [], connected), | 1006 | | - connected, | | 985 | + all, | | 986 | + environments, | 1007 | 987 | forKeys(x), | 1008 | | - from(identities), | 1009 | | - ).serverWrites, | 1010 | | - ).toEqual([]); | | 988 | + from(providers([a("x")], [a("x", other)])), | | 989 | + ).serverWrites.map((write) => write.patch), | | 990 | + ).toEqual([forKeys(x), forKeys(x)]); | 1011 | 991 | }); | 1012 | 992 | }); |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/settings/scopedSettings.ts +1-6Lines changed: 1 addition & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -249,7 +249,6 @@ function forDestinationModels( | 249 | 249 | patch: ServerSettingsPatch, | 250 | 250 | modelSource: ScopedModelSource | null | undefined, | 251 | 251 | destination: ScopedSettingsEnvironment, | 252 | | - matched: Map<string, ServerProvider[]>, | 253 | 252 | ): ServerSettingsPatch | null { | 254 | 253 | if (!modelSource || destination.environmentId === modelSource.target.environmentId) return patch; | 255 | 254 | const { target: source, providersByEnvironment } = modelSource; | @@ -274,16 +273,13 @@ function forDestinationModels( | 274 | 273 | continue; | 275 | 274 | } | 276 | 275 | if (!providers) return null; | 277 | | - const peers = matched.get(key) ?? []; | 278 | 276 | const instanceId = resolveScopedModelInstanceId( | 279 | 277 | selection.instanceId, | 280 | 278 | { providers: providersByEnvironment.get(source.environmentId) ?? [] }, | 281 | 279 | { providers, settings }, | 282 | | - peers, | 283 | 280 | key, | 284 | 281 | ); | 285 | 282 | if (instanceId === null) return null; | 286 | | - matched.set(key, [...peers, providers.find((provider) => provider.instanceId === instanceId)!]); | 287 | 283 | Object.assign(next, { [key]: { ...selection, instanceId } }); | 288 | 284 | } | 289 | 285 | return next; | @@ -314,9 +310,8 @@ export function planScopedSettingsPatch( | 314 | 310 | ? serverKeys.filter((key) => !isProjectScopedSettingKey(key)) | 315 | 311 | : []; | 316 | 312 | const unmatched = new Set<string>(); | 317 | | - const matched = new Map<string, ServerProvider[]>(); | 318 | 313 | const serverPatchFor = (environment: ScopedSettingsEnvironment) => { | 319 | | - const mapped = forDestinationModels(serverPatch, modelSource, environment, matched); | | 314 | + const mapped = forDestinationModels(serverPatch, modelSource, environment); | 320 | 315 | if (mapped === null) unmatched.add(environment.label); | 321 | 316 | return mapped ?? serverPatch; | 322 | 317 | }; | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/settings/useScopedModelAvailability.ts +1-8Lines changed: 1 addition & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { | 2 | | - type ProviderInstanceId, | 3 | | - type ServerProvider, | 4 | | - type UnifiedSettings, | 5 | | -} from "@t3tools/contracts"; | | 1 | +import { type ProviderInstanceId, type UnifiedSettings } from "@t3tools/contracts"; | 6 | 2 |
| 7 | 3 | import { mergeEnvironmentSettings } from "../../hooks/useSettings"; | 8 | 4 | import { getCustomModelOptionsByInstance } from "../../modelSelection"; | @@ -34,7 +30,6 @@ export function useScopedModelDisabledReason( | 34 | 30 | providers: (source && providersByEnvironment.get(source.environmentId)) ?? [], | 35 | 31 | }; | 36 | 32 | const shownGenerator = scopedTextGenerator(settings, instanceId, key); | 37 | | - const peers: ServerProvider[] = []; | 38 | 33 | for (const candidate of targets) { | 39 | 34 | const environment = environments.find( | 40 | 35 | (entry) => entry.environmentId === candidate.environmentId, | @@ -59,13 +54,11 @@ export function useScopedModelDisabledReason( | 59 | 54 | instanceId, | 60 | 55 | sourceEnvironment, | 61 | 56 | { providers, settings: candidate.settings }, | 62 | | - peers, | 63 | 57 | key, | 64 | 58 | ); | 65 | 59 | if (localId === null) { | 66 | 60 | return `A unique matching account could not be verified on ${environment.label}. Select that environment to choose its model separately.`; | 67 | 61 | } | 68 | | - peers.push(providers.find((provider) => provider.instanceId === localId)!); | 69 | 62 | const entry = applyProviderInstanceSettings( | 70 | 63 | deriveProviderInstanceEntries(providers), | 71 | 64 | candidate.settings, | |
|