@devries-cameron20 / t3code Mirror · read-only / commits / 11fd54fcbb
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 23, 14:15 UTC

fix(web): removing a project no longer opens a new thread for it (#15864)

Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

shivam committed Oct 14, 2026 at 08:34 UTC 11fd54fcbbfb39f824bb4c908d428c20d801267c
8 files changed +87 -24
apps/web/src/components/LegacySidebar.tsx
+2
@@ -91,6 +91,7 @@ import {
91 useProjects,
92 useThreadShells,
93 useThreadShellsForProjectRefs,
94 + waitForProjectRemoval,
95 } from "../state/entities";
96 import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
97 import { useThreadRunningTerminalIds } from "../state/terminalSessions";
@@ -1542,6 +1543,7 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
1543 if (result._tag === "Failure") {
1544 return result;
1545 }
1546 + await waitForProjectRemoval(memberProjectRef);
1547 const draftStore = useComposerDraftStore.getState();
1548 releaseProjectDraftUploads(
1549 memberProjectRef,
apps/web/src/components/settings/ProjectSettingsPanel.test.tsx
+1
@@ -76,6 +76,7 @@ vi.mock("~/state/environments", () => ({
76 vi.mock("~/state/entities", () => ({
77 useProjects: () => state.projects,
78 useThreadShells: () => [],
79 + waitForProjectRemoval: async () => true,
80 }));
81 vi.mock("~/state/projects", () => ({
82 projectEnvironment: { update: "update", delete: "delete" },
apps/web/src/components/settings/ProjectSettingsPanel.tsx
+2 -1
@@ -25,7 +25,7 @@ import {
25 type SidebarProjectSnapshot,
26 } from "../../sidebarProjectGrouping";
27 import { useEnvironments, usePrimaryEnvironmentId } from "../../state/environments";
28 -import { useThreadShells } from "../../state/entities";
28 +import { useThreadShells, waitForProjectRemoval } from "../../state/entities";
29 import { projectEnvironment } from "../../state/projects";
30 import { ProjectFavicon } from "../ProjectFavicon";
31 import { Alert, AlertDescription } from "../ui/alert";
@@ -395,6 +395,7 @@ function ProjectDetail({
395 return;
396 }
397 const projectRef = scopeProjectRef(member.environmentId, member.id);
398 + await waitForProjectRemoval(projectRef);
399 releaseProjectDraftUploads(
400 projectRef,
401 memberThreads.map((thread) => scopeThreadRef(thread.environmentId, thread.id)),
apps/web/src/hooks/useHandleNewThread.test.ts
+40 -15
@@ -4,6 +4,7 @@ import type { RuntimeMode } from "@t3tools/contracts";
4 const testState = vi.hoisted(() => {
5 let completeProjectFileRead: (value: null) => void = () => undefined;
6 let projectFileRead = Promise.resolve<null>(null);
7 + let projectRemoved = false;
8 let targetSettings = {
9 defaultThreadEnvMode: "local" as "local" | "worktree",
10 newWorktreesStartFromOrigin: false,
@@ -42,6 +43,12 @@ const testState = vi.hoisted(() => {
43 get projectFileRead() {
44 return projectFileRead;
45 },
46 + get projectRemoved() {
47 + return projectRemoved;
48 + },
49 + removeProject() {
50 + projectRemoved = true;
51 + },
52 get targetSettings() {
53 return targetSettings;
54 },
@@ -53,6 +60,7 @@ const testState = vi.hoisted(() => {
60 },
61 ) {
62 storedDraft = nextStoredDraft;
63 + projectRemoved = false;
64 targetSettings = {
65 defaultThreadEnvMode: workspaceDefaults.envMode,
66 newWorktreesStartFromOrigin: workspaceDefaults.startFromOrigin,
@@ -159,21 +167,23 @@ vi.mock("../logicalProject", () => ({
167 getProjectOrderKey: () => "remote-project",
168 selectProjectGroupingSettings: () => ({}),
169 }));
162 -vi.mock("../state/entities", () => ({
163 - readProjects: () => [
164 - {
165 - id: "project-remote",
166 - environmentId: "environment-ssh",
167 - workspaceRoot: "/remote/project",
168 - defaultThreadEnvMode: null,
169 - defaultModelSelection: null,
170 - },
171 - ],
172 - readThreadShell: () => null,
173 - readThreadShells: () => [],
174 - useProjects: () => [],
175 - useThread: () => null,
176 -}));
170 +vi.mock("../state/entities", () => {
171 + const project = {
172 + id: "project-remote",
173 + environmentId: "environment-ssh",
174 + workspaceRoot: "/remote/project",
175 + defaultThreadEnvMode: null,
176 + defaultModelSelection: null,
177 + };
178 + return {
179 + readProject: () => (testState.projectRemoved ? null : project),
180 + readProjects: () => (testState.projectRemoved ? [] : [project]),
181 + readThreadShell: () => null,
182 + readThreadShells: () => [],
183 + useProjects: () => [],
184 + useThread: () => null,
185 + };
186 +});
187 vi.mock("../state/server", () => ({
188 environmentServerConfigsAtom: {},
189 primaryServerSettingsAtom: "primary-settings",
@@ -238,6 +248,21 @@ describe.each([
248 expect(testState.draftStore.setLogicalProjectDraftThreadId).not.toHaveBeenCalled();
249 });
250
251 + it("abandons a delayed draft open when the project is removed meanwhile", async () => {
252 + testState.reset(draft);
253 + const pendingOpen = useNewThreadHandler()(
254 + { environmentId: "environment-ssh", projectId: "project-remote" } as never,
255 + { replace: true },
256 + );
257 +
258 + testState.removeProject();
259 + testState.completeProjectFileRead(null);
260 +
261 + await expect(pendingOpen).resolves.toBeNull();
262 + expect(testState.router.navigate).not.toHaveBeenCalled();
263 + expect(testState.draftStore.setLogicalProjectDraftThreadId).not.toHaveBeenCalled();
264 + });
265 +
266 it.each([true, false])(
267 "uses the target environment's start-from-origin default of %s",
268 async (startFromOrigin) => {
apps/web/src/hooks/useHandleNewThread.ts
+7 -2
@@ -24,6 +24,7 @@ import {
24 } from "../logicalProject";
25 import { resolveNewThreadEnvMode, resolveProjectSettings } from "@t3tools/shared/projectSettings";
26 import {
27 + readProject,
28 readProjects,
29 readThreadShell,
30 readThreadShells,
@@ -134,6 +135,10 @@ export function useNewThreadHandler() {
135 candidate.id === projectRef.projectId &&
136 candidate.environmentId === projectRef.environmentId,
137 );
138 + // Removal can land while the project file is read; a draft opened
139 + // after that belongs to a project that no longer exists.
140 + const abandonedSinceRequest = () =>
141 + routeChangedSinceRequest() || (project !== undefined && readProject(projectRef) === null);
142 // The resolver applies project overrides and, until the server has
143 // folded them, the aggregate's own legacy fields.
144 const projectSettings = resolveProjectSettings(
@@ -237,7 +242,7 @@ export function useNewThreadHandler() {
242 workspaceContext = pickExplicitWorkspaceOptions(options);
243 } else if (!isDraftAlreadyOpen) {
244 const defaultEnvMode = await resolveDefaultEnvMode();
240 - if (routeChangedSinceRequest()) {
245 + if (abandonedSinceRequest()) {
246 return null;
247 }
248 // The await yields. If the draft was opened (a concurrent
@@ -376,7 +381,7 @@ export function useNewThreadHandler() {
381 const createdAt = new Date().toISOString();
382 return (async () => {
383 const initialEnvMode = options?.envMode ?? (await resolveDefaultEnvMode());
379 - if (routeChangedSinceRequest()) {
384 + if (abandonedSinceRequest()) {
385 return null;
386 }
387 // The await yields, so a concurrent invocation may have registered a
apps/web/src/hooks/useRemoveClonedProject.ts
+2
@@ -6,6 +6,7 @@ import { useCallback } from "react";
6 import { useComposerDraftStore } from "../composerDraftStore";
7 import { resolveThreadRouteTarget } from "../threadRoutes";
8 import { releaseProjectDraftUploads } from "../lib/composerDraftUploads";
9 +import { waitForProjectRemoval } from "../state/entities";
10 import { projectEnvironment } from "../state/projects";
11 import { useAtomCommand } from "../state/use-atom-command";
12 import { stackedThreadToast, toastManager } from "../components/ui/toast";
@@ -41,6 +42,7 @@ export function useRemoveClonedProject() {
42 );
43 return false;
44 }
45 + await waitForProjectRemoval(projectRef);
46 // Read the route after the await: the user may have moved on while the
47 // delete was in flight, and only a draft of this project needs to go.
48 const routeParams = router.state.matches[router.state.matches.length - 1]?.params ?? {};
apps/web/src/routes/_chat.index.tsx
+19 -6
@@ -14,6 +14,7 @@ import { SidebarInset } from "../components/ui/sidebar";
14 import { WorkspacePageHeader } from "../components/WorkspacePageHeader";
15 import { useNewThreadHandler } from "../hooks/useHandleNewThread";
16 import {
17 + readProject,
18 useAllEnvironmentShellsBootstrapped,
19 useProjects,
20 useThreadShells,
@@ -64,13 +65,25 @@ function IndexDraftLanding() {
65 if (mostRecentProject === null || startingRef.current || !isStillOnIndex()) {
66 return;
67 }
68 + const projectRef = scopeProjectRef(mostRecentProject.environmentId, mostRecentProject.id);
69 + // A project removed since this render is gone from the store; the next
70 + // render picks another one.
71 + if (readProject(projectRef) === null) {
72 + return;
73 + }
74 startingRef.current = true;
68 - void handleNewThread(scopeProjectRef(mostRecentProject.environmentId, mostRecentProject.id), {
69 - replace: true,
70 - }).catch(() => {
71 - startingRef.current = false;
72 - setStartState((state) => ({ ...state, failed: true }));
73 - });
75 + void handleNewThread(projectRef, { replace: true })
76 + .then((opened) => {
77 + // The project was removed while its draft started; land on the next one.
78 + if (opened === null) {
79 + startingRef.current = false;
80 + setStartState((state) => ({ ...state, retryRequest: state.retryRequest + 1 }));
81 + }
82 + })
83 + .catch(() => {
84 + startingRef.current = false;
85 + setStartState((state) => ({ ...state, failed: true }));
86 + });
87 }, [handleNewThread, mostRecentProject, startState.retryRequest]);
88
89 if (!bootstrapped) {
apps/web/src/state/entities.ts
+14
@@ -194,6 +194,20 @@ export function waitForProject(
194 });
195 }
196
197 +/**
198 + * Resolves once a deleted project leaves the live client store. The delete
199 + * command returns before the shell stream does, so anything that picks a
200 + * project in between (the index route's draft landing) still sees it.
201 + */
202 +export function waitForProjectRemoval(ref: ScopedProjectRef, timeoutMs = 5_000): Promise<boolean> {
203 + return waitForAtomValue({
204 + registry: appAtomRegistry,
205 + atom: environmentProjects.projectAtom(ref),
206 + predicate: (project) => project === null,
207 + timeoutMs,
208 + });
209 +}
210 +
211 export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
212 return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
213 }