The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { RuntimeContext } from "alchemy/RuntimeContext"; |
| 2 | /** Git operations without HTTP requests, user services, or authorization callbacks. */ |
| 3 | import * as Context from "effect/Context"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Fiber from "effect/Fiber"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import * as Semaphore from "effect/Semaphore"; |
| 10 | import { PushDenied, RefConflict, RepoNotFound } from "./Api/Schema.ts"; |
| 11 | import { BlobStore } from "./BlobStore.ts"; |
| 12 | import { Hasher } from "./Hasher/Hasher.ts"; |
| 13 | import { Operations, OperationsLive } from "./Operations.ts"; |
| 14 | import { StoreError } from "./Protocol/Store.ts"; |
| 15 | import { |
| 16 | incomingStates, |
| 17 | invalidPush, |
| 18 | preparedStates, |
| 19 | type PreparedPush, |
| 20 | type PushInput, |
| 21 | type RefUpdate, |
| 22 | } from "./Push.ts"; |
| 23 | import { encodeStagedBatch } from "./PushWire.ts"; |
| 24 | import { ulid } from "./RegistryObject.ts"; |
| 25 | import { |
| 26 | MAX_PACK_BYTES, |
| 27 | PUSH_WAIT_TIMEOUT, |
| 28 | RepoStore, |
| 29 | ingestPackFrom, |
| 30 | isolatePushGate, |
| 31 | pushPermitsFor, |
| 32 | type CommitPushInput, |
| 33 | type IngestResult, |
| 34 | type IngestStore, |
| 35 | type RepoMetaData, |
| 36 | } from "./RepoObject.ts"; |
| 37 | import { incomingKey, wirePackId } from "./Store/Keys.ts"; |
| 38 | import { sliceRandomAccess } from "./Store/PackSource.ts"; |
| 39 | |
| 40 | const path = (repo: RepoMetaData) => ({ owner: repo.owner, repo: repo.name }); |
| 41 | |
| 42 | const asStoreError = (error: { readonly _tag: string; readonly reason?: string }) => |
| 43 | Schema.is(StoreError)(error) |
| 44 | ? error |
| 45 | : StoreError.make({ |
| 46 | reason: `${error._tag}${error.reason === undefined ? "" : `: ${error.reason}`}`, |
| 47 | }); |
| 48 | |
| 49 | const scopedMutation = <A, E, R>( |
| 50 | updates: ReadonlyArray<RefUpdate>, |
| 51 | commit: Effect.Effect<A, E, R>, |
| 52 | ) => |
| 53 | Effect.gen(function* () { |
| 54 | let active = true; |
| 55 | yield* Effect.addFinalizer(() => |
| 56 | Effect.sync(() => { |
| 57 | active = false; |
| 58 | }), |
| 59 | ); |
| 60 | return Object.freeze({ |
| 61 | updates: Object.freeze(updates.map((update) => Object.freeze({ ...update }))), |
| 62 | commit: Effect.suspend<A, E | StoreError, R>(() => { |
| 63 | if (!active) return Effect.fail(invalidPush()); |
| 64 | active = false; |
| 65 | return commit; |
| 66 | }), |
| 67 | }); |
| 68 | }); |
| 69 | |
| 70 | const make = Effect.gen(function* () { |
| 71 | const operations = yield* Operations; |
| 72 | const repos = yield* RepoStore; |
| 73 | const blobs = yield* BlobStore; |
| 74 | const hasher = yield* Hasher; |
| 75 | const gate = yield* isolatePushGate; |
| 76 | const owner = {}; |
| 77 | |
| 78 | const get = (path: { readonly owner: string; readonly repo: string }) => |
| 79 | Effect.gen(function* () { |
| 80 | const name = { |
| 81 | owner: path.owner.toLowerCase(), |
| 82 | repo: path.repo.toLowerCase().replace(/\.git$/, ""), |
| 83 | }; |
| 84 | const entry = yield* operations.resolveCached(name.owner, name.repo); |
| 85 | if (entry === undefined || entry.deletedAt !== null) return yield* RepoNotFound.make(name); |
| 86 | return yield* repos.getByName(entry.repoId).getRepoMeta(); |
| 87 | }); |
| 88 | |
| 89 | const preparePush = (repo: RepoMetaData, input: PushInput) => |
| 90 | Effect.gen(function* () { |
| 91 | const source = incomingStates.get(input); |
| 92 | if (source === undefined || !source.active || source.claimed) return yield* invalidPush(); |
| 93 | source.claimed = true; |
| 94 | const { feeder, receiving, packStart } = source; |
| 95 | const stub = repos.getByName(repo.repoId); |
| 96 | const staging: Array<Fiber.Fiber<void, StoreError>> = []; |
| 97 | let active = true; |
| 98 | let committed = false; |
| 99 | let ingest: IngestResult | undefined; |
| 100 | const receiveId = yield* ulid(); |
| 101 | const parkedKey = incomingKey(repo.repoId, receiveId); |
| 102 | // Registered before ingest: cancellation and policy failure share the same owner. |
| 103 | const begun = yield* Effect.acquireRelease( |
| 104 | stub.beginPush({ commands: input.updates }), |
| 105 | (begun) => |
| 106 | Effect.gen(function* () { |
| 107 | active = false; |
| 108 | if (begun._tag === "denied") return; |
| 109 | yield* Fiber.interruptAll(staging.splice(0)); |
| 110 | if (committed) return; |
| 111 | // Ask durable state before deleting bytes: an interrupted RPC may have committed. |
| 112 | const keys = [parkedKey, ...(ingest?.resolvedKey?.split(",") ?? [])].filter(Boolean); |
| 113 | const packIds = keys.map((key) => wirePackId(key.slice(key.lastIndexOf("/") + 1, -5))); |
| 114 | const aborted = yield* stub |
| 115 | .abortPush(begun.pushId, packIds) |
| 116 | .pipe(Effect.orElseSucceed(() => false)); |
| 117 | if (aborted) { |
| 118 | yield* blobs.delete(keys).pipe(Effect.ignore); |
| 119 | } |
| 120 | }), |
| 121 | ); |
| 122 | if (begun._tag === "denied") |
| 123 | return yield* PushDenied.make({ |
| 124 | ref: input.updates[0]?.ref ?? "", |
| 125 | reason: begun.reason, |
| 126 | }); |
| 127 | const pushId = begun.pushId; |
| 128 | const permits = pushPermitsFor(source.declaredBytes); |
| 129 | const acquired = yield* Effect.acquireRelease( |
| 130 | Semaphore.take(gate, permits).pipe(Effect.timeoutOption(PUSH_WAIT_TIMEOUT)), |
| 131 | (acquired) => (Option.isSome(acquired) ? Semaphore.release(gate, permits) : Effect.void), |
| 132 | ); |
| 133 | if (Option.isNone(acquired)) |
| 134 | return yield* StoreError.make({ reason: "push admission timed out" }); |
| 135 | const started = Date.now(); |
| 136 | const stageGate = yield* Semaphore.make(6); |
| 137 | const bases = new Map<string, Effect.Success<ReturnType<typeof stub.readPushBase>>>(); |
| 138 | const store: IngestStore = { |
| 139 | insertStagedBatch: (id, objects) => |
| 140 | Effect.gen(function* () { |
| 141 | const encoded = encodeStagedBatch(objects); |
| 142 | const fiber = yield* stub |
| 143 | .stagePush(id, encoded) |
| 144 | .pipe( |
| 145 | Effect.mapError(asStoreError), |
| 146 | Effect.provide(RuntimeContext.phantom), |
| 147 | Semaphore.withPermits(stageGate, 1), |
| 148 | Effect.forkDetach, |
| 149 | ); |
| 150 | staging.push(fiber); |
| 151 | }), |
| 152 | settle: Effect.gen(function* () { |
| 153 | for (const fiber of staging) yield* Fiber.join(fiber); |
| 154 | staging.length = 0; |
| 155 | }), |
| 156 | readBase: (oid) => |
| 157 | bases.has(oid) |
| 158 | ? Effect.succeed(bases.get(oid)) |
| 159 | : stub.readPushBase(oid).pipe( |
| 160 | Effect.mapError(asStoreError), |
| 161 | Effect.tap((value) => |
| 162 | Effect.sync(() => { |
| 163 | bases.set(oid, value); |
| 164 | }), |
| 165 | ), |
| 166 | Effect.provide(RuntimeContext.phantom), |
| 167 | ), |
| 168 | }; |
| 169 | const probe = yield* feeder.source.read(packStart, 12); |
| 170 | if (probe.length > 0 && probe.length < 12) { |
| 171 | return yield* StoreError.make({ reason: "truncated pack header" }); |
| 172 | } |
| 173 | const hasPack = probe.length === 12; |
| 174 | if (hasPack) { |
| 175 | ingest = yield* ingestPackFrom(sliceRandomAccess(feeder.source, packStart), { |
| 176 | store, |
| 177 | pushId, |
| 178 | hasher, |
| 179 | spill: { |
| 180 | body: feeder.source, |
| 181 | feeder, |
| 182 | packStart, |
| 183 | blobs, |
| 184 | key: parkedKey, |
| 185 | packId: wirePackId(receiveId), |
| 186 | repoId: repo.repoId, |
| 187 | threshold: MAX_PACK_BYTES, |
| 188 | }, |
| 189 | }); |
| 190 | } |
| 191 | if (store.settle) yield* store.settle; |
| 192 | const received = yield* Fiber.join(receiving).pipe(Effect.flatMap(Effect.fromResult)); |
| 193 | const commitInput: CommitPushInput = { |
| 194 | pushId, |
| 195 | commands: input.updates, |
| 196 | atomic: input.atomic, |
| 197 | commits: ingest?.commits ?? [], |
| 198 | referenced: ingest?.referenced ?? [], |
| 199 | referencedParents: ingest?.referencedParents ?? [], |
| 200 | promoted: ingest?.promoted ?? 0, |
| 201 | stats: { |
| 202 | objects: ingest?.objectCount ?? 0, |
| 203 | bytes: hasPack ? received.total - packStart : 0, |
| 204 | ingestMs: Date.now() - started, |
| 205 | stageMs: ingest?.stageMs ?? 0, |
| 206 | phases: ingest?.phases, |
| 207 | }, |
| 208 | }; |
| 209 | // Connectivity is checked before application validation, then again at commit. |
| 210 | yield* stub.validatePush(commitInput).pipe(Effect.mapError(asStoreError)); |
| 211 | const prepared: PreparedPush = Object.freeze({ |
| 212 | repo: Object.freeze({ ...repo }), |
| 213 | updates: input.updates, |
| 214 | readObject: (oid: string, maxBytes = 1024 * 1024) => |
| 215 | Effect.suspend(() => |
| 216 | !active || !source.active |
| 217 | ? Effect.fail(invalidPush()) |
| 218 | : stub.readPreparedObject(pushId, oid, maxBytes).pipe(Effect.mapError(asStoreError)), |
| 219 | ), |
| 220 | }); |
| 221 | preparedStates.set(prepared, { |
| 222 | owner, |
| 223 | commit: Effect.suspend(() => { |
| 224 | if (!active || !source.active) return Effect.fail(invalidPush()); |
| 225 | active = false; |
| 226 | return stub.commitPush(commitInput).pipe( |
| 227 | Effect.mapError(asStoreError), |
| 228 | Effect.tap((result) => |
| 229 | Effect.sync(() => { |
| 230 | committed = result.results.some((ref) => ref.ok); |
| 231 | }), |
| 232 | ), |
| 233 | ); |
| 234 | }), |
| 235 | }); |
| 236 | return prepared; |
| 237 | }); |
| 238 | |
| 239 | const commitPush = (prepared: PreparedPush) => |
| 240 | Effect.suspend(() => { |
| 241 | const state = preparedStates.get(prepared); |
| 242 | return state?.owner === owner ? state.commit : Effect.fail(invalidPush()); |
| 243 | }); |
| 244 | |
| 245 | const prepareRefUpdate = ( |
| 246 | repo: RepoMetaData, |
| 247 | input: { |
| 248 | readonly ref: string; |
| 249 | readonly newOid: string; |
| 250 | readonly expectedOid?: string | null; |
| 251 | }, |
| 252 | ) => |
| 253 | Effect.gen(function* () { |
| 254 | const stub = repos.getByName(repo.repoId); |
| 255 | const current = yield* stub.getRef(input.ref).pipe( |
| 256 | Effect.map((ref) => ref.oid), |
| 257 | Effect.catchTag("RefNotFound", () => Effect.succeed("0".repeat(40))), |
| 258 | ); |
| 259 | if ( |
| 260 | input.expectedOid !== undefined && |
| 261 | input.expectedOid !== (current === "0".repeat(40) ? null : current) |
| 262 | ) |
| 263 | return yield* RefConflict.make({ |
| 264 | ref: input.ref, |
| 265 | currentOid: current as import("./Api.ts").Oid, |
| 266 | }); |
| 267 | const update = { ref: input.ref, oldOid: current, newOid: input.newOid }; |
| 268 | const commit = stub.updateRef({ |
| 269 | name: input.ref, |
| 270 | newOid: input.newOid, |
| 271 | expectedOid: current === "0".repeat(40) ? null : current, |
| 272 | }); |
| 273 | return yield* scopedMutation([update], commit); |
| 274 | }); |
| 275 | |
| 276 | const prepareRefRemoval = ( |
| 277 | repo: RepoMetaData, |
| 278 | input: { readonly ref: string; readonly expectedOid?: string }, |
| 279 | ) => |
| 280 | Effect.gen(function* () { |
| 281 | const stub = repos.getByName(repo.repoId); |
| 282 | const current = yield* stub.getRef(input.ref); |
| 283 | if (input.expectedOid !== undefined && input.expectedOid !== current.oid) |
| 284 | return yield* RefConflict.make({ |
| 285 | ref: input.ref, |
| 286 | currentOid: current.oid as import("./Api.ts").Oid, |
| 287 | }); |
| 288 | return yield* scopedMutation( |
| 289 | [{ ref: input.ref, oldOid: current.oid, newOid: "0".repeat(40) }], |
| 290 | stub.removeRef({ name: input.ref, expectedOid: current.oid }), |
| 291 | ); |
| 292 | }); |
| 293 | |
| 294 | const prepareMerge = ( |
| 295 | repo: RepoMetaData, |
| 296 | input: Parameters<ReturnType<typeof repos.getByName>["mergePull"]>[0], |
| 297 | ) => |
| 298 | Effect.gen(function* () { |
| 299 | const stub = repos.getByName(repo.repoId); |
| 300 | const pull = yield* stub.getPull(input.number); |
| 301 | return yield* scopedMutation( |
| 302 | [ |
| 303 | { |
| 304 | ref: pull.baseRef, |
| 305 | oldOid: pull.baseOid ?? "0".repeat(40), |
| 306 | newOid: pull.headOid ?? "0".repeat(40), |
| 307 | }, |
| 308 | ], |
| 309 | stub.mergePull({ |
| 310 | ...input, |
| 311 | expectedBaseOid: pull.baseOid ?? "0".repeat(40), |
| 312 | expectedHeadOid: input.expectedHeadOid ?? pull.headOid ?? "0".repeat(40), |
| 313 | }), |
| 314 | ); |
| 315 | }); |
| 316 | |
| 317 | // These operate on repository data; authorization is composed by the caller. |
| 318 | return { |
| 319 | repositories: { |
| 320 | get, |
| 321 | create: (payload: Parameters<typeof operations.repos.create>[0]["payload"]) => |
| 322 | operations.repos.create({ payload }), |
| 323 | list: (query: Parameters<typeof operations.repos.list>[0]["query"]) => |
| 324 | operations.repos.list({ query }), |
| 325 | update: ( |
| 326 | repo: RepoMetaData, |
| 327 | payload: Parameters<typeof operations.repos.update>[0]["payload"], |
| 328 | ) => operations.repos.update({ params: path(repo), payload }), |
| 329 | remove: (repo: RepoMetaData) => operations.repos.delete({ params: path(repo) }), |
| 330 | fork: (repo: RepoMetaData, payload: Parameters<typeof operations.repos.fork>[0]["payload"]) => |
| 331 | operations.repos.fork({ params: path(repo), payload }), |
| 332 | import: (payload: Parameters<typeof operations.repos.import>[0]["payload"]) => |
| 333 | operations.repos.import({ payload }), |
| 334 | compact: (repo: RepoMetaData) => operations.repos.compact({ params: path(repo) }), |
| 335 | }, |
| 336 | refs: { |
| 337 | list: (repo: RepoMetaData) => repos.getByName(repo.repoId).listRefs(), |
| 338 | get: (repo: RepoMetaData, name: string) => repos.getByName(repo.repoId).getRef(name), |
| 339 | update: ( |
| 340 | repo: RepoMetaData, |
| 341 | input: Parameters<ReturnType<typeof repos.getByName>["updateRef"]>[0], |
| 342 | ) => repos.getByName(repo.repoId).updateRef(input), |
| 343 | remove: ( |
| 344 | repo: RepoMetaData, |
| 345 | input: Parameters<ReturnType<typeof repos.getByName>["removeRef"]>[0], |
| 346 | ) => repos.getByName(repo.repoId).removeRef(input), |
| 347 | }, |
| 348 | pulls: { |
| 349 | create: ( |
| 350 | repo: RepoMetaData, |
| 351 | payload: Parameters<typeof operations.pulls.create>[0]["payload"], |
| 352 | ) => operations.pulls.create({ params: path(repo), payload }), |
| 353 | list: (repo: RepoMetaData, query: Parameters<typeof operations.pulls.list>[0]["query"]) => |
| 354 | operations.pulls.list({ params: path(repo), query }), |
| 355 | update: ( |
| 356 | repo: RepoMetaData, |
| 357 | number: number, |
| 358 | payload: Parameters<typeof operations.pulls.update>[0]["payload"], |
| 359 | ) => operations.pulls.update({ params: { ...path(repo), number }, payload }), |
| 360 | get: (repo: RepoMetaData, number: number) => repos.getByName(repo.repoId).getPull(number), |
| 361 | merge: ( |
| 362 | repo: RepoMetaData, |
| 363 | input: Parameters<ReturnType<typeof repos.getByName>["mergePull"]>[0], |
| 364 | ) => repos.getByName(repo.repoId).mergePull(input), |
| 365 | }, |
| 366 | objects: { |
| 367 | commit: (repo: RepoMetaData, oid: string) => |
| 368 | operations.objects.commit({ |
| 369 | params: { ...path(repo), oid: oid as import("./Api.ts").Oid }, |
| 370 | }), |
| 371 | file: ( |
| 372 | repo: RepoMetaData, |
| 373 | input: Parameters<ReturnType<typeof repos.getByName>["readFileAtPath"]>[0], |
| 374 | ) => repos.getByName(repo.repoId).readFileAtPath(input), |
| 375 | log: (repo: RepoMetaData, query: Parameters<typeof operations.objects.log>[0]["query"]) => |
| 376 | operations.objects.log({ params: path(repo), query }), |
| 377 | tree: ( |
| 378 | repo: RepoMetaData, |
| 379 | oid: Parameters<typeof operations.objects.tree>[0]["params"]["oid"], |
| 380 | ) => operations.objects.tree({ params: { ...path(repo), oid } }), |
| 381 | diff: ( |
| 382 | repo: RepoMetaData, |
| 383 | oid: Parameters<typeof operations.objects.diff>[0]["params"]["oid"], |
| 384 | ) => operations.objects.diff({ params: { ...path(repo), oid } }), |
| 385 | compare: ( |
| 386 | repo: RepoMetaData, |
| 387 | query: Parameters<typeof operations.objects.compare>[0]["query"], |
| 388 | ) => operations.objects.compare({ params: path(repo), query }), |
| 389 | read: (repo: RepoMetaData, oid: string) => repos.getByName(repo.repoId).readPushBase(oid), |
| 390 | }, |
| 391 | preparePush, |
| 392 | commitPush, |
| 393 | prepareRefUpdate, |
| 394 | prepareRefRemoval, |
| 395 | prepareMerge, |
| 396 | }; |
| 397 | }); |
| 398 | |
| 399 | /** Git operations usable from HTTP handlers, jobs, and ordinary Effects. */ |
| 400 | export class Engine extends Context.Service<Engine, Effect.Success<typeof make>>()( |
| 401 | "alchemy/Git/Engine", |
| 402 | ) {} |
| 403 | |
| 404 | /** |
| 405 | * Provides the Git engine using the application's repository, registry, blob, and hasher layers. |
| 406 | * |
| 407 | * @layer |
| 408 | * @provides Git.Engine |
| 409 | */ |
| 410 | export const EngineLive = Layer.effect(Engine, make).pipe(Layer.provideMerge(OperationsLive)); |