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

shellReducer.ts

151 lines5.5 KB

1import type {
2 OrchestrationProjectShell,
3 OrchestrationV2ShellSnapshot,
4 OrchestrationV2ShellStreamItem,
5} from "@t3tools/contracts";
6​
7function upsertById<T extends { readonly id: unknown }>(
8 items: ReadonlyArray<T>,
9 item: T,
10): ReadonlyArray<T> {
11 const index = items.findIndex((candidate) => candidate.id === item.id);
12 if (index === -1) return [...items, item];
13 return items.map((candidate, candidateIndex) => (candidateIndex === index ? item : candidate));
15​
16function retainRepositoryIdentity(
17 previous: OrchestrationProjectShell | undefined,
18 next: OrchestrationProjectShell,
19): OrchestrationProjectShell {
20 if (
21 next.repositoryIdentity == null &&
22 previous?.repositoryIdentity != null &&
23 previous.workspaceRoot === next.workspaceRoot
24 ) {
25 return { ...next, repositoryIdentity: previous.repositoryIdentity };
26 }
27 return next;
29​
30export interface MergeShellSnapshotOptions {
31 /**
32 * Enrichment refresh: structure and sequence must not roll back when at or
33 * below cache; listed roots accept identity exactly (including null).
34 * Omit this options object for authoritative HTTP/initial WebSocket snapshots.
35 */
36 readonly resolvedRepositoryIdentityRoots: ReadonlyArray<string>;
38​
39/**
40 * Merge an incoming full shell snapshot into prior client state.
41 *
42 * Authoritative snapshots (no options) replace structure and sequence even when
43 * lower than cache, while retaining a prior non-null identity when the candidate
44 * is still unresolved/null for the same root.
45 *
46 * Enrichment snapshots (options present) never resurrect or roll back
47 * projects/threads/archives/sequence when at or below cache; they only patch
48 * repository identity for matching current projects.
49 */
50export function mergeShellSnapshotProjects(
51 previous: OrchestrationV2ShellSnapshot | null | undefined,
52 next: OrchestrationV2ShellSnapshot,
53 options?: MergeShellSnapshotOptions,
54): OrchestrationV2ShellSnapshot {
55 if (previous === null || previous === undefined) {
56 return next;
57 }
58​
59 const isEnrichment = options !== undefined;
60 const resolvedRootSet = isEnrichment ? new Set(options.resolvedRepositoryIdentityRoots) : null;
61​
62 if (isEnrichment && next.snapshotSequence <= previous.snapshotSequence) {
63 const nextById = new Map(next.projects.map((project) => [project.id, project] as const));
64 return {
65 ...previous,
66 projects: previous.projects.map((project) => {
67 const candidate = nextById.get(project.id);
68 if (candidate === undefined || candidate.workspaceRoot !== project.workspaceRoot) {
69 return project;
70 }
71 if (resolvedRootSet?.has(project.workspaceRoot) === true) {
72 return { ...project, repositoryIdentity: candidate.repositoryIdentity };
73 }
74 if (project.repositoryIdentity == null && candidate.repositoryIdentity != null) {
75 return { ...project, repositoryIdentity: candidate.repositoryIdentity };
76 }
77 return project;
78 }),
79 };
80 }
81​
82 const previousById = new Map(previous.projects.map((project) => [project.id, project] as const));
83 return {
84 ...next,
85 projects: next.projects.map((project) => {
86 const prior = previousById.get(project.id);
87 if (resolvedRootSet?.has(project.workspaceRoot) === true) {
88 return project;
89 }
90 return retainRepositoryIdentity(prior, project);
91 }),
92 };
94​
95/** Applies one committed V2 shell delta while preserving active/archive exclusivity. */
96export function applyShellStreamEvent(
97 snapshot: OrchestrationV2ShellSnapshot,
98 event: Exclude<
99 OrchestrationV2ShellStreamItem,
100 { readonly kind: "snapshot" } | { readonly kind: "synchronized" }
101 >,
102): OrchestrationV2ShellSnapshot {
103 if (event.sequence <= snapshot.snapshotSequence) return snapshot;
104​
105 switch (event.kind) {
106 case "project.updated": {
107 // Enrichment is async. A project mutation can land with null
108 // repositoryIdentity while an earlier snapshot already resolved it.
109 // Keep the prior identity for the same workspace root so multi-env
110 // grouping does not split until a full snapshot refresh arrives.
111 const previous = snapshot.projects.find((project) => project.id === event.project.id);
112 const project = retainRepositoryIdentity(previous, event.project);
113 return {
114 ...snapshot,
115 projects: upsertById(snapshot.projects, project),
116 snapshotSequence: event.sequence,
117 };
118 }
119 case "project.removed":
120 return {
121 ...snapshot,
122 projects: snapshot.projects.filter((project) => project.id !== event.projectId),
123 snapshotSequence: event.sequence,
124 };
125 case "thread.updated": {
126 const withoutThread = (threads: OrchestrationV2ShellSnapshot["threads"]) =>
127 threads.filter((thread) => thread.id !== event.thread.id);
128 return {
129 ...snapshot,
130 threads:
131 event.location === "active"
132 ? upsertById(withoutThread(snapshot.threads), event.thread)
133 : withoutThread(snapshot.threads),
134 archivedThreads:
135 event.location === "archive"
136 ? upsertById(withoutThread(snapshot.archivedThreads), event.thread)
137 : withoutThread(snapshot.archivedThreads),
138 snapshotSequence: event.sequence,
139 };
140 }
141 case "thread.removed":
142 return {
143 ...snapshot,
144 threads: snapshot.threads.filter((thread) => thread.id !== event.threadId),
145 archivedThreads: snapshot.archivedThreads.filter((thread) => thread.id !== event.threadId),
146 snapshotSequence: event.sequence,
147 };
148 default:
149 return snapshot;
150 }