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.each([
191 ["running", "running"],
192 [null, "completed"],
193 ])(
194 "shows a subagent whose child activity is %s as %s after its delegated task settled",
195 (childActivity, expected) => {
196 const parent = ThreadId.make("thread-parent");
197 const child = ThreadId.make("thread-child");
198 const graph = deriveThreadRelationshipGraph({
199 threads: [
200 { id: parent, status: "completed", forkedFrom: null, lineage: { parentThreadId: null } },
201 {
202 id: child,
203 status: childActivity ?? "completed",
204 activityRunStatus: childActivity,
205 forkedFrom: null,
206 lineage: { parentThreadId: parent, relationshipToParent: "subagent" },
207 },
208 ] as never,
209 projection: {
210 thread: { id: parent },
211 subagents: [{ childThreadId: child, status: "completed" }],
212 contextTransfers: [],
213 } as never,
214 });
215
216 const row = immediateThreadRelationships(graph, parent)[0]!;
217 expect(threadRelationshipRowStatus(graph, row)).toBe(expected);
218 },
219 );
220
221 it("keeps the live shell when an archived snapshot contains the same thread id", () => {
222 const parent = ThreadId.make("thread-parent");
223 const staleParent = ThreadId.make("thread-stale-parent");
224 const child = ThreadId.make("thread-child");
225 const liveChild = {
226 id: child,
227 title: "Live child",
228 status: "running",
229 archivedAt: null,
230 forkedFrom: { type: "run", threadId: parent, runId: "run-live" },
231 lineage: {
232 rootThreadId: parent,
233 parentThreadId: parent,
234 relationshipToParent: "fork",
235 },
236 } as const;
237 const staleArchivedChild = {
238 ...liveChild,
239 title: "Stale archived child",
240 status: "completed",
241 archivedAt: "2026-06-24T00:00:00.000Z",
242 forkedFrom: { type: "run", threadId: staleParent, runId: "run-stale" },
243 lineage: {
244 rootThreadId: staleParent,
245 parentThreadId: staleParent,
246 relationshipToParent: "fork",
247 },
248 } as const;
249
250 const graph = deriveThreadRelationshipGraph({
251 threads: [liveChild, staleArchivedChild] as never,
252 projection: null,
253 });
254
255 expect(graph.nodes.get(child)?.thread).toMatchObject({
256 title: "Live child",
257 status: "running",
258 archivedAt: null,
259 });
260 expect(graph.edges).toEqual([
261 expect.objectContaining({
262 sourceThreadId: parent,
263 targetThreadId: child,
264 }),
265 ]);
266 expect(graph.nodes.has(staleParent)).toBe(false);
267 });
268
269 it("resolves merge-back only for forks and prefers the recorded fork source", () => {
270 const source = ThreadId.make("thread-source");
271 const fallbackParent = ThreadId.make("thread-parent");
272 const fork = ThreadId.make("thread-fork");
273
274 expect(
275 resolveMergeBackTargetThreadId({
276 thread: {
277 id: fork,
278 forkedFrom: { type: "run", threadId: source, runId: "run-source" },
279 lineage: {
280 rootThreadId: source,
281 parentThreadId: fallbackParent,
282 relationshipToParent: "fork",
283 },
284 },
285 } as never),
286 ).toBe(source);
287 expect(
288 resolveMergeBackTargetThreadId({
289 thread: {
290 id: fork,
291 forkedFrom: null,
292 lineage: {
293 rootThreadId: source,
294 parentThreadId: fallbackParent,
295 relationshipToParent: "fork",
296 },
297 },
298 } as never),
299 ).toBe(fallbackParent);
300 expect(
301 resolveMergeBackTargetThreadId({
302 thread: {
303 id: fork,
304 forkedFrom: null,
305 lineage: {
306 rootThreadId: source,
307 parentThreadId: fallbackParent,
308 relationshipToParent: "subagent",
309 },
310 },
311 } as never),
312 ).toBeNull();
313 });
314});
315
316const current = ThreadId.make("thread-current");
317
318function forkShell(input: {
319 readonly id: ThreadId;
320 readonly parentThreadId: ThreadId | null;
321 readonly createdAt?: string;
322 readonly updatedAt?: string;
323 readonly status?: string;
324}) {
325 return {
326 id: input.id,
327 title: input.id,
328 status: input.status ?? "completed",
329 archivedAt: null,
330 forkedFrom:
331 input.parentThreadId === null
332 ? null
333 : { type: "run", threadId: input.parentThreadId, runId: `run-${input.id}` },
334 lineage: {
335 rootThreadId: input.parentThreadId ?? input.id,
336 parentThreadId: input.parentThreadId,
337 relationshipToParent: input.parentThreadId === null ? null : "fork",
338 },
339 createdAt: input.createdAt === undefined ? undefined : DateTime.makeUnsafe(input.createdAt),
340 updatedAt: DateTime.makeUnsafe(input.updatedAt ?? "2026-06-20T00:00:00.000Z"),
341 };
342}
343
344function orderedLineageIds(input: {
345 readonly threads: ReadonlyArray<unknown>;
346 readonly mergeTargetThreadId?: ThreadId | null;
347 readonly projection?: unknown;
348}): ReadonlyArray<ThreadId> {
349 const graph = deriveThreadRelationshipGraph({
350 threads: input.threads as never,
351 projection: (input.projection ?? null) as never,
352 });
353 return orderWebThreadLineageRows({
354 graph,
355 rows: immediateThreadRelationships(graph, current),
356 currentThreadId: current,
357 mergeTargetThreadId: input.mergeTargetThreadId ?? null,
358 }).map(({ threadId }) => threadId);
359}
360
361describe("web thread lineage ordering", () => {
362 it("orders sibling forks newest created first", () => {
363 const oldest = ThreadId.make("thread-fork-oldest");
364 const middle = ThreadId.make("thread-fork-middle");
365 const newest = ThreadId.make("thread-fork-newest");
366
367 expect(
368 orderedLineageIds({
369 threads: [
370 forkShell({ id: current, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
371 forkShell({
372 id: oldest,
373 parentThreadId: current,
374 createdAt: "2026-06-02T00:00:00.000Z",
375 }),
376 forkShell({
377 id: middle,
378 parentThreadId: current,
379 createdAt: "2026-06-03T00:00:00.000Z",
380 }),
381 forkShell({
382 id: newest,
383 parentThreadId: current,
384 createdAt: "2026-06-04T00:00:00.000Z",
385 }),
386 ],
387 }),
388 ).toEqual([newest, middle, oldest]);
389 });
390
391 it("does not reorder when related-thread activity arrives", () => {
392 const first = ThreadId.make("thread-fork-a");
393 const second = ThreadId.make("thread-fork-b");
394 const third = ThreadId.make("thread-fork-c");
395 const before = orderedLineageIds({
396 threads: [
397 forkShell({ id: current, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
398 forkShell({ id: first, parentThreadId: current, createdAt: "2026-06-02T00:00:00.000Z" }),
399 forkShell({ id: second, parentThreadId: current, createdAt: "2026-06-03T00:00:00.000Z" }),
400 forkShell({ id: third, parentThreadId: current, createdAt: "2026-06-04T00:00:00.000Z" }),
401 ],
402 });
403
404 // The shell snapshot arrives ordered by `updated_at`, and a client-side
405 // `thread.updated` re-appends the shell it replaces, so both the input
406 // order and the mutable timestamps move under us while the panel is open.
407 const after = orderedLineageIds({
408 threads: [
409 forkShell({
410 id: second,
411 parentThreadId: current,
412 createdAt: "2026-06-03T00:00:00.000Z",
413 updatedAt: "2026-07-30T00:00:00.000Z",
414 status: "running",
415 }),
416 forkShell({ id: third, parentThreadId: current, createdAt: "2026-06-04T00:00:00.000Z" }),
417 forkShell({ id: current, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
418 forkShell({
419 id: first,
420 parentThreadId: current,
421 createdAt: "2026-06-02T00:00:00.000Z",
422 updatedAt: "2026-07-31T00:00:00.000Z",
423 status: "failed",
424 }),
425 ],
426 });
427
428 expect(before).toEqual([third, second, first]);
429 expect(after).toEqual(before);
430 });
431
432 it("pins the parent first and a distinct merge-back target second", () => {
433 // The panel keeps its two action rows in place regardless of creation time:
434 // parent first, then a merge-back target that is not the parent. The two
435 // diverge while a shell update and its projection disagree about the fork
436 // source, which is exactly when a moving row would misfire a merge.
437 const parent = ThreadId.make("thread-parent");
438 const mergeTarget = ThreadId.make("thread-merge-target");
439 const newestFork = ThreadId.make("thread-newest-fork");
440
441 expect(
442 orderedLineageIds({
443 threads: [
444 forkShell({ id: parent, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
445 forkShell({
446 id: current,
447 parentThreadId: parent,
448 createdAt: "2026-06-02T00:00:00.000Z",
449 }),
450 forkShell({
451 id: mergeTarget,
452 parentThreadId: current,
453 createdAt: "2026-06-03T00:00:00.000Z",
454 }),
455 forkShell({
456 id: newestFork,
457 parentThreadId: current,
458 createdAt: "2026-06-09T00:00:00.000Z",
459 }),
460 ],
461 mergeTargetThreadId: mergeTarget,
462 }),
463 ).toEqual([parent, mergeTarget, newestFork]);
464 });
465
466 it("sinks missing nodes and shells without a decoded createdAt", () => {
467 const missingTransfer = ThreadId.make("thread-missing-transfer");
468 const undated = ThreadId.make("thread-undated-fork");
469 const dated = ThreadId.make("thread-dated-fork");
470
471 expect(
472 orderedLineageIds({
473 threads: [
474 forkShell({ id: current, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
475 forkShell({ id: undated, parentThreadId: current }),
476 forkShell({ id: dated, parentThreadId: current, createdAt: "2026-06-02T00:00:00.000Z" }),
477 ],
478 projection: {
479 thread: { id: current },
480 subagents: [],
481 contextTransfers: [
482 { sourceThreadId: current, targetThreadId: missingTransfer, status: "completed" },
483 ],
484 },
485 }),
486 ).toEqual([dated, missingTransfer, undated]);
487 });
488
489 it("breaks equal creation times by thread id", () => {
490 const first = ThreadId.make("thread-fork-a");
491 const second = ThreadId.make("thread-fork-b");
492 const third = ThreadId.make("thread-fork-c");
493
494 expect(
495 orderedLineageIds({
496 threads: [
497 forkShell({ id: current, parentThreadId: null, createdAt: "2026-06-01T00:00:00.000Z" }),
498 forkShell({ id: third, parentThreadId: current, createdAt: "2026-06-02T00:00:00.000Z" }),
499 forkShell({ id: first, parentThreadId: current, createdAt: "2026-06-02T00:00:00.000Z" }),
500 forkShell({ id: second, parentThreadId: current, createdAt: "2026-06-02T00:00:00.000Z" }),
501 ],
502 }),
503 ).toEqual([first, second, third]);
504 });
505});