fix(herdr): ensure unique agent name via sha1 truncation and align mock errors

- Replace legacy s[:16]-s[-15:] truncation with 8-char SHA-1 hash suffix in sanitize.py and lib.sh to prevent workspace name collisions
- Align mock Herdr error messages in tests/conftest.py to real Herdr 0.8.0 output format
- Add missing required/invalid_agent_name to early abort regex in lib.sh
- Remove legacy heuristics in lib.sh has-session and unify mock agent lookups
- Add unit tests in tests/test_sanitize_and_mock_errors.py (256/256 passed)
This commit is contained in:
2026-08-15 09:00:48 +09:00
parent 301ff5bb1f
commit 14b9de14fc
5 changed files with 165 additions and 27 deletions
+26 -8
View File
@@ -36,9 +36,17 @@ _sanitize_herdr_agent_name() {
s=$(echo "$n" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9_-]/-/g')
if [[ ! "$s" =~ ^[a-z] ]]; then s="x-$s"; fi
if [ "${#s}" -gt 32 ]; then
local p="${s:0:16}"
local suf="${s: -15}"
s="${p}-${suf}"
local h=""
if command -v shasum >/dev/null 2>&1; then
h=$(printf '%s' "$s" | shasum 2>/dev/null | awk '{print $1}' | cut -c 1-8)
elif command -v sha1sum >/dev/null 2>&1; then
h=$(printf '%s' "$s" | sha1sum 2>/dev/null | awk '{print $1}' | cut -c 1-8)
elif command -v openssl >/dev/null 2>&1; then
h=$(printf '%s' "$s" | openssl sha1 2>/dev/null | awk '{print $NF}' | cut -c 1-8)
else
h=$(python3 -c "import hashlib,sys; print(hashlib.sha1(sys.argv[1].encode()).hexdigest()[:8])" "$s" 2>/dev/null || echo "00000000")
fi
s="${s:0:23}-${h}"
fi
printf '%s\n' "${s:0:32}"
}
@@ -215,9 +223,17 @@ _sanitize_herdr_agent_name() {
s=$(echo "$n" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9_-]/-/g')
if [[ ! "$s" =~ ^[a-z] ]]; then s="x-$s"; fi
if [ "${#s}" -gt 32 ]; then
local p="${s:0:16}"
local suf="${s: -15}"
s="${p}-${suf}"
local h=""
if command -v shasum >/dev/null 2>&1; then
h=$(printf '%s' "$s" | shasum 2>/dev/null | awk '{print $1}' | cut -c 1-8)
elif command -v sha1sum >/dev/null 2>&1; then
h=$(printf '%s' "$s" | sha1sum 2>/dev/null | awk '{print $1}' | cut -c 1-8)
elif command -v openssl >/dev/null 2>&1; then
h=$(printf '%s' "$s" | openssl sha1 2>/dev/null | awk '{print $NF}' | cut -c 1-8)
else
h=$(python3 -c "import hashlib,sys; print(hashlib.sha1(sys.argv[1].encode()).hexdigest()[:8])" "$s" 2>/dev/null || echo "00000000")
fi
s="${s:0:23}-${h}"
fi
printf '%s\n' "${s:0:32}"
}
@@ -272,13 +288,15 @@ case "$cmd" in
fi
if _real_herdr agent list 2>/dev/null | TARGET_NAME="$sess" python3 -c "
import sys, json, os
from lib_py.agents.sanitize import sanitize_herdr_agent_name
tn = os.environ.get('TARGET_NAME', '')
stn = sanitize_herdr_agent_name(tn)
try:
d = json.loads(sys.stdin.read())
agents = d.get('result', {}).get('agents', [])
for a in agents:
an = a.get('name', '')
if an == tn or (len(tn) > 16 and an.startswith(tn[:14]) and an.endswith(tn[-12:])):
if an == tn or an == stn:
sys.exit(0)
except Exception:
pass
@@ -507,7 +525,7 @@ except Exception:
success=1
break
fi
if echo "$res" | grep -qiE "^usage:|unknown option|unknown flag"; then
if echo "$res" | grep -qiE "^usage:|unknown option|unknown flag|missing required|invalid_agent_name|^error:"; then
break
fi
if [ "$i" -lt 2 ]; then