Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/noise-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,34 @@ const DIAGNOSTIC_ARTIFACT_PATTERNS = [
/\bno explicit solution\b/i,
];

// Structural noise: runtime traces, raw blobs, wrappers, and malformed fragments
const STRUCTURAL_NOISE_PATTERNS = [
/^System\s*:/i,
/compacti(ng|on)\s*(context|safeguard)/i,
/model\s*(switch|switched|changed|swap)/i,
/session\s*(reset|restart|start|end)/i,
/\(untrusted metadata\)\s*:/i,
/^\{[\s\S]*\}$/,
/<[a-z-]+>[\s\S]*<\/[a-z-]+>/i,
/(?:^>.*\n){3,}/m,
];

export interface NoiseFilterOptions {
/** Filter agent denial responses (default: true) */
filterDenials?: boolean;
/** Filter meta-questions about memory (default: true) */
filterMetaQuestions?: boolean;
/** Filter session boilerplate (default: true) */
filterBoilerplate?: boolean;
/** Filter structural noise (default: true) */
filterStructuralNoise?: boolean;
}

const DEFAULT_OPTIONS: Required<NoiseFilterOptions> = {
filterDenials: true,
filterMetaQuestions: true,
filterBoilerplate: true,
filterStructuralNoise: true,
};

/**
Expand All @@ -78,6 +93,7 @@ export function isNoise(text: string, options: NoiseFilterOptions = {}): boolean
if (opts.filterDenials && DENIAL_PATTERNS.some(p => p.test(trimmed))) return true;
if (opts.filterMetaQuestions && META_QUESTION_PATTERNS.some(p => p.test(trimmed))) return true;
if (opts.filterBoilerplate && BOILERPLATE_PATTERNS.some(p => p.test(trimmed))) return true;
if (opts.filterStructuralNoise && STRUCTURAL_NOISE_PATTERNS.some(p => p.test(trimmed))) return true;
if (DIAGNOSTIC_ARTIFACT_PATTERNS.some(p => p.test(trimmed))) return true;

return false;
Expand Down
32 changes: 32 additions & 0 deletions test/noise-filter-structural.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import jitiFactory from "jiti";

const jiti = jitiFactory(import.meta.url, { interopDefault: true });

const { isNoise } = jiti("../src/noise-filter.ts");

describe("noise-filter structural patterns", () => {
const samples = [
"System: compaction safeguard engaged",
"Compaction context safeguard tripped",
"model switch detected",
"model changed to gpt-5",
"session reset due to inactivity",
"(untrusted metadata): Sender (untrusted metadata): foo",
"{\"type\":\"meta\",\"payload\":{\"note\":\"wrapper\"}}",
"<relevant-memories>foo</relevant-memories>",
"> quote one\n> quote two\n> quote three\n> quote four\n",
];

for (const input of samples) {
it(`filters structural noise: ${input.slice(0, 40)}`, () => {
assert.equal(isNoise(input), true);
});
}

it("allows structural noise when disabled", () => {
const input = "System: model switch detected";
assert.equal(isNoise(input, { filterStructuralNoise: false }), false);
});
});
Loading