karajan-code 4.16.0 → 4.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "karajan-code",
3
- "version": "4.16.0",
3
+ "version": "4.17.0",
4
4
  "description": "Local multi-agent coding orchestrator with TDD, SonarQube, and code review pipeline",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0",
@@ -30,7 +30,7 @@ export const ADVANCED_GROUPS = [
30
30
  { title: "Pipeline (piezas sueltas)", commands: ["autorun", "code", "review", "solomon", "agent", "scan", "tournament"] },
31
31
  { title: "Análisis pre-run", commands: ["discover", "triage", "researcher", "architect", "onboard", "brief"] },
32
32
  { title: "Búsqueda / RAG", commands: ["rag", "qmd", "watch"] },
33
- { title: "Calidad / auditoría", commands: ["audit", "check", "mutate", "webperf", "sonar", "privacy", "release"] },
33
+ { title: "Calidad / auditoría", commands: ["audit", "check", "mutate", "webperf", "sonar", "privacy", "release", "policy"] },
34
34
  { title: "Sesión / board", commands: ["resume", "report", "board", "hu", "adr", "worktree", "undo", "standby", "sentinel"] },
35
35
  { title: "Infra / setup", commands: ["install-tools", "ollama", "skills", "roles", "agents", "env"] },
36
36
  { title: "Mantenimiento", commands: ["clean", "sync", "telemetry", "report-issue"] },
@@ -284,6 +284,31 @@ export function registerMeta(program, { pkgVersion }) {
284
284
  });
285
285
  });
286
286
 
287
+ // KJC-TSK-0733 PL-A — policy as code: motor determinista en modo warn.
288
+ const policyCmd = program.command("policy").description("Policy as code (.karajan/policy.yml, vocabulario cerrado) — PL-A: modo warn");
289
+ policyCmd.command("eval")
290
+ .description("Evalúa UNA tool call: imprime {decision, rule_id, reason}; --strict devuelve exit 2 en deny (contrato para adaptadores de hooks)")
291
+ .option("--role <role>", "Rol del agente", "coder")
292
+ .option("--tool <tool>", "Tool (Write, Edit, Bash…)")
293
+ .option("--input <json>", "tool_input en JSON")
294
+ .option("--strict", "Exit 2 si deny")
295
+ .action(async (flags) => {
296
+ await withConfig(pkgVersion, "policy-eval", flags, async ({ config }) => {
297
+ const { policyCommand } = await import("../commands/policy.js");
298
+ process.exitCode = await policyCommand({ action: "eval", config, flags });
299
+ });
300
+ });
301
+ policyCmd.command("check")
302
+ .description("Comprueba el diff STAGED contra la policy (write-deny + invariantes) — siempre warn en PL-A; exit 1 solo si policy.yml es inválido")
303
+ .option("--role <role>", "Rol del agente", "coder")
304
+ .option("--json", "Machine-readable")
305
+ .action(async (flags) => {
306
+ await withConfig(pkgVersion, "policy-check", flags, async ({ config }) => {
307
+ const { policyCommand } = await import("../commands/policy.js");
308
+ process.exitCode = await policyCommand({ action: "check", config, flags });
309
+ });
310
+ });
311
+
287
312
  // KJC-TSK-0713 — the Sentinel: the method state the harness hooks record.
288
313
  const sentinel = program.command("sentinel").description("Deterministic method supervisor wired into the harness hooks (Claude Code)");
289
314
  sentinel.command("status")
