sdk_wheel_matrix.sh 11 KB

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