Apéndice C: Archivos fuente de hooks y herramientas

[ GUÍA ]Apéndice C

Apéndice C: archivos fuente de hooks y herramientas

El código fuente completo, en versión general, de los cuatro hooks del plugin, la biblioteca compartida de patrones de datos personales, el instalador, la biblioteca y el verificador de Markdown compartidos, la herramienta de proyecto nuevo y la especificación de las herramientas de puesta al día.

El código y el contenido de los archivos quedan en inglés, como en el repositorio público de la guía.

Estos son los programas de esta guía, en versión general. Copialos tal cual y cambiá solo los valores CONFIG. Todos son módulos ES de Node (.mjs) sin dependencias, así que corren sin cambios en macOS y en Linux.

Los caracteres invisibles (los separadores de línea de Unicode, la marca de orden de bytes) se arman con String.fromCharCode en vez de escribirse como escapes \u, porque un escape escrito con las herramientas de archivos de la IA puede llegar como el carácter crudo (ver 10 §3).

Valores de configuración compartidos

Cada programa que necesita rutas las define al principio. Mantenelas idénticas en todos los programas:

ConstanteValor de referenciaSignificado
ROOT_DIRux-workCarpeta dentro de la carpeta personal que guarda todo
CENTRAL_DIR000-centralNombre de la carpeta del repositorio central
PROJECTS_DIR001-projectsNombre de la carpeta de proyectos
WORK_DIR_governance-workInformes y copias de seguridad locales
PLUGINux-governanceNombre del plugin, para los mensajes

hooks/hooks.json

El archivo está en el 14. Claude Code reemplaza ${CLAUDE_PLUGIN_ROOT} por la carpeta del plugin. Los comandos del control de datos personales, del control de la terminal y del control de la carpeta central terminan con || exit 2, así que si falta node o el programa se cae, la llamada queda bloqueada en vez de pasar; el control de versión termina con || true.

hooks/lib/pii.mjs

Creá este archivo en la fase 4 del plan de armado, junto con tools/lib/md.mjs, porque md.mjs importa sus patrones. Los hooks que también lo usan se suman en la fase 6.

// Personal-information patterns shared by the PII guard hook and the central tools.
// Lives inside the plugin so the hook still works when the plugin is installed as a cached copy.
// Lookarounds stop matches inside hex keys and IDs.
// CONFIG: email domains that are not personal information (example domains and your organisation's own).
const ALLOWED_EMAIL_DOMAINS = ['example.com']; // add your organisation's domain, <ORG_EMAIL_DOMAIN>

// CONFIG: replace the phone and identifier patterns with your region's formats (see 17 Adapting).
// The examples below are neutral shapes that run as-is:
//   phone_international: a + and a country code of 1 to 3 digits, then 8 to 12 digits with optional spaces or hyphens
//   phone_national:      a leading 0, then 8 to 11 digits in all (the 0 included) in any grouping of spaces or hyphens;
//                        adjust to your region
//   account_ref:         an organisation identifier made of a fixed prefix (here "ACC-") and 8 digits
export const PII_PATTERNS = {
  email: /(?<![\w.+-])[\w.+-]+@([\w-]+\.)+[A-Za-z]{2,}(?![\w-])/g,
  phone_international: /(?<![\w+])\+\d{1,3}[\s-]?(?:\d[\s-]?){8,12}(?!\w)/g,
  phone_national: /(?<![\w+.])0(?:[\s-]?\d){7,10}(?![\w.])/g,
  account_ref: /(?<!\w)ACC-\d{8}(?!\w)/g,
};

export function findPii(text) {
  const hits = {};
  for (const [kind, re] of Object.entries(PII_PATTERNS)) {
    const found = (text.match(re) || []).filter((v) => kind !== 'email'
      || !ALLOWED_EMAIL_DOMAINS.some((d) => v.toLowerCase().endsWith('@' + d)));
    if (found.length) hits[kind] = found;
  }
  return hits;
}

// First two and last two characters kept; six characters or fewer are fully hidden.
export function mask(value) {
  return value.length <= 6 ? '****' : value.slice(0, 2) + '*'.repeat(value.length - 4) + value.slice(-2);
}

// Replaces every match in a text with its masked form.
export function maskAll(text) {
  let out = String(text);
  for (const [kind, re] of Object.entries(PII_PATTERNS)) {
    out = out.replace(re, (v) => (kind === 'email' && ALLOWED_EMAIL_DOMAINS.some((d) => v.toLowerCase().endsWith('@' + d)) ? v : mask(v)));
  }
  return out;
}

Los patrones de teléfono y de identificador de arriba son ejemplos neutros. Reemplazalos por los formatos exactos de tu región (cuanto más ajustado el patrón, menos falsos positivos) y probalos contra valores inventados, fechas, números de versión y claves hexadecimales largas. El patrón nacional es amplio a propósito: también atrapa otros números de 8 a 11 dígitos que empiezan con cero, como algunos códigos postales con extensión, así que esperá algún falso positivo hasta que lo ajustes. El patrón internacional no necesita cambios en la mayoría de las regiones: atrapa un número escrito con un + adelante, un código de país y de 8 a 12 dígitos más.

hooks/pii-guard.mjs

#!/usr/bin/env node
// PreToolUse guard: blocks outbound tool calls (web, MCP, published pages, subagent prompts) whose input contains
// possible personal information. Exit 2 blocks the call and shows stderr to the AI.
// Fails closed: if the patterns cannot load, the input cannot be read, or anything else goes wrong, the call is blocked.
// Its command in hooks.json ends with `|| exit 2`, so a missing node or a crash blocks too.
const block = (msg) => { process.stderr.write(`Blocked by UX governance PII guard: ${msg}\n`); process.exit(2); };
const BOM = String.fromCharCode(0xfeff);

let lib;
try {
  lib = await import('./lib/pii.mjs');
} catch (e) {
  block(`its pattern library failed to load (${e.message}). Tell the user; the guard fails closed.`);
}

let raw = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', (c) => { raw += c; });
process.stdin.on('end', () => {
  try {
    const input = JSON.parse(raw.startsWith(BOM) ? raw.slice(1) : raw);
    if (!input || typeof input !== 'object') throw new Error('input is not an object');
    // Scan every string value of the input, one per line, plus its URL-decoded form when that differs. Scanning the
    // decoded values (not the JSON text) means a value after a line break or tab is still seen, and %40 in a link is
    // still an @. Adding the decoded form only when it differs keeps the counts in the block message honest.
    const leaves = (v) => (typeof v === 'string' ? [v] : Array.isArray(v) ? v.flatMap(leaves) : v && typeof v === 'object' ? Object.values(v).flatMap(leaves) : []);
    const decode = (s) => { try { return decodeURIComponent(s); } catch { return s; } };
    const kinds = Object.entries(lib.findPii(leaves(input.tool_input ?? {}).flatMap((s) => (decode(s) === s ? [s] : [s, decode(s)])).join('\n')));
    if (!kinds.length) process.exit(0);
    const summary = kinds.map(([k, v]) => `${k} x${v.length} (e.g. ${lib.mask(v[0])})`).join(', ');
    block(`the ${input.tool_name} input contains possible personal information: ${summary}. `
      + 'Customer personal information must not leave its research file (governance/privacy.md). '
      + 'Remove or de-identify it (use participant IDs such as P01) and retry. If this is a false positive, ask the user.');
  } catch (e) {
    block(`the tool input could not be checked (${e.message}). Retry; if it persists, tell the user.`);
  }
});

hooks/shell-guard.mjs

#!/usr/bin/env node
// PreToolUse guard for shell commands. Files change only through Claude's Edit and Write tools: an Edit fails when
// its anchor does not match, each call changes one file and shows a diff, and /rewind can undo it. This guard denies
// shell commands that write, copy, move or bulk-delete files, run inline code, reach the network, download and run
// packages, publish, or discard git work, and its reason names what to use instead.
// A deny goes to the AI, not to the person, so it never interrupts: the AI switches tool or asks the user.
// It reads the command text only: it stops the forms an AI writes, not a script file that writes files itself.
// Fails closed: input it cannot read is denied. Its command in hooks.json ends with `|| exit 2`, so a missing node
// or a crash blocks the command too.
const BOM = String.fromCharCode(0xfeff);

