feat(desktop): attest and manage the owned Windows host lifecycle

This commit is contained in:
Agent Workbench Dev
2026-10-31 09:03:01 +08:00
parent e634a252dd
commit 249f191ebc
32 changed files with 1765 additions and 70 deletions
+14 -51
View File
@@ -1,56 +1,19 @@
[CmdletBinding()]
param([int]$Port = 0, [switch]$NoBrowser)
param([int]$Port = 0, [switch]$NoBrowser, [string]$DataRoot = '', [string]$NodePath = '')
$ErrorActionPreference = 'Stop'
[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false)
$appRoot = [IO.Path]::GetFullPath((Split-Path -Parent $PSScriptRoot))
if ($Port -eq 0) { $Port = if ($env:AGENT_WORKBENCH_PORT) { [int]$env:AGENT_WORKBENCH_PORT } else { 4318 } }
if ($Port -lt 1 -or $Port -gt 65535) { throw 'Please use a port between 1 and 65535.' }
$workbenchUrl = "http://127.0.0.1:$Port"
function Read-WorkbenchHealth {
try { return Invoke-RestMethod -Uri "$workbenchUrl/api/health" -TimeoutSec 2 }
catch { return $null }
}
$health = Read-WorkbenchHealth
if ($health) {
if ($health.app -ne 'agent-workbench' -or -not $health.ready) { throw "The port is in use by another service: $Port" }
Write-Output "Agent Workbench is already running: $workbenchUrl"
if (-not $NoBrowser) { Start-Process -FilePath $workbenchUrl }
exit 0
}
$serverFile = Join-Path $appRoot 'dist-server\server.mjs'
if (-not (Test-Path -LiteralPath $serverFile)) { throw 'Build the source first with npm.cmd run build, or use the portable package.' }
$nodeFile = Join-Path $appRoot 'runtime\node.exe'
if (-not (Test-Path -LiteralPath $nodeFile)) {
$installedNode = Get-Command node.exe -ErrorAction SilentlyContinue
if (-not $installedNode) { throw 'Node.js 24 was not found. Use the portable package with its bundled runtime.' }
$nodeFile = $installedNode.Source
}
$dataRoot = if ($env:AGENT_WORKBENCH_DATA_DIR) { [IO.Path]::GetFullPath($env:AGENT_WORKBENCH_DATA_DIR) } else { Join-Path $env:LOCALAPPDATA 'AgentWorkbench' }
$logsRoot = Join-Path $dataRoot 'logs'
[IO.Directory]::CreateDirectory($logsRoot) | Out-Null
$stdoutFile = Join-Path $logsRoot 'service.stdout.log'
$stderrFile = Join-Path $logsRoot 'service.stderr.log'
$env:AGENT_WORKBENCH_APP_DIR = $appRoot
$env:AGENT_WORKBENCH_DATA_DIR = $dataRoot
$env:AGENT_WORKBENCH_PORT = [string]$Port
$childProcess = Start-Process -FilePath $nodeFile -ArgumentList ('"' + $serverFile + '"') -WorkingDirectory $appRoot -WindowStyle Hidden -PassThru -RedirectStandardOutput $stdoutFile -RedirectStandardError $stderrFile
$deadline = [DateTime]::UtcNow.AddSeconds(15)
do {
Start-Sleep -Milliseconds 200
$childProcess.Refresh()
if ($childProcess.HasExited) {
$diagnostic = if (Test-Path -LiteralPath $stderrFile) { (Get-Content -LiteralPath $stderrFile -Tail 8) -join "`n" } else { '' }
throw "Agent Workbench could not start. $diagnostic"
}
$health = Read-WorkbenchHealth
} while ((-not $health -or -not $health.ready) -and [DateTime]::UtcNow -lt $deadline)
if (-not $health -or $health.app -ne 'agent-workbench' -or -not $health.ready) { throw "Service did not become ready. See $stderrFile" }
$serviceInfo = @{ app = 'agent-workbench'; pid = $childProcess.Id; port = $Port; appRoot = $appRoot; dataRoot = $dataRoot; startedAt = [DateTime]::UtcNow.ToString('o') } | ConvertTo-Json
[IO.File]::WriteAllText((Join-Path $dataRoot 'service-info.json'), $serviceInfo, [Text.UTF8Encoding]::new($false))
Write-Output "Agent Workbench is ready: $workbenchUrl"
Write-Output "Data: $dataRoot"
if (-not $NoBrowser) { Start-Process -FilePath $workbenchUrl }
if (-not $DataRoot) { $DataRoot = if ($env:AGENT_WORKBENCH_DATA_DIR) { $env:AGENT_WORKBENCH_DATA_DIR } else { Join-Path $env:LOCALAPPDATA 'AgentWorkbench' } }
$DataRoot = [IO.Path]::GetFullPath($DataRoot)
if (-not $NodePath) { $NodePath = Join-Path $appRoot 'runtime\node.exe' }
if (-not (Test-Path -LiteralPath $NodePath)) { $NodePath = (Get-Command node.exe -ErrorAction Stop).Source }
$launcher = Join-Path $appRoot 'dist-server\host-launcher.mjs'
if (-not (Test-Path -LiteralPath $launcher)) { throw 'Build the source first, or use the complete portable package.' }
$output = & $NodePath $launcher start --app-root $appRoot --data-root $DataRoot --node $NodePath --port ([string]$Port)
if ($LASTEXITCODE -ne 0) { throw 'The host could not be verified. See the original diagnostic above.' }
$result = $output | ConvertFrom-Json
Write-Output ('Agent Workbench is ready: ' + $result.url)
Write-Output ('Data: ' + $DataRoot)
Write-Output ('Verified host PID: ' + $result.identity.pid + ' · Instance: ' + $result.identity.instanceId)
if (-not $NoBrowser) { Start-Process -FilePath $result.url }