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

Fix temp collector PowerShell ack handling

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

+ 124 - 3
tests/unit/test_remote_scripts.py

@@ -147,6 +147,120 @@ class RemoteScriptTests(unittest.TestCase):
         ]:
         ]:
             self.assertIn(marker, output)
             self.assertIn(marker, output)
 
 
+    def test_collect_j6_host_sensor_real_fan_ptc_handles_discarded_ack_arrays(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 Read-HostPortAvailableLines")
+        helper_end = script.index("function Invoke-StartupPreflight")
+        helpers = script[helper_start:helper_end]
+
+        with tempfile.TemporaryDirectory() as tmp:
+            probe_path = Path(tmp) / "probe.ps1"
+            probe_path.write_text(
+                helpers
+                + """
+$hostRaw = New-Object System.Collections.Generic.List[byte]
+$hostWindow = New-Object System.Text.StringBuilder
+$hostReports = New-Object System.Collections.Generic.List[object]
+$queue = New-Object System.Collections.Generic.Queue[object]
+$queue.Enqueue([ordered]@{
+    ack_line = "OK seq=00000001 cmd=STATUS"
+    raw = "OK seq=00000001 cmd=STATUS"
+    token = "OK seq=00000001 cmd=STATUS"
+    class = "ok"
+    seq = 1
+    cmd = "STATUS"
+    ack_offset = 1
+    response_offset = 1
+    delimiter = "CR"
+    timestamp = (Get-Date).ToUniversalTime().ToString("o")
+}) | Out-Null
+$queue.Enqueue([ordered]@{
+    ack_line = "OK seq=00000001 cmd=PTCCH"
+    raw = "OK seq=00000001 cmd=PTCCH"
+    token = "OK seq=00000001 cmd=PTCCH"
+    class = "ok"
+    seq = 1
+    cmd = "PTCCH"
+    ack_offset = 10
+    response_offset = 10
+    delimiter = "CR"
+    timestamp = (Get-Date).ToUniversalTime().ToString("o")
+}) | Out-Null
+$serial = [pscustomobject]@{ BytesToRead = 0 }
+$ack = Read-HostAckLine $serial $hostRaw $hostWindow $hostReports $queue 5 200 1 "PTCCH"
+if ($ack.ack_line -ne "OK seq=00000001 cmd=PTCCH") { throw "candidate ack not returned: $($ack.ack_line)" }
+if ($ack.discarded_acks.Count -ne 1) { throw "discarded ack count was $($ack.discarded_acks.Count)" }
+if (-not $ack.discarded_acks[0].stale) { throw "discarded ack was not marked stale" }
+""",
+                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:
+            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 Read-HostPortAvailableLines")
+        helper_end = script.index("function Invoke-StartupPreflight")
+        helpers = script[helper_start:helper_end]
+
+        with tempfile.TemporaryDirectory() as tmp:
+            probe_path = Path(tmp) / "probe.ps1"
+            probe_path.write_text(
+                helpers
+                + """
+$hostRaw = New-Object System.Collections.Generic.List[byte]
+$hostWindow = New-Object System.Text.StringBuilder
+$hostReports = New-Object System.Collections.Generic.List[object]
+$queue = New-Object System.Collections.Generic.Queue[object]
+$queue.Enqueue([ordered]@{
+    ack_line = "OK seq=00000001 cmd=STATUS"
+    raw = "OK seq=00000001 cmd=STATUS"
+    token = "OK seq=00000001 cmd=STATUS"
+    class = "ok"
+    seq = 1
+    cmd = "STATUS"
+    ack_offset = 1
+    response_offset = 1
+    delimiter = "CR"
+    timestamp = (Get-Date).ToUniversalTime().ToString("o")
+}) | Out-Null
+$serial = [pscustomobject]@{ BytesToRead = 0 }
+$ack = Read-HostAckLine $serial $hostRaw $hostWindow $hostReports $queue 5 100 1 "PTCCH"
+if ($ack.class -ne "timeout") { throw "ack class was $($ack.class)" }
+if ($ack.discarded_acks.Count -ne 1) { throw "discarded ack count was $($ack.discarded_acks.Count)" }
+if (-not $ack.discarded_acks[0].stale) { throw "discarded ack was not marked stale" }
+""",
+                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_preserves_empty_raw_replay_byte_array(self) -> None:
     def test_collect_j6_host_sensor_real_fan_ptc_preserves_empty_raw_replay_byte_array(self) -> None:
         powershell = shutil.which("powershell")
         powershell = shutil.which("powershell")
         if powershell is None:
         if powershell is None:
@@ -330,6 +444,7 @@ if ($events[2].fields.cmd -ne "FANCH") { throw "ack cmd was $($events[2].fields.
             "excluded_self_referential_file",
             "excluded_self_referential_file",
         ]:
         ]:
             self.assertIn(marker, script)
             self.assertIn(marker, script)
+        self.assertNotIn("Set-ExecutionPolicy", script)
         for marker in [
         for marker in [
             "formal-uart",
             "formal-uart",
             "j6-host-sensor-real-fan-ptc-formal-uart-debug921-v1",
             "j6-host-sensor-real-fan-ptc-formal-uart-debug921-v1",
@@ -418,10 +533,16 @@ if ($events[2].fields.cmd -ne "FANCH") { throw "ack cmd was $($events[2].fields.
                     self.assertIn(f"-Profile {profile}", start_here)
                     self.assertIn(f"-Profile {profile}", start_here)
                     self.assertIn(f".\\bitstreams\\{bitstream.name}", start_here)
                     self.assertIn(f".\\bitstreams\\{bitstream.name}", start_here)
                     self.assertIn(f".\\td-receipts\\{td_receipt.name}", start_here)
                     self.assertIn(f".\\td-receipts\\{td_receipt.name}", start_here)
-                    self.assertIn(
-                        ".\\collector\\collect-j6-host-sensor-real-fan-ptc.ps1 -HostPort COM8 -DebugPort COM7",
-                        start_here,
+                    self.assertIn("powershell.exe -NoProfile -ExecutionPolicy Bypass -File", start_here)
+                    self.assertIn("-HostPort COM8 -DebugPort COM7", start_here)
+                    self.assertNotIn("Set-ExecutionPolicy", start_here)
+                    collect_template = (stage / "commands" / "collect-template.ps1").read_text(
+                        encoding="utf-8-sig"
                     )
                     )
+                    self.assertNotIn("Set-ExecutionPolicy", collect_template)
+                    self.assertIn("-NoProfile", collect_template)
+                    self.assertIn("-ExecutionPolicy Bypass", collect_template)
+                    self.assertIn("-File .\\collector\\collect-j6-host-sensor-real-fan-ptc.ps1", collect_template)
                     self.assertNotIn("blocked_pin_conflict_with_locked_debug_uart", start_here)
                     self.assertNotIn("blocked_pin_conflict_with_locked_debug_uart", start_here)
 
 
                     manifest = json.loads((stage / "manifest.json").read_text(encoding="utf-8-sig"))
                     manifest = json.loads((stage / "manifest.json").read_text(encoding="utf-8-sig"))

+ 4 - 6
tools/package_j6_host_sensor_real_fan_ptc_debug921.ps1

@@ -104,17 +104,16 @@ bitstreams/$BitName
 
 
 ## 2. 刷完 SRAM 后采集
 ## 2. 刷完 SRAM 后采集
 
 
-确认 SRAM 下载完成后,在本包根目录打开 PowerShell,复制执行:
+确认 SRAM 下载完成后,在本包根目录打开 PowerShell,复制执行下面这一条命令:
 
 
 ~~~powershell
 ~~~powershell
-Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
-.\collector\collect-j6-host-sensor-real-fan-ptc.ps1 -HostPort COM8 -DebugPort COM7 -DurationSeconds 180 -OutputDir .\evidence-com8-host-com7-debug -Profile $SelectedProfile -Bitstream .\bitstreams\$BitName -TdReceipt .\td-receipts\$TdName -AssumeManualSramProgrammed `$true
+powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\collector\collect-j6-host-sensor-real-fan-ptc.ps1 -HostPort COM8 -DebugPort COM7 -DurationSeconds 180 -OutputDir .\evidence-com8-host-com7-debug -Profile $SelectedProfile -Bitstream .\bitstreams\$BitName -TdReceipt .\td-receipts\$TdName -AssumeManualSramProgrammed `$true
 ~~~
 ~~~
 
 
 也可以执行包内快捷脚本:
 也可以执行包内快捷脚本:
 
 
 ~~~powershell
 ~~~powershell
-.\commands\collect-template.ps1
+powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\commands\collect-template.ps1
 ~~~
 ~~~
 
 
 ## 接线
 ## 接线
@@ -167,8 +166,7 @@ Copy-IntoPackage $pinInventoryPath (Join-Path $stage "config") | Out-Null
 $bitName = Split-Path -Leaf $bitstreamCopy
 $bitName = Split-Path -Leaf $bitstreamCopy
 $tdName = Split-Path -Leaf $tdReceiptCopy
 $tdName = Split-Path -Leaf $tdReceiptCopy
 $template = @'
 $template = @'
-Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
-.\collector\collect-j6-host-sensor-real-fan-ptc.ps1 -HostPort COM8 -DebugPort COM7 -DurationSeconds 180 -OutputDir .\evidence-com8-host-com7-debug -Profile __PROFILE__ -Bitstream .\bitstreams\__BITSTREAM__ -TdReceipt .\td-receipts\__TD_RECEIPT__ -AssumeManualSramProgrammed $true
+& "$PSHOME\powershell.exe" -NoProfile -ExecutionPolicy Bypass -File .\collector\collect-j6-host-sensor-real-fan-ptc.ps1 -HostPort COM8 -DebugPort COM7 -DurationSeconds 180 -OutputDir .\evidence-com8-host-com7-debug -Profile __PROFILE__ -Bitstream .\bitstreams\__BITSTREAM__ -TdReceipt .\td-receipts\__TD_RECEIPT__ -AssumeManualSramProgrammed $true
 '@
 '@
 $template = $template.Replace("__PROFILE__", $Profile).Replace("__BITSTREAM__", $bitName).Replace("__TD_RECEIPT__", $tdName)
 $template = $template.Replace("__PROFILE__", $Profile).Replace("__BITSTREAM__", $bitName).Replace("__TD_RECEIPT__", $tdName)
 Set-Content -LiteralPath (Join-Path $stage "commands\collect-template.ps1") -Encoding UTF8 -Value $template
 Set-Content -LiteralPath (Join-Path $stage "commands\collect-template.ps1") -Encoding UTF8 -Value $template

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

@@ -514,12 +514,12 @@ function Read-HostAckLine($Serial, [System.Collections.Generic.List[byte]]$HostR
                 $stale = $true
                 $stale = $true
                 continue
                 continue
             }
             }
-            $candidate["discarded_acks"] = @($discarded)
+            $candidate["discarded_acks"] = $discarded.ToArray()
             return $candidate
             return $candidate
         }
         }
         Start-Sleep -Milliseconds 20
         Start-Sleep -Milliseconds 20
     }
     }
-    return New-TimeoutAck $HostRaw.Count $ExpectedSeq $ExpectedCmd $late $stale @($discarded)
+    return New-TimeoutAck $HostRaw.Count $ExpectedSeq $ExpectedCmd $late $stale $discarded.ToArray()
 }
 }
 
 
 function Invoke-StartupPreflight($HostSerial, $DebugSerial, [System.Collections.Generic.List[byte]]$HostRaw, [System.Text.StringBuilder]$HostAsciiWindow, [System.Collections.Generic.List[object]]$HostReports, [System.Collections.Generic.Queue[object]]$AckQueue, [System.Collections.Generic.List[byte]]$DebugRaw, [System.Text.StringBuilder]$DebugAsciiWindow, [System.Collections.Generic.List[object]]$DebugEvents) {
 function Invoke-StartupPreflight($HostSerial, $DebugSerial, [System.Collections.Generic.List[byte]]$HostRaw, [System.Text.StringBuilder]$HostAsciiWindow, [System.Collections.Generic.List[object]]$HostReports, [System.Collections.Generic.Queue[object]]$AckQueue, [System.Collections.Generic.List[byte]]$DebugRaw, [System.Text.StringBuilder]$DebugAsciiWindow, [System.Collections.Generic.List[object]]$DebugEvents) {