agentroam 0.2.0-preview.8 → 0.2.0-preview.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/README.md +61 -15
  2. package/bin/agentroam.mjs +10 -1
  3. package/bin/cli-progress.d.mts +9 -0
  4. package/bin/cli-progress.mjs +65 -0
  5. package/bin/desktop-download.mjs +69 -0
  6. package/bin/node-preflight.mjs +171 -0
  7. package/bin/node-runtime-policy.json +4 -0
  8. package/bin/runtime-policy.d.mts +5 -0
  9. package/bin/runtime-policy.mjs +37 -0
  10. package/dist/abort.js +25 -0
  11. package/dist/args.js +136 -5
  12. package/dist/cli.js +295 -44
  13. package/dist/cloudflared/bundled-asset.js +3 -2
  14. package/dist/cloudflared/manifest.js +2 -2
  15. package/dist/codex-desktop-command.js +70 -0
  16. package/dist/codex-desktop-force-stop.js +46 -0
  17. package/dist/codex-desktop-idle-guard.js +15 -0
  18. package/dist/codex-desktop-preflight.js +63 -0
  19. package/dist/codex-desktop-probe.js +152 -0
  20. package/dist/codex-desktop-setup.js +154 -0
  21. package/dist/codex-desktop-status.js +85 -0
  22. package/dist/codex-desktop-windows.js +122 -0
  23. package/dist/codex-runtime-manager.js +368 -0
  24. package/dist/codex-sharing.js +155 -0
  25. package/dist/codex-windows-backend-host.js +59 -0
  26. package/dist/codex-windows-backend.js +120 -0
  27. package/dist/desktop-authorization-prompt.js +84 -0
  28. package/dist/desktop-authorization.js +177 -0
  29. package/dist/device-pairing.js +228 -0
  30. package/dist/error-log.js +113 -0
  31. package/dist/instance-takeover.js +143 -0
  32. package/dist/local-desktop-url.js +8 -0
  33. package/dist/local-service-request.js +69 -0
  34. package/dist/mesh/mesh-command.js +51 -0
  35. package/dist/mesh/mesh-config.js +95 -0
  36. package/dist/mesh/mesh-payload.js +20 -0
  37. package/dist/mesh/mesh-runtime.js +101 -0
  38. package/dist/native-runtime.js +68 -0
  39. package/dist/network.js +46 -3
  40. package/dist/node-runtime-manager.js +251 -0
  41. package/dist/opencode-runtime-manager.js +260 -0
  42. package/dist/platform-packages.js +18 -5
  43. package/dist/platform.js +6 -4
  44. package/dist/power/sleep-inhibitor.js +161 -0
  45. package/dist/runtime-manager.js +139 -19
  46. package/dist/self-hosted-relay.json +1 -0
  47. package/dist/service/launcher-cleanup.js +79 -0
  48. package/dist/service/macos-launch-agent.js +288 -0
  49. package/dist/service/runtime-state.js +43 -0
  50. package/dist/service/service-command.js +226 -0
  51. package/dist/service/service-controller.js +29 -0
  52. package/dist/service/service-files.js +78 -0
  53. package/dist/service/windows-service-host.js +59 -0
  54. package/dist/service/windows-task-service.js +289 -0
  55. package/dist/startup-lock.js +81 -0
  56. package/dist/tailscale/bundled-asset.js +43 -0
  57. package/dist/tailscale/daemon-manager.js +112 -0
  58. package/dist/tailscale/installer.js +131 -0
  59. package/dist/tailscale/manifest.js +15 -0
  60. package/dist/tailscale/tailnet-cli.js +101 -0
  61. package/dist/todo-mcp-command.js +27 -0
  62. package/dist/tunnel/cloudflare-provider.js +4 -1
  63. package/dist/tunnel/public-readiness.js +76 -50
  64. package/dist/tunnel/relay-orchestrator.js +53 -19
  65. package/dist/tunnel/self-hosted-provider.js +147 -0
  66. package/dist/tunnel/tunnel-controller.js +232 -0
  67. package/dist/unlock-service.js +105 -0
  68. package/dist/update/update-channel.js +38 -0
  69. package/dist/update/update-command.js +97 -0
  70. package/dist/update/update-install.js +34 -0
  71. package/dist/update/update-state.js +43 -0
  72. package/dist/update/update-worker.js +82 -0
  73. package/install/README.md +55 -0
  74. package/install/codex-shared-watchdog.sh +16 -0
  75. package/install/install-agentroam.ps1 +358 -0
  76. package/install/install-agentroam.sh +457 -0
  77. package/install/install-manifest.json +15 -0
  78. package/install/start-codex-desktop-shared.sh +152 -0
  79. package/install/write-relay-default.mjs +6 -0
  80. package/package.json +15 -10
  81. package/dist/args.test.js +0 -27
  82. package/dist/cloudflared/bundled-asset.test.js +0 -76
  83. package/dist/cloudflared/installer.test.js +0 -95
  84. package/dist/native-runtime.test.js +0 -19
  85. package/dist/network.test.js +0 -20
  86. package/dist/pairing.js +0 -2
  87. package/dist/platform-packages.test.js +0 -75
  88. package/dist/platform.test.js +0 -3
  89. package/dist/port.test.js +0 -3
  90. package/dist/process-supervisor.test.js +0 -42
  91. package/dist/tunnel/cloudflare-provider.test.js +0 -33
  92. package/dist/tunnel/pinggy-provider.test.js +0 -40
  93. package/dist/tunnel/proxy-settings.test.js +0 -111
  94. package/dist/tunnel/public-readiness.test.js +0 -104
  95. package/dist/tunnel/relay-orchestrator.test.js +0 -72
