public/Undo-OctaRun.ps1

function Undo-OctaRun {
    <#
        .SYNOPSIS
        Reverts a prior Octa run using its recorded prior-state snapshots (FR-008).

        .PARAMETER RunId
        A specific run id (folder name), or 'latest' for the most recent run.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][string]$RunId
    )

    $strings = Get-OctaStrings
    $record = Get-OctaRunRecord -RunId $RunId
    if (-not $record) {
        return [pscustomobject]@{ Status = 'NotFound'; Message = ($strings.undoNotFound -f $RunId) }
    }

    if ($record.Status -eq 'Undone') {
        return [pscustomobject]@{ Status = 'AlreadyUndone'; Message = 'This run was already undone.' }
    }

    $restored = @()
    $couldNotUndo = @($record.IrreversibleActionRefs)

    foreach ($snapshot in @($record.ActionSnapshots)) {
        try {
            Restore-OctaActionSnapshot -Snapshot $snapshot -RunFolder $record.RunFolder
            $restored += $snapshot.ActionRef
            Write-Host ($strings.undoRestored -f $snapshot.ActionRef)
        }
        catch {
            $couldNotUndo += $snapshot.ActionRef
        }
    }

    foreach ($ref in $couldNotUndo) {
        Write-Host ($strings.undoIrreversible -f $ref)
    }

    Add-Member -InputObject $record -MemberType NoteProperty -Name 'Status' -Value 'Undone' -Force
    Save-OctaRunRecord -RunFolder $record.RunFolder -RunRecord $record

    return [pscustomobject]@{ Status = 'Success'; Restored = $restored; CouldNotUndo = $couldNotUndo }
}