Skip to content
Source
Sign inSign up
TanStack/schemas

Infer origin links from a matching id, serve the chain root, and mark first-party rows #734

Closedtombeckenhamwants to merge 9 commits intomain fromfeat/719-inferred-linksopened

tombeckenhamcommented

Stacked on #731 (the maker fact). Implements #719 and the firstParty scope in its comment.

Tiers

reconcileSameAs keeps the stated tiers in SQL unchanged (exact id, alias, dots as hyphens; ambiguous or missing gives null) and adds a name tier after them, in TypeScript over the rows the same query now returns.

  • A row with stated upstream evidence is never linked by name, even when the evidence resolves to nothing. That keeps Groq's meta-llama/..., Hugging Face re-uploads, Replicate accounts and Together's packaged ids unlinked.
  • Name-tier sources: rows with no stated upstream at a provider that is not a declared maker (Neon, OpenCode, bare LLM Gateway ids).
  • Candidates: rows whose stated link resolves (each stands for its chain's end), and a declared maker's own rows with no stated upstream (each stands for itself). "Own row" is isOwnRow in src/server/first-party.ts, the same predicate firstParty uses: the provider is declared, maker is null or names the provider itself, and on a maker that also hosts other makers' models the maker source was read for the row. Name-inferred rows are never candidates, so one inference never feeds another.
  • Key: originNameKey in src/lib/origin-key.ts, the one normaliser: lowercase, drop a leading x/ (or Bedrock-form x.) when x is a provider id or a claimed namespace (read from providers and provider_model_namespaces, no typed maker list), drop Bedrock's version on a Bedrock-form id (the adapter's BEDROCK_VERSION regex, moved into that module and imported by the adapter), dots as hyphens. Lowercasing applies in the name tier only; the stated tiers keep their exact matching.
  • Single-root rule: a source links to the root when every candidate at another provider leads to one row AND that row is a maker's own row. A chain that ends at a host's row (LLM Gateway routes to Together or Novita, gateways' qwen/ ids ending at dashscope) gives no link, so dashscope-cn never links to dashscope. Two roots (regional twins such as moonshot and moonshotai-cn, or minimax and minimax-cn), a looping chain, a root that is the source itself or at its own provider: no link.
  • factSources.sameAs on an inferred link is { derivation: "name-inferred", key, candidates: [{ provider, rawId }] }, stored in models.same_as_inference. A stated link always wins: the moment a provider states an upstream for the row, the inference is cleared.

The scorer in src/lib/completeness.ts is not changed. Its originIdKeys still mirrors the stated tiers only, so name-inferred links raise originLink.have without changing need. The name normaliser has no SQL twin to drift from.

Variants

Only OpenRouter states variants in a form readable at poll time: GET /api/v1/models gives a :batch or :free row the same canonical_slug as its base row (96 of 103 suffixed ids today; the other 7 have no base row). openRouterVariantUpstream states the base as the row's upstream, with path: data[].canonical_slug and variant: "batch" in the evidence. Vercel, LLM Gateway, Replicate, Fireworks, Cloudflare and others list -fast ids, but no source states that -fast is a variant of another id, so those rows stay unlinked. No suffix list is typed anywhere.

Chain root: stored

reconcileSameAs resolves each link's chain end (at most 8 hops; a loop drops the link) and stores it in models.root_model_id only when it is more than one hop away. The catalog serves sameAsRoot as that row, or as sameAs when the column is null: one more primary-key join, no recursive read. The first-party linked-from count is two correlated count(*) subqueries per row, each an index search (models_rootModelId_idx, covering; models_sameAsModelId_idx). The maker context is three small reads per request (providers, namespaces, and SELECT DISTINCT provider_id, maker ... WHERE maker IS NOT NULL on the covering models_maker_providerId_idx). Query plans checked with EXPLAIN QUERY PLAN.

firstParty

Derived on read in src/server/first-party.ts, never stored per row. In order:

  1. A served sameAs to another provider's row: false (decidedBy: same-as).
  2. maker names a company that is not this provider: false (decidedBy: maker with the maker source). "Names this provider" means the whole name or its first word, letters and digits only, equals the provider id or a namespace it claims (Google DeepMind to gemini via google).
  3. Only a declared maker's own row (isOwnRow) can be true. On a declared maker that also hosts other makers' models (a provider with any row whose maker names someone else, today mistral and byteplus), a row the maker source does not cover is null.
  4. Such a row is true with linked-from and the count when other rows' chains end here, else maker (mixed hosts) or provider-declared.
  5. Otherwise null. A host's row that other rows link to (together/zai-org/GLM-5, cerebras/gpt-oss-120b) is null, not true: the issue comment's "true whatever the declaration" rule was dropped on review.

ProviderConfig.makesOwnModels is mirrored to providers.makes_own_models by ensureProviderRow. Declared makers: anthropic, openai, grok, gemini, mistral, byteplus, elevenlabs, deepseek, cohere, ant-ling, assemblyai, bfl, cartesia, deepgram, fish-audio, jina, kling, meta, minimax, minimax-cn, moonshot, moonshotai-cn, voyage, worldlabs, xiaomi, zai, stability. Left unset because they host other makers' models with no maker source: dashscope, dashscope-cn, nvidia, runway, reactor, perplexity, and the coding-plan wrappers (kimi-code-plan-, zai-coding-plan, zhipuai-coding-plan). Unknown and left unset: ollaya, typesafe. BytePlus now claims the bytedance namespace, so its Provider column's ByteDance names BytePlus itself; this also lets five gateway `bytedance/seed-` ids link by the stated tiers.

Estimated first reconcile

From one read of GET /v1/models (7,883 live rows, not rate-limited) and OpenRouter's listing, approximating which rows state an upstream (prod has no maker yet): about 230 new name-inferred links (llmgateway 87, cloudflare-ai-gateway 58, opencode 36, neon 24, google-vertex 13, opencode-go 8, others 5), about 360 name matches refused (two roots, a loop, or a root that is a host's row), about 100 existing links gaining a multi-hop root, about 66 OpenRouter variants linking to a linked base, and about 5 bytedance/ links. Each sameAs or root change emits one model.updated: roughly 400 events on the first run, then only real changes. A change to the inference evidence alone (a new candidate) writes no event.

API shape

Rows gain sameAsRoot and firstParty; factSources gains firstParty (FirstPartySource); FactSource gains derivation: name-inferred with key and candidates, and variant. MODEL_ROW_SHAPE 13, openapi.json and the client regenerated, @modelschemas/client minor changeset, docs/api-changes.md, README and the CLAUDE.md sameAs paragraph updated.

Migration 0021 is hand-written and additive (in-place ALTER TABLE ADD, two indexes; drizzle-kit wanted a full models rebuild). Old code never reads the new columns, so the minutes between migration and deploy are safe.

Stays unlinked

  • A model whose maker has no API row (Llama, Qwen open weights, gpt-oss): out of scope.
  • Llama on Groq and Together: stated, resolves to nothing, never falls through. Fireworks' accounts/fireworks/models/... ids match nothing.
  • -fast and other suffixes no source states as variants.
  • Regional twins and anything else whose candidates lead to two roots.

Worth a look

  • Cloudflare AI Gateway states no upstream (no adapter states one until it has its own listing); the name tier applies to its rows like any other provider with no stated upstream (about 58 links).

Known limits

  • The scorer in src/lib/completeness.ts is unchanged: originIdKeys still mirrors only the stated tiers and stays looser than the SQL (it reads dots as hyphens on both sides). Name-inferred links raise originLink.have; need does not use the name key.

Checks

pnpm run typecheck, pnpm run lint, pnpm run check, pnpm run check:client and the full pnpm run test (261 files, 2,670 tests) pass.

    1. tombeckenham pushedc6e1956

    2. Replaced by a pull request opened directly against main from the same branch, so that it opens on GitHub by itself. Review and fix history stays here.

    3. tombeckenham closed this

Sign in to join this conversation.