@ref0/git 0.25.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -209,13 +209,22 @@ export interface ReceiveInput extends CallInput {
209
209
  * `too-large`. Sync's import alone sets it.
210
210
  */
211
211
  readonly cutMost?: number;
212
+ /**
213
+ * Whether the repo held any loose object rows at `listed.version`, as core's opening snapshot
214
+ * read `stats/loose`. When `false`, the receive skips reading object rows upfront and checks
215
+ * listed packs directly, falling back to object rows only for IDs still unresolved after listed
216
+ * packs and before unlisted-pack discovery (so a stale `false` costs at most extra reads on a
217
+ * miss); when `true` or left out, it reads rows upfront as usual. A host whose own objects
218
+ * ignore it keeps reading rows.
219
+ */
220
+ readonly hasLooseRows?: boolean;
212
221
  }
213
222
  /**
214
223
  * A pack to stage for its receive in steps, an import segment's or a push upload's: its bytes, as
215
224
  * `receive` takes them, and the boundary, needs and cut's most, or the shallow lines, its steps
216
225
  * check as `ReceiveInput` says. Sync's import and a push upload's job stage one.
217
226
  */
218
- export type StageInput = Omit<ReceiveInput, 'partial'>;
227
+ export type StageInput = Omit<ReceiveInput, 'partial' | 'hasLooseRows'>;
219
228
  /** A segment's pack staged: its ID, which each step and the landing name, and its bytes. */
