Skip to content

Commit fd78ea4

Browse files
only21milcodexmaria-rcks
authored
fix(web): show an error when merge-back or disconnect fails (#23753)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 8689c7a commit fd78ea4

2 files changed

Lines changed: 152 additions & 3 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2+
import * as Cause from "effect/Cause";
3+
import { AsyncResult } from "effect/reactivity";
4+
import { expect, it, vi } from "vite-plus/test";
5+
import { page } from "vite-plus/test/browser";
6+
7+
const state = vi.hoisted(() => ({ projection: null as unknown, commandSucceeds: false }));
8+
9+
vi.mock(import("@effect/atom-react"), async (original) => ({
10+
...(await original()),
11+
useAtomValue: (() => false) as never,
12+
}));
13+
14+
vi.mock(import("../../state/entities"), () => ({
15+
useQuietThreadShell: (() => null) as never,
16+
useThreadProjection: ((ref: unknown) =>
17+
ref === null ? null : { projection: state.projection }) as never,
18+
useThreadRelationshipShells: (() =>
19+
state.projection === null ? [] : [(state.projection as { thread: unknown }).thread]) as never,
20+
useProjects: (() => []) as never,
21+
}));
22+
vi.mock(import("../../state/environments"), () => ({
23+
useEnvironment: (() => null) as never,
24+
useEnvironmentProviders: (() => new Map()) as never,
25+
}));
26+
vi.mock(import("../../lib/archivedThreadsState"), () => ({
27+
useArchivedThreadSnapshots: (() => ({ snapshots: [] })) as never,
28+
}));
29+
vi.mock(import("../../state/use-atom-command"), () => ({
30+
useAtomCommand: (() => async () =>
31+
state.commandSucceeds
32+
? AsyncResult.success(undefined)
33+
: AsyncResult.failure(Cause.fail(new Error("Connection lost")))) as never,
34+
}));
35+
36+
import { render } from "../../../test/render";
37+
import { ToastProvider } from "../ui/toast";
38+
import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl";
39+
40+
const environmentId = EnvironmentId.make("environment");
41+
const threadId = ThreadId.make("fork");
42+
43+
function setup() {
44+
state.commandSucceeds = false;
45+
state.projection = {
46+
thread: {
47+
id: threadId,
48+
title: "Fork",
49+
activeProviderThreadId: null,
50+
lineage: { parentThreadId: ThreadId.make("source"), relationshipToParent: "fork" },
51+
forkedFrom: null,
52+
},
53+
runs: [{ id: "run", ordinal: 1, status: "completed" }],
54+
subagents: [],
55+
contextTransfers: [],
56+
providerThreads: [],
57+
providerSessions: [{ id: "session", status: "ready" }],
58+
};
59+
}
60+
61+
it.each([
62+
["merge", "Could not merge back"],
63+
["disconnect", "Could not disconnect agent session"],
64+
])("reports a rejected %s command and clears the busy state", async (action, title) => {
65+
setup();
66+
await using screen = await render(
67+
<ToastProvider timeout={0}>
68+
<ThreadRelationshipsPanel environmentId={environmentId} threadId={threadId} />
69+
</ToastProvider>,
70+
);
71+
72+
if (action === "merge") {
73+
await screen.getByRole("button", { name: "Merge back to source conversation" }).click();
74+
} else {
75+
await screen.getByRole("button", { name: "More thread actions" }).click();
76+
await page.getByRole("menuitem", { name: "Disconnect agent session" }).click();
77+
}
78+
79+
await expect.element(page.getByText(title, { exact: true })).toBeVisible();
80+
await expect
81+
.element(
82+
screen.getByRole("button", {
83+
name: action === "merge" ? "Merge back to source conversation" : "More thread actions",
84+
}),
85+
)
86+
.toBeEnabled();
87+
});
88+
89+
it.each([
90+
["merge", "Could not merge back"],
91+
["disconnect", "Could not disconnect agent session"],
92+
])("clears the previous %s error after a successful retry", async (action, title) => {
93+
setup();
94+
await using screen = await render(
95+
<ToastProvider timeout={0}>
96+
<ThreadRelationshipsPanel environmentId={environmentId} threadId={threadId} />
97+
</ToastProvider>,
98+
{ router: "/environment/fork" },
99+
);
100+
101+
const runAction = async () => {
102+
if (action === "merge") {
103+
await screen.getByRole("button", { name: "Merge back to source conversation" }).click();
104+
} else {
105+
await screen.getByRole("button", { name: "More thread actions" }).click();
106+
await page.getByRole("menuitem", { name: "Disconnect agent session" }).click();
107+
}
108+
};
109+
110+
await runAction();
111+
await expect.element(page.getByText(title, { exact: true })).toBeVisible();
112+
expect(screen.router?.state.location.pathname).toBe("/environment/fork");
113+
114+
state.commandSucceeds = true;
115+
await runAction();
116+
117+
await expect.element(page.getByText(title, { exact: true })).not.toBeInTheDocument();
118+
await vi.waitFor(() => {
119+
expect(screen.router?.state.location.pathname).toBe(
120+
action === "merge" ? "/environment/source" : "/environment/fork",
121+
);
122+
});
123+
if (action === "disconnect") {
124+
await expect.element(screen.getByRole("button", { name: "More thread actions" })).toBeEnabled();
125+
}
126+
});

‎apps/web/src/components/chat/ThreadRelationshipsControl.tsx‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
SquareIcon,
5555
UnplugIcon,
5656
} from "lucide-react";
57-
import { useState, type ReactNode } from "react";
57+
import { useRef, useState, type ReactNode } from "react";
5858

