Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(models): resolve relay model context from the weight owner's record

pengwei269446668361c6d480de8
diff --git a/apps/desktop/electron/main/models-dev-catalog.ts b/apps/desktop/electron/main/models-dev-catalog.ts
index a8a6b310f..4a775851e 100644
--- a/apps/desktop/electron/main/models-dev-catalog.ts
+++ b/apps/desktop/electron/main/models-dev-catalog.ts
@@ -17,6 +17,7 @@ import {
stripReleaseSuffix,
stripVariantSuffix,
} from "@pi-desktop/shared";
+import { RELAY_FAMILY_PUBLISHERS } from "./pi-model-metadata.ts";
import type {
ModelCost,
ModelCostTier,
@@ -1084,27 +1085,6 @@ function sameModelSpelling(catalogId: string, requested: string): boolean {
);
}
-/**
- * Record to use when nothing identifies the row's provider.
- *
- * A relay or a gateway the catalog cannot place still serves models the catalog
- * knows, and asking for one of them by id used to answer nothing at all. What
- * every publisher of that model agrees on can be claimed instead: the same
- * intersection and median `borrowedModel` computes for an anchored row.
- *
- * One thing is deliberately not claimed. Which *wire shape* a deployment accepts
- * for reasoning is a property of that deployment — the same model behind an
- * OpenAI-compatible gateway and behind Anthropic's own API takes different
- * reasoning fields — so a record borrowed this way drops its `reasoningOptions`
- * rather than speaking for an endpoint no publisher describes. `catalogModelConfigFor`
- * still applies Anthropic's own shape to an Anthropic Messages row that is left
- * without one.
- */
-function unanchoredConsensus(entries: readonly ModelsDevModel[]): ModelsDevModel | undefined {
- const consensus = borrowedModel(entries);
- return consensus ? { ...consensus, reasoningOptions: undefined } : undefined;
-}
-
/**
* Which publisher's records answer for an id the row's own publisher lacks.
*
@@ -1193,6 +1173,40 @@ function dropDeploymentMarker(value: string): string {
return DEPLOYMENT_MARKER_SUFFIXES.has(match[2].toLowerCase()) ? match[1] : value;
}
+/** The last `/`-segment of a normalized id; the whole id when it has none. */
+function leafOf(normalized: string): string {
+ return normalized.slice(normalized.lastIndexOf("/") + 1);
+}
+
+/**
+ * The narrow deployment labels the legacy relay lookup was allowed to strip to
+ * reach a published base ID. Model variants such as `thinking`, `preview`,
+ * dated releases, `asr` or `pro` name distinct models and are deliberately
+ * absent: they must keep their own records or stay unmatched.
+ */
+const RELAY_DEPLOYMENT_SUFFIXES: ReadonlySet<string> = new Set([
+ "test",
+ "staging",
+ "canary",
+ "dev",
+ "1m",
+ "2m",
+ "4m",
+ "32k",
+ "64k",
+ "128k",
+ "200k",
+ "256k",
+ "512k",
+]);
+
+/** The id without one whitelisted trailing label, or undefined when it has none. */
+function relayDeploymentSuffixStrip(value: string): string | undefined {
+ const match = /^(.*)[-_:]([a-z0-9]+)$/i.exec(value);
+ if (!match || !RELAY_DEPLOYMENT_SUFFIXES.has(match[2].toLowerCase())) return undefined;
+ return match[1];
+}
+
/**
* Published ids to read when nothing the catalog publishes matches the id the
* service serves: the id behind a route prefix, and that id without one
@@ -1553,13 +1567,15 @@ export class ModelsDevCatalog {
dropping the model to the generic shape.
This only fills a miss the lookup already had — a record the preferred
- provider does publish stays authoritative. Without that anchor the scores
- prove nothing about identity, and an unknown endpoint keeps the existing
- behaviour: a unique unambiguous match, official disambiguation, shared
- capabilities, or nothing. Deployment-marker / variant-suffix fallbacks
- are intentionally not applied here (approved #1047 matching rules). */
+ provider does publish stays authoritative. A row the catalog cannot place
+ (an unidentified relay) still answers when the model's own family names
+ a publisher the catalog ships: the weight owner's record is what
+ describes the model the relay serves, not a reseller's flags about its
+ own deployment. Deployment-marker / variant-suffix fallbacks are
+ intentionally not applied to the borrowed result (approved #1047
+ matching rules). */
const borrowed = result ?? deploymentFallback ??
- (preferredProvider ? this.borrowedAcrossProviders(input) : undefined);
+ (preferredProvider ? this.borrowedAcrossProviders(input) : this.unanchoredOwnerRecord(requested));
// Cache the result (a miss included) so a repeated miss is also O(1) and
// cannot grow the candidate index with query-dependent keys.
this.lookupMemo.set(memoKey, borrowed);
@@ -1668,6 +1684,55 @@ export class ModelsDevCatalog {
return borrowedModel(borrowPool(matches, requested));
}
+ /**
+ * Unanchored relay fallback: the endpoint's own publisher is unknown, so
+ * nothing owns a negative answer for this id. Resolve it the way the
+ * pre-catalog relay lookup did — the model's own family names the
+ * publisher that owns its weights, and that owner's published record is
+ * what describes the model a relay serves, not a reseller's flags about
+ * its own deployment. A relay-only `deepseek-v4-flash` reads DeepSeek's
+ * record and keeps its window and output visible instead of dropping to
+ * the generic 128k text-only seed.
+ *
+ * Two rules keep the result honest:
+ *
+ * - Only the owner's own records answer. Reseller copies and route
+ * prefixes never participate, and a split owner answer still states
+ * nothing.
+ * - The wire ID is never rewritten: at most one whitelisted deployment
+ * label (`-1m`, `-test`, …) is stripped to reach the owner's published
+ * base ID; model variants such as `-thinking` or dated releases are
+ * distinct models and stay unmatched.
+ */
+ private unanchoredOwnerRecord(requested: string): ModelsDevModel | undefined {
+ const ownerKey = RELAY_FAMILY_PUBLISHERS[leafOf(requested).split(/[-.]/)[0] ?? ""];
+ if (!ownerKey) return undefined;
+ const ownerKeys = new Set(providerKeyCandidates(ownerKey));
+ const ownerHits = (id: string): ModelsDevModel[] =>
+ (this.lookupIndex?.candidates(id) ?? [])
+ .filter(({ model, provider }) =>
+ normalizedModelId(model.modelId) === id &&
+ ownerKeys.has(normalizedProviderKey(provider.providerKey)),
+ )
+ .map((entry) => entry.model);
+ let lookupId = leafOf(requested);
+ let hits = ownerHits(lookupId);
+ // Remove one whitelisted trailing label at a time, stopping at the
+ // nearest published base ID; never rewrite the id used on the wire.
+ while (hits.length === 0) {
+ const base = relayDeploymentSuffixStrip(lookupId);
+ if (!base) break;
+ lookupId = base;
+ hits = ownerHits(lookupId);
+ }
+ if (hits.length === 1) return hits[0];
+ if (hits.length > 1) {
+ const signature = capabilitySignature(hits[0]);
+ if (hits.every((model) => capabilitySignature(model) === signature)) return hits[0];
+ }
+ return undefined;
+ }
+
/**
* Thinking wire metadata Anthropic publishes for exactly this id, or nothing.
* Aliases and other publishers' copies never answer: only Anthropic's own
diff --git a/apps/desktop/electron/main/pi-model-metadata.ts b/apps/desktop/electron/main/pi-model-metadata.ts
index aa20fa1c6..af518549a 100644
--- a/apps/desktop/electron/main/pi-model-metadata.ts
+++ b/apps/desktop/electron/main/pi-model-metadata.ts
@@ -17,7 +17,7 @@ export const PI_VENDOR_ALIASES: Record<string, string> = {
* the configured wire ID is never rewritten, and identified providers remain
* authoritative.
*/
-const publishers: Record<string, string> = {
+export const RELAY_FAMILY_PUBLISHERS: Record<string, string> = {
claude: "anthropic", gpt: "openai", o1: "openai", o3: "openai", o4: "openai",
gemini: "google", grok: "xai",
// Open-weight families whose owner publishes the IDs a relay actually serves.
@@ -59,7 +59,7 @@ export function relayChatMetadata(models: Models, id: string): Model<Api> | unde
hits = findHits(lookupId);
}
if (!hits.length) return undefined;
- const publisher = publishers[lookupId.split(/[-.]/)[0] ?? ""];
+ const publisher = RELAY_FAMILY_PUBLISHERS[lookupId.split(/[-.]/)[0] ?? ""];
const official = hits.filter(model => model.provider === publisher);
if (official.length === 1) return official[0];
return hits.every(model => capabilities(model) === capabilities(hits[0])) ? hits[0] : undefined;
diff --git a/apps/desktop/test/models-dev-catalog.test.mjs b/apps/desktop/test/models-dev-catalog.test.mjs
index 7d79bf3e8..2bc87cdb6 100644
--- a/apps/desktop/test/models-dev-catalog.test.mjs
+++ b/apps/desktop/test/models-dev-catalog.test.mjs
@@ -742,7 +742,9 @@ test("resellers still answer for an id no shipped publisher states", async (t) =
test("route prefixes and official deployment markers resolve without rewriting served IDs", async (t) => {
// Exact last-segment match allows `test/mimo-v2.5` → `mimo-v2.5`.
- // Markers like `-thinking` / `-test` are part of the leaf and do not strip.
+ // Model variants like `-thinking` / `-asr` are parts of the leaf and never
+ // strip; a whitelisted deployment label (`-test`, `-1m`) still reduces to
+ // the owner's published base ID.
const catalog = await loadFixtureCatalog(t, {
gateway: { api: "https://gateway.example/v1", models: {} },
google: { models: {
@@ -773,7 +775,7 @@ test("route prefixes and official deployment markers resolve without rewriting s
assert.ok(info.capabilities.includes("vision"));
assert.equal(relay("mimo-v2.5-thinking"), undefined);
- assert.equal(relay("test/mimo-v2.5-pro-test"), undefined);
+ assert.equal(relay("test/mimo-v2.5-pro-test")?.modelId, "mimo-v2.5-pro");
assert.equal(relay("mimo-v2.5-asr"), undefined);
assert.equal(relay("gemini-2.5-pro-1m")?.modelId, "gemini-2.5-pro");
});
@@ -805,9 +807,12 @@ test("a shipped publisher answers first for a row anchored to a reseller", async
assert.equal(match.limit.output, 500_000);
});
-test("ambiguous same-leaf spellings with conflicting modalities stay unmatched", async (t) => {
+test("an unanchored relay reads its family owner's record over a conflicting reseller spelling", async (t) => {
// `mimo-v2.5` and `XiaomiMiMo/MiMo-V2.5` share a case-insensitive leaf but
- // disagree on modalities, and neither publisher is an official/source family.
+ // disagree on modalities, so the shared-capabilities enrichment misses. The
+ // weight owner's exact record still answers an unidentified relay — the way
+ // the pre-catalog relay lookup did: a reseller's narrower copy never decides
+ // the window or modalities.
const catalog = await loadFixtureCatalog(t, {
gateway: { api: "https://gateway.example/v1", models: {} },
xiaomi: { models: { "mimo-v2.5": {
@@ -821,11 +826,14 @@ test("ambiguous same-leaf spellings with conflicting modalities stay unmatched",
limit: { context: 1_048_576, output: 131_072 },
} } },
});
- assert.equal(catalog.findModel({
+ const match = catalog.findModel({
vendorKey: "custom",
baseUrl: "https://relay.example/v1",
modelId: "mimo-v2.5",
- }), undefined);
+ });
+ assert.equal(match?.providerKey, "xiaomi", "the owner's record wins the unanchored relay");
+ assert.deepEqual(match?.modalities.input, ["text", "image", "audio"]);
+ assert.equal(match?.limit.context, 1_048_576);
});
test("unique leaf enrichment coexists with exact-id borrowing and unknown misses", async (t) => {
diff --git a/apps/desktop/test/provider-endpoint-metadata.test.mjs b/apps/desktop/test/provider-endpoint-metadata.test.mjs
index 2ff6e1b7e..5a09cf54e 100644
--- a/apps/desktop/test/provider-endpoint-metadata.test.mjs
+++ b/apps/desktop/test/provider-endpoint-metadata.test.mjs
@@ -4,9 +4,10 @@
* Two reported failures live here. A custom row pointed at
* `https://open.bigmodel.cn/api/v1` (Zhipu's OpenAI Responses endpoint) listed
* its models but showed a generic 128k / 8k text-only row for every one of them,
- * because the catalog only accepted that host's own published path. And a row on
- * a relay the catalog cannot place got nothing at all for models the catalog
- * describes, because one publisher's record must never answer for another.
+ * because the catalog only accepted that host's own published path. A row on a
+ * relay the catalog cannot place used to get nothing at all; it now still reads
+ * the weight owner's published record for ids the owner states, so a relay-only
+ * `deepseek-v4-flash` keeps DeepSeek's window instead of the generic seed.
*
* This drives the real handlers with the bundled snapshot, so it pins the whole
* chain: discovery, endpoint resolution, catalog anchoring, and the hand-typed
@@ -184,15 +185,20 @@ test("a relay enriches official IDs, strips safe deployment labels, and preserve
const byId = new Map(result.models.map((model) => [model.modelId, model]));
- // No official publisher states this leaf, so a reseller must not decide it.
- assert.equal(byId.get("deepseek-v4-flash").catalogSource, undefined);
- // Reseller copies disagree; an unanchored relay stays on the generic shape.
+ // The weight owner (DeepSeek) publishes this id, so an unanchored relay reads
+ // the owner's record instead of dropping to the generic seed.
+ const deepseek = byId.get("deepseek-v4-flash");
+ assert.equal(deepseek.catalogSource, "models.dev");
+ assert.equal(deepseek.contextWindow, 1_000_000);
+ assert.equal(deepseek.maxTokens, 393_216);
+ // Xiaomi publishes the base id, so both the plain and the `-1m` alias read
+ // the owner's record without rewriting the served wire ID.
for (const id of ["mimo-v2.5-pro", "mimo-v2.5-pro-1m"]) {
const mimo = byId.get(id);
assert.equal(mimo.modelId, id); // lookup never rewrites the served wire ID
- assert.equal(mimo.catalogSource, undefined);
- assert.equal(mimo.contextWindow, 128_000);
- assert.equal(mimo.maxTokens, 8_192);
+ assert.equal(mimo.catalogSource, "models.dev");
+ assert.equal(mimo.contextWindow, 1_048_576);
+ assert.equal(mimo.maxTokens, 131_072);
}
// Matching Xiaomi reseller records agree on the published speech limits.