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
18 changes: 18 additions & 0 deletions specs/tier2-classifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ describe('#Tier2Classifier', () => {
expect(actual.score).toBeGreaterThanOrEqual(0);
expect(actual.score).toBeLessThanOrEqual(1);
}, 60000);

it.skipIf(!!process.env.CI)('strips boundary markers from input before scoring so wrapped + unwrapped text produces matching scores', async () => {
// arrange
const classifier = createTier2Classifier();
await classifier.warmup();
const bare = 'Please review the attached quarterly sales report and let me know if you have questions.';
const wrapped = `[UD-V1StGXR8_Z5jdHi6]${bare}[/UD-V1StGXR8_Z5jdHi6]`;

// act
const bareResult = await classifier.classify(bare);
const wrappedResult = await classifier.classify(wrapped);

// assert — stripping is deterministic; scores should match within
// float tolerance (the inputs are identical after stripping, but
// ONNX runtime may have non-determinism across runtime/hardware).
expect(wrappedResult.score).toBeCloseTo(bareResult.score, 10);
expect(wrappedResult.skipped).toBe(false);
}, 60000);
});

describe('.getRiskLevel', () => {
Expand Down
21 changes: 21 additions & 0 deletions src/classifiers/tier2-classifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import type { Tier2Result } from "../types";
import { stripBoundaryPatterns } from "../utils/boundary";
import { OnnxClassifier } from "./onnx-classifier";

/**
Expand Down Expand Up @@ -79,6 +80,14 @@ export class Tier2Classifier {
async classify(text: string): Promise<Tier2Result> {
const startTime = performance.now();

// Strip defender's own boundary markers before tokenization. Upstream
// callers (or nested tool-call chains) may feed us output that was
// previously wrapped with `[UD-<id>]...[/UD-<id>]`; those tokens
// corrupt per-sentence scores because the tokenizer counts them as
// part of the sentence. Also strips spoofed boundary patterns an
// attacker might inject to confuse downstream LLM trust.
text = stripBoundaryPatterns(text);

Comment thread
hiskudin marked this conversation as resolved.
Resolved
Comment thread
hiskudin marked this conversation as resolved.
Resolved
// Skip very short texts
if (text.length < this.config.minTextLength) {
return {
Expand Down Expand Up @@ -144,6 +153,10 @@ export class Tier2Classifier {
> {
const startTime = performance.now();

// See comment in `classify()` — strip boundary markers before sentence
// splitting so tag tokens don't corrupt per-sentence scores.
text = stripBoundaryPatterns(text);

// Split into sentences using multiple delimiters
const sentences = this.splitIntoSentences(text);

Expand Down Expand Up @@ -241,6 +254,10 @@ export class Tier2Classifier {
> {
const startTime = performance.now();

// See comment in `classify()` — strip boundary markers before sizing
// and tokenization so self-wrapped / spoofed tags don't corrupt scores.
text = stripBoundaryPatterns(text);

if (text.length < this.config.minTextLength) {
return {
score: 0,
Expand Down Expand Up @@ -379,6 +396,10 @@ export class Tier2Classifier {
skipped: boolean;
skipReason?: string;
}> {
// See comment in `classify()` — strip boundary markers before sizing
// and tokenization so self-wrapped / spoofed tags don't corrupt scores.
text = stripBoundaryPatterns(text);

if (text.length < this.config.minTextLength) {
return { chunks: [], skipped: true, skipReason: "Text below minTextLength" };
}
Expand Down
7 changes: 5 additions & 2 deletions src/utils/boundary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export function wrapWithBoundary(content: string, boundary: DataBoundary): strin
* Boundary tags like [UD-xyz]...[/UD-xyz] corrupt per-sentence model scores
* because the tokenizer treats the tag text as part of the sentence.
*
* Does NOT trim whitespace — callers who want that should call `.trim()`
* themselves. Auto-trimming changes semantics for inputs that contain
* leading/trailing whitespace but no boundary markers.
*
* @param content - Content that may contain boundary tags
* @returns Content with all boundary tags removed
*/
Expand All @@ -70,8 +74,7 @@ export function stripBoundaryPatterns(content: string): string {
.replace(/\[UD-[A-Za-z0-9_-]+\]/g, "")
.replace(/\[\/UD-[A-Za-z0-9_-]+\]/g, "")
.replace(/<user-data-[A-Za-z0-9_-]+>/g, "")
.replace(/<\/user-data-[A-Za-z0-9_-]+>/g, "")
.trim();
.replace(/<\/user-data-[A-Za-z0-9_-]+>/g, "");
}

/**
Expand Down
Loading