const USE_EDIT = 'Change files with the Edit or Write tool instead, one file per call, and read the change back.';
const ASK_USER = 'If it is really needed, ask the user to run it.';

const OUTWARD = new Set(['curl', 'wget', 'scp', 'sftp', 'rsync', 'ssh', 'nc', 'ncat', 'netcat', 'socat', 'telnet', 'ftp', 'lftp', 'aria2c', 'http', 'https']);
const SHELLS = new Set(['sh', 'bash', 'zsh', 'dash', 'ksh', 'fish']);
// The short flags that make each interpreter run code given on the command line.
const INLINE = { python: 'c', node: 'ep', nodejs: 'ep', perl: 'eEi', ruby: 'ei', php: 'r', bun: 'e', osascript: 'e', deno: '' };
// Flags that, used alone, run no code (an interpreter given no script and no flags reads code from standard input).
const SOLO = ['--version', '-v', '-V', '--help', '-h', '--test'];
// Commands that run the command after them, each with its flags that take a value.
const WRAPPERS = {
  sudo: ['-u', '-g', '-h', '-p', '-C', '-D', '-r', '-t', '-U'], doas: ['-u', '-C'], env: ['-u', '-C', '-S'], command: [], builtin: [],
  exec: ['-a'], nohup: [], nice: ['-n'], ionice: ['-c', '-n', '-p'], stdbuf: [], timeout: ['-s', '-k'], time: ['-f', '-o'],
  xargs: ['-I', '-d', '-E', '-L', '-n', '-P', '-s', '-a'],
};
const KEYWORDS = new Set(['{', '}', '!', 'if', 'then', 'else', 'elif', 'do', 'while', 'until']);
// > and >> with an optional stream number, &> and >|, then the target. 2>&1 and >&2 copy a stream and write no file.
const REDIRECT = /(?:&>>?|\d*>>?\|?)(&?)[ \t]*([^\s;&|<>()]*)/g;
const QUIET = /^\/dev\/(null|stdout|stderr|tty|fd\/\d+)$/;
const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;

const base = (word) => word.replace(/^.*\//, '').toLowerCase();
const isFlag = (word) => /^-./.test(word);
// True when a short flag cluster such as -rf or -i.bak holds any of the letters.
const hasLetter = (flags, letters) => flags.some((f) => {
  const m = /^-([A-Za-z]+)/.exec(f);
  return Boolean(m) && [...m[1]].some((l) => letters.includes(l));
});
const hasLong = (flags, ...names) => flags.some((f) => names.some((n) => f === n || f.startsWith(n + '=')));

// Where the ( at `open` closes, counting nested parentheses and skipping single-quoted text.
function closing(s, open) {
  let depth = 0;
  for (let k = open; k < s.length; k++) {
    if (s[k] === '\\') k++;
    else if (s[k] === "'") { const e = s.indexOf("'", k + 1); if (e < 0) return s.length; k = e; }
    else if (s[k] === '(') depth++;
    else if (s[k] === ')' && --depth === 0) return k;
  }
  return s.length;
}

// The command's structure: quoted text becomes Q, heredoc bodies are dropped because they are data, and the code
// inside $( ) or backticks within double quotes is appended as commands of its own, because it still runs.
function skeleton(cmd) {
  let out = '';
  const inner = [];
  const heredocs = [];
  let i = 0;
  while (i < cmd.length) {
    const c = cmd[i];
    if (c === '\\') { out += ' '; i += 2; continue; }
    if (c === "'") { const e = cmd.indexOf("'", i + 1); out += 'Q'; i = e < 0 ? cmd.length : e + 1; continue; }
    if (c === '"') {
      let j = i + 1;
      while (j < cmd.length && cmd[j] !== '"') {
        if (cmd[j] === '\\') j += 2;
        else if (cmd[j] === '$' && cmd[j + 1] === '(') { const e = closing(cmd, j + 1); inner.push(cmd.slice(j + 2, e)); j = e + 1; }
        else if (cmd[j] === '`') { let e = cmd.indexOf('`', j + 1); if (e < 0) e = cmd.length; inner.push(cmd.slice(j + 1, e)); j = e + 1; }
        else j++;
      }
      out += 'Q'; i = j + 1; continue;
    }
    // A heredoc (<<), but not a here-string (<<<).
    if (c === '<' && cmd[i + 1] === '<' && cmd[i + 2] !== '<' && cmd[i - 1] !== '<') {
      const m = /^<<(-?)[ \t]*(?:'([^'\n]*)'|"([^"\n]*)"|\\?([^\s;&|<>()]+))/.exec(cmd.slice(i));
      if (m) { heredocs.push({ end: m[2] ?? m[3] ?? m[4], tabs: m[1] === '-' }); out += ' '; i += m[0].length; continue; }
    }
    if (c === '\n' && heredocs.length) {
      let j = i + 1;
      for (const { end, tabs } of heredocs.splice(0)) {
        for (;;) {
          const e = cmd.indexOf('\n', j);
          const line = cmd.slice(j, e < 0 ? cmd.length : e);
          j = e < 0 ? cmd.length : e + 1;
          if ((tabs ? line.replace(/^\t+/, '') : line) === end || e < 0) break;
        }
      }
      out += ';'; i = j; continue;
    }
    out += c; i++;
  }
  return [out, ...inner.map(skeleton)].join(' ; ');
}

// Git subcommands that discard work, rewrite what was pushed, change remotes or write files outside git.
function checkGit(args) {
  let k = 0;
  while (k < args.length && isFlag(args[k])) k += ['-C', '-c', '--git-dir', '--work-tree', '--namespace'].includes(args[k]) ? 2 : 1;
  const sub = args[k];
  const rest = args.slice(k + 1);
  const flags = rest.filter(isFlag);
  const positional = rest.filter((a) => !isFlag(a));
  if (sub === 'push' && (hasLetter(flags, 'fd') || hasLong(flags, '--force', '--force-with-lease', '--force-if-includes', '--mirror', '--delete', '--prune')
    || positional.some((p) => /^[+:]/.test(p)))) {
    return `force pushes, mirror pushes and remote deletes rewrite or remove what was pushed. ${ASK_USER}`;
  }
  if ((sub === 'reset' && hasLong(flags, '--hard')) || sub === 'clean'
    || (sub === 'checkout' && (rest.includes('--') || positional.includes('.') || hasLetter(flags, 'f') || hasLong(flags, '--force')))
    || (sub === 'restore' && (!(hasLetter(flags, 'S') || hasLong(flags, '--staged')) || hasLetter(flags, 'W') || hasLong(flags, '--worktree')))
    || (sub === 'switch' && (hasLetter(flags, 'f') || hasLong(flags, '--force', '--discard-changes')))
    || (sub === 'stash' && ['drop', 'clear'].includes(positional[0]))) {
    return `\`git ${sub}\` used this way discards work that git cannot bring back. To undo an edit, use the Edit tool. ${ASK_USER}`;
  }
  if (sub === 'remote' && ['add', 'set-url'].includes(positional[0])) {
    return `changing where the repository fetches from or pushes to is the user's decision. ${ASK_USER}`;
  }
  if (hasLong(flags, '--output', '--output-directory') || sub === 'format-patch' || (sub === 'archive' && hasLetter(flags, 'o'))) {
    return `this git command writes files. ${USE_EDIT}`;
  }
  return undefined;
}

