Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ This changelog is automatically generated by AI when releases are published.

## [Unreleased]

### Fixed

- **Upgrading on Windows while the background service runs works, and a failed upgrade says so.** Windows does not replace a program that is running, and the background service runs `autter.exe`. An npm upgrade (`npm install -g @autter/cli@<version>`) then failed with EPERM, kept the old version and still exited 0; `autter upgrade` ran the installer in the background and exited 0 before it knew the result. Now the npm package, `autter upgrade` and `install.ps1` stop the service first (as `autter bg shutdown` does), rename a binary that is still in use to `autter.exe.old` and put the new one in its place, and start the service again on the new binary if it was running. The `.old` file is deleted once nothing runs it, by the next install or when the service starts. `autter upgrade` on Windows now waits for the installer and exits with its result.
- If the binary still cannot be replaced (for example, another program holds it open), the install exits non-zero, says the file is in use and to run `autter bg shutdown` and try again, and leaves the previous version installed and working.
- On macOS and Linux nothing changes, except that the npm install now fails instead of passing when it cannot move the new binary into place.

## [3.9.1] - 2026-11-13

### Fixed
Expand Down
194 changes: 123 additions & 71 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ $dryRun = ($env:AUTTER_INSTALL_DRY_RUN -eq '1')
# no operator that chains them.
$NpmFallbackHint = "This is usually temporary: wait a minute and run the installer again.`nIf it keeps failing on this network, install with npm instead (needs Node.js 18+):`n npm install -g @autter/cli`n autter onboard"

# Set when the background service was running and was stopped to replace
# its binary: it is started again at the end, on whatever binary is then in
# place (also when the install fails and the previous binary stays).
$serviceWasRunning = $false

