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

vcsRefInvalidation.ts

129 lines4.4 KB

1import type { EnvironmentId } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as PartitionedSemaphore from "effect/PartitionedSemaphore";
4import { Atom, type AtomRegistry } from "effect/reactivity";
5​
6import { safeErrorLogAttributes } from "../errors/safeLog.ts";
7import * as Persistence from "../platform/persistence.ts";
8​
9export interface VcsRefsInvalidationTarget {
10 readonly environmentId: EnvironmentId;
12​
13export interface CachedVcsRefsInvalidationTarget extends VcsRefsInvalidationTarget {
14 readonly cwd: string;
16​
17export interface VcsRefsCacheState {
18 readonly revision: number;
19 readonly persistedCacheReadable: boolean;
21​
22const stateByEnvironment = Atom.family((environmentId: EnvironmentId) =>
23 Atom.make<VcsRefsCacheState>({
24 revision: 0,
25 persistedCacheReadable: true,
26 }).pipe(Atom.keepAlive, Atom.withLabel(`environment-data:vcs:list-refs-state:${environmentId}`)),
27);
28const persistenceLock = PartitionedSemaphore.makeUnsafe<EnvironmentId>({ permits: 1 });
29​
30export function vcsRefsCacheStateAtom(target: VcsRefsInvalidationTarget) {
31 return stateByEnvironment(target.environmentId);
33​
34export function invalidateVcsRefs(
35 registry: AtomRegistry.AtomRegistry,
36 target: VcsRefsInvalidationTarget,
37 persistedCacheReadable?: boolean,
38): void {
39 registry.update(vcsRefsCacheStateAtom(target), (state) => ({
40 revision: state.revision + 1,
41 persistedCacheReadable: persistedCacheReadable ?? state.persistedCacheReadable,
42 }));
44​
45export function withVcsRefsPersistenceLock<A, E, R>(
46 environmentId: EnvironmentId,
47 effect: Effect.Effect<A, E, R>,
48): Effect.Effect<A, E, R> {
49 return persistenceLock.withPermit(environmentId)(effect);
51​
52/**
53 * Clearing first prevents the restarted stream from rehydrating the invalidated
54 * snapshot. A refresh that wins the lock is cleared afterward; one that loses
55 * observes the new revision and cannot repersist its pre-mutation result.
56 */
57const clearCachedVcsRefs = Effect.fn("VcsRefsState.invalidateCached")(function* (
58 registry: AtomRegistry.AtomRegistry,
59 target: CachedVcsRefsInvalidationTarget,
60 updateDigests: () => void,
61) {
62 const cache = yield* Persistence.EnvironmentCacheStore;
63 yield* withVcsRefsPersistenceLock(
64 target.environmentId,
65 Effect.gen(function* () {
66 const persistedCacheReadable = yield* cache.clearVcsRefs(target.environmentId).pipe(
67 Effect.as(true),
68 Effect.catch((error) =>
69 Effect.logWarning("Could not remove invalidated cached Git refs.").pipe(
70 Effect.annotateLogs({
71 environmentId: target.environmentId,
72 cwd: target.cwd,
73 ...safeErrorLogAttributes(error),
74 }),
75 Effect.as(false),
76 ),
77 ),
78 );
79 updateDigests();
80 invalidateVcsRefs(registry, target, persistedCacheReadable);
81 }),
82 );
83});
84​
85export const invalidateCachedVcsRefs = (
86 registry: AtomRegistry.AtomRegistry,
87 target: CachedVcsRefsInvalidationTarget,
88) =>
89 clearCachedVcsRefs(registry, target, () =>
90 registry.set(refsDigestsByEnvironment(target.environmentId), new Map()),
91 );
92​
93// Ref digests from each cwd's last status refresh; cleared when ref-changing commands settle.
94const refsDigestsByEnvironment = Atom.family((environmentId: EnvironmentId) =>
95 Atom.make<ReadonlyMap<string, string>>(new Map()).pipe(
96 Atom.keepAlive,
97 Atom.withLabel(`environment-data:vcs:refs-digests:${environmentId}`),
98 ),
99);
100​
101/** Skips the branch list reload when a status refresh saw the same refs as the last one. */
102export const invalidateCachedVcsRefsIfChanged = Effect.fn("VcsRefsState.invalidateCachedIfChanged")(
103 function* (
104 registry: AtomRegistry.AtomRegistry,
105 target: CachedVcsRefsInvalidationTarget,
106 refsDigest: string | undefined,
107 startedAtRevision?: number,
108 ) {
109 const digestsAtom = refsDigestsByEnvironment(target.environmentId);
110 const isStale = () =>
111 startedAtRevision !== undefined &&
112 registry.get(vcsRefsCacheStateAtom(target)).revision !== startedAtRevision;
113 if (
114 refsDigest !== undefined &&
115 registry.get(digestsAtom).get(target.cwd) === refsDigest &&
116 !isStale()
117 ) {
118 return;
119 }
120 yield* clearCachedVcsRefs(registry, target, () => {
121 registry.update(digestsAtom, (digests) => {
122 const next = new Map(digests);
123 if (refsDigest === undefined || isStale()) next.delete(target.cwd);
124 else next.set(target.cwd, refsDigest);
125 return next;
126 });
127 });
128 },
129);