# wal-flush.ps1 - WAL persist-only Stop hook (H22 S111). KHONG sinh noi dung, KHONG push, KHONG block.
# Fail-open MOI nhanh: exit 0 ke ca loi (Stop-hook exit!=0 co hanh vi rieng - CAM).
# Per-path add + CAND commit (X3: add/commit da-pathspec = all-or-nothing khi 1 path vang).
try {
  $root = if ($env:CLAUDE_PROJECT_DIR) { $env:CLAUDE_PROJECT_DIR } else { "." }
  Set-Location $root
  $paths = @(".claude/WAL.md", ".claude/agent-memory/", ".claude/workflows/runs/")
  $cand = @()
  foreach ($p in $paths) {
    git add -- $p 2>$null
    git diff --cached --quiet -- $p 2>$null
    if ($LASTEXITCODE -ne 0) { $cand += $p }
  }
  if ($cand.Count -gt 0) {
    $ts = Get-Date -Format "yyyyMMddTHHmm"
    git commit -q -m "wal: flush $ts" -- $cand 2>$null | Out-Null
  }
} catch { }
exit 0
