Files
CASAN/AINative_OKR_CASAN5/packages/casan-harness/scripts/powershell/drift-detect.ps1
T
thanhnvandClaude Opus 4.8 664bd1f00c feat(plan-01): Phase 1 — relocate harness code to packages/casan-harness (symlink facade)
Physically move the pure-code subtrees out of .specify into the package, leaving
compat symlinks at the old .specify/<dir> paths so every existing reference (internal
CASAN_HARNESS_ROOT + external CI/docker/mjs) keeps resolving. Runtime state stays put.

Moved (git mv): scripts/ tests/ security/ templates/ config/ governance/ memory/
  .specify/<dir>  ->  packages/casan-harness/<dir>   (+ .specify/<dir> symlink)
Stays in .specify (state/governance/domain, handled later): logs/ agentops/ level5/
  init-options.json traceability-map.json

Python `.resolve()` self-location followed the compat symlink into packages and lost
the app root; generate-casan-demo-context.py, generate-agentops-dashboard.py and
dashboard-server.py now walk UP for the `.specify` state marker instead of a fixed
parent depth (fixes "missing trace files" in run-casan4).

Full gate: PASS=64 FAIL=0 SKIP=3 (CASAN_CI_STEP_TIMEOUT_SEC=1200 — track-a ~450s runs
close to the 600s default and can tip over under load; this is timing variance, not a
regression — it passed cleanly with headroom). Runtime log/audit artifacts kept unstaged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 00:06:00 +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