Open History to see the last commit
1import * as DateTime from "effect/DateTime";
2import { EnvironmentId, NodeId, ThreadId } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import { AsyncResult } from "effect/reactivity";
5import { expect, it, vi } from "vite-plus/test";
6import { page, userEvent } from "vite-plus/test/browser";
7
8const state = vi.hoisted(() => ({
9 projection: null as unknown,
10 commandSucceeds: false,
11 relatedShells: [] as unknown[],
12}));
13
14vi.mock(import("@effect/atom-react"), async (original) => ({
15 ...(await original()),
16 useAtomValue: (() => false) as never,
17}));
18
19vi.mock(import("../../state/entities"), () => ({
20 useQuietThreadShell: (() => null) as never,
21 useThreadProjection: ((ref: unknown) =>
22 ref === null ? null : { projection: state.projection }) as never,
23 useThreadRelationshipShells: (() =>
24 state.projection === null
25 ? []
26 : [(state.projection as { thread: unknown }).thread, ...state.relatedShells]) as never,
27 useProjects: (() => []) as never,
28 useHiddenThreadKeys: (() => new Set()) as never,
29 useAnyEnvironmentHiding: (() => false) as never,
30}));
31vi.mock(import("../../state/environments"), () => ({
32 useEnvironment: (() => null) as never,
33 useEnvironmentProviders: (() => new Map()) as never,
34}));
35vi.mock(import("../../lib/archivedThreadsState"), () => ({
36 useArchivedThreadSnapshots: (() => ({ snapshots: [] })) as never,
37}));
38vi.mock(import("../../state/use-atom-command"), () => ({
39 useAtomCommand: (() => async () =>
40 state.commandSucceeds
41 ? AsyncResult.success(undefined)
42 : AsyncResult.failure(Cause.fail(new Error("Connection lost")))) as never,
43}));
44
45import { render } from "../../../test/render";
46import { makeThreadProjectionFixture } from "../../test-fixtures";
47import { ToastProvider } from "../ui/toast";
48import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl";
49
50const environmentId = EnvironmentId.make("environment");
51const threadId = ThreadId.make("fork");
52
53function setup() {
54 state.commandSucceeds = false;
55 state.relatedShells = [];
56 state.projection = {
57 thread: {
58 id: threadId,
59 title: "Fork",
60 activeProviderThreadId: null,
61 lineage: { parentThreadId: ThreadId.make("source"), relationshipToParent: "fork" },
62 forkedFrom: null,
63 },
64 runs: [{ id: "run", ordinal: 1, status: "completed" }],
65 subagents: [],
66 contextTransfers: [],
67 providerThreads: [],
68 providerSessions: [{ id: "session", status: "ready" }],
69 };
70}
71
72it.each([
73 ["merge", "Could not merge back"],
74 ["disconnect", "Could not disconnect agent session"],
75])("reports a rejected %s command and clears the busy state", async (action, title) => {
76 setup();
77 await using screen = await render(
78 <ToastProvider timeout={0}>
79 <ThreadRelationshipsPanel
80 environmentId={environmentId}
81 threadId={threadId}
82 showLineage
83 showSideChats
84 />
85 </ToastProvider>,
86 );
87
88 if (action === "merge") {
89 await screen.getByRole("button", { name: "Merge back to source conversation" }).click();
90 } else {
91 await screen.getByRole("button", { name: "More thread actions" }).click();
92 await page.getByRole("menuitem", { name: "Disconnect agent session" }).click();
93 }
94
95 await expect.element(page.getByText(title, { exact: true })).toBeVisible();
96 await expect
97 .element(
98 screen.getByRole("button", {
99 name: action === "merge" ? "Merge back to source conversation" : "More thread actions",
100 }),
101 )
102 .toBeEnabled();
103});
104
105it.each([
106 ["merge", "Could not merge back"],
107 ["disconnect", "Could not disconnect agent session"],
108])("clears the previous %s error after a successful retry", async (action, title) => {
109 setup();
110 await using screen = await render(
111 <ToastProvider timeout={0}>
112 <ThreadRelationshipsPanel
113 environmentId={environmentId}
114 threadId={threadId}
115 showLineage
116 showSideChats
117 />
118 </ToastProvider>,
119 { router: "/environment/fork" },
120 );
121
122 const runAction = async () => {
123 if (action === "merge") {
124 await screen.getByRole("button", { name: "Merge back to source conversation" }).click();
125 } else {
126 await screen.getByRole("button", { name: "More thread actions" }).click();
127 await page.getByRole("menuitem", { name: "Disconnect agent session" }).click();
128 }
129 };
130
131 await runAction();
132 await expect.element(page.getByText(title, { exact: true })).toBeVisible();
133 expect(screen.router?.state.location.pathname).toBe("/environment/fork");
134
135 state.commandSucceeds = true;
136 await runAction();
137
138 await expect.element(page.getByText(title, { exact: true })).not.toBeInTheDocument();
139 await vi.waitFor(() => {
140 expect(screen.router?.state.location.pathname).toBe(
141 action === "merge" ? "/environment/source" : "/environment/fork",
142 );
143 });
144 if (action === "disconnect") {
145 await expect.element(screen.getByRole("button", { name: "More thread actions" })).toBeEnabled();
146 }
147});
148
149it("opens an idle child from Lineage when its shell has not been loaded", async () => {
150 setup();
151 const projection = makeThreadProjectionFixture();
152 const childId = ThreadId.make("omitted-child");
153 const environmentId = EnvironmentId.make("local");
154 state.projection = {
155 ...projection,
156 subagents: [
157 {
158 id: NodeId.make("agent"),
159 threadId: projection.thread.id,
160 runId: null,
161 parentNodeId: NodeId.make("parent"),
162 origin: "app_owned",
163 createdBy: "agent",
164 driver: "codex",
165 providerInstanceId: projection.thread.providerInstanceId,
166 providerThreadId: null,
167 childThreadId: childId,
168 nativeTaskRef: null,
169 prompt: "Inspect the change",
170 title: "Idle child",
171 model: null,
172 status: "completed",
173 result: null,
174 startedAt: null,
175 completedAt: projection.thread.updatedAt,
176 updatedAt: projection.thread.updatedAt,
177 },
178 ],
179 };
180 await using screen = await render(
181 <ThreadRelationshipsPanel
182 environmentId={environmentId}
183 threadId={projection.thread.id}
184 showLineage
185 showSideChats
186 />,
187 );
188 await screen.getByRole("button", { name: /Previous agents/ }).click();
189 const child = screen.getByRole("button", { name: /Idle child/ });
190 await expect.element(child).toBeEnabled();
191 await child.click();
192 await vi.waitFor(() =>
193 expect(screen.router?.state.location.pathname).toBe("/local/omitted-child"),
194 );
195});
196
197it("shows a lineage thread's full title in its tooltip", async () => {
198 setup();
199 const longTitle = "Compare the lineage tooltip against a title that the row truncates";
200 const sourceThread = {
201 ...(state.projection as { thread: Record<string, unknown> }).thread,
202 id: "source",
203 title: longTitle,
204 lineage: { parentThreadId: null, relationshipToParent: null },
205 };
206 state.relatedShells = [sourceThread];
207 await using _screen = await render(
208 <ThreadRelationshipsPanel
209 environmentId={environmentId}
210 threadId={threadId}
211 showLineage
212 showSideChats={false}
213 />,
214 );
215
216 await userEvent.hover(page.getByText(longTitle, { exact: true }));
217 await vi.waitFor(() =>
218 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(longTitle),
219 );
220});
221
222it("shows a side chat's full title in its tooltip", async () => {
223 setup();
224 const longTitle = "Side chat question with a title that the side chat row truncates";
225 state.relatedShells = [
226 {
227 id: "side",
228 title: longTitle,
229 lineage: {
230 parentThreadId: threadId,
231 relationshipToParent: "fork",
232 rootThreadId: threadId,
233 sideChat: true,
234 },
235 },
236 ];
237 await using _screen = await render(
238 <ThreadRelationshipsPanel
239 environmentId={environmentId}
240 threadId={threadId}
241 showLineage={false}
242 showSideChats
243 />,
244 );
245
246 await userEvent.hover(page.getByText(longTitle, { exact: true }));
247 await vi.waitFor(() =>
248 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(longTitle),
249 );
250});
251
252it("shows a remote subagent's full title in its tooltip even when its environment is unavailable", async () => {
253 setup();
254 const longTitle = "Investigate a remote subagent issue across linked environments";
255 const remoteEnvId = EnvironmentId.make("remote-env");
256 const remoteThreadId = ThreadId.make("remote-thread");
257 const now = DateTime.makeUnsafe("2026-10-31T20:00:00.000Z");
258 state.projection = {
259 ...(state.projection as Record<string, unknown>),
260 subagents: [
261 {
262 id: NodeId.make("remote-agent"),
263 threadId,
264 runId: null,
265 parentNodeId: NodeId.make("parent"),
266 origin: "app_owned",
267 createdBy: "agent",
268 driver: "codex",
269 providerInstanceId: "provider",
270 providerThreadId: null,
271 childThreadId: null,
272 nativeTaskRef: null,
273 prompt: "Run remote task",
274 title: longTitle,
275 model: null,
276 status: "completed",
277 result: null,
278 startedAt: null,
279 completedAt: null,
280 updatedAt: now,
281 remoteChild: {
282 environmentId: remoteEnvId,
283 threadId: remoteThreadId,
284 label: "Remote VM",
285 },
286 },
287 ],
288 };
289 await using screen = await render(
290 <ThreadRelationshipsPanel
291 environmentId={environmentId}
292 threadId={threadId}
293 showLineage
294 showSideChats={false}
295 />,
296 { router: "/environment/fork" },
297 );
298
299 const row = screen.getByRole("button", { name: new RegExp(longTitle) });
300 await userEvent.hover(row);
301 await vi.waitFor(() =>
302 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(longTitle),
303 );
304 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(
305 "Runs on Remote VM. Connect to it to open the thread there.",
306 );
307
308 await row.click({ force: true });
309 expect(screen.router?.state.location.pathname).toBe("/environment/fork");
310});
311
312it("shows a delegated-from thread's full title in its tooltip even when its environment is unavailable", async () => {
313 setup();
314 const longTitle = "Parent task delegated from another machine that has a long description";
315 state.projection = {
316 ...(state.projection as Record<string, unknown>),
317 thread: {
318 ...(state.projection as { thread: Record<string, unknown> }).thread,
319 delegatedFrom: {
320 environmentId: EnvironmentId.make("parent-env"),
321 threadId: ThreadId.make("parent-thread"),
322 title: longTitle,
323 },
324 },
325 };
326 await using screen = await render(
327 <ThreadRelationshipsPanel
328 environmentId={environmentId}
329 threadId={threadId}
330 showLineage
331 showSideChats={false}
332 />,
333 { router: "/environment/fork" },
334 );
335
336 const row = screen.getByRole("button", { name: new RegExp(longTitle) });
337 await userEvent.hover(row);
338 await vi.waitFor(() =>
339 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(longTitle),
340 );
341 expect(document.querySelector('[data-slot="tooltip-popup"]')?.textContent).toContain(
342 "Delegated from a linked environment. Connect to it to open the parent thread.",
343 );
344
345 await row.click({ force: true });
346 expect(screen.router?.state.location.pathname).toBe("/environment/fork");
347});