O(n²) regex in FORCE_REASONING frontier-planning pattern stalls requests with long single-line payloads (61s/call @ 200KB, both /v1/messages and /v1/chat/completions)
Summary
The frontier-planning entry in FORCE_REASONING_PATTERNS (src/routing/complexity-analyzer.js) is a single regex with two leading .* lookaheads:
/(?=.*\b(plans?|roadmaps?|strateg(y|ies)|blueprints?|proposals?|rfcs?|design\s+docs?)\b)(?=.*\b(refactor\w*|implement\w*|migrat\w+|architect\w+|redesign\w*|restructur\w+|rewrit\w+|overhaul\w*|codebases?|infrastructure|pipelines?|deploy\w*|systems?)\b)/i
. never crosses a newline, so on a long single line every start position rescans .* to end-of-line — O(n²) in content length. A 200KB single-line user message spends ~61 s per .test() call, and determineProviderSmart runs the force-pattern scan twice per request, so the request stalls >120 s before any provider call is attempted.
This affects both ingress endpoints (/v1/messages and /v1/chat/completions) — they share the same routing pipeline.
Repro
Any request whose last user message contains one very long line. Node microbenchmark of the regex alone (.test() on the content):
| content | time |
|---|---|
| 50 KB single line | 3,814 ms |
| 100 KB single line | 15,292 ms — 4× time for 2× size (quadratic signature) |
| 200 KB single line | ~61,000 ms |
| 200 KB multi-line | 18 ms |
End-to-end against a live deployment (payload = ~200 KB unique filler in one line + a short final question):
POST /v1/messages, 50k tokens, single-line body → 188.4 s
POST /v1/messages, 200k tokens (~800 KB) → timeout at 240 s (HTTP 000)
The upstream provider (Together, GLM-5.3-Flash) answered the identical 200 KB payload in ~4 s once the request actually reached it — the latency is entirely proxy-side.
Real-world triggers: minified JSON tool results, base64 blobs, single-paragraph pastes — anything an agent harness sends as one long line.
Root cause
Two failure modes in one regex:
- Quadratic backtracking:
(?=.*A)(?=.*B)forces a full rescan of.*from every candidate start position. V8's--cpu-profcannot even attribute this time (it shows as(idle)/ native frames — 952 s of profile time with ~0.5 s of JS), which makes it deceptively hard to diagnose; per-thread/proc/<pid>/task/*/statsampling showsMainThreadpegged at 100%. - Latent semantic bug: because
.doesn't cross newlines, the documented intent — "a plan word AND a technical scope word must both be present" (see the comment above the pattern) — is only honored when both words sit on the same line. Multi-line agent asks (plan word in the first line, scope word in the body — the normal case) silently miss the escalation.
Proposed fix
Replace the single quadratic lookahead with two independent linear whole-content tests, preserving the documented co-occurrence-anywhere intent:
const PLANNING_ASK_RE = /\b(plans?|roadmaps?|strateg(y|ies)|blueprints?|proposals?|rfcs?|design\s+docs?)\b/i;
const TECHNICAL_SCOPE_RE = /\b(refactor\w*|implement\w*|migrat\w+|architect\w+|redesign\w*|restructur\w+|rewrit\w+|overhaul\w*|codebases?|infrastructure|pipelines?|deploy\w*|systems?)\b/i;
function isFrontierPlanningAsk(content) {
return PLANNING_ASK_RE.test(content) && TECHNICAL_SCOPE_RE.test(content);
}
Wired as a fallback in shouldForceReasoning() (after the array scan, respecting the FORCE_TIER_PATTERNS=false kill-switch) and as a short-circuit in scoreTaskType() (pattern: 'frontier_planning_two_sided').
Verification (deployment running the fix)
| measurement | before | after |
|---|---|---|
shouldForceReasoning on 200 KB single-line |
61,139 ms | 2 ms |
/v1/messages 50k-token probe, end-to-end |
188.4 s | 6.05 s |
/v1/messages 200k-token probe (~800 KB) |
timeout @240 s | 4.25 s (correct answer returned) |
Regression tests added in test/force-patterns.test.js:
- 2 cross-line firing cases (lock the co-occurrence-anywhere contract),
- a 200 KB single-line perf guard (
< 1 s; a quadratic regression measures ~61 s and fails loudly), - a
scoreTaskTypecontract test for the two-sided short-circuit.