@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 5,387 lines 213 KB
Raw
1 /**
2 * The per-repo Durable Object (DESIGN.md §2, §3.4, §3.6, §8).
3 *
4 * One DO per repo, addressed by `getByName(repoId)` (a ULID — never
5 * `owner/name`, so renames never move data). The DO owns refs, the object
6 * index and loose bytes, tokens, the commit graph, push staging, and the
7 * alarm jobs (import / fork / delete-purge / staging-GC). It runs the git
8 * v0 wire protocol in its `fetch` handler — advertisement, upload-pack,
9 * receive-pack — because every byte those need lives in this DO's SQLite.
10 *
11 * NOTE: the protocol *choreography* (advertisement builder, request
12 * parsers, pack writer/parser) is implemented in this file against the
13 * pure codec primitives in `src/Protocol/` (`Pkt`, `Sideband`, `ObjectCodec`,
14 * `Delta`, `Zlib`, `Store`). DESIGN.md places these in
15 * `src/git/{Advertise,UploadPack,ReceivePack,PackParser,PackWriter}.ts`;
16 * they live here (exported) until the git-layer split lands — moving them
17 * is a mechanical extraction.
18 *
19 * ### Wire endpoints
20 * The Worker proxies these untouched to `stub.fetch`:
21 * **Example:** Routes served by this DO's fetch handler
22 * ```
23 * GET /:owner/:repo[.git]/info/refs?service=git-(upload|receive)-pack
24 * POST /:owner/:repo[.git]/git-upload-pack
25 * POST /:owner/:repo[.git]/git-receive-pack
26 * ```
27 */
28 import * as Cloudflare from "@alchemy.run/cloudflare";
29 import type * as cf from "@cloudflare/workers-types";
30 import type { HttpEffect } from "alchemy/Http";
31 import { RuntimeContext } from "alchemy/RuntimeContext";
32 import * as Context from "effect/Context";
33 import * as Effect from "effect/Effect";
34 import * as Fiber from "effect/Fiber";
35 import * as FetchHttpClient from "effect/http/FetchHttpClient";
36 import * as HttpServerRequest from "effect/http/HttpServerRequest";
37 import * as HttpServerResponse from "effect/http/HttpServerResponse";
38 import * as Option from "effect/Option";
39 import * as Result from "effect/Result";
40 import * as Schema from "effect/Schema";
41 import * as Semaphore from "effect/Semaphore";
42 import * as Stream from "effect/Stream";
43 import {
44 BranchMissing,
45 MergeConflict,
46 NoMergeBase,
47 NothingToMerge,
48 ObjectNotFound,
49 PullExists,
50 PullNotFound,
51 PullStateConflict,
52 ReadOnlyRepo,
53 RefConflict,
54 RefNotFound,
55 RepoNotFound,
56 ValidationError,
57 WrongObjectType,
58 type Oid as ApiOid,
59 type RepoStatus,
60 } from "./Api.ts";
61 import type { BlobMultipart, UploadedPart } from "./BlobStore.ts";
62 import { BlobStore, type BlobStoreShape } from "./BlobStore.ts";
63 import { type HasherShape, type HashPartResult } from "./Hasher/Hasher.ts";
64 import { bundleCovers, runBundleJob, type BundleInfo } from "./Jobs/Bundle.ts";
65 import { runCompactJob, runGeometricMergeJob, shouldCompact } from "./Jobs/Compact.ts";
66 import { runForkJob, snapshotStream, type SnapshotChunk } from "./Jobs/Fork.ts";
67 import { runImport, type ImportSource } from "./Jobs/Import.ts";
68 import { runPurgeJob } from "./Jobs/Purge.ts";
69 import { applyDelta } from "./Protocol/Delta.ts";
70 import {
71 bytesToHex,
72 concatBytes,
73 decodeOfsDeltaOffset,
74 decodeTypeSize,
75 encodeCommit,
76 encodeTypeSize,
77 hashObject,
78 isDeltaType,
79 isOid,
80 makeSha1,
81 ObjectType,
82 objectTypeName,
83 parseCommit,
84 parseTag,
85 parseTree,
86 treeEntryKind,
87 utf8Encode,
88 ZERO_OID,
89 type ObjectTypeName,
90 type Oid,
91 } from "./Protocol/ObjectCodec.ts";
92 import * as PackParser from "./Protocol/PackParser.ts";
93 import type { RandomAccess } from "./Protocol/PackParser.ts";
94 import * as PartialScan from "./Protocol/PartialScan.ts";
95 import type { ScanResult } from "./Protocol/PartialScan.ts";
96 import type { DeltaBase, DeltaJob, UnresolvedDelta } from "./Protocol/PartialScan.ts";
97 import {
98 decodePktLines,
99 errPkt,
100 flushPkt,
101 pktPayloadText,
102 pktText,
103 readPktLineAt,
104 type PktLine,
105 } from "./Protocol/Pkt.ts";
106 import { progressMessage, pumpPackBody } from "./Protocol/Sideband.ts";
107 import { StoreError, type ManifestEntry, type ObjectSource } from "./Protocol/Store.ts";
108 import {
109 applyTreeChanges,
110 conflictingPaths,
111 diffTrees,
112 type DiffEntryData,
113 } from "./Protocol/TreeDiff.ts";
114 import * as Zlib from "./Protocol/Zlib.ts";
115 import { decodeStagedBatch } from "./PushWire.ts";
116 import { RegistryStore, ulid } from "./RegistryObject.ts";
117 import { computeClosure } from "./Store/Closure.ts";
118 import { encodeHeadSnapshot, type HeadSnapshot } from "./Store/HeadSnapshot.ts";
119 import { SPILL_PART_BYTES } from "./Store/IncomingBody.ts";
120 import { headKey, packKeyOf } from "./Store/Keys.ts";
121 import {
122 makeObjectStore,
123 MAX_OBJECT_SIZE,
124 type ObjectStore,
125 type StagedObject,
126 LIVE_OBJECTS,
127 } from "./Store/ObjectStore.ts";
128 import { blobRandomAccess } from "./Store/PackSource.ts";
129 import {
130 initRepoSchema,
131 makeSqlClient,
132 type JobRow,
133 type PullRow,
134 type RefRow,
135 } from "./Store/Sql.ts";
136 import type { StreamingFeeder } from "./Store/StreamingSource.ts";
137 import { BACKPRESSURE_BYTES, RETAIN_BYTES } from "./Store/StreamingSource.ts";
138
139 // ─────────────────────────────────────────────────────────────────────────────
140 // Constants
141 // ─────────────────────────────────────────────────────────────────────────────
142
143 /**
144 * Pushes up to this size are ingested straight from memory; larger ones are
145 * parked in R2 and parsed from there (DESIGN.md §3.6). A **buffering
146 * threshold, not a limit** — a push larger than this is no longer rejected.
147 *
148 * The spill threshold of the streaming receive (`receiveWireBody`): bodies
149 * up to this size are buffered and parsed from memory (the fast path, at
150 * 50 MiB the bound v1 validated for a 128 MB isolate); larger bodies are
151 * never materialized — they stream into an R2 multipart upload as they
152 * arrive and are parsed back through bounded windows.
153 *
154 * Measured on the 38 MiB alchemy-repo push: ~20 s in memory, ~37 s through
155 * R2. The R2 path costs about 2x, and is unbounded — the full 67 MiB
156 * alchemy history (44k objects) pushed in 92 s.
157 */
158 export const MAX_PACK_BYTES = 4 * 1024 * 1024;
159 // 4 MiB (was 50, then 24): with promoted wire packs (DESIGN §22.5) the
160 // spilled path is the FAST one — it never writes blob bytes to SQLite, so
161 // staging and the staged→live flip cost milliseconds instead of seconds
162 // (a 5.7 MiB in-memory push spent 6 s in finalize on production; a 40 MiB
163 // spilled one 0.7 s). Only genuinely small pushes stay in memory.
164 // 24 MiB, down from the 50 MiB v1 validated with ONE active repo: instances
165 // of this class share an isolate, so the in-memory pack, the ingest's
166 // resolved-content LRU (20 MiB) and staging batch (8 MiB) sit beside every
167 // other active repo's working set. DESIGN §22.4 records the OOM that
168 // motivated it; the R2 path costs ~2x on the bytes above the threshold.
169
170 /** Objects staged per SQL transaction during ingest (DESIGN.md §16.6). */
171 /** Bytes per hasher part — the spill part size, so a part is one R2 part (DESIGN §22.7). */
172 export const HASH_PART_BYTES = 4 * 1024 * 1024;
173 /** Hasher calls in flight per push (DESIGN §22.8). */
174 export const HASH_CONCURRENCY = 8;
175 // 4 MiB (was 8): the hasher chain is sequential (each part needs the
176 // previous part's boundary), so a smaller part shortens both the tail after
177 // the upload ends and the granularity at which the upload can run ahead.
178
179 export const STAGE_BATCH_OBJECTS = 2048;
180 // 2048 (was 256): measured on production, staging cost tracked the number
181 // of transaction COMMITs (~100 ms each), not rows — 61 commits for a
182 // 15.6k-object push. Objects average a few KiB, so 2048 stays well under
183 // STAGE_BATCH_BYTES; the byte cap still bounds memory for large objects.
184
185 /** Byte budget per staging batch, so a batch of large blobs stays bounded. */
186 export const STAGE_BATCH_BYTES = 8 * 1024 * 1024;
187
188 // The tree-diff caps (`MAX_DIFF_FILES`, `MAX_DIFF_TREE_READS`) live with
189 // the pure walk in `Protocol/TreeDiff.ts`; re-exported here alongside the other
190 // serving-plane bounds.
191 export { MAX_DIFF_FILES, MAX_DIFF_TREE_READS } from "./Protocol/TreeDiff.ts";
192
193 /** Max commits popped by the merge-base paint walk. */
194 export const MAX_COMPARE_WALK = 10_000;
195
196 /** Max commits returned in a Comparison's `commits` array. */
197 export const MAX_COMPARE_COMMITS = 250;
198
199 /**
200 * Max commits popped by the PR live-field walk (`getPull`). Saturation
201 * makes the numeric detail fields `null` (reason `"unknown"`); merge still
202 * works because {@link MAX_PULL_MERGE_WALK} gives the merge path a deeper
203 * budget.
204 */
205 export const MAX_PULL_WALK = 5_000;
206
207 /** Deeper walk budget for `mergePull` (needs only reachability + one base). */
208 export const MAX_PULL_MERGE_WALK = 50_000;
209
210 /** Conflicting paths reported on a `MergeConflict` (the set may be larger). */
211 export const MAX_CONFLICT_PATHS = 20;
212
213 /** Agent string advertised on the wire. */
214 export const GIT_AGENT = "git-service/1";
215
216 /** How long a push waits for ingest memory before answering 503. */
217 export const PUSH_WAIT_TIMEOUT = "30 seconds";
218
219 /**
220 * Memory the Repo DO lends to concurrent push ingests, in MiB — one
221 * permit per MiB (DESIGN.md §2.1).
222 *
223 * The semaphore exists for MEMORY, not correctness: a DO is
224 * single-threaded, but its input gate is only closed across *storage*
225 * awaits, and receive-pack awaits the network and blob storage — so two
226 * pushes genuinely interleave. Correctness across that interleave is
227 * already handled by data scoping (`staged_push = pushId`, and every
228 * read filtering `staged_push IS NULL`) plus a synchronous
229 * `transactionSync` finalize. What is left is that two concurrent
230 * 50 MiB buffers would exhaust the 128 MB isolate.
231 *
232 * Bounding the resource rather than the count is what lets pushes to
233 * different branches proceed together: a normal push is kilobytes and
234 * takes one permit, so it no longer queues behind a large one.
235 */
236 export const PUSH_MEMORY_BUDGET_MB = 64;
237
238 let sharedPushGate: Semaphore.Semaphore | undefined;
239 /**
240 * The push-admission semaphore, ONE per isolate (DESIGN §21.2, §22.4). DO
241 * instances of a class share an isolate, so a per-instance gate would let
242 * N repos each admit a full budget's worth of concurrent pushes.
243 */
244 export const isolatePushGate: Effect.Effect<Semaphore.Semaphore> = Effect.suspend(() =>
245 sharedPushGate === undefined
246 ? Semaphore.make(PUSH_MEMORY_BUDGET_MB).pipe(
247 Effect.tap((gate) =>
248 Effect.sync(() => {
249 sharedPushGate = gate;
250 }),
251 ),
252 )
253 : Effect.succeed(sharedPushGate),
254 );
255
256 /**
257 * Permits a push must reserve: its buffered ceiling in MiB. A body
258 * larger than {@link MAX_PACK_BYTES} spills to blob storage instead of
259 * growing in memory, so that is the cap; an absent `content-length`
260 * (chunked upload) is charged the same worst case.
261 */
262 export const pushPermitsFor = (contentLength: number | undefined): number => {
263 const mib = (bytes: number) => Math.ceil(bytes / (1024 * 1024));
264 // What a push can hold at once, in MiB — never its body. An unknown
265 // length is treated as spilled. Memory safety beats concurrency: on a
266 // 64 MiB budget that admits one large push per isolate alongside small ones.
267 // Streaming (DESIGN §22.6–7): the feeder retains RETAIN_BYTES behind the
268 // pump and buffers BACKPRESSURE_BYTES ahead of it, plus one staging batch.
269 const spilled = mib(RETAIN_BYTES + BACKPRESSURE_BYTES + STAGE_BATCH_BYTES);
270 // In memory the caches cannot outgrow what the (small) body can inflate
271 // to, so charge the body twice (compressed + inflated) plus one for the
272 // batch, rather than the caches' ceilings.
273 const permits =
274 contentLength === undefined || !Number.isFinite(contentLength) || contentLength > MAX_PACK_BYTES
275 ? spilled
276 : mib(Math.max(contentLength, 0)) * 2 + 1;
277 return Math.max(1, Math.min(PUSH_MEMORY_BUDGET_MB, permits));
278 };
279
280 /** How long crashed push staging survives before the GC alarm reaps it. */
281 export const STAGING_TTL_MS = 24 * 60 * 60 * 1000;
282
283 /** The realm sent on wire 401s. */
284 export const WWW_AUTHENTICATE = 'Basic realm="git-service"' as const;
285
286 /**
287 * Worker↔DO headers for the clone-bundle splice (DESIGN.md §11): the DO
288 * answers an eligible clone with these *instead of* the pack, and the
289 * Worker streams the named R2 object to the client. Only
290 * {@link BUNDLE_HASH_HEADER} is kept on the client-visible response, as
291 * observable proof the fast path served the clone.
292 */
293 export const BUNDLE_KEY_HEADER = "x-git-bundle-key" as const;
294 export const BUNDLE_HASH_HEADER = "x-git-bundle" as const;
295 export const BUNDLE_COUNT_HEADER = "x-git-bundle-count" as const;
296 export const BUNDLE_SIDEBAND_HEADER = "x-git-bundle-sideband" as const;
297
298 // ─────────────────────────────────────────────────────────────────────────────
299 // Errors
300 // ─────────────────────────────────────────────────────────────────────────────
301
302 /**
303 * A push pack failed to ingest (malformed, oversize, checksum mismatch,
304 * missing thin base, delta error). Reported in-band as `unpack <reason>` +
305 * per-ref `ng` — never as an HTTP error.
306 */
307 export class PackIngestError extends Schema.TaggedError<PackIngestError>()("PackIngestError", {
308 reason: Schema.String,
309 }) {}
310
311 /**
312 * A syntactically valid request that the v0 grammar does not allow
313 * (missing wants, malformed command lines, unsupported deepen flavors).
314 * Answered with an `ERR` pkt.
315 */
316 export class WireProtocolError extends Schema.TaggedError<WireProtocolError>()(
317 "WireProtocolError",
318 { reason: Schema.String },
319 ) {}
320
321 // ─────────────────────────────────────────────────────────────────────────────
322 // RPC data shapes (plain serializable data — no Schema classes over RPC)
323 // ─────────────────────────────────────────────────────────────────────────────
324
325 /** Repo metadata as stored in the `config` table. */
326 export interface RepoMetaData {
327 readonly repoId: string;
328 readonly owner: string;
329 readonly name: string;
330 readonly defaultBranch: string;
331 readonly description: string | null;
332 readonly readOnly: boolean;
333 /** Anyone can read/clone without a token (GitHub public-repo model). */
334 readonly public: boolean;
335 readonly forkOf: string | null;
336 readonly status: RepoStatus;
337 readonly createdAt: number;
338 /** Object storage breakdown (DESIGN.md §12.1). */
339 readonly objects: ObjectStatsData;
340 /** Server-side timing of the last push (DESIGN.md §19 phase 0). */
341 readonly lastPush: PushStatsData | null;
342 }
343
344 /** `beginPush` input: the parsed ref commands (DESIGN §22.10). */
345 export interface BeginPushInput {
346 readonly commands: ReadonlyArray<RefCommand>;
347 }
348 /** `beginPush` outcome. `denied` is reported per ref (`ng <ref> <reason>`). */
349 export type BeginPushResult =
350 | { readonly _tag: "ok"; readonly pushId: string }
351 | { readonly _tag: "denied"; readonly reason: string };
352 /** One ref's outcome in a push report. */
353 export interface PushRefResult {
354 readonly ref: string;
355 readonly ok: boolean;
356 readonly reason?: string | undefined;
357 }
358 /** `commitPush` input: what the Worker-side ingest learned (DESIGN §22.10). */
359 export interface CommitPushInput {
360 readonly pushId: string;
361 readonly commands: ReadonlyArray<RefCommand>;
362 readonly atomic: boolean;
363 readonly commits: ReadonlyArray<StagedCommitInfo>;
364 readonly referenced: ReadonlyArray<Oid>;
365 readonly referencedParents: ReadonlyArray<Oid>;
366 /** Rows staged as references into the wire pack (it must then outlive the push). */
367 readonly promoted: number;
368 readonly stats: {
369 readonly objects: number;
370 readonly bytes: number;
371 readonly ingestMs: number;
372 readonly stageMs: number;
373 readonly phases: Record<string, number> | undefined;
374 };
375 }
376 /** `commitPush` outcome: the `unpack` line and per-ref results. */
377 export interface CommitPushResult {
378 readonly unpack: string;
379 readonly results: ReadonlyArray<PushRefResult>;
380 }
381
382 /** Server-side push timing — see the REST `PushStats` schema. */
383 export interface PushStatsData {
384 readonly objects: number;
385 readonly bytes: number;
386 readonly ingestMs: number;
387 readonly stageMs: number;
388 readonly connectivityMs: number;
389 readonly finalizeMs: number;
390 readonly totalMs: number;
391 /** Ingest split by phase (ms) — see the REST `PushStats.phases` caveat. */
392 readonly phases?: Record<string, number> | undefined;
393 }
394
395 /** Where a repo's objects live — see the REST `ObjectStats` schema. */
396 export interface ObjectStatsData {
397 /** Blobs still in SQLite rows, awaiting compaction into an R2 pack. */
398 readonly loose: number;
399 /** Commits, trees and tags: always SQLite rows (never packed). */
400 readonly resident: number;
401 /** Blobs in R2 packs. */
402 readonly packed: number;
403 readonly r2: number;
404 readonly bytes: number;
405 }
406
407 /** Patch accepted by {@link GitRepoShape.updateRepoMeta}. */
408 export interface RepoMetaPatch {
409 readonly description?: string | null | undefined;
410 /** Must resolve to an existing branch. */
411 readonly defaultBranch?: string | undefined;
412 readonly readOnly?: boolean | undefined;
413 /** Anyone can read/clone without a token when `true`. */
414 readonly public?: boolean | undefined;
415 }
416
417 /** One ref (peeled target present for annotated tags). */
418 export interface RefData {
419 readonly name: string;
420 readonly oid: string;
421 readonly peeled?: string | undefined;
422 }
423
424 /** Result of {@link GitRepoShape.listRefs}. */
425 export interface RefsPage {
426 /** The default branch's full ref name, `null` on an unborn repo. */
427 readonly head: string | null;
428 readonly refs: ReadonlyArray<RefData>;
429 }
430
431 /** Input of {@link GitRepoShape.initRepo} (also fork/import bootstrap). */
432 export interface InitRepoInput {
433 readonly repoId: string;
434 readonly owner: string;
435 readonly name: string;
436 readonly defaultBranch: string;
437 readonly description: string | null;
438 readonly readOnly: boolean;
439 /** Anyone can read/clone without a token when `true`. */
440 readonly public: boolean;
441 readonly forkOf: string | null;
442 }
443
444 /** Result of init/fork/import bootstrap: the meta + a bootstrap token. */
445 export interface InitRepoResult {
446 readonly meta: RepoMetaData;
447 }
448
449 /** Input of {@link GitRepoShape.updateRef}. */
450 export interface UpdateRefInput {
451 readonly name: string;
452 readonly newOid: string;
453 /**
454 * CAS token: `string` = must currently equal; `null` = must not exist;
455 * absent = unconditional.
456 */
457 readonly expectedOid?: string | null | undefined;
458 }
459
460 /** Input of {@link GitRepoShape.removeRef}. */
461 export interface RemoveRefInput {
462 readonly name: string;
463 readonly expectedOid?: string | undefined;
464 }
465
466 /** Input of {@link GitRepoShape.readObject}. */
467 export interface ReadObjectInput {
468 readonly oid: string;
469 /** When set, a different actual type fails with `WrongObjectType`. */
470 readonly expect?: ObjectTypeName | undefined;
471 }
472
473 /** A fully-read object (content inflated, ≤ 64 MiB by the ingest cap). */
474 export interface ObjectData {
475 readonly oid: string;
476 readonly type: ObjectTypeName;
477 readonly size: number;
478 readonly content: Uint8Array;
479 }
480
481 /** One commit for the REST read surface. */
482 export interface CommitData {
483 readonly oid: string;
484 readonly tree: string;
485 readonly parents: ReadonlyArray<string>;
486 readonly author: SignatureData;
487 readonly committer: SignatureData;
488 readonly message: string;
489 }
490
491 /** An author/committer signature. */
492 export interface SignatureData {
493 readonly name: string;
494 readonly email: string;
495 readonly date: number;
496 readonly tz: string;
497 }
498
499 /** Input of {@link GitRepoShape.readCommitLog}. */
500 export interface CommitLogInput {
501 /** Refname or 40-hex oid; default = HEAD (the default branch). */
502 readonly ref?: string | undefined;
503 readonly cursor?: string | undefined;
504 readonly limit?: number | undefined;
505 }
506
507 /** One page of {@link GitRepoShape.readCommitLog}. */
508 export interface CommitLogPage {
509 readonly items: ReadonlyArray<CommitData>;
510 readonly nextCursor: string | null;
511 readonly hasMore: boolean;
512 }
513
514 // `DiffEntryData` (one changed file) is defined next to the pure walk in
515 // `Protocol/TreeDiff.ts`; re-exported here with the other RPC data shapes.
516 export type { DiffEntryData } from "./Protocol/TreeDiff.ts";
517
518 /** Result of {@link GitRepoShape.readCommitDiff}. */
519 export interface CommitDiffData {
520 readonly oid: string;
521 readonly parent: string | null;
522 readonly files: ReadonlyArray<DiffEntryData>;
523 readonly truncated: boolean;
524 }
525
526 /** Input of {@link GitRepoShape.compareCommits}. */
527 export interface CompareInput {
528 /** Refname or 40-hex oid. */
529 readonly base: string;
530 readonly head: string;
531 }
532
533 /** Result of {@link GitRepoShape.compareCommits}. */
534 export interface CompareData {
535 readonly base: string;
536 readonly head: string;
537 readonly mergeBase: string;
538 readonly aheadBy: number;
539 readonly behindBy: number;
540 readonly commits: ReadonlyArray<CommitData>;
541 readonly commitsTruncated: boolean;
542 readonly files: ReadonlyArray<DiffEntryData>;
543 readonly filesTruncated: boolean;
544 }
545
546 /** Input of {@link GitRepoShape.readFileAtPath}. */
547 export interface ReadFileInput {
548 /** Refname or 40-hex oid; default = HEAD. */
549 readonly ref?: string | undefined;
550 /** Slash-separated path inside the tree. */
551 readonly path: string;
552 }
553
554 /** A file read via tree walk. */
555 export interface FileData {
556 readonly oid: string;
557 /** Octal mode string, e.g. `100644`. */
558 readonly mode: string;
559 readonly size: number;
560 readonly content: Uint8Array;
561 }
562
563 /** One pull-request row (see the REST `Pull` schema). */
564 export interface PullData {
565 readonly number: number;
566 readonly title: string;
567 readonly body: string | null;
568 /** Full base ref name, e.g. `refs/heads/main`. */
569 readonly baseRef: string;
570 /** Full head ref name, e.g. `refs/heads/feature`. */
571 readonly headRef: string;
572 readonly state: "open" | "closed" | "merged";
573 readonly createdAt: number;
574 readonly updatedAt: number;
575 readonly mergedAt: number | null;
576 /** FF: the head tip; the merge commit otherwise. Set iff state=merged. */
577 readonly mergeCommit: string | null;
578 }
579
580 /** Detail = row + live compare fields (`null` = uncomputable, see REST docs). */
581 export interface PullDetailData extends PullData {
582 readonly baseOid: string | null;
583 readonly headOid: string | null;
584 readonly mergeBase: string | null;
585 readonly aheadBy: number | null;
586 readonly behindBy: number | null;
587 readonly mergeable: boolean | null;
588 readonly mergeableReason: "ff" | "merge-commit" | "conflict" | "up-to-date" | "unknown" | null;
589 }
590
591 /** Input of {@link GitRepoShape.createPull}. */
592 export interface CreatePullInput {
593 readonly title: string;
594 readonly body?: string | undefined;
595 /** Short or full branch name; normalized to `refs/heads/…` here. */
596 readonly base: string;
597 readonly head: string;
598 }
599
600 /** Input of {@link GitRepoShape.listPulls}. */
601 export interface ListPullsInput {
602 readonly state?: "open" | "closed" | "merged" | "all" | undefined;
603 /** Last-seen PR number as a string (keyset pagination, newest first). */
604 readonly cursor?: string | undefined;
605 /** Default 20, max 100. */
606 readonly limit?: number | undefined;
607 }
608
609 /** One page of {@link GitRepoShape.listPulls}. */
610 export interface PullsPage {
611 readonly items: ReadonlyArray<PullData>;
612 readonly nextCursor: string | null;
613 readonly hasMore: boolean;
614 }
615
616 /** Input of {@link GitRepoShape.updatePull}. */
617 export interface UpdatePullInput {
618 readonly number: number;
619 readonly title?: string | undefined;
620 /** `null` clears the body. */
621 readonly body?: string | null | undefined;
622 readonly state?: "open" | "closed" | undefined;
623 }
624
625 /** Input of {@link GitRepoShape.mergePull}. */
626 export interface MergePullInput {
627 readonly number: number;
628 /** Merge-commit message; ignored on fast-forward. */
629 readonly message?: string | undefined;
630 /** Race guard: fail RefConflict when the head tip moved since inspection. */
631 readonly expectedHeadOid?: string | undefined;
632 /** Race guard for application inspection of the base branch. */
633 readonly expectedBaseOid?: string | undefined;
634 }
635
636 /** Result of {@link GitRepoShape.mergePull}. */
637 export interface MergePullResult {
638 readonly method: "ff" | "merge-commit";
639 /** The oid the base ref now points at. */
640 readonly oid: string;
641 readonly pull: PullData;
642 }
643
644 /** Input of {@link GitRepoShape.startImport}. */
645 export interface StartImportInput extends InitRepoInput {
646 readonly source: ImportSource;
647 }
648
649 /** Input of {@link GitRepoShape.startFork}. */
650 export interface StartForkInput extends InitRepoInput {
651 readonly parentRepoId: string;
652 }
653
654 /** The common auth+repo error union of most RPC methods. */
655 export type RepoAuthError = RepoNotFound | StoreError;
656
657 /**
658 * The Repo DO's typed RPC surface. All methods are `RuntimeContext`-colored
659 * (callable only through a stub / inside the deployed Worker). Note: no
660 * method may be named `delete` (Cloudflare stub proxy reserves it).
661 */
662 export interface GitRepoShape {
663 /** Seeds config (repo create). */
664 readonly initRepo: (
665 input: InitRepoInput,
666 ) => Effect.Effect<InitRepoResult, StoreError, RuntimeContext>;
667 /** Seeds config with `status: importing`, arms the import alarm job. */
668 readonly startImport: (
669 input: StartImportInput,
670 ) => Effect.Effect<InitRepoResult, StoreError, RuntimeContext>;
671 /** Seeds config with `status: forking`, arms the fork alarm job. */
672 readonly startFork: (
673 input: StartForkInput,
674 ) => Effect.Effect<InitRepoResult, StoreError, RuntimeContext>;
675 /** Flips `status: deleting` and arms the purge alarm job. */
676 /**
677 * Arms a compaction run now (admin): loose object bytes are moved into an
678 * immutable R2 pack by the alarm (DESIGN.md §12.1). Normally armed
679 * automatically by the post-push size check.
680 */
681 readonly startCompact: () => Effect.Effect<void, RepoAuthError, RuntimeContext>;
682 readonly startPurge: () => Effect.Effect<void, RepoAuthError, RuntimeContext>;
683 readonly getRepoMeta: () => Effect.Effect<RepoMetaData, RepoAuthError, RuntimeContext>;
684 /** Repo metadata for the Worker's own bookkeeping. */
685 readonly readMeta: () => Effect.Effect<RepoMetaData, RepoNotFound | StoreError, RuntimeContext>;
686 readonly updateRepoMeta: (
687 patch: RepoMetaPatch,
688 ) => Effect.Effect<RepoMetaData, RepoAuthError | RefNotFound, RuntimeContext>;
689 readonly listRefs: (prefix?: string) => Effect.Effect<RefsPage, RepoAuthError, RuntimeContext>;
690 readonly getRef: (
691 name: string,
692 ) => Effect.Effect<RefData, RepoAuthError | RefNotFound, RuntimeContext>;
693 readonly updateRef: (
694 input: UpdateRefInput,
695 ) => Effect.Effect<
696 RefData,
697 RepoAuthError | RefConflict | ObjectNotFound | ReadOnlyRepo,
698 RuntimeContext
699 >;
700 readonly removeRef: (
701 input: RemoveRefInput,
702 ) => Effect.Effect<
703 void,
704 RepoAuthError | RefNotFound | RefConflict | ReadOnlyRepo,
705 RuntimeContext
706 >;
707 readonly readObject: (
708 input: ReadObjectInput,
709 ) => Effect.Effect<ObjectData, RepoAuthError | ObjectNotFound | WrongObjectType, RuntimeContext>;
710 readonly readCommitLog: (
711 input: CommitLogInput,
712 ) => Effect.Effect<CommitLogPage, RepoAuthError | RefNotFound, RuntimeContext>;
713 /**
714 * The changed-file list of a commit vs its FIRST parent (empty tree for
715 * a root commit) — see the REST `diff` endpoint.
716 */
717 readonly readCommitDiff: (input: {
718 readonly oid: string;
719 }) => Effect.Effect<
720 CommitDiffData,
721 RepoAuthError | ObjectNotFound | WrongObjectType,
722 RuntimeContext
723 >;
724 /**
725 * Three-dot comparison of two revisions: merge base, ahead/behind
726 * counts, head-side commits, and the mergeBase..head file diff — see
727 * the REST `compare` endpoint.
728 */
729 readonly compareCommits: (
730 input: CompareInput,
731 ) => Effect.Effect<
732 CompareData,
733 RepoAuthError | RefNotFound | ObjectNotFound | WrongObjectType | NoMergeBase,
734 RuntimeContext
735 >;
736 readonly readFileAtPath: (
737 input: ReadFileInput,
738 ) => Effect.Effect<FileData, RepoAuthError | RefNotFound | ObjectNotFound, RuntimeContext>;
739 /** Opens a pull request (same-repo branches, `write` scope). */
740 readonly createPull: (
741 input: CreatePullInput,
742 ) => Effect.Effect<
743 PullData,
744 RepoAuthError | BranchMissing | PullExists | ValidationError,
745 RuntimeContext
746 >;
747 /** Lists PRs newest-first with keyset pagination (`read` scope). */
748 readonly listPulls: (
749 input: ListPullsInput,
750 ) => Effect.Effect<PullsPage, RepoAuthError, RuntimeContext>;
751 /** Reads one PR with live compare fields recomputed from current tips. */
752 readonly getPull: (
753 number: number,
754 ) => Effect.Effect<PullDetailData, RepoAuthError | PullNotFound, RuntimeContext>;
755 /** Patches title/body, or closes/reopens via `state` (`write` scope). */
756 readonly updatePull: (
757 input: UpdatePullInput,
758 ) => Effect.Effect<PullData, RepoAuthError | PullNotFound | PullStateConflict, RuntimeContext>;
759 /**
760 * Merges an open PR: fast-forward when possible, else a two-parent merge
761 * commit iff the three-way tree merge is trivial (no path changed on both
762 * sides vs the merge base). Conflicts are a typed 409 — the server never
763 * writes conflict markers.
764 */
765 readonly mergePull: (
766 input: MergePullInput,
767 ) => Effect.Effect<
768 MergePullResult,
769 | RepoAuthError
770 | PullNotFound
771 | PullStateConflict
772 | BranchMissing
773 | NothingToMerge
774 | MergeConflict
775 | RefConflict
776 | ReadOnlyRepo,
777 RuntimeContext
778 >;
779 /**
780 * Streams a row snapshot for a fork (DESIGN.md §2.3 Fork). Internal —
781 * called only by a sibling Repo DO's fork alarm job; never routed from
782 * the Worker.
783 */
784 readonly snapshotRows: () => Stream.Stream<SnapshotChunk, StoreError>;
785 /**
786 * Push, phase 1 (DESIGN §22.10): authorize the parsed commands and open
787 * a staging push. The Worker then streams the pack through the hasher
788 * fan-out and the spill, staging rows with `stagePush`.
789 */
790 readonly beginPush: (
791 input: BeginPushInput,
792 ) => Effect.Effect<BeginPushResult, RepoNotFound | StoreError, RuntimeContext>;
793 /** Push, phase 2: stage one batch of rows (see `PushWire.ts`). */
794 readonly stagePush: (
795 pushId: string,
796 batch: Uint8Array,
797 ) => Effect.Effect<void, RepoNotFound | StoreError, RuntimeContext>;
798 /** Abort uncommitted staging; false means committed data must be retained. */
799 readonly abortPush: (
800 pushId: string,
801 packIds?: ReadonlyArray<string>,
802 ) => Effect.Effect<boolean, RepoNotFound | StoreError, RuntimeContext>;
803 /** Validate connectivity without moving refs. Commit repeats this check. */
804 readonly validatePush: (
805 input: CommitPushInput,
806 ) => Effect.Effect<void, RepoNotFound | StoreError, RuntimeContext>;
807 /** Read from this push's staging area and the live store only. */
808 readonly readPreparedObject: (
809 pushId: string,
810 oid: Oid,
811 maxBytes: number,
812 ) => Effect.Effect<
813 { readonly type: ObjectType; readonly content: Uint8Array } | undefined,
814 RepoNotFound | StoreError,
815 RuntimeContext
816 >;
817 /** A live object for thin-delta base resolution, or `undefined`. */
818 readonly readPushBase: (
819 oid: Oid,
820 ) => Effect.Effect<
821 { readonly type: ObjectType; readonly content: Uint8Array } | undefined,
822 RepoNotFound | StoreError,
823 RuntimeContext
824 >;
825 /**
826 * Push, phase 3: connectivity over the staged rows, the commit-graph
827 * rows, the transactional ref CAS, and post-commit bookkeeping.
828 */
829 readonly commitPush: (
830 input: CommitPushInput,
831 ) => Effect.Effect<CommitPushResult, RepoNotFound | StoreError, RuntimeContext>;
832 /** The git wire protocol (info/refs, upload-pack). */
833 readonly fetch: HttpEffect<Cloudflare.DurableObjectState | RuntimeContext>;
834 /** Alarm job dispatcher (import / fork / purge / gc). */
835 readonly alarm: (
836 info?: Cloudflare.AlarmInvocationInfo,
837 ) => Effect.Effect<void, never, RuntimeContext>;
838 }
839
840 // ─────────────────────────────────────────────────────────────────────────────
841 // Protocol choreography — pure helpers (exported for tests / future split)
842 // ─────────────────────────────────────────────────────────────────────────────
843
844 /** Upload-pack capability list (DESIGN.md §4). */
845 export const uploadCapabilities = (defaultBranch: string): string =>
846 `multi_ack_detailed no-done side-band-64k shallow ofs-delta agent=${GIT_AGENT} symref=HEAD:refs/heads/${defaultBranch} object-format=sha1`;
847
848 /** Receive-pack capability list (DESIGN.md §4). */
849 export const receiveCapabilities = (): string =>
850 `report-status report-status-v2 delete-refs side-band-64k atomic ofs-delta object-format=sha1 agent=${GIT_AGENT}`;
851
852 /** The two smart-HTTP services. */
853 export type GitService = "git-upload-pack" | "git-receive-pack";
854
855 /**
856 * Builds the v0 `info/refs` advertisement (with `# service=` prelude):
857 * HEAD first with the capability line, refs sorted, annotated tags peeled
858 * (`^{}` lines), zero-id `capabilities^{}` on an empty repo.
859 */
860 export const buildAdvertisement = (options: {
861 readonly service: GitService;
862 readonly defaultBranch: string;
863 /** Sorted by name; peeled targets included for annotated tags. */
864 readonly refs: ReadonlyArray<RefData>;
865 }): Uint8Array => {
866 const caps =
867 options.service === "git-upload-pack"
868 ? uploadCapabilities(options.defaultBranch)
869 : receiveCapabilities();
870 const parts: Array<Uint8Array> = [pktText(`# service=${options.service}`), flushPkt];
871 const lines: Array<string> = [];
872 const headRef = options.refs.find((ref) => ref.name === `refs/heads/${options.defaultBranch}`);
873 if (options.service === "git-upload-pack" && headRef !== undefined) {
874 lines.push(`${headRef.oid} HEAD`);
875 }
876 for (const ref of options.refs) {
877 lines.push(`${ref.oid} ${ref.name}`);
878 if (ref.peeled !== undefined) {
879 lines.push(`${ref.peeled} ${ref.name}^{}`);
880 }
881 }
882 if (lines.length === 0) {
883 parts.push(pktText(`${ZERO_OID} capabilities^{}\0${caps}`));
884 } else {
885 lines.forEach((line, index) => {
886 parts.push(index === 0 ? pktText(`${line}\0${caps}`) : pktText(line));
887 });
888 }
889 parts.push(flushPkt);
890 return concatBytes(parts);
891 };
892
893 /** A parsed v0 upload-pack request. */
894 export interface UploadPackRequest {
895 readonly wants: ReadonlyArray<Oid>;
896 readonly haves: ReadonlyArray<Oid>;
897 readonly done: boolean;
898 readonly depth: number | undefined;
899 readonly clientShallow: ReadonlyArray<Oid>;
900 readonly capabilities: ReadonlySet<string>;
901 }
902
903 /**
904 * Parses a v0 upload-pack POST body (already gunzipped) into wants /
905 * shallow / deepen / haves / done. Rejects `deepen-since` / `deepen-not` /
906 * `filter` (not advertised) with {@link WireProtocolError}.
907 */
908 export const parseUploadPackRequest = (
909 pkts: ReadonlyArray<PktLine>,
910 ): Effect.Effect<UploadPackRequest, WireProtocolError> =>
911 Effect.suspend(() => {
912 const wants: Array<Oid> = [];
913 const haves: Array<Oid> = [];
914 const clientShallow: Array<Oid> = [];
915 let done = false;
916 let depth: number | undefined;
917 let capabilities: ReadonlySet<string> = new Set();
918
919 for (const pkt of pkts) {
920 if (pkt._tag !== "data") continue;
921 const text = pktPayloadText(pkt.payload);
922 if (text === "done") {
923 done = true;
924 continue;
925 }
926 if (text.startsWith("want ")) {
927 let rest = text.slice(5);
928 if (wants.length === 0) {
929 const space = rest.indexOf(" ");
930 if (space !== -1) {
931 capabilities = new Set(
932 rest
933 .slice(space + 1)
934 .split(" ")
935 .filter(Boolean),
936 );
937 rest = rest.slice(0, space);
938 }
939 }
940 if (!isOid(rest)) {
941 return Effect.fail(WireProtocolError.make({ reason: `malformed want line: ${text}` }));
942 }
943 wants.push(rest);
944 continue;
945 }
946 if (text.startsWith("have ")) {
947 const oid = text.slice(5, 45);
948 if (isOid(oid)) haves.push(oid);
949 continue;
950 }
951 if (text.startsWith("shallow ")) {
952 const oid = text.slice(8);
953 if (!isOid(oid)) {
954 return Effect.fail(
955 WireProtocolError.make({
956 reason: `malformed shallow line: ${text}`,
957 }),
958 );
959 }
960 clientShallow.push(oid);
961 continue;
962 }
963 if (text.startsWith("deepen ")) {
964 const n = Number.parseInt(text.slice(7), 10);
965 if (!Number.isInteger(n) || n <= 0) {
966 return Effect.fail(WireProtocolError.make({ reason: `malformed deepen line: ${text}` }));
967 }
968 depth = n;
969 continue;
970 }
971 if (text.startsWith("deepen-since ") || text.startsWith("deepen-not ")) {
972 return Effect.fail(
973 WireProtocolError.make({
974 reason: `${text.split(" ")[0]} is not supported`,
975 }),
976 );
977 }
978 if (text.startsWith("filter ")) {
979 return Effect.fail(WireProtocolError.make({ reason: "filter is not supported" }));
980 }
981 // ignore unknown lines defensively
982 }
983 if (wants.length === 0) {
984 return Effect.fail(WireProtocolError.make({ reason: "upload-pack request has no wants" }));
985 }
986 return Effect.succeed({
987 wants,
988 haves,
989 done,
990 depth,
991 clientShallow,
992 capabilities,
993 });
994 });
995
996 /** One receive-pack command (`<old> <new> <refname>`). */
997 export interface RefCommand {
998 readonly oldOid: Oid;
999 readonly newOid: Oid;
1000 readonly ref: string;
1001 }
1002
1003 /** A parsed v0 receive-pack request head. */
1004 export interface ReceivePackRequest {
1005 readonly commands: ReadonlyArray<RefCommand>;
1006 readonly capabilities: ReadonlySet<string>;
1007 /** Byte offset of the pack within the body (== length when no pack). */
1008 readonly packStart: number;
1009 /** True for git's bare-flush `http.postBuffer` probe. */
1010 readonly probe: boolean;
1011 }
1012
1013 const REF_NAME_REGEX = /^refs\/[^\s~^:?*[\\]+$/;
1014
1015 /**
1016 * Parses a v0 receive-pack POST body (already gunzipped): command
1017 * pkt-lines (first carries `\0caps`) up to the flush, leaving the raw pack
1018 * bytes at `packStart`. Detects the empty-flush probe (DESIGN.md §2.3
1019 * Push step 2).
1020 */
1021 export const parseReceivePackRequest = (
1022 body: Uint8Array,
1023 ): Effect.Effect<ReceivePackRequest, WireProtocolError> =>
1024 Effect.suspend(() => {
1025 const commands: Array<RefCommand> = [];
1026 let capabilities: ReadonlySet<string> = new Set();
1027 let pos = 0;
1028 let first = true;
1029
1030 for (;;) {
1031 if (pos >= body.length) {
1032 // Body ended without a flush: only legal as the bare probe of an
1033 // entirely empty body.
1034 if (commands.length === 0 && pos === 0) {
1035 return Effect.succeed({
1036 commands: [],
1037 capabilities,
1038 packStart: body.length,
1039 probe: true,
1040 });
1041 }
1042 return Effect.fail(
1043 WireProtocolError.make({
1044 reason: "receive-pack request ended before flush",
1045 }),
1046 );
1047 }
1048 const r = readPktLineAt(body, pos);
1049 if (r._tag === "incomplete" || r._tag === "invalid") {
1050 return Effect.fail(
1051 WireProtocolError.make({
1052 reason: r._tag === "invalid" ? r.reason : "truncated receive-pack request",
1053 }),
1054 );
1055 }
1056 pos = r.next;
1057 if (r.pkt._tag === "flush") {
1058 return Effect.succeed({
1059 commands,
1060 capabilities,
1061 packStart: pos,
1062 probe: commands.length === 0 && pos >= body.length,
1063 });
1064 }
1065 if (r.pkt._tag !== "data") continue;
1066 let text = pktPayloadText(r.pkt.payload);
1067 if (first) {
1068 const nul = text.indexOf("\0");
1069 if (nul !== -1) {
1070 capabilities = new Set(
1071 text
1072 .slice(nul + 1)
1073 .split(" ")
1074 .filter(Boolean),
1075 );
1076 text = text.slice(0, nul);
1077 }
1078 first = false;
1079 }
1080 const oldOid = text.slice(0, 40);
1081 const newOid = text.slice(41, 81);
1082 const ref = text.slice(82);
1083 if (
1084 !isOid(oldOid) ||
1085 !isOid(newOid) ||
1086 text[40] !== " " ||
1087 text[81] !== " " ||
1088 !REF_NAME_REGEX.test(ref)
1089 ) {
1090 return Effect.fail(WireProtocolError.make({ reason: `malformed command line: ${text}` }));
1091 }
1092 commands.push({ oldOid, newOid, ref });
1093 }
1094 });
1095
1096 /** The 12-byte pack header: `PACK`, version 2, entry count. */
1097 export const packHeader = (count: number): Uint8Array => {
1098 const out = new Uint8Array(12);
1099 out.set(utf8Encode("PACK"), 0);
1100 const view = new DataView(out.buffer);
1101 view.setUint32(4, 2);
1102 view.setUint32(8, count);
1103 return out;
1104 };
1105
1106 /**
1107 * The §3.5 no-delta pack emitter: header + per-entry
1108 * `varint(type,size) + stored zdata` (verbatim — zero compression CPU) +
1109 * incremental SHA-1 trailer. Constant memory; the manifest is already
1110 * materialized so the count is known upfront.
1111 */
1112 export const packStream = (
1113 entries: ReadonlyArray<ManifestEntry>,
1114 objects: ObjectSource,
1115 ): Stream.Stream<Uint8Array, StoreError> =>
1116 Stream.suspend(() => {
1117 const sha = makeSha1();
1118 const tap = (bytes: Uint8Array): Uint8Array => {
1119 sha.update(bytes);
1120 return bytes;
1121 };
1122 const header = Stream.sync(() => tap(packHeader(entries.length)));
1123 const body = packEntryStream(entries, objects).pipe(Stream.map(tap));
1124 const trailer = Stream.sync(() => sha.digest());
1125 return header.pipe(Stream.concat(body), Stream.concat(trailer));
1126 });
1127
1128 /**
1129 * The entry stream alone — no header, no trailer. Because entries are
1130 * stored non-delta (`varint(type,size) + zdata`), entry streams from
1131 * different sources concatenate into one valid pack under a rewritten
1132 * header and a fresh trailer. That is what lets a clone be served as
1133 * "bundle bytes + a small delta" (DESIGN.md §21.2), and it is the same
1134 * property geometric pack merging relies on.
1135 */
1136 export const packEntryStream = (
1137 entries: ReadonlyArray<ManifestEntry>,
1138 objects: ObjectSource,
1139 ): Stream.Stream<Uint8Array, StoreError> =>
1140 objects.packEntries !== undefined
1141 ? objects.packEntries(entries)
1142 : Stream.fromIterable(entries).pipe(
1143 Stream.flatMap((entry) =>
1144 Stream.sync(() => encodeTypeSize(entry.type, entry.size)).pipe(
1145 Stream.concat(objects.readZData(entry.oid)),
1146 ),
1147 ),
1148 );
1149
1150 // ─────────────────────────────────────────────────────────────────────────────
1151 // Pack ingest (DESIGN.md §3.6) — buffered v1, RandomAccess seam is the buffer
1152 // ─────────────────────────────────────────────────────────────────────────────
1153
1154 /** A commit staged by an ingest, with what the commit graph needs. */
1155 export interface StagedCommitInfo {
1156 readonly oid: Oid;
1157 readonly tree: Oid;
1158 readonly parents: ReadonlyArray<Oid>;
1159 readonly commitTime: number;
1160 }
1161
1162 /** The result of a successful pack ingest. */
1163 export interface IngestResult {
1164 /** Objects in the pack (including already-existing re-pushed ones). */
1165 readonly objectCount: number;
1166 /** SQL staging time, when measured by the streaming ingest path. */
1167 readonly stageMs?: number | undefined;
1168 /** Per-phase CPU split of ingest (ms) — see `PackParser` `phases`. */
1169 readonly phases?: Record<string, number> | undefined;
1170 /** Objects staged as references into a promoted wire pack (DESIGN §22.5). */
1171 readonly promoted?: number | undefined;
1172 /** The completed spilled object's key, when the body spilled (DESIGN §22.10). */
1173 readonly parkedKey?: string | undefined;
1174 /** The resolved pack's key — delta-resolved blobs, promoted (DESIGN §22.13). */
1175 readonly resolvedKey?: string | undefined;
1176 /** The pack's trailer checksum (hex). */
1177 readonly packSha: string;
1178 /** Staged commits (commit-graph rows are inserted at finalize). */
1179 readonly commits: ReadonlyArray<StagedCommitInfo>;
1180 /** Oids referenced by staged trees/tags + commit trees (hard edges). */
1181 readonly referenced: ReadonlyArray<Oid>;
1182 /** Oids referenced as commit parents (soft under shallow import). */
1183 readonly referencedParents: ReadonlyArray<Oid>;
1184 }
1185
1186 /** Bounded LRU of inflated contents shared across delta resolution. */
1187 const makeContentCache = (budget: number) => {
1188 const map = new Map<string, Uint8Array>();
1189 let total = 0;
1190 return {
1191 get: (key: string): Uint8Array | undefined => {
1192 const value = map.get(key);
1193 if (value !== undefined) {
1194 map.delete(key);
1195 map.set(key, value);
1196 }
1197 return value;
1198 },
1199 set: (key: string, value: Uint8Array): void => {
1200 if (value.byteLength > budget || map.has(key)) return;
1201 map.set(key, value);
1202 total += value.byteLength;
1203 for (const [k, v] of map) {
1204 if (total <= budget) break;
1205 map.delete(k);
1206 total -= v.byteLength;
1207 }
1208 },
1209 };
1210 };
1211
1212 /** Delta-resolution content cache budget (DESIGN.md §3.6): 20 MiB. */
1213 const CACHE_BUDGET = 20 * 1024 * 1024;
1214 /** Maximum delta-chain depth tolerated (git's own default is 50). */
1215 const MAX_DELTA_DEPTH = 64;
1216
1217 interface RawEntry {
1218 readonly offset: number;
1219 readonly kind: "object" | "ofs" | "ref";
1220 /** Real object type for `kind: "object"`. */
1221 readonly type: ObjectType | undefined;
1222 /** Header size (object size, or delta payload size for deltas). */
1223 readonly declaredSize: number;
1224 /** Where this entry's zlib stream starts. */
1225 readonly zstart: number;
1226 /** Exact compressed span length. */
1227 readonly zlen: number;
1228 readonly baseOffset: number | undefined;
1229 readonly baseOid: Oid | undefined;
1230 /** Filled once resolved. */
1231 oid: Oid | undefined;
1232 resolvedType: ObjectType | undefined;
1233 }
1234
1235 /**
1236 * The store surface ingest needs (DESIGN §22.10): staging rows and thin
1237 * delta bases. The Repo DO satisfies it directly; the Worker satisfies it
1238 * over RPC so the pack never enters the Durable Object.
1239 */
1240 export interface IngestStore {
1241 readonly insertStagedBatch: ObjectStore["insertStagedBatch"];
1242 /** Awaits staging that `insertStagedBatch` may have left in flight. */
1243 readonly settle?: Effect.Effect<void, StoreError> | undefined;
1244 /** A live base object for a thin delta, or `undefined` when absent. */
1245 readonly readBase: (
1246 oid: Oid,
1247 ) => Effect.Effect<
1248 { readonly type: ObjectType; readonly content: Uint8Array } | undefined,
1249 StoreError
1250 >;
1251 }
1252
1253 /** An {@link IngestStore} over a full {@link ObjectStore}. */
1254 export const ingestStoreOf = (store: ObjectStore): IngestStore => ({
1255 insertStagedBatch: store.insertStagedBatch,
1256 readBase: (oid) =>
1257 store
1258 .getMeta(oid)
1259 .pipe(
1260 Effect.flatMap((meta) =>
1261 meta === undefined
1262 ? Effect.undefined
1263 : store.readContent(oid).pipe(Effect.map((content) => ({ type: meta.type, content }))),
1264 ),
1265 ),
1266 });
1267
1268 /** Options of {@link ingestPack}. */
1269 export interface IngestOptions {
1270 readonly store: IngestStore;
1271 readonly pushId: string;
1272 /**
1273 * When the wire body was spilled to blob storage, stage non-delta blobs
1274 * as references into it (DESIGN §22.5): `packId` is the wire pack's id,
1275 * `base` the pack's byte offset within the spilled object (the request's
1276 * pkt-line commands precede it).
1277 */
1278 readonly promote?:
1279 | (() => { readonly packId: string; readonly base: number } | undefined)
1280 | undefined;
1281 /**
1282 * When set, entries are inflated and hashed by the hasher service one
1283 * spilled part at a time (DESIGN §22.7) instead of by the in-process
1284 * parser; the DO only pumps bytes and stages rows.
1285 */
1286 readonly hasher?: HasherShape | undefined;
1287 /** Bytes per hasher part (tests shrink it). @default HASH_PART_BYTES */
1288 readonly partBytes?: number | undefined;
1289 /**
1290 * The streaming spill (DESIGN §22.10): the pump reads the whole request
1291 * BODY in uniform parts, each hasher writes its part to blob storage,
1292 * and the completed object is the promotion target (`packId`) and the
1293 * source's fallback reader. Bodies that end within `threshold` bytes
1294 * stay in memory and nothing is written.
1295 */
1296 readonly spill?:
1297 | {
1298 readonly body: RandomAccess;
1299 readonly feeder: StreamingFeeder;
1300 readonly packStart: number;
1301 readonly blobs: BlobStoreShape;
1302 readonly key: string;
1303 readonly packId: string;
1304 readonly repoId: string;
1305 readonly threshold: number;
1306 }
1307 | undefined;
1308 /**
1309 * Shallow-import mode: commit parents may be absent (they become walk
1310 * boundaries); trees/blobs/tags must still be fully connected.
1311 */
1312 readonly allowMissingParents?: boolean | undefined;
1313 }
1314
1315 /**
1316 * Single-pass buffered pack ingest (DESIGN.md §3.6): parse entries,
1317 * store non-delta compressed spans **verbatim**, resolve ofs/ref deltas
1318 * (thin bases from the live store), enforce the 64 MiB object cap, verify
1319 * the trailer SHA-1, and stage everything under `pushId`.
1320 *
1321 * The v1.x streaming upgrade seam: this function's only view of the pack
1322 * is `pack: Uint8Array` random access — swapping in an R2-backed
1323 * `RandomAccess` touches this implementation, not the protocol drivers.
1324 */
1325 /**
1326 * Ingests a pack from a {@link RandomAccess} source — the path that has **no
1327 * size cap** (DESIGN.md §3.6). Memory is bounded by the source's read
1328 * window and the delta LRU rather than by the pack, so the source can be an
1329 * R2 object of any size (see `Store/PackSource.ts`).
1330 *
1331 * Delegates the wire-format work to the pure `PackParser` (which the pack
1332 * fixtures in `test/pack.test.ts` cover directly) and does the repo-side
1333 * work in its sink: stage the object, and record commit-graph edges.
1334 * Blobs are staged without inflating — only commits, trees and tags need
1335 * their content parsed, and those are small.
1336 */
1337 export const ingestPackFrom = (
1338 source: RandomAccess,
1339 options: IngestOptions,
1340 ): Effect.Effect<IngestResult, PackIngestError | StoreError> =>
1341 Effect.gen(function* () {
1342 const { store, pushId } = options;
1343 const commits: Array<StagedCommitInfo> = [];
1344 const referenced = new Set<Oid>();
1345 const referencedParents = new Set<Oid>();
1346
1347 const record = Effect.fn(function* (oid: Oid, type: ObjectType, content: Uint8Array) {
1348 switch (type) {
1349 case ObjectType.commit: {
1350 const parsed = yield* parseCommit(content).pipe(
1351 Effect.mapError((error) =>
1352 PackIngestError.make({
1353 reason: `bad commit ${oid}: ${error.reason}`,
1354 }),
1355 ),
1356 );
1357 commits.push({
1358 oid,
1359 tree: parsed.tree,
1360 parents: parsed.parents,
1361 commitTime: parsed.committer.when,
1362 });
1363 referenced.add(parsed.tree);
1364 for (const parent of parsed.parents) referencedParents.add(parent);
1365 return;
1366 }
1367 case ObjectType.tree: {
1368 const parsed = yield* parseTree(content).pipe(
1369 Effect.mapError((error) =>
1370 PackIngestError.make({
1371 reason: `bad tree ${oid}: ${error.reason}`,
1372 }),
1373 ),
1374 );
1375 for (const entry of parsed) {
1376 if (treeEntryKind(entry.mode) !== "commit") {
1377 referenced.add(entry.oid);
1378 }
1379 }
1380 return;
1381 }
1382 case ObjectType.tag: {
1383 const parsed = yield* parseTag(content).pipe(
1384 Effect.mapError((error) =>
1385 PackIngestError.make({
1386 reason: `bad tag ${oid}: ${error.reason}`,
1387 }),
1388 ),
1389 );
1390 referenced.add(parsed.object);
1391 return;
1392 }
1393 case ObjectType.blob:
1394 return;
1395 }
1396 });
1397
1398 // Stage in batches: one transaction per batch instead of one (plus an
1399 // existence probe) per object. That per-object cost is what made a
1400 // 13.7k-object push take ~20 s inside a single-threaded DO — the work
1401 // is inherently serial, so the only lever is fewer operations
1402 // (DESIGN.md §16.6).
1403 // Buffered entries keep their pack coordinates so the promote/inline
1404 // decision is made at FLUSH time (DESIGN §22.6): a streaming body only
1405 // becomes a wire pack once it crosses the spill threshold, but the wire
1406 // object holds the body from byte 0, so entries buffered before that
1407 // moment can still be promoted when the flush happens after it.
1408 /** What the parser or the hasher hands over per entry. */
1409 interface Incoming {
1410 readonly oid: Oid;
1411 readonly type: ObjectType;
1412 readonly size: number;
1413 /** Absent for hasher-scanned non-delta entries: read from the source if the row stays inline. */
1414 readonly zdata?: Uint8Array | undefined;
1415 readonly span?: number | undefined;
1416 readonly dataOffset: number;
1417 readonly fromDelta: boolean;
1418 readonly content?: Uint8Array | undefined;
1419 /** Already in a pack in blob storage (the resolved pack, DESIGN §22.13). */
1420 readonly pack?: { readonly packId: string; readonly offset: number } | undefined;
1421 }
1422 let batch: Array<Incoming> = [];
1423 let batchBytes = 0;
1424 let promoted = 0;
1425 /** Time spent in SQL staging; the rest of ingest is CPU. */
1426 let stageMs = 0;
1427 const flush = Effect.fn(function* () {
1428 if (batch.length === 0) return;
1429 const pending = batch;
1430 batch = [];
1431 batchBytes = 0;
1432 const target =
1433 options.promote?.() ??
1434 (spilling && options.spill !== undefined
1435 ? { packId: options.spill.packId, base: options.spill.packStart }
1436 : undefined);
1437 const staged: Array<StagedObject> = [];
1438 for (const entry of pending) {
1439 const pack =
1440 entry.pack ??
1441 (target !== undefined &&
1442 !entry.fromDelta &&
1443 entry.type === ObjectType.blob &&
1444 entry.dataOffset >= 0
1445 ? { packId: target.packId, offset: target.base + entry.dataOffset }
1446 : undefined);
1447 if (pack !== undefined) promoted += 1;
1448 // A promoted row needs no bytes; an inline row needs its zdata —
1449 // the hasher path did not ship it, so read the span back.
1450 const zdata =
1451 entry.zdata ??
1452 (pack === undefined
1453 ? Uint8Array.from(
1454 readSpan?.(entry.dataOffset, entry.span ?? 0) ??
1455 (yield* source.read(entry.dataOffset, entry.span ?? 0)),
1456 )
1457 : new Uint8Array(0));
1458 staged.push({
1459 oid: entry.oid,
1460 type: entry.type,
1461 size: entry.size,
1462 zdata,
1463 zsize: entry.zdata?.byteLength ?? entry.span,
1464 pack,
1465 });
1466 }
1467 const at = yield* Effect.sync(() => performance.now());
1468 yield* store.insertStagedBatch(pushId, staged);
1469 stageMs += yield* Effect.sync(() => performance.now() - at);
1470 });
1471
1472 const phases: Record<string, number> = {};
1473 /** The spill's multipart upload, once the pump decided to spill. */
1474 let upload: BlobMultipart | undefined;
1475 let spilling = false;
1476 /** Set once the spilled object is complete: it is the wire pack. */
1477 let parkedKey: string | undefined;
1478 /** Set once the resolved pack(s) are written (DESIGN §22.13). */
1479 let resolvedKey: string | undefined;
1480 const resolvedKeys: Array<string> = [];
1481 /**
1482 * Delta-resolved blobs held back from staging while the body spills:
1483 * their fresh zlib goes into one resolved pack in blob storage and the
1484 * rows become coordinates, so the DO never writes those bytes.
1485 */
1486 const resolvedBlobs: Array<Incoming> = [];
1487 /** In-memory span reads over the retained parts (set by the pump). */
1488 let readSpan: ((at: number, length: number) => Uint8Array | undefined) | undefined;
1489 /**
1490 * Stages a run of resolved entries (DESIGN §22.5): the parser's
1491 * synchronous fast path hands over up to SINK_BATCH non-delta entries
1492 * per fiber hop; delta-resolved entries arrive one at a time.
1493 */
1494 /** Every oid this push stages: connectivity only asks the store about the rest. */
1495 const stagedOids = new Set<Oid>();
1496 const stage = (entries: ReadonlyArray<Incoming>) =>
1497 Effect.gen(function* () {
1498 for (const entry of entries) {
1499 stagedOids.add(entry.oid);
1500 if (
1501 spilling &&
1502 entry.fromDelta &&
1503 entry.type === ObjectType.blob &&
1504 entry.pack === undefined &&
1505 entry.zdata !== undefined
1506 ) {
1507 resolvedBlobs.push(entry);
1508 continue;
1509 }
1510 batch.push(entry);
1511 // Counted as inline until the flush decides; promoted rows carry
1512 // no BLOB, so this over-counts — safe for a memory cap.
1513 batchBytes += entry.zdata?.byteLength ?? entry.span ?? 0;
1514 if (batch.length >= STAGE_BATCH_OBJECTS || batchBytes >= STAGE_BATCH_BYTES) {
1515 yield* flush();
1516 }
1517 if (entry.type !== ObjectType.blob) {
1518 if (entry.content === undefined) {
1519 return yield* PackIngestError.make({
1520 reason: `no content for ${entry.oid} (type ${entry.type})`,
1521 });
1522 }
1523 yield* record(entry.oid, entry.type, entry.content);
1524 }
1525 }
1526 });
1527 /**
1528 * The hasher pipeline (DESIGN §22.7): pump the pack in parts through
1529 * the hasher; every part carries the previous part's incomplete tail.
1530 * The DO's own CPU here is a memcpy, the trailer SHA-1 and staging.
1531 */
1532 const pumpThroughHasher = (hasher: HasherShape) =>
1533 Effect.gen(function* () {
1534 const spill = options.spill;
1535 // Spill parts (uniform, ≥ 5 MiB for R2) and hash chunks: a hasher
1536 // may want smaller chunks than a part (a Lambda's 6 MB payload);
1537 // chunks then divide parts and the pump uploads the parts itself.
1538 const partBytes =
1539 options.partBytes ?? (spill === undefined ? HASH_PART_BYTES : SPILL_PART_BYTES);
1540 const wanted = hasher.chunkBytes ?? partBytes;
1541 const hashBytes = wanted < partBytes && partBytes % wanted === 0 ? wanted : partBytes;
1542 const asIngest = (error: { readonly _tag: string; readonly reason?: string }) =>
1543 PackIngestError.make({
1544 reason: `${error._tag}${error.reason === undefined ? "" : `: ${error.reason}`}`,
1545 });
1546 const header = yield* source.read(0, 12);
1547 if (
1548 header.length < 12 ||
1549 header[0] !== 0x50 ||
1550 header[1] !== 0x41 ||
1551 header[2] !== 0x43 ||
1552 header[3] !== 0x4b
1553 ) {
1554 return yield* PackIngestError.make({ reason: "bad pack magic" });
1555 }
1556 const count = new DataView(header.buffer, header.byteOffset).getUint32(8);
1557 // Trailer SHA-1 over [0, total − 20): hash with a 20-byte lag, so
1558 // the last 20 bytes seen are the trailer itself.
1559 const trailerSha = makeSha1();
1560 let lag = new Uint8Array(0);
1561 const feed = (bytes: Uint8Array) => {
1562 const joined = lag.length === 0 ? bytes : concatBytes([lag, bytes]);
1563 if (joined.length > 20) {
1564 trailerSha.update(joined.subarray(0, joined.length - 20));
1565 lag = Uint8Array.from(joined.subarray(joined.length - 20));
1566 } else {
1567 lag = Uint8Array.from(joined);
1568 }
1569 };
1570 interface Known {
1571 readonly oid: Oid;
1572 readonly type: ObjectType;
1573 readonly dataOffset: number;
1574 readonly span: number;
1575 readonly zdata?: Uint8Array | undefined;
1576 readonly content?: Uint8Array | undefined;
1577 }
1578 const known = new Map<number, Known>(); // by header offset
1579 const knownByOid = new Map<string, Known>();
1580 const unresolved: Array<UnresolvedDelta> = [];
1581 // Raw-chunk dispatch (DESIGN §22.9–10): NO scanning here. Every
1582 // chunk goes to a hasher as it arrives (bounded concurrency), which
1583 // resyncs to the first entry boundary inside the chunk and scans and
1584 // hashes from there — and, when the body spills, writes the chunk
1585 // as its part of the multipart upload. Results are consumed in
1586 // chunk order, concurrently with the receive; the one entry
1587 // straddling each chunk edge is stitched from the neighbouring
1588 // payloads and hashed by a small extra call. A boundary that does
1589 // not line up (a resync false positive) falls back to a sequential
1590 // rescan of that chunk from the known boundary.
1591 const gate = yield* Semaphore.make(hasher.concurrency ?? HASH_CONCURRENCY);
1592 interface Chunk {
1593 readonly index: number;
1594 /** Pack-relative offset of payload[0] (negative for a body-aligned first part). */
1595 readonly base: number;
1596 readonly payload: Uint8Array;
1597 readonly fiber: Fiber.Fiber<HashPartResult, PackIngestError>;
1598 }
1599 const chunks: Array<Chunk> = [];
1600 const bytesFrom = (from: number, to: number): Uint8Array => {
1601 const pieces: Array<Uint8Array> = [];
1602 for (const c of chunks) {
1603 const start = Math.max(from, c.base);
1604 const end = Math.min(to, c.base + c.payload.length);
1605 if (end > start) pieces.push(c.payload.subarray(start - c.base, end - c.base));
1606 }
1607 if (pieces.length === 1) return pieces[0]!;
1608 // Neighbouring chunks are views of one slab: hand out one view.
1609 const first = pieces[0]!;
1610 let contiguous = true;
1611 let end = first.byteOffset + first.byteLength;
1612 for (let i = 1; i < pieces.length; i++) {
1613 const piece = pieces[i]!;
1614 if (piece.buffer !== first.buffer || piece.byteOffset !== end) {
1615 contiguous = false;
1616 break;
1617 }
1618 end += piece.byteLength;
1619 }
1620 return contiguous
1621 ? new Uint8Array(first.buffer, first.byteOffset, end - first.byteOffset)
1622 : concatBytes(pieces);
1623 };
1624 // Retained payloads serve every span read (inline rows, delta
1625 // bases and deltas) so nothing goes back to the source or the
1626 // spilled object during ingest.
1627 readSpan = (at, length) => {
1628 const bytes = bytesFrom(at, at + length);
1629 return bytes.length === length ? bytes : undefined;
1630 };
1631 const stageResult = (payload: Uint8Array, base: number, result: ScanResult) =>
1632 Effect.gen(function* () {
1633 const incoming: Array<Incoming> = [];
1634 for (const e of result.entries) {
1635 // A hasher may omit non-blob content to bound its response
1636 // (a Lambda): inflate it here from the fresh zdata or the
1637 // retained span — trees, commits and tags are small.
1638 const content =
1639 e.content ??
1640 (e.type === ObjectType.blob
1641 ? undefined
1642 : yield* Zlib.inflate(
1643 e.zdata ??
1644 payload.subarray(e.dataOffset - base, e.dataOffset - base + e.span),
1645 ).pipe(Effect.mapError(asIngest)));
1646 const item: Known = {
1647 oid: e.oid,
1648 type: e.type,
1649 dataOffset: e.dataOffset,
1650 span: e.span,
1651 zdata: e.zdata,
1652 content,
1653 };
1654 known.set(e.offset, item);
1655 knownByOid.set(e.oid, item);
1656 incoming.push({
1657 oid: e.oid,
1658 type: e.type,
1659 size: e.size,
1660 zdata:
1661 e.zdata ?? payload.subarray(e.dataOffset - base, e.dataOffset - base + e.span),
1662 span: e.span,
1663 dataOffset: e.dataOffset,
1664 fromDelta: e.zdata !== undefined,
1665 content,
1666 });
1667 }
1668 yield* stage(incoming);
1669 // Batches flush by size (STAGE_BATCH_OBJECTS / _BYTES), not per
1670 // chunk: the retained parts hold every span until ingest ends.
1671 for (const u of result.unresolved) unresolved.push(u);
1672 return result.count;
1673 });
1674
1675 // ── phase 1 (producer): read parts as they arrive, dispatch each ──
1676 // Wall-clock phase split (Date.now advances across awaits; the
1677 // platform's cpuTime is the CPU attribution): `PushStats.phases`.
1678 const pumpStarted = Date.now();
1679 let regionCalls = 0;
1680 // Each part's identity arrives after its upload; joined before
1681 // the multipart upload is completed, never on the scan path.
1682 const parts: Array<Effect.Effect<UploadedPart, PackIngestError>> = [];
1683 let producerDone = false;
1684 const wakers: Array<() => void> = [];
1685 const notifyConsumer = () => {
1686 for (const w of wakers.splice(0)) w();
1687 };
1688 const awaitChunk = (k: number) =>
1689 Effect.suspend(() =>
1690 k < chunks.length || producerDone
1691 ? Effect.void
1692 : Effect.callback<void>((resume) => {
1693 wakers.push(() => resume(Effect.void));
1694 }),
1695 );
1696 const availableEnd = () => {
1697 const last = chunks[chunks.length - 1];
1698 return last === undefined ? 12 : last.base + last.payload.length;
1699 };
1700 const dispatch = (
1701 payload: Uint8Array,
1702 opts: {
1703 readonly base: number;
1704 readonly skip: number;
1705 readonly resync: boolean;
1706 readonly partNumber: number;
1707 },
1708 ) =>
1709 hasher
1710 .hashPart(payload, {
1711 base: opts.base,
1712 skip: opts.skip,
1713 remaining: count,
1714 maxObjectSize: MAX_OBJECT_SIZE,
1715 resync: opts.resync,
1716 spill:
1717 upload === undefined || spill === undefined || !hasher.writesSpill
1718 ? undefined
1719 : {
1720 key: spill.key,
1721 uploadId: upload.uploadId,
1722 partNumber: opts.partNumber,
1723 },
1724 })
1725 .pipe(Effect.mapError(asIngest), Semaphore.withPermits(gate, 1), Effect.forkChild);
1726 const produce = Effect.gen(function* () {
1727 if (spill === undefined) {
1728 // Pack-relative parts from the (in-memory or spilled) source.
1729 feed(header);
1730 let offset = 12;
1731 let index = 0;
1732 while (true) {
1733 const chunk = yield* source.read(offset, partBytes);
1734 if (chunk.length === 0) break;
1735 feed(chunk);
1736 const fiber = yield* dispatch(chunk, {
1737 base: offset,
1738 skip: 0,
1739 resync: index > 0,
1740 partNumber: index + 1,
1741 });
1742 chunks.push({ index, base: offset, payload: chunk, fiber });
1743 offset += chunk.length;
1744 index += 1;
1745 notifyConsumer();
1746 source.release?.(offset);
1747 if (chunk.length < partBytes) break;
1748 }
1749 return;
1750 }
1751 // Body-aligned parts (DESIGN §22.10): part k is body
1752 // [k·P, (k+1)·P); the first carries the command section and
1753 // the pack header before its first entry (`skip`). Chunks of
1754 // `hashBytes` divide the parts.
1755 const { body, packStart } = spill;
1756 const uploadGate = yield* Semaphore.make(3);
1757 let partStart = 0;
1758 /** Upload body [partStart, end) as the next part (pump-owned spill). */
1759 const uploadPart = (end: number) =>
1760 Effect.gen(function* () {
1761 if (upload === undefined || end <= partStart) return;
1762 const settled = upload;
1763 const partNumber = Math.floor(partStart / partBytes) + 1;
1764 const bytes = bytesFrom(partStart - packStart, end - packStart);
1765 partStart = end;
1766 const fiber = yield* spill.blobs
1767 .uploadPart(spill.key, settled.uploadId, partNumber, bytes)
1768 .pipe(
1769 Effect.mapError((error) =>
1770 PackIngestError.make({
1771 reason: `spill part ${partNumber}: ${error.reason}`,
1772 }),
1773 ),
1774 Effect.provide(RuntimeContext.phantom),
1775 Semaphore.withPermits(uploadGate, 1),
1776 Effect.forkDetach,
1777 );
1778 parts.push(Fiber.join(fiber));
1779 });
1780 let index = 0;
1781 let received = 0;
1782 while (true) {
1783 const at = index * hashBytes;
1784 const chunk = yield* body.read(at, hashBytes);
1785 if (chunk.length === 0) break;
1786 if (index === 0) {
1787 // Decide the spill once the first chunk is in: a body that
1788 // ended within the in-memory threshold stays in memory.
1789 const ended = spill.feeder.source.ended();
1790 if (!(ended && spill.feeder.source.size <= spill.threshold)) {
1791 upload = yield* spill.blobs.multipart(spill.key).pipe(
1792 Effect.mapError((error) =>
1793 PackIngestError.make({
1794 reason: `spill: ${error.reason}`,
1795 }),
1796 ),
1797 Effect.provide(RuntimeContext.phantom),
1798 );
1799 spill.feeder.expectFallback();
1800 spilling = true;
1801 }
1802 }
1803 const skip = index === 0 ? packStart + 12 : 0;
1804 feed(index === 0 ? chunk.subarray(packStart) : chunk);
1805 const base = at - packStart;
1806 const fiber = yield* dispatch(chunk, {
1807 base: base + skip,
1808 skip,
1809 resync: index > 0,
1810 partNumber: Math.floor(at / partBytes) + 1,
1811 });
1812 console.log(
1813 `[pump] chunk ${index + 1} read at +${Date.now() - pumpStarted}ms (${chunk.length} bytes)`,
1814 );
1815 chunks.push({ index, base, payload: chunk, fiber });
1816 index += 1;
1817 received = at + chunk.length;
1818 notifyConsumer();
1819 body.release?.(received);
1820 if (!hasher.writesSpill && received % partBytes === 0) {
1821 yield* uploadPart(received);
1822 }
1823 if (chunk.length < hashBytes) break;
1824 }
1825 if (!hasher.writesSpill) yield* uploadPart(received);
1826 }).pipe(
1827 Effect.ensuring(
1828 Effect.sync(() => {
1829 producerDone = true;
1830 notifyConsumer();
1831 }),
1832 ),
1833 );
1834
1835 // ── phase 2 (consumer): settle results in chunk order ──────────
1836 // `prevEnd` is the last settled boundary. A chunk's resync result
1837 // covers [firstOffset, …); whatever lies in [prevEnd, firstOffset)
1838 // — usually one straddling entry, but any number when chunks are
1839 // tiny — is scanned sequentially as one region from the retained
1840 // payloads. Entries the region scan reaches at or past
1841 // `firstOffset` are duplicates of the chunk's own result and are
1842 // dropped; a region that does not end exactly at `firstOffset`
1843 // means the resync was a false positive, and the chunk is rescanned
1844 // sequentially from `prevEnd` instead.
1845 const keepBelow = (result: ScanResult, limit: number): ScanResult => {
1846 const entries = result.entries.filter((e) => e.offset < limit);
1847 const unresolved = result.unresolved.filter((u) => u.offset < limit);
1848 // The settled boundary is the END of the last kept entry — which
1849 // may lie past `limit` when that entry straddles a chunk edge.
1850 let end = result.count === 0 ? result.consumedTo : -1;
1851 for (const e of entries) end = Math.max(end, e.dataOffset + e.span);
1852 for (const u of unresolved) end = Math.max(end, u.dataOffset + u.span);
1853 return {
1854 firstOffset: result.firstOffset,
1855 entries,
1856 unresolved,
1857 consumedTo: end < 0 ? result.consumedTo : end,
1858 count: entries.length + unresolved.length,
1859 };
1860 };
1861 const trace =
1862 process.env.GIT_PUMP_DEBUG === "1"
1863 ? (...args: Array<unknown>) => console.log("[pump]", ...args)
1864 : () => {};
1865 // The consumer is the forked side: hash fibers are children of THIS
1866 // fiber (a child's children die with it), so the producer runs here
1867 // and the consumer settles results as they land.
1868 // The entry straddling a part edge is hashed as soon as its part
1869 // settles — sized from its own header, from the retained bytes —
1870 // so a large object crossing several parts (a 10 MB blob spans
1871 // three) is verified alongside the parts, not after the last one.
1872 interface Speculative {
1873 readonly start: number;
1874 readonly fiber: Fiber.Fiber<
1875 { readonly bytes: Uint8Array; readonly r: ScanResult },
1876 PackIngestError
1877 >;
1878 }
1879 const speculate = (start: number) =>
1880 Effect.gen(function* () {
1881 const head = bytesFrom(start, Math.min(start + 32, availableEnd()));
1882 let need = yield* Effect.try({
1883 try: () => {
1884 const h = decodeTypeSize(head, 0);
1885 // zlib's worst case is ~0.1% + a few bytes; deltas carry an
1886 // offset varint before their stream.
1887 return start + h.next + Math.ceil(h.size * 1.001) + 80;
1888 },
1889 catch: (error) => PackIngestError.make({ reason: String(error) }),
1890 }).pipe(
1891 // Header cut by the received edge: wait for more below.
1892 Effect.orElseSucceed(() => start + 64),
1893 );
1894 while (true) {
1895 const end = Math.min(need, availableEnd());
1896 const bytes = bytesFrom(start, end);
1897 const r = yield* hasher
1898 .hashPart(bytes, {
1899 base: start,
1900 remaining: 1,
1901 maxObjectSize: MAX_OBJECT_SIZE,
1902 })
1903 .pipe(Effect.mapError(asIngest));
1904 if (r.count === 1) return { bytes, r };
1905 if (producerDone && end >= availableEnd()) return { bytes, r };
1906 // Not complete in what has arrived: wait for another part.
1907 if (end >= availableEnd()) yield* awaitChunk(chunks.length);
1908 if (end >= need) need = availableEnd();
1909 }
1910 });
1911 const consume = Effect.gen(function* () {
1912 let staged = 0;
1913 let prevEnd = 12;
1914 let speculative: Speculative | undefined;
1915 for (let k = 0; ; k++) {
1916 yield* awaitChunk(k);
1917 if (k >= chunks.length) break;
1918 const current = chunks[k]!;
1919 const result = yield* Fiber.join(current.fiber);
1920 console.log(`[pump] chunk ${k + 1} settled at +${Date.now() - pumpStarted}ms`);
1921 if (result.part !== undefined) {
1922 parts.push(result.part.pipe(Effect.mapError(asIngest)));
1923 }
1924 const chunkEnd = current.base + current.payload.length;
1925 trace(
1926 `chunk ${k} base=${current.base} len=${current.payload.length} first=${result.firstOffset} consumedTo=${result.consumedTo} count=${result.count} prevEnd=${prevEnd} staged=${staged}`,
1927 );
1928 const sequential = () =>
1929 Effect.gen(function* () {
1930 // Everything from the settled boundary through this chunk,
1931 // scanned in order (plus a little of the next chunk so the last
1932 // entry can complete, if there is one).
1933 yield* awaitChunk(k + 1);
1934 const next = chunks[k + 1];
1935 const to =
1936 next === undefined
1937 ? chunkEnd
1938 : Math.min(chunkEnd + 64, next.base + next.payload.length);
1939 const bytes = bytesFrom(prevEnd, to);
1940 const r = yield* hasher
1941 .hashPart(bytes, {
1942 base: prevEnd,
1943 remaining: count - staged,
1944 maxObjectSize: MAX_OBJECT_SIZE,
1945 })
1946 .pipe(Effect.mapError(asIngest));
1947 return { bytes, base: prevEnd, r: keepBelow(r, chunkEnd) };
1948 });
1949 if (result.firstOffset < 0) {
1950 // No boundary in this chunk (tiny, or inside one huge entry):
1951 // its bytes are settled by the pending straddler or a later
1952 // chunk's region scan.
1953 continue;
1954 }
1955 if (speculative !== undefined) {
1956 const sp = yield* Fiber.join(speculative.fiber);
1957 const start = speculative.start;
1958 speculative = undefined;
1959 if (sp.r.count === 1 && start === prevEnd && sp.r.consumedTo <= result.firstOffset) {
1960 staged += yield* stageResult(sp.bytes, start, sp.r);
1961 prevEnd = sp.r.consumedTo;
1962 }
1963 // Otherwise the boundary is suspect: the paths below rescan.
1964 }
1965 if (k === 0 && result.firstOffset !== 12) {
1966 const again = yield* sequential();
1967 staged += yield* stageResult(again.bytes, again.base, again.r);
1968 prevEnd = again.r.consumedTo;
1969 continue;
1970 }
1971 if (result.firstOffset < prevEnd) {
1972 // Resync landed inside an already-settled entry: rescan.
1973 const again = yield* sequential();
1974 staged += yield* stageResult(again.bytes, again.base, again.r);
1975 prevEnd = again.r.consumedTo;
1976 continue;
1977 }
1978 if (result.firstOffset > prevEnd) {
1979 trace(` region [${prevEnd}, ${result.firstOffset})`);
1980 regionCalls += 1;
1981 const region = bytesFrom(prevEnd, Math.min(result.firstOffset + 64, chunkEnd));
1982 const scanned = yield* hasher
1983 .hashPart(region, {
1984 base: prevEnd,
1985 remaining: count - staged,
1986 maxObjectSize: MAX_OBJECT_SIZE,
1987 })
1988 .pipe(Effect.mapError(asIngest));
1989 const ends =
1990 scanned.entries.some((e) => e.dataOffset + e.span === result.firstOffset) ||
1991 scanned.unresolved.some((u) => u.dataOffset + u.span === result.firstOffset);
1992 trace(
1993 ` region scan count=${scanned.count} consumedTo=${scanned.consumedTo} ends=${ends}`,
1994 );
1995 if (!ends) {
1996 trace(` → sequential fallback`);
1997 const again = yield* sequential();
1998 staged += yield* stageResult(again.bytes, again.base, again.r);
1999 prevEnd = again.r.consumedTo;
2000 continue;
2001 }
2002 staged += yield* stageResult(region, prevEnd, keepBelow(scanned, result.firstOffset));
2003 }
2004 staged += yield* stageResult(current.payload, current.base, result);
2005 prevEnd = result.consumedTo;
2006 if (prevEnd < chunkEnd && staged < count) {
2007 const start = prevEnd;
2008 speculative = {
2009 start,
2010 fiber: yield* Effect.forkChild(speculate(start)),
2011 };
2012 }
2013 }
2014 // A straddler still pending at the end (the last parts held no
2015 // boundary): settle it before the tail scan.
2016 if (speculative !== undefined) {
2017 const sp = yield* Fiber.join(speculative.fiber);
2018 const start = speculative.start;
2019 speculative = undefined;
2020 if (sp.r.count === 1 && start === prevEnd) {
2021 staged += yield* stageResult(sp.bytes, start, sp.r);
2022 prevEnd = sp.r.consumedTo;
2023 }
2024 }
2025 return { staged, prevEnd };
2026 });
2027 const consumer = yield* Effect.forkChild(consume);
2028 yield* produce;
2029 console.log(`[pump] body ended at +${Date.now() - pumpStarted}ms`);
2030 phases.upload = Date.now() - pumpStarted;
2031 let { staged, prevEnd } = yield* Fiber.join(consumer);
2032 // The tail: chunks too small to hold two whole entries report no
2033 // boundary, so entries after the last settled boundary may remain.
2034 // Scan that final region sequentially (it ends at the trailer).
2035 if (staged < count) {
2036 const last = chunks[chunks.length - 1];
2037 if (last !== undefined) {
2038 const to = last.base + last.payload.length;
2039 if (to > prevEnd) {
2040 const bytes = bytesFrom(prevEnd, to);
2041 const r = yield* hasher
2042 .hashPart(bytes, {
2043 base: prevEnd,
2044 remaining: count - staged,
2045 maxObjectSize: MAX_OBJECT_SIZE,
2046 })
2047 .pipe(Effect.mapError(asIngest));
2048 staged += yield* stageResult(bytes, prevEnd, r);
2049 prevEnd = r.consumedTo;
2050 }
2051 }
2052 }
2053 const consumedTo = prevEnd;
2054 phases.scan = Date.now() - pumpStarted;
2055 phases.chunks = chunks.length;
2056 phases.regions = regionCalls;
2057 if (staged !== count) {
2058 return yield* PackIngestError.make({
2059 reason: `pack declared ${count} entries, scanned ${staged}`,
2060 });
2061 }
2062 const total =
2063 source.awaitEnd === undefined
2064 ? source.size
2065 : yield* source.awaitEnd.pipe(Effect.mapError(asIngest));
2066 if (consumedTo !== total - 20) {
2067 return yield* PackIngestError.make({
2068 reason: `pack has ${total - 20 - consumedTo} unconsumed bytes after ${count} entries`,
2069 });
2070 }
2071 // Everything read so far went through `feed`; the last part's
2072 // bytes did too, so the lag now holds the trailer.
2073 if (lag.length !== 20) {
2074 return yield* PackIngestError.make({
2075 reason: "truncated pack trailer",
2076 });
2077 }
2078 const expected = bytesToHex(lag);
2079 const actual = trailerSha.digestHex();
2080 if (expected !== actual) {
2081 return yield* PackIngestError.make({
2082 reason: `pack checksum mismatch: expected ${expected}, got ${actual}`,
2083 });
2084 }
2085 // Cross-part and thin deltas: bases are in the retained payloads
2086 // (or in the live store). Fixpoint for chains.
2087 const deltasStarted = Date.now();
2088 phases.unresolved = unresolved.length;
2089 // The resolved pack (DESIGN §22.13): every delta-resolved blob's
2090 // fresh zlib, `typeSize header + zdata` per entry, one object; rows
2091 // point at their zdata like promoted wire-pack rows do. Written in
2092 // two parts so the in-chunk resolutions — most of them — upload
2093 // while the cross-chunk rounds run.
2094 const writeResolvedPack = (suffix: string) =>
2095 Effect.gen(function* () {
2096 if (resolvedBlobs.length === 0 || spill === undefined) return;
2097 const packId = `${spill.packId}${suffix}`;
2098 const key = packKeyOf(spill.repoId, packId);
2099 const pieces: Array<Uint8Array> = [];
2100 const rows: Array<Incoming> = [];
2101 let cursor = 0;
2102 for (const entry of resolvedBlobs) {
2103 const header = encodeTypeSize(entry.type, entry.size);
2104 pieces.push(header, entry.zdata!);
2105 rows.push({
2106 ...entry,
2107 pack: { packId, offset: cursor + header.length },
2108 });
2109 cursor += header.length + entry.zdata!.byteLength;
2110 }
2111 resolvedBlobs.length = 0;
2112 const bytes = concatBytes(pieces);
2113 const writeStarted = Date.now();
2114 const writing = yield* Effect.forkDetach(
2115 spill.blobs.put(key, bytes).pipe(
2116 Effect.mapError((error) =>
2117 PackIngestError.make({
2118 reason: `resolved pack: ${error.reason}`,
2119 }),
2120 ),
2121 Effect.provide(RuntimeContext.phantom),
2122 ),
2123 );
2124 resolvedKeys.push(key);
2125 resolvedKey = resolvedKeys.join(",");
2126 phases.resolvedBytes = (phases.resolvedBytes ?? 0) + bytes.length;
2127 yield* stage(rows);
2128 return { writing, writeStarted };
2129 });
2130 const early = yield* writeResolvedPack("-r");
2131 const spanOf = (at: number, length: number) =>
2132 Effect.suspend(() => {
2133 const bytes = readSpan?.(at, length);
2134 return bytes === undefined ? source.read(at, length) : Effect.succeed(bytes);
2135 });
2136 const _inflateSpan = (item: Known) =>
2137 item.content !== undefined
2138 ? Effect.succeed(item.content)
2139 : item.zdata !== undefined
2140 ? Zlib.inflate(item.zdata).pipe(Effect.mapError(asIngest))
2141 : spanOf(item.dataOffset, item.span).pipe(
2142 Effect.flatMap((z) => Zlib.inflate(z)),
2143 Effect.mapError(asIngest),
2144 );
2145 // Rounds (DESIGN §22.13): every delta whose base is known goes into
2146 // byte-bounded batches the hasher resolves in parallel (its bases
2147 // shipped once per batch); a delta on a still-unresolved delta waits
2148 // for the next round. The receiver only assembles bytes it already
2149 // holds.
2150 const batchBudget = hasher.chunkBytes ?? HASH_PART_BYTES;
2151 const baseBytesOf = (item: Known) =>
2152 Effect.gen(function* () {
2153 if (item.content !== undefined) {
2154 return { bytes: item.content, isContent: true };
2155 }
2156 if (item.zdata !== undefined) {
2157 return { bytes: item.zdata, isContent: false };
2158 }
2159 return {
2160 bytes: yield* spanOf(item.dataOffset, item.span),
2161 isContent: false,
2162 };
2163 });
2164 let pending = unresolved;
2165 while (pending.length > 0) {
2166 const next: Array<UnresolvedDelta> = [];
2167 interface Ready {
2168 readonly u: UnresolvedDelta;
2169 readonly base: Known;
2170 readonly baseKey: string;
2171 }
2172 const ready: Array<Ready> = [];
2173 for (const u of pending) {
2174 let base: Known | undefined =
2175 u.baseOffset !== undefined ? known.get(u.baseOffset) : undefined;
2176 if (base === undefined && u.baseOid !== undefined) {
2177 base = knownByOid.get(u.baseOid);
2178 if (base === undefined) {
2179 const live = yield* store.readBase(u.baseOid);
2180 if (live !== undefined) {
2181 base = {
2182 oid: u.baseOid,
2183 type: live.type,
2184 dataOffset: -1,
2185 span: 0,
2186 content: live.content,
2187 };
2188 knownByOid.set(u.baseOid, base);
2189 }
2190 }
2191 }
2192 if (base === undefined) {
2193 next.push(u);
2194 continue;
2195 }
2196 ready.push({
2197 u,
2198 base,
2199 baseKey: base.dataOffset >= 0 ? `o:${base.dataOffset}` : `oid:${base.oid}`,
2200 });
2201 }
2202 if (ready.length === 0) {
2203 return yield* PackIngestError.make({
2204 reason: `delta base not found for ${next.length} entries`,
2205 });
2206 }
2207 // Greedy byte-bounded batches; a base shared by several deltas is
2208 // carried once per batch.
2209 interface Batch {
2210 readonly bases: Array<DeltaBase>;
2211 readonly index: Map<string, number>;
2212 readonly jobs: Array<DeltaJob>;
2213 readonly items: Array<Ready>;
2214 bytes: number;
2215 }
2216 const batches: Array<Batch> = [];
2217 let current: Batch | undefined;
2218 for (const r of ready) {
2219 const delta = yield* spanOf(r.u.dataOffset, r.u.span);
2220 const baseBytes = yield* baseBytesOf(r.base);
2221 const baseCost = current?.index.has(r.baseKey) ? 0 : baseBytes.bytes.length;
2222 if (
2223 current === undefined ||
2224 (current.jobs.length > 0 && current.bytes + delta.length + baseCost > batchBudget)
2225 ) {
2226 current = {
2227 bases: [],
2228 index: new Map(),
2229 jobs: [],
2230 items: [],
2231 bytes: 0,
2232 };
2233 batches.push(current);
2234 }
2235 let baseIndex = current.index.get(r.baseKey);
2236 if (baseIndex === undefined) {
2237 baseIndex = current.bases.push(baseBytes) - 1;
2238 current.index.set(r.baseKey, baseIndex);
2239 current.bytes += baseBytes.bytes.length;
2240 }
2241 current.jobs.push({
2242 id: current.jobs.length,
2243 type: r.base.type,
2244 base: baseIndex,
2245 delta,
2246 });
2247 current.items.push(r);
2248 current.bytes += delta.length;
2249 }
2250 const resolved = yield* Effect.forEach(
2251 batches,
2252 (batch) =>
2253 hasher
2254 .resolveDeltas(batch.bases, batch.jobs, {
2255 maxObjectSize: MAX_OBJECT_SIZE,
2256 })
2257 .pipe(
2258 Effect.catchTag("HashError", (error) => {
2259 console.warn(`[pump] ${error.reason}; resolving inline`);
2260 return PartialScan.resolveDeltas(batch.bases, batch.jobs, {
2261 maxObjectSize: MAX_OBJECT_SIZE,
2262 });
2263 }),
2264 Effect.mapError(asIngest),
2265 Effect.map((results) => ({ batch, results })),
2266 ),
2267 { concurrency: hasher.concurrency ?? HASH_CONCURRENCY },
2268 );
2269 for (const { batch, results } of resolved) {
2270 const incoming: Array<Incoming> = [];
2271 for (const result of results) {
2272 const r = batch.items[result.id]!;
2273 const item: Known = {
2274 oid: result.oid,
2275 type: r.base.type,
2276 dataOffset: r.u.dataOffset,
2277 span: r.u.span,
2278 zdata: result.zdata,
2279 content: result.content,
2280 };
2281 known.set(r.u.offset, item);
2282 knownByOid.set(result.oid, item);
2283 incoming.push({
2284 oid: result.oid,
2285 type: r.base.type,
2286 size: result.size,
2287 zdata: result.zdata,
2288 span: r.u.span,
2289 dataOffset: r.u.dataOffset,
2290 fromDelta: true,
2291 content: result.content,
2292 });
2293 }
2294 yield* stage(incoming);
2295 }
2296 pending = next;
2297 }
2298 phases.deltas = Date.now() - deltasStarted;
2299 const late = yield* writeResolvedPack("-r2");
2300 yield* flush();
2301 for (const pending of [early, late]) {
2302 if (pending === undefined) continue;
2303 yield* Fiber.join(pending.writing);
2304 phases.resolvedPack = Math.max(
2305 phases.resolvedPack ?? 0,
2306 Date.now() - pending.writeStarted,
2307 );
2308 }
2309 // Settle the spill: every part was written by its hasher; complete
2310 // the object and hand it to the source as the fallback reader.
2311 if (upload !== undefined && spill !== undefined) {
2312 const settled = upload;
2313 const uploaded: Array<UploadedPart> = [];
2314 for (const part of parts) uploaded.push(yield* part);
2315 yield* settled.complete(uploaded).pipe(
2316 Effect.mapError((error) =>
2317 PackIngestError.make({
2318 reason: `spill complete: ${error.reason}`,
2319 }),
2320 ),
2321 Effect.provide(RuntimeContext.phantom),
2322 );
2323 spill.feeder.setFallback(
2324 blobRandomAccess({
2325 blobs: spill.blobs,
2326 key: spill.key,
2327 size: yield* spill.feeder.source.awaitEnd.pipe(Effect.mapError(asIngest)),
2328 }),
2329 );
2330 parkedKey = spill.key;
2331 }
2332 return { count };
2333 }).pipe(
2334 // A failed ingest must not leave a dangling multipart upload —
2335 // R2 bills incomplete uploads until aborted or lifecycle-expired.
2336 Effect.onError(() =>
2337 Effect.suspend(() =>
2338 upload === undefined || parkedKey !== undefined
2339 ? Effect.void
2340 : upload.abort.pipe(Effect.provide(RuntimeContext.phantom)),
2341 ).pipe(Effect.ignore),
2342 ),
2343 Effect.onError(() =>
2344 Effect.suspend(() =>
2345 resolvedKeys.length === 0 || options.spill === undefined
2346 ? Effect.void
2347 : options.spill.blobs
2348 .delete([...resolvedKeys])
2349 .pipe(Effect.provide(RuntimeContext.phantom)),
2350 ).pipe(Effect.ignore),
2351 ),
2352 );
2353
2354 const summary =
2355 options.hasher !== undefined
2356 ? yield* pumpThroughHasher(options.hasher)
2357 : yield* PackParser.ingestPack({
2358 source,
2359 store,
2360 maxObjectSize: MAX_OBJECT_SIZE,
2361 phases,
2362 sink: (entry) => stage([entry]),
2363 sinkBatch: stage,
2364 }).pipe(
2365 Effect.mapError((error) =>
2366 error._tag === "StoreError" || error._tag === "PackIngestError"
2367 ? error
2368 : PackIngestError.make({ reason: packParserReason(error) }),
2369 ),
2370 );
2371
2372 yield* flush();
2373 if (store.settle !== undefined) {
2374 const at = yield* Effect.sync(() => performance.now());
2375 yield* store.settle;
2376 stageMs += yield* Effect.sync(() => performance.now() - at);
2377 }
2378
2379 return {
2380 stageMs,
2381 phases,
2382 promoted,
2383 parkedKey,
2384 resolvedKey,
2385 objectCount: summary.count,
2386 // The parser verifies the trailer itself; the checksum is not used
2387 // downstream, so it is not re-derived here.
2388 packSha: "",
2389 commits,
2390 // The connectivity check (DESIGN §22.14) needs only the edges that
2391 // leave the push: everything staged here is known present.
2392 referenced: Array.from(referenced).filter((oid) => !stagedOids.has(oid)),
2393 referencedParents: Array.from(referencedParents).filter((oid) => !stagedOids.has(oid)),
2394 } satisfies IngestResult;
2395 });
2396
2397 /** Renders a `PackParser` failure as an in-band `unpack <reason>` string. */
2398 const packParserReason = (error: {
2399 readonly _tag: string;
2400 readonly reason?: string;
2401 readonly expected?: string;
2402 readonly actual?: string;
2403 readonly baseOid?: string;
2404 readonly size?: number;
2405 readonly limit?: number;
2406 }): string => {
2407 switch (error._tag) {
2408 case "PackChecksumMismatch":
2409 return "pack checksum mismatch";
2410 case "MissingDeltaBaseError":
2411 return `missing delta base ${error.baseOid ?? ""}`.trim();
2412 case "ObjectTooLargeError":
2413 return `object too large (${error.size ?? 0} > ${error.limit ?? 0})`;
2414 default:
2415 return error.reason ?? error._tag;
2416 }
2417 };
2418
2419 export const ingestPack = (
2420 pack: Uint8Array,
2421 options: Omit<IngestOptions, "store"> & { readonly store: ObjectStore },
2422 ): Effect.Effect<IngestResult, PackIngestError | StoreError> =>
2423 Effect.gen(function* () {
2424 const { store, pushId } = options;
2425 if (pack.length < 12 + 20) {
2426 return yield* PackIngestError.make({ reason: "pack too small" });
2427 }
2428 if (pack[0] !== 0x50 || pack[1] !== 0x41 || pack[2] !== 0x43 || pack[3] !== 0x4b) {
2429 return yield* PackIngestError.make({ reason: "bad pack signature" });
2430 }
2431 const view = new DataView(pack.buffer, pack.byteOffset, pack.byteLength);
2432 const version = view.getUint32(4);
2433 if (version !== 2) {
2434 return yield* PackIngestError.make({
2435 reason: `unsupported pack version ${version}`,
2436 });
2437 }
2438 const count = view.getUint32(8);
2439
2440 // Trailer verification first — cheap, and everything after assumes an
2441 // intact buffer.
2442 const trailer = bytesToHex(pack.subarray(pack.length - 20));
2443 const sha = makeSha1();
2444 sha.update(pack.subarray(0, pack.length - 20));
2445 const actual = sha.digestHex();
2446 if (actual !== trailer) {
2447 return yield* PackIngestError.make({ reason: "pack checksum mismatch" });
2448 }
2449
2450 const cache = makeContentCache(CACHE_BUDGET);
2451 const entries: Array<RawEntry> = [];
2452 const byOffset = new Map<number, RawEntry>();
2453 const byOid = new Map<Oid, RawEntry>();
2454
2455 const commits: Array<StagedCommitInfo> = [];
2456 const referenced = new Set<Oid>();
2457 const referencedParents = new Set<Oid>();
2458
2459 /** Record graph facts + referenced edges for a resolved object. */
2460 const recordObject = Effect.fn(function* (oid: Oid, type: ObjectType, content: Uint8Array) {
2461 switch (type) {
2462 case ObjectType.commit: {
2463 const parsed = yield* parseCommit(content).pipe(
2464 Effect.mapError((error) =>
2465 PackIngestError.make({
2466 reason: `bad commit ${oid}: ${error.reason}`,
2467 }),
2468 ),
2469 );
2470 commits.push({
2471 oid,
2472 tree: parsed.tree,
2473 parents: parsed.parents,
2474 commitTime: parsed.committer.when,
2475 });
2476 referenced.add(parsed.tree);
2477 for (const parent of parsed.parents) {
2478 referencedParents.add(parent);
2479 }
2480 return;
2481 }
2482 case ObjectType.tree: {
2483 const parsed = yield* parseTree(content).pipe(
2484 Effect.mapError((error) =>
2485 PackIngestError.make({
2486 reason: `bad tree ${oid}: ${error.reason}`,
2487 }),
2488 ),
2489 );
2490 for (const entry of parsed) {
2491 if (treeEntryKind(entry.mode) !== "commit") {
2492 referenced.add(entry.oid);
2493 }
2494 }
2495 return;
2496 }
2497 case ObjectType.tag: {
2498 const parsed = yield* parseTag(content).pipe(
2499 Effect.mapError((error) =>
2500 PackIngestError.make({
2501 reason: `bad tag ${oid}: ${error.reason}`,
2502 }),
2503 ),
2504 );
2505 referenced.add(parsed.object);
2506 return;
2507 }
2508 case ObjectType.blob:
2509 return;
2510 }
2511 });
2512
2513 const stage = Effect.fn(function* (
2514 oid: Oid,
2515 type: ObjectType,
2516 content: Uint8Array,
2517 zdata: Uint8Array,
2518 ) {
2519 const staged: StagedObject = {
2520 oid,
2521 type,
2522 size: content.length,
2523 zdata,
2524 };
2525 yield* store.insertStaged(pushId, staged);
2526 yield* recordObject(oid, type, content);
2527 });
2528
2529 // ── Pass 1: scan entries, stage non-deltas verbatim ─────────────────────
2530 let offset = 12;
2531 const zlibToIngest = (error: Zlib.ZlibError): PackIngestError =>
2532 PackIngestError.make({ reason: error.reason });
2533
2534 for (let i = 0; i < count; i++) {
2535 const entryStart = offset;
2536 const header = yield* Effect.try({
2537 try: () => decodeTypeSize(pack, offset),
2538 catch: (error) => PackIngestError.make({ reason: `entry ${i}: ${String(error)}` }),
2539 });
2540 if (!isDeltaType(header.type)) {
2541 if (header.size > MAX_OBJECT_SIZE) {
2542 return yield* PackIngestError.make({ reason: "object too large" });
2543 }
2544 const inflated = yield* Zlib.inflateEntry(pack, header.next, {
2545 maxOutput: header.size,
2546 }).pipe(Effect.mapError(zlibToIngest));
2547 if (inflated.content.length !== header.size) {
2548 return yield* PackIngestError.make({
2549 reason: `entry ${i}: size mismatch (${inflated.content.length} != ${header.size})`,
2550 });
2551 }
2552 const type = header.type as ObjectType;
2553 const oid = yield* hashObject(type, inflated.content);
2554 const zdata = pack.subarray(header.next, header.next + inflated.bytesConsumed);
2555 const entry: RawEntry = {
2556 offset: entryStart,
2557 kind: "object",
2558 type,
2559 declaredSize: header.size,
2560 zstart: header.next,
2561 zlen: inflated.bytesConsumed,
2562 baseOffset: undefined,
2563 baseOid: undefined,
2564 oid,
2565 resolvedType: type,
2566 };
2567 entries.push(entry);
2568 byOffset.set(entryStart, entry);
2569 byOid.set(oid, entry);
2570 cache.set(oid, inflated.content);
2571 yield* stage(oid, type, inflated.content, zdata);
2572 offset = header.next + inflated.bytesConsumed;
2573 } else {
2574 let zstart = header.next;
2575 let baseOffset: number | undefined;
2576 let baseOid: Oid | undefined;
2577 if (header.type === 6) {
2578 const decoded = yield* Effect.try({
2579 try: () => decodeOfsDeltaOffset(pack, header.next),
2580 catch: (error) => PackIngestError.make({ reason: `entry ${i}: ${String(error)}` }),
2581 });
2582 baseOffset = entryStart - decoded.value;
2583 if (baseOffset < 12) {
2584 return yield* PackIngestError.make({
2585 reason: `entry ${i}: ofs-delta base offset out of range`,
2586 });
2587 }
2588 zstart = decoded.next;
2589 } else {
2590 if (header.next + 20 > pack.length) {
2591 return yield* PackIngestError.make({
2592 reason: `entry ${i}: truncated ref-delta base id`,
2593 });
2594 }
2595 baseOid = bytesToHex(pack.subarray(header.next, header.next + 20));
2596 zstart = header.next + 20;
2597 }
2598 const inflated = yield* Zlib.inflateEntry(pack, zstart, {
2599 maxOutput: header.size,
2600 }).pipe(Effect.mapError(zlibToIngest));
2601 const entry: RawEntry = {
2602 offset: entryStart,
2603 kind: header.type === 6 ? "ofs" : "ref",
2604 type: undefined,
2605 declaredSize: header.size,
2606 zstart,
2607 zlen: inflated.bytesConsumed,
2608 baseOffset,
2609 baseOid,
2610 oid: undefined,
2611 resolvedType: undefined,
2612 };
2613 entries.push(entry);
2614 byOffset.set(entryStart, entry);
2615 offset = zstart + inflated.bytesConsumed;
2616 }
2617 if (offset > pack.length - 20) {
2618 return yield* PackIngestError.make({
2619 reason: `entry ${i}: pack truncated`,
2620 });
2621 }
2622 }
2623 if (offset !== pack.length - 20) {
2624 return yield* PackIngestError.make({
2625 reason: "trailing garbage between entries and pack trailer",
2626 });
2627 }
2628
2629 // ── Pass 2: resolve deltas (bases may chain; thin bases from store) ─────
2630 const contentOf = (
2631 entry: RawEntry,
2632 depth: number,
2633 ): Effect.Effect<
2634 { readonly content: Uint8Array; readonly type: ObjectType },
2635 PackIngestError | StoreError
2636 > =>
2637 Effect.gen(function* () {
2638 if (depth > MAX_DELTA_DEPTH) {
2639 return yield* PackIngestError.make({ reason: "delta chain too deep" });
2640 }
2641 if (entry.oid !== undefined && entry.resolvedType !== undefined) {
2642 const cached = cache.get(entry.oid);
2643 if (cached !== undefined) {
2644 return { content: cached, type: entry.resolvedType };
2645 }
2646 }
2647 if (entry.kind === "object") {
2648 const inflated = yield* Zlib.inflateEntry(pack, entry.zstart, {
2649 maxOutput: entry.declaredSize,
2650 }).pipe(Effect.mapError(zlibToIngest));
2651 if (entry.oid !== undefined) cache.set(entry.oid, inflated.content);
2652 return { content: inflated.content, type: entry.type! };
2653 }
2654 // delta — resolve the base first
2655 const resolveBase = Effect.gen(function* () {
2656 if (entry.kind === "ofs") {
2657 const baseEntry = byOffset.get(entry.baseOffset!);
2658 if (baseEntry === undefined) {
2659 return yield* PackIngestError.make({
2660 reason: "ofs-delta base is not an entry boundary",
2661 });
2662 }
2663 return yield* contentOf(baseEntry, depth + 1);
2664 }
2665 const inPack = byOid.get(entry.baseOid!);
2666 if (inPack !== undefined) {
2667 return yield* contentOf(inPack, depth + 1);
2668 }
2669 // Thin base: must exist in the live store.
2670 const meta = yield* store.getMeta(entry.baseOid!);
2671 if (meta === undefined) {
2672 return yield* PackIngestError.make({
2673 reason: `missing thin-pack base ${entry.baseOid}`,
2674 });
2675 }
2676 if (meta.size > MAX_OBJECT_SIZE) {
2677 return yield* PackIngestError.make({ reason: "object too large" });
2678 }
2679 const content = yield* store.readContent(entry.baseOid!);
2680 cache.set(entry.baseOid!, content);
2681 return { content, type: meta.type };
2682 });
2683 const base = yield* resolveBase;
2684 const delta = yield* Zlib.inflateEntry(pack, entry.zstart, {
2685 maxOutput: entry.declaredSize,
2686 }).pipe(Effect.mapError(zlibToIngest));
2687 const content = yield* applyDelta(base.content, delta.content).pipe(
2688 Effect.mapError((error) => PackIngestError.make({ reason: error.reason })),
2689 );
2690 if (content.length > MAX_OBJECT_SIZE) {
2691 return yield* PackIngestError.make({ reason: "object too large" });
2692 }
2693 return { content, type: base.type };
2694 });
2695
2696 for (const entry of entries) {
2697 if (entry.kind === "object" || entry.oid !== undefined) continue;
2698 const resolved = yield* contentOf(entry, 0);
2699 const oid = yield* hashObject(resolved.type, resolved.content);
2700 entry.oid = oid;
2701 entry.resolvedType = resolved.type;
2702 byOid.set(oid, entry);
2703 cache.set(oid, resolved.content);
2704 const zdata = yield* Zlib.deflate(resolved.content).pipe(Effect.mapError(zlibToIngest));
2705 yield* stage(oid, resolved.type, resolved.content, zdata);
2706 }
2707
2708 return {
2709 objectCount: count,
2710 packSha: trailer,
2711 commits,
2712 referenced: Array.from(referenced),
2713 referencedParents: Array.from(referencedParents),
2714 } satisfies IngestResult;
2715 });
2716
2717 // ─────────────────────────────────────────────────────────────────────────────
2718 // Small shared helpers
2719 // ─────────────────────────────────────────────────────────────────────────────
2720
2721 const asOid = (s: string): ApiOid => s as ApiOid;
2722
2723 /** A node of the merge-base paint walk's max-heap (gen desc, time desc). */
2724 export interface WalkHeapNode {
2725 readonly oid: string;
2726 readonly gen: number;
2727 readonly time: number;
2728 }
2729
2730 const heapBefore = (a: WalkHeapNode, b: WalkHeapNode): boolean =>
2731 a.gen !== b.gen ? a.gen > b.gen : a.time > b.time;
2732
2733 /** Pushes onto a binary max-heap keyed `(gen desc, commit_time desc)`. */
2734 export const heapPush = (heap: Array<WalkHeapNode>, node: WalkHeapNode): void => {
2735 heap.push(node);
2736 let i = heap.length - 1;
2737 while (i > 0) {
2738 const parent = (i - 1) >> 1;
2739 if (!heapBefore(heap[i]!, heap[parent]!)) break;
2740 const tmp = heap[parent]!;
2741 heap[parent] = heap[i]!;
2742 heap[i] = tmp;
2743 i = parent;
2744 }
2745 };
2746
2747 /** Pops the max node. Callers must check `heap.length > 0` first. */
2748 export const heapPop = (heap: Array<WalkHeapNode>): WalkHeapNode => {
2749 const top = heap[0]!;
2750 const last = heap.pop()!;
2751 if (heap.length > 0) {
2752 heap[0] = last;
2753 let i = 0;
2754 for (;;) {
2755 const left = 2 * i + 1;
2756 const right = left + 1;
2757 let max = i;
2758 if (left < heap.length && heapBefore(heap[left]!, heap[max]!)) {
2759 max = left;
2760 }
2761 if (right < heap.length && heapBefore(heap[right]!, heap[max]!)) {
2762 max = right;
2763 }
2764 if (max === i) break;
2765 const tmp = heap[max]!;
2766 heap[max] = heap[i]!;
2767 heap[i] = tmp;
2768 i = max;
2769 }
2770 }
2771 return top;
2772 };
2773
2774 /** Decompresses a request body when it is gzip (header or 1f8b sniff). */
2775 export const gunzipIfNeeded = (
2776 body: Uint8Array,
2777 contentEncoding: string | undefined,
2778 ): Effect.Effect<Uint8Array, PackIngestError> => {
2779 const looksGzip = body.length >= 2 && body[0] === 0x1f && body[1] === 0x8b;
2780 if (contentEncoding?.toLowerCase() !== "gzip" && !looksGzip) {
2781 return Effect.succeed(body);
2782 }
2783 if (!looksGzip) {
2784 // Content-Encoding claimed gzip but the magic disagrees — trust bytes.
2785 return Effect.succeed(body);
2786 }
2787 return Effect.tryPromise({
2788 try: () =>
2789 new Response(
2790 new Response(body as BodyInit).body!.pipeThrough(new DecompressionStream("gzip")),
2791 )
2792 .arrayBuffer()
2793 .then((buffer) => new Uint8Array(buffer)),
2794 catch: (error) =>
2795 PackIngestError.make({
2796 reason: `gzip decompression failed: ${error instanceof Error ? error.message : String(error)}`,
2797 }),
2798 });
2799 };
2800
2801 const noCache = { "cache-control": "no-cache" } as const;
2802
2803 // ─────────────────────────────────────────────────────────────────────────────
2804 // The Durable Object
2805 // ─────────────────────────────────────────────────────────────────────────────
2806
2807 /**
2808 * The per-repo Durable Object class (modular form). The runtime
2809 * implementation is {@link GitRepoLive}; the hosting Worker provides it via
2810 * `Effect.provide(GitRepoLive)` and consumers `yield* GitRepo` for the
2811 * namespace handle.
2812 */
2813 export class GitRepo extends Cloudflare.DurableObject<GitRepo, GitRepoShape>()("GitRepo", {
2814 // Every tagged error the RPC surface can fail with: the calling Worker
2815 // reconstructs real class instances from the RPC wire (see the
2816 // `DurableObjectProps.errors` doc), so catchTag/instanceof/HttpApi
2817 // encoding all see the classes the DO actually failed with.
2818 errors: [
2819 ReadOnlyRepo,
2820 RepoNotFound,
2821 RefNotFound,
2822 RefConflict,
2823 ObjectNotFound,
2824 WrongObjectType,
2825 NoMergeBase,
2826 PullNotFound,
2827 PullExists,
2828 PullStateConflict,
2829 BranchMissing,
2830 NothingToMerge,
2831 MergeConflict,
2832 ValidationError,
2833 StoreError,
2834 PackIngestError,
2835 WireProtocolError,
2836 ],
2837 }) {}
2838
2839 /**
2840 * A repository's RPC stub: {@link GitRepoShape} as the Worker calls it,
2841 * with `fetch` taking the request.
2842 */
2843 export type RepoStub = ReturnType<Cloudflare.Workers.DurableObject<GitRepoShape>["getByName"]>;
2844
2845 /** What {@link RepoStore} provides: a repository's stub by id. */
2846 export interface RepoStoreShape {
2847 /** The repository's stub, by its registry id (a ULID). */
2848 readonly getByName: (repoId: string) => RepoStub;
2849 }
2850
2851 /**
2852 * `Git.RepoStore` — the repository block as a service: every route reaches
2853 * a repository through it. `ReposDurableObject` provides it over the
2854 * {@link GitRepo} Durable Object namespace. It is the seam for decorating
2855 * repositories (hook `commitPush`, `updateRef`, `mergePull` to observe ref
2856 * moves) or for implementing {@link GitRepoShape} over another store.
2857 *
2858 * @binding
2859 */
2860 export class RepoStore extends Context.Service<RepoStore, RepoStoreShape>()(
2861 "alchemy/Git/RepoStore",
2862 ) {}
2863
2864 /**
2865 * The Repo DO implementation Layer. Requires the {@link BlobStore}
2866 * service (provide `Git.BlobStoreR2(yourBucket)` — or any other
2867 * implementation — in the same layer graph) and the {@link Registry}
2868 * namespace.
2869 */
2870 export const GitRepoLive = GitRepo.make(
2871 Effect.gen(function* () {
2872 // ── Outer init: resolve bindings + namespaces (runs at plan time too) ──
2873 const state = yield* Cloudflare.DurableObjectState;
2874 // The building-blocks seam: bulk bytes live behind whatever BlobStore
2875 // the user's layer graph provides (R2 by default, S3, ...).
2876 const blobs: BlobStoreShape = yield* BlobStore;
2877 // The push pipeline's hasher (DESIGN §22.7): a self-binding fan-out in
2878 // production, in-process in tests without the binding.
2879 const registry = yield* RegistryStore;
2880 const selfNamespace = yield* Cloudflare.DurableObjectScope;
2881
2882 return Effect.gen(function* () {
2883 // ── Inner init: per-instance construction (runtime only) ────────────
2884 const sql = makeSqlClient(state);
2885 yield* initRepoSchema(sql).pipe(Effect.orDie);
2886
2887 // ── config helpers ───────────────────────────────────────────────────
2888 const getConfig = (key: string) =>
2889 sql
2890 .first<{ value: string }>(`SELECT value FROM config WHERE key = ?`, key)
2891 .pipe(Effect.map((row) => row?.value));
2892
2893 const setConfig = (key: string, value: string) =>
2894 sql.run(
2895 `INSERT INTO config (key, value) VALUES (?, ?)
2896 ON CONFLICT (key) DO UPDATE SET value = excluded.value`,
2897 key,
2898 value,
2899 );
2900
2901 /**
2902 * Repo metadata from the `config` table. The `objects` breakdown is
2903 * a GROUP BY over the whole `objects` table — on a large repo that
2904 * scan walks pages dense with inline zdata and was measured at
2905 * ~1.5 s per call (DESIGN §22). It is therefore opt-in: only the
2906 * REST repo-detail responses pay for it; the wire path, auth, and
2907 * every other caller get zeros there and never touch the table.
2908 */
2909 const readMetaWith = (
2910 withStats: boolean,
2911 ): Effect.Effect<RepoMetaData | undefined, StoreError> =>
2912 Effect.gen(function* () {
2913 const rows = yield* sql.all<{ key: string; value: string }>(
2914 `SELECT key, value FROM config`,
2915 );
2916 const map = new Map(rows.map((row) => [row.key, row.value]));
2917 const repoId = map.get("repo_id");
2918 if (repoId === undefined) return undefined;
2919 const stats = withStats
2920 ? yield* sql.all<{
2921 bucket: string;
2922 n: number;
2923 bytes: number;
2924 }>(
2925 // Row-resident blobs are "loose" (awaiting compaction);
2926 // row-resident commits/trees/tags are "resident" — they stay
2927 // in SQLite for the life of the repo (Compact.ts BLOB_TYPE).
2928 `SELECT CASE WHEN location = 'row' AND type <> 3 THEN 'resident'
2929 WHEN location = 'row' THEN 'loose'
2930 ELSE location END AS bucket,
2931 COUNT(*) AS n, COALESCE(SUM(zsize), 0) AS bytes
2932 FROM objects WHERE ${LIVE_OBJECTS} GROUP BY bucket`,
2933 )
2934 : [];
2935 const byLocation = new Map(stats.map((row) => [row.bucket, row]));
2936 return {
2937 repoId,
2938 owner: map.get("owner") ?? "",
2939 name: map.get("name") ?? "",
2940 defaultBranch: map.get("default_branch") ?? "main",
2941 description: map.get("description") ?? null,
2942 readOnly: map.get("read_only") === "1",
2943 public: map.get("public") === "1",
2944 forkOf: map.get("fork_of") ?? null,
2945 status: (map.get("status") ?? "ready") as RepoStatus,
2946 createdAt: Number(map.get("created_at") ?? 0),
2947 lastPush:
2948 map.get("last_push") === undefined
2949 ? null
2950 : (JSON.parse(map.get("last_push")!) as PushStatsData),
2951 objects: {
2952 loose: byLocation.get("loose")?.n ?? 0,
2953 resident: byLocation.get("resident")?.n ?? 0,
2954 packed: byLocation.get("pack")?.n ?? 0,
2955 r2: byLocation.get("r2")?.n ?? 0,
2956 bytes: stats.reduce((sum, row) => sum + row.bytes, 0),
2957 },
2958 } satisfies RepoMetaData;
2959 });
2960
2961 const readMeta = readMetaWith(false);
2962 const readMetaStats = readMetaWith(true);
2963
2964 const requireFrom = (
2965 read: Effect.Effect<RepoMetaData | undefined, StoreError>,
2966 ): Effect.Effect<RepoMetaData, RepoNotFound | StoreError> =>
2967 read.pipe(
2968 Effect.flatMap((meta) =>
2969 meta === undefined
2970 ? Effect.fail(RepoNotFound.make({ owner: "", repo: "" }))
2971 : Effect.succeed(meta),
2972 ),
2973 );
2974 /** Cheap: config only. `objects` reads as zeros. */
2975 const requireMeta = requireFrom(readMeta);
2976 /** The REST detail shape, with the full objects aggregate. */
2977 const requireMetaStats = requireFrom(readMetaStats);
2978
2979 /** The object store, keyed by the current repoId. */
2980 const storeFor = (repoId: string): ObjectStore => makeObjectStore({ sql, blobs, repoId });
2981
2982 // ── refs ─────────────────────────────────────────────────────────────
2983 /** Peels a (possibly annotated-tag) oid to its final non-tag target. */
2984 const peelOid = (repoId: string, oid: Oid): Effect.Effect<Oid | undefined, StoreError> =>
2985 Effect.gen(function* () {
2986 const objects = storeFor(repoId);
2987 let current = oid;
2988 for (let hops = 0; hops < 16; hops++) {
2989 const meta = yield* objects.getMeta(current);
2990 if (meta === undefined) return undefined;
2991 if (meta.type !== ObjectType.tag) {
2992 return hops === 0 ? undefined : current;
2993 }
2994 const content = yield* objects.readContent(current);
2995 const parsed = yield* parseTag(content).pipe(
2996 Effect.mapError((error) => StoreError.make({ reason: error.reason })),
2997 );
2998 current = parsed.object;
2999 }
3000 return current;
3001 });
3002
3003 /**
3004 * Resolves a refname (short or full) or 40-hex oid to an object oid.
3005 * Candidates for short names: `refs/heads/<name>`, `refs/tags/<name>`.
3006 * `undefined` means HEAD (the default branch).
3007 */
3008 const resolveRevision = Effect.fn(function* (defaultBranch: string, rev: string | undefined) {
3009 const refName = rev === undefined ? `refs/heads/${defaultBranch}` : rev;
3010 if (isOid(refName)) return refName;
3011 const candidates = refName.startsWith("refs/")
3012 ? [refName]
3013 : [`refs/heads/${refName}`, `refs/tags/${refName}`];
3014 for (const candidate of candidates) {
3015 const row = yield* sql.first<RefRow>(
3016 `SELECT name, oid FROM refs WHERE name = ?`,
3017 candidate,
3018 );
3019 if (row !== undefined) return row.oid;
3020 }
3021 return yield* RefNotFound.make({ ref: refName });
3022 });
3023
3024 /** Resolves a revision all the way to a COMMIT oid (peels tags). */
3025 const resolveToCommit = Effect.fn(function* (
3026 repoId: string,
3027 defaultBranch: string,
3028 rev: string,
3029 ) {
3030 const oid = yield* resolveRevision(defaultBranch, rev);
3031 const objects = storeFor(repoId);
3032 const meta = yield* objects.getMeta(oid);
3033 if (meta === undefined) return yield* ObjectNotFound.make({ oid });
3034 if (meta.type === ObjectType.commit) return oid;
3035 if (meta.type === ObjectType.tag) {
3036 const peeled = yield* peelOid(repoId, oid);
3037 if (peeled === undefined) {
3038 return yield* ObjectNotFound.make({ oid });
3039 }
3040 const peeledMeta = yield* objects.getMeta(peeled);
3041 if (peeledMeta === undefined) {
3042 return yield* ObjectNotFound.make({ oid: peeled });
3043 }
3044 if (peeledMeta.type !== ObjectType.commit) {
3045 return yield* WrongObjectType.make({
3046 oid: peeled,
3047 expected: "commit",
3048 actual: objectTypeName(peeledMeta.type),
3049 });
3050 }
3051 return peeled;
3052 }
3053 return yield* WrongObjectType.make({
3054 oid,
3055 expected: "commit",
3056 actual: objectTypeName(meta.type),
3057 });
3058 });
3059
3060 const listAllRefs = (
3061 repoId: string,
3062 prefix?: string,
3063 ): Effect.Effect<Array<RefData>, StoreError> =>
3064 Effect.gen(function* () {
3065 const rows =
3066 prefix === undefined || prefix.length === 0
3067 ? yield* sql.all<RefRow>(`SELECT name, oid FROM refs ORDER BY name`)
3068 : yield* sql.all<RefRow>(
3069 `SELECT name, oid FROM refs WHERE name >= ? AND name < ? ORDER BY name`,
3070 prefix,
3071 // next lexicographic sibling of the prefix
3072 `${prefix.slice(0, -1)}${String.fromCharCode(prefix.charCodeAt(prefix.length - 1) + 1)}`,
3073 );
3074 const out: Array<RefData> = [];
3075 for (const row of rows) {
3076 const peeled = row.name.startsWith("refs/tags/")
3077 ? yield* peelOid(repoId, row.oid)
3078 : undefined;
3079 out.push(
3080 peeled === undefined
3081 ? { name: row.name, oid: row.oid }
3082 : { name: row.name, oid: row.oid, peeled },
3083 );
3084 }
3085 return out;
3086 });
3087
3088 /**
3089 * The single transactional ref-CAS path (DESIGN.md §3.4). All ref
3090 * mutations — push commands, REST writes, import finalize — run here.
3091 */
3092 interface RefResult {
3093 readonly ref: string;
3094 readonly ok: boolean;
3095 readonly reason?: string | undefined;
3096 }
3097 const finalizeRefTxn = (options: {
3098 readonly commands: ReadonlyArray<RefCommand>;
3099 readonly atomic: boolean;
3100 /** Skip the old-oid CAS entirely (import finalize). */
3101 readonly unconditional?: boolean | undefined;
3102 readonly pushId?: string | undefined;
3103 /** Leave the per-row `staged_push` rewrite to `flipPush` (after the response). */
3104 readonly deferFlip?: boolean | undefined;
3105 readonly graph: ReadonlyArray<{
3106 readonly oid: string;
3107 readonly tree: string;
3108 readonly gen: number;
3109 readonly commitTime: number;
3110 readonly parents: ReadonlyArray<string>;
3111 }>;
3112 /**
3113 * Extra statements run inside the transaction body, only when at
3114 * least one command succeeded (right after the graph inserts) —
3115 * e.g. the PR merge stamps its pulls row atomically with the ref
3116 * flip and the staged-object promotion. MUST be fully synchronous.
3117 */
3118 readonly onCommitted?: ((raw: cf.SqlStorage) => void) | undefined;
3119 }): Effect.Effect<Array<RefResult>, StoreError, RuntimeContext> =>
3120 sql
3121 .transactionSync((raw) => {
3122 if (options.pushId !== undefined) {
3123 const push = raw
3124 .exec<{ state: string }>(
3125 `SELECT state FROM pushes WHERE push_id = ?`,
3126 options.pushId,
3127 )
3128 .toArray()[0];
3129 if (push?.state !== "staging")
3130 return options.commands.map((command) => ({
3131 ref: command.ref,
3132 ok: false,
3133 reason: "push is no longer active",
3134 }));
3135 }
3136 const currentOf = (name: string): string => {
3137 const rows = raw
3138 .exec<RefRow>(`SELECT name, oid FROM refs WHERE name = ?`, name)
3139 .toArray();
3140 return rows.length > 0 ? rows[0]!.oid : ZERO_OID;
3141 };
3142 const results: Array<RefResult> = [];
3143 // atomic: verify every CAS before writing anything
3144 if (options.atomic && options.unconditional !== true) {
3145 const failed = options.commands.some((cmd) => currentOf(cmd.ref) !== cmd.oldOid);
3146 if (failed) {
3147 for (const cmd of options.commands) {
3148 results.push({
3149 ref: cmd.ref,
3150 ok: false,
3151 reason:
3152 currentOf(cmd.ref) === cmd.oldOid
3153 ? "atomic transaction failed"
3154 : "fetch first",
3155 });
3156 }
3157 return results;
3158 }
3159 }
3160 let anyOk = false;
3161 for (const cmd of options.commands) {
3162 if (options.unconditional !== true && currentOf(cmd.ref) !== cmd.oldOid) {
3163 results.push({
3164 ref: cmd.ref,
3165 ok: false,
3166 reason: "fetch first",
3167 });
3168 continue;
3169 }
3170 if (cmd.newOid === ZERO_OID) {
3171 raw.exec(`DELETE FROM refs WHERE name = ?`, cmd.ref);
3172 } else {
3173 raw.exec(
3174 `INSERT INTO refs (name, oid) VALUES (?, ?)
3175 ON CONFLICT (name) DO UPDATE SET oid = excluded.oid`,
3176 cmd.ref,
3177 cmd.newOid,
3178 );
3179 }
3180 results.push({ ref: cmd.ref, ok: true });
3181 anyOk = true;
3182 }
3183 if (anyOk || options.commands.length === 0) {
3184 if (options.pushId !== undefined) {
3185 // Committing the push row is what makes its objects live
3186 // (LIVE_OBJECTS, DESIGN §22.14); nulling `staged_push` on
3187 // every row is a rewrite of the whole staged set and runs
3188 // after the response (`flipPush`), or inside this
3189 // transaction for callers that want it settled now.
3190 if (options.deferFlip !== true) {
3191 raw.exec(
3192 `UPDATE objects SET staged_push = NULL WHERE staged_push = ?`,
3193 options.pushId,
3194 );
3195 }
3196 raw.exec(`UPDATE pushes SET state = 'committed' WHERE push_id = ?`, options.pushId);
3197 }
3198 // Multi-row inserts under SQLite's 100-parameter cap: a
3199 // per-row exec was most of finalize once rows were
3200 // coordinates (DESIGN §22.14).
3201 for (let at = 0; at < options.graph.length; at += 25) {
3202 const part = options.graph.slice(at, at + 25);
3203 raw.exec(
3204 `INSERT OR IGNORE INTO commits (oid, tree, gen, commit_time) VALUES ${part.map(() => "(?, ?, ?, ?)").join(", ")}`,
3205 ...part.flatMap((commit) => [
3206 commit.oid,
3207 commit.tree,
3208 commit.gen,
3209 commit.commitTime,
3210 ]),
3211 );
3212 }
3213 const parentRows = options.graph.flatMap((commit) =>
3214 commit.parents.map((parent, ord) => [commit.oid, parent, ord]),
3215 );
3216 for (let at = 0; at < parentRows.length; at += 33) {
3217 const part = parentRows.slice(at, at + 33);
3218 raw.exec(
3219 `INSERT OR IGNORE INTO commit_parents (oid, parent, ord) VALUES ${part.map(() => "(?, ?, ?)").join(", ")}`,
3220 ...part.flat(),
3221 );
3222 }
3223 if (options.onCommitted !== undefined) {
3224 options.onCommitted(raw);
3225 }
3226 // First-branch-push default branch fixup: if the configured
3227 // default branch does not exist but a branch was just created,
3228 // repoint the default at it (prefer main/master).
3229 const configured = raw
3230 .exec<{ value: string }>(`SELECT value FROM config WHERE key = 'default_branch'`)
3231 .toArray()[0]?.value;
3232 if (configured !== undefined) {
3233 const exists = raw
3234 .exec<RefRow>(
3235 `SELECT name, oid FROM refs WHERE name = ?`,
3236 `refs/heads/${configured}`,
3237 )
3238 .toArray();
3239 if (exists.length === 0) {
3240 const branches = raw
3241 .exec<RefRow>(
3242 `SELECT name, oid FROM refs WHERE name LIKE 'refs/heads/%' ORDER BY name`,
3243 )
3244 .toArray()
3245 .map((row) => row.name.slice("refs/heads/".length));
3246 const pick =
3247 branches.find((b) => b === "main") ??
3248 branches.find((b) => b === "master") ??
3249 branches[0];
3250 if (pick !== undefined) {
3251 raw.exec(`UPDATE config SET value = ? WHERE key = 'default_branch'`, pick);
3252 }
3253 }
3254 }
3255 }
3256 return results;
3257 })
3258 .pipe(
3259 // Any committed ref movement changes what an anonymous reader
3260 // can see — republish the head snapshot (DESIGN.md §21).
3261 Effect.tap((results) =>
3262 results.some((result) => result.ok) ? writeHeadSnapshot : Effect.void,
3263 ),
3264 );
3265
3266 /** Computes gen/commit_time rows for staged commits (§3.6 step 6). */
3267 const computeGraphRows = (
3268 staged: ReadonlyArray<StagedCommitInfo>,
3269 ): Effect.Effect<
3270 Array<{
3271 oid: string;
3272 tree: string;
3273 gen: number;
3274 commitTime: number;
3275 parents: ReadonlyArray<string>;
3276 }>,
3277 StoreError
3278 > =>
3279 Effect.gen(function* () {
3280 const stagedByOid = new Map(staged.map((c) => [c.oid, c]));
3281 const externalParents = Array.from(
3282 new Set(staged.flatMap((c) => c.parents.filter((p) => !stagedByOid.has(p)))),
3283 );
3284 const liveGens = new Map<string, number>();
3285 if (externalParents.length > 0) {
3286 const rows = yield* sql.inChunks<{ oid: string; gen: number }>(
3287 (ph) => `SELECT oid, gen FROM commits WHERE oid IN (${ph})`,
3288 externalParents,
3289 );
3290 for (const row of rows) liveGens.set(row.oid, row.gen);
3291 }
3292 const gens = new Map<string, number>();
3293 const genOf = (oid: string, depth: number): number => {
3294 const known = gens.get(oid);
3295 if (known !== undefined) return known;
3296 const live = liveGens.get(oid);
3297 if (live !== undefined) return live;
3298 const commit = stagedByOid.get(oid);
3299 // Missing parent (shallow boundary) counts as gen 0.
3300 if (commit === undefined || depth > 100_000) return 0;
3301 const gen =
3302 commit.parents.length === 0
3303 ? 1
3304 : Math.max(...commit.parents.map((p) => genOf(p, depth + 1))) + 1;
3305 gens.set(oid, gen);
3306 return gen;
3307 };
3308 return staged.map((commit) => ({
3309 oid: commit.oid,
3310 tree: commit.tree,
3311 gen: genOf(commit.oid, 0),
3312 commitTime: commit.commitTime,
3313 parents: commit.parents,
3314 }));
3315 });
3316
3317 // ── pull requests ────────────────────────────────────────────────────
3318
3319 /** `main` or `refs/heads/main` → `refs/heads/main`; branches only. */
3320 const normalizeBranchRef = Effect.fn(function* (name: string) {
3321 const full = name.startsWith("refs/") ? name : `refs/heads/${name}`;
3322 if (
3323 !full.startsWith("refs/heads/") ||
3324 full.length <= "refs/heads/".length ||
3325 !REF_NAME_REGEX.test(full)
3326 ) {
3327 return yield* ValidationError.make({
3328 message: `not a branch name: ${name} (only refs/heads/* is legal)`,
3329 });
3330 }
3331 return full;
3332 });
3333
3334 const pullStateOf = (state: string): "open" | "closed" | "merged" =>
3335 state === "closed" ? "closed" : state === "merged" ? "merged" : "open";
3336
3337 const rowToPull = (row: PullRow): PullData => ({
3338 number: row.number,
3339 title: row.title,
3340 body: row.body,
3341 baseRef: row.base_ref,
3342 headRef: row.head_ref,
3343 state: pullStateOf(row.state),
3344 createdAt: row.created_at,
3345 updatedAt: row.updated_at,
3346 mergedAt: row.merged_at,
3347 mergeCommit: row.merge_commit,
3348 });
3349
3350 const loadPull = Effect.fn(function* (number: number) {
3351 const row = yield* sql.first<PullRow>(`SELECT * FROM pulls WHERE number = ?`, number);
3352 if (row === undefined) {
3353 return yield* PullNotFound.make({ number });
3354 }
3355 return row;
3356 });
3357
3358 /** Result of the bounded two-tip ancestor {@link paintWalk}. */
3359 interface PullCompare {
3360 /** Best common ancestor; `undefined` = none found (see saturated). */
3361 readonly mergeBase: string | undefined;
3362 /** Commits reachable from head but not base. */
3363 readonly aheadBy: number;
3364 /** Commits reachable from base but not head. */
3365 readonly behindBy: number;
3366 /** The walk hit its pop bound — counts/base are unreliable. */
3367 readonly saturated: boolean;
3368 }
3369
3370 /**
3371 * The same gen-ordered merge-base paint walk `compareCommits` runs,
3372 * reduced to what the PR surface needs: the best common ancestor plus
3373 * ahead/behind counts. `mergeBase === head` ⇔ head is reachable from
3374 * base (up-to-date); `mergeBase === base` ⇔ base is reachable from
3375 * head (fast-forwardable) — the gen ordering guarantees a node's
3376 * flags are complete when it pops. A tip missing from the commit
3377 * graph reports `saturated` (uncomputable), never an error.
3378 */
3379 const paintWalk = Effect.fn(function* (baseOid: string, headOid: string, maxPops: number) {
3380 const BASE = 1;
3381 const HEAD = 2;
3382 const BOTH = 3;
3383 const flags = new Map<string, number>();
3384 flags.set(baseOid, BASE);
3385 flags.set(headOid, (flags.get(headOid) ?? 0) | HEAD);
3386 const heap: Array<WalkHeapNode> = [];
3387 const rowOf = (oid: string) =>
3388 sql.first<{ gen: number; commit_time: number }>(
3389 `SELECT gen, commit_time FROM commits WHERE oid = ?`,
3390 oid,
3391 );
3392 for (const oid of new Set([baseOid, headOid])) {
3393 const row = yield* rowOf(oid);
3394 if (row === undefined) {
3395 return {
3396 mergeBase: undefined,
3397 aheadBy: 0,
3398 behindBy: 0,
3399 saturated: true,
3400 } satisfies PullCompare;
3401 }
3402 heapPush(heap, { oid, gen: row.gen, time: row.commit_time });
3403 }
3404 let mergeBase: string | undefined;
3405 let aheadBy = 0;
3406 let behindBy = 0;
3407 let pops = 0;
3408 while (heap.length > 0 && pops < maxPops) {
3409 // stop once nothing in the heap can still change the counts
3410 if (mergeBase !== undefined && heap.every((n) => (flags.get(n.oid)! & BOTH) === BOTH)) {
3411 break;
3412 }
3413 const node = heapPop(heap);
3414 pops++;
3415 const f = flags.get(node.oid)!;
3416 if ((f & BOTH) === BOTH) {
3417 if (mergeBase === undefined) mergeBase = node.oid;
3418 } else if (f === HEAD) {
3419 aheadBy++;
3420 } else {
3421 behindBy++;
3422 }
3423 const parents = yield* sql.all<{ parent: string }>(
3424 `SELECT parent FROM commit_parents WHERE oid = ? ORDER BY ord`,
3425 node.oid,
3426 );
3427 for (const { parent } of parents) {
3428 const prev = flags.get(parent) ?? 0;
3429 const next = prev | f;
3430 if (next === prev) continue;
3431 flags.set(parent, next);
3432 if (prev === 0) {
3433 const row = yield* rowOf(parent);
3434 if (row === undefined) continue; // shallow boundary
3435 heapPush(heap, {
3436 oid: parent,
3437 gen: row.gen,
3438 time: row.commit_time,
3439 });
3440 }
3441 }
3442 }
3443 return {
3444 mergeBase,
3445 aheadBy,
3446 behindBy,
3447 saturated: pops >= maxPops && heap.length > 0,
3448 } satisfies PullCompare;
3449 });
3450
3451 /** The root tree of a commit, via the commit-graph table. */
3452 const treeOfCommit = Effect.fn(function* (oid: string) {
3453 const row = yield* sql.first<{ tree: string }>(
3454 `SELECT tree FROM commits WHERE oid = ?`,
3455 oid,
3456 );
3457 if (row === undefined) {
3458 return yield* StoreError.make({
3459 reason: `commit ${oid} missing from the commit graph`,
3460 });
3461 }
3462 return row.tree;
3463 });
3464
3465 // ── alarm scheduling ─────────────────────────────────────────────────
3466 const armAlarmAt = (time: number) =>
3467 Effect.gen(function* () {
3468 const existing = yield* state.storage.getAlarm();
3469 if (existing === null || existing > time) {
3470 yield* state.storage.setAlarm(time);
3471 }
3472 });
3473
3474 const upsertJob = (kind: string, detail: string | null) =>
3475 sql.run(
3476 `INSERT INTO jobs (kind, state, detail, updated_at) VALUES (?, 'running', ?, ?)
3477 ON CONFLICT (kind) DO UPDATE SET state = 'running', detail = excluded.detail, updated_at = excluded.updated_at`,
3478 kind,
3479 detail,
3480 Date.now(),
3481 );
3482
3483 // ── bootstrap (create / fork / import) ───────────────────────────────
3484 const seedConfig = Effect.fn(function* (input: InitRepoInput, status: RepoStatus) {
3485 const createdAt = Date.now();
3486 yield* setConfig("repo_id", input.repoId);
3487 yield* setConfig("owner", input.owner.toLowerCase());
3488 yield* setConfig("name", input.name.toLowerCase());
3489 yield* setConfig("default_branch", input.defaultBranch);
3490 if (input.description !== null) {
3491 yield* setConfig("description", input.description);
3492 }
3493 yield* setConfig("read_only", input.readOnly ? "1" : "0");
3494 yield* setConfig("public", input.public ? "1" : "0");
3495 if (input.forkOf !== null) {
3496 yield* setConfig("fork_of", input.forkOf);
3497 }
3498 yield* setConfig("status", status);
3499 yield* readMeta.pipe(
3500 Effect.flatMap((meta) => (meta === undefined ? Effect.void : syncSummary(meta))),
3501 Effect.ignore,
3502 );
3503 const existing = yield* getConfig("created_at");
3504 if (existing === undefined) {
3505 yield* setConfig("created_at", String(createdAt));
3506 }
3507 const meta = yield* readMeta;
3508 return meta!;
3509 });
3510
3511 const bootstrap = Effect.fn(function* (input: InitRepoInput, status: RepoStatus) {
3512 const meta = yield* seedConfig(input, status);
3513 return { meta } satisfies InitRepoResult;
3514 });
3515
3516 // ── wire protocol drivers ────────────────────────────────────────────
3517
3518 /** Parses `/:owner/:repo[.git]/<endpoint>` out of the request URL. */
3519 const parseWirePath = (
3520 url: string,
3521 ):
3522 | {
3523 readonly owner: string;
3524 readonly repo: string;
3525 readonly endpoint: string;
3526 readonly service: string | undefined;
3527 }
3528 | undefined => {
3529 const parsed = new URL(url, "http://repo");
3530 const segments = parsed.pathname.split("/").filter(Boolean);
3531 if (segments.length < 3) return undefined;
3532 const owner = decodeURIComponent(segments[0]!).toLowerCase();
3533 let repo = decodeURIComponent(segments[1]!).toLowerCase();
3534 if (repo.endsWith(".git")) repo = repo.slice(0, -4);
3535 const endpoint = segments.slice(2).join("/");
3536 return {
3537 owner,
3538 repo,
3539 endpoint,
3540 service: parsed.searchParams.get("service") ?? undefined,
3541 };
3542 };
3543
3544 const wire503 = HttpServerResponse.empty({
3545 status: 503,
3546 headers: { "retry-after": "10" },
3547 });
3548
3549 /** GET info/refs — the v0 advertisement. */
3550 const handleInfoRefs = (
3551 request: HttpServerRequest.HttpServerRequest,
3552 service: string | undefined,
3553 meta: RepoMetaData,
3554 ) =>
3555 Effect.gen(function* () {
3556 if (service !== "git-upload-pack" && service !== "git-receive-pack") {
3557 return HttpServerResponse.text("smart HTTP only (dumb protocol not supported)", {
3558 status: 400,
3559 });
3560 }
3561 const refs = yield* listAllRefs(meta.repoId);
3562 const body = buildAdvertisement({
3563 service,
3564 defaultBranch: meta.defaultBranch,
3565 refs,
3566 });
3567 return HttpServerResponse.uint8Array(body, {
3568 contentType: `application/x-${service}-advertisement`,
3569 headers: noCache,
3570 });
3571 });
3572
3573 /** POST git-upload-pack — negotiation + pack (DESIGN.md §2.3). */
3574 const handleUploadPack = (request: HttpServerRequest.HttpServerRequest, meta: RepoMetaData) =>
3575 Effect.gen(function* () {
3576 const rawBody = new Uint8Array(yield* request.arrayBuffer);
3577 const body = yield* gunzipIfNeeded(rawBody, request.headers["content-encoding"]);
3578 const resultType = "application/x-git-upload-pack-result";
3579 const errResponse = (message: string) =>
3580 HttpServerResponse.uint8Array(errPkt(message), {
3581 contentType: resultType,
3582 headers: noCache,
3583 });
3584
3585 const parsed = yield* decodePktLines(body).pipe(
3586 Effect.flatMap(parseUploadPackRequest),
3587 Effect.asSome,
3588 Effect.catchTags({
3589 PktLineError: (e) =>
3590 Effect.succeed(Option.none<UploadPackRequest>()).pipe(
3591 Effect.tap(() => Effect.logWarning(`upload-pack: ${e.reason}`)),
3592 ),
3593 WireProtocolError: (e) =>
3594 Effect.succeed(Option.none<UploadPackRequest>()).pipe(
3595 Effect.tap(() => Effect.logWarning(`upload-pack: ${e.reason}`)),
3596 ),
3597 }),
3598 );
3599 if (Option.isNone(parsed)) {
3600 return errResponse("malformed upload-pack request");
3601 }
3602 const req = parsed.value;
3603 const objects = storeFor(meta.repoId);
3604 const sideband = req.capabilities.has("side-band-64k");
3605 const noDone = req.capabilities.has("no-done");
3606
3607 const commons = yield* objects.filterExisting(req.haves);
3608
3609 // Negotiation round without done and nothing in common: answer
3610 // NAK and wait for the next round (or done). EXCEPT when the
3611 // request carries `deepen` — the shallow/unshallow section must
3612 // precede the NAK ("fatal: expected shallow/unshallow, got NAK"
3613 // otherwise), so depth-carrying rounds fall through to the
3614 // closure computation below.
3615 if (!req.done && commons.length === 0 && req.depth === undefined) {
3616 return HttpServerResponse.uint8Array(pktText("NAK"), {
3617 contentType: resultType,
3618 headers: noCache,
3619 });
3620 }
3621
3622 // Bundle fast path (DESIGN.md §12.2): a full clone whose wants are
3623 // all covered by the current bundle is served as bytes straight
3624 // from R2 — no closure walk, no per-object reads. `bundleCovers`
3625 // refuses anything with haves/depth/shallow state.
3626 const bundle = yield* readBundle;
3627 if (
3628 req.done &&
3629 bundle !== undefined &&
3630 bundleCovers(bundle, {
3631 wants: req.wants,
3632 haves: req.haves,
3633 depth: req.depth,
3634 clientShallow: req.clientShallow,
3635 })
3636 ) {
3637 // The DO does NOT stream the pack: it hands the Worker a
3638 // *marker* naming the R2 object, and the Worker streams those
3639 // bytes to the client itself (DESIGN.md §11, serving plane).
3640 // Auth and planning still happen here — where tokens and refs
3641 // live — but no pack byte transits this object, which is the
3642 // whole point of splitting the planes.
3643 const found = yield* blobs
3644 .head(bundle.key)
3645 .pipe(
3646 Effect.mapError((error) => StoreError.make({ reason: `bundle: ${error.reason}` })),
3647 );
3648 if (found !== null) {
3649 return HttpServerResponse.empty({
3650 status: 200,
3651 headers: {
3652 ...noCache,
3653 "content-type": resultType,
3654 [BUNDLE_KEY_HEADER]: bundle.key,
3655 [BUNDLE_HASH_HEADER]: bundle.refsHash,
3656 [BUNDLE_COUNT_HEADER]: String(bundle.objectCount),
3657 [BUNDLE_SIDEBAND_HEADER]: sideband ? "1" : "0",
3658 },
3659 });
3660 }
3661 }
3662
3663 // A depth-limited import leaves the repo itself shallow; those
3664 // boundary commits must bound every walk and surface as
3665 // `shallow` lines so clones inherit the shallowness (fsck-clean).
3666 const shallowRoots = JSON.parse(
3667 (yield* getConfig("shallow_roots")) ?? "[]",
3668 ) as ReadonlyArray<string>;
3669 const closureResult = yield* Effect.result(
3670 computeClosure({ sql, objects })({
3671 wants: req.wants,
3672 haves: commons,
3673 depth: req.depth,
3674 clientShallow: req.clientShallow,
3675 repoShallow: shallowRoots,
3676 }),
3677 );
3678 if (Result.isFailure(closureResult)) {
3679 const error = closureResult.failure;
3680 return errResponse(
3681 error._tag === "ManifestTooLarge"
3682 ? `repository too large to serve in one pack (${error.count} > ${error.cap})`
3683 : error.reason,
3684 );
3685 }
3686 const closure = closureResult.success;
3687
3688 // v0 clients only parse a shallow/unshallow section when THEY
3689 // brought shallowness into the exchange (`deepen` or `shallow`
3690 // lines). If this repo's own boundary truncated the walk for a
3691 // client that did neither, an unsolicited section would derail
3692 // its parser — refuse cleanly instead.
3693 const clientShallowAware = req.depth !== undefined || req.clientShallow.length > 0;
3694 if (closure.shallow.length > 0 && !clientShallowAware) {
3695 return errResponse(
3696 "repository has shallow history (imported with depth); clone with --depth",
3697 );
3698 }
3699 const head: Array<Uint8Array> = [];
3700 if (clientShallowAware) {
3701 for (const oid of closure.shallow) {
3702 head.push(pktText(`shallow ${oid}`));
3703 }
3704 for (const oid of closure.unshallow) {
3705 head.push(pktText(`unshallow ${oid}`));
3706 }
3707 head.push(flushPkt);
3708 }
3709
3710 // Depth-carrying negotiation round with nothing in common and no
3711 // done yet: the response is the shallow section ALONE (lines +
3712 // flush) — no NAK, no pack. The stateless client buffers this
3713 // response, sends `done`, and expects the NEXT response to begin
3714 // with the shallow section again; a trailing NAK here would sit
3715 // in its buffer and derail that parse ("expected shallow list").
3716 if (!req.done && commons.length === 0) {
3717 return HttpServerResponse.uint8Array(concatBytes(head), {
3718 contentType: resultType,
3719 headers: noCache,
3720 });
3721 }
3722 const lastCommon = commons[commons.length - 1];
3723 if (req.done) {
3724 head.push(lastCommon === undefined ? pktText("NAK") : pktText(`ACK ${lastCommon}`));
3725 } else {
3726 // !done implies commons.length > 0 (the NAK-only branch above).
3727 const ready = lastCommon ?? ZERO_OID;
3728 for (const common of commons) {
3729 head.push(pktText(`ACK ${common} common`));
3730 }
3731 head.push(pktText(`ACK ${ready} ready`));
3732 if (!noDone) {
3733 // Client will re-POST with done; no pack this round.
3734 return HttpServerResponse.uint8Array(concatBytes(head), {
3735 contentType: resultType,
3736 headers: noCache,
3737 });
3738 }
3739 head.push(pktText(`ACK ${ready}`));
3740 }
3741
3742 // The pack goes out through the native pump (Sideband.ts
3743 // `pumpPackBody`, DESIGN §22.3): the emitter's batched chunks are
3744 // large, so the Effect→ReadableStream bridge is a handful of
3745 // pulls, and framing/flush happen at platform speed.
3746 const source = yield* packStream(closure.entries, objects).pipe(
3747 Stream.toReadableStreamEffect(),
3748 );
3749 const prefix = sideband
3750 ? concatBytes([
3751 concatBytes(head),
3752 progressMessage(`Enumerating objects: ${closure.entries.length}, done.`),
3753 ])
3754 : concatBytes(head);
3755 return HttpServerResponse.raw(pumpPackBody({ prefix, source, sideband }), {
3756 contentType: resultType,
3757 headers: noCache,
3758 });
3759 });
3760
3761 // ── alarm jobs ───────────────────────────────────────────────────────
3762 /** Nulls a committed push's `staged_push` column and drops its row. */
3763 const flipPush = (pushId: string) =>
3764 Effect.gen(function* () {
3765 yield* sql.run(`UPDATE objects SET staged_push = NULL WHERE staged_push = ?`, pushId);
3766 yield* sql.run(`DELETE FROM pushes WHERE push_id = ? AND state = 'committed'`, pushId);
3767 });
3768
3769 const runGcJob = Effect.gen(function* () {
3770 // Committed pushes whose deferred flip never ran (DESIGN §22.14).
3771 yield* sql.run(
3772 `UPDATE objects SET staged_push = NULL WHERE staged_push IN
3773 (SELECT push_id FROM pushes WHERE state = 'committed')`,
3774 );
3775 yield* sql.run(`DELETE FROM pushes WHERE state = 'committed'`);
3776 const cutoff = Date.now() - STAGING_TTL_MS;
3777 yield* sql.run(
3778 `DELETE FROM objects WHERE staged_push IN
3779 (SELECT push_id FROM pushes WHERE state = 'staging' AND started_at < ?)`,
3780 cutoff,
3781 );
3782 yield* sql.run(`DELETE FROM pushes WHERE state = 'staging' AND started_at < ?`, cutoff);
3783 yield* sql.run(`DELETE FROM jobs WHERE kind = 'gc'`);
3784 });
3785
3786 /**
3787 * Compaction (DESIGN.md §12.1): moves loose object bytes out of DO
3788 * SQLite into an immutable R2 pack, bounded per run and re-armed
3789 * while more remain.
3790 */
3791 const runCompactAlarm = Effect.gen(function* () {
3792 const meta = yield* requireMeta;
3793 // Grace-delete packs a PREVIOUS merge unreferenced: only after
3794 // a minute, so any read planned before that row flip has long
3795 // finished its ranged reads (DESIGN.md §21).
3796 const pendingRaw = yield* getConfig("packs_pending_delete");
3797 const pending =
3798 pendingRaw === undefined || pendingRaw === ""
3799 ? undefined
3800 : (JSON.parse(pendingRaw) as { keys: Array<string>; at: number });
3801 if (pending !== undefined && Date.now() - pending.at > 60_000) {
3802 yield* blobs.delete(pending.keys).pipe(Effect.ignore);
3803 yield* setConfig("packs_pending_delete", "");
3804 }
3805 const outcome = yield* runCompactJob({
3806 repoId: meta.repoId,
3807 sql,
3808 blobs,
3809 });
3810 // Restore the geometric invariant over the repo's packs, so pack
3811 // count — and with it read fan-out — stays O(log bytes).
3812 const merge = yield* runGeometricMergeJob({
3813 repoId: meta.repoId,
3814 sql,
3815 blobs,
3816 });
3817 if (merge.pendingDelete.length > 0) {
3818 const carried =
3819 pending !== undefined && !(Date.now() - pending.at > 60_000) ? pending.keys : [];
3820 yield* setConfig(
3821 "packs_pending_delete",
3822 JSON.stringify({
3823 keys: [...carried, ...merge.pendingDelete],
3824 at: Date.now(),
3825 }),
3826 );
3827 }
3828 if (outcome.more || merge.more) {
3829 yield* armAlarmAt(Date.now() + 1_000);
3830 return;
3831 }
3832 yield* sql.run(`DELETE FROM jobs WHERE kind = 'compact'`);
3833 });
3834
3835 /** The repo's current clone bundle, if one has been cut. */
3836 /**
3837 * Pushes the denormalised display fields to the Registry so listing
3838 * repos never has to wake this DO (DESIGN.md §15 bottleneck 7).
3839 * Best-effort: the DO remains the source of truth, and a failed
3840 * refresh only makes a listing momentarily stale.
3841 */
3842 const syncSummary = Effect.fn(function* (meta: RepoMetaData) {
3843 yield* registry
3844 .updateSummary(meta.repoId, {
3845 defaultBranch: meta.defaultBranch,
3846 readOnly: meta.readOnly,
3847 status: meta.status,
3848 public: meta.public,
3849 })
3850 .pipe(Effect.ignore);
3851 });
3852
3853 /** Re-reads metadata and refreshes the Registry summary. */
3854 const refreshSummary: Effect.Effect<void, never, RuntimeContext> = readMeta.pipe(
3855 Effect.flatMap((meta) => (meta === undefined ? Effect.void : syncSummary(meta))),
3856 Effect.ignore,
3857 );
3858
3859 const readBundle: Effect.Effect<BundleInfo | undefined, StoreError> = getConfig(
3860 "bundle",
3861 ).pipe(
3862 Effect.map((value) =>
3863 value === undefined ? undefined : (JSON.parse(value) as BundleInfo),
3864 ),
3865 );
3866
3867 /**
3868 * Republishes the repo's head snapshot (DESIGN.md §21): a tiny
3869 * JSON object in the BlobStore from which the Worker serves
3870 * anonymous public reads — the upload-pack advertisement and
3871 * bundle-covered clones — without waking this DO. Called after
3872 * every commit that changes what an anonymous reader can see.
3873 *
3874 * Best-effort by design: it runs AFTER the SQLite commit (which
3875 * stays authoritative), so a failed write only leaves a stale
3876 * snapshot that the next mutation — or the post-push bundle
3877 * alarm, seconds later — repairs.
3878 */
3879 const writeHeadSnapshot: Effect.Effect<void, never, RuntimeContext> = Effect.gen(
3880 function* () {
3881 const meta = yield* requireMeta;
3882 const refs = yield* listAllRefs(meta.repoId);
3883 const bundle = yield* readBundle;
3884 const snapshot: HeadSnapshot = {
3885 v: 1,
3886 repoId: meta.repoId,
3887 owner: meta.owner,
3888 name: meta.name,
3889 public: meta.public,
3890 readOnly: meta.readOnly,
3891 defaultBranch: meta.defaultBranch,
3892 refs,
3893 bundle,
3894 };
3895 yield* blobs.put(headKey(meta.repoId), utf8Encode(encodeHeadSnapshot(snapshot)));
3896 },
3897 ).pipe(
3898 Effect.catch((error) => Effect.logWarning(`head snapshot write failed: ${String(error)}`)),
3899 Effect.asVoid,
3900 );
3901
3902 /**
3903 * Cuts a clone bundle for the current refs (DESIGN.md §12.2) so future
3904 * clones are served as bytes from R2 instead of a fresh closure walk.
3905 */
3906 const runBundleAlarm = Effect.gen(function* () {
3907 const meta = yield* requireMeta;
3908 const objects = storeFor(meta.repoId);
3909 const refRows = yield* sql.all<RefRow>(`SELECT name, oid FROM refs ORDER BY name`);
3910 const shallowRoots = JSON.parse(
3911 (yield* getConfig("shallow_roots")) ?? "[]",
3912 ) as ReadonlyArray<string>;
3913 const closure = yield* computeClosure({ sql, objects })({
3914 wants: refRows.map((ref) => ref.oid),
3915 haves: [],
3916 clientShallow: [],
3917 repoShallow: shallowRoots,
3918 }).pipe(
3919 Effect.catchTag("ManifestTooLarge", (error) =>
3920 Effect.fail(StoreError.make({ reason: `bundle: ${error._tag}` })),
3921 ),
3922 );
3923 const info = yield* runBundleJob({
3924 repoId: meta.repoId,
3925 refs: refRows.map((ref) => ({ name: ref.name, oid: ref.oid })),
3926 entries: closure.entries,
3927 packStream: (entries) => packStream(entries, objects),
3928 blobs,
3929 });
3930 if (info !== undefined) {
3931 yield* setConfig("bundle", JSON.stringify(info));
3932 // The snapshot now names a bundle covering the current refs —
3933 // full anonymous clones go DO-less from here (DESIGN.md §21).
3934 yield* writeHeadSnapshot;
3935 }
3936 yield* sql.run(`DELETE FROM jobs WHERE kind = 'bundle'`);
3937 });
3938
3939 const runImportAlarm = (job: JobRow) =>
3940 Effect.gen(function* () {
3941 const meta = yield* requireMeta;
3942 const detail = JSON.parse(job.detail ?? "{}") as {
3943 readonly source: ImportSource;
3944 readonly attempts?: number;
3945 };
3946 const attempts = detail.attempts ?? 0;
3947 const outcome = yield* Effect.result(
3948 runImport({ source: detail.source }).pipe(Effect.provide(FetchHttpClient.layer)),
3949 );
3950 if (Result.isFailure(outcome)) {
3951 if (attempts + 1 >= 3) {
3952 yield* sql.run(
3953 `UPDATE jobs SET state = 'failed', detail = ?, updated_at = ? WHERE kind = 'import'`,
3954 JSON.stringify({ ...detail, error: outcome.failure.reason }),
3955 Date.now(),
3956 );
3957 // Give the empty repo back rather than wedging in 'importing'.
3958 yield* setConfig("status", "ready");
3959 yield* refreshSummary;
3960 yield* refreshSummary;
3961 return;
3962 }
3963 yield* sql.run(
3964 `UPDATE jobs SET detail = ?, updated_at = ? WHERE kind = 'import'`,
3965 JSON.stringify({ ...detail, attempts: attempts + 1 }),
3966 Date.now(),
3967 );
3968 yield* armAlarmAt(Date.now() + 15_000);
3969 return;
3970 }
3971 const result = outcome.success;
3972 const objects = storeFor(meta.repoId);
3973 const pushId = yield* ulid();
3974 // Imports use the same staging lifecycle as pushes: finalize
3975 // requires an active row, and GC needs it to reap abandoned data.
3976 yield* sql.run(
3977 `INSERT INTO pushes (push_id, started_at, state) VALUES (?, ?, 'staging')`,
3978 pushId,
3979 Date.now(),
3980 );
3981 yield* upsertJob("gc", null);
3982 yield* armAlarmAt(Date.now() + STAGING_TTL_MS);
3983 let graph: Array<{
3984 oid: string;
3985 tree: string;
3986 gen: number;
3987 commitTime: number;
3988 parents: ReadonlyArray<string>;
3989 }> = [];
3990 if (result.pack !== undefined) {
3991 const ingest = yield* ingestPack(result.pack, {
3992 store: objects,
3993 pushId,
3994 allowMissingParents: true,
3995 }).pipe(
3996 Effect.mapError((error) =>
3997 StoreError.make({
3998 reason:
3999 error._tag === "PackIngestError"
4000 ? `import ingest: ${error.reason}`
4001 : error.reason,
4002 }),
4003 ),
4004 );
4005 // Connectivity: hard edges only — shallow imports may lack
4006 // parent commits (they become walk boundaries).
4007 const missing = yield* objects.missingObjects([...ingest.referenced], pushId);
4008 if (missing.length > 0) {
4009 return yield* StoreError.make({
4010 reason: `import produced ${missing.length} dangling objects`,
4011 });
4012 }
4013 graph = yield* computeGraphRows(ingest.commits);
4014 // A depth-limited import leaves parents un-ingested: record the
4015 // boundary commits so upload-pack serves this repo as shallow
4016 // (walk bound + `shallow` lines — see handleUploadPack).
4017 const parentOids = Array.from(
4018 new Set(ingest.commits.flatMap((commit) => commit.parents)),
4019 );
4020 const missingParents = new Set(yield* objects.missingObjects(parentOids, pushId));
4021 if (missingParents.size > 0) {
4022 const shallowRoots = ingest.commits
4023 .filter((commit) => commit.parents.some((parent) => missingParents.has(parent)))
4024 .map((commit) => commit.oid);
4025 yield* setConfig("shallow_roots", JSON.stringify(shallowRoots));
4026 }
4027 }
4028 const commands: Array<RefCommand> = result.refs.map((ref) => ({
4029 oldOid: ZERO_OID,
4030 newOid: ref.oid,
4031 ref: ref.name,
4032 }));
4033 const results = yield* finalizeRefTxn({
4034 commands,
4035 atomic: false,
4036 unconditional: true,
4037 pushId,
4038 graph,
4039 });
4040 const rejected = results.find((ref) => !ref.ok);
4041 if (rejected !== undefined) {
4042 return yield* StoreError.make({
4043 reason: `import ref ${rejected.ref}: ${rejected.reason}`,
4044 });
4045 }
4046 yield* flipPush(pushId);
4047 if (result.defaultBranch !== null) {
4048 yield* setConfig("default_branch", result.defaultBranch);
4049 }
4050 yield* setConfig("status", "ready");
4051 yield* refreshSummary;
4052 yield* writeHeadSnapshot;
4053 yield* sql.run(`DELETE FROM jobs WHERE kind = 'import'`);
4054 });
4055
4056 const runForkAlarm = (job: JobRow) =>
4057 Effect.gen(function* () {
4058 const detail = JSON.parse(job.detail ?? "{}") as {
4059 readonly parentRepoId: string;
4060 readonly attempts?: number;
4061 };
4062 const parentStub = selfNamespace.getByName(detail.parentRepoId) as unknown as RepoStub;
4063 const applied = yield* Effect.result(
4064 runForkJob({
4065 sql,
4066 snapshot: parentStub.snapshotRows(),
4067 }),
4068 );
4069 if (Result.isFailure(applied)) {
4070 const attempts = (detail.attempts ?? 0) + 1;
4071 if (attempts >= 3) {
4072 yield* sql.run(
4073 `UPDATE jobs SET state = 'failed', detail = ?, updated_at = ? WHERE kind = 'fork'`,
4074 JSON.stringify({ ...detail, error: applied.failure.reason }),
4075 Date.now(),
4076 );
4077 yield* setConfig("status", "ready");
4078 yield* refreshSummary;
4079 yield* refreshSummary;
4080 return;
4081 }
4082 yield* sql.run(
4083 `UPDATE jobs SET detail = ?, updated_at = ? WHERE kind = 'fork'`,
4084 JSON.stringify({ ...detail, attempts }),
4085 Date.now(),
4086 );
4087 yield* armAlarmAt(Date.now() + 15_000);
4088 return;
4089 }
4090 yield* setConfig("status", "ready");
4091 yield* refreshSummary;
4092 yield* writeHeadSnapshot;
4093 yield* sql.run(`DELETE FROM jobs WHERE kind = 'fork'`);
4094 });
4095
4096 /** Returns true when storage was wiped (stop touching SQLite). */
4097 const runPurgeAlarm = Effect.gen(function* () {
4098 const meta = yield* readMeta;
4099 if (meta === undefined) return true;
4100 const registryStub = registry;
4101 const outcome = yield* runPurgeJob({
4102 repoId: meta.repoId,
4103 blobs,
4104 // RuntimeContext is genuinely satisfied here (we run inside the
4105 // DO); discharge the coloring so the purge job's deps stay R=never.
4106 forkCount: registryStub.bumpForkCount(meta.repoId, 0).pipe(
4107 Effect.mapError((error) => StoreError.make({ reason: String(error) })),
4108 Effect.provide(RuntimeContext.phantom),
4109 ),
4110 deleteAllStorage: state.storage.deleteAll().pipe(
4111 Effect.mapError((error) => StoreError.make({ reason: String(error) })),
4112 Effect.provide(RuntimeContext.phantom),
4113 ),
4114 removeRegistryRow: registryStub.removeRow(meta.repoId).pipe(
4115 Effect.mapError((error) => StoreError.make({ reason: String(error) })),
4116 Effect.provide(RuntimeContext.phantom),
4117 ),
4118 });
4119 if (outcome._tag === "continue") {
4120 yield* armAlarmAt(Date.now() + (outcome.forkPinned ? 60_000 : 5_000));
4121 return false;
4122 }
4123 yield* state.storage.deleteAlarm();
4124 return true;
4125 });
4126
4127 // ── the shape ────────────────────────────────────────────────────────
4128 const shape: GitRepoShape = {
4129 initRepo: (input) => bootstrap(input, "ready").pipe(Effect.tap(() => writeHeadSnapshot)),
4130
4131 startImport: Effect.fn(function* (input: StartImportInput) {
4132 const result = yield* bootstrap(input, "importing");
4133 yield* upsertJob("import", JSON.stringify({ source: input.source }));
4134 yield* armAlarmAt(Date.now());
4135 return result;
4136 }),
4137
4138 startFork: Effect.fn(function* (input: StartForkInput) {
4139 const result = yield* bootstrap(input, "forking");
4140 yield* upsertJob("fork", JSON.stringify({ parentRepoId: input.parentRepoId }));
4141 yield* armAlarmAt(Date.now());
4142 return result;
4143 }),
4144
4145 startCompact: Effect.fn(function* () {
4146 yield* requireMeta;
4147 yield* upsertJob("compact", null);
4148 yield* armAlarmAt(Date.now());
4149 }),
4150
4151 startPurge: Effect.fn(function* () {
4152 const meta = yield* requireMeta;
4153 yield* setConfig("status", "deleting");
4154 // Kill the DO-less fast path NOW — the prefix drain gets the
4155 // rest, but anonymous reads must stop serving immediately.
4156 yield* blobs.delete(headKey(meta.repoId)).pipe(Effect.ignore);
4157 yield* refreshSummary;
4158 yield* upsertJob("purge", null);
4159 yield* armAlarmAt(Date.now());
4160 }),
4161
4162 getRepoMeta: Effect.fn(function* () {
4163 const meta = yield* requireMetaStats;
4164 return meta;
4165 }),
4166
4167 readMeta: Effect.fn(function* () {
4168 return yield* requireMetaStats;
4169 }),
4170
4171 updateRepoMeta: Effect.fn(function* (patch: RepoMetaPatch) {
4172 const meta = yield* requireMeta;
4173 if (patch.defaultBranch !== undefined) {
4174 const ref = yield* sql.first<RefRow>(
4175 `SELECT name, oid FROM refs WHERE name = ?`,
4176 `refs/heads/${patch.defaultBranch}`,
4177 );
4178 if (ref === undefined) {
4179 return yield* RefNotFound.make({
4180 ref: `refs/heads/${patch.defaultBranch}`,
4181 });
4182 }
4183 yield* setConfig("default_branch", patch.defaultBranch);
4184 }
4185 if (patch.description !== undefined) {
4186 if (patch.description === null) {
4187 yield* sql.run(`DELETE FROM config WHERE key = 'description'`);
4188 } else {
4189 yield* setConfig("description", patch.description);
4190 }
4191 }
4192 if (patch.readOnly !== undefined) {
4193 yield* setConfig("read_only", patch.readOnly ? "1" : "0");
4194 }
4195 if (patch.public !== undefined) {
4196 yield* setConfig("public", patch.public ? "1" : "0");
4197 }
4198 const updated = yield* readMetaStats;
4199 const result = updated ?? meta;
4200 // Keep the Registry's denormalised copy in step (listing reads it).
4201 yield* syncSummary(result);
4202 yield* writeHeadSnapshot;
4203 return result;
4204 }),
4205
4206 listRefs: Effect.fn(function* (prefix?: string) {
4207 const meta = yield* requireMeta;
4208 const refs = yield* listAllRefs(meta.repoId, prefix);
4209 const headRef = `refs/heads/${meta.defaultBranch}`;
4210 const headExists = yield* sql.first<RefRow>(
4211 `SELECT name, oid FROM refs WHERE name = ?`,
4212 headRef,
4213 );
4214 return {
4215 head: headExists === undefined ? null : headRef,
4216 refs,
4217 } satisfies RefsPage;
4218 }),
4219
4220 getRef: Effect.fn(function* (name: string) {
4221 const meta = yield* requireMeta;
4222 const row = yield* sql.first<RefRow>(`SELECT name, oid FROM refs WHERE name = ?`, name);
4223 if (row === undefined) {
4224 return yield* RefNotFound.make({ ref: name });
4225 }
4226 const peeled = name.startsWith("refs/tags/")
4227 ? yield* peelOid(meta.repoId, row.oid)
4228 : undefined;
4229 return peeled === undefined
4230 ? { name: row.name, oid: row.oid }
4231 : ({ name: row.name, oid: row.oid, peeled } satisfies RefData);
4232 }),
4233
4234 updateRef: Effect.fn(function* (input: UpdateRefInput) {
4235 const meta = yield* requireMeta;
4236 if (meta.readOnly) {
4237 return yield* ReadOnlyRepo.make({});
4238 }
4239 const objects = storeFor(meta.repoId);
4240 const exists = yield* objects.has(input.newOid);
4241 if (!exists) {
4242 return yield* ObjectNotFound.make({ oid: input.newOid });
4243 }
4244 yield* sql.transactionSync<void, RefConflict>((raw, rollback) => {
4245 const rows = raw
4246 .exec<RefRow>(`SELECT name, oid FROM refs WHERE name = ?`, input.name)
4247 .toArray();
4248 const current = rows.length > 0 ? rows[0]!.oid : null;
4249 if (input.expectedOid !== undefined) {
4250 const expected = input.expectedOid; // string | null
4251 if (current !== expected) {
4252 rollback(
4253 RefConflict.make({
4254 ref: input.name,
4255 currentOid: current === null ? null : asOid(current),
4256 }),
4257 );
4258 }
4259 }
4260 raw.exec(
4261 `INSERT INTO refs (name, oid) VALUES (?, ?)
4262 ON CONFLICT (name) DO UPDATE SET oid = excluded.oid`,
4263 input.name,
4264 input.newOid,
4265 );
4266 });
4267 yield* writeHeadSnapshot;
4268 return { name: input.name, oid: input.newOid } satisfies RefData;
4269 }),
4270
4271 removeRef: Effect.fn(function* (input: RemoveRefInput) {
4272 const meta = yield* requireMeta;
4273 if (meta.readOnly) {
4274 return yield* ReadOnlyRepo.make({});
4275 }
4276 yield* sql.transactionSync<void, RefConflict | RefNotFound>((raw, rollback) => {
4277 const rows = raw
4278 .exec<RefRow>(`SELECT name, oid FROM refs WHERE name = ?`, input.name)
4279 .toArray();
4280 if (rows.length === 0) {
4281 rollback(RefNotFound.make({ ref: input.name }));
4282 }
4283 const current = rows[0]!.oid;
4284 if (input.expectedOid !== undefined && current !== input.expectedOid) {
4285 rollback(
4286 RefConflict.make({
4287 ref: input.name,
4288 currentOid: asOid(current),
4289 }),
4290 );
4291 }
4292 raw.exec(`DELETE FROM refs WHERE name = ?`, input.name);
4293 });
4294 yield* writeHeadSnapshot;
4295 }),
4296
4297 beginPush: Effect.fn(function* (_input: BeginPushInput) {
4298 const meta = yield* requireMeta;
4299 if (meta.readOnly) {
4300 return {
4301 _tag: "denied",
4302 reason: "repository is read-only",
4303 } as const;
4304 }
4305 const pushId = yield* ulid();
4306 yield* sql.run(
4307 `INSERT INTO pushes (push_id, started_at, state) VALUES (?, ?, 'staging')`,
4308 pushId,
4309 Date.now(),
4310 );
4311 return { _tag: "ok", pushId } as const;
4312 }),
4313
4314 stagePush: Effect.fn(function* (pushId: string, batch: Uint8Array) {
4315 const meta = yield* requireMeta;
4316 yield* storeFor(meta.repoId).insertStagedBatch(pushId, decodeStagedBatch(batch));
4317 }),
4318
4319 abortPush: Effect.fn(function* (pushId: string, packIds: ReadonlyArray<string> = []) {
4320 yield* requireMeta;
4321 return yield* sql.transactionSync((raw) => {
4322 const push = raw
4323 .exec<{ state: string }>(`SELECT state FROM pushes WHERE push_id = ?`, pushId)
4324 .toArray()[0];
4325 if (push?.state === "committed") return false;
4326 raw.exec(`DELETE FROM objects WHERE staged_push = ?`, pushId);
4327 raw.exec(`DELETE FROM pushes WHERE push_id = ? AND state = 'staging'`, pushId);
4328 // Another concurrent push may have adopted staged rows. Retain any
4329 // shared pack bytes even though this transaction has been aborted.
4330 for (const packId of packIds) {
4331 if (
4332 raw.exec(`SELECT 1 FROM objects WHERE pack_id = ? LIMIT 1`, packId).toArray()
4333 .length > 0
4334 )
4335 return false;
4336 }
4337 return true;
4338 });
4339 }),
4340
4341 validatePush: Effect.fn(function* (input: CommitPushInput) {
4342 const meta = yield* requireMeta;
4343 const referenced = new Set([...input.referenced, ...input.referencedParents]);
4344 for (const command of input.commands)
4345 if (command.newOid !== ZERO_OID) referenced.add(command.newOid);
4346 const missing = yield* storeFor(meta.repoId).missingObjects(
4347 Array.from(referenced),
4348 input.pushId,
4349 );
4350 if (missing.length > 0)
4351 return yield* StoreError.make({
4352 reason: `missing objects (${missing.length})`,
4353 });
4354 }),
4355
4356 readPreparedObject: Effect.fn(function* (pushId: string, oid: Oid, maxBytes: number) {
4357 const meta = yield* requireMeta;
4358 const push = yield* sql.first<{ state: string }>(
4359 `SELECT state FROM pushes WHERE push_id = ?`,
4360 pushId,
4361 );
4362 if (push?.state !== "staging")
4363 return yield* StoreError.make({
4364 reason: "push is no longer active",
4365 });
4366 return yield* storeFor(meta.repoId).readPrepared(pushId, oid, maxBytes);
4367 }),
4368
4369 readPushBase: Effect.fn(function* (oid: Oid) {
4370 const meta = yield* requireMeta;
4371 return yield* ingestStoreOf(storeFor(meta.repoId)).readBase(oid);
4372 }),
4373
4374 commitPush: Effect.fn(function* (input: CommitPushInput) {
4375 const meta = yield* requireMeta;
4376 const objects = storeFor(meta.repoId);
4377 const startedAt = yield* Effect.sync(() => performance.now());
4378 const since = (from: number) => Effect.sync(() => performance.now() - from);
4379 const allNg = (reason: string) =>
4380 input.commands.map((cmd) => ({
4381 ref: cmd.ref,
4382 ok: false,
4383 reason,
4384 }));
4385 if (meta.readOnly) return { unpack: "ok", results: allNg("repository is read-only") };
4386 // Full connectivity check (§3.6 step 5) over the staged rows.
4387 const referenced = new Set<string>(input.referenced);
4388 for (const parent of input.referencedParents) referenced.add(parent);
4389 for (const cmd of input.commands) {
4390 if (cmd.newOid !== ZERO_OID) referenced.add(cmd.newOid);
4391 }
4392 const connectivityStarted = yield* Effect.sync(() => performance.now());
4393 const missing = yield* objects.missingObjects(Array.from(referenced), input.pushId);
4394 const connectivityMs = yield* since(connectivityStarted);
4395 if (missing.length > 0) {
4396 return {
4397 unpack: `missing objects (${missing.length})`,
4398 results: allNg("missing necessary objects"),
4399 } satisfies CommitPushResult;
4400 }
4401 const graph = yield* computeGraphRows(input.commits);
4402 const finalizeStarted = yield* Effect.sync(() => performance.now());
4403 const results = yield* finalizeRefTxn({
4404 commands: input.commands,
4405 atomic: input.atomic,
4406 pushId: input.pushId,
4407 deferFlip: true,
4408 graph,
4409 });
4410 const finalizeMs = yield* since(finalizeStarted);
4411 // The per-row flip, off the response path (DESIGN §22.14).
4412 yield* state.waitUntil(flipPush(input.pushId).pipe(Effect.ignore));
4413
4414 yield* setConfig(
4415 "last_push",
4416 JSON.stringify({
4417 objects: input.stats.objects,
4418 bytes: input.stats.bytes,
4419 ingestMs: Math.round(input.stats.ingestMs),
4420 phases:
4421 input.stats.phases === undefined
4422 ? undefined
4423 : Object.fromEntries(
4424 Object.entries(input.stats.phases).map(([k, v]) => [k, Math.round(v)]),
4425 ),
4426 stageMs: Math.round(input.stats.stageMs),
4427 connectivityMs: Math.round(connectivityMs),
4428 finalizeMs: Math.round(finalizeMs),
4429 totalMs: Math.round(yield* since(startedAt)),
4430 } satisfies PushStatsData),
4431 ).pipe(Effect.ignore);
4432
4433 // Post-commit bookkeeping off the response path: staged-row GC
4434 // and, once enough loose bytes have accumulated, compaction into
4435 // an R2 pack (DESIGN.md §12.1).
4436 yield* state.waitUntil(
4437 upsertJob("gc", null).pipe(
4438 Effect.flatMap(() => armAlarmAt(Date.now() + STAGING_TTL_MS)),
4439 Effect.flatMap(() => shouldCompact(sql)),
4440 Effect.flatMap((due) =>
4441 due
4442 ? upsertJob("compact", null).pipe(
4443 Effect.flatMap(() => armAlarmAt(Date.now() + 1_000)),
4444 )
4445 : Effect.void,
4446 ),
4447 // Re-cut the clone bundle for the new ref snapshot. Debounced
4448 // by the alarm itself: rapid-fire pushes coalesce into one
4449 // run because the job row is upserted, not queued.
4450 Effect.flatMap(() => upsertJob("bundle", null)),
4451 Effect.flatMap(() => armAlarmAt(Date.now() + 2_000)),
4452 Effect.ignore,
4453 ),
4454 );
4455 return { unpack: "ok", results } satisfies CommitPushResult;
4456 }),
4457
4458 readObject: Effect.fn(function* (input: ReadObjectInput) {
4459 const meta = yield* requireMeta;
4460 const objects = storeFor(meta.repoId);
4461 const objectMeta = yield* objects.getMeta(input.oid);
4462 if (objectMeta === undefined) {
4463 return yield* ObjectNotFound.make({ oid: input.oid });
4464 }
4465 const actual = objectTypeName(objectMeta.type);
4466 if (input.expect !== undefined && actual !== input.expect) {
4467 return yield* WrongObjectType.make({
4468 oid: input.oid,
4469 expected: input.expect,
4470 actual,
4471 });
4472 }
4473 const content = yield* objects.readContent(input.oid);
4474 return {
4475 oid: input.oid,
4476 type: actual,
4477 size: objectMeta.size,
4478 content,
4479 } satisfies ObjectData;
4480 }),
4481
4482 readCommitLog: Effect.fn(function* (input: CommitLogInput) {
4483 const meta = yield* requireMeta;
4484 const objects = storeFor(meta.repoId);
4485 const limit = Math.max(1, Math.min(input.limit ?? 20, 100));
4486
4487 // Resolve the starting commit.
4488 const start = yield* resolveRevision(meta.defaultBranch, input.ref);
4489
4490 // Commit-time-ordered walk (v1: recomputed from the start each
4491 // page; the cursor is the last emitted oid).
4492 const frontier: Array<{ oid: string; time: number }> = [];
4493 const visited = new Set<string>();
4494 const items: Array<CommitData> = [];
4495 let skipping = input.cursor !== undefined;
4496 let hasMore = false;
4497
4498 const pushFrontier = Effect.fn(function* (oid: string) {
4499 if (visited.has(oid)) return;
4500 visited.add(oid);
4501 const row = yield* sql.first<{ commit_time: number }>(
4502 `SELECT commit_time FROM commits WHERE oid = ?`,
4503 oid,
4504 );
4505 if (row !== undefined) {
4506 frontier.push({ oid, time: row.commit_time });
4507 }
4508 });
4509 // The start may be a tag — peel to a commit.
4510 const startMeta = yield* objects.getMeta(start);
4511 if (startMeta === undefined) {
4512 return yield* RefNotFound.make({ ref: input.ref ?? "HEAD" });
4513 }
4514 const startCommit =
4515 startMeta.type === ObjectType.tag
4516 ? yield* peelOid(meta.repoId, start).pipe(Effect.map((peeled) => peeled ?? start))
4517 : start;
4518 yield* pushFrontier(startCommit);
4519
4520 while (frontier.length > 0) {
4521 // pop max commit_time
4522 let best = 0;
4523 for (let i = 1; i < frontier.length; i++) {
4524 if (frontier[i]!.time > frontier[best]!.time) best = i;
4525 }
4526 const [next] = frontier.splice(best, 1);
4527 const oid = next!.oid;
4528 const parents = yield* sql.all<{ parent: string; ord: number }>(
4529 `SELECT parent, ord FROM commit_parents WHERE oid = ? ORDER BY ord`,
4530 oid,
4531 );
4532 for (const parent of parents) {
4533 yield* pushFrontier(parent.parent);
4534 }
4535 if (skipping) {
4536 if (oid === input.cursor) skipping = false;
4537 continue;
4538 }
4539 if (items.length >= limit) {
4540 hasMore = true;
4541 break;
4542 }
4543 const content = yield* objects.readContent(oid);
4544 const parsed = yield* parseCommit(content).pipe(
4545 Effect.mapError((error) => StoreError.make({ reason: error.reason })),
4546 );
4547 items.push({
4548 oid,
4549 tree: parsed.tree,
4550 parents: parsed.parents,
4551 author: {
4552 name: parsed.author.name,
4553 email: parsed.author.email,
4554 date: parsed.author.when,
4555 tz: parsed.author.tz,
4556 },
4557 committer: {
4558 name: parsed.committer.name,
4559 email: parsed.committer.email,
4560 date: parsed.committer.when,
4561 tz: parsed.committer.tz,
4562 },
4563 message: parsed.message,
4564 });
4565 }
4566
4567 return {
4568 items,
4569 nextCursor: hasMore ? items[items.length - 1]!.oid : null,
4570 hasMore,
4571 } satisfies CommitLogPage;
4572 }),
4573
4574 readCommitDiff: Effect.fn(function* (input: { readonly oid: string }) {
4575 const meta = yield* requireMeta;
4576 const objects = storeFor(meta.repoId);
4577
4578 const readCommitOrFail = Effect.fn(function* (oid: string) {
4579 const objectMeta = yield* objects.getMeta(oid);
4580 if (objectMeta === undefined) {
4581 return yield* ObjectNotFound.make({ oid });
4582 }
4583 if (objectMeta.type !== ObjectType.commit) {
4584 return yield* WrongObjectType.make({
4585 oid,
4586 expected: "commit",
4587 actual: objectTypeName(objectMeta.type),
4588 });
4589 }
4590 const content = yield* objects.readContent(oid);
4591 return yield* parseCommit(content).pipe(
4592 Effect.mapError((error) => StoreError.make({ reason: error.reason })),
4593 );
4594 });
4595
4596 const commit = yield* readCommitOrFail(input.oid);
4597 // First-parent diff (the GitHub default for merge commits).
4598 const parent = commit.parents[0] ?? null;
4599 const parentTree = parent === null ? undefined : (yield* readCommitOrFail(parent)).tree;
4600
4601 const result = yield* diffTrees(objects, parentTree, commit.tree);
4602 return {
4603 oid: input.oid,
4604 parent,
4605 files: result.files,
4606 truncated: result.truncated,
4607 } satisfies CommitDiffData;
4608 }),
4609
4610 compareCommits: Effect.fn(function* (input: CompareInput) {
4611 const meta = yield* requireMeta;
4612 const objects = storeFor(meta.repoId);
4613
4614 const baseOid = yield* resolveToCommit(meta.repoId, meta.defaultBranch, input.base);
4615 const headOid = yield* resolveToCommit(meta.repoId, meta.defaultBranch, input.head);
4616
4617 // The classic merge-base paint walk over the commit-graph tables,
4618 // ordered by the pre-computed generation number (`gen`, maintained
4619 // at ingest): a common ancestor's gen is strictly less than any
4620 // node on a path to it, so processing gen-descending guarantees
4621 // the first both-flagged pop is a best common ancestor. Nodes
4622 // carrying both flags propagate both but are counted as neither.
4623 const BASE = 1;
4624 const HEAD = 2;
4625 const BOTH = 3;
4626 const flags = new Map<string, number>();
4627 flags.set(baseOid, BASE);
4628 flags.set(headOid, (flags.get(headOid) ?? 0) | HEAD);
4629 const heap: Array<WalkHeapNode> = [];
4630 const timeOf = new Map<string, number>();
4631
4632 const rowOf = (oid: string) =>
4633 sql.first<{ gen: number; commit_time: number }>(
4634 `SELECT gen, commit_time FROM commits WHERE oid = ?`,
4635 oid,
4636 );
4637
4638 // seed (base === head short-circuits: mergeBase = both, 0/0)
4639 for (const oid of new Set([baseOid, headOid])) {
4640 const row = yield* rowOf(oid);
4641 if (row === undefined) {
4642 return yield* ObjectNotFound.make({ oid });
4643 }
4644 heapPush(heap, { oid, gen: row.gen, time: row.commit_time });
4645 timeOf.set(oid, row.commit_time);
4646 }
4647
4648 let mergeBase: string | undefined;
4649 let aheadBy = 0;
4650 let behindBy = 0;
4651 let pops = 0;
4652 const headSide: Array<string> = [];
4653
4654 while (heap.length > 0 && pops < MAX_COMPARE_WALK) {
4655 // stop when nothing in the heap can still change the counts
4656 if (mergeBase !== undefined && heap.every((n) => (flags.get(n.oid)! & BOTH) === BOTH)) {
4657 break;
4658 }
4659 const node = heapPop(heap);
4660 pops++;
4661 const f = flags.get(node.oid)!;
4662 if ((f & BOTH) === BOTH) {
4663 if (mergeBase === undefined) mergeBase = node.oid;
4664 } else if (f === HEAD) {
4665 aheadBy++;
4666 headSide.push(node.oid);
4667 } else {
4668 behindBy++;
4669 }
4670 const parents = yield* sql.all<{ parent: string }>(
4671 `SELECT parent FROM commit_parents WHERE oid = ? ORDER BY ord`,
4672 node.oid,
4673 );
4674 for (const { parent } of parents) {
4675 const prev = flags.get(parent) ?? 0;
4676 const next = prev | f;
4677 if (next === prev) continue;
4678 flags.set(parent, next);
4679 if (prev === 0) {
4680 const row = yield* rowOf(parent);
4681 if (row === undefined) continue; // shallow boundary
4682 heapPush(heap, {
4683 oid: parent,
4684 gen: row.gen,
4685 time: row.commit_time,
4686 });
4687 timeOf.set(parent, row.commit_time);
4688 }
4689 // already-heaped nodes just get richer flags; no decrease-key
4690 // needed (flags only grow; both-flagged pops count as neither)
4691 }
4692 }
4693 if (mergeBase === undefined) {
4694 return yield* NoMergeBase.make({
4695 base: asOid(baseOid),
4696 head: asOid(headOid),
4697 });
4698 }
4699
4700 // head-side commits, committer-time descending, capped.
4701 headSide.sort((a, b) => (timeOf.get(b) ?? 0) - (timeOf.get(a) ?? 0));
4702 const commitOids = headSide.slice(0, MAX_COMPARE_COMMITS);
4703 const commits: Array<CommitData> = [];
4704 for (const oid of commitOids) {
4705 const content = yield* objects.readContent(oid);
4706 const parsed = yield* parseCommit(content).pipe(
4707 Effect.mapError((error) => StoreError.make({ reason: error.reason })),
4708 );
4709 commits.push({
4710 oid,
4711 tree: parsed.tree,
4712 parents: parsed.parents,
4713 author: {
4714 name: parsed.author.name,
4715 email: parsed.author.email,
4716 date: parsed.author.when,
4717 tz: parsed.author.tz,
4718 },
4719 committer: {
4720 name: parsed.committer.name,
4721 email: parsed.committer.email,
4722 date: parsed.committer.when,
4723 tz: parsed.committer.tz,
4724 },
4725 message: parsed.message,
4726 });
4727 }
4728
4729 // three-dot file diff: mergeBase tree vs head tree
4730 const treeOf = Effect.fn(function* (oid: string) {
4731 const row = yield* sql.first<{ tree: string }>(
4732 `SELECT tree FROM commits WHERE oid = ?`,
4733 oid,
4734 );
4735 if (row === undefined) {
4736 return yield* StoreError.make({
4737 reason: `commit ${oid} missing from the commit graph`,
4738 });
4739 }
4740 return row.tree;
4741 });
4742 const mergeBaseTree = yield* treeOf(mergeBase);
4743 const headTree = yield* treeOf(headOid);
4744 const filesResult = yield* diffTrees(objects, mergeBaseTree, headTree);
4745
4746 return {
4747 base: baseOid,
4748 head: headOid,
4749 mergeBase,
4750 aheadBy,
4751 behindBy,
4752 commits,
4753 commitsTruncated: headSide.length > MAX_COMPARE_COMMITS || pops >= MAX_COMPARE_WALK,
4754 files: filesResult.files,
4755 filesTruncated: filesResult.truncated,
4756 } satisfies CompareData;
4757 }),
4758
4759 readFileAtPath: Effect.fn(function* (input: ReadFileInput) {
4760 const meta = yield* requireMeta;
4761 const objects = storeFor(meta.repoId);
4762
4763 const startOid = yield* resolveRevision(meta.defaultBranch, input.ref);
4764
4765 // Resolve start → commit → root tree (peel tags on the way).
4766 const resolveRootTree = Effect.gen(function* () {
4767 let current = startOid;
4768 for (let hops = 0; hops < 16; hops++) {
4769 const objectMeta = yield* objects.getMeta(current);
4770 if (objectMeta === undefined) {
4771 return yield* ObjectNotFound.make({ oid: current });
4772 }
4773 if (objectMeta.type === ObjectType.tree) return current;
4774 const content = yield* objects.readContent(current);
4775 if (objectMeta.type === ObjectType.commit) {
4776 const parsed = yield* parseCommit(content).pipe(
4777 Effect.mapError((error) => StoreError.make({ reason: error.reason })),
4778 );
4779 return parsed.tree;
4780 }
4781 if (objectMeta.type === ObjectType.tag) {
4782 const parsed = yield* parseTag(content).pipe(
4783 Effect.mapError((error) => StoreError.make({ reason: error.reason })),
4784 );
4785 current = parsed.object;
4786 continue;
4787 }
4788 return yield* ObjectNotFound.make({ oid: current });
4789 }
4790 return yield* ObjectNotFound.make({ oid: current });
4791 });
4792 const rootTree = yield* resolveRootTree;
4793
4794 const segments = input.path.split("/").filter(Boolean);
4795 if (segments.length === 0) {
4796 return yield* ObjectNotFound.make({ oid: rootTree });
4797 }
4798 let treeOid = rootTree;
4799 for (let i = 0; i < segments.length; i++) {
4800 const content = yield* objects.readContent(treeOid);
4801 const entries = yield* parseTree(content).pipe(
4802 Effect.mapError((error) => StoreError.make({ reason: error.reason })),
4803 );
4804 const entry = entries.find((e) => e.name === segments[i]);
4805 if (entry === undefined) {
4806 return yield* ObjectNotFound.make({ oid: `${input.path}` });
4807 }
4808 const kind = treeEntryKind(entry.mode);
4809 if (i === segments.length - 1) {
4810 if (kind !== "blob") {
4811 return yield* ObjectNotFound.make({ oid: input.path });
4812 }
4813 const blobMeta = yield* objects.getMeta(entry.oid);
4814 if (blobMeta === undefined) {
4815 return yield* ObjectNotFound.make({ oid: entry.oid });
4816 }
4817 const blob = yield* objects.readContent(entry.oid);
4818 return {
4819 oid: entry.oid,
4820 mode: entry.mode,
4821 size: blobMeta.size,
4822 content: blob,
4823 } satisfies FileData;
4824 }
4825 if (kind !== "tree") {
4826 return yield* ObjectNotFound.make({ oid: input.path });
4827 }
4828 treeOid = entry.oid;
4829 }
4830 // unreachable — the loop returns or fails
4831 return yield* ObjectNotFound.make({ oid: input.path });
4832 }),
4833
4834 createPull: Effect.fn(function* (input: CreatePullInput) {
4835 yield* requireMeta;
4836 const baseRef = yield* normalizeBranchRef(input.base);
4837 const headRef = yield* normalizeBranchRef(input.head);
4838 if (baseRef === headRef) {
4839 return yield* ValidationError.make({
4840 message: "base and head must be different branches",
4841 });
4842 }
4843 for (const ref of [baseRef, headRef]) {
4844 const row = yield* sql.first<RefRow>(`SELECT name, oid FROM refs WHERE name = ?`, ref);
4845 if (row === undefined) {
4846 return yield* BranchMissing.make({ ref });
4847 }
4848 }
4849 const duplicate = yield* sql.first<{ number: number }>(
4850 `SELECT number FROM pulls WHERE state = 'open' AND base_ref = ? AND head_ref = ?`,
4851 baseRef,
4852 headRef,
4853 );
4854 if (duplicate !== undefined) {
4855 return yield* PullExists.make({ number: duplicate.number });
4856 }
4857 const now = Date.now();
4858 // Allocate the number and insert the row in ONE transaction —
4859 // the DO's input/output gates make the counter race-free.
4860 const number = yield* sql.transactionSync((raw) => {
4861 const seq = raw
4862 .exec<{ value: string }>(`SELECT value FROM config WHERE key = 'pull_seq'`)
4863 .toArray()[0];
4864 const next = (seq === undefined ? 0 : Number(seq.value)) + 1;
4865 raw.exec(
4866 `INSERT INTO config (key, value) VALUES ('pull_seq', ?)
4867 ON CONFLICT (key) DO UPDATE SET value = excluded.value`,
4868 String(next),
4869 );
4870 raw.exec(
4871 `INSERT INTO pulls (number, title, body, base_ref, head_ref, state, created_at, updated_at)
4872 VALUES (?, ?, ?, ?, ?, 'open', ?, ?)`,
4873 next,
4874 input.title,
4875 input.body ?? null,
4876 baseRef,
4877 headRef,
4878 now,
4879 now,
4880 );
4881 return next;
4882 });
4883 return {
4884 number,
4885 title: input.title,
4886 body: input.body ?? null,
4887 baseRef,
4888 headRef,
4889 state: "open",
4890 createdAt: now,
4891 updatedAt: now,
4892 mergedAt: null,
4893 mergeCommit: null,
4894 } satisfies PullData;
4895 }),
4896
4897 listPulls: Effect.fn(function* (input: ListPullsInput) {
4898 yield* requireMeta;
4899 const state = input.state ?? "open";
4900 const limit = Math.max(1, Math.min(input.limit ?? 20, 100));
4901 const cursor = input.cursor === undefined ? undefined : Number(input.cursor);
4902 const where: Array<string> = [];
4903 const bindings: Array<string | number> = [];
4904 if (state !== "all") {
4905 where.push("state = ?");
4906 bindings.push(state);
4907 }
4908 if (cursor !== undefined && Number.isFinite(cursor)) {
4909 where.push("number < ?");
4910 bindings.push(cursor);
4911 }
4912 const rows = yield* sql.all<PullRow>(
4913 `SELECT * FROM pulls
4914 ${where.length > 0 ? `WHERE ${where.join(" AND ")}` : ""}
4915 ORDER BY number DESC LIMIT ?`,
4916 ...bindings,
4917 limit + 1,
4918 );
4919 const items = rows.slice(0, limit).map(rowToPull);
4920 const hasMore = rows.length > limit;
4921 return {
4922 items,
4923 nextCursor: hasMore ? String(items[items.length - 1]!.number) : null,
4924 hasMore,
4925 } satisfies PullsPage;
4926 }),
4927
4928 getPull: Effect.fn(function* (number: number) {
4929 const meta = yield* requireMeta;
4930 const row = yield* loadPull(number);
4931 const pull = rowToPull(row);
4932 const empty = {
4933 baseOid: null,
4934 headOid: null,
4935 mergeBase: null,
4936 aheadBy: null,
4937 behindBy: null,
4938 mergeable: null,
4939 mergeableReason: null,
4940 } as const;
4941 // A merged PR's record is `mergeCommit` — live fields stay null.
4942 if (pull.state === "merged") {
4943 return { ...pull, ...empty } satisfies PullDetailData;
4944 }
4945 const baseRow = yield* sql.first<RefRow>(
4946 `SELECT name, oid FROM refs WHERE name = ?`,
4947 row.base_ref,
4948 );
4949 const headRow = yield* sql.first<RefRow>(
4950 `SELECT name, oid FROM refs WHERE name = ?`,
4951 row.head_ref,
4952 );
4953 if (baseRow === undefined || headRow === undefined) {
4954 // A branch is gone: everything downstream is uncomputable.
4955 return {
4956 ...pull,
4957 ...empty,
4958 baseOid: baseRow?.oid ?? null,
4959 headOid: headRow?.oid ?? null,
4960 } satisfies PullDetailData;
4961 }
4962 const baseTip = baseRow.oid;
4963 const headTip = headRow.oid;
4964 const cmp = yield* paintWalk(baseTip, headTip, MAX_PULL_WALK);
4965 if (cmp.saturated) {
4966 return {
4967 ...pull,
4968 ...empty,
4969 baseOid: baseTip,
4970 headOid: headTip,
4971 mergeableReason: "unknown",
4972 } satisfies PullDetailData;
4973 }
4974 const live = {
4975 ...pull,
4976 baseOid: baseTip,
4977 headOid: headTip,
4978 mergeBase: cmp.mergeBase ?? null,
4979 aheadBy: cmp.aheadBy,
4980 behindBy: cmp.behindBy,
4981 };
4982 if (cmp.mergeBase === undefined) {
4983 // Disjoint histories: our trivial-merge rules can never merge.
4984 return {
4985 ...live,
4986 mergeable: false,
4987 mergeableReason: "conflict",
4988 } satisfies PullDetailData;
4989 }
4990 if (cmp.mergeBase === headTip) {
4991 return {
4992 ...live,
4993 mergeable: false,
4994 mergeableReason: "up-to-date",
4995 } satisfies PullDetailData;
4996 }
4997 if (cmp.mergeBase === baseTip) {
4998 return {
4999 ...live,
5000 mergeable: true,
Showing first 5,000 of 5,387 lines. View raw