commit.ts
This file
Path
packages/project/src/code/commit.ts
297 lines · 8.5 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 1–4why, one step back1import { 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
lines 5–6why, one step back5import { CommitDiff } from "@repo/protocol/domain/code";6import type { ChangedFile } from "@repo/protocol/domain/code";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 7–9why, one step back7import { BlobSha, TreeSha } from "@repo/protocol/ids";8import type { CommitSha, RepoName } from "@repo/protocol/ids";9import { UnknownCommit } from "@repo/protocol/refusal";Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
line 10why, one step back10import { Array, Effect, Option, Order, Schema } from "effect";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 11why, one step back11Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
line 12why, one step back12import { CodeStore } from "../code-store.ts";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 13–16why, one step back13import type { CodeProject } from "../code.ts";14import { CodeCache } from "./cache.ts";15import { contentsOf } from "./file.ts";16import type { Contents } from "./file.ts";Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
line 17why, one step back17import { commitView } from "./log.ts";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 18–25why, one step back18import { filesUnder, PATHS_CAP } from "./paths.ts";19import { commitAt } from "./revision.ts";20import { entriesOf, entryAt } from "./tree.ts";2122export const DIFF_FILES = 300;2324export const DIFF_TEXT = 2 * 1024 * 1024;25Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 26why, one step back26/** Bump when what is kept changes. */Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 27–33why, one step back27export const COMMIT_VERSION = 1;2829export const commitKey = (repo: RepoName, sha: CommitSha) =>30 `${repo}/commit/v${COMMIT_VERSION}/${sha}`;3132const SIDES_AT_ONCE = 8;33Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 34–35why, one step back34const KEPT_CHANGES = 1000;35Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 36why, one step back36/** Whether each side is a submodule, whose id names no blob here. */Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 37–116why, one step back37interface Changed extends Change {38 readonly links: { readonly before: boolean; readonly after: boolean };39}4041const isLink = (entry: Option.Option<TreeEntry>) =>42 Option.exists(entry, ({ type }) => type === "commit");4344const rootChanges = (repo: RepoName, tree: string) =>45 Effect.map(filesUnder(repo, tree, PATHS_CAP), ({ files }) =>46 files.map(({ entry, path }): Changed => ({47 after: BlobSha.make(entry.sha),48 links: { after: entry.type === "commit", before: false },49 path,50 status: "added",51 }))52 );5354const changesBetween = (repo: RepoName, base: string, head: string) =>55 Effect.gen(function* () {56 const host = yield* RepoHost;5758 const changes = yield* treeDiff(59 repo,60 TreeSha.make(base),61 TreeSha.make(head)62 ).pipe(63 Effect.provideService(64 RepoHost,65 RepoHost.of({66 ...host,67 readTree: (name, id) =>68 name === repo69 ? Effect.asSome(70 Effect.provideService(entriesOf(repo, id), RepoHost, host)71 )72 : host.readTree(name, id),73 })74 )75 );7677 return yield* Effect.forEach(78 changes,79 (change): Effect.Effect<Changed, RepoNotFound, RepoHost> =>80 Effect.map(81 Effect.all([82 entryAt(repo, base, change.path),83 entryAt(repo, head, change.path),84 ]),85 ([before, after]) => ({86 ...change,87 links: { after: isLink(after), before: isLink(before) },88 })89 ),90 { concurrency: 8 }91 );92 });9394const byPath = Order.mapInput(Order.String, (change: Changed) => change.path);9596const sideOf = (97 repo: RepoName,98 blob: BlobSha | undefined,99 link: boolean100): Effect.Effect<Option.Option<Contents>, RepoNotFound, RepoHost> =>101 blob === undefined || link ? Effect.succeedNone : contentsOf(repo, blob);102103const pathOnly = ({ after, before, path, status }: Changed): ChangedFile => {104 const file = { binary: false, path, status };105106 if (before !== undefined && after !== undefined) {107 return { ...file, after, before };108 }109110 if (before !== undefined) {111 return { ...file, before };112 }113114 return after === undefined ? file : { ...file, after };115};116Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 117–125why, one step back117const sidesOf = (repo: RepoName, change: Changed) =>118 Effect.all([119 sideOf(repo, change.before, change.links.before),120 sideOf(repo, change.after, change.links.after),121 ]);122123const lengthOf = (text: Option.Option<string>) =>124 Option.match(text, { onNone: () => 0, onSome: ({ length }) => length });125Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 126why, one step back126/** 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 127–136why, one step back127const withText = (repo: RepoName, changes: readonly Changed[]) =>128 Effect.gen(function* () {129 if (changes.length > DIFF_FILES) {130 return { files: changes.map(pathOnly), truncated: true };131 }132133 const files: ChangedFile[] = [];134 let budget = DIFF_TEXT;135 let truncated = false;136Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
line 137why, one step back137 for (const window of Array.chunksOf(changes, SIDES_AT_ONCE)) {Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 138why, one step back138 if (truncated) {Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
line 139why, one step back139 files.push(...window.map(pathOnly));Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 140–142why, one step back140 continue;141 }142Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 143–147why, one step back143 const sides = yield* Effect.forEach(144 window,145 (change) => sidesOf(repo, change),146 { concurrency: SIDES_AT_ONCE }147 );Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 148why, one step back148Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 149–150why, one step back149 for (const [index, change] of window.entries()) {150 const [before, after] = sides[index] ?? [Option.none(), Option.none()];Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 151why, one step back151Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 152–155why, one step back152 if (truncated) {153 files.push(pathOnly(change));154 continue;155 }Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 156why, one step back156Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 157–159why, one step back157 const oldText = Option.flatMap(before, ({ text }) => text);158 const newText = Option.flatMap(after, ({ text }) => text);159 const size = lengthOf(oldText) + lengthOf(newText);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 160why, one step back160Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 161–183why, one step back161 if (size > budget) {162 truncated = true;163 files.push(pathOnly(change));164 continue;165 }166167 budget -= size;168169 files.push({170 ...pathOnly(change),171 binary:172 Option.exists(before, ({ binary }) => binary) ||173 Option.exists(after, ({ binary }) => binary),174 ...Option.match(oldText, {175 onNone: () => ({}),176 onSome: (text) => ({ oldText: text }),177 }),178 ...Option.match(newText, {179 onNone: () => ({}),180 onSome: (text) => ({ newText: text }),181 }),182 });183 }Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 184–188why, one step back184 }185186 return { files, truncated };187 });188Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 189–195why, one step back189const Kept = Schema.fromJsonString(CommitDiff);190191const decodeKept = Schema.decodeUnknownOption(Kept);192193const encodeKept = Schema.encodeEffect(Kept);194195const withLanding = (Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 196why, one step back196 project: CodeProject,Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 197–222why, one step back197 diff: CommitDiff198): Effect.Effect<CommitDiff> =>199 Effect.map(project.landings([diff.commit.sha]), (landings) =>200 Option.match(201 Array.findFirst(landings, ({ sha }) => sha === diff.commit.sha),202 {203 onNone: () => diff,204 onSome: (landing) => ({205 ...diff,206 commit: { ...diff.commit, landing },207 }),208 }209 )210 );211212const keptDiff = (repo: RepoName, sha: CommitSha) =>213 Effect.gen(function* () {214 const store = yield* CodeStore;215 const text = yield* store.get(commitKey(repo, sha));216217 return Option.filter(218 Option.flatMap(text, decodeKept),219 ({ commit }) => commit.sha === sha220 );221 });222Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 223why, one step back223/** Kept without its landing, which is asked of the record on every call. */Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 224–237why, one step back224const keepDiff = (repo: RepoName, sha: CommitSha, diff: CommitDiff) =>225 Effect.gen(function* () {226 const store = yield* CodeStore;227228 yield* encodeKept(diff).pipe(229 Effect.flatMap((text) => store.put(commitKey(repo, sha), text)),230 Effect.catch((error) =>231 Effect.logWarning(`${sha}'s diff was not kept`, error)232 )233 );234 });235236const computedDiff = (237 repo: RepoName,Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 238–276why, one step back238 sha: CommitSha239): Effect.Effect<CommitDiff, UnknownCommit, RepoHost> =>240 Effect.gen(function* () {241 const cache = yield* CodeCache;242243 const commit = yield* commitAt(repo, sha).pipe(244 Effect.orDie,245 Effect.flatMap(246 Option.match({247 onNone: () => Effect.fail(new UnknownCommit({ sha })),248 onSome: Effect.succeed,249 })250 )251 );252253 const parent = yield* Option.match(254 Option.fromUndefinedOr(commit.parents[0]),255 {256 onNone: () => Effect.succeedNone,257 onSome: (first) => commitAt(repo, first),258 }259 ).pipe(Effect.orDie);260261 const changes = yield* cache262 .through(263 `${repo}:changes:${sha}`,264 Option.match(parent, {265 onNone: () => rootChanges(repo, commit.tree),266 onSome: ({ tree }) => changesBetween(repo, tree, commit.tree),267 }),268 { heavy: true, keep: (all) => all.length <= KEPT_CHANGES }269 )270 .pipe(Effect.orDie);271272 const { files, truncated } = yield* withText(273 repo,274 Array.sort(changes, byPath)275 ).pipe(Effect.orDie);276Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 277–278why, one step back277 return { commit: commitView(commit, Option.none()), files, truncated };278 });Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 279why, one step back279Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 280–289why, one step back280export const commitDiff = (281 project: CodeProject,282 sha: CommitSha283): Effect.Effect<CommitDiff, UnknownCommit, RepoHost> =>284 Effect.gen(function* () {285 const { repo } = project;286 const kept = yield* keptDiff(repo, sha);287288 if (Option.isSome(kept)) {289 return yield* withLanding(project, kept.value);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 290–291why, one step back290 }291Merge build/1213-platform-performance-and-agent-surfacesoscabrielbefore adoption1d75a0d
lines 292–296why, one step back292 const diff = yield* computedDiff(repo, sha);293294 yield* keepDiff(repo, sha, diff);295296 return yield* withLanding(project, diff);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 297why, one step back297 });