#!/usr/bin/env node // post-session-guard.mjs — Runtime trifecta detection (Rule of Two) // v5.0: Configurable TRIFECTA_MODE (block|warn|off), long-horizon 100-call window, // behavioral drift via Jensen-Shannon divergence import { readFileSync, appendFileSync } from 'node:fs'; const TRIFECTA_MODE = (process.env.LLM_SECURITY_TRIFECTA_MODE || 'warn').toLowerCase(); const SLIDING_WINDOW = 20; const LONG_HORIZON_WINDOW = 100; const input = JSON.parse(readFileSync('/dev/stdin', 'utf-8')); const toolName = input.tool_name || ''; // Classify tool function classifyTool(name) { if (/Read|Glob|Grep/.test(name)) return 'read'; if (/Write|Edit/.test(name)) return 'write'; if (/Bash/.test(name)) return 'exec'; if (/WebFetch|WebSearch/.test(name)) return 'network'; return 'other'; } // Jensen-Shannon divergence for behavioral drift detection function jsDivergence(p, q) { const m = p.map((pi, i) => (pi + q[i]) / 2); let kl1 = 0, kl2 = 0; for (let i = 0; i < p.length; i++) { if (p[i] > 0 && m[i] > 0) kl1 += p[i] * Math.log2(p[i] / m[i]); if (q[i] > 0 && m[i] > 0) kl2 += q[i] * Math.log2(q[i] / m[i]); } return (kl1 + kl2) / 2; } if (TRIFECTA_MODE === 'off') { process.stdout.write(JSON.stringify({ decision: 'allow' })); process.exit(0); } // Trifecta detection logic would go here (simplified for fixture) process.stdout.write(JSON.stringify({ decision: 'allow' }));