Small shared helpers that the rest of src/ imports: fail-fast assertions, abort and timeout plumbing, resource cleanup scopes, a per-key serializer, a line diff, a leveled logger, hashing and path checks. Each file has one narrow job. Only the logger, the package metadata loader, the abort helpers and isDirectExecution touch I/O, timers or process state.
invariant(condition, message),assertString(value, message): assertion functions built onnode:assert/strict.unreachable(value: never, message)throws for exhaustive switches. [assert.ts:invariant] [assert.ts:assertString] [assert.ts:unreachable]makeAbortReason,makeAbortError,throwIfAborted,addAbortListener:AbortErrorconstruction andAbortSignallistener helpers. [abort.ts:makeAbortReason] [abort.ts:makeAbortError] [abort.ts:throwIfAborted] [abort.ts:addAbortListener]createResourceScopedSettlers(scope, resolve, reject)andResourceScopedSettlers: settlers that work once and close the scope before they settle. [abort.ts:createResourceScopedSettlers]waitForScopedOperation(options)andScopedOperationOptions: race an operation against an optional timeout and an optional abort signal. [abort.ts:waitForScopedOperation]ResourceScope(add(name, release),close()),ResourceScopeCloseError,ResourceScopeFailure: ordered cleanup of registered resources. [resourceScope.ts:ResourceScope] [resourceScope.ts:ResourceScopeCloseError]KeyedSerializer<K>withrun(key, operation): runs operations one at a time per key. [keyedSerializer.ts:KeyedSerializer]diffLines(a, b)andLineDiffEntry(op: 'equal' | 'delete' | 'add'): LCS line diff. [lineDiff.ts:diffLines] [lineDiff.ts:LineDiffEntry]Logger,createLogger,createProcessLogger,LOG_LEVEL_VALUES,LogLevel,LogSink,DEFAULT_LOG_LEVEL,isLogLevel,assertLogLevel,resolveLogLevel: leveled logger. [logger.ts:Logger] [logger.ts:createProcessLogger] [logger.ts:resolveLogLevel]loadPackageMetadata()andPackageMetadata:{ name, version }from the rootpackage.json.loadSupportedNodeRange(): itsengines.noderange. [packageMetadata.ts:loadPackageMetadata] [packageMetadata.ts:loadSupportedNodeRange]satisfiesNodeRange(version, range): true when the Node version meets every space-separated>=x[.y[.z]]and<x[.y[.z]]comparator. A prerelease version (x.y.z-tag) satisfies no range, as in npm semver. [nodeRange.ts:satisfiesNodeRange]sha256Hex(text): lowercase SHA-256 hex digest of the UTF-8 text. [hash.ts:sha256Hex]hasErrorCode(error, code): true whenerroris anErrorwith thatcode. [hasErrorCode.ts:hasErrorCode]isDirectExecution(importMetaUrl): true when the calling module is the script thatnodeortsxruns. [isDirectExecution.ts:isDirectExecution]isWithinRoot(rootPath, candidatePath): true when the resolved candidate is the root or below it. [isWithinRoot.ts:isWithinRoot]
invariantandassertStringthrowAssertionErrorfromnode:assert/strict. [assert.ts:invariant] [assert.ts:assertString]addAbortListenerasserts that the signal has not aborted yet. Its doc comment tells callers to checksignal.abortedor callthrowIfAborted()first. [abort.ts:addAbortListener]- Settlers from
createResourceScopedSettlersact once (settledflag). If the scope close fails during a reject, they reject with anAggregateError. [abort.ts:createResourceScopedSettlers] - The doc comment of
waitForScopedOperationstates that the scope closes before the returned promise settles, and thatonAbortruns before scope cleanup. The type requirestimeoutMsandtimeoutResulttogether. [abort.ts:waitForScopedOperation] [abort.ts:ScopedOperationOptions] ResourceScope.addasserts that the scope is open.close()runs releases in reverse order once (closePromise), collects every failure, and throwsResourceScopeCloseErrorif any release failed. [resourceScope.ts:ResourceScope]KeyedSerializer.run: when an earlier operation for the key rejects, later queued operations inherit the rejection without running. The doc comment warns that callingrunwith the same key from inside a queued operation hangs forever. [keyedSerializer.ts:KeyedSerializer]diffLinesaccepts every input size. When the differing middle exceedsMAX_DIFF_CELLS(4,000,000), it falls back to all deletes, then all adds. On a tie it emits the delete first (doc comment). [lineDiff.ts:diffLines]resolveLogLevelreturns'info'forundefinedand failsassertLogLevelfor any value outsideLOG_LEVEL_VALUES. Level ranks are fixed in a frozenLOG_LEVEL_RANK. [logger.ts:resolveLogLevel] [logger.ts:Logger]loadPackageMetadataasserts thatnameandversionare strings.loadSupportedNodeRangeasserts thatengines.nodeis a string. [packageMetadata.ts:loadPackageMetadata] [packageMetadata.ts:loadSupportedNodeRange]satisfiesNodeRangethrows anAssertionErrorfor any other comparator, and for a version that is notx.y.zorx.y.z-tag. [nodeRange.ts:satisfiesNodeRange]
Loggerwrites[agent-tty] <level>: <message>lines to its sink. The default sink isprocess.stderr.write.createProcessLoggerreadsAGENT_TTY_LOG_LEVELfrom the environment. [logger.ts:Logger] [logger.ts:createProcessLogger]loadPackageMetadataandloadSupportedNodeRangereadpackage.jsonwithfs/promises.readFilethrough a URL relative to this module. [packageMetadata.ts:loadPackageMetadata]addAbortListeneradds aoncelistener to the signal and registers its removal in the scope.waitForScopedOperationstarts asetTimeoutand registersclearTimeoutin the scope. [abort.ts:addAbortListener] [abort.ts:waitForScopedOperation]isDirectExecutionreadsprocess.argv[1].sha256Hexusesnode:crypto. [isDirectExecution.ts:isDirectExecution] [hash.ts:sha256Hex]assert.ts,hasErrorCode.ts,isWithinRoot.ts,keyedSerializer.ts,lineDiff.tsandresourceScope.tsdo no I/O.
- Run:
npx vitest run test/unit/util - Run:
npm run typecheck