Payload/scripts/kaden/harness/harness-own-slice-status.ps1

#Requires -Version 5.1
<#
.SYNOPSIS
    Executable "own it" checklist from implementation through review_ready.

.DESCRIPTION
    Prints a machine-readable + human checklist of what remains before ownership_status
    can honestly be review_ready. Does NOT run agents or forge reviewer/quality artifacts.
    Use when the operator says OWN IT / move to gates -- so "own" is a runnable status, not
    a prompt interpretation.

    -Next prints exactly one hop command (or a review_ready COMMIT line) with no checklist.
    Open Low/suggestion findings from quality reports and slice-verdict always appear in
    the checklist view (before and after review_ready).

.EXAMPLE
    pwsh -File scripts/kaden/harness-invoke.ps1 harness-own-slice-status.ps1 -Feature saved-search-queries -Slice mo-save-list

.EXAMPLE
    pwsh -File scripts/kaden/harness-invoke.ps1 harness-own-slice-status.ps1 -Feature saved-search-queries -Slice mo-save-list -Next
#>

[CmdletBinding()]
param(
    [Parameter(Mandatory = $true)]
    [string]$Feature,

    [Parameter(Mandatory = $true)]
    [string]$Slice,

    [string]$FeatureRoot = '',

    [string]$RepoRoot = '',

    [switch]$AllowArbitraryFeatureRoot,

    [switch]$Json,

    [switch]$Next,

    [switch]$CommitReady
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

Import-Module (Join-Path $PSScriptRoot 'lib\HarnessContract.psm1') -Force
Import-Module (Join-Path $PSScriptRoot 'lib\HarnessJournal.psm1') -Force
Test-HarnessContractModuleReady

if ([string]::IsNullOrWhiteSpace($RepoRoot)) {
    $RepoRoot = Get-HarnessRepoRoot -StartPath $PSScriptRoot
}

$featureRootPath = Get-HarnessFeatureRoot -RepoRoot $RepoRoot -Feature $Feature -FeatureRootOverride $FeatureRoot -AllowArbitraryFeatureRoot:$AllowArbitraryFeatureRoot

$result = Get-HarnessOwnSliceReport -FeatureRoot $featureRootPath -Feature $Feature -Slice $Slice -RepoRoot $RepoRoot
$nextStep = $result.next_step
$todoCount = $result.remaining_count

if ($CommitReady) {
    $commitFindings = @(Get-HarnessCommitReadyOpenFindings -FeatureRoot $featureRootPath -SliceId $Slice -RepoRoot $RepoRoot)
    if ($commitFindings.Count -gt 0) {
        Write-Output "COMMIT blocked: $($commitFindings.Count) gate_blocking finding(s) without disposition"
        foreach ($f in $commitFindings) {
            $fix = [string]$f.suggested_fix
            Write-Output (" {0} ({1}) origin={2} location={3}" -f $f.id, $f.severity, $f.origin, $f.location)
            if ($fix) { Write-Output " suggested_fix: $fix" }
        }
        exit 2
    }
    exit 0
}

if ($Next) {
    if ($result.ownership_status -eq 'review_ready' -and $todoCount -eq 0) {
        Write-Output 'review_ready -- ready for human COMMIT'
    }
    elseif ($nextStep -and -not [string]::IsNullOrWhiteSpace([string]$nextStep.next)) {
        Write-Output ([string]$nextStep.next)
    }
    else {
        Write-Output 'no next command available'
    }
    if ($todoCount -gt 0) {
        exit 2
    }
    exit 0
}

if (-not $Json) {
    Format-HarnessOwnSliceHumanReport -Report $result -Header 'Own-slice status' | ForEach-Object { Write-Output $_ }
}

$result | ConvertTo-Json -Depth 6
if ($todoCount -gt 0) {
    exit 2
}

exit 0