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

shellReducer.ts

184 lines6.7 KB

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