Files
ARCADIA-Studio/harness/experiments/scenario-spacing-gates-0409a48-20260913/original/finalize.ps1
T

100 lines
12 KiB
PowerShell

#Requires -Version 7.0
param([Parameter(Mandatory=$true)][ValidatePattern('^[0-9a-f]{40}$')][string]$SourceCommit,[Parameter(Mandatory=$true)][switch]$Reviewed)
Set-StrictMode -Version Latest
$ErrorActionPreference='Stop'
$workspace='C:/Users/User/Documents/Git/ARCADIA-Studio/.local/scenario-spacing-ytr83r-20260913'
$root='C:/Users/User/Documents/Git/ARCADIA-Studio'
if(-not $Reviewed){throw 'Parent must review the integrated source and closed diagnostic before finalization.'}
if(Test-Path -LiteralPath (Join-Path $PSScriptRoot 'finalization.json')){throw 'Already finalized; preserve this gate.'}
foreach($name in @('enumeration.json','expected-inputs.json','external-inputs.json','inputs-complete-before.json','toolchain-versions.json','list.log','list-receipt.json')){
if(Test-Path -LiteralPath (Join-Path $PSScriptRoot $name)){throw ('Prior capture or run exists: '+$name)}
}
function Hash-Input([string]$Path){(Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()}
function Save-Record([string]$Name,$Record){$Record|ConvertTo-Json -Depth 16|Set-Content -LiteralPath (Join-Path $PSScriptRoot $Name) -Encoding utf8}
Push-Location -LiteralPath $workspace
try {
$head=git rev-parse HEAD
if($LASTEXITCODE -ne 0 -or $head -cne $SourceCommit){throw 'Source is not at the reviewed pin.'}
$status=@(git status --porcelain)
if($LASTEXITCODE -ne 0 -or $status.Count){throw 'Source must be committed and clean.'}
$correction=Get-Content -Raw -LiteralPath (Join-Path $PSScriptRoot 'correction-template.json')|ConvertFrom-Json
$changes=@(git diff --name-only $correction.previous_source $SourceCommit)
if($LASTEXITCODE -ne 0 -or $changes.Count -ne 3 -or @(Compare-Object @($correction.expected_changed_paths|Sort-Object) @($changes|Sort-Object)).Count){throw 'Expected exactly the three reviewed test-file changes from c4.'}
$reviewDiff=@(git diff --name-only $correction.reviewed_correction_source $SourceCommit)
if($LASTEXITCODE -ne 0 -or $reviewDiff.Count){throw 'Integrated tree differs from reviewed 0409 correction; revise/review the template first.'}
$instructionBlob=git rev-parse 'HEAD:AGENTS.md'
if($LASTEXITCODE -ne 0 -or $instructionBlob -cne $correction.instruction_git_blob -or (Get-Item -LiteralPath 'AGENTS.md').Length -ne $correction.instruction_bytes -or (Hash-Input 'AGENTS.md') -cne $correction.instruction_sha256){throw 'User instructions differ from reviewed bytes.'}
$serverChanges=@(git diff --name-only 37be41092beb1c940f345072df1e017ce45fe2fc $SourceCommit -- crates/oa-core crates/oa-server Cargo.toml Cargo.lock rust-toolchain.toml)
if($LASTEXITCODE -ne 0 -or $serverChanges.Count){throw 'Core/server provenance changed; parent must revise gate scope.'}
} finally {Pop-Location}
# All evidence admission precedes mutation of the pending template or source captures.
foreach($binding in $correction.prior_gate_bindings){
if((Get-Item -LiteralPath $binding.path).Length -ne $binding.bytes -or (Hash-Input $binding.path) -cne $binding.sha256){throw ('Prior evidence changed: '+$binding.path)}
}
$priorGate=Split-Path -Parent $correction.previous_capture.path
$list=Get-Content -Raw -LiteralPath (Join-Path $priorGate 'list-receipt.json')|ConvertFrom-Json
$focused=Get-Content -Raw -LiteralPath (Join-Path $priorGate 'focused-receipt.json')|ConvertFrom-Json
if($list.base -cne $correction.previous_source -or $list.cargoExitCode -ne 0 -or -not $list.selectionValid -or -not $list.inputsUnchanged -or @($list.listedTests).Count -ne 1082){throw 'Prior c4 listing is not the bound successful 1082 inventory.'}
$focusedResult=@($focused.testResultSummaries)
if($focused.base -cne $correction.previous_source -or $focused.cargoExitCode -ne 101 -or $focused.selectionValid -or -not $focused.inputsUnchanged -or $focusedResult.Count -ne 1 -or $focusedResult[0].passed -ne 286 -or $focusedResult[0].failed -ne 2 -or $focusedResult[0].ignored -ne 0 -or $focusedResult[0].filtered -ne 794){throw 'Prior focused failure differs from reviewed 286 passed / 2 failed evidence.'}
$diagnosticPath=Join-Path $correction.diagnostic_directory 'diagnostic-all-receipt.json'
if(-not (Test-Path -LiteralPath $diagnosticPath)){throw 'Wait for the full c4 diagnostic to close and parent review; no inferred result is permitted.'}
$diagnostic=Get-Content -Raw -LiteralPath $diagnosticPath|ConvertFrom-Json
if($diagnostic.source_commit -cne $correction.previous_source -or $diagnostic.classification -cne 'diagnostic_after_failed_focused_gate' -or $diagnostic.successful_full_gate -or $diagnostic.waives_failed_focused_gate -or -not $diagnostic.complete_inventory_valid -or -not $diagnostic.inputs_unchanged -or $diagnostic.postflight_failure -or @($diagnostic.listed_tests).Count -ne 1082 -or @($diagnostic.ignored_tests).Count -ne 4){throw 'Diagnostic must have closed with valid complete inventory and unchanged inputs; it is not an acceptance gate.'}
if(@(Compare-Object @($list.listedTests|Sort-Object) @($diagnostic.listed_tests|Sort-Object)).Count){throw 'Diagnostic does not cover the exact old list.'}
$diagnosticHashes=[ordered]@{'diagnostic-all.log'=$diagnostic.raw_log_sha256;'provenance-before.json'=$diagnostic.provenance_sha256;'inputs-complete-diagnostic-all-before.json'=$diagnostic.before_capture_sha256;'inputs-complete-diagnostic-all-after.json'=$diagnostic.after_capture_sha256}
foreach($name in $diagnosticHashes.Keys){if((Hash-Input (Join-Path $correction.diagnostic_directory $name)) -cne $diagnosticHashes[$name]){throw ('Diagnostic evidence changed: '+$name)}}
$diagnosticBindings=@(foreach($name in @('diagnostic-all-receipt.json')+@($diagnosticHashes.Keys)){$path=Join-Path $correction.diagnostic_directory $name;[ordered]@{path=$path;bytes=(Get-Item -LiteralPath $path).Length;sha256=(Hash-Input $path)}})
foreach($name in @('run.ps1','capture.ps1','enumerate-inputs.ps1','diagnostic.ps1')){
$path=Join-Path $PSScriptRoot $name
$text=[IO.File]::ReadAllText($path)
if(-not $text.Contains('PENDING_REVIEWED_SOURCE_PIN')){throw 'Template was already modified; inspect before retry.'}
[IO.File]::WriteAllText($path,$text.Replace('PENDING_REVIEWED_SOURCE_PIN',$SourceCommit),[Text.UTF8Encoding]::new($false))
}
$selection=Get-Content -Raw -LiteralPath (Join-Path $PSScriptRoot 'expected-tests.json')|ConvertFrom-Json
if(@($selection.tests).Count -ne 289 -or @($selection.tests|Sort-Object -Unique).Count -ne 289 -or @($selection.desktop_tests).Count -ne 1083 -or @($selection.desktop_tests|Sort-Object -Unique).Count -ne 1083){throw 'Expected membership changed; parent must review selection.'}
if(@($selection.tests|Where-Object {$_ -cnotin $selection.desktop_tests}).Count){throw 'Focused test absent from expected desktop listing.'}
$selection.source_commit=$SourceCommit
Save-Record 'expected-tests.json' $selection
& (Join-Path $PSScriptRoot 'enumerate-inputs.ps1')
$enumeration=Get-Content -Raw -LiteralPath (Join-Path $PSScriptRoot 'enumeration.json')|ConvertFrom-Json
$inputs=@(foreach($relative in $enumeration.gitPaths){$path=Join-Path $workspace $relative;$item=Get-Item -LiteralPath $path;[ordered]@{path=$relative;bytes=$item.Length;sha256=(Hash-Input $path)}})
$previousCapture=$correction.previous_capture
$previous=Get-Content -Raw -LiteralPath $previousCapture.path|ConvertFrom-Json
if($previous.base -cne $correction.previous_source -or $previous.count -ne 610 -or $inputs.Count -ne 610){throw 'Reviewed correction must preserve all 610 input paths.'}
$remaining=@{}
foreach($file in $previous.files){if($remaining.ContainsKey($file.path)){throw 'Duplicate previous input'};$remaining[$file.path]=$file}
$changedInputs=@(foreach($file in $inputs){
if(-not $remaining.ContainsKey($file.path)){throw ('New compiled input: '+$file.path)}
$old=$remaining[$file.path]
if($file.bytes -ne $old.bytes -or $file.sha256 -cne $old.sha256){
if($file.path -cnotin $correction.expected_changed_paths){throw ('Unreviewed compiled input changed: '+$file.path)}
$file.path
}
$remaining.Remove($file.path)
})
if($remaining.Count -or $changedInputs.Count -ne 3){throw 'Expected precisely three changed test inputs and 607 byte-identical inputs.'}
Save-Record 'correction-provenance.json' ([ordered]@{source_commit=$SourceCommit;reviewed_correction_source=$correction.reviewed_correction_source;previous_source=$correction.previous_source;tree_equal_to_reviewed=$true;input_count=610;changed_inputs=$changedInputs;unchanged_input_count=607;instruction_git_blob=$instructionBlob;status='Parent-reviewed integrated tree verified; source-only capture, no Cargo executed'})
Save-Record 'expected-inputs.json' ([ordered]@{base=$SourceCommit;workspace=$workspace;count=$inputs.Count;files=$inputs})
$external=Get-Content -Raw -LiteralPath (Join-Path $PSScriptRoot 'external-input-template.json')|ConvertFrom-Json
$externalFiles=@(foreach($file in $external.files){$present=Test-Path -LiteralPath $file.path;if($present){$item=Get-Item -LiteralPath $file.path;[ordered]@{path=$file.path;present=$true;bytes=$item.Length;sha256=(Hash-Input $file.path)}}else{[ordered]@{path=$file.path;present=$false}}})
Save-Record 'external-inputs.json' ([ordered]@{base=$SourceCommit;status='Fresh external environment/config snapshot; not version-controlled inputs';files=$externalFiles})
$server=Get-Content -Raw -LiteralPath (Join-Path $PSScriptRoot 'server-provenance-template.json')|ConvertFrom-Json
$server.source=$SourceCommit
Save-Record 'server-provenance.json' $server
Save-Record 'cache-policy.json' ([ordered]@{base=$SourceCommit;retainExistingTarget=$true;cleanRequested=$false;target=(Join-Path $root 'target');scope='Combined spacing worktree rebuild. Retain shared target/cache and jobs1/incremental0; no clearance is performed by this recipe. Parent owns any separately authorized package clearance. Exact source and executable binding are required.'})
Save-Record 'prior-failure-provenance.json' ([ordered]@{
source=$SourceCommit
previousSource=$correction.previous_source
scope='Closed c4 listing passed (1082); focused failed (286 passed, 2 failed). Full-inventory direct-executable diagnostic is separate and never waives that failure. All corrected-source acceptance stages must pass anew.'
bindings=$correction.prior_gate_bindings
list=[ordered]@{cargoExitCode=$list.cargoExitCode;selectionValid=$list.selectionValid;inputsUnchanged=$list.inputsUnchanged;count=@($list.listedTests).Count}
focused=[ordered]@{cargoExitCode=$focused.cargoExitCode;selectionValid=$focused.selectionValid;inputsUnchanged=$focused.inputsUnchanged;result=$focused.testResultSummaries;failed=@($focused.executedTests|Where-Object result -eq 'FAILED')}
diagnostic=[ordered]@{bindings=$diagnosticBindings;classification=$diagnostic.classification;successful_full_gate=$false;waives_failed_focused_gate=$false;complete_inventory_valid=$diagnostic.complete_inventory_valid;inputs_unchanged=$diagnostic.inputs_unchanged;exit_code=$diagnostic.test_exit_code;result=$diagnostic.test_result_summaries;failed=$diagnostic.failed_tests}
})
& (Join-Path $PSScriptRoot 'capture.ps1') -Stage before
& (Join-Path $PSScriptRoot 'validate-scripts.ps1')
& (Join-Path $PSScriptRoot 'verify-parser.ps1')
Save-Record 'finalization.json' ([ordered]@{base=$SourceCommit;capturedUtc=[DateTime]::UtcNow.ToString('o');sourceCount=$inputs.Count;focusedCount=289;desktopExpectedCount=1083;cleanPerformed=$false;cargoExecuted=$false;sourceBeforeSha256=(Hash-Input (Join-Path $PSScriptRoot 'inputs-complete-before.json'));selectionSha256=(Hash-Input (Join-Path $PSScriptRoot 'expected-tests.json'));parserSha256=(Hash-Input (Join-Path $PSScriptRoot 'parser-verification.json'));status='Prepared only; parent dispatches list, focused, all, clippy, build sequentially'})
Write-Output ('Finalized source-only gate for '+$SourceCommit+' with '+$inputs.Count+' inputs; no Cargo or clean executed.')