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

Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-14 08:34:04 +05:30
1 parent aba3a6ddc0
commit 11fd54fcbb
8 files changed
+87 -24

No files matched your search

@@ -91,6 +91,7 @@ import {
useProjects,
useThreadShells,
useThreadShellsForProjectRefs,
waitForProjectRemoval,
} from "../state/entities";
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
import { useThreadRunningTerminalIds } from "../state/terminalSessions";
@@ -1542,6 +1543,7 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
if (result._tag === "Failure") {
return result;
}
await waitForProjectRemoval(memberProjectRef);
const draftStore = useComposerDraftStore.getState();
releaseProjectDraftUploads(
memberProjectRef,
@@ -76,6 +76,7 @@ vi.mock("~/state/environments", () => ({
vi.mock("~/state/entities", () => ({
useProjects: () => state.projects,
useThreadShells: () => [],
waitForProjectRemoval: async () => true,
}));
vi.mock("~/state/projects", () => ({
projectEnvironment: { update: "update", delete: "delete" },
@@ -25,7 +25,7 @@ import {
type SidebarProjectSnapshot,
} from "../../sidebarProjectGrouping";
import { useEnvironments, usePrimaryEnvironmentId } from "../../state/environments";
import { useThreadShells } from "../../state/entities";
import { useThreadShells, waitForProjectRemoval } from "../../state/entities";
import { projectEnvironment } from "../../state/projects";
import { ProjectFavicon } from "../ProjectFavicon";
import { Alert, AlertDescription } from "../ui/alert";
@@ -395,6 +395,7 @@ function ProjectDetail({
return;
}
const projectRef = scopeProjectRef(member.environmentId, member.id);
await waitForProjectRemoval(projectRef);
releaseProjectDraftUploads(
projectRef,
memberThreads.map((thread) => scopeThreadRef(thread.environmentId, thread.id)),
+40 -15
View File
@@ -4,6 +4,7 @@ import type { RuntimeMode } from "@t3tools/contracts";
const testState = vi.hoisted(() => {
let completeProjectFileRead: (value: null) => void = () => undefined;
let projectFileRead = Promise.resolve<null>(null);
let projectRemoved = false;
let targetSettings = {
defaultThreadEnvMode: "local" as "local" | "worktree",
newWorktreesStartFromOrigin: false,
@@ -42,6 +43,12 @@ const testState = vi.hoisted(() => {
get projectFileRead() {
return projectFileRead;
},
get projectRemoved() {
return projectRemoved;
},
removeProject() {
projectRemoved = true;
},
get targetSettings() {
return targetSettings;
},
@@ -53,6 +60,7 @@ const testState = vi.hoisted(() => {
},
) {
storedDraft = nextStoredDraft;
projectRemoved = false;
targetSettings = {
defaultThreadEnvMode: workspaceDefaults.envMode,
newWorktreesStartFromOrigin: workspaceDefaults.startFromOrigin,
@@ -159,21 +167,23 @@ vi.mock("../logicalProject", () => ({
getProjectOrderKey: () => "remote-project",
selectProjectGroupingSettings: () => ({}),
}));
vi.mock("../state/entities", () => ({
readProjects: () => [
{
id: "project-remote",
environmentId: "environment-ssh",
workspaceRoot: "/remote/project",
defaultThreadEnvMode: null,
defaultModelSelection: null,
},
],
readThreadShell: () => null,
readThreadShells: () => [],
useProjects: () => [],
useThread: () => null,
}));
vi.mock("../state/entities", () => {
const project = {
id: "project-remote",
environmentId: "environment-ssh",
workspaceRoot: "/remote/project",
defaultThreadEnvMode: null,
defaultModelSelection: null,
};
return {
readProject: () => (testState.projectRemoved ? null : project),
readProjects: () => (testState.projectRemoved ? [] : [project]),
readThreadShell: () => null,
readThreadShells: () => [],
useProjects: () => [],
useThread: () => null,
};
});
vi.mock("../state/server", () => ({
environmentServerConfigsAtom: {},
primaryServerSettingsAtom: "primary-settings",
@@ -238,6 +248,21 @@ describe.each([
expect(testState.draftStore.setLogicalProjectDraftThreadId).not.toHaveBeenCalled();
});
it("abandons a delayed draft open when the project is removed meanwhile", async () => {
testState.reset(draft);
const pendingOpen = useNewThreadHandler()(
{ environmentId: "environment-ssh", projectId: "project-remote" } as never,
{ replace: true },
);
testState.removeProject();
testState.completeProjectFileRead(null);
await expect(pendingOpen).resolves.toBeNull();
expect(testState.router.navigate).not.toHaveBeenCalled();
expect(testState.draftStore.setLogicalProjectDraftThreadId).not.toHaveBeenCalled();
});
it.each([true, false])(
"uses the target environment's start-from-origin default of %s",
async (startFromOrigin) => {
+7 -2
View File
@@ -24,6 +24,7 @@ import {
} from "../logicalProject";
import { resolveNewThreadEnvMode, resolveProjectSettings } from "@t3tools/shared/projectSettings";
import {
readProject,
readProjects,
readThreadShell,
readThreadShells,
@@ -134,6 +135,10 @@ export function useNewThreadHandler() {
candidate.id === projectRef.projectId &&
candidate.environmentId === projectRef.environmentId,
);
// Removal can land while the project file is read; a draft opened
// after that belongs to a project that no longer exists.
const abandonedSinceRequest = () =>
routeChangedSinceRequest() || (project !== undefined && readProject(projectRef) === null);
// The resolver applies project overrides and, until the server has
// folded them, the aggregate's own legacy fields.
const projectSettings = resolveProjectSettings(
@@ -237,7 +242,7 @@ export function useNewThreadHandler() {
workspaceContext = pickExplicitWorkspaceOptions(options);
} else if (!isDraftAlreadyOpen) {
const defaultEnvMode = await resolveDefaultEnvMode();
if (routeChangedSinceRequest()) {
if (abandonedSinceRequest()) {
return null;
}
// The await yields. If the draft was opened (a concurrent
@@ -376,7 +381,7 @@ export function useNewThreadHandler() {
const createdAt = new Date().toISOString();
return (async () => {
const initialEnvMode = options?.envMode ?? (await resolveDefaultEnvMode());
if (routeChangedSinceRequest()) {
if (abandonedSinceRequest()) {
return null;
}
// The await yields, so a concurrent invocation may have registered a
@@ -6,6 +6,7 @@ import { useCallback } from "react";
import { useComposerDraftStore } from "../composerDraftStore";
import { resolveThreadRouteTarget } from "../threadRoutes";
import { releaseProjectDraftUploads } from "../lib/composerDraftUploads";
import { waitForProjectRemoval } from "../state/entities";
import { projectEnvironment } from "../state/projects";
import { useAtomCommand } from "../state/use-atom-command";
import { stackedThreadToast, toastManager } from "../components/ui/toast";
@@ -41,6 +42,7 @@ export function useRemoveClonedProject() {
);
return false;
}
await waitForProjectRemoval(projectRef);
// Read the route after the await: the user may have moved on while the
// delete was in flight, and only a draft of this project needs to go.
const routeParams = router.state.matches[router.state.matches.length - 1]?.params ?? {};
+19 -6
View File
@@ -14,6 +14,7 @@ import { SidebarInset } from "../components/ui/sidebar";
import { WorkspacePageHeader } from "../components/WorkspacePageHeader";
import { useNewThreadHandler } from "../hooks/useHandleNewThread";
import {
readProject,
useAllEnvironmentShellsBootstrapped,
useProjects,
useThreadShells,
@@ -64,13 +65,25 @@ function IndexDraftLanding() {
if (mostRecentProject === null || startingRef.current || !isStillOnIndex()) {
return;
}
const projectRef = scopeProjectRef(mostRecentProject.environmentId, mostRecentProject.id);
// A project removed since this render is gone from the store; the next
// render picks another one.
if (readProject(projectRef) === null) {
return;
}
startingRef.current = true;
void handleNewThread(scopeProjectRef(mostRecentProject.environmentId, mostRecentProject.id), {
replace: true,
}).catch(() => {
startingRef.current = false;
setStartState((state) => ({ ...state, failed: true }));
});
void handleNewThread(projectRef, { replace: true })
.then((opened) => {
// The project was removed while its draft started; land on the next one.
if (opened === null) {
startingRef.current = false;
setStartState((state) => ({ ...state, retryRequest: state.retryRequest + 1 }));
}
})
.catch(() => {
startingRef.current = false;
setStartState((state) => ({ ...state, failed: true }));
});
}, [handleNewThread, mostRecentProject, startState.retryRequest]);
if (!bootstrapped) {
+14
View File
@@ -194,6 +194,20 @@ export function waitForProject(
});
}
/**
* Resolves once a deleted project leaves the live client store. The delete
* command returns before the shell stream does, so anything that picks a
* project in between (the index route's draft landing) still sees it.
*/
export function waitForProjectRemoval(ref: ScopedProjectRef, timeoutMs = 5_000): Promise<boolean> {
return waitForAtomValue({
registry: appAtomRegistry,
atom: environmentProjects.projectAtom(ref),
predicate: (project) => project === null,
timeoutMs,
});
}
export function readThreadShell(ref: ScopedThreadRef): EnvironmentThreadShell | null {
return appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref));
}