diff --git a/src/lib/actions/inference-set.ts b/src/lib/actions/inference-set.ts index 4469b15dc64..bf9b8a181b8 100644 --- a/src/lib/actions/inference-set.ts +++ b/src/lib/actions/inference-set.ts @@ -1070,6 +1070,20 @@ function observeInferenceRouteForSelection(input: { return observeInferenceRouteBeforeMutation(input.deps, input.gatewayName); } +function sharedInferenceRouteChanged( + nativeProvider: boolean, + previous: OpenShellInferenceRouteObservation, + provider: string, + model: string, +): boolean { + if (nativeProvider) return false; + return ( + previous.state === "unconfigured" || + previous.route.provider !== provider || + previous.route.model !== model + ); +} + function providerBackedRouteLacksRollbackTarget(input: { nativeProvider: boolean; directProviderBinding: boolean; @@ -1591,6 +1605,7 @@ async function runInferenceSetWithoutHostLock( selectingNative: selectingNative && !selectingNativeLocal, provider, expectedAttachment: nativeProviderAuthority, + previousAttachment: previousNativeAttachment, gatewayName: preparedRoute.gatewayName, sandboxName, endpointUrl: registryMetadata.endpointUrl, @@ -2088,12 +2103,15 @@ async function runInferenceSetWithoutHostLock( patched.changed, retryingOpenClawConfigSync, appliedProvider, - rollbackRoute?.provider !== provider, - rollbackRoute?.model !== model, + Boolean(nativeSelection.providerChanged), + nativeAttachmentChanged, + nativeLocalAttachmentChanged, + sharedInferenceRouteChanged(selectingNative, preMutationRoute, provider, model), previousProvider !== provider, previousModel !== model, previousInferenceApi !== preferredInferenceApi, (entry.endpointUrl ?? null) !== (registryMetadata.endpointUrl ?? null), + (entry.credentialEnv ?? null) !== (registryMetadata.credentialEnv ?? null), ], expectedGatewayName, ); diff --git a/src/lib/actions/inference-set/native.ts b/src/lib/actions/inference-set/native.ts index 84d963d65df..9591f961aff 100644 --- a/src/lib/actions/inference-set/native.ts +++ b/src/lib/actions/inference-set/native.ts @@ -188,6 +188,7 @@ export async function prepareNativeSelection(input: { selectingNative: boolean; provider: string; expectedAttachment?: NativeProviderAttachment; + previousAttachment?: NativeProviderAttachment; gatewayName: string; sandboxName: string; endpointUrl?: string | null; @@ -198,6 +199,7 @@ export async function prepareNativeSelection(input: { }): Promise<{ attachment?: NativeProviderAttachment; attachmentChanged: boolean; + providerChanged?: boolean; endpointUrl?: string; }> { if (!input.selectingNative) return { attachmentChanged: false }; @@ -251,17 +253,20 @@ export async function prepareNativeSelection(input: { hostedNativeProvider(input.provider, definition.endpointUrl)?.endpoint, ); const target = { kind: "named", gatewayName: input.gatewayName } as const; + // Repeated selections reuse the sandbox's attached NVIDIA credential. + // Hermes host credentials use NOUS_API_KEY; OPENAI_API_KEY is its injection slot. + const credentialValue = + input.expectedAttachment && + (!isNativeNvidiaProvider(input.provider) || + input.previousAttachment?.providerName === input.expectedAttachment.providerName) + ? null + : input.deps.resolveCredentialValue( + input.provider === "hermes-provider" ? "NOUS_API_KEY" : definition.credentialEnv, + ) || null; const ensured = await lifecycle.ensureNativeProvider({ adapter: input.deps.providerAdapter, target, - // Hosted model selection must not rotate a gateway-shared provider credential. - // Hermes host credentials use NOUS_API_KEY; OPENAI_API_KEY is its injection slot. - credentialValue: - input.expectedAttachment && !isNativeNvidiaProvider(input.provider) - ? null - : input.deps.resolveCredentialValue( - input.provider === "hermes-provider" ? "NOUS_API_KEY" : definition.credentialEnv, - ) || null, + credentialValue, ...(input.expectedAttachment ? { expected: input.expectedAttachment } : {}), ...(input.expectedAttachment && !authority.read(input.gatewayName) ? { recordedSandboxName: input.sandboxName } @@ -285,6 +290,7 @@ export async function prepareNativeSelection(input: { return { attachment: attached.receipt, attachmentChanged: attached.changed, + providerChanged: !input.expectedAttachment || credentialValue !== null, endpointUrl: attached.receipt.endpointUrl, }; }