// `pdfnative govern ` — expose pdfnative's AI // governance / Human-in-the-Loop (HITL) contract to agents driving the CLI. // // govern rules Print the human/agent protocol (AGENT_RULES). // govern policy Print the machine-readable policy (JSON). // govern verify-issue Validate an issue/PR draft against the policy. // // This makes the pdfnative 1.5.0 governance model actionable from any agent // pipeline: draft locally, verify, then let a HUMAN review and submit. import { type ParsedArgs, getStringFlag, hasFlag } from '../utils/args.js'; import { readFileOrStdin, assertJsonSizeLimit } from '../utils/io.js'; import { CliError, ErrorCode } from '../utils/error.js'; import { isJsonMode } from '../utils/agent.js'; import { serializeJson } from '../utils/projection.js'; import { AI_GOVERNANCE_POLICY, AGENT_RULES_TEXT, validateGovernanceDraft, type GovernanceValidation, } from '../utils/governance.js'; const SUBCOMMANDS = ['rules', 'policy', 'verify-issue']; function printPolicy(pretty: boolean): void { process.stdout.write(serializeJson(AI_GOVERNANCE_POLICY, pretty) + '\n'); } async function verifyIssue(args: ParsedArgs): Promise { const draftPath = args.positionals[1] ?? getStringFlag(args.flags, 'input', 'i'); if (draftPath === undefined) { throw new CliError('Usage: pdfnative govern verify-issue ', 2); } const buf = await readFileOrStdin(draftPath); assertJsonSizeLimit(buf); const result: GovernanceValidation = validateGovernanceDraft(buf.toString('utf8')); const jsonOut = isJsonMode() || getStringFlag(args.flags, 'format', 'f') === 'json'; if (jsonOut) { const pretty = hasFlag(args.flags, 'pretty') || !isJsonMode(); process.stdout.write(serializeJson(result, pretty) + '\n'); } else { for (const w of result.warnings) process.stderr.write(`warning: ${w}\n`); if (result.ok) { process.stdout.write( 'Draft validation PASSED. Reminder: this draft must be reviewed and ' + 'submitted by a HUMAN under their own GitHub identity.\n', ); } else { for (const e of result.errors) process.stderr.write(`error: ${e}\n`); } } if (!result.ok) { // Non-zero exit so pipelines can gate on the verdict. In agent mode the // error envelope (stderr) carries the detail; the report is on stdout. const detail = result.errors.join(' '); throw new CliError(jsonOut ? detail : '', 1, ErrorCode.POLICY); } } export async function govern(args: ParsedArgs): Promise { const sub = args.positionals[0]; if (sub === undefined) { throw new CliError(`Usage: pdfnative govern <${SUBCOMMANDS.join('|')}>`, 2); } switch (sub) { case 'rules': process.stdout.write(AGENT_RULES_TEXT); return; case 'policy': printPolicy(hasFlag(args.flags, 'pretty') || !isJsonMode()); return; case 'verify-issue': await verifyIssue(args); return; default: throw new CliError( `Unknown govern subcommand "${sub}". Valid: ${SUBCOMMANDS.join(', ')}.`, 2, ); } }