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