pipeline {
  agent { label 'agent-harness' }

  options {
    disableConcurrentBuilds()
    skipDefaultCheckout(true)
    timestamps()
  }

  triggers {
    cron('TZ=Asia/Seoul\nH 8 * * 1-5')
  }

  stages {
    stage('Verify Workspace') {
      steps {
        powershell '''
          $ErrorActionPreference = 'Stop'

          if ([string]::IsNullOrWhiteSpace($env:KRI_DEV_ROOT)) {
            throw 'KRI_DEV_ROOT must point to the existing KRI multi-repository workspace root.'
          }

          $harnessRoot = Join-Path $env:KRI_DEV_ROOT '_________AgentHarnessBase'
          $verificationScript = [System.IO.Path]::Combine(
            $harnessRoot,
            'scripts',
            'verify-workspace.ps1'
          )

          if (-not (Test-Path -LiteralPath $verificationScript -PathType Leaf)) {
            throw "Workspace verification script not found: $verificationScript"
          }

          & $verificationScript -WorkspaceRoot $env:KRI_DEV_ROOT
          if ($LASTEXITCODE -ne 0) {
            exit $LASTEXITCODE
          }
        '''
      }
    }
  }

  post {
    success {
      echo 'All registered Agent Harness documents are current.'
    }
    failure {
      echo 'Agent Harness drift or an invalid workspace registration was detected.'
    }
  }
}
