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