Public/Invoke-KadenOperatorModelApply.ps1

function Invoke-KadenOperatorModelApply {
    <#
    .SYNOPSIS
        Apply the child operator model file to managed agent frontmatter.
    .DESCRIPTION
        Reads gitignored .kaden.model.json at the child root. Writes the declared
        model, verbatim, into Kaden-managed agents for that host stack. A stack
        with no entry is left untuned and named in the outcome. The operator file
        is not modified. This is the same apply step Initialize-KadenRepo and
        Update-KadenRepo run after a completed managed apply.
        Extra fields are ignored. No class, price, floor, or cap behavior runs.
        When an update transaction is incomplete, this command changes no
        frontmatter and does not record its outcome on that transaction.
    .PARAMETER Path
        Child repository root. Relative paths, including '.', use the current location.
    .EXAMPLE
        Invoke-KadenOperatorModelApply -Path .
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Parameter(Mandatory = $true)]
        [string]$Path
    )

    $childRoot = Resolve-KadenChildRoot -Path $Path
    if (-not (Test-Path -LiteralPath $childRoot -PathType Container)) {
        throw "KDN-TXN-PREFLIGHT-FAILED: Child repository path does not exist: $childRoot"
    }
    $sharedPath = Join-Path $childRoot '.kaden.json'
    if (-not (Test-Path -LiteralPath $sharedPath -PathType Leaf)) {
        throw 'KDN-TXN-PREFLIGHT-FAILED: Child repository is not initialized (.kaden.json missing). Run Initialize-KadenRepo first.'
    }
    if (-not $PSCmdlet.ShouldProcess($childRoot, 'Apply operator model choices')) {
        return
    }
    if (Test-KadenIncompleteTransaction -ChildRoot $childRoot) {
        $operatorRelative = '.kaden.model.json'
        $operatorPath = Join-Path $childRoot $operatorRelative
        $fingerprint = Get-KadenOperatorFileFingerprint -LiteralPath $operatorPath
        $unchanged = Test-KadenOperatorFileBytesUnchanged -Before $fingerprint -After $fingerprint
        $outcome = New-KadenOperatorModelApplyOutcome -Status 'skipped' -Reason 'an incomplete Kaden transaction remains; run Repair-KadenRepo before applying' -Path $childRoot -OperatorFile $operatorRelative -OperatorFileBytesUnchanged $unchanged -Changed $false -Stacks @() -TunedFiles @() -NotTuned @()
        try {
            Write-KadenOperatorModelOutcome -ChildRoot $childRoot -Outcome $outcome -SkipBesideJournal
        } catch {
            $outcome.Reason = "$($outcome.Reason); outcome file was not recorded: $($_.Exception.Message)"
        }
        return $outcome
    }
    return Invoke-KadenOperatorModelApplySafe -ChildRoot $childRoot
}