Просмотр исходного кода

Fix temp collector ack array handling

Codex 5 месяцев назад
Родитель
Сommit
3edc0b9020

+ 2 - 2
docs/j6-host-sensor-real-fan-ptc-debug921-runbook.zh-CN.md

@@ -1,10 +1,10 @@
 # J6 Host Sensor Real FAN/PTC Debug921
 
-本说明对应 `j6-host-sensor-real-fan-ptc-temp-uart-ptc-remap-debug921-v2-epochfix` 远端包。包内路径、文件名、脚本名和命令均为英文;说明正文为简体中文。
+本说明对应 `j6-host-sensor-real-fan-ptc-temp-uart-ptc-remap-debug921-v2-epochfix-psarrayfix` 远端包。包内路径、文件名、脚本名和命令均为英文;说明正文为简体中文。
 
 ## 当前版本
 
-- `temp-uart-ptc-remap` 是当前唯一交付版本:三路调试串口保持现场现有接法,6 路与串口冲突的 PTC 输出改到复核空闲 GPIO。v2-epochfix 增加 HostPort READY、DebugPort J6READY 和 `startup-preflight.json`,用于隔离脚本启动前板上旧程序与手动刷 SRAM 后的新程序,正式 12 条命令仍必须 12/12 ACK 严格匹配。
+- `temp-uart-ptc-remap` 是当前唯一交付版本:三路调试串口保持现场现有接法,6 路与串口冲突的 PTC 输出改到复核空闲 GPIO。v2-epochfix-psarrayfix 增加 HostPort READY、DebugPort J6READY 和 `startup-preflight.json`,用于隔离脚本启动前板上旧程序与手动刷 SRAM 后的新程序,正式 12 条命令仍必须 12/12 ACK 严格匹配,并修复 PowerShell 单元素数组包裹导致的 pre-SRAM ACK 记录失败。
 - 历史真实引脚直连方案若未做 PTC/UART 冲突处理,必须 fail closed 并报告 `blocked_pin_conflict_with_locked_debug_uart`;本 v2 包不交付该方案。
 
 本版本只做 SRAM 诊断,不刷 flash,不安装驱动,不枚举未知 COM,不写 DebugPort,不声明真实热性能、真实 PTC/FAN 负载合格或 PID 闭环通过。

+ 54 - 0
tests/unit/test_remote_scripts.py

@@ -318,6 +318,60 @@ if (-not $ack.seq_match -or -not $ack.cmd_match) { throw "formal ack mismatch" }
 
             self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
 
+    def test_collect_j6_host_sensor_real_fan_ptc_marks_clear_queue_results_as_pre_sram_noise(self) -> None:
+        powershell = shutil.which("powershell")
+        if powershell is None:
+            self.skipTest("PowerShell is not available")
+
+        script_path = ROOT / "tools" / "remote" / "collect-j6-host-sensor-real-fan-ptc.ps1"
+        script = script_path.read_text(encoding="utf-8-sig")
+        helper_start = script.index("function Clear-HostAckQueue")
+        helper_end = script.index("function Update-PreflightEpochReady")
+        helpers = script[helper_start:helper_end]
+
+        with tempfile.TemporaryDirectory() as tmp:
+            probe_path = Path(tmp) / "probe.ps1"
+            probe_path.write_text(
+                helpers
+                + """
+$preflight = New-StartupPreflightRecord
+$ackQueue = New-Object System.Collections.Generic.Queue[object]
+$ackQueue.Enqueue([ordered]@{
+    ack_line = "OK seq=0000000D cmd=STATUS"
+    raw = "OK seq=0000000D cmd=STATUS"
+    token = "OK seq=0000000D cmd=STATUS"
+    class = "ok"
+    seq = 13
+    cmd = "STATUS"
+    ack_offset = 25
+    response_offset = 25
+    delimiter = "CR"
+    timestamp = (Get-Date).ToUniversalTime().ToString("o")
+}) | Out-Null
+$moreStale = @(Clear-HostAckQueue $ackQueue)
+foreach ($ack in $moreStale) {
+    Set-AckMetadata $ack "pre_sram_noise" $true
+    Add-PreSramNoise $preflight "ack" $ack
+}
+if ($moreStale.Count -ne 1) { throw "unexpected stale count $($moreStale.Count)" }
+if ($moreStale[0] -is [array]) { throw "stale ack was nested array" }
+if (-not $moreStale[0].pre_sram_noise) { throw "pre_sram_noise was not set" }
+if ($preflight.pre_sram_noise_count -ne 1) { throw "preflight noise count was $($preflight.pre_sram_noise_count)" }
+if ($preflight.pre_sram_noise[0].ack_line -ne "OK seq=0000000D cmd=STATUS") { throw "wrong pre_sram noise ack" }
+""",
+                encoding="utf-8",
+            )
+
+            result = subprocess.run(
+                [powershell, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(probe_path)],
+                cwd=ROOT,
+                text=True,
+                capture_output=True,
+                check=False,
+            )
+
+            self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
+
     def test_collect_j6_host_sensor_real_fan_ptc_handles_timeout_discarded_ack_arrays(self) -> None:
         powershell = shutil.which("powershell")
         if powershell is None:

+ 4 - 4
tools/package_j6_host_sensor_real_fan_ptc_debug921.ps1

@@ -1,5 +1,5 @@
 param(
-    [string]$OutputDir = ".\j6-host-sensor-real-fan-ptc-temp-uart-ptc-remap-debug921-v2-epochfix-package",
+    [string]$OutputDir = ".\j6-host-sensor-real-fan-ptc-temp-uart-ptc-remap-debug921-v2-epochfix-psarrayfix-package",
     [string]$PackageName = "",
     [Parameter(Mandatory = $true)]
     [ValidateSet("temp-uart-ptc-remap")]
@@ -14,7 +14,7 @@
 $ErrorActionPreference = "Stop"
 
 # Package profile:
-# temp-uart-ptc-remap -> j6-host-sensor-real-fan-ptc-temp-uart-ptc-remap-debug921-v2-epochfix
+# temp-uart-ptc-remap -> j6-host-sensor-real-fan-ptc-temp-uart-ptc-remap-debug921-v2-epochfix-psarrayfix
 # The copied collector is tools\remote\collect-j6-host-sensor-real-fan-ptc.ps1.
 # Start the collector first, then run manual PROGRAM_SRAM so READY/J6READY is captured.
 
@@ -80,7 +80,7 @@ function New-StartHereText([string]$SelectedProfile, [string]$BitName, [string]$
     if ($SelectedProfile -ne "temp-uart-ptc-remap") {
         throw "Only temp-uart-ptc-remap is supported by this v2 packager"
     }
-    $title = "J6 Host Sensor Real FAN/PTC temp-uart-ptc-remap Debug921 v2 epochfix"
+    $title = "J6 Host Sensor Real FAN/PTC temp-uart-ptc-remap Debug921 v2 epochfix psarrayfix"
     $wiring = @"
 - BMC UART:CPLD TX=T4,RX=R5。
 - DebugPort:COM7,P10/R10,921600 8N1。
@@ -142,7 +142,7 @@ $out = (Resolve-Path -LiteralPath (New-Item -ItemType Directory -Force -Path $Ou
 
 if ([string]::IsNullOrWhiteSpace($PackageName)) {
     $stamp = Get-Date -Format "yyyyMMdd-HHmmss"
-    $PackageName = "j6-host-sensor-real-fan-ptc-temp-uart-ptc-remap-debug921-v2-epochfix-$stamp"
+    $PackageName = "j6-host-sensor-real-fan-ptc-temp-uart-ptc-remap-debug921-v2-epochfix-psarrayfix-$stamp"
 }
 
 $stage = Join-Path $out $PackageName

+ 23 - 3
tools/remote/collect-j6-host-sensor-real-fan-ptc.ps1

@@ -510,7 +510,27 @@ function Clear-HostAckQueue([System.Collections.Generic.Queue[object]]$AckQueue)
     while ($AckQueue.Count -gt 0) {
         $discarded.Add($AckQueue.Dequeue()) | Out-Null
     }
-    return ,$discarded.ToArray()
+    return $discarded.ToArray()
+}
+
+function Unwrap-SingleItemArray($Value, [string]$Context) {
+    while ($Value -is [array] -and $Value.Count -eq 1) {
+        $Value = $Value[0]
+    }
+    if ($Value -is [array]) {
+        throw "$Context expected one ack object, got nested array count=$($Value.Count)"
+    }
+    return $Value
+}
+
+function Set-AckMetadata($Ack, [string]$Key, $Value) {
+    $normalized = Unwrap-SingleItemArray $Ack "Set-AckMetadata"
+    if ($null -eq $normalized -or -not ($normalized -is [System.Collections.IDictionary])) {
+        $typeName = if ($null -eq $normalized) { "<null>" } else { $normalized.GetType().FullName }
+        throw "Set-AckMetadata expected IDictionary ack, got $typeName"
+    }
+    $normalized[$Key] = $Value
+    return $normalized
 }
 
 function New-StartupPreflightRecord {
@@ -644,7 +664,7 @@ function Invoke-StartupPreflight($HostSerial, $DebugSerial, [System.Collections.
 
         while ($AckQueue.Count -gt 0) {
             $ack = $AckQueue.Dequeue()
-            $ack["pre_sram_noise"] = $true
+            $ack = Set-AckMetadata $ack "pre_sram_noise" $true
             Add-PreSramNoise $preflight "ack" $ack
             $preflight.stale_acks = @($preflight.stale_acks) + $ack
         }
@@ -672,7 +692,7 @@ function Invoke-StartupPreflight($HostSerial, $DebugSerial, [System.Collections.
 
     $moreStale = @(Clear-HostAckQueue $AckQueue)
     foreach ($ack in $moreStale) {
-        $ack["pre_sram_noise"] = $true
+        $ack = Set-AckMetadata $ack "pre_sram_noise" $true
         Add-PreSramNoise $preflight "ack" $ack
     }
     if ($moreStale.Count -gt 0) {