Files
CASAN/packages/casan-harness/scripts/powershell/drift-detect.ps1
T
thanhnvandClaude Opus 4.8 36a4812ef3 refactor(structure): promote app to repo root + remove redundant workspace cruft
Standard production layout: the OKR app (was nested under AINative_OKR_CASAN5/) is now
the repository root. No more wrapper directory.

- Promote AINative_OKR_CASAN5/* -> repo root (backend/ frontend/ packages/ apps/
  .specify/ docs/ infra/ nginx/ scripts/ + configs). Merge tool dirs: .gitea (kept the
  active deploy ci.yml, added harness-ci.yml + runbooks), .claude (agents/commands +
  launch.json), .github moved up.
- Remove redundant: 00_SUBMISSION_PACKAGE, scattered root notes (FPT_CASAN_Full.md,
  tu-tuong-casan.md, casan-tu-sinh..., casan_harness_assessment.md, source-review...,
  README_CASAN5_REFINED.md), casan-next-plans/ and optimize-docs/ (competition/planning
  artifacts — roadmap + design history preserved in git log / commit messages).
- Update all references to the old layout:
  - .gitea/workflows/{ci,harness-ci}.yml, .github/workflows/{ci,deploy}.yml:
    working-directory .; drop AINative_OKR_CASAN5/ prefix; .specify/{tests,scripts}
    -> packages/casan-harness/... (.specify/logs state kept)
  - .claude/launch.json, .gitea/*-runbook.md: path prefixes
  - CLAUDE.md, README.md: docs/input -> apps/okr/domain/input
  - policy-bundle.yaml: 8 policy paths -> packages/casan-harness/...; manifest re-signed
- secrets-scan.sh: fixture excludes -> new package/domain paths.

Full gate from the new root: PASS=64 FAIL=0 SKIP=3.

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