@@ -0,0 +1,70 @@
1
+ /**
2
+ * `kj policy` — PL-A parte 3b (KJC-TSK-0733, épica KJC-PCS-0074). El motor
3
+ * en modo WARN: eval de una tool call y check del diff staged contra
4
+ * `.karajan/policy.yml`. En PL-A nada bloquea salvo un fichero de policy
5
+ * inválido (exit 1: declarar lo inaplicable es error del fichero, no aviso).
6
+ * `eval --strict` devuelve exit 2 en deny — el contrato para los adaptadores
7
+ * de hooks de PL-B. Los dientes de check llegan en PL-B/PL-C.
8
+ */
9
+ import { execFile } from "node:child_process";
10
+ import { promisify } from "node:util";
11
+ import { checkStagedDiff, evalToolCall, loadPolicy } from "../policy/engine.js";
12
+
13
+ const execFileAsync = promisify(execFile);
14
+
15
+ async function stagedFacts(projectDir, gitFn) {
16
+ const run =
17
+ gitFn ||
18
+ (async (args) => (await execFileAsync("git", args, { cwd: projectDir, maxBuffer: 16 * 1024 * 1024 })).stdout);
19
+ const files = (await run(["diff", "--cached", "--name-only"]))
20
+ .split("\n")
21
+ .map((s) => s.trim())
22
+ .filter(Boolean);
23
+ let net = 0;
24
+ for (const line of (await run(["diff", "--cached", "--numstat"])).split("\n")) {
25
+ const [a, r] = line.trim().split(/\s+/);
26
+ if (a && a !== "-") net += Number(a) || 0;
27
+ if (r && r !== "-") net -= Number(r) || 0;
28
+ }
29
+ return { files, netLinesAdded: net };
30
+ }
31
+
32
+ export async function policyCommand({ action, config = {}, flags = {}, logger = console, deps = {} }) {
33
+ const projectDir = config?.projectDir || process.cwd();
34
+ const { policy, errors } = loadPolicy({ projectDir, deps });
35
+ if (errors.length > 0) {
36
+ for (const e of errors) logger.error?.(`✗ ${e}`);
37
+ logger.error?.("policy: el fichero declara lo que el motor no puede aplicar — corrígelo (una regla que miente es peor que ninguna)");
38
+ return 1;
39
+ }
40
+
41
+ if (action === "eval") {
42
+ let input;
43
+ try {
44
+ input = flags.input ? JSON.parse(flags.input) : {};
45
+ } catch {
46
+ logger.error?.("policy eval: --input debe ser JSON válido");
47
+ return 1;
48
+ }
49
+ const verdict = evalToolCall(policy, { role: flags.role || "coder", tool: flags.tool, input, root: projectDir });
50
+ logger.info?.(JSON.stringify(verdict));
51
+ return verdict.decision === "deny" && flags.strict ? 2 : 0;
52
+ }
53
+
54
+ // check — sobre el diff staged (único modo en PL-A), SIEMPRE warn.
55
+ const facts = await stagedFacts(projectDir, deps.gitFn);
56
+ const violations = checkStagedDiff(policy, { role: flags.role || "coder", ...facts });
57
+ if (flags.json) {
58
+ logger.info?.(JSON.stringify({ mode: "warn", violations }));
59
+ return 0;
60
+ }
61
+ if (violations.length === 0) {
62
+ logger.info?.("policy check: limpio");
63
+ return 0;
64
+ }
65
+ for (const v of violations) {
66
+ logger.warn?.(`⚠ policy [${v.rule_id}] ${v.reason}${v.file ? ` (${v.file})` : ""}`);
67
+ }
68
+ logger.warn?.(`policy check: ${violations.length} aviso(s) — modo warn (PL-A): no bloquea; PL-B le dará dientes`);
69
+ return 0;
70
+ }
@@ -49,6 +49,21 @@ export const branchOf = () => {
49
49
  try { return execSync("git rev-parse --abbrev-ref HEAD", { cwd: ROOT, stdio: ["ignore", "pipe", "ignore"] }).toString().trim(); }
50
50
  catch { return null; }
51
51
  };
