Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256/** The landing loop: land, and after each lost race replay and revalidate outside both locks, until the commit lands, its replay conflicts, revalidation rejects it, or the target cannot take it.
Each replay rebuilds the prepared commit from the preparation base onto the target that won, so every replay starts from the same prepared change. After `landing.reserveAfterLostRaces` lost races the transaction takes the landing reservation before it replays (`commit-landing-reservation.ts`), and every landing attempt yields to another transaction's live reservation; the reservation is released when the loop ends.
@module */import { tagged, } from '@monochromatic-dev/module-logger/ts';import { landingFindingResult, } from './commit-landing-findings.ts';import type { LandingCandidate, LandingLoopInput, LandingLoopOutcome,} from './commit-landing-loop-types.ts';import { replayOrFail, } from './commit-landing-replay-step.ts';import { openLandingReservation, } from './commit-landing-reservation.ts';import { type LandingOutcome, landTransaction,} from './commit-landing.ts';import { reachTransactionPhase, } from './commit-transaction-test-phase.ts';import { replayPlumbingAvailable, } from './replay-plumbing.ts';import { appendEvents, createLandingRaceLostEvent, createLandingReservedEvent,} from './events-concurrency.ts';
export type { LandingLoopInput, LandingLoopOutcome,} from './commit-landing-loop-types.ts';
/** Module logger. */const l = tagged({ tag: 'cli-git', },);
/** Result for a landing outcome that landed nothing and cannot replay.
@param input - loop input
@param candidate - candidate whose landing failed
@param outcome - failed outcome
@param replayPlumbingMissing - whether a replayable commit failed only because Git cannot replay
@returns blocking result */function unreplayableResult({ input, candidate, outcome, replayPlumbingMissing = false,}: Readonly<{ input: LandingLoopInput; candidate: LandingCandidate; outcome: Exclude<LandingOutcome, Readonly<{ kind: 'landed'; }>>; replayPlumbingMissing?: boolean;}>,): LandingLoopOutcome { return { kind: 'failed', result: landingFindingResult({ pass: { ...input.settled, events: appendEvents({ events: input.settled .events, appended: candidate.events, },), }, outcome, capture: input.context .capture, preparedOid: input.prepared .oid, replayPlumbingMissing, },), };}
/** Lands a prepared commit, replaying and revalidating after every lost race.
@param input - loop input
@returns landed commit or blocking result
@throws {@link NativeCommitFailedError} when a re-run `pre-commit` rejects a replayed tree
@example ```ts await landWithReplay({ context, mode: 'explicit-path', prepared, settled, options, committedPaths, addedPaths, worktreeRecords, hookChanges, indexLockTimeoutMs: 1_000, reserveAfterLostRaces: 1 }); ``` */export async function landWithReplay(input: LandingLoopInput,): Promise<LandingLoopOutcome> { /** Tagged loop logger. */ const rl = tagged({ tag: landWithReplay.name, l, },); /** Invocation facts. */ const { context, prepared, } = input; /** Landing reservation, released when the loop ends. */ await using reservation = await openLandingReservation({ registryRoot: context.capture .registryRoot, transactionDirectory: context.workspace .directory, transactionId: context.workspace .transactionId, },); // The candidate is replaced after each replay; it lives only inside the loop. for (let candidate: LandingCandidate = { newOid: prepared.oid, treeOid: prepared.treeOid, expectedOld: context.capture .base, indexPath: context.workspace .commitIndexPath, exactPrivateIndex: true, approvedTree: prepared.treeOid, committedPaths: input.committedPaths, addedPaths: input.addedPaths, worktreeRecords: input.worktreeRecords, hookChanges: input.hookChanges, events: [], attempt: 1, lostRaces: 0, };;) { /** Outcome of this attempt. */ // oxlint-disable-next-line no-await-in-loop -- Each landing attempt follows the replay of the previous lost race. const outcome = await landTransaction({ gitPath: context.gitPath, cwd: context.cwd, capture: context.capture, workspace: context.workspace, mode: input.mode, payload: { operation: 'commit', newOid: candidate.newOid, landedTreeOid: candidate.treeOid, expectedOld: candidate.expectedOld, landedIndexPath: candidate.indexPath, exactPrivateIndex: candidate.exactPrivateIndex, guardedPaths: candidate.hookChanges .paths, }, committedPaths: candidate.committedPaths, addedPaths: candidate.addedPaths, selectedWorktreePaths: candidate.worktreeRecords, indexLockTimeoutMs: input.indexLockTimeoutMs, attempt: candidate.attempt, reservation, },); if (outcome.kind === 'landed') return { kind: 'landed', oid: outcome.oid, events: candidate.events, worktreeRecords: candidate.worktreeRecords, addedPaths: candidate.addedPaths, }; if ((outcome.kind === 'branch-switched') || (!outcome.replayable) || (outcome.current .kind !== 'commit')) return unreplayableResult({ input, candidate, outcome, },); // oxlint-disable-next-line no-await-in-loop -- Probed after a lost race, before any replay work; the answer is cached. if (!await replayPlumbingAvailable({ gitPath: context.gitPath, cwd: context.cwd, commit: outcome.current .oid, },)) { rl.debug('this Git cannot replay; failing fast with head-moved',); return unreplayableResult({ input, candidate, outcome, replayPlumbingMissing: true, },); } /** Lost races including this one. */ const lostRaces = candidate.lostRaces + 1; rl.debug(`lost landing race ${String(lostRaces,)} to ${outcome.current .oid}`,); /** Whether this lost race earned the reservation, granted before the replay. */ // oxlint-disable-next-line no-await-in-loop -- The reservation is taken between ordered landing attempts. const reserved = (lostRaces >= input.reserveAfterLostRaces) && (await reservation.reserve(lostRaces,)); // oxlint-disable-next-line no-await-in-loop -- Test-only marker between ordered landing attempts. await reachTransactionPhase({ phase: 'race-lost', occurrence: lostRaces, },); /** Replayed candidate or final outcome. */ // oxlint-disable-next-line no-await-in-loop -- Replay runs between ordered landing attempts. const next = await replayOrFail({ input, candidate: { ...candidate, events: [ ...candidate.events, createLandingRaceLostEvent({ sequence: 0, attempt: lostRaces, winningOid: outcome.current .oid, },), ...(reserved ? [createLandingReservedEvent({ sequence: 0, lostRaces, },),] : []), ], }, onto: outcome.current .oid, },); if ('kind' in next) return next; candidate = next; }}