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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432//region Settled commit transaction/** Completes a converged private-index commit: private native preparation in the shadow repository, serial landing, and post-landing completion.
@module */import { randomUUID, } from 'node:crypto';import { tagged, } from '@monochromatic-dev/module-logger/ts';import type { CandidateFile, } from '../api/policy-types.ts';import { computeHookDispatchPlan, } from '../hook-dispatch/hook-dispatch-plan.ts';import { writeHookShim, } from '../hook-dispatch/hook-shim-writer.ts';import { formatPreparationLease, } from '../hook-dispatch/preparation-lease.ts';import { parseGlobalOptions, } from '../parse-global-options.ts';import type { ConcurrencyConfig, } from '../trust/config-validation-concurrency.ts';import { type AddedPathTracker, settleAddedPathRecords,} from './commit-transaction-added-path-tracker.ts';import { describeHookChanges, } from './commit-hook-changes.ts';import { landWithReplay, } from './commit-landing-loop.ts';import { replayOptions, } from './commit-replay-options.ts';import { installAddedWorktreeFiles, } from './commit-transaction-added-paths.ts';import type { InvocationCapture, } from './commit-transaction-capture.ts';import { writePrivateTree, } from './commit-transaction-index.ts';import { JOURNAL_SCHEMA_VERSION, PREPARED_FILENAME, type TransactionMode, writeJournalRecord,} from './commit-transaction-journal-states.ts';import { completeNoChangeTransaction, NO_CHANGE_NOT_APPLICABLE,} from './commit-transaction-no-change.ts';import { resolvePrivateCommitArgs, } from './commit-transaction-selection.ts';import { selectedWorktreeRecords, } from './commit-transaction-selected-worktree.ts';import { reachTransactionPhase, } from './commit-transaction-test-phase.ts';import type { CommitTransactionPolicyOptions, CommitTransactionResult,} from './commit-transaction-types.ts';import type { CommitTransactionWorkspace, } from './commit-transaction-workspace.ts';import { runAutoMaintenance, } from './commit-landing-auto-maintenance.ts';import { runPostCommitHook, } from './commit-landing-post-commit-hook.ts';import { readPreparedCommit, runNativePreparation, splitCommitInvocation, withoutAllFlag,} from './commit-preparation-native.ts';import { appendEvents, } from './events-concurrency.ts';import { withChangedFixSummary, } from './fix-summary.ts';import type { PolicyEngineResult, } from './types.ts';
/** Module logger. */const l = tagged({ tag: 'cli-git', },);
/** Settled transaction state a conclusion needs. */export type SettledCommitTransaction = Readonly<{ /** Transaction workspace. */ workspace: CommitTransactionWorkspace; /** Invocation capture. */ capture: InvocationCapture; /** Real Git executable. */ gitPath: string; /** Exact wrapper arguments, which revalidation after a replay hands the policies again. */ args: readonly string[]; /** Trusted policy options revalidation re-runs policies with. */ policyOptions: CommitTransactionPolicyOptions; /** Effective repository directory. */ cwd: string; /** Canonical worktree root. */ repositoryRoot: string; /** Git selection mode. */ mode: TransactionMode; /** User explicitly requested amend. */ amend: boolean; /** User explicitly permits an empty commit. */ allowEmpty: boolean; /** Merge or sequencer conclusion must reach Git. */ concludesSequencer: boolean; /** Original parsed selection tokens. */ pathspecs: readonly string[]; /** Interactive or include selection consumed privately. */ readOnlySelection: boolean; /** Whether `-a` was staged privately and must not reach native Git. */ stagedAll: boolean; /** Candidates before any policy correction. */ initialCandidates: readonly CandidateFile[]; /** Paths fixed by core final-newline. */ newlinePaths: ReadonlySet<string>; /** Paths included through the policy-added tracker. */ addedPaths: AddedPathTracker; /** Settled policy engine result. */ pass: PolicyEngineResult; /** Number of converged changed passes. */ changedPasses: number; /** Paths changed by those passes. */ changedPaths: readonly string[]; /** Concurrency tuning. */ concurrency: ConcurrencyConfig; /** Whether this invocation runs under a valid outer preparation lease. */ inheritedLeaseValid: boolean;}>;
/** Completes a settled policy pass through a landed commit or a normalization-only reconciliation.
@param settled - settled transaction state
@returns landed, normalized, or blocked result
@example ```ts await concludeCommitTransaction(settled); ``` */export async function concludeCommitTransaction(settled: SettledCommitTransaction,): Promise<CommitTransactionResult> { /** Tagged conclusion logger. */ const rl = tagged({ tag: concludeCommitTransaction.name, l, },); /** Settled transaction state. */ const { workspace, capture, gitPath, cwd, repositoryRoot, mode, pass, changedPasses, changedPaths, concurrency, } = settled; /** Exact intended tree written from stable private candidate state. */ const intendedTreeOid = await writePrivateTree({ workspace, gitPath, cwd, },); /** Added paths with blobs their worktree copies receive after landing. */ const addedPathRecords = await settleAddedPathRecords({ gitPath, cwd, indexPath: workspace.commitIndexPath, pending: settled.addedPaths .pending(), },); /** Selected corrected files whose worktree equals their original staged blob. */ const selectedWorktreePaths = await selectedWorktreeRecords({ gitPath, cwd, repositoryRoot, indexPath: workspace.commitIndexPath, initialCandidates: settled.initialCandidates, newlinePaths: settled.newlinePaths, },); /** Selected and added paths the commit carries. */ const committedPaths = settled.addedPaths .candidatePaths(); /** Correction can remove every selected difference from the base. */ const noChange = await completeNoChangeTransaction({ workspace, capture, gitPath, cwd, repositoryRoot, mode, eligible: (settled.newlinePaths .size > 0) && (!settled.allowEmpty) && (!settled.amend) && (!settled.concludesSequencer), intendedTreeOid, committedPaths, addedPaths: addedPathRecords, selectedWorktreePaths, pass, changedPasses, changedPaths, indexLockTimeoutMs: concurrency.indexLock .unprovenOwnerTimeoutMs, },); if (noChange !== NO_CHANGE_NOT_APPLICABLE) return noChange; /** Real Git arguments against the complete private intended index. */ const privateArgs = resolvePrivateCommitArgs({ args: pass.args, pathspecs: settled.pathspecs, mode, selectedPrivately: settled.readOnlySelection, },); /** Kept global options and the commit region. */ const invocation = splitCommitInvocation({ args: privateArgs, subcommandIndex: parseGlobalOptions(privateArgs,) .subcommandIndex, },); /** Dispatch plan shared by the preparation shim and the post-landing `post-commit`. */ const plan = await computeHookDispatchPlan({ gitPath, cwd, globalArgs: invocation.globalArgs, commonDir: capture.commonDir, worktreeRoot: repositoryRoot, lease: formatPreparationLease({ directory: workspace.directory, token: randomUUID(), },), concurrentCommits: concurrency.hooks .concurrentCommits, inheritedLeaseValid: settled.inheritedLeaseValid, environment: process.env, },); await writeHookShim({ hooksDirectory: workspace.hooksDirectory, plan, nodePath: process.execPath, },); await runNativePreparation({ gitPath, cwd, globalArgs: invocation.globalArgs, commitArgs: settled.stagedAll ? withoutAllFlag(invocation.commitArgs,) : invocation.commitArgs, shadowPath: workspace.shadowPath, worktreeRoot: repositoryRoot, hooksDirectory: workspace.hooksDirectory, commitIndexPath: workspace.commitIndexPath, },); /** Prepared commit in the shadow. */ const prepared = await readPreparedCommit({ gitPath, shadowPath: workspace.shadowPath, },); /** What preparation hooks staged; the commit keeps it, as native Git does. */ const hookChanges = await describeHookChanges({ gitPath, cwd, objectDirectory: workspace.objectDirectory, fromTree: intendedTreeOid, toTree: prepared.treeOid, },); rl.debug(`prepared ${prepared.oid}; hooks changed ${String(hookChanges.paths .length,)} paths`,); /** Worktree completions: selected corrections, then hook changes. */ const worktreeRecords = [ ...selectedWorktreePaths, ...hookChanges.worktreeRecords, ]; await writeJournalRecord({ directory: workspace.directory, filename: PREPARED_FILENAME, record: { schemaVersion: JOURNAL_SCHEMA_VERSION, state: 'prepared', shadowPath: workspace.shadowPath, preparedOid: prepared.oid, signed: prepared.signed, intendedTreeOid, committedPaths, addedPaths: addedPathRecords, selectedWorktreePaths: worktreeRecords, }, },); await reachTransactionPhase({ phase: 'preparation-done', },); /** Settled preparation pass with its fix summary. */ const settledResult = withChangedFixSummary({ result: pass, trigger: 'pre-forward', passes: changedPasses, changedPaths, },); /** Replay-relevant options native preparation ran with. */ const options = replayOptions(invocation.commitArgs,); /** Landing, with replay and revalidation after every lost race. */ const outcome = await landWithReplay({ context: { gitPath, cwd, capture, workspace, args: settled.args, policyOptions: settled.policyOptions, readOnlySelection: settled.readOnlySelection, repositoryRoot, globalArgs: invocation.globalArgs, noVerify: options.noVerify, }, mode, prepared, settled: settledResult, options, committedPaths, addedPaths: addedPathRecords, worktreeRecords, hookChanges, indexLockTimeoutMs: concurrency.indexLock .unprovenOwnerTimeoutMs, reserveAfterLostRaces: concurrency.landing .reserveAfterLostRaces, },); if (outcome.kind === 'failed') return { policyResult: outcome.result, committed: false, }; // Before cleanup, so an interruption here leaves the journal for startup recovery to finish the worktree copies. await installAddedWorktreeFiles({ gitPath, cwd, repositoryRoot, records: [ ...outcome.addedPaths, ...outcome.worktreeRecords, ], objectDirectory: workspace.objectDirectory, },); workspace.finishTransaction(); await workspace[Symbol.asyncDispose](); // Native order: automatic maintenance, then post-commit; it bounds the pack count each landing grows. await runAutoMaintenance({ gitPath, cwd: repositoryRoot, globalArgs: invocation.globalArgs, },); await runPostCommitHook({ gitPath, cwd: repositoryRoot, globalArgs: invocation.globalArgs, realIndexPath: capture.realIndexPath, landedOid: outcome.oid, plan, },); return { policyResult: { ...settledResult, events: appendEvents({ events: settledResult.events, appended: outcome.events, },), }, committed: true, landedOid: outcome.oid, };}//endregion Settled commit transaction