From 5584144978e00c42d2956e7b1e56d54e7462144c Mon Sep 17 00:00:00 2001 From: igor04091968 Date: Mon, 27 Apr 2026 03:00:53 +0300 Subject: [PATCH] fix(windows): suppress duplicate logon markers per session --- windows/ActivityWatch.Windows.Common.psm1 | 41 +++++++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/windows/ActivityWatch.Windows.Common.psm1 b/windows/ActivityWatch.Windows.Common.psm1 index 741c9f7..31884b2 100755 --- a/windows/ActivityWatch.Windows.Common.psm1 +++ b/windows/ActivityWatch.Windows.Common.psm1 @@ -527,13 +527,33 @@ function Send-LogonMarkerIfNeeded { } `$stateRoot = [string]`$Config.paths.stateRoot - if ([string]::IsNullOrWhiteSpace(`$stateRoot)) { - return + `$markerRoots = New-Object System.Collections.Generic.List[string] + if (-not [string]::IsNullOrWhiteSpace(`$env:LOCALAPPDATA)) { + `$markerRoots.Add((Join-Path `$env:LOCALAPPDATA 'ActivityWatch-Phase2\markers')) + } + if (-not [string]::IsNullOrWhiteSpace(`$stateRoot)) { + `$markerRoots.Add((Join-Path `$stateRoot 'markers')) } - `$markerDir = Join-Path `$stateRoot 'markers' - if (-not (Test-Path -LiteralPath `$markerDir)) { - New-Item -Path `$markerDir -ItemType Directory -Force | Out-Null + `$markerDir = `$null + foreach (`$candidate in `$markerRoots) { + try { + if (-not (Test-Path -LiteralPath `$candidate)) { + New-Item -Path `$candidate -ItemType Directory -Force | Out-Null + } + + `$probePath = Join-Path `$candidate 'write-test.tmp' + Set-Content -LiteralPath `$probePath -Value 'ok' -Encoding ASCII + Remove-Item -LiteralPath `$probePath -Force -ErrorAction SilentlyContinue + `$markerDir = `$candidate + break + } + catch { + } + } + + if (-not `$markerDir) { + return } `$markerFile = Join-Path `$markerDir ("logon-{0}-{1}.marker" -f `$env:USERNAME, `$SessionId) @@ -541,6 +561,8 @@ function Send-LogonMarkerIfNeeded { return } + Set-Content -LiteralPath `$markerFile -Value ((Get-Date).ToUniversalTime().ToString('o')) -Encoding UTF8 + `$bucketId = ('{0}_{1}' -f `$bucketPrefix, `$script:Hostname) Ensure-Bucket -BucketId `$bucketId -ClientName 'aw-session-events' -BucketType 'aw.session.event' @@ -557,8 +579,13 @@ function Send-LogonMarkerIfNeeded { } } | ConvertTo-Json -Depth 5 -Compress - Invoke-AwJsonPost -Uri "`$(`$script:ApiBase)/buckets/`$bucketId/heartbeat?pulsetime=1" -Json `$payload - Set-Content -LiteralPath `$markerFile -Value ((Get-Date).ToUniversalTime().ToString('o')) -Encoding UTF8 + try { + Invoke-AwJsonPost -Uri "`$(`$script:ApiBase)/buckets/`$bucketId/heartbeat?pulsetime=1" -Json `$payload + } + catch { + Remove-Item -LiteralPath `$markerFile -Force -ErrorAction SilentlyContinue + throw + } } function Start-CollectorScriptIfNeeded {