Compare commits

...
Author SHA1 Message Date
thdxr c039174124 fix(core): reuse models across credential switches
Credential changes altered the provider snapshot even when the available provider definitions stayed identical. That forced every retained Location to rematerialize its full active model catalog. Reuse the snapshot when availability is unchanged, while preserving rebuilds for real provider and account-bound inventory changes.
2026-09-15 04:30:22 +00:00
2 changed files with 45 additions and 0 deletions
+8
View File
@@ -366,6 +366,14 @@ const layer = Layer.effect(
if (integration?.connections.length) return true
return record.provider.integrationID === undefined && !integration
})
if (
cached?.records === records &&
cached.value.available.length === available.length &&
cached.value.available.every((record, index) => record === available[index])
) {
cached = { ...cached, access }
return cached.value
}
const value = freeze({ records, available, providers: available.map((record) => record.provider) }, true)
cached = { records, access, value }
return value
+37
View File
@@ -148,6 +148,43 @@ describe("Provider and Model", () => {
}).pipe(Effect.scoped, Effect.provide(localProviderLayer))
})
it.effect("reuses materialized models when the active credential changes", () =>
Effect.gen(function* () {
const providers = yield* Provider.Service
const models = yield* Model.Service
const integrations = yield* Integration.Service
const credentials = yield* Credential.Service
const providerID = Provider.ID.make("switchable")
const integrationID = Integration.ID.make(providerID)
yield* integrations.transform((editor) => editor.update(integrationID, () => {}))
yield* providers.transform((editor) =>
editor.add({
info: Provider.Info.empty(providerID),
models: Array.from({ length: 1_000 }, (_, index) =>
Model.Info.default(providerID, Model.ID.make(`model-${index}`)),
),
}),
)
expect(yield* models.available()).toEqual([])
const first = yield* credentials.create({
integrationID,
value: Credential.Key.make({ type: "key", key: "first" }),
})
const materialized = yield* models.available()
expect(materialized).toHaveLength(1_000)
yield* credentials.create({
integrationID,
value: Credential.Key.make({ type: "key", key: "second" }),
})
expect(yield* models.available()).toBe(materialized)
yield* credentials.activate(first.id)
expect(yield* models.available()).toBe(materialized)
}),
)
it.effect("derives availability from a provider's integration", () => {
const integrationID = Integration.ID.make("gateway")
const providerID = Provider.ID.make("remote")