Skip to content
Myr

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).

authored by oscabriel · committed by oscabriel · parent 01199d6 · Browse files · 6 files+198 added−28 removed
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

6 files+198 added−28 removed

modified:packages/artifacts/src/repo-host.ts+7 added-1 removed
266 unmodified lines
267);268269/**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 */275export 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 }283284 const scope = yield* Effect.scope;285 const opening = yield* Semaphore.make(1);286 const held = new Map<string, unknown>();287
27 unmodified lines
modified:packages/artifacts/test/binding.test.ts+42 added-14 removed
1034 unmodified lines
1035 })1036 )1037 );10381039 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 };10481048 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);10561057 expect([1058 counts.gets - before.gets,1059 counts.disposed - before.disposed,1060 ]).toStrictEqual([1, 1]);1061 })1062 )1063 );10641065 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));10751076 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 );10861087 it.effect(1088 "fails the read once four asks after the first were all rate limited",1089 () =>1090 within(1091 () =>
12 unmodified lines
modified:packages/project/src/code/history.ts+5 added-4 removed
modified:packages/project/src/code/log.ts+5 added-2 removed
14 unmodified lines
15 * 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 */19import { Revision, RepoHost } from "@repo/artifacts/repo-host";19import { inRepoSession, Revision, RepoHost } from "@repo/artifacts/repo-host";20import type {21 Commit,22 RepoNotFound,23 TreeEntry,
306 unmodified lines
330 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 lines
513 key,514 asOneView(computed(repo, commit, path, scan))515 );516517 // 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 lines
8 * many reads under way and looks each commit's entry up once; a value9 * that no longer decodes is computed again and replaced.10 */11import { describe, expect, it } from "@effect/vitest";12import { RepoHost } from "@repo/artifacts/repo-host";12import { RepoHost, RepoSession } from "@repo/artifacts/repo-host";13import type { RepoSessionApi } from "@repo/artifacts/repo-host";14import { RepoHostMemory } from "@repo/artifacts/repo-host/memory";15import type { LandingView } from "@repo/protocol/domain/landing";16import {17 AskId,
9 unmodified lines
27import { CodeStore } from "../src/code-store.ts";28import { CodeStoreCounts, CodeStoreMemory } from "../src/code-store/memory.ts";29import { logOf, whyOf } from "../src/code.ts";30import type { CodeProject } from "../src/code.ts";30import { CodeCacheLive } from "../src/code/cache.ts";31import { CodeCache, CodeCacheLive } from "../src/code/cache.ts";32import type { CodeReads } from "../src/code/cache.ts";33import { keyOf, StoredWhy, whyKept } from "../src/code/history.ts";34import { WHY_SCAN } from "../src/code/why.ts";35import { Content, writer } from "./code-fixtures.ts";36import type { Tree } from "./code-fixtures.ts";
489 unmodified lines
526 path: Option.some(RepoPath.make("count.txt")),527 });528529 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 long
34 unmodified lines
571 })572 )573 );574});575576/**577 * The memory store, the session each commit, tree, blob and log read ran578 * in noted in `sessions`, in order.579 */580const 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 );590591 return Layer.effect(592 RepoHost,593 Effect.gen(function* () {594 const host = yield* RepoHost;595596 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};607608/** How many sessions `sessions` names, and whether a read ran in none. */609const sessionsIn = (sessions: readonly Option.Option<RepoSessionApi>[]) => ({610 outside: sessions.some(Option.isNone),611 sessions: new Set(sessions.flatMap(Option.toArray)).size,612});613614describe("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 >([]);620621 yield* Effect.gen(function* () {622 const { grown } = yield* history;623 const { code } = yield* recordOf(grown);624625 yield* Ref.set(sessions, []);626 yield* onIsolate(whyOf(code, Option.none(), format));627 }).pipe(628 Effect.provide(Layer.merge(sessioned(sessions), CodeStoreMemory))629 );630631 expect(sessionsIn(yield* Ref.get(sessions))).toStrictEqual({632 outside: false,633 sessions: 1,634 });635 })636 );637638 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 >([]);643644 yield* Effect.gen(function* () {645 const { grown } = yield* history;646 const { code } = yield* recordOf(grown);647648 yield* Ref.set(sessions, []);649 yield* onIsolate(logOf(code, formatLog(grown)));650 }).pipe(651 Effect.provide(Layer.merge(sessioned(sessions), CodeStoreMemory))652 );653654 expect(sessionsIn(yield* Ref.get(sessions))).toStrictEqual({655 outside: false,656 sessions: 1,657 });658 })659 );660});661662describe("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);670671 const seen = yield* Ref.make<readonly (readonly [string, boolean])[]>(672 []673 );674675 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 };685686 yield* Effect.all([687 whyOf(code, Option.none(), format),688 logOf(code, formatLog(grown)),689 ]).pipe(Effect.provideService(CodeCache, spy));690691 const kept = (yield* Ref.get(seen)).filter(([key]) =>692 /\/(?:why|path-log)\/v\d+\//u.test(key)693 );694695 expect(kept.length).toBeGreaterThan(0);696 expect(kept.filter(([, heavy]) => !heavy)).toStrictEqual([]);697 })698 )699 );700});