param([switch]$EnableHosting) $ErrorActionPreference = 'Stop' $projectRoot = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../..')) $localDirectory = Join-Path $projectRoot '.runtime/local-cluster' $gatewayFile = Join-Path $localDirectory 'gateway.env' $reportFile = Join-Path $localDirectory 'smoke-report.json' $triggerFile = Join-Path $projectRoot 'backend/target/classes/.reloadtrigger' if (-not (Test-Path -LiteralPath $reportFile)) { throw 'Run the actual local isolation smoke checks first.' } $report = Get-Content -LiteralPath $reportFile -Raw | ConvertFrom-Json if (-not $report.passed -or [DateTimeOffset]::UtcNow.ToUnixTimeSeconds() - $report.checkedAtUnix -gt 86400) { throw 'The local isolation checks must have passed within the last 24 hours.' } if (-not (Test-Path -LiteralPath $triggerFile)) { throw 'Compile and start the local preview backend first.' } # Restrict the directory before reading or writing local infrastructure credentials. $acl = [Security.AccessControl.DirectorySecurity]::new() $acl.SetAccessRuleProtection($true, $false) foreach ($identity in @([Security.Principal.WindowsIdentity]::GetCurrent().User, [Security.Principal.SecurityIdentifier]::new('S-1-5-18'))) { $acl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new($identity, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')) } Set-Acl -LiteralPath $localDirectory -AclObject $acl $gatewayValues = @{} Get-Content -LiteralPath $gatewayFile | ForEach-Object { $pair = $_ -split '=', 2 if ($pair.Count -eq 2) { $gatewayValues[$pair[0]] = $pair[1] } } foreach ($key in @('SANDBOX_TOKEN', 'RUNTIME_TOKEN')) { if ($gatewayValues[$key].Length -lt 32) { throw 'Local gateway credentials are missing or invalid.' } } $health = Invoke-RestMethod -Uri http://127.0.0.1:8090/health/ready -Headers @{Authorization = ('Bearer ' + $gatewayValues['SANDBOX_TOKEN'])} -TimeoutSec 10 if (-not $health.ready) { throw 'Gateway Kubernetes readiness check failed.' } if ($EnableHosting) { $runtime = Invoke-RestMethod -Uri http://127.0.0.1:8090/runtime/source-ready -Headers @{Authorization = ('Bearer ' + $gatewayValues['RUNTIME_TOKEN'])} -TimeoutSec 10 if (-not $runtime.ready) { throw 'Local source runtime is not configured. Deploy the updated gateway first.' } } $infra = @{app = @{ 'worker-enabled' = $true; 'worker-capacity' = 4 'sandbox-url' = 'http://127.0.0.1:8090'; 'sandbox-token' = $gatewayValues['SANDBOX_TOKEN'] 'runtime-url' = 'http://127.0.0.1:8090'; 'runtime-token' = $gatewayValues['RUNTIME_TOKEN'] 'runtime-worker-enabled' = [bool]$EnableHosting }} $infra | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath (Join-Path $localDirectory 'preview-infrastructure.yml') -Encoding utf8 Set-Content -LiteralPath $triggerFile -Value (Get-Date -Format o) Write-Output 'Local preview AI execution enabled. Check READY in the administrator server page.'