Skip to content
TypeScript

fix(web): show an error when merge-back or disconnect fails (#23753)

Victor Vogelfd78ea42fcce
diff --git a/apps/web/src/components/chat/ThreadRelationshipsControl.browser.test.tsx b/apps/web/src/components/chat/ThreadRelationshipsControl.browser.test.tsx
new file mode 100644
index 00000000000..88bd956decc
--- /dev/null
+++ b/apps/web/src/components/chat/ThreadRelationshipsControl.browser.test.tsx
@@ -0,0 +1,126 @@
+import { EnvironmentId, ThreadId } from "@t3tools/contracts";
+import * as Cause from "effect/Cause";
+import { AsyncResult } from "effect/reactivity";
+import { expect, it, vi } from "vite-plus/test";
+import { page } from "vite-plus/test/browser";
+
+const state = vi.hoisted(() => ({ projection: null as unknown, commandSucceeds: false }));
+
+vi.mock(import("@effect/atom-react"), async (original) => ({
+ ...(await original()),
+ useAtomValue: (() => false) as never,
+}));
+
+vi.mock(import("../../state/entities"), () => ({
+ useQuietThreadShell: (() => null) as never,
+ useThreadProjection: ((ref: unknown) =>
+ ref === null ? null : { projection: state.projection }) as never,
+ useThreadRelationshipShells: (() =>
+ state.projection === null ? [] : [(state.projection as { thread: unknown }).thread]) as never,
+ useProjects: (() => []) as never,
+}));
+vi.mock(import("../../state/environments"), () => ({
+ useEnvironment: (() => null) as never,
+ useEnvironmentProviders: (() => new Map()) as never,
+}));
+vi.mock(import("../../lib/archivedThreadsState"), () => ({
+ useArchivedThreadSnapshots: (() => ({ snapshots: [] })) as never,
+}));
+vi.mock(import("../../state/use-atom-command"), () => ({
+ useAtomCommand: (() => async () =>
+ state.commandSucceeds
+ ? AsyncResult.success(undefined)
+ : AsyncResult.failure(Cause.fail(new Error("Connection lost")))) as never,
+}));
+
+import { render } from "../../../test/render";
+import { ToastProvider } from "../ui/toast";
+import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl";
+
+const environmentId = EnvironmentId.make("environment");
+const threadId = ThreadId.make("fork");
+
+function setup() {
+ state.commandSucceeds = false;
+ state.projection = {
+ thread: {
+ id: threadId,
+ title: "Fork",
+ activeProviderThreadId: null,
+ lineage: { parentThreadId: ThreadId.make("source"), relationshipToParent: "fork" },
+ forkedFrom: null,
+ },
+ runs: [{ id: "run", ordinal: 1, status: "completed" }],
+ subagents: [],
+ contextTransfers: [],
+ providerThreads: [],
+ providerSessions: [{ id: "session", status: "ready" }],
+ };
+}
+
+it.each([
+ ["merge", "Could not merge back"],
+ ["disconnect", "Could not disconnect agent session"],
+])("reports a rejected %s command and clears the busy state", async (action, title) => {
+ setup();
+ await using screen = await render(
+ <ToastProvider timeout={0}>
+ <ThreadRelationshipsPanel environmentId={environmentId} threadId={threadId} />
+ </ToastProvider>,
+ );
+
+ if (action === "merge") {
+ await screen.getByRole("button", { name: "Merge back to source conversation" }).click();
+ } else {
+ await screen.getByRole("button", { name: "More thread actions" }).click();
+ await page.getByRole("menuitem", { name: "Disconnect agent session" }).click();
+ }
+
+ await expect.element(page.getByText(title, { exact: true })).toBeVisible();
+ await expect
+ .element(
+ screen.getByRole("button", {
+ name: action === "merge" ? "Merge back to source conversation" : "More thread actions",
+ }),
+ )
+ .toBeEnabled();
+});
+
+it.each([
+ ["merge", "Could not merge back"],
+ ["disconnect", "Could not disconnect agent session"],
+])("clears the previous %s error after a successful retry", async (action, title) => {
+ setup();
+ await using screen = await render(
+ <ToastProvider timeout={0}>
+ <ThreadRelationshipsPanel environmentId={environmentId} threadId={threadId} />
+ </ToastProvider>,
+ { router: "/environment/fork" },
+ );
+
+ const runAction = async () => {
+ if (action === "merge") {
+ await screen.getByRole("button", { name: "Merge back to source conversation" }).click();
+ } else {
+ await screen.getByRole("button", { name: "More thread actions" }).click();
+ await page.getByRole("menuitem", { name: "Disconnect agent session" }).click();
+ }
+ };
+
+ await runAction();
+ await expect.element(page.getByText(title, { exact: true })).toBeVisible();
+ expect(screen.router?.state.location.pathname).toBe("/environment/fork");
+
+ state.commandSucceeds = true;
+ await runAction();
+
+ await expect.element(page.getByText(title, { exact: true })).not.toBeInTheDocument();
+ await vi.waitFor(() => {
+ expect(screen.router?.state.location.pathname).toBe(
+ action === "merge" ? "/environment/source" : "/environment/fork",
+ );
+ });
+ if (action === "disconnect") {
+ await expect.element(screen.getByRole("button", { name: "More thread actions" })).toBeEnabled();
+ }
+});
diff --git a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx
index 69403392b5c..63a8635c066 100644
--- a/apps/web/src/components/chat/ThreadRelationshipsControl.tsx
+++ b/apps/web/src/components/chat/ThreadRelationshipsControl.tsx
@@ -54,7 +54,7 @@ import {
SquareIcon,
UnplugIcon,
} from "lucide-react";
-import { useState, type ReactNode } from "react";
+import { useRef, useState, type ReactNode } from "react";
import { useArchivedThreadSnapshots } from "../../lib/archivedThreadsState";
import { buildThreadRouteParams } from "../../threadRoutes";
@@ -385,6 +385,8 @@ export function ThreadRelationshipsPanel(props: {
orchestrationEnvironment.stopWorkflow.permissionAtom(props.environmentId),
);
const [busyAction, setBusyAction] = useState<"merge" | "detach" | null>(null);
+ const mergeErrorToastId = useRef<string | null>(null);
+ const detachErrorToastId = useRef<string | null>(null);
const [stoppingId, setStoppingId] = useState<string | null>(null);
// Derive graphs after hooks to preserve their compiler cache.
const providers = providersByEnvironment.get(props.environmentId);
@@ -492,6 +494,10 @@ export function ThreadRelationshipsPanel(props: {
const merge = async () => {
if (!latestMergeBackRun || mergeTargetThreadId === null || busyAction !== null) return;
+ if (mergeErrorToastId.current !== null) {
+ toastManager.close(mergeErrorToastId.current);
+ mergeErrorToastId.current = null;
+ }
setBusyAction("merge");
const result = await mergeBack({
environmentId: props.environmentId,
@@ -502,17 +508,34 @@ export function ThreadRelationshipsPanel(props: {
},
});
setBusyAction(null);
- if (result._tag === "Success") openThread(mergeTargetThreadId);
+ if (result._tag === "Failure") {
+ mergeErrorToastId.current = toastManager.add({
+ type: "error",
+ title: "Could not merge back",
+ });
+ return;
+ }
+ openThread(mergeTargetThreadId);
};
const detach = async () => {
if (!canDetach || busyAction !== null) return;
+ if (detachErrorToastId.current !== null) {
+ toastManager.close(detachErrorToastId.current);
+ detachErrorToastId.current = null;
+ }
setBusyAction("detach");
- await stopSession({
+ const result = await stopSession({
environmentId: props.environmentId,
input: { threadId: props.threadId },
});
setBusyAction(null);
+ if (result._tag === "Failure") {
+ detachErrorToastId.current = toastManager.add({
+ type: "error",
+ title: "Could not disconnect agent session",
+ });
+ }
};
const stopSubagent = async (