Skip to content

Commit acabc34

Browse files
committed
fix(mobile): new threads no longer get stuck on Starting
1 parent 511d20c commit acabc34

7 files changed

Lines changed: 43 additions & 24 deletions

File tree

‎apps/mobile/src/features/threads/ThreadAgentsSheet.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { deriveSubagentElapsedMs, formatDuration } from "@t3tools/shared/orchestrationTiming";
1010
import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
1111
import * as DateTime from "effect/DateTime";
12+
import { Atom } from "effect/reactivity";
1213
import * as Haptics from "expo-haptics";
1314
import { useEffect, useState } from "react";
1415
import { Platform, Pressable, ScrollView, View } from "react-native";
@@ -28,8 +29,16 @@ const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Pl
2829

2930
type AgentsTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };
3031

31-
export function useThreadTurnSubagents(target: AgentsTarget): ThreadTurnSubagents | null {
32-
return useAtomValue(environmentThreadDetails.turnSubagentsAtom(target));
32+
const EMPTY_TURN_SUBAGENTS_ATOM = Atom.make<ThreadTurnSubagents | null>(null).pipe(
33+
Atom.withLabel("mobile-thread-turn-subagents:empty"),
34+
);
35+
36+
export function useThreadTurnSubagents(target: AgentsTarget | null): ThreadTurnSubagents | null {
37+
return useAtomValue(
38+
target === null
39+
? EMPTY_TURN_SUBAGENTS_ATOM
40+
: environmentThreadDetails.turnSubagentsAtom(target),
41+
);
3342
}
3443

3544
export function ThreadAgentsSheet({ route }: StaticScreenProps<AgentsTarget>) {

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
ProviderInteractionMode,
2525
RuntimeMode,
2626
RuntimeRequestId,
27+
ScopedThreadRef,
2728
ServerConfig as T3ServerConfig,
2829
ThreadId,
2930
UsageLimitsReport,
@@ -155,6 +156,11 @@ export interface ThreadDetailScreenProps {
155156
readonly setupWorkingStartedAt?: string | null;
156157
readonly canOperateThread: boolean;
157158
readonly selectedThread: EnvironmentThreadShell;
159+
/**
160+
* Read thread details through this, not `selectedThread.id`: it stays null until
161+
* a queued creation reaches the server; see `useThreadSelection`.
162+
*/
163+
readonly selectedThreadDetailRef: ScopedThreadRef | null;
158164
readonly contentPresentation: ThreadContentPresentation;
159165
readonly screenTone: StatusTone;
160166
readonly connectionError: string | null;
@@ -330,10 +336,7 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
330336
const usesNativeWorkspaceColumns = useNativeWorkspaceColumnsSupported();
331337
const navigation = useNavigation();
332338
const { session: voiceInputSession } = useGlobalVoiceInput();
333-
const reportedModelSelection = useThreadReportedModelSelection({
334-
environmentId: props.environmentId,
335-
threadId: props.selectedThread.id,
336-
});
339+
const reportedModelSelection = useThreadReportedModelSelection(props.selectedThreadDetailRef);
337340
const deviceState = useEnvironmentQuery(
338341
deviceEnvironment.state({ environmentId: props.environmentId, input: {} }),
339342
);
@@ -411,14 +414,8 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
411414
threadId: props.selectedThread.id,
412415
});
413416
const composerError = useAtomValue(threadComposerErrorsAtom)[selectedThreadKey]?.message ?? null;
414-
const queuedCount = useThreadQueuedCount({
415-
environmentId: props.environmentId,
416-
threadId: props.selectedThread.id,
417-
});
418-
const turnSubagents = useThreadTurnSubagents({
419-
environmentId: props.environmentId,
420-
threadId: props.selectedThread.id,
421-
});
417+
const queuedCount = useThreadQueuedCount(props.selectedThreadDetailRef);
418+
const turnSubagents = useThreadTurnSubagents(props.selectedThreadDetailRef);
422419
const agentsSegment = resolveSubagentPillSegment(turnSubagents);
423420
const composerEditorRef = useRef<ComposerEditorHandle>(null);
424421
// A provider-native subagent shows status instead of a composer.

‎apps/mobile/src/features/threads/ThreadQueueControl.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type StaticScreenProps, useNavigation } from "@react-navigation/native";
22
import { useAtomValue } from "@effect/atom-react";
33
import type { ChatAttachment, EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
4+
import { Atom } from "effect/reactivity";
45
import { Image } from "expo-image";
56
import * as Haptics from "expo-haptics";
67
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
@@ -39,6 +40,9 @@ import { threadDragGapOffset } from "./threadDragGap";
3940
const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version);
4041
const REMOVE_ACTION_WIDTH = 76;
4142
const THUMBNAIL_LIMIT = 3;
43+
const EMPTY_QUEUED_COUNT_ATOM = Atom.make(0).pipe(
44+
Atom.withLabel("mobile-thread-queue-count:empty"),
45+
);
4246

