test_uart_receipts.py 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. #!/usr/bin/env python3
  2. """Tests for non-ChipWatcher UART/RS232 receipt validation."""
  3. from __future__ import annotations
  4. import json
  5. import hashlib
  6. import re
  7. import sys
  8. import tempfile
  9. import unittest
  10. from pathlib import Path
  11. sys.dont_write_bytecode = True
  12. ROOT = Path(__file__).resolve().parents[2]
  13. sys.path.insert(0, str(ROOT / "tools"))
  14. from uart_receipts import ( # noqa: E402
  15. REAL_BMC_J6_HOST_SENSOR_REAL_FAN_PTC_ALLOWED_PIN_PROFILES,
  16. REAL_BMC_J6_HOST_SENSOR_REAL_FAN_PTC_ALLOWED_TOP_MODULES_BY_PIN_PROFILE,
  17. _host_sensor_row_fields,
  18. _parse_host_sensor_report_text,
  19. _parse_j6raw_frames,
  20. _parse_lossless_key_value_lines,
  21. backend_capabilities,
  22. build_parser,
  23. build_protocol_offline_receipt,
  24. compute_file_hash,
  25. validate_cpld_bmc_acceptance_v1_receipt,
  26. validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt,
  27. validate_real_bmc_j6_windows_gate_v2_receipt,
  28. validate_offline_fidelity_gate_v1_receipt,
  29. validate_final_gate_v2_receipt,
  30. validate_uart_receipt,
  31. )
  32. class UartReceiptsTests(unittest.TestCase):
  33. def valid_offline_toolchain(self) -> dict:
  34. tool_safety = {
  35. "opens_com_ports": False,
  36. "toggles_dtr_rts": False,
  37. "installs_drivers": False,
  38. "programs_flash": False,
  39. "touches_ptc_fan_bmc": False,
  40. }
  41. tools = {
  42. "python": {"name": "python", "status": "present", "path": r"C:\Python\python.exe"},
  43. "iverilog": {"name": "iverilog", "status": "present", "path": r"C:\tools\iverilog.exe"},
  44. "vvp": {"name": "vvp", "status": "present", "path": r"C:\tools\vvp.exe"},
  45. "verilator": {"name": "verilator", "status": "present", "path": r"C:\tools\verilator_bin.exe"},
  46. "yosys": {"name": "yosys", "status": "present", "path": r"C:\tools\yosys.exe"},
  47. "sby": {"name": "sby", "status": "present", "path": r"C:\tools\sby.exe"},
  48. "cocotb": {"name": "python_module:cocotb", "status": "present", "python": r"C:\venv\Scripts\python.exe"},
  49. "td_cli": {"name": "td_commands_prompt", "status": "present", "path": r"C:\TD\bin\td_commands_prompt.exe"},
  50. }
  51. return {
  52. "status": "pass",
  53. "toolchain": {"status": "pass", "tools": tools},
  54. "manifest": {
  55. "install_roots": {
  56. "oss_cad_suite": r"C:\Users\Administrator\CodexToolchains\CPLD\oss-cad-suite\latest",
  57. "cocotb_venv": r"C:\Users\Administrator\CodexToolchains\CPLD\venvs\pid-study-cocotb-venv",
  58. "downloads": r"C:\Users\Administrator\CodexToolchains\CPLD\downloads",
  59. "manifests": r"C:\Users\Administrator\CodexToolchains\CPLD\manifests",
  60. },
  61. "tools": [{"name": name, "status": "present", "path": data.get("path") or data.get("python")} for name, data in tools.items()],
  62. "downloads": [],
  63. "created_paths": [],
  64. "reused_paths": [],
  65. "cleanup_plan": {
  66. "default": "dry_run",
  67. "requires_owner_marker": True,
  68. "allowed_root": r"C:\Users\Administrator\CodexToolchains\CPLD",
  69. },
  70. "safety": tool_safety,
  71. },
  72. "cleanup_policy": {
  73. "default": "dry_run",
  74. "requires_owner_marker": True,
  75. "allowed_root": r"C:\Users\Administrator\CodexToolchains\CPLD",
  76. "owner_marker_name": ".cpld_toolchain_owner.json",
  77. "actual_delete_requires_apply": True,
  78. },
  79. "safety": tool_safety,
  80. "authority_boundary": "user-space offline toolchain bootstrap only",
  81. }
  82. def valid_offline_layers(self) -> dict:
  83. coverage_categories = [
  84. "protocol",
  85. "uart_bit",
  86. "control",
  87. "safety",
  88. "debug",
  89. "virtual_driver_lab",
  90. "thermal",
  91. "fault_injection",
  92. "cocotb_virtual_lab",
  93. "formal",
  94. "mutation",
  95. "td_toolchain",
  96. ]
  97. shim_names = [
  98. "pyserial_loopback_url",
  99. "socket_pipe_transport",
  100. "bmc_text_driver",
  101. "host_register_plane",
  102. "debug_uart_decoder",
  103. "fault_injection_facade",
  104. ]
  105. fault_contracts = [
  106. "timeout",
  107. "disconnect",
  108. "partial_read",
  109. "partial_write",
  110. "permission_denied",
  111. "buffer_overflow",
  112. "tx_backpressure",
  113. "rx_overflow",
  114. ]
  115. thermal_models = [
  116. "conservative",
  117. "optimistic",
  118. "extreme",
  119. "random_perturbation",
  120. "sensor_drift",
  121. "sensor_delay",
  122. "crosstalk",
  123. "fan_degradation",
  124. "ptc_thermal_inertia",
  125. ]
  126. mutants = [
  127. "invert_fault_condition",
  128. "remove_force_off",
  129. "break_channel_compare",
  130. "drop_debug_event",
  131. "break_debug_checksum",
  132. "bad_formal_property",
  133. ]
  134. return {
  135. "static": {"status": "pass", "returncode": 0},
  136. "model": {"status": "pass", "tests": {"test_pid_model.py": {"returncode": 0}}},
  137. "replay": {"status": "pass", "sample_count": 1},
  138. "protocol_fuzz": {"status": "pass", "scenarios": [{"name": "no_echo_bare_hex", "status": "pass"}]},
  139. "hdl_sim": {
  140. "status": "pass",
  141. "tests": [{"name": "uart_rs232_j6_host_sensor_real_fan_ptc_debug921_top_tb", "status": "pass"}],
  142. },
  143. "cocotb_virtual_lab": {"status": "pass", "sim": {"status": "pass", "tests": [{"name": "pid_core_smoke_scoreboard", "status": "pass"}]}},
  144. "virtual_driver_lab": {
  145. "status": "pass",
  146. "user_mode_shims": [{"name": name, "status": "pass", "transcript": {"roundtrip": True}} for name in shim_names],
  147. "fault_contracts": fault_contracts,
  148. "trace_hash": "a" * 64,
  149. },
  150. "formal": {"status": "pass", "targets": [{"name": "fault_implies_ptc_off", "status": "pass"}]},
  151. "detector_selftest": {"status": "pass", "results": [{"name": "debug_checksum", "status": "pass"}]},
  152. "mutation": {
  153. "status": "pass",
  154. "mutants": [
  155. {"name": name, "status": "pass", "mutation_status": "killed", "artifact_sha256": "b" * 64}
  156. for name in mutants
  157. ],
  158. },
  159. "thermal_digital_twin": {
  160. "status": "pass",
  161. "models": [{"name": name, "status": "pass", "trace_hash": "c" * 64, "violations": []} for name in thermal_models],
  162. },
  163. "coverage_matrix": {
  164. "status": "pass",
  165. "items": [{"category": category, "status": "pass", "covered_by": ["contract_evidence"]} for category in coverage_categories],
  166. },
  167. "td_build": {"status": "pass", "elaboration": {"status": "pass", "database": {"sha256": "d" * 64}, "plan": {"script_sha256": "e" * 64}}},
  168. "debug_bundle": {"status": "pass", "path": "tmp/debug_bundle/offline"},
  169. }
  170. def valid_offline_receipt(self) -> dict:
  171. layers = self.valid_offline_layers()
  172. return {
  173. "schema": "offline-fidelity-gate-v1-receipt-v1",
  174. "status": "pass",
  175. "generated_at": "2026-06-30T00:00:00Z",
  176. "toolchain": self.valid_offline_toolchain(),
  177. "layers": layers,
  178. "coverage_matrix": layers["coverage_matrix"],
  179. "safety": {
  180. "opens_com_ports": False,
  181. "toggles_dtr_rts": False,
  182. "programs_flash": False,
  183. "installs_drivers": False,
  184. "real_ptc_fan_bmc_action_attempted": False,
  185. },
  186. "authority_boundary": "offline logic, protocol, safety, debug, and virtual thermal evidence only",
  187. }
  188. def write_real_bmc_windows_gate_v2_fixture(
  189. self,
  190. root: Path,
  191. *,
  192. poll: int = 3,
  193. valid: int = 3,
  194. parser_error: int = 0,
  195. stale: int = 0,
  196. uerr: int = 0,
  197. derr: int = 0,
  198. mode: str = "PROGRAM_SRAM",
  199. ) -> Path:
  200. bitstream = root / "uart_rs232_j6_system_debug_primary.bit"
  201. td_receipt = root / "td_debug.json"
  202. transcript = root / "j6sys-transcript.txt"
  203. event_stream = root / "j6sys-event-stream.json"
  204. register_dump = root / "j6sys-register-dump.json"
  205. sram_attestation = root / "manual-sram-attestation.json"
  206. bitstream.write_bytes(b"real bmc j6 windows debug bitstream")
  207. tx = 3 + 43 * poll
  208. rx = 300 if poll >= 3 else 1
  209. tok = 144 if valid >= 3 else 1
  210. transcript.write_text(
  211. "\n".join(
  212. [
  213. "J6SYS wake=00000003 poll=00000001 tx=0000002E rx=00000001 uerr=00 state=0 tok=00000001 seq=00000001 valid=00000001 perr=0 stale=0 frame=0 duovf=0 dbg=00000001 derr=00000000",
  214. f"J6SYS wake=00000003 poll={poll:08X} tx={tx:08X} rx={rx:08X} uerr={uerr:02X} state=0 tok={tok:08X} seq={valid:08X} valid={valid:08X} perr={parser_error:X} stale={stale:X} frame=0 duovf=0 dbg=00000002 derr={derr:08X}",
  215. ]
  216. )
  217. + "\n",
  218. encoding="utf-8",
  219. )
  220. event_stream.write_text(
  221. json.dumps(
  222. {
  223. "schema": "j6sys-event-stream-v1",
  224. "status": "pass",
  225. "events": [
  226. {"poll": poll, "valid": valid, "source_id": "BMC_UART_TEXT", "parser_error": bool(parser_error), "stale": bool(stale)}
  227. ],
  228. }
  229. ),
  230. encoding="utf-8",
  231. )
  232. register_dump.write_text(
  233. json.dumps(
  234. {
  235. "schema": "j6sys-register-dump-v1",
  236. "status": "pass",
  237. "debug_registers": {"wake": 3, "poll": poll, "rx": rx, "valid": valid, "uerr": uerr, "derr": derr},
  238. }
  239. ),
  240. encoding="utf-8",
  241. )
  242. td_receipt.write_text(
  243. json.dumps(
  244. {
  245. "status": "pass",
  246. "top": "uart_rs232_j6_system_debug_primary",
  247. "top_module": "uart_rs232_j6_system_debug_top",
  248. "mode": "implementation",
  249. "pin_profile": "primary",
  250. "bitstream": {"path": str(bitstream), "sha256": compute_file_hash(bitstream)},
  251. "timing_gate": {"status": "pass"},
  252. }
  253. ),
  254. encoding="utf-8",
  255. )
  256. sram_attestation.write_text(
  257. json.dumps(
  258. {
  259. "status": "pass",
  260. "mode": mode,
  261. "operator": "manual",
  262. "programmed_at": "2026-07-01T00:00:00Z",
  263. "bitstream_sha256": compute_file_hash(bitstream),
  264. "td_receipt_sha256": compute_file_hash(td_receipt),
  265. "td_log_sha256": "0" * 64,
  266. }
  267. ),
  268. encoding="utf-8",
  269. )
  270. receipt = root / "real_bmc_j6_windows_gate_v2.json"
  271. payload = {
  272. "schema": "real-bmc-j6-windows-gate-v2-receipt-v1",
  273. "status": "pass",
  274. "counterparty": "real_bmc",
  275. "pin_profile": {"name": "primary", "connector": "J6", "tx_ball": "T4", "rx_ball": "R5", "debug_rx_ball": "P10", "debug_tx_ball": "R10"},
  276. "programming": {"mode": mode, "manual_programming": True, "bitstream": {"path": str(bitstream), "sha256": compute_file_hash(bitstream)}},
  277. "layers": {
  278. "td_bitstream": {"status": "pass", "receipt": {"path": str(td_receipt), "sha256": compute_file_hash(td_receipt)}},
  279. "manual_sram_programming": {
  280. "status": "pass",
  281. "operator": "manual",
  282. "mode": mode,
  283. "manual_programming": True,
  284. "programmed_at": "2026-07-01T00:00:00Z",
  285. "attestation": {"path": str(sram_attestation), "sha256": compute_file_hash(sram_attestation)},
  286. },
  287. "windows_serial_capture": {
  288. "status": "pass",
  289. "port": "COM7",
  290. "baud": 115200,
  291. "format": "8N1",
  292. "duration_s": 90,
  293. "transcript": {"path": str(transcript), "sha256": compute_file_hash(transcript)},
  294. "event_stream": {"path": str(event_stream), "sha256": compute_file_hash(event_stream)},
  295. "register_dump": {"path": str(register_dump), "sha256": compute_file_hash(register_dump)},
  296. },
  297. "real_bmc_protocol": {"status": "pass", "valid_frame_count": valid, "source_id": "BMC_UART_TEXT"},
  298. "safety_contract": {"status": "pass", "ptc_fan_pins_exported": False, "real_actuator_action_attempted": False},
  299. "artifact_hashes": {
  300. "status": "pass",
  301. "bitstream_sha256": compute_file_hash(bitstream),
  302. "td_receipt_sha256": compute_file_hash(td_receipt),
  303. "manual_sram_attestation_sha256": compute_file_hash(sram_attestation),
  304. "transcript_sha256": compute_file_hash(transcript),
  305. "event_stream_sha256": compute_file_hash(event_stream),
  306. "register_dump_sha256": compute_file_hash(register_dump),
  307. },
  308. },
  309. "safety": {
  310. "flash_attempted": False,
  311. "driver_install_attempted": False,
  312. "programs_flash": False,
  313. "opens_unknown_com_ports": False,
  314. "toggles_dtr_rts": False,
  315. "ptc_fan_pins_exported": False,
  316. "real_ptc_fan_action_attempted": False,
  317. "uncontrolled_real_bmc_action_attempted": False,
  318. },
  319. "authority_boundary": "real-bmc-j6-windows-gate-v2 proves Windows P10/R10 J6SYS evidence for controlled J6 BMC UART/parser/safety only.",
  320. }
  321. receipt.write_text(json.dumps(payload), encoding="utf-8")
  322. return receipt
  323. def write_host_sensor_real_fan_ptc_debug921_fixture(
  324. self,
  325. root: Path,
  326. *,
  327. hardware_status: str = "not_run",
  328. ) -> Path:
  329. root.mkdir(parents=True, exist_ok=True)
  330. bitstream = root / "uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921.bit"
  331. td_receipt = root / "td_host_sensor_real_fan_ptc_debug921.json"
  332. sram_attestation = root / "manual-sram-attestation.json"
  333. host_raw = root / "host-port-raw.bin"
  334. host_escaped = root / "host-port-escaped.txt"
  335. debug_raw = root / "debug-uart-raw.bin"
  336. debug_escaped = root / "debug-uart-escaped.txt"
  337. replay = root / "bmc-raw-replay.bin"
  338. debug_frame_stream = root / "debug-frame-stream.json"
  339. host_sensor_report = root / "host-sensor-report.json"
  340. host_transcript = root / "host-command-transcript.json"
  341. manifest = root / "j6-host-sensor-real-fan-ptc-capture-manifest.json"
  342. commands = [
  343. "SET PTC 01 10",
  344. "SET PTC 24 20",
  345. "SET FAN 01 40",
  346. "SET FAN 04 30",
  347. "SET PID ENABLE",
  348. "GET STATUS",
  349. "GET MAP",
  350. "GET SENSOR",
  351. "GET SENSOR",
  352. "GET SENSOR",
  353. "SET PID DISABLE",
  354. "DISABLE",
  355. ]
  356. expected_ack_cmds = {
  357. "SET PTC 01 10": "PTCCH",
  358. "SET PTC 24 20": "PTCCH",
  359. "SET FAN 01 40": "FANCH",
  360. "SET FAN 04 30": "FANCH",
  361. "SET PID ENABLE": "PID_EN",
  362. "GET STATUS": "STATUS",
  363. "GET MAP": "MAP___",
  364. "GET SENSOR": "SENSOR",
  365. "SET PID DISABLE": "PID_DI",
  366. "DISABLE": "DISABL",
  367. }
  368. host_lines: list[str] = []
  369. debug_lines: list[str] = [
  370. "J6READY profile=temp-uart-ptc-remap",
  371. "J6CMD",
  372. "J6EVT s=06 cmd=08 c=01 p=000 ok=1 e=0 to=0",
  373. "J6PWM seq=00000006 src=host_manual pid=disabled timeout=0 ptc01=010 ptc24=020 fan01=040 fan04=030",
  374. "J6MOD seq=00000006 pid=disabled pid_requested=1 pid_active=0 bmc=observe_only host=slave pwm=host_manual timeout_s=5",
  375. "J6MAP profile=temp-uart-ptc-remap bmc_tx=T4 bmc_rx=R5 host_tx=P7 host_rx=P8 debug_rx=P10 debug_tx=R10 ptc=24 fan=4 fg=8 install=4",
  376. ]
  377. dimm_samples: list[dict[str, object]] = []
  378. replay_bytes = bytearray()
  379. raw_offset = 0
  380. for sample_seq in range(1, 4):
  381. payload_bytes = bytes([0xFF, 0xFF] * 24) if sample_seq == 2 else bytes(
  382. [value for dimm in range(24) for value in (0x30 + dimm + sample_seq, 0x00)]
  383. )
  384. replay_bytes.extend(payload_bytes)
  385. checksum = sum(payload_bytes) & 0xFFFF
  386. debug_lines.append(
  387. f"J6RAW s={sample_seq:04X} o={raw_offset:08X} l=30 u={checksum:04X} d={payload_bytes.hex().upper()}"
  388. )
  389. raw_offset += len(payload_bytes)
  390. valid = 0 if sample_seq == 2 else 24
  391. missing = 24 if sample_seq == 2 else 0
  392. status = "all_ff" if sample_seq == 2 else "valid"
  393. host_lines.append(
  394. f"SENS seq={sample_seq:08X} status={status} valid={valid:02d} missing={missing:02d} fault=0 raw=1 drop=00000000"
  395. )
  396. debug_lines.append(
  397. f"J6TEMP seq={sample_seq:08X} status={status} valid={valid:02d} missing={missing:02d} fault=0 host_report_drop=00000000"
  398. )
  399. rows = []
  400. for dimm in range(24):
  401. missing_flag = sample_seq == 2
  402. temp = -1 if missing_flag else 48 + dimm + sample_seq
  403. raw_pair = "FFFF" if missing_flag else f"{temp:02X}00"
  404. host_lines.append(
  405. f"DIMM seq={sample_seq:08X} id={dimm:02X} raw={raw_pair} temp={temp:03d} "
  406. f"missing={1 if missing_flag else 0} fault=0 status={status}"
  407. )
  408. rows.append({"id": dimm, "raw": raw_pair, "temp": temp, "missing": missing_flag, "fault": False})
  409. dimm_samples.append({"seq": sample_seq, "status": status, "valid": valid, "missing": missing, "dimm_rows": rows})
  410. host_lines.extend(
  411. [
  412. "PWM seq=00000008 src=host_manual timeout=1 ptc01=000 ptc24=000 fan01=030 fan04=030",
  413. "PID req=1 active=0 src=host_manual",
  414. "STAT hd=00000000 rd=00000000 he=00000000",
  415. ]
  416. )
  417. debug_lines.extend(
  418. [
  419. "J6PWM seq=0000000A src=host_manual pid=disabled timeout=1 ptc01=000 ptc24=000 fan01=030 fan04=030",
  420. "J6MOD seq=0000000A pid=disabled pid_requested=1 pid_active=0 bmc=observe_only host=slave pwm=host_manual timeout_s=5",
  421. f"J6STAT raw_in={len(replay_bytes):08X} raw_out={len(replay_bytes):08X} rd=00000000 hd=00000000 be=00000000 he=00000000",
  422. ]
  423. )
  424. host_text = "\r\n".join(host_lines) + "\r\n"
  425. debug_text = "\r\n".join(debug_lines) + "\r\n"
  426. bitstream.write_bytes(b"debug921 host sensor real fan ptc bitstream")
  427. host_raw.write_bytes(host_text.encode("ascii"))
  428. host_escaped.write_text(host_text, encoding="utf-8", newline="\n")
  429. debug_raw.write_bytes(debug_text.encode("ascii"))
  430. debug_escaped.write_text(debug_text, encoding="utf-8", newline="\n")
  431. replay.write_bytes(bytes(replay_bytes))
  432. td_receipt.write_text(
  433. json.dumps(
  434. {
  435. "status": "pass",
  436. "top": "uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921",
  437. "top_module": "uart_rs232_j6_host_sensor_real_fan_ptc_temp_uart_ptc_remap_debug921_top",
  438. "mode": "implementation",
  439. "pin_profile": "temp-uart-ptc-remap",
  440. "bitstream": {"path": str(bitstream), "sha256": compute_file_hash(bitstream)},
  441. "timing_gate": {"status": "pass"},
  442. }
  443. ),
  444. encoding="utf-8",
  445. )
  446. sram_attestation.write_text(
  447. json.dumps(
  448. {
  449. "status": "pass",
  450. "mode": "PROGRAM_SRAM",
  451. "operator": "manual",
  452. "programmed_at": "2026-07-01T00:00:00Z",
  453. "bitstream_sha256": compute_file_hash(bitstream),
  454. "td_receipt_sha256": compute_file_hash(td_receipt),
  455. }
  456. ),
  457. encoding="utf-8",
  458. )
  459. host_transcript.write_text(
  460. json.dumps(
  461. {
  462. "schema": "j6-host-sensor-real-fan-ptc-debug921-host-command-transcript-v1",
  463. "status": "pass",
  464. "host_port": "COM8",
  465. "baud": 115200,
  466. "format": "8N1",
  467. "writes_to_serial": True,
  468. "write_port_role": "HostPort",
  469. "debug_port_writes_to_serial": False,
  470. "preflight": {
  471. "schema": "j6-host-sensor-real-fan-ptc-startup-preflight-v1",
  472. "status": "pass",
  473. "ready_seen": True,
  474. "ready_line": "READY profile=temp-uart-ptc-remap seq=00000000",
  475. "j6ready_seen": True,
  476. "j6ready_line": "J6READY profile=temp-uart-ptc-remap",
  477. "sync_command": "GET STATUS",
  478. "sync_ack_seen": True,
  479. "preflight_ack": "OK seq=00000001 cmd=STATUS",
  480. "counts_as_formal_command": False,
  481. "formal_sequence_base": 0,
  482. },
  483. "commands": [
  484. {
  485. "seq": index + 1,
  486. "command": command,
  487. "allowed": True,
  488. "bytes_written": len(command) + 1,
  489. "expected_ack_cmd": expected_ack_cmds[command],
  490. "ack_line": f"OK seq={index + 1:08X} cmd={expected_ack_cmds[command]}",
  491. "ack_offset": index * 32,
  492. "response_offset": index * 32,
  493. "response_raw": f"OK seq={index + 1:08X} cmd={expected_ack_cmds[command]}",
  494. "response_token": f"OK seq={index + 1:08X} cmd={expected_ack_cmds[command]}",
  495. "response_class": "ok",
  496. "response_seq": index + 1,
  497. "response_cmd": expected_ack_cmds[command],
  498. "seq_match": True,
  499. "cmd_match": True,
  500. "response_late": False,
  501. "stale": False,
  502. "accepted": True,
  503. }
  504. for index, command in enumerate(commands)
  505. ],
  506. }
  507. ),
  508. encoding="utf-8",
  509. )
  510. host_sensor_report.write_text(
  511. json.dumps(
  512. {
  513. "schema": "j6-host-sensor-real-fan-ptc-host-sensor-report-v1",
  514. "status": "pass",
  515. "samples": dimm_samples,
  516. "host_report_drop": 0,
  517. "bmc_timeout": False,
  518. "source": "HostPort",
  519. }
  520. ),
  521. encoding="utf-8",
  522. )
  523. debug_frame_stream.write_text(
  524. json.dumps(
  525. {
  526. "schema": "j6-host-sensor-real-fan-ptc-debug-frame-stream-v1",
  527. "status": "pass",
  528. "frames": debug_lines,
  529. "raw_drop": 0,
  530. "host_report_drop": 0,
  531. "bmc_berr": 0,
  532. "host_err": 0,
  533. }
  534. ),
  535. encoding="utf-8",
  536. )
  537. manifest.write_text(
  538. json.dumps(
  539. {
  540. "schema": "j6-host-sensor-real-fan-ptc-capture-manifest-v1",
  541. "status": "pass",
  542. "host_port": "COM8",
  543. "debug_port": "COM7",
  544. "host_baud": 115200,
  545. "debug_baud": 921600,
  546. "host_port_writes_to_serial": True,
  547. "debug_port_writes_to_serial": False,
  548. "enumerates_com_ports": False,
  549. "opens_unknown_com_ports": False,
  550. "pin_profile_name": "temp-uart-ptc-remap",
  551. "pin_profile": {
  552. "name": "temp-uart-ptc-remap",
  553. "connector": "J6",
  554. "tx_ball": "T4",
  555. "rx_ball": "R5",
  556. "bmc_tx_ball": "T4",
  557. "bmc_rx_ball": "R5",
  558. "host_tx_ball": "P7",
  559. "host_rx_ball": "P8",
  560. "debug_rx_ball": "P10",
  561. "debug_tx_ball": "R10",
  562. },
  563. "raw_replay": {"path": "bmc-raw-replay.bin", "sha256": compute_file_hash(replay)},
  564. }
  565. ),
  566. encoding="utf-8",
  567. )
  568. payload = {
  569. "schema": "real-bmc-j6-host-sensor-real-fan-ptc-debug921-v1-receipt-v1",
  570. "status": hardware_status,
  571. "counterparty": "real_bmc",
  572. "diagnostic_scope": "host_sensor_real_fan_ptc_debug921",
  573. "pin_profile_name": "temp-uart-ptc-remap",
  574. "pin_profile": {
  575. "name": "temp-uart-ptc-remap",
  576. "connector": "J6",
  577. "tx_ball": "T4",
  578. "rx_ball": "R5",
  579. "bmc_tx_ball": "T4",
  580. "bmc_rx_ball": "R5",
  581. "host_tx_ball": "P7",
  582. "host_rx_ball": "P8",
  583. "debug_rx_ball": "P10",
  584. "debug_tx_ball": "R10",
  585. },
  586. "programming": {"mode": "PROGRAM_SRAM", "manual_programming": True, "bitstream": {"path": str(bitstream), "sha256": compute_file_hash(bitstream)}},
  587. "collector": {
  588. "host_port": "COM8",
  589. "debug_port": "COM7",
  590. "baud": 115200,
  591. "debug_baud": 921600,
  592. "format": "8N1",
  593. "flow_control": "none",
  594. "dtr_enable": False,
  595. "rts_enable": False,
  596. "writes_to_serial": True,
  597. "write_port_role": "HostPort",
  598. "host_port_writes_to_serial": True,
  599. "debug_port_writes_to_serial": False,
  600. "opens_unknown_com_ports": False,
  601. "enumerates_com_ports": False,
  602. },
  603. "layers": {
  604. "td_bitstream": {"status": "pass", "receipt": {"path": str(td_receipt), "sha256": compute_file_hash(td_receipt)}},
  605. "manual_sram_programming": {
  606. "status": "pass",
  607. "operator": "manual",
  608. "mode": "PROGRAM_SRAM",
  609. "manual_programming": True,
  610. "programmed_at": "2026-07-01T00:00:00Z",
  611. "attestation": {"path": str(sram_attestation), "sha256": compute_file_hash(sram_attestation)},
  612. },
  613. "host_command_sequence": {"status": "pass", "commands": commands},
  614. "host_control_transcript": {
  615. "status": "pass",
  616. "host_command_transcript": {"path": str(host_transcript), "sha256": compute_file_hash(host_transcript)},
  617. },
  618. "host_sensor_reports": {
  619. "status": "pass",
  620. "host_transcript_text": {"path": str(host_escaped), "sha256": compute_file_hash(host_escaped)},
  621. "host_raw_bin": {"path": str(host_raw), "sha256": compute_file_hash(host_raw)},
  622. "host_sensor_report": {"path": str(host_sensor_report), "sha256": compute_file_hash(host_sensor_report)},
  623. },
  624. "debug_frame_capture": {
  625. "status": "pass",
  626. "debug_transcript": {"path": str(debug_escaped), "sha256": compute_file_hash(debug_escaped)},
  627. "raw_bin": {"path": str(debug_raw), "sha256": compute_file_hash(debug_raw)},
  628. "raw_replay": {"path": str(replay), "sha256": compute_file_hash(replay)},
  629. "debug_frame_stream": {"path": str(debug_frame_stream), "sha256": compute_file_hash(debug_frame_stream)},
  630. "manifest": {"path": str(manifest), "sha256": compute_file_hash(manifest)},
  631. },
  632. "bmc_raw_sampling": {"status": "pass", "minimum_completed_samples": 3, "completed_samples": 3, "bmc_timeout": False},
  633. "pid_boundary": {"status": "pass", "pid_requested": True, "pid_requested_ever": True, "pid_active": False, "pwm_source": "host_manual"},
  634. "timeout_evidence": {"status": "pass", "timeout_evidence": {"timeout_s": 5, "ptc_all_percent": 0, "fan_all_percent": 30}},
  635. "pin_map": {
  636. "status": "pass",
  637. "profile": "temp-uart-ptc-remap",
  638. "ptc_count": 24,
  639. "fan_count": 4,
  640. "fan_fg_count": 8,
  641. "fan_install_count": 4,
  642. "bmc_tx_ball": "T4",
  643. "bmc_rx_ball": "R5",
  644. "host_tx_ball": "P7",
  645. "host_rx_ball": "P8",
  646. "debug_rx_ball": "P10",
  647. "debug_tx_ball": "R10",
  648. },
  649. "safety_contract": {
  650. "status": "pass",
  651. "pid_default_enabled": False,
  652. "pid_active": False,
  653. "bmc_controls_pwm": False,
  654. "debug_port_writes_to_serial": False,
  655. "host_port_writes_to_serial": True,
  656. "real_thermal_performance_claimed": False,
  657. },
  658. "real_fan_ptc_hardware_status": {"status": hardware_status, "real_fan_ptc_hardware_present": False, "qualifies_final_gate": False},
  659. "artifact_hashes": {
  660. "status": "pass",
  661. "bitstream_sha256": compute_file_hash(bitstream),
  662. "td_receipt_sha256": compute_file_hash(td_receipt),
  663. "manual_sram_attestation_sha256": compute_file_hash(sram_attestation),
  664. "host_transcript_text_sha256": compute_file_hash(host_escaped),
  665. "host_raw_bin_sha256": compute_file_hash(host_raw),
  666. "host_sensor_report_sha256": compute_file_hash(host_sensor_report),
  667. "debug_transcript_sha256": compute_file_hash(debug_escaped),
  668. "debug_raw_bin_sha256": compute_file_hash(debug_raw),
  669. "raw_replay_sha256": compute_file_hash(replay),
  670. "debug_frame_stream_sha256": compute_file_hash(debug_frame_stream),
  671. "host_command_transcript_sha256": compute_file_hash(host_transcript),
  672. "manifest_sha256": compute_file_hash(manifest),
  673. },
  674. },
  675. "safety": {
  676. "flash_attempted": False,
  677. "driver_install_attempted": False,
  678. "programs_flash": False,
  679. "programs_nonvolatile": False,
  680. "opens_unknown_com_ports": False,
  681. "enumerates_com_ports": False,
  682. "toggles_dtr_rts": False,
  683. "debug_port_writes_to_serial": False,
  684. "host_port_writes_to_serial": True,
  685. "uncontrolled_real_bmc_action_attempted": False,
  686. "real_thermal_performance_claimed": False,
  687. },
  688. "authority_boundary": (
  689. "real-bmc-j6-host-sensor-real-fan-ptc-debug921-v1 validates SRAM-only COM8 host "
  690. "1-based real FAN/PTC command evidence, COM7 debug921 frame replay, BMC raw DIMM "
  691. "sampling, HostPort temperature reporting, PID requested-but-inactive boundary, timeout fallback, "
  692. "and real FAN/PTC hardware status. It does not satisfy final-gate-v2, real-bmc-j6-windows-gate-v2, "
  693. "flash, thermal performance, production PTC/FAN qualification, or real PID closed-loop pass."
  694. ),
  695. }
  696. receipt = root / "real_bmc_j6_host_sensor_real_fan_ptc_debug921.json"
  697. receipt.write_text(json.dumps(payload), encoding="utf-8")
  698. return receipt
  699. def test_backend_capabilities_report_missing_optional_python_modules(self) -> None:
  700. caps = backend_capabilities()
  701. self.assertIn(caps["serial_backend"], {"pyserial", "powershell_dotnet_limited"})
  702. self.assertIn(caps["schema_backend"], {"jsonschema", "builtin_limited"})
  703. self.assertIn("strict_gate_requires", caps)
  704. def test_public_cli_only_exposes_latest_real_fan_ptc_and_retained_gates(self) -> None:
  705. parser = build_parser()
  706. subparsers = next(action for action in parser._actions if action.dest == "command")
  707. commands = set(subparsers.choices)
  708. expected_commands = {
  709. "preflight",
  710. "protocol-offline",
  711. "validate",
  712. "final-gate-v2-validate",
  713. "offline-fidelity-gate-v1-validate",
  714. "real-bmc-j6-windows-gate-v2-validate",
  715. "cpld-bmc-acceptance-v1-validate",
  716. "real-bmc-j6-host-sensor-real-fan-ptc-debug921-v1-validate",
  717. }
  718. self.assertEqual(commands, expected_commands)
  719. for command in [
  720. "real-bmc-j6-cr-raw-diagnostic-v1-validate",
  721. "real-bmc-j6-sensor-raw-diagnostic-v1-validate",
  722. "real-bmc-j6-host-control-pwm-diagnostic-v1-validate",
  723. "real-bmc-j6-host-control-pwm-debug921-lossless-v1-validate",
  724. "real-bmc-j6-host-sensor-pwm-debug921-v1-validate",
  725. "real-bmc-j6-gate-v1-validate",
  726. ]:
  727. self.assertNotIn(command, commands)
  728. def test_real_fan_ptc_validator_does_not_adapt_through_legacy_host_sensor_pwm(self) -> None:
  729. source = (ROOT / "tools" / "uart_receipts.py").read_text(encoding="utf-8")
  730. start = source.index("def validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt")
  731. end = source.index("def validate_cpld_bmc_acceptance_v1_receipt", start)
  732. body = source[start:end]
  733. self.assertNotIn("_host_sensor_real_fan_ptc_as_pwm_payload", body)
  734. self.assertNotIn("validate_real_bmc_j6_host_sensor_pwm_debug921_v1_receipt(", body)
  735. def test_protocol_offline_receipt_accepts_echo_and_loose_hex(self) -> None:
  736. receipt = build_protocol_offline_receipt(
  737. text="ipmitool raw 0x36 0x6c 0xdf 0xdd 0x00 0x03\n"
  738. + " ".join(["0x23", "0x4b"] * 24),
  739. scenario="echo_0x_multiline",
  740. )
  741. self.assertEqual(receipt["schema"], "bmc-uart-protocol-receipt-v1")
  742. self.assertEqual(receipt["status"], "pass")
  743. self.assertEqual(receipt["parser"]["sample_count"], 24)
  744. self.assertGreaterEqual(receipt["parser"]["rejected_echo_windows"], 1)
  745. def test_protocol_offline_receipt_validates_itself_strictly(self) -> None:
  746. with tempfile.TemporaryDirectory() as tmp:
  747. receipt = build_protocol_offline_receipt(
  748. text=" ".join(["23", "4b"] * 24),
  749. scenario="bare_hex",
  750. )
  751. receipt_path = Path(tmp) / "protocol.json"
  752. receipt_path.write_text(json.dumps(receipt), encoding="utf-8")
  753. result = validate_uart_receipt(receipt_path, strict=True)
  754. self.assertEqual(result["status"], "pass")
  755. def test_loopback_strict_validation_rejects_blocked_and_missing_hashes(self) -> None:
  756. with tempfile.TemporaryDirectory() as tmp:
  757. receipt_path = Path(tmp) / "loopback.json"
  758. receipt_path.write_text(
  759. json.dumps(
  760. {
  761. "schema": "uart-loopback-receipt-v1",
  762. "status": "blocked_no_allowlist",
  763. "counterparty": "pc_emulator",
  764. "serial": {"port": "COM7", "baud": 115200, "format": "8N1", "allowlisted": False},
  765. "transcript": {"path": "transcript.txt"},
  766. "safety": {"flash_attempted": False, "real_ptc_fan_bmc_action_attempted": False, "driver_install_attempted": False},
  767. }
  768. ),
  769. encoding="utf-8",
  770. )
  771. result = validate_uart_receipt(receipt_path, strict=True)
  772. self.assertEqual(result["status"], "blocked_no_allowlist")
  773. self.assertIn("missing_transcript_hash", result["failures"])
  774. def test_board_sram_receipt_requires_sram_only_bitstream_and_debug_evidence(self) -> None:
  775. with tempfile.TemporaryDirectory() as tmp:
  776. root = Path(tmp)
  777. bit = root / "system.bit"
  778. transcript = root / "uart.log"
  779. debug = root / "debug.json"
  780. bit.write_bytes(b"fake bitstream")
  781. transcript.write_text("tx ipmitool\nrx ok\n", encoding="utf-8")
  782. debug.write_text(json.dumps({"events": [{"frame_seq": 1}], "register_dump": {"0x00": 1}}), encoding="utf-8")
  783. receipt = root / "board.json"
  784. receipt.write_text(
  785. json.dumps(
  786. {
  787. "schema": "uart-board-sram-receipt-v1",
  788. "status": "pass",
  789. "counterparty": "pc_emulator",
  790. "programming": {"mode": "PROGRAM_SRAM", "bitstream": {"path": str(bit), "sha256": compute_file_hash(bit)}},
  791. "serial": {"port": "COM7", "baud": 115200, "format": "8N1", "allowlisted": True, "dtr": False, "rts": False},
  792. "transcript": {"path": str(transcript), "sha256": compute_file_hash(transcript)},
  793. "debug_evidence": {"event_stream": str(debug), "register_dump": str(debug)},
  794. "safety": {"flash_attempted": False, "real_ptc_fan_bmc_action_attempted": False, "driver_install_attempted": False},
  795. }
  796. ),
  797. encoding="utf-8",
  798. )
  799. result = validate_uart_receipt(receipt, strict=True)
  800. self.assertEqual(result["status"], "pass")
  801. def test_board_sram_receipt_fails_closed_on_flash_marker(self) -> None:
  802. with tempfile.TemporaryDirectory() as tmp:
  803. receipt = Path(tmp) / "board.json"
  804. receipt.write_text(
  805. json.dumps(
  806. {
  807. "schema": "uart-board-sram-receipt-v1",
  808. "status": "pass",
  809. "counterparty": "pc_emulator",
  810. "programming": {"mode": "PROGRAM_FLASH", "bitstream": {"path": "x.bit", "sha256": "abc"}},
  811. "serial": {"port": "COM7", "baud": 115200, "format": "8N1", "allowlisted": True},
  812. "transcript": {"path": "uart.log", "sha256": "abc"},
  813. "debug_evidence": {"event_stream": "debug.json", "register_dump": "debug.json"},
  814. "safety": {"flash_attempted": True, "real_ptc_fan_bmc_action_attempted": False, "driver_install_attempted": False},
  815. }
  816. ),
  817. encoding="utf-8",
  818. )
  819. result = validate_uart_receipt(receipt, strict=True)
  820. self.assertEqual(result["status"], "fail_forbidden_action")
  821. def test_uart_soak_receipt_accepts_round_receipts_without_top_level_transcript(self) -> None:
  822. with tempfile.TemporaryDirectory() as tmp:
  823. root = Path(tmp)
  824. round_receipt = root / "round1.json"
  825. round_receipt.write_text(
  826. json.dumps(
  827. {
  828. "schema": "uart-loopback-receipt-v1",
  829. "status": "pass",
  830. "counterparty": "pc_emulator",
  831. "serial": {"port": "COM7", "baud": 115200, "format": "8N1", "allowlisted": True},
  832. "transcript": {"path": "transcript.txt", "sha256": compute_file_hash(root / "transcript.txt") if (root / "transcript.txt").exists() else "placeholder"},
  833. "safety": {"flash_attempted": False, "real_ptc_fan_bmc_action_attempted": False, "driver_install_attempted": False},
  834. }
  835. ),
  836. encoding="utf-8",
  837. )
  838. transcript = root / "transcript.txt"
  839. transcript.write_text("loopback ok\n", encoding="utf-8")
  840. payload = json.loads(round_receipt.read_text(encoding="utf-8"))
  841. payload["transcript"]["sha256"] = compute_file_hash(transcript)
  842. round_receipt.write_text(json.dumps(payload), encoding="utf-8")
  843. soak = root / "soak.json"
  844. soak.write_text(
  845. json.dumps(
  846. {
  847. "schema": "uart-soak-receipt-v1",
  848. "status": "pass",
  849. "counterparty": "pc_emulator",
  850. "duration_hours_requested": 0.1,
  851. "duration_hours_observed": 0.1,
  852. "rounds": [{"index": 1, "status": "pass", "receipt": str(round_receipt)}],
  853. "safety": {"flash_attempted": False, "real_ptc_fan_bmc_action_attempted": False, "driver_install_attempted": False},
  854. }
  855. ),
  856. encoding="utf-8",
  857. )
  858. result = validate_uart_receipt(soak, strict=True)
  859. self.assertEqual(result["status"], "pass")
  860. def test_final_gate_v2_rejects_chipwatcher_and_blocked_layers(self) -> None:
  861. receipt = {
  862. "schema": "final-gate-v2-receipt-v1",
  863. "status": "pass",
  864. "layers": {
  865. "static": {"status": "pass"},
  866. "model": {"status": "pass"},
  867. "replay": {"status": "pass"},
  868. "rtl_sim": {"status": "blocked_missing_tool"},
  869. "formal": {"status": "pass"},
  870. "detector_selftest": {"status": "pass"},
  871. "td_build": {"status": "pass"},
  872. "uart_strict": {"status": "pass", "schema": "uart-board-sram-validation-v1"},
  873. "chipwatcher": {"status": "pass"},
  874. },
  875. "authority_boundary": "control communication only",
  876. }
  877. result = validate_final_gate_v2_receipt(receipt, strict=True)
  878. self.assertEqual(result["status"], "fail")
  879. self.assertIn("blocked_layer:rtl_sim", result["failures"])
  880. self.assertIn("forbidden_chipwatcher_layer", result["failures"])
  881. def test_final_gate_v2_rejects_blocked_top_level_status_even_when_layers_pass(self) -> None:
  882. receipt = {
  883. "schema": "final-gate-v2-receipt-v1",
  884. "status": "blocked_missing_tool",
  885. "layers": {
  886. "static": {"status": "pass"},
  887. "model": {"status": "pass"},
  888. "replay": {"status": "pass"},
  889. "rtl_sim": {"status": "pass"},
  890. "formal": {"status": "pass"},
  891. "detector_selftest": {"status": "pass"},
  892. "td_build": {"status": "pass"},
  893. "uart_strict": {"status": "pass"},
  894. },
  895. "authority_boundary": "control communication only",
  896. }
  897. result = validate_final_gate_v2_receipt(receipt, strict=True)
  898. self.assertEqual(result["status"], "fail")
  899. self.assertIn("top_status_not_pass:blocked_missing_tool", result["failures"])
  900. def test_final_gate_v2_rejects_non_final_receipt_schema(self) -> None:
  901. receipt = {
  902. "schema": "not-a-final-gate-receipt-v1",
  903. "status": "pass",
  904. "layers": {},
  905. "authority_boundary": "not final gate evidence",
  906. }
  907. result = validate_final_gate_v2_receipt(receipt, strict=True)
  908. self.assertEqual(result["status"], "fail")
  909. self.assertIn("fail_schema", result["failures"])
  910. self.assertIn("missing_layer:static", result["failures"])
  911. self.assertIn("missing_layer:uart_strict", result["failures"])
  912. def test_offline_fidelity_gate_rejects_hardware_layers_and_blocked_required_layers(self) -> None:
  913. layers = {name: {"status": "pass"} for name in [
  914. "static",
  915. "model",
  916. "replay",
  917. "protocol_fuzz",
  918. "hdl_sim",
  919. "cocotb_virtual_lab",
  920. "virtual_driver_lab",
  921. "formal",
  922. "detector_selftest",
  923. "mutation",
  924. "thermal_digital_twin",
  925. "coverage_matrix",
  926. "td_build",
  927. "debug_bundle",
  928. ]}
  929. layers["hdl_sim"] = {"status": "blocked_missing_tool"}
  930. layers["hardware"] = {"status": "pass"}
  931. layers["uart_strict"] = {"status": "pass"}
  932. layers["chipwatcher"] = {"status": "pass"}
  933. layers["thermal_performance_pass"] = {"status": "pass"}
  934. receipt = {
  935. "schema": "offline-fidelity-gate-v1-receipt-v1",
  936. "status": "pass",
  937. "generated_at": "2026-06-30T00:00:00Z",
  938. "toolchain": {
  939. "status": "pass",
  940. "safety": {
  941. "opens_com_ports": False,
  942. "toggles_dtr_rts": False,
  943. "installs_drivers": False,
  944. "programs_flash": False,
  945. "touches_ptc_fan_bmc": False,
  946. },
  947. },
  948. "layers": layers,
  949. "coverage_matrix": {"status": "pass"},
  950. "safety": {
  951. "opens_com_ports": False,
  952. "toggles_dtr_rts": False,
  953. "programs_flash": False,
  954. "installs_drivers": False,
  955. "real_ptc_fan_bmc_action_attempted": False,
  956. },
  957. "authority_boundary": "offline thermal_performance_pass",
  958. }
  959. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  960. self.assertEqual(result["status"], "fail")
  961. self.assertIn("blocked_layer:hdl_sim", result["failures"])
  962. self.assertIn("forbidden_layer:hardware", result["failures"])
  963. self.assertIn("forbidden_layer:uart_strict", result["failures"])
  964. self.assertIn("forbidden_layer:chipwatcher", result["failures"])
  965. self.assertIn("forbidden_layer:thermal_performance_pass", result["failures"])
  966. self.assertIn("authority_overclaim_thermal_performance", result["failures"])
  967. def test_offline_fidelity_gate_rejects_uart_strict_and_chipwatcher_authority_overclaim(self) -> None:
  968. layers = {name: {"status": "pass"} for name in [
  969. "static",
  970. "model",
  971. "replay",
  972. "protocol_fuzz",
  973. "hdl_sim",
  974. "cocotb_virtual_lab",
  975. "virtual_driver_lab",
  976. "formal",
  977. "detector_selftest",
  978. "mutation",
  979. "thermal_digital_twin",
  980. "coverage_matrix",
  981. "td_build",
  982. "debug_bundle",
  983. ]}
  984. receipt = {
  985. "schema": "offline-fidelity-gate-v1-receipt-v1",
  986. "status": "pass",
  987. "generated_at": "2026-06-30T00:00:00Z",
  988. "toolchain": {
  989. "status": "pass",
  990. "safety": {
  991. "opens_com_ports": False,
  992. "toggles_dtr_rts": False,
  993. "installs_drivers": False,
  994. "programs_flash": False,
  995. "touches_ptc_fan_bmc": False,
  996. },
  997. },
  998. "layers": layers,
  999. "coverage_matrix": {"status": "pass"},
  1000. "safety": {
  1001. "opens_com_ports": False,
  1002. "toggles_dtr_rts": False,
  1003. "programs_flash": False,
  1004. "installs_drivers": False,
  1005. "real_ptc_fan_bmc_action_attempted": False,
  1006. },
  1007. "authority_boundary": "offline uart_strict pass and chipwatcher evidence passed",
  1008. }
  1009. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1010. self.assertEqual(result["status"], "fail")
  1011. self.assertIn("authority_overclaim_uart_strict", result["failures"])
  1012. self.assertIn("authority_overclaim_chipwatcher", result["failures"])
  1013. def test_offline_fidelity_gate_rejects_empty_pass_layers(self) -> None:
  1014. layers = {name: {"status": "pass"} for name in [
  1015. "static",
  1016. "model",
  1017. "replay",
  1018. "protocol_fuzz",
  1019. "hdl_sim",
  1020. "cocotb_virtual_lab",
  1021. "virtual_driver_lab",
  1022. "formal",
  1023. "detector_selftest",
  1024. "mutation",
  1025. "thermal_digital_twin",
  1026. "coverage_matrix",
  1027. "td_build",
  1028. "debug_bundle",
  1029. ]}
  1030. receipt = {
  1031. "schema": "offline-fidelity-gate-v1-receipt-v1",
  1032. "status": "pass",
  1033. "generated_at": "2026-06-30T00:00:00Z",
  1034. "toolchain": {
  1035. "status": "pass",
  1036. "safety": {
  1037. "opens_com_ports": False,
  1038. "toggles_dtr_rts": False,
  1039. "installs_drivers": False,
  1040. "programs_flash": False,
  1041. "touches_ptc_fan_bmc": False,
  1042. },
  1043. },
  1044. "layers": layers,
  1045. "coverage_matrix": {"status": "pass"},
  1046. "safety": {
  1047. "opens_com_ports": False,
  1048. "toggles_dtr_rts": False,
  1049. "programs_flash": False,
  1050. "installs_drivers": False,
  1051. "real_ptc_fan_bmc_action_attempted": False,
  1052. },
  1053. "authority_boundary": "offline logic, protocol, safety, debug, and virtual thermal evidence only",
  1054. }
  1055. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1056. self.assertEqual(result["status"], "fail")
  1057. self.assertIn("missing_layer_evidence:protocol_fuzz:scenarios", result["failures"])
  1058. self.assertIn("missing_layer_evidence:virtual_driver_lab:user_mode_shims", result["failures"])
  1059. self.assertIn("missing_layer_evidence:mutation:mutants", result["failures"])
  1060. self.assertIn("missing_layer_evidence:td_build:elaboration", result["failures"])
  1061. def test_offline_fidelity_gate_accepts_required_layers_with_contract_evidence(self) -> None:
  1062. receipt = self.valid_offline_receipt()
  1063. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1064. self.assertEqual(result["status"], "pass")
  1065. def test_offline_fidelity_gate_rejects_default_validation_with_empty_pass_layers(self) -> None:
  1066. receipt = self.valid_offline_receipt()
  1067. receipt["layers"]["virtual_driver_lab"] = {"status": "pass"}
  1068. result = validate_offline_fidelity_gate_v1_receipt(receipt)
  1069. self.assertEqual(result["status"], "fail")
  1070. self.assertIn("missing_layer_evidence:virtual_driver_lab:user_mode_shims", result["failures"])
  1071. def test_offline_fidelity_gate_rejects_incomplete_contract_coverage_matrix(self) -> None:
  1072. receipt = self.valid_offline_receipt()
  1073. receipt["layers"]["coverage_matrix"] = {
  1074. "status": "pass",
  1075. "items": [{"category": "protocol", "status": "pass", "covered_by": ["protocol_fuzz"]}],
  1076. }
  1077. receipt["coverage_matrix"] = receipt["layers"]["coverage_matrix"]
  1078. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1079. self.assertEqual(result["status"], "fail")
  1080. self.assertIn("missing_layer_evidence:coverage_matrix:uart_bit", result["failures"])
  1081. self.assertIn("missing_layer_evidence:coverage_matrix:td_toolchain", result["failures"])
  1082. def test_offline_fidelity_gate_rejects_underspecified_virtual_driver_lab(self) -> None:
  1083. receipt = self.valid_offline_receipt()
  1084. receipt["layers"]["virtual_driver_lab"] = {
  1085. "status": "pass",
  1086. "user_mode_shims": [{"name": "pyserial_loopback_url", "status": "pass", "transcript": {"roundtrip": True}}],
  1087. "fault_contracts": ["timeout"],
  1088. "trace_hash": "a" * 64,
  1089. }
  1090. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1091. self.assertEqual(result["status"], "fail")
  1092. self.assertIn("missing_layer_evidence:virtual_driver_lab:socket_pipe_transport", result["failures"])
  1093. self.assertIn("missing_layer_evidence:virtual_driver_lab:partial_write", result["failures"])
  1094. def test_offline_fidelity_gate_rejects_underspecified_or_failed_thermal_models(self) -> None:
  1095. receipt = self.valid_offline_receipt()
  1096. receipt["layers"]["thermal_digital_twin"] = {
  1097. "status": "pass",
  1098. "models": [{"name": "conservative", "status": "fail", "trace_hash": "c" * 64, "violations": ["ptc_not_fail_closed"]}],
  1099. }
  1100. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1101. self.assertEqual(result["status"], "fail")
  1102. self.assertIn("missing_layer_evidence:thermal_digital_twin:optimistic", result["failures"])
  1103. self.assertIn("missing_layer_evidence:thermal_digital_twin:model_status", result["failures"])
  1104. def test_offline_fidelity_gate_rejects_missing_toolchain_manifest_and_cleanup_policy(self) -> None:
  1105. receipt = self.valid_offline_receipt()
  1106. receipt["toolchain"] = {
  1107. "status": "pass",
  1108. "safety": {
  1109. "opens_com_ports": False,
  1110. "toggles_dtr_rts": False,
  1111. "installs_drivers": False,
  1112. "programs_flash": False,
  1113. "touches_ptc_fan_bmc": False,
  1114. },
  1115. "authority_boundary": "user-space offline toolchain bootstrap only",
  1116. }
  1117. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1118. self.assertEqual(result["status"], "fail")
  1119. self.assertIn("missing_toolchain_manifest", result["failures"])
  1120. self.assertIn("missing_toolchain_cleanup_policy", result["failures"])
  1121. def test_offline_fidelity_gate_requires_virtual_driver_lab(self) -> None:
  1122. layers = {name: {"status": "pass"} for name in [
  1123. "static",
  1124. "model",
  1125. "replay",
  1126. "protocol_fuzz",
  1127. "hdl_sim",
  1128. "cocotb_virtual_lab",
  1129. "formal",
  1130. "detector_selftest",
  1131. "mutation",
  1132. "thermal_digital_twin",
  1133. "coverage_matrix",
  1134. "td_build",
  1135. "debug_bundle",
  1136. ]}
  1137. receipt = {
  1138. "schema": "offline-fidelity-gate-v1-receipt-v1",
  1139. "status": "pass",
  1140. "generated_at": "2026-06-30T00:00:00Z",
  1141. "toolchain": {
  1142. "status": "pass",
  1143. "safety": {
  1144. "opens_com_ports": False,
  1145. "toggles_dtr_rts": False,
  1146. "installs_drivers": False,
  1147. "programs_flash": False,
  1148. "touches_ptc_fan_bmc": False,
  1149. },
  1150. },
  1151. "layers": layers,
  1152. "coverage_matrix": {"status": "pass"},
  1153. "safety": {
  1154. "opens_com_ports": False,
  1155. "toggles_dtr_rts": False,
  1156. "programs_flash": False,
  1157. "installs_drivers": False,
  1158. "real_ptc_fan_bmc_action_attempted": False,
  1159. },
  1160. "authority_boundary": "offline logic, protocol, safety, debug, and virtual thermal evidence only",
  1161. }
  1162. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1163. self.assertEqual(result["status"], "fail")
  1164. self.assertIn("missing_layer:virtual_driver_lab", result["failures"])
  1165. def test_offline_fidelity_gate_rejects_toolchain_and_safety_overclaims(self) -> None:
  1166. layers = {name: {"status": "pass"} for name in [
  1167. "static",
  1168. "model",
  1169. "replay",
  1170. "protocol_fuzz",
  1171. "hdl_sim",
  1172. "cocotb_virtual_lab",
  1173. "virtual_driver_lab",
  1174. "formal",
  1175. "detector_selftest",
  1176. "mutation",
  1177. "thermal_digital_twin",
  1178. "coverage_matrix",
  1179. "td_build",
  1180. "debug_bundle",
  1181. ]}
  1182. receipt = {
  1183. "schema": "offline-fidelity-gate-v1-receipt-v1",
  1184. "status": "pass",
  1185. "generated_at": "2026-06-30T00:00:00Z",
  1186. "toolchain": {
  1187. "status": "blocked_missing_tool",
  1188. "safety": {
  1189. "opens_com_ports": False,
  1190. "toggles_dtr_rts": False,
  1191. "installs_drivers": True,
  1192. "programs_flash": False,
  1193. "touches_ptc_fan_bmc": False,
  1194. },
  1195. },
  1196. "layers": layers,
  1197. "coverage_matrix": {"status": "pass"},
  1198. "safety": {
  1199. "opens_com_ports": True,
  1200. "toggles_dtr_rts": False,
  1201. "programs_flash": False,
  1202. "installs_drivers": False,
  1203. "real_ptc_fan_bmc_action_attempted": False,
  1204. },
  1205. "notes": "DRIVER_INSTALL PROGRAM_FLASH",
  1206. "authority_boundary": "offline logic, protocol, safety, debug, and virtual thermal evidence only",
  1207. }
  1208. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1209. self.assertEqual(result["status"], "fail")
  1210. self.assertIn("toolchain_status_not_pass:blocked_missing_tool", result["failures"])
  1211. self.assertIn("toolchain_safety_not_false:installs_drivers", result["failures"])
  1212. self.assertIn("safety_not_false:opens_com_ports", result["failures"])
  1213. self.assertIn("forbidden_action_marker:DRIVER_INSTALL", result["failures"])
  1214. self.assertIn("forbidden_action_marker:PROGRAM_FLASH", result["failures"])
  1215. def test_offline_fidelity_gate_rejects_offline_forbidden_hardware_markers(self) -> None:
  1216. receipt = self.valid_offline_receipt()
  1217. receipt["notes"] = ["OPEN_COM COM3", "DTR asserted", "PROGRAM_SRAM", "connect_cable -open"]
  1218. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1219. self.assertEqual(result["status"], "fail")
  1220. self.assertIn("forbidden_action_marker:OPEN_COM", result["failures"])
  1221. self.assertIn("forbidden_action_marker:DTR", result["failures"])
  1222. self.assertIn("forbidden_action_marker:PROGRAM_SRAM", result["failures"])
  1223. self.assertIn("forbidden_action_marker:CONNECT_CABLE", result["failures"])
  1224. def test_offline_fidelity_gate_forbidden_marker_scan_avoids_safe_substring_false_positives(self) -> None:
  1225. receipt = self.valid_offline_receipt()
  1226. receipt["toolchain"]["manifest"]["downloads_dir"] = r"C:\Users\Administrator\CodexToolchains\CPLD\downloads"
  1227. receipt["toolchain"]["authority_boundary"] = "Offline probe checks user-space tools and does not enumerate COM ports."
  1228. receipt["td_log_tail"] = "Exported ports\nsafety downloads_to_hardware=false\n"
  1229. receipt["layers"]["td_build"]["authority_boundary"] = "does not generate/download/program a bitstream"
  1230. receipt["coverage_matrix"]["items"][0]["required"] = ["no_download_no_program"]
  1231. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1232. self.assertEqual(result["status"], "pass")
  1233. self.assertNotIn("forbidden_action_marker:DOWNLOAD", result["failures"])
  1234. self.assertNotIn("forbidden_action_marker:RTS", result["failures"])
  1235. def test_offline_fidelity_gate_still_rejects_standalone_download_action(self) -> None:
  1236. receipt = self.valid_offline_receipt()
  1237. receipt["notes"] = "DOWNLOAD bitstream to device"
  1238. result = validate_offline_fidelity_gate_v1_receipt(receipt, strict=True)
  1239. self.assertEqual(result["status"], "fail")
  1240. self.assertIn("forbidden_action_marker:DOWNLOAD", result["failures"])
  1241. def test_host_sensor_text_parser_splits_literal_escaped_cr_without_newlines(self) -> None:
  1242. lines = ["SENS seq=00000001 status=valid valid=24 missing=00 fault=0 raw=1 drop=00000000"]
  1243. lines.extend(
  1244. f"DIMM seq=00000001 id={dimm:02X} raw=1F00 temp=031 missing=0 fault=0 status=valid"
  1245. for dimm in range(24)
  1246. )
  1247. parsed = _parse_host_sensor_report_text("\\r".join(lines) + "\\r")
  1248. self.assertEqual(parsed["sample_count"], 1)
  1249. self.assertEqual(parsed["dimm_rows_per_sample"], [24])
  1250. self.assertEqual(
  1251. sorted(row["id"] for row in parsed["samples"][0]["dimm_rows"]),
  1252. list(range(24)),
  1253. )
  1254. def test_host_sensor_text_parser_accepts_compact_seq_temp_aliases(self) -> None:
  1255. lines = ["SENS s=0003 st=v v=18 m=00"]
  1256. lines.extend(f"DIMM s=0003 id={dimm:02X} tp={0x30 + dimm:02X} m=0 st=v" for dimm in range(24))
  1257. parsed = _parse_host_sensor_report_text("\r\n".join(lines) + "\r\n")
  1258. self.assertEqual(parsed["sample_count"], 1)
  1259. self.assertEqual(parsed["dimm_rows_per_sample"], [24])
  1260. self.assertEqual(parsed["samples"][0]["summary"]["seq"], 3)
  1261. self.assertEqual(parsed["samples"][0]["summary"]["valid"], 24)
  1262. self.assertEqual(parsed["samples"][0]["summary"]["missing"], 0)
  1263. self.assertEqual(parsed["samples"][0]["dimm_rows"][0]["temp"], 0x30)
  1264. def test_host_sensor_text_parser_assigns_compact_dimm_rows_to_latest_sens(self) -> None:
  1265. lines = ["SENS s=0003 st=v v=18 m=00"]
  1266. lines.extend(f"DIMM i={dimm:02X} t={0x30 + dimm:02X} m=0" for dimm in range(24))
  1267. parsed = _parse_host_sensor_report_text("\r\n".join(lines) + "\r\n")
  1268. self.assertEqual(parsed["sample_count"], 1)
  1269. self.assertEqual(parsed["dimm_rows_per_sample"], [24])
  1270. self.assertEqual(parsed["samples"][0]["seq"], 3)
  1271. self.assertEqual(parsed["samples"][0]["dimm_rows"][0]["seq"], 3)
  1272. self.assertEqual(parsed["samples"][0]["dimm_rows"][0]["temp"], 0x30)
  1273. self.assertEqual(
  1274. sorted(row["id"] for row in parsed["samples"][0]["dimm_rows"]),
  1275. list(range(24)),
  1276. )
  1277. def test_host_sensor_sidecar_recovers_compact_hex_id_from_raw_line(self) -> None:
  1278. row = {
  1279. "raw": "DIMM i=10 t=00 m=1 ",
  1280. "fields": {"i": 10, "t": 0, "m": 1, "temp": 0, "missing": 1, "seq": 12},
  1281. "temp": 0,
  1282. "missing": 1,
  1283. "seq": 12,
  1284. }
  1285. fields = _host_sensor_row_fields(row)
  1286. self.assertEqual(fields["id"], 16)
  1287. def test_j6raw_parser_accepts_compact_replay_fields(self) -> None:
  1288. frames = _parse_j6raw_frames("J6RAW s=002A o=00000100 l=04 u=000A d=01020304\r\n")
  1289. self.assertEqual(len(frames), 1)
  1290. self.assertEqual(frames[0]["seq"], 0x2A)
  1291. self.assertEqual(frames[0]["off"], 0x100)
  1292. self.assertEqual(frames[0]["len"], 4)
  1293. self.assertEqual(frames[0]["sum"], 0x000A)
  1294. self.assertEqual(frames[0]["data_hex"], "01020304")
  1295. def test_j6evt_parser_normalizes_compact_event_fields(self) -> None:
  1296. events = _parse_lossless_key_value_lines("J6EVT s=06 cmd=PID_E c=01 p=000 ok=1 e=0 to=0\r\n", "J6EVT")
  1297. self.assertEqual(len(events), 1)
  1298. self.assertEqual(events[0]["seq"], 6)
  1299. self.assertEqual(events[0]["cmd"], "PID_E")
  1300. self.assertEqual(events[0]["ch"], 1)
  1301. self.assertEqual(events[0]["pct"], 0)
  1302. self.assertEqual(events[0]["ok"], 1)
  1303. self.assertEqual(events[0]["err"], 0)
  1304. self.assertEqual(events[0]["timeout"], 0)
  1305. def test_real_bmc_j6_host_sensor_real_fan_ptc_debug921_uses_post_j6ready_raw_epoch(self) -> None:
  1306. with tempfile.TemporaryDirectory() as tmp:
  1307. receipt = self.write_host_sensor_real_fan_ptc_debug921_fixture(
  1308. Path(tmp), hardware_status="blocked_waiting_for_real_fan_ptc_hardware"
  1309. )
  1310. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1311. debug_text = Path(payload["layers"]["debug_frame_capture"]["debug_transcript"]["path"])
  1312. replay = Path(payload["layers"]["debug_frame_capture"]["raw_replay"]["path"])
  1313. debug_stream = Path(payload["layers"]["debug_frame_capture"]["debug_frame_stream"]["path"])
  1314. post_payload = bytes.fromhex("01020304")
  1315. debug_lines = [
  1316. "J6RAW s=00FE o=00100000 l=02 u=00FF d=AA55",
  1317. "J6READY profile=temp-uart-ptc-remap",
  1318. "J6RAW s=0001 o=00000000 l=04 u=000A d=01020304",
  1319. "J6CMD",
  1320. "J6EVT s=01 cmd=02 c=01 p=010 ok=1 e=0 to=0",
  1321. "J6PWM seq=00000008 src=host_manual pid=disabled timeout=1 ptc01=000 ptc24=000 fan01=030 fan04=030",
  1322. "J6MOD seq=00000008 pid=disabled pid_requested=1 pid_active=0 bmc=observe_only host=slave pwm=host_manual timeout_s=5",
  1323. "J6MAP profile=temp-uart-ptc-remap bmc_tx=T4 bmc_rx=R5 host_tx=P7 host_rx=P8 debug_rx=P10 debug_tx=R10 ptc=24 fan=4 fg=8 install=4",
  1324. "J6TEMP seq=00000001 status=valid valid=24 missing=00 fault=0 host_report_drop=00000000",
  1325. "J6STAT ri=00000004 ro=00000004 rd=00 hd=00 be=00 he=00",
  1326. ]
  1327. debug_text.write_text("\r\n".join(debug_lines) + "\r\n", encoding="utf-8")
  1328. replay.write_bytes(post_payload)
  1329. debug_stream.write_text(
  1330. json.dumps(
  1331. {
  1332. "schema": "j6-host-sensor-real-fan-ptc-debug-frame-stream-v1",
  1333. "status": "pass",
  1334. "frames": [{"raw": line} for line in debug_lines],
  1335. "raw_drop": 0,
  1336. "host_report_drop": 0,
  1337. "bmc_berr": 0,
  1338. "host_err": 0,
  1339. }
  1340. ),
  1341. encoding="utf-8",
  1342. )
  1343. payload["layers"]["debug_frame_capture"]["debug_transcript"]["sha256"] = compute_file_hash(debug_text)
  1344. payload["layers"]["debug_frame_capture"]["raw_replay"]["sha256"] = compute_file_hash(replay)
  1345. payload["layers"]["debug_frame_capture"]["debug_frame_stream"]["sha256"] = compute_file_hash(debug_stream)
  1346. payload["layers"]["artifact_hashes"]["debug_transcript_sha256"] = compute_file_hash(debug_text)
  1347. payload["layers"]["artifact_hashes"]["raw_replay_sha256"] = compute_file_hash(replay)
  1348. payload["layers"]["artifact_hashes"]["debug_frame_stream_sha256"] = compute_file_hash(debug_stream)
  1349. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1350. result = validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt(receipt, strict=True)
  1351. self.assertNotIn("raw_frame_seq_gap", result["failures"])
  1352. self.assertNotIn("raw_frame_offset_gap", result["failures"])
  1353. self.assertNotIn("raw_in_replay_len_mismatch", result["failures"])
  1354. self.assertNotIn("raw_out_replay_len_mismatch", result["failures"])
  1355. self.assertNotIn("raw_replay_artifact_mismatch", result["failures"])
  1356. self.assertEqual(result["raw_replay"]["first_seq"], 1)
  1357. self.assertEqual(result["raw_replay"]["first_off"], 0)
  1358. self.assertEqual(result["raw_replay"]["bytes"], len(post_payload))
  1359. def test_real_bmc_j6_host_sensor_real_fan_ptc_debug921_accepts_contiguous_post_stat_raw_tail(self) -> None:
  1360. with tempfile.TemporaryDirectory() as tmp:
  1361. receipt = self.write_host_sensor_real_fan_ptc_debug921_fixture(
  1362. Path(tmp), hardware_status="blocked_waiting_for_real_fan_ptc_hardware"
  1363. )
  1364. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1365. debug_text = Path(payload["layers"]["debug_frame_capture"]["debug_transcript"]["path"])
  1366. replay = Path(payload["layers"]["debug_frame_capture"]["raw_replay"]["path"])
  1367. debug_stream = Path(payload["layers"]["debug_frame_capture"]["debug_frame_stream"]["path"])
  1368. first = bytes.fromhex("01020304")
  1369. tail = bytes.fromhex("AA55BB66")
  1370. debug_lines = [
  1371. "J6READY profile=temp-uart-ptc-remap",
  1372. "J6RAW s=0001 o=00000000 l=04 u=000A d=01020304",
  1373. "J6STAT ri=00000004 ro=00000004 rd=00 hd=00 be=00 he=00",
  1374. "J6RAW s=0002 o=00000004 l=04 u=0220 d=AA55BB66",
  1375. "J6CMD",
  1376. "J6EVT s=01 cmd=02 c=01 p=010 ok=1 e=0 to=0",
  1377. "J6PWM seq=00000008 src=host_manual pid=disabled timeout=1 ptc01=000 ptc24=000 fan01=030 fan04=030",
  1378. "J6MOD seq=00000008 pid=disabled pid_requested=1 pid_active=0 bmc=observe_only host=slave pwm=host_manual timeout_s=5",
  1379. "J6MAP profile=temp-uart-ptc-remap bmc_tx=T4 bmc_rx=R5 host_tx=P7 host_rx=P8 debug_rx=P10 debug_tx=R10 ptc=24 fan=4 fg=8 install=4",
  1380. "J6TEMP seq=00000001 status=valid valid=24 missing=00 fault=0 host_report_drop=00000000",
  1381. "J6TEMP seq=00000002 status=all_ff valid=00 missing=24 fault=0 host_report_drop=00000000",
  1382. "J6TEMP seq=00000003 status=valid valid=24 missing=00 fault=0 host_report_drop=00000000",
  1383. "J6RAW s=0003",
  1384. ]
  1385. debug_text.write_text("\r\n".join(debug_lines), encoding="utf-8")
  1386. replay.write_bytes(first + tail)
  1387. debug_stream.write_text(
  1388. json.dumps(
  1389. {
  1390. "schema": "j6-host-sensor-real-fan-ptc-debug-frame-stream-v1",
  1391. "status": "pass",
  1392. "frames": [{"raw": line} for line in debug_lines[:-1]],
  1393. "tail": {
  1394. "post_j6stat_tail_bytes": len(tail),
  1395. "incomplete_final_line": debug_lines[-1],
  1396. "last_j6stat_covers_replay": False,
  1397. },
  1398. "raw_drop": 0,
  1399. "host_report_drop": 0,
  1400. "bmc_berr": 0,
  1401. "host_err": 0,
  1402. }
  1403. ),
  1404. encoding="utf-8",
  1405. )
  1406. payload["layers"]["debug_frame_capture"]["debug_transcript"]["sha256"] = compute_file_hash(debug_text)
  1407. payload["layers"]["debug_frame_capture"]["raw_replay"]["sha256"] = compute_file_hash(replay)
  1408. payload["layers"]["debug_frame_capture"]["debug_frame_stream"]["sha256"] = compute_file_hash(debug_stream)
  1409. payload["layers"]["artifact_hashes"]["debug_transcript_sha256"] = compute_file_hash(debug_text)
  1410. payload["layers"]["artifact_hashes"]["raw_replay_sha256"] = compute_file_hash(replay)
  1411. payload["layers"]["artifact_hashes"]["debug_frame_stream_sha256"] = compute_file_hash(debug_stream)
  1412. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1413. result = validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt(receipt, strict=True)
  1414. self.assertEqual(result["status"], "blocked_waiting_for_real_fan_ptc_hardware", result["failures"])
  1415. self.assertNotIn("raw_in_replay_len_mismatch", result["failures"])
  1416. self.assertNotIn("raw_out_replay_len_mismatch", result["failures"])
  1417. self.assertNotIn("raw_replay_artifact_mismatch", result["failures"])
  1418. self.assertEqual(result["raw_replay"]["bytes"], len(first) + len(tail))
  1419. self.assertEqual(result["raw_replay"]["post_j6stat_tail_bytes"], len(tail))
  1420. self.assertFalse(result["raw_replay"]["last_j6stat_covers_replay"])
  1421. def test_real_bmc_j6_host_sensor_real_fan_ptc_debug921_rejects_post_stat_raw_tail_with_offset_gap(self) -> None:
  1422. with tempfile.TemporaryDirectory() as tmp:
  1423. receipt = self.write_host_sensor_real_fan_ptc_debug921_fixture(
  1424. Path(tmp), hardware_status="blocked_waiting_for_real_fan_ptc_hardware"
  1425. )
  1426. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1427. debug_text = Path(payload["layers"]["debug_frame_capture"]["debug_transcript"]["path"])
  1428. replay = Path(payload["layers"]["debug_frame_capture"]["raw_replay"]["path"])
  1429. debug_stream = Path(payload["layers"]["debug_frame_capture"]["debug_frame_stream"]["path"])
  1430. replay_bytes = bytes.fromhex("01020304AA55BB66")
  1431. debug_lines = [
  1432. "J6READY profile=temp-uart-ptc-remap",
  1433. "J6RAW s=0001 o=00000000 l=04 u=000A d=01020304",
  1434. "J6STAT ri=00000004 ro=00000004 rd=00 hd=00 be=00 he=00",
  1435. "J6RAW s=0002 o=00000005 l=04 u=0220 d=AA55BB66",
  1436. "J6CMD",
  1437. "J6EVT s=01 cmd=02 c=01 p=010 ok=1 e=0 to=0",
  1438. "J6PWM seq=00000008 src=host_manual pid=disabled timeout=1 ptc01=000 ptc24=000 fan01=030 fan04=030",
  1439. "J6MOD seq=00000008 pid=disabled pid_requested=1 pid_active=0 bmc=observe_only host=slave pwm=host_manual timeout_s=5",
  1440. "J6MAP profile=temp-uart-ptc-remap bmc_tx=T4 bmc_rx=R5 host_tx=P7 host_rx=P8 debug_rx=P10 debug_tx=R10 ptc=24 fan=4 fg=8 install=4",
  1441. "J6TEMP seq=00000001 status=valid valid=24 missing=00 fault=0 host_report_drop=00000000",
  1442. "J6TEMP seq=00000002 status=all_ff valid=00 missing=24 fault=0 host_report_drop=00000000",
  1443. "J6TEMP seq=00000003 status=valid valid=24 missing=00 fault=0 host_report_drop=00000000",
  1444. ]
  1445. debug_text.write_text("\r\n".join(debug_lines) + "\r\n", encoding="utf-8")
  1446. replay.write_bytes(replay_bytes)
  1447. debug_stream.write_text(
  1448. json.dumps(
  1449. {
  1450. "schema": "j6-host-sensor-real-fan-ptc-debug-frame-stream-v1",
  1451. "status": "pass",
  1452. "frames": [{"raw": line} for line in debug_lines],
  1453. "raw_drop": 0,
  1454. "host_report_drop": 0,
  1455. "bmc_berr": 0,
  1456. "host_err": 0,
  1457. }
  1458. ),
  1459. encoding="utf-8",
  1460. )
  1461. payload["layers"]["debug_frame_capture"]["debug_transcript"]["sha256"] = compute_file_hash(debug_text)
  1462. payload["layers"]["debug_frame_capture"]["raw_replay"]["sha256"] = compute_file_hash(replay)
  1463. payload["layers"]["debug_frame_capture"]["debug_frame_stream"]["sha256"] = compute_file_hash(debug_stream)
  1464. payload["layers"]["artifact_hashes"]["debug_transcript_sha256"] = compute_file_hash(debug_text)
  1465. payload["layers"]["artifact_hashes"]["raw_replay_sha256"] = compute_file_hash(replay)
  1466. payload["layers"]["artifact_hashes"]["debug_frame_stream_sha256"] = compute_file_hash(debug_stream)
  1467. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1468. result = validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt(receipt, strict=True)
  1469. self.assertEqual(result["status"], "blocked_debug_frame_loss")
  1470. self.assertIn("raw_frame_offset_gap", result["failures"])
  1471. def test_real_bmc_j6_host_sensor_real_fan_ptc_debug921_blocks_until_real_hardware_is_present(self) -> None:
  1472. with tempfile.TemporaryDirectory() as tmp:
  1473. receipt = self.write_host_sensor_real_fan_ptc_debug921_fixture(Path(tmp))
  1474. result = validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt(receipt, strict=True)
  1475. final_result = validate_final_gate_v2_receipt(receipt, strict=True)
  1476. self.assertEqual(result["status"], "blocked_waiting_for_real_fan_ptc_hardware", result["failures"])
  1477. self.assertEqual(result["real_fan_ptc_hardware_status"]["status"], "not_run")
  1478. self.assertIn("real_fan_ptc_hardware_not_run", result["failures"])
  1479. self.assertEqual(result["pin_map"]["fan_count"], 4)
  1480. self.assertFalse(result["real_fan_ptc_hardware_status"]["qualifies_final_gate"])
  1481. self.assertEqual(result["host_commands"][:4], ["SET PTC 01 10", "SET PTC 24 20", "SET FAN 01 40", "SET FAN 04 30"])
  1482. self.assertEqual(final_result["status"], "fail")
  1483. self.assertIn("fail_schema", final_result["failures"])
  1484. def test_real_bmc_j6_host_sensor_real_fan_ptc_debug921_identifies_startup_ack_shift(self) -> None:
  1485. with tempfile.TemporaryDirectory() as tmp:
  1486. receipt = self.write_host_sensor_real_fan_ptc_debug921_fixture(Path(tmp))
  1487. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1488. transcript = Path(payload["layers"]["host_control_transcript"]["host_command_transcript"]["path"])
  1489. transcript_payload = json.loads(transcript.read_text(encoding="utf-8"))
  1490. commands = transcript_payload["commands"]
  1491. commands[0].update(
  1492. {
  1493. "ack_line": "",
  1494. "response_raw": "",
  1495. "response_token": "",
  1496. "response_class": "timeout",
  1497. "response_seq": None,
  1498. "response_cmd": "",
  1499. "seq_match": False,
  1500. "cmd_match": False,
  1501. "accepted": False,
  1502. }
  1503. )
  1504. for index in range(1, len(commands)):
  1505. previous = commands[index - 1]
  1506. commands[index].update(
  1507. {
  1508. "ack_line": previous["ack_line"] or f"OK seq={index:08X} cmd={previous['expected_ack_cmd']}",
  1509. "response_raw": previous["response_raw"] or f"OK seq={index:08X} cmd={previous['expected_ack_cmd']}",
  1510. "response_token": previous["response_token"] or f"OK seq={index:08X} cmd={previous['expected_ack_cmd']}",
  1511. "response_class": "ok",
  1512. "response_seq": index,
  1513. "response_cmd": previous["expected_ack_cmd"],
  1514. "seq_match": False,
  1515. "cmd_match": commands[index]["expected_ack_cmd"] == previous["expected_ack_cmd"],
  1516. "accepted": False,
  1517. }
  1518. )
  1519. transcript_payload["status"] = "blocked_host_ack"
  1520. transcript_payload["preflight"] = {
  1521. "status": "missing",
  1522. "ready_seen": False,
  1523. "sync_ack_seen": False,
  1524. }
  1525. transcript.write_text(json.dumps(transcript_payload), encoding="utf-8")
  1526. new_hash = compute_file_hash(transcript)
  1527. payload["layers"]["host_control_transcript"]["host_command_transcript"]["sha256"] = new_hash
  1528. payload["layers"]["artifact_hashes"]["host_command_transcript_sha256"] = new_hash
  1529. payload["layers"]["host_control_transcript"]["status"] = "blocked_host_ack"
  1530. payload["status"] = "blocked_host_ack"
  1531. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1532. result = validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt(receipt, strict=True)
  1533. self.assertEqual(result["status"], "blocked_host_ack")
  1534. self.assertIn("blocked_host_startup_ack_shift", result["failures"])
  1535. self.assertIn("host_preflight_missing", result["failures"])
  1536. def test_real_bmc_j6_host_sensor_real_fan_ptc_debug921_identifies_mixed_epoch_ready_after_old_acks(self) -> None:
  1537. with tempfile.TemporaryDirectory() as tmp:
  1538. receipt = self.write_host_sensor_real_fan_ptc_debug921_fixture(Path(tmp))
  1539. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1540. transcript = Path(payload["layers"]["host_control_transcript"]["host_command_transcript"]["path"])
  1541. transcript_payload = json.loads(transcript.read_text(encoding="utf-8"))
  1542. transcript_payload["status"] = "blocked_host_ack"
  1543. transcript_payload["preflight"] = {
  1544. "schema": "j6-host-sensor-real-fan-ptc-startup-preflight-v2",
  1545. "status": "blocked_waiting_for_ready_epoch",
  1546. "ready_seen": False,
  1547. "j6ready_seen": False,
  1548. "epoch_locked": False,
  1549. "sync_ack_seen": True,
  1550. "preflight_ack": "OK seq=0000000D cmd=STATUS",
  1551. "pre_sram_noise": [],
  1552. "pre_sram_noise_count": 0,
  1553. "counts_as_formal_command": False,
  1554. }
  1555. commands = transcript_payload["commands"]
  1556. for index, command in enumerate(commands):
  1557. response_seq = index + 14 if index < 5 else None
  1558. response_class = "ok" if index < 5 else "timeout"
  1559. response_cmd = command["expected_ack_cmd"] if index < 5 else ""
  1560. response_raw = f"OK seq={response_seq:08X} cmd={response_cmd}" if response_seq is not None else ""
  1561. command.update(
  1562. {
  1563. "ack_line": response_raw,
  1564. "response_raw": response_raw,
  1565. "response_token": response_raw,
  1566. "response_class": response_class,
  1567. "response_seq": response_seq,
  1568. "response_cmd": response_cmd,
  1569. "seq_match": False,
  1570. "cmd_match": index < 5,
  1571. "stale": index >= 9,
  1572. "accepted": False,
  1573. }
  1574. )
  1575. transcript.write_text(json.dumps(transcript_payload), encoding="utf-8")
  1576. host_text = Path(payload["layers"]["host_sensor_reports"]["host_transcript_text"]["path"])
  1577. host_text.write_text(
  1578. "OK seq=0000000D cmd=STATUS\r\n"
  1579. "OK seq=0000000E cmd=PTCCH\r\n"
  1580. "OK seq=0000000F cmd=PTCCH\r\n"
  1581. "OK seq=00000010 cmd=FANCH\r\n"
  1582. "OK seq=00000011 cmd=FANCH\r\n"
  1583. "OK seq=00000012 cmd=PID_EN\r\n"
  1584. "READY profile=temp-uart-ptc-remap seq=00000000\r\n"
  1585. "OK seq=00000001 cmd=SENSOR\r\n",
  1586. encoding="utf-8",
  1587. )
  1588. payload["layers"]["host_control_transcript"]["host_command_transcript"]["sha256"] = compute_file_hash(transcript)
  1589. payload["layers"]["host_sensor_reports"]["host_transcript_text"]["sha256"] = compute_file_hash(host_text)
  1590. payload["layers"]["artifact_hashes"]["host_command_transcript_sha256"] = compute_file_hash(transcript)
  1591. payload["layers"]["artifact_hashes"]["host_transcript_text_sha256"] = compute_file_hash(host_text)
  1592. payload["layers"]["host_control_transcript"]["status"] = "blocked_host_ack"
  1593. payload["status"] = "blocked_host_ack"
  1594. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1595. result = validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt(receipt, strict=True)
  1596. self.assertEqual(result["status"], "blocked_host_epoch_mixed")
  1597. self.assertIn("blocked_host_epoch_mixed", result["failures"])
  1598. self.assertIn("host_preflight_epoch_not_locked", result["failures"])
  1599. def test_real_bmc_j6_host_sensor_real_fan_ptc_debug921_rejects_zero_based_and_out_of_range_fan_commands(self) -> None:
  1600. scenarios = [
  1601. ("SET PTC 00 10", "host_command_not_allowed:SET PTC 00 10"),
  1602. ("SET PTC 25 10", "host_command_not_allowed:SET PTC 25 10"),
  1603. ("SET FAN 00 40", "host_command_not_allowed:SET FAN 00 40"),
  1604. ("SET FAN 05 40", "host_command_not_allowed:SET FAN 05 40"),
  1605. ]
  1606. for bad_command, expected_failure in scenarios:
  1607. with self.subTest(command=bad_command):
  1608. with tempfile.TemporaryDirectory() as tmp:
  1609. receipt = self.write_host_sensor_real_fan_ptc_debug921_fixture(Path(tmp))
  1610. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1611. payload["layers"]["host_command_sequence"]["commands"][0] = bad_command
  1612. transcript = Path(payload["layers"]["host_control_transcript"]["host_command_transcript"]["path"])
  1613. transcript_payload = json.loads(transcript.read_text(encoding="utf-8"))
  1614. transcript_payload["commands"][0]["command"] = bad_command
  1615. transcript.write_text(json.dumps(transcript_payload), encoding="utf-8")
  1616. payload["layers"]["host_control_transcript"]["host_command_transcript"]["sha256"] = compute_file_hash(transcript)
  1617. payload["layers"]["artifact_hashes"]["host_command_transcript_sha256"] = compute_file_hash(transcript)
  1618. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1619. result = validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt(receipt, strict=True)
  1620. self.assertEqual(result["status"], "fail_forbidden_action")
  1621. self.assertIn(expected_failure, result["failures"])
  1622. def test_real_bmc_j6_host_sensor_real_fan_ptc_debug921_rejects_wrong_td_top(self) -> None:
  1623. with tempfile.TemporaryDirectory() as tmp:
  1624. receipt = self.write_host_sensor_real_fan_ptc_debug921_fixture(Path(tmp))
  1625. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1626. td_path = Path(payload["layers"]["td_bitstream"]["receipt"]["path"])
  1627. td_payload = json.loads(td_path.read_text(encoding="utf-8"))
  1628. td_payload["top_module"] = "uart_rs232_j6_host_sensor_pwm_debug921_top"
  1629. td_path.write_text(json.dumps(td_payload), encoding="utf-8")
  1630. payload["layers"]["td_bitstream"]["receipt"]["sha256"] = compute_file_hash(td_path)
  1631. payload["layers"]["artifact_hashes"]["td_receipt_sha256"] = compute_file_hash(td_path)
  1632. attestation_path = Path(payload["layers"]["manual_sram_programming"]["attestation"]["path"])
  1633. attestation = json.loads(attestation_path.read_text(encoding="utf-8"))
  1634. attestation["td_receipt_sha256"] = compute_file_hash(td_path)
  1635. attestation_path.write_text(json.dumps(attestation), encoding="utf-8")
  1636. payload["layers"]["manual_sram_programming"]["attestation"]["sha256"] = compute_file_hash(attestation_path)
  1637. payload["layers"]["artifact_hashes"]["manual_sram_attestation_sha256"] = compute_file_hash(attestation_path)
  1638. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1639. result = validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt(receipt, strict=True)
  1640. self.assertEqual(result["status"], "fail")
  1641. self.assertIn("td_bitstream_not_host_sensor_real_fan_ptc_top", result["failures"])
  1642. def test_real_bmc_j6_host_sensor_real_fan_ptc_debug921_rejects_old_six_fan_pin_map(self) -> None:
  1643. with tempfile.TemporaryDirectory() as tmp:
  1644. receipt = self.write_host_sensor_real_fan_ptc_debug921_fixture(Path(tmp))
  1645. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1646. payload["layers"]["pin_map"]["fan_count"] = 6
  1647. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1648. result = validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt(receipt, strict=True)
  1649. self.assertEqual(result["status"], "fail")
  1650. self.assertIn("pin_map_fan_count_not_4", result["failures"])
  1651. def test_real_bmc_j6_host_sensor_real_fan_ptc_debug921_rejects_legacy_host_sensor_pwm_sidecars(self) -> None:
  1652. with tempfile.TemporaryDirectory() as tmp:
  1653. receipt = self.write_host_sensor_real_fan_ptc_debug921_fixture(Path(tmp))
  1654. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1655. report_path = Path(payload["layers"]["host_sensor_reports"]["host_sensor_report"]["path"])
  1656. report_payload = json.loads(report_path.read_text(encoding="utf-8"))
  1657. report_payload["schema"] = "j6-host-sensor-pwm-host-sensor-report-v1"
  1658. report_path.write_text(json.dumps(report_payload), encoding="utf-8")
  1659. stream_path = Path(payload["layers"]["debug_frame_capture"]["debug_frame_stream"]["path"])
  1660. stream_payload = json.loads(stream_path.read_text(encoding="utf-8"))
  1661. stream_payload["schema"] = "j6-host-sensor-pwm-debug-frame-stream-v1"
  1662. stream_path.write_text(json.dumps(stream_payload), encoding="utf-8")
  1663. payload["layers"]["host_sensor_reports"]["host_sensor_report"]["sha256"] = compute_file_hash(report_path)
  1664. payload["layers"]["debug_frame_capture"]["debug_frame_stream"]["sha256"] = compute_file_hash(stream_path)
  1665. payload["layers"]["artifact_hashes"]["host_sensor_report_sha256"] = compute_file_hash(report_path)
  1666. payload["layers"]["artifact_hashes"]["debug_frame_stream_sha256"] = compute_file_hash(stream_path)
  1667. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1668. result = validate_real_bmc_j6_host_sensor_real_fan_ptc_debug921_v1_receipt(receipt, strict=True)
  1669. self.assertEqual(result["status"], "fail")
  1670. self.assertIn("host_sensor_report_wrong_schema", result["failures"])
  1671. self.assertIn("debug_frame_stream_wrong_schema", result["failures"])
  1672. def test_real_bmc_j6_windows_required_layers_remain_unchanged(self) -> None:
  1673. with tempfile.TemporaryDirectory() as tmp:
  1674. receipt = self.write_real_bmc_windows_gate_v2_fixture(Path(tmp))
  1675. result = validate_real_bmc_j6_windows_gate_v2_receipt(receipt, strict=True)
  1676. self.assertEqual(
  1677. result["required_layers"],
  1678. [
  1679. "td_bitstream",
  1680. "manual_sram_programming",
  1681. "windows_serial_capture",
  1682. "real_bmc_protocol",
  1683. "safety_contract",
  1684. "artifact_hashes",
  1685. ],
  1686. )
  1687. def test_real_bmc_j6_windows_gate_v2_accepts_j6sys_strict_evidence(self) -> None:
  1688. with tempfile.TemporaryDirectory() as tmp:
  1689. receipt = self.write_real_bmc_windows_gate_v2_fixture(Path(tmp))
  1690. result = validate_real_bmc_j6_windows_gate_v2_receipt(receipt, strict=True)
  1691. self.assertEqual(result["status"], "pass")
  1692. self.assertEqual(result["last_j6sys"]["wake"], 3)
  1693. self.assertEqual(result["last_j6sys"]["poll"], 3)
  1694. self.assertEqual(result["last_j6sys"]["valid"], 3)
  1695. def test_real_bmc_j6_windows_gate_v2_rejects_non_sram_and_j6sys_errors(self) -> None:
  1696. with tempfile.TemporaryDirectory() as tmp:
  1697. receipt = self.write_real_bmc_windows_gate_v2_fixture(
  1698. Path(tmp),
  1699. parser_error=1,
  1700. stale=1,
  1701. uerr=1,
  1702. derr=1,
  1703. mode="PROGRAM_FLASH",
  1704. )
  1705. result = validate_real_bmc_j6_windows_gate_v2_receipt(receipt, strict=True)
  1706. self.assertEqual(result["status"], "fail_forbidden_action")
  1707. self.assertIn("programming_not_sram", result["failures"])
  1708. self.assertIn("j6sys_parser_error", result["failures"])
  1709. self.assertIn("j6sys_stale", result["failures"])
  1710. self.assertIn("j6sys_uart_error", result["failures"])
  1711. self.assertIn("j6sys_debug_uart_error", result["failures"])
  1712. def test_real_bmc_j6_windows_gate_v2_rejects_short_protocol_and_hash_mismatch(self) -> None:
  1713. with tempfile.TemporaryDirectory() as tmp:
  1714. receipt = self.write_real_bmc_windows_gate_v2_fixture(Path(tmp), poll=2, valid=2)
  1715. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1716. payload["layers"]["artifact_hashes"]["transcript_sha256"] = "0" * 64
  1717. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1718. result = validate_real_bmc_j6_windows_gate_v2_receipt(receipt, strict=True)
  1719. self.assertEqual(result["status"], "fail")
  1720. self.assertIn("j6sys_poll_lt_3", result["failures"])
  1721. self.assertIn("j6sys_valid_lt_3", result["failures"])
  1722. self.assertIn("artifact_hash_mismatch:transcript_sha256", result["failures"])
  1723. def test_real_bmc_j6_windows_gate_v2_rejects_blocked_sidecar_json_artifacts(self) -> None:
  1724. with tempfile.TemporaryDirectory() as tmp:
  1725. receipt = self.write_real_bmc_windows_gate_v2_fixture(Path(tmp))
  1726. payload = json.loads(receipt.read_text(encoding="utf-8"))
  1727. capture_layer = payload["layers"]["windows_serial_capture"]
  1728. artifact_layer = payload["layers"]["artifact_hashes"]
  1729. event_stream = Path(capture_layer["event_stream"]["path"])
  1730. register_dump = Path(capture_layer["register_dump"]["path"])
  1731. event_stream.write_text(
  1732. json.dumps({"schema": "j6sys-event-stream-v1", "status": "blocked_missing_debug_capture", "events": []}),
  1733. encoding="utf-8",
  1734. )
  1735. register_dump.write_text(
  1736. json.dumps({"schema": "j6sys-register-dump-v1", "status": "fail_bad_capture", "debug_registers": {}}),
  1737. encoding="utf-8",
  1738. )
  1739. capture_layer["event_stream"]["sha256"] = compute_file_hash(event_stream)
  1740. capture_layer["register_dump"]["sha256"] = compute_file_hash(register_dump)
  1741. artifact_layer["event_stream_sha256"] = compute_file_hash(event_stream)
  1742. artifact_layer["register_dump_sha256"] = compute_file_hash(register_dump)
  1743. receipt.write_text(json.dumps(payload), encoding="utf-8")
  1744. result = validate_real_bmc_j6_windows_gate_v2_receipt(receipt, strict=True)
  1745. self.assertEqual(result["status"], "fail")
  1746. self.assertIn("event_stream_status_not_pass:blocked_missing_debug_capture", result["failures"])
  1747. self.assertIn("register_dump_status_not_pass:fail_bad_capture", result["failures"])
  1748. self.assertIn("event_stream_status_not_pass:empty_events", result["failures"])
  1749. self.assertIn("register_dump_status_not_pass:empty_registers", result["failures"])
  1750. def test_cpld_bmc_acceptance_v1_requires_final_and_real_bmc_windows_pass(self) -> None:
  1751. with tempfile.TemporaryDirectory() as tmp:
  1752. root = Path(tmp)
  1753. real_bmc = self.write_real_bmc_windows_gate_v2_fixture(root)
  1754. final = root / "final.json"
  1755. final.write_text(
  1756. json.dumps(
  1757. {
  1758. "schema": "final-gate-v2-receipt-v1",
  1759. "status": "pass",
  1760. "layers": {name: {"status": "pass"} for name in [
  1761. "static",
  1762. "model",
  1763. "replay",
  1764. "rtl_sim",
  1765. "formal",
  1766. "detector_selftest",
  1767. "td_build",
  1768. "uart_strict",
  1769. ]},
  1770. "authority_boundary": "control communication only",
  1771. }
  1772. ),
  1773. encoding="utf-8",
  1774. )
  1775. acceptance = root / "acceptance.json"
  1776. acceptance.write_text(
  1777. json.dumps(
  1778. {
  1779. "schema": "cpld-bmc-acceptance-v1-receipt-v1",
  1780. "status": "pass",
  1781. "layers": {
  1782. "final_gate_v2": {"status": "pass", "receipt": {"path": str(final), "sha256": compute_file_hash(final)}},
  1783. "real_bmc_j6_windows_gate_v2": {"status": "pass", "receipt": {"path": str(real_bmc), "sha256": compute_file_hash(real_bmc)}},
  1784. },
  1785. "authority_boundary": "composite acceptance for control communication and real J6 BMC Windows evidence only.",
  1786. }
  1787. ),
  1788. encoding="utf-8",
  1789. )
  1790. result = validate_cpld_bmc_acceptance_v1_receipt(acceptance, strict=True)
  1791. self.assertEqual(result["status"], "pass")
  1792. def test_cpld_bmc_acceptance_v1_propagates_child_gate_failures(self) -> None:
  1793. with tempfile.TemporaryDirectory() as tmp:
  1794. root = Path(tmp)
  1795. real_bmc = self.write_real_bmc_windows_gate_v2_fixture(root, valid=2)
  1796. final = root / "final.json"
  1797. final.write_text(
  1798. json.dumps(
  1799. {
  1800. "schema": "final-gate-v2-receipt-v1",
  1801. "status": "blocked_missing_tool",
  1802. "layers": {},
  1803. "authority_boundary": "control communication only",
  1804. }
  1805. ),
  1806. encoding="utf-8",
  1807. )
  1808. acceptance = root / "acceptance.json"
  1809. acceptance.write_text(
  1810. json.dumps(
  1811. {
  1812. "schema": "cpld-bmc-acceptance-v1-receipt-v1",
  1813. "status": "pass",
  1814. "layers": {
  1815. "final_gate_v2": {"status": "pass", "receipt": {"path": str(final), "sha256": compute_file_hash(final)}},
  1816. "real_bmc_j6_windows_gate_v2": {"status": "pass", "receipt": {"path": str(real_bmc), "sha256": compute_file_hash(real_bmc)}},
  1817. },
  1818. "authority_boundary": "composite acceptance for control communication and real J6 BMC Windows evidence only.",
  1819. }
  1820. ),
  1821. encoding="utf-8",
  1822. )
  1823. result = validate_cpld_bmc_acceptance_v1_receipt(acceptance, strict=True)
  1824. self.assertEqual(result["status"], "fail")
  1825. self.assertIn("final_gate_v2_not_pass:fail", result["failures"])
  1826. self.assertIn("real_bmc_j6_windows_gate_v2_not_pass:fail", result["failures"])
  1827. def test_cpld_bmc_acceptance_v1_dict_input_still_validates_child_receipts(self) -> None:
  1828. with tempfile.TemporaryDirectory() as tmp:
  1829. root = Path(tmp)
  1830. real_bmc = self.write_real_bmc_windows_gate_v2_fixture(root, valid=2)
  1831. final = root / "final.json"
  1832. final.write_text(
  1833. json.dumps(
  1834. {
  1835. "schema": "final-gate-v2-receipt-v1",
  1836. "status": "pass",
  1837. "layers": {name: {"status": "pass"} for name in [
  1838. "static",
  1839. "model",
  1840. "replay",
  1841. "rtl_sim",
  1842. "formal",
  1843. "detector_selftest",
  1844. "td_build",
  1845. "uart_strict",
  1846. ]},
  1847. "authority_boundary": "control communication only",
  1848. }
  1849. ),
  1850. encoding="utf-8",
  1851. )
  1852. payload = {
  1853. "schema": "cpld-bmc-acceptance-v1-receipt-v1",
  1854. "status": "pass",
  1855. "layers": {
  1856. "final_gate_v2": {"status": "pass", "receipt": {"path": str(final), "sha256": compute_file_hash(final)}},
  1857. "real_bmc_j6_windows_gate_v2": {"status": "pass", "receipt": {"path": str(real_bmc), "sha256": compute_file_hash(real_bmc)}},
  1858. },
  1859. "authority_boundary": "composite acceptance for control communication and real J6 BMC Windows evidence only.",
  1860. }
  1861. result = validate_cpld_bmc_acceptance_v1_receipt(payload, strict=True)
  1862. self.assertEqual(result["status"], "fail")
  1863. self.assertIn("real_bmc_j6_windows_gate_v2_not_pass:fail", result["failures"])
  1864. def test_legacy_real_bmc_j6_remote_collect_script_is_not_user_entrypoint(self) -> None:
  1865. self.assertFalse((ROOT / "tools" / "remote" / "collect-real-bmc-j6.ps1").exists())
  1866. script = (ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1").read_text(
  1867. encoding="utf-8"
  1868. )
  1869. self.assertIn("host-command-transcript.json", script)
  1870. self.assertIn("debug-frame-stream.json", script)
  1871. self.assertIn("bmc-raw-replay.bin", script)
  1872. self.assertIn("real-bmc-j6-host-sensor-real-fan-ptc-debug921-v1", script)
  1873. if __name__ == "__main__":
  1874. unittest.main()