function Start-DaemonIfRequested {
if ($env:AUTTER_RESTART_DAEMON_AFTER_INSTALL -ne '1') {
if ($env:AUTTER_RESTART_DAEMON_AFTER_INSTALL -ne '1' -and -not $serviceWasRunning) {
return
}
# A dry run starts nothing, also when it stops on an error.
Expand Down Expand Up @@ -137,80 +142,122 @@ function Stop-AutterBackgroundService {
}
}

function Get-AutterManagedProcesses {
function Wait-ForFileAvailable {
param(
[Parameter(Mandatory = $true)][string]$InstallDir
)

$targetPaths = @(
(Normalize-PathString (Join-Path $InstallDir 'autter.exe')),
(Normalize-PathString (Join-Path $InstallDir 'git.exe'))
[Parameter(Mandatory = $true)][string]$Path,
[Parameter(Mandatory = $false)][int]$MaxWaitSeconds = 10
)

$processes = @(Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {
$_.ProcessId -ne $PID -and
$_.ExecutablePath -and
($targetPaths -contains (Normalize-PathString $_.ExecutablePath))
})

return $processes
$deadline = (Get-Date).AddSeconds($MaxWaitSeconds)
while ((Get-Date) -lt $deadline) {
if (Test-FileAvailable -Path $Path) {
return $true
}
Start-Sleep -Milliseconds 250
}
return (Test-FileAvailable -Path $Path)
}

function Stop-AutterManagedProcesses {
# Stop the background service before its binary is replaced, the way
# `autter bg shutdown` does, and wait for it to let go of the file. Returns
# whether a service was running, so it is started again afterwards. A file
# that stays held is not fatal: Install-ExecutableFile renames it aside.
function Stop-ServiceForReplace {
param(
[Parameter(Mandatory = $true)][string]$InstallDir
[Parameter(Mandatory = $true)][string]$AutterExe
)

$processes = @(Get-AutterManagedProcesses -InstallDir $InstallDir)
if ($processes.Count -eq 0) {
if (-not (Test-Path -LiteralPath $AutterExe)) {
return $false
}
$wasRunning = Stop-AutterBackgroundService -AutterExe $AutterExe
if ($wasRunning) {
Write-Host 'Stopped the autter background service to replace its binary.'
if (-not (Wait-ForFileAvailable -Path $AutterExe -MaxWaitSeconds 15)) {
[void](Stop-AutterBackgroundService -AutterExe $AutterExe -Hard)
[void](Wait-ForFileAvailable -Path $AutterExe -MaxWaitSeconds 5)
}
}
return $wasRunning
}

$pids = @($processes | Sort-Object ProcessId -Unique | Select-Object -ExpandProperty ProcessId)
Write-Warning ("Stopping lingering autter processes: {0}" -f ($pids -join ', '))
# Put $Source at $Destination. Windows does not replace or delete an .exe that
# a running process has open (the background service, a git command run
# through autter, `autter upgrade` itself), but it does rename one. So the
# file in place is renamed aside to <name>.old (or <name>.<pid>.old) and the
# new one moved in; if that fails, the old one is moved back. The aside file
# is deleted by Remove-LeftoverExecutables once nothing runs it.
# Returns @{ Error = $null or why it failed; Aside = $null or the aside path }.
function Install-ExecutableFile {
param(
[Parameter(Mandatory = $true)][string]$Source,
[Parameter(Mandatory = $true)][string]$Destination
)

foreach ($managedPid in $pids) {
$aside = $null
if (Test-Path -LiteralPath $Destination) {
$aside = "$Destination.old"
if (Test-Path -LiteralPath $aside) {
try {
Remove-Item -LiteralPath $aside -Force -ErrorAction Stop
} catch {
$aside = "$Destination.$PID.old"
}
}
try {
Stop-Process -Id $managedPid -Force -ErrorAction Stop
} catch { }
[IO.File]::Move($Destination, $aside)
} catch {
return [PSCustomObject]@{ Error = $_.Exception.Message; Aside = $null }
}
}

return $true
try {
[IO.File]::Move($Source, $Destination)
} catch {
$reason = $_.Exception.Message
if ($aside) {
try {
[IO.File]::Move($aside, $Destination)
} catch {
$reason += " The previous file could not be put back either: rename $aside to $Destination."
}
}
return [PSCustomObject]@{ Error = $reason; Aside = $null }
}
return [PSCustomObject]@{ Error = $null; Aside = $aside }
}

function Wait-ForFileAvailable {
# Put the previous binary back after the new one failed its check.
function Restore-PreviousExecutable {
param(
[Parameter(Mandatory = $true)][string]$Path,
[Parameter(Mandatory = $true)][string]$InstallDir,
[Parameter(Mandatory = $false)][int]$MaxWaitSeconds = 300,
[Parameter(Mandatory = $false)][int]$RetryIntervalSeconds = 5,
[Parameter(Mandatory = $false)][int]$ForceKillAfterSeconds = 20
[Parameter(Mandatory = $false)][string]$Aside,
[Parameter(Mandatory = $true)][string]$Destination
)

$elapsed = 0
$autterExe = Join-Path $InstallDir 'autter.exe'

[void](Stop-AutterBackgroundService -AutterExe $autterExe)

while ($elapsed -lt $MaxWaitSeconds) {
if (Test-FileAvailable -Path $Path) {
return $true
}
if (-not $Aside -or -not (Test-Path -LiteralPath $Aside)) {
return $false
}
try {
Remove-Item -LiteralPath $Destination -Force -ErrorAction SilentlyContinue
[IO.File]::Move($Aside, $Destination)
return $true
} catch {
return $false
}
}

if ($elapsed -ge $ForceKillAfterSeconds) {
[void](Stop-AutterBackgroundService -AutterExe $autterExe -Hard)
[void](Stop-AutterManagedProcesses -InstallDir $InstallDir)
}
# Delete binaries renamed aside by earlier installs that nothing runs any
# more. One still running stays until the next install (the background
# service also deletes them when it starts).
function Remove-LeftoverExecutables {
param(
[Parameter(Mandatory = $true)][string]$InstallDir
)

if (-not (Test-FileAvailable -Path $Path)) {
if ($elapsed -eq 0) {
Write-Host "Waiting for file to be available: $Path" -ForegroundColor Yellow
}
Start-Sleep -Seconds $RetryIntervalSeconds
$elapsed += $RetryIntervalSeconds
foreach ($pattern in @('autter.exe.*old', 'git.exe.*old')) {
foreach ($leftover in @(Get-ChildItem -LiteralPath $InstallDir -Filter $pattern -File -ErrorAction SilentlyContinue)) {
try { Remove-Item -LiteralPath $leftover.FullName -Force -ErrorAction Stop } catch { }
}
}
return $false
}

function Verify-Checksum {
Expand Down Expand Up @@ -511,7 +558,7 @@ if ($dryRun) {
}

if (Test-Path -LiteralPath $dryExe) {
[void]$drySteps.Add("replace $dryExe (the autter background service is stopped first, so the file is free)")
[void]$drySteps.Add("replace $dryExe (the autter background service is stopped first and started again afterwards; a file still in use is renamed aside to autter.exe.old)")
} else {
[void]$drySteps.Add("install it as $dryExe")
}
Expand Down Expand Up @@ -917,15 +964,15 @@ Verify-Checksum -File $tmpFile -BinaryName $downloadedBinaryName

$finalExe = Join-Path $installDir 'autter.exe'

# Wait for autter.exe to be available if it exists and is in use
if (Test-Path -LiteralPath $finalExe) {
if (-not (Wait-ForFileAvailable -Path $finalExe -InstallDir $installDir -MaxWaitSeconds 300 -RetryIntervalSeconds 5)) {
Remove-Item -Force -ErrorAction SilentlyContinue $tmpFile
Write-ErrorAndExit "Timeout waiting for $finalExe to be available. Please close any running autter processes and try again."
}
}
# Stop the background service first; it is started again (on the new
# binary) by Start-DaemonIfRequested below.
$serviceWasRunning = Stop-ServiceForReplace -AutterExe $finalExe

Move-Item -Force -Path $tmpFile -Destination $finalExe
$swap = Install-ExecutableFile -Source $tmpFile -Destination $finalExe
if ($swap.Error) {
Remove-Item -Force -ErrorAction SilentlyContinue $tmpFile
Write-ErrorAndExit ("Could not replace {0}: the file is in use ({1})`nThe autter background service, a git command run through autter, or another program (such as an antivirus scan) still has it open, and Windows does not replace a running program.`nThe previous autter is still installed and works.`nFix: run 'autter bg shutdown', then run the installer again." -f $finalExe, $swap.Error)
}
try { Unblock-File -Path $finalExe -ErrorAction SilentlyContinue } catch { }
# This installer now owns the binary: drop the npm package's marker, so the
# background service updates it again and `autter upgrade` stops pointing at npm.
Expand All @@ -941,25 +988,29 @@ try {
} catch {
$versionError = $_.Exception.Message
}
if ($versionError) {
if ($versionError -or $versionExitCode -ne 0 -or [string]::IsNullOrWhiteSpace($installedVersion)) {
$versionDetail = if ($versionError) { " $versionError" } else { "`n$installedVersion" }
Remove-Item -Force -ErrorAction SilentlyContinue $finalExe
Write-ErrorAndExit "The autter binary could not run on this system: $versionError"
}
if ($versionExitCode -ne 0 -or [string]::IsNullOrWhiteSpace($installedVersion)) {
Remove-Item -Force -ErrorAction SilentlyContinue $finalExe
Write-ErrorAndExit "The autter binary could not run on this system:`n$installedVersion"
if (Restore-PreviousExecutable -Aside $swap.Aside -Destination $finalExe) {
$versionDetail += "`nThe previous autter is still installed."
}
Write-ErrorAndExit "The autter binary could not run on this system:$versionDetail"
}
Write-Host "Installed autter $installedVersion"

# Refresh git.exe for existing wrapper users (it's a copy, not a symlink on Windows)
$gitShim = Join-Path $installDir 'git.exe'
if (Test-Path -LiteralPath $gitShim) {
if (-not (Wait-ForFileAvailable -Path $gitShim -InstallDir $installDir -MaxWaitSeconds 300 -RetryIntervalSeconds 5)) {
Write-ErrorAndExit "Timeout waiting for $gitShim to be available. Please close any running git processes and try again."
$shimTmp = Join-Path $installDir "git.tmp.$PID.exe"
Copy-Item -Force -Path $finalExe -Destination $shimTmp
$shimSwap = Install-ExecutableFile -Source $shimTmp -Destination $gitShim
if ($shimSwap.Error) {
Remove-Item -Force -ErrorAction SilentlyContinue $shimTmp
Write-ErrorAndExit ("autter.exe was updated, but {0} could not be replaced: the file is in use ({1})`nA git command run through autter is probably still running. The previous git.exe still works.`nFix: let running git commands finish, run 'autter bg shutdown', then run the installer again." -f $gitShim, $shimSwap.Error)
}
Copy-Item -Force -Path $finalExe -Destination $gitShim
try { Unblock-File -Path $gitShim -ErrorAction SilentlyContinue } catch { }
}
Remove-LeftoverExecutables -InstallDir $installDir

# Login user with install token if provided
$needLogin = $false
Expand All @@ -978,7 +1029,8 @@ if ($env:INSTALL_NONCE -and $env:API_BASE) {
Write-Host 'IDE/agent hooks and git capture are configured during onboard (with consent)...'
Write-Host " Run: $finalExe onboard"

# Best-effort restart only for daemon-initiated self-updates.
# Start the background service again if it was running before (or for the
# service's own self-update), now on the new binary. Best effort.
Start-DaemonIfRequested

$skipPathUpdate = $env:AUTTER_SKIP_PATH_UPDATE -eq '1'
Expand Down
5 changes: 4 additions & 1 deletion npm/bin/autter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ async function main() {
} catch (err) {
if (binaryExists) {
// The binary from before still runs; install this version next time.
console.error(`autter: could not install this package's version of the binary (${err.message}); running the one already installed`);
const [first, ...rest] = String(err.message).split('\n');
console.error(`autter: could not install this package's version of the binary: ${first}`);
for (const line of rest) console.error(`autter: ${line}`);
console.error('autter: running the one already installed');
} else {
console.error(`autter: could not install the native binary: ${err.message}`);
console.error('autter: alternative installer: curl -fsSL https://api.autter.dev/install.sh | bash');
Expand Down
Loading
Loading