feat: PII detection pipeline and post-generation audit#2
Merged
Conversation
Adds the two-stage detection pipeline at the heart of Guardian RAG. - Stage 1: Presidio regex recognisers (email, phone, IBAN, credit card, IP, SSN, URL). Bypasses spaCy / the NlpEngine so detection stays in the ~5ms latency budget. - Stage 2: GLiNER NER for unstructured PII (names, organisations, locations, etc). Backend is injected via Protocol so tests substitute deterministic fakes; real `gliner` import lives in `_real_backends.py`. - Stage 3: Conditional DeBERTa zero-shot validation that fires only on GLiNER spans below `GUARDIAN_DEBERTA_THRESHOLD` (default 0.85). Safety net without the inference cost of running DeBERTa on every span. Outputs: - `PiiDetector` orchestrates the three stages and returns entities in source order. - `Redactor` replaces spans with typed placeholders (`[PERSON_1]`, `[EMAIL_1]`, ...) preserving grammar so the chunk remains useful for RAG. Same span resolves to the same placeholder within a document. - `PostGenerationAuditor` re-runs the detector + redactor over an LLM answer before it reaches the user. Fail-closed by default: any exception inside the audit returns a safe placeholder. Tests are hermetic. No weights are downloaded; backends are fake. ADRs: 001 Presidio + GLiNER orchestration 002 GLiNER over openai/privacy-filter 003 Conditional DeBERTa validation 005 Typed placeholders 006 Post-generation audit
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
Protocol-injected backends, conditional DeBERTa validation, typed-placeholder redactorPostGenerationAuditor) that re-runs the same detector + redactor over generated LLM answerspii/_real_backends.py, pyright-excluded). Tests substitute deterministic fakesTest plan
uv run ruff check .cleanuv run pyright0 errors / 0 warningsuv run pytest tests/test_pii_*.pyall greenADRs referenced