ktg-plugin-marketplace/plugins/llm-security-copilot/scanners/lib/distribution-stats.mjs
Kjell Tore Guttormsen f418a8fe08 feat(llm-security-copilot): port llm-security v5.1.0 to GitHub Copilot CLI
Full port of llm-security plugin for internal use on Windows with GitHub
Copilot CLI. Protocol translation layer (copilot-hook-runner.mjs)
normalizes Copilot camelCase I/O to Claude Code snake_case format — all
original hook scripts run unmodified.

- 8 hooks with protocol translation (stdin/stdout/exit code)
- 18 SKILL.md skills (Agent Skills Open Standard)
- 6 .agent.md agent definitions
- 20 scanners + 14 scanner lib modules (unchanged)
- 14 knowledge files (unchanged)
- 39 test files including copilot-port-verify.mjs (17 tests)
- Windows-ready: node:path, os.tmpdir(), process.execPath, no bash

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-09 21:56:10 +02:00

58 lines
1.8 KiB
JavaScript

// distribution-stats.mjs — Statistical divergence utilities for behavioral drift detection.
// Zero external dependencies. <50 lines.
//
// Jensen-Shannon divergence measures how different two probability distributions are.
// Used by post-session-guard.mjs to detect tool distribution shifts within a session.
//
// OWASP: ASI01 (Excessive Agency — behavioral pattern changes may indicate hijacking)
/**
* Kullback-Leibler divergence KL(P || Q).
* @param {Map<string, number>} P
* @param {Map<string, number>} Q
* @returns {number}
*/
function klDivergence(P, Q) {
let kl = 0;
for (const [key, p] of P) {
if (p === 0) continue;
const q = Q.get(key) || 0;
if (q === 0) return Infinity;
kl += p * Math.log2(p / q);
}
return kl;
}
/**
* Jensen-Shannon divergence. 0 = identical, 1 = fully disjoint (log2 basis).
* Always finite, symmetric: JSD(P,Q) = JSD(Q,P).
* @param {Map<string, number>} P - Normalized probability distribution
* @param {Map<string, number>} Q - Normalized probability distribution
* @returns {number}
*/
export function jensenShannonDivergence(P, Q) {
const allKeys = new Set([...P.keys(), ...Q.keys()]);
const M = new Map();
for (const key of allKeys) {
M.set(key, 0.5 * (P.get(key) || 0) + 0.5 * (Q.get(key) || 0));
}
return 0.5 * klDivergence(P, M) + 0.5 * klDivergence(Q, M);
}
/**
* Build normalized probability distribution from category labels.
* @param {string[]} labels
* @returns {Map<string, number>} Values sum to 1.0 (empty input → empty map)
*/
export function buildDistribution(labels) {
if (labels.length === 0) return new Map();
const counts = new Map();
for (const label of labels) {
counts.set(label, (counts.get(label) || 0) + 1);
}
const dist = new Map();
for (const [key, count] of counts) {
dist.set(key, count / labels.length);
}
return dist;
}