@@ -0,0 +1,358 @@
1
+ $ErrorActionPreference = "Stop"
2
+ $ProgressPreference = "SilentlyContinue"
3
+
4
+ $NodeVersion = "22.22.0"
5
+ $MinimumNodeVersion = "22.22.0"
6
+ # 默认跟随 npm preview 最新标签;可用 $env:AGENTROAM_VERSION 钉精确版本
7
+ # 例如:$env:AGENTROAM_VERSION = "<x.y.z-preview.N>"; ./install-agentroam.ps1
8
+ $AgentRoamVersionRequest = if ($env:AGENTROAM_VERSION) { $env:AGENTROAM_VERSION } else { "preview" }
9
+ $AgentRoamVersion = "pending-version"
10
+ $NodeArchive = "node-v22.22.0-win-x64.zip"
11
+ $NodeSha256 = "c97fa376d2becdc8863fcd3ca2dd9a83a9f3468ee7ccf7a6d076ec66a645c77a"
12
+ $NodeUrl = "https://nodejs.org/dist/v22.22.0/$NodeArchive"
13
+ $NpmRegistry = "https://registry.npmjs.org"
14
+ $DataDir = if ($env:AGENTROAM_DATA_DIR) { $env:AGENTROAM_DATA_DIR } else { Join-Path $HOME ".agentroam" }
15
+ $NodeParent = Join-Path $DataDir "runtimes\node"
16
+ $NodeRoot = Join-Path $NodeParent $NodeVersion
17
+ $NodeLock = "$NodeRoot.lock"
18
+ $LauncherParent = Join-Path $DataDir "launcher"
19
+ $LauncherRoot = Join-Path $LauncherParent $AgentRoamVersion
20
+ $LauncherLock = "$LauncherRoot.lock"
21
+ $WrapperDir = Join-Path $HOME ".agentroam\bin"
22
+ $WrapperPath = Join-Path $WrapperDir "agentroam.cmd"
23
+ $ServiceRootExplicit = -not [string]::IsNullOrWhiteSpace($env:AGENTROAM_ROOT)
24
+ $ServiceRoot = if ($ServiceRootExplicit) { $env:AGENTROAM_ROOT } else { (Get-Location).Path }
25
+ if (-not (Test-Path -LiteralPath $ServiceRoot -PathType Container)) { throw "AgentRoam root is not a directory: $ServiceRoot" }
26
+ $ServiceRoot = [System.IO.Path]::GetFullPath($ServiceRoot).TrimEnd([System.IO.Path]::DirectorySeparatorChar)
27
+ New-Item -ItemType Directory -Force -Path $NodeParent, $LauncherParent, $WrapperDir | Out-Null
28
+
29
+ function Test-NodeVersion([string]$VersionText) {
30
+ if ($VersionText -notmatch '^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$') { return $false }
31
+ try { return [Version]($VersionText.Split('+')[0]) -ge [Version]$MinimumNodeVersion } catch { return $false }
32
+ }
33
+
34
+ function Test-SupportedNode([string]$NodeBin) {
35
+ if (-not (Test-Path -LiteralPath $NodeBin -PathType Leaf)) { return $false }
36
+ try { return Test-NodeVersion ((& $NodeBin -p 'process.versions.node' 2>$null).Trim()) } catch { return $false }
37
+ }
38
+
39
+ function Get-NvmRoots {
40
+ $Candidates = @()
41
+ if ($env:NVM_HOME) { $Candidates += $env:NVM_HOME }
42
+ if ($env:APPDATA) { $Candidates += (Join-Path $env:APPDATA "nvm") }
43
+ $NvmCommand = Get-Command nvm.exe -ErrorAction SilentlyContinue
44
+ if ($NvmCommand) {
45
+ try {
46
+ $RootOutput = (& $NvmCommand.Source root 2>$null | Select-Object -Last 1)
47
+ if ($RootOutput) {
48
+ $RootText = $RootOutput.Trim()
49
+ if ($RootText -match '^[^:]+:\s+(.+)$') { $RootText = $Matches[1].Trim() }
50
+ $Candidates += $RootText
51
+ }
52
+ } catch {}
53
+ }
54
+ @($Candidates | Where-Object { $_ -and (Test-Path -LiteralPath $_ -PathType Container) } | Select-Object -Unique)
55
+ }
56
+
57
+ function Find-NvmNode {
58
+ $VersionMatches = foreach ($NvmRoot in (Get-NvmRoots)) {
59
+ foreach ($VersionDirectory in Get-ChildItem -LiteralPath $NvmRoot -Directory -ErrorAction SilentlyContinue) {
60
+ $Candidate = Join-Path $VersionDirectory.FullName "node.exe"
61
+ if (-not (Test-Path -LiteralPath $Candidate -PathType Leaf)) { continue }
62
+ try {
63
+ $VersionText = (& $Candidate -p 'process.versions.node' 2>$null).Trim()
64
+ if (Test-NodeVersion $VersionText) {
65
+ $Version = [Version]($VersionText.Split('+')[0])
66
+ [pscustomobject]@{ Path = $Candidate; Version = $Version }
67
+ }
68
+ } catch {}
69
+ }
70
+ }
71
+ $Best = $VersionMatches | Sort-Object Version -Descending | Select-Object -First 1
72
+ if ($Best) { return $Best.Path }
73
+ return $null
74
+ }
75
+
76
+ function Test-ManagedNode {
77
+ $NodeBin = Join-Path $NodeRoot "node.exe"
78
+ $NpmCli = Join-Path $NodeRoot "node_modules\npm\bin\npm-cli.js"
79
+ if (-not (Test-Path -LiteralPath $NodeBin -PathType Leaf) -or -not (Test-Path -LiteralPath $NpmCli -PathType Leaf)) { return $false }
80
+ try { return (& $NodeBin --version 2>$null) -eq "v$NodeVersion" } catch { return $false }
81
+ }
82
+
83
+ function Enter-InstallLock([string]$Path) {
84
+ $Deadline = [DateTime]::UtcNow.AddMinutes(15)
85
+ while ($true) {
86
+ try {
87
+ $Stream = [System.IO.File]::Open($Path, [System.IO.FileMode]::CreateNew, [System.IO.FileAccess]::Write, [System.IO.FileShare]::None)
88
+ $Bytes = [Text.Encoding]::UTF8.GetBytes("$PID`n$([DateTime]::UtcNow.ToString('o'))`n")
89
+ $Stream.Write($Bytes, 0, $Bytes.Length)
90
+ return $Stream
91
+ } catch [System.IO.IOException] {
92
+ if (Test-Path -LiteralPath $Path) {
93
+ $Age = [DateTime]::UtcNow - (Get-Item -LiteralPath $Path).LastWriteTimeUtc
94
+ if ($Age.TotalMinutes -gt 15) { Remove-Item -LiteralPath $Path -Force -ErrorAction SilentlyContinue; continue }
95
+ }
96
+ if ([DateTime]::UtcNow -ge $Deadline) { throw "Timed out waiting for install lock $Path" }
97
+ Start-Sleep -Milliseconds 500
98
+ }
99
+ }
100
+ }
101
+
102
+ $SystemNode = Get-Command node.exe -ErrorAction SilentlyContinue
103
+ if ($env:AGENTROAM_BOOTSTRAP_TEST -eq "1" -and $env:AGENTROAM_FORCE_PRIVATE_NODE -eq "1") { $SystemNode = $null }
104
+ if ($SystemNode -and (Test-SupportedNode $SystemNode.Source)) {
105
+ $NodeBin = $SystemNode.Source
106
+ } else {
107
+ $NvmNode = if ($env:AGENTROAM_BOOTSTRAP_TEST -eq "1" -and $env:AGENTROAM_FORCE_PRIVATE_NODE -eq "1") { $null } else { Find-NvmNode }
108
+ if ($NvmNode) {
109
+ $NodeBin = $NvmNode
110
+ Write-Host "Using Node.js $(& $NodeBin --version) from NVM: $NodeBin"
111
+ } else {
112
+ if (-not (Test-ManagedNode)) {
113
+ $NodeLockHandle = Enter-InstallLock $NodeLock
114
+ try {
115
+ if (-not (Test-ManagedNode)) {
116
+ $TempRoot = Join-Path $NodeParent ".node-$NodeVersion-$PID-$([Guid]::NewGuid().ToString('N'))"
117
+ $ArchivePath = Join-Path $TempRoot $NodeArchive
118
+ $ExtractPath = Join-Path $TempRoot "extract"
119
+ New-Item -ItemType Directory -Force -Path $ExtractPath | Out-Null
120
+ try {
121
+ Write-Host "Downloading Node.js $NodeVersion..."
122
+ if ($env:AGENTROAM_NODE_ARCHIVE_FILE) {
123
+ Copy-Item -LiteralPath $env:AGENTROAM_NODE_ARCHIVE_FILE -Destination $ArchivePath
124
+ } else {
125
+ Invoke-WebRequest -UseBasicParsing -TimeoutSec 600 -Uri $NodeUrl -OutFile $ArchivePath
126
+ }
127
+ $ActualHash = (Get-FileHash -Algorithm SHA256 -LiteralPath $ArchivePath).Hash.ToLowerInvariant()
128
+ if ($ActualHash -ne $NodeSha256) { throw "Node.js archive checksum mismatch" }
129
+ Expand-Archive -LiteralPath $ArchivePath -DestinationPath $ExtractPath -Force
130
+ $Entries = @(Get-ChildItem -LiteralPath $ExtractPath)
131
+ if ($Entries.Count -ne 1 -or $Entries[0].Name -ne "node-v$NodeVersion-win-x64") { throw "Unexpected Node.js archive layout" }
132
+ $Extracted = $Entries[0].FullName
133
+ $ExtractedNode = Join-Path $Extracted "node.exe"
134
+ $ExtractedNpm = Join-Path $Extracted "node_modules\npm\bin\npm-cli.js"
135
+ if (-not (Test-Path -LiteralPath $ExtractedNpm) -or (& $ExtractedNode --version) -ne "v$NodeVersion") { throw "Node.js runtime validation failed" }
136
+ Remove-Item -LiteralPath $NodeRoot -Recurse -Force -ErrorAction SilentlyContinue
137
+ Move-Item -LiteralPath $Extracted -Destination $NodeRoot
138
+ if (-not (Test-ManagedNode)) { throw "Node.js activation validation failed" }
139
+ } finally {
140
+ Remove-Item -LiteralPath $TempRoot -Recurse -Force -ErrorAction SilentlyContinue
141
+ }
142
+ }
143
+ } finally {
144
+ $NodeLockHandle.Dispose()
145
+ Remove-Item -LiteralPath $NodeLock -Force -ErrorAction SilentlyContinue
146
+ }
147
+ }
148
+ $NodeBin = Join-Path $NodeRoot "node.exe"
149
+ }
150
+ }
151
+
152
+ if ($env:AGENTROAM_BOOTSTRAP_TEST -eq "1" -and $env:AGENTROAM_BOOTSTRAP_NODE_DISCOVERY_ONLY -eq "1") {
153
+ Write-Output $NodeBin
154
+ exit 0
155
+ }
156
+
157
+ $NodeDirectory = Split-Path -Parent $NodeBin
158
+ $NpmCandidates = @(
159
+ (Join-Path $NodeDirectory "node_modules\npm\bin\npm-cli.js"),
160
+ (Join-Path $NodeDirectory "..\node_modules\npm\bin\npm-cli.js")
161
+ )
162
+ $NpmCli = $NpmCandidates | Where-Object { Test-Path -LiteralPath $_ -PathType Leaf } | Select-Object -First 1
163
+ if (-not $NpmCli) { throw "npm CLI was not found beside $NodeBin" }
164
+
165
+ # 解析 dist-tag → 精确版本(安装最总是最新发布)
166
+ if ($AgentRoamVersionRequest -notmatch '\d$') {
167
+ $Resolved = (& $NodeBin $NpmCli view "agentroam@$AgentRoamVersionRequest" version --registry $NpmRegistry 2>$null | Select-Object -Last 1)
168
+ if (-not $Resolved) { throw "could not resolve agentroam@$AgentRoamVersionRequest from $NpmRegistry" }
169
+ $AgentRoamVersion = ([string]$Resolved).Trim()
170
+ Write-Host "Installing AgentRoam $AgentRoamVersion (resolved from $AgentRoamVersionRequest)"
171
+ } else {
172
+ $AgentRoamVersion = $AgentRoamVersionRequest
173
+ }
174
+ $LauncherRoot = Join-Path $LauncherParent $AgentRoamVersion
175
+ $LauncherLock = "$LauncherRoot.lock"
176
+ $PackageSpec = "agentroam@$AgentRoamVersion"
177
+ $ExtraPackageSpecs = @()
178
+ if ($env:AGENTROAM_BOOTSTRAP_TEST -eq "1" -and $env:AGENTROAM_PACKAGE_SPEC) {
179
+ $PackageSpec = $env:AGENTROAM_PACKAGE_SPEC
180
+ if ($env:AGENTROAM_BOOTSTRAP_EXTRA_SPECS) { $ExtraPackageSpecs = @($env:AGENTROAM_BOOTSTRAP_EXTRA_SPECS.Split(';') | Where-Object { $_ }) }
181
+ }
182
+ $Entry = Join-Path $LauncherRoot "node_modules\agentroam\bin\agentroam.mjs"
183
+
184
+ $LauncherCheckSource = @'
185
+ const { readFileSync, statSync } = require("node:fs");
186
+ const { createHash } = require("node:crypto");
187
+ const { createRequire } = require("node:module");
188
+ const { dirname, resolve, sep } = require("node:path");
189
+ const [prefix, launcherVersion, minimumNodeVersion, target, mode] = process.argv.slice(process.argv[1] === "-" ? 2 : 1);
190
+ try {
191
+ const packageName = {
192
+ "darwin-arm64": "agentroam-runtime-darwin-arm64",
193
+ "windows-amd64": "agentroam-runtime-win32-x64",
194
+ }[target];
195
+ if (!packageName) throw new Error("Unsupported runtime target: " + target);
196
+ const launcherPath = resolve(prefix, "node_modules/agentroam/package.json");
197
+ const launcher = JSON.parse(readFileSync(launcherPath, "utf8"));
198
+ if (launcher.version !== launcherVersion) throw new Error("AgentRoam launcher version mismatch");
199
+ const version = launcher.optionalDependencies?.[packageName];
200
+ if (typeof version !== "string" || !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(version)) {
201
+ throw new Error("Runtime dependency must use an exact version: " + packageName);
202
+ }
203
+ if (mode === "spec") {
204
+ console.log(packageName + "@" + version);
205
+ } else {
206
+ if (mode !== "verify") throw new Error("Unknown runtime validation mode: " + mode);
207
+ const launcherRequire = createRequire(launcherPath);
208
+ const manifestPath = launcherRequire.resolve(packageName + "/manifest.json");
209
+ const runtimeRoot = dirname(launcherRequire.resolve(packageName + "/runtime"));
210
+ const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
211
+ const runtimePackage = JSON.parse(readFileSync(resolve(dirname(manifestPath), "package.json"), "utf8"));
212
+ if (runtimePackage.name !== packageName || runtimePackage.version !== version ||
213
+ manifest.packageVersion !== version || manifest.target !== target ||
214
+ manifest.schemaVersion !== 2 || manifest.minimumNodeVersion !== minimumNodeVersion ||
215
+ !manifest.nativeFiles || typeof manifest.nativeFiles !== "object" || Array.isArray(manifest.nativeFiles)) {
216
+ throw new Error("Invalid runtime manifest: " + manifestPath);
217
+ }
218
+ const nativeTarget = target === "darwin-arm64" ? "darwin-arm64" : "win32-x64";
219
+ for (const file of [
220
+ "node_modules/better-sqlite3/prebuilds/" + nativeTarget + ".node",
221
+ "node_modules/node-pty/prebuilds/" + nativeTarget + "/pty.node",
222
+ ...(target === "darwin-arm64"
223
+ ? ["node_modules/node-pty/prebuilds/darwin-arm64/spawn-helper"]
224
+ : ["node_modules/node-pty/prebuilds/win32-x64/conpty.node",
225
+ "node_modules/node-pty/prebuilds/win32-x64/conpty/conpty.dll",
226
+ "node_modules/node-pty/prebuilds/win32-x64/conpty/OpenConsole.exe"]),
227
+ ]) {
228
+ if (!manifest.nativeFiles[file]) throw new Error("Missing runtime native inventory: " + file);
229
+ }
230
+ for (const file of ["bin/agentroam.mjs"]) {
231
+ if (!statSync(resolve(dirname(launcherPath), file)).isFile()) throw new Error("Missing launcher file: " + file);
232
+ }
233
+ for (const file of ["ws-server.mjs", ".next/BUILD_ID"]) {
234
+ if (!statSync(resolve(runtimeRoot, file)).isFile()) throw new Error("Missing runtime file: " + file);
235
+ }
236
+ for (const [file, expectedHash] of Object.entries(manifest.nativeFiles)) {
237
+ const path = resolve(runtimeRoot, file);
238
+ if (!path.startsWith(runtimeRoot + sep) || !/^[a-f0-9]{64}$/.test(expectedHash)) {
239
+ throw new Error("Invalid runtime native inventory: " + file);
240
+ }
241
+ const actualHash = createHash("sha256").update(readFileSync(path)).digest("hex");
242
+ if (actualHash !== expectedHash) throw new Error("Runtime native file checksum mismatch: " + file);
243
+ }
244
+ const runtimeRequire = createRequire(resolve(runtimeRoot, "package.json"));
245
+ for (const name of ["better-sqlite3", "node-pty"]) {
246
+ if (!runtimeRequire.resolve(name).startsWith(runtimeRoot + sep)) throw new Error("Missing runtime module: " + name);
247
+ }
248
+ const Database = runtimeRequire("better-sqlite3");
249
+ const database = new Database(":memory:");
250
+ try {
251
+ if (database.prepare("SELECT 1 AS ok").get()?.ok !== 1) throw new Error("SQLite runtime query failed");
252
+ } finally {
253
+ database.close();
254
+ }
255
+ if (typeof runtimeRequire("node-pty").spawn !== "function") throw new Error("Invalid PTY runtime module");
256
+ }
257
+ } catch (error) {
258
+ console.error("AgentRoam runtime validation failed: " + error.message);
259
+ process.exitCode = 1;
260
+ }
261
+ '@
262
+
263
+ function Test-LauncherRuntime([string]$Prefix) {
264
+ try {
265
+ # Pipe JavaScript to Node so Windows PowerShell 5 does not strip its quotes.
266
+ $LauncherCheckSource | & $NodeBin - $Prefix $AgentRoamVersion $MinimumNodeVersion windows-amd64 verify 2>$null
267
+ return $LASTEXITCODE -eq 0
268
+ } catch { return $false }
269
+ }
270
+
271
+ function Test-Launcher {
272
+ return Test-LauncherRuntime $LauncherRoot
273
+ }
274
+
275
+ if (-not (Test-Launcher)) {
276
+ $LauncherLockHandle = Enter-InstallLock $LauncherLock
277
+ try {
278
+ if (-not (Test-Launcher)) {
279
+ $TempLauncher = Join-Path $LauncherParent ".launcher-$AgentRoamVersion-$PID-$([Guid]::NewGuid().ToString('N'))"
280
+ try {
281
+ $NpmArgs = @("install", "--no-audit", "--no-fund", "--registry", $NpmRegistry, "--prefix", $TempLauncher) + $ExtraPackageSpecs + @($PackageSpec)
282
+ & $NodeBin $NpmCli @NpmArgs
283
+ if ($LASTEXITCODE -ne 0) { throw "npm install failed with exit code $LASTEXITCODE" }
284
+ $RuntimeSpec = [string]($LauncherCheckSource | & $NodeBin - $TempLauncher $AgentRoamVersion $MinimumNodeVersion windows-amd64 spec)
285
+ if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($RuntimeSpec)) { throw "AgentRoam runtime dependency validation failed" }
286
+ if (-not (Test-LauncherRuntime $TempLauncher)) {
287
+ Remove-Item -LiteralPath (Join-Path $TempLauncher "node_modules\agentroam-runtime-win32-x64") -Recurse -Force -ErrorAction SilentlyContinue
288
+ $RuntimeArgs = @("install", "--save-exact", "--include=optional", "--no-audit", "--no-fund", "--registry", $NpmRegistry, "--prefix", $TempLauncher, $RuntimeSpec.Trim())
289
+ & $NodeBin $NpmCli @RuntimeArgs
290
+ if ($LASTEXITCODE -ne 0) { throw "Required AgentRoam runtime install failed with exit code $LASTEXITCODE" }
291
+ }
292
+ $LauncherCheckSource | & $NodeBin - $TempLauncher $AgentRoamVersion $MinimumNodeVersion windows-amd64 verify
293
+ if ($LASTEXITCODE -ne 0) { throw "AgentRoam runtime validation failed; previous installation preserved" }
294
+ $PreviousLauncher = $null
295
+ if (Test-Path -LiteralPath $LauncherRoot) {
296
+ $PreviousLauncher = Join-Path $LauncherParent ".previous-$AgentRoamVersion-$([Guid]::NewGuid().ToString('N'))"
297
+ Move-Item -LiteralPath $LauncherRoot -Destination $PreviousLauncher
298
+ }
299
+ try {
300
+ Move-Item -LiteralPath $TempLauncher -Destination $LauncherRoot
301
+ } catch {
302
+ if ($PreviousLauncher -and -not (Test-Path -LiteralPath $LauncherRoot)) {
303
+ Move-Item -LiteralPath $PreviousLauncher -Destination $LauncherRoot
304
+ }
305
+ throw
306
+ }
307
+ if ($PreviousLauncher) { Remove-Item -LiteralPath $PreviousLauncher -Recurse -Force }
308
+ } finally {
309
+ Remove-Item -LiteralPath $TempLauncher -Recurse -Force -ErrorAction SilentlyContinue
310
+ }
311
+ }
312
+ } finally {
313
+ $LauncherLockHandle.Dispose()
314
+ Remove-Item -LiteralPath $LauncherLock -Force -ErrorAction SilentlyContinue
315
+ }
316
+ }
317
+
318
+ $WrapperTemp = "$WrapperPath.$PID.tmp"
319
+ $WrapperContent = "@echo off`r`n`"$NodeBin`" `"$Entry`" %*`r`n"
320
+ [System.IO.File]::WriteAllText($WrapperTemp, $WrapperContent, [Text.Encoding]::ASCII)
321
+ Move-Item -LiteralPath $WrapperTemp -Destination $WrapperPath -Force
322
+
323
+ $UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
324
+ $PathParts = @($UserPath -split ";" | Where-Object { $_ })
325
+ if ($PathParts -notcontains $WrapperDir) {
326
+ $UpdatedPath = if ($UserPath) { "$UserPath;$WrapperDir" } else { $WrapperDir }
327
+ [Environment]::SetEnvironmentVariable("Path", $UpdatedPath, "User")
328
+ $env:Path = "$env:Path;$WrapperDir"
329
+ }
330
+
331
+ & $NodeBin $Entry doctor --data-dir $DataDir
332
+ if ($LASTEXITCODE -ne 0) { Write-Warning "Some component checks failed; continuing background service installation. Affected features may be unavailable. Run agentroam doctor to retry." }
333
+ if ($env:AGENTROAM_INSTALL_SKIP_SERVICE -eq "1") {
334
+ Write-Host "AgentRoam service registration skipped for isolated verification."
335
+ } else {
336
+ & $NodeBin $Entry service install --root $ServiceRoot --data-dir $DataDir --setup-codex-desktop
337
+ if ($LASTEXITCODE -ne 0) { throw "CLI installed, but the background service failed (exit $LASTEXITCODE). Run agentroam service logs, fix the error, then run agentroam service start and agentroam pair." }
338
+ }
339
+ Write-Host "`nAgentRoam $AgentRoamVersion installed: $WrapperPath"
340
+ Write-Host "Open a new terminal, then run: agentroam service status"
341
+
342
+ $DesktopChoice = if ($env:AGENTROAM_INSTALL_DESKTOP) { $env:AGENTROAM_INSTALL_DESKTOP } else { "ask" }
343
+ if ($env:AGENTROAM_INSTALL_SKIP_SERVICE -eq "1" -and $DesktopChoice -eq "ask") { $DesktopChoice = "no" }
344
+ if ($DesktopChoice -eq "ask") {
345
+ $DesktopChoice = "no"
346
+ if ([Environment]::UserInteractive -and -not [Console]::IsInputRedirected) {
347
+ try { $DesktopChoice = Read-Host "Download the desktop app for remote viewing and control? [y/N]" } catch { $DesktopChoice = "no" }
348
+ }
349
+ }
350
+ if ($DesktopChoice -match '^(y|yes|是)$') {
351
+ $DesktopHelper = Join-Path $LauncherRoot "node_modules\agentroam\bin\desktop-download.mjs"
352
+ if (Test-Path -LiteralPath $DesktopHelper -PathType Leaf) {
353
+ & $NodeBin $DesktopHelper $AgentRoamVersion
354
+ if ($LASTEXITCODE -ne 0) { Write-Warning "CLI installation completed. You can retry the desktop download later." }
355
+ } else {
356
+ Write-Warning "This CLI version does not support desktop downloads. CLI installation completed; upgrade to retry."
357
+ }
358
+ } else { Write-Host "Desktop download skipped. CLI is ready to use." }