Skip to content

fix(skills_guard): pre-compile THREAT_PATTERNS at module load time - #32712

Closed
ErnestHysa wants to merge 3 commits into
NousResearch:mainfrom
ErnestHysa:fix/n14-skills-guard-regex
Closed

fix(skills_guard): pre-compile THREAT_PATTERNS at module load time#32712
ErnestHysa wants to merge 3 commits into
NousResearch:mainfrom
ErnestHysa:fix/n14-skills-guard-regex

Conversation

@ErnestHysa

Copy link
Copy Markdown
Contributor

Summary

Fixes N14 — recompiled regex patterns inside nested loops.

Pain Before

called inside nested loops — 100+ threat patterns × N lines × M files = same patterns recompiled thousands of times per scan.

What Was Fixed

Added module-level list that pre-compiles all with once at import time. now iterates over pre-compiled pattern objects and calls directly.

Performance: O(1) pattern lookup per line instead of O(n) regex recompilation.

…her recovery

N43 — Silent plugin/bundle errors:
- Plugin command dispatch: logger.debug() -> logger.warning()
- Bundle dispatch: logger.debug() -> logger.warning()
Plugin/auth failures are no longer invisible to operators.

N42 — O(n^2) pending_watchers recovery:
- Both recovery loops (startup + per-message) used while+pop(0) which is O(n) per pop
- Replaced with enumerate() over the list + periodic asyncio.sleep(0) yield points
- Clears the list after iteration instead of per-pop
- Batch size of 100 balances throughput vs event-loop responsiveness
THREAT_PATTERNS were used with re.search(pattern, line, re.IGNORECASE)
inside two nested loops (for each pattern, for each line) on every
scan_file() call. Pre-compile all patterns once at module load into
_COMPILED_PATTERNS so scan_file() reuses the compiled objects instead
of recompiling the same 100+ patterns on every file scan.
N15 agent_runtime_helpers.py (strip_think_blocks):
  - Move inline re.sub() calls (11 patterns) to two module-level
    compiled regexes: _STRIP_THINK_BLOCKS_THINK_PATTERNS and
    _STRIP_THINK_BLOCKS_TOOL_CALL_PATTERNS.
  - Eliminates per-call re.compile() overhead on every invocation.

N16 model_metadata.py (parse_context_limit_from_error,
  parse_available_output_tokens_from_error):
  - Compile the pattern lists once using func._patterns attrs
    (lazy init on first call) instead of re-compiling on every call.

N17 cronjob_tools.py (_CRON_THREAT_PATTERNS,
  _CRON_SKILL_ASSEMBLED_PATTERNS, _CRON_EXFIL_COMMAND_PATTERNS):
  - Pre-compile all pattern+name tuples at module load time.
  - Replace re.search(pattern, ...) calls with pattern.search(...).

N18 hermes_cli/voice.py (speak_text):
  - Add 'import re'.
  - Pre-compile 10 markdown-stripping regexes at module level.
  - Replace 10 sequential re.sub() calls with compiled .sub() calls.

N19 hermes_cli/runtime_provider.py:
  - Add _STRIP_V1_SUFFIX module-level compiled regex.
  - Add _strip_v1_suffix() helper; replace 4 inline re.sub() calls.
@alt-glitch alt-glitch added type/perf Performance improvement or optimization P3 Low — cosmetic, nice to have comp/tools Tool registry, model_tools, toolsets tool/skills Skills system (list, view, manage) duplicate This issue or pull request already exists labels May 26, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #32713 — same author, same files changed, identical diff (154 additions, 132 deletions across 7 files). #32713 covers N14 (skills_guard) as part of the broader N15-N19 batch.

@ErnestHysa

Copy link
Copy Markdown
Contributor Author

Closing as duplicate — #32713 covers the same THREAT_PATTERNS pre-compilation fix as part of the broader N15-N19 batch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/tools Tool registry, model_tools, toolsets duplicate This issue or pull request already exists P3 Low — cosmetic, nice to have tool/skills Skills system (list, view, manage) type/perf Performance improvement or optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants