From 340d746f0334c413ebec9dfc981ea5b4fc1ee470 Mon Sep 17 00:00:00 2001 From: Tom Beckenham <34339192+tombeckenham@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:17:50 +1100 Subject: [PATCH] Resolve a Mistral replacement that is only a relation id (#783) A model page can name the successor as a Payload id and not embed that record. Deprecations stay on the overview, which still throws when the id is missing. The overview resolves the id from the sibling record. --- src/server/providers/deprecations.test.ts | 43 +++++++ src/server/providers/mistral-catalog.ts | 113 ++++++++++++++++--- src/server/providers/mistral-pricing.test.ts | 33 ++++++ src/server/providers/model-facts.ts | 6 +- 4 files changed, 178 insertions(+), 17 deletions(-) diff --git a/src/server/providers/deprecations.test.ts b/src/server/providers/deprecations.test.ts index 17dc6792..48599573 100644 --- a/src/server/providers/deprecations.test.ts +++ b/src/server/providers/deprecations.test.ts @@ -243,6 +243,49 @@ describe('mistral deprecations catalog', () => { expect(() => parseMistralDeprecations(catalogHtml([catalogRow({ replacedBy: 100 })])), ).toThrow(/no API name/) + const successor = catalogRow({ + id: 100, + slug: 'ministral-14b-25-12', + status: 'ga', + modelIds: ['ministral-14b-2512', 'ministral-14b-latest'], + deprecatedAt: null, + retiredAt: null, + replacedBy: null, + }) + expect( + parseMistralDeprecations( + catalogHtml([catalogRow({ replacedBy: 100 }), successor]), + ).find((row) => row.rawId === 'pixtral-12b-2409')?.replacedBy, + ).toEqual(['ministral-14b-2512']) + expect(() => + parseMistralDeprecations( + catalogHtml([ + catalogRow({ replacedBy: 100 }), + successor, + catalogRow({ + id: 100, + slug: 'other-successor', + status: 'ga', + modelIds: ['other-id'], + replacedBy: null, + }), + ]), + ), + ).toThrow(/names two API lists/) + expect(() => + parseMistralDeprecations( + catalogHtml([ + catalogRow({ replacedBy: 100 }), + catalogRow({ + id: 100, + slug: 'family-page', + status: 'ga', + modelIds: [], + replacedBy: null, + }), + ]), + ), + ).toThrow(/no API name/) const populated = catalogRow() expect( parseMistralDeprecations( diff --git a/src/server/providers/mistral-catalog.ts b/src/server/providers/mistral-catalog.ts index ae29196a..c505a65e 100644 --- a/src/server/providers/mistral-catalog.ts +++ b/src/server/providers/mistral-catalog.ts @@ -155,16 +155,16 @@ function prices(value: unknown, slug: string): Array { /** * `null` is no successor. A record's `modelIds` are the successor's API - * ids. A number is Payload's unpopulated relation: the same page also - * embeds the record, and a page that never does throws. + * ids. A number is Payload's unpopulated relation id. The page resolves + * it later, from the catalog record with that `id`. */ function replacementIds( value: unknown, slug: string, -): { ids: Array | null; unread: boolean } { - if (value === null) return { ids: null, unread: false } +): { ids: Array | null; unread: boolean; relationId: number | null } { + if (value === null) return { ids: null, unread: false, relationId: null } if (typeof value === 'number' && Number.isInteger(value)) { - return { ids: null, unread: true } + return { ids: null, unread: true, relationId: value } } if (!isRecord(value)) { throw new Error(`mistral catalog ${slug}: replacement has no API name`) @@ -173,7 +173,14 @@ function replacementIds( if (parsed.length === 0) { throw new Error(`mistral catalog ${slug}: replacement has no API name`) } - return { ids: parsed, unread: false } + return { ids: parsed, unread: false, relationId: null } +} + +/** Payload's numeric id, when the record carries one. */ +function recordId(node: Record, slug: string): number | null { + if (!('id' in node)) return null + if (typeof node.id === 'number' && Number.isInteger(node.id)) return node.id + throw new Error(`mistral catalog ${slug}: id ${JSON.stringify(node.id)}`) } /** One catalog record, or null when `node` is some other RSC value. */ @@ -211,6 +218,7 @@ function readModel(node: Record): CatalogDraft | null { const replacement = replacementIds(node.replacedBy, slug) return { slug, + recordId: recordId(node, slug), status: status as MistralCatalogModel['status'], modelIds: ids(node.modelIds, slug, 'modelIds'), shortDescription: node.shortDescription ?? '', @@ -218,6 +226,7 @@ function readModel(node: Record): CatalogDraft | null { retiredAt: instant(node.retiredAt, slug, 'retiredAt'), replacementIds: replacement.ids, replacementUnread: replacement.unread, + replacementRelationId: replacement.relationId, inputs: strings(node.inputs, slug, 'inputs'), outputs: strings(node.outputs, slug, 'outputs'), features: strings(node.features, slug, 'features'), @@ -229,12 +238,18 @@ function readModel(node: Record): CatalogDraft | null { } interface CatalogDraft extends MistralCatalogModel { + /** Payload id. Null when this copy of the record does not carry one. */ + recordId: number | null /** True when this copy's `replacedBy` is an unpopulated relation id. */ replacementUnread: boolean + /** The relation id, when `replacementUnread` is true. */ + replacementRelationId: number | null } function sameReplacement(prior: CatalogDraft, model: CatalogDraft): boolean { - if (prior.replacementUnread && model.replacementUnread) return true + if (prior.replacementUnread && model.replacementUnread) { + return prior.replacementRelationId === model.replacementRelationId + } // An unpopulated relation id agrees with the populated record, not with // a copy that says there is no successor. if (prior.replacementUnread) return model.replacementIds !== null @@ -246,10 +261,41 @@ function sameReplacement(prior: CatalogDraft, model: CatalogDraft): boolean { } function withoutReplacement(model: CatalogDraft): string { - const { replacementIds: _ids, replacementUnread: _unread, ...rest } = model + const { + replacementIds: _ids, + replacementUnread: _unread, + replacementRelationId: _relation, + ...rest + } = model return JSON.stringify(rest) } +/** + * API ids of the catalog record whose Payload `id` is `relationId`. + * Undefined when this page embeds no such record. Two records with that + * id and different `modelIds` throw. An empty list is a successor the + * page names with no API id. + */ +function relationModelIds( + models: Array, + relationId: number, +): Array | undefined { + let found: Array | undefined + for (const model of models) { + if (model.recordId !== relationId) continue + if (found === undefined) { + found = model.modelIds + continue + } + if (JSON.stringify(found) !== JSON.stringify(model.modelIds)) { + throw new Error( + `mistral catalog: replacement ${String(relationId)} names two API lists`, + ) + } + } + return found +} + function walk(node: unknown, out: Array): void { if (Array.isArray(node)) { for (const item of node) walk(item, out) @@ -261,12 +307,26 @@ function walk(node: unknown, out: Array): void { for (const value of Object.values(node)) walk(value, out) } +export interface ParseMistralCatalogOptions { + /** + * A model page does not have to embed the successor. Deprecations are + * read from the overview, which stays fail-closed (this flag off). + * An unresolved relation id becomes `replacementIds: null`. + */ + allowUnreadReplacement?: boolean +} + /** * Catalog records in a docs HTML page, one per slug. An HTML page with * no catalog record returns an empty list. Two records for one slug that - * disagree throw. + * disagree throw. A `replacedBy` number is the successor's Payload id: + * the record with that id on the same page supplies the API ids. The + * overview throws when that record is absent. A model page does not. */ -export function parseMistralCatalog(html: string): Array { +export function parseMistralCatalog( + html: string, + options?: ParseMistralCatalogOptions, +): Array { if (!html.includes('__next_f')) return [] const found: Array = [] for (const value of rscValues(html)) walk(value, found) @@ -287,13 +347,34 @@ export function parseMistralCatalog(html: string): Array { bySlug.set(model.slug, model) } } - return [...bySlug.values()].map((model) => { + const drafts = [...bySlug.values()] + return drafts.map((model) => { if (model.replacementUnread) { - throw new Error( - `mistral catalog ${model.slug}: replacement has no API name`, - ) + const resolved = + model.replacementRelationId === null + ? undefined + : relationModelIds(drafts, model.replacementRelationId) + if (resolved === undefined) { + if (!options?.allowUnreadReplacement) { + throw new Error( + `mistral catalog ${model.slug}: replacement has no API name`, + ) + } + model.replacementIds = null + } else if (resolved.length === 0) { + throw new Error( + `mistral catalog ${model.slug}: replacement has no API name`, + ) + } else { + model.replacementIds = resolved + } } - const { replacementUnread: _unread, ...rest } = model + const { + recordId: _recordId, + replacementUnread: _unread, + replacementRelationId: _relation, + ...rest + } = model return rest }) } @@ -303,7 +384,7 @@ export function mistralPageCatalog( html: string, slug: string, ): MistralCatalogModel | null { - const models = parseMistralCatalog(html) + const models = parseMistralCatalog(html, { allowUnreadReplacement: true }) if (models.length === 0) return null const own = models.filter((model) => model.slug === slug) if (own.length !== 1) { diff --git a/src/server/providers/mistral-pricing.test.ts b/src/server/providers/mistral-pricing.test.ts index 6cba4945..be3d562c 100644 --- a/src/server/providers/mistral-pricing.test.ts +++ b/src/server/providers/mistral-pricing.test.ts @@ -1292,6 +1292,39 @@ describe('mistral catalog records', () => { expect(parseMistralCatalog('no payload')).toEqual([]) }) + it('reads a model page whose replacement is only a relation id', () => { + const page = catalogHtml([ + catalogModel({ + slug: 'mistral-medium-3-5-26-04', + modelIds: ['mistral-medium-3-5', 'mistral-medium-latest'], + replacedBy: 100, + }), + ]) + const own = mistralPageCatalog(page, 'mistral-medium-3-5-26-04') + expect(own?.modelIds).toEqual([ + 'mistral-medium-3-5', + 'mistral-medium-latest', + ]) + expect(own?.replacementIds).toBeNull() + expect(() => parseMistralCatalog(page)).toThrow(/no API name/) + const resolved = catalogHtml([ + catalogModel({ + slug: 'mistral-medium-3-5-26-04', + modelIds: ['mistral-medium-3-5'], + replacedBy: 100, + }), + catalogModel({ + id: 100, + slug: 'mistral-large-4-0', + modelIds: ['mistral-large-4', 'mistral-large-4-0'], + replacedBy: null, + }), + ]) + expect( + mistralPageCatalog(resolved, 'mistral-medium-3-5-26-04')?.replacementIds, + ).toEqual(['mistral-large-4', 'mistral-large-4-0']) + }) + it('prices a dropped row from the catalog and does not fetch the markdown twin', async () => { const medium = catalogModel({ slug: 'mistral-medium-3-5-26-04', diff --git a/src/server/providers/model-facts.ts b/src/server/providers/model-facts.ts index 3e2af749..51c4ad8e 100644 --- a/src/server/providers/model-facts.ts +++ b/src/server/providers/model-facts.ts @@ -309,7 +309,11 @@ export const DOCS_TTL_SECONDS = 6 * 60 * 60 // parses; a v50 entry is the old parse. // v52: Mistral's overview, model pages, reasoning guide and web-search // guide moved (#771). A v51 model-page entry has no catalog read. -const DOCS_CACHE_VERSION = 'v52' +// v53: a Mistral `replacedBy` that is only a Payload relation id resolves +// to the sibling record with that id (#783). A model page that never +// embeds the successor no longer throws. A v52 entry was parsed before +// that lookup, and a failed model-page load was not cached. +const DOCS_CACHE_VERSION = 'v53' /** Fresh-window key for one parsed docs page. Shared with the stale copy. */ export function docsCacheKey(url: string): string {