Files
turnstone/tests/test_validate_regex_pattern.py
Patrick Buckley 8ba669cf57 fix(deadline): prefer a ready result over a same-window deadline/cancel
run_with_deadline checked the deadline/cancel before reading the result
queue, so a call that completed in the same scheduling window could be
reported as a spurious timeout. Drain the queue first.

Also from review:
- test_validate_regex_pattern stubs run_with_deadline, so the probe regex
  never runs — use a benign pattern instead of a real backtracking literal
  (the literal tripped a ReDoS scanner).
- output_guard_judge docstring: reference IntentJudge._parse_verdict instead
  of brittle judge.py line numbers.

The _runner BaseException catch is intentional and kept: it relays (not
swallows) whatever fn() raises to the caller via the queue; narrowing to
Exception would let a BaseException escape the worker so the caller never
gets a value, degrading the no-hang guarantee.
2026-06-16 17:10:12 -07:00

41 lines
1.5 KiB
Python

"""Tests for turnstone.console.server._validate_regex_pattern.
The catastrophic-backtracking branch is verified by simulating the deadline
firing rather than running a real ReDoS regex — a genuine runaway pattern would
leave a CPU-pinned daemon worker for the rest of the suite. The daemon-abandon
mechanism itself is covered in tests/test_deadline.py.
"""
from __future__ import annotations
from turnstone.console.server import _validate_regex_pattern
from turnstone.core.deadline import DeadlineExceededError
def test_valid_pattern_returns_none() -> None:
assert _validate_regex_pattern(r"\d{3}-\d{4}") is None
def test_invalid_pattern_returns_error() -> None:
msg = _validate_regex_pattern(r"(unclosed")
assert msg is not None
assert msg.startswith("Invalid regex")
def test_catastrophic_backtracking_returns_message(monkeypatch) -> None:
def _deadline(*_args, **_kwargs):
raise DeadlineExceededError
monkeypatch.setattr("turnstone.console.server.run_with_deadline", _deadline)
# The pattern is arbitrary — run_with_deadline is stubbed to raise, so the
# probe never runs; a real backtracking literal here would only trip CodeQL.
assert _validate_regex_pattern(r"\w+") == "Regex appears to have catastrophic backtracking"
def test_probe_error_returns_generic_message(monkeypatch) -> None:
def _err(*_args, **_kwargs):
raise RuntimeError("boom")
monkeypatch.setattr("turnstone.console.server.run_with_deadline", _err)
assert _validate_regex_pattern(r"abc") == "Regex caused an error during test"