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

90 lines
3.7 KiB
PowerShell

#!/usr/bin/env pwsh
# CASAN L5 Tool Registry Gate - PowerShell port of tool-registry-gate.sh
# Usage:
# tool-registry-gate.ps1 <tool-id> [idempotency-key]
#
# Exit codes: 0=approved, 2=denied, 64=usage error
param(
[Parameter(Mandatory=$true, Position=0)][string]$ToolId,
[Parameter(Position=1)][string]$IdempotencyKey = ""
)
$ErrorActionPreference = "Stop"
$scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
$projectRoot = (Resolve-Path (Join-Path $scriptDir "../../..")).Path
$registry = Join-Path $projectRoot ".specify/level5/tool-registry.yaml"
$logDir = Join-Path $projectRoot ".specify/logs/level5"
$txLog = Join-Path $logDir "tool-registry.jsonl"
$toolCallLog = Join-Path $projectRoot ".specify/logs/audit/tool-calls.jsonl"
if (!(Test-Path $logDir)) { New-Item -ItemType Directory -Force -Path $logDir | Out-Null }
if (!(Test-Path (Split-Path $toolCallLog -Parent))) { New-Item -ItemType Directory -Force -Path (Split-Path $toolCallLog -Parent) | Out-Null }
function New-TraceId {
try { return [System.Guid]::NewGuid().ToString("D") } catch { return "tool-$(Get-Date -Format 'yyyyMMddHHmmss')-$PID" }
}
if (!(Test-Path $registry)) {
Write-Error "TOOL_REGISTRY_ERROR: registry not found at $registry"
exit 1
}
$traceId = New-TraceId
$timestamp = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
# ── Parse YAML registry (simple line-based parser) ─────────────────────────
$yamlText = Get-Content $registry -Raw
$toolBlocks = $yamlText -split "\n\s*-\s+id:\s+"
$tools = @{}
foreach ($block in $toolBlocks[1..($toolBlocks.Count-1)]) {
$lines = $block -split "`n"
$tid = $lines[0].Trim()
$attrs = @{ id = $tid }
foreach ($line in $lines[1..($lines.Count-1)]) {
if ($line -match '^\s{4}(\w[^:]+):\s+(.+)$') {
$k = $Matches[1].Trim(); $v = $Matches[2].Trim().Trim('"')
$attrs[$k] = $v
}
}
$tools[$tid] = $attrs
}
$tool = $tools[$ToolId]
if (!$tool) {
$line = "{`"timestamp`":`"$timestamp`",`"trace_id`":`"$traceId`",`"harness`":`"L5-tool-registry`",`"tool_id`":`"$ToolId`",`"decision`":`"denied`",`"reason`":`"unknown_tool`"}"
Add-Content -Path $txLog -Value $line -Encoding UTF8
Write-Error "TOOL_DENIED unknown_tool=$ToolId"
exit 2
}
$sideEffect = $tool["side_effect"] -eq "true"
$idemRequired = $tool["idempotency_required"] -eq "true"
$riskLevel = $tool["risk_level"]
$owner = $tool["owner"]
$decision = "approved"
$reason = "registered"
if ($sideEffect -and $idemRequired -and [string]::IsNullOrEmpty($IdempotencyKey)) {
$decision = "denied"
$reason = "missing_idempotency_key"
}
$record = "{`"timestamp`":`"$timestamp`",`"trace_id`":`"$traceId`",`"harness`":`"L5-tool-registry`",`"tool_id`":`"$ToolId`",`"owner`":`"$owner`",`"risk_level`":`"$riskLevel`",`"side_effect`":$($sideEffect.ToString().ToLower()),`"idempotency_required`":$($idemRequired.ToString().ToLower()),`"idempotency_key_present`":$(-not [string]::IsNullOrEmpty($IdempotencyKey) | ForEach-Object { $_.ToString().ToLower() }),`"decision`":`"$decision`",`"reason`":`"$reason`"}"
Add-Content -Path $txLog -Value $record -Encoding UTF8
# Per-call audit in tool-calls.jsonl (H2 audit requirement)
$auditLine = "{`"timestamp`":`"$timestamp`",`"trace_id`":`"$traceId`",`"tool`":`"$ToolId`",`"idempotency_key`":`"$IdempotencyKey`",`"decision`":`"$decision`",`"reason`":`"$reason`",`"risk_level`":`"$riskLevel`",`"owner`":`"$owner`"}"
Add-Content -Path $toolCallLog -Value $auditLine -Encoding UTF8
Write-Output "TOOL_$($decision.ToUpper()) tool=$ToolId reason=$reason"
if ($decision -ne "approved") {
Write-Error "TOOL_REGISTRY_DENIED: '$ToolId' — $reason. Set CASAN_IDEMPOTENCY_KEY env var."
exit 2
}
exit 0