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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295/** Trust management command runtime. @module */import { createInterface, } from 'node:readline/promises';import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';import { tagged, } from '@monochromatic-dev/module-logger/ts';import { createEngineFailureEvent, type EngineFailureCode, renderPolicyEvents,} from '../policy-engine/events.ts';import { resolveAccountRegistryRoot, } from './account-root.ts';import { CONFIG_ABSENT, discoverConfig, resolveConfigRepositoryRoot,} from './config-discovery.ts';import { inspectTrust, trustConfig, untrustConfig, untrustRepository,} from './trust-service.ts';import { TrustedConfigError, } from './config-loader.ts';import type { TrustConsentOutcome, } from './types.ts';
/** Logger root for trust management boundaries. @example ```ts const rl = tagged({ tag: someFunction.name, l, }); ``` */const l = tagged({ tag: 'cli-git', },);
/** Trust management action parsed by management argv parser. */export type TrustManagementAction = Readonly<{ /** Action discriminator. */ command: 'status' | 'trust' | 'untrust'; /** Explicit noninteractive consent for trust only. */ yes?: true;}>;
/** Trust management runtime options. */export type TrustManagementOptions = Readonly<{ /** Parsed action. */ action: TrustManagementAction; /** Git global options before `cli-git`. */ gitGlobalArgs: readonly string[]; /** Internal complete test registry root. */ registryRoot?: string;}>;
/** Writes trust disclosure to stderr. @param text - complete human-readable disclosure */function discloseTrust(text: string,): void { console.error(text,);}
/** Supplies current audit time. @returns current Date */function currentTime(): Date { return new Date();}
/** Emits one trust-management failure on stdout JSONL. @param code - stable engine failure code @param message - human-readable failure detail @returns engine failure exit code */function emitTrustFailure({ code, message,}: Readonly<{ code: EngineFailureCode; message: string;}>,): 2 { process.stdout .write(renderPolicyEvents([createEngineFailureEvent({ sequence: 0, code, message, },),],),); return 2;}
/** Requests explicit interactive consent without auto-trusting CI. @returns explicit prompt outcome, including unavailable terminal streams @throws unexpected readline failure that is not terminal EOF */async function promptForTrust(): Promise<TrustConsentOutcome> { /** Function-tagged prompt lifecycle logger. */ const rl = tagged({ tag: promptForTrust.name, l, },); if ((!process.stdin .isTTY) || (!process.stderr .isTTY)) return 'unavailable'; /** Disposable readline prompt bound to terminal streams. */ using prompt = createInterface({ input: process.stdin, output: process.stderr, },); try { /** Exact interactive response. */ const answer = await prompt.question('Type yes to trust this exact snapshot: ',); return answer === 'yes' ? 'approved' : 'declined'; } catch (error: unknown) { if (Error.isError(error,) && (error.name === 'AbortError') && ('code' in error) && (error.code === 'ABORT_ERR')) { rl.debug(`interactive trust prompt ended without input: ${String(error,)}`,); return 'unavailable'; } throw error; }}
/** Runs trust, untrust, or status without executing live configuration in preflight. @param action - parsed management action @param gitGlobalArgs - Git global options determining effective repository @param registryRoot - internal test override unavailable from environment or config @returns settled cli-git exit code @example ```ts await runTrustManagement({ action: { command: 'status' }, gitGlobalArgs: [], registryRoot }); ``` */export async function runTrustManagement({ action, gitGlobalArgs, registryRoot,}: TrustManagementOptions,): Promise<0 | 2> { try { /** Canonical config discovered without execution. */ const discovered = await discoverConfig(gitGlobalArgs,); if (discovered === CONFIG_ABSENT) { if (action.command === 'status') { console.log(JSON.stringify({ schemaVersion: 1, type: 'trust-status', configPresent: false, trusted: false, unchanged: false, reason: 'no-config', },),); return 0; } if (action.command === 'untrust') { /** Canonical repository root retained after config deletion. */ const repositoryRoot = await resolveConfigRepositoryRoot(gitGlobalArgs,); if (repositoryRoot === CONFIG_ABSENT) return emitTrustFailure({ code: 'trust-failed', message: 'No repository was found for trust recovery.', },); /** Injected test root or OS-account-derived production root. */ const recoveredRegistryRoot = registryRoot ?? await resolveAccountRegistryRoot(); /** Recovered recursive revocation summary. */ const result = await untrustRepository({ repositoryRoot, registryRoot: recoveredRegistryRoot, disclose: discloseTrust, },); console.log(JSON.stringify({ schemaVersion: 1, type: 'untrust-summary', configPath: null, removed: result.removed, affectedRoots: result.affectedRoots, },),); return 0; } return emitTrustFailure({ code: 'trust-failed', message: 'No repository configuration was found at the canonical repository root.', },); } /** Injected test root or OS-account-derived production root. */ const effectiveRegistryRoot = registryRoot ?? await resolveAccountRegistryRoot(); if (action.command === 'status') { console.log(JSON.stringify({ schemaVersion: 1, type: 'trust-status', ...await inspectTrust({ discovered, registryRoot: effectiveRegistryRoot, },), },),); return 0; } if (action.command === 'untrust') { /** Recursive revocation summary after pre-mutation disclosure. */ const result = await untrustConfig({ discovered, registryRoot: effectiveRegistryRoot, disclose: discloseTrust, },); console.log(JSON.stringify({ schemaVersion: 1, type: 'untrust-summary', configPath: discovered.configPath, removed: result.removed, affectedRoots: result.affectedRoots, },),); return 0; } await trustConfig({ discovered, registryRoot: effectiveRegistryRoot, yes: action.yes === true, adapters: { disclose: discloseTrust, prompt: promptForTrust, now: currentTime, }, },); console.log(JSON.stringify({ schemaVersion: 1, type: 'trust-summary', configPath: discovered.configPath, trusted: true, },),); return 0; } catch (error: unknown) { return emitTrustFailure({ code: error instanceof TrustedConfigError ? error.code : 'trust-failed', message: caughtValueText(error,), },); }}