Repository navigation
Conversation
Spike tasks: - Analyze RAGEngine guardrail code for reuse potential - Minimal Python ext_proc prototype with SSE streaming - Benchmark Python vs Go (latency, memory, throughput) - Performance/reuse trade-off decision Success criteria: - Clear technical decision (Go/Python/Hybrid) - Reuse effort estimated - Baseline measurements established
Isolated experiment environment for Go vs Python ext_proc evaluation: - Go ext_proc baseline (existing PoC logic) - Mock vLLM backend with SSE streaming test cases - E2E test script (same Envoy config, different backends) - Test cases: normal response, secret split across chunks, boundary Metrics to track: latency (p50/p99), memory, throughput, correctness Next: add python/main.py and envoy/envoy.yaml
Python ext_proc implementation: - Async gRPC processor (matches Go behavior) - SSE parsing + logical content buffering - Holdback mechanism for pattern detection - Secret redaction logic - MockExtProc fallback for protobuf types (pending real generation) Envoy configuration: - Listener on :10000 → routes to backend (:8000) - ext_proc filter on response_body (FULL_DUPLEX_STREAMED) - Cluster config for ext_proc backend (:9000) - failure_mode_allow: false (fail-closed) Same config works for both Go and Python backends
Proto generation script: - Clones envoyproxy/envoy v1.29.2 (matching Istio 1.29.2) - Generates Python bindings for ext_proc service - Creates /gen with Python stubs Next: run generate_proto.sh and update python/main.py with real proto
Python 1.29.2 ext_proc PoC: - venv with grpcio + protobuf - requirements.txt for dependencies - proceed with mock proto for local E2E - official proto generation deferred to separate task Next: run local E2E with mock backend + Envoy + Python ext_proc
Direct unit tests of Python ext_proc (no Envoy needed): ✓ Test 1: Redact Normal secret detected and redacted ✓ Test 2: Boundary Secret split across chunks correctly handled ✓ Test 3: Holdback Holdback mechanism prevents unsafe prefix leakage Example: 'sk-12' never leaked, always waited for full pattern ✓ Test 4: Concurrency Multiple concurrent requests maintain independent state Result: Python ext_proc core logic is correct Ready for Envoy/gRPC integration in next phase
Status: - Core SSE parsing logic: VERIFIED - Holdback + scan mechanism: VERIFIED - Boundary case (secret split across chunks): VERIFIED - Concurrency (independent per-request state): VERIFIED - 4/4 test cases PASS Results: ✓ Redact - secret detection and redaction works ✓ Boundary - cross-chunk holdback prevents unsafe prefix leakage ✓ Holdback - logical buffer management is correct ✓ Concurrency - request state isolation is correct Pending for Phase 1+: - Real gRPC ExternalProcessor implementation - Envoy 1.29.2 integration - Official Envoy proto bindings (Go/Python) - Production E2E testing This PoC proves: 1. Python ext_proc architecture is sound 2. SSE streaming correctness is verified 3. Ready for Envoy gateway integration Next phase: implement real gRPC service + full E2E with actual Envoy
Python gRPC proto generation hit dependency hell. Switching to Go implementation for Phase 1 real E2E: - go-control-plane has official Envoy 1.29.2 bindings - Skip proto compilation entirely - Same SSE logic, just in Go gRPC service - Test: normal streaming + boundary redact via real Envoy If Go E2E passes → proven that ext_proc can work Then decide: Python vs Go for production
Phase 1.1 foundation: Minimal gRPC ext_proc server stub. - ext_proc/ module with __init__ and server.py - ExternalProcessorServicer placeholder (Process() raises NotImplementedError) - requirements.txt with core dependencies (grpcio, protobuf, pydantic, pyyaml, tiktoken) - Dockerfile for containerization (python:3.11-slim) - setup.py for package management - pytest.ini and test structure - .gitignore for Python/IDE artifacts Tests: - test_version: confirms version is 0.1.0 - test_imports: verifies core dependencies importable - test_servicer_placeholder: confirms ExternalProcessorServicer exists All tests pass. Ready for Phase 1.2 (SSE Parser) to start. Depends on: none Blocks: PR-1.2 (SSE Parser), PR-1.4 (Scanner Interface)
Phase 1.2: Core SSE parsing for OpenAI-compatible LLM streaming.
streaming/sse_parser.py:
- split_sse_events(buf: bytes) → (events, remaining)
Splits raw bytes on \n\n delimiter, returns complete events + remainder
- parse_sse_event(event: bytes) → Optional[dict]
Parses "data: {...}" JSON, extracts delta.content from OpenAI format
- is_done_event(event: bytes) → bool
Detects [DONE] marker for stream termination
Handles:
- Complete events split cleanly on delimiters
- Incomplete events held for next buffer chunk
- Malformed JSON (logs warning, returns None)
- OpenAI delta format with finish_reason, etc.
- Empty/missing content fields
- Non-data lines (comments, etc.)
Tests (16 tests, 100% pass):
- TestSplitSSEEvents: single, multiple, incomplete, empty
- TestParseSSEEvent: delta content, finish_reason, [DONE], edge cases
- TestIsDoneEvent: detection with/without prefix, malformed
- TestIntegration: realistic streaming sequence (chunks end mid-event)
Depends on: PR-1.1
Blocks: PR-1.3 (Logical Stream + Holdback)
Phase 1.3: Core stream buffering and cross-event pattern detection. streaming/logical_stream.py: LogicalStreamBuffer class - append(content: str): Accumulates delta.content from SSE events - get() → str: Returns current buffer contents - clear(): Clears the buffer - flush_and_clear() → str: Get and clear in one atomic operation - Handles concatenation of fragments across multiple SSE events streaming/holdback.py: Cross-event pattern detection mechanism - release_safe_prefix(pending: str, pattern_length: int) → (safe, held) Core holdback logic: holds back (pattern_length - 1) bytes to prevent unsafe prefix leakage. Ensures patterns split across event boundaries are caught by next scan. Example: for "sk-123456789" (12 chars), holds back 11 chars to guarantee detection. - detect_cross_boundary_pattern(previously_held, new_content, pattern) → bool Utility for debugging/testing cross-boundary scenarios. Tests (34 tests, 100% pass): - TestLogicalStreamBuffer (5): single/multiple append, clear, flush - TestReleaseSafePrefix (5): small buffer, exact size, large buffer, cross-event scenario - TestDetectCrossBoundaryPattern (5): pattern within held/new, spans boundary, nonexistent - TestIntegration (2): streaming with holdback, multiple chunks - TestHoldbackMechanism (8): various pattern sizes, accumulation, prefix leakage prevention - TestCrossBoundaryDetection (5): boundary detection edge cases - TestHoldbackEdgeCases (4): pattern_length edge cases Depends on: PR-1.1, PR-1.2 Blocks: PR-1.4 (Scanner Interface), PR-1.5 (GuardEngine)
Phase 1.4: Extensible scanner architecture for pattern detection. scanners/interface.py: - ScanResult dataclass: Encapsulates scan results (scanner_name, pattern, found, match_index, match_text) with validation ensuring consistency (e.g., if found=True, match_index >= 0 and match_text is non-empty) - Scanner abstract base class: Defines interface all pattern detectors must implement - name property: Unique scanner identifier - pattern property: Pattern string being detected - scan(content: str) → ScanResult: Core scanning method scanners/registry.py: ScannerRegistry for managing multiple scanners - register(scanner): Add scanner to registry (rejects duplicates) - unregister(name): Remove scanner by name - get(name) → Optional[Scanner]: Lookup scanner by name - list_scanners() → List[str]: Get all registered scanner names - scan(content, scanner_name=None) → List[ScanResult]: - If scanner_name specified: scan with only that scanner - If None: scan with all registered scanners - Returns list of ScanResult objects Tests (23 tests, 100% pass): - ScanResult validation (6 tests): found/not-found consistency checks - Registry operations (15 tests): register, unregister, get, list, scan (single/all) - Integration (2 tests): realistic security scanning, dynamic registration Key design decisions: - ScanResult validation at construction time prevents invalid state propagation - Registry.scan() supports both single and bulk scanning modes - Scanners are completely decoupled from each other - Scanner order preserved (dict maintains insertion order in Python 3.7+) Depends on: PR-1.1, PR-1.2, PR-1.3 Blocks: PR-1.5 (GuardEngine), PR-1.6 (BanSubstrings Scanner)
xiaoqi-7
requested review from
Fei-Guo,
rambohe-ch,
techworldhello and
zhuangqh
as code owners
October 2, 2026 05:09
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 1.4: Extensible scanner architecture for pattern detection. Defines the base Scanner interface and a registry system for managing multiple scanner implementations.
Components
ScanResult Dataclass
Scanner Abstract Base Class
All pattern detectors inherit from this base:
nameproperty: Unique identifier for this scannerpatternproperty: Pattern string being searched forscan(content: str) → ScanResult: Execute the scanScannerRegistry
Central registry for managing multiple scanner instances:
register(scanner): Add a scanner (rejects duplicate names)unregister(name): Remove scanner by nameget(name): Lookup a specific scannerlist_scanners(): Get all scanner names in registration orderscan(content, scanner_name=None):Design Principles
Test Coverage
23 tests, 100% pass rate
Use Case Example
Files Changed
scanners/interface.py- ScanResult & Scanner interface (~60 LOC)scanners/registry.py- ScannerRegistry implementation (~50 LOC)tests/test_scanner_interface.py- Comprehensive test suite (~110 tests)Dependencies
Next Steps
PR-1.5 will implement the GuardEngine coordinator that uses this registry to orchestrate scanning.