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