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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317/** Management-command parser and dispatcher. @module */import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';import { MANAGEMENT_HELP, MANAGEMENT_REFUSED, MANAGEMENT_USAGE, parseManagementArgs, TRUST_HELP,} from './management-parser.ts';import { createEngineFailureEvent, renderPolicyEvents,} from './policy-engine/events.ts';import { prepareDirectCheckFacts, } from './policy-engine/direct-check-facts.ts';import { runDirectFix, } from './policy-engine/direct-fix.ts';import { runPolicyEngine, } from './policy-engine/engine.ts';import { TrustedConfigError, } from './trust/config-loader.ts';import { runTrustManagement, } from './trust/management-runtime.ts';import { resolveRuntimeConfig, RUNTIME_CONFIG_ABSENT,} from './trust/runtime-config.ts';
/** Detects positional input before explicit pathspec separator. @param args - complete management arguments @returns whether non-option input appears before `--` */function hasPreSeparatorPositional(args: readonly string[],): boolean { /** Tokens before pathspec separator, excluding command name. */ const separatorIndex = args.indexOf('--',); /** Option region that cannot contain direct-check paths. */ const optionRegion = args.slice( 1, separatorIndex === (-1) ? args.length : separatorIndex, ); /** Parser state for separated `--policy <id>` values. */ const state = optionRegion.reduce( function inspectOptionRegion( current, token, ): Readonly<{ expectsPolicyValue: boolean; positionalFound: boolean }> { if (current.positionalFound) return current; if (current.expectsPolicyValue) { return { expectsPolicyValue: false, positionalFound: false, }; } if (token === '--policy') { return { expectsPolicyValue: true, positionalFound: false, }; } return { expectsPolicyValue: false, positionalFound: (token === '-') || (!token.startsWith('-',)), }; }, { expectsPolicyValue: false, positionalFound: false, }, ); return state.positionalFound;}
/** Direct-check runtime configuration resolution. */type DirectRuntimeResolution = | Readonly<{ loaded: Awaited<ReturnType<typeof resolveRuntimeConfig>>; }> | Readonly<{ error: unknown; }>;
/** Loads trusted direct-check config while retaining stable failure output. @param gitGlobalArgs - global options determining repository @param registryRoot - internal private registry root @returns loaded config or captured failure */async function resolveDirectRuntime({ gitGlobalArgs, registryRoot,}: Readonly<{ gitGlobalArgs: readonly string[]; registryRoot?: string;}>,): Promise<DirectRuntimeResolution> { try { return { loaded: await resolveRuntimeConfig({ args: gitGlobalArgs, forceLoad: true, ...(registryRoot === undefined ? {} : { registryRoot, }), },), }; } catch (error: unknown) { return { error, }; }}
/** Parses and runs one namespaced management command. @param args - arguments following `git cli-git` @param gitGlobalArgs - arguments preceding `cli-git` @param registryRoot - internal complete test registry root @returns settled cli-git exit code @example ```ts await runManagementCommand({ args: ['status'], gitGlobalArgs: [] }); ``` */export async function runManagementCommand({ args, gitGlobalArgs, registryRoot,}: Readonly<{ args: readonly string[]; gitGlobalArgs: readonly string[]; registryRoot?: string;}>,): Promise<0 | 1 | 2> { /** Resolved management action, or refusal. */ const parsed = parseManagementArgs(args,); if (parsed === MANAGEMENT_REFUSED) { console.error(MANAGEMENT_USAGE,); return 2; } if (parsed.command === 'help') { console.log(parsed.topic === 'trust' ? TRUST_HELP : MANAGEMENT_HELP,); return 0; }
if ((parsed.command === 'trust') || (parsed.command === 'untrust') || (parsed.command === 'status')) { return await runTrustManagement({ action: { command: parsed.command, ...(('yes' in parsed) && parsed.yes ? { yes: true, } : {}), }, gitGlobalArgs, ...(registryRoot === undefined ? {} : { registryRoot, }), },); }
if (hasPreSeparatorPositional(args,)) { console.error(`git cli-git ${parsed.command} pathspecs must follow --.`,); return 2; } /** Position of required pathspec separator. */ const separatorIndex = args.indexOf('--',); /** Whether invocation selected non-empty pathspec scope. */ const hasPathspecScope = (separatorIndex !== (-1)) && (parsed.pathspecs .length > 0); /** Whether invocation selected complete repository scope. */ const hasAllScope = parsed.all; if (hasAllScope === hasPathspecScope) { console.error(`git cli-git ${parsed.command} requires exactly one scope: --all or non-empty pathspecs after --.`,); return 2; } if ((separatorIndex === (-1)) && (parsed.pathspecs .length > 0)) { console.error(`git cli-git ${parsed.command} pathspecs must follow --.`,); return 2; }
/** Deduplicated direct-check filter preserving first occurrence order. */ const selectedPolicyIds = parsed.policies; /** Trusted direct-check config resolution. */ const runtimeResolution = await resolveDirectRuntime({ gitGlobalArgs, ...(registryRoot === undefined ? {} : { registryRoot, }), },); if ('error' in runtimeResolution) { /** Stable direct trust failure code. */ const code = runtimeResolution.error instanceof TrustedConfigError ? runtimeResolution.error .code : 'trust-failed'; process.stdout .write(renderPolicyEvents([createEngineFailureEvent({ sequence: 0, code, message: caughtValueText(runtimeResolution.error,), },),],),); return 2; } /** Loaded config or no-config sentinel. */ const runtimeConfig = runtimeResolution.loaded; /** Concrete direct-check Git pathspec scope. */ const directPathspecs: readonly string[] = hasAllScope ? [':/',] : parsed.pathspecs; if (parsed.command === 'fix') { /** Converged direct-fix operation. */ const fixed = await runDirectFix({ gitGlobalArgs, pathspecs: directPathspecs, policyOptions: { selectedPolicyIds, ...(runtimeConfig === RUNTIME_CONFIG_ABSENT ? {} : { config: { policies: runtimeConfig.validated .policySeverities, }, registeredPolicies: runtimeConfig.validated .registeredPolicies, policyOptions: runtimeConfig.validated .policyOptions, }), }, },); /** Stable direct-fix JSONL. */ const renderedEvents = renderPolicyEvents(fixed.policyResult .events,); if (renderedEvents !== '') process.stdout .write(renderedEvents,); return fixed.policyResult .exitCode; } /** Exact private worktree/index projection or stable setup failure. */ const directFacts = await prepareDirectCheckFacts({ args, gitGlobalArgs, pathspecs: directPathspecs, },); if (directFacts.kind === 'failed') { process.stdout .write(renderPolicyEvents(directFacts.result .events,)); return directFacts.result .exitCode; } /** Scope-bound exact direct-check candidate facts. */ await using scopedDirectFacts = directFacts.scope; /** Built-in and trusted-plugin direct-check decision. */ const result = await runPolicyEngine({ args: gitGlobalArgs, trigger: 'direct-check', selectedPolicyIds, gitFacts: scopedDirectFacts.gitFacts, repositoryRoot: scopedDirectFacts.repositoryRoot, ...(runtimeConfig === RUNTIME_CONFIG_ABSENT ? {} : { config: { policies: runtimeConfig.validated .policySeverities, }, registeredPolicies: runtimeConfig.validated .registeredPolicies, policyOptions: runtimeConfig.validated .policyOptions, }), },); /** Stable direct-command JSONL. */ const renderedEvents = renderPolicyEvents(result.events,); if (renderedEvents !== '') process.stdout .write(renderedEvents,); return result.exitCode;}