From 103c2b043f0da2693266e67206feb43b7e1759ae Mon Sep 17 00:00:00 2001 From: Hisku Date: Tue, 20 Oct 2026 11:17:34 +0100 Subject: [PATCH 1/2] fix(ENG-3282): swap the Tier-2 model and recalibrate its threshold Bundles candidate 3 (jbv2-mh-grid-agpjb2pinstr-s2, ONNX md5 3f23821d74d23b39a832f6fa30615e47) in place of v5. It stays in the minilm-multihead-v5 folder so consumers that reference that path keep working; the tokenizer and encoder config are unchanged. - Default single-head highRiskThreshold 0.64 -> 0.52 (T=2.41 unchanged). 0.52 matches the shipped false-positive rate. Against v5 at 0.64: org1 FP 1.11 vs 1.66, SFE FP 1.38 vs 4.15, agentic recall 44.5 vs 19.1, holdout recall 63.2 vs 64.0 (within noise). - Multihead presets for this model ship in classifier_config.json: High 0.77/0.325, Medium 0.88/0.325, Low 0.94/0.30. The old presets are wrong for it. - Tests that pinned 0.64 now pin 0.52; medium is now [0.5, 0.52). Release gates cleared 2026-10-14 (aux/preset re-tune, score parity, seed). Platform presets follow in connect-sdk (ENG-3393). Co-Authored-By: Claude Opus 5.5 --- README.md | 5 +-- specs/onnx-classifier.spec.ts | 2 +- specs/tier2-classifier.spec.ts | 23 ++++++------- specs/tier2-multihead.spec.ts | 4 +-- .../classifier_config.json | 34 +++++++++++++++++-- .../minilm-multihead-v5/model_quantized.onnx | 4 +-- 6 files changed, 49 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 62c1968..0af5560 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 (`main`/`aux`, block when `main >= main AND aux < aux`): 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 From cbde38169edeeafd580051517f804aaaf527dfbf Mon Sep 17 00:00:00 2001 From: Hisku Date: Tue, 20 Oct 2026 11:21:55 +0100 Subject: [PATCH 2/2] docs(ENG-3282): state the multihead preset rule with the threshold names Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0af5560..56e21a4 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ Fine-tuned multi-head MiniLM classifier with sentence-level analysis: - Fine-tuned MiniLM-L6-v2, int8 quantized (~22MB), bundled in the package — no external download needed - 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 (`main`/`aux`, block when `main >= main AND aux < aux`): 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`. +- 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)