4347
type QueueTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };
4448
type QueueAction = "steer" | "edit" | "up" | "down" | "remove";
@@ -48,8 +52,10 @@ export function useThreadQueueWorkflow(target: QueueTarget) {
4852
return useAtomValue(environmentThreadDetails.queueWorkflowAtom(target));
4953
}
5054

51-
export function useThreadQueuedCount(target: QueueTarget) {
52-
return useAtomValue(environmentThreadDetails.queuedCountAtom(target));
55+
export function useThreadQueuedCount(target: QueueTarget | null) {
56+
return useAtomValue(
57+
target === null ? EMPTY_QUEUED_COUNT_ATOM : environmentThreadDetails.queuedCountAtom(target),
58+
);
5359
}
5460

5561
export function ThreadQueueSheet({ route }: StaticScreenProps<QueueTarget>) {

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ function ThreadRouteContent(
274274
const {
275275
selectedThread,
276276
selectedThreadCreation,
277+
selectedThreadDetailRef,
277278
selectedThreadProject,
278279
selectedEnvironmentConnection,
279280
} = useThreadSelection();
@@ -988,6 +989,7 @@ function ThreadRouteContent(
988989
<ThreadDetailScreen
989990
canOperateThread={canOperateThread}
990991
selectedThread={selectedThreadWithDraftSettings ?? selectedThread}
992+
selectedThreadDetailRef={selectedThreadDetailRef}
991993
contentPresentation={contentPresentation}
992994
screenTone={connectionTone(routeConnectionState)}
993995
connectionError={routeConnectionError}

‎apps/mobile/src/state/entities.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const EMPTY_THREAD_SHELL_ATOM = Atom.make<EnvironmentThreadShell | null>(null).p
3131
const EMPTY_SERVER_CONFIG_ATOM = Atom.make<ServerConfig | null>(null).pipe(
3232
Atom.withLabel("mobile-server-config:empty"),
3333
);
34+
const EMPTY_THREAD_ATOM = Atom.make<EnvironmentThread | null>(null).pipe(
35+
Atom.withLabel("mobile-thread:empty"),
36+
);
3437
const EMPTY_THREAD_TITLES: ReadonlyMap<ThreadId, string> = new Map();
3538
const EMPTY_THREAD_TITLES_ATOM = Atom.make(EMPTY_THREAD_TITLES).pipe(
3639
Atom.withLabel("mobile-thread-titles:empty"),
@@ -133,6 +136,9 @@ export function useServerConfigs(): ReadonlyMap<EnvironmentId, ServerConfig> {
133136
const selectReportedModelSelection = (thread: EnvironmentThread | null) =>
134137
thread === null ? null : deriveReportedModelSelection(thread.projection);
135138

136-
export function useThreadReportedModelSelection(ref: ScopedThreadRef) {
137-
return useAtomValue(environmentThreadDetails.threadAtom(ref), selectReportedModelSelection);
139+
export function useThreadReportedModelSelection(ref: ScopedThreadRef | null) {
140+
return useAtomValue(
141+
ref === null ? EMPTY_THREAD_ATOM : environmentThreadDetails.threadAtom(ref),
142+
selectReportedModelSelection,
143+
);
138144
}

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export function useThreadComposerState() {
183183
const {
184184
selectedThread: selectedThreadShell,
185185
selectedThreadCreation,
186+
selectedThreadDetailRef,
186187
selectedEnvironmentRuntime,
187188
} = useThreadSelection();
188189
const selectedThreadProjection = useSelectedThreadProjection();
@@ -310,12 +311,9 @@ export function useThreadComposerState() {
310311
// Steering needs a live provider turn the adapter can interrupt; the queue
311312
// workflow already derives that from the session's capabilities.
312313
const queueWorkflow = useAtomValue(
313-
selectedThreadShell === null
314+
selectedThreadDetailRef === null
314315
? EMPTY_QUEUE_WORKFLOW_ATOM
315-
: environmentThreadDetails.queueWorkflowAtom({
316-
environmentId: selectedThreadShell.environmentId,
317-
threadId: selectedThreadShell.id,
318-
}),
316+
: environmentThreadDetails.queueWorkflowAtom(selectedThreadDetailRef),
319317
);
320318
const canSteerActiveTurn = queueWorkflow?.canPromoteToSteer === true;
321319
const queuedRunEdit = useQueuedRunEdit(selectedThreadKey);

‎apps/mobile/src/state/use-thread-selection.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ function useResolvedThreadSelection(params: ThreadSelectionRouteParams | undefin
158158
return message === null ? null : { message, outcome };
159159
}, [creationOutcome, queuedMessagesByThreadKey, selectedThreadKey]);
160160
// Until the creation is delivered the server has no thread to subscribe
161-
// to; subscribing anyway would retry "not found" for the whole setup.
161+
// to. Every detail reader must use this ref, not the route id: an earlier
162+
// read gets a definitive not-found, which the thread state keeps as deleted.
162163
const selectedThreadDetailRef =
163164
selectedThreadShell !== null ||
164165
pendingCreation === null ||

0 commit comments

Comments
 (0)