@bli-cockpit/cli 0.2.21 → 0.2.22

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.
package/dist/autostart.js CHANGED
@@ -8,6 +8,21 @@ export const AUTOSTART_LABEL = "com.bli.cockpit.sync";
8
8
  export const WINDOWS_AUTOSTART_TASK_NAME = "BLI Cockpit Sync";
9
9
  const WINDOWS_AUTOSTART_SCRIPT_NAME = "autostart-sync.ps1";
10
10
  const WINDOWS_AUTOSTART_REGISTRATION_SCRIPT_NAME = "autostart-register.ps1";
11
+ /**
12
+ * Windowless launcher for the scheduled sync (BLI-2677). Task Scheduler ran
13
+ * powershell.exe directly under an interactive token, and PowerShell is a
14
+ * console-subsystem binary, so every 15-minute tick flashed a conhost window
15
+ * over whatever the operator was doing \u2014 on every Windows machine in the
16
+ * fleet. wscript.exe is a GUI-subsystem host, so it never allocates a console,
17
+ * and it starts the PowerShell child with window style 0 (hidden). S4U was
18
+ * considered and rejected: it breaks on Microsoft-account logons and the task
19
+ * deliberately stays on InteractiveToken with least privilege.
20
+ */
21
+ const WINDOWS_AUTOSTART_LAUNCHER_NAME = "autostart-sync.vbs";
22
+ const WINDOWS_SYNC_LOG_NAME = "sync.log";
23
+ /** With the window hidden, the log is the only place output goes; cap it so an
24
+ * always-on machine syncing every 15 minutes cannot grow it without bound. */
25
+ const WINDOWS_SYNC_LOG_LIMIT_BYTES = 10 * 1024 * 1024;
11
26
  const UTF8_BOM = "\uFEFF";
12
27
  export const DEFAULT_AUTOSTART_INTERVAL_SECONDS = 15 * 60;
13
28
  /**
@@ -179,6 +194,7 @@ async function installWindowsTask(options) {
179
194
  const intervalMinutes = Math.max(1, Math.ceil(intervalSeconds / 60));
180
195
  const scriptPath = path.join(getCollectorRuntimePaths(homeDir).state_dir, WINDOWS_AUTOSTART_SCRIPT_NAME);
181
196
  const registrationPath = path.join(getCollectorRuntimePaths(homeDir).state_dir, WINDOWS_AUTOSTART_REGISTRATION_SCRIPT_NAME);
197
+ const launcherPath = windowsAutostartLauncherPath(homeDir);
182
198
  const nodeExecutable = path.win32.resolve(options.nodeExecutable ?? process.execPath);
183
199
  const cliEntryPoint = path.win32.resolve(options.cliEntryPoint ?? process.argv[1] ?? "");
184
200
  await mkdir(path.dirname(scriptPath), { recursive: true });
@@ -188,10 +204,18 @@ async function installWindowsTask(options) {
188
204
  dashboardUrl,
189
205
  nodeExecutable,
190
206
  cliEntryPoint,
207
+ syncLogPath: windowsSyncLogPath(homeDir),
191
208
  })}`, "utf8");
209
+ // No BOM on the launcher: wscript.exe reads .vbs files as ANSI and treats a
210
+ // UTF-8 BOM as an invalid character. The rendered content is ASCII-safe for
211
+ // every path this fleet has.
212
+ await writeFile(launcherPath, renderWindowsLauncherScript({
213
+ powershellPath: windowsPowerShellPath(),
214
+ scriptPath,
215
+ }), "utf8");
192
216
  await writeFile(registrationPath, `${UTF8_BOM}${renderWindowsRegistrationScript({
193
217
  taskName: WINDOWS_AUTOSTART_TASK_NAME,
194
- scriptPath,
218
+ launcherPath,
195
219
  intervalMinutes,
196
220
  })}`, "utf8");
197
221
  const created = await options.exec("powershell.exe", [
@@ -240,10 +264,12 @@ async function installWindowsTask(options) {
240
264
  async function uninstallWindowsTask(options) {
241
265
  const scriptPath = windowsAutostartScriptPath(options.homeDir);
242
266
  const registrationPath = windowsAutostartRegistrationScriptPath(options.homeDir);
267
+ const launcherPath = windowsAutostartLauncherPath(options.homeDir);
243
268
  const current = await windowsTaskStatus(options);
244
269
  if (current.status === "absent") {
245
270
  await rm(scriptPath, { force: true });
246
271
  await rm(registrationPath, { force: true });
272
+ await rm(launcherPath, { force: true });
247
273
  return {
248
274
  ...current,
249
275
  plist_path: scriptPath,
@@ -259,6 +285,7 @@ async function uninstallWindowsTask(options) {
259
285
  if (removed.code === 0) {
260
286
  await rm(scriptPath, { force: true });
261
287
  await rm(registrationPath, { force: true });
288
+ await rm(launcherPath, { force: true });
262
289
  }
263
290
  return {
264
291
  status: removed.code === 0 ? "uninstalled" : "not_loaded",
@@ -275,6 +302,7 @@ async function uninstallWindowsTask(options) {
275
302
  }
276
303
  async function windowsTaskStatus(options) {
277
304
  const scriptPath = windowsAutostartScriptPath(options.homeDir);
305
+ const launcherPath = windowsAutostartLauncherPath(options.homeDir);
278
306
  const query = await options.exec("schtasks.exe", [
279
307
  "/Query",
280
308
  "/TN",
@@ -304,9 +332,9 @@ async function windowsTaskStatus(options) {
304
332
  const intervalSeconds = options.intervalSeconds ?? DEFAULT_AUTOSTART_INTERVAL_SECONDS;
305
333
  const intervalMinutes = Math.max(1, Math.ceil(intervalSeconds / 60));
306
334
  const registrationProblems = windowsTaskRegistrationProblems(query.stdout, {
307
- scriptPath,
335
+ launcherPath,
308
336
  intervalMinutes,
309
- powershellPath: windowsPowerShellPath(),
337
+ wscriptPath: windowsWScriptPath(),
310
338
  });
311
339
  if (disabled)
312
340
  registrationProblems.unshift("task is disabled");
@@ -321,12 +349,32 @@ async function windowsTaskStatus(options) {
321
349
  dashboardUrl: options.dashboardUrl ?? DEFAULT_DASHBOARD_URL,
322
350
  nodeExecutable: path.win32.resolve(options.nodeExecutable ?? process.execPath),
323
351
  cliEntryPoint: path.win32.resolve(options.cliEntryPoint ?? process.argv[1] ?? ""),
352
+ syncLogPath: windowsSyncLogPath(options.homeDir),
324
353
  })}`;
325
354
  const currentScript = await readFile(scriptPath, "utf8").catch(() => null);
326
355
  if (currentScript !== expectedScript) {
327
356
  registrationProblems.push("sync script does not match the current roots or Cockpit runtime");
328
357
  }
329
358
  }
359
+ // Same shape as the sync-script checks: existence always, content only when
360
+ // the caller supplied roots (a bare `autostart status` stays permissive by
361
+ // design — see BLI-2362). The launcher's content depends on nothing but the
362
+ // PowerShell path and the sync-script path, but gating keeps the two
363
+ // commands' disagreement surface identical to the script check above.
364
+ const launcherExists = await fileExists(launcherPath);
365
+ if (!launcherExists) {
366
+ registrationProblems.push("sync launcher is missing");
367
+ }
368
+ else if (options.repoRoots && options.repoRoots.length > 0) {
369
+ const expectedLauncher = renderWindowsLauncherScript({
370
+ powershellPath: windowsPowerShellPath(),
371
+ scriptPath,
372
+ });
373
+ const currentLauncher = await readFile(launcherPath, "utf8").catch(() => null);
374
+ if (currentLauncher !== expectedLauncher) {
375
+ registrationProblems.push("sync launcher does not match the current Cockpit runtime");
376
+ }
377
+ }
330
378
  // Both branches log. A line that only fires on failure cannot answer "did
331
379
  // background collection validate at all today?", which is the question that
332
380
  // would have caught a validator rejecting every healthy Windows task.
@@ -362,12 +410,23 @@ function windowsAutostartScriptPath(homeDir) {
362
410
  function windowsAutostartRegistrationScriptPath(homeDir) {
363
411
  return path.join(getCollectorRuntimePaths(homeDir ?? os.homedir()).state_dir, WINDOWS_AUTOSTART_REGISTRATION_SCRIPT_NAME);
364
412
  }
413
+ function windowsAutostartLauncherPath(homeDir) {
414
+ return path.join(getCollectorRuntimePaths(homeDir ?? os.homedir()).state_dir, WINDOWS_AUTOSTART_LAUNCHER_NAME);
415
+ }
416
+ function windowsSyncLogPath(homeDir) {
417
+ return path.join(getCollectorRuntimePaths(homeDir ?? os.homedir()).state_dir, WINDOWS_SYNC_LOG_NAME);
418
+ }
365
419
  function renderWindowsRegistrationScript(options) {
366
420
  return [
367
421
  "$ErrorActionPreference = 'Stop'",
368
422
  `$taskName = ${powershellLiteral(options.taskName)}`,
369
- `$syncScriptPath = ${powershellLiteral(options.scriptPath)}`,
370
- "$powershellPath = Join-Path $PSHOME 'powershell.exe'",
423
+ `$launcherPath = ${powershellLiteral(options.launcherPath)}`,
424
+ // wscript.exe is the windowless script host: the task runs the .vbs
425
+ // launcher, which starts PowerShell with its window hidden, so the
426
+ // 15-minute sync never flashes a terminal (BLI-2677). //B is batch mode —
427
+ // a script error becomes a nonzero exit instead of a modal dialog that
428
+ // would hang the scheduled run.
429
+ "$wscriptPath = Join-Path (Join-Path $env:SystemRoot 'System32') 'wscript.exe'",
371
430
  // Windows PowerShell 5.1 passes a native argument's embedded quotes to
372
431
  // CommandLineToArgvW unescaped, so a /TR value with bare quotes falls out
373
432
  // of the quoted region at the first space inside a quoted path:
@@ -376,8 +435,8 @@ function renderWindowsRegistrationScript(options) {
376
435
  // Escaping the embedded quotes as \" keeps the whole /TR value one
377
436
  // argument; Task Scheduler still normalizes what it stores (BLI-2541),
378
437
  // which the read-back validator already compares path-wise.
379
- "$actionArgs = '-NoProfile -NonInteractive -ExecutionPolicy Bypass -File \\\"' + $syncScriptPath + '\\\"'",
380
- "$taskCommand = '\\\"' + $powershellPath + '\\\" ' + $actionArgs",
438
+ "$actionArgs = '//B \\\"' + $launcherPath + '\\\"'",
439
+ "$taskCommand = '\\\"' + $wscriptPath + '\\\" ' + $actionArgs",
381
440
  `& schtasks.exe /Create /TN $taskName /TR $taskCommand /SC MINUTE /MO ${options.intervalMinutes} /IT /RL LIMITED /F | Out-Null`,
382
441
  "if ($LASTEXITCODE -ne 0) { throw \"schtasks /Create exited $LASTEXITCODE\" }",
383
442
  "$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -MultipleInstances IgnoreNew",
@@ -385,6 +444,30 @@ function renderWindowsRegistrationScript(options) {
385
444
  "",
386
445
  ].join("\r\n");
387
446
  }
447
+ /**
448
+ * The .vbs launcher the scheduled task actually runs. WshShell.Run with window
449
+ * style 0 starts PowerShell with its console hidden — no flash, no focus
450
+ * steal — and waiting on the child propagates the sync exit code back to Task
451
+ * Scheduler as LastTaskResult. VBScript escapes a quote inside a string by
452
+ * doubling it; Windows paths cannot contain quotes, so the doubled quotes here
453
+ * are only the fixed delimiters around the two paths.
454
+ */
455
+ function renderWindowsLauncherScript(options) {
456
+ const quotedPowershell = `""${options.powershellPath}""`;
457
+ const quotedScript = `""${options.scriptPath}""`;
458
+ return [
459
+ "' BLI Cockpit sync launcher (BLI-2677). wscript.exe never allocates a",
460
+ "' console and starts PowerShell hidden, so the 15-minute background sync",
461
+ "' does not flash a terminal over the operator. Sync output lands in",
462
+ "' sync.log next to this file instead of a visible window.",
463
+ "Option Explicit",
464
+ "Dim windowsShell, syncExitCode",
465
+ 'Set windowsShell = CreateObject("WScript.Shell")',
466
+ `syncExitCode = windowsShell.Run("${quotedPowershell} -NoProfile -NonInteractive -ExecutionPolicy Bypass -File ${quotedScript}", 0, True)`,
467
+ "WScript.Quit syncExitCode",
468
+ "",
469
+ ].join("\r\n");
470
+ }
388
471
  function renderWindowsSyncScript(options) {
389
472
  const dashboardArgs = options.dashboardUrl === DEFAULT_DASHBOARD_URL
390
473
  ? ""
@@ -392,18 +475,29 @@ function renderWindowsSyncScript(options) {
392
475
  const discoveryArgs = options.discoveryArgs.length > 0 ? ` ${options.discoveryArgs.join(" ")}` : "";
393
476
  const commands = options.workDirs.flatMap((root) => [
394
477
  "try {",
395
- ` & $nodeExecutable $cliEntryPoint sync --workspace ${powershellLiteral(root)}${dashboardArgs}${discoveryArgs} --json`,
478
+ ` & $nodeExecutable $cliEntryPoint sync --workspace ${powershellLiteral(root)}${dashboardArgs}${discoveryArgs} --json 2>&1 | ForEach-Object { "$_" } | Add-Content -LiteralPath $syncLogPath -Encoding UTF8`,
396
479
  " if ($LASTEXITCODE -ne 0) { $exitCode = $LASTEXITCODE }",
397
480
  "} catch {",
398
- " [Console]::Error.WriteLine($_.Exception.Message)",
481
+ " Add-Content -LiteralPath $syncLogPath -Encoding UTF8 -Value $_.Exception.Message",
399
482
  " $exitCode = 1",
400
483
  "}",
401
484
  ]);
402
485
  return [
403
- "$ErrorActionPreference = 'Stop'",
486
+ // 'Continue', not 'Stop': the collector logs to stderr on success as well
487
+ // as failure (that is the repo's logging contract), and Windows PowerShell
488
+ // 5.1 under 'Stop' turns the first redirected stderr line of a native
489
+ // command into a terminating NativeCommandError — 'Stop' would kill every
490
+ // healthy run at its first log line. Failures are tracked by exit code.
491
+ "$ErrorActionPreference = 'Continue'",
404
492
  "$exitCode = 0",
405
493
  `$nodeExecutable = ${powershellLiteral(options.nodeExecutable)}`,
406
494
  `$cliEntryPoint = ${powershellLiteral(options.cliEntryPoint)}`,
495
+ // The task window is hidden (BLI-2677), so this log is the only place the
496
+ // sync's output exists; sync.err.log stays macOS-only because launchd does
497
+ // stream separation for free and PowerShell 5.1 does not.
498
+ `$syncLogPath = ${powershellLiteral(options.syncLogPath)}`,
499
+ `$syncLogFile = Get-Item -LiteralPath $syncLogPath -ErrorAction SilentlyContinue`,
500
+ `if ($syncLogFile -and $syncLogFile.Length -gt ${WINDOWS_SYNC_LOG_LIMIT_BYTES}) { Set-Content -LiteralPath $syncLogPath -Value '' -Encoding UTF8 }`,
407
501
  ...commands,
408
502
  "exit $exitCode",
409
503
  "",
@@ -462,7 +556,7 @@ function windowsTaskRegistrationProblems(taskXml, expected) {
462
556
  const actionArguments = exactExecBlock
463
557
  .match(/<Arguments>\s*([^<]*?)\s*<\/Arguments>/iu)?.[1]
464
558
  ?.trim() ?? "";
465
- const expectedArgumentFlags = "-NoProfile -NonInteractive -ExecutionPolicy Bypass -File";
559
+ const expectedArgumentFlags = "//B";
466
560
  const requirements = [
467
561
  [
468
562
  /<DisallowStartIfOnBatteries>\s*false\s*<\/DisallowStartIfOnBatteries>/iu,
@@ -531,10 +625,10 @@ function windowsTaskRegistrationProblems(taskXml, expected) {
531
625
  if (!exactExecBlock) {
532
626
  problems.push("task must contain exactly one Exec action");
533
627
  }
534
- if (!sameWindowsPath(command, expected.powershellPath)) {
535
- problems.push("task action does not run the expected Windows PowerShell");
628
+ if (!sameWindowsPath(command, expected.wscriptPath)) {
629
+ problems.push("task action does not run the expected windowless script host");
536
630
  }
537
- const actionArgumentProblem = windowsActionArgumentProblem(actionArguments, expectedArgumentFlags, expected.scriptPath);
631
+ const actionArgumentProblem = windowsActionArgumentProblem(actionArguments, expectedArgumentFlags, expected.launcherPath);
538
632
  if (actionArgumentProblem) {
539
633
  problems.push(actionArgumentProblem);
540
634
  }
@@ -547,30 +641,34 @@ function windowsPowerShellPath(env = process.env) {
547
641
  const windowsRoot = env.SystemRoot ?? env.WINDIR ?? "C:\\Windows";
548
642
  return path.win32.join(windowsRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
549
643
  }
644
+ function windowsWScriptPath(env = process.env) {
645
+ const windowsRoot = env.SystemRoot ?? env.WINDIR ?? "C:\\Windows";
646
+ return path.win32.join(windowsRoot, "System32", "wscript.exe");
647
+ }
550
648
  /**
551
649
  * Task Scheduler stores a normalized form of the action it was given, not the
552
- * string we passed: `schtasks /Create /TR "<powershell> <args>"` is split into
650
+ * string we passed: `schtasks /Create /TR "<host> <args>"` is split into
553
651
  * <Command> plus <Arguments>, and the quotes around a space-free script path
554
652
  * are dropped. Comparing <Arguments> against the exact string we built
555
653
  * therefore fails on a task that is registered perfectly — observed on
556
654
  * DESKTOP-G2UO1GK (CLI 0.2.13, 2026-08-12), where the stored value differed
557
- * from the expected one only by those quotes.
655
+ * from the expected one only by those quotes (BLI-2541).
558
656
  *
559
- * Compare the flags exactly, because those are ours, and compare the script as
560
- * a path, because that is what Windows normalizes. Each failure returns its own
561
- * reason so a repair message says which half is wrong rather than "arguments
562
- * differ".
657
+ * Compare the flags exactly, because those are ours, and compare the launcher
658
+ * as a path, because that is what Windows normalizes. Each failure returns its
659
+ * own reason so a repair message says which half is wrong rather than
660
+ * "arguments differ".
563
661
  */
564
- function windowsActionArgumentProblem(actionArguments, expectedFlags, expectedScriptPath) {
662
+ function windowsActionArgumentProblem(actionArguments, expectedFlags, expectedLauncherPath) {
565
663
  if (!actionArguments.startsWith(expectedFlags)) {
566
- return "task action does not run PowerShell with the expected Cockpit sync flags";
664
+ return "task action does not run the launcher with the expected batch-mode flag";
567
665
  }
568
- const scriptArgument = unquoteWindowsArgument(actionArguments.slice(expectedFlags.length));
569
- if (!scriptArgument) {
570
- return "task action names no sync script to run";
666
+ const launcherArgument = unquoteWindowsArgument(actionArguments.slice(expectedFlags.length));
667
+ if (!launcherArgument) {
668
+ return "task action names no sync launcher to run";
571
669
  }
572
- if (!sameWindowsPath(scriptArgument, expectedScriptPath)) {
573
- return "task action runs a script other than the Cockpit sync script";
670
+ if (!sameWindowsPath(launcherArgument, expectedLauncherPath)) {
671
+ return "task action runs a script other than the Cockpit sync launcher";
574
672
  }
575
673
  return null;
576
674
  }
@@ -15,7 +15,7 @@ export async function runCockpitCli(argv, io) {
15
15
  }
16
16
 
17
17
  if (command === "--version" || command === "-V" || command === "version") {
18
- writeLine(io?.stdout ?? process.stdout, "0.2.21");
18
+ writeLine(io?.stdout ?? process.stdout, "0.2.22");
19
19
  return 0;
20
20
  }
21
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bli-cockpit/cli",
3
- "version": "0.2.21",
3
+ "version": "0.2.22",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "bin": {