sdk_wheel_matrix.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/usr/bin/env bash
  2. # Python 3.11/3.12/3.13 wheel matrix against a live Mock UDS daemon.
  3. # Missing interpreters are reported, not silently counted as pass.
  4. set -euo pipefail
  5. ROOT="$(cd "$(dirname "$0")/.." && pwd)"
  6. SDK="$ROOT/sdk/python"
  7. WORKDIR="${TMPDIR:-/tmp}/hth-sdk-matrix"
  8. mkdir -p "$WORKDIR/dist" "$WORKDIR/run"
  9. export PATH="/opt/python-3.11/bin:/opt/python-3.12/bin:/usr/bin:/data/prod2u/opt/go1.25.13/bin:/root/go/bin:${PATH}"
  10. export GOTOOLCHAIN="${GOTOOLCHAIN:-local}"
  11. if [[ -d /data/prod2u/opt/go1.25.13 ]]; then
  12. export GOROOT=/data/prod2u/opt/go1.25.13
  13. fi
  14. PY_BUILD="${HTH_PYTHON:-python3.13}"
  15. command -v "$PY_BUILD" >/dev/null || PY_BUILD=python3
  16. BUILD_VENV="$WORKDIR/build-venv"
  17. rm -rf "$BUILD_VENV"
  18. "$PY_BUILD" -m venv "$BUILD_VENV"
  19. "$BUILD_VENV/bin/pip" install -q -i https://mirrors.aliyun.com/pypi/simple build
  20. "$BUILD_VENV/bin/python" -m build --wheel --outdir "$WORKDIR/dist" "$SDK"
  21. WHEEL="$(ls -1t "$WORKDIR/dist"/hth_sdk-*.whl | head -n1)"
  22. echo "wheel=$WHEEL"
  23. CFG="$WORKDIR/run/hthd.yaml"
  24. RUNTIME="$WORKDIR/run"
  25. mkdir -p "$RUNTIME/observe" "$RUNTIME/control"
  26. OBS="$RUNTIME/observe/observe.sock"
  27. CTL="$RUNTIME/control/control.sock"
  28. cat > "$CFG" <<EOF
  29. api_version: hth.api.v1
  30. device_id: prod2u-0
  31. backend: mock
  32. profile: $ROOT/profiles/prod2u-door-temp/profile.json
  33. runtime_root: $RUNTIME
  34. sockets:
  35. observe: $OBS
  36. control: $CTL
  37. poll:
  38. temperature_ms: 80
  39. motor_status_ms: 50
  40. watchdog:
  41. heartbeat_period_ms: 200
  42. lease:
  43. min_ttl_ms: 50
  44. max_ttl_ms: 5000
  45. safety:
  46. fan: 10
  47. ptc: 0
  48. door_close_percent: 0
  49. ring_buffer_size: 32
  50. subscriber_buffer_size: 4
  51. grpc:
  52. reflection: false
  53. mock:
  54. seed: 1
  55. startup_complete: true
  56. EOF
  57. HTHD="$WORKDIR/run/hthd"
  58. (cd "$ROOT" && CGO_ENABLED=0 go build -o "$HTHD" ./cmd/hthd)
  59. start_hthd() {
  60. rm -f "$OBS" "$CTL"
  61. "$HTHD" --config "$CFG" &
  62. HTHD_PID=$!
  63. for i in $(seq 1 50); do
  64. [[ -S "$OBS" && -S "$CTL" ]] && return 0
  65. sleep 0.1
  66. done
  67. echo "hthd sockets not ready"
  68. return 1
  69. }
  70. stop_hthd() {
  71. if [[ -n "${HTHD_PID:-}" ]]; then
  72. kill "$HTHD_PID" 2>/dev/null || true
  73. wait "$HTHD_PID" 2>/dev/null || true
  74. HTHD_PID=
  75. fi
  76. }
  77. cleanup() { stop_hthd; }
  78. trap cleanup EXIT
  79. start_hthd
  80. fail=0
  81. ran=0
  82. seen=" "
  83. for py in python3.11 python3.12 python3.13 python3; do
  84. if ! command -v "$py" >/dev/null 2>&1; then
  85. echo "MISSING $py"
  86. continue
  87. fi
  88. ver="$("$py" -c 'import sys; print("%d.%d"%sys.version_info[:2])')"
  89. case "$ver" in
  90. 3.11|3.12|3.13) ;;
  91. *) echo "SKIP $py ($ver not in 3.11/3.12/3.13)"; continue ;;
  92. esac
  93. if [[ "$seen" == *" $ver "* ]]; then
  94. echo "SKIP $py (already ran $ver)"
  95. continue
  96. fi
  97. seen="$seen$ver "
  98. venv="$WORKDIR/venv-$ver"
  99. rm -rf "$venv"
  100. if ! "$py" -m venv "$venv" >/tmp/hth-venv-"$ver".log 2>&1; then
  101. echo "SKIP $py (venv failed)"
  102. cat /tmp/hth-venv-"$ver".log | tail -5
  103. continue
  104. fi
  105. ran=$((ran+1))
  106. stop_hthd
  107. start_hthd
  108. # shellcheck disable=SC1091
  109. source "$venv/bin/activate"
  110. pip install -q -i https://mirrors.aliyun.com/pypi/simple "$WHEEL[live]"
  111. HTH_OBSERVE_SOCK="$OBS" HTH_CONTROL_SOCK="$CTL" python - <<'PY' || fail=1
  112. import asyncio, os
  113. from datetime import timedelta
  114. import hth_sdk
  115. from hth_sdk.generated.hth.api.v1 import hth_pb2, hth_pb2_grpc
  116. from hth_sdk.client import connect
  117. from hth_sdk.thermal import ThermalHTH, connect_thermal
  118. from hth_sdk.types import ActionBatch
  119. from hth_sdk.profiles import prod2u
  120. assert hth_pb2 is not None and hth_pb2_grpc is not None
  121. assert ThermalHTH is not None and connect_thermal is not None
  122. async def main():
  123. async with await connect(observe=os.environ["HTH_OBSERVE_SOCK"], control=os.environ["HTH_CONTROL_SOCK"], role="control") as client:
  124. caps = await client.get_capabilities("prod2u-0")
  125. assert caps.api_version == "hth.api.v1"
  126. assert caps.timing is not None and caps.timing.recommended_rpc_deadline_ms > 2000
  127. assert caps.timing.recommended_rpc_deadline_ms >= caps.timing.temperature_transaction_worst_case_ms
  128. snap = await client.get_snapshot("prod2u-0")
  129. n = 0
  130. async for ev in client.subscribe_telemetry("prod2u-0"):
  131. n += 1
  132. if n >= 2:
  133. break
  134. async with await client.acquire_lease("prod2u-0", "matrix", timedelta(seconds=8), auto_renew=False) as lease:
  135. b = prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision)
  136. for act in caps.actuators:
  137. tgt = snap.target(act.id)
  138. if tgt is not None:
  139. b.set(act.id, tgt.commanded_target)
  140. else:
  141. b.set(act.id, act.safety_default)
  142. batch = b.build()
  143. val = await lease.validate(batch)
  144. assert val is not None
  145. res = await lease.apply(batch)
  146. assert res.accepted, res
  147. assert res.confirmation != "CONFIRMED"
  148. last_eid = 0
  149. n_ev = 0
  150. async for ev in client.subscribe_events("prod2u-0"):
  151. eid = int(ev.event_id)
  152. assert eid > 0
  153. if last_eid:
  154. assert eid > last_eid, (last_eid, eid)
  155. last_eid = eid
  156. n_ev += 1
  157. if n_ev >= 3:
  158. break
  159. assert n_ev >= 1
  160. safe = await lease.enter_safe_mode("wheel-matrix")
  161. assert safe is not None
  162. async with await client.acquire_lease("prod2u-0", "matrix-resume", timedelta(seconds=8), auto_renew=False) as lease2:
  163. snap2 = await client.get_snapshot("prod2u-0")
  164. rb = prod2u.builder("prod2u-0", capabilities=caps).based_on(snap2.snapshot_seq, snap2.control_revision)
  165. for act in caps.actuators:
  166. tgt = snap2.target(act.id)
  167. if tgt is not None:
  168. rb.set(act.id, tgt.commanded_target)
  169. else:
  170. rb.set(act.id, act.safety_default)
  171. resumed = await lease2.resume_from_safe(rb.build())
  172. assert resumed.accepted, resumed
  173. asyncio.run(main())
  174. print("matrix-ok", getattr(hth_sdk, "__version__", "0.3.0"))
  175. PY
  176. deactivate
  177. done
  178. # Production timing: 5s GET_TEMPERATURE + wheel + control lease + periodic Apply.
  179. TIMING_CFG="$WORKDIR/run/hthd-timing.yaml"
  180. cat > "$TIMING_CFG" <<EOF
  181. api_version: hth.api.v1
  182. device_id: prod2u-0
  183. backend: mock
  184. profile: $ROOT/profiles/prod2u-door-temp/profile.json
  185. runtime_root: $RUNTIME
  186. sockets:
  187. observe: $OBS
  188. control: $CTL
  189. poll:
  190. temperature_ms: 80
  191. motor_status_ms: 200
  192. watchdog:
  193. heartbeat_period_ms: 8000
  194. lease:
  195. min_ttl_ms: 50
  196. max_ttl_ms: 20000
  197. safety:
  198. fan: 10
  199. ptc: 0
  200. door_close_percent: 0
  201. ring_buffer_size: 32
  202. subscriber_buffer_size: 4
  203. grpc:
  204. reflection: false
  205. mock:
  206. seed: 1
  207. startup_complete: true
  208. slow_get_ms: 5000
  209. EOF
  210. CFG="$TIMING_CFG"
  211. stop_hthd
  212. start_hthd
  213. PY_TIMING=python3.13
  214. command -v "$PY_TIMING" >/dev/null || PY_TIMING=python3
  215. venv_t="$WORKDIR/venv-timing"
  216. rm -rf "$venv_t"
  217. "$PY_TIMING" -m venv "$venv_t"
  218. # shellcheck disable=SC1091
  219. source "$venv_t/bin/activate"
  220. pip install -q -i https://mirrors.aliyun.com/pypi/simple "$WHEEL[live]"
  221. HTH_OBSERVE_SOCK="$OBS" HTH_CONTROL_SOCK="$CTL" python - <<'PY' || fail=1
  222. import asyncio, os, time
  223. from datetime import timedelta
  224. from hth_sdk.client import connect
  225. from hth_sdk.exceptions import ActionRejected
  226. from hth_sdk.profiles import prod2u
  227. def _fill(b, caps, snap):
  228. for act in caps.actuators:
  229. tgt = snap.target(act.id)
  230. b.set(act.id, tgt.commanded_target if tgt is not None else act.safety_default)
  231. return b
  232. async def main():
  233. async with await connect(observe=os.environ["HTH_OBSERVE_SOCK"], control=os.environ["HTH_CONTROL_SOCK"], role="control") as client:
  234. caps = await client.get_capabilities("prod2u-0")
  235. assert caps.timing.temperature_transaction_worst_case_ms >= 5000
  236. rec = caps.timing.recommended_action_ttl_ms
  237. assert rec > 2000
  238. for _ in range(80):
  239. snap = await client.get_snapshot("prod2u-0")
  240. if snap.lifecycle == "READY":
  241. break
  242. await asyncio.sleep(0.05)
  243. await asyncio.sleep(0.25)
  244. async with await client.acquire_lease("prod2u-0", "timing", timedelta(seconds=20), auto_renew=True) as lease:
  245. rejected = False
  246. for _ in range(25):
  247. snap = await client.get_snapshot("prod2u-0")
  248. b = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
  249. batch = b.valid_for(timedelta(seconds=2)).build()
  250. t0 = time.monotonic()
  251. try:
  252. res = await lease.apply(batch)
  253. except ActionRejected as exc:
  254. dt = time.monotonic() - t0
  255. if dt < 0.8 and "EXPIRED_DEADLINE" in str(exc):
  256. rejected = True
  257. break
  258. raise
  259. dt = time.monotonic() - t0
  260. if (not res.accepted) and dt < 0.8:
  261. rejected = True
  262. break
  263. await asyncio.sleep(0.05)
  264. assert rejected, "2s deadline must be rejected during 5s GET_TEMPERATURE"
  265. ok = 0
  266. for i in range(3):
  267. snap = await client.get_snapshot("prod2u-0")
  268. b = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
  269. t0 = time.monotonic()
  270. res = await lease.apply(b.build())
  271. dt = time.monotonic() - t0
  272. assert res.accepted, (i, res, dt)
  273. assert res.confirmation != "CONFIRMED"
  274. ok += 1
  275. assert ok == 3
  276. # Latest-wins: two overlapping applies during the 5s GET.
  277. snap = await client.get_snapshot("prod2u-0")
  278. b1 = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
  279. b2 = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
  280. r1, r2 = await asyncio.gather(lease.apply(b1.build()), lease.apply(b2.build()), return_exceptions=True)
  281. # One may be superseded or both accepted if the GET finished; neither may be CONFIRMED.
  282. for r in (r1, r2):
  283. if isinstance(r, Exception):
  284. continue
  285. assert getattr(r, "confirmation", "") != "CONFIRMED"
  286. asyncio.run(main())
  287. print("python-sdk-5s-timing-ok")
  288. PY
  289. deactivate
  290. stop_hthd
  291. echo "interpreters_run=$ran"
  292. if [[ "$ran" -lt 1 ]]; then
  293. echo "Python wheel matrix NOT RUN (no 3.11/3.12/3.13 interpreter)"
  294. exit 2
  295. fi
  296. if [[ "$ran" -lt 3 ]]; then
  297. echo "Python 3.11/3.12/3.13 matrix PARTIAL (ran $ran); missing interpreters are NOT RUN, not PASS"
  298. fi
  299. exit "$fail"