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