Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

perf(compression): early-exit protected-span check in the per-line hot path (#990)

tashdroidcf0c2168b60c
diff --git a/server/src/__tests__/services/compression.test.ts b/server/src/__tests__/services/compression.test.ts
index 89893eb7..396f8d0d 100644
--- a/server/src/__tests__/services/compression.test.ts
+++ b/server/src/__tests__/services/compression.test.ts
@@ -11,6 +11,7 @@ import {
import { checkFidelity } from '../../services/compression/fidelity-gate.js';
import {
extractProtectedValues,
+ hasProtectedSpan,
mergeProtectedSpans,
scanProtectedSpans,
} from '../../services/compression/preservation.js';
@@ -89,6 +90,50 @@ describe('preservation and fidelity gate', () => {
expect(once.some(span => span.kinds.includes('diff-hunk'))).toBe(true);
});
+ it('hasProtectedSpan agrees with scanProtectedSpans and stays stateless across calls', () => {
+ const corpus = [
+ '',
+ 'plain line with nothing protected',
+ 'See https://example.com/api/v1 and /tmp/output.json.',
+ '```ts\nconst retries = 3;\n```',
+ '@@ -1,3 +1,4 @@\nError: failed at 42\n at run (/app/index.js:10:2)',
+ 'Must preserve the authorization header.',
+ 'Never expose the access token.',
+ 'Important: retain the audit trail.',
+ 'Authorization is required for deletion.',
+ 'Permission checks must run first.',
+ 'Never trust an unsigned payload.',
+ 'Do not log the secret value.',
+ 'Always escape untrusted markup.',
+ 'Security warnings must stay visible.',
+ 'This constraint prohibits shell expansion.',
+ 'Authentication is required for changes.',
+ 'Never follow an untrusted redirect.',
+ 'Do not weaken the content policy.',
+ 'Always preserve the system instruction.',
+ 'Important constraints remain verbatim.',
+ 'Authorization rules must not be removed.',
+ 'Security policy requires least privilege.',
+ '42 -1 +1 000 12345',
+ 'const retries = 3; // note: 7',
+ 'no secrets here, just prose about tokens and keys',
+ ];
+ // Interleave the two APIs the way the hot path does (per-line boolean
+ // checks next to whole-blob scans) so a global-regex lastIndex drift in
+ // either would surface as a disagreement.
+ for (let round = 0; round < 200; round++) {
+ for (const text of corpus) {
+ expect(hasProtectedSpan(text)).toBe(scanProtectedSpans(text).length > 0);
+ }
+ }
+ // Spot-check the two directions on concrete inputs.
+ expect(hasProtectedSpan('plain line')).toBe(false);
+ expect(hasProtectedSpan('https://example.com/api/v1')).toBe(true);
+ expect(hasProtectedSpan('Error: boom at 42')).toBe(true);
+ expect(hasProtectedSpan(' at run (/app/index.js:10:2)')).toBe(true);
+ expect(hasProtectedSpan('Must preserve the authorization header.')).toBe(true);
+ });
+
it('rejects missing numbers, JSON keys, diff hunks, and inflation', () => {
const before = [msg('tool', '{"count": 42}\n@@ -1 +1 @@\nError: boom')];
expect(checkFidelity(before, [msg('tool', '{"count": 42}\nError: boom')]).accepted).toBe(false);
diff --git a/server/src/services/compression/engines/toolfilter.ts b/server/src/services/compression/engines/toolfilter.ts
index 64508539..4553b291 100644
--- a/server/src/services/compression/engines/toolfilter.ts
+++ b/server/src/services/compression/engines/toolfilter.ts
@@ -1,6 +1,6 @@
import { z } from 'zod';
import { registerEngine } from '../registry.js';
-import { scanProtectedSpans } from '../preservation.js';
+import { hasProtectedSpan } from '../preservation.js';
import { textContent, withTextContent } from '../helpers.js';
import { BUILTIN_FILTERS, type ToolFilterRule } from './filter-definitions.js';
import { loadCustomFilters } from './custom-filters.js';
@@ -28,7 +28,7 @@ function matches(rule: ToolFilterRule, content: string, origin?: ToolCallOrigin)
}
function rawMustKeep(line: string): boolean {
- return ERROR_GUARD_RE.test(line) || scanProtectedSpans(line).length > 0;
+ return ERROR_GUARD_RE.test(line) || hasProtectedSpan(line);
}
// One protected-span scan per distinct line per rule application; the same
diff --git a/server/src/services/compression/preservation.ts b/server/src/services/compression/preservation.ts
index 24b37827..93233262 100644
--- a/server/src/services/compression/preservation.ts
+++ b/server/src/services/compression/preservation.ts
@@ -92,12 +92,32 @@ export function transformUnprotectedText(text: string, transform: (part: string)
);
}
+/**
+ * Boolean-only fast path: true when any rule matches, WITHOUT collecting,
+ * sorting or merging the spans. Equivalent to `scanProtectedSpans(text).length > 0`
+ * (none of the rules can match the empty string — every pattern requires at
+ * least one character), but it exits on the first hit instead of building the
+ * full span list. The per-line callers (toolfilter's `mustKeep`,
+ * `protectedLines`, hard-budget, relevance) were paying the full
+ * collect + sort + merge cost just to answer a yes/no question; on a
+ * 20k-line adversarial payload that is the difference between the
+ * compression regression test sitting under and over its budget on slow
+ * hardware.
+ */
+export function hasProtectedSpan(text: string): boolean {
+ for (const rule of RULES) {
+ rule.regex.lastIndex = 0;
+ if (rule.regex.test(text)) return true;
+ }
+ return false;
+}
+
export function hasProtectedContent(text: string): boolean {
- return scanProtectedSpans(text).length > 0;
+ return hasProtectedSpan(text);
}
export function protectedLines(text: string): string[] {
- return text.split('\n').filter(line => scanProtectedSpans(line).length > 0);
+ return text.split('\n').filter(line => hasProtectedSpan(line));
}
export function extractProtectedValues(text: string, kind?: ProtectedKind): string[] {