Skip to content
TypeScript

fix(web,mobile): no pending handoff for a provider the next send would skip (#19523)

Jair Mezaa9af48a3d2a0
diff --git a/apps/mobile/src/state/thread-task-permissions.test.ts b/apps/mobile/src/state/thread-task-permissions.test.ts
index 7461660cbaa..1b1a2550ccd 100644
--- a/apps/mobile/src/state/thread-task-permissions.test.ts
+++ b/apps/mobile/src/state/thread-task-permissions.test.ts
@@ -1,4 +1,21 @@
-import { AuthOrchestrationOperateScope, RuntimeRequestId } from "@t3tools/contracts";
+import {
+ AuthOrchestrationOperateScope,
+ DEFAULT_SERVER_SETTINGS,
+ MessageId,
+ ProjectId,
+ ProviderDriverKind,
+ ProviderInstanceId,
+ RunId,
+ RuntimeRequestId,
+ TurnItemId,
+ type ModelSelection,
+ type OrchestrationV2ThreadProjection,
+ type ServerConfig,
+} from "@t3tools/contracts";
+import {
+ v2Now,
+ v2Projection,
+} from "../../../../packages/client-runtime/src/state/orchestrationV2TestFixtures";
import type { DraftComposerImageAttachment } from "../lib/composerImages";
import type { PendingThreadRequests } from "@t3tools/client-runtime/state/thread-requests";
import { AsyncResult, type Atom } from "effect/reactivity";
@@ -7,7 +24,13 @@ import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
const state = vi.hoisted(() => ({
grantedEnvironments: new Set<string>(),
connectionState: "connected",
- draft: { text: "Keep my draft", attachments: [] as DraftComposerImageAttachment[] },
+ draft: {
+ text: "Keep my draft",
+ attachments: [] as DraftComposerImageAttachment[],
+ modelSelection: undefined as ModelSelection | undefined,
+ },
+ serverConfig: null as ServerConfig | null,
+ projection: null as OrchestrationV2ThreadProjection | null,
pendingRequests: { approvals: [], userInputs: [] } as PendingThreadRequests,
enqueue: vi.fn(async () => undefined),
clearDraft: vi.fn(),
@@ -25,6 +48,7 @@ const state = vi.hoisted(() => ({
thread: {
id: "thread",
environmentId: "secondary",
+ projectId: "project",
modelSelection: { instanceId: "codex", model: "gpt-5.4" },
runtimeMode: "full-access",
interactionMode: "default",
@@ -70,15 +94,13 @@ vi.mock("./use-thread-selection", () => ({
selectedThreadCreation: null,
selectedEnvironmentRuntime: {
connectionState: state.connectionState,
- serverConfig: {
- providers: [{ instanceId: "codex", driver: "codex" }],
- environment: { capabilities: {} },
- },
+ serverConfig: state.serverConfig,
},
}),
}));
vi.mock("./use-thread-detail", () => ({
- useSelectedThreadProjection: () => null,
+ useSelectedThreadProjection: () =>
+ state.projection === null ? null : { projection: state.projection },
useSelectedThreadVisibleTurnItems: () => [],
useSelectedThreadPendingRequests: () => state.pendingRequests,
}));
@@ -126,7 +148,7 @@ vi.mock("./use-composer-drafts", async () => {
mergeComposerDraftContent: vi.fn(),
removeComposerDraftAttachment: vi.fn(),
useComposerDraft: () => state.draft,
- useComposerDraftSettings: () => ({}),
+ useComposerDraftSettings: () => state.draft,
questionAttachmentDraftsAtom: () => Atom.make({}),
};
});
@@ -144,7 +166,6 @@ vi.mock("../lib/commandMetadata", () => ({
}),
}));
vi.mock("../lib/copyTextWithHaptic", () => ({ copyTextWithHaptic: vi.fn() }));
-vi.mock("../lib/modelOptions", () => ({ isModelSelectionUnavailable: () => false }));
vi.mock("./use-remote-environment-registry", () => ({ setPendingConnectionError: vi.fn() }));
import { useThreadComposerState } from "./use-thread-composer-state";
@@ -153,7 +174,12 @@ import { useSelectedThreadRequests } from "./use-selected-thread-requests";
beforeEach(() => {
state.grantedEnvironments = new Set(["primary"]);
state.connectionState = "connected";
- state.draft = { text: "Keep my draft", attachments: [] };
+ state.draft = { text: "Keep my draft", attachments: [], modelSelection: undefined };
+ state.projection = null;
+ state.serverConfig = {
+ providers: [{ instanceId: "codex", driver: "codex", enabled: true }],
+ environment: { capabilities: {} },
+ } as unknown as ServerConfig;
state.pendingRequests = {
approvals: [
{
@@ -260,3 +286,113 @@ describe("mobile task permissions", () => {
});
});
});
+
+describe("mobile handoff preview and submission", () => {
+ it("follows project enablement and provider availability for the selected destination", async () => {
+ state.grantedEnvironments.add("secondary");
+ const instanceId = ProviderInstanceId.make("antigravity");
+ const projectId = ProjectId.make(state.thread.projectId);
+ const selection = { instanceId, model: "gemini" };
+ const provider = {
+ instanceId,
+ driver: ProviderDriverKind.make("antigravity"),
+ enabled: false,
+ installed: true,
+ auth: { status: "authenticated" as const },
+ availability: "available" as "available" | "unavailable",
+ models: [{ slug: selection.model }],
+ };
+ const projectOverride = { enabled: false };
+ state.serverConfig = {
+ ...state.serverConfig,
+ providers: [provider],
+ settings: {
+ ...DEFAULT_SERVER_SETTINGS,
+ providerInstances: { [instanceId]: { driver: provider.driver, enabled: false } },
+ projectSettingsOverrides: {
+ [projectId]: { providerInstances: { [instanceId]: projectOverride } },
+ },
+ },
+ } as unknown as ServerConfig;
+ state.draft.modelSelection = selection;
+ const source = v2Projection.thread.modelSelection;
+ state.projection = {
+ ...v2Projection,
+ runs: [
+ {
+ id: RunId.make("source-run"),
+ threadId: v2Projection.thread.id,
+ ordinal: 1,
+ providerInstanceId: source.instanceId,
+ modelSelection: source,
+ providerThreadId: null,
+ userMessageId: MessageId.make("source-message"),
+ rootNodeId: null,
+ activeAttemptId: null,
+ status: "completed",
+ requestedAt: v2Now,
+ startedAt: v2Now,
+ completedAt: v2Now,
+ checkpointId: null,
+ contextHandoffId: null,
+ },
+ ],
+ visibleTurnItems: [
+ {
+ position: 0,
+ visibility: "local",
+ sourceThreadId: v2Projection.thread.id,
+ sourceItemId: TurnItemId.make("reply"),
+ item: {
+ id: TurnItemId.make("reply"),
+ threadId: v2Projection.thread.id,
+ runId: RunId.make("source-run"),
+ nodeId: null,
+ providerThreadId: null,
+ providerTurnId: null,
+ nativeItemRef: null,
+ parentItemId: null,
+ ordinal: 1,
+ type: "assistant_message",
+ messageId: MessageId.make("reply"),
+ status: "completed",
+ title: null,
+ text: "Task started",
+ streaming: false,
+ startedAt: v2Now,
+ completedAt: v2Now,
+ updatedAt: v2Now,
+ },
+ },
+ ],
+ };
+
+ const disabled = useThreadComposerState();
+ expect(disabled.pendingHandoff).toBeNull();
+ expect(await disabled.onSendMessage()).toBeNull();
+ expect(state.enqueue).not.toHaveBeenCalled();
+ expect(state.clearDraft).not.toHaveBeenCalled();
+
+ projectOverride.enabled = true;
+ expect(useThreadComposerState().pendingHandoff).toEqual({ from: [source], to: selection });
+
+ provider.availability = "unavailable";
+ const unavailable = useThreadComposerState();
+ expect(unavailable.pendingHandoff).toBeNull();
+ expect(await unavailable.onSendMessage()).toBeNull();
+ expect(state.enqueue).not.toHaveBeenCalled();
+ expect(state.clearDraft).not.toHaveBeenCalled();
+
+ provider.availability = "available";
+ const available = useThreadComposerState();
+ expect(available.pendingHandoff).toEqual({ from: [source], to: selection });
+ expect(await available.onSendMessage()).toBe("message");
+ expect(state.enqueue).toHaveBeenCalledExactlyOnceWith(
+ expect.objectContaining({
+ modelSelection: selection,
+ text: "Keep my draft",
+ }),
+ );
+ expect(state.clearDraft).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts
index c33cf3267fe..c979ac6d4b6 100644
--- a/apps/mobile/src/state/use-thread-composer-state.ts
+++ b/apps/mobile/src/state/use-thread-composer-state.ts
@@ -300,10 +300,13 @@ export function useThreadComposerState() {
const selectedThreadQueueCount = selectedThreadQueuedMessages.length;
const selectedThread = selectedThreadShell;
const modelSelection = selectedDraft.modelSelection ?? selectedThread?.modelSelection ?? null;
+ const serverConfig = selectedEnvironmentRuntime?.serverConfig;
const pendingHandoff = resolvePendingHandoff(
selectedThreadProjection?.projection,
- modelSelection,
- selectedEnvironmentRuntime?.serverConfig?.providers ?? [],
+ isModelSelectionUnavailable(serverConfig, modelSelection, selectedThread?.projectId ?? null)
+ ? null
+ : modelSelection,
+ serverConfig?.providers ?? [],
);
const runtimeMode = selectedDraft.runtimeMode ?? selectedThread?.runtimeMode ?? null;
const selectedProvider = selectedEnvironmentRuntime?.serverConfig?.providers.find(
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index 4d41e9b2701..43fd030f8e1 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -5433,9 +5433,16 @@ export default function ChatView(props: ChatViewProps) {
item.type === "user_message" &&
(item.text.trim().toLowerCase() !== "/compact" || item.attachments.length > 0),
);
+ const pendingHandoffSelection = composerSelectedModelSelection ?? activeThread?.modelSelection;
+ const pendingHandoffSelectionUsable = providerInstanceEntries.some(
+ (entry) =>
+ entry.instanceId === pendingHandoffSelection?.instanceId &&
+ entry.enabled &&
+ entry.isAvailable,
+ );
const pendingHandoff = resolvePendingHandoff(
serverProjection,
- composerSelectedModelSelection ?? activeThread?.modelSelection,
+ pendingHandoffSelectionUsable ? pendingHandoffSelection : null,
providerStatuses,
);
const compactThreadUnavailable =