# rebuild_bootpack_full_hub_v1.1.ps1 (regenerated, FIX10: snapshot fallback to latest) # Purpose : Safe preview of BootPack rebuild context (HUB) with deterministic ASCII output. # Notes : PS 5.1 strict, UTF-8 with BOM, ASCII-only text. No here-strings. No PS7 operators. No ternary. # PREVIEW only (no write). Adds robust snapshot selection: if today's snapshot is missing, # falls back to the latest snapshot_*.json found under snapshots/. # Encoding: UTF-8 with BOM param( [Parameter(Mandatory=$true)] [string]$Root, [switch]$Preview, [switch]$Write ) # --- Utilities (PS 5.1) ------------------------------------------------------- function Sha256Hex([string]$p){ if([string]::IsNullOrWhiteSpace($p)){ return "MISSING" } if(Test-Path -LiteralPath $p){ return (Get-FileHash -LiteralPath $p -Algorithm SHA256).Hash } else { return "MISSING" } } function SizeBytes([string]$p){ if([string]::IsNullOrWhiteSpace($p)){ return 0 } if(Test-Path -LiteralPath $p){ return (Get-Item -LiteralPath $p).Length } else { return 0 } } function IsoTime([string]$p){ if([string]::IsNullOrWhiteSpace($p)){ return "MISSING" } if(Test-Path -LiteralPath $p){ return (Get-Item -LiteralPath $p).LastWriteTime.ToString("o") } else { return "MISSING" } } function Count-KbEntries([string]$Path){ if(-not (Test-Path -LiteralPath $Path)){ return -1 } try{ $raw = Get-Content -LiteralPath $Path -Raw -Encoding UTF8 # purge internal U+FEFF if any $raw = $raw -replace "`uFEFF","" # try direct parse try{ $json = $raw | ConvertFrom-Json }catch{ # strip trailing noise after last closing brace $last = $raw.LastIndexOf('}') if($last -gt 0){ $raw = $raw.Substring(0, $last + 1) } # remove C/JS comments if present $raw = [regex]::Replace($raw, '/\*.*?\*/', '', 'Singleline') $raw = [regex]::Replace($raw, '^\s*//.*$', '', 'Multiline') $json = $raw | ConvertFrom-Json } if($null -ne $json.entries){ if($json.entries -is [System.Array]){ return $json.entries.Count } $props = $json.entries.PSObject.Properties return @($props).Count } else { return 0 } }catch{ return -1 } } function Read-KbPointerFromBootPack([string]$BootPackPath){ if(-not (Test-Path -LiteralPath $BootPackPath)){ return $null } try{ $bp = Get-Content -LiteralPath $BootPackPath -Raw -Encoding UTF8 $bp = $bp -replace "`uFEFF","" $m = [regex]::Match($bp, '^\[BUG_KB_JSON_POINTER\][\r\n]+Path=(.+)$', 'Multiline') if($m.Success){ return $m.Groups[1].Value.Trim() } return $null }catch{ return $null } } # --- Resolve paths ------------------------------------------------------------ if([string]::IsNullOrWhiteSpace($Root)){ Write-Host "[NOK] Root is empty." -ForegroundColor Red return } $BootPack = Join-Path $Root "bootpack\bootpack.txt" $ProfileFile = Join-Path $Root "profiles\project\HUB\PROJECT_PROFILE.txt" # Snapshot selection: prefer today's, else latest snapshot_*.json $snapDir = Join-Path $Root "snapshots" $Snapshot = Join-Path $snapDir ("snapshot_{0}.json" -f (Get-Date -Format yyyy-MM-dd)) if(-not (Test-Path -LiteralPath $Snapshot)){ if(Test-Path -LiteralPath $snapDir){ $candidates = Get-ChildItem -LiteralPath $snapDir -Filter "snapshot_*.json" -File | Sort-Object LastWriteTime -Descending if($candidates -and $candidates.Count -gt 0){ $Snapshot = $candidates[0].FullName } } } $KBPointerPath = Read-KbPointerFromBootPack -BootPackPath $BootPack if([string]::IsNullOrWhiteSpace($KBPointerPath)){ $KBPointerPath = Join-Path $Root "bug_kb\BUG_KB.json.txt" } # --- Measures ----------------------------------------------------------------- $kbSha = Sha256Hex $KBPointerPath $kbEntries = Count-KbEntries $KBPointerPath $kbSize = SizeBytes $KBPointerPath $kbUpdated = IsoTime $KBPointerPath $profileSha = Sha256Hex $ProfileFile $snapSha = Sha256Hex $Snapshot # ruleset (ASCII only) $L0 = @( "GOV_SCRIPT_GATE v1.4", "SAFE-WRITE v1.1", "TXT-ONLY v1.0", "SYNC-MEM-ARCHIVE-RULE v1.0", "SYNC-GUARD v1.1" ) $rules = ($L0 -join " ; ") $memId = (Get-Date).ToString("s") $manifestSha = "UNKNOWN" # --- Preview plan (deterministic, ASCII) -------------------------------------- Write-Host "== PREVIEW :: REBUILD BOOTPACK (HUB) ==" Write-Host ("Target BootPack : {0}" -f $BootPack) Write-Host ("PROFILE_FILE : {0} (SHA={1})" -f $ProfileFile, $profileSha) Write-Host ("KB Pointer : {0} (SHA={1}, Entries={2}, Size={3}, Updated={4})" -f $KBPointerPath,$kbSha,$kbEntries,$kbSize,$kbUpdated) Write-Host ("SNAPSHOT_FILE : {0} (SHA={1})" -f $Snapshot, $snapSha) Write-Host ("RULESET_ACTIVE : {0}" -f $rules) Write-Host ("MEM_SYNC_ID : {0}" -f $memId) Write-Host ("MANIFEST_SHA256 : {0}" -f $manifestSha) # --- Mode handling ------------------------------------------------------------ if($Preview -or -not $Write){ Write-Host "Mode PREVIEW : aucune ecriture effectuee." return } Write-Host "[BLOCK] Write path is disabled in this micro-step." -ForegroundColor Yellow return