diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b2a9850c..093a985b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -84,20 +84,44 @@ jobs: - name: Test isolation refusal and readiness guards if: runner.os == 'Windows' shell: pwsh - run: ./scripts/test-windows-disposable-profile-guards.ps1 - - name: Prove disposable-profile isolation + run: | + ./scripts/test-windows-disposable-profile-guards.ps1 + ./scripts/test-windows-recovery-guards.ps1 + - name: Prove disposable-profile isolation, recovery row 1 (Local older, Roaming newer) if: runner.os == 'Windows' shell: pwsh run: >- ./scripts/test-windows-disposable-profile.ps1 -ServerExe target/debug/aw-server.exe - -EvidencePath "${{ runner.temp }}/windows-disposable-profile-evidence.json" - - name: Retain Windows isolation evidence + -User awrow1 + -EvidencePath "${{ runner.temp }}/windows-recovery-row1-evidence.json" + -ScenarioPath scripts/windows-recovery-scenarios/row1-local-older-roaming-newer.ps1 + # Each row gets its own disposable user and profile, so rows cannot see + # one another's databases. A failed row does not hide the others. + - name: Windows recovery row 2 (stale Local testing history, newer Roaming) + if: ${{ !cancelled() && runner.os == 'Windows' }} + shell: pwsh + run: >- + ./scripts/test-windows-disposable-profile.ps1 + -ServerExe target/debug/aw-server.exe + -User awrow2 + -EvidencePath "${{ runner.temp }}/windows-recovery-row2-evidence.json" + -ScenarioPath scripts/windows-recovery-scenarios/row2-stale-local-testing-newer-roaming.ps1 + - name: Windows recovery row 3 (Roaming history, missed Python history) + if: ${{ !cancelled() && runner.os == 'Windows' }} + shell: pwsh + run: >- + ./scripts/test-windows-disposable-profile.ps1 + -ServerExe target/debug/aw-server.exe + -User awrow3 + -EvidencePath "${{ runner.temp }}/windows-recovery-row3-evidence.json" + -ScenarioPath scripts/windows-recovery-scenarios/row3-roaming-with-missed-python-history.ps1 + - name: Retain Windows recovery evidence if: always() && runner.os == 'Windows' uses: actions/upload-artifact@v7 with: name: windows-disposable-profile-evidence - path: ${{ runner.temp }}/windows-disposable-profile-evidence.json + path: ${{ runner.temp }}/windows-recovery-row*-evidence.json if-no-files-found: warn - name: Run tests run: cargo test --workspace --verbose diff --git a/scripts/test-windows-disposable-profile.ps1 b/scripts/test-windows-disposable-profile.ps1 index 6a578978..8d10d800 100644 --- a/scripts/test-windows-disposable-profile.ps1 +++ b/scripts/test-windows-disposable-profile.ps1 @@ -31,7 +31,11 @@ param( [int]$TimeoutSeconds = 120, - [string]$EvidencePath = (Join-Path $env:TEMP 'windows-disposable-profile-evidence.json') + [string]$EvidencePath = (Join-Path $env:TEMP 'windows-disposable-profile-evidence.json'), + + # Optional recovery scenario, dot-sourced after the isolation proof passes + # and while the disposable user, profile and first server still exist. + [string]$ScenarioPath ) Set-StrictMode -Version Latest @@ -145,6 +149,20 @@ function Wait-ProfileInitTask { throw "profile-init task timed out after $TimeoutSeconds seconds" } +function Wait-ServerHttpReady { + param($Process, [int]$Port, [int]$TimeoutSeconds = 120) + $deadline = (Get-Date).AddSeconds($TimeoutSeconds) + while ((Get-Date) -lt $deadline) { + if ($Process.HasExited) { break } + try { + $null = Invoke-RestMethod -Uri "http://127.0.0.1:$Port/api/0/info" -TimeoutSec 3 + return + } + catch { Start-Sleep -Milliseconds 500 } + } + throw "server HTTP API did not become ready on port $Port (exited: $($Process.HasExited))" +} + function Get-ProfileTail { param([string]$Dir) if (-not (Test-Path -LiteralPath $Dir)) { @@ -453,6 +471,18 @@ try { Write-Host 'Disposable-profile isolation proven:' Write-Host " datastore: $expectedDb ($($evidence['datastore_bytes']) bytes, owner $owner)" Write-Host " runner profile untouched: $runnerActivityWatch absent" + + if ($ScenarioPath) { + # A SQLite header precedes Rocket startup. Never write fixtures until + # the server accepts HTTP, even when native-root isolation is proven. + Wait-ServerHttpReady -Process $serverProcess -Port $port -TimeoutSeconds $TimeoutSeconds + $evidence['scenario_http_ready'] = $true + # Scenario context: $exe $User $credential $disposableProfile $serverProcess + # $port $runId $evidence. The scenario owns every process it starts. + Write-Host "Running recovery scenario: $ScenarioPath" + . (Resolve-Path -LiteralPath $ScenarioPath).Path + $evidence['conclusion'] = 'disposable Known Folder roots proven; recovery scenario passed' + } } catch { $failure = $_.Exception.Message diff --git a/scripts/test-windows-recovery-guards.ps1 b/scripts/test-windows-recovery-guards.ps1 new file mode 100644 index 00000000..ff256a7f --- /dev/null +++ b/scripts/test-windows-recovery-guards.ps1 @@ -0,0 +1,177 @@ +#Requires -Version 5.1 +# Hermetic regressions for the recovery harness; no Windows account or server. +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' +$mainPath = Join-Path $PSScriptRoot 'test-windows-disposable-profile.ps1' +$commonPath = Join-Path $PSScriptRoot 'windows-recovery-scenarios/common.ps1' +$tokens = $null +$errors = $null +$ast = [System.Management.Automation.Language.Parser]::ParseFile($mainPath, [ref]$tokens, [ref]$errors) +if ($errors.Count) { throw ($errors | Out-String) } +$waiter = $ast.Find({ param($node) + $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and + $node.Name -eq 'Wait-ServerHttpReady' +}, $true) +if ($null -eq $waiter) { throw 'missing HTTP readiness waiter' } +. ([scriptblock]::Create($waiter.Extent.Text)) +. $commonPath +function Assert-True { + param([bool]$Condition, [string]$Message) + if (-not $Condition) { throw $Message } +} +$root = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString('N')) +New-Item -ItemType Directory -Path $root | Out-Null +$savedPublic = $env:Public +$savedSystemRoot = $env:SystemRoot +try { + & { + $script:clock = [datetime]'2026-01-01' + $script:polls = 0 + function Get-Date { return $script:clock } + function Start-Sleep { param($Milliseconds) $script:clock = $script:clock.AddMilliseconds($Milliseconds) } + function Invoke-RestMethod { + param($Uri, $TimeoutSec) + $script:polls++ + if ($script:polls -lt 3) { throw 'HTTP not ready' } + return @{ version = 'test' } + } + Wait-ServerHttpReady -Process ([pscustomobject]@{ HasExited = $false }) -Port 1234 -TimeoutSeconds 2 + Assert-True ($script:polls -eq 3) 'HTTP readiness did not retry transient startup failures' + function Invoke-RestMethod { param($Uri, $TimeoutSec) throw 'never ready' } + $caught = $null + try { Wait-ServerHttpReady -Process ([pscustomobject]@{ HasExited = $false }) -Port 1234 -TimeoutSeconds 1 } + catch { $caught = $_.Exception.Message } + Assert-True ($null -ne $caught -and $caught -like '*HTTP*') 'HTTP timeout accepted' + + # Execute the actual scenario hook: even with a valid SQLite file, + # a failed HTTP wait must stop it before the scenario writes fixtures. + $hook = $ast.Find({ param($node) + $node -is [System.Management.Automation.Language.IfStatementAst] -and + $node.Extent.Text -like 'if ($ScenarioPath)*' + }, $true) + $ScenarioPath = Join-Path $root 'scenario.ps1' + Set-Content -LiteralPath $ScenarioPath -Value '$script:scenarioRan = $true' + $script:scenarioRan = $false + # Pin the URL the hook polls: the mock used to ignore $Uri, so a + # harness polling the wrong port would have passed this guard. + $script:polledUris = @() + function Invoke-RestMethod { param($Uri, $TimeoutSec) $script:polledUris += $Uri; throw 'never ready' } + $serverProcess = [pscustomobject]@{ HasExited = $false } + $port = 1234 + $TimeoutSeconds = 1 + $evidence = @{} + $caught = $null + try { . ([scriptblock]::Create($hook.Extent.Text)) } + catch { $caught = $_.Exception.Message } + Assert-True ($null -ne $caught -and -not $script:scenarioRan) 'scenario ran before HTTP was ready' + Assert-True (($script:polledUris.Count -gt 0) -and -not ($script:polledUris | Where-Object { $_ -ne 'http://127.0.0.1:1234/api/0/info' })) ` + "scenario hook polled the wrong URL: $($script:polledUris -join ', ')" + } + + & { + $env:Public = $root + $env:SystemRoot = $root + $exe = 'fake-server.exe' + $credential = $null + $runId = 'guard' + $scenario = @{} + $script:killed = $false + $script:waited = $false + $script:clock = [datetime]'2026-01-01' + function Get-Date { $script:clock = $script:clock.AddSeconds(121); return $script:clock } + function Start-Process { + param($FilePath, $ArgumentList, $Credential, [switch]$LoadUserProfile, [switch]$UseNewEnvironment, $WorkingDirectory, [switch]$PassThru) + Set-Content -LiteralPath (Join-Path $root 'aw-timeout-guard.log') -Value 'startup diagnostic' + $proc = [pscustomobject]@{ Id = 42; HasExited = $false } + $proc | Add-Member ScriptMethod WaitForExit { param($Timeout) $script:waited = $true; return $true } + return $proc + } + function taskkill.exe { + Assert-True (($args -join ' ') -eq '/PID 42 /T /F') 'wrong process tree killed' + $script:killed = $true + } + $caught = $null + try { Start-ScenarioServer -Name 'timeout' -Port 1234 -ExtraArgs '' } + catch { $caught = $_.Exception.Message } + Assert-True ($caught -like '*startup diagnostic*') 'startup diagnostic lost' + Assert-True ($script:killed -and $script:waited) 'timed-out process was leaked' + Assert-True (-not (Test-Path (Join-Path $root 'aw-timeout-guard.cmd'))) 'wrapper leaked' + Assert-True (-not (Test-Path (Join-Path $root 'aw-timeout-guard.log'))) 'log leaked' + Assert-True ($scenario['log_1234'] -contains 'startup diagnostic') 'cleanup discarded evidence' + } + + & { + $scenario = @{} + $script:clock = [datetime]'2026-01-01' + $script:reads = 0 + function Get-Date { return $script:clock } + function Start-Sleep { param($Seconds) $script:clock = $script:clock.AddSeconds($Seconds) } + function Invoke-RestMethod { + param($Uri, $TimeoutSec) + if ($Uri -notlike '*/events*') { return [pscustomobject]@{ 'aw-guard' = 1 } } + $script:reads++ + if ($script:reads -lt 3) { throw 'bucket not found' } + return @([pscustomobject]@{ data = [pscustomobject]@{ sentinel = 'A-1' } }) + } + $got = Wait-ScenarioSentinels -Port 1234 -Expected @('A-1') -Bucket 'aw-guard' + Assert-True ((($got -join ',') -eq 'A-1') -and $script:reads -eq 3) 'sentinel polling aborted on a transient read failure' + function Invoke-RestMethod { param($Uri, $TimeoutSec) throw 'bucket never appeared' } + $caught = $null + try { $null = Wait-ScenarioSentinels -Port 1234 -Expected @('A-1') -Bucket 'aw-guard' -TimeoutSeconds 5 } + catch { $caught = $_.Exception.Message } + Assert-True ($caught -like '*never appeared*') 'persistent sentinel read failure was reported as empty' + } + + & { + $scenario = @{} + foreach ($case in @('zero', 'inserted', 'skipped', 'missing')) { + $inserted = if ($case -eq 'inserted') { 1 } else { 0 } + $skipped = if ($case -eq 'skipped') { 3 } else { 0 } + $scenario['log_1234'] = if ($case -eq 'missing') { @('unrelated log') } else { + @("INFO History recovery from source: 0 buckets created, $inserted events inserted, $skipped duplicates skipped") + } + $caught = $null + try { Assert-NoRecoveryWork -Port 1234 } + catch { $caught = $_.Exception.Message } + Assert-True (($null -eq $caught) -eq ($case -eq 'zero')) "incorrect zero-work verdict for $case" + } + $scenario['log_1234'] = @('INFO History recovery from source: 0 buckets created, 0 events inserted, 3 duplicates skipped') + Assert-NoRecoveryWork -Port 1234 -AllowSkipped + } + & { + $rowPath = Join-Path $PSScriptRoot 'windows-recovery-scenarios/row1-local-older-roaming-newer.ps1' + $rowAst = [System.Management.Automation.Language.Parser]::ParseFile($rowPath, [ref]$tokens, [ref]$errors) + if ($errors.Count) { throw ($errors | Out-String) } + $preservation = $rowAst.Find({ param($node) + $node -is [System.Management.Automation.Language.ForEachStatementAst] -and + $node.Extent.Text -like '*pre-migration-*' + }, $true) + if ($null -eq $preservation) { throw 'missing original-tree assertions' } + $disposableProfile = $root + $localApp = Join-Path $root 'Local/aw-server-rust' + $roamApp = Join-Path $root 'Roaming/aw-server-rust' + $localBackup = "$localApp.pre-migration-test" + $roamBackup = "$roamApp.migrated-test" + foreach ($dir in @($localApp, $roamApp, $localBackup, $roamBackup)) { + New-Item -ItemType Directory -Path $dir -Force | Out-Null + Set-Content -LiteralPath (Join-Path $dir 'sqlite.db') -Value 'fixture' + } + $scenario = @{} + . ([scriptblock]::Create($preservation.Extent.Text)) + foreach ($backup in @($localBackup, $roamBackup)) { + $db = Join-Path $backup 'sqlite.db' + Remove-Item -LiteralPath $db + $caught = $null + try { . ([scriptblock]::Create($preservation.Extent.Text)) } + catch { $caught = $_.Exception.Message } + Assert-True ($null -ne $caught -and $caught -like '*not retained*') 'active database masked a missing original' + Set-Content -LiteralPath $db -Value 'fixture' + } + } + Write-Host 'PASS: HTTP readiness gates fixtures, startup timeout cleans its process tree and evidence, sentinel polling retries transient reads, restart counts fail closed, both originals required' +} +finally { + $env:Public = $savedPublic + $env:SystemRoot = $savedSystemRoot + Remove-Item -LiteralPath $root -Recurse -Force +} diff --git a/scripts/windows-recovery-scenarios/common.ps1 b/scripts/windows-recovery-scenarios/common.ps1 new file mode 100644 index 00000000..b79e79d2 --- /dev/null +++ b/scripts/windows-recovery-scenarios/common.ps1 @@ -0,0 +1,122 @@ +# Shared helpers for the Windows recovery scenarios. Dot-sourced by each +# scenario after it defines $bucketId and $scenario. Context from the isolation +# script: $exe $credential $disposableProfile $serverProcess $port $runId. +# Fixtures are created by the real server (schema stays migration-owned); +# --dbpath is used for fixture creation only, never for the assertion runs. + +function Wait-ScenarioSentinels { + # Poll until the bucket holds exactly the expected sentinels (recovery and + # import can finish after the HTTP API is already answering). + param([int]$Port, [string[]]$Expected, [string]$Bucket = $bucketId, [int]$TimeoutSeconds = 60) + # The bucket may not exist yet (404) while recovery or import still runs, + # so a failed read is retried; if the last read still failed, rethrow it + # rather than report an empty sentinel list. + $deadline = (Get-Date).AddSeconds($TimeoutSeconds) + $got = @() + do { + $lastError = $null + try { + $got = Get-FixtureSentinels -Port $Port -Bucket $Bucket + if (($got -join ',') -eq ($Expected -join ',')) { break } + } + catch { $lastError = $_ } + Start-Sleep -Seconds 2 + } while ((Get-Date) -lt $deadline) + if ($null -ne $lastError) { throw $lastError } + return $got +} + +function Stop-FixtureProcess { + # Kill the isolation script's default-profile server (fixture phase). + param($Process) + & taskkill.exe /PID $Process.Id /T /F 2>$null | Out-Null + $null = $Process.WaitForExit(15000) +} + +function Start-ScenarioServer { + param([string]$Name, [int]$Port, [string]$ExtraArgs) + $log = Join-Path $env:Public "aw-$Name-$runId.log" + $cmd = Join-Path $env:Public "aw-$Name-$runId.cmd" + $line = '"{0}" --host 127.0.0.1 --port {1} {2} --verbose >> "{3}" 2>&1' -f $exe, $Port, $ExtraArgs, $log + Set-Content -LiteralPath $cmd -Value @('@echo off', $line) -Encoding ascii + $proc = Start-Process -FilePath "$env:SystemRoot\System32\cmd.exe" ` + -ArgumentList @('/d', '/s', '/c', ('""{0}""' -f $cmd)) ` + -Credential $credential -LoadUserProfile -UseNewEnvironment ` + -WorkingDirectory $env:SystemRoot -PassThru + $deadline = (Get-Date).AddSeconds(120) + while ((Get-Date) -lt $deadline) { + if ($proc.HasExited) { break } + try { + $null = Invoke-RestMethod -Uri "http://127.0.0.1:$Port/api/0/info" -TimeoutSec 3 + return [pscustomobject]@{ Process = $proc; Log = $log; Cmd = $cmd; Port = $Port } + } + catch { Start-Sleep -Milliseconds 500 } + } + $tail = if (Test-Path -LiteralPath $log) { (Get-Content -LiteralPath $log -Tail 30) -join "`n" } else { '(no log)' } + $exited = $proc.HasExited + # The caller has no handle yet: retain diagnostics and clean up here. + Stop-ScenarioServer ([pscustomobject]@{ Process = $proc; Log = $log; Cmd = $cmd; Port = $Port }) + throw "scenario server '$Name' did not become ready on port $Port (exited: $exited).`n$tail" +} + +function Stop-ScenarioServer { + param($Server) + # taskkill /F is deliberate: committed events stay WAL-only, unflushed. + & taskkill.exe /PID $Server.Process.Id /T /F 2>$null | Out-Null + $null = $Server.Process.WaitForExit(15000) + $scenario["log_$($Server.Port)"] = @(Get-Content -LiteralPath $Server.Log -ErrorAction SilentlyContinue) + Remove-Item -LiteralPath $Server.Log, $Server.Cmd -Force -ErrorAction SilentlyContinue +} + +function Assert-NoRecoveryWork { + # Require actual recovery summaries, not just unchanged event contents. + # Row 1 promises zero inserts; row 2 also promises zero duplicate scans. + param([int]$Port, [switch]$AllowSkipped) + $counts = @($scenario["log_$Port"] | Select-String -Pattern 'History recovery from .*: (\d+) buckets created, (\d+) events inserted, (\d+) duplicates skipped') + Assert-True ($counts.Count -gt 0) "restart on port $Port has no recovery summaries" + foreach ($count in $counts) { + Assert-True ([long]$count.Matches[0].Groups[2].Value -eq 0) "restart on port $Port inserted events again" + if (-not $AllowSkipped) { + Assert-True ([long]$count.Matches[0].Groups[3].Value -eq 0) "restart on port $Port scanned duplicate events again" + } + } +} + +function Add-FixtureEvents { + param([int]$Port, [string]$Tag, [string]$Day, [string]$Bucket = $bucketId) + $base = "http://127.0.0.1:$Port/api/0/buckets/$Bucket" + $body = ConvertTo-Json -Compress -InputObject @{ client = 'recovery-test'; type = 'test'; hostname = 'recovery-host' } + try { Invoke-RestMethod -Method Post -Uri $base -ContentType 'application/json' -Body $body | Out-Null } + catch { if ($_.Exception.Response.StatusCode.value__ -ne 304) { throw } } + $events = @(1..3 | ForEach-Object { + @{ timestamp = "$Day`T0$_`:00:00+00:00"; duration = 1; data = @{ sentinel = "$Tag-$_" } } + }) + Invoke-RestMethod -Method Post -Uri "$base/events" -ContentType 'application/json' ` + -Body (ConvertTo-Json -Compress -Depth 5 -InputObject $events) | Out-Null +} + +function Wait-FixtureCommitted { + # The datastore worker blocks on its request queue and only evaluates the + # 15 s batch window after handling a request (worker.rs), so idle waiting + # never commits queued inserts. Creating a bucket is a committing command: + # it flushes everything queued before it. Require a non-empty WAL as + # evidence of committed-but-uncheckpointed state before the kill. + param([int]$Port, [string]$Dir, [string]$Name, [string]$Db = 'sqlite.db') + $body = ConvertTo-Json -Compress -InputObject @{ client = 'recovery-test'; type = 'test'; hostname = 'recovery-host' } + Invoke-RestMethod -Method Post -Uri "http://127.0.0.1:$Port/api/0/buckets/aw-recovery-flush-$Name" ` + -ContentType 'application/json' -Body $body | Out-Null + $wal = Join-Path $Dir "$Db-wal" + $len = if (Test-Path -LiteralPath $wal) { (Get-Item -LiteralPath $wal).Length } else { 0 } + $scenario["wal_bytes_$Name"] = $len + Assert-True ($len -gt 0) "$Name fixture has no committed WAL content to recover from" +} + +function Get-FixtureSentinels { + param([int]$Port, [string]$Bucket = $bucketId) + $events = @(Invoke-RestMethod -Uri "http://127.0.0.1:$Port/api/0/buckets/$Bucket/events?limit=1000" -TimeoutSec 15) + # PS 5.1 turns an empty JSON array into $null; drop it before StrictMode + # property access, and keep a bucket inventory for the evidence file. + $buckets = Invoke-RestMethod -Uri "http://127.0.0.1:$Port/api/0/buckets/" -TimeoutSec 15 + $scenario["buckets_$Port"] = @($buckets.PSObject.Properties.Name) + return @($events | Where-Object { $null -ne $_ } | ForEach-Object { $_.data.sentinel } | Sort-Object) +} diff --git a/scripts/windows-recovery-scenarios/row1-local-older-roaming-newer.ps1 b/scripts/windows-recovery-scenarios/row1-local-older-roaming-newer.ps1 new file mode 100644 index 00000000..8f31308b --- /dev/null +++ b/scripts/windows-recovery-scenarios/row1-local-older-roaming-newer.ps1 @@ -0,0 +1,81 @@ +# Recovery matrix row 1: existing Local history, newer v0.14.0 Roaming history. +# Dot-sourced by test-windows-disposable-profile.ps1 after isolation is proven. +# Context: $exe $User $credential $disposableProfile $serverProcess $port +# $runId $evidence. +# Helpers live in common.ps1. + +$bucketId = 'aw-recovery-row1' +$localApp = Join-Path $disposableProfile 'AppData\Local\activitywatch\aw-server-rust' +$roamApp = Join-Path $disposableProfile 'AppData\Roaming\activitywatch\aw-server-rust' +$scenario = [ordered]@{ row = 'local-older-roaming-newer' } +$evidence['scenario'] = $scenario + +. (Join-Path $PSScriptRoot 'common.ps1') + +$expected = @('LOCAL-1', 'LOCAL-2', 'LOCAL-3', 'ROAM-1', 'ROAM-2', 'ROAM-3') +$server = $null +try { + # 1. Local history (older): the isolation proof's default-profile server + # already created the Local datastore. Write through it, then kill it. + Add-FixtureEvents -Port $port -Tag 'LOCAL' -Day '2026-01-01' + Wait-FixtureCommitted -Port $port -Dir $localApp -Name 'local' + & taskkill.exe /PID $serverProcess.Id /T /F 2>$null | Out-Null + $null = $serverProcess.WaitForExit(15000) + $scenario['local_dir_after_fixture'] = @(Get-ChildItem -LiteralPath $localApp -Name -ErrorAction SilentlyContinue) + + # 2. Roaming history (newer), colliding autoincrement ids, WAL-only. + New-Item -ItemType Directory -Path $roamApp -Force | Out-Null + $roamPort = $port + 1 + $server = Start-ScenarioServer -Name 'roam-fixture' -Port $roamPort -ExtraArgs ('--dbpath "{0}"' -f (Join-Path $roamApp 'sqlite.db')) + Add-FixtureEvents -Port $roamPort -Tag 'ROAM' -Day '2026-06-01' + Wait-FixtureCommitted -Port $roamPort -Dir $roamApp -Name 'roaming' + Stop-ScenarioServer $server + $server = $null + Assert-True (Test-Path -LiteralPath (Join-Path $roamApp 'sqlite.db')) 'Roaming fixture database was not created' + $scenario['roaming_dir_after_fixture'] = @(Get-ChildItem -LiteralPath $roamApp -Name) + + # 3. Real default-profile start must pick Roaming (newest event), migrate + # it and union the Local history into the selected database. + $server = Start-ScenarioServer -Name 'assert1' -Port ($port + 2) -ExtraArgs '--profile default' + $got = Wait-ScenarioSentinels -Port ($port + 2) -Expected $expected + $scenario['sentinels_first_start'] = $got + Assert-True (($got -join ',') -eq ($expected -join ',')) ` + "first start: expected union $($expected -join ',') but found $($got -join ',')" + $scenario['roaming_tree_after_recovery'] = @(Get-ChildItem -LiteralPath (Split-Path -Parent $roamApp) -Name -ErrorAction SilentlyContinue) + $scenario['local_tree_after_recovery'] = @(Get-ChildItem -LiteralPath (Split-Path -Parent $localApp) -Name -ErrorAction SilentlyContinue) + Stop-ScenarioServer $server + $server = $null + + # 4. Fresh process restart: same content, no duplicates, no re-union. + $server = Start-ScenarioServer -Name 'assert2' -Port ($port + 3) -ExtraArgs '--profile default' + $got = Get-FixtureSentinels -Port ($port + 3) + $scenario['sentinels_restart'] = $got + Assert-True (($got -join ',') -eq ($expected -join ',')) ` + "restart: expected $($expected -join ',') but found $($got -join ',')" + Stop-ScenarioServer $server + $server = $null + + Assert-NoRecoveryWork -Port ($port + 3) -AllowSkipped + + # 5. Both original trees must survive under their migration backup names, + # not merely as the active, unioned Local database. + foreach ($source in @( + @{ Name = 'local'; App = $localApp; Pattern = 'aw-server-rust.pre-migration-*' }, + @{ Name = 'roaming'; App = $roamApp; Pattern = 'aw-server-rust.migrated-*' } + )) { + $trees = @(Get-ChildItem -LiteralPath (Split-Path -Parent $source.App) ` + -Directory -Filter $source.Pattern -ErrorAction SilentlyContinue) + $preserved = @($trees | ForEach-Object { Join-Path $_.FullName 'sqlite.db' } | + Where-Object { Test-Path -LiteralPath $_ -PathType Leaf }) + $scenario["preserved_$($source.Name)_databases"] = @($preserved | ForEach-Object { $_.Substring($disposableProfile.Length) }) + Assert-True ($preserved.Count -ge 1) "original $($source.Name) database is not retained in $($source.Pattern)" + } + $scenario['result'] = 'pass' +} +catch { + $scenario['result'] = "fail: $($_.Exception.Message)" + throw +} +finally { + if ($null -ne $server) { Stop-ScenarioServer $server } +} diff --git a/scripts/windows-recovery-scenarios/row2-stale-local-testing-newer-roaming.ps1 b/scripts/windows-recovery-scenarios/row2-stale-local-testing-newer-roaming.ps1 new file mode 100644 index 00000000..8d324575 --- /dev/null +++ b/scripts/windows-recovery-scenarios/row2-stale-local-testing-newer-roaming.ps1 @@ -0,0 +1,100 @@ +# Recovery matrix row 2: stale Local history that also holds a saved +# `testing`-profile database (sqlite-testing.db), newer v0.14.0 Roaming history. +# Dot-sourced by test-windows-disposable-profile.ps1 after isolation is proven. +# Context: $exe $User $credential $disposableProfile $serverProcess $port +# $runId $evidence. Helpers live in common.ps1. +# +# Expected: the default profile selects Roaming (newer events) and unions the +# stale Local default history; the Local testing database must not be lost, so a +# `--profile testing` start recovers it from the preserved Local tree. + +$bucketId = 'aw-recovery-row2' +$localApp = Join-Path $disposableProfile 'AppData\Local\activitywatch\aw-server-rust' +$roamApp = Join-Path $disposableProfile 'AppData\Roaming\activitywatch\aw-server-rust' +$scenario = [ordered]@{ row = 'stale-local-testing-newer-roaming' } +$evidence['scenario'] = $scenario +. (Join-Path $PSScriptRoot 'common.ps1') + +$expected = @('LOCAL-1', 'LOCAL-2', 'LOCAL-3', 'ROAM-1', 'ROAM-2', 'ROAM-3') +$expectedTesting = @('TEST-1', 'TEST-2', 'TEST-3') +$server = $null +try { + # 1. Stale Local default history through the isolation proof's server. + Add-FixtureEvents -Port $port -Tag 'LOCAL' -Day '2026-01-01' + Wait-FixtureCommitted -Port $port -Dir $localApp -Name 'local' + Stop-FixtureProcess $serverProcess + + # 2. Saved testing-profile history in the same Local directory (legacy + # shared layout: sqlite-testing.db beside sqlite.db). Fixture only. + $server = Start-ScenarioServer -Name 'testing-fixture' -Port ($port + 1) ` + -ExtraArgs ('--profile default --dbpath "{0}"' -f (Join-Path $localApp 'sqlite-testing.db')) + Add-FixtureEvents -Port ($port + 1) -Tag 'TEST' -Day '2026-02-01' + Wait-FixtureCommitted -Port ($port + 1) -Dir $localApp -Name 'testing' -Db 'sqlite-testing.db' + Stop-ScenarioServer $server + $server = $null + Assert-True (Test-Path -LiteralPath (Join-Path $localApp 'sqlite-testing.db')) 'Local testing fixture was not created' + $scenario['local_dir_after_fixture'] = @(Get-ChildItem -LiteralPath $localApp -Name -ErrorAction SilentlyContinue) + + # 3. Newer Roaming default history, colliding ids, WAL-only. + New-Item -ItemType Directory -Path $roamApp -Force | Out-Null + $server = Start-ScenarioServer -Name 'roam-fixture' -Port ($port + 2) ` + -ExtraArgs ('--profile default --dbpath "{0}"' -f (Join-Path $roamApp 'sqlite.db')) + Add-FixtureEvents -Port ($port + 2) -Tag 'ROAM' -Day '2026-06-01' + Wait-FixtureCommitted -Port ($port + 2) -Dir $roamApp -Name 'roaming' + Stop-ScenarioServer $server + $server = $null + + # 4. Default start: Roaming selected, stale Local unioned. Debug builds + # default to the `testing` profile, which would have created the + # new-layout `activitywatch-testing` root during fixture creation; that + # root takes precedence over legacy files and would hide the scenario + # (CI run 42962833628). The fixture servers therefore run as `default`; + # prove they left no such root before asserting. + Assert-True (-not (Test-Path -LiteralPath (Join-Path $disposableProfile 'AppData\Local\activitywatch-testing'))) ` + 'fixture creation made the new-layout testing root; the legacy testing scenario would be hidden' + $server = Start-ScenarioServer -Name 'assert-default1' -Port ($port + 3) -ExtraArgs '--profile default' + $got = Wait-ScenarioSentinels -Port ($port + 3) -Expected $expected + $scenario['sentinels_default_first'] = $got + Assert-True (($got -join ',') -eq ($expected -join ',')) ` + "default first start: expected $($expected -join ',') but found $($got -join ',')" + $scenario['local_tree_after_recovery'] = @(Get-ChildItem -LiteralPath (Split-Path -Parent $localApp) -Name -ErrorAction SilentlyContinue) + Stop-ScenarioServer $server + $server = $null + + # 5. Saved testing history must be recovered by a fresh testing-profile + # process, then stay stable across a second one. + foreach ($n in 1, 2) { + $server = Start-ScenarioServer -Name "assert-testing$n" -Port ($port + 3 + $n) -ExtraArgs '--profile testing' + $got = Wait-ScenarioSentinels -Port ($port + 3 + $n) -Expected $expectedTesting + $scenario["sentinels_testing_$n"] = $got + Assert-True (($got -join ',') -eq ($expectedTesting -join ',')) ` + "testing start ${n}: expected $($expectedTesting -join ',') but found $($got -join ',')" + Stop-ScenarioServer $server + $server = $null + if ($n -eq 2) { Assert-NoRecoveryWork -Port ($port + 3 + $n) } + } + + # 6. Default history unchanged after the testing recovery. + $server = Start-ScenarioServer -Name 'assert-default2' -Port ($port + 6) -ExtraArgs '--profile default' + $got = Get-FixtureSentinels -Port ($port + 6) + $scenario['sentinels_default_restart'] = $got + Assert-True (($got -join ',') -eq ($expected -join ',')) ` + "default restart: expected $($expected -join ',') but found $($got -join ',')" + Stop-ScenarioServer $server + $server = $null + + # 7. Original trees retained. + $preserved = @(Get-ChildItem -LiteralPath (Join-Path $disposableProfile 'AppData') -Recurse -File ` + -Filter 'sqlite*.db' -ErrorAction SilentlyContinue) + $scenario['preserved_databases'] = @($preserved | ForEach-Object { $_.FullName.Substring($disposableProfile.Length) }) + Assert-True (@($preserved | Where-Object { $_.FullName -like '*\Roaming\*' }).Count -ge 1) ` + 'original Roaming database is not retained under Roaming' + $scenario['result'] = 'pass' +} +catch { + $scenario['result'] = "fail: $($_.Exception.Message)" + throw +} +finally { + if ($null -ne $server) { Stop-ScenarioServer $server } +} diff --git a/scripts/windows-recovery-scenarios/row3-roaming-with-missed-python-history.ps1 b/scripts/windows-recovery-scenarios/row3-roaming-with-missed-python-history.ps1 new file mode 100644 index 00000000..e50922f6 --- /dev/null +++ b/scripts/windows-recovery-scenarios/row3-roaming-with-missed-python-history.ps1 @@ -0,0 +1,122 @@ +# Recovery matrix row 3: v0.14.0 Roaming history plus a Python aw-server +# database (%LOCALAPPDATA%\activitywatch\activitywatch\aw-server\ +# peewee-sqlite.v2.db) that v0.14.0's first-start import never read. +# Dot-sourced by test-windows-disposable-profile.ps1 after isolation is proven. +# Context: $exe $User $credential $disposableProfile $serverProcess $port +# $runId $evidence. Helpers live in common.ps1. +# +# Expected: Roaming is migrated to Local, the missed Python import runs once on +# that first default-profile start, and a fresh-process restart neither re-runs +# it nor duplicates events. +# +# Limit: the Python database is a peewee schema (bucketmodel/eventmodel, the +# tables aw-datastore's importer reads) created with the runner's Python +# sqlite3, not by a Python aw-server. Its journal mode is the default rollback +# journal, so no WAL-only Python events are tested. + +$bucketId = 'aw-recovery-row3' +$pythonBucket = 'aw-recovery-row3-python' +$localApp = Join-Path $disposableProfile 'AppData\Local\activitywatch\aw-server-rust' +$roamApp = Join-Path $disposableProfile 'AppData\Roaming\activitywatch\aw-server-rust' +$pythonDir = Join-Path $disposableProfile 'AppData\Local\activitywatch\activitywatch\aw-server' +$pythonDb = Join-Path $pythonDir 'peewee-sqlite.v2.db' +$scenario = [ordered]@{ row = 'roaming-with-missed-python-history' } +$evidence['scenario'] = $scenario +. (Join-Path $PSScriptRoot 'common.ps1') + +$expectedRoam = @('ROAM-1', 'ROAM-2', 'ROAM-3') +$expectedPython = @('PY-1', 'PY-2', 'PY-3') +$importMarker = 'Database was migrated from the v0.14.0 location' +$server = $null +try { + # 1. Only Roaming holds a Rust database, as for a user who ran v0.14.0: drop + # the empty Local datastore the isolation proof's server created. + Stop-FixtureProcess $serverProcess + # The killed server's file handles can linger briefly on Windows. + for ($attempt = 1; $attempt -le 10 -and (Test-Path -LiteralPath $localApp); $attempt++) { + Remove-Item -LiteralPath $localApp -Recurse -Force -ErrorAction SilentlyContinue + if (Test-Path -LiteralPath $localApp) { Start-Sleep -Seconds 1 } + } + Assert-True (-not (Test-Path -LiteralPath $localApp)) 'could not clear the Local datastore before the Roaming fixture' + + # 2. Roaming history, created by the real server, WAL-only. + New-Item -ItemType Directory -Path $roamApp -Force | Out-Null + $server = Start-ScenarioServer -Name 'roam-fixture' -Port ($port + 1) ` + -ExtraArgs ('--dbpath "{0}"' -f (Join-Path $roamApp 'sqlite.db')) + Add-FixtureEvents -Port ($port + 1) -Tag 'ROAM' -Day '2026-06-01' + Wait-FixtureCommitted -Port ($port + 1) -Dir $roamApp -Name 'roaming' + Stop-ScenarioServer $server + $server = $null + + # 3. The Python database v0.14.0 missed (colliding integer ids 1..3). + $python = @('python', 'py') | ForEach-Object { Get-Command $_ -ErrorAction SilentlyContinue } | + Select-Object -First 1 + Assert-True ($null -ne $python) 'no python on PATH to create the Python aw-server fixture database' + New-Item -ItemType Directory -Path $pythonDir -Force | Out-Null + $builder = Join-Path $env:Public "aw-pyfixture-$runId.py" + Set-Content -LiteralPath $builder -Encoding ascii -Value @' +import json, sqlite3, sys +path, bucket = sys.argv[1], sys.argv[2] +con = sqlite3.connect(path) +con.execute("CREATE TABLE bucketmodel (key INTEGER PRIMARY KEY, id TEXT, created TEXT, name TEXT, type TEXT, client TEXT, hostname TEXT)") +con.execute("CREATE TABLE eventmodel (id INTEGER PRIMARY KEY, bucket_id INTEGER, timestamp TEXT, duration REAL, datastr TEXT)") +con.execute("INSERT INTO bucketmodel (key, id, created, name, type, client, hostname) VALUES (1, ?, '2025-12-01T00:00:00+00:00', NULL, 'test', 'recovery-test', 'recovery-host')", (bucket,)) +for i in (1, 2, 3): + con.execute("INSERT INTO eventmodel (id, bucket_id, timestamp, duration, datastr) VALUES (?, 1, ?, 1.0, ?)", + (i, "2025-12-01 0%d:00:00+00:00" % i, json.dumps({"sentinel": "PY-%d" % i}))) +con.commit() +con.close() +'@ + & $python.Source $builder $pythonDb $pythonBucket + Assert-True ($LASTEXITCODE -eq 0) 'Python fixture builder failed' + Remove-Item -LiteralPath $builder -Force -ErrorAction SilentlyContinue + Assert-True (Test-Path -LiteralPath $pythonDb) 'Python fixture database was not created' + $scenario['python_fixture_bytes'] = (Get-Item -LiteralPath $pythonDb).Length + + # 4. First real default-profile start: migrate Roaming, import Python once. + $server = Start-ScenarioServer -Name 'assert1' -Port ($port + 2) -ExtraArgs '--profile default' + $gotRoam = Wait-ScenarioSentinels -Port ($port + 2) -Expected $expectedRoam + $gotPy = Wait-ScenarioSentinels -Port ($port + 2) -Expected $expectedPython -Bucket $pythonBucket + $scenario['sentinels_first_roaming'] = $gotRoam + $scenario['sentinels_first_python'] = $gotPy + Assert-True (($gotRoam -join ',') -eq ($expectedRoam -join ',')) ` + "first start: expected Roaming $($expectedRoam -join ',') but found $($gotRoam -join ',')" + Assert-True (($gotPy -join ',') -eq ($expectedPython -join ',')) ` + "first start: expected missed Python history $($expectedPython -join ',') but found $($gotPy -join ',')" + Stop-ScenarioServer $server + $server = $null + $firstLog = @($scenario["log_$($port + 2)"]) + Assert-True (@($firstLog | Where-Object { $_ -like "*$importMarker*" }).Count -ge 1) ` + 'first start did not log the post-migration Python import' + + # 5. Fresh-process restart: same content, no duplicates, import not repeated. + $server = Start-ScenarioServer -Name 'assert2' -Port ($port + 3) -ExtraArgs '--profile default' + $gotRoam = Get-FixtureSentinels -Port ($port + 3) + $gotPy = Get-FixtureSentinels -Port ($port + 3) -Bucket $pythonBucket + $scenario['sentinels_restart_roaming'] = $gotRoam + $scenario['sentinels_restart_python'] = $gotPy + Assert-True (($gotRoam -join ',') -eq ($expectedRoam -join ',')) ` + "restart: expected Roaming $($expectedRoam -join ',') but found $($gotRoam -join ',')" + Assert-True (($gotPy -join ',') -eq ($expectedPython -join ',')) ` + "restart: expected Python $($expectedPython -join ',') but found $($gotPy -join ',')" + Stop-ScenarioServer $server + $server = $null + $restartLog = @($scenario["log_$($port + 3)"]) + Assert-True (@($restartLog | Where-Object { $_ -like "*$importMarker*" }).Count -eq 0) ` + 'restart repeated the one-shot post-migration Python import' + + # 6. Originals retained: Roaming tree and the Python database itself. + $preserved = @(Get-ChildItem -LiteralPath (Join-Path $disposableProfile 'AppData\Roaming\activitywatch') ` + -Recurse -File -Filter 'sqlite*.db' -ErrorAction SilentlyContinue) + $scenario['preserved_roaming_databases'] = @($preserved | ForEach-Object { $_.FullName.Substring($disposableProfile.Length) }) + Assert-True ($preserved.Count -ge 1) 'original Roaming database is not retained under Roaming' + Assert-True (Test-Path -LiteralPath $pythonDb) 'the Python database must be left in place' + $scenario['result'] = 'pass' +} +catch { + $scenario['result'] = "fail: $($_.Exception.Message)" + throw +} +finally { + if ($null -ne $server) { Stop-ScenarioServer $server } +}