Skip to content

Commit 594f876

Browse files
fix(server): match hubs to instances by URL origin
Comparing hosts as text missed an endpoint that spells out its default port and matched one on the same host over another scheme. Compare normalized origins instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 58ce7e8 commit 594f876

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

‎apps/server/src/usage/UsageLimitSources.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ describe("instanceHubIds", () => {
182182
[UsageLimitSourceId.make("fleet")]: source("https://hub.test:8443/"),
183183
[UsageLimitSourceId.make("local")]: source("http://127.0.0.1:8317"),
184184
[UsageLimitSourceId.make("off")]: source("https://off.test", false),
185+
[UsageLimitSourceId.make("plain")]: source("https://plain.test"),
185186
},
186187
providerInstances: {
187188
[ProviderInstanceId.make("claudeProxy")]: {
@@ -196,6 +197,13 @@ describe("instanceHubIds", () => {
196197
launchArgs: `-c 'model_providers.p={base_url="http://127.0.0.1:8317/v1"}' https://off.test`,
197198
},
198199
},
200+
[ProviderInstanceId.make("defaultPort")]: {
201+
driver: ProviderDriverKind.make("claudeAgent"),
202+
environment: [
203+
{ name: "ANTHROPIC_BASE_URL", value: "https://plain.test:443/v1", sensitive: false },
204+
{ name: "OTHER_URL", value: "http://hub.test:8443", sensitive: false },
205+
],
206+
},
199207
[ProviderInstanceId.make("otherPort")]: {
200208
driver: ProviderDriverKind.make("codex"),
201209
config: { launchArgs: `-c 'model_providers.p={base_url="http://127.0.0.1:83170/v1"}'` },
@@ -206,6 +214,7 @@ describe("instanceHubIds", () => {
206214
it.each([
207215
["claudeProxy", ["fleet"]],
208216
["codexProxy", ["local"]],
217+
["defaultPort", ["plain"]],
209218
["otherPort", []],
210219
["missing", []],
211220
])("matches %s to the enabled hubs its settings name", (instanceId, expected) => {

‎apps/server/src/usage/UsageLimitSources.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,19 @@ export function instanceHubIds(
208208
): ReadonlySet<string> {
209209
const instance = settings.providerInstances[instanceId];
210210
if (!instance) return new Set();
211-
const routing = JSON.stringify([instance.environment ?? [], instance.config ?? null]);
211+
const origins = new Set(
212+
(
213+
JSON.stringify([instance.environment ?? [], instance.config ?? null]).match(
214+
/https?:\/\/[^\s"'\\]+/gi,
215+
) ?? []
216+
).flatMap((url) => (URL.canParse(url) ? [new URL(url).origin] : [])),
217+
);
212218
return new Set(
213-
Object.entries(settings.usageLimitSources).flatMap(([id, config]) => {
214-
if (!config.enabled || !URL.canParse(config.url)) return [];
215-
const host = new URL(config.url).host.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
216-
return new RegExp(`//${host}(?![\\w.:-])`, "i").test(routing) ? [id] : [];
217-
}),
219+
Object.entries(settings.usageLimitSources).flatMap(([id, config]) =>
220+
config.enabled && URL.canParse(config.url) && origins.has(new URL(config.url).origin)
221+
? [id]
222+
: [],
223+
),
218224
);
219225
}
220226

0 commit comments

Comments
 (0)