Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { ThreadId } from "@t3tools/contracts";
3
4import {
5 deriveThreadRelationshipGraph,
6 immediateThreadRelationships,
7 relatedThreadIds,
8 resolveMergeBackTargetThreadId,
9 walkThreadRelationships,
10} from "./threadRelationships.ts";
11
12describe("thread relationships", () => {
13 it("keeps missing parents and cycles navigable without recursive traversal", () => {
14 const root = ThreadId.make("thread-root");
15 const child = ThreadId.make("thread-child");
16 const missing = ThreadId.make("thread-missing");
17 const graph = deriveThreadRelationshipGraph({
18 threads: [
19 {
20 id: root,
21 title: "Root",
22 status: "completed",
23 forkedFrom: { type: "run", threadId: child, runId: "run-cycle" },
24 lineage: { rootThreadId: root, parentThreadId: child, relationshipToParent: "fork" },
25 },
26 {
27 id: child,
28 title: "Child",
29 status: "completed",
30 forkedFrom: { type: "run", threadId: missing, runId: "run-missing" },
31 lineage: { rootThreadId: root, parentThreadId: missing, relationshipToParent: "fork" },
32 },
33 ] as never,
34 projection: null,
35 });
36
37 expect(graph.nodes.get(missing)?.missing).toBe(true);
38 expect(relatedThreadIds(graph, root)).toEqual([child]);
39 expect(relatedThreadIds(graph, child)).toEqual([root, missing]);
40 expect(
41 walkThreadRelationships(graph, root).map(({ threadId, depth }) => [threadId, depth]),
42 ).toEqual([
43 [child, 1],
44 [missing, 2],
45 ]);
46 expect(immediateThreadRelationships(graph, root).map(({ threadId }) => threadId)).toEqual([
47 child,
48 ]);
49 });
50
51 it("combines subagent and transfer edges with archived shell state", () => {
52 const parent = ThreadId.make("thread-parent");
53 const child = ThreadId.make("thread-child");
54 const transferTarget = ThreadId.make("thread-transfer");
55 const graph = deriveThreadRelationshipGraph({
56 threads: [
57 {
58 id: parent,
59 title: "Parent",
60 status: "completed",
61 archivedAt: null,
62 forkedFrom: null,
63 lineage: { rootThreadId: parent, parentThreadId: null, relationshipToParent: null },
64 },
65 {
66 id: child,
67 title: "Subagent",
68 status: "completed",
69 archivedAt: "2026-06-24T00:00:00.000Z",
70 forkedFrom: null,
71 lineage: {
72 rootThreadId: parent,
73 parentThreadId: parent,
74 relationshipToParent: "subagent",
75 },
76 },
77 ] as never,
78 projection: {
79 thread: { id: parent },
80 subagents: [{ childThreadId: child, status: "completed" }],
81 contextTransfers: [
82 {
83 sourceThreadId: child,
84 targetThreadId: transferTarget,
85 status: "completed",
86 },
87 ],
88 } as never,
89 });
90
91 expect(graph.nodes.get(child)?.thread?.archivedAt).not.toBeNull();
92 expect(graph.nodes.get(transferTarget)?.missing).toBe(true);
93 expect(graph.edges).toEqual(
94 expect.arrayContaining([
95 expect.objectContaining({
96 sourceThreadId: parent,
97 targetThreadId: child,
98 kind: "subagent",
99 }),
100 expect.objectContaining({
101 sourceThreadId: child,
102 targetThreadId: transferTarget,
103 kind: "transfer",
104 }),
105 ]),
106 );
107 });
108
109 it("keeps the live shell when an archived snapshot contains the same thread id", () => {
110 const parent = ThreadId.make("thread-parent");
111 const staleParent = ThreadId.make("thread-stale-parent");
112 const child = ThreadId.make("thread-child");
113 const liveChild = {
114 id: child,
115 title: "Live child",
116 status: "running",
117 archivedAt: null,
118 forkedFrom: { type: "run", threadId: parent, runId: "run-live" },
119 lineage: {
120 rootThreadId: parent,
121 parentThreadId: parent,
122 relationshipToParent: "fork",
123 },
124 } as const;
125 const staleArchivedChild = {
126 ...liveChild,
127 title: "Stale archived child",
128 status: "completed",
129 archivedAt: "2026-06-24T00:00:00.000Z",
130 forkedFrom: { type: "run", threadId: staleParent, runId: "run-stale" },
131 lineage: {
132 rootThreadId: staleParent,
133 parentThreadId: staleParent,
134 relationshipToParent: "fork",
135 },
136 } as const;
137
138 const graph = deriveThreadRelationshipGraph({
139 threads: [liveChild, staleArchivedChild] as never,
140 projection: null,
141 });
142
143 expect(graph.nodes.get(child)?.thread).toMatchObject({
144 title: "Live child",
145 status: "running",
146 archivedAt: null,
147 });
148 expect(graph.edges).toEqual([
149 expect.objectContaining({
150 sourceThreadId: parent,
151 targetThreadId: child,
152 }),
153 ]);
154 expect(graph.nodes.has(staleParent)).toBe(false);
155 });
156
157 it("resolves merge-back only for forks and prefers the recorded fork source", () => {
158 const source = ThreadId.make("thread-source");
159 const fallbackParent = ThreadId.make("thread-parent");
160 const fork = ThreadId.make("thread-fork");
161
162 expect(
163 resolveMergeBackTargetThreadId({
164 thread: {
165 id: fork,
166 forkedFrom: { type: "run", threadId: source, runId: "run-source" },
167 lineage: {
168 rootThreadId: source,
169 parentThreadId: fallbackParent,
170 relationshipToParent: "fork",
171 },
172 },
173 } as never),
174 ).toBe(source);
175 expect(
176 resolveMergeBackTargetThreadId({
177 thread: {
178 id: fork,
179 forkedFrom: null,
180 lineage: {
181 rootThreadId: source,
182 parentThreadId: fallbackParent,
183 relationshipToParent: "fork",
184 },
185 },
186 } as never),
187 ).toBe(fallbackParent);
188 expect(
189 resolveMergeBackTargetThreadId({
190 thread: {
191 id: fork,
192 forkedFrom: null,
193 lineage: {
194 rootThreadId: source,
195 parentThreadId: fallbackParent,
196 relationshipToParent: "subagent",
197 },
198 },
199 } as never),
200 ).toBeNull();
201 });
202});