Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion specs/onnx-classifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
23 changes: 10 additions & 13 deletions specs/tier2-classifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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();
Expand All @@ -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", () => {
Expand Down Expand Up @@ -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);
});
});
Expand Down
4 changes: 2 additions & 2 deletions specs/tier2-multihead.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"hidden_size": 384,
"freeze_layers": 4,
"pooling": "mean",
"optimal_threshold": 0.4,
"optimal_threshold": 0.15,
"datasets": [
"qualifire",
"jayavibhav",
Expand Down Expand Up @@ -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."
}
}
Git LFS file not shown