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

ProjectCloneTracker.ts

527 lines19.4 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import type {
3 ProjectCloneSnapshot,
4 ProjectCloneStage,
5 ProjectCloneStartInput,
6 ProjectCloneStartResult,
7 ProjectId,
8 SourceControlCloneProtocol,
9 SourceControlRepositoryInfo,
10} from "@t3tools/contracts";
11import {
12 OrchestrationDispatchCommandError,
13 PROJECT_CLONE_DETAIL_MAX_LENGTH,
14 PROJECT_CLONE_ERROR_MAX_LENGTH,
15 SourceControlRepositoryError,
16} from "@t3tools/contracts";
17import * as Cause from "effect/Cause";
18import * as Context from "effect/Context";
19import * as DateTime from "effect/DateTime";
20import * as Effect from "effect/Effect";
21import * as Exit from "effect/Exit";
22import * as Fiber from "effect/Fiber";
23import * as Layer from "effect/Layer";
24import * as Option from "effect/Option";
25import * as PubSub from "effect/PubSub";
26import * as Queue from "effect/Queue";
27import * as Ref from "effect/Ref";
28import * as Schema from "effect/Schema";
29import * as Semaphore from "effect/Semaphore";
30import * as Scope from "effect/Scope";
31import * as Stream from "effect/Stream";
32​
33import * as SourceControlRepositoryService from "../sourceControl/SourceControlRepositoryService.ts";
34import type { ProjectConflictError } from "./ProjectService.ts";
35​
36export class ProjectCloneTracker extends Context.Service<
37 ProjectCloneTracker,
38 {
39 readonly start: (
40 input: ProjectCloneStartInput,
41 hooks: ProjectCloneHooks,
42 ) => Effect.Effect<
43 ProjectCloneStartResult,
44 SourceControlRepositoryError | OrchestrationDispatchCommandError
45 >;
46 /** Interrupts a running clone and deletes the partial checkout. */
47 readonly cancel: (projectId: ProjectId) => Effect.Effect<boolean>;
48 readonly retry: (projectId: ProjectId) => Effect.Effect<boolean, SourceControlRepositoryError>;
49 /** Stops tracking, interrupts any running clone and removes unfinished checkouts. */
50 readonly discard: (projectId: ProjectId) => Effect.Effect<void>;
51 readonly get: (projectId: ProjectId) => Effect.Effect<ProjectCloneSnapshot | null>;
52 readonly stream: Stream.Stream<ReadonlyArray<ProjectCloneSnapshot>>;
53 }
54>()("t3/project/ProjectCloneTracker") {}
55​
56export interface ProjectCloneHooks {
57 /** Fails with `ProjectConflictError` when another project already owns the destination. */
58 readonly createProject: (input: {
59 readonly projectId: ProjectId;
60 readonly title: string;
61 readonly workspaceRoot: string;
62 readonly createdAt: string;
63 }) => Effect.Effect<void, OrchestrationDispatchCommandError | ProjectConflictError>;
64 readonly onCloned: (input: {
65 readonly projectId: ProjectId;
66 readonly workspaceRoot: string;
67 }) => Effect.Effect<void>;
69​
70const DONE_RETENTION = "30 seconds";
71// Git prints nothing while a transfer waits on the network, and the kernel can
72// take 15+ minutes to give up on a dead connection. Without this the banner
73// keeps showing the last transfer speed as if data were still arriving.
74const STALL_AFTER = "20 seconds";
75​
76function clampText(text: string, maxLength: number): string {
77 return text.length <= maxLength ? text : `${text.slice(0, maxLength - 1)}…`;
79​
80const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
81​
82interface TrackedClone {
83 readonly snapshot: ProjectCloneSnapshot;
84 readonly fiber: Fiber.Fiber<unknown, unknown> | null;
85 readonly hooks: ProjectCloneHooks;
86 readonly input: {
87 /** What git is given; may carry credentials and never leaves the server. */
88 readonly cloneUrl: string;
89 readonly destinationPath: string;
90 readonly repository: SourceControlRepositoryInfo | null;
91 readonly protocol: SourceControlCloneProtocol;
92 };
94​
95/** @public */
96export const make = Effect.gen(function* () {
97 const repositories = yield* SourceControlRepositoryService.SourceControlRepositoryService;
98 const clones = yield* Ref.make(new Map<ProjectId, TrackedClone>());
99 const changes = yield* PubSub.unbounded<ReadonlyArray<ProjectCloneSnapshot>>();
100 const retentionFibers = new Map<ProjectId, Fiber.Fiber<unknown, never>>();
101 let sequence = 0;
102 const cloneScope = yield* Scope.make("parallel");
103 yield* Effect.addFinalizer(() => Scope.close(cloneScope, Exit.void));
104 // Serialize actions on the shared entry and directory so retries cannot race clones.
105 const actionLock = yield* Semaphore.make(1);
106 const locked = <A, E, R>(effect: Effect.Effect<A, E, R>) => actionLock.withPermits(1)(effect);
107​
108 const list = Ref.get(clones).pipe(
109 Effect.map((current) => Array.from(current.values(), (tracked) => tracked.snapshot)),
110 );
111 const publish = list.pipe(Effect.flatMap((snapshots) => PubSub.publish(changes, snapshots)));
112​
113 const modify = (
114 projectId: ProjectId,
115 mutate: (tracked: TrackedClone) => TrackedClone,
116 ): Effect.Effect<ProjectCloneSnapshot | null> =>
117 Ref.modify(clones, (current) => {
118 const existing = current.get(projectId);
119 if (!existing) return [null, current] as const;
120 const nextTracked = mutate(existing);
121 const nextSnapshot = { ...nextTracked.snapshot, sequence: ++sequence };
122 const next = new Map(current);
123 next.set(projectId, { ...nextTracked, snapshot: nextSnapshot });
124 return [nextSnapshot, next] as const;
125 }).pipe(Effect.tap((snapshot) => (snapshot ? publish : Effect.void)));
126​
127 const clearRetention = (projectId: ProjectId) => {
128 const fiber = retentionFibers.get(projectId);
129 retentionFibers.delete(projectId);
130 return fiber ? Fiber.interrupt(fiber).pipe(Effect.ignore) : Effect.void;
131 };
132​
133 const remove = (projectId: ProjectId) =>
134 Ref.update(clones, (current) => {
135 if (!current.has(projectId)) return current;
136 const next = new Map(current);
137 next.delete(projectId);
138 return next;
139 }).pipe(Effect.andThen(publish));
140​
141 const scheduleRemoval = Effect.fn("ProjectCloneTracker.scheduleRemoval")(function* (
142 projectId: ProjectId,
143 ) {
144 yield* clearRetention(projectId);
145 const fiber = yield* remove(projectId).pipe(
146 Effect.delay(DONE_RETENTION),
147 Effect.ensuring(
148 Effect.sync(() => {
149 if (retentionFibers.get(projectId) === fiber) retentionFibers.delete(projectId);
150 }),
151 ),
152 Effect.forkDetach,
153 );
154 retentionFibers.set(projectId, fiber);
155 });
156​
157 const progress = (
158 projectId: ProjectId,
159 update: {
160 readonly stage: ProjectCloneStage;
161 readonly percent: number | null;
162 readonly detail: string | null;
163 },
164 ) =>
165 modify(projectId, (tracked) => ({
166 ...tracked,
167 snapshot: {
168 ...tracked.snapshot,
169 stage: update.stage,
170 percent: update.percent,
171 detail:
172 update.detail === null ? null : clampText(update.detail, PROJECT_CLONE_DETAIL_MAX_LENGTH),
173 stalled: false,
174 },
175 })).pipe(Effect.asVoid);
176​
177 const markStalled = (projectId: ProjectId) =>
178 modify(projectId, (tracked) => ({
179 ...tracked,
180 snapshot: { ...tracked.snapshot, stalled: true },
181 })).pipe(Effect.asVoid);
182​
183 /** Marks the clone stalled once, then waits for git to speak again. */
184 const watchForStall = (projectId: ProjectId, pulses: Queue.Dequeue<void>) =>
185 Queue.take(pulses).pipe(
186 Effect.timeoutOption(STALL_AFTER),
187 Effect.flatMap((pulse) =>
188 Option.isSome(pulse)
189 ? Effect.void
190 : markStalled(projectId).pipe(Effect.andThen(Queue.take(pulses))),
191 ),
192 Effect.forever,
193 );
194​
195 const finish = Effect.fn("ProjectCloneTracker.finish")(function* (
196 projectId: ProjectId,
197 phase: "done" | "failed" | "cancelled",
198 error: string | null,
199 ) {
200 const endedAt = yield* nowIso;
201 yield* modify(projectId, (tracked) => ({
202 ...tracked,
203 fiber: null,
204 snapshot: {
205 ...tracked.snapshot,
206 phase,
207 endedAt,
208 percent: phase === "done" ? 100 : tracked.snapshot.percent,
209 stalled: false,
210 error: error === null ? null : clampText(error, PROJECT_CLONE_ERROR_MAX_LENGTH),
211 },
212 }));
213 if (phase === "done") yield* scheduleRemoval(projectId);
214 });
215​
216 const cloneWatchingForStall = Effect.fn("ProjectCloneTracker.cloneWatchingForStall")(function* (
217 projectId: ProjectId,
218 tracked: TrackedClone,
219 ) {
220 const pulses = yield* Queue.sliding<void>(1);
221 yield* watchForStall(projectId, pulses).pipe(Effect.forkScoped);
222 return yield* repositories.cloneRepository(
223 {
224 remoteUrl: tracked.input.cloneUrl,
225 destinationPath: tracked.input.destinationPath,
226 protocol: tracked.input.protocol,
227 },
228 {
229 onProgress: (update) =>
230 Queue.offer(pulses, undefined).pipe(Effect.andThen(progress(projectId, update))),
231 timeoutMs: null,
232 },
233 );
234 }, Effect.scoped);
235​
236 /**
237 * `onExit` records outcomes even on interruption; `matchCause` would be skipped.
238 * Mark the clone done before the post-clone hook so a late Cancel cannot remove it.
239 */
240 const runClone = (projectId: ProjectId, tracked: TrackedClone) =>
241 cloneWatchingForStall(projectId, tracked).pipe(
242 Effect.onExit((exit) =>
243 Exit.isSuccess(exit)
244 ? finish(projectId, "done", null)
245 : Cause.hasInterruptsOnly(exit.cause)
246 ? finish(projectId, "cancelled", null)
247 : finish(projectId, "failed", describeCloneFailure(exit.cause)),
248 ),
249 Effect.flatMap(() =>
250 tracked.hooks
251 .onCloned({ projectId, workspaceRoot: tracked.input.destinationPath })
252 .pipe(Effect.ignoreCause({ log: true })),
253 ),
254 Effect.ignoreCause(),
255 );
256​
257 const launch = Effect.fn("ProjectCloneTracker.launch")(function* (projectId: ProjectId) {
258 const current = yield* Ref.get(clones);
259 const tracked = current.get(projectId);
260 if (!tracked) return;
261 const fiber = yield* runClone(projectId, tracked).pipe(Effect.forkIn(cloneScope));
262 yield* Ref.update(clones, (map) => {
263 const existing = map.get(projectId);
264 if (!existing) return map;
265 const next = new Map(map);
266 next.set(projectId, { ...existing, fiber });
267 return next;
268 });
269 });
270​
271 const moveClaim = (from: ProjectId, to: ProjectId) =>
272 Ref.modify(clones, (current) => {
273 const claim = current.get(from);
274 if (!claim || current.has(to)) return [false, current] as const;
275 const next = new Map(current);
276 next.delete(from);
277 next.set(to, { ...claim, snapshot: { ...claim.snapshot, projectId: to } });
278 return [true, next] as const;
279 });
280​
281 const start: ProjectCloneTracker["Service"]["start"] = Effect.fn("ProjectCloneTracker.start")(
282 function* (input, hooks) {
283 const prepared = yield* repositories.prepareClone(input);
284 const startedAt = yield* nowIso;
285 const snapshot: ProjectCloneSnapshot = {
286 projectId: input.projectId,
287 remoteUrl: prepared.remoteUrl,
288 destinationPath: prepared.destinationPath,
289 repository: prepared.repository,
290 phase: "running",
291 stage: "connecting",
292 percent: null,
293 detail: null,
294 stalled: false,
295 error: null,
296 startedAt,
297 endedAt: null,
298 sequence: ++sequence,
299 };
300 const claim = yield* Ref.modify(clones, (current) => {
301 const overlapping = Array.from(current.values()).filter(
302 (tracked) =>
303 tracked.snapshot.projectId === input.projectId ||
304 tracked.input.destinationPath === prepared.destinationPath,
305 );
306 // Only a running clone owns its destination; a failed or cancelled one
307 // left nothing behind, so a fresh start supersedes it.
308 if (overlapping.some((tracked) => tracked.snapshot.phase === "running")) {
309 return [null, current] as const;
310 }
311 const next = new Map(current);
312 for (const stale of overlapping) next.delete(stale.snapshot.projectId);
313 next.set(input.projectId, {
314 snapshot,
315 fiber: null,
316 hooks,
317 input: {
318 cloneUrl: prepared.cloneUrl,
319 destinationPath: prepared.destinationPath,
320 repository: prepared.repository,
321 protocol: input.protocol ?? "auto",
322 },
323 });
324 return [overlapping.map((stale) => stale.snapshot.projectId), next] as const;
325 });
326 const alreadyInProgress = new SourceControlRepositoryError({
327 operation: "cloneRepository",
328 provider: input.provider ?? SourceControlProviderKind.make("unknown"),
329 detail: "A clone into this destination is already in progress.",
330 });
331 if (claim === null) return yield* alreadyInProgress;
332 // A superseded done entry's pending removal would otherwise delete the new claim.
333 yield* Effect.forEach(claim, clearRetention, { discard: true });
334 // Finish or release the claim despite disconnects; a claim without a fiber cannot be cancelled or retried.
335 const projectId = yield* Effect.uninterruptible(
336 Effect.gen(function* () {
337 yield* clearRetention(input.projectId);
338 // Register before creation so racing thread.create and project.delete see the clone.
339 const projectId = yield* hooks
340 .createProject({
341 projectId: input.projectId,
342 title: input.title,
343 workspaceRoot: prepared.destinationPath,
344 createdAt: input.createdAt,
345 })
346 .pipe(
347 Effect.as(input.projectId),
348 // A failed clone's project still owns its empty folder after a restart
349 // forgets the clone; clone for that project instead.
350 Effect.catchTags({
351 ProjectConflictError: (conflict) =>
352 moveClaim(input.projectId, conflict.conflictingProjectId).pipe(
353 Effect.flatMap((moved) =>
354 moved ? Effect.succeed(conflict.conflictingProjectId) : alreadyInProgress,
355 ),
356 ),
357 }),
358 );
359 yield* publish;
360 yield* launch(projectId);
361 return projectId;
362 }).pipe(Effect.tapError(() => remove(input.projectId))),
363 );
364 return {
365 projectId,
366 cwd: prepared.destinationPath,
367 remoteUrl: prepared.remoteUrl,
368 repository: prepared.repository,
369 };
370 },
371 );
372​
373 const get: ProjectCloneTracker["Service"]["get"] = (projectId) =>
374 Ref.get(clones).pipe(Effect.map((current) => current.get(projectId)?.snapshot ?? null));
375​
376 const cancel: ProjectCloneTracker["Service"]["cancel"] = Effect.fn("ProjectCloneTracker.cancel")(
377 function* (projectId) {
378 const current = yield* Ref.get(clones);
379 const tracked = current.get(projectId);
380 if (!tracked || tracked.snapshot.phase !== "running" || !tracked.fiber) return false;
381 // Finish cancellation despite disconnects to avoid dead fibers behind running snapshots.
382 yield* Effect.uninterruptible(
383 Effect.gen(function* () {
384 yield* Fiber.interrupt(tracked.fiber!);
385 const after = yield* get(projectId);
386 if (after?.phase === "done") return;
387 if (after?.phase === "running") yield* finish(projectId, "cancelled", null);
388 yield* repositories.discardClone(tracked.input.destinationPath).pipe(Effect.ignore);
389 }),
390 );
391 return true;
392 },
393 );
394​
395 const retry: ProjectCloneTracker["Service"]["retry"] = Effect.fn("ProjectCloneTracker.retry")(
396 function* (projectId) {
397 const current = yield* Ref.get(clones);
398 const tracked = current.get(projectId);
399 if (
400 !tracked ||
401 (tracked.snapshot.phase !== "failed" && tracked.snapshot.phase !== "cancelled")
402 ) {
403 return false;
404 }
405 yield* repositories.discardClone(tracked.input.destinationPath);
406 const startedAt = yield* nowIso;
407 yield* modify(projectId, (entry) => ({
408 ...entry,
409 snapshot: {
410 ...entry.snapshot,
411 phase: "running",
412 stage: "connecting",
413 percent: null,
414 detail: null,
415 stalled: false,
416 error: null,
417 startedAt,
418 endedAt: null,
419 },
420 }));
421 yield* launch(projectId);
422 return true;
423 },
424 );
425​
426 const discard: ProjectCloneTracker["Service"]["discard"] = Effect.fn(
427 "ProjectCloneTracker.discard",
428 )(function* (projectId) {
429 const current = yield* Ref.get(clones);
430 const tracked = current.get(projectId);
431 if (!tracked) return;
432 if (tracked.fiber) yield* Fiber.interrupt(tracked.fiber);
433 // Git may have finished while the interrupt was landing; the project
434 // is going away either way, but a complete checkout is the user's.
435 const after = yield* get(projectId);
436 if (after?.phase !== "done") {
437 yield* repositories.discardClone(tracked.input.destinationPath).pipe(Effect.ignore);
438 }
439 yield* clearRetention(projectId);
440 yield* remove(projectId);
441 });
442​
443 const stream: ProjectCloneTracker["Service"]["stream"] = Stream.callback<
444 ReadonlyArray<ProjectCloneSnapshot>
445 >(
446 (mailbox) =>
447 Effect.gen(function* () {
448 const subscription = yield* PubSub.subscribe(changes);
449 Queue.offerUnsafe(mailbox, yield* list);
450 yield* Stream.fromSubscription(subscription).pipe(
451 Stream.runForEach((snapshots) =>
452 Effect.sync(() => Queue.offerUnsafe(mailbox, snapshots)),
453 ),
454 Effect.forkScoped,
455 );
456 }),
457 { bufferSize: 1, strategy: "sliding" },
458 );
459​
460 // Forwarders retain the underlying operation spans.
461 return ProjectCloneTracker.of({
462 start: Effect.fnUntraced(function* (input, hooks) {
463 return yield* locked(start(input, hooks));
464 }),
465 cancel: Effect.fnUntraced(function* (projectId) {
466 return yield* locked(cancel(projectId));
467 }),
468 retry: Effect.fnUntraced(function* (projectId) {
469 return yield* locked(retry(projectId));
470 }),
471 discard: Effect.fnUntraced(function* (projectId) {
472 return yield* locked(discard(projectId));
473 }),
474 get,
475 stream,
476 });
477});
478​
479const isSourceControlRepositoryError = Schema.is(SourceControlRepositoryError);
480​
481/**
482 * Every dispatch transport (WebSocket, HTTP) runs this before normalizing so no
483 * client can start a thread on an empty tree, and no attachment copies are made
484 * for a command that is about to be refused.
485 */
486export const rejectCommandsDuringClone = Effect.fn("ProjectCloneTracker.rejectCommandsDuringClone")(
487 function* (
488 tracker: ProjectCloneTracker["Service"],
489 command: {
490 readonly type: string;
491 readonly projectId?: ProjectId;
492 readonly bootstrap?: unknown;
493 },
494 ): Effect.fn.Return<void, OrchestrationDispatchCommandError> {
495 const projectId =
496 command.type === "thread.create"
497 ? (command.projectId ?? null)
498 : command.type === "thread.turn.start"
499 ? bootstrapProjectId(command.bootstrap)
500 : null;
501 if (projectId === null) return;
502 const clone = yield* tracker.get(projectId);
503 if (clone === null || clone.phase === "done") return;
504 return yield* new OrchestrationDispatchCommandError({
505 message:
506 clone.phase === "running"
507 ? "The repository is still being cloned."
508 : "The repository was not cloned. Retry the clone first.",
509 });
510 },
511);
512​
513function bootstrapProjectId(bootstrap: unknown): ProjectId | null {
514 if (typeof bootstrap !== "object" || bootstrap === null) return null;
515 const createThread = (bootstrap as { createThread?: { projectId?: ProjectId } }).createThread;
516 return createThread?.projectId ?? null;
518​
519function describeCloneFailure(cause: Cause.Cause<unknown>): string {
520 const error = Cause.squash(cause);
521 if (isSourceControlRepositoryError(error)) return error.detail;
522 return error instanceof Error && error.message.trim().length > 0
523 ? error.message
524 : "The repository could not be cloned.";
526​
527export const layer = Layer.effect(ProjectCloneTracker, make);