/** * Command Execution Contract — 契约层模块。 * * 职责边界: * - 负责:CommandMode 等 契约层职责 * - 不负责:不属于本模块的职责由对应模块承担 * * 被谁调用:契约验证、注册表查询 * 调用谁:privacy_secret_contract、logger * * 数据流:契约定义 → 验证逻辑 → 通过/拒绝结果 * 持久化:读写本地文件(详见代码内路径) */ export type CommandMode = "plan_only" | "dry_run" | "execute"; export type CommandRisk = "read_only" | "local_write" | "dependency_write" | "long_running" | "network" | "database" | "destructive" | "external_side_effect"; export type CommandSource = "project_script" | "project_config" | "user_provided" | "soloforge_generated" | "knowledge_template" | "heuristic"; export type CommandPurpose = "verify" | "build" | "test" | "lint" | "install" | "generate" | "migrate" | "dev_server" | "deploy" | "cleanup" | "diagnostic"; export type Idempotency = "idempotent" | "non_idempotent" | "unknown"; export type ConfirmationRequirement = "auto_allowed" | "workflow_allowed" | "require_human" | "hard_block"; export interface CommandContract { command_id: string; command: string; mode: CommandMode; cwd: string; shell?: string; source: CommandSource; purpose: CommandPurpose; risk: CommandRisk; idempotency: Idempotency; requires_confirmation: ConfirmationRequirement; timeout_ms: number; allowed_env: string[]; expected_outputs: string[]; forbidden_patterns: string[]; } export interface CommandExecutionEvidence { command_id: string; executed_at: string; cwd: string; exit_code: number | null; timed_out: boolean; duration_ms: number; stdout_ref?: string; stderr_ref?: string; redaction_records: string[]; changed_files: string[]; summary: string; } export interface CommandGovernanceFinding { severity: "hard_fail" | "advisory"; rule: string; command_id?: string; message: string; } /** 禁止执行的命令模式列表(hard block) */ export declare const FORBIDDEN_COMMAND_PATTERNS: Array<{ pattern: RegExp; reason: string; }>; /** * 对命令进行风险分类。 * 基于命令内容匹配风险模式,返回最高优先级的风险等级。 * @param command - 命令字符串 * @returns 风险等级 */ export declare function classifyCommandRisk(command: string): CommandRisk; /** * 获取指定风险等级的默认确认要求。 * @param risk - 风险等级 * @returns 确认要求 */ export declare function getDefaultConfirmation(risk: CommandRisk): ConfirmationRequirement; /** * 获取指定用途的默认幂等性。 * @param purpose - 命令用途 * @returns 幂等性 */ export declare function getDefaultIdempotency(purpose: CommandPurpose): Idempotency; /** * 获取指定用途的默认超时时间(毫秒)。 * @param purpose - 命令用途 * @returns 超时时间(毫秒) */ export declare function getDefaultTimeout(purpose: CommandPurpose): number; /** * 判断命令是否为禁止执行的命令。 * @param command - 命令字符串 * @returns 禁止结果及原因 */ export declare function isForbiddenCommand(command: string): { forbidden: boolean; reason: string; }; /** * 创建命令执行契约。 * 自动分类风险等级、确认要求和幂等性。 * @param options.command - 命令字符串 * @param options.cwd - 工作目录 * @param options.source - 命令来源 * @param options.purpose - 命令用途 * @param options.mode - 执行模式(可选,默认 plan_only) * @param options.shell - shell 类型(可选) * @param options.timeout_ms - 超时时间(可选) * @param options.allowed_env - 允许的环境变量(可选) * @returns 命令执行契约实例 */ export declare function createCommandContract(options: { command: string; cwd: string; source: CommandSource; purpose: CommandPurpose; mode?: CommandMode; shell?: string; timeout_ms?: number; allowed_env?: string[]; }): CommandContract; /** * 创建命令执行证据。 * 自动对敏感内容进行脱敏处理。 * @param options.command_id - 命令 ID * @param options.cwd - 工作目录 * @param options.exit_code - 退出码 * @param options.timed_out - 是否超时 * @param options.duration_ms - 执行时长(毫秒) * @param options.stdout_ref - stdout 文件引用(可选) * @param options.stderr_ref - stderr 文件引用(可选) * @param options.stdout_content - 实际 stdout 内容(可选) * @param options.stderr_content - 实际 stderr 内容(可选) * @param options.changed_files - 变更文件列表(可选) * @param options.summary - 执行摘要 * @returns 命令执行证据实例(含自动生成的 executed_at 时间戳) */ export declare function createExecutionEvidence(options: { command_id: string; cwd: string; exit_code: number | null; timed_out: boolean; duration_ms: number; /** 文件路径引用 — 非内容,非脱敏 */ stdout_ref?: string; /** 文件路径引用 — 非内容,非脱敏 */ stderr_ref?: string; /** 实际 stdout 内容 — 已对密钥/PII 脱敏 */ stdout_content?: string; /** 实际 stderr 内容 — 已对密钥/PII 脱敏 */ stderr_content?: string; changed_files?: string[]; summary: string; }): CommandExecutionEvidence; /** * 判断命令是否可以执行。 * 检查禁止模式、执行模式、风险等级和确认要求。 * @param contract - 命令执行契约 * @returns 执行许可结果 */ export declare function canExecute(contract: CommandContract): { allowed: boolean; reason: string; }; /** * 判断命令是否可以自动重试。 * 仅幂等命令失败后才允许自动重试。 * @param contract - 命令执行契约 * @param evidence - 命令执行证据 * @returns 重试许可结果 */ export declare function canAutoRetry(contract: CommandContract, evidence: CommandExecutionEvidence): { allowed: boolean; reason: string; }; /** * 判断用途是否为长进程。 * @param purpose - 命令用途 * @returns 是否为长进程 */ export declare function isLongRunningPurpose(purpose: CommandPurpose): boolean; /** * 判断风险等级是否为长进程。 * @param risk - 风险等级 * @returns 是否为长进程 */ export declare function isLongRunningRisk(risk: CommandRisk): boolean; /** * 验证长进程命令的配置是否合理。 * @param contract - 命令执行契约 * @returns 验证结果和问题列表 */ export declare function validateLongRunning(contract: CommandContract): { valid: boolean; issues: string[]; }; /** * 判断工作目录是否在项目根目录范围内。 * @param cwd - 工作目录 * @param projectRoot - 项目根目录 * @returns 是否允许 */ export declare function isCwdAllowed(cwd: string, projectRoot: string): boolean; /** * 构建命令执行反馈信息。 * @param contract - 命令执行契约 * @returns 格式化的反馈文本 */ export declare function buildCommandFeedback(contract: CommandContract): string; /** * 验证命令执行合规性。 * 检查执行模式、风险确认、证据完整性等治理规则。 * @param options.contracts - 命令执行契约列表 * @param options.evidences - 命令执行证据列表 * @returns 治理发现列表 */ export declare function validateCommandExecution(options: { contracts: CommandContract[]; evidences: CommandExecutionEvidence[]; }): CommandGovernanceFinding[];