Open History to see the last commit
1import {
2 OrchestrationProjectShell,
3 OrchestrationV2ThreadShell,
4 RepositoryIdentity,
5 ThreadPullRequestLink,
6 type OrchestrationV2ShellSnapshot,
7 type OrchestrationV2ShellStreamItem,
8} from "@t3tools/contracts";
9import * as Schema from "effect/Schema";
10
11const sameThreadShell = Schema.toEquivalence(OrchestrationV2ThreadShell);
12const sameProjectShell = Schema.toEquivalence(OrchestrationProjectShell);
13const sameLinks = Schema.toEquivalence(Schema.UndefinedOr(Schema.Array(ThreadPullRequestLink)));
14const sameRepositoryIdentity = Schema.toEquivalence(
15 Schema.UndefinedOr(Schema.NullOr(RepositoryIdentity)),
16);
17
18export function sameThreadPullRequests(
19 left: OrchestrationV2ThreadShell["pullRequests"],
20 right: OrchestrationV2ThreadShell["pullRequests"],
21): boolean {
22 return left === right || sameLinks(left, right);
23}
24
25/**
26 * Links are compared apart from the rest of the row because a deferred snapshot arrives without
27 * them; such a row keeps its links until they fill in.
28 */
29export function reuseUnchangedThreadShells(
30 previous: OrchestrationV2ShellSnapshot | null,
31 next: OrchestrationV2ShellSnapshot,
32): OrchestrationV2ShellSnapshot {
33 if (previous === null || previous.threads.length === 0) return next;
34 const previousById = new Map(previous.threads.map((thread) => [thread.id, thread] as const));
35 let changed = false;
36 const threads = next.threads.map((thread) => {
37 const prior = previousById.get(thread.id);
38 if (prior === undefined) return thread;
39 const candidate =
40 thread.pullRequests === undefined && prior.pullRequests !== undefined
41 ? { ...thread, pullRequests: prior.pullRequests }
42 : thread;
43 const retained = sameThreadShell(prior, candidate) ? prior : candidate;
44 if (retained !== thread) changed = true;
45 return retained;
46 });
47 return changed ? { ...next, threads } : next;
48}
49
50function upsertById<T extends { readonly id: unknown }>(
51 items: ReadonlyArray<T>,
52 item: T,
53): ReadonlyArray<T> {
54 const index = items.findIndex((candidate) => candidate.id === item.id);
55 if (index === -1) return [...items, item];
56 return items.map((candidate, candidateIndex) => (candidateIndex === index ? item : candidate));
57}
58
59function retainRepositoryIdentity(
60 previous: OrchestrationProjectShell | undefined,
61 next: OrchestrationProjectShell,
62): OrchestrationProjectShell {
63 if (
64 next.repositoryIdentity == null &&
65 previous?.repositoryIdentity != null &&
66 previous.workspaceRoot === next.workspaceRoot
67 ) {
68 return { ...next, repositoryIdentity: previous.repositoryIdentity };
69 }
70 return next;
71}
72
73export interface MergeShellSnapshotOptions {
74 /**
75 * Metadata-only enrichment refresh: structure and sequence never change;
76 * listed roots accept identity exactly (including null).
77 * Omit this options object for authoritative HTTP/initial WebSocket snapshots.
78 */
79 readonly resolvedRepositoryIdentityRoots: ReadonlyArray<string>;
80}
81
82export function mergeShellSnapshotProjects(
83 previous: OrchestrationV2ShellSnapshot | null | undefined,
84 next: OrchestrationV2ShellSnapshot,
85 options?: MergeShellSnapshotOptions,
86): OrchestrationV2ShellSnapshot {
87 if (previous === null || previous === undefined) {
88 return next;
89 }
90
91 const isEnrichment = options !== undefined;
92 const resolvedRootSet = isEnrichment ? new Set(options.resolvedRepositoryIdentityRoots) : null;
93
94 if (isEnrichment) {
95 const nextById = new Map(next.projects.map((project) => [project.id, project] as const));
96 return {
97 ...previous,
98 projects: previous.projects.map((project) => {
99 const candidate = nextById.get(project.id);
100 if (candidate === undefined || candidate.workspaceRoot !== project.workspaceRoot) {
101 return project;
102 }
103 if (resolvedRootSet?.has(project.workspaceRoot) === true) {
104 // Refreshes re-resolve every root; an unchanged identity keeps the project's identity
105 // so every subscriber (each pull request row, the sidebar) skips re-rendering.
106 if (sameRepositoryIdentity(project.repositoryIdentity, candidate.repositoryIdentity)) {
107 return project;
108 }
109 return { ...project, repositoryIdentity: candidate.repositoryIdentity };
110 }
111 if (project.repositoryIdentity == null && candidate.repositoryIdentity != null) {
112 return { ...project, repositoryIdentity: candidate.repositoryIdentity };
113 }
114 return project;
115 }),
116 };
117 }
118
119 const previousById = new Map(previous.projects.map((project) => [project.id, project] as const));
120 return {
121 ...next,
122 projects: next.projects.map((project) => {
123 const prior = previousById.get(project.id);
124 if (resolvedRootSet?.has(project.workspaceRoot) === true) {
125 return project;
126 }
127 const merged = retainRepositoryIdentity(prior, project);
128 // Preserve identity across cached-to-live handoffs.
129 return prior !== undefined && sameProjectShell(prior, merged) ? prior : merged;
130 }),
131 };
132}
133
134export function applyShellStreamEvent(
135 snapshot: OrchestrationV2ShellSnapshot,
136 event: Exclude<
137 OrchestrationV2ShellStreamItem,
138 { readonly kind: "snapshot" } | { readonly kind: "synchronized" }
139 >,
140): OrchestrationV2ShellSnapshot {
141 if (event.sequence <= snapshot.snapshotSequence) return snapshot;
142
143 switch (event.kind) {
144 case "project.updated": {
145 // Enrichment is async. A project mutation can land with null
146 // repositoryIdentity while an earlier snapshot already resolved it.
147 // Keep the prior identity for the same workspace root so multi-env
148 // grouping does not split until a full snapshot refresh arrives.
149 const previous = snapshot.projects.find((project) => project.id === event.project.id);
150 const project = retainRepositoryIdentity(previous, event.project);
151 return {
152 ...snapshot,
153 projects: upsertById(snapshot.projects, project),
154 snapshotSequence: event.sequence,
155 };
156 }
157 case "project.removed":
158 return {
159 ...snapshot,
160 projects: snapshot.projects.filter((project) => project.id !== event.projectId),
161 snapshotSequence: event.sequence,
162 };
163 case "thread.updated": {
164 const existing =
165 event.location === "active"
166 ? snapshot.threads.find((thread) => thread.id === event.thread.id)
167 : undefined;
168 if (existing !== undefined && sameThreadShell(existing, event.thread)) {
169 return { ...snapshot, snapshotSequence: event.sequence };
170 }
171 const withoutThread = (threads: OrchestrationV2ShellSnapshot["threads"]) =>
172 threads.filter((thread) => thread.id !== event.thread.id);
173 return {
174 ...snapshot,
175 threads:
176 event.location === "active"
177 ? upsertById(snapshot.threads, event.thread)
178 : withoutThread(snapshot.threads),
179 // The archive has its own bounded query/subscription. Older servers may
180 // still send archive-located deltas here; remove them from the normal
181 // shell instead of growing its persisted cache again.
182 archivedThreads: withoutThread(snapshot.archivedThreads),
183 snapshotSequence: event.sequence,
184 };
185 }
186 case "thread.removed":
187 return {
188 ...snapshot,
189 threads: snapshot.threads.filter((thread) => thread.id !== event.threadId),
190 archivedThreads: snapshot.archivedThreads.filter((thread) => thread.id !== event.threadId),
191 snapshotSequence: event.sequence,
192 };
193 default:
194 return snapshot;
195 }
196}