Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /GRID-INTELLIGENCE_GRID /safety /observability /canary.py
| """ | |
| Project GUARDIAN: Safety Canary Utility. | |
| Injects and detects hidden markers in AI responses to identify adversarial recycling. | |
| """ | |
| import re | |
| import secrets | |
| # These are invisible or rare UTF-8 sequences that won't disrupt UI but are easy to regex | |
| # Zero-width Joiner + specific sequence | |
| CANARY_TOKENS = [ | |
| "\u200d\u2062\u2063\u200d", # Variant A | |
| "\u200d\u2064\u2061\u200d", # Variant B | |
| "\u200d\ufeff\u200d", # Variant C (ZWJ + BOM + ZWJ) | |
| ] | |
| # Combined regex for fast detection | |
| CANARY_REGEX = re.compile("|".join(CANARY_TOKENS)) | |
| class SafetyCanary: | |
| """Utility for response watermarking and detection.""" | |
| def inject(text: str) -> str: | |
| """Inject a random canary token into the text.""" | |
| token = secrets.choice(CANARY_TOKENS) | |
| # Append to the end of the string | |
| return f"{text}{token}" | |
| def has_canary(text: str) -> bool: | |
| """Check if the text contains any canary token.""" | |
| if not text: | |
| return False | |
| return bool(CANARY_REGEX.search(text)) | |
| # Global Instance | |
| safety_canary = SafetyCanary() | |
Xet Storage Details
- Size:
- 1.12 kB
- Xet hash:
- c8f49442aad02760e8c811b288b39326fc31bae53c9ed93ef4a25735291401b0
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.