52
+ const _git = (args, cwd) => {
53
+ try { return execSync("git " + args, { cwd, stdio: ["ignore", "pipe", "ignore"] }).toString().trim(); } catch { return ""; }
54
+ };
55
+ let _home;
56
+ // MONO-0 (ADR 0002): lane identity = worktree toplevel; repo identity =
57
+ // git-common-dir. Same repo but another toplevel than OUR root => foreign
58
+ // lane (returns its toplevel); anything else => null.
59
+ export const foreignLane = (targetDir) => {
60
+ const top = _git("rev-parse --show-toplevel", targetDir);
61
+ if (!top) return null;
62
+ _home ||= { top: _git("rev-parse --show-toplevel", ROOT), common: _git("rev-parse --path-format=absolute --git-common-dir", ROOT) };
63
+ if (!_home.top || top === _home.top) return null;
64
+ const common = _git("rev-parse --path-format=absolute --git-common-dir", targetDir);
65
+ return common && common === _home.common ? top : null;
66
+ };
52
67
  export const violations = (s, branch) => {
53
68
  const v = [];
54
69
  if (!s || !(s.edited_sources || []).length) return v;
@@ -177,9 +192,10 @@ const PRETOOL_BODY = `#!/usr/bin/env node
177
192
  // before the damage, not in the post-mortem. Exit 2 blocks (stderr says the
178
193
  // remediation); read-only tools are never wired here; every honored escape
179
194
  // is recorded as an auditable event. Fails OPEN on anything unexpected.
180
- import { relative } from "node:path";
195
+ import { dirname, relative, resolve } from "node:path";
196
+ import { existsSync, realpathSync, statSync } from "node:fs";
181
197
  import { spawnSync } from "node:child_process";
182
- import { CODE, TESTS, ROOT, BASE_BRANCHES, CARD, branchOf, load, violations, recordEscape } from "./sentinel-lib.mjs";
198
+ import { CODE, TESTS, ROOT, BASE_BRANCHES, CARD, branchOf, foreignLane, load, violations, recordEscape } from "./sentinel-lib.mjs";
183
199
  const EDIT_TOOLS = ["Write", "Edit", "MultiEdit", "NotebookEdit"];
184
200
  const PUBLISH = /\\bnpm\\s+publish\\b|\\bfirebase\\s+deploy\\b|\\bgh\\s+release\\s+create\\b/;
185
201
  const PUSH = /\\bgit\\s+push\\b/;
@@ -210,6 +226,109 @@ process.stdin.on("end", () => {
210
226
  process.exit(2);
211
227
  }
212
228
  if (process.env.KJ_SENTINEL_OFF === "1") process.exit(0);
229
+ // MONO-0 (KJC-TSK-0737, ADR 0002): cada sesion muta solo SU worktree;
230
+ // otro carril del MISMO repo se deniega ANTES del dano. Lectura libre.
231
+ const laneOf = (p) => {
232
+ // Sube al primer ancestro EXISTENTE (un subdir aun no creado escapaba).
233
+ let d = String(p);
234
+ if (!(existsSync(d) && statSync(d).isDirectory())) d = dirname(d);
235
+ while (d && !existsSync(d)) {
236
+ const up = dirname(d);
237
+ if (up === d) return null;
238
+ d = up;
239
+ }
240
+ // Canonicaliza (reviewer catch: un symlink local hacia otro carril
241
+ // esquivaba la clasificacion por ruta literal).
242
+ try { d = realpathSync(d); } catch { /* se clasifica tal cual */ }
243
+ return d ? foreignLane(d) : null;
244
+ };
245
+ const laneDeny = (what, lane) => {
246
+ if (process.env.KJ_ALLOW_CROSS_LANE === "1") { recordEscape(sid, "KJ_ALLOW_CROSS_LANE", tool); return false; }
247
+ console.error("kj sentinel: " + what + " vive en otro carril (" + lane + ") de este repo — cada sesion muta solo SU worktree (MONO-0). Cruce deliberado: KJ_ALLOW_CROSS_LANE=1, queda registrado.");
248
+ return true;
249
+ };
250
+ if (EDIT_TOOLS.includes(tool)) {
251
+ const target = input.file_path || input.notebook_path;
252
+ const lane = target ? laneOf(target) : null;
253
+ if (lane && laneDeny(String(target), lane)) process.exit(2);
254
+ }
255
+ if (tool === "Bash") {
256
+ const cmd = String(input.command || "");
257
+ // Deny-unless-known-read-only: un allowlist de verbos mutadores era
258
+ // esquivable (dd, interpretes). Solo un comando SIMPLE de lectura (sin
259
+ // encadenar ni redirigir) puede nombrar otro carril; leer carriles es
260
+ // trabajo de las tools Read/Grep, como con los ficheros del supervisor.
261
+ // Solo LITERALES: expansión/sustitución en un comando "de lectura"
262
+ // puede ejecutar cualquier cosa (reviewer catch: grep foo $(rm ...)).
263
+ // (find fuera: -delete/-exec mutan — reviewer catch; sus tokens de
264
+ // carril los caza el escaner de abajo.)
265
+ const READONLY = /^[ \\t]*(grep|rg|cat|head|tail|less|ls|wc|diff|stat|file|du|tree|git (log|show|diff|status|blame))\\b[^;|&<>$\`(){}\\n\\r]*$/;
266
+ if (!READONLY.test(cmd)) {
267
+ // cd/pushd invalida TODO razonamiento textual de rutas posteriores
268
+ // (carrera de bypasses confirmada en review: destino bare, con $,
269
+ // relativas post-cd...): en un comando NO-read-only, cambiar de
270
+ // directorio se deniega conservadoramente — el remedio es no
271
+ // necesitarlo (git -C, npm --prefix, rutas absolutas).
272
+ if (/(^|[;&|(\\s])(cd|pushd)([ \\t]|$)/.test(cmd)) {
273
+ if (process.env.KJ_ALLOW_CROSS_LANE === "1") { recordEscape(sid, "KJ_ALLOW_CROSS_LANE", tool); }
274
+ else {
275
+ console.error("kj sentinel: cd/pushd en un comando mutador — las rutas posteriores no son verificables por el guard de carriles (MONO-0); usa git -C / npm --prefix / rutas ABSOLUTAS sin cd (o KJ_ALLOW_CROSS_LANE=1, queda registrado).");
276
+ process.exit(2);
277
+ }
278
+ }
279
+ // Absolute AND dot-relative tokens (../lane escapaba al primer scan —
280
+ // reviewer catch); relative se resuelve contra el cwd de la sesion.
281
+ // EVERY token (un tope seria un bypass); dedup acota el coste git.
282
+ // Fallo de solomon (seguridad no arbitrable) + carrera de bypasses:
283
+ // la expansion ($, backtick) solo se tolera en segmentos que son
284
+ // asignaciones puras o runners del toolchain propio (npm/pnpm/yarn/
285
+ // vitest/jest/kj/gh/echo/printf, git sin -C/--git-dir/--work-tree) —
286
+ // sus argumentos son datos. En herramientas genericas de fichero o
287
+ // interpretes el objetivo queda oculto al texto => deny. Los paths
288
+ // LITERALES (tambien dentro de sustituciones) los escanea el bucle
289
+ // de tokens de abajo; el residuo (expansion anidada en segmentos
290
+ // runner) queda documentado como en la regla de ficheros PROTECTED.
291
+ if (/>{1,2}[ \\t]*["']?[\\$\`]/.test(cmd)) {
292
+ if (process.env.KJ_ALLOW_CROSS_LANE === "1") { recordEscape(sid, "KJ_ALLOW_CROSS_LANE", tool); }
293
+ else {
294
+ console.error("kj sentinel: redireccion con destino tras variable/sustitucion — el guard de carriles no puede verificarlo (MONO-0); usa una ruta LITERAL (o KJ_ALLOW_CROSS_LANE=1, queda registrado).");
295
+ process.exit(2);
296
+ }
297
+ }
298
+ // Sustitucion de comandos ($(...) o backticks) EJECUTA su contenido
299
+ // en cualquier posicion, y una ruta entrecomillada con espacios es
300
+ // invisible al tokenizador: ambas => deny conservador (endpoint
301
+ // pedido por el reviewer; friccion asumida, el remedio es literal).
302
+ if (/\\$\\(|\`/.test(cmd) || /["'][^"'\\n]*\\/[^"'\\n]* [^"'\\n]*["']/.test(cmd)) {
303
+ if (process.env.KJ_ALLOW_CROSS_LANE === "1") { recordEscape(sid, "KJ_ALLOW_CROSS_LANE", tool); }
304
+ else {
305
+ console.error("kj sentinel: sustitucion de comandos o ruta entrecomillada con espacios en un comando mutador — no verificable por el guard de carriles (MONO-0); usa valores/rutas LITERALES sin sustitucion (o KJ_ALLOW_CROSS_LANE=1, queda registrado).");
306
+ process.exit(2);
307
+ }
308
+ }
309
+ const SAFE_EXP_SEG = /^([A-Za-z_][A-Za-z0-9_]*=[^ \\t]*[ \\t]*)*((npm|pnpm|yarn|vitest|jest|kj|gh|echo|printf|true|test)\\b|git[ \\t](?![^\\n]*(-C[ \\t]|--git-dir|--work-tree)))[^;|&\\n]*$|^[A-Za-z_][A-Za-z0-9_]*=[^;|&\\n]*$/;
310
+ const segs = cmd.split(/&&|\\|\\||[;|\\n]/).map((s) => s.trim()).filter(Boolean);
311
+ if (!segs.every((s) => !/[\\$]/.test(s) || SAFE_EXP_SEG.test(s))) {
312
+ if (process.env.KJ_ALLOW_CROSS_LANE === "1") { recordEscape(sid, "KJ_ALLOW_CROSS_LANE", tool); }
313
+ else {
314
+ console.error("kj sentinel: expansion de shell en una herramienta generica de fichero/interprete — el objetivo no es verificable por el guard de carriles (MONO-0); usa rutas LITERALES o un runner del toolchain (o KJ_ALLOW_CROSS_LANE=1, queda registrado).");
315
+ process.exit(2);
316
+ }
317
+ }
318
+ // ...incluidas relativas A PELO (los carriles kj viven en
319
+ // .kj/worktrees/). Un falso token (s/a/b/) resuelve al arbol propio
320
+ // y foreignLane da null — solo deniega lo que CAE en otro carril.
321
+ const seen = new Set();
322
+ for (const m of cmd.matchAll(/(?:^|[\\s"'=;(><])((?:\\.\\.?\\/)[^\\s"'\`;|&)]*|\\/[^\\s"'\`;|&)]+|~\\/[^\\s"'\`;|&)]+|[A-Za-z0-9_.@-]+\\/[^\\s"'\`;|&)]+)/g)) {
323
+ const t = m[1].startsWith("~/") ? (process.env.HOME || "~") + m[1].slice(1) : m[1];
324
+ const abs = resolve(t);
325
+ if (seen.has(abs)) continue;
326
+ seen.add(abs);
327
+ const lane = laneOf(abs);
328
+ if (lane && laneDeny(m[1], lane)) process.exit(2);
329
+ }
330
+ }
331
+ }
213
332
  if (EDIT_TOOLS.includes(tool)) {
214
333
  const file = input.file_path || input.notebook_path;
215
334
  const rel = file ? relative(ROOT, String(file)).replaceAll("\\\\", "/") : "";
@@ -0,0 +1,276 @@
1
+ /**
2
+ * policy engine — PL-A (KJC-TSK-0733, épica KJC-PCS-0074, ADR 0001).
3
+ * Policy as DATA (`.karajan/policy.yml`, vocabulario CERRADO), engine as
4
+ * code: módulo puro, determinista, sin red ni LLM. Una regla que el motor
5
+ * no puede aplicar falla FUERTE al cargar — jamás se ignora en silencio.
6
+ * Deny gana a allow; una allow-list convierte el exterior en denegación.
7
+ * Consumidores: kj policy (PL-A) → gates PL-B → CI PL-C → middleware PL-D.
8
+ */
9
+ import { readFileSync } from "node:fs";
10
+ import { isAbsolute, join, posix, relative } from "node:path";
11
+ import yaml from "js-yaml";
12
+ import { commandPatternToRegExp, matchesAny } from "./glob.js";
13
+
14
+ // Vocabulario CERRADO e incremental: cada capacidad entra al set EN el PR
15
+ // que trae su evaluación — declararla antes sería una regla que miente.
16
+ const ROLE_CAPS = new Set(["write", "shell"]);
17
+ const INVARIANT_KINDS = new Set(["diff-threshold"]);
18
+ const INVARIANT_METRICS = new Set(["net_lines_added"]);
19
+ const WRITE_TOOLS = new Set(["Write", "Edit", "MultiEdit", "NotebookEdit"]);
20
+ // Registro de tools conocidas (fallo de solomon: lo desconocido no se
21
+ // permite a un rol DECLARADO — crece por PR consciente).
22
+ const READONLY_TOOLS = new Set(["Read", "Grep", "Glob", "LS", "WebFetch", "WebSearch"]);
23
+ export const DEFAULT_POLICY = Object.freeze({ version: 1, roles: {}, invariants: [] });
24
+
25
+ function defaultReadFile(projectDir) {
26
+ try {
27
+ return readFileSync(join(projectDir, ".karajan", "policy.yml"), "utf8");
28
+ } catch (err) {
29
+ // Solo ENOENT es "sin policy"; EACCES/EIO seria un fallback silencioso.
30
+ if (err.code === "ENOENT") return null;
31
+ throw err;
32
+ }
33
+ }
34
+
35
+ /**
36
+ * @returns {{policy: object, errors: string[]}} errors non-empty ⇒ the file
37
+ * declares something this engine cannot enforce — callers must surface it.
38
+ */
39
+ export function loadPolicy({ projectDir = process.cwd(), deps = {} } = {}) {
40
+ const { readFile = defaultReadFile } = deps;
41
+ let raw;
42
+ try {
43
+ raw = readFile(projectDir);
44
+ } catch (err) {
45
+ return { policy: DEFAULT_POLICY, errors: [`policy.yml: ilegible (${err.code || err.message}) — presente pero no leible NO es "sin policy"`] };
46
+ }
47
+ if (raw == null) return { policy: DEFAULT_POLICY, errors: [] };
48
+
49
+ const errors = [];
50
+ let doc;
51
+ try {
52
+ doc = yaml.load(raw) || {};
53
+ } catch (err) {
54
+ return { policy: DEFAULT_POLICY, errors: [`policy.yml: YAML invalido — ${err.message}`] };
55
+ }
56
+ if (typeof doc !== "object" || Array.isArray(doc)) {
57
+ return { policy: DEFAULT_POLICY, errors: ["policy.yml: la raiz debe ser un objeto"] };
58
+ }
59
+ if (doc.version !== 1) {
60
+ errors.push(`policy.yml: version "${doc.version}" no soportada — este motor habla version 1`);
61
+ }
62
+ const roles = doc.roles ?? {};
63
+ if (typeof roles !== "object" || roles === null || Array.isArray(roles)) {
64
+ errors.push("policy.yml: roles debe ser un objeto");
65
+ } else {
66
+ validateRoles(roles, errors);
67
+ }
68
+ const invs = doc.invariants ?? [];
69
+ if (Array.isArray(invs)) {
70
+ for (const inv of invs) {
71
+ if (!INVARIANT_KINDS.has(inv?.kind)) {
72
+ errors.push(`policy.yml: invariant "${inv?.id}" con kind "${inv?.kind}" no existe en el vocabulario v1`);
73
+ } else if (!INVARIANT_METRICS.has(inv?.metric) || !Number.isFinite(inv?.max) || typeof inv?.id !== "string") {
74
+ errors.push(`policy.yml: invariant "${inv?.id}" requiere id string, metric (${[...INVARIANT_METRICS].join(", ")}) y max numérico`);
75
+ }
76
+ }
77
+ } else {
78
+ errors.push("policy.yml: invariants debe ser una lista");
79
+ }
80
+ return { policy: { roles: {}, invariants: [], ...doc }, errors };
81
+ }
82
+
83
+ function validateRoles(roles, errors) {
84
+ for (const [role, caps] of Object.entries(roles)) {
85
+ for (const [cap, spec] of Object.entries(caps || {})) {
86
+ if (!ROLE_CAPS.has(cap)) {
87
+ errors.push(`policy.yml: roles.${role}.${cap} no existe en el vocabulario (v1: ${[...ROLE_CAPS].join(", ")})`);
88
+ continue;
89
+ }
90
+ if (typeof spec !== "object" || spec === null || Array.isArray(spec)) {
91
+ errors.push(`policy.yml: roles.${role}.${cap} debe ser un objeto {allow, deny}`);
92
+ continue;
93
+ }
94
+ for (const key of Object.keys(spec)) {
95
+ if (key !== "allow" && key !== "deny") {
96
+ errors.push(`policy.yml: roles.${role}.${cap}.${key} no existe en el vocabulario (v1: allow, deny)`);
97
+ }
98
+ }
99
+ for (const list of ["allow", "deny"]) {
100
+ const v = spec?.[list];
101
+ if (v !== undefined && (!Array.isArray(v) || v.some((g) => typeof g !== "string"))) {
102
+ errors.push(`policy.yml: roles.${role}.${cap}.${list} debe ser una lista de strings (glob)`);
103
+ }
104
+ }
105
+ }
106
+ }
107
+ }
108
+
109
+ const deny = (rule_id, reason) => ({ decision: "deny", rule_id, reason });
110
+ const ALLOW = Object.freeze({ decision: "allow" });
111
+
112
+ // Normaliza el destino a ruta RELATIVA canónica: absolutas se relativizan
113
+ // contra root (sin root ⇒ null), y `..`/escapes ⇒ null (reviewer catch:
114
+ // src/../.env no puede esquivar los globs, ni una absoluta salirse del root).
115
+ function normalizeTarget(filePath, root) {
116
+ let p = String(filePath).replaceAll("\\", "/");
117
+ if (isAbsolute(p)) {
118
+ if (!root) return null;
119
+ p = relative(root, p).replaceAll("\\", "/");
120
+ }
121
+ p = posix.normalize(p).replace(/^\.\//, "");
122
+ return p.startsWith("..") || isAbsolute(p) ? null : p;
123
+ }
124
+
125
+ function evalWrite(policy, role, filePath, root) {
126
+ const write = policy.roles?.[role]?.write;
127
+ if (!write) return ALLOW;
128
+ // Rol restringido + destino ausente/no-canonizable = no verificable ⇒ deny
129
+ // (reviewer catches: tool call malformado, traversal, absoluta sin root).
130
+ const p = filePath ? normalizeTarget(filePath, root) : null;
131
+ if (p == null) return deny(`roles.${role}.write`, `destino "${filePath ?? ""}" no verificable contra la policy del rol ${role}`);
132
+ if (matchesAny(p, write.deny)) {
133
+ return deny(`roles.${role}.write.deny`, `${p} esta denegado para el rol ${role}`);
134
+ }
135
+ if (Array.isArray(write.allow) && !matchesAny(p, write.allow)) {
136
+ return deny(`roles.${role}.write.allow`, `${p} esta fuera de la allow-list de escritura del rol ${role}`);
137
+ }
138
+ return ALLOW;
139
+ }
140
+
141
+ // Launchers y asignaciones de entorno se PELAN antes de casar patrones
142
+ // (reviewer catch: "sudo git push" o "FOO=1 git add -A" no esquivan la
143
+ // regla del subcomando real); capas excesivas ⇒ opaco ⇒ deny.
144
+ const LAUNCHER_RE = /^(sudo(\s+-\S+)*|command|nohup|time|nice(\s+-n\s*\d+)?|stdbuf\s+\S+|env)\s+|^([A-Za-z_][A-Za-z0-9_]*=\S*\s+)+/;
145
+ function stripLaunchers(seg) {
146
+ let s = seg;
147
+ for (let i = 0; i < 5; i++) {
148
+ const m = s.match(LAUNCHER_RE);
149
+ if (!m) return s;
150
+ // Asignaciones que alteran QUÉ ejecutable resuelve (PATH, LD_*, GIT_*,
151
+ // NODE_OPTIONS) o llevan expansión son opacas — no se pelan, denegan.
152
+ if (/^(PATH|LD_\w*|NODE_OPTIONS|GIT_\w*)=/.test(m[0]) || m[0].includes("$")) return null;
153
+ s = s.slice(m[0].length);
154
+ }
155
+ return null; // sigue envuelto tras 5 capas: no verificable
156
+ }
157
+
158
+ // Tokenizador consciente de comillas: separa segmentos SOLO por operadores
159
+ // no entrecomillados (reviewer catch: un ';' dentro de un mensaje no es
160
+ // separador) y canonicaliza tokens sin comillas a la vez. Cualquier
161
+ // construcción que ejecute o redirija texto opaco al análisis — escapes,
162
+ // expansión ($ salvo entre comillas simples), sustitución/backticks,
163
+ // redirecciones, backgrounding, comillas sin cerrar — devuelve null.
164
+ function parseCommand(cmd) {
165
+ const segs = [];
166
+ let toks = [];
167
+ let cur = "";
168
+ let q = null;
169
+ const pushTok = () => {
170
+ if (cur) {
171
+ toks.push(cur);
172
+ cur = "";
173
+ }
174
+ };
175
+ const pushSeg = () => {
176
+ pushTok();
177
+ if (toks.length > 0) {
178
+ segs.push(toks.join(" "));
179
+ toks = [];
180
+ }
181
+ };
182
+ const s = String(cmd);
183
+ for (let i = 0; i < s.length; i++) {
184
+ const ch = s[i];
185
+ if (ch === "\\" || ch === "`") return null;
186
+ if (ch === "$" && q !== "'") return null;
187
+ if (q) {
188
+ if (ch === q) q = null;
189
+ else cur += ch;
190
+ continue;
191
+ }
192
+ if (ch === '"' || ch === "'") {
193
+ q = ch;
194
+ continue;
195
+ }
196
+ if (ch === ">" || ch === "<") return null;
197
+ if (ch === "&" && s[i + 1] === "&") {
198
+ pushSeg();
199
+ i++;
200
+ continue;
201
+ }
202
+ if (ch === "|" && s[i + 1] === "|") {
203
+ pushSeg();
204
+ i++;
205
+ continue;
206
+ }
207
+ if (ch === "&") return null;
208
+ if (ch === ";" || ch === "|" || ch === "\n") {
209
+ pushSeg();
210
+ continue;
211
+ }
212
+ if (/\s/.test(ch)) {
213
+ pushTok();
214
+ continue;
215
+ }
216
+ cur += ch;
217
+ }
218
+ if (q) return null;
219
+ pushSeg();
220
+ return segs;
221
+ }
222
+
223
+ // Envolturas que re-ejecutan texto (lección del guard del Sentinel).
224
+ const WRAPPER_RE = /^(sh|bash|zsh|dash|ksh)\s+(-\S+\s+)*-\S*c(\s|$)|^eval\b|^xargs\b|^source\b|^\.\s/;
225
+
226
+ // Deny si CUALQUIER segmento casa; con allow-list, CADA segmento debe casar.
227
+ function evalShell(policy, role, command) {
228
+ const shell = policy.roles?.[role]?.shell;
229
+ if (!shell) return ALLOW;
230
+ if (!command) return deny(`roles.${role}.shell`, `comando ausente — no verificable para el rol ${role}`);
231
+ const segs = parseCommand(command);
232
+ if (segs == null) {
233
+ return deny(`roles.${role}.shell`, "construcciones opacas (escapes, expansión, sustitución, redirecciones, backgrounding o comillas sin cerrar) — no verificable");
234
+ }
235
+ for (const raw of segs) {
236
+ const seg = stripLaunchers(raw);
237
+ if (seg == null) return deny(`roles.${role}.shell`, `el segmento "${raw}" no es canonicalizable — no verificable`);
238
+ if (WRAPPER_RE.test(seg)) return deny(`roles.${role}.shell`, `el segmento "${seg}" re-ejecuta texto (envoltura/eval) — no verificable`);
239
+ const hit = Array.isArray(shell.deny) && shell.deny.find((p) => commandPatternToRegExp(p).test(seg));
240
+ if (hit) return deny(`roles.${role}.shell.deny`, `el segmento "${seg}" casa con el patrón denegado "${hit}"`);
241
+ if (Array.isArray(shell.allow) && !shell.allow.some((p) => commandPatternToRegExp(p).test(seg))) {
242
+ return deny(`roles.${role}.shell.allow`, `el segmento "${seg}" está fuera de la allow-list de shell del rol ${role}`);
243
+ }
244
+ }
245
+ return ALLOW;
246
+ }
247
+
248
+ /** One tool call against the policy. `root` permite evaluar file_path
249
+ * absolutos. Un rol DECLARADO solo usa tools del registro: lo desconocido
250
+ * es no-verificable ⇒ deny (roles sin declarar quedan como hoy). */
251
+ export function evalToolCall(policy, { role = "coder", tool, input = {}, root = null }) {
252
+ if (WRITE_TOOLS.has(tool)) return evalWrite(policy, role, input.file_path || input.notebook_path, root);
253
+ if (tool === "Bash") return evalShell(policy, role, input.command);
254
+ if (READONLY_TOOLS.has(tool) || !policy.roles?.[role]) return ALLOW;
255
+ return deny(`roles.${role}.tools`, `tool "${tool}" fuera del registro de la policy — no verificable para el rol declarado ${role}`);
256
+ }
257
+
258
+ /** El gate del RESULTADO: ficheros del diff + métricas contra rol e invariantes. */
259
+ export function checkStagedDiff(policy, { role = "coder", files = [], netLinesAdded = null } = {}) {
260
+ const violations = [];
261
+ for (const f of files) {
262
+ const r = evalWrite(policy, role, f);
263
+ if (r.decision === "deny") violations.push({ rule_id: r.rule_id, reason: r.reason, file: f });
264
+ }
265
+ for (const inv of policy.invariants || []) {
266
+ if (inv.kind !== "diff-threshold" || inv.metric !== "net_lines_added") continue;
267
+ if (!Number.isFinite(netLinesAdded)) {
268
+ // Métrica ausente con invariante declarado = no verificable, jamás un
269
+ // pase silencioso (reviewer catch).
270
+ violations.push({ rule_id: inv.id, reason: "net_lines_added no disponible — el invariante no es verificable sin la métrica" });
271
+ } else if (netLinesAdded > inv.max) {
272
+ violations.push({ rule_id: inv.id, reason: `net_lines_added=${netLinesAdded} supera el máximo ${inv.max}` });
273
+ }
274
+ }
275
+ return violations;
276
+ }
@@ -0,0 +1,44 @@
1
+ // glob → RegExp anclada — primitivo de la policy layer (PL-A, KJC-TSK-0733).
2
+ // Semántica: doble asterisco cruza directorios; doble asterisco + barra es
3
+ // prefijo de directorio OPCIONAL (así "**" "/*.env*" casa también con .env
4
+ // en la raíz); "*" no sale de su segmento; "?" es un carácter. Sin
5
+ // dependencias: el vocabulario es pequeño a propósito — una lib general
6
+ // traería semánticas que la policy no declara.
7
+ export function globToRegExp(glob) {
8
+ let re = "";
9
+ const s = String(glob);
10
+ let i = 0;
11
+ while (i < s.length) {
12
+ const c = s[i];
13
+ if (c === "*" && s[i + 1] === "*" && s[i + 2] === "/") {
14
+ re += "(?:.*/)?";
15
+ i += 3;
16
+ } else if (c === "*" && s[i + 1] === "*") {
17
+ re += ".*";
18
+ i += 2;
19
+ } else if (c === "*") {
20
+ re += "[^/]*";
21
+ i += 1;
22
+ } else if (c === "?") {
23
+ re += "[^/]";
24
+ i += 1;
25
+ } else {
26
+ re += c.replaceAll(/[.+^${}()|[\]\\]/g, String.raw`\$&`);
27
+ i += 1;
28
+ }
29
+ }
30
+ return new RegExp(`^${re}$`);
31
+ }
32
+
33
+ /** true si `value` casa con ALGUNO de los globs (lista no-array ⇒ false). */
34
+ export const matchesAny = (value, globs) =>
35
+ Array.isArray(globs) && globs.some((g) => globToRegExp(g).test(value));
36
+
37
+ /** Patrón de comando shell → RegExp anclada al INICIO: `*` = cualquier cosa
38
+ * (los comandos no son rutas — sin semántica de segmentos). */
39
+ export function commandPatternToRegExp(pattern) {
40
+ const re = String(pattern)
41
+ .replaceAll(/[.+^${}()|[\]\\?]/g, String.raw`\$&`)
42
+ .replaceAll("*", ".*");
43
+ return new RegExp(`^${re}`);
44
+ }