sdk_wheel_matrix.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.types import ActionBatch
  118. from hth_sdk.profiles import prod2u
  119. assert hth_pb2 is not None and hth_pb2_grpc is not None
  120. async def main():
  121. async with await connect(observe=os.environ["HTH_OBSERVE_SOCK"], control=os.environ["HTH_CONTROL_SOCK"], role="control") as client:
  122. caps = await client.get_capabilities("prod2u-0")
  123. assert caps.api_version == "hth.api.v1"
  124. assert caps.timing is not None and caps.timing.recommended_rpc_deadline_ms > 2000
  125. assert caps.timing.recommended_rpc_deadline_ms >= caps.timing.temperature_transaction_worst_case_ms
  126. snap = await client.get_snapshot("prod2u-0")
  127. n = 0
  128. async for ev in client.subscribe_telemetry("prod2u-0"):
  129. n += 1
  130. if n >= 2:
  131. break
  132. async with await client.acquire_lease("prod2u-0", "matrix", timedelta(seconds=8), auto_renew=False) as lease:
  133. b = prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision)
  134. for act in caps.actuators:
  135. tgt = snap.target(act.id)
  136. if tgt is not None:
  137. b.set(act.id, tgt.commanded_target)
  138. else:
  139. b.set(act.id, act.safety_default)
  140. batch = b.build()
  141. val = await lease.validate(batch)
  142. assert val is not None
  143. res = await lease.apply(batch)
  144. assert res.accepted, res
  145. assert res.confirmation != "CONFIRMED"
  146. last_eid = 0
  147. n_ev = 0
  148. async for ev in client.subscribe_events("prod2u-0"):
  149. eid = int(ev.event_id)
  150. assert eid > 0
  151. if last_eid:
  152. assert eid > last_eid, (last_eid, eid)
  153. last_eid = eid
  154. n_ev += 1
  155. if n_ev >= 3:
  156. break
  157. assert n_ev >= 1
  158. safe = await lease.enter_safe_mode("wheel-matrix")
  159. assert safe is not None
  160. async with await client.acquire_lease("prod2u-0", "matrix-resume", timedelta(seconds=8), auto_renew=False) as lease2:
  161. snap2 = await client.get_snapshot("prod2u-0")
  162. rb = prod2u.builder("prod2u-0", capabilities=caps).based_on(snap2.snapshot_seq, snap2.control_revision)
  163. for act in caps.actuators:
  164. tgt = snap2.target(act.id)
  165. if tgt is not None:
  166. rb.set(act.id, tgt.commanded_target)
  167. else:
  168. rb.set(act.id, act.safety_default)
  169. resumed = await lease2.resume_from_safe(rb.build())
  170. assert resumed.accepted, resumed
  171. asyncio.run(main())
  172. print("matrix-ok", getattr(hth_sdk, "__version__", "0.3.0"))
  173. PY
  174. deactivate
  175. done
  176. # Production timing: 5s GET_TEMPERATURE + wheel + control lease + periodic Apply.
  177. TIMING_CFG="$WORKDIR/run/hthd-timing.yaml"
  178. cat > "$TIMING_CFG" <<EOF
  179. api_version: hth.api.v1
  180. device_id: prod2u-0
  181. backend: mock
  182. profile: $ROOT/profiles/prod2u-door-temp/profile.json
  183. runtime_root: $RUNTIME
  184. sockets:
  185. observe: $OBS
  186. control: $CTL
  187. poll:
  188. temperature_ms: 80
  189. motor_status_ms: 200
  190. watchdog:
  191. heartbeat_period_ms: 8000
  192. lease:
  193. min_ttl_ms: 50
  194. max_ttl_ms: 20000
  195. safety:
  196. fan: 10
  197. ptc: 0
  198. door_close_percent: 0
  199. ring_buffer_size: 32
  200. subscriber_buffer_size: 4
  201. grpc:
  202. reflection: false
  203. mock:
  204. seed: 1
  205. startup_complete: true
  206. slow_get_ms: 5000
  207. EOF
  208. CFG="$TIMING_CFG"
  209. stop_hthd
  210. start_hthd
  211. PY_TIMING=python3.13
  212. command -v "$PY_TIMING" >/dev/null || PY_TIMING=python3
  213. venv_t="$WORKDIR/venv-timing"
  214. rm -rf "$venv_t"
  215. "$PY_TIMING" -m venv "$venv_t"
  216. # shellcheck disable=SC1091
  217. source "$venv_t/bin/activate"
  218. pip install -q -i https://mirrors.aliyun.com/pypi/simple "$WHEEL[live]"
  219. HTH_OBSERVE_SOCK="$OBS" HTH_CONTROL_SOCK="$CTL" python - <<'PY' || fail=1
  220. import asyncio, os, time
  221. from datetime import timedelta
  222. from hth_sdk.client import connect
  223. from hth_sdk.exceptions import ActionRejected
  224. from hth_sdk.profiles import prod2u
  225. def _fill(b, caps, snap):
  226. for act in caps.actuators:
  227. tgt = snap.target(act.id)
  228. b.set(act.id, tgt.commanded_target if tgt is not None else act.safety_default)
  229. return b
  230. async def main():
  231. async with await connect(observe=os.environ["HTH_OBSERVE_SOCK"], control=os.environ["HTH_CONTROL_SOCK"], role="control") as client:
  232. caps = await client.get_capabilities("prod2u-0")
  233. assert caps.timing.temperature_transaction_worst_case_ms >= 5000
  234. rec = caps.timing.recommended_action_ttl_ms
  235. assert rec > 2000
  236. for _ in range(80):
  237. snap = await client.get_snapshot("prod2u-0")
  238. if snap.lifecycle == "READY":
  239. break
  240. await asyncio.sleep(0.05)
  241. await asyncio.sleep(0.25)
  242. async with await client.acquire_lease("prod2u-0", "timing", timedelta(seconds=20), auto_renew=True) as lease:
  243. rejected = False
  244. for _ in range(25):
  245. snap = await client.get_snapshot("prod2u-0")
  246. b = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
  247. batch = b.valid_for(timedelta(seconds=2)).build()
  248. t0 = time.monotonic()
  249. try:
  250. res = await lease.apply(batch)
  251. except ActionRejected as exc:
  252. dt = time.monotonic() - t0
  253. if dt < 0.8 and "EXPIRED_DEADLINE" in str(exc):
  254. rejected = True
  255. break
  256. raise
  257. dt = time.monotonic() - t0
  258. if (not res.accepted) and dt < 0.8:
  259. rejected = True
  260. break
  261. await asyncio.sleep(0.05)
  262. assert rejected, "2s deadline must be rejected during 5s GET_TEMPERATURE"
  263. ok = 0
  264. for i in range(3):
  265. snap = await client.get_snapshot("prod2u-0")
  266. b = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
  267. t0 = time.monotonic()
  268. res = await lease.apply(b.build())
  269. dt = time.monotonic() - t0
  270. assert res.accepted, (i, res, dt)
  271. assert res.confirmation != "CONFIRMED"
  272. ok += 1
  273. assert ok == 3
  274. # Latest-wins: two overlapping applies during the 5s GET.
  275. snap = await client.get_snapshot("prod2u-0")
  276. b1 = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
  277. b2 = _fill(prod2u.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision), caps, snap)
  278. r1, r2 = await asyncio.gather(lease.apply(b1.build()), lease.apply(b2.build()), return_exceptions=True)
  279. # One may be superseded or both accepted if the GET finished; neither may be CONFIRMED.
  280. for r in (r1, r2):
  281. if isinstance(r, Exception):
  282. continue
  283. assert getattr(r, "confirmation", "") != "CONFIRMED"
  284. asyncio.run(main())
  285. print("python-sdk-5s-timing-ok")
  286. PY
  287. deactivate
  288. stop_hthd
  289. echo "interpreters_run=$ran"
  290. if [[ "$ran" -lt 1 ]]; then
  291. echo "Python wheel matrix NOT RUN (no 3.11/3.12/3.13 interpreter)"
  292. exit 2
  293. fi
  294. if [[ "$ran" -lt 3 ]]; then
  295. echo "Python 3.11/3.12/3.13 matrix PARTIAL (ran $ran); missing interpreters are NOT RUN, not PASS"
  296. fi
  297. exit "$fail"