// The reason to deny one simple command, or undefined to leave it to the permission rules.
function check(words, viaXargs) {
  if (!words.length) return undefined;
  const name = base(words[0]);
  const args = words.slice(1);
  const flags = args.filter(isFlag);
  const positional = args.filter((a) => !isFlag(a));
  const lead = positional.slice(0, 3);
  const interpreter = /^python[0-9.]*$/.test(name) ? 'python' : name;
  const stdinOnly = !positional.length && !(flags.length && flags.every((f) => SOLO.includes(f)));

  if (OUTWARD.has(name)) return `\`${name}\` reaches the network. Read web pages with the WebFetch tool, which the PII guard checks. ${ASK_USER}`;
  if (['npm', 'pnpm', 'yarn', 'bun'].includes(name) && lead.includes('publish')) return `publishing a package is never the assistant's step. ${ASK_USER}`;
  if (['npx', 'pnpx', 'bunx'].includes(name) || (['pnpm', 'yarn'].includes(name) && lead.includes('dlx'))
    || (name === 'npm' && (lead.includes('exec') || lead.includes('x'))) || (name === 'bun' && positional[0] === 'x')) {
    return 'this downloads and runs a package that is not pinned in the lockfile. Add it as an exact-pinned dependency (an install, which asks the user) and run it with `pnpm exec`.';
  }
  if (name === 'eval' || (SHELLS.has(name) && (hasLetter(flags, 'cs') || hasLong(flags, '--command') || stdinOnly))) {
    return `\`${name}\` running code from a string or from standard input hides that code from the permission rules and this guard. Run the command itself.`;
  }
  if (Object.hasOwn(INLINE, interpreter) && (hasLetter(flags, INLINE[interpreter]) || hasLong(flags, '--eval', '--print')
    || positional.includes('-') || (interpreter === 'deno' && positional[0] === 'eval') || stdinOnly)) {
    return 'inline code (such as `python3 -c`, `node -e` or a heredoc into an interpreter) can change files with no diff and no rewind. '
      + `To read, use the Read or Grep tool; to run a check, run a script file from the repository. ${USE_EDIT}`;
  }
  if (['sed', 'gsed'].includes(name) && (hasLetter(flags, 'i') || hasLong(flags, '--in-place'))) {
    return `\`sed -i\` edits files in place: a pattern that matches nothing changes nothing and still succeeds, and a glob edits every file at once. ${USE_EDIT}`;
  }
  if (['awk', 'gawk'].includes(name) && /(^| )(-i ?|--include[= ]?)inplace( |$)/.test(args.join(' '))) return `\`${name} -i inplace\` edits files in place. ${USE_EDIT}`;
  if (name === 'tee' && positional.some((p) => !QUIET.test(p))) return `\`tee\` writes files. ${USE_EDIT}`;
  if ((name === 'dd' && args.some((a) => a.startsWith('of='))) || ['truncate', 'shred'].includes(name)) return `\`${name}\` writes files. ${USE_EDIT}`;
  if (['cp', 'mv', 'install'].includes(name)) {
    return 'copying and moving files bypass the Edit and Write tools and cannot be rewound. To create a file, Write it; to copy, move or rename files, ask the user.';
  }
  if (name === 'rm' && (viaXargs || hasLetter(flags, 'rR') || hasLong(flags, '--recursive') || positional.some((p) => /[*?[{]/.test(p)))) {
    return 'recursive, wildcard and piped deletes remove many files at once and cannot be rewound. Delete one named file per `rm`, which the user approves, or ask the user to delete the folder.';
  }
  if (name === 'find' && args.some((a) => ['-delete', '-exec', '-execdir', '-ok', '-okdir', '-fprint', '-fprint0', '-fprintf', '-fls'].includes(a))) {
    return '`find` with -delete or -exec acts on every file it matches. Find and search files with the Glob or Grep tool, then change each one with Edit or Write, or ask the user.';
  }
  if (name === 'git') return checkGit(args);
  return undefined;
}

// Skips variable assignments, shell keywords and wrappers such as sudo, env and xargs, then checks the command they run.
function analyse(words) {
  let i = 0;
  let viaXargs = false;
  while (i < words.length) {
    const word = words[i];
    const name = base(word);
    if (KEYWORDS.has(word) || ASSIGNMENT.test(word)) { i++; continue; }
    if (!Object.hasOwn(WRAPPERS, name)) break;
    if (name === 'xargs') viaXargs = true;
    i++;
    while (i < words.length && (isFlag(words[i]) || ASSIGNMENT.test(words[i]))) i += WRAPPERS[name].includes(words[i]) ? 2 : 1;
    if (name === 'timeout') i++; // its duration
  }
  return check(words.slice(i), viaXargs);
}

// The reason to deny the whole command line, or undefined when every part of it is left to the permission rules.
function review(command) {
  const shape = skeleton(command);
  for (const [, copy, target] of shape.matchAll(REDIRECT)) {
    if (!target || QUIET.test(target) || (copy && /^(\d+|-)$/.test(target))) continue;
    return `a redirection (\`>\` or \`>>\`) writes a file. ${USE_EDIT}`;
  }
  const parts = shape.replace(REDIRECT, ' ').replace(/\$\(|[()`]/g, ';').split(/\|\||&&|\|&|[;|&\n]/);
  for (const part of parts) {
    const reason = analyse(part.trim().split(/[ \t\r]+/).filter(Boolean));
    if (reason) return reason;
  }
  return undefined;
}

let raw = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', (c) => { raw += c; });
process.stdin.on('end', () => {
  let reason;
  try {
    const input = JSON.parse(raw.startsWith(BOM) ? raw.slice(1) : raw);
    if (!input || typeof input !== 'object') throw new Error('input is not an object');
    reason = review(String(input.tool_input?.command ?? ''));
  } catch (e) {
    reason = `the command could not be checked (${e.message}). Retry; if it persists, tell the user.`;
  }
  if (reason) {
    process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny',
      permissionDecisionReason: `Blocked by UX governance shell guard: ${reason}` } }));
  }
});

El control escribe su decisión una sola vez y deja que Node termine solo, así la salida está completa antes de que termine el proceso. Un comando para el que no tiene regla no recibe salida, y deciden las reglas de permisos: rm de un solo archivo con nombre, un git push simple y las instalaciones siguen preguntando, y todo lo demás sigue el modo de permisos de la sesión.

Rechaza algunos comandos de solo lectura que parecen escrituras, como find . -exec grep … o un > escrito como comparación. Es a propósito: su motivo nombra la herramienta que hay que usar en su lugar (Grep, Glob, Read), así el asistente sigue adelante sin preguntarte.

hooks/central-guard.mjs

#!/usr/bin/env node
// PreToolUse guard for shell commands: Edit permission rules do not cover files that a program writes itself,
// so every shell command that names central governance (or runs inside it) becomes an approval prompt,
// unless it is read-only or ordinary work on a short list. The shell guard refuses shell writes before this runs.
// Fails towards asking: unreadable input or any error also asks. Its command in hooks.json ends with `|| exit 2`,
// so a missing node or a crash blocks the command instead of letting it through.
import path from 'node:path';
import os from 'node:os';

// CONFIG
const ROOT_DIR = 'ux-work';
const CENTRAL_DIR = '000-central';
const PLUGIN = 'ux-governance';
const BOM = String.fromCharCode(0xfeff);

const ask = (reason) => {
  process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'ask', permissionDecisionReason: reason } }));
  process.exit(0);
};

// Read-only commands allowed without a prompt, with plain arguments only: no quotes, $, backticks, redirection,
// chaining, subexpressions or line breaks. Separators are spaces and tabs only: \s would also match a newline
// and let a second command through.
const READ_ONLY = /^[ \t]*(ls|pwd|cat|head|tail|wc|grep|git[ \t]+status)([ \t]+[^\s"'$`|;&<>()]+)*[ \t]*$/i;

// Central's own read-only tools, run by relative or absolute path, quoted or not. For example:
//   node tools/check-md.mjs governance --json
//   node "/Users/sam/ux-work/000-central/tools/retrofit-audit.mjs" --project <dir> --decider "Sam Example" ...
// Nothing may come between node and the script (no node flags). Each word is plain text, or a quoted part with
// no $, backticks or backslashes inside. The script must resolve to central's own tools folder: a copy elsewhere asks.
const READ_ONLY_TOOLS = ['check-md.mjs', 'retrofit-audit.mjs'];
const WORD = `(?:[^\\s"'$\`|;&<>()\\\\]|"[^"$\`\\\\\\r\\n]*"|'[^'\\r\\n]*')+`;
const TOOL_CALL = new RegExp(`^[ \\t]*node[ \\t]+(${WORD})(?:[ \\t]+${WORD})*[ \\t]*$`);
// Ordinary work in central that asks for no approval: git commands that write no file or record edits the
// administrator approved one by one (a quoted commit message is fine), the tool tests and the plugin validator.
// --output would make git write a file, so it still asks; so do push, pull and every other git command.
const GIT_WORK = new RegExp(`^[ \\t]*git[ \\t]+(status|diff|log|show|add|commit|fetch)(?:[ \\t]+${WORD})*[ \\t]*$`);
const CHECKS = /^[ \t]*(node[ \t]+--test[ \t]+(['"])tools\/test\/\*\.test\.mjs\2|claude[ \t]+plugin[ \t]+validate([ \t]+[^\s"'$`|;&<>()]+)?)[ \t]*$/;
const unquote = (word) => word.replace(/"([^"]*)"|'([^']*)'/g, (m, a, b) => a ?? b);
// The shell expands a leading ~/ only when it is not quoted.
const scriptPath = (word) => (word.startsWith('~/') ? path.join(os.homedir(), unquote(word.slice(2))) : unquote(word));

const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

let raw = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', (c) => { raw += c; });
process.stdin.on('end', () => {
  try {
    const input = JSON.parse(raw.startsWith(BOM) ? raw.slice(1) : raw);
    if (!input || typeof input !== 'object') throw new Error('input is not an object');
    const command = String(input.tool_input?.command ?? '');
    const central = path.join(os.homedir(), ROOT_DIR, CENTRAL_DIR).toLowerCase();
    const cwd = path.resolve(typeof input.cwd === 'string' && input.cwd ? input.cwd : process.cwd()).toLowerCase();
    const touchesCentral = new RegExp(escapeRe(CENTRAL_DIR), 'i').test(command) || cwd === central || cwd.startsWith(central + path.sep);
    if (!touchesCentral || READ_ONLY.test(command) || CHECKS.test(command)) process.exit(0);
    if (GIT_WORK.test(command) && !/--output/.test(command)) process.exit(0);
    const call = command.match(TOOL_CALL);
    if (call) {
      const script = path.resolve(cwd, scriptPath(call[1])).toLowerCase();
      if (READ_ONLY_TOOLS.some((t) => script === path.join(central, 'tools', t))) process.exit(0);
    }
    ask(`This shell command names central UX governance or runs inside it, and is not on the guard's read-only list. Central changes need the administrator's approval and go through /${PLUGIN}:governance-change.`);
  } catch {
    ask('UX governance could not read this shell command to check it; approve only if it does not change central governance.');
  }
});

Las rutas se comparan en minúsculas, así una ruta escrita con otras mayúsculas igual coincide en un sistema de archivos que no distingue mayúsculas, como el que trae macOS por defecto; en uno que sí las distingue, la comparación tiene el mismo efecto.

El control pregunta por cualquier otro comando que toque la carpeta central, incluidos git push, git pull y las herramientas que escriben, new-project.mjs y retrofit-apply.mjs. Es así a propósito: publicar la carpeta central, traer cambios, crear un proyecto y ejecutar una puesta al día son aprobaciones. retrofit-audit.mjs queda exento solo porque su especificación, más abajo, lo obliga a negarse a escribir dentro de la carpeta central. git add y git commit corren sin preguntar porque cada edición que registran ya preguntó (ask sobre Edit dentro de la carpeta central), y las reglas centrales igual hacen que el asistente le pregunte a quien administra antes de cada commit.

hooks/version-check.mjs

#!/usr/bin/env node
// SessionStart: when the session is inside a UX project, report the project's governance version against central,
// its process step and its Doing count, the decider, plus a reminder when the design-library map is due a check.
// Prints facts only; prints nothing outside the governed folders, and nothing at all on any error.
// What it prints goes into the AI's context, so every value read from PRD.md is checked before it is printed.
// Self-contained (no imports from outside the plugin), so it also works from a cached plugin copy.
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { fileURLToPath } from 'node:url';

// CONFIG
const ROOT_DIR = 'ux-work';
const CENTRAL_DIR = '000-central';
const PROJECTS_DIR = '001-projects';
const PLUGIN = 'ux-governance';
const MAP_FILE = ['governance', 'brand', 'design-library-structure.md'];

const central = path.join(os.homedir(), ROOT_DIR, CENTRAL_DIR);
const projects = path.join(os.homedir(), ROOT_DIR, PROJECTS_DIR);
const BOM = String.fromCharCode(0xfeff);
const BREAKS = `\\x00-\\x1f\\x7f${String.fromCharCode(0x85, 0x2028, 0x2029)}`;
const PERSON_BAD = new RegExp(`[${BREAKS}$|@\`]|\\{\\{`);

function centralVersion() {
  try { return fs.readFileSync(path.join(central, 'VERSION'), 'utf8').trim(); } catch { /* fall back to the plugin version */ }
  const manifest = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '.claude-plugin', 'plugin.json');
  try { return JSON.parse(fs.readFileSync(manifest, 'utf8')).version; } catch { return 'unknown'; }
}

// Double-quoted values are JSON strings; single-quoted ones use '' for a quote.
function unquote(v) {
  if (/^".*"$/.test(v)) { try { return JSON.parse(v); } catch { return v; } }
  if (/^'.*'$/.test(v)) return v.slice(1, -1).replaceAll("''", "'");
  return v;
}

function frontmatter(text) {
  const m = (text.startsWith(BOM) ? text.slice(1) : text).match(/^---\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/);
  const data = {};
  if (m) for (const line of m[1].split(/\r?\n/)) { const kv = line.match(/^([\w-]+):\s*(.*)$/); if (kv) data[kv[1]] = unquote(kv[2].trim()); }
  return data;
}

function badPersonValue(v) {
  return typeof v !== 'string' || !v.trim() || v.startsWith('--') || PERSON_BAD.test(v);
}

// Reminder line when the design-library map is past its next-check date; null when not due or unreadable.
function mapReminder() {
  try {
    const data = frontmatter(fs.readFileSync(path.join(central, ...MAP_FILE), 'utf8'));
    const d = new Date();
    const today = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
    if (!data.figma_next_check || today < data.figma_next_check) return null;
    return `Design-library map last verified ${data.figma_verified || 'unknown'}; its ${data.figma_check_every_days || 14}-day check was due ${data.figma_next_check}. Run /${PLUGIN}:figma-library-check.`;
  } catch { return null; }
}

let raw = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', (c) => { raw += c; });
process.stdin.on('end', () => {
  try {
    let cwd = process.cwd();
    try { const c = JSON.parse(raw.startsWith(BOM) ? raw.slice(1) : raw)?.cwd; if (typeof c === 'string' && c) cwd = c; } catch { /* use process cwd */ }
    const norm = (p) => path.resolve(p).toLowerCase();
    const version = centralVersion();
    const reminder = mapReminder();

    if (norm(cwd) === norm(central) || norm(cwd).startsWith(norm(central) + path.sep)) {
      console.log(`UX governance: this session is in central governance (version ${version}). Changes here go through /${PLUGIN}:governance-change.`);
      if (reminder) console.log(reminder);
      return;
    }
    if (!norm(cwd).startsWith(norm(projects) + path.sep)) return;

    const projectDir = path.join(projects, path.relative(projects, cwd).split(path.sep)[0]);
    const prdPath = path.join(projectDir, 'PRD.md');
    const name = path.basename(projectDir);
    if (!fs.existsSync(prdPath)) {
      console.log(`UX governance ${version}: project "${name}" has no PRD.md, so it has not been set up or retrofitted. Retrofit with /${PLUGIN}:retrofit-project.`);
      if (reminder) console.log(reminder);
      return;
    }
    const text = fs.readFileSync(prdPath, 'utf8');
    const data = frontmatter(text);
    // Only allowed values are printed; anything else is reported as invalid, never echoed.
    const STEPS = ['understand', 'define', 'sketch', 'decide', 'validate', 'deliver', 'n/a'];
    const projectVersion = /^\d+\.\d+\.\d+$/.test(data.governance_version ?? '') ? data.governance_version : 'unknown';
    const step = STEPS.includes(data.process_step) ? data.process_step : 'unknown (not an allowed value)';
    // The board's Doing line: a count of 0 to 3, then `none` or exactly that many card IDs (C-001, C-002).
    const doing = text.match(/■ \*\*Doing \(([0-3])\/3\):\*\*[ \t]*(none|C-\d{3,}(?:, C-\d{3,})*)[ \t]*$/m);
    const doingValid = doing && (doing[2] === 'none' ? 0 : doing[2].split(', ').length) === Number(doing[1]);
    const lines = [
      `UX governance: project "${name}" follows governance ${projectVersion}; central is ${version}.`,
      `Current process step (PRD.md): ${step}.`,
      doingValid ? `Doing: ${doing[1]}/3 (${doing[2]}).` : 'Kanban board: no valid Doing line found in PRD.md.',
    ];
    const decider = data.decider;
    const address = data.decider_address;
    if ((decider && badPersonValue(decider)) || (address && badPersonValue(address))) lines.push('PRD.md decider value is invalid; ask the user who the decider is and how to address them.');
    else if (decider && address) lines.push(`Decider: ${decider} (address as ${address}); they approve everything in this project.`);
    else if (decider) lines.push(`Decider: ${decider} (no decider_address yet: address them by name and they/them, and ask once how they would like to be addressed); they approve everything in this project.`);
    else lines.push('PRD.md has no decider: ask the user who the decider is and how to address them before any project work.');
    if (projectVersion !== version) lines.push(`The project's governance version is not central's; /${PLUGIN}:retrofit-project brings it to ${version}.`);
    if (reminder) lines.push(reminder);
    console.log(lines.join('\n'));
  } catch { /* silent on error: a session-start report must never get in the way of a session */ }
});

Instalador

user-config/install.mjs:

#!/usr/bin/env node
// Installs central user-level config into ~/.claude, then registers the governance plugin.
// Backs up the current files first. Merges settings: central keys win, other keys you have are kept,
// permission lists are combined without duplicates. Never replaces your CLAUDE.md: the governance text sits
// between two marker lines, appended on the first run and replaced on later runs; the rest of the file is kept.
// Each file is written to a .tmp copy and then renamed into place. Exits non-zero if any step fails.
// Usage: node user-config/install.mjs [--name "<full name>"] [--address "<how to address you>"] [--dry-run]
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import readline from 'node:readline/promises';
import { execFileSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';

// CONFIG
const ROOT_DIR = 'ux-work';
const WORK_DIR = '_governance-work';
const MARKETPLACE = 'ux-central';
const PLUGIN = 'ux-governance';
const BEGIN = '<!-- ux-governance:begin -->';
const END = '<!-- ux-governance:end -->';

const dryRun = process.argv.includes('--dry-run');
const arg = (n) => { const i = process.argv.indexOf(`--${n}`); return i > -1 ? process.argv[i + 1] : undefined; };
const here = path.dirname(fileURLToPath(import.meta.url));
const central = path.resolve(here, '..');
const claudeDir = path.join(os.homedir(), '.claude');
// Milliseconds keep two runs in the same second apart; a backup is never overwritten (COPYFILE_EXCL below).
const stamp = new Date().toISOString().slice(0, 23).replace(/[:T.]/g, '-');
const backupDir = path.join(os.homedir(), ROOT_DIR, WORK_DIR, 'backups', `${stamp}-user-config`);
const run = (cmd, args) => execFileSync(cmd, args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
const die = (msg) => { console.error(`Install failed: ${msg}`); process.exit(1); };
const BREAKS = `\\x00-\\x1f\\x7f${String.fromCharCode(0x85, 0x2028, 0x2029)}`;
const PERSON_BAD = new RegExp(`[${BREAKS}$|@\`]|\\{\\{`);

// The plugin hooks run `node`; if it is not on PATH they cannot run and the guards would not block anything.
try { console.log(`node ${run('node', ['--version'])} found on PATH`); } catch { die('`node` is not on PATH. Install Node LTS and restart your editor, then re-run.'); }
try { console.log(`claude ${run('claude', ['--version'])} found on PATH`); } catch { die('`claude` is not on PATH.'); }

const userSettingsPath = path.join(claudeDir, 'settings.json');
const userClaudeMdPath = path.join(claudeDir, 'CLAUDE.md');
let current = {};
try { if (fs.existsSync(userSettingsPath)) current = JSON.parse(fs.readFileSync(userSettingsPath, 'utf8')); } catch (e) { die(`${userSettingsPath} is not valid JSON (${e.message}). Fix it by hand, then re-run.`); }
if (!current || typeof current !== 'object' || Array.isArray(current)) die(`${userSettingsPath} does not hold a JSON object. Fix it by hand, then re-run.`);
// These settings switch the guards off (no hook runs, or nothing asks), so the install would protect nothing.
if (current.disableAllHooks === true || current.permissions?.defaultMode === 'bypassPermissions') {
  die(`${userSettingsPath} turns the governance guards off ("disableAllHooks": true or "defaultMode": "bypassPermissions"). Remove those settings, then re-run.`);
}

// Permission paths are written centrally as {{HOME_POSIX}} and {{CENTRAL_POSIX}} and resolved for this user:
// the absolute path without its leading slash, so /Users/sam becomes Users/sam and /home/sam becomes home/sam.
const homePosix = os.homedir().replace(/^\//, '');
const centralPosix = central.replace(/^\//, '');
for (const [what, p] of [['home folder', homePosix], ['central folder', centralPosix]]) {
  if (/["*?[\]()]/.test(p)) die(`the ${what} path contains a character that is not safe in a permission pattern: ${p}`);
}
const fillTokens = (s) => s.replaceAll('{{HOME_POSIX}}', () => homePosix).replaceAll('{{CENTRAL_POSIX}}', () => centralPosix);
const resolveTokens = (v) => (typeof v === 'string' ? fillTokens(v)
  : Array.isArray(v) ? v.map(resolveTokens)
    : v && typeof v === 'object' ? Object.fromEntries(Object.entries(v).map(([k, x]) => [k, resolveTokens(x)])) : v);
const ours = resolveTokens(JSON.parse(fs.readFileSync(path.join(here, 'settings.json'), 'utf8')));
// Refuse to install a placeholder left in central's settings.json, such as <OPUS_MODEL_ID> or an unknown {{TOKEN}}.
const leftover = [...new Set(JSON.stringify(ours).match(/<[A-Za-z][\w .-]*>|\{\{[A-Za-z_]+\}\}/g) || [])];
if (leftover.length) die(`user-config/settings.json still holds placeholders: ${leftover.join(', ')}. Fill them in, then re-run.`);

const personProblem = (v) => {
  if (typeof v !== 'string' || !v.trim()) return 'is required and cannot be blank';
  if (v.startsWith('--')) return 'is missing a value (the next flag was taken as the value)';
  if (PERSON_BAD.test(v)) return 'must be one line and cannot contain control characters, {{, $, |, @ or backticks';
  return null;
};

// The governance block in the user's CLAUDE.md: '' when absent, null when the markers are damaged.
const governanceBlock = (text) => {
  const begins = text.split(BEGIN).length - 1;
  const ends = text.split(END).length - 1;
  if (begins === 0 && ends === 0) return '';
  if (begins !== 1 || ends !== 1 || text.indexOf(END) < text.indexOf(BEGIN)) return null;
  return text.slice(text.indexOf(BEGIN), text.indexOf(END) + END.length);
};
// Returns the user's CLAUDE.md with the governance block appended (first run) or replaced (later runs).
const withGovernanceBlock = (text, body) => {
  const old = governanceBlock(text);
  const block = `${BEGIN}\n${body.trim()}\n${END}`;
  if (old) return text.replace(old, () => block);
  return text.trim() ? `${text.trimEnd()}\n\n${block}\n` : `${block}\n`;
};

const previous = fs.existsSync(userClaudeMdPath) ? fs.readFileSync(userClaudeMdPath, 'utf8') : '';
const previousBlock = governanceBlock(previous);
if (previousBlock === null) die(`${userClaudeMdPath} has damaged governance markers. Keep exactly one "${BEGIN}" line and one "${END}" line after it (or remove both), then re-run.`);
const fromPrevious = (label) => { const v = previousBlock.match(new RegExp(`^- ${label}: (.+)$`, 'm'))?.[1]?.trim(); return v && !personProblem(v) ? v : undefined; };
const fromFlag = (n) => {
  if (!process.argv.includes(`--${n}`)) return undefined;
  const problem = personProblem(arg(n));
  if (problem) die(`--${n} ${problem}`);
  return arg(n).trim();
};
let userName = fromFlag('name') || fromPrevious('Name');
let userAddress = fromFlag('address') || fromPrevious('Address as');
if (!userName || !userAddress) {
  if (!process.stdin.isTTY) die('pass --name "<full name>" and --address "<how to address you>" (no terminal to ask in).');
  const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
  const ask = async (question, what) => {
    const v = await rl.question(question);
    const problem = personProblem(v);
    if (problem) { rl.close(); die(`${what} ${problem}`); }
    return v.trim();
  };
  userName ||= await ask('Your full name: ', 'the name you entered');
  userAddress ||= await ask('How should the assistant address you (name, and pronouns if you like)? ', 'the address you entered');
  rl.close();
}

const uniq = (a = [], b = []) => [...new Set([...a, ...b])];
const merged = {
  ...current,
  ...ours,
  modelSettings: { ...(current.modelSettings || {}), ...(ours.modelSettings || {}) },
  env: { ...(current.env || {}), ...(ours.env || {}) },
  permissions: {
    ...(current.permissions || {}),
    ...(ours.permissions || {}),
    allow: uniq(current.permissions?.allow, ours.permissions?.allow),
    ask: uniq(current.permissions?.ask, ours.permissions?.ask),
    deny: uniq(current.permissions?.deny, ours.permissions?.deny),
  },
};
const governanceText = fs.readFileSync(path.join(here, 'CLAUDE.md'), 'utf8')
  .replaceAll('{{USER_NAME}}', () => userName).replaceAll('{{USER_ADDRESS}}', () => userAddress);
if (governanceText.includes(BEGIN) || governanceText.includes(END)) die('user-config/CLAUDE.md or a person value contains a governance marker line.');
const claudeMd = withGovernanceBlock(previous, governanceText);

console.log(`User:      ${userName} (address as: ${userAddress})`);
console.log(`Backup:    ${backupDir}`);
if (dryRun) {
  console.log(`\n${userSettingsPath} would become:\n${JSON.stringify(merged, null, 2)}`);
  console.log(`\n${userClaudeMdPath} would become:\n${claudeMd}`);
  process.exit(0);
}

// Writes next to the real file (a linked settings file stays linked), then renames: a crash never leaves half a file.
const writeAtomic = (file, text) => {
  const target = fs.existsSync(file) ? fs.realpathSync(file) : file;
  fs.writeFileSync(`${target}.tmp`, text);
  fs.renameSync(`${target}.tmp`, target);
};
try {
  fs.mkdirSync(backupDir, { recursive: true });
  for (const f of [userSettingsPath, userClaudeMdPath]) {
    if (fs.existsSync(f)) fs.copyFileSync(f, path.join(backupDir, path.basename(f)), fs.constants.COPYFILE_EXCL);
  }
  fs.mkdirSync(claudeDir, { recursive: true });
  writeAtomic(userSettingsPath, JSON.stringify(merged, null, 2) + '\n');
  writeAtomic(userClaudeMdPath, claudeMd);
} catch (e) { die(`writing the user files: ${e.message}. Any backups made before the failure are in ${backupDir}.`); }
console.log('Settings and CLAUDE.md written.');

const marketplaceDir = path.join(central, 'marketplace');
let marketplaces = '';
try { marketplaces = run('claude', ['plugin', 'marketplace', 'list']); } catch { /* treated as not yet added */ }
if (!marketplaces.includes(MARKETPLACE)) {
  try { console.log(run('claude', ['plugin', 'marketplace', 'add', marketplaceDir])); } catch (e) { die(`marketplace add: ${e.stderr || e.message}`); }
}
let plugins = '';
try { plugins = run('claude', ['plugin', 'list']); } catch { /* treated as not installed */ }
if (!plugins.includes(`${PLUGIN}@${MARKETPLACE}`)) {
  try { console.log(run('claude', ['plugin', 'install', `${PLUGIN}@${MARKETPLACE}`])); } catch (e) { die(`plugin install: ${e.stderr || e.message}`); }
}
console.log('Done. Restart your editor (or run /reload-plugins) so new sessions pick up the plugin, PATH and settings.');

En el user-config/settings.json de la carpeta central, escribí la regla de Edit para la carpeta central como Edit(//{{CENTRAL_POSIX}}/**). El instalador completa {{CENTRAL_POSIX}} con la ruta absoluta de la carpeta central sin la barra inicial, y {{HOME_POSIX}} de la misma manera con la carpeta personal. Correlo primero con --dry-run: muestra los dos archivos tal como quedarían escritos y no cambia nada.

tools/lib/md.mjs

md.mjs importa sus patrones de datos personales del hooks/lib/pii.mjs del plugin, así que creá pii.mjs (más arriba) en la fase 4, junto con este archivo; sin él, la importación falla.

// Shared helpers for the governance scripts: frontmatter parsing, the md standard schema, PII patterns.
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

export const REQUIRED_FIELDS = [
  'title', 'description', 'doc_type', 'project', 'process_step', 'brand',
  'status', 'owner', 'created', 'updated', 'governance_version', 'contains_pii',
];

// CONFIG: `all`, then one slug per brand (one brand is fine; add more as needed).
export const BRANDS = ['all', 'brand-a'];

export const ALLOWED = {
  doc_type: ['readme', 'prd', 'research-note', 'meeting-notes', 'synthesis', 'define', 'sketch',
    'decision', 'test-plan', 'spec', 'governance', 'index', 'other'],
  process_step: ['understand', 'define', 'sketch', 'decide', 'validate', 'deliver', 'n/a'],
  brand: BRANDS,
  status: ['draft', 'in-review', 'approved', 'superseded'],
  contains_pii: ['true', 'false'],
};

// The AI tool defines the format of these files, so the md standard does not apply. Case-insensitive.
// Only the tool's own agents folders are exempt (.claude/agents, a plugin's agents folder).
export function isExempt(file) {
  const base = path.basename(file).toLowerCase();
  const parts = file.split(path.sep);
  return base === 'skill.md' || base === 'claude.md' || base === 'claude.local.md'
    || parts.some((p, i) => p === 'agents' && (parts[i - 1] === '.claude' || parts[i - 2] === 'plugins'))
    || parts.includes('node_modules') || parts.includes('.git');
}

const BOM = String.fromCharCode(0xfeff);
export function readText(file) {
  const text = fs.readFileSync(file, 'utf8');
  return text.startsWith(BOM) ? text.slice(1) : text;
}

function unquote(v) {
  if (/^".*"$/.test(v)) { try { return JSON.parse(v); } catch { return v; } }
  if (/^'.*'$/.test(v)) return v.slice(1, -1).replaceAll("''", "'");
  return v;
}

// Minimal parser for flat key: value frontmatter. The closing line is --- with optional trailing spaces or tabs.
export function parseFrontmatter(text) {
  const m = text.match(/^---\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/);
  if (!m) return { data: null, body: text, close: -1 };
  const data = {};
  for (const line of m[1].split(/\r?\n/)) {
    const kv = line.match(/^([A-Za-z_][\w-]*):\s*(.*)$/);
    if (kv) data[kv[1]] = unquote(kv[2].trim());
  }
  return { data, body: text.slice(m[0].length), close: m[1].split(/\r?\n/).length + 1 };
}

// H1 lines outside fenced code blocks.
export function h1Lines(body) {
  let fenced = false;
  const out = [];
  for (const line of body.split(/\r?\n/)) {
    if (/^\s*(```|~~~)/.test(line)) { fenced = !fenced; continue; }
    if (!fenced && /^# /.test(line)) out.push(line.slice(2).trim());
  }
  return out;
}

// Control characters and Unicode line breaks, built from char codes (no raw separators in the source).
const BREAKS = `\\x00-\\x1f\\x7f${String.fromCharCode(0x85, 0x2028, 0x2029)}`;
const LINE_BREAKS = new RegExp(`[${BREAKS}]+`, 'g');
const PERSON_BAD = new RegExp(`[${BREAKS}$|@\`]|\\{\\{`);

export function oneLine(v) {
  return String(v).replace(LINE_BREAKS, ' ').trim();
}

// Quotes a value for flat YAML frontmatter when YAML would treat it specially. contains_pii is written bare.
export function yamlValue(v, key) {
  const s = oneLine(v);
  if (key === 'contains_pii' && (s === 'true' || s === 'false')) return s;
  const scalar = /^(true|false|yes|no|y|n|on|off|null|~|=|<<|[-+]?(\d[\d._]*(e[-+]?\d+)?|\.\d[\d_]*(e[-+]?\d+)?|\.inf|\.nan)|[-+]?0x[0-9a-f_]+|[-+]?0o[0-7_]+|[-+]?0b[01_]+)$/i;
  return /[:#\[\]{}&*!|>'"%@`]|^[-?,]/.test(s) || scalar.test(s) ? JSON.stringify(s) : s;
}

// Checks a one-line person value (decider, how to address them). Returns a problem message, or null.
export function personValueProblem(v) {
  if (typeof v !== 'string' || !v.trim()) return 'is required and cannot be blank';
  if (v.startsWith('--')) return 'is missing a value (the next flag was taken as the value)';
  if (PERSON_BAD.test(v)) return 'must be one line and cannot contain control characters, {{, $, |, @ or backticks';
  return null;
}

// Fills {{KEY}} placeholders: YAML-quoted inside frontmatter, plain in the body. Unknown keys are left in place.
export function fillTemplate(text, values) {
  const fill = (s) => s.replace(/\{\{([A-Z_]+)\}\}/g, (m, k) => (k in values ? String(values[k]) : m));
  const m = text.match(/^---\r?\n[\s\S]*?\r?\n---[ \t]*(?:\r?\n|$)/);
  if (!m) return fill(text);
  const fm = m[0].replace(/^([A-Za-z_][\w-]*):[ \t]*(.*\{\{[A-Z_]+\}\}.*)$/gm, (line, key, value) => {
    const filled = fill(value);
    return filled === value ? line : `${key}: ${yamlValue(filled, key)}`;
  });
  return fm + fill(text.slice(m[0].length));
}

// Never follows symbolic links: they can point outside the project or loop.
export function walk(dir, filter = () => true, out = []) {
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
    if (entry.name === 'node_modules' || entry.name === '.git' || entry.isSymbolicLink()) continue;
    const full = path.join(dir, entry.name);
    if (entry.isDirectory()) walk(full, filter, out);
    else if (filter(full)) out.push(full);
  }
  return out;
}

// One source for personal-information patterns: the plugin's hook library. CONFIG: plugin folder name.
export { PII_PATTERNS, findPii, mask, maskAll } from '../../marketplace/plugins/ux-governance/hooks/lib/pii.mjs';

export function centralRoot() {
  return path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
}

export function governanceVersion() {
  return fs.readFileSync(path.join(centralRoot(), 'VERSION'), 'utf8').trim();
}

export function today() {
  const d = new Date();
  return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
}

Las herramientas de puesta al día necesitan más funciones en esta biblioteca: encodingProblem (UTF-16, bytes NUL, UTF-8 inválido), frontmatterProblem (claves que las herramientas no pueden leer, separadores --- tomados por un encabezado), fieldValueProblem (valores sin reemplazar, valores permitidos, fechas reales del calendario, formato de versión), isInside (sin distinguir mayúsculas, como el sistema de archivos que trae macOS por defecto; resuelve los enlaces simbólicos), toFrontmatter y withDefineCandidates. No se incluyen acá. Armalas, junto con retrofit-audit.mjs y retrofit-apply.mjs, como una tarjeta propia en el tablero de la carpeta central la primera vez que un proyecto necesite una puesta al día (ver Herramientas de puesta al día: especificación).

tools/check-md.mjs

#!/usr/bin/env node
// Validates Markdown files against governance/md-standard.md.
// Usage: node tools/check-md.mjs <file-or-dir>... [--template] [--json]
//   --template  accept {{PLACEHOLDER}} values (for project-template/); without it, any {{PLACEHOLDER}} is a problem
// Exit codes: 0 all pass, 1 a file fails, 64 no file or folder given, 66 a file or folder given does not exist.
import fs from 'node:fs';
import path from 'node:path';
import { REQUIRED_FIELDS, ALLOWED, isExempt, readText, parseFrontmatter, walk, h1Lines } from './lib/md.mjs';

const args = process.argv.slice(2);
const template = args.includes('--template');
const asJson = args.includes('--json');
const targets = args.filter((a) => !a.startsWith('--'));
if (!targets.length) {
  console.error('Usage: node tools/check-md.mjs <file-or-dir>... [--template] [--json]');
  process.exit(64);
}

const isPlaceholder = (v) => template && /^\{\{[A-Z_]+\}\}$/.test(v);
const missing = targets.filter((t) => !fs.existsSync(t));
if (missing.length) { console.error(`Not found: ${missing.join(', ')}`); process.exit(66); }
const files = targets.flatMap((t) => (fs.statSync(t).isDirectory() ? walk(t, (f) => f.endsWith('.md')) : [t]))
  .filter((f) => !isExempt(f));

const results = files.map((file) => {
  const problems = [];
  const { data, body } = parseFrontmatter(readText(file));
  if (!data) {
    problems.push('no frontmatter');
  } else {
    for (const field of REQUIRED_FIELDS) {
      const v = data[field];
      if (v === undefined || v === '') { problems.push(`missing ${field}`); continue; }
      if (isPlaceholder(v)) continue;
      if (!template && /\{\{[A-Z_]+\}\}/.test(v)) { problems.push(`${field}: unfilled placeholder in "${v}"`); continue; }
      if (ALLOWED[field] && !ALLOWED[field].includes(v)) problems.push(`${field}: "${v}" not in [${ALLOWED[field].join(', ')}]`);
      if ((field === 'created' || field === 'updated') && !/^\d{4}-\d{2}-\d{2}$/.test(v)) problems.push(`${field}: "${v}" is not YYYY-MM-DD`);
    }
  }
  const h1s = h1Lines(body);
  if (h1s.length !== 1) problems.push(`expected exactly one H1, found ${h1s.length}`);
  else if (data?.title && !isPlaceholder(data.title) && h1s[0] !== data.title) {
    problems.push(`H1 "${h1s[0]}" does not match title "${data.title}"`);
  }
  return { file, ok: problems.length === 0, problems };
});

if (asJson) {
  console.log(JSON.stringify(results, null, 2));
} else {
  for (const r of results.filter((x) => !x.ok)) console.log(`FAIL ${path.relative(process.cwd(), r.file)}\n  - ${r.problems.join('\n  - ')}`);
  console.log(`${results.filter((r) => r.ok).length}/${results.length} files pass the md standard`);
}
process.exit(results.every((r) => r.ok) ? 0 : 1);

tools/new-project.mjs

#!/usr/bin/env node
// Creates a project folder from project-template/ and fills the mechanical placeholders.
// Content placeholders ({{OBJECTIVES}} etc.) are left for the new-project skill to draft from the brief.
// Usage: node tools/new-project.mjs --name "<Project name>" [--brand all|<slug>] --summary "<one line>"
//          --decider "<full name>" --decider-address "<how to address them>" [--projects-dir <dir>]
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { centralRoot, governanceVersion, today, walk, fillTemplate, personValueProblem, BRANDS } from './lib/md.mjs';

// CONFIG
const ROOT_DIR = 'ux-work';
const PROJECTS_DIR = '001-projects';
// One label per entry in BRANDS (md.mjs), using each brand's display name.
const BRAND_LABELS = { all: 'All brands', 'brand-a': 'Brand A only' };
// Under phases/: a research folder in each of the first five steps (local only, never committed), prototypes in the sixth.
const PHASE_FOLDERS = ['01-understand/research', '02-define/research', '03-sketch/research',
  '04-decide/research', '05-validate/research', '06-deliver/prototypes'];

const BREAKS = new RegExp(`[\\x00-\\x1f\\x7f${String.fromCharCode(0x85, 0x2028, 0x2029)}]`);
const arg = (name, fallback) => { const i = process.argv.indexOf(`--${name}`); return i > -1 ? process.argv[i + 1] : fallback; };
const name = arg('name');
const brand = arg('brand', 'all');
const summary = arg('summary', '{{ONE_LINE_SUMMARY}}');
const decider = arg('decider');
const deciderAddress = arg('decider-address');
const projectsDir = arg('projects-dir', path.join(os.homedir(), ROOT_DIR, PROJECTS_DIR));
const refuse = (msg) => { console.error(msg); process.exit(64); };

if (!name) refuse('--name is required');
if (name.startsWith('--')) refuse('--name is missing a value (the next flag was taken as the value)');
if (!BRANDS.includes(brand)) refuse(`--brand must be one of ${BRANDS.join(', ')}`);
// Names must be valid folder names on macOS and Linux and safe to pass through a shell.
if (/[<>:"/\\|?*]/.test(name) || BREAKS.test(name) || /[. ]$/.test(name)) refuse('Project name contains characters not allowed in folder names');
if (BREAKS.test(summary) || summary.startsWith('--')) refuse('--summary must be one line with no control characters and no leading --');
for (const [flag, v] of [['--decider', decider], ['--decider-address', deciderAddress]]) {
  const problem = personValueProblem(v);
  if (problem) refuse(`${flag} ${problem}`);
}

const dest = path.join(projectsDir, name);
if (fs.existsSync(dest)) { console.error(`Refusing: ${dest} already exists. Use retrofit-project for existing projects.`); process.exit(1); }

// Read everything that can fail (VERSION) before the first write, so a failure leaves no half-made project.
const values = {
  PROJECT_NAME: name, BRAND: brand, BRAND_SCOPE: BRAND_LABELS[brand],
  DATE: today(), GOVERNANCE_VERSION: governanceVersion(),
  DECIDER: decider, DECIDER_ADDRESS: deciderAddress,
};
if (summary !== '{{ONE_LINE_SUMMARY}}') values.ONE_LINE_SUMMARY = summary;

fs.cpSync(path.join(centralRoot(), 'project-template'), dest, { recursive: true });
// Git ignores the empty research folders inside central, so make sure they exist in the copy.
for (const d of PHASE_FOLDERS) fs.mkdirSync(path.join(dest, 'phases', d), { recursive: true });
for (const file of walk(dest, (f) => f.endsWith('.md'))) {
  fs.writeFileSync(file, fillTemplate(fs.readFileSync(file, 'utf8'), values));
}

// Everything still to fill: {{NAME}} content placeholders, and <NAME> placeholders left in central's template.
const remaining = walk(dest, (f) => /\.(md|json)$/.test(f))
  .flatMap((f) => [...fs.readFileSync(f, 'utf8').matchAll(/\{\{[A-Z_]+\}\}|<[A-Z][A-Z0-9_]*>/g)].map((m) => `${path.relative(dest, f)}: ${m[0]}`));
console.log(JSON.stringify({ created: dest, governance_version: values.GOVERNANCE_VERSION, placeholders_to_fill: remaining }, null, 2));

Herramientas de puesta al día: especificación

retrofit-audit.mjs y retrofit-apply.mjs se especifican acá, pero no se incluyen. Armalas como una tarjeta propia en el tablero de la carpeta central la primera vez que un proyecto necesite una puesta al día: las programa un subagente con Sonnet, las revisa code-stack-reviewer y las pruebas se escriben primero.

retrofit-audit.mjs (solo lectura)

node tools/retrofit-audit.mjs --project <dir> --out <WORK>/reports/YYYY-MM-DD-<Project name> --brand <slug>
                              --decider "<full name>" --decider-address "<address form>"
RequisitoDetalle
Nunca escribe dentro del proyecto ni de la carpeta centralRechaza un --out dentro del proyecto o dentro de la carpeta central (sin distinguir mayúsculas y siguiendo los enlaces); rechaza un --out que no esté vacío, salvo que sea el informe anterior de este proyecto. El control de la carpeta central deja correr esta herramienta sin preguntar solo por este rechazo
InventarioCada archivo: ruta, tipo, tamaño, fechas, estado del encabezado, cantidad de coincidencias de datos personales (de pii.mjs)
Cambios de Markdown (md_changes)Para cada archivo Markdown no exento: el encabezado para agregar o combinar (solo los campos que se agregan) y los auto_fields que el agente auditor tiene que afinar; las fechas salen de la historia del archivo, salvo que el contenido tenga su propia fecha
Archivos nuevos (new_files)README, PRD, CLAUDE.md, .claude/settings.json, .gitignore y los archivos _index.md de cada paso, desde la plantilla, con los valores para reemplazar listados en la sección 1 del plan
Cambios de nombre (proposed_renames)Las carpetas de paso pasan a los nombres de la plantilla; solo el nombre propio de una carpeta; nunca una carpeta dentro de otra a la que se le cambia el nombre
Cambios en el cuerpo (body_changes)Con path, change, insert_text, insert_before (por ejemplo, la sección que nombra a quien decide, en un PRD viejo); elementos de ‘arreglo a mano’ para archivos inseguros, datos personales sin contains_pii: true y nombres de archivos de entrevistas que pueden llevar el nombre de una persona
Paso del procesoInferido y presentado como propuesta; frases del problema candidatas, nunca acordadas
PrivacidadLos informes llevan solo cantidades y muestras enmascaradas, nunca valores del encabezado existente ni texto de las entrevistas
EnlacesSe informan, no se siguen
Salidasplan.md (secciones: 1 valores para reemplazar, 2 resumen, 3 encabezado, 4 archivos nuevos, 5 cambios de nombre, 6 cambios en el cuerpo, 7 datos personales, 8 duplicados, 9 excluidos), inventory.json, changes.diff

retrofit-apply.mjs

node tools/retrofit-apply.mjs --inventory <report>/inventory.json --backup <WORK>/backups/YYYY-MM-DD-<Project name>[-n]
RequisitoDetalle
Solo lo aprobadoLee approved: md_changes, new_files (true/false), los body_changes y renames exactos (tienen que coincidir con las propuestas en cada campo), con el registro de la aprobación
Fase 1: planificar y revisar todoRutas normalizadas y dentro del proyecto, sin duplicados bajo ningún nombre; los archivos son Markdown en UTF-8, no exentos, con permiso de escritura y sin bloquear; el encabezado se puede leer; los valores son válidos (sin valores para reemplazar, valores permitidos, fechas reales, formato de versión); los archivos nuevos no tienen valores para reemplazar y su JSON es válido; los cambios de nombre no se superponen, los orígenes no contienen enlaces y cada destino está dentro de una carpeta; la carpeta de copia de seguridad está vacía. Ante cualquier falla: rechazar, no cambiar nada y no crear copia de seguridad
Fase 2: copia de seguridad, después escrituraCopiar cada archivo que va a cambiar (y cada archivo que va a tocar un cambio aprobado en el cuerpo) a <backup>/files/<path>; los elementos con nombre nuevo, a <backup>/renamed/, sin node_modules ni .git; nunca sobrescribir una copia de seguridad
Combinación del encabezadoInsertar líneas antes del --- de cierre; conservar el BOM y cada fin de línea existente; volver a leer el encabezado nuevo
Archivos nuevosSe crean con opciones que impiden sobrescribir
Cambios en el cuerpoEl programa no los aplica; los aplica la IA con Edit después
RegistroCada paso queda registrado; ante una falla de escritura, frenar e informar lo que se aplicó; pedirle a quien usa que corra pnpm install por cada node_modules movido

Pruebas

Se corren con node --test 'tools/test/*.test.mjs' (en el Node.js actual, la forma con carpeta, node --test tools/test, falla). Proyectos de prueba en una carpeta temporal que cubren: el ensayo no cambia el hash de ningún archivo; se rechaza un --out dentro del proyecto o dentro de la carpeta central; cada rechazo del paso de escritura deja el proyecto sin cambios y no crea copia de seguridad; el BOM y los CRLF se conservan al combinar; las copias de seguridad nunca se sobrescriben; se rechazan los archivos exentos y los que no son UTF-8; se rechazan las aprobaciones alteradas; se rechazan los cambios de nombre que se superponen; se rechazan los orígenes que son enlaces; los datos personales nunca aparecen en los archivos de informe.

Contenido de la guía