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

VcsStatusBroadcaster.ts

664 lines21.9 KB

1import * as Cause from "effect/Cause";
2import * as Context from "effect/Context";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Exit from "effect/Exit";
6import * as Fiber from "effect/Fiber";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as PubSub from "effect/PubSub";
10import * as Ref from "effect/Ref";
11import * as Schedule from "effect/Schedule";
12import * as Scope from "effect/Scope";
13import * as Stream from "effect/Stream";
14import * as SynchronizedRef from "effect/SynchronizedRef";
15import type {
16 GitManagerServiceError,
17 VcsStatusInput,
18 VcsStatusLocalResult,
19 VcsStatusRemoteResult,
20 VcsStatusResult,
21 VcsStatusStreamEvent,
22} from "@t3tools/contracts";
23import { mergeGitStatusParts } from "@t3tools/shared/git";
24​
25import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
26import * as GitWorkflowService from "../git/GitWorkflowService.ts";
27import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
28​
29const DEFAULT_VCS_STATUS_REFRESH_INTERVAL = Duration.seconds(30);
30const VCS_STATUS_REFRESH_FAILURE_BASE_DELAY = Duration.seconds(30);
31const VCS_STATUS_REFRESH_FAILURE_MAX_DELAY = Duration.minutes(15);
32const MAX_FAILURE_DIAGNOSTIC_VALUES = 8;
33const MAX_FAILURE_DIAGNOSTIC_VALUE_LENGTH = 128;
34​
35function boundedDiagnosticValue(value: string): string {
36 return value.slice(0, MAX_FAILURE_DIAGNOSTIC_VALUE_LENGTH);
38​
39function diagnosticValueTag(value: unknown): string {
40 try {
41 if (
42 typeof value === "object" &&
43 value !== null &&
44 "_tag" in value &&
45 typeof value._tag === "string"
46 ) {
47 return boundedDiagnosticValue(value._tag);
48 }
49 if (value instanceof Error) {
50 return boundedDiagnosticValue(value.name);
51 }
52 return typeof value;
53 } catch {
54 return "Uninspectable";
55 }
57​
58function diagnosticFailureOperation(value: unknown): string | undefined {
59 try {
60 if (
61 typeof value === "object" &&
62 value !== null &&
63 "operation" in value &&
64 typeof value.operation === "string"
65 ) {
66 return boundedDiagnosticValue(value.operation);
67 }
68 } catch {
69 return undefined;
70 }
71 return undefined;
73​
74function addUniqueDiagnosticValue(values: Array<string>, value: string | undefined): void {
75 if (
76 value !== undefined &&
77 values.length < MAX_FAILURE_DIAGNOSTIC_VALUES &&
78 !values.includes(value)
79 ) {
80 values.push(value);
81 }
83​
84export function remoteRefreshFailureDiagnostics(cause: Cause.Cause<unknown>) {
85 const failureTags: Array<string> = [];
86 const failureOperations: Array<string> = [];
87 const defectTags: Array<string> = [];
88 let failureCount = 0;
89 let defectCount = 0;
90 let interruptionCount = 0;
91​
92 for (const reason of cause.reasons) {
93 if (Cause.isFailReason(reason)) {
94 failureCount += 1;
95 addUniqueDiagnosticValue(failureTags, diagnosticValueTag(reason.error));
96 addUniqueDiagnosticValue(failureOperations, diagnosticFailureOperation(reason.error));
97 continue;
98 }
99 if (Cause.isDieReason(reason)) {
100 defectCount += 1;
101 addUniqueDiagnosticValue(defectTags, diagnosticValueTag(reason.defect));
102 continue;
103 }
104 interruptionCount += 1;
105 }
106​
107 return {
108 reasonCount: cause.reasons.length,
109 failureCount,
110 failureTags,
111 failureOperations,
112 defectCount,
113 defectTags,
114 interruptionCount,
115 };
117​
118interface VcsStatusChange {
119 readonly cwd: string;
120 readonly event: VcsStatusStreamEvent;
122​
123interface CachedValue<T> {
124 readonly fingerprint: string;
125 readonly value: T;
127​
128interface CachedVcsStatus {
129 readonly local: CachedValue<VcsStatusLocalResult> | null;
130 readonly remote: CachedValue<VcsStatusRemoteResult | null> | null;
132​
133interface ActiveRemotePoller {
134 readonly fiber: Fiber.Fiber<void, never>;
135 readonly subscriberCount: number;
136 readonly demandCwds: Ref.Ref<ReadonlyMap<string, number>>;
138​
139interface StreamStatusOptions {
140 readonly automaticRemoteRefreshInterval?: Effect.Effect<Duration.Duration, never>;
142​
143export class VcsAutoPullPolicy extends Context.Reference<{
144 readonly isEnabled: (cwd: string) => Effect.Effect<boolean, never>;
145}>("t3/vcs/VcsAutoPullPolicy", {
146 defaultValue: () => ({ isEnabled: () => Effect.succeed(false) }),
147}) {}
148​
149export const autoPullPolicyLayer = Layer.effect(
150 VcsAutoPullPolicy,
151 Effect.gen(function* () {
152 const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
153 return {
154 isEnabled: (cwd: string) =>
155 snapshots.getActiveProjectByWorkspaceRoot(cwd).pipe(
156 Effect.map((project) => project._tag === "Some" && project.value.autoPull === true),
157 Effect.orElseSucceed(() => false),
158 ),
159 };
160 }),
161);
162​
163export function remoteRefreshFailureDelay(
164 consecutiveFailures: number,
165 configuredInterval: Duration.Duration,
166) {
167 const exponent = Math.max(0, consecutiveFailures - 1);
168 const backoffMs =
169 Duration.toMillis(VCS_STATUS_REFRESH_FAILURE_BASE_DELAY) * Math.pow(2, exponent);
170 const cappedBackoff = Duration.min(
171 Duration.millis(backoffMs),
172 VCS_STATUS_REFRESH_FAILURE_MAX_DELAY,
173 );
174 return Duration.max(configuredInterval, cappedBackoff);
176​
177export class VcsStatusBroadcaster extends Context.Service<
178 VcsStatusBroadcaster,
179 {
180 readonly getStatus: (
181 input: VcsStatusInput,
182 ) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
183 readonly refreshLocalStatus: (
184 cwd: string,
185 ) => Effect.Effect<VcsStatusLocalResult, GitManagerServiceError>;
186 readonly refreshStatus: (cwd: string) => Effect.Effect<VcsStatusResult, GitManagerServiceError>;
187 readonly streamStatus: (
188 input: VcsStatusInput,
189 options?: StreamStatusOptions,
190 ) => Stream.Stream<VcsStatusStreamEvent, GitManagerServiceError>;
191 }
192>()("t3/vcs/VcsStatusBroadcaster") {}
193​
194function fingerprintStatusPart(status: unknown): string {
195 return JSON.stringify(status);
197​
198const normalizeCwd = (cwd: string) =>
199 Effect.service(FileSystem.FileSystem).pipe(
200 Effect.flatMap((fs) => fs.realPath(cwd)),
201 Effect.orElseSucceed(() => cwd),
202 );
203​
204export const make = Effect.gen(function* () {
205 const autoPullPolicy = yield* VcsAutoPullPolicy;
206 const workflow = yield* GitWorkflowService.GitWorkflowService;
207 const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
208 const fs = yield* FileSystem.FileSystem;
209 const changesPubSub = yield* Effect.acquireRelease(
210 PubSub.unbounded<VcsStatusChange>(),
211 (pubsub) => PubSub.shutdown(pubsub),
212 );
213 const broadcasterScope = yield* Effect.acquireRelease(Scope.make(), (scope) =>
214 Scope.close(scope, Exit.void),
215 );
216 const cacheRef = yield* Ref.make(new Map<string, CachedVcsStatus>());
217 const pollersRef = yield* SynchronizedRef.make(new Map<string, ActiveRemotePoller>());
218​
219 const getCachedStatus = Effect.fn("VcsStatusBroadcaster.getCachedStatus")(function* (
220 cwd: string,
221 ) {
222 return yield* Ref.get(cacheRef).pipe(Effect.map((cache) => cache.get(cwd) ?? null));
223 });
224​
225 const updateCachedLocalStatus = Effect.fn("VcsStatusBroadcaster.updateCachedLocalStatus")(
226 function* (cwd: string, local: VcsStatusLocalResult, options?: { publish?: boolean }) {
227 const nextLocal = {
228 fingerprint: fingerprintStatusPart(local),
229 value: local,
230 } satisfies CachedValue<VcsStatusLocalResult>;
231 const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
232 const previous = cache.get(cwd) ?? { local: null, remote: null };
233 const nextCache = new Map(cache);
234 nextCache.set(cwd, {
235 ...previous,
236 local: nextLocal,
237 });
238 return [previous.local?.fingerprint !== nextLocal.fingerprint, nextCache] as const;
239 });
240​
241 if (options?.publish && shouldPublish) {
242 yield* PubSub.publish(changesPubSub, {
243 cwd,
244 event: {
245 _tag: "localUpdated",
246 local,
247 },
248 });
249 }
250​
251 return local;
252 },
253 );
254​
255 const updateCachedRemoteStatus = Effect.fn("VcsStatusBroadcaster.updateCachedRemoteStatus")(
256 function* (cwd: string, remote: VcsStatusRemoteResult | null, options?: { publish?: boolean }) {
257 const nextRemote = {
258 fingerprint: fingerprintStatusPart(remote),
259 value: remote,
260 } satisfies CachedValue<VcsStatusRemoteResult | null>;
261 const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
262 const previous = cache.get(cwd) ?? { local: null, remote: null };
263 const nextCache = new Map(cache);
264 nextCache.set(cwd, {
265 ...previous,
266 remote: nextRemote,
267 });
268 return [previous.remote?.fingerprint !== nextRemote.fingerprint, nextCache] as const;
269 });
270​
271 if (options?.publish && shouldPublish) {
272 yield* PubSub.publish(changesPubSub, {
273 cwd,
274 event: {
275 _tag: "remoteUpdated",
276 remote,
277 },
278 });
279 }
280​
281 return remote;
282 },
283 );
284​
285 const updateCachedStatus = Effect.fn("VcsStatusBroadcaster.updateCachedStatus")(function* (
286 cwd: string,
287 local: VcsStatusLocalResult,
288 remote: VcsStatusRemoteResult | null,
289 options?: { publish?: boolean },
290 ) {
291 const nextLocal = {
292 fingerprint: fingerprintStatusPart(local),
293 value: local,
294 } satisfies CachedValue<VcsStatusLocalResult>;
295 const nextRemote = {
296 fingerprint: fingerprintStatusPart(remote),
297 value: remote,
298 } satisfies CachedValue<VcsStatusRemoteResult | null>;
299 const shouldPublish = yield* Ref.modify(cacheRef, (cache) => {
300 const previous = cache.get(cwd) ?? { local: null, remote: null };
301 const nextCache = new Map(cache);
302 nextCache.set(cwd, {
303 local: nextLocal,
304 remote: nextRemote,
305 });
306 return [
307 previous.local?.fingerprint !== nextLocal.fingerprint ||
308 previous.remote?.fingerprint !== nextRemote.fingerprint,
309 nextCache,
310 ] as const;
311 });
312​
313 if (options?.publish && shouldPublish) {
314 yield* PubSub.publish(changesPubSub, {
315 cwd,
316 event: {
317 _tag: "snapshot",
318 local,
319 remote,
320 },
321 });
322 }
323​
324 return mergeGitStatusParts(local, remote);
325 });
326​
327 const loadLocalStatus = Effect.fn("VcsStatusBroadcaster.loadLocalStatus")(function* (
328 cwd: string,
329 ) {
330 const local = yield* workflow.localStatus({ cwd });
331 return yield* updateCachedLocalStatus(cwd, local);
332 });
333​
334 const getOrLoadLocalStatus = Effect.fn("VcsStatusBroadcaster.getOrLoadLocalStatus")(function* (
335 cwd: string,
336 ) {
337 const cached = yield* getCachedStatus(cwd);
338 if (cached?.local) {
339 return cached.local.value;
340 }
341 return yield* loadLocalStatus(cwd);
342 });
343​
344 const withFileSystem = Effect.provideService(FileSystem.FileSystem, fs);
345​
346 const getStatus: VcsStatusBroadcaster["Service"]["getStatus"] = Effect.fn(
347 "VcsStatusBroadcaster.getStatus",
348 )(function* (input) {
349 const cwd = yield* withFileSystem(normalizeCwd(input.cwd));
350 const cached = yield* getCachedStatus(cwd);
351 if (cached?.local && cached.remote) {
352 return mergeGitStatusParts(cached.local.value, cached.remote.value);
353 }
354 const [local, remote] = yield* Effect.all(
355 [
356 cached?.local ? Effect.succeed(cached.local.value) : workflow.localStatus({ cwd }),
357 cached?.remote ? Effect.succeed(cached.remote.value) : workflow.remoteStatus({ cwd }),
358 ],
359 { concurrency: "unbounded" },
360 );
361 return yield* updateCachedStatus(cwd, local, remote);
362 });
363​
364 const refreshLocalStatusCore = Effect.fn("VcsStatusBroadcaster.refreshLocalStatusCore")(
365 function* (cwd: string) {
366 yield* workflow.invalidateLocalStatus(cwd);
367 const local = yield* workflow.localStatus({ cwd });
368 return yield* updateCachedLocalStatus(cwd, local, { publish: true });
369 },
370 );
371​
372 const refreshLocalStatus: VcsStatusBroadcaster["Service"]["refreshLocalStatus"] = Effect.fn(
373 "VcsStatusBroadcaster.refreshLocalStatus",
374 )(function* (rawCwd) {
375 const cwd = yield* withFileSystem(normalizeCwd(rawCwd));
376 return yield* refreshLocalStatusCore(cwd);
377 });
378​
379 const maybeAutoPull = Effect.fn("VcsStatusBroadcaster.maybeAutoPull")(function* (
380 cwd: string,
381 remote: VcsStatusRemoteResult | null,
382 policyCwds: ReadonlyArray<string>,
383 ) {
384 return yield* Effect.gen(function* () {
385 const autoPullEnabled = (yield* Effect.forEach(policyCwds, autoPullPolicy.isEnabled, {
386 concurrency: "unbounded",
387 })).some(Boolean);
388 if (
389 remote === null ||
390 !remote.hasUpstream ||
391 remote.aheadCount > 0 ||
392 remote.behindCount <= 0 ||
393 !autoPullEnabled
394 ) {
395 return null;
396 }
397​
398 yield* workflow.invalidateLocalStatus(cwd);
399 const local = yield* workflow.localStatus({ cwd });
400 if (!local.isRepo || !local.isDefaultRef || local.hasWorkingTreeChanges) return null;
401​
402 yield* workflow.pullCurrentBranch(cwd);
403 yield* workflow.invalidateStatus(cwd);
404 const [refreshedLocal, refreshedRemote] = yield* Effect.all(
405 [workflow.localStatus({ cwd }), workflow.remoteStatus({ cwd }, { refreshUpstream: false })],
406 { concurrency: "unbounded" },
407 );
408 yield* updateCachedStatus(cwd, refreshedLocal, refreshedRemote, { publish: true });
409 return { local: refreshedLocal, remote: refreshedRemote };
410 }).pipe(
411 Effect.catch(() =>
412 Effect.logWarning("Automatic project pull failed", { cwd }).pipe(Effect.as(null)),
413 ),
414 );
415 });
416​
417 const refreshRemoteStatus = Effect.fn("VcsStatusBroadcaster.refreshRemoteStatus")(function* (
418 cwd: string,
419 options?: {
420 readonly refreshUpstream?: boolean;
421 readonly policyCwds?: ReadonlyArray<string>;
422 },
423 ) {
424 if (options?.refreshUpstream !== false) {
425 yield* workflow.invalidateRemoteStatus(cwd);
426 }
427 const remote = yield* workflow.remoteStatus({ cwd }, options);
428 const pulled = yield* maybeAutoPull(cwd, remote, options?.policyCwds ?? [cwd]);
429 if (pulled !== null) return pulled.remote;
430 return yield* updateCachedRemoteStatus(cwd, remote, { publish: true });
431 });
432​
433 const refreshStatus: VcsStatusBroadcaster["Service"]["refreshStatus"] = Effect.fn(
434 "VcsStatusBroadcaster.refreshStatus",
435 )(function* (rawCwd) {
436 const cwd = yield* withFileSystem(normalizeCwd(rawCwd));
437 // invalidateStatus (not the two partial invalidations) so an explicit
438 // refresh also bypasses GitManager's slow PR-lookup cache.
439 yield* workflow.invalidateStatus(cwd);
440 const [local, remote] = yield* Effect.all(
441 [workflow.localStatus({ cwd }), workflow.remoteStatus({ cwd })],
442 { concurrency: "unbounded" },
443 );
444 const pulled = yield* maybeAutoPull(cwd, remote, [rawCwd]);
445 if (pulled !== null) return mergeGitStatusParts(pulled.local, pulled.remote);
446 return yield* updateCachedStatus(cwd, local, remote, { publish: true });
447 });
448​
449 const makeRemoteRefreshLoop = (
450 cwd: string,
451 demandCwdsRef: Ref.Ref<ReadonlyMap<string, number>>,
452 automaticRemoteRefreshInterval: Effect.Effect<Duration.Duration, never>,
453 refreshImmediately: boolean,
454 ) => {
455 return Effect.gen(function* () {
456 const consecutiveFailuresRef = yield* Ref.make(0);
457 const needsInitialRefreshRef = yield* Ref.make(refreshImmediately);
458 const refreshRemoteStatusIfEnabled = Effect.gen(function* () {
459 const configuredInterval = yield* automaticRemoteRefreshInterval;
460 const activeInterval = Duration.isZero(configuredInterval)
461 ? DEFAULT_VCS_STATUS_REFRESH_INTERVAL
462 : configuredInterval;
463 const needsInitialRefresh = yield* Ref.get(needsInitialRefreshRef);
464 if (Duration.isZero(configuredInterval) && !needsInitialRefresh) {
465 return activeInterval;
466 }
467​
468 const demandCwds = yield* Ref.get(demandCwdsRef);
469 const shouldRun =
470 needsInitialRefresh ||
471 (yield* Effect.all(
472 [...demandCwds.keys()].map((demandCwd) =>
473 backgroundPolicy.shouldRunScopeWork({
474 type: "vcs-status",
475 cwd: demandCwd,
476 }),
477 ),
478 { concurrency: "unbounded" },
479 )).some(Boolean);
480 if (!shouldRun) {
481 return activeInterval;
482 }
483​
484 const exit = yield* refreshRemoteStatus(cwd, {
485 refreshUpstream: !Duration.isZero(configuredInterval),
486 policyCwds: [...demandCwds.keys()],
487 }).pipe(Effect.exit);
488 if (Exit.isSuccess(exit)) {
489 yield* Ref.set(needsInitialRefreshRef, false);
490 yield* Ref.set(consecutiveFailuresRef, 0);
491 return activeInterval;
492 }
493​
494 const interruptionReasons = exit.cause.reasons.filter(Cause.isInterruptReason);
495 if (interruptionReasons.length > 0) {
496 return yield* Effect.failCause(Cause.fromReasons<never>(interruptionReasons));
497 }
498​
499 const consecutiveFailures = yield* Ref.updateAndGet(
500 consecutiveFailuresRef,
501 (count) => count + 1,
502 );
503 const nextDelay = remoteRefreshFailureDelay(consecutiveFailures, activeInterval);
504 yield* Effect.logWarning("VCS remote status refresh failed", {
505 cwdLength: cwd.length,
506 ...remoteRefreshFailureDiagnostics(exit.cause),
507 consecutiveFailures,
508 nextDelayMs: Duration.toMillis(nextDelay),
509 });
510 return nextDelay;
511 });
512​
513 if (!refreshImmediately) {
514 const configuredInterval = yield* automaticRemoteRefreshInterval;
515 yield* Effect.sleep(
516 Duration.isZero(configuredInterval)
517 ? DEFAULT_VCS_STATUS_REFRESH_INTERVAL
518 : configuredInterval,
519 );
520 }
521​
522 return yield* refreshRemoteStatusIfEnabled.pipe(
523 Effect.repeat(
524 Schedule.identity<Duration.Duration>().pipe(
525 Schedule.addDelay(({ output: delay }) => Effect.succeed(delay)),
526 ),
527 ),
528 Effect.asVoid,
529 );
530 });
531 };
532​
533 const retainRemotePoller = Effect.fn("VcsStatusBroadcaster.retainRemotePoller")(function* (
534 cwd: string,
535 demandCwd: string,
536 automaticRemoteRefreshInterval: Effect.Effect<Duration.Duration, never>,
537 refreshImmediately: boolean,
538 ) {
539 yield* SynchronizedRef.modifyEffect(pollersRef, (activePollers) => {
540 const existing = activePollers.get(cwd);
541 if (existing) {
542 return Ref.update(existing.demandCwds, (demandCwds) => {
543 const next = new Map(demandCwds);
544 next.set(demandCwd, (next.get(demandCwd) ?? 0) + 1);
545 return next;
546 }).pipe(
547 Effect.map(() => {
548 const nextPollers = new Map(activePollers);
549 nextPollers.set(cwd, {
550 ...existing,
551 subscriberCount: existing.subscriberCount + 1,
552 });
553 return [undefined, nextPollers] as const;
554 }),
555 );
556 }
557​
558 return Ref.make<ReadonlyMap<string, number>>(new Map([[demandCwd, 1]])).pipe(
559 Effect.flatMap((demandCwds) =>
560 makeRemoteRefreshLoop(
561 cwd,
562 demandCwds,
563 automaticRemoteRefreshInterval,
564 refreshImmediately,
565 ).pipe(
566 Effect.forkIn(broadcasterScope),
567 Effect.map((fiber) => {
568 const nextPollers = new Map(activePollers);
569 nextPollers.set(cwd, {
570 fiber,
571 subscriberCount: 1,
572 demandCwds,
573 });
574 return [undefined, nextPollers] as const;
575 }),
576 ),
577 ),
578 );
579 });
580 });
581​
582 const releaseRemotePoller = Effect.fn("VcsStatusBroadcaster.releaseRemotePoller")(function* (
583 cwd: string,
584 demandCwd: string,
585 ) {
586 const pollerToInterrupt = yield* SynchronizedRef.modifyEffect(pollersRef, (activePollers) => {
587 const existing = activePollers.get(cwd);
588 if (!existing) {
589 return Effect.succeed([null, activePollers] as const);
590 }
591​
592 if (existing.subscriberCount > 1) {
593 return Ref.update(existing.demandCwds, (demandCwds) => {
594 const nextDemandCwds = new Map(demandCwds);
595 const count = nextDemandCwds.get(demandCwd) ?? 0;
596 if (count <= 1) {
597 nextDemandCwds.delete(demandCwd);
598 } else {
599 nextDemandCwds.set(demandCwd, count - 1);
600 }
601 return nextDemandCwds;
602 }).pipe(
603 Effect.as([
604 null,
605 new Map(activePollers).set(cwd, {
606 ...existing,
607 subscriberCount: existing.subscriberCount - 1,
608 }),
609 ] as const),
610 );
611 }
612​
613 return Effect.succeed([
614 existing.fiber,
615 new Map([...activePollers].filter(([activeCwd]) => activeCwd !== cwd)),
616 ] as const);
617 });
618​
619 if (pollerToInterrupt) {
620 yield* Fiber.interrupt(pollerToInterrupt).pipe(Effect.ignore);
621 }
622 });
623​
624 const streamStatus: VcsStatusBroadcaster["Service"]["streamStatus"] = (input, options) =>
625 Stream.unwrap(
626 Effect.gen(function* () {
627 const cwd = yield* withFileSystem(normalizeCwd(input.cwd));
628 const subscription = yield* PubSub.subscribe(changesPubSub);
629 const initialLocal = yield* getOrLoadLocalStatus(cwd);
630 const cachedStatus = yield* getCachedStatus(cwd);
631 const initialRemote = cachedStatus?.remote?.value ?? null;
632 yield* retainRemotePoller(
633 cwd,
634 input.cwd,
635 options?.automaticRemoteRefreshInterval ??
636 Effect.succeed(DEFAULT_VCS_STATUS_REFRESH_INTERVAL),
637 cachedStatus?.remote === null || cachedStatus?.remote === undefined,
638 );
639​
640 const release = releaseRemotePoller(cwd, input.cwd).pipe(Effect.ignore, Effect.asVoid);
641​
642 return Stream.concat(
643 Stream.make({
644 _tag: "snapshot" as const,
645 local: initialLocal,
646 remote: initialRemote,
647 }),
648 Stream.fromSubscription(subscription).pipe(
649 Stream.filter((event) => event.cwd === cwd),
650 Stream.map((event) => event.event),
651 ),
652 ).pipe(Stream.ensuring(release));
653 }),
654 );
655​
656 return VcsStatusBroadcaster.of({
657 getStatus,
658 refreshLocalStatus,
659 refreshStatus,
660 streamStatus,
661 });
662});
663​
664export const layer = Layer.effect(VcsStatusBroadcaster, make);