diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 57c12959ff..6185408df5 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -2,6 +2,7 @@ import { EnvironmentId, MessageId, ProjectId, + ProviderDriverKind, ProviderInstanceId, ThreadId, TurnId, @@ -17,6 +18,7 @@ import { buildThreadTurnInterruptInput, createLocalDispatchSnapshot, deriveComposerSendState, + deriveLockedProvider, dismissBranchMismatchForSession, getStartedThreadModelChangeBlockReason, hasServerAcknowledgedLocalDispatch, @@ -599,3 +601,99 @@ describe("hasServerAcknowledgedLocalDispatch", () => { expect(hasServerAcknowledgedLocalDispatch({ ...common, threadError: "failed" })).toBe(true); }); }); + +describe("deriveLockedProvider", () => { + const hermesInstanceId = ProviderInstanceId.make("hermes-siva-local-c9cc"); + const providers = [ + { instanceId: hermesInstanceId, driver: ProviderDriverKind.make("hermes") }, + { instanceId: ProviderInstanceId.make("codex"), driver: ProviderDriverKind.make("codex") }, + ]; + + it("leaves an unstarted thread unlocked", () => { + expect( + deriveLockedProvider({ + thread: makeThread(), + selectedProvider: null, + threadProvider: "codex", + providers, + }), + ).toBe(null); + }); + + it("follows the open session's driver kind when there is one", () => { + expect( + deriveLockedProvider({ + thread: makeThread({ session: readySession }), + selectedProvider: null, + threadProvider: null, + providers, + }), + ).toBe("codex"); + }); + + it("resolves a sessionless thread's instance id back to its driver kind", () => { + // A Hermes home thread never opens a session — deliveries bypass the turn + // machinery — so `modelSelection.instanceId` is the only binding left. + // Before this resolved, the composer fell through to the unknown-instance + // defaults and showed a runtime mode selector Hermes opts out of. + expect( + deriveLockedProvider({ + thread: makeThread({ + session: null, + messages: [ + { + id: MessageId.make("message-1"), + role: "assistant", + text: "Gateway online", + turnId: null, + streaming: false, + createdAt: now, + updatedAt: now, + }, + ], + modelSelection: { instanceId: hermesInstanceId, model: "hermes" }, + }), + selectedProvider: null, + threadProvider: hermesInstanceId, + providers, + }), + ).toBe("hermes"); + }); + + it("prefers the registry over the slug shape for an instance id", () => { + // `isProviderDriverKind` validates slug *shape*, not membership, so an + // instance id passes it. Checking shape first would hand back + // `hermes-siva-local-c9cc` as a driver kind matching no driver — which is + // exactly how a home thread ended up with another provider's composer + // controls. + expect( + deriveLockedProvider({ + thread: makeThread({ latestTurn: completedTurn }), + selectedProvider: null, + threadProvider: hermesInstanceId, + providers, + }), + ).toBe("hermes"); + }); + + it("stays unlocked when the thread carries no usable binding", () => { + expect( + deriveLockedProvider({ + thread: makeThread({ latestTurn: completedTurn }), + selectedProvider: null, + threadProvider: null, + providers, + }), + ).toBe(null); + }); + + it("degrades to slug-only matching when no snapshots are supplied", () => { + expect( + deriveLockedProvider({ + thread: makeThread({ latestTurn: completedTurn }), + selectedProvider: null, + threadProvider: "codex", + }), + ).toBe("codex"); + }); +}); diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index 466c9b24c8..fb9e256b8a 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -342,15 +342,19 @@ export function threadHasStarted(thread: Thread | null | undefined): boolean { // // `selectedProvider` takes the same open-string shape because the composer // now tracks the picker selection as a `ProviderInstanceId` (e.g. -// `codex_personal`). Custom instance ids that don't directly match a -// registered driver resolve to `null` here, which matches the existing -// "unknown driver -> unlocked" semantics. Callers that want the lock to track -// a custom instance's underlying driver kind should resolve the instance id -// upstream and pass the correlated kind. +// `codex_personal`). Such an id is not itself a driver-kind slug, so +// `providers` is consulted to map it back to its owning kind; without that +// lookup a thread bound to a user-authored instance reads as unlocked. export function deriveLockedProvider(input: { thread: Thread | null | undefined; selectedProvider: string | null; threadProvider: string | null; + /** + * Instance snapshots used to resolve a `ProviderInstanceId` to its driver + * kind. Optional so callers that already hold a kind can omit it; absent (or + * not yet streamed) it degrades to the previous slug-only matching. + */ + providers?: ReadonlyArray>; }): ProviderDriverKind | null { if (!threadHasStarted(input.thread)) { return null; @@ -359,15 +363,24 @@ export function deriveLockedProvider(input: { if (sessionProvider && isProviderDriverKind(sessionProvider)) { return sessionProvider; } - const narrowedThreadProvider = - input.threadProvider && isProviderDriverKind(input.threadProvider) - ? input.threadProvider - : null; - const narrowedSelectedProvider = - input.selectedProvider && isProviderDriverKind(input.selectedProvider) - ? input.selectedProvider - : null; - return narrowedThreadProvider ?? narrowedSelectedProvider ?? null; + // A Hermes home thread never opens a session (deliveries bypass the turn + // machinery), so `providerName` is absent and the only binding left is the + // instance id on `modelSelection`. Resolving it through the snapshots is + // what keeps the composer on the thread's own instance instead of falling + // through to the unknown-instance defaults, which show a workspace runtime + // mode selector and a plan toggle that Hermes explicitly opts out of. + // + // The registry lookup must come FIRST. `isProviderDriverKind` validates slug + // *shape*, not membership, so an instance id like `hermes-siva-local-c9cc` + // satisfies it and would otherwise be returned as a driver kind that matches + // no driver — locking the thread to a provider that does not exist. + const narrow = (candidate: string | null): ProviderDriverKind | null => { + if (!candidate) return null; + const resolved = input.providers?.find((entry) => entry.instanceId === candidate)?.driver; + if (resolved) return resolved; + return isProviderDriverKind(candidate) ? candidate : null; + }; + return narrow(input.threadProvider) ?? narrow(input.selectedProvider) ?? null; } export function getStartedThreadModelChangeBlockReason(input: { diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 4f9b0e3af6..4c5ae68c5f 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -1799,16 +1799,17 @@ function ChatViewContent(props: ChatViewProps) { activeThread?.modelSelection.instanceId ?? activeProject?.defaultModelSelection?.instanceId ?? null; - const lockedProvider = deriveLockedProvider({ - thread: activeThread, - selectedProvider: selectedProviderByThreadId, - threadProvider, - }); // Once a thread selects an environment, never substitute the primary // environment's config while the selected environment is still loading. const serverConfig = activeThread ? (activeEnvironment?.serverConfig ?? null) : (primaryEnvironment?.serverConfig ?? null); + const lockedProvider = deriveLockedProvider({ + thread: activeThread, + selectedProvider: selectedProviderByThreadId, + threadProvider, + providers: serverConfig?.providers ?? EMPTY_PROVIDERS, + }); const versionMismatch = resolveServerConfigVersionMismatch(serverConfig); const versionMismatchDismissKey = versionMismatch && activeThread diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index 35accb4f5f..a70f7ffd9e 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -187,6 +187,7 @@ import { sortProviderInstanceEntries, type ProviderInstanceEntry, } from "../../providerInstances"; +import { homeThreadComposerPlaceholder, isHomeThreadId } from "../../homeThreads"; import { type AppModelOption, getAppModelOptionsForInstance } from "../../modelSelection"; import type { UnifiedSettings } from "@t3tools/contracts/settings"; import type { SessionPhase, Thread } from "../../types"; @@ -401,7 +402,7 @@ const ComposerFooterModeControls = memo(function ComposerFooterModeControls(prop render={