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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545/** Runtime-authoritative loaded configuration validation. @module */import * as v from 'valibot';import type { PolicySeverity, PolicyTrigger,} from '../api/policy-types.ts';import { BUILT_IN_POLICIES, } from '../policy-engine/built-ins.ts';import type { RuntimePolicyDefinition, } from '../policy-engine/types.ts';import { CONCURRENCY_CONFIG_KEYS, type ConcurrencyConfig, validateConcurrencyConfig,} from './config-validation-concurrency.ts';import { ConfigValidationError, } from './config-validation-error.ts';import { INPUTS_UNDECLARED, resolvePolicyInputs, validateInputsDeclaration,} from './policy-inputs-schema.ts';
export { ConfigValidationError, } from './config-validation-error.ts';
/** Prepared runtime policy configuration. */export type ValidatedConfig = Readonly<{ /** Whether config requests recursive authority. */ recursiveChildren: boolean; /** Built-ins followed by namespaced plugins, each enabled policy carrying its resolved inputs. */ registeredPolicies: readonly RuntimePolicyDefinition[]; /** Effective severity values for every registered policy. */ policySeverities: Readonly<Record<string, PolicySeverity>>; /** Runtime-parsed policy option outputs. */ policyOptions: ReadonlyMap<string, unknown>; /** Concurrent-commit tuning with defaults applied. */ concurrency: ConcurrencyConfig;}>;
/** Allowed lifecycle trigger values. */const POLICY_TRIGGERS: ReadonlySet<string> = new Set([ 'pre-forward', 'post-commit', 'manual-push', 'direct-check', 'direct-fix',]);/** Allowed severity values. */const POLICY_SEVERITIES: ReadonlySet<string> = new Set([ 'off', 'warn', 'error',]);/** Allowed top-level keys. */const CONFIG_KEYS: ReadonlySet<string> = new Set([ 'plugins', 'policies', 'trust', ...CONCURRENCY_CONFIG_KEYS,]);
/** Asserts ordinary non-array object. @param value - unknown candidate @returns Nothing after narrowing candidate @throws ConfigValidationError when candidate is not an object record */function assertRecord(value: unknown,): asserts value is Record<string, unknown> { if (((typeof value) !== 'object') || (value === null) || Array.isArray(value,)) throw new ConfigValidationError('Configuration value must be an object.',);}
/** Checks one ASCII kebab identifier character. @param character - one UTF-16 code unit @returns whether character is lowercase ASCII, decimal, or hyphen */function isKebabCharacter(character: string,): boolean { return ((character >= 'a') && (character <= 'z')) || ((character >= '0') && (character <= '9')) || (character === '-');}
/** Validates kebab-case identifier without syntax-boundary regular expressions. @param value - unknown identifier @returns whether value is non-empty lower kebab case */function isKebabIdentifier(value: unknown,): value is string { if (((typeof value) !== 'string') || (value.length === 0) || value.startsWith('-',) || value.endsWith('-',) || value.includes('/',)) return false; for (let index = 0; index < value.length; index += 1) { if ((!isKebabCharacter(value.charAt(index,),)) || ((value.charAt(index,) === '-') && (value.charAt(index - 1,) === '-'))) return false; } return true;}
/** Validates known string as kebab identifier. @param value - string candidate @returns whether value is valid lower kebab case */function isKebabString(value: string,): boolean { return isKebabIdentifier(value,);}
/** Checks policy severity. @param value - unknown candidate @returns whether value is supported severity */function isPolicySeverity(value: unknown,): value is PolicySeverity { return ((typeof value) === 'string') && POLICY_SEVERITIES.has(value,);}
/** Checks lifecycle trigger. @param value - unknown candidate @returns whether value is supported trigger */function isPolicyTrigger(value: unknown,): value is PolicyTrigger { return ((typeof value) === 'string') && POLICY_TRIGGERS.has(value,);}
/** Checks unknown array without leaking `any` from Array.isArray. @param value - unknown candidate @returns whether candidate is an array of unknown values */function isUnknownArray(value: unknown,): value is readonly unknown[] { return Array.isArray(value,);}
/** Asserts unknown array without leaking `any` from Array.isArray. @param value - unknown candidate @returns Nothing after narrowing candidate */function assertUnknownArray(value: unknown,): asserts value is readonly unknown[] { if (!isUnknownArray(value,)) throw new ConfigValidationError('Expected an array.',);}
/** Asserts policy check callback. @param value - unknown callback @returns Nothing after narrowing callback */function assertPolicyCheck( value: unknown,): asserts value is RuntimePolicyDefinition['check'] { if ((typeof value) !== 'function') throw new ConfigValidationError('Policy must provide a check function.',);}
/** Asserts Valibot-compatible schema runtime protocol. @param value - unknown options schema @returns Nothing after narrowing schema @throws ConfigValidationError when protocol is unavailable */function assertSchema(value: unknown,): asserts value is v.GenericSchema<unknown, unknown> { assertRecord(value,); if ((!('~run' in value)) || ((typeof value['~run']) !== 'function')) throw new ConfigValidationError('Policy options must use a Valibot schema.',);}
/** Validates policy declaration and assigns effective ID. @param value - unknown policy declaration @param effectiveId - complete engine policy ID @returns copied runtime policy definition */function validatePolicy({ value, effectiveId,}: Readonly<{ value: unknown; effectiveId: string;}>,): RuntimePolicyDefinition { assertRecord(value,); if (!isKebabIdentifier(value.name,)) throw new ConfigValidationError(`Policy ${effectiveId} has an invalid name.`,); if ((typeof value.warnSafe) !== 'boolean') throw new ConfigValidationError(`Policy ${effectiveId} must declare warnSafe.`,); if (!isPolicySeverity(value.defaultSeverity,)) throw new ConfigValidationError(`Policy ${effectiveId} has an invalid default severity.`,); assertUnknownArray(value.triggers,); if (!value.triggers .every(isPolicyTrigger,)) throw new ConfigValidationError(`Policy ${effectiveId} has invalid triggers.`,); assertPolicyCheck(value.check,); if (value.options !== undefined) assertSchema(value.options,); /** Validated static inputs or function, or the undeclared sentinel for the unrestricted default. */ const inputs = validateInputsDeclaration({ value: value.inputs, effectiveId, },);
return { name: effectiveId, defaultSeverity: value.defaultSeverity, warnSafe: value.warnSafe, triggers: value.triggers .filter(isPolicyTrigger,), ...(value.options === undefined ? {} : { options: value.options, }), ...(inputs === INPUTS_UNDECLARED ? {} : { inputs, }), check: value.check, };}
/** Mutable view at Valibot callback boundary. */type MutablePolicySchema = { -readonly [Key in keyof v.GenericSchema]: v.GenericSchema[Key]};
/** Invokes plugin-defined schema at mutable validation boundary. @param optionsSchema - plugin-defined Valibot schema @param rawOptions - unvalidated plugin option value @mutates optionsSchema - v.safeParse plugin callback may mutate retained schema state @mutates rawOptions - v.safeParse plugin callback may mutate supplied option value @returns Valibot safe parse result @example ```ts parsePolicyOptions({ optionsSchema: v.unknown(), rawOptions: 'value' }); ``` */function parsePolicyOptions({ optionsSchema, rawOptions,}: { optionsSchema: MutablePolicySchema; rawOptions: unknown;},): v.SafeParseResult<MutablePolicySchema> { return v.safeParse( optionsSchema, rawOptions, );}
/** Parses one policy setting and options through declared schema. @param policyName - effective registered policy ID @param defaultSeverity - severity used when setting is absent @param optionsSchema - optional plugin-defined Valibot schema @param setting - explicit setting or declaration default @mutates optionsSchema - v.safeParse plugin callback may mutate retained schema state @mutates setting - v.safeParse plugin callback may mutate supplied option value @returns active severity and parsed options */function parsePolicySetting({ policyName, defaultSeverity, optionsSchema, setting,}: { policyName: string; defaultSeverity: PolicySeverity; optionsSchema?: MutablePolicySchema; setting: unknown;},): Readonly<{ severity: PolicySeverity; options: unknown}> { /** Effective explicit setting or declaration default. */ const effectiveSetting = setting ?? defaultSeverity; /** Whether setting uses severity-plus-options tuple. */ const tupleSetting = isUnknownArray(effectiveSetting,); /** Safely narrowed tuple values. */ const settingValues: readonly unknown[] = tupleSetting ? effectiveSetting : []; /** Candidate severity value. */ const severity: unknown = tupleSetting ? settingValues[0] : effectiveSetting; /** Candidate policy options. */ const rawOptions: unknown = tupleSetting ? settingValues[1] : undefined; if (!isPolicySeverity(severity,)) throw new ConfigValidationError(`Policy ${policyName} has an invalid severity.`,); if (tupleSetting && (settingValues.length !== 2)) throw new ConfigValidationError(`Policy ${policyName} setting tuple must contain severity and options.`,); if (optionsSchema === undefined) { if (tupleSetting) throw new ConfigValidationError(`Policy ${policyName} does not accept options.`,); return { severity, options: undefined, }; } /** Valibot runtime options result. */ const parsed = parsePolicyOptions({ optionsSchema, rawOptions, },); if (!parsed.success) throw new ConfigValidationError(`Policy ${policyName} options failed Valibot validation.`,); return { severity, options: parsed.output, };}
/** Validates imported default export and prepares policy runtime values. @param value - imported default export @mutates value - plugin-defined Valibot schemas may mutate their schema or option state @returns validated config plus ordered policy registry and parsed settings @example ```ts validateConfig({ plugins: {}, policies: {} }); ``` */export function validateConfig(value: unknown,): ValidatedConfig { assertRecord(value,); /** First unsupported top-level key. */ const unknownKey = Object.keys(value,) .find(function isUnknownConfigKey(key,) { return !CONFIG_KEYS.has(key,); },); if (unknownKey !== undefined) throw new ConfigValidationError(`Unknown configuration key: ${unknownKey}`,); /** Concurrent-commit tuning, validated before plugins as the config contract orders it. */ const concurrency = validateConcurrencyConfig(value,);
/** Declared plugin namespace map. */ const pluginsValue = value.plugins ?? {}; assertRecord(pluginsValue,); /** Namespaced plugin policies in config insertion and declaration order. */ const pluginPolicies = Object.entries(pluginsValue,) .flatMap(function validatePlugin([ namespace, pluginValue, ],) { if (!isKebabString(namespace,)) throw new ConfigValidationError(`Invalid plugin namespace: ${namespace}`,); assertRecord(pluginValue,); if (!isKebabIdentifier(pluginValue.name,)) throw new ConfigValidationError(`Plugin ${namespace} has an invalid name.`,); if (!isUnknownArray(pluginValue.policies,)) throw new ConfigValidationError(`Plugin ${namespace} policies must be an array.`,); /** Local policy names used for duplicate rejection. */ const localNames = new Set<string>(); return pluginValue.policies .map(function validatePluginPolicy(policyValue,) { assertRecord(policyValue,); if ((!isKebabIdentifier(policyValue.name,)) || localNames.has(policyValue.name,)) throw new ConfigValidationError(`Plugin ${namespace} has an invalid or duplicate policy name.`,); localNames.add(policyValue.name,); return validatePolicy({ value: policyValue, effectiveId: `${namespace}/${policyValue.name}`, },); },); },); /** Fixed built-ins followed by namespaced plugin policies. */ const registeredPolicies: readonly RuntimePolicyDefinition[] = [ ...BUILT_IN_POLICIES, ...pluginPolicies, ];
/** Explicit settings map or empty defaults. */ const settingsValue = value.policies ?? {}; assertRecord(settingsValue,); /** Every effective policy ID. */ const knownIds = new Set(registeredPolicies.map(function policyId(policy,) { return policy.name; },),); /** First setting without registered policy. */ const unknownPolicyId = Object.keys(settingsValue,) .find(function isUnknownPolicy(policyId,) { return !knownIds.has(policyId,); },); if (unknownPolicyId !== undefined) throw new ConfigValidationError(`Unknown policy ID: ${unknownPolicyId}`,);
/** Parsed option outputs by effective ID. */ const policyOptions = new Map<string, unknown>(); /** Effective severity map prepared alongside options. */ const policySeverities: Record<string, PolicySeverity> = {}; for (const policy of registeredPolicies) { /** Parsed setting for current policy. */ const parsed = parsePolicySetting({ policyName: policy.name, defaultSeverity: policy.defaultSeverity, ...(policy.options === undefined ? {} : { optionsSchema: policy.options, }), setting: settingsValue[policy.name], },); policyOptions.set( policy.name, parsed.options, ); policySeverities[policy.name] = parsed.severity; } /** Registry with every enabled policy's inputs resolved after every option parsed. */ const resolvedPolicies = registeredPolicies.map(function resolveInputs(policy,): RuntimePolicyDefinition { return policySeverities[policy.name] === 'off' ? policy : { ...policy, inputs: resolvePolicyInputs({ ...(policy.inputs === undefined ? {} : { declaration: policy.inputs, }), options: policyOptions.get(policy.name,), effectiveId: policy.name, },), }; },);
if (value.trust !== undefined) { assertRecord(value.trust,); /** Keys present in trust declaration. */ const trustKeys = Object.keys(value.trust,); if (trustKeys.some(function isUnknownTrustKey(key,) { return key !== 'children'; },) || ((value.trust .children !== undefined) && ((typeof value.trust .children) !== 'boolean'))) throw new ConfigValidationError('Configuration trust declaration is invalid.',); }
return { recursiveChildren: (value.trust !== undefined) && (value.trust .children === true), registeredPolicies: resolvedPolicies, policySeverities, policyOptions, concurrency, };}