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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378import { mergeAsArrived, } from './async-queue.ts';import { buildEstimates, buildSnapshot, computePending, type Signals,} from './assemble.ts';import { combineEstimates, } from './combine.ts';import { METRIC_DEFAULT, METRIC_DEFAULT_BRANCH_ONLY, MS_PER_SECOND, SCOPE,} from './constants.ts';import { localExact, } from './local-exact.ts';import { applySignal, churnSignal, commitSignal, deepenSignal, refsSignal, shallowSignal, storageSignal, tree0Signal,} from './probe-tasks.ts';import { cloneShallow, } from './probe-shallow.ts';import type { LocalSource, RemoteSource, Source,} from './source.ts';import { makeTempDir, } from './temp-dir.ts';import type { EstimateSnapshot, } from './types.ts';
/** Runtime options controlling the estimate, all with defaults supplied by the CLI layer (no required knob; streaming plus SIGINT remain the primary control). */export type EstimateOptions = { readonly defaultBranchOnly: boolean; readonly maxProbeSeconds: number; readonly maxDeepenCommits: number; readonly maxPackBytes: number; readonly signal?: AbortSignal;};
/** Selects the metric contract line for the active mode. @param defaultBranchOnly - whether both sides are restricted to the default branch @returns the metric contract string @example ```ts metricOf({ defaultBranchOnly: false }); // default-metric line ``` */function metricOf({ defaultBranchOnly, }: { readonly defaultBranchOnly: boolean; },): string { return defaultBranchOnly ? METRIC_DEFAULT_BRANCH_ONLY : METRIC_DEFAULT;}
/** Builds one snapshot from the current signals: rebuild estimators, fuse, and assemble. `pending` is empty on the final snapshot. @param signals - current accumulated signals @param source - the source being estimated @param metric - metric contract line @param defaultBranchOnly - whether branch correction is skipped @param done - whether this is the final snapshot @returns one snapshot ready to serialize @example ```ts snapshotOf({ signals: {}, source, metric, defaultBranchOnly: false, done: false }); ``` */function snapshotOf( { signals, source, metric, defaultBranchOnly, done, }: { readonly signals: Signals; readonly source: Source; readonly metric: string; readonly defaultBranchOnly: boolean; readonly done: boolean; },): EstimateSnapshot { /** Fused belief over the current estimators. */ const fused = combineEstimates({ estimates: buildEstimates({ signals, defaultBranchOnly, },), },); return buildSnapshot({ done, fused, metric, pending: done ? [] : computePending({ signals, source, },), scope: SCOPE, ...signals.shallowBytes === undefined ? {} : { shallowBytes: signals.shallowBytes, }, },);}
/** Estimates a complete local repository exactly: a coarse prior snapshot first, then the exact pack-objects measurement as the final, very-high-confidence snapshot. @param source - local repository source @param options - runtime options @returns async generator of snapshots */async function* estimateLocal( { source, options, }: { readonly source: LocalSource; readonly options: EstimateOptions },): AsyncGenerator<EstimateSnapshot> { /** Metric contract line for this run. */ const metric = metricOf({ defaultBranchOnly: options.defaultBranchOnly, },); /** Mutable container holding the immutable signal accumulator. */ const state: { signals: Signals; } = { signals: {}, }; yield snapshotOf({ signals: state.signals, source, metric, defaultBranchOnly: options.defaultBranchOnly, done: false, },);
/** Exact (or size-pack proxy) local measurement. Byte fields are omitted when unmeasurable, so a degenerate repo degrades to the prior instead of recording a fabricated zero or crashing the stream. */ const result = await localExact({ path: source.path, maxPackBytes: options.maxPackBytes, },); state.signals = { ...state.signals, ...result.fullBytes === undefined ? {} : { local: { basis: result.basis, confidence: result.confidence, fullBytes: result.fullBytes, }, }, ...result.shallowBytes === undefined ? {} : { shallowBytes: result.shallowBytes, }, }; yield snapshotOf({ signals: state.signals, source, metric, defaultBranchOnly: options.defaultBranchOnly, done: true, },);}
/** Combines a probe-budget signal with an optional caller cancellation signal. @param budgetSignal - Owned timeout signal for probe budget. @param callerSignal - Optional caller cancellation capability. @returns Budget signal alone or combined dependent signal. @mutates budgetSignal through DOM commit 5796f716 AbortSignal.any dependent-signal relations. @mutates callerSignal through DOM commit 5796f716 AbortSignal.any dependent-signal relations. @example ```ts combineProbeSignals({ budgetSignal: AbortSignal.timeout(1), callerSignal: undefined }); ``` */function combineProbeSignals({ budgetSignal, callerSignal,}: { readonly budgetSignal: AbortSignal; readonly callerSignal?: AbortSignal;},): AbortSignal { return callerSignal === undefined ? budgetSignal : AbortSignal.any([ budgetSignal, callerSignal, ],);}
/** Estimates a remote repository: launches every cheap probe concurrently, folds each signal in as it lands (fastest first), and yields a refined snapshot per arrival, ending with the tightest fused snapshot. Temp clones live in disposable directories removed when the generator finishes or is closed. @param source - remote repository source @param options - runtime options @returns async generator of snapshots */async function* estimateRemote( { source, options, }: { readonly source: RemoteSource; readonly options: EstimateOptions },): AsyncGenerator<EstimateSnapshot> { /** Metric contract line for this run. */ const metric = metricOf({ defaultBranchOnly: options.defaultBranchOnly, },); /** Mutable container holding the immutable signal accumulator. */ const state: { signals: Signals; } = { signals: {}, }; yield snapshotOf({ signals: state.signals, source, metric, defaultBranchOnly: options.defaultBranchOnly, done: false, },);
/** Disposable temp dir for the shallow + deepen clone. */ await using shallowDir = await makeTempDir({ prefix: 'gcs-shallow-', },); /** Disposable temp dir for the tree:0 commit-count clone. */ await using tree0Dir = await makeTempDir({ prefix: 'gcs-tree0-', },); /** Disposable temp dir for the blobless churn clone. */ await using blobDir = await makeTempDir({ prefix: 'gcs-blob-', },);
/** Wall-clock budget signal; aborting kills in-flight clones. */ const budgetSignal = AbortSignal.timeout(options.maxProbeSeconds * MS_PER_SECOND,); /** Effective abort signal: the budget alone, or merged with the caller's signal (SIGINT) so either source can abort the probes. */ const signal = combineProbeSignals({ budgetSignal, ...options.signal === undefined ? {} : { callerSignal: options.signal, }, },); /** Single shallow clone, shared by the shallow and deepen tasks. */ const shallowPromise = cloneShallow({ url: source.url, dest: shallowDir.path, signal, },);
/** All probe tasks, launched concurrently and merged fastest-first. */ const tasks = [ shallowSignal({ shallowPromise, },), deepenSignal({ shallowPromise, maxDeepenCommits: options.maxDeepenCommits, signal, },), refsSignal({ url: source.url, },), storageSignal({ source, },), commitSignal({ source, },), tree0Signal({ url: source.url, dest: tree0Dir.path, signal, },), churnSignal({ url: source.url, dest: blobDir.path, signal, },), ];
for await (const arrived of mergeAsArrived({ tasks, },)) { state.signals = applySignal({ signals: state.signals, signal: arrived, },); yield snapshotOf({ signals: state.signals, source, metric, defaultBranchOnly: options.defaultBranchOnly, done: false, },); } yield snapshotOf({ signals: state.signals, source, metric, defaultBranchOnly: options.defaultBranchOnly, done: true, },);}
/** Streams progressive clone-size estimates for a source. A local complete repo is measured exactly; a remote (or incomplete local repo via its origin) is estimated from cheap probes fused into a tightening range. Yields a snapshot the instant the first signal lands and again on every refinement, never blocking on the slowest probe and never refusing. @param source - resolved local or remote source @param options - runtime options @returns async generator of progressive snapshots @example ```ts for await (const snapshot of estimate({ source, options })) { process.stdout.write(`${JSON.stringify(snapshot)}\n`); } ``` */export async function* estimate( { source, options, }: { readonly source: Source; readonly options: EstimateOptions },): AsyncGenerator<EstimateSnapshot> { if (source.kind === 'local') { yield* estimateLocal({ source, options, },); return; } yield* estimateRemote({ source, options, },);}