Open History to see the last commit
1import type {
2 OrchestrationV2ThreadProjection,
3 OrchestrationV2ThreadShell,
4 ThreadId,
5} from "@t3tools/contracts";
6
7export type ThreadRelationshipKind = "parent" | "fork" | "subagent" | "transfer";
8
9export interface ThreadRelationshipNode {
10 readonly threadId: ThreadId;
11 readonly thread: OrchestrationV2ThreadShell | null;
12 readonly missing: boolean;
13}
14
15export interface ThreadRelationshipEdge {
16 readonly sourceThreadId: ThreadId;
17 readonly targetThreadId: ThreadId;
18 readonly kind: ThreadRelationshipKind;
19 readonly status: string | null;
20}
21
22export interface ThreadRelationshipGraph {
23 readonly nodes: ReadonlyMap<ThreadId, ThreadRelationshipNode>;
24 readonly edges: ReadonlyArray<ThreadRelationshipEdge>;
25}
26
27export interface ThreadRelationshipWalkRow {
28 readonly threadId: ThreadId;
29 readonly fromThreadId: ThreadId;
30 readonly depth: number;
31 readonly edge: ThreadRelationshipEdge;
32}
33
34export function resolveMergeBackTargetThreadId(
35 projection: Pick<OrchestrationV2ThreadProjection, "thread"> | null,
36): ThreadId | null {
37 if (projection?.thread.lineage.relationshipToParent !== "fork") return null;
38 return projection.thread.forkedFrom?.type === "run"
39 ? projection.thread.forkedFrom.threadId
40 : projection.thread.lineage.parentThreadId;
41}
42
43function edgeKey(edge: ThreadRelationshipEdge): string {
44 return `${edge.sourceThreadId}\u001f${edge.targetThreadId}\u001f${edge.kind}`;
45}
46
47export function deriveThreadRelationshipGraph(input: {
48 readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
49 readonly projection: OrchestrationV2ThreadProjection | null;
50}): ThreadRelationshipGraph {
51 const nodes = new Map<ThreadId, ThreadRelationshipNode>(
52 input.threads.map(
53 (thread) => [thread.id, { threadId: thread.id, thread, missing: false }] as const,
54 ),
55 );
56 const edgesByKey = new Map<string, ThreadRelationshipEdge>();
57 const ensureNode = (threadId: ThreadId) => {
58 if (!nodes.has(threadId)) {
59 nodes.set(threadId, { threadId, thread: null, missing: true });
60 }
61 };
62 const addEdge = (edge: ThreadRelationshipEdge) => {
63 ensureNode(edge.sourceThreadId);
64 ensureNode(edge.targetThreadId);
65 edgesByKey.set(edgeKey(edge), edge);
66 };
67
68 for (const thread of input.threads) {
69 const parentThreadId =
70 thread.forkedFrom?.type === "run"
71 ? thread.forkedFrom.threadId
72 : thread.lineage.parentThreadId;
73 if (parentThreadId === null) continue;
74 addEdge({
75 sourceThreadId: parentThreadId,
76 targetThreadId: thread.id,
77 kind: thread.lineage.relationshipToParent === "subagent" ? "subagent" : "fork",
78 status: thread.status,
79 });
80 }
81
82 if (input.projection !== null) {
83 const ownerThreadId = input.projection.thread.id;
84 for (const subagent of input.projection.subagents) {
85 if (subagent.childThreadId === null) continue;
86 addEdge({
87 sourceThreadId: ownerThreadId,
88 targetThreadId: subagent.childThreadId,
89 kind: "subagent",
90 status: subagent.status,
91 });
92 }
93 for (const transfer of input.projection.contextTransfers) {
94 if (transfer.sourceThreadId === transfer.targetThreadId) continue;
95 addEdge({
96 sourceThreadId: transfer.sourceThreadId,
97 targetThreadId: transfer.targetThreadId,
98 kind: "transfer",
99 status: transfer.status,
100 });
101 }
102 }
103
104 return { nodes, edges: [...edgesByKey.values()] };
105}
106
107export function relatedThreadIds(
108 graph: ThreadRelationshipGraph,
109 threadId: ThreadId,
110): ReadonlyArray<ThreadId> {
111 const ids = new Set<ThreadId>();
112 for (const edge of graph.edges) {
113 if (edge.sourceThreadId === threadId) ids.add(edge.targetThreadId);
114 if (edge.targetThreadId === threadId) ids.add(edge.sourceThreadId);
115 }
116 return [...ids];
117}
118
119export function walkThreadRelationships(
120 graph: ThreadRelationshipGraph,
121 threadId: ThreadId,
122): ReadonlyArray<ThreadRelationshipWalkRow> {
123 const visited = new Set<ThreadId>([threadId]);
124 const pending: Array<{ readonly threadId: ThreadId; readonly depth: number }> = [
125 { threadId, depth: 0 },
126 ];
127 const rows: ThreadRelationshipWalkRow[] = [];
128
129 for (let index = 0; index < pending.length; index += 1) {
130 const current = pending[index];
131 if (current === undefined) continue;
132 for (const edge of graph.edges) {
133 const relatedId =
134 edge.sourceThreadId === current.threadId
135 ? edge.targetThreadId
136 : edge.targetThreadId === current.threadId
137 ? edge.sourceThreadId
138 : null;
139 if (relatedId === null || visited.has(relatedId)) continue;
140 visited.add(relatedId);
141 const depth = current.depth + 1;
142 rows.push({ threadId: relatedId, fromThreadId: current.threadId, depth, edge });
143 pending.push({ threadId: relatedId, depth });
144 }
145 }
146
147 return rows;
148}
149
150export function immediateThreadRelationships(
151 graph: ThreadRelationshipGraph,
152 threadId: ThreadId,
153): ReadonlyArray<ThreadRelationshipWalkRow> {
154 const visited = new Set<ThreadId>();
155 const rows: ThreadRelationshipWalkRow[] = [];
156
157 for (const edge of graph.edges) {
158 const relatedId =
159 edge.sourceThreadId === threadId
160 ? edge.targetThreadId
161 : edge.targetThreadId === threadId
162 ? edge.sourceThreadId
163 : null;
164 if (relatedId === null || visited.has(relatedId)) continue;
165 visited.add(relatedId);
166 rows.push({ threadId: relatedId, fromThreadId: threadId, depth: 1, edge });
167 }
168
169 return rows;
170}