Files
ai-agent-harness/scripts/run-scheduled-verification.ps1

90 lines
2.6 KiB
PowerShell

[CmdletBinding()]
param(
[string] $WorkspaceRoot,
[string] $LogDirectory,
[ValidateRange(1, 3650)]
[int] $RetentionDays = 30
)
$ErrorActionPreference = 'Stop'
$scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$harnessRoot = Split-Path -Parent $scriptRoot
$verificationScript = Join-Path $scriptRoot 'verify-workspace.ps1'
if ([string]::IsNullOrWhiteSpace($WorkspaceRoot)) {
$WorkspaceRoot = Split-Path -Parent $harnessRoot
}
$resolvedWorkspaceRoot = [System.IO.Path]::GetFullPath($WorkspaceRoot)
if ([string]::IsNullOrWhiteSpace($LogDirectory)) {
$LogDirectory = Join-Path $harnessRoot '.local\verification-logs'
}
$resolvedLogDirectory = [System.IO.Path]::GetFullPath($LogDirectory)
[System.IO.Directory]::CreateDirectory($resolvedLogDirectory) | Out-Null
$timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$logFile = Join-Path $resolvedLogDirectory "verify-$timestamp.log"
$utf8WithoutBom = [System.Text.UTF8Encoding]::new($false)
$logWriter = [System.IO.StreamWriter]::new($logFile, $false, $utf8WithoutBom)
$exitCode = 1
function Write-VerificationLogLine {
param([AllowEmptyString()][string] $Line)
Write-Host $Line
$logWriter.WriteLine($Line)
$logWriter.Flush()
}
try {
Write-VerificationLogLine "[Agent Harness] scheduled verification started=$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss zzz')"
Write-VerificationLogLine "[Agent Harness] computer=$env:COMPUTERNAME"
Write-VerificationLogLine "[Agent Harness] log=$logFile"
$powershellCommand = Join-Path $PSHOME 'powershell.exe'
& $powershellCommand `
-NoProfile `
-NonInteractive `
-ExecutionPolicy Bypass `
-File $verificationScript `
-WorkspaceRoot $resolvedWorkspaceRoot 2>&1 |
ForEach-Object {
Write-VerificationLogLine $_.ToString()
}
$exitCode = $LASTEXITCODE
if ($null -eq $exitCode) {
$exitCode = 1
}
Write-VerificationLogLine "[Agent Harness] scheduled verification exitCode=$exitCode"
}
catch {
Write-VerificationLogLine "[Agent Harness] scheduled verification error=$($_.Exception.Message)"
$exitCode = 1
}
finally {
$logWriter.Dispose()
}
$retentionCutoff = (Get-Date).AddDays(-$RetentionDays)
try {
Get-ChildItem -LiteralPath $resolvedLogDirectory -File -Filter 'verify-*.log' |
Where-Object {
$_.FullName -ne $logFile -and $_.LastWriteTime -lt $retentionCutoff
} |
ForEach-Object {
Remove-Item -LiteralPath $_.FullName -Force
}
}
catch {
Write-Warning "Failed to remove an expired verification log: $($_.Exception.Message)"
}
exit $exitCode