<# .SYNOPSIS Creates a sanitized, evidence-aware CPLD workspace archive for another agent. Historical compact/replay entry; current v2.1 releases use tools/package_unified_analysis.py. .EXAMPLE powershell.exe -NoProfile -ExecutionPolicy Bypass -File tools\package_agent_handoff.ps1 -AuditOnly .EXAMPLE powershell.exe -NoProfile -ExecutionPolicy Bypass -File tools\package_agent_handoff.ps1 -OutputDir C:\handoff #> param( [string]$SourceRoot = "", [string]$OutputDir = "", [string]$PackageName = "", [switch]$ForceOverwrite, [switch]$AuditOnly ) $ErrorActionPreference = "Stop" if (-not ("AgentHandoff.NativePath" -as [type])) { Add-Type -TypeDefinition @" using System; using System.ComponentModel; using System.IO; using System.Runtime.InteropServices; using System.Text; using Microsoft.Win32.SafeHandles; namespace AgentHandoff { public static class NativePath { private const uint FileShareRead = 0x00000001; private const uint FileShareWrite = 0x00000002; private const uint FileShareDelete = 0x00000004; private const uint OpenExisting = 3; private const uint FileFlagBackupSemantics = 0x02000000; [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern SafeFileHandle CreateFile( string fileName, uint desiredAccess, uint shareMode, IntPtr securityAttributes, uint creationDisposition, uint flagsAndAttributes, IntPtr templateFile); [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern uint GetFinalPathNameByHandle( SafeFileHandle file, StringBuilder filePath, uint filePathLength, uint flags); public static string Resolve(string path) { string fullPath = Path.GetFullPath(path); using (SafeFileHandle handle = CreateFile( fullPath, 0, FileShareRead | FileShareWrite | FileShareDelete, IntPtr.Zero, OpenExisting, FileFlagBackupSemantics, IntPtr.Zero)) { if (handle.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to open path for final-path resolution"); } StringBuilder buffer = new StringBuilder(512); uint length = GetFinalPathNameByHandle(handle, buffer, (uint)buffer.Capacity, 0); if (length == 0) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to resolve final path"); } if (length >= buffer.Capacity) { buffer.Capacity = checked((int)length + 1); length = GetFinalPathNameByHandle(handle, buffer, (uint)buffer.Capacity, 0); if (length == 0 || length >= buffer.Capacity) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to resolve final path"); } } string resolved = buffer.ToString(); if (resolved.StartsWith(@"\\?\UNC\", StringComparison.OrdinalIgnoreCase)) { return @"\\" + resolved.Substring(8); } if (resolved.StartsWith(@"\\?\", StringComparison.OrdinalIgnoreCase)) { return resolved.Substring(4); } return resolved; } } } } "@ -ErrorAction Stop } function Resolve-FinalPath { param([string]$Path) return [AgentHandoff.NativePath]::Resolve($Path) } $repo = Resolve-FinalPath ( (Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)) ) $core = Join-Path $repo "tools\package_agent_handoff.py" $effectiveSourceRoot = if ($SourceRoot) { Resolve-FinalPath $SourceRoot } else { $repo } $pythonIsolationFlags = @("-I", "-S", "-B") function Test-PathWithinRoot { param( [string]$Path, [string]$Root ) $fullPath = [System.IO.Path]::GetFullPath($Path) $fullRoot = [System.IO.Path]::GetFullPath($Root) if ([string]::Equals($fullPath, $fullRoot, [System.StringComparison]::OrdinalIgnoreCase)) { return $true } if (-not $fullRoot.EndsWith([System.IO.Path]::DirectorySeparatorChar)) { $fullRoot += [System.IO.Path]::DirectorySeparatorChar } return $fullPath.StartsWith($fullRoot, [System.StringComparison]::OrdinalIgnoreCase) } function Test-ExternalPythonCandidate { param([string]$Executable) if (-not (Test-Path -LiteralPath $Executable -PathType Leaf)) { return $false } try { $candidatePath = Resolve-FinalPath $Executable } catch { return $false } return -not ( (Test-PathWithinRoot -Path $candidatePath -Root $repo) -or (Test-PathWithinRoot -Path $candidatePath -Root $effectiveSourceRoot) ) } function Test-Python311 { param( [string]$Executable, [string[]]$Prefix ) try { & $Executable @Prefix @pythonIsolationFlags -c "import sys; raise SystemExit(0 if sys.version_info >= (3, 11) else 1)" *> $null return $LASTEXITCODE -eq 0 } catch { return $false } } function Resolve-Python311 { $candidates = @() $python = Get-Command python.exe -ErrorAction SilentlyContinue if (-not $python) { $python = Get-Command python -ErrorAction SilentlyContinue } if ($python) { $candidates += ,([ordered]@{ exe = $python.Source; prefix = @() }) } $py = Get-Command py.exe -ErrorAction SilentlyContinue if ($py) { $candidates += ,([ordered]@{ exe = $py.Source; prefix = @("-3") }) } foreach ($candidate in $candidates) { if (-not (Test-ExternalPythonCandidate -Executable $candidate.exe)) { continue } if (Test-Python311 -Executable $candidate.exe -Prefix $candidate.prefix) { return $candidate } } throw "Python 3.11 or newer is required to create the agent handoff package" } if (-not (Test-Path -LiteralPath $core -PathType Leaf)) { throw "Agent handoff packager core is missing: $core" } $python311 = Resolve-Python311 $arguments = @($python311.prefix) + $pythonIsolationFlags + @($core) if ($SourceRoot) { $arguments += @("--source-root", $SourceRoot) } if ($OutputDir) { $arguments += @("--output-dir", $OutputDir) } if ($PackageName) { $arguments += @("--package-name", $PackageName) } if ($ForceOverwrite) { $arguments += "--force-overwrite" } if ($AuditOnly) { $arguments += "--audit-only" } & $python311.exe @arguments exit $LASTEXITCODE