cache.ts
This file
Path
packages/project/src/code/cache.ts
144 lines · 4.4 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
The walk stopped after 200 commits: lines older than fc4691d point at it.
Merge build/1003oscabrielbefore adoptione8561dc
lines 1–3why, one step back1import { inRepoSession } from "@repo/artifacts/repo-host";2import { Cache, Context, Deferred, Effect, Exit, Layer, Option } from "effect";3import type { Cause, Scope } from "effect";Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
line 4why, one step back4Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 5–9why, one step back5export interface Keeping<A> {6 readonly heavy?: boolean;7 readonly keep?: (value: A) => boolean;8}9Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 10–12why, one step back10export interface CodeReads {11 readonly through: <A, E, R>(12 key: string,Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 13–14why, one step back13 read: Effect.Effect<A, E, R>,14 keeping?: Keeping<A>Add the code-browser, policy and drafting contracts, peeled tags and commit times on the port, and the AI bindingoscabrielbefore adoptionfc4691d
lines 15–20why, one step back15 ) => Effect.Effect<A, E, R>;16}1718export const CodeCache = Context.Reference<CodeReads>("project/CodeCache", {19 defaultValue: () => ({ through: (_key, read) => read }),20});Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 21why, one step back21Keep only finished reads in the code cache, so a cancelled request never hangs the nextoscabrielbefore adoption0e4a6e9
line 22why, one step back22const always = () => true;Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 23–24why, one step back2324const cacheOf = (capacity: number) =>Keep only finished reads in the code cache, so a cancelled request never hangs the nextoscabrielbefore adoption0e4a6e9
line 25why, one step back25 Cache.make<string, unknown>({Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 26why, one step back26 capacity,Keep only finished reads in the code cache, so a cancelled request never hangs the nextoscabrielbefore adoption0e4a6e9
line 27why, one step back27 lookup: (key) => Effect.die(`the code cache never looks up ${key}`),Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 28–29why, one step back28 });29Merge build/comments-project into mainoscabrielbefore adoption49efd2a
lines 30–31why, one step back30// A read under way is never shared: a Worker whose client goes away is cancelled31// without interruption, so a joined read may never end and its joiner would hang.Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 32why, one step back32const throughCache =Keep only finished reads in the code cache, so a cancelled request never hangs the nextoscabrielbefore adoption0e4a6e9
line 33why, one step back33 (cache: Cache.Cache<string, unknown>) =>Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 34–39why, one step back34 <A, E, R>(35 key: string,36 read: Effect.Effect<A, E, R>,37 keep: (value: A) => boolean38 ) =>39 Effect.gen(function* () {Keep only finished reads in the code cache, so a cancelled request never hangs the nextoscabrielbefore adoption0e4a6e9
line 40why, one step back40 const kept = yield* Cache.getSuccess(cache, key);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 41why, one step back41Keep only finished reads in the code cache, so a cancelled request never hangs the nextoscabrielbefore adoption0e4a6e9
lines 42–47why, one step back42 if (Option.isSome(kept)) {43 // SAFETY: every key names one kind of read (`<repo>:<kind>:<id>`), so the44 // value kept under it is what a read typed as this one gave.45 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- one cache holds every kind of read, so its values are typed `unknown`; see SAFETY.46 return kept.value as A;47 }Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 48why, one step back48Keep only finished reads in the code cache, so a cancelled request never hangs the nextoscabrielbefore adoption0e4a6e9
line 49why, one step back49 const value = yield* read;Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 50why, one step back50Keep only finished reads in the code cache, so a cancelled request never hangs the nextoscabrielbefore adoption0e4a6e9
lines 51–52why, one step back51 if (keep(value)) {52 yield* Cache.set(cache, key, value);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 53–54why, one step back53 }54Keep only finished reads in the code cache, so a cancelled request never hangs the nextoscabrielbefore adoption0e4a6e9
line 55why, one step back55 return value;Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 56–81why, one step back56 });5758export const CodeCacheLive = (59 capacity: number,60 heavy = Math.max(1, Math.ceil(capacity / 50))61) =>62 Layer.effect(63 CodeCache,64 Effect.gen(function* () {65 const light = throughCache(yield* cacheOf(capacity));66 const weighty = throughCache(yield* cacheOf(heavy));6768 const reads: CodeReads = {69 through: <A, E, R>(70 key: string,71 read: Effect.Effect<A, E, R>,72 keeping: Keeping<A> = {}73 ) =>74 (keeping.heavy === true ? weighty : light)(75 key,76 read,77 keeping.keep ?? always78 ),79 };8081 return reads;Merge build/1003oscabrielbefore adoptione8561dc
lines 82–142why, one step back82 })83 );8485const joined = <B, F>(86 under: Deferred.Deferred<Exit.Exit<unknown, unknown>>87): Effect.Effect<B, F> =>88 Effect.flatMap(Deferred.await(under), (exit) => {89 if (Exit.isSuccess(exit)) {90 // SAFETY: every key names one kind of read (`<repo>:<kind>:<id>`), so91 // the read under it was typed as the one that joins it.92 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- one view shares every kind of read, so its map holds `unknown`; see SAFETY.93 return Effect.succeed(exit.value as B);94 }9596 // SAFETY: as above, for the read's failure.97 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- one view shares every kind of read, so its map holds `unknown`; see SAFETY.98 return Effect.failCause(exit.cause as Cause.Cause<F>);99 });100101export const asOneView = <A, E, R>(102 effect: Effect.Effect<A, E, R>103): Effect.Effect<A, E, Exclude<R, Scope.Scope>> =>104 inRepoSession(105 Effect.gen(function* () {106 const outer = yield* CodeCache;107 const scope = yield* Effect.scope;108109 const reading = new Map<110 string,111 Deferred.Deferred<Exit.Exit<unknown, unknown>>112 >();113114 const shared: CodeReads = {115 through: <B, F, S>(116 key: string,117 read: Effect.Effect<B, F, S>,118 keeping: Keeping<B> = {}119 ) =>120 keeping.heavy === true || keeping.keep !== undefined121 ? outer.through(key, read, keeping)122 : Effect.suspend(() => {123 const found = reading.get(key);124125 if (found !== undefined) {126 return joined<B, F>(found);127 }128129 const done = Deferred.makeUnsafe<Exit.Exit<unknown, unknown>>();130131 reading.set(key, done);132133 return Effect.forkIn(134 Effect.exit(outer.through(key, read, keeping)).pipe(135 Effect.flatMap((exit) => Deferred.succeed(done, exit))136 ),137 scope138 ).pipe(Effect.andThen(joined<B, F>(done)));139 }),140 };141142 return yield* Effect.provideService(effect, CodeCache, shared);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 143–144why, one step back143 })144 );