31 lines
852 B
Python
31 lines
852 B
Python
# sanitize.py — Unified agent name sanitization contract for herdr 0.8.0 (E3 & E4)
|
|
|
|
import re
|
|
|
|
def sanitize_herdr_agent_name(name: str) -> str:
|
|
"""
|
|
Sanitizes an agent name according to herdr 0.8.0 rules:
|
|
1. Convert to lowercase.
|
|
2. Replace invalid characters ([^a-z0-9_-]) with '-'.
|
|
3. Ensure starts with a lowercase letter [a-z], prefixing with 'x-' if needed.
|
|
4. Truncate to maximum 32 characters (16 + '-' + 15).
|
|
"""
|
|
if not name:
|
|
return "agent"
|
|
|
|
# 1. Lowercase
|
|
s = str(name).lower()
|
|
|
|
# 2. Replace illegal characters with '-'
|
|
s = re.sub(r'[^a-z0-9_-]', '-', s)
|
|
|
|
# 3. Ensure starts with a letter [a-z]
|
|
if not s or not s[0].isalpha():
|
|
s = "x-" + s
|
|
|
|
# 4. Truncate to 32 chars if needed
|
|
if len(s) > 32:
|
|
s = s[:16] + '-' + s[-15:]
|
|
|
|
return s
|