Files
CASAN/AINative_OKR_CASAN5/.specify/scripts/powershell/drift-detect.ps1
T
2026-06-30 02:21:39 +09:00

94 lines
3.7 KiB
PowerShell

#!/usr/bin/env pwsh
# CASAN L5 Drift Detector - PowerShell port of drift-detect.sh
# Usage:
# drift-detect.ps1 <golden-file> <candidate-file> <report-json>
#
# Exit codes: 0=pass/warn, 2=drift-fail
param(
[Parameter(Mandatory=$true, Position=0)][string]$GoldenFile,
[Parameter(Mandatory=$true, Position=1)][string]$CandidateFile,
[Parameter(Mandatory=$true, Position=2)][string]$ReportFile
)
$ErrorActionPreference = "Stop"
$scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
$projectRoot = (Resolve-Path (Join-Path $scriptDir "../../..")).Path
$l5LogDir = Join-Path $projectRoot ".specify/logs/level5"
foreach ($d in @($l5LogDir, (Split-Path $ReportFile -Parent))) {
if ($d -and !(Test-Path $d)) { New-Item -ItemType Directory -Force -Path $d | Out-Null }
}
if (!(Test-Path $GoldenFile)) { Write-Error "Golden file not found: $GoldenFile"; exit 1 }
if (!(Test-Path $CandidateFile)) { Write-Error "Candidate file not found: $CandidateFile"; exit 1 }
function Get-Sha256 ([string]$text) {
$bytes = [System.Text.Encoding]::UTF8.GetBytes($text)
$hash = [System.Security.Cryptography.SHA256]::Create().ComputeHash($bytes)
return ($hash | ForEach-Object { $_.ToString("x2") }) -join ""
}
# Levenshtein-inspired similarity via longest common subsequence on words
function Get-SimilarityRatio ([string]$a, [string]$b) {
$wordsA = $a -split '\s+' | Where-Object { $_ }
$wordsB = $b -split '\s+' | Where-Object { $_ }
if ($wordsA.Count -eq 0 -and $wordsB.Count -eq 0) { return 1.0 }
if ($wordsA.Count -eq 0 -or $wordsB.Count -eq 0) { return 0.0 }
# Simple Jaccard similarity on word sets (fast, portable, no python needed)
$setA = [System.Collections.Generic.HashSet[string]]$wordsA
$setB = [System.Collections.Generic.HashSet[string]]$wordsB
$intersection = ($setA | Where-Object { $setB.Contains($_) }).Count
$union = ($setA + $setB | Sort-Object -Unique).Count
return [math]::Round($intersection / [math]::Max($union, 1), 4)
}
$golden = Get-Content $GoldenFile -Raw -Encoding UTF8
$candidate = Get-Content $CandidateFile -Raw -Encoding UTF8
if (!$golden) { $golden = "" }
if (!$candidate) { $candidate = "" }
$similarity = Get-SimilarityRatio $golden $candidate
$lenGolden = [math]::Max($golden.Length, 1)
$lengthDelta = [math]::Round([math]::Abs($candidate.Length - $golden.Length) / $lenGolden, 4)
$driftStatus = "pass"
$driftAction = "allow"
if ($similarity -lt 0.70 -or $lengthDelta -gt 0.50) {
$driftStatus = "fail"; $driftAction = "block_or_fallback"
} elseif ($similarity -lt 0.85 -or $lengthDelta -gt 0.30) {
$driftStatus = "warn"; $driftAction = "require_review"
}
$timestamp = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
$goldenHash = Get-Sha256 $golden
$candidHash = Get-Sha256 $candidate
$report = @"
{
"timestamp": "$timestamp",
"harness": "L5-drift-detection",
"status": "$driftStatus",
"action": "$driftAction",
"similarity_ratio": $similarity,
"length_delta_ratio": $lengthDelta,
"golden_hash": "$goldenHash",
"candidate_hash": "$candidHash",
"golden_file": "$GoldenFile",
"candidate_file": "$CandidateFile"
}
"@
Set-Content -Path $ReportFile -Value $report -Encoding UTF8
# Append to L5 log
$logLine = "{`"timestamp`":`"$timestamp`",`"harness`":`"L5-drift-detection`",`"status`":`"$driftStatus`",`"similarity`":$similarity,`"length_delta`":$lengthDelta,`"golden`":`"$GoldenFile`",`"candidate`":`"$CandidateFile`"}"
Add-Content -Path (Join-Path $l5LogDir "drift.jsonl") -Value $logLine -Encoding UTF8
Write-Output "DRIFT_$($driftStatus.ToUpper()) similarity=$similarity length_delta=$lengthDelta report=$ReportFile"
if ($driftStatus -eq "fail") { exit 2 }
exit 0