From adf9732f96a99b0faeffa1e0cf6dcebc0adb71d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20CORTIER?= Date: Wed, 2 Dec 2026 05:28:21 +0900 Subject: [PATCH 1/2] ci: remove the probe directories the Agent service locks down The standard-user probe failed at cleanup with "Access to the path ...\config\identity is denied" when the Agent service had created its identity directory before the probe stopped it: the service gives that directory to LocalSystem alone, so the elevated runner account could not delete it, and whether it existed depended on how far the service got. Cleanup now removes what the runner account cannot through a one-off scheduled task running as LocalSystem. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- ci/agent-service-standard-user-probe.ps1 | 33 +++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/ci/agent-service-standard-user-probe.ps1 b/ci/agent-service-standard-user-probe.ps1 index f1876fb64..b8b3721f1 100644 --- a/ci/agent-service-standard-user-probe.ps1 +++ b/ci/agent-service-standard-user-probe.ps1 @@ -394,6 +394,28 @@ function Get-DirectorySnapshot { ForEach-Object { "$($_.FullName)|$($_.Length)|$($_.LastWriteTimeUtc.Ticks)" }) -join "`n" } +# Removes a directory tree through a one-off scheduled task running as LocalSystem. +function Remove-DirectoryAsSystem { + param([string] $Path) + + $taskName = "agent-service-probe-cleanup-$([guid]::NewGuid().ToString('N'))" + $action = New-ScheduledTaskAction -Execute (Join-Path $env:SystemRoot "System32\cmd.exe") -Argument "/d /c rd /s /q `"$Path`"" + $principal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest + Register-ScheduledTask -TaskName $taskName -Action $action -Principal $principal -ErrorAction Stop | Out-Null + try { + Start-ScheduledTask -TaskName $taskName -ErrorAction Stop + $deadline = [DateTime]::UtcNow.AddSeconds(60) + while ((Test-Path -LiteralPath $Path) -and [DateTime]::UtcNow -lt $deadline) { + Start-Sleep -Milliseconds 250 + } + } finally { + Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue + } + if (Test-Path -LiteralPath $Path) { + throw "LocalSystem did not remove '$Path' within 60 seconds" + } +} + function Invoke-ServiceProbe { param([string] $ResolvedAgentPath) @@ -505,7 +527,16 @@ function Invoke-ServiceProbe { $account.Password.Dispose() } try { - if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force -ErrorAction Stop } + if (Test-Path -LiteralPath $root) { + # The service creates directories, such as config\identity, that only LocalSystem may access, and whether one + # exists by now depends on how far the service got, so what the runner account cannot remove is removed as + # LocalSystem. + try { + Remove-Item -LiteralPath $root -Recurse -Force -ErrorAction Stop + } catch { + Remove-DirectoryAsSystem $root + } + } } catch { $cleanupErrors.Add("Directory cleanup failed: $_") } From 0858fe13349aa09df66e29f9927189fcd2fda7c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20CORTIER?= Date: Wed, 2 Dec 2026 07:31:16 +0900 Subject: [PATCH 2/2] ci: remove the locked probe directories with PsExec Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- ci/agent-service-standard-user-probe.ps1 | 37 +++++++----------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/ci/agent-service-standard-user-probe.ps1 b/ci/agent-service-standard-user-probe.ps1 index b8b3721f1..bda3a2c07 100644 --- a/ci/agent-service-standard-user-probe.ps1 +++ b/ci/agent-service-standard-user-probe.ps1 @@ -394,28 +394,6 @@ function Get-DirectorySnapshot { ForEach-Object { "$($_.FullName)|$($_.Length)|$($_.LastWriteTimeUtc.Ticks)" }) -join "`n" } -# Removes a directory tree through a one-off scheduled task running as LocalSystem. -function Remove-DirectoryAsSystem { - param([string] $Path) - - $taskName = "agent-service-probe-cleanup-$([guid]::NewGuid().ToString('N'))" - $action = New-ScheduledTaskAction -Execute (Join-Path $env:SystemRoot "System32\cmd.exe") -Argument "/d /c rd /s /q `"$Path`"" - $principal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest - Register-ScheduledTask -TaskName $taskName -Action $action -Principal $principal -ErrorAction Stop | Out-Null - try { - Start-ScheduledTask -TaskName $taskName -ErrorAction Stop - $deadline = [DateTime]::UtcNow.AddSeconds(60) - while ((Test-Path -LiteralPath $Path) -and [DateTime]::UtcNow -lt $deadline) { - Start-Sleep -Milliseconds 250 - } - } finally { - Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue - } - if (Test-Path -LiteralPath $Path) { - throw "LocalSystem did not remove '$Path' within 60 seconds" - } -} - function Invoke-ServiceProbe { param([string] $ResolvedAgentPath) @@ -528,13 +506,20 @@ function Invoke-ServiceProbe { } try { if (Test-Path -LiteralPath $root) { - # The service creates directories, such as config\identity, that only LocalSystem may access, and whether one - # exists by now depends on how far the service got, so what the runner account cannot remove is removed as - # LocalSystem. + # The service creates directories, such as config\identity, that only LocalSystem may access. + # Whether one exists by now depends on how far the service got. + # What the runner account cannot remove is therefore removed as LocalSystem, as the job's other cleanups do. + # UNTESTED: no run is sure to leave such a directory, so passing runs may never reach the LocalSystem removal. + # When changing it, run the probe with a LocalSystem-only tree under the config directory and check that cleanup removes it. try { Remove-Item -LiteralPath $root -Recurse -Force -ErrorAction Stop } catch { - Remove-DirectoryAsSystem $root + $accountError = $_ + $systemOutput = & psexec.exe -accepteula -nobanner -s cmd.exe /d /c rd /s /q "$root" 2>&1 + if (Test-Path -LiteralPath $root) { + throw "$accountError; removing it as LocalSystem also failed with exit code ${LASTEXITCODE}: $($systemOutput -join ' ')" + } + $global:LASTEXITCODE = 0 } } } catch {