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

threadRelationships.ts

178 lines5.7 KB

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;
14​
15export interface ThreadRelationshipEdge {
16 readonly sourceThreadId: ThreadId;
17 readonly targetThreadId: ThreadId;
18 readonly kind: ThreadRelationshipKind;
19 readonly status: string | null;
21​
22export interface ThreadRelationshipGraph {
23 readonly nodes: ReadonlyMap<ThreadId, ThreadRelationshipNode>;
24 readonly edges: ReadonlyArray<ThreadRelationshipEdge>;
26​
27export interface ThreadRelationshipWalkRow {
28 readonly threadId: ThreadId;
29 readonly fromThreadId: ThreadId;
30 readonly depth: number;
31 readonly edge: ThreadRelationshipEdge;
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;
42​
43function edgeKey(edge: ThreadRelationshipEdge): string {
44 return `${edge.sourceThreadId}\u001f${edge.targetThreadId}\u001f${edge.kind}`;
46​
47export function deriveThreadRelationshipGraph(input: {
48 readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
49 readonly projection: OrchestrationV2ThreadProjection | null;
50}): ThreadRelationshipGraph {
51 const threadsById = new Map<ThreadId, OrchestrationV2ThreadShell>();
52 for (const thread of input.threads) {
53 // Callers order shells from most to least authoritative. In particular,
54 // live shells precede archived snapshots, which may still contain a stale
55 // copy during archive refresh.
56 if (!threadsById.has(thread.id)) {
57 threadsById.set(thread.id, thread);
58 }
59 }
60 const threads = [...threadsById.values()];
61 const nodes = new Map<ThreadId, ThreadRelationshipNode>(
62 threads.map((thread) => [thread.id, { threadId: thread.id, thread, missing: false }]),
63 );
64 const edgesByKey = new Map<string, ThreadRelationshipEdge>();
65 const ensureNode = (threadId: ThreadId) => {
66 if (!nodes.has(threadId)) {
67 nodes.set(threadId, { threadId, thread: null, missing: true });
68 }
69 };
70 const addEdge = (edge: ThreadRelationshipEdge) => {
71 ensureNode(edge.sourceThreadId);
72 ensureNode(edge.targetThreadId);
73 edgesByKey.set(edgeKey(edge), edge);
74 };
75​
76 for (const thread of threads) {
77 const parentThreadId =
78 thread.forkedFrom?.type === "run"
79 ? thread.forkedFrom.threadId
80 : thread.lineage.parentThreadId;
81 if (parentThreadId === null) continue;
82 addEdge({
83 sourceThreadId: parentThreadId,
84 targetThreadId: thread.id,
85 kind: thread.lineage.relationshipToParent === "subagent" ? "subagent" : "fork",
86 status: thread.status,
87 });
88 }
89​
90 if (input.projection !== null) {
91 const ownerThreadId = input.projection.thread.id;
92 for (const subagent of input.projection.subagents) {
93 if (subagent.childThreadId === null) continue;
94 addEdge({
95 sourceThreadId: ownerThreadId,
96 targetThreadId: subagent.childThreadId,
97 kind: "subagent",
98 status: subagent.status,
99 });
100 }
101 for (const transfer of input.projection.contextTransfers) {
102 if (transfer.sourceThreadId === transfer.targetThreadId) continue;
103 addEdge({
104 sourceThreadId: transfer.sourceThreadId,
105 targetThreadId: transfer.targetThreadId,
106 kind: "transfer",
107 status: transfer.status,
108 });
109 }
110 }
111​
112 return { nodes, edges: [...edgesByKey.values()] };
114​
115export function relatedThreadIds(
116 graph: ThreadRelationshipGraph,
117 threadId: ThreadId,
118): ReadonlyArray<ThreadId> {
119 const ids = new Set<ThreadId>();
120 for (const edge of graph.edges) {
121 if (edge.sourceThreadId === threadId) ids.add(edge.targetThreadId);
122 if (edge.targetThreadId === threadId) ids.add(edge.sourceThreadId);
123 }
124 return [...ids];
126​
127export function walkThreadRelationships(
128 graph: ThreadRelationshipGraph,
129 threadId: ThreadId,
130): ReadonlyArray<ThreadRelationshipWalkRow> {
131 const visited = new Set<ThreadId>([threadId]);
132 const pending: Array<{ readonly threadId: ThreadId; readonly depth: number }> = [
133 { threadId, depth: 0 },
134 ];
135 const rows: ThreadRelationshipWalkRow[] = [];
136​
137 for (let index = 0; index < pending.length; index += 1) {
138 const current = pending[index];
139 if (current === undefined) continue;
140 for (const edge of graph.edges) {
141 const relatedId =
142 edge.sourceThreadId === current.threadId
143 ? edge.targetThreadId
144 : edge.targetThreadId === current.threadId
145 ? edge.sourceThreadId
146 : null;
147 if (relatedId === null || visited.has(relatedId)) continue;
148 visited.add(relatedId);
149 const depth = current.depth + 1;
150 rows.push({ threadId: relatedId, fromThreadId: current.threadId, depth, edge });
151 pending.push({ threadId: relatedId, depth });
152 }
153 }
154​
155 return rows;
157​
158export function immediateThreadRelationships(
159 graph: ThreadRelationshipGraph,
160 threadId: ThreadId,
161): ReadonlyArray<ThreadRelationshipWalkRow> {
162 const visited = new Set<ThreadId>();
163 const rows: ThreadRelationshipWalkRow[] = [];
164​
165 for (const edge of graph.edges) {
166 const relatedId =
167 edge.sourceThreadId === threadId
168 ? edge.targetThreadId
169 : edge.targetThreadId === threadId
170 ? edge.sourceThreadId
171 : null;
172 if (relatedId === null || visited.has(relatedId)) continue;
173 visited.add(relatedId);
174 rows.push({ threadId: relatedId, fromThreadId: threadId, depth: 1, edge });
175 }
176​
177 return rows;