@trillboards/edge-sdk 0.2.159 → 0.2.165

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 (51) hide show
  1. package/deploy/macos/TrillboardsSetupApp.m +36 -0
  2. package/deploy/macos/pair.command +13 -0
  3. package/deploy/windows/README.md +17 -0
  4. package/deploy/windows/ci/test-local-ai-diagnostic.ps1 +213 -0
  5. package/deploy/windows/ci/test-published-install.ps1 +243 -0
  6. package/deploy/windows/e2e/test-local-sense.ps1 +32 -3
  7. package/deploy/windows/installer.nsi +12 -0
  8. package/deploy/windows/setup-detector.ps1 +20 -42
  9. package/deploy/windows/setup-litertlm.ps1 +126 -37
  10. package/deploy/windows/stop-local-ai.ps1 +122 -4
  11. package/deploy/windows/trillboards-pair.cmd +8 -0
  12. package/deploy/windows/trillboards-setup.ps1 +68 -17
  13. package/dist/EdgeAgent.js +1 -1
  14. package/dist/InstallerOnboardingClient.d.ts +1 -0
  15. package/dist/InstallerOnboardingClient.d.ts.map +1 -1
  16. package/dist/InstallerOnboardingClient.js +10 -0
  17. package/dist/InstallerOnboardingClient.js.map +1 -1
  18. package/dist/LiteRtVlmProvider.d.ts +6 -3
  19. package/dist/LiteRtVlmProvider.d.ts.map +1 -1
  20. package/dist/LiteRtVlmProvider.js +12 -5
  21. package/dist/LiteRtVlmProvider.js.map +1 -1
  22. package/dist/SetupSessionClient.d.ts +5 -0
  23. package/dist/SetupSessionClient.d.ts.map +1 -1
  24. package/dist/SetupSessionClient.js +1 -0
  25. package/dist/SetupSessionClient.js.map +1 -1
  26. package/dist/autoSense.d.ts +10 -1
  27. package/dist/autoSense.d.ts.map +1 -1
  28. package/dist/autoSense.js +135 -5
  29. package/dist/autoSense.js.map +1 -1
  30. package/dist/cli.d.ts.map +1 -1
  31. package/dist/cli.js +24 -1
  32. package/dist/cli.js.map +1 -1
  33. package/dist/connectorCameraSetup.d.ts +26 -0
  34. package/dist/connectorCameraSetup.d.ts.map +1 -0
  35. package/dist/connectorCameraSetup.js +225 -0
  36. package/dist/connectorCameraSetup.js.map +1 -0
  37. package/dist/diagnose.d.ts +1 -1
  38. package/dist/diagnose.js +1 -1
  39. package/dist/installerOnboardingMode.d.ts +1 -0
  40. package/dist/installerOnboardingMode.d.ts.map +1 -1
  41. package/dist/installerOnboardingMode.js +25 -5
  42. package/dist/installerOnboardingMode.js.map +1 -1
  43. package/dist/setupMode.d.ts +38 -0
  44. package/dist/setupMode.d.ts.map +1 -1
  45. package/dist/setupMode.js +217 -49
  46. package/dist/setupMode.js.map +1 -1
  47. package/dist/setupProgress.d.ts +1 -0
  48. package/dist/setupProgress.d.ts.map +1 -1
  49. package/dist/setupProgress.js +2 -0
  50. package/dist/setupProgress.js.map +1 -1
  51. package/package.json +1 -1
@@ -87,7 +87,15 @@ FunctionEnd
87
87
  !insertmacro MUI_UNPAGE_INSTFILES
88
88
  !insertmacro MUI_LANGUAGE "English"
89
89
 
90
+ ; A PowerShell7 caller lends its Core module path to native Windows
91
+ ; PowerShell5.1 through this GUI process. Let native children construct their
92
+ ; own module path. This changes only this process and its future children.
93
+ !macro NativePowerShellModuleEnvironment
94
+ System::Call 'kernel32::SetEnvironmentVariableW(w "PSModulePath", p 0) i .r9'
95
+ !macroend
96
+
90
97
  Function .onInit
98
+ !insertmacro NativePowerShellModuleEnvironment
91
99
  ; -- Unattended parameters ------------------------------------------------
92
100
  ; /S NSIS silent install (no pages, no prompts)
93
101
  ; /ENROLLMENT_TOKEN=<tok> the FLEET enrollment credential from the portal
@@ -272,6 +280,10 @@ Section "Install"
272
280
  WriteRegDWORD HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\TrillboardsEdge" "NoRepair" 1
273
281
  SectionEnd
274
282
 
283
+ Function un.onInit
284
+ !insertmacro NativePowerShellModuleEnvironment
285
+ FunctionEnd
286
+
275
287
  Section "Uninstall"
276
288
  ; Stop + remove the background tasks (connector + litert-lm serve + detector).
277
289
  nsExec::ExecToLog 'powershell -NoProfile -ExecutionPolicy Bypass -File "$INSTDIR\bin\stop-existing.ps1" -InstallDir "$INSTDIR" -IncludeLocalAi'
@@ -131,52 +131,19 @@ if (-not $haveDetectorSetupMutex) {
131
131
  }
132
132
  $pausedDetectorTask = $null
133
133
  $pausedDetectorWasEnabled = $false
134
+ $pausedDetectorWasRunning = $false
134
135
  $detectorTaskReplaced = $false
135
136
 
