Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { ThreadId } from "@t3tools/contracts";
3import * as DateTime from "effect/DateTime";
4
5import {
6 deriveThreadRelationshipGraph,
7 immediateThreadRelationships,
8 orderWebThreadLineageRows,
9 relatedThreadIds,
10 resolveMergeBackTargetThreadId,
11 walkThreadRelationships,
12} from "./threadRelationships.ts";
13
14describe("thread relationships", () => {
15 it("keeps an older activity run visible over a newer cancelled run", () => {
16 const parent = ThreadId.make("thread-parent");
17 const child = ThreadId.make("thread-child");
18 const graph = deriveThreadRelationshipGraph({
19 threads: [
20 {
21 id: child,
22 title: "Active child",
23 activityRunStatus: "running",
24 status: "cancelled",
25 forkedFrom: { type: "run", threadId: parent, runId: "run-parent" },
26 lineage: {
27 rootThreadId: parent,
28 parentThreadId: parent,
29 relationshipToParent: "subagent",
30 },
31 },
32 ] as never,
33 projection: null,
34 });
35
36 expect(graph.edges).toEqual([
37 expect.objectContaining({ targetThreadId: child, status: "running" }),
38 ]);
39 });
40
41 it("keeps missing parents and cycles navigable without recursive traversal", () => {
42 const root = ThreadId.make("thread-root");
43 const child = ThreadId.make("thread-child");
44 const missing = ThreadId.make("thread-missing");
45 const graph = deriveThreadRelationshipGraph({
46 threads: [
47 {
48 id: root,
49 title: "Root",
50 status: "completed",
51 forkedFrom: { type: "run", threadId: child, runId: "run-cycle" },
52 lineage: { rootThreadId: root, parentThreadId: child, relationshipToParent: "fork" },
53 },
54 {
55 id: child,
56 title: "Child",
57 status: "completed",
58 forkedFrom: { type: "run", threadId: missing, runId: "run-missing" },
59 lineage: { rootThreadId: root, parentThreadId: missing, relationshipToParent: "fork" },
60 },
61 ] as never,
62 projection: null,
63 });
64
65 expect(graph.nodes.get(missing)?.missing).toBe(true);
66 expect(relatedThreadIds(graph, root)).toEqual([child]);
67 expect(relatedThreadIds(graph, child)).toEqual([root, missing]);
68 expect(
69 walkThreadRelationships(graph, root).map(({ threadId, depth }) => [threadId, depth]),
70 ).toEqual([
71 [child, 1],
72 [missing, 2],
73 ]);
74 expect(immediateThreadRelationships(graph, root).map(({ threadId }) => threadId)).toEqual([
75 child,
76 ]);
77 });
78
79 it("combines subagent and transfer edges with archived shell state", () => {
80 const parent = ThreadId.make("thread-parent");
81 const child = ThreadId.make("thread-child");
82 const transferTarget = ThreadId.make("thread-transfer");
83 const graph = deriveThreadRelationshipGraph({
84 threads: [
85 {
86 id: parent,
87 title: "Parent",
88 status: "completed",
89 archivedAt: null,
90 forkedFrom: null,
91 lineage: { rootThreadId: parent, parentThreadId: null, relationshipToParent: null },
92 },
93 {
94 id: child,
95 title: "Subagent",
96 status: "completed",
97 archivedAt: "2026-06-24T00:00:00.000Z",
98 forkedFrom: null,
99 lineage: {
100 rootThreadId: parent,
101 parentThreadId: parent,
102 relationshipToParent: "subagent",
103 },
104 },
105 ] as never,
106 projection: {
107 thread: { id: parent },
108 subagents: [{ childThreadId: child, status: "completed" }],
109 contextTransfers: [
110 {
111 sourceThreadId: child,
112 targetThreadId: transferTarget,
113 status: "completed",
114 },
115 ],
116 } as never,
117 });
118
119 expect(graph.nodes.get(child)?.thread?.archivedAt).not.toBeNull();
120 expect(graph.nodes.get(transferTarget)?.missing).toBe(true);
121 expect(graph.edges).toEqual(
122 expect.arrayContaining([
123 expect.objectContaining({
124 sourceThreadId: parent,
125 targetThreadId: child,
126 kind: "subagent",
127 }),
128 expect.objectContaining({
129 sourceThreadId: child,
130 targetThreadId: transferTarget,
131 kind: "transfer",
132 }),
133 ]),
134 );
135 });
136
137 it("keeps the live shell when an archived snapshot contains the same thread id", () => {
138 const parent = ThreadId.make("thread-parent");
139 const staleParent = ThreadId.make("thread-stale-parent");
140 const child = ThreadId.make("thread-child");
141 const liveChild = {
142 id: child,
143 title: "Live child",
144 status: "running",
145 archivedAt: null,
146 forkedFrom: { type: "run", threadId: parent, runId: "run-live" },
147 lineage: {
148 rootThreadId: parent,
149 parentThreadId: parent,
150 relationshipToParent: "fork",
151 },
152 } as const;
153 const staleArchivedChild = {
154 ...liveChild,
155 title: "Stale archived child",
156 status: "completed",
157 archivedAt: "2026-06-24T00:00:00.000Z",
158 forkedFrom: { type: "run", threadId: staleParent, runId: "run-stale" },
159 lineage: {
160 rootThreadId: staleParent,
161 parentThreadId: staleParent,
162 relationshipToParent: "fork",
163 },
164 } as const;
165
166 const graph = deriveThreadRelationshipGraph({
167 threads: [liveChild, staleArchivedChild] as never,
168 projection: null,
169 });
170
171 expect(graph.nodes.get(child)?.thread).toMatchObject({
172 title: "Live child",
173 status: "running",
174 archivedAt: null,
175 });
176 expect(graph.edges).toEqual([
177 expect.objectContaining({
178 sourceThreadId: parent,
179 targetThreadId: child,
180 }),
181 ]);
182 expect(graph.nodes.has(staleParent)).toBe(false);
183 });
184
185 it("resolves merge-back only for forks and prefers the recorded fork source", () => {
186 const source = ThreadId.make("thread-source");
187 const fallbackParent = ThreadId.make("thread-parent");
188 const fork = ThreadId.make("thread-fork");
189
190 expect(
191 resolveMergeBackTargetThreadId({
192 thread: {
193 id: fork,
194 forkedFrom: { type: "run", threadId: source, runId: "run-source" },
195 lineage: {
196 rootThreadId: source,
197 parentThreadId: fallbackParent,
198 relationshipToParent: "fork",
199 },
200 },
201 } as never),
202 ).toBe(source);
203 expect(
204 resolveMergeBackTargetThreadId({
205 thread: {
206 id: fork,
207 forkedFrom: null,
208 lineage: {
209 rootThreadId: source,
210 parentThreadId: fallbackParent,
211 relationshipToParent: "fork",
212 },
213 },
214 } as never),
215 ).toBe(fallbackParent);
216 expect(
217 resolveMergeBackTargetThreadId({
218 thread: {
219 id: fork,
220 forkedFrom: null,
221 lineage: {
222 rootThreadId: source,
223 parentThreadId: fallbackParent,
224 relationshipToParent: "subagent",
225 },
226 },
227 } as never),
228 ).toBeNull();
229 });
230});
231
232const current = ThreadId.make("thread-current");
233
234function forkShell(input: {
235 readonly id: ThreadId;
236 readonly parentThreadId: ThreadId | null;
237 readonly createdAt?: string;
238 readonly updatedAt?: string;
239 readonly status?: string;
240}) {
241 return {
242 id: input.id,
243 title: input.id,
244 status: input.status ?? "completed",
245 archivedAt: null,
246 forkedFrom:
247 input.parentThreadId === null
248 ? null
249 : { type: "run", threadId: input.parentThreadId, runId: `run-${input.id}` },
250 lineage: {
251 rootThreadId: input.parentThreadId ?? input.id,
252 parentThreadId: input.parentThreadId,
253 relationshipToParent: input.parentThreadId === null ? null : "fork",
254 },
255 createdAt: input.createdAt === undefined ? undefined : DateTime.makeUnsafe(input.createdAt),
256 updatedAt: DateTime.makeUnsafe(input.updatedAt ?? "2026-06-20T00:00:00.000Z"),
257 };
258}
259
260function orderedLineageIds(input: {
261 readonly threads: ReadonlyArray<unknown>;
262 readonly mergeTargetThreadId?: ThreadId | null;
263 readonly projection?: unknown;
264}): ReadonlyArray<ThreadId> {
265 const graph = deriveThreadRelationshipGraph({
266 threads: input.threads as never,
267 projection: (input.projection ?? null) as never,
268 });
269 return orderWebThreadLineageRows({
270 graph,
271 rows: immediateThreadRelationships(graph, current),
272 currentThreadId: current,
273 mergeTargetThreadId: input.mergeTargetThreadId ?? null,
274 }).map(({ threadId }) => threadId);
275}
276
277describe("web thread lineage ordering", () => {
278 it("orders sibling forks newest created first", () => {
279 const oldest = ThreadId.make("thread-fork-oldest");
280 const middle = ThreadId.make("thread-fork-middle");
281 const newest = ThreadId.make("thread-fork-newest");
282
283 expect(
284 orderedLineageIds({
285 threads: [
286 forkShell({ id: current, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
287 forkShell({
288 id: oldest,
289 parentThreadId: current,
290 createdAt: "2026-06-02T00:00:00.000Z",
291 }),
292 forkShell({
293 id: middle,
294 parentThreadId: current,
295 createdAt: "2026-06-03T00:00:00.000Z",
296 }),
297 forkShell({
298 id: newest,
299 parentThreadId: current,
300 createdAt: "2026-06-04T00:00:00.000Z",
301 }),
302 ],
303 }),
304 ).toEqual([newest, middle, oldest]);
305 });
306
307 it("does not reorder when related-thread activity arrives", () => {
308 const first = ThreadId.make("thread-fork-a");
309 const second = ThreadId.make("thread-fork-b");
310 const third = ThreadId.make("thread-fork-c");
311 const before = orderedLineageIds({
312 threads: [
313 forkShell({ id: current, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
314 forkShell({ id: first, parentThreadId: current, createdAt: "2026-06-02T00:00:00.000Z" }),
315 forkShell({ id: second, parentThreadId: current, createdAt: "2026-06-03T00:00:00.000Z" }),
316 forkShell({ id: third, parentThreadId: current, createdAt: "2026-06-04T00:00:00.000Z" }),
317 ],
318 });
319
320 // The shell snapshot arrives ordered by `updated_at`, and a client-side
321 // `thread.updated` re-appends the shell it replaces, so both the input
322 // order and the mutable timestamps move under us while the panel is open.
323 const after = orderedLineageIds({
324 threads: [
325 forkShell({
326 id: second,
327 parentThreadId: current,
328 createdAt: "2026-06-03T00:00:00.000Z",
329 updatedAt: "2026-07-30T00:00:00.000Z",
330 status: "running",
331 }),
332 forkShell({ id: third, parentThreadId: current, createdAt: "2026-06-04T00:00:00.000Z" }),
333 forkShell({ id: current, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
334 forkShell({
335 id: first,
336 parentThreadId: current,
337 createdAt: "2026-06-02T00:00:00.000Z",
338 updatedAt: "2026-07-31T00:00:00.000Z",
339 status: "failed",
340 }),
341 ],
342 });
343
344 expect(before).toEqual([third, second, first]);
345 expect(after).toEqual(before);
346 });
347
348 it("pins the parent first and a distinct merge-back target second", () => {
349 // The panel keeps its two action rows in place regardless of creation time:
350 // parent first, then a merge-back target that is not the parent. The two
351 // diverge while a shell update and its projection disagree about the fork
352 // source, which is exactly when a moving row would misfire a merge.
353 const parent = ThreadId.make("thread-parent");
354 const mergeTarget = ThreadId.make("thread-merge-target");
355 const newestFork = ThreadId.make("thread-newest-fork");
356
357 expect(
358 orderedLineageIds({
359 threads: [
360 forkShell({ id: parent, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
361 forkShell({
362 id: current,
363 parentThreadId: parent,
364 createdAt: "2026-06-02T00:00:00.000Z",
365 }),
366 forkShell({
367 id: mergeTarget,
368 parentThreadId: current,
369 createdAt: "2026-06-03T00:00:00.000Z",
370 }),
371 forkShell({
372 id: newestFork,
373 parentThreadId: current,
374 createdAt: "2026-06-09T00:00:00.000Z",
375 }),
376 ],
377 mergeTargetThreadId: mergeTarget,
378 }),
379 ).toEqual([parent, mergeTarget, newestFork]);
380 });
381
382 it("sinks missing nodes and shells without a decoded createdAt", () => {
383 const missingTransfer = ThreadId.make("thread-missing-transfer");
384 const undated = ThreadId.make("thread-undated-fork");
385 const dated = ThreadId.make("thread-dated-fork");
386
387 expect(
388 orderedLineageIds({
389 threads: [
390 forkShell({ id: current, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
391 forkShell({ id: undated, parentThreadId: current }),
392 forkShell({ id: dated, parentThreadId: current, createdAt: "2026-06-02T00:00:00.000Z" }),
393 ],
394 projection: {
395 thread: { id: current },
396 subagents: [],
397 contextTransfers: [
398 { sourceThreadId: current, targetThreadId: missingTransfer, status: "completed" },
399 ],
400 },
401 }),
402 ).toEqual([dated, missingTransfer, undated]);
403 });
404
405 it("breaks equal creation times by thread id", () => {
406 const first = ThreadId.make("thread-fork-a");
407 const second = ThreadId.make("thread-fork-b");
408 const third = ThreadId.make("thread-fork-c");
409
410 expect(
411 orderedLineageIds({
412 threads: [
413 forkShell({ id: current, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
414 forkShell({ id: third, parentThreadId: current, createdAt: "2026-06-02T00:00:00.000Z" }),
415 forkShell({ id: first, parentThreadId: current, createdAt: "2026-06-02T00:00:00.000Z" }),
416 forkShell({ id: second, parentThreadId: current, createdAt: "2026-06-02T00:00:00.000Z" }),
417 ],
418 }),
419 ).toEqual([first, second, third]);
420 });
421});