sailfork / t3code-github / apps/ server/ src/ pullRequest/ PullRequestReadCache.ts

import * as Cache from "effect/Cache";
import * as Clock from "effect/Clock";
import * as Equal from "effect/Equal";
import * as Hash from "effect/Hash";
import { PullRequestOperationError, PullRequestUnavailableError } from "@t3tools/contracts";
import * as Digest from "effect/crypto/Digest";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Hex from "effect/encoding/Hex";
import * as Context from "effect/Context";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schedule from "effect/Schedule";
import * as Schema from "effect/Schema";
import * as Semaphore from "effect/Semaphore";
import * as KeyValueStore from "effect/persistence/KeyValueStore";
import * as Persistable from "effect/persistence/Persistable";
import * as PersistedCache from "effect/persistence/PersistedCache";
import * as Persistence from "effect/persistence/Persistence";
import * as ServerConfig from "../config.ts";
import { detachStackFrame } from "../git/detachStackFrame.ts";
import { forkParked } from "../serverActivation.ts";

const CONCURRENT_READS = 512;
/** Invalidation excludes only reads whose scopes share its stripe, not every PR read. */
const SCOPE_STRIPES = 64;
// Entry filenames are the URI-encoded `<storeId>:<key>`; the scope revisions file has
// no store id. Effect before 4.1 wrote `<storeId><key>`, still pruned here.
const STORE_ID = "pr-v2";
const ENTRY_FILE_NAME = new RegExp(`^${STORE_ID}(?:%3A)?[0-9a-f]{64}

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	apps/server/src/pullRequest/PullRequestReadCache.ts at main · sailfork/t3code-github · Sailfork
	
		
); /** * A file untouched for a day is long expired; the day only leaves slack for * clock changes. */ const ENTRY_FILE_MAX_AGE = Duration.days(1); type ReadError = PullRequestOperationError | PullRequestUnavailableError; const revisionCodec = Schema.fromJsonString( Schema.Record( Schema.String, Schema.Struct({ revision: Schema.String, expiresAt: Schema.Finite }), ), ); class Read extends Persistable.Class<{ payload: { key: string; revision: string; lookup: Effect.Effect<string, ReadError> }; }>()("PullRequestRead", { primaryKey: ({ key }) => key, success: Schema.Struct({ payload: Schema.String, expiresAt: Schema.Finite, revision: Schema.optionalKey(Schema.String), }), error: Schema.Union([PullRequestOperationError, PullRequestUnavailableError]), }) { matchesRevision(revision: string | undefined): boolean { const stored = revision?.split(":") ?? []; return this.revision .split(":") .every((value, index) => value === "" || value === stored[index]); } [Equal.symbol](that: unknown): boolean { return that instanceof Read && that.key === this.key && that.revision === this.revision; } [Hash.symbol](): number { return Hash.string(`${this.key}:${this.revision}`); } } export class PullRequestReadCache extends Context.Service< PullRequestReadCache, { readonly get: ( key: string, lookup: Effect.Effect<string, ReadError>, scopes?: ReadonlyArray<string>, ) => Effect.Effect<string, ReadError>; readonly invalidate: (scope: string) => Effect.Effect<void>; } >()("t3/pullRequest/PullRequestReadCache") {} export const make = Effect.gen(function* () { const backing = yield* KeyValueStore.KeyValueStore; const random = yield* SecureRandom.SecureRandom; const digester = yield* Digest.Digest; const clock = yield* Clock.Clock; let enabled = true; const lock = yield* Semaphore.make(CONCURRENT_READS); const stripes = yield* Effect.forEach(Array.from({ length: SCOPE_STRIPES }), () => Semaphore.make(CONCURRENT_READS), ); const stripeOf = (scope: string) => (Hash.string(scope) >>> 0) % SCOPE_STRIPES; const revisionWrites = yield* Semaphore.make(1); const digest = (key: string) => digester.digest(Digest.sha256, new TextEncoder().encode(key)).pipe(Effect.map(Hex.encode)); const revisions = yield* Cache.makeWith( () => backing.get("revisions").pipe( Effect.flatMap((raw) => Schema.decodeEffect(revisionCodec)(raw ?? "{}")), Effect.catchTags({ SchemaError: Effect.fn("PullRequestReadCache.recoverRevisions")(function* () { // Without scope revisions, persisted PR entries cannot be trusted. yield* backing.clear; yield* Effect.logWarning("PR cache reset after invalid scope revisions"); return yield* Schema.decodeEffect(revisionCodec)("{}"); }), }), ), { capacity: 1, timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero), }, ); const timeToLive: Persistable.TimeToLiveFn<Read> = (exit) => Exit.isSuccess(exit) ? Duration.millis(Math.max(0, exit.value.expiresAt - clock.currentTimeMillisUnsafe())) : Duration.zero; const cache = yield* PersistedCache.make( (request: Read) => request.lookup.pipe( Effect.map((payload) => ({ payload, expiresAt: clock.currentTimeMillisUnsafe() + 60_000, revision: request.revision, })), ), { storeId: STORE_ID, timeToLive, inMemoryTTL: timeToLive, inMemoryCapacity: CONCURRENT_READS, }, ).pipe(Effect.provide(Persistence.layerKvs)); const refreshes = yield* Cache.makeWith( Effect.fn("PullRequestReadCache.refresh")(function* (request: Read) { const stored = yield* cache.get(request); if (request.matchesRevision(stored.revision)) return stored; yield* cache.invalidate(request); return yield* cache.get(request); }), { capacity: CONCURRENT_READS, timeToLive: () => Duration.zero }, ); // A cache entry keeps the fiber that filled it, whose lazy stack frames would keep the caller's // data (e.g. a whole sync sweep) alive until eviction. return PullRequestReadCache.of({ get: Effect.fn("PullRequestReadCache.get")(function* (key, lookup, scopes = []) { if (!enabled) return yield* lookup; const read = yield* Effect.cached(lookup); return yield* Effect.gen(function* () { const current = yield* Cache.get(revisions, undefined); const now = clock.currentTimeMillisUnsafe(); const revision = scopes .map((scope) => { const value = current[scope]; return value !== undefined && value.expiresAt > now ? value.revision : ""; }) .join(":"); const request = new Read({ key: yield* digest(key), revision, lookup: read }); const stored = yield* cache.get(request); return ( request.matchesRevision(stored.revision) ? stored : yield* Cache.get(refreshes, request) ).payload; }).pipe( Effect.catchTags({ KeyValueStoreError: () => read, PersistenceError: () => read, SchemaError: () => read, }), // A read stores its result under the revisions it started with, so an invalidation of // one of its scopes must wait for it; reads of other scopes run on. (effect) => [...new Set(scopes.map(stripeOf))] .toSorted((left, right) => left - right) .reduceRight((inner, stripe) => stripes[stripe]!.withPermits(1)(inner), effect), lock.withPermits(1), ); }, detachStackFrame), invalidate: Effect.fn("PullRequestReadCache.invalidate")( function* (scope) { const now = clock.currentTimeMillisUnsafe(); const current = yield* Cache.get(revisions, undefined); const next = Object.fromEntries( Object.entries(current).filter(([, value]) => value.expiresAt > now), ); next[scope] = { revision: yield* random.uuidV4(), expiresAt: now + 60_000 }; const encoded = yield* Schema.encodeEffect(revisionCodec)(next); yield* backing.set("revisions", encoded); yield* Cache.set(revisions, undefined, next); }, Effect.catch(() => { enabled = false; return Effect.logWarning("PR cache disabled after clearing failed"); }), Effect.uninterruptible, revisionWrites.withPermits(1), (effect, scope) => stripes[stripeOf(scope)]!.withPermits(CONCURRENT_READS)(effect), detachStackFrame, ), }); }); /** * The persisted cache drops an expired entry only when it is read again, so * files for PRs nobody reopens would otherwise stay forever. */ export const pruneExpiredEntryFiles = Effect.fn("PullRequestReadCache.pruneExpiredEntryFiles")( function* (directory: string) { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const cutoff = (yield* Clock.currentTimeMillis) - Duration.toMillis(ENTRY_FILE_MAX_AGE); const entries = (yield* fileSystem.readDirectory(directory)).filter((name) => ENTRY_FILE_NAME.test(name), ); const [, failures] = yield* Effect.partition(entries, (name) => { const entryPath = path.join(directory, name); return fileSystem.stat(entryPath).pipe( Effect.flatMap((info) => Option.exists(info.mtime, (mtime) => mtime.getTime() < cutoff) ? fileSystem.remove(entryPath) : Effect.void, ), Effect.catchReason("PlatformError", "NotFound", () => Effect.void), ); }); if (failures.length > 0) { yield* Effect.logWarning("Failed to prune some PR cache files", { failed: failures.length, cause: failures[0], }); } }, ); export const layer = Layer.unwrap( Effect.gen(function* () { const config = yield* ServerConfig.ServerConfig; const path = yield* Path.Path; const directory = path.join(config.providerStatusCacheDir, "pull-requests"); return Layer.effect(PullRequestReadCache, make).pipe( Layer.provide( // Memoize cleanup acquisition with the store; Layer.tap runs per consumer. Layer.effectDiscard( forkParked( pruneExpiredEntryFiles(directory).pipe( Effect.catch((cause) => Effect.logWarning("Failed to prune PR cache files", { cause }), ), Effect.repeat(Schedule.spaced(Duration.hours(1))), ), ), ).pipe( Layer.provideMerge(KeyValueStore.layerFileSystem(directory)), Layer.catch(() => Layer.effectDiscard( Effect.logWarning("PR cache directory unavailable; using memory cache"), ).pipe(Layer.provideMerge(KeyValueStore.layerMemory)), ), ), ), ); }), );
apps/server/src/pullRequest/PullRequestReadCache.ts at main · sailfork/t3code-github · Sailfork
);\n/**\n * A file untouched for a day is long expired; the day only leaves slack for\n * clock changes.\n */\nconst ENTRY_FILE_MAX_AGE = Duration.days(1);\ntype ReadError = PullRequestOperationError | PullRequestUnavailableError;\nconst revisionCodec = Schema.fromJsonString(\n Schema.Record(\n Schema.String,\n Schema.Struct({ revision: Schema.String, expiresAt: Schema.Finite }),\n ),\n);\nclass Read extends Persistable.Class\u003c{\n payload: { key: string; revision: string; lookup: Effect.Effect\u003cstring, ReadError> };\n}>()(\"PullRequestRead\", {\n primaryKey: ({ key }) => key,\n success: Schema.Struct({\n payload: Schema.String,\n expiresAt: Schema.Finite,\n revision: Schema.optionalKey(Schema.String),\n }),\n error: Schema.Union([PullRequestOperationError, PullRequestUnavailableError]),\n}) {\n matchesRevision(revision: string | undefined): boolean {\n const stored = revision?.split(\":\") ?? [];\n return this.revision\n .split(\":\")\n .every((value, index) => value === \"\" || value === stored[index]);\n }\n\n [Equal.symbol](that: unknown): boolean {\n return that instanceof Read && that.key === this.key && that.revision === this.revision;\n }\n [Hash.symbol](): number {\n return Hash.string(`${this.key}:${this.revision}`);\n }\n}\n\nexport class PullRequestReadCache extends Context.Service\u003c\n PullRequestReadCache,\n {\n readonly get: (\n key: string,\n lookup: Effect.Effect\u003cstring, ReadError>,\n scopes?: ReadonlyArray\u003cstring>,\n ) => Effect.Effect\u003cstring, ReadError>;\n readonly invalidate: (scope: string) => Effect.Effect\u003cvoid>;\n }\n>()(\"t3/pullRequest/PullRequestReadCache\") {}\n\nexport const make = Effect.gen(function* () {\n const backing = yield* KeyValueStore.KeyValueStore;\n const random = yield* SecureRandom.SecureRandom;\n const digester = yield* Digest.Digest;\n const clock = yield* Clock.Clock;\n let enabled = true;\n const lock = yield* Semaphore.make(CONCURRENT_READS);\n const stripes = yield* Effect.forEach(Array.from({ length: SCOPE_STRIPES }), () =>\n Semaphore.make(CONCURRENT_READS),\n );\n const stripeOf = (scope: string) => (Hash.string(scope) >>> 0) % SCOPE_STRIPES;\n const revisionWrites = yield* Semaphore.make(1);\n const digest = (key: string) =>\n digester.digest(Digest.sha256, new TextEncoder().encode(key)).pipe(Effect.map(Hex.encode));\n const revisions = yield* Cache.makeWith(\n () =>\n backing.get(\"revisions\").pipe(\n Effect.flatMap((raw) => Schema.decodeEffect(revisionCodec)(raw ?? \"{}\")),\n Effect.catchTags({\n SchemaError: Effect.fn(\"PullRequestReadCache.recoverRevisions\")(function* () {\n // Without scope revisions, persisted PR entries cannot be trusted.\n yield* backing.clear;\n yield* Effect.logWarning(\"PR cache reset after invalid scope revisions\");\n return yield* Schema.decodeEffect(revisionCodec)(\"{}\");\n }),\n }),\n ),\n {\n capacity: 1,\n timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),\n },\n );\n const timeToLive: Persistable.TimeToLiveFn\u003cRead> = (exit) =>\n Exit.isSuccess(exit)\n ? Duration.millis(Math.max(0, exit.value.expiresAt - clock.currentTimeMillisUnsafe()))\n : Duration.zero;\n const cache = yield* PersistedCache.make(\n (request: Read) =>\n request.lookup.pipe(\n Effect.map((payload) => ({\n payload,\n expiresAt: clock.currentTimeMillisUnsafe() + 60_000,\n revision: request.revision,\n })),\n ),\n {\n storeId: STORE_ID,\n timeToLive,\n inMemoryTTL: timeToLive,\n inMemoryCapacity: CONCURRENT_READS,\n },\n ).pipe(Effect.provide(Persistence.layerKvs));\n const refreshes = yield* Cache.makeWith(\n Effect.fn(\"PullRequestReadCache.refresh\")(function* (request: Read) {\n const stored = yield* cache.get(request);\n if (request.matchesRevision(stored.revision)) return stored;\n yield* cache.invalidate(request);\n return yield* cache.get(request);\n }),\n { capacity: CONCURRENT_READS, timeToLive: () => Duration.zero },\n );\n // A cache entry keeps the fiber that filled it, whose lazy stack frames would keep the caller's\n // data (e.g. a whole sync sweep) alive until eviction.\n return PullRequestReadCache.of({\n get: Effect.fn(\"PullRequestReadCache.get\")(function* (key, lookup, scopes = []) {\n if (!enabled) return yield* lookup;\n const read = yield* Effect.cached(lookup);\n return yield* Effect.gen(function* () {\n const current = yield* Cache.get(revisions, undefined);\n const now = clock.currentTimeMillisUnsafe();\n const revision = scopes\n .map((scope) => {\n const value = current[scope];\n return value !== undefined && value.expiresAt > now ? value.revision : \"\";\n })\n .join(\":\");\n const request = new Read({ key: yield* digest(key), revision, lookup: read });\n const stored = yield* cache.get(request);\n return (\n request.matchesRevision(stored.revision) ? stored : yield* Cache.get(refreshes, request)\n ).payload;\n }).pipe(\n Effect.catchTags({\n KeyValueStoreError: () => read,\n PersistenceError: () => read,\n SchemaError: () => read,\n }),\n // A read stores its result under the revisions it started with, so an invalidation of\n // one of its scopes must wait for it; reads of other scopes run on.\n (effect) =>\n [...new Set(scopes.map(stripeOf))]\n .toSorted((left, right) => left - right)\n .reduceRight((inner, stripe) => stripes[stripe]!.withPermits(1)(inner), effect),\n lock.withPermits(1),\n );\n }, detachStackFrame),\n invalidate: Effect.fn(\"PullRequestReadCache.invalidate\")(\n function* (scope) {\n const now = clock.currentTimeMillisUnsafe();\n const current = yield* Cache.get(revisions, undefined);\n const next = Object.fromEntries(\n Object.entries(current).filter(([, value]) => value.expiresAt > now),\n );\n next[scope] = { revision: yield* random.uuidV4(), expiresAt: now + 60_000 };\n const encoded = yield* Schema.encodeEffect(revisionCodec)(next);\n yield* backing.set(\"revisions\", encoded);\n yield* Cache.set(revisions, undefined, next);\n },\n Effect.catch(() => {\n enabled = false;\n return Effect.logWarning(\"PR cache disabled after clearing failed\");\n }),\n Effect.uninterruptible,\n revisionWrites.withPermits(1),\n (effect, scope) => stripes[stripeOf(scope)]!.withPermits(CONCURRENT_READS)(effect),\n detachStackFrame,\n ),\n });\n});\n\n/**\n * The persisted cache drops an expired entry only when it is read again, so\n * files for PRs nobody reopens would otherwise stay forever.\n */\nexport const pruneExpiredEntryFiles = Effect.fn(\"PullRequestReadCache.pruneExpiredEntryFiles\")(\n function* (directory: string) {\n const fileSystem = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const cutoff = (yield* Clock.currentTimeMillis) - Duration.toMillis(ENTRY_FILE_MAX_AGE);\n const entries = (yield* fileSystem.readDirectory(directory)).filter((name) =>\n ENTRY_FILE_NAME.test(name),\n );\n const [, failures] = yield* Effect.partition(entries, (name) => {\n const entryPath = path.join(directory, name);\n return fileSystem.stat(entryPath).pipe(\n Effect.flatMap((info) =>\n Option.exists(info.mtime, (mtime) => mtime.getTime() \u003c cutoff)\n ? fileSystem.remove(entryPath)\n : Effect.void,\n ),\n Effect.catchReason(\"PlatformError\", \"NotFound\", () => Effect.void),\n );\n });\n if (failures.length > 0) {\n yield* Effect.logWarning(\"Failed to prune some PR cache files\", {\n failed: failures.length,\n cause: failures[0],\n });\n }\n },\n);\n\nexport const layer = Layer.unwrap(\n Effect.gen(function* () {\n const config = yield* ServerConfig.ServerConfig;\n const path = yield* Path.Path;\n const directory = path.join(config.providerStatusCacheDir, \"pull-requests\");\n return Layer.effect(PullRequestReadCache, make).pipe(\n Layer.provide(\n // Memoize cleanup acquisition with the store; Layer.tap runs per consumer.\n Layer.effectDiscard(\n forkParked(\n pruneExpiredEntryFiles(directory).pipe(\n Effect.catch((cause) =>\n Effect.logWarning(\"Failed to prune PR cache files\", { cause }),\n ),\n Effect.repeat(Schedule.spaced(Duration.hours(1))),\n ),\n ),\n ).pipe(\n Layer.provideMerge(KeyValueStore.layerFileSystem(directory)),\n Layer.catch(() =>\n Layer.effectDiscard(\n Effect.logWarning(\"PR cache directory unavailable; using memory cache\"),\n ).pipe(Layer.provideMerge(KeyValueStore.layerMemory)),\n ),\n ),\n ),\n );\n }),\n);\n","preview":null,"history":null,"version":"7f03beef3a245c837493d26261e28e30150ad942"},"/_session?shell=1":null}