5959
import { useArchivedThreadSnapshots } from "../../lib/archivedThreadsState";
6060
import { buildThreadRouteParams } from "../../threadRoutes";
@@ -385,6 +385,8 @@ export function ThreadRelationshipsPanel(props: {
385385
orchestrationEnvironment.stopWorkflow.permissionAtom(props.environmentId),
386386
);
387387
const [busyAction, setBusyAction] = useState<"merge" | "detach" | null>(null);
388+
const mergeErrorToastId = useRef<string | null>(null);
389+
const detachErrorToastId = useRef<string | null>(null);
388390
const [stoppingId, setStoppingId] = useState<string | null>(null);
389391
// Derive graphs after hooks to preserve their compiler cache.
390392
const providers = providersByEnvironment.get(props.environmentId);
@@ -492,6 +494,10 @@ export function ThreadRelationshipsPanel(props: {
492494

493495
const merge = async () => {
494496
if (!latestMergeBackRun || mergeTargetThreadId === null || busyAction !== null) return;
497+
if (mergeErrorToastId.current !== null) {
498+
toastManager.close(mergeErrorToastId.current);
499+
mergeErrorToastId.current = null;
500+
}
495501
setBusyAction("merge");
496502
const result = await mergeBack({
497503
environmentId: props.environmentId,
@@ -502,17 +508,34 @@ export function ThreadRelationshipsPanel(props: {
502508
},
503509
});
504510
setBusyAction(null);
505-
if (result._tag === "Success") openThread(mergeTargetThreadId);
511+
if (result._tag === "Failure") {
512+
mergeErrorToastId.current = toastManager.add({
513+
type: "error",
514+
title: "Could not merge back",
515+
});
516+
return;
517+
}
518+
openThread(mergeTargetThreadId);
506519
};
507520

508521
const detach = async () => {
509522
if (!canDetach || busyAction !== null) return;
523+
if (detachErrorToastId.current !== null) {
524+
toastManager.close(detachErrorToastId.current);
525+
detachErrorToastId.current = null;
526+
}
510527
setBusyAction("detach");
511-
await stopSession({
528+
const result = await stopSession({
512529
environmentId: props.environmentId,
513530
input: { threadId: props.threadId },
514531
});
515532
setBusyAction(null);
533+
if (result._tag === "Failure") {
534+
detachErrorToastId.current = toastManager.add({
535+
type: "error",
536+
title: "Could not disconnect agent session",
537+
});
538+
}
516539
};
517540

518541
const stopSubagent = async (

0 commit comments

Comments
 (0)