136
137
  function Stop-OwnedDetectorForRepair {
138
+ $script:pausedDetectorHelper = Join-Path $Here 'stop-local-ai.ps1'
137
139
  $ownLauncher = [IO.Path]::GetFullPath((Join-Path $LocalBin 'trillboards-detector.exe'))
138
- $ownToolRoot = [IO.Path]::GetFullPath((Join-Path $env:APPDATA 'uv\tools\trillboards-detector')).TrimEnd('\') + '\'
139
- $task = Get-ScheduledTask -TaskName 'TrillboardsDetector' -ErrorAction SilentlyContinue
140
- if ($task) {
141
- $actions = @($task.Actions)
142
- $ownedAction = $actions.Count -eq 1 -and [IO.Path]::GetFullPath($actions[0].Execute) -ieq $ownLauncher
143
- if ($actions.Count -eq 1 -and -not $ownedAction) {
144
- $wscript = Join-Path $env:WINDIR 'System32\wscript.exe'
145
- $hiddenHost = Join-Path $Here 'trillboards-hidden-run.vbs'
146
- $prefix = '//B //NoLogo "' + $hiddenHost + '" --wait "' + $ownLauncher + '" serve '
147
- $ownedAction = [IO.Path]::GetFullPath($actions[0].Execute) -ieq [IO.Path]::GetFullPath($wscript) -and
148
- ([string]$actions[0].Arguments).StartsWith($prefix, [StringComparison]::OrdinalIgnoreCase)
149
- }
150
- if (-not $ownedAction) {
151
- throw 'The detector task is not owned by this user installation; refusing to stop or replace it'
152
- }
153
- $taskSid = (New-Object System.Security.Principal.NTAccount($task.Principal.UserId)).Translate([System.Security.Principal.SecurityIdentifier]).Value
154
- if ($taskSid -ne $detectorUserSid) { throw 'The detector task principal belongs to another user' }
155
- if (-not $script:pausedDetectorTask) {
156
- $script:pausedDetectorTask = $task
157
- $script:pausedDetectorWasEnabled = [bool]$task.Settings.Enabled
158
- }
159
- Disable-ScheduledTask -TaskName $task.TaskName -TaskPath $task.TaskPath -ErrorAction Stop | Out-Null
160
- Stop-ScheduledTask -TaskName $task.TaskName -TaskPath $task.TaskPath -ErrorAction Stop
161
- }
162
- # Task Scheduler may leave a child after stopping its launcher. Scope by this
163
- # user's exact tool paths AND SID, never by all Python processes or a port.
164
- foreach ($process in @(Get-CimInstance Win32_Process -ErrorAction Stop)) {
165
- $exe = $process.ExecutablePath
166
- if (-not $exe -or ($exe -ine $ownLauncher -and -not $exe.StartsWith($ownToolRoot, [StringComparison]::OrdinalIgnoreCase))) { continue }
167
- try { $owner = Invoke-CimMethod -InputObject $process -MethodName GetOwnerSid -ErrorAction Stop } catch {
168
- # Stopping the launcher can also finish a child from this CIM snapshot.
169
- # Only an independently confirmed absent PID is already stopped; a live
170
- # process with an unverifiable owner remains a visible repair failure.
171
- if (-not (Get-CimInstance Win32_Process -Filter ("ProcessId=" + $process.ProcessId) -ErrorAction Stop)) { continue }
172
- throw
173
- }
174
- if ($owner.ReturnValue -ne 0 -or $owner.Sid -ne $detectorUserSid) { throw 'Cannot verify detector process ownership' }
175
- Log "stopping this user's detector process before venv repair (pid $($process.ProcessId))"
176
- Stop-Process -Id $process.ProcessId -Force -ErrorAction SilentlyContinue
177
- $remaining = Get-Process -Id $process.ProcessId -ErrorAction SilentlyContinue
178
- if ($remaining -and -not $remaining.WaitForExit(10000)) { throw 'Owned detector process did not stop before venv repair' }
140
+ $prior = & $script:pausedDetectorHelper -Service Detector -ToolPath $ownLauncher -Port ([int]$Port) -Inspect
141
+ if ($prior.TaskPresent -and -not $script:pausedDetectorTask) {
142
+ $script:pausedDetectorTask = [pscustomobject]@{ TaskName = $prior.TaskName; TaskPath = '\' }
143
+ $script:pausedDetectorWasEnabled = $prior.Enabled
144
+ $script:pausedDetectorWasRunning = $prior.Running
179
145
  }
146
+ & $script:pausedDetectorHelper -Service Detector -ToolPath $ownLauncher -Port ([int]$Port) | Out-Null
180
147
  }
181
148
 
182
149
  try {
@@ -438,7 +405,18 @@ Log "done"
438
405
  exit 0
439
406
  } finally {
440
407
  if ($pausedDetectorTask -and -not $detectorTaskReplaced -and $pausedDetectorWasEnabled) {
441
- Enable-ScheduledTask -TaskName $pausedDetectorTask.TaskName -TaskPath $pausedDetectorTask.TaskPath -ErrorAction SilentlyContinue | Out-Null
408
+ try {
409
+ # A failed uv install/version check/child registration must also restore
410
+ # availability for a logon-only task. Do not duplicate surviving children.
411
+ $currentTask = & $pausedDetectorHelper -Service Detector -ToolPath (Join-Path $LocalBin 'trillboards-detector.exe') -Port ([int]$Port) -InspectTaskOnly
412
+ if (-not $currentTask.TaskPresent) { throw 'The previous detector task is missing after failed repair' }
413
+ Enable-ScheduledTask -TaskName $pausedDetectorTask.TaskName -TaskPath $pausedDetectorTask.TaskPath -ErrorAction Stop | Out-Null
414
+ $current = & $pausedDetectorHelper -Service Detector -ToolPath (Join-Path $LocalBin 'trillboards-detector.exe') -Port ([int]$Port) -Inspect
415
+ if ($pausedDetectorWasRunning -and $current.TaskState -notin @('Running','Queued') -and
416
+ $current.OwnedProcessCount -eq 0 -and $current.ListenerCount -eq 0) {
417
+ Start-ScheduledTask -TaskName $pausedDetectorTask.TaskName -TaskPath $pausedDetectorTask.TaskPath -ErrorAction Stop
418
+ }
419
+ } catch { Log ('WARN: could not restore the detector after failed repair: ' + $_.Exception.Message) }
442
420
  }
443
421
  if ($haveDetectorSetupMutex) { $detectorSetupMutex.ReleaseMutex() }
444
422
  $detectorSetupMutex.Dispose()
@@ -111,6 +111,25 @@ function Write-DownloadProgress {
111
111
  Write-SetupProgress -Stage "local_ai" -Title "Downloading local AI" -Message "Gemma is downloading to this computer. Keep it online; cloud inference remains off." -Phase "downloading_model" -Percent $percent -DownloadedBytes $downloaded -TotalBytes $ExpectedModelBytes
112
112
  }
113
113
 
114
+ # Retain the native process handle before polling. Windows PowerShell 5.1 can
115
+ # otherwise return a null ExitCode after Start-Process/Refresh/WaitForExit, so a
116
+ # completed model download is never imported even though curl succeeded.
117
+ function Invoke-ModelDownload {
118
+ param([string]$Url, [string]$Destination, [string]$StdoutPath, [string]$StderrPath)
119
+ $process = Start-Process -FilePath "curl.exe" -ArgumentList @("--fail", "--location", "--output", ('"' + $Destination + '"'), $Url) -WindowStyle Hidden -RedirectStandardOutput $StdoutPath -RedirectStandardError $StderrPath -PassThru -ErrorAction Stop
120
+ try {
121
+ $null = $process.Handle
122
+ while (-not $process.HasExited) {
123
+ Write-DownloadProgress
124
+ Start-Sleep -Seconds 2
125
+ $process.Refresh()
126
+ }
127
+ $process.WaitForExit()
128
+ if ($null -eq $process.ExitCode) { throw 'Model download ended without a trustworthy exit code' }
129
+ return [int]$process.ExitCode
130
+ } finally { $process.Dispose() }
131
+ }
132
+
114
133
  function Test-ServeHealthy {
115
134
  try {
116
135
  $response = Invoke-WebRequest -Uri "http://127.0.0.1:$Port/v1/models" -UseBasicParsing -TimeoutSec 3
@@ -169,6 +188,53 @@ $script:LitertlmProbeJpegB64 = "/9j/4AAQSkZJRgABAgAAAQABAAD//gAPTGF2YzYzLjEuMTAx
169
188
 
170
189
  function Get-LitertlmConfigPath { Join-Path $LitertDir "config.json" }
171
190
 
191
+ $script:LitertlmVerifiedInferenceModel = ''
192
+
193
+ # The pinned CLI resolves a bare model id through config.models[id].backend.
194
+ # Reuse that selection on a retry instead of rebuilding a failed GPU engine
195
+ # after the preceding setup has already proved its CPU fallback.
196
+ function Get-LitertlmPreferredInferenceModel {
197
+ try {
198
+ $path = Get-LitertlmConfigPath
199
+ if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { return '' }
200
+ $config = Get-Content -LiteralPath $path -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
201
+ $models = $config.PSObject.Properties['models']
202
+ if ($null -eq $models) { return '' }
203
+ $model = $models.Value.PSObject.Properties[$ModelId]
204
+ if ($null -eq $model) { return '' }
205
+ $backend = $model.Value.PSObject.Properties['backend']
206
+ if ($null -ne $backend -and [string]$backend.Value -in @('cpu','gpu','npu')) { return $ModelId }
207
+ } catch { }
208
+ return ''
209
+ }
210
+
211
+ function Set-LitertlmVerifiedBackend([string]$InferenceModel) {
212
+ $ErrorActionPreference = 'Stop'
213
+ if ($InferenceModel -notin @($ModelId, "$ModelId,cpu", "$ModelId,gpu", "$ModelId,npu")) {
214
+ throw 'Cannot retain an unverified inference model'
215
+ }
216
+ $path = Get-LitertlmConfigPath
217
+ $config = Get-Content -LiteralPath $path -Raw -ErrorAction Stop | ConvertFrom-Json -ErrorAction Stop
218
+ $models = $config.PSObject.Properties['models']
219
+ if ($null -eq $models) { throw 'The verified model configuration is missing' }
220
+ $model = $models.Value.PSObject.Properties[$ModelId]
221
+ if ($null -eq $model) { throw 'The verified model configuration is missing' }
222
+ $previous = $model.Value.PSObject.Properties['backend']
223
+ $backend = if ($InferenceModel -eq $ModelId) {
224
+ if ($null -ne $previous -and [string]$previous.Value -in @('cpu','gpu','npu')) { ([string]$previous.Value).ToLowerInvariant() } else { 'cpu' }
225
+ } else { $InferenceModel.Substring($ModelId.Length + 1) }
226
+ if ($null -ne $previous -and [string]$previous.Value -ceq $backend) { return }
227
+ $model.Value | Add-Member -NotePropertyName backend -NotePropertyValue $backend -Force
228
+ $temporary = "$path.$PID.backend.tmp"
229
+ try {
230
+ [IO.File]::WriteAllText($temporary, ($config | ConvertTo-Json -Depth 12), [Text.UTF8Encoding]::new($false))
231
+ Move-Item -LiteralPath $temporary -Destination $path -Force -ErrorAction Stop
232
+ } finally {
233
+ if (Test-Path -LiteralPath $temporary) { Remove-Item -LiteralPath $temporary -Force -ErrorAction SilentlyContinue }
234
+ }
235
+ Log "retained verified inference backend '$backend' in $path"
236
+ }
237
+
172
238
  # $VisionBackend = "gpu" pins the vision encoder; "" leaves it on the CPU
173
239
  # default. $SpeculativeDecoding is the second engine setting this file writes.
174
240
  #
@@ -232,6 +298,7 @@ function Write-LitertlmConfig {
232
298
  # GPU encoder from one that fails on first use.
233
299
  function Test-LitertlmVision {
234
300
  param([string]$InferenceModel = "$ModelId,gpu")
301
+ $script:LitertlmVerifiedInferenceModel = ''
235
302
  try {
236
303
  $dataUri = "data:image/jpeg;base64," + $script:LitertlmProbeJpegB64
237
304
  $body = @{
@@ -251,8 +318,10 @@ function Test-LitertlmVision {
251
318
  $proof = ($resp.Content | ConvertFrom-Json).runtimeModel
252
319
  $runtimeMatches = $proof.runtimeModelId -eq $InferenceModel `
253
320
  -or ($InferenceModel -eq $ModelId -and $proof.runtimeModelId -eq "$ModelId,cpu")
254
- return ($null -ne $proof -and $proof.version -eq 1 -and $proof.format -eq "litert-lm" `
321
+ $verified = ($null -ne $proof -and $proof.version -eq 1 -and $proof.format -eq "litert-lm" `
255
322
  -and $proof.sha256 -eq $BundledModelSha256 -and $runtimeMatches)
323
+ if ($verified) { $script:LitertlmVerifiedInferenceModel = $InferenceModel }
324
+ return $verified
256
325
  } catch {
257
326
  Log "vision probe for '$InferenceModel' did not answer: $_"
258
327
  return $false
@@ -291,6 +360,10 @@ function Write-DelegateDiagnostic {
291
360
  }
292
361
 
293
362
  function Complete-Setup {
363
+ try { Set-LitertlmVerifiedBackend $script:LitertlmVerifiedInferenceModel } catch {
364
+ Fail-LocalAi "Local AI responded, but its verified runtime configuration could not be saved. Retry setup."
365
+ throw
366
+ }
294
367
  Log "local AI is healthy on :$Port"
295
368
  Write-DelegateDiagnostic
296
369
  Write-SetupProgress -Stage "ready" -Title "Camera setup is complete" -Message "Local AI is installed and healthy. Trillboards is running in the background." -Phase "verifying" -Percent 100
@@ -384,6 +457,23 @@ function Restore-ImportedModelService {
384
457
 
385
458
  # One owner for the shim on fresh installs AND upgrades. None of these helpers
386
459
  # import a model, rewrite model config, install dependencies or register tasks.
460
+ # Windows PowerShell 5.1 turns redirected native stderr into ErrorRecords.
461
+ # The adapter prints ACTIVE there on a successful import. Only the native exit
462
+ # status decides success; keep this preference local to this invocation so the
463
+ # caller's filesystem/backup verification remains terminating.
464
+ function Invoke-LitertlmShimNative {
465
+ param([string]$Executable, [string[]]$Arguments, [switch]$LogOutput)
466
+ $ErrorActionPreference = 'Continue'
467
+ # Native execution updates the automatic global status, even in a function.
468
+ $global:LASTEXITCODE = $null
469
+ if ($LogOutput) {
470
+ & $Executable @Arguments *>> $LogPath
471
+ return [pscustomobject]@{ ExitCode = $global:LASTEXITCODE; Output = @() }
472
+ }
473
+ $output = @(& $Executable @Arguments 2>$null)
474
+ return [pscustomobject]@{ ExitCode = $global:LASTEXITCODE; Output = $output }
475
+ }
476
+
387
477
  function Get-LitertlmShimPaths([string]$UvPath) {
388
478
  $shim = $null
389
479
  foreach ($candidate in @(
@@ -393,17 +483,15 @@ function Get-LitertlmShimPaths([string]$UvPath) {
393
483
  if (-not $shim) { throw 'The released litert-lm shim is missing' }
394
484
  # PS 5.1 stops a native process when Select-Object -First has its line,
395
485
  # turning a successful lookup into exit -1. Drain stdout before selecting.
396
- $toolDirOutput = @(& $UvPath tool dir 2>$null)
397
- $toolDirExit = $LASTEXITCODE
398
- $toolDir = $toolDirOutput | Select-Object -First 1
399
- if ($toolDirExit -ne 0 -or [string]::IsNullOrWhiteSpace($toolDir)) { throw 'Cannot resolve uv tool dir' }
486
+ $toolDirResult = Invoke-LitertlmShimNative -Executable $UvPath -Arguments @('tool', 'dir')
487
+ $toolDir = $toolDirResult.Output | Select-Object -First 1
488
+ if ($toolDirResult.ExitCode -ne 0 -or [string]::IsNullOrWhiteSpace($toolDir)) { throw 'Cannot resolve uv tool dir' }
400
489
  $venv = [IO.Path]::GetFullPath((Join-Path $toolDir.Trim() 'litert-lm'))
401
490
  $py = Join-Path $venv 'Scripts\python.exe'
402
491
  if (-not (Test-Path -LiteralPath $py -PathType Leaf)) { throw 'The installed litert-lm Python is missing' }
403
- $siteOutput = @(& $py -c "import sysconfig;print(sysconfig.get_paths()['purelib'])" 2>$null)
404
- $siteExit = $LASTEXITCODE
405
- $site = $siteOutput | Select-Object -First 1
406
- if ($siteExit -ne 0 -or [string]::IsNullOrWhiteSpace($site)) { throw 'Cannot resolve litert-lm site-packages' }
492
+ $siteResult = Invoke-LitertlmShimNative -Executable $py -Arguments @('-c', "import sysconfig;print(sysconfig.get_paths()['purelib'])")
493
+ $site = $siteResult.Output | Select-Object -First 1
494
+ if ($siteResult.ExitCode -ne 0 -or [string]::IsNullOrWhiteSpace($site)) { throw 'Cannot resolve litert-lm site-packages' }
407
495
  $site = [IO.Path]::GetFullPath($site.Trim())
408
496
  if (-not $site.StartsWith(($venv.TrimEnd('\') + '\'), [StringComparison]::OrdinalIgnoreCase) -or
409
497
  -not (Test-Path -LiteralPath $site -PathType Container)) { throw 'The runtime site is outside its managed venv' }
@@ -500,8 +588,8 @@ function Install-LitertlmShim {
500
588
  }
501
589
  # A fresh interpreter proves the installed wrapper imports. No engine,
502
590
  # weights, image inference, model config or camera credentials are loaded.
503
- & $paths.Python -c "import litert_lm,sys; sys.exit(0 if getattr(litert_lm.Engine.create_conversation,'_trillboards_litertlm_shim',False) else 1)" *>> $LogPath
504
- if ($LASTEXITCODE -ne 0) { throw 'The refreshed model-service shim did not import' }
591
+ $importResult = Invoke-LitertlmShimNative -Executable $paths.Python -Arguments @('-c', "import litert_lm,sys; sys.exit(0 if getattr(litert_lm.Engine.create_conversation,'_trillboards_litertlm_shim',False) else 1)") -LogOutput
592
+ if ($importResult.ExitCode -ne 0) { throw 'The refreshed model-service shim did not import' }
505
593
  Log "litert-lm fast-conversation shim active in $($paths.Site)"
506
594
  return $true
507
595
  } catch {
@@ -544,11 +632,10 @@ if (-not $haveSetupMutex) {
544
632
  exit 0
545
633
  }
546
634
  for ($attempt = 0; $attempt -lt 600 -and -not $haveSetupMutex; $attempt++) {
547
- if ((Test-ServeHealthy) -and (Test-LitertlmVision -InferenceModel "$ModelId,gpu")) {
548
- Complete-Setup
549
- $setupMutex.Dispose()
550
- exit 0
551
- }
635
+ # The owner may be switching/restarting inference backends. A waiter
636
+ # must not initialize a GPU engine or claim readiness during that work.
637
+ # Acquire ownership first; the existing version/task/image checks below
638
+ # then verify the completed configuration before taking their fast path.
552
639
  if ((Get-PartialModelBytes) -gt 0) {
553
640
  Write-DownloadProgress
554
641
  } else {
@@ -652,18 +739,27 @@ try {
652
739
  ([string]$serviceActions[0].Arguments).Equals($hiddenPrefix, [StringComparison]::OrdinalIgnoreCase)
653
740
  } catch { }
654
741
  if ($serveHealthy -and ($installedLitertLmVersion -eq $LitertLmVersion) -and $serveTaskHidden) {
655
- if (Test-LitertlmVision -InferenceModel "$ModelId,gpu") {
656
- Complete-Setup
657
- Stop-SetupLock
658
- exit 0
659
- }
660
- if (Test-LitertlmVision -InferenceModel $ModelId) {
661
- Log "local AI healthy on the previously configured full CPU fallback (slower, but serving)"
662
- Complete-Setup
663
- Stop-SetupLock
664
- exit 0
742
+ $preferredInferenceModel = Get-LitertlmPreferredInferenceModel
743
+ if ($preferredInferenceModel) {
744
+ if (Test-LitertlmVision -InferenceModel $preferredInferenceModel) {
745
+ Complete-Setup
746
+ Stop-SetupLock
747
+ exit 0
748
+ }
749
+ } else {
750
+ if (Test-LitertlmVision -InferenceModel "$ModelId,gpu") {
751
+ Complete-Setup
752
+ Stop-SetupLock
753
+ exit 0
754
+ }
755
+ if (Test-LitertlmVision -InferenceModel $ModelId) {
756
+ Log "local AI healthy on the previously configured full CPU fallback (slower, but serving)"
757
+ Complete-Setup
758
+ Stop-SetupLock
759
+ exit 0
760
+ }
665
761
  }
666
- Log "WARN: the pinned serve answers /v1/models but not a real GPU or CPU image inference; reprovisioning local AI"
762
+ Log "WARN: the pinned serve answers /v1/models but not a verified image inference on its selected backend; reprovisioning local AI"
667
763
  }
668
764
  if ($serveHealthy -and ($installedLitertLmVersion -ne $LitertLmVersion)) {
669
765
  Log "serve is healthy but litert-lm is $(if ($installedLitertLmVersion) { $installedLitertLmVersion } else { 'unknown' }) not pinned $LitertLmVersion; reinstalling and restarting the serve"
@@ -855,16 +951,9 @@ if ($haveModel) {
855
951
  $downloadPath = Join-Path $StoreDir "approved-model.tmp"
856
952
  New-Item -ItemType Directory -Force -Path $StoreDir | Out-Null
857
953
  $modelUrl = "https://huggingface.co/litert-community/gemma-4-E2B-it-litert-lm/resolve/84b6978eff6e4eea02825bc2ee4ea48579f13109/gemma-4-E2B-it.litertlm"
858
- $process = Start-Process -FilePath "curl.exe" -ArgumentList @("--fail", "--location", "--output", ('"' + $downloadPath + '"'), $modelUrl) -WindowStyle Hidden -RedirectStandardOutput $stdoutPath -RedirectStandardError $stderrPath -PassThru
859
- while (-not $process.HasExited) {
860
- Write-DownloadProgress
861
- Start-Sleep -Seconds 2
862
- $process.Refresh()
863
- }
864
- $process.WaitForExit()
865
- Log "model download exited with code $($process.ExitCode)"
866
- if ($process.ExitCode -eq 0) { $haveModel = Import-VerifiedModel $downloadPath $litertlmPath }
867
- $process.Dispose()
954
+ $downloadExitCode = Invoke-ModelDownload -Url $modelUrl -Destination $downloadPath -StdoutPath $stdoutPath -StderrPath $stderrPath
955
+ Log "model download exited with code $downloadExitCode"
956
+ if ($downloadExitCode -eq 0) { $haveModel = Import-VerifiedModel $downloadPath $litertlmPath }
868
957
  } catch {
869
958
  Log "WARN: model import process failed: $_"
870
959
  }
@@ -3,13 +3,16 @@
3
3
  behind when Task Scheduler ends its waiting hidden host. Return the original
4
4
  schedule state; callers own re-registration, restoration or task deletion.
5
5
  -Inspect verifies the same ownership without changing anything.
6
+ -InspectTaskOnly verifies task identity without native queries for enablement
7
+ recovery; native availability still requires a full inspection before start.
6
8
  #>
7
9
  [CmdletBinding()]
8
10
  param(
9
11
  [Parameter(Mandatory = $true)][ValidateSet('Model','Detector')][string]$Service,
10
12
  [string]$ToolPath = '',
11
13
  [ValidateRange(0,65535)][int]$Port = 0,
12
- [switch]$Inspect
14
+ [switch]$Inspect,
15
+ [switch]$InspectTaskOnly
13
16
  )
14
17
  $ErrorActionPreference = 'Stop'
15
18
  $toolName = if ($Service -eq 'Model') { 'litert-lm.exe' } else { 'trillboards-detector.exe' }
@@ -35,6 +38,20 @@ if ($task) {
35
38
  if ($actions.Count -ne 1) { throw 'Cannot verify the local AI task action; nothing was stopped' }
36
39
  $execute = [string]$actions[0].Execute
37
40
  $arguments = [string]$actions[0].Arguments
41
+ # Recover only the historical array-splatting corruption already recognized
42
+ # by the updater. A malformed task cannot carry a valid old serve port.
43
+ if ($Service -eq 'Detector' -and $execute -eq '-DetectorPath') {
44
+ $ownTool = [IO.Path]::GetFullPath((Join-Path $env:USERPROFILE '.local\bin\trillboards-detector.exe'))
45
+ if ($ToolPath -ine $ownTool -or $arguments -notmatch '^serve --host 127\.0\.0\.1 --port (.+?) --log-file "-RamBudgetMb" --ram-budget-mb \d+$') {
46
+ throw 'Cannot verify the malformed detector task; nothing was stopped'
47
+ }
48
+ $recovered = $Matches[1]
49
+ if (-not [IO.Path]::IsPathRooted($recovered) -or [IO.Path]::GetFullPath($recovered) -ine $ownTool -or -not (Test-Path -LiteralPath $ownTool -PathType Leaf)) {
50
+ throw 'The malformed detector task does not identify this user installation'
51
+ }
52
+ $execute = $ownTool
53
+ $arguments = 'serve --host 127.0.0.1 --port ' + $(if ($Port) { $Port } else { 9380 })
54
+ }
38
55
  $wscript = Join-Path $env:WINDIR 'System32\wscript.exe'
39
56
  if ([IO.Path]::GetFullPath($execute) -ieq [IO.Path]::GetFullPath($wscript)) {
40
57
  $prefix = '//B //NoLogo "' + (Join-Path $PSScriptRoot 'trillboards-hidden-run.vbs') + '" --wait "'
@@ -54,7 +71,97 @@ if ($task) {
54
71
  }
55
72
  if (-not $Port) { $Port = if ($Service -eq 'Model') { 9379 } else { 9380 } }
56
73
  $context = [pscustomobject]@{ TaskName=$taskName; TaskPresent=[bool]$task; Enabled=($task -and [bool]$task.Settings.Enabled); Running=($task -and $task.State -eq 'Running'); TaskState=$(if($task){[string]$task.State}else{'Absent'}); Port=$Port }
57
- $toolRoot = [IO.Path]::GetFullPath((Join-Path $env:APPDATA ('uv\tools\' + $packageName))).TrimEnd('\') + '\'
74
+ # Enablement recovery needs task ownership even when a foreign process occupies
75
+ # the port. This read-only mode never enumerates or stops native processes.
76
+ if ($InspectTaskOnly) { return $context }
77
+
78
+ # uv source formats: crates/uv-trampoline-builder/src/lib.rs (current PE
79
+ # resources and released0.8.22 EOF metadata). Read data; never execute a probe.
80
+ function Get-RecordedUvToolRuntime([string]$Launcher, [string]$PackageName) {
81
+ if (-not [IO.File]::Exists($Launcher)) { return }
82
+ if (-not ('TrillboardsUvToolMetadata' -as [type])) {
83
+ Add-Type -TypeDefinition @'
84
+ using System;
85
+ using System.IO;
86
+ using System.Text;
87
+ using System.Runtime.InteropServices;
88
+ public static class TrillboardsUvToolMetadata {
89
+ [DllImport("kernel32.dll", CharSet=CharSet.Unicode)] static extern IntPtr LoadLibraryExW(string path, IntPtr file, uint flags);
90
+ [DllImport("kernel32.dll", CharSet=CharSet.Unicode)] static extern IntPtr FindResourceW(IntPtr module, string name, IntPtr type);
91
+ [DllImport("kernel32.dll")] static extern uint SizeofResource(IntPtr module, IntPtr resource);
92
+ [DllImport("kernel32.dll")] static extern IntPtr LoadResource(IntPtr module, IntPtr resource);
93
+ [DllImport("kernel32.dll")] static extern IntPtr LockResource(IntPtr resource);
94
+ [DllImport("kernel32.dll")] static extern bool FreeLibrary(IntPtr module);
95
+ static byte[] Resource(IntPtr module, string name) {
96
+ IntPtr info=FindResourceW(module,name,new IntPtr(10)); if(info==IntPtr.Zero)return null;
97
+ uint size=SizeofResource(module,info); if(size==0||size>32768)return null;
98
+ IntPtr data=LockResource(LoadResource(module,info)); if(data==IntPtr.Zero)return null;
99
+ byte[] bytes=new byte[size]; Marshal.Copy(data,bytes,0,bytes.Length); return bytes;
100
+ }
101
+ static string Decode(byte[] bytes) {
102
+ try { return new UTF8Encoding(false,true).GetString(bytes); }
103
+ catch(DecoderFallbackException) { return null; }
104
+ }
105
+ public static string Read(string path) {
106
+ // Map PE data/resources only: no entrypoint, imports or launcher execution.
107
+ IntPtr module=LoadLibraryExW(path,IntPtr.Zero,0x22); if(module==IntPtr.Zero)return null;
108
+ try {
109
+ byte[] kind=Resource(module,"UV_TRAMPOLINE_KIND");
110
+ byte[] python=Resource(module,"UV_PYTHON_PATH");
111
+ if(kind!=null||python!=null) {
112
+ if(kind==null||kind.Length!=1||(kind[0]!=1&&kind[0]!=2)||python==null)return null;
113
+ return Decode(python);
114
+ }
115
+ } finally { FreeLibrary(module); }
116
+ // Released uv0.8.x: UTF8 interpreter + u32LE byte count + UVSC/UVPY
117
+ // at EOF. Do not search arbitrary strings or an unmerged ZIP layout.
118
+ try {
119
+ using(FileStream file=new FileStream(path,FileMode.Open,FileAccess.Read,FileShare.Read)) {
120
+ if(file.Length<9||file.Length>16*1024*1024)return null;
121
+ file.Seek(-8,SeekOrigin.End); byte[] tail=new byte[8];
122
+ if(file.Read(tail,0,tail.Length)!=tail.Length)return null;
123
+ string magic=Encoding.ASCII.GetString(tail,4,4);
124
+ if(magic!="UVSC"&&magic!="UVPY")return null;
125
+ uint length=BitConverter.ToUInt32(tail,0);
126
+ if(length==0||length>32768||length>file.Length-8)return null;
127
+ file.Seek(-8-(long)length,SeekOrigin.End); byte[] bytes=new byte[length];
128
+ if(file.Read(bytes,0,bytes.Length)!=bytes.Length)return null;
129
+ return Decode(bytes);
130
+ }
131
+ } catch(IOException) { return null; }
132
+ catch(UnauthorizedAccessException) { return null; }
133
+ }
134
+ }
135
+ '@
136
+ }
137
+ $python = [TrillboardsUvToolMetadata]::Read($Launcher)
138
+ if ([string]::IsNullOrWhiteSpace($python) -or $python.IndexOf([char]0) -ge 0) { return }
139
+ try {
140
+ if ([IO.Path]::IsPathRooted($python) -and $python -notmatch '^(?:[a-z]:[\\/]|\\\\[^\\]+\\[^\\]+\\)') { return }
141
+ if (-not [IO.Path]::IsPathRooted($python)) { $python = Join-Path ([IO.Path]::GetDirectoryName($Launcher)) $python }
142
+ $python = [IO.Path]::GetFullPath($python)
143
+ $scripts = [IO.Path]::GetDirectoryName($python)
144
+ $toolRoot = [IO.Path]::GetDirectoryName($scripts)
145
+ if ([IO.Path]::GetFileName($python) -notin @('python.exe','pythonw.exe') -or
146
+ [IO.Path]::GetFileName($scripts) -ine 'Scripts' -or [IO.Path]::GetFileName($toolRoot) -ine $PackageName) { return }
147
+ [pscustomobject]@{ Python = $python; ToolRoot = ($toolRoot.TrimEnd('\') + '\') }
148
+ } catch { return }
149
+ }
150
+
151
+ function Get-FirstCommandExecutable([string]$CommandLine) {
152
+ $first = [regex]::Match($CommandLine, '^\s*(?:"([^"]+)"|([^\s"]+))(?=\s|$)')
153
+ if (-not $first.Success) { return }
154
+ $path = if ($first.Groups[1].Success) { $first.Groups[1].Value } else { $first.Groups[2].Value }
155
+ if ($path -notmatch '^(?:[a-z]:[\\/]|\\\\[^\\]+\\[^\\]+\\)') { return }
156
+ try { [IO.Path]::GetFullPath($path) } catch { return }
157
+ }
158
+
159
+ # Resolve the recorded launcher's venv, including custom UV_TOOL_DIR installs.
160
+ # A current environment variable cannot identify a previous installation.
161
+ $recordedRuntime = Get-RecordedUvToolRuntime $ToolPath $packageName
162
+ $toolRoot = if ($recordedRuntime) { $recordedRuntime.ToolRoot } else {
163
+ [IO.Path]::GetFullPath((Join-Path $env:APPDATA ('uv\tools\' + $packageName))).TrimEnd('\') + '\'
164
+ }
58
165
  $portPattern = '(?:^|\s)--port(?:\s+|=)' + $Port + '(?=\s|$)'
59
166
 
60
167
  function Get-OwnedServiceProcesses {
@@ -69,8 +176,11 @@ function Get-OwnedServiceProcesses {
69
176
  if ($owner.ReturnValue -ne 0 -or -not $owner.Sid) { throw 'Cannot verify a local AI process owner; nothing unverified was stopped' }
70
177
  if ($owner.Sid -ne $ownerSid) { continue }
71
178
  $exe = [string]$process.ExecutablePath
179
+ # A venv redirector can report the shared base Python as ExecutablePath.
180
+ # Trust only the exact recorded interpreter in the FIRST command argument.
181
+ $recordedInterpreter = $recordedRuntime -and (Get-FirstCommandExecutable $process.CommandLine) -ieq $recordedRuntime.Python
72
182
  if (-not $exe -or ($exe -ine $ToolPath -and -not $exe.StartsWith($toolRoot, [StringComparison]::OrdinalIgnoreCase) -and
73
- $process.CommandLine.IndexOf($ToolPath, [StringComparison]::OrdinalIgnoreCase) -lt 0)) { continue }
183
+ -not $recordedInterpreter -and $process.CommandLine.IndexOf($ToolPath, [StringComparison]::OrdinalIgnoreCase) -lt 0)) { continue }
74
184
  $process
75
185
  }
76
186
  }
@@ -104,7 +214,15 @@ try {
104
214
  # identity, including creation time, so a recycled PID is never a target.
105
215
  $live = Get-CimInstance Win32_Process -Filter ('ProcessId=' + $process.ProcessId) -OperationTimeoutSec 5 -ErrorAction Stop
106
216
  if (-not $live) { continue }
107
- if ($live.CreationDate -ne $process.CreationDate -or $live.ExecutablePath -ine $process.ExecutablePath) { throw 'The local AI process identity changed; retry the operation' }
217
+ # CIM can return an incomplete record while a child is exiting. Never
218
+ # terminate that PID; the bounded outer loop takes a fresh owned snapshot.
219
+ # A known creation-time or executable change still fails closed.
220
+ if (($live.CreationDate -and $process.CreationDate -and $live.CreationDate -ne $process.CreationDate) -or
221
+ ($live.ExecutablePath -and $live.ExecutablePath -ine $process.ExecutablePath)) {
222
+ throw ("The local AI process identity changed; retry the operation (PID {0}; creation '{1}' -> '{2}'; executable '{3}' -> '{4}')" -f
223
+ $process.ProcessId, $process.CreationDate, $live.CreationDate, $process.ExecutablePath, $live.ExecutablePath)
224
+ }
225
+ if (-not $process.CreationDate -or -not $live.CreationDate -or -not $live.ExecutablePath) { continue }
108
226
  try { Stop-Process -Id $process.ProcessId -Force -ErrorAction Stop } catch {
109
227
  if (Get-CimInstance Win32_Process -Filter ('ProcessId=' + $process.ProcessId) -OperationTimeoutSec 5 -ErrorAction Stop) { throw }
110
228
  }
@@ -51,6 +51,14 @@ if not errorlevel 1 (
51
51
  echo.
52
52
  )
53
53
 
54
+ REM Existing-PC authorization runs before normal resume and never touches service/task/model setup.
55
+ if defined UI_STATUS_FILE if exist "%CFG%" (
56
+ call "%~dp0trillboards-edge.cmd" installer-onboarding --authorize-existing-if-requested --config "%CFG%" --status-file "%UI_STATUS_FILE%"
57
+ if not errorlevel 1 exit /b 0
58
+ if errorlevel 11 goto :done_fail
59
+ if not errorlevel 10 goto :done_fail
60
+ )
61
+
54
62
  REM ── Resume path: a saved, complete pairing skips the code prompt. Branch on
55
63
  REM WHY verification failed: only an auth rejection (3) or a token-less config
56
64
  REM (4) needs a new code — a network/server failure (2/5) means the saved