diff --git a/README.md b/README.md index 62c1968..56e21a4 100644 --- a/README.md +++ b/README.md @@ -97,8 +97,9 @@ Regex-based detection that scores content and escalates risk — it does **not** Fine-tuned multi-head MiniLM classifier with sentence-level analysis: - Splits text into sentences and scores each one (0.0 = safe, 1.0 = injection) - Fine-tuned MiniLM-L6-v2, int8 quantized (~22MB), bundled in the package — no external download needed -- Bundled model is **multi-head** (variant `minilm-multihead-v5`). The auxiliary head identifies meta-discussion / documentation phrasing — under multi-head mode a chunk blocks only when `main >= mainThr AND aux < auxThr`, so docs that quote injection text aren't over-flagged. Reported on the result as `tier2AuxScore` and `tier2MultiheadBlocked`. -- The bundled model carries calibrated thresholds (`highRiskThreshold ≈ 0.64`) in its `classifier_config.json`; these override library defaults when the model is loaded. +- Bundled model is **multi-head** (variant `jbv2-mh-grid-agpjb2pinstr-s2` since 0.9.3; it still lives in the `minilm-multihead-v5` folder so existing paths keep working). The auxiliary head identifies meta-discussion / documentation phrasing — under multi-head mode a chunk blocks only when `main >= mainThr AND aux < auxThr`, so docs that quote injection text aren't over-flagged. Reported on the result as `tier2AuxScore` and `tier2MultiheadBlocked`. +- The bundled model carries calibrated thresholds (`highRiskThreshold` 0.52 at temperature 2.41) in its `classifier_config.json`; these override library defaults when the model is loaded. Thresholds are model-specific: 0.64 was right for the previous model, not this one. +- Multi-head presets tuned for this model, as `mainThreshold`/`auxThreshold` (a chunk blocks when its main score >= `mainThreshold` and its aux score < `auxThreshold`): High 0.77/0.325, Medium 0.88/0.325, Low 0.94/0.30. They're also in `classifier_config.json` under `multihead_presets`. - Catches attacks that evade pattern-based detection - Latency: ~10ms/sample (after model warmup) diff --git a/specs/onnx-classifier.spec.ts b/specs/onnx-classifier.spec.ts index 62eb5e8..ae48d9c 100644 --- a/specs/onnx-classifier.spec.ts +++ b/specs/onnx-classifier.spec.ts @@ -243,7 +243,7 @@ describe.skipIf(!!process.env.CI)("Tier2Classifier ONNX mode", () => { it("should return correct risk levels", () => { expect(classifier.getRiskLevel(0.9)).toBe("high"); - expect(classifier.getRiskLevel(0.6)).toBe("medium"); + expect(classifier.getRiskLevel(0.51)).toBe("medium"); expect(classifier.getRiskLevel(0.3)).toBe("low"); }); }); diff --git a/specs/tier2-classifier.spec.ts b/specs/tier2-classifier.spec.ts index e12cfb6..a7b9e05 100644 --- a/specs/tier2-classifier.spec.ts +++ b/specs/tier2-classifier.spec.ts @@ -105,7 +105,8 @@ describe("#Tier2Classifier", () => { const classifier = createTier2Classifier(); // act / assert - expect(classifier.getRiskLevel(0.6)).toBe("medium"); + // The bundled model's high line is 0.52, so medium is [0.5, 0.52). + expect(classifier.getRiskLevel(0.51)).toBe("medium"); }); it("returns low for scores below the medium threshold", () => { @@ -118,11 +119,10 @@ describe("#Tier2Classifier", () => { }); describe(".getConfig", () => { - // Since 0.7, the default model (v5) ships with calibration defaults in - // its classifier_config.json — Tier2Classifier auto-loads them, so the - // out-of-the-box highRiskThreshold reflects v5's calibrated threshold - // (0.64 = raw 0.8 at T=2.41). The legacy default (0.8) still applies - // for models without a calibration block (e.g. user-supplied paths). + // The bundled model ships calibration defaults in its classifier_config.json and + // Tier2Classifier auto-loads them: highRiskThreshold 0.52 at T=2.41 (ENG-3282; v5 + // used 0.64). The legacy default (0.8) still applies for models without a + // calibration block (e.g. user-supplied paths). it("returns the model calibration highRiskThreshold when present", () => { // arrange const classifier = createTier2Classifier(); @@ -131,12 +131,9 @@ describe("#Tier2Classifier", () => { const actual = classifier.getConfig(); // assert - // v5's classifier_config.json ships highRiskThreshold = 0.64 - // (math-equivalent to raw 0.8 at T=2.41). Assert the exact value so - // an accidentally-removed or malformed calibration block — which - // silently falls back to the library default 0.8 — fails this test - // instead of slipping through under a "any positive value" guard. - expect(actual.highRiskThreshold).toBeCloseTo(0.64, 2); + // Assert the exact value so an accidentally-removed or malformed calibration + // block, which silently falls back to the library default 0.8, fails this test. + expect(actual.highRiskThreshold).toBeCloseTo(0.52, 2); }); it("returns the configured mediumRiskThreshold", () => { @@ -166,7 +163,7 @@ describe("#Tier2Classifier", () => { highRiskThreshold: undefined, }); const actual = classifier.getConfig(); - expect(actual.highRiskThreshold).toBeCloseTo(0.64, 2); + expect(actual.highRiskThreshold).toBeCloseTo(0.52, 2); expect(actual.temperatureT).toBeCloseTo(2.41, 2); }); }); diff --git a/specs/tier2-multihead.spec.ts b/specs/tier2-multihead.spec.ts index 9378b9f..1bbdb0e 100644 --- a/specs/tier2-multihead.spec.ts +++ b/specs/tier2-multihead.spec.ts @@ -323,10 +323,10 @@ describe.skipIf(!!process.env.CI)("PromptDefense — Bug 3: tier2Score reflects describe.skipIf(!!process.env.CI)("Tier2Classifier — auto-load calibration from classifier_config.json", () => { it("reads calibration block when present in model dir", () => { - // v5's classifier_config.json sets { calibration: { temperatureT: 2.41, highRiskThreshold: 0.64 } } + // The bundled classifier_config.json sets { calibration: { temperatureT: 2.41, highRiskThreshold: 0.52 } } const tier2 = createTier2Classifier({ onnxModelPath: multiheadPath }); expect(tier2.getTemperature()).toBeCloseTo(2.41, 2); - expect(tier2.getConfig().highRiskThreshold).toBeCloseTo(0.64, 2); + expect(tier2.getConfig().highRiskThreshold).toBeCloseTo(0.52, 2); }); it("user-provided config overrides model calibration defaults", () => { diff --git a/src/classifiers/models/minilm-multihead-v5/classifier_config.json b/src/classifiers/models/minilm-multihead-v5/classifier_config.json index e9624d4..1dea9ae 100644 --- a/src/classifiers/models/minilm-multihead-v5/classifier_config.json +++ b/src/classifiers/models/minilm-multihead-v5/classifier_config.json @@ -5,7 +5,7 @@ "hidden_size": 384, "freeze_layers": 4, "pooling": "mean", - "optimal_threshold": 0.4, + "optimal_threshold": 0.15, "datasets": [ "qualifire", "jayavibhav", @@ -37,11 +37,39 @@ "three_class": false, "multi_head": true, "aux_loss_alpha": 0.5, + "extra_benchmarks": [ + "hd-negatives-v1", + "hd-pairs-v1", + "hd-negatives-v2", + "hd-pairs-v2", + "hd-negatives-v4", + "hd-pairs-v4", + "hd-short-v1", + "hd-negatives-v5", + "hd-pairs-v5", + "hd-pairs-agent-v1" + ], + "variant": "jbv2-mh-grid-agpjb2pinstr-s2", "calibration": { "temperatureT": 2.41, - "highRiskThreshold": 0.64, + "highRiskThreshold": 0.52, "ece": 0.09, "fitted_on": "labeled plugin events 2026-05-13", - "notes": "Raw highRiskThreshold 0.8 is math-equivalent to calibrated 0.64 at T=2.41. tier2Score is reported as calibrated probability (post-sigmoid(logit/T))." + "notes": "tier2Score is the calibrated probability sigmoid(logit/T) at T=2.41. highRiskThreshold 0.52 is the single-head operating point for this model, chosen to match the shipped false-positive rate (v5 used 0.64). Do not reuse 0.64 with this model." + }, + "multihead_presets": { + "High": { + "mainThreshold": 0.77, + "auxThreshold": 0.325 + }, + "Medium": { + "mainThreshold": 0.88, + "auxThreshold": 0.325 + }, + "Low": { + "mainThreshold": 0.94, + "auxThreshold": 0.3 + }, + "notes": "Tuned 2026-10-14 for this model only (docs/2026-09-30-tier2-prompt-injection-identifiability.md, release gate 1). Do not reuse for another model." } } diff --git a/src/classifiers/models/minilm-multihead-v5/model_quantized.onnx b/src/classifiers/models/minilm-multihead-v5/model_quantized.onnx index 24a0136..d961fd9 100644 --- a/src/classifiers/models/minilm-multihead-v5/model_quantized.onnx +++ b/src/classifiers/models/minilm-multihead-v5/model_quantized.onnx @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:68685f34a646d66c53239d9ee54acd279803f507f70f86f9f99854e3f08368c8 -size 22984052 +oid sha256:3d4a1ba798c395c140202992c50e6c8de02fb1b9bd5aba6540848705b9c1e92c +size 22984191