Skip to content

Commit 4003d2f

Browse files
fix(mobile): new threads move from draft to Working in one step
The server commits a new thread before its first run, so the sidebar showed nothing, then the thread in the active list, then moved it to Working. Threads this client is launching now read as preparing from Send until their first run arrives. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent def6577 commit 4003d2f

5 files changed

Lines changed: 110 additions & 6 deletions

File tree

‎apps/mobile/src/features/home/HomeRouteScreen.tsx‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616

1717
import { useProjects } from "../../state/entities";
1818
import { environmentThreadShells } from "../../state/threads";
19-
import { usePendingNewTasks } from "../../state/use-pending-new-tasks";
19+
import { useLaunchingThreads, usePendingNewTasks } from "../../state/use-pending-new-tasks";
2020
import { useWorkspaceState } from "../../state/workspace";
2121
import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
2222
import {
@@ -46,9 +46,8 @@ export function HomeRouteScreen() {
4646
const projects = useProjects();
4747
// Stop rebuilding the streaming thread list while Thread covers Home.
4848
const visible = useHomeRouteVisible();
49-
const threads = useAtomValueWhileVisible(
50-
environmentThreadShells.navigationThreadShellsAtom,
51-
visible,
49+
const threads = useLaunchingThreads(
50+
useAtomValueWhileVisible(environmentThreadShells.navigationThreadShellsAtom, visible),
5251
);
5352
const { environments: workspaceEnvironments, state: catalogState } = useWorkspaceState();
5453
const { savedConnectionsById } = useSavedRemoteConnections();

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ import { useThreadListV2ShelfPreferences } from "./use-thread-list-v2-shelf-pref
3232
import { useThreadListNow } from "./use-thread-list-now";
3333
import { usePendingThreadOrder } from "../../state/thread-order";
3434
import { threadListEnvironmentsAtom } from "../../state/presentation";
35-
import { usePendingNewTasks, type PendingNewTask } from "../../state/use-pending-new-tasks";
35+
import {
36+
useLaunchingThreads,
37+
usePendingNewTasks,
38+
type PendingNewTask,
39+
} from "../../state/use-pending-new-tasks";
3640
import { useQueuedThreadKeys } from "../../state/use-thread-outbox";
3741
import { useWorkspaceState } from "../../state/workspace";
3842
import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
@@ -137,6 +141,7 @@ function ThreadNavigationSidebarPane(props: ThreadNavigationSidebarProps) {
137141
regenerateThreadTitle,
138142
} = useThreadListActions();
139143
const pendingTasks = usePendingNewTasks();
144+
const launchingThreads = useLaunchingThreads(threads);
140145
const activeNewTask = useActiveNewTask();
141146
const queuedThreadKeys = useQueuedThreadKeys();
142147
const selectDetail = useSelectWorkspaceDetail();
@@ -265,7 +270,7 @@ function ThreadNavigationSidebarPane(props: ThreadNavigationSidebarProps) {
265270
contentOnlyThreadKeys,
266271
olderSettledShelf,
267272
} = useThreadListV2({
268-
threads,
273+
threads: launchingThreads,
269274
pendingTasks,
270275
environmentId: options.selectedEnvironmentId,
271276
projectRefs: selectedProjectScope === null ? null : selectedProjectScope.projectRefs,

‎apps/mobile/src/state/pending-thread-creation.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import {
44
MessageId,
55
ProjectId,
66
ProviderInstanceId,
7+
RunId,
78
ThreadId,
89
} from "@t3tools/contracts";
10+
import { isThreadWorking } from "@t3tools/client-runtime/state/thread-inbox";
911
import { describe, expect, it } from "vite-plus/test";
1012

1113
import {
14+
pendingThreadCreationShell,
1215
resolvePendingThreadCreation,
1316
selectThreadCreationDetail,
17+
withLaunchingThreads,
1418
type PendingThreadCreation,
1519
} from "./pending-thread-creation";
1620
import type { QueuedThreadMessage } from "./thread-outbox-model";
@@ -186,3 +190,30 @@ describe("selectThreadCreationDetail", () => {
186190
).toEqual({ pendingCreation: null, detailRef: threadRef });
187191
});
188192
});
193+
194+
describe("withLaunchingThreads", () => {
195+
it("reads as working from Send until the thread's first run arrives", () => {
196+
const launch = (threads: Parameters<typeof withLaunchingThreads>[0]["threads"]) =>
197+
withLaunchingThreads({ threads, launches: [creation], hiddenThreadKeys: new Set() });
198+
// Before the shell arrives, and after the server commits the thread without its run.
199+
const created = { ...pendingThreadCreationShell(creation)!, title: "Server title" };
200+
for (const threads of [launch([]), launch([created])]) {
201+
expect(threads).toHaveLength(1);
202+
expect(isThreadWorking(threads[0]!)).toBe(true);
203+
}
204+
expect(launch([created])[0]!.title).toBe("Server title");
205+
206+
const started = {
207+
...created,
208+
latestRun: {
209+
runId: RunId.make("run-1"),
210+
status: "completed" as const,
211+
requestedAt: null,
212+
startedAt: null,
213+
completedAt: null,
214+
assistantMessageId: null,
215+
},
216+
};
217+
expect(launch([started])[0]).toBe(started);
218+
});
219+
});

‎apps/mobile/src/state/pending-thread-creation.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from "@t3tools/client-runtime/state/shell";
55
import type { LocalThreadMessage } from "../lib/threadActivity";
66
import { DEFAULT_PROVIDER_INTERACTION_MODE, DEFAULT_RUNTIME_MODE } from "@t3tools/contracts";
7+
import { threadKey } from "@t3tools/client-runtime/state/entities";
78
import { Atom } from "effect/reactivity";
89
import * as DateTime from "effect/DateTime";
910

@@ -133,6 +134,49 @@ export function pendingThreadCreationMessage(message: QueuedThreadMessage): Loca
133134
};
134135
}
135136

137+
/**
138+
* Threads this client is launching read as preparing from Send until their first run lands.
139+
* The server commits the thread before its run, so the list would otherwise place it twice.
140+
*/
141+
export function withLaunchingThreads(input: {
142+
readonly threads: ReadonlyArray<EnvironmentThreadShell>;
143+
readonly launches: ReadonlyArray<QueuedThreadMessage>;
144+
readonly hiddenThreadKeys: ReadonlySet<string>;
145+
}): ReadonlyArray<EnvironmentThreadShell> {
146+
if (input.launches.length === 0) return input.threads;
147+
const launchByKey = new Map<string, QueuedThreadMessage>();
148+
for (const message of input.launches) {
149+
if (!message.creation || input.hiddenThreadKeys.has(threadKey(message))) continue;
150+
launchByKey.set(scopedThreadKey(message.environmentId, message.threadId), message);
151+
}
152+
if (launchByKey.size === 0) return input.threads;
153+
const threads = input.threads.map((thread) => {
154+
const key = scopedThreadKey(thread.environmentId, thread.id);
155+
const launch = launchByKey.get(key);
156+
if (!launch) return thread;
157+
launchByKey.delete(key);
158+
return thread.latestRun === null && thread.runtime === null
159+
? { ...thread, runtime: launchingRuntime(thread) }
160+
: thread;
161+
});
162+
for (const launch of launchByKey.values()) {
163+
const shell = pendingThreadCreationShell(launch);
164+
if (shell) threads.push({ ...shell, runtime: launchingRuntime(shell) });
165+
}
166+
return threads;
167+
}
168+
169+
function launchingRuntime(thread: EnvironmentThreadShell): EnvironmentThreadShell["runtime"] {
170+
return {
171+
status: "preparing",
172+
activeRunId: null,
173+
providerInstanceId: thread.providerInstanceId,
174+
providerName: null,
175+
lastError: null,
176+
updatedAt: thread.updatedAt,
177+
};
178+
}
179+
136180
export function pendingThreadCreationShell(
137181
message: QueuedThreadMessage,
138182
): EnvironmentThreadShell | null {

‎apps/mobile/src/state/use-pending-new-tasks.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { useAtomValue } from "@effect/atom-react";
2+
import { useMemo } from "react";
23
import { threadKey } from "@t3tools/client-runtime/state/entities";
4+
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
35
import { buildPendingNewTasks, type PendingNewTask } from "./pending-new-tasks-model";
6+
import { pendingThreadCreationOutcomesAtom, withLaunchingThreads } from "./pending-thread-creation";
47
import { connectedEnvironmentIdsAtom } from "./presentation";
58
import { flattenQueuedThreadMessages } from "./thread-outbox-model";
69
import { environmentThreadShells } from "./threads";
@@ -28,3 +31,25 @@ export function usePendingNewTasks(): ReadonlyArray<PendingNewTask> {
2831
connectedEnvironmentIds: new Set(connectedEnvironmentIds),
2932
});
3033
}
34+
35+
/** The list's threads, with creations a connected environment is delivering placed as working. */
36+
export function useLaunchingThreads(
37+
threads: ReadonlyArray<EnvironmentThreadShell>,
38+
): ReadonlyArray<EnvironmentThreadShell> {
39+
const queuedMessagesByThreadKey = useThreadOutboxMessages();
40+
const outcomes = useAtomValue(pendingThreadCreationOutcomesAtom);
41+
const hiddenThreadKeys = useAtomValue(environmentThreadShells.hiddenThreadKeysAtom);
42+
const connectedEnvironmentIds = useAtomValue(connectedEnvironmentIdsAtom);
43+
return useMemo(() => {
44+
const connected = new Set(connectedEnvironmentIds);
45+
const launches = [
46+
...flattenQueuedThreadMessages(queuedMessagesByThreadKey).filter((message) =>
47+
connected.has(message.environmentId),
48+
),
49+
...Object.values(outcomes).flatMap((outcome) =>
50+
outcome.kind === "delivered" ? [outcome.message] : [],
51+
),
52+
];
53+
return withLaunchingThreads({ threads, launches, hiddenThreadKeys });
54+
}, [threads, queuedMessagesByThreadKey, outcomes, hiddenThreadKeys, connectedEnvironmentIds]);
55+
}

0 commit comments

Comments
 (0)