Skip to content
Myr

commit.ts

This file

Path

packages/project/src/code/commit.ts

343 lines · 11.1 KiB · TypeScript

Last changed by

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

History of this file

Take it

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 1–6why, one step back
1/**
2 * One commit and what it changed against its first parent (a root commit3 * against the empty tree), through `treeDiff` (`@repo/git/tree-diff`): each4 * file's status, both blob ids, whether it is binary, and its old and new5 * text. At most 300 files and 2 MiB of text in all: past 300 files every6 * file is a path only, and once the text runs past 2 MiB the files after

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 7–21why, one step back
7 * it are; either way the diff says it is `truncated`.
8 *9 * Each file's two sides are read `SIDES_AT_ONCE` files at a time, in path10 * order, and the budget is spent in that order, so the diff is the one a11 * read of one file after another gives. A tree entry carries no size, so12 * a side the budget drops cannot be known before it is read: once the13 * budget runs out no further window is read, and at most one window's14 * reads go unused.15 *16 * What a commit changed never changes, so the finished diff is kept in the17 * code store by its repo and id (`commitKey`), without its landing: the18 * landing is asked of the project's record on every call, so a later19 * landing shows at once and the store holds none (principle 7). A kept20 * value that no longer decodes is computed again; the list of changes is21 * also kept in the isolate's cache by the commit's id.

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 22–26why, one step back
22 */
23import { RepoHost } from "@repo/artifacts/repo-host";24import type { RepoNotFound, TreeEntry } from "@repo/artifacts/repo-host";25import { treeDiff } from "@repo/git/tree-diff";26import type { Change } from "@repo/git/tree-diff";

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 27–28why, one step back
27import { CommitDiff } from "@repo/protocol/domain/code";
28import type { ChangedFile } from "@repo/protocol/domain/code";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 29–31why, one step back
29import { BlobSha, TreeSha } from "@repo/protocol/ids";
30import type { CommitSha, RepoName } from "@repo/protocol/ids";31import { UnknownCommit } from "@repo/protocol/refusal";

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 32why, one step back
32import { Array, Effect, Option, Order, Schema } from "effect";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 33why, one step back
33

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 34why, one step back
34import { CodeStore } from "../code-store.ts";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 35–38why, one step back
35import type { CodeProject } from "../code.ts";
36import { CodeCache } from "./cache.ts";37import { contentsOf } from "./file.ts";38import type { Contents } from "./file.ts";

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 39why, one step back
39import { commitView } from "./log.ts";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 40–49why, one step back
40import { filesUnder, PATHS_CAP } from "./paths.ts";
41import { commitAt } from "./revision.ts";42import { entriesOf, entryAt } from "./tree.ts";4344/** The most files a commit's diff carries text for. */45export const DIFF_FILES = 300;4647/** The most text a commit's diff carries in all: 2 MiB. */48export const DIFF_TEXT = 2 * 1024 * 1024;49

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 50–59why, one step back
50/** The kept commit's version, in its key: it moves when what is kept changes. */
51export const COMMIT_VERSION = 1;5253/** Where a commit's diff is kept in the code store: by its repo and its id. */54export const commitKey = (repo: RepoName, sha: CommitSha) =>55 `${repo}/commit/v${COMMIT_VERSION}/${sha}`;5657/** How many files' sides are read at once: the fan-out the changes are listed with. */58const SIDES_AT_ONCE = 8;59

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 60–150why, one step back
60/** The longest list of changes the cache keeps. */
61const KEPT_CHANGES = 1000;6263/** A change, and whether each side is a submodule (whose id names no blob here). */64interface Changed extends Change {65 readonly links: { readonly before: boolean; readonly after: boolean };66}6768const isLink = (entry: Option.Option<TreeEntry>) =>69 Option.exists(entry, ({ type }) => type === "commit");7071/** Every file under a root commit's tree, each added. */72const rootChanges = (repo: RepoName, tree: string) =>73 Effect.map(filesUnder(repo, tree, PATHS_CAP), ({ files }) =>74 files.map(({ entry, path }): Changed => ({75 after: BlobSha.make(entry.sha),76 links: { after: entry.type === "commit", before: false },77 path,78 status: "added",79 }))80 );8182/**83 * What changed from `base` to `head`, by `treeDiff` over this repo's trees84 * read through the cache (so the side lookups that follow read none again).85 */86const changesBetween = (repo: RepoName, base: string, head: string) =>87 Effect.gen(function* () {88 const host = yield* RepoHost;8990 const changes = yield* treeDiff(91 repo,92 TreeSha.make(base),93 TreeSha.make(head)94 ).pipe(95 Effect.provideService(96 RepoHost,97 RepoHost.of({98 ...host,99 readTree: (name, id) =>100 name === repo101 ? Effect.asSome(102 Effect.provideService(entriesOf(repo, id), RepoHost, host)103 )104 : host.readTree(name, id),105 })106 )107 );108109 return yield* Effect.forEach(110 changes,111 (change): Effect.Effect<Changed, RepoNotFound, RepoHost> =>112 Effect.map(113 Effect.all([114 entryAt(repo, base, change.path),115 entryAt(repo, head, change.path),116 ]),117 ([before, after]) => ({118 ...change,119 links: { after: isLink(after), before: isLink(before) },120 })121 ),122 { concurrency: 8 }123 );124 });125126const byPath = Order.mapInput(Order.String, (change: Changed) => change.path);127128/** A side's contents, unless it is a submodule or absent. */129const sideOf = (130 repo: RepoName,131 blob: BlobSha | undefined,132 link: boolean133): Effect.Effect<Option.Option<Contents>, RepoNotFound, RepoHost> =>134 blob === undefined || link ? Effect.succeedNone : contentsOf(repo, blob);135136/** A change as a path, its status and its blob ids, with no text. */137const pathOnly = ({ after, before, path, status }: Changed): ChangedFile => {138 const file = { binary: false, path, status };139140 if (before !== undefined && after !== undefined) {141 return { ...file, after, before };142 }143144 if (before !== undefined) {145 return { ...file, before };146 }147148 return after === undefined ? file : { ...file, after };149};150

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 151–166why, one step back
151/** Both sides of `change`: its old contents and its new, where it has them. */
152const sidesOf = (repo: RepoName, change: Changed) =>153 Effect.all([154 sideOf(repo, change.before, change.links.before),155 sideOf(repo, change.after, change.links.after),156 ]);157158/** The length of a side's text, or 0 when it has none. */159const lengthOf = (text: Option.Option<string>) =>160 Option.match(text, { onNone: () => 0, onSome: ({ length }) => length });161162/**163 * Each change with its text while the budget lasts, and whether any went164 * without: the sides read a window of `SIDES_AT_ONCE` files at a time, the165 * budget spent in path order, and no window read once it ran out.166 */

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 167–176why, one step back
167const withText = (repo: RepoName, changes: readonly Changed[]) =>
168 Effect.gen(function* () {169 if (changes.length > DIFF_FILES) {170 return { files: changes.map(pathOnly), truncated: true };171 }172173 const files: ChangedFile[] = [];174 let budget = DIFF_TEXT;175 let truncated = false;176

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 177why, one step back
177 for (const window of Array.chunksOf(changes, SIDES_AT_ONCE)) {

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 178why, one step back
178 if (truncated) {

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

line 179why, one step back
179 files.push(...window.map(pathOnly));

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 180–182why, one step back
180 continue;
181 }182

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 183–187why, one step back
183 const sides = yield* Effect.forEach(
184 window,185 (change) => sidesOf(repo, change),186 { concurrency: SIDES_AT_ONCE }187 );

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 188why, one step back
188

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 189–190why, one step back
189 for (const [index, change] of window.entries()) {
190 const [before, after] = sides[index] ?? [Option.none(), Option.none()];

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 191why, one step back
191

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 192–195why, one step back
192 if (truncated) {
193 files.push(pathOnly(change));194 continue;195 }

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 196why, one step back
196

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 197–199why, one step back
197 const oldText = Option.flatMap(before, ({ text }) => text);
198 const newText = Option.flatMap(after, ({ text }) => text);199 const size = lengthOf(oldText) + lengthOf(newText);

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 200why, one step back
200

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 201–223why, one step back
201 if (size > budget) {
202 truncated = true;203 files.push(pathOnly(change));204 continue;205 }206207 budget -= size;208209 files.push({210 ...pathOnly(change),211 binary:212 Option.exists(before, ({ binary }) => binary) ||213 Option.exists(after, ({ binary }) => binary),214 ...Option.match(oldText, {215 onNone: () => ({}),216 onSome: (text) => ({ oldText: text }),217 }),218 ...Option.match(newText, {219 onNone: () => ({}),220 onSome: (text) => ({ newText: text }),221 }),222 });223 }

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 224–228why, one step back
224 }
225226 return { files, truncated };227 });228

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 229–237why, one step back
229/** A diff as the store keeps it: JSON, by the protocol's own schema. */
230const Kept = Schema.fromJsonString(CommitDiff);231232const decodeKept = Schema.decodeUnknownOption(Kept);233234const encodeKept = Schema.encodeEffect(Kept);235236/** `diff` with its commit's landing, asked of the project's record now. */237const withLanding = (

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 238why, one step back
238 project: CodeProject,

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 239–281why, one step back
239 diff: CommitDiff
240): Effect.Effect<CommitDiff> =>241 Effect.map(project.landings([diff.commit.sha]), (landings) =>242 Option.match(243 Array.findFirst(landings, ({ sha }) => sha === diff.commit.sha),244 {245 onNone: () => diff,246 onSome: (landing) => ({247 ...diff,248 commit: { ...diff.commit, landing },249 }),250 }251 )252 );253254/** The diff kept for `sha` in the code store, if one is and it still decodes. */255const keptDiff = (repo: RepoName, sha: CommitSha) =>256 Effect.gen(function* () {257 const store = yield* CodeStore;258 const text = yield* store.get(commitKey(repo, sha));259260 return Option.filter(261 Option.flatMap(text, decodeKept),262 ({ commit }) => commit.sha === sha263 );264 });265266/** `diff`, which carries no landing, kept for `sha`; a store that fails keeps nothing. */267const keepDiff = (repo: RepoName, sha: CommitSha, diff: CommitDiff) =>268 Effect.gen(function* () {269 const store = yield* CodeStore;270271 yield* encodeKept(diff).pipe(272 Effect.flatMap((text) => store.put(commitKey(repo, sha), text)),273 Effect.catch((error) =>274 Effect.logWarning(`${sha}'s diff was not kept`, error)275 )276 );277 });278279/** The diff of `sha` against its first parent, computed, with no landing. */280const computedDiff = (281 repo: RepoName,

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 282–321why, one step back
282 sha: CommitSha
283): Effect.Effect<CommitDiff, UnknownCommit, RepoHost> =>284 Effect.gen(function* () {285 const cache = yield* CodeCache;286287 const commit = yield* commitAt(repo, sha).pipe(288 Effect.orDie,289 Effect.flatMap(290 Option.match({291 onNone: () => Effect.fail(new UnknownCommit({ sha })),292 onSome: Effect.succeed,293 })294 )295 );296297 const parent = yield* Option.match(298 Option.fromUndefinedOr(commit.parents[0]),299 {300 onNone: () => Effect.succeedNone,301 onSome: (first) => commitAt(repo, first),302 }303 ).pipe(Effect.orDie);304305 const changes = yield* cache306 .through(307 `${repo}:changes:${sha}`,308 Option.match(parent, {309 onNone: () => rootChanges(repo, commit.tree),310 onSome: ({ tree }) => changesBetween(repo, tree, commit.tree),311 }),312 // A longer list is listed again, over trees the cache keeps.313 { heavy: true, keep: (all) => all.length <= KEPT_CHANGES }314 )315 .pipe(Effect.orDie);316317 const { files, truncated } = yield* withText(318 repo,319 Array.sort(changes, byPath)320 ).pipe(Effect.orDie);321

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 322–323why, one step back
322 return { commit: commitView(commit, Option.none()), files, truncated };
323 });

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 324why, one step back
324

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 325–335why, one step back
325/** One commit and the files it changed against its first parent. */
326export const commitDiff = (327 project: CodeProject,328 sha: CommitSha329): Effect.Effect<CommitDiff, UnknownCommit, RepoHost> =>330 Effect.gen(function* () {331 const { repo } = project;332 const kept = yield* keptDiff(repo, sha);333334 if (Option.isSome(kept)) {335 return yield* withLanding(project, kept.value);

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 336–337why, one step back
336 }
337

Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d

lines 338–342why, one step back
338 const diff = yield* computedDiff(repo, sha);
339340 yield* keepDiff(repo, sha, diff);341342 return yield* withLanding(project, diff);

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 343why, one step back
343 });