220
229
  export interface Staged {
221
230
  readonly pack: string;
@@ -68,16 +68,17 @@ export type PushContext = Acting & {
68
68
  */
69
69
  readonly changes: Effect.Effect<readonly TreeChange[], GitError>;
70
70
  /**
71
- * The push's changes commit by commit, read only as it's pulled, in the push's call scope: each commit the push brings, and
72
- * in each the paths whose entry differs from every parent's that the push brings or that's the
73
- * ref's old tip, names only, one commit at a time, a commit with none counted as a root. A ref
74
- * moved onto a commit the push doesn't bring, and a write through the API, list the ref's tip
75
- * diff instead, credited to the new tip, so what the tip takes from a commit no rule saw, such
76
- * as a merge commit's other parent, is listed as its own. `Rules.files` also counts a parent at
77
- * the tip of a ref it covers, and, on a refusal, one on the first-parent chain of the ref's old
78
- * tip or of a default branch it covers, 1,000 commits back (Q732, Q734). A push of several refs
79
- * lists its commits once, for each ref. A walk past its allotment fails with the allotment's
80
- * refusal, which `Rules.files` answers as its own.
71
+ * The push's changes commit by commit, read only as it's pulled, in the push's call scope: each
72
+ * commit the push brings, and in each the paths whose entry differs from every parent's that the
73
+ * push brings or that's the ref's old tip, names only, one commit at a time, a commit with none
74
+ * counted as a root. A ref moved onto a commit the push doesn't bring, and a write through the
75
+ * API, list each commit the move adds to the ref the same way, so what one takes from a commit no
76
+ * rule saw, such as a merge commit's other parent, is listed as its own. `Rules.files` also
77
+ * counts a parent at the tip of a ref it covers, and, on a refusal, one on the first-parent chain
78
+ * of the ref's old tip or of a default branch it covers, 1,000 commits back (Q732, Q734). A push
79
+ * of several refs lists its commits once, for each ref. A walk past its allotment fails with the
80
+ * allotment's refusal, and a move's past 10,000 commits it adds, a merge counted once for each
81
+ * parent, with `walk-limit`: `Rules.files` answers each as its own.
81
82
  */
82
83
  readonly commitChanges: Stream.Stream<CommitChange, GitError>;
83
84
  };
@@ -2,6 +2,7 @@ import { fail, gitError } from "../contracts/result.mjs";
2
2
  import { openTreeBytes } from "../storage/budget.mjs";
3
3
  import { scopedReceive, walkObjects } from "../storage/calls.mjs";
4
4
  import { isUnindexedRead } from "../storage/index-visible.mjs";
5
+ import { walkedVisible } from "../storage/visible.mjs";
5
6
  import { openTreesRefusal } from "../storage/push-allotment.mjs";
6
7
  import { isLoose } from "../storage/loose.mjs";
7
8
  import { listedIds } from "../storage/listed-ids.mjs";
@@ -35,7 +36,7 @@ import { liveReservations, reservedTarget } from "../store/reservations.mjs";
35
36
  import { settleName } from "../store/settle.mjs";
36
37
  import { collections, looseKey, newChangeId, prunedKey, pushedKey, pushedUnder, reachedKey, repoRootsKey, repoScope } from "../store/scopes.mjs";
37
38
  import { headOf, indexJobs, openKeyed, openRepo, refIndexJobs, repoAccess } from "./access.mjs";
38
- import { kindsOf } from "./ancestry.mjs";
39
+ import { ancestryLimit, kindsOf } from "./ancestry.mjs";
39
40
  import { isRefName } from "./names.mjs";
40
41
  import { runHookGroups } from "./hooks/groups.mjs";
41
42
  import { Changes } from "./changes.mjs";
@@ -69,6 +70,11 @@ import * as Equal from "effect/Equal";
69
70
  * name it can't write is `unseenRef`'s, the same whatever is there.
70
71
  */
71
72
  const pushWhole = /* @__PURE__ */ WholeRepo.mint();
73
+ /** How far a walk of what a ref's old tip reaches goes, past which a move's walk is refused. */
74
+ const judgedWalk = {
75
+ most: ancestryLimit,
76
+ commits: true
77
+ };
72
78
  const packsRead = { collection: collections.packs };
73
79
  const refsRead = { collection: collections.refs };
74
80
  const timesRead = { collection: collections.stats };
@@ -816,7 +822,7 @@ const unindexedPush = () => gitError("RESOURCE_EXHAUSTED", "This push can't be c
816
822
  /** `error`, a read by ID the index can't close yet told as the push's own. */
817
823
  const asPushCheck = (error) => isUnindexedRead(error) ? unindexedPush() : error;
818
824
  /** The request's pack into its quarantine, or `undefined` for a push with none. */
819
- const receive = (objects, listed, request) => {
825
+ const receive = (objects, listed, request, hasLooseRows) => {
820
826
  const { pack, shallow, signal } = request;
821
827
  if (pack === void 0) return Effect.succeed({
822
828
  received: void 0,
@@ -832,7 +838,8 @@ const receive = (objects, listed, request) => {
832
838
  });
833
839
  const input = {
834
840
  pack: pack.bytes,
835
- objects: pack.objects
841
+ objects: pack.objects,
842
+ hasLooseRows
836
843
  };
837
844
  if (pack.length !== void 0) input.length = pack.length;
838
845
  if (pack.atMost !== void 0) input.atMost = pack.atMost;
@@ -908,7 +915,7 @@ const pushOnto = (request, { listed, defaultBranch, mark, roots, hasLooseRows, r
908
915
  const { received, rooted } = yield* receive(yield* read({
909
916
  view,
910
917
  at: seen
911
- }), listed, request).pipe(Effect.provideService(HeldBy, heldUntil(heldAt, fetchLimits)), Effect.tapError((error) => isAwaitingUpkeep(error) ? bestEffort(ensureRepack(core, core.jobs, listed.repo, fetchLimits)) : Effect.void), Effect.mapError(asPushCheck));
918
+ }), listed, request, hasLooseRows).pipe(Effect.provideService(HeldBy, heldUntil(heldAt, fetchLimits)), Effect.tapError((error) => isAwaitingUpkeep(error) ? bestEffort(ensureRepack(core, core.jobs, listed.repo, fetchLimits)) : Effect.void), Effect.mapError(asPushCheck));
912
919
  const ownedPack = ownPack(request, received);
913
920
  let listing = false;
914
921
  if (ownedPack !== void 0) yield* Effect.addFinalizer((exit) => listing || Exit.hasInterrupts(exit) ? Effect.void : Effect.flatMap(Clock.currentTimeMillis, (at) => abandonOwn({
@@ -1121,6 +1128,15 @@ const pushOnto = (request, { listed, defaultBranch, mark, roots, hasLooseRows, r
1121
1128
  ...changing,
1122
1129
  signal: unaborted
1123
1130
  };
1131
+ judging.reach = (tips) => SealedObjects.visibleWhole(sealed, pushWhole, decide.at, {
1132
+ tips,
1133
+ row: indexRow?.value,
1134
+ roots
1135
+ }, () => walkedVisible(whole, decide.at, {
1136
+ tips,
1137
+ roots
1138
+ }, judgedWalk));
1139
+ judging.roots = roots;
1124
1140
  if (by !== void 0) judging.covered = coveredTips(read.refs, (ref) => by.covers(ref) && !movesAsGitsOwn(hidden, request.as, ref));
1125
1141
  if (by !== void 0) judging.refuses = by.refuses;
1126
1142
  if (by?.covers(read.head) === true) judging.defaultTip = read.refs.get(read.head);
@@ -1,8 +1,11 @@
1
+ import { gitError } from "../../contracts/result.mjs";
1
2
  import { emptyTree } from "../../protocol/objects/object.mjs";
2
- import { combinedPaths } from "../../engine/combined.mjs";
3
+ import { WalkRefused, combinedPaths } from "../../engine/combined.mjs";
4
+ import { noRepoRoots, parentsWithin } from "../../protocol/objects/commit.mjs";
3
5
  import { allotmentWithin } from "../../contracts/object-values.mjs";
4
6
  import { openTreeBytes } from "../../storage/budget.mjs";
5
7
  import { openTreesRefusal } from "../../storage/push-allotment.mjs";
8
+ import "../ancestry.mjs";
6
9
  import { commitOf, treeOf, withEmptyTree } from "./changes.mjs";
7
10
  import * as Effect from "effect/Effect";
8
11
  import * as Predicate from "effect/Predicate";
@@ -66,10 +69,9 @@ const chainSearch = (input, starts) => {
66
69
  };
67
70
  /**
68
71
  * `commit`'s paths against the parents that count. On the first path the rule refuses, when a
69
- * parent was left out, it searches the chains for those left out, and for the tip itself on a move
70
- * (Q734). A tip it finds was judged on its way there, so nothing more is listed; a parent it finds
71
- * counts, and the commit is walked again against it and each parent at a covered tip. Otherwise
72
- * the walk goes on, and the path is listed. A push with no refused path pays for no search.
72
+ * parent was left out, it searches the chains for those left out (Q734): a parent it finds counts,
73
+ * and the commit is walked again against it and each parent at a covered tip. Otherwise the walk
74
+ * goes on, and the path is listed. A push with no refused path pays for no search.
73
75
  */
74
76
  const judgedPaths = (input, commit, search, allotment) => {
75
77
  const { refuses } = input;
@@ -77,20 +79,18 @@ const judgedPaths = (input, commit, search, allotment) => {
77
79
  oid: commit.oid,
78
80
  parents: commit.counted
79
81
  };
80
- if (!(refuses !== void 0 && (commit.left.length > 0 || commit.self !== void 0))) return commitPaths(input, walked, allotment);
82
+ if (!(refuses !== void 0 && commit.left.length > 0)) return commitPaths(input, walked, allotment);
81
83
  return Stream.suspend(() => {
82
- const candidates = commit.self === void 0 ? commit.left : [commit.self, ...commit.left];
83
84
  let trusted;
84
85
  const listed = (change) => {
85
86
  if (trusted !== void 0 || !refuses(change)) return Effect.succeed(change);
86
- return Effect.map(search(candidates), (found) => {
87
+ return Effect.map(search(commit.left), (found) => {
87
88
  trusted = found;
88
89
  return found.length > 0 ? void 0 : change;
89
90
  });
90
91
  };
91
92
  const again = Stream.suspend(() => {
92
93
  if (trusted === void 0 || trusted.length === 0) return Stream.empty;
93
- if (commit.self !== void 0 && trusted.includes(commit.self)) return Stream.empty;
94
94
  const counts = /* @__PURE__ */ new Set([...commit.counted, ...trusted]);
95
95
  const parents = commit.parents.filter((parent) => counts.has(parent) || input.covered?.(parent) === true);
96
96
  return commitPaths(input, {
@@ -102,36 +102,108 @@ const judgedPaths = (input, commit, search, allotment) => {
102
102
  });
103
103
  };
104
104
  /**
105
- * How a move's new tip is walked, one that brings no commits of its own: an API write, or a push
106
- * of a ref onto a commit it doesn't bring (Q732, Q734). When the tip's first parent is the old tip,
107
- * or the tip of a ref `covered` holds, it's walked against that parent and each later one that
108
- * counts, so a merge commit of a covered branch is judged as a push of that one commit is.
109
- * Otherwise it's walked against the old tip, or as a root for a ref it makes, so what the tip takes
110
- * from a commit the rule never saw, made unpublished, a fork's pull head or an older commit of a
111
- * covered branch, is listed, unless a refusal's search finds it judged. A squash, a rebase and a
112
- * fast-forward still differ from the old tip by what they bring.
105
+ * A move's walk past `limit` commits it adds: what it adds can't be judged, so the rule refuses the
106
+ * ref with it, as it does a walk its allotment stops.
113
107
  */
114
- const movedTip = /* @__PURE__ */ Effect.fnUntraced(function* (input, update) {
108
+ const movePastLimit = (limit) => new WalkRefused({ refusal: gitError("RESOURCE_EXHAUSTED", `This move adds over ${limit} commits to check, a merge counted per parent`, {
109
+ hint: "Move the ref in steps",
110
+ context: { reason: "walk-limit" }
111
+ }) });
112
+ /** What the old tip reaches, past its walk's bound, refuses the ref as the walk's limit does. */
113
+ const refusedPastBound = (error) => Effect.fail(error.code === "RESOURCE_EXHAUSTED" ? new WalkRefused({ refusal: error }) : error);
114
+ /**
115
+ * The commits from `to` down to `ends` and the repo's roots, each read once, less those `held`
116
+ * answers for and the commits behind them: none past `limit`, each counted for its parents.
117
+ */
118
+ const walkedDown = /* @__PURE__ */ Effect.fnUntraced(function* (input, to, ends, held, limit) {
119
+ const read = withEmptyTree(input.read);
120
+ const added = [];
121
+ const passed = /* @__PURE__ */ new Set([to]);
122
+ let frontier = [to];
123
+ let taken = 0;
124
+ while (frontier.length > 0) {
125
+ const open = frontier.filter((oid) => !ends(oid));
126
+ const kept = held === void 0 || open.length === 0 ? /* @__PURE__ */ new Set() : yield* held(open);
127
+ frontier = [];
128
+ for (const oid of open) {
129
+ if (kept.has(oid)) continue;
130
+ if (taken >= limit) return void 0;
131
+ const commit = yield* commitOf(read, oid, input.signal);
132
+ if (commit === void 0) continue;
133
+ const parents = parentsWithin(input.roots ?? noRepoRoots, oid, commit.parents);
134
+ taken += Math.max(1, parents.length);
135
+ added.push({
136
+ oid,
137
+ parents
138
+ });
139
+ for (const parent of parents) {
140
+ if (passed.has(parent)) continue;
141
+ passed.add(parent);
142
+ frontier.push(parent);
143
+ }
144
+ }
145
+ }
146
+ return added;
147
+ });
148
+ /** The commits of `walked` whose parents lead down to `from`: newer than it, so none it reaches. */
149
+ const leadingTo = (walked, from) => {
150
+ const children = /* @__PURE__ */ new Map();
151
+ for (const commit of walked) for (const parent of commit.parents) children.set(parent, [...children.get(parent) ?? [], commit.oid]);
152
+ const leads = /* @__PURE__ */ new Set();
153
+ const waiting = [...children.get(from) ?? []];
154
+ for (let oid = waiting.pop(); oid !== void 0; oid = waiting.pop()) {
155
+ if (leads.has(oid)) continue;
156
+ leads.add(oid);
157
+ waiting.push(...children.get(oid) ?? []);
158
+ }
159
+ return leads;
160
+ };
161
+ /**
162
+ * Which of `oids`, commits newer than the old tip, the default branch reaches when the rule covers
163
+ * it and it isn't the old tip itself: none past its walk's bound, so those are judged, never let
164
+ * through unread.
165
+ */
166
+ const defaultHolds = (input, from, oids) => {
167
+ const { defaultTip } = input;
168
+ const reach = defaultTip === void 0 || defaultTip === from ? void 0 : input.reach?.([defaultTip]);
169
+ if (reach === void 0 || oids.length === 0) return Effect.succeed(/* @__PURE__ */ new Set());
170
+ return reach.has(oids, { signal: input.signal }).pipe(Effect.catchIf((error) => error.code === "RESOURCE_EXHAUSTED", () => Effect.succeed(/* @__PURE__ */ new Set())));
171
+ };
172
+ /**
173
+ * The commits a move's new tip adds to its ref, one that brings no commits of its own: an API
174
+ * write, or a push of a ref onto a commit it doesn't bring. A walk from the tip ends at each commit
175
+ * already on the ref, which the old tip reaches, as `reach` answers, or on the default branch when
176
+ * the rule covers it, and at each covered tip, which a rule judged on its way there, but not at a
177
+ * covered tip's older commits. Each commit it adds is then walked as a push's brought one is
178
+ * (#3016), so a commit made unpublished and moved onto later, deleting what its parent added,
179
+ * can't hide the parent's paths. Past `limit` commits it's refused, as a push's walks are past
180
+ * `ancestryLimit`.
181
+ *
182
+ * It walks first without `reach`, within `limit`, and asks the old tip's and the default branch's
183
+ * of the commits whose parents don't lead down to the old tip. Those that do, newer than it, are
184
+ * asked of the default branch's alone: a fast-forward, or a merge commit onto the old tip, of a
185
+ * branch the default isn't, so a repo with no reachability index never walks the ref's whole
186
+ * history to answer that its new commits aren't in it. A move onto older history, past `limit`
187
+ * that way, walks again asking.
188
+ */
189
+ const addedBy = /* @__PURE__ */ Effect.fnUntraced(function* (input, update) {
115
190
  const { to, from } = update;
116
- if (input.covered?.(to) === true) return void 0;
117
- const commit = yield* commitOf(withEmptyTree(input.read), to, input.signal);
118
- const counts = (parent) => parent === from || input.covered?.(parent) === true;
119
- const parents = commit?.parents ?? [];
120
- const [first, ...rest] = parents;
121
- if (first !== void 0 && counts(first)) return {
122
- oid: to,
123
- parents,
124
- counted: [first, ...rest.filter(counts)],
125
- left: rest.filter((parent) => !counts(parent))
126
- };
127
- const fallback = from === null ? [] : [from];
128
- return {
129
- oid: to,
130
- parents: [...fallback, ...parents],
131
- counted: fallback,
132
- left: parents,
133
- self: to
134
- };
191
+ const limit = input.limit ?? 1e4;
192
+ const tips = [from, input.defaultTip].filter((tip) => tip !== null && tip !== void 0);
193
+ const judged = tips.length === 0 ? void 0 : input.reach?.(tips);
194
+ const ends = (oid) => oid === from || input.covered?.(oid) === true;
195
+ const asking = judged === void 0 ? void 0 : (open) => judged.has(open, { signal: input.signal }).pipe(Effect.catch(refusedPastBound));
196
+ const walked = yield* walkedDown(input, to, ends, void 0, limit);
197
+ if (walked !== void 0) {
198
+ const leads = leadingTo(walked, from);
199
+ const asked = walked.filter((commit) => !leads.has(commit.oid)).map((commit) => commit.oid);
200
+ const held = asking === void 0 || asked.length === 0 ? /* @__PURE__ */ new Set() : yield* asking(asked);
201
+ const onDefault = yield* defaultHolds(input, from, [...leads]);
202
+ return walked.filter((commit) => !held.has(commit.oid) && !onDefault.has(commit.oid));
203
+ }
204
+ const added = asking === void 0 ? void 0 : yield* walkedDown(input, to, ends, asking, limit);
205
+ if (added === void 0) return yield* Effect.fail(movePastLimit(limit));
206
+ return added;
135
207
  });
136
208
  /**
137
209
  * `CommitChangesInput.covered` for a rule over the refs `covers` names: whether a commit is the
@@ -153,7 +225,7 @@ const commitChangesOf = (input, updates) => Stream.unwrap(Effect.gen(function* (
153
225
  const olds = updates.map((update) => update.from);
154
226
  const search = chainSearch(input, [...olds, input.defaultTip]);
155
227
  const tips = /* @__PURE__ */ new Set();
156
- const pushedPaths = (commit, listed) => Stream.unwrap(Effect.map(input.source === "git" ? pushedJudged(input, commit, listed, olds) : Effect.succeed({
228
+ const pushedPaths = (commit, listed, judges = input.source === "git") => Stream.unwrap(Effect.map(judges ? pushedJudged(input, commit, listed, olds) : Effect.succeed({
157
229
  ...commit,
158
230
  counted: commit.parents,
159
231
  left: []
@@ -179,16 +251,12 @@ const commitChangesOf = (input, updates) => Stream.unwrap(Effect.gen(function* (
179
251
  const moved = Stream.fromIterable(updates).pipe(Stream.flatMap((update) => Stream.unwrap(Effect.gen(function* () {
180
252
  const { to } = update;
181
253
  if (to === null || tips.has(to)) return Stream.empty;
182
- const judged = yield* movedTip(input, {
254
+ const added = yield* addedBy(input, {
183
255
  ...update,
184
256
  to
185
257
  });
186
- if (judged === void 0) return Stream.empty;
187
- return judgedPaths(input, judged, search, allotment).pipe(Stream.map((change) => ({
188
- commit: to,
189
- root: false,
190
- change
191
- })));
258
+ const listed = new Set(added.map((commit) => commit.oid));
259
+ return Stream.fromIterable(added).pipe(Stream.flatMap((commit) => pushedPaths(commit, listed, true)));
192
260
  }))));
193
261
  return Stream.concat(pushed, moved);
194
262
  }));
@@ -1,5 +1,6 @@
1
1
  import { gitError } from "../contracts/result.mjs";
2
2
  import { hexOf } from "../protocol/bytes/hex.mjs";
3
+ import { emptyTree } from "../protocol/objects/object.mjs";
3
4
  import { indexHeadBytes } from "../protocol/packs/idx.mjs";
4
5
  import { checkTypesHeader, checkTypesPack, typeAt, typeOfByte, typesPackAt } from "../protocol/packs/types-file.mjs";
5
6
  import { readWhole } from "./calls.mjs";
@@ -135,7 +136,7 @@ const storedOf = (pack) => isPackId(pack.id) ? Effect.succeed({
135
136
  * push asks its links, they make at most `heldTypesCalls` of the listed packs in all.
136
137
  */
137
138
  const repoTypes = (input) => {
138
- const { store, bucket, indexes, listed, memory, reader, visible } = input;
139
+ const { store, bucket, indexes, listed, memory, reader, visible, hasLooseRows = true } = input;
139
140
  const rows = rowHeaders(store, listed);
140
141
  const forward = /* @__PURE__ */ new Map();
141
142
  const opened = /* @__PURE__ */ new Map();
@@ -204,11 +205,18 @@ const repoTypes = (input) => {
204
205
  const oids = yield* shownOf(asked);
205
206
  yield* charge(oids.length, sizes);
206
207
  const answer = /* @__PURE__ */ new Map();
207
- for (const [oid, { type }] of yield* rows(oids)) answer.set(oid, {
208
+ const addRows = Effect.fnUntraced(function* (ids) {
209
+ for (const [oid, { type }] of yield* rows(ids)) answer.set(oid, {
210
+ pack: void 0,
211
+ type
212
+ });
213
+ });
214
+ if (hasLooseRows) yield* addRows(oids);
215
+ else if (oids.includes("4b825dc642cb6eb9a060e54bf8d69288fbee4904")) answer.set(emptyTree, {
208
216
  pack: void 0,
209
- type
217
+ type: "tree"
210
218
  });
211
- let left = oids.filter((oid) => !answer.has(oid));
219
+ let left = answer.size === 0 ? oids : oids.filter((oid) => !answer.has(oid));
212
220
  for (let from = 0; from < listed.packs.length && left.length > 0; from += 6) {
213
221
  const window = listed.packs.slice(from, from + 6);
214
222
  const asked = left;
@@ -226,6 +234,7 @@ const repoTypes = (input) => {
226
234
  });
227
235
  left = left.filter((oid) => !answer.has(oid));
228
236
  }
237
+ if (!hasLooseRows && left.length > 0) yield* addRows(left);
229
238
  return answer;
230
239
  }),
231
240
  beyond: Effect.fnUntraced(function* (asked) {
@@ -312,6 +312,225 @@ const partBytesMost = uploadPartBytes;
312
312
  /** A bitmap's entry in a part: its position, its number and its EWAH's bytes. */
313
313
  const partEntryBytes = 12;
314
314
  /**
315
+ * Preferred byte budget for earlier bitmaps-part footer tables held at once by `PriorParts` in a
316
+ * multi-part whole build: each entry takes 16 bytes (12-byte footer entry plus 4-byte offset), so
317
+ * 1 MiB holds about 65,000 entries (~40 full 8 MiB parts). `room` is
318
+ * `min(totalFooters, max(priorFootersMost, largestFooter))`, so `room` can exceed 1 MiB up to the
319
+ * single largest part's footer (strictly below `partBytesMost`, 8 MiB, since each entry needs 12
320
+ * footer bytes plus at least 4 payload bytes, bounding `n <= floor((8 MiB - 4) / 16)` and
321
+ * `16n + 4 < 8 MiB`), and is always charged alongside `priorLookupBytes` before allocation.
322
+ */
323
+ const priorFootersMost = 1048576;
324
+ /** Bytes `PriorParts` holds for one part's footer table: raw entries + count, plus prefix offsets. */
325
+ const priorFooterBytes = (count) => count * 16 + 4;
326
+ /**
327
+ * Bytes `PriorParts` holds for its sorted position lookup (`Uint32Array` positions and order
328
+ * indices, 8 bytes per completed bitmap) plus cumulative part counts (`Uint32Array`, 4 bytes per
329
+ * earlier part). For 2,157 bitmaps across 2 parts that is ~17 KiB; even if all 1,000,000 objects
330
+ * in a 1,000,000-object repo were chosen across 128 parts, it is 8,000,512 bytes (~7.63 MiB),
331
+ * computed and charged before allocation and released on exit.
332
+ */
333
+ const priorLookupBytes = (parts, bitmaps) => bitmaps * 8 + parts * 4;
334
+ /** Memory reservation `PriorParts` needs before construction: lookup tables plus footer LRU room. */
335
+ const priorPartsRoom = (parts, most) => {
336
+ if (parts.length === 0) return {
337
+ lookup: 0,
338
+ footers: 0,
339
+ total: 0
340
+ };
341
+ let bitmaps = 0;
342
+ let totalFooters = 0;
343
+ let largestFooter = 0;
344
+ for (const part of parts) {
345
+ bitmaps += part.count;
346
+ const footerSize = priorFooterBytes(part.count);
347
+ totalFooters += footerSize;
348
+ largestFooter = Math.max(largestFooter, footerSize);
349
+ }
350
+ const lookup = priorLookupBytes(parts.length, bitmaps);
351
+ const footers = Math.min(totalFooters, Math.max(most, largestFooter));
352
+ return {
353
+ lookup,
354
+ footers,
355
+ total: lookup + footers
356
+ };
357
+ };
358
+ /** Sorts parallel `keys` and `values` ascending by `keys` in place with O(1) extra space. */
359
+ const sortPairs = (keys, values) => {
360
+ const length = keys.length;
361
+ const swap = (a, b) => {
362
+ const key = keys[a] ?? 0;
363
+ keys[a] = keys[b] ?? 0;
364
+ keys[b] = key;
365
+ const value = values[a] ?? 0;
366
+ values[a] = values[b] ?? 0;
367
+ values[b] = value;
368
+ };
369
+ const siftDown = (start, end) => {
370
+ let root = start;
371
+ while (root * 2 + 1 < end) {
372
+ const left = root * 2 + 1;
373
+ const right = left + 1;
374
+ let largest = root;
375
+ if ((keys[left] ?? 0) > (keys[largest] ?? 0)) largest = left;
376
+ if (right < end && (keys[right] ?? 0) > (keys[largest] ?? 0)) largest = right;
377
+ if (largest === root) return;
378
+ swap(root, largest);
379
+ root = largest;
380
+ }
381
+ };
382
+ for (let start = Math.floor(length / 2) - 1; start >= 0; start -= 1) siftDown(start, length);
383
+ for (let end = length - 1; end > 0; end -= 1) {
384
+ swap(0, end);
385
+ siftDown(0, end);
386
+ }
387
+ };
388
+ /**
389
+ * Reads bitmaps written by earlier parts of a multi-part whole build on demand, holding at most
390
+ * `room` bytes of part footer tables in LRU order (`room` can exceed `most`, up to the single
391
+ * largest part's footer, strictly below `partBytesMost`) and `lookupBytes` of typed lookup tables.
392
+ * Each part's footer is read once per cache residency (one size lookup plus one suffix range read;
393
+ * eviction permits rereads) and each needed bitmap takes one small range read, avoiding a full
394
+ * graph re-walk through all edges files on every later part.
395
+ */
396
+ var PriorParts = class {
397
+ #bucket;
398
+ #parts;
399
+ #positions;
400
+ #orderIndices;
401
+ #partEnds;
402
+ #held = /* @__PURE__ */ new Map();
403
+ #bytes = 0;
404
+ /** The bytes held by the typed position lookup and cumulative part-count arrays. */
405
+ lookupBytes;
406
+ /** The most bytes of footer tables it holds at once (may exceed `most`, up to largest footer). */
407
+ room;
408
+ /** Part footers read, for tests to count. */
409
+ footerReads = 0;
410
+ /** Individual bitmaps range-read from earlier parts, for tests to count. */
411
+ bitmapReads = 0;
412
+ constructor(bucket, parts, order, most) {
413
+ this.#bucket = bucket;
414
+ this.#parts = parts;
415
+ const plan = priorPartsRoom(parts, most);
416
+ this.lookupBytes = plan.lookup;
417
+ this.room = plan.footers;
418
+ this.#partEnds = new Uint32Array(parts.length);
419
+ let at = 0;
420
+ for (const [partNumber, part] of parts.entries()) {
421
+ at += part.count;
422
+ this.#partEnds[partNumber] = at;
423
+ }
424
+ const count = Math.min(at, order.length);
425
+ this.#positions = new Uint32Array(count);
426
+ this.#orderIndices = new Uint32Array(count);
427
+ for (let ord = 0; ord < count; ord += 1) {
428
+ this.#positions[ord] = order[ord]?.position ?? 0;
429
+ this.#orderIndices[ord] = ord;
430
+ }
431
+ sortPairs(this.#positions, this.#orderIndices);
432
+ }
433
+ /** The bytes of footer tables held now, for tests to check against `room`. */
434
+ get held() {
435
+ return this.#bytes;
436
+ }
437
+ #orderIndexOf(position) {
438
+ let low = 0;
439
+ let high = this.#positions.length - 1;
440
+ while (low <= high) {
441
+ const mid = low + high >>> 1;
442
+ const value = this.#positions[mid] ?? 0;
443
+ if (value === position) return this.#orderIndices[mid] ?? 0;
444
+ if (value < position) low = mid + 1;
445
+ else high = mid - 1;
446
+ }
447
+ return -1;
448
+ }
449
+ #locate(position) {
450
+ const ord = this.#orderIndexOf(position);
451
+ if (ord === -1) return void 0;
452
+ let low = 0;
453
+ let high = this.#partEnds.length - 1;
454
+ while (low < high) {
455
+ const mid = low + high >>> 1;
456
+ if ((this.#partEnds[mid] ?? 0) <= ord) low = mid + 1;
457
+ else high = mid;
458
+ }
459
+ const start = low === 0 ? 0 : this.#partEnds[low - 1] ?? 0;
460
+ return {
461
+ part: low,
462
+ index: ord - start
463
+ };
464
+ }
465
+ has(position) {
466
+ return this.#orderIndexOf(position) !== -1;
467
+ }
468
+ read(position) {
469
+ return Effect.suspend(() => {
470
+ const location = this.#locate(position);
471
+ const part = location === void 0 ? void 0 : this.#parts[location.part];
472
+ if (location === void 0 || part === void 0) return Effect.fail(lost(`No prior part holds bitmap position ${position}`));
473
+ return Effect.flatMap(this.#footerOf(location.part, part), (footer) => {
474
+ const entry = location.index * partEntryBytes;
475
+ const storedPosition = footer.view.getUint32(entry);
476
+ if (storedPosition !== position) return Effect.fail(lost(`${part.key}'s entry ${location.index} is position ${storedPosition}, not ${position}`));
477
+ const length = footer.view.getUint32(entry + 8);
478
+ const offset = footer.offsets[location.index] ?? 0;
479
+ return Effect.map(readExactly(this.#bucket, part.key, offset, length), (raw) => {
480
+ this.bitmapReads += 1;
481
+ const ewah = new Uint32Array(length / 4);
482
+ const rawView = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
483
+ for (let word = 0; word < ewah.length; word += 1) ewah[word] = rawView.getUint32(word * 4);
484
+ return ewah;
485
+ });
486
+ });
487
+ });
488
+ }
489
+ #footerOf(partNumber, part) {
490
+ return Effect.suspend(() => {
491
+ const cached = this.#held.get(partNumber);
492
+ if (cached !== void 0) {
493
+ this.#held.delete(partNumber);
494
+ this.#held.set(partNumber, cached);
495
+ return Effect.succeed(cached);
496
+ }
497
+ const needed = priorFooterBytes(part.count);
498
+ for (const [heldPart, kept] of this.#held) {
499
+ if (this.#bytes + needed <= this.room) break;
500
+ this.#held.delete(heldPart);
501
+ this.#bytes -= kept.bytes;
502
+ }
503
+ const rawFooterBytes = part.count * partEntryBytes + 4;
504
+ return Effect.flatMap(storedSize(this.#bucket, part.key), (size) => {
505
+ if (size === void 0 || size < rawFooterBytes) return Effect.fail(lost(`${part.key} is missing or cut short`));
506
+ return Effect.flatMap(readExactly(this.#bucket, part.key, size - rawFooterBytes, rawFooterBytes), (bytes) => {
507
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
508
+ const count = view.getUint32(bytes.byteLength - 4);
509
+ if (count !== part.count) return Effect.fail(lost(`${part.key} holds ${count} bitmaps, not ${part.count}`));
510
+ const offsets = new Uint32Array(count);
511
+ let offset = 0;
512
+ for (let at = 0; at < count; at += 1) {
513
+ const length = view.getUint32(at * partEntryBytes + 8);
514
+ if (length % 4 !== 0) return Effect.fail(lost(`${part.key}'s bitmap ${at} has unaligned length`));
515
+ offsets[at] = offset;
516
+ offset += length;
517
+ }
518
+ if (offset !== size - rawFooterBytes) return Effect.fail(lost(`${part.key}'s entries don't fill its bytes`));
519
+ this.footerReads += 1;
520
+ const footer = {
521
+ view,
522
+ offsets,
523
+ bytes: needed
524
+ };
525
+ this.#held.set(partNumber, footer);
526
+ this.#bytes += needed;
527
+ return Effect.succeed(footer);
528
+ });
529
+ });
530
+ });
531
+ }
532
+ };
533
+ /**
315
534
  * One try's part of a whole build's bitmaps, held until it's put: each bitmap's EWAH words,
316
535
  * big-endian, as a `.bitmaps` holds them, then each one's position, number and length, then
317
536
  * their count. At most `most` bytes.
@@ -471,6 +690,15 @@ const bitmapsPut = /* @__PURE__ */ Effect.fnUntraced(function* (bucket, key, pos
471
690
  };
472
691
  });
473
692
  /**
693
+ * What `writeBitmaps` grows its allotment by before walking: the walk's buffers (`walkBytes`),
694
+ * the bitmaps it keeps in `made` (`kept`), the streaming put buffer when writing `.bitmaps`
695
+ * directly (not a part), and the layer-below arrays when updating.
696
+ */
697
+ const bitmapsWalkBytes = (positions, kept, options) => {
698
+ const words = bitmapWords(positions);
699
+ return walkBytes(positions) + kept + (options?.part === true ? 0 : Math.min(bitmapsMostBytes(positions, options?.count ?? 0), uploadPartBytes)) + (options?.below === true ? words * 12 : 0);
700
+ };
701
+ /**
474
702
  * A bitmap for each of `input.chosen`, oldest first, each the set of positions its object
475
703
  * reaches: forward sweeps over the positions, each taking the links of every set position not
476
704
  * yet taken, until one sets nothing behind it. A position with a bitmap already made is ORed
@@ -480,13 +708,16 @@ const bitmapsPut = /* @__PURE__ */ Effect.fnUntraced(function* (bucket, key, pos
480
708
  * bucket as they're made, the table after them.
481
709
  */
482
710
  const writeBitmaps = /* @__PURE__ */ Effect.fnUntraced(function* (input) {
483
- const { edges, positions, below, part } = input;
711
+ const { edges, positions, below, part, prior } = input;
484
712
  const words = bitmapWords(positions);
485
713
  const order = bitmapOrder(input.chosen);
486
714
  const count = order.length;
487
- const atMost = bitmapsMostBytes(positions, count);
488
- const held = walkBytes(positions) + input.kept + (part === void 0 ? Math.min(atMost, uploadPartBytes) : 0) + (below === void 0 ? 0 : words * 12);
489
- yield* input.allotment.grow(held);
715
+ const held = input.walkReserved === true ? 0 : bitmapsWalkBytes(positions, input.kept, {
716
+ part: part !== void 0,
717
+ below: below !== void 0,
718
+ count
719
+ });
720
+ if (held > 0) yield* input.allotment.grow(held);
490
721
  const put = part === void 0 ? yield* bitmapsPut(input.bucket, input.key, positions, count) : void 0;
491
722
  const reached = new Uint32Array(words);
492
723
  const taken = new Uint32Array(words);
@@ -501,6 +732,21 @@ const writeBitmaps = /* @__PURE__ */ Effect.fnUntraced(function* (input) {
501
732
  let madeBytes = 0;
502
733
  let again = false;
503
734
  let word = 0;
735
+ const keepMade = (position, ewah) => {
736
+ if (ewah.byteLength > input.kept) return;
737
+ const existing = made.get(position);
738
+ if (existing !== void 0) {
739
+ made.delete(position);
740
+ madeBytes -= existing.byteLength;
741
+ }
742
+ made.set(position, ewah);
743
+ madeBytes += ewah.byteLength;
744
+ for (const [keptPosition, kept] of made) {
745
+ if (madeBytes <= input.kept) break;
746
+ made.delete(keptPosition);
747
+ madeBytes -= kept.byteLength;
748
+ }
749
+ };
504
750
  const reach = (target) => {
505
751
  const at = target >>> 5;
506
752
  const bit = 1 << (target & 31);
@@ -527,8 +773,15 @@ const writeBitmaps = /* @__PURE__ */ Effect.fnUntraced(function* (input) {
527
773
  const closed = yield* below.ready(position);
528
774
  if (closed === void 0) waiting[word] = (waiting[word] ?? 0) | 1 << bit;
529
775
  else for (const into of closing) orInto(closed, into);
530
- } else if (stored !== void 0) for (const into of closing) orEwah(stored, into);
531
- else if (!edges.links(position, reach)) {
776
+ } else if (stored !== void 0) {
777
+ made.delete(position);
778
+ made.set(position, stored);
779
+ for (const into of closing) orEwah(stored, into);
780
+ } else if (position !== chosen.position && prior?.has(position) === true) {
781
+ const ewah = yield* prior.read(position);
782
+ for (const into of closing) if (!orEwah(ewah, into)) return yield* Effect.fail(lost(`The prior bitmap at ${position} is unreadable`));
783
+ keepMade(position, ewah);
784
+ } else if (!edges.links(position, reach)) {
532
785
  yield* edges.load;
533
786
  continue;
534
787
  }
@@ -561,26 +814,20 @@ const writeBitmaps = /* @__PURE__ */ Effect.fnUntraced(function* (input) {
561
814
  }
562
815
  if (!closedAll) {
563
816
  if (put !== void 0) yield* put.abort;
564
- input.allotment.shrink(held);
817
+ if (held > 0) input.allotment.shrink(held);
565
818
  return;
566
819
  }
567
820
  const ewah = ewahOf(reached);
568
821
  if (put === void 0) part?.into.add(chosen.position, chosen.number, ewah);
569
822
  else yield* put.add(chosen.position, chosen.number, ewah);
570
- made.set(chosen.position, ewah);
571
- madeBytes += ewah.byteLength;
572
- for (const [position, kept] of made) {
573
- if (madeBytes <= input.kept) break;
574
- made.delete(position);
575
- madeBytes -= kept.byteLength;
576
- }
823
+ keepMade(chosen.position, ewah);
577
824
  }
578
825
  const written = put === void 0 ? {
579
826
  count: part?.into.count ?? 0,
580
827
  table: 0,
581
828
  bytes: 0
582
829
  } : yield* put.finish;
583
- input.allotment.shrink(held);
830
+ if (held > 0) input.allotment.shrink(held);
584
831
  return written;
585
832
  }, Effect.scoped);
586
833
  /** How many bytes of a layer's `.bitmaps` a merge reads as one range, an entry past it alone. */
@@ -824,4 +1071,4 @@ const storedBitmaps = /* @__PURE__ */ Effect.fnUntraced(function* (bucket, key,
824
1071
  bytes: size
825
1072
  };
826
1073
  });
827
- export { BitmapsPart, EdgeFiles, bitmapOrder, bitmapsMostBytes, bitmapsPut, copyBitmaps, copyHeldBytes, copyRunBytes, eachBitmapBytes, edgesBytesOf, edgesFileOf, ewahMostWords, isKeepUnclosed, isMergeIncomplete, keepUnclosed, mergeIncomplete, noBase, noNumber, openBitmaps, orderKey, partBitmaps, partBytesMost, runGapBytes, runsMost, storedBitmaps, storedSize, walkBytes, writeBitmaps };
1074
+ export { BitmapsPart, EdgeFiles, PriorParts, bitmapOrder, bitmapsMostBytes, bitmapsPut, bitmapsWalkBytes, copyBitmaps, copyHeldBytes, copyRunBytes, eachBitmapBytes, edgesBytesOf, edgesFileOf, ewahMostWords, isKeepUnclosed, isMergeIncomplete, keepUnclosed, mergeIncomplete, noBase, noNumber, openBitmaps, orderKey, partBitmaps, partBytesMost, priorFooterBytes, priorFootersMost, priorLookupBytes, priorPartsRoom, runGapBytes, runsMost, storedBitmaps, storedSize, walkBytes, writeBitmaps };
@@ -11,7 +11,7 @@ import { rootsDigest } from "../store/repo-record.mjs";
11
11
  import { randomText } from "../store/random.mjs";
12
12
  import { allottableOf, uploadPartBytes } from "./budget.mjs";
13
13
  import { bucketDeleteMany, bucketDo, bucketPut, joined, readEach, readWhole, sizedPutCalls, streamedPutCalls } from "./calls.mjs";
14
- import { BitmapsPart, EdgeFiles, bitmapOrder, bitmapsMostBytes, bitmapsPut, copyBitmaps, copyHeldBytes, copyRunBytes, edgesBytesOf, edgesFileOf, ewahMostWords, noNumber, orderKey, partBitmaps, partBytesMost, runsMost, storedBitmaps, storedSize, writeBitmaps } from "./index-bitmaps.mjs";
14
+ import { BitmapsPart, EdgeFiles, PriorParts, bitmapOrder, bitmapsMostBytes, bitmapsPut, bitmapsWalkBytes, copyBitmaps, copyHeldBytes, copyRunBytes, edgesBytesOf, edgesFileOf, ewahMostWords, noNumber, orderKey, partBitmaps, partBytesMost, priorFootersMost, priorPartsRoom, runsMost, storedBitmaps, storedSize, writeBitmaps } from "./index-bitmaps.mjs";
15
15
  import { BasesWriter, TypesWriter, indexBasesBytes, indexTypesBytes, openBases, openTypes, typeCodes } from "./index-files.mjs";
16
16
  import { bitmapOf, bitmapWords, ewahOf, orEwah } from "./ewah.mjs";
17
17
  import { absent, graphChunk, indexGraphBytes, keptIdsBytes, keptIdsOf, mergeHeldBytes, mergeIndexGraphs, openGraphLayers, openIndexGraph, writeIndexGraph } from "./index-graph.mjs";
@@ -1493,13 +1493,31 @@ const bitmapsCutTry = /* @__PURE__ */ Effect.fnUntraced(function* (parts, files,
1493
1493
  };
1494
1494
  }
1495
1495
  const into = new BitmapsPart(partBytesMost);
1496
- const made = yield* Effect.result(cutAtSpent(writeBitmaps({
1497
- ...input,
1498
- part: {
1499
- from: cut.done,
1500
- into
1501
- }
1502
- })));
1496
+ const walkHeld = bitmapsWalkBytes(input.positions, input.kept, { part: true });
1497
+ yield* allotment.grow(walkHeld);
1498
+ let priorCharged = 0;
1499
+ const made = yield* Effect.ensuring(Effect.gen(function* () {
1500
+ const priorParts = cut.parts.map((held, number) => ({
1501
+ key: files.bitmapsPart(cut.order, number),
1502
+ count: held
1503
+ }));
1504
+ const priorPlan = priorPartsRoom(priorParts, priorFootersMost);
1505
+ if (priorPlan.total > 0 && allotment.tryGrow(priorPlan.total)) priorCharged = priorPlan.total;
1506
+ const prior = priorCharged > 0 ? new PriorParts(input.bucket, priorParts, order, priorFootersMost) : void 0;
1507
+ const writeInput = {
1508
+ ...input,
1509
+ part: {
1510
+ from: cut.done,
1511
+ into
1512
+ },
1513
+ walkReserved: true
1514
+ };
1515
+ if (prior !== void 0) writeInput.prior = prior;
1516
+ return yield* Effect.result(cutAtSpent(writeBitmaps(writeInput)));
1517
+ }), Effect.sync(() => {
1518
+ if (priorCharged > 0) allotment.shrink(priorCharged);
1519
+ allotment.shrink(walkHeld);
1520
+ }));
1503
1521
  if (Result.isFailure(made)) {
1504
1522
  const spent = spentOf(made.failure);
1505
1523
  if (spent === void 0) return yield* Effect.fail(made.failure);
@@ -410,10 +410,12 @@ const storePack = /* @__PURE__ */ Effect.fnUntraced(function* (input, opened, st
410
410
  const boundary = new Set(joining === void 0 ? receive.boundary : []);
411
411
  yield* room.take(boundary.size * 120) ?? Effect.void;
412
412
  const named = !partial && new Set(receive.boundary).size === 0;
413
+ const looseRows = receive.hasLooseRows === false ? { hasLooseRows: false } : {};
413
414
  const typesHeld = repoTypes({
414
415
  ...reading,
415
416
  memory: memory.join,
416
- reader
417
+ reader,
418
+ ...looseRows
417
419
  });
418
420
  const linkedTypes = (there) => {
419
421
  const types = /* @__PURE__ */ new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ref0/git",
3
- "version": "0.25.0",
3
+ "version": "0.26.0",
4
4
  "private": false,
5
5
  "description": "Git hosting as a library: a server, a client and their merges",
6
6
  "license": "Apache-2.0",
@@ -47,12 +47,12 @@
47
47
  }
48
48
  },
49
49
  "dependencies": {
50
- "@ref0/infra": "0.25.0",
50
+ "@ref0/infra": "0.26.0",
51
51
  "pako": "2.1.0"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@effect/vitest": "4.1.7",
55
- "@ref0/xdiff-wasm": "0.25.0",
55
+ "@ref0/xdiff-wasm": "0.26.0",
56
56
  "effect": "4.1.7",
57
57
  "vite-plus": "1.8.0",
58
58
  "vitest": "5.1.1",