fix(autostart): hide console window at logon via PowerShell wrapper - #1654
Conversation
Fixes trycua#1645. cua-driver.exe is a CUI (console-subsystem) binary. When Task Scheduler launches it at logon without a parent console, Windows allocates a new console window that stays visible on the desktop for the daemon lifetime. Replace the direct task action with a PowerShell hidden wrapper: Before: Action: cua-driver.exe serve After: Action: powershell.exe -NoProfile -WindowStyle Hidden -NonInteractive -Command "Start-Process -FilePath <exe> -ArgumentList 'serve' -WindowStyle Hidden -WorkingDirectory <home>" Start-Process -WindowStyle Hidden spawns the daemon fully detached with no visible window. The powershell.exe wrapper exits immediately after, leaving only cua-driver.exe in the process tree. No flash, no console.
|
@nishantpurohit04 is attempting to deploy a commit to the Cua Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThe PR updates the Windows autostart registration script to hide the daemon console window on interactive logon. The scheduled task action is changed from launching ChangesWindows autostart hidden-console wrapper
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@libs/cua-driver-rs/crates/cua-driver/src/autostart.rs`:
- Around line 165-168: The PowerShell snippet used to create the scheduled task
wraps $env:CUA_DRIVER_AS_EXE and $env:USERPROFILE in single quotes so they are
not expanded; change the nested Start-Process invocation in the string (the line
building the New-ScheduledTaskAction command) to use double quotes around the
environment variables (e.g. -FilePath "$env:CUA_DRIVER_AS_EXE" and
-WorkingDirectory "$env:USERPROFILE") so PowerShell expands them at runtime, and
update the Rust string escaping accordingly so the inner double quotes are
properly escaped in the autostart code that constructs the scheduled-task
action.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: b8230c17-28b9-4ba5-ae4b-65f545f3fb4d
📒 Files selected for processing (1)
libs/cua-driver-rs/crates/cua-driver/src/autostart.rs
| $action = New-ScheduledTaskAction ` | ||
| -Execute 'powershell.exe' ` | ||
| -Argument "-NoProfile -WindowStyle Hidden -NonInteractive -Command `"Start-Process -FilePath '$env:CUA_DRIVER_AS_EXE' -ArgumentList 'serve' -WindowStyle Hidden -WorkingDirectory '$env:USERPROFILE'`"" ` | ||
| -WorkingDirectory $env:USERPROFILE |
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
In PowerShell, are environment variables like $env:CUA_DRIVER_AS_EXE and $env:USERPROFILE expanded inside single-quoted strings, or only in double-quoted/unquoted arguments?
💡 Result:
In PowerShell, environment variables like $env:CUA_DRIVER_AS_EXE and $env:USERPROFILE are not expanded inside single-quoted strings [1][2]. Single-quoted strings in PowerShell are treated as verbatim strings, meaning no variable substitution or expression evaluation occurs; the content is passed exactly as written [1][2][3]. Conversely, environment variables are expanded inside double-quoted strings because these are defined as expandable strings [1][2]. Additionally, when passing arguments to commands in PowerShell, unquoted arguments are also subject to expansion and interpretation [1]. If you need to include the value of an environment variable within a single-quoted context, you must use other methods such as string concatenation (e.g., '$' + $env:USERPROFILE + '$') or the format operator (e.g., "'{0}'" -f $env:USERPROFILE) [4].
Citations:
- 1: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.6
- 2: https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/7.4/Microsoft.PowerShell.Core/About/about_Quoting_Rules.md
- 3: https://www.red-gate.com/simple-talk/sysadmin/powershell/when-to-quote-in-powershell/
- 4: https://stackoverflow.com/questions/53241654/expand-variable-and-add-results-in-single-quotes
🏁 Script executed:
#!/bin/bash
set -euo pipefail
FILE="libs/cua-driver-rs/crates/cua-driver/src/autostart.rs"
nl -ba "$FILE" | sed -n '140,190p'Repository: trycua/cua
Length of output: 97
🏁 Script executed:
#!/bin/bash
set -euo pipefail
FILE="libs/cua-driver-rs/crates/cua-driver/src/autostart.rs"
# Print with line numbers (cat -n should exist)
cat -n "$FILE" | sed -n '150,190p'Repository: trycua/cua
Length of output: 3108
🏁 Script executed:
#!/bin/bash
set -euo pipefail
rg -n "Start-Process -FilePath" libs/cua-driver-rs/crates/cua-driver/src/autostart.rs libs/cua-driver-rs/crates/cua-driver/src -SRepository: trycua/cua
Length of output: 1020
Fix PowerShell scheduled-task quoting so $env:* expands in autostart.
In libs/cua-driver-rs/crates/cua-driver/src/autostart.rs line 167, $env:CUA_DRIVER_AS_EXE and $env:USERPROFILE are wrapped in single quotes inside the nested Start-Process -Command, so PowerShell won’t expand them; the task will pass literal $env:* text instead of real paths.
🐛 Proposed fix
$action = New-ScheduledTaskAction `
-Execute 'powershell.exe' `
- -Argument "-NoProfile -WindowStyle Hidden -NonInteractive -Command `"Start-Process -FilePath '$env:CUA_DRIVER_AS_EXE' -ArgumentList 'serve' -WindowStyle Hidden -WorkingDirectory '$env:USERPROFILE'`"" `
+ -Argument "-NoProfile -WindowStyle Hidden -NonInteractive -Command `"Start-Process -FilePath `"$env:CUA_DRIVER_AS_EXE`" -ArgumentList 'serve' -WindowStyle Hidden -WorkingDirectory `"$env:USERPROFILE`"`"" `
-WorkingDirectory $env:USERPROFILE📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| $action = New-ScheduledTaskAction ` | |
| -Execute 'powershell.exe' ` | |
| -Argument "-NoProfile -WindowStyle Hidden -NonInteractive -Command `"Start-Process -FilePath '$env:CUA_DRIVER_AS_EXE' -ArgumentList 'serve' -WindowStyle Hidden -WorkingDirectory '$env:USERPROFILE'`"" ` | |
| -WorkingDirectory $env:USERPROFILE | |
| $action = New-ScheduledTaskAction ` | |
| -Execute 'powershell.exe' ` | |
| -Argument "-NoProfile -WindowStyle Hidden -NonInteractive -Command `"Start-Process -FilePath `"$env:CUA_DRIVER_AS_EXE`" -ArgumentList 'serve' -WindowStyle Hidden -WorkingDirectory `"$env:USERPROFILE`"`"" ` | |
| -WorkingDirectory $env:USERPROFILE |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@libs/cua-driver-rs/crates/cua-driver/src/autostart.rs` around lines 165 -
168, The PowerShell snippet used to create the scheduled task wraps
$env:CUA_DRIVER_AS_EXE and $env:USERPROFILE in single quotes so they are not
expanded; change the nested Start-Process invocation in the string (the line
building the New-ScheduledTaskAction command) to use double quotes around the
environment variables (e.g. -FilePath "$env:CUA_DRIVER_AS_EXE" and
-WorkingDirectory "$env:USERPROFILE") so PowerShell expands them at runtime, and
update the Rust string escaping accordingly so the inner double quotes are
properly escaped in the autostart code that constructs the scheduled-task
action.
|
Tested empirically — CodeRabbit's quoting concern is a false positive in this case. The Verified on a clean Windows 11 24H2 VM. After Paths expanded correctly.
Merging — thanks for the patch @hippoley! |
…ry install when it already exists (#1685) Symptom: user upgrades cua-driver (e.g. via install-local.ps1 to pick up the hidden-console wrapper from #1654), then runs `cua-driver autostart kick`, then sees a visible console window again. Repro confirmed: the scheduled task's <Command> path stayed hard-pointed at the previous release-install dir (a binary lacking the wrapper code), so kick spawned the OLD binary. Both install.ps1 and install-local.ps1 only re-registered the task when -AutoStart was passed. Users on the upgrade path don't pass it (they're not opting INTO autostart - they already have it). Fix: both scripts now sniff for an existing `cua-driver-serve` task post-install and re-register it pointing at the freshly-installed binary, even without -AutoStart. The re-register is idempotent and covers the upgrade case explicitly. If no task is registered, nothing changes (still need -AutoStart to opt in initially). End-to-end: user runs install-local.ps1, the just-built binary's REGISTER_PS produces the wrapped task action, kick now spawns hidden. Replaced em-dashes in added comments with ASCII hyphens - the file gets rewritten as UTF-8 on edit, but PS 5.1's parser had read older em-dashes in the same file as windows-1252 successfully (mixed- encoding), and treating new bytes as UTF-8 surfaced unterminated- string errors. Stay on ASCII in newly-added blocks to avoid drift.
Problem
The
cua-driver-serveScheduled Task launches a visible console window at every interactive logon (issue #1645).cua-driver.exeis a CUI binary — without a parent console, Task Scheduler allocates one and it stays on the desktop for the daemon lifetime.Fix
Implement Option 2 from the issue: wrap the task action in a hidden PowerShell launcher.
Before:
After:
Start-Process -WindowStyle Hiddenspawns the daemon fully detached. Thepowershell.exewrapper exits immediately, leaving onlycua-driver.exein the process tree. No visible window, no flash.Fixes #1645
Summary by CodeRabbit
Bug Fixes
Documentation