mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 04:12:10 +03:00
v3.8.40 cycle integration → main. All test gates green (Unit/Integration/Coverage/Node-compat/Quality-Ratchet). The only red check, 'PR Test Policy', is the test-masking heuristic firing on the cumulative ~57-commit release diff (legitimate assert consolidations already reviewed per-PR — Gemini CLI removal #5246, retired GPT models #5280, provider catalog refreshes); overridden with --admin per the documented release-PR convention. CodeQL/SonarQube advisory scans non-blocking; #5278's code already passed CodeQL on main. Homologated on VPS 192.168.0.15 (v3.8.40 healthy).
59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
import {
|
|
QUANTUM_PATTERNS,
|
|
type QuantumLockConfig,
|
|
type VolatileSpan,
|
|
} from "./quantumPatterns.ts";
|
|
|
|
interface PrioritizedSpan extends VolatileSpan {
|
|
prio: number;
|
|
}
|
|
|
|
/**
|
|
* Detect non-semantic volatile spans in `text`, in the FIXED pattern order, then merge
|
|
* overlapping spans so a token is never double-replaced. Pure + fail-open: a throwing
|
|
* pattern aborts the scan and returns [] (QuantumLock must never corrupt a request).
|
|
*
|
|
* Merge rule (widest wins): sort by start asc, then by width desc, then by priority asc
|
|
* (earlier pattern = higher precedence); greedily keep a span only if it starts at/after
|
|
* the last accepted span's end.
|
|
*/
|
|
export function detectVolatileSpans(text: string, cfg: QuantumLockConfig): VolatileSpan[] {
|
|
if (!text) return [];
|
|
const allow = cfg.categories && cfg.categories.length > 0 ? new Set(cfg.categories) : null;
|
|
const raw: PrioritizedSpan[] = [];
|
|
|
|
try {
|
|
QUANTUM_PATTERNS.forEach(({ category, pattern }, prio) => {
|
|
if (allow && !allow.has(category)) return;
|
|
pattern.lastIndex = 0;
|
|
let m: RegExpExecArray | null;
|
|
while ((m = pattern.exec(text)) !== null) {
|
|
if (m[0].length === 0) {
|
|
pattern.lastIndex++;
|
|
continue;
|
|
}
|
|
raw.push({ start: m.index, end: m.index + m[0].length, category, prio });
|
|
}
|
|
});
|
|
} catch {
|
|
return [];
|
|
}
|
|
|
|
raw.sort((a, b) => a.start - b.start || b.end - a.end || a.prio - b.prio);
|
|
|
|
// Greedy non-overlapping sweep: accept a span only if it starts at/after the last accepted
|
|
// span's end. A nested or PARTIALLY-overlapping span is dropped whole (never split) — this is
|
|
// always SAFE (it can only under-stabilize, never corrupt text or shift placeholder numbering).
|
|
// With the current `\b`-anchored patterns true partial overlaps are unreachable; the drop rule
|
|
// is the conservative default if a future pattern can produce one.
|
|
const merged: VolatileSpan[] = [];
|
|
let lastEnd = -1;
|
|
for (const s of raw) {
|
|
if (s.start >= lastEnd) {
|
|
merged.push({ start: s.start, end: s.end, category: s.category });
|
|
lastEnd = s.end;
|
|
}
|
|
}
|
|
return merged;
|
|
}
|