Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion libs/cua-driver-rs/crates/cua-driver/src/autostart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ mod platform {
/// install.ps1 surfaces any divergence; the moment install.ps1 changes
/// shape, this script needs the same edit.
///
/// **Hidden-console launch (issue #1645):** the task action wraps
/// `cua-driver.exe serve` in `powershell -WindowStyle Hidden` +
/// `Start-Process -WindowStyle Hidden`. Without this, Task Scheduler
/// allocates a new CUI console window (because `cua-driver.exe` is a
/// console-subsystem binary) that stays visible on the user's desktop
/// for the lifetime of the daemon. The PowerShell wrapper exits
/// immediately after spawning the child, leaving only `cua-driver.exe`
/// in the process tree.
///
/// **RunLevel = Highest** (since 2026-05-21): the daemon is registered to
/// run at the user's elevated/admin token rather than the filtered
/// standard-user token. This is what lets the daemon drive UWP /
Expand Down Expand Up @@ -145,7 +154,18 @@ if ($env:USERDOMAIN -and $env:USERDOMAIN -ne 'WORKGROUP' -and $env:USERDOMAIN -n
$domain = $env:COMPUTERNAME
}
$user = "$domain\$env:USERNAME"
$action = New-ScheduledTaskAction -Execute $env:CUA_DRIVER_AS_EXE -Argument 'serve' -WorkingDirectory $env:USERPROFILE
# Use a hidden PowerShell wrapper as the task action so Windows never
# allocates a visible console window when the daemon is launched at
# logon. cua-driver.exe is a CUI (console-subsystem) binary: without
# this wrapper, Task Scheduler allocates a new console and the window
# stays on the user's desktop for the lifetime of the daemon (issue #1645).
# Start-Process -WindowStyle Hidden spawns the child fully detached;
# the powershell.exe wrapper exits immediately after, leaving only the
# cua-driver.exe daemon in the process tree.
$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
Comment on lines +165 to +168

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

🧩 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:


🏁 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 -S

Repository: 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.

Suggested change
$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.

$trigger = New-ScheduledTaskTrigger -AtLogOn -User $user
$principal = New-ScheduledTaskPrincipal -UserId $user -LogonType Interactive -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1) -ExecutionTimeLimit (New-TimeSpan -Hours 0)
Expand Down