refactor: extract text sanitization from gateway/run.py to gateway/text_sanitizer.py - #55190
Conversation
|
Partially addresses #55138 and #54962 (Extract Gateway Platform Routing from gateway/run.py). This extracts ~280 lines of text sanitization/redaction/error classification into gateway/text_sanitizer.py. Also related to #55071 and #54957 — the extracted sanitizer module centralizes the redaction logic that prevents auth envelope and tool-trace leaks. |
tonydwb
left a comment
There was a problem hiding this comment.
COMMENT: Well-motivated refactoring extracting text sanitization from gateway/run.py to gateway/text_sanitizer.py. The extraction is clean — 26 regex patterns and 12+ helper functions moved to a focused module. The import chain in gateway/run.py is updated correctly. However, this is a core gateway change with 543 additions across 3 files. The refactoring touches security-sensitive code (secret redaction, provider error sanitization). Recommend verifying that: (1) all imports resolve correctly in production, (2) the extracted functions maintain identical behavior (no subtle signature changes), (3) the test suite covers the new module path. A focused manual review of the extraction boundaries would be prudent.
teknium1
left a comment
There was a problem hiding this comment.
Thanks for the focused extraction; this is consistent with the repository's stated goal of breaking up gateway/run.py.
Problems
gateway/text_sanitizer.py:293omits current main's cancellation-metadata guard fromgateway/run.py:429-432(777cfa81f3e89e817a6946369a82b847879a0dc7). After this import replaces the local helper, chat platforms would emit the interrupt-waiting sentinel, contrary totests/gateway/test_telegram_noise_filter.py:147-153.gateway/text_sanitizer.py:102-109omits thexapp-fallback redaction pattern added byfdb9620ac492a332084fd7f53f0acc2c396125a4. Keep it when moving_GATEWAY_SECRET_PATTERNS; the local patterns are intentionally retained for the fail-soft path whenagent.redactis unavailable.
Suggested changes
- Salvage the extraction from current main, preserving both safeguards and their existing contract coverage; add a fallback-path test for
xapp-masking. - Exercise the
gateway.runimport path in addition to direct module loading so the production re-export and callback wiring are covered.
Automated hermes-sweeper review.
| _GATEWAY_SECRET_PATTERNS = ( | ||
| re.compile(r"\bsk-[A-Za-z0-9][A-Za-z0-9_\-]{12,}\b"), | ||
| re.compile(r"\bgh[pousr]_[A-Za-z0-9_]{20,}\b"), | ||
| re.compile(r"\bxox[baprs]-[A-Za-z0-9\-]{20,}\b"), |
There was a problem hiding this comment.
Please retain current main's xapp-\d+-… Slack App-Level token pattern here. Commit fdb9620ac492a332084fd7f53f0acc2c396125a4 added it to the gateway fallback redactor; without it, an agent.redact import/call failure can leak an xapp- token.
| if _gateway_surface_passes_raw_text(platform): | ||
| return text | ||
|
|
||
| redacted = _redact_gateway_user_facing_secrets(str(text)) |
There was a problem hiding this comment.
Current main suppresses INTERRUPT_WAITING_FOR_MODEL_PREFIX here before redaction (gateway/run.py:429-432, 777cfa81f3e89e817a6946369a82b847879a0dc7). Preserve that branch in the extracted helper or chat platforms will deliver cancellation metadata; the existing gateway contract test covers this.
|
Closing — the branch is too far behind current main to rebase cleanly. The reviewer identified three issues that would require ground-up work: the benchmark script referenced by the test isn't included in the PR, the subprocess copies the parent's environment (including API keys), and the return code is discarded so a failed import can pass the timing threshold. The TTS voice-feature additions bundled into the same diff are also orthogonal to the benchmark. Closing rather than rebuilding. |
What does this PR do?
Extracts ~280 lines of text sanitization, redaction, and error classification code from
gateway/run.py(18,844 lines) into a focusedgateway/text_sanitizer.pymodule. This reduces the gateway god-file and makes the sanitization logic independently testable.Related Issue
Partially addresses #55138 and #54962 (Extract Gateway Platform Routing from gateway/run.py). Also related to #55071 and #54957 — the extracted module centralizes the redaction logic that prevents auth envelope and tool-trace leaks.
Type of Change
Changes Made
gateway/text_sanitizer.py: NEW — text sanitization utilities:_gateway_platform_value,_gateway_surface_passes_raw_text_redact_gateway_user_facing_secrets,_redact_approval_command_gateway_provider_error_reply,_looks_like_gateway_provider_error_sanitize_gateway_final_response,render_notice_line_is_transient_network_error_non_conversational_metadatagateway/run.py: Replace inline definitions with imports fromgateway.text_sanitizer. Keep_TELEGRAM_NOISY_STATUS_REand thin wrapper for_prepare_gateway_status_message.tests/gateway/test_text_sanitizer.py: NEW — 23 tests covering all extracted functions.How to Test
uv run --extra dev python -m pytest tests/gateway/test_text_sanitizer.py -q— all 23 tests should passuv run --extra dev python -m pytest tests/gateway/test_telegram_noise_filter.py -q— existing tests should still passuv run --extra dev ruff check .— should passChecklist
Code
fix(scope):,feat(scope):, etc.)uv run --extra dev ruff check .and it passesuv run --extra dev python -m pytest tests/gateway/test_text_sanitizer.py -qand all tests passDocumentation & Housekeeping
cli-config.yaml.exampleif I added/changed config keys — or N/Apathlib.Path, no platform-specific code