Read one view's repo through one session, and keep stored history with the heavy reads
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Review fixes for 1003: - A cold why opened two repo sessions (the walk's in asOneView, then viewOf's), and a path's log read its scan outside any session, so a view asked the binding for two handles, against "one repo handle per view". inRepoSession now joins an enclosing session, and whyView and logPage each run whole in one. - Kept whys and path logs entered the isolate cache's light side, which is bounded by count for few-KiB values; a why's ranges grow with its file (up to 1 MiB). recall and remember now keep them with the heavy reads. - The rate-limit retry test now refuses exactly four asks before reading, the boundary beside the five that fail. - The path log's once-per-commit test comment now says what its count holds: the store's tree reads (the view shares a read under way). Tests added: - code-history.test.ts: a cold why and a cold path's log page read every commit, tree, blob and log in one session; kept history goes through the cache as heavy. - binding.test.ts: a session inside another reads over the outer's handle (one get, one dispose).
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+198 added−28 removed
- tests:+175 added−18 removed
- other:+23 added−10 removed
Provenance
- Commit
81a3dda- Tree
715fd91- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent
01199d6
6 files changed+198 added−28 removed
modified:packages/artifacts/src/repo-host.ts+7 added-1 removed
266 unmodified lines267 );268 269 /**270 * `effect` with its reads in one session: what a host holds for them is271- * released when `effect` ends, whatever it came to.271+ * released when `effect` ends, whatever it came to. Inside a session272+ * already, `effect` joins it, so a view whose parts each ask for a273+ * session still reads over one handle.274 */275 export const inRepoSession = <A, E, R>(276 effect: Effect.Effect<A, E, R>277 ): Effect.Effect<A, E, Exclude<R, Scope.Scope>> =>278 Effect.scoped(279 Effect.gen(function* () {280+ if (Option.isSome(yield* RepoSession)) {281+ return yield* effect;282+ }283+284 const scope = yield* Effect.scope;285 const opening = yield* Semaphore.make(1);286 const held = new Map<string, unknown>();287 27 unmodified linesmodified:packages/artifacts/test/binding.test.ts+42 added-14 removed
1034 unmodified lines1035 })1036 )1037 );1038 1039- it.effect("asks again after a rate-limited read, then reads", () =>1040- within(1041- () =>1039+ it.live(1040+ "reads in a session inside another over the outer session's handle",1041+ () =>1042+ within(({ counts }) =>1043 Effect.gen(function* () {1044 const repos = yield* RepoHost;1044- const name = RepoName.make("limited");1045+ const name = RepoName.make("nested");1046 const { first } = yield* seeded(name);1046- const read = yield* onTestClock(repos.readCommit(name, first));1047+ const before = { ...counts };1048 1048- expect(1049- Exit.map(1050- read,1051- Option.map(({ sha }) => sha)1052- )1053- ).toStrictEqual(Exit.succeed(Option.some(first)));1054- }),1055- { rateLimited: 3 }1056- )1049+ yield* inRepoSession(1050+ Effect.all([1051+ repos.readCommit(name, first),1052+ inRepoSession(repos.log(name, at, 5)),1053+ repos.readFile(name, at, RepoPath.make("a.txt")),1054+ ])1055+ ).pipe(Effect.orDie);1056+1057+ expect([1058+ counts.gets - before.gets,1059+ counts.disposed - before.disposed,1060+ ]).toStrictEqual([1, 1]);1061+ })1062+ )1063 );1064 1065 it.effect(1066+ "asks again after each of four rate-limited reads, then reads",1067+ () =>1068+ within(1069+ () =>1070+ Effect.gen(function* () {1071+ const repos = yield* RepoHost;1072+ const name = RepoName.make("limited");1073+ const { first } = yield* seeded(name);1074+ const read = yield* onTestClock(repos.readCommit(name, first));1075+1076+ expect(1077+ Exit.map(1078+ read,1079+ Option.map(({ sha }) => sha)1080+ )1081+ ).toStrictEqual(Exit.succeed(Option.some(first)));1082+ }),1083+ { rateLimited: 4 }1084+ )1085+ );1086+1087+ it.effect(1088 "fails the read once four asks after the first were all rate limited",1089 () =>1090 within(1091 () =>12 unmodified linesmodified:packages/project/src/code/history.ts+5 added-4 removed
14 unmodified lines15161718192019202122232425102 unmodified linesExpand all12812913013113113213313413513610 unmodified lines14714814915015015115215315415513 unmodified lines14 unmodified lines * Values: ids and ranges, encoded with Schema; never a file's text, and * never a landing (each view asks the project's record for those, so a * later landing shows at once). A value that fails to decode is a miss. * * Read order: the isolate's `CodeCache`, then the `CodeStore`, then the * walk; what a walk computed is put in both. A walk may start from what is * Read order: the isolate's `CodeCache` (with the heavy reads: a why's * ranges grow with its file), then the `CodeStore`, then the walk; what a * walk computed is put in both. A walk may start from what is * kept at a first-parent ancestor at most `INCREMENT_REACH` commits back, * so a new landing on main costs a handful of reads. */import { WhyRange } from "@repo/protocol/domain/code";102 unmodified linesExpand all
return yield* cache.through( key, Effect.map(store.get(key), Option.flatMap(decode)), { keep: Option.isSome } { heavy: true, keep: Option.isSome } ); });
/** `value` kept under `key`, in the isolate's cache and in the store. */10 unmodified lines Schema.fromJsonString(kept.schema) )(value).pipe(Effect.orDie);
yield* store.put(key, encoded); yield* cache.through(key, Effect.succeedSome(value), {}); yield* cache.through(key, Effect.succeedSome(value), { heavy: true }); });
/** * What is kept under `key`, else `compute` run and its value kept: a13 unmodified linesmodified:packages/project/src/code/log.ts+5 added-2 removed
14 unmodified lines15 * changed the path, cut to the scan; else each commit of the scan has its16 * entry looked up once (`READS_AT_ONCE` under way, in one ordered pass),17 * and neighbours compared.18 */19-import { Revision, RepoHost } from "@repo/artifacts/repo-host";19+import { inRepoSession, Revision, RepoHost } from "@repo/artifacts/repo-host";20 import type {21 Commit,22 RepoNotFound,23 TreeEntry,306 unmodified lines330 return pageWith(331 commits,332 scan.length > PATH_SCAN ? Option.some(from + PATH_SCAN) : Option.none()333 );334- });334+ }).pipe(335+ // The scan and the walk over it: one session, one handle.336+ inRepoSession337+ );modified:packages/project/src/code/why.ts+6 added-3 removed
512 unmodified lines513 key,514 asOneView(computed(repo, commit, path, scan))515 );516 517- // The commits it names are read through one session, as the walk's are.518- return yield* inRepoSession(viewOf(project, commit, path, kept));519- }).pipe(Effect.catchTag("RepoNotFound", Effect.die));517+ return yield* viewOf(project, commit, path, kept);518+ }).pipe(519+ // The walk and the commits it names: one session, one handle.520+ inRepoSession,521+ Effect.catchTag("RepoNotFound", Effect.die)522+ );modified:packages/project/test/code-history.test.ts+133 added-4 removed
7 unmodified lines8 * many reads under way and looks each commit's entry up once; a value9 * that no longer decodes is computed again and replaced.10 */11 import { describe, expect, it } from "@effect/vitest";12-import { RepoHost } from "@repo/artifacts/repo-host";12+import { RepoHost, RepoSession } from "@repo/artifacts/repo-host";13+import type { RepoSessionApi } from "@repo/artifacts/repo-host";14 import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";15 import type { LandingView } from "@repo/protocol/domain/landing";16 import {17 AskId,9 unmodified lines27 import { CodeStore } from "../src/code-store.ts";28 import { CodeStoreCounts, CodeStoreMemory } from "../src/code-store/memory.ts";29 import { logOf, whyOf } from "../src/code.ts";30 import type { CodeProject } from "../src/code.ts";30-import { CodeCacheLive } from "../src/code/cache.ts";31+import { CodeCache, CodeCacheLive } from "../src/code/cache.ts";32+import type { CodeReads } from "../src/code/cache.ts";33 import { keyOf, StoredWhy, whyKept } from "../src/code/history.ts";34 import { WHY_SCAN } from "../src/code/why.ts";35 import { Content, writer } from "./code-fixtures.ts";36 import type { Tree } from "./code-fixtures.ts";489 unmodified lines526 path: Option.some(RepoPath.make("count.txt")),527 });528 529 expect(commits).toHaveLength(30);528- // Each commit's root tree once: no commit is looked up again as529- // the one after it's parent.530+ // Each commit's root tree read once from the store. The view531+ // shares a tree read under way among its fibers, so this count532+ // holds the store's reads, not how often the walk asked.533 expect((yield* Ref.get(seen)).trees).toBe(200);534 })535 ),536 long34 unmodified lines571 })572 )573 );574 });575+576+/**577+ * The memory store, the session each commit, tree, blob and log read ran578+ * in noted in `sessions`, in order.579+ */580+const sessioned = (581+ sessions: Ref.Ref<readonly Option.Option<RepoSessionApi>[]>582+) => {583+ const noted = <A, E>(read: Effect.Effect<A, E>) =>584+ Effect.flatMap(RepoSession, (session) =>585+ Effect.andThen(586+ Ref.update(sessions, (was) => [...was, session]),587+ read588+ )589+ );590+591+ return Layer.effect(592+ RepoHost,593+ Effect.gen(function* () {594+ const host = yield* RepoHost;595+596+ return RepoHost.of({597+ ...host,598+ log: (name, at, limit, offset) =>599+ noted(host.log(name, at, limit, offset)),600+ readBlob: (name, id) => noted(host.readBlob(name, id)),601+ readCommit: (name, sha) => noted(host.readCommit(name, sha)),602+ readTree: (name, id) => noted(host.readTree(name, id)),603+ });604+ })605+ ).pipe(Layer.provide(RepoHostMemory));606+};607+608+/** How many sessions `sessions` names, and whether a read ran in none. */609+const sessionsIn = (sessions: readonly Option.Option<RepoSessionApi>[]) => ({610+ outside: sessions.some(Option.isNone),611+ sessions: new Set(sessions.flatMap(Option.toArray)).size,612+});613+614+describe("one view's reads", () => {615+ it.effect("of a cold why all run in one session", () =>616+ Effect.gen(function* () {617+ const sessions = yield* Ref.make<618+ readonly Option.Option<RepoSessionApi>[]619+ >([]);620+621+ yield* Effect.gen(function* () {622+ const { grown } = yield* history;623+ const { code } = yield* recordOf(grown);624+625+ yield* Ref.set(sessions, []);626+ yield* onIsolate(whyOf(code, Option.none(), format));627+ }).pipe(628+ Effect.provide(Layer.merge(sessioned(sessions), CodeStoreMemory))629+ );630+631+ expect(sessionsIn(yield* Ref.get(sessions))).toStrictEqual({632+ outside: false,633+ sessions: 1,634+ });635+ })636+ );637+638+ it.effect("of a cold path's log page all run in one session", () =>639+ Effect.gen(function* () {640+ const sessions = yield* Ref.make<641+ readonly Option.Option<RepoSessionApi>[]642+ >([]);643+644+ yield* Effect.gen(function* () {645+ const { grown } = yield* history;646+ const { code } = yield* recordOf(grown);647+648+ yield* Ref.set(sessions, []);649+ yield* onIsolate(logOf(code, formatLog(grown)));650+ }).pipe(651+ Effect.provide(Layer.merge(sessioned(sessions), CodeStoreMemory))652+ );653+654+ expect(sessionsIn(yield* Ref.get(sessions))).toStrictEqual({655+ outside: false,656+ sessions: 1,657+ });658+ })659+ );660+});661+662+describe("kept history in the isolate's cache", () => {663+ it.effect(664+ "is kept with the heavy reads: a why's ranges grow with its file",665+ () =>666+ within(() =>667+ Effect.gen(function* () {668+ const { grown } = yield* history;669+ const { code } = yield* recordOf(grown);670+671+ const seen = yield* Ref.make<readonly (readonly [string, boolean])[]>(672+ []673+ );674+675+ const spy: CodeReads = {676+ through: (key, read, keeping = {}) =>677+ Effect.andThen(678+ Ref.update(seen, (was) => [679+ ...was,680+ [key, keeping.heavy === true] as const,681+ ]),682+ read683+ ),684+ };685+686+ yield* Effect.all([687+ whyOf(code, Option.none(), format),688+ logOf(code, formatLog(grown)),689+ ]).pipe(Effect.provideService(CodeCache, spy));690+691+ const kept = (yield* Ref.get(seen)).filter(([key]) =>692+ /\/(?:why|path-log)\/v\d+\//u.test(key)693+ );694+695+ expect(kept.length).toBeGreaterThan(0);696+ expect(kept.filter(([, heavy]) => !heavy)).toStrictEqual([]);697+ })698+ )699+ );700+});