Skip to content
Myr

commit.ts

This file

Path

packages/project/src/code/commit.ts

310 lines · 8.9 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

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

lines 1–4why, one step back
1import { RepoHost } from "@repo/artifacts/repo-host";
2import type { RepoNotFound, TreeEntry } from "@repo/artifacts/repo-host";3import { treeDiff } from "@repo/git/tree-diff";4import type { Change } from "@repo/git/tree-diff";

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

line 5why, one step back
5import { CommitDiff } from "@repo/protocol/domain/code";

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 6why, one step back
6import type { ChangedFile, Rev } from "@repo/protocol/domain/code";

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

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

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 10why, one step back
10import type { UnknownRevision } from "@repo/protocol/refusal";

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

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

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

line 12why, one step back
12

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

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

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

lines 14–17why, one step back
14import type { CodeProject } from "../code.ts";
15import { CodeCache } from "./cache.ts";16import { contentsOf } from "./file.ts";17import type { Contents } from "./file.ts";

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 18why, one step back
18import { keptOnlyIfCanonical } from "./history.ts";

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

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

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

line 20why, one step back
20import { filesUnder, PATHS_CAP } from "./paths.ts";

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 21why, one step back
21import { commitAt, repoAt } from "./revision.ts";

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

lines 22–27why, one step back
22import { entriesOf, entryAt } from "./tree.ts";
2324export const DIFF_FILES = 300;2526export const DIFF_TEXT = 2 * 1024 * 1024;27

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 28why, one step back
28/** Bump when what is kept changes. */

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

lines 29–35why, one step back
29export const COMMIT_VERSION = 1;
3031export const commitKey = (repo: RepoName, sha: CommitSha) =>32 `${repo}/commit/v${COMMIT_VERSION}/${sha}`;3334const SIDES_AT_ONCE = 8;35

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

lines 36–37why, one step back
36const KEPT_CHANGES = 1000;
37

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 38why, one step back
38/** Whether each side is a submodule, whose id names no blob here. */

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

lines 39–118why, one step back
39interface Changed extends Change {
40 readonly links: { readonly before: boolean; readonly after: boolean };41}4243const isLink = (entry: Option.Option<TreeEntry>) =>44 Option.exists(entry, ({ type }) => type === "commit");4546const rootChanges = (repo: RepoName, tree: string) =>47 Effect.map(filesUnder(repo, tree, PATHS_CAP), ({ files }) =>48 files.map(({ entry, path }): Changed => ({49 after: BlobSha.make(entry.sha),50 links: { after: entry.type === "commit", before: false },51 path,52 status: "added",53 }))54 );5556const changesBetween = (repo: RepoName, base: string, head: string) =>57 Effect.gen(function* () {58 const host = yield* RepoHost;5960 const changes = yield* treeDiff(61 repo,62 TreeSha.make(base),63 TreeSha.make(head)64 ).pipe(65 Effect.provideService(66 RepoHost,67 RepoHost.of({68 ...host,69 readTree: (name, id) =>70 name === repo71 ? Effect.asSome(72 Effect.provideService(entriesOf(repo, id), RepoHost, host)73 )74 : host.readTree(name, id),75 })76 )77 );7879 return yield* Effect.forEach(80 changes,81 (change): Effect.Effect<Changed, RepoNotFound, RepoHost> =>82 Effect.map(83 Effect.all([84 entryAt(repo, base, change.path),85 entryAt(repo, head, change.path),86 ]),87 ([before, after]) => ({88 ...change,89 links: { after: isLink(after), before: isLink(before) },90 })91 ),92 { concurrency: 8 }93 );94 });9596const byPath = Order.mapInput(Order.String, (change: Changed) => change.path);9798const sideOf = (99 repo: RepoName,100 blob: BlobSha | undefined,101 link: boolean102): Effect.Effect<Option.Option<Contents>, RepoNotFound, RepoHost> =>103 blob === undefined || link ? Effect.succeedNone : contentsOf(repo, blob);104105const pathOnly = ({ after, before, path, status }: Changed): ChangedFile => {106 const file = { binary: false, path, status };107108 if (before !== undefined && after !== undefined) {109 return { ...file, after, before };110 }111112 if (before !== undefined) {113 return { ...file, before };114 }115116 return after === undefined ? file : { ...file, after };117};118

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

lines 119–127why, one step back
119const sidesOf = (repo: RepoName, change: Changed) =>
120 Effect.all([121 sideOf(repo, change.before, change.links.before),122 sideOf(repo, change.after, change.links.after),123 ]);124125const lengthOf = (text: Option.Option<string>) =>126 Option.match(text, { onNone: () => 0, onSome: ({ length }) => length });127

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 128why, one step back
128/** The budget is spent in path order, and no window is read once it runs out. */

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

lines 129–138why, one step back
129const withText = (repo: RepoName, changes: readonly Changed[]) =>
130 Effect.gen(function* () {131 if (changes.length > DIFF_FILES) {132 return { files: changes.map(pathOnly), truncated: true };133 }134135 const files: ChangedFile[] = [];136 let budget = DIFF_TEXT;137 let truncated = false;138

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

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

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

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

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

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

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

lines 142–144why, one step back
142 continue;
143 }144

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

lines 145–149why, one step back
145 const sides = yield* Effect.forEach(
146 window,147 (change) => sidesOf(repo, change),148 { concurrency: SIDES_AT_ONCE }149 );

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

line 150why, one step back
150

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

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

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

line 153why, one step back
153

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

lines 154–157why, one step back
154 if (truncated) {
155 files.push(pathOnly(change));156 continue;157 }

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

line 158why, one step back
158

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

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

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

line 162why, one step back
162

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

lines 163–185why, one step back
163 if (size > budget) {
164 truncated = true;165 files.push(pathOnly(change));166 continue;167 }168169 budget -= size;170171 files.push({172 ...pathOnly(change),173 binary:174 Option.exists(before, ({ binary }) => binary) ||175 Option.exists(after, ({ binary }) => binary),176 ...Option.match(oldText, {177 onNone: () => ({}),178 onSome: (text) => ({ oldText: text }),179 }),180 ...Option.match(newText, {181 onNone: () => ({}),182 onSome: (text) => ({ newText: text }),183 }),184 });185 }

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

lines 186–190why, one step back
186 }
187188 return { files, truncated };189 });190

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

lines 191–197why, one step back
191const Kept = Schema.fromJsonString(CommitDiff);
192193const decodeKept = Schema.decodeUnknownOption(Kept);194195const encodeKept = Schema.encodeEffect(Kept);196197const withLanding = (

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

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

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

lines 199–224why, one step back
199 diff: CommitDiff
200): Effect.Effect<CommitDiff> =>201 Effect.map(project.landings([diff.commit.sha]), (landings) =>202 Option.match(203 Array.findFirst(landings, ({ sha }) => sha === diff.commit.sha),204 {205 onNone: () => diff,206 onSome: (landing) => ({207 ...diff,208 commit: { ...diff.commit, landing },209 }),210 }211 )212 );213214const keptDiff = (repo: RepoName, sha: CommitSha) =>215 Effect.gen(function* () {216 const store = yield* CodeStore;217 const text = yield* store.get(commitKey(repo, sha));218219 return Option.filter(220 Option.flatMap(text, decodeKept),221 ({ commit }) => commit.sha === sha222 );223 });224

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 225why, one step back
225/** Kept without its landing, which is asked of the record on every call. */

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

lines 226–239why, one step back
226const keepDiff = (repo: RepoName, sha: CommitSha, diff: CommitDiff) =>
227 Effect.gen(function* () {228 const store = yield* CodeStore;229230 yield* encodeKept(diff).pipe(231 Effect.flatMap((text) => store.put(commitKey(repo, sha), text)),232 Effect.catch((error) =>233 Effect.logWarning(`${sha}'s diff was not kept`, error)234 )235 );236 });237238const computedDiff = (239 repo: RepoName,

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

lines 240–278why, one step back
240 sha: CommitSha
241): Effect.Effect<CommitDiff, UnknownCommit, RepoHost> =>242 Effect.gen(function* () {243 const cache = yield* CodeCache;244245 const commit = yield* commitAt(repo, sha).pipe(246 Effect.orDie,247 Effect.flatMap(248 Option.match({249 onNone: () => Effect.fail(new UnknownCommit({ sha })),250 onSome: Effect.succeed,251 })252 )253 );254255 const parent = yield* Option.match(256 Option.fromUndefinedOr(commit.parents[0]),257 {258 onNone: () => Effect.succeedNone,259 onSome: (first) => commitAt(repo, first),260 }261 ).pipe(Effect.orDie);262263 const changes = yield* cache264 .through(265 `${repo}:changes:${sha}`,266 Option.match(parent, {267 onNone: () => rootChanges(repo, commit.tree),268 onSome: ({ tree }) => changesBetween(repo, tree, commit.tree),269 }),270 { heavy: true, keep: (all) => all.length <= KEPT_CHANGES }271 )272 .pipe(Effect.orDie);273274 const { files, truncated } = yield* withText(275 repo,276 Array.sort(changes, byPath)277 ).pipe(Effect.orDie);278

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

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

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

line 281why, one step back
281

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 282why, one step back
282/** `at` names the fork the commit is read in; the canonical repo when it names none. */

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

lines 283–284why, one step back
283export const commitDiff = (
284 project: CodeProject,

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 285–287why, one step back
285 sha: CommitSha,
286 at: Option.Option<Rev> = Option.none()287): Effect.Effect<CommitDiff, UnknownCommit | UnknownRevision, RepoHost> =>

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

line 288why, one step back
288 Effect.gen(function* () {

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 289–290why, one step back
289 const where = yield* repoAt(project, at);
290 const { repo } = where;

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

line 291why, one step back
291

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 292–295why, one step back
292 const diff = yield* keptOnlyIfCanonical(
293 where,294 Effect.gen(function* () {295 const kept = yield* keptDiff(repo, sha);

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

line 296why, one step back
296

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 297–299why, one step back
297 if (Option.isSome(kept)) {
298 return kept.value;299 }

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

line 300why, one step back
300

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 301–307why, one step back
301 const computed = yield* computedDiff(repo, sha);
302303 yield* keepDiff(repo, sha, computed);304305 return computed;306 })307 );

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

lines 308–309why, one step back
308
309 return yield* withLanding(project, diff);

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

line 310why, one step back
310 });