Files
CASAN/packages/casan-harness/scripts/powershell/casan-harness.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

106 lines
4.3 KiB
PowerShell

#!/usr/bin/env pwsh
# CASAN unified harness wrapper - PowerShell port of casan-harness.sh
# Usage:
# casan-harness.ps1 <input-file> <output-file> [action-name] [-- <command> [args...]]
#
# Runs: H4-input → H5-governance → H6-metrics(real cmd) → H4-output
# Includes idempotency caching without bypassing H4/H5/H4-output gates.
param(
[Parameter(Mandatory=$true, Position=0)][string]$InputFile,
[Parameter(Mandatory=$true, Position=1)][string]$FinalOutput,
[Parameter(Position=2)][string]$ActionName = "agent_step",
[Parameter(ValueFromRemainingArguments=$true)][string[]]$RemainingArgs
)
$ErrorActionPreference = "Stop"
$scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
$projectRoot = (Resolve-Path (Join-Path $scriptDir "../../..")).Path
$tmpDir = Join-Path $projectRoot ".specify/logs/tmp"
$cacheDir = Join-Path $projectRoot ".specify/logs/idempotency"
foreach ($d in @($tmpDir, $cacheDir, (Split-Path $FinalOutput -Parent))) {
if ($d -and !(Test-Path $d)) { New-Item -ItemType Directory -Force -Path $d | Out-Null }
}
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 ""
}
# Strip "--" separator
$cmdArgs = $RemainingArgs
if ($cmdArgs -and $cmdArgs[0] -eq "--") { $cmdArgs = $cmdArgs[1..($cmdArgs.Count-1)] }
# ── Idempotency check ──────────────────────────────────────────────────────
$inputContent = Get-Content $InputFile -Raw -Encoding UTF8
if (!$inputContent) { $inputContent = "" }
$cmdStr = if ($cmdArgs) { $cmdArgs -join " " } else { "no_cmd" }
$inputHash = Get-Sha256 $inputContent
$cmdHash = Get-Sha256 $cmdStr
$idemKey = Get-Sha256 "$inputHash|$cmdHash|$ActionName"
$cacheMeta = Join-Path $cacheDir "$idemKey.json"
$cacheOut = Join-Path $cacheDir "$idemKey.output"
# ── Normal flow ────────────────────────────────────────────────────────────
$suffix = "$(Get-Date -Format 'yyyyMMddHHmmss')-$PID"
$safeInput = Join-Path $tmpDir "security-input-$suffix.txt"
$approvedIn = Join-Path $tmpDir "governance-approved-$suffix.txt"
$rawOutput = Join-Path $tmpDir "raw-output-$suffix.txt"
# H4 input security
& "$scriptDir/security-check.ps1" $InputFile $safeInput input
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
# H5 governance
& "$scriptDir/governance-check.ps1" $safeInput $approvedIn $ActionName
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
# H6 metrics around real command or cached output.
if ((Test-Path $cacheMeta) -and (Test-Path $cacheOut)) {
Copy-Item -Path $cacheOut -Destination $rawOutput -Force
$metricsExit = 0
$cacheStatus = "cached"
} elseif ($cmdArgs -and $cmdArgs.Count -gt 0) {
& "$scriptDir/agent-metrics.ps1" $approvedIn $rawOutput "--" @cmdArgs
$metricsExit = $LASTEXITCODE
$cacheStatus = "stored"
} else {
& "$scriptDir/agent-metrics.ps1" $approvedIn $rawOutput
$metricsExit = $LASTEXITCODE
$cacheStatus = "stored"
}
# H4 output security
& "$scriptDir/security-check.ps1" $rawOutput $FinalOutput output
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
# ── Save idempotency cache ─────────────────────────────────────────────────
$outputContent = Get-Content $FinalOutput -Raw -Encoding UTF8
if (!$outputContent) { $outputContent = "" }
$outputHash = Get-Sha256 $outputContent
if ($cacheStatus -eq "stored") {
@"
{
"idempotency_key": "$idemKey",
"timestamp": "$($(Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ"))",
"action": "$ActionName",
"command": "$(($cmdStr -replace '"','\"'))",
"output_hash": "$outputHash"
}
"@ | Set-Content -Path $cacheMeta -Encoding UTF8
Copy-Item -Path $FinalOutput -Destination $cacheOut -Force
}
# Clean up tmp files
foreach ($f in @($safeInput, $approvedIn, $rawOutput)) {
if (Test-Path $f) { Remove-Item $f -Force -ErrorAction SilentlyContinue }
}
Write-Output "CASAN_HARNESS_COMPLETE cache=$cacheStatus key=$idemKey output=$FinalOutput"
exit $metricsExit