fix(hook): make check-messages.sh JSON sanitizer work without python3 on PATH
sanitize_json() called `python3` unconditionally, but on ACG Windows boxes
the Microsoft Store python3 alias is disabled and `py` is the launcher
(feedback_python_windows). When `python3` was missing the function silently
returned empty, and the surrounding `result_safe='{"messages":[]}'` default
dropped every unread coord message — no error, no warning, no toast.
Now prefers identity.json's `.python.command` (set during machine onboarding,
matching the pattern other scripts already use), falls back to
`command -v python3 || command -v py || command -v python`, and if no Python
is available falls back to `tr -d '\000-\037'` so jq can still parse — lossy
on real \n/\t in string fields but keeps messages visible instead of dropping
them.
This commit is contained in:
@@ -22,19 +22,38 @@ if [ -f "$IDENTITY_FILE" ]; then
|
|||||||
USER_ALIAS=$(jq -r '.user // empty' "$IDENTITY_FILE" 2>/dev/null)
|
USER_ALIAS=$(jq -r '.user // empty' "$IDENTITY_FILE" 2>/dev/null)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Resolve a Python interpreter for the JSON sanitizer. Prefer the value in
|
||||||
|
# identity.json (.python.command — set during machine onboarding); fall back
|
||||||
|
# to a PATH lookup. On ACG Windows boxes the Microsoft Store `python3` alias
|
||||||
|
# is disabled and `py` is the canonical launcher, so an unconditional
|
||||||
|
# `python3` call would silently fail and cause every coord message to be
|
||||||
|
# dropped. If no Python is available at all, the sanitizer falls back to
|
||||||
|
# `tr -d` (lossy on real \n/\t in string fields but keeps messages visible).
|
||||||
|
PY=""
|
||||||
|
if [ -f "$IDENTITY_FILE" ]; then
|
||||||
|
PY=$(jq -r '.python.command // empty' "$IDENTITY_FILE" 2>/dev/null)
|
||||||
|
fi
|
||||||
|
if [ -z "$PY" ]; then
|
||||||
|
PY=$(command -v python3 || command -v py || command -v python)
|
||||||
|
fi
|
||||||
|
|
||||||
# Sanitize JSON: the coord API sometimes returns message bodies with unescaped
|
# Sanitize JSON: the coord API sometimes returns message bodies with unescaped
|
||||||
# control chars (U+0000-U+001F) — invalid JSON per RFC 8259, which makes jq
|
# control chars (U+0000-U+001F) — invalid JSON per RFC 8259, which makes jq
|
||||||
# abort with "Invalid string: control characters ... must be escaped". Round-
|
# abort with "Invalid string: control characters ... must be escaped". Round-
|
||||||
# trip through python's json.loads(strict=False) (which accepts them) and
|
# trip through python's json.loads(strict=False) (which accepts them) and
|
||||||
# re-emit properly escaped JSON so jq downstream works on every payload.
|
# re-emit properly escaped JSON so jq downstream works on every payload.
|
||||||
sanitize_json() {
|
sanitize_json() {
|
||||||
python3 -c '
|
if [ -n "$PY" ]; then
|
||||||
|
"$PY" -c '
|
||||||
import json, sys
|
import json, sys
|
||||||
try:
|
try:
|
||||||
print(json.dumps(json.loads(sys.stdin.read(), strict=False)))
|
print(json.dumps(json.loads(sys.stdin.read(), strict=False)))
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
' 2>/dev/null
|
' 2>/dev/null
|
||||||
|
else
|
||||||
|
tr -d '\000-\037'
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# --- Unread messages ---------------------------------------------------------
|
# --- Unread messages ---------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user