@devries-cameron20 / t3code Mirror · read-only / commits / a452c2dfdc
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 14:30 UTC

fix(mobile): opening a new thread before it is created no longer marks it unavailable (#25632)

Co-authored-by: Theo Browne <me@theo.li>

Ali Younes committed Oct 27, 2026 at 06:27 UTC a452c2dfdc54c0ee9bae5191610725c88bd9f367
2 files changed +127
apps/mobile/src/state/threads.test.ts new
+110
@@ -0,0 +1,110 @@
1 +import {
2 + CommandId,
3 + EnvironmentId,
4 + MessageId,
5 + ProjectId,
6 + ProviderInstanceId,
7 + ThreadId,
8 +} from "@t3tools/contracts";
9 +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
10 +
11 +import { appAtomRegistry } from "./atom-registry";
12 +import { pendingThreadCreationOutcomesAtom } from "./pending-thread-creation";
13 +import { threadOutboxManager } from "./thread-outbox";
14 +import type { QueuedThreadMessage } from "./thread-outbox-model";
15 +import { startOpeningEnvironmentThread } from "./threads";
16 +
17 +const loads = vi.hoisted(() => [] as string[]);
18 +
19 +vi.mock("../connection/runtime", () => ({ connectionAtomRuntime: {} }));
20 +vi.mock("../connection/catalog", () => ({ environmentCatalog: {} }));
21 +vi.mock("./shell", () => ({ environmentShell: {} }));
22 +vi.mock("@t3tools/client-runtime/state/threads", async (importOriginal) => {
23 + const { Atom } = await import("effect/reactivity");
24 + const stateAtom = Atom.family((key: string) =>
25 + Atom.make(() => {
26 + loads.push(key);
27 + return null;
28 + }),
29 + );
30 + return {
31 + ...(await importOriginal<object>()),
32 + createThreadFamilyAtoms: () => ({}),
33 + createOlderSettledThreadAtoms: () => ({}),
34 + createThreadEnvironmentAtoms: () => ({}),
35 + createEnvironmentThreadDetailAtoms: () => ({}),
36 + createEnvironmentThreadShellAtoms: () => ({}),
37 + createEnvironmentThreadStateAtoms: () => ({
38 + stateAtom: (environmentId: string, threadId: string) =>
39 + stateAtom(`${environmentId}:${threadId}`),
40 + }),
41 + };
42 +});
43 +
44 +const environmentId = EnvironmentId.make("environment-1");
45 +const threadId = ThreadId.make("thread-1");
46 +const threadKey = `${environmentId}:${threadId}`;
47 +const creation: QueuedThreadMessage = {
48 + environmentId,
49 + threadId,
50 + messageId: MessageId.make("message-1"),
51 + commandId: CommandId.make("command-1"),
52 + text: "Fix the flaky login test",
53 + attachments: [],
54 + modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" },
55 + creation: {
56 + projectId: ProjectId.make("project-1"),
57 + workspaceMode: "local",
58 + branch: "main",
59 + worktreePath: null,
60 + },
61 + createdAt: "2026-08-24T12:00:00.000Z",
62 +};
63 +
64 +describe("startOpeningEnvironmentThread", () => {
65 + beforeEach(() => {
66 + vi.useFakeTimers();
67 + });
68 +
69 + afterEach(() => {
70 + vi.runAllTimers();
71 + vi.useRealTimers();
72 + loads.length = 0;
73 + appAtomRegistry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, {});
74 + appAtomRegistry.set(pendingThreadCreationOutcomesAtom, {});
75 + });
76 +
77 + it("waits for a queued thread creation to be delivered before loading the thread", () => {
78 + appAtomRegistry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, {
79 + [threadKey]: [creation],
80 + });
81 + startOpeningEnvironmentThread(environmentId, threadId);
82 + expect(loads).toEqual([]);
83 +
84 + appAtomRegistry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, {});
85 + appAtomRegistry.set(pendingThreadCreationOutcomesAtom, {
86 + [threadKey]: { kind: "delivered", message: creation },
87 + });
88 + startOpeningEnvironmentThread(environmentId, threadId);
89 + expect(loads).toEqual([threadKey]);
90 + });
91 +
92 + it("does not load a thread whose creation failed", () => {
93 + appAtomRegistry.set(pendingThreadCreationOutcomesAtom, {
94 + [threadKey]: { kind: "failed", message: creation, reason: "Checkout failed" },
95 + });
96 + startOpeningEnvironmentThread(environmentId, threadId);
97 + expect(loads).toEqual([]);
98 + });
99 +
100 + it("loads threads without a pending creation", () => {
101 + const { creation: _creation, ...followUp } = creation;
102 + const otherThreadId = ThreadId.make("thread-2");
103 + appAtomRegistry.set(threadOutboxManager.queuedMessagesByThreadKeyAtom, {
104 + [threadKey]: [followUp],
105 + });
106 + startOpeningEnvironmentThread(environmentId, threadId);
107 + startOpeningEnvironmentThread(environmentId, otherThreadId);
108 + expect(loads).toEqual([threadKey, `${environmentId}:${otherThreadId}`]);
109 + });
110 +});
apps/mobile/src/state/threads.ts
+17
@@ -16,8 +16,14 @@ import { useEffect } from "react";
16
17 import { environmentCatalog } from "../connection/catalog";
18 import { connectionAtomRuntime } from "../connection/runtime";
19 +import { scopedThreadKey } from "../lib/scopedEntities";
20 import { appAtomRegistry } from "./atom-registry";
21 +import {
22 + pendingThreadCreationOutcomesAtom,
23 + selectThreadCreationDetail,
24 +} from "./pending-thread-creation";
25 import { environmentShell } from "./shell";
26 +import { threadOutboxManager } from "./thread-outbox";
27
28 export const threadFamilies = createThreadFamilyAtoms(
29 connectionAtomRuntime,
@@ -59,6 +65,17 @@ const OPENING_THREAD_HOLD_MS = 3_000;
65 * otherwise waits for navigation to commit.
66 */
67 export function startOpeningEnvironmentThread(environmentId: EnvironmentId, threadId: ThreadId) {
68 + // A thread whose creation is still queued would load as not found and stay deleted;
69 + // the opened screen loads it once creation is delivered.
70 + const { detailRef } = selectThreadCreationDetail({
71 + threadRef: { environmentId, threadId },
72 + threadKey: scopedThreadKey(environmentId, threadId),
73 + queuedMessagesByThreadKey: appAtomRegistry.get(
74 + threadOutboxManager.queuedMessagesByThreadKeyAtom,
75 + ),
76 + outcomes: appAtomRegistry.get(pendingThreadCreationOutcomesAtom),
77 + });
78 + if (detailRef === null) return;
79 const release = appAtomRegistry.mount(environmentThreads.stateAtom(environmentId, threadId));
80 setTimeout(release, OPENING_THREAD_HOLD_MS);
81 }