package_agent_handoff.ps1 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <#
  2. .SYNOPSIS
  3. Creates a sanitized, evidence-aware CPLD workspace archive for another agent.
  4. Historical compact/replay entry; current v2.1 releases use tools/package_unified_analysis.py.
  5. .EXAMPLE
  6. powershell.exe -NoProfile -ExecutionPolicy Bypass -File tools\package_agent_handoff.ps1 -AuditOnly
  7. .EXAMPLE
  8. powershell.exe -NoProfile -ExecutionPolicy Bypass -File tools\package_agent_handoff.ps1 -OutputDir C:\handoff
  9. #>
  10. param(
  11. [string]$SourceRoot = "",
  12. [string]$OutputDir = "",
  13. [string]$PackageName = "",
  14. [switch]$ForceOverwrite,
  15. [switch]$AuditOnly
  16. )
  17. $ErrorActionPreference = "Stop"
  18. if (-not ("AgentHandoff.NativePath" -as [type])) {
  19. Add-Type -TypeDefinition @"
  20. using System;
  21. using System.ComponentModel;
  22. using System.IO;
  23. using System.Runtime.InteropServices;
  24. using System.Text;
  25. using Microsoft.Win32.SafeHandles;
  26. namespace AgentHandoff
  27. {
  28. public static class NativePath
  29. {
  30. private const uint FileShareRead = 0x00000001;
  31. private const uint FileShareWrite = 0x00000002;
  32. private const uint FileShareDelete = 0x00000004;
  33. private const uint OpenExisting = 3;
  34. private const uint FileFlagBackupSemantics = 0x02000000;
  35. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  36. private static extern SafeFileHandle CreateFile(
  37. string fileName,
  38. uint desiredAccess,
  39. uint shareMode,
  40. IntPtr securityAttributes,
  41. uint creationDisposition,
  42. uint flagsAndAttributes,
  43. IntPtr templateFile);
  44. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  45. private static extern uint GetFinalPathNameByHandle(
  46. SafeFileHandle file,
  47. StringBuilder filePath,
  48. uint filePathLength,
  49. uint flags);
  50. public static string Resolve(string path)
  51. {
  52. string fullPath = Path.GetFullPath(path);
  53. using (SafeFileHandle handle = CreateFile(
  54. fullPath,
  55. 0,
  56. FileShareRead | FileShareWrite | FileShareDelete,
  57. IntPtr.Zero,
  58. OpenExisting,
  59. FileFlagBackupSemantics,
  60. IntPtr.Zero))
  61. {
  62. if (handle.IsInvalid)
  63. {
  64. throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to open path for final-path resolution");
  65. }
  66. StringBuilder buffer = new StringBuilder(512);
  67. uint length = GetFinalPathNameByHandle(handle, buffer, (uint)buffer.Capacity, 0);
  68. if (length == 0)
  69. {
  70. throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to resolve final path");
  71. }
  72. if (length >= buffer.Capacity)
  73. {
  74. buffer.Capacity = checked((int)length + 1);
  75. length = GetFinalPathNameByHandle(handle, buffer, (uint)buffer.Capacity, 0);
  76. if (length == 0 || length >= buffer.Capacity)
  77. {
  78. throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to resolve final path");
  79. }
  80. }
  81. string resolved = buffer.ToString();
  82. if (resolved.StartsWith(@"\\?\UNC\", StringComparison.OrdinalIgnoreCase))
  83. {
  84. return @"\\" + resolved.Substring(8);
  85. }
  86. if (resolved.StartsWith(@"\\?\", StringComparison.OrdinalIgnoreCase))
  87. {
  88. return resolved.Substring(4);
  89. }
  90. return resolved;
  91. }
  92. }
  93. }
  94. }
  95. "@ -ErrorAction Stop
  96. }
  97. function Resolve-FinalPath {
  98. param([string]$Path)
  99. return [AgentHandoff.NativePath]::Resolve($Path)
  100. }
  101. $repo = Resolve-FinalPath (
  102. (Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path))
  103. )
  104. $core = Join-Path $repo "tools\package_agent_handoff.py"
  105. $effectiveSourceRoot = if ($SourceRoot) {
  106. Resolve-FinalPath $SourceRoot
  107. }
  108. else {
  109. $repo
  110. }
  111. $pythonIsolationFlags = @("-I", "-S", "-B")
  112. function Test-PathWithinRoot {
  113. param(
  114. [string]$Path,
  115. [string]$Root
  116. )
  117. $fullPath = [System.IO.Path]::GetFullPath($Path)
  118. $fullRoot = [System.IO.Path]::GetFullPath($Root)
  119. if ([string]::Equals($fullPath, $fullRoot, [System.StringComparison]::OrdinalIgnoreCase)) {
  120. return $true
  121. }
  122. if (-not $fullRoot.EndsWith([System.IO.Path]::DirectorySeparatorChar)) {
  123. $fullRoot += [System.IO.Path]::DirectorySeparatorChar
  124. }
  125. return $fullPath.StartsWith($fullRoot, [System.StringComparison]::OrdinalIgnoreCase)
  126. }
  127. function Test-ExternalPythonCandidate {
  128. param([string]$Executable)
  129. if (-not (Test-Path -LiteralPath $Executable -PathType Leaf)) {
  130. return $false
  131. }
  132. try {
  133. $candidatePath = Resolve-FinalPath $Executable
  134. }
  135. catch {
  136. return $false
  137. }
  138. return -not (
  139. (Test-PathWithinRoot -Path $candidatePath -Root $repo) -or
  140. (Test-PathWithinRoot -Path $candidatePath -Root $effectiveSourceRoot)
  141. )
  142. }
  143. function Test-Python311 {
  144. param(
  145. [string]$Executable,
  146. [string[]]$Prefix
  147. )
  148. try {
  149. & $Executable @Prefix @pythonIsolationFlags -c "import sys; raise SystemExit(0 if sys.version_info >= (3, 11) else 1)" *> $null
  150. return $LASTEXITCODE -eq 0
  151. }
  152. catch {
  153. return $false
  154. }
  155. }
  156. function Resolve-Python311 {
  157. $candidates = @()
  158. $python = Get-Command python.exe -ErrorAction SilentlyContinue
  159. if (-not $python) { $python = Get-Command python -ErrorAction SilentlyContinue }
  160. if ($python) { $candidates += ,([ordered]@{ exe = $python.Source; prefix = @() }) }
  161. $py = Get-Command py.exe -ErrorAction SilentlyContinue
  162. if ($py) { $candidates += ,([ordered]@{ exe = $py.Source; prefix = @("-3") }) }
  163. foreach ($candidate in $candidates) {
  164. if (-not (Test-ExternalPythonCandidate -Executable $candidate.exe)) {
  165. continue
  166. }
  167. if (Test-Python311 -Executable $candidate.exe -Prefix $candidate.prefix) {
  168. return $candidate
  169. }
  170. }
  171. throw "Python 3.11 or newer is required to create the agent handoff package"
  172. }
  173. if (-not (Test-Path -LiteralPath $core -PathType Leaf)) {
  174. throw "Agent handoff packager core is missing: $core"
  175. }
  176. $python311 = Resolve-Python311
  177. $arguments = @($python311.prefix) + $pythonIsolationFlags + @($core)
  178. if ($SourceRoot) { $arguments += @("--source-root", $SourceRoot) }
  179. if ($OutputDir) { $arguments += @("--output-dir", $OutputDir) }
  180. if ($PackageName) { $arguments += @("--package-name", $PackageName) }
  181. if ($ForceOverwrite) { $arguments += "--force-overwrite" }
  182. if ($AuditOnly) { $arguments += "--audit-only" }
  183. & $python311.exe @arguments
  184. exit $LASTEXITCODE