Skip to content

Commit d7a9fc9

Browse files
Yuruzuuclaude
andcommitted
fix(web): keep a side chat's draft until its deletion is confirmed
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8bd2d46 commit d7a9fc9

3 files changed

Lines changed: 43 additions & 16 deletions

File tree

‎apps/web/src/hooks/useRightPanelSurfaceClose.browser.test.tsx‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const fixture = vi.hoisted(() => ({
1212
sessions: new Map<string, string>(),
1313
failClose: false,
1414
sideChatShell: null as { latestRun: { runId: string } | null } | null,
15+
threadRemoved: false,
16+
failDelete: false,
17+
shellStatus: "live",
1518
clearedDrafts: [] as ScopedThreadRef[],
1619
}));
1720
vi.mock("../lib/terminalCloseConfirm", () => ({
@@ -34,6 +37,7 @@ vi.mock("../state/entities", () => ({
3437
useQuietThreadShell: () => null,
3538
useProject: () => null,
3639
readThreadShell: () => fixture.sideChatShell,
40+
waitForThreadRemoval: async () => fixture.threadRemoved,
3741
}));
3842
vi.mock("../state/terminalSessions", () => ({ useKnownTerminalSessions: () => null }));
3943
vi.mock("../composerDraftStore", async () => {
@@ -56,6 +60,7 @@ vi.mock("../state/use-atom-command", () => ({
5660
}) => {
5761
const key = `${request.environmentId}/${request.input.threadId}/${request.input.terminalId}`;
5862
if (label === "terminal close" && fixture.failClose) return { _tag: "Failure" };
63+
if (label === "side chat delete" && fixture.failDelete) return { _tag: "Failure" };
5964
if (label === "terminal close" || label === "terminal write") fixture.sessions.delete(key);
6065
return { _tag: "Success" };
6166
},
@@ -65,7 +70,9 @@ vi.mock("../state/terminal", () => ({ terminalEnvironment: { open: {}, close: {}
6570
vi.mock("../state/preview", () => ({ previewEnvironment: { close: {} } }));
6671
vi.mock("../state/threads", () => ({ threadEnvironment: { delete: {} } }));
6772
vi.mock("../state/shell", () => ({ environmentShell: { stateValueAtom: () => null } }));
68-
vi.mock("../rpc/atomRegistry", () => ({ appAtomRegistry: { get: () => ({ status: "live" }) } }));
73+
vi.mock("../rpc/atomRegistry", () => ({
74+
appAtomRegistry: { get: () => ({ status: fixture.shellStatus }) },
75+
}));
6976
vi.mock("@t3tools/client-runtime/state/runtime", () => ({
7077
isAtomCommandInterrupted: () => false,
7178
}));
@@ -75,6 +82,9 @@ function setup() {
7582
fixture.sessions = new Map();
7683
fixture.failClose = false;
7784
fixture.sideChatShell = null;
85+
fixture.threadRemoved = false;
86+
fixture.failDelete = false;
87+
fixture.shellStatus = "live";
7888
fixture.clearedDrafts = [];
7989
useRightPanelStore.setState({ byThreadKey: {} });
8090
useTerminalUiStateStore.setState({ terminalUiStateByThreadKey: {} });
@@ -145,14 +155,20 @@ it.each(["cancel", "revoked"])(
145155
},
146156
);
147157

158+
// The local shell lags the server, so a run-less shell never proves the side chat was deleted.
148159
it.each([
149-
{ latestRun: null, cleared: true },
150-
{ latestRun: { runId: "run-1" }, cleared: false },
160+
{ name: "the thread leaves the live shell", threadRemoved: true, cleared: true },
161+
{ name: "the server keeps the thread", threadRemoved: false, cleared: false },
162+
{ name: "the delete fails", threadRemoved: true, failDelete: true, cleared: false },
163+
{ name: "the shell is not live", threadRemoved: true, shellStatus: "loading", cleared: false },
151164
])(
152-
"clears a closed side chat's draft only when it never ran, cleared=$cleared",
153-
async ({ latestRun, cleared }) => {
165+
"clears a closed side chat's draft only after deletion: $name",
166+
async ({ threadRemoved, failDelete = false, shellStatus = "live", cleared }) => {
154167
const { original } = setup();
155-
fixture.sideChatShell = { latestRun };
168+
fixture.sideChatShell = { latestRun: null };
169+
fixture.threadRemoved = threadRemoved;
170+
fixture.failDelete = failDelete;
171+
fixture.shellStatus = shellStatus;
156172
const sideChatId = ThreadId.make("side-chat-a");
157173
useRightPanelStore.getState().openSideChat(original, sideChatId);
158174
const surface = selectThreadRightPanelState(
@@ -168,6 +184,7 @@ it.each([
168184
}
169185
await using screen = await render(<SideChatHarness />);
170186
await screen.getByRole("button").click();
187+
await act(async () => {});
171188
expect(fixture.clearedDrafts).toEqual(
172189
cleared ? [{ environmentId: original.environmentId, threadId: sideChatId }] : [],
173190
);

‎apps/web/src/hooks/useRightPanelSurfaceClose.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
type RightPanelSurface,
2222
} from "../rightPanelStore";
2323
import { appAtomRegistry } from "../rpc/atomRegistry";
24-
import { readThreadShell } from "../state/entities";
24+
import { readThreadShell, waitForThreadRemoval } from "../state/entities";
2525
import { previewEnvironment } from "../state/preview";
2626
import { readEnvironmentScope, useEnvironmentScope } from "../state/session";
2727
import { environmentShell } from "../state/shell";
@@ -71,23 +71,23 @@ export function useRightPanelSurfaceClose(input: {
7171
terminalActions.closeTerminalSession(terminalId, ref);
7272
}
7373
}
74+
if (surface.kind !== "side-chat") return;
75+
const sideChatRef = scopeThreadRef(ref.environmentId, surface.threadId);
76+
const shellIsLive = () =>
77+
appAtomRegistry.get(environmentShell.stateValueAtom(ref.environmentId)).status === "live";
7478
if (
75-
surface.kind === "side-chat" &&
7679
readEnvironmentScope(ref.environmentId, AuthOrchestrationOperateScope) &&
7780
// A live shell without the side chat means it is already deleted.
78-
!(
79-
appAtomRegistry.get(environmentShell.stateValueAtom(ref.environmentId)).status === "live" &&
80-
readThreadShell(scopeThreadRef(ref.environmentId, surface.threadId)) === null
81-
)
81+
!(shellIsLive() && readThreadShell(sideChatRef) === null)
8282
) {
83-
const sideChatRef = scopeThreadRef(ref.environmentId, surface.threadId);
84-
if (readThreadShell(sideChatRef)?.latestRun === null) {
85-
useComposerDraftStore.getState().clearDraftThread(sideChatRef);
86-
}
8783
// The server keeps a side chat with a run or message. Reopen skips one it deleted.
8884
void deleteThread({
8985
environmentId: ref.environmentId,
9086
input: { threadId: surface.threadId, onlyIfUnused: true },
87+
}).then(async (result) => {
88+
// Success also covers a kept side chat, whose draft must survive.
89+
if (result._tag !== "Success" || !(await waitForThreadRemoval(sideChatRef))) return;
90+
if (shellIsLive()) useComposerDraftStore.getState().clearDraftThread(sideChatRef);
9191
});
9292
}
9393
};

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ export function waitForThreadShell(ref: ScopedThreadRef, timeoutMs = 5_000): Pro
321321
});
322322
}
323323

324+
/** Waits for removal from the live store; delete replies arrive before the shell stream. */
325+
export function waitForThreadRemoval(ref: ScopedThreadRef, timeoutMs = 5_000): Promise<boolean> {
326+
return waitForAtomValue({
327+
registry: appAtomRegistry,
328+
atom: environmentThreadShells.threadShellAtom(ref),
329+
predicate: (thread) => thread === null,
330+
timeoutMs,
331+
});
332+
}
333+
324334
export function useEnvironmentSupportsServerBrowser(environmentId: EnvironmentId | null): boolean {
325335
const configs = useServerConfigs();
326336
return (

0 commit comments

Comments
 (0)