Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(models): match relay IDs against official metadata
vastsa989261ae5143
diff --git a/apps/desktop/electron/main/models-dev-catalog.ts b/apps/desktop/electron/main/models-dev-catalog.tsindex 43da4a331..430a42321 100644--- a/apps/desktop/electron/main/models-dev-catalog.ts+++ b/apps/desktop/electron/main/models-dev-catalog.ts@@ -201,10 +201,28 @@ export class ModelsDevCatalog {modelConfigFor(input: CatalogTarget, unpublishedConfig?: ModelConfig): ModelConfig {const model = this.findModel(input);- if (model) return { ...(this.effectiveConfigs.get(model) ?? modelConfigFromPi(model)), ...(input.baseUrl ? { baseUrl: input.baseUrl } : {}) };+ const binding = input.providerId ? this.accountRows.get(input.providerId)?.models?.find((entry) => entry.id.trim().toLowerCase() === input.modelId.trim().toLowerCase()) : undefined;+ if (model) {+ // A wrapped account provider already applied its binding inside+ // `configureAccount`. An unidentified relay has no wrapper to apply it,+ // so the stored row is resolved here — otherwise a `user` pin on a relay+ // hit would be silently replaced by the published number while still+ // claiming `user` provenance (spec §9.1: a user limit is never replaced).+ const projected = this.effectiveConfigs.get(model);+ let config = projected;+ if (!config) {+ const baseline = modelConfigFromPi(model);+ if (binding) {+ const limits = resolveBindingLimits(baseline, binding);+ config = modelConfigWithBinding(limits.catalogConfig, limits.binding);+ } else {+ config = baseline;+ }+ }+ return { ...config, ...(input.baseUrl ? { baseUrl: input.baseUrl } : {}) };+ }// Hand-typed custom IDs have no published metadata. Preserve historical// explicit limits without pretending that the generic seed is a catalog.- const binding = input.providerId ? this.accountRows.get(input.providerId)?.models?.find((entry) => entry.id.trim().toLowerCase() === input.modelId.trim().toLowerCase()) : undefined;const limits = resolveBindingLimits(unpublishedConfig ?? genericModelConfig(input.modelId, input.baseUrl ?? ""), binding);return modelConfigWithBinding(limits.catalogConfig, limits.binding);}diff --git a/apps/desktop/electron/main/pi-model-metadata.ts b/apps/desktop/electron/main/pi-model-metadata.tsindex 90f9950fc..2540bb45c 100644--- a/apps/desktop/electron/main/pi-model-metadata.ts+++ b/apps/desktop/electron/main/pi-model-metadata.ts@@ -9,19 +9,57 @@ export const PI_VENDOR_ALIASES: Record<string, string> = {"zai-coding-plan": "zai-coding-plan",};+/**+ * ID family to the publisher that owns the model. An unknown endpoint serving+ * one of these leaves takes that publisher's own record when it publishes the+ * matched base ID, so a reseller's narrower copy never decides the window or+ * thinking shape. Exact IDs win before whitelisted deployment-label fallback;+ * the configured wire ID is never rewritten, and identified providers remain+ * authoritative.+ */const 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.+ mimo: "xiaomi", glm: "zai", deepseek: "deepseek",+ kimi: "moonshotai", minimax: "minimax",};const leaf = (id: string) => id.toLowerCase().split("/").at(-1);+// Only deployment-added environment/context labels are safe to remove as a+// fallback. Model variants such as `thinking`, `preview`, and dated releases+// are deliberately absent: those can name a distinct model with different+// capabilities. Exact leaves are always checked before this fallback.+const DEPLOYMENT_SUFFIXES = new Set([+ "test", "staging", "canary", "dev",+ "1m", "2m", "4m", "32k", "64k", "128k", "200k", "256k", "512k",+]);+function withoutDeploymentSuffix(id: string): string | undefined {+ const match = /^(.*)[-_:]([a-z0-9]+)$/i.exec(id);+ if (!match || !DEPLOYMENT_SUFFIXES.has(match[2].toLowerCase())) return undefined;+ return match[1];+}const capabilities = (m: Model<Api>) => JSON.stringify([m.input, m.reasoning, m.contextWindow, m.maxTokens, m.thinkingLevelMap]);-/** Exact leaves only; ambiguous deployments remain unknown. No suffix stripping. */+/** Match exact route leaves first, then a narrow deployment-suffix fallback. */export function relayChatMetadata(models: Models, id: string): Model<Api> | undefined {const requested = leaf(id.trim());- const hits = models.getModelsOfType("chat").filter(model => leaf(model.id) === requested);+ if (!requested) return undefined;+ const available = models.getModelsOfType("chat");+ const findHits = (candidate: string) => available.filter(model => leaf(model.id) === candidate);++ // A provider may append multiple deployment labels (`-1m-test`). Remove+ // only one whitelisted trailing label at a time, stopping at the nearest+ // published base ID. Never rewrite the model ID used on the wire.+ let lookupId = requested;+ let hits = findHits(lookupId);+ while (!hits.length) {+ const base = withoutDeploymentSuffix(lookupId);+ if (!base) break;+ lookupId = base;+ hits = findHits(lookupId);+ }if (!hits.length) return undefined;- const publisher = publishers[requested?.split(/[-.]/)[0] ?? ""];+ const publisher = 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.mjsindex ebaca4948..b5a14f043 100644--- a/apps/desktop/test/models-dev-catalog.test.mjs+++ b/apps/desktop/test/models-dev-catalog.test.mjs@@ -185,6 +185,86 @@ test("unknown relays match the exact model leaf without rewriting the requestedassert.equal(catalog.findModel({ ...input, modelId: "route/claude-fixture-latest" }), undefined);});+/*+ An unidentified relay keeps answering from the ID family's owner instead of+ from whichever reseller it happens to serve. Each family below publishes an+ owner record next to a reseller copy that states a far narrower window, so the+ assertion proves the owner won. A leaf no owner states stays unmatched: a+ reseller's numbers must never decide a window for a deployment they do not+ describe.+*/+test("an unknown relay answers an open-weight leaf from its owning publisher", () => {+ const relay = { vendorKey: "custom", baseUrl: "https://relay.example/v1" };+ const families = [+ ["mimo-v2.6-pro", "xiaomi"],+ ["glm-5.3", "zai"],+ ["deepseek-v4-pro", "deepseek"],+ ["kimi-k3", "moonshotai"],+ ["MiniMax-M3", "minimax"],+ ];+ for (const [id, owner] of families) {+ const catalog = new ModelsDevCatalog({ providers: [+ fixtureProvider(owner, [{ id, contextWindow: 999_999, maxTokens: 77_777 }]),+ fixtureProvider("openrouter", [{ id, contextWindow: 200_000, maxTokens: 8_192 }]),+ ] });+ const found = catalog.findModel({ ...relay, modelId: `route/${id}` });+ assert.equal(found?.provider, owner, id);+ assert.equal(found?.contextWindow, 999_999, id);+ }+ // Several resellers publish conflicting copies and no owner states the leaf:+ // adopting any one of them would invent a window for this deployment.+ const noOwner = new ModelsDevCatalog({ providers: [+ fixtureProvider("openrouter", [{ id: "reseller-only", contextWindow: 200_000, maxTokens: 8_192 }]),+ fixtureProvider("vercel-ai-gateway", [{ id: "reseller-only", contextWindow: 900_000, maxTokens: 64_000 }]),+ ] });+ assert.equal(noOwner.findModel({ ...relay, modelId: "reseller-only" }), undefined);+});++test("relay IDs accept route prefixes and only whitelisted deployment suffixes", () => {+ const catalog = new ModelsDevCatalog({ providers: [+ fixtureProvider("deepseek", [{ id: "deepseek-v4.1-flash", contextWindow: 1_000_000 }]),+ fixtureProvider("xiaomi", [+ { id: "mimo-v2.5-pro", contextWindow: 1_048_576 },+ { id: "mimo-v2.6-flash", contextWindow: 1_048_576 },+ // Keep the user's `fash` example literal: matching strips only `-test`,+ // it does not autocorrect a model-name typo to `flash`.+ { id: "mimo-v2.6-fash", contextWindow: 1_048_576 },+ ]),+ ] });+ const relay = { vendorKey: "custom", baseUrl: "https://relay.example/v1" };+ for (const id of ["pro/deepseek-v4.1-flash", "deepseek/deepseek-v4.1-flash"]) {+ assert.equal(catalog.findModel({ ...relay, modelId: id })?.id, "deepseek-v4.1-flash", id);+ }+ assert.equal(catalog.findModel({ ...relay, modelId: "mimo-v2.5-pro-1m" })?.id, "mimo-v2.5-pro");+ assert.equal(catalog.findModel({ ...relay, modelId: "mimo-v2.6-flash-test" })?.id, "mimo-v2.6-flash");+ assert.equal(catalog.findModel({ ...relay, modelId: "mimo-v2.6-fash-test" })?.id, "mimo-v2.6-fash");+ // Semantic and release suffixes are not deployment labels, and a typo is+ // never repaired unless that exact base ID is itself published.+ assert.equal(catalog.findModel({ ...relay, modelId: "mimo-v2.6-flash-thinking" }), undefined);+ assert.equal(catalog.findModel({ ...relay, modelId: "mimo-v2.6-flash-2026" }), undefined);+ assert.equal(catalog.findModel({ ...relay, modelId: "mimo-v2.6-flaash-test" }), undefined);+});++/*+ A relay row has no wrapped provider to apply its binding, so `modelConfigFor`+ resolves the stored row itself. Without that a hand-pinned window would be+ replaced by the published number while still claiming `user` provenance.+*/+test("a relay hit resolves the stored binding instead of dropping it", () => {+ const catalog = new ModelsDevCatalog({ providers: [fixtureProvider("xiaomi", [{ id: "mimo-v2.6-pro", contextWindow: 1_048_576, maxTokens: 131_072 }])] });+ const row = {+ id: "relay", vendorKey: "custom", baseUrl: "https://relay.example/v1",+ models: [binding({ id: "mimo-v2.6-pro", contextWindow: 500_000, contextWindowSource: "user", maxTokens: 32_100, maxTokensSource: "user" })],+ };+ catalog.configureAccount(row);+ const input = { providerId: "relay", vendorKey: "custom", baseUrl: row.baseUrl, modelId: "mimo-v2.6-pro" };+ const pinned = catalog.modelConfigFor(input);+ assert.equal(pinned.contextWindow, 500_000);+ assert.equal(pinned.maxTokens, 32_100);+ // The catalog record still answers: only the pin differs from it.+ assert.notEqual(pinned.source, "generic");+});+test("a stored extended level cannot invent native Pi support", () => {const catalog = new ModelsDevCatalog({ providers: [fixtureProvider("example", [{ id: "chat", reasoning: true, thinkingLevelMap: { off: null, low: "low", high: "high" } }])] });const row = { id: "account", vendorKey: "example", models: [binding({ thinkingLevels: ["low", "max"] })] };diff --git a/apps/desktop/test/provider-endpoint-metadata.test.mjs b/apps/desktop/test/provider-endpoint-metadata.test.mjsindex bb17e5bf4..977493bb2 100644--- a/apps/desktop/test/provider-endpoint-metadata.test.mjs+++ b/apps/desktop/test/provider-endpoint-metadata.test.mjs@@ -165,17 +165,18 @@ test("a hand-typed id on the same relay answers with the same record", async (t)assert.equal(unknown.info, null);});-test("a relay enriches unique/official leaves and leaves ambiguous or marker leaves unmatched", async (t) => {+test("a relay enriches official IDs, strips safe deployment labels, and preserves variants", async (t) => {/*- #1047: exact last-segment match + official/shared-capabilities disambiguation.- Multi-publisher leaves with conflicting capabilities stay generic. Deployment- markers (`-1m`) stay part of the leaf and do not strip to a sibling.+ Exact last-segment matches run first. A unique official publisher wins over+ reseller copies, and only whitelisted deployment labels such as `-1m` are+ stripped as a fallback; semantic variants and release dates stay distinct.*/const row = rowOf({ id: "row-4", name: "Relay", baseUrl: "https://relay.example/v1" });const { result } = await handlersFor(t, row, {data: [{ id: "deepseek-v4-flash" },{ id: "mimo-v2.5-pro" },+ { id: "mimo-v2.5-pro-1m" },{ id: "mimo-v2.5-tts" },{ id: "gemini-2.5-pro-1m" },{ id: "claude-sonnet-4-5" },@@ -184,26 +185,33 @@ test("a relay enriches unique/official leaves and leaves ambiguous or marker leaconst byId = new Map(result.models.map((model) => [model.modelId, model]));- // Ambiguous non-official leaves with conflicting publisher caps stay generic.+ // No official publisher states this leaf, so a reseller must not decide it.assert.equal(byId.get("deepseek-v4-flash").catalogSource, undefined);- assert.equal(byId.get("mimo-v2.5-pro").catalogSource, undefined);+ // The mimo family's owner publishes the base ID; both served IDs inherit it.+ 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.contextWindow, 1_048_576);+ assert.equal(mimo.maxTokens, 131_072);+ }- // Unique leaf still enriches.+ // A unique operation-metadata leaf still enriches without becoming a Pi chat.const tts = byId.get("mimo-v2.5-tts");assert.equal(tts.source, "bundled");assert.equal(tts.catalogSource, undefined);assert.ok(tts.capabilities.includes("audio"));assert.equal(tts.contextWindow, 8_192);- // Official Anthropic disambiguation still enriches Claude leaves.+ // Official publisher disambiguation also applies after a `-1m` deployment tag.+ const gemini = byId.get("gemini-2.5-pro-1m");+ assert.equal(gemini.catalogSource, "pi");+ assert.equal(gemini.contextWindow, 1_048_576);++ // Official Anthropic disambiguation still enriches exact leaves.const claude = byId.get("claude-sonnet-4-5");assert.equal(claude.catalogSource, "pi");assert.equal(claude.contextWindow, 1_000_000);-- // Marker leaf is not stripped to gemini-2.5-pro.- assert.equal(byId.get("gemini-2.5-pro-1m").catalogSource, undefined);});-test("a relay enriches a uniquely published dated leaf without reseller majority voting", async (t) => {/*#1047: exact leaf match. If the dated id is published uniquely (or sharesdiff --git a/docs/spec/03-runtime/13-model-catalog-and-selection.md b/docs/spec/03-runtime/13-model-catalog-and-selection.mdindex 5497dcab2..f67f02f24 100644--- a/docs/spec/03-runtime/13-model-catalog-and-selection.md+++ b/docs/spec/03-runtime/13-model-catalog-and-selection.md@@ -464,7 +464,11 @@ thinking ladder for explicit manual opt-in.The Pi adapter preserves the configured wire ID. Unknown endpoints may attachmetadata from an exact, case-insensitive final `/` segment. A unique officialpublisher compatible with the ID family wins; conflicting capability/thinking-candidates remain unknown. No thinking/date/deployment suffix is stripped.+candidates remain unknown. Exact served IDs are checked first. Only a narrow+whitelist of deployment-added environment/context labels (for example `-test`+and `-1m`) may be removed as a fallback to a published base ID. Thinking,+preview, and dated-release suffixes are not stripped, and spelling is never+fuzzy-corrected.Known provider lookup and aliases stay authoritative. An ambiguous knownsame-endpoint miss does not borrow from arbitrary publishers. There is no