Skip to content

fix(core): reuse model catalog across credential changes - #49255

Merged
thdxr merged 2 commits into
v2from
catalog-sharing
Sep 16, 2026
Merged

thdxr merged 2 commits into
v2from
catalog-sharing

Conversation

@thdxr

@thdxr thdxr commented Sep 16, 2026

Copy link
Copy Markdown
Member

Issue for this PR

Closes #49109 (supersedes it). Reported in #core: /connect took 3–5 seconds with 10+ Locations open.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Problem. Every credential change fans out to each open Location. Provider.snapshot() keyed its cache on every connection of every integration, so switching the active credential produced a new snapshot even when the same providers stayed available. Model used snapshot identity as its invalidation signal and rebuilt the whole catalog: structuredClone of every model, replay of every transform, re-merge of provider settings, deep freeze, then provider.updated + model.updated, which made clients refetch the full model list. Credential.create fires two events, so this ran twice per Location per /connect.

Fix, in three parts.

  1. Provider.snapshot() now keys its cache on which provider definitions are available (elementwise compare of the available list). A credential switch that leaves the same providers connected returns the same snapshot, so nothing downstream reacts and no events fire. This is the same idea as fix(core): reuse models across credential switches #49109 with one cache key instead of two; the old access string is removed.
  2. Model no longer deep-copies every model on rebuild. initial() points at the provider's frozen definition maps; a provider's map and a model are copied on their first edit only (whether via update(), get(), or list()), mirroring the writable() pattern already in Provider's editor. read() remembers each model's merged output while its source definition and provider record are unchanged. A rebuild for a genuinely new provider therefore costs that provider's models, not the catalog — the case fix(core): reuse models across credential switches #49109 did not cover.
  3. Provider's definition index takes the providerID and gives foreign definitions the registering provider's identity. Model.initial() used to do this fix-up on every rebuild; doing it once where definitions are registered is what lets Model share them as-is.

No user-facing or plugin-facing changes. The editor API and its mutable list()/get() results are unchanged, transforms see the same models they saw before (connected providers only), and the HTTP surface is untouched.

Measured with a 6,000-model provider in one Location, before → after:

Scenario Before After
First connect 55 ms 13 ms
No-op credential switch 38 ms, 2 events, new array 1 ms, 0 events, same array
Adding a 10-model provider 47 ms, all re-merged 6 ms, existing models reused

How did you verify your code works?

  • Three new tests in packages/core/test/catalog.test.ts: a credential switch reuses the catalog and publishes no model.updated; foreign definitions get the registering provider's identity; a newly available provider leaves existing model objects untouched. The first and third fail on v2 without this change; the second fails if the fix-up in Provider.index is removed.
  • bun test across the model/provider/plugin suites in packages/core (1,048 pass), bun typecheck, bun run lint.
  • Temporary benchmark against both this branch and unchanged v2 for the table above (not committed).

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

from OpenCode

Every credential change reached each open Location and, because the
provider snapshot was keyed on every connection of every integration,
produced a new snapshot even when the same providers stayed available.
Model treated that as a catalog change and rebuilt from scratch: deep
copying every model, replaying every transform, re-merging provider
settings, and publishing model.updated so clients refetched. With many
Locations open, one /connect did all of that per Location, twice.

Key the snapshot on which provider definitions are available, so a
credential switch that changes nothing returns the same snapshot and
publishes nothing. Let Model share provider definition maps and copy a
provider's map and a model only on their first edit, and reuse merged
output for models whose definition and provider record are unchanged,
so the rebuild for a genuinely new provider costs only that provider.
Provider's index now gives foreign definitions the registering
provider's identity, which Model previously did on every rebuild.
Wait for the connect's model.updated before switching credentials so
the event count no longer depends on subscriber fibers keeping pace,
and add a test that direct edits to models returned by list and get
persist into the catalog without touching shared definitions.

Also tidy the definition index write, draft's early returns, and the
provider selection in list.
@thdxr
thdxr merged commit 9bf5faa into v2 Sep 16, 2026
8 checks passed
@thdxr
thdxr deleted the catalog-sharing branch September 16, 2026 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant