TypeScript
fix(mobile): a new thread's first send no longer sticks on Starting (#25477)
Julius Marminge59a3f52ad332
diff --git a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx b/apps/mobile/src/features/threads/ThreadDetailScreen.tsxindex 77351fbd23c..4ac5b75e72d 100644--- a/apps/mobile/src/features/threads/ThreadDetailScreen.tsx+++ b/apps/mobile/src/features/threads/ThreadDetailScreen.tsx@@ -1290,6 +1290,7 @@ function ThreadDetailBody(props: ThreadDetailScreenProps) {<ThreadFeedenvironmentId={props.environmentId}threadId={props.selectedThread.id}+ detailRef={props.selectedThreadDetailRef}workspaceRoot={props.threadCwd}feed={props.selectedThreadFeed}pendingHandoff={props.pendingHandoff}diff --git a/apps/mobile/src/features/threads/ThreadFeed.tsx b/apps/mobile/src/features/threads/ThreadFeed.tsxindex eca66821c71..36fe66a0264 100644--- a/apps/mobile/src/features/threads/ThreadFeed.tsx+++ b/apps/mobile/src/features/threads/ThreadFeed.tsx@@ -51,6 +51,7 @@ import {type OrchestrationV2ProjectedTurnItem,type RunId,type ScheduledTaskId,+ type ScopedThreadRef,} from "@t3tools/contracts";import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations";import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";@@ -332,6 +333,8 @@ export interface ThreadFeedProps {readonly onEditPendingMessage: ((message: QueuedThreadMessage) => void) | null;readonly environmentId: EnvironmentId;readonly threadId: ThreadId;+ /** Null while the thread's creation is queued: reading detail then caches a not-found as deleted. */+ readonly detailRef: ScopedThreadRef | null;readonly threadTitle: string;readonly workspaceRoot?: string | null;readonly feed: ReadonlyArray<ThreadFeedEntry>;@@ -2098,12 +2101,8 @@ export const ThreadFeed = memo(function ThreadFeed({...props}: ThreadFeedProps & { readonly ref?: Ref<ThreadFeedHandle> }) {const { listRef, onEndFollowEnabledChange, onHeaderMaterialVisibilityChange } = props;- const workflowOpening = useWorkflowOpening(props.environmentId, props.threadId);- const guestThreadRef = useMemo(- () => scopeThreadRef(props.environmentId, props.threadId),- [props.environmentId, props.threadId],- );- const guestShare = useThreadGuestShare(guestThreadRef);+ const workflowOpening = useWorkflowOpening(props.detailRef);+ const guestShare = useThreadGuestShare(props.detailRef);const deviceType = useDeviceType();const duoColumns = useNativeWorkspaceColumnsSupported() && isDuoDevice(deviceType);const openPullRequest = useOpenPullRequest();diff --git a/apps/mobile/src/features/threads/thread-workflow-opening.tsx b/apps/mobile/src/features/threads/thread-workflow-opening.tsxindex 5165a4ac016..b16a82b7190 100644--- a/apps/mobile/src/features/threads/thread-workflow-opening.tsx+++ b/apps/mobile/src/features/threads/thread-workflow-opening.tsx@@ -6,7 +6,9 @@ import {workflowOpeningParentThreadId,type RuntimeSubagent,} from "@t3tools/client-runtime/state/subagentRuntime";-import type { EnvironmentId, ThreadId } from "@t3tools/contracts";+import type { EnvironmentId, ScopedThreadRef, ThreadId } from "@t3tools/contracts";+import type { EnvironmentThread } from "@t3tools/client-runtime/state/shell";+import { Atom } from "effect/reactivity";import { useState } from "react";import { Pressable, View } from "react-native";@@ -20,8 +22,10 @@ import { useOpenLinkedThread } from "./useOpenLinkedThread";const FOLDED_SCRIPT_LINES = 10;-export function useWorkflowOpening(environmentId: EnvironmentId, threadId: ThreadId) {- const threadAtom = environmentThreadDetails.threadAtom(scopeThreadRef(environmentId, threadId));+const NO_THREAD_ATOM = Atom.make<EnvironmentThread | null>(null);++export function useWorkflowOpening(ref: ScopedThreadRef | null) {+ const threadAtom = ref === null ? NO_THREAD_ATOM : environmentThreadDetails.threadAtom(ref);// Selected as a primitive so an ordinary thread's updates never change the result.const parentId = useAtomValue(threadAtom, (thread) =>thread === null@@ -35,7 +39,9 @@ export function useWorkflowOpening(environmentId: EnvironmentId, threadId: ThreaparentId === null ? null : (thread?.projection ?? null),);const parentSubagents = useAtomValue(- environmentThreadDetails.threadAtom(scopeThreadRef(environmentId, parentId ?? threadId)),+ ref === null || parentId === null+ ? NO_THREAD_ATOM+ : environmentThreadDetails.threadAtom(scopeThreadRef(ref.environmentId, parentId)),(parent) => (parentId === null ? null : (parent?.projection.subagents ?? null)),);return projection === nulldiff --git a/apps/mobile/src/lib/filePreview.test.ts b/apps/mobile/src/lib/filePreview.test.tsindex 02c683ccf70..7525868e5b0 100644--- a/apps/mobile/src/lib/filePreview.test.ts+++ b/apps/mobile/src/lib/filePreview.test.ts@@ -435,6 +435,7 @@ describe("ThreadInlineFilePreview playback", () => {createElement(ThreadFeed, {environmentId,threadId,+ detailRef: null,threadTitle: "preview",workspaceRoot: "/repo",feed,@@ -1374,6 +1375,7 @@ describe("ThreadFeed previews", () => {createElement(ThreadFeed, {environmentId,threadId,+ detailRef: null,threadTitle: "preview",workspaceRoot: "/repo",feed: feedFor(threadId),diff --git a/apps/mobile/src/state/pending-thread-creation.test.ts b/apps/mobile/src/state/pending-thread-creation.test.tsindex 67c21708107..9cf0fc11bea 100644--- a/apps/mobile/src/state/pending-thread-creation.test.ts+++ b/apps/mobile/src/state/pending-thread-creation.test.ts@@ -10,6 +10,7 @@ import { describe, expect, it } from "vite-plus/test";import {resolvePendingThreadCreation,+ selectThreadCreationDetail,type PendingThreadCreation,} from "./pending-thread-creation";import type { QueuedThreadMessage } from "./thread-outbox-model";@@ -138,3 +139,51 @@ describe("resolvePendingThreadCreation", () => {).toBeNull();});});++describe("selectThreadCreationDetail", () => {+ const threadKey = `${creation.environmentId}:${creation.threadId}`;+ const threadRef = { environmentId: creation.environmentId, threadId: creation.threadId };++ it("withholds the detail ref until the creation is delivered", () => {+ const queued = selectThreadCreationDetail({+ threadRef,+ threadKey,+ queuedMessagesByThreadKey: { [threadKey]: [creation] },+ outcomes: {},+ });+ expect(queued).toEqual({+ pendingCreation: { message: creation, outcome: null },+ detailRef: null,+ });++ const failed = selectThreadCreationDetail({+ threadRef,+ threadKey,+ queuedMessagesByThreadKey: {},+ outcomes: { [threadKey]: { kind: "failed", message: creation, reason: "Checkout failed" } },+ });+ expect(failed.pendingCreation?.outcome?.kind).toBe("failed");+ expect(failed.detailRef).toBeNull();++ const delivered = selectThreadCreationDetail({+ threadRef,+ threadKey,+ queuedMessagesByThreadKey: {},+ outcomes: { [threadKey]: { kind: "delivered", message: creation } },+ });+ expect(delivered.pendingCreation?.outcome?.kind).toBe("delivered");+ expect(delivered.detailRef).toBe(threadRef);+ });++ it("does not hold back existing threads with only follow-ups queued", () => {+ const { creation: _creation, ...followUp } = creation;+ expect(+ selectThreadCreationDetail({+ threadRef,+ threadKey,+ queuedMessagesByThreadKey: { [threadKey]: [followUp] },+ outcomes: {},+ }),+ ).toEqual({ pendingCreation: null, detailRef: threadRef });+ });+});diff --git a/apps/mobile/src/state/pending-thread-creation.ts b/apps/mobile/src/state/pending-thread-creation.tsindex 45e07d02fc2..4f7457c7a2d 100644--- a/apps/mobile/src/state/pending-thread-creation.ts+++ b/apps/mobile/src/state/pending-thread-creation.ts@@ -21,6 +21,35 @@ export type PendingThreadCreation = {readonly outcome: PendingThreadCreationOutcome | null;};+export function selectThreadCreationDetail<Ref>(input: {+ readonly threadRef: Ref | null;+ readonly threadKey: string | null;+ readonly queuedMessagesByThreadKey: Readonly<+ Record<string, ReadonlyArray<QueuedThreadMessage> | undefined>+ >;+ readonly outcomes: Readonly<Record<string, PendingThreadCreationOutcome | undefined>>;+}): { readonly pendingCreation: PendingThreadCreation | null; readonly detailRef: Ref | null } {+ const pendingCreation = ((): PendingThreadCreation | null => {+ if (input.threadKey === null) {+ return null;+ }+ const queued = input.queuedMessagesByThreadKey[input.threadKey]?.find(+ (message) => message.creation !== undefined,+ );+ const outcome = input.outcomes[input.threadKey] ?? null;+ const message = queued ?? outcome?.message ?? null;+ return message === null ? null : { message, outcome };+ })();+ // The shell can arrive before creation is acknowledged. Wait for delivery+ // before subscribing: an early not-found is cached as a deleted thread.+ // Every detail reader must use this ref, not the route id.+ const detailRef =+ pendingCreation === null || pendingCreation.outcome?.kind === "delivered"+ ? input.threadRef+ : null;+ return { pendingCreation, detailRef };+}+export function resolvePendingThreadCreation(input: {readonly threadKey: string | null;readonly pending: PendingThreadCreation | null;diff --git a/apps/mobile/src/state/use-thread-selection.ts b/apps/mobile/src/state/use-thread-selection.tsindex d1e0d94df86..17a4a26a186 100644--- a/apps/mobile/src/state/use-thread-selection.ts+++ b/apps/mobile/src/state/use-thread-selection.ts@@ -26,6 +26,7 @@ import {resolvePendingThreadCreation,pendingThreadCreationOutcomesAtom,pendingThreadCreationShell,+ selectThreadCreationDetail,type PendingThreadCreation,} from "./pending-thread-creation";import {@@ -145,24 +146,12 @@ function useResolvedThreadSelection(params: ThreadSelectionRouteParams | undefin: scopedThreadKey(selectedThreadRef.environmentId, selectedThreadRef.threadId);const queuedMessagesByThreadKey = useThreadOutboxMessages();const creationOutcome = useAtomValue(pendingThreadCreationOutcomesAtom);- const pendingCreation = ((): PendingThreadCreation | null => {- if (selectedThreadKey === null) {- return null;- }- const queued = queuedMessagesByThreadKey[selectedThreadKey]?.find(- (message) => message.creation !== undefined,- );- const outcome = creationOutcome[selectedThreadKey] ?? null;- const message = queued ?? outcome?.message ?? null;- return message === null ? null : { message, outcome };- })();- // The shell can arrive before creation is acknowledged. Wait for delivery- // before subscribing: an early not-found is cached as a deleted thread.- // Every detail reader must use this ref, not the route id.- const selectedThreadDetailRef =- pendingCreation === null || pendingCreation.outcome?.kind === "delivered"- ? selectedThreadRef- : null;+ const { pendingCreation, detailRef: selectedThreadDetailRef } = selectThreadCreationDetail({+ threadRef: selectedThreadRef,+ threadKey: selectedThreadKey,+ queuedMessagesByThreadKey,+ outcomes: creationOutcome,+ });const [previousCreation, setPreviousCreation] = useState<PendingThreadCreation | null>(null);const needsDetail =selectedThreadShell === null || pendingCreation !== null || previousCreation !== null;