Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadRelationships.test.ts

417 lines13.2 KB

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