Commit Graph
2 Commits
Author SHA1 Message Date
Satya Devineni e87a351219 feat(router): add min_total_chars / max_total_chars whole-conversation length conditions (#2958) (#3181)
* Adding a new server side leaf: min and max total chars

* Undo GUI bug fixes: created a separate PR #3180

* CI bug fixes
2026-08-24 21:33:12 +00:00
Ramakrishnan SivakumarandClaude Opus 4.8 71d7f742a0 [Router] Deterministic conditions: keywords / regex / min_chars / has_* / metadata (M4, #2380) (#2491)
* feat(router): deterministic leaf conditions (M4, #2380)

Implement the deterministic match conditions for the routing engine:
keywords_any/keywords_all, regex, min_chars/max_chars, has_tools/has_images,
and metadata. Pure CPU, no model, no tokenizer. Each is a Condition built by a
named leaf factory and plugged into make_leaf_factory as deterministic_factories.

Frozen v1 semantics match route_policy.schema.json exactly:
- keywords: case-insensitive substring, ASCII-only case fold (locale-independent)
- regex: ECMAScript (std::regex), compiled at policy-load (fail-fast on bad pattern)
- min_chars/max_chars: inclusive bounds on input length in UTF-8 bytes
- has_tools/has_images: boolean request feature equals authored value
- metadata: case-sensitive; value decoded into a comma-split, trimmed token set;
  equals (raw exact) / any (set intersection) / exists (presence); missing or
  empty value matches only exists:false

Public surface is a single symbol, make_deterministic_leaf_factories(); the
condition classes stay file-local. Config is validated when each condition is
built (factory throws std::invalid_argument on malformed leaves); evaluate()
never throws, preserving the engine's fail-open contract.

Frozen-edge decisions (aligned with the non-empty/no-silent-match precedent set
on the sibling semantic classifier): reject empty keyword arrays and empty
keyword items rather than defining a vacuous fold (minItems/minLength tightened
in the still-unreleased schema, lock refreshed); ASCII-only case fold pinned
explicitly in the frozen-semantics README rather than left to interpretation.

Adds test_routing_policy_deterministic.cpp (50 checks: keywords all-vs-any,
case-insensitivity, ECMAScript regex, UTF-8 byte-boundary chars, metadata
equals/any/exists incl. scalar vs comma-list and missing-key, malformed-config
rejection, trace emission, and the registry's multi-key implicit-all wiring),
registers RoutingPolicyDeterministicTest in CMake, and wires it into CI.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* fix(router): tighten regex non-empty and metadata whitespace presence (#2380)

Two frozen-edge refinements surfaced by scenario testing, both applying the same
"no silent always-match / empty == absent" principle already used for keywords:

1. regex must be non-empty (minLength:1). An empty pattern matches every input —
   an accidental always-match rule. Express always-match by omitting the
   condition, not an empty regex.

2. A whitespace-only metadata value now counts as absent (matches only
   exists:false), consistent with the comma-split token model where equals/any
   already see no content. Previously a "   " value read as exists:true while
   yielding no tokens for any/equals.

Schema descriptions + frozen-semantics README updated, schema-lock refreshed,
and the unit test gains whitespace-only metadata coverage.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* feat(router): reject catastrophic-backtracking regex at policy load (#2380)

std::regex is a backtracking engine with no portable step limit, and the input
matched by a regex condition is untrusted end-user text — so a policy author who
writes a pattern with a nested unbounded quantifier (the (a+)+ / (a*)* / (.*)+ /
(\d+){2,} ReDoS family) creates a denial-of-service vector: a crafted request can
pin a worker thread.

reject_catastrophic_regex() walks the pattern at policy-load time and throws
std::invalid_argument when it finds an unbounded quantifier applied to a group
that itself contains an unbounded quantifier, so the dangerous pattern never
reaches the request hot path. This is a mitigation, not a linear-time proof —
alternation-overlap ReDoS like (a|a)+ is not detected; a hard guarantee would
need a non-backtracking engine (RE2), which would be a separate dialect op. The
runtime try/catch on std::regex_error stays as cross-platform defense-in-depth
(MSVC throws error_complexity; libstdc++/libc++ may not).

Also rejects an empty regex pattern in the factory (defense-in-depth behind the
schema minLength). Bounded quantifiers (outer {1,3} or inner {1,3}) and
inner-only groups remain accepted. Schema/README document the rejection; unit
test adds 12 ReDoS accept/reject cases.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* fix(router): address review feedback on deterministic conditions (#2380)

Review fixes from #2491 (fl0rianr, SlawomirNowaczyk):

- CI: build the test_routing_policy_deterministic target in both workflow
  sections; it was added to the ctest filter but never built, so CI failed with
  "Unable to find executable".
- ReDoS detector: a wrapper group hid its child's unbounded quantifier, so
  ((a+))+ and (a(b+))+ slipped through. Propagate an unbounded inner group to the
  enclosing group on close so a later quantifier on the wrapper is caught.
- regex: cap the input fed to std::regex at 1 MiB; an oversized input is a
  non-match (fail-safe) rather than a potentially hung worker, covering the case
  the static check cannot bound.
- keywords: memoize the ASCII-lowered input on EvalContext so multiple keyword
  leaves fold the (constant) request input at most once.
- Schema/factory parity for metadata: schema now requires non-empty key and a
  non-empty any[] of non-empty items, matching the factory's validation; lock
  refreshed.
- Soften the regex schema wording so it reads as a mitigation, not a guarantee
  that all catastrophic patterns are prevented.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
2026-06-30 21:13:17 +00:00