Recover an unused maintenance helper with no applied decision

This commit is contained in:
2026-11-17 17:56:29 +01:00
parent c94c3939f1
commit 22e514a048
3 changed files with 78 additions and 6 deletions
@@ -89,7 +89,38 @@ try {
[ordered]@{already_retired=$true;verified=$true;applied=$true;case_id=$case;helper_id=$helper;decision_id=$decision;attempts=$rows.Count}|ConvertTo-Json -Compress;return
}
Check ($db.Scalar("SELECT count(*) FROM reconcile_meta WHERE id=1 AND journal_id='$journal' AND helper_id='$helper' AND helper_checkpoint=$checkpoint AND last_decision='$decision' AND pending_request IS NULL AND (fault IS NULL OR fault='HELPER_JOURNAL_DISAPPEARED')") -eq '1') 'Agent and signed central final checkpoint differ'
Check ($db.Scalar("SELECT count(*) FROM proof.helper_sync_meta WHERE journal_id='$helper' AND agent_journal_id='$journal' AND checkpoint>=$checkpoint AND last_decision='$decision' AND fault IS NULL") -eq '1') 'Archived helper has not applied the same signed final decision'
$matched=$db.Scalar("SELECT count(*) FROM proof.helper_sync_meta WHERE journal_id='$helper' AND agent_journal_id='$journal' AND checkpoint>=$checkpoint AND last_decision='$decision' AND fault IS NULL") -eq '1'
$emptyCaptures=0
if (!$matched) {
$first=$Witness.prepared_first_capture
Check ($first -and $checkpoint -eq 1 -and $rows.Count -eq 0) 'Archived helper has not applied the same signed final decision'
$boot=Uuid $first.boot_id;$request=Uuid $first.request_id
$binding=[string]$first.binding_sha256
Check ($binding -cmatch '^[0-9a-f]{64}$' -and $first.request_hash -cmatch '^[0-9a-f]{64}$') 'Missing signed first-capture bindings'
$requestBytes=$db.Bytes("SELECT request FROM reconcile_requests WHERE id='$request' AND response IS NOT NULL")
Check ((HashBytes $requestBytes) -ceq $first.request_hash) 'Signed decision differs from the original agent request'
Check ($db.Scalar("SELECT count(*) FROM proof.helper_sync_meta WHERE id=1 AND journal_id='$helper' AND binding_sha256='$binding' AND agent_journal_id IS NULL AND agent_checkpoint=0 AND last_decision IS NULL AND fault IS NULL") -eq '1') 'Expected an uninitialized, device-bound helper'
foreach($table in @('helper_sync_decisions','helper_sync_rows','consumed','execution_evidence')) {
Check ($db.Scalar("SELECT count(*) FROM proof.$table") -eq '0') 'The never-activated helper has existing decisions, grants or effects'
}
# Check every retained capture. An empty Rust ledger hashes its exact serialized [].
$emptyHash=HashBytes ([Text.Encoding]::UTF8.GetBytes('[]'))
$emptyCaptures=[long]$db.Scalar('SELECT count(*) FROM proof.helper_sync_captures')
Check ($emptyCaptures -gt 0 -and $db.Scalar('SELECT min(checkpoint) FROM proof.helper_sync_captures') -eq '1' -and
$db.Scalar('SELECT max(checkpoint) FROM proof.helper_sync_captures') -eq [string]$emptyCaptures -and
$db.Scalar('SELECT checkpoint FROM proof.helper_sync_meta WHERE id=1') -eq [string]$emptyCaptures) 'Incomplete first-capture chain'
$validCaptures=$db.Scalar(@"
SELECT count(*) FROM proof.helper_sync_captures WHERE fingerprint='$emptyHash'
AND (SELECT count(*) FROM json_each(CAST(snapshot AS TEXT)))=6
AND json_type(CAST(snapshot AS TEXT),'$.captured_at')='text'
AND json_extract(CAST(snapshot AS TEXT),'$.captured_at')=replace(captured_at,'+00:00','Z')
AND json_extract(CAST(snapshot AS TEXT),'$.journal_id')='$helper' AND json_extract(CAST(snapshot AS TEXT),'$.boot_id')='$boot'
AND json_type(CAST(snapshot AS TEXT),'$.checkpoint')='integer' AND json_extract(CAST(snapshot AS TEXT),'$.checkpoint')=checkpoint
AND json_type(CAST(snapshot AS TEXT),'$.previous_decision_id')='null'
AND json_type(CAST(snapshot AS TEXT),'$.attempts')='array' AND json_array_length(CAST(snapshot AS TEXT),'$.attempts')=0
"@)
Check ($validCaptures -eq [string]$emptyCaptures) 'Unexpected boot, prior decision or attempts in archived helper captures'
}
Check ($db.Scalar("SELECT count(*) FROM job_attempts WHERE state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')") -eq '0') 'Local unfinished attempt'
Check ($db.Scalar('SELECT count(*) FROM proof.consumed') -eq [string]$rows.Count -and $db.Scalar('SELECT count(*) FROM proof.execution_evidence') -eq [string]$rows.Count) 'Central and archived helper attempt counts differ'
if($apply){
@@ -128,7 +159,7 @@ CREATE TRIGGER IF NOT EXISTS local.deny_retired_windows_update_grant BEFORE INSE
$db.Exec("INSERT INTO retired_windows_update_windows VALUES('$case','$helper','$journal','$decision',$checkpoint); UPDATE reconcile_meta SET helper_id=NULL,helper_checkpoint=0,fault=NULL WHERE id=1; COMMIT")
} else {$db.Exec('ROLLBACK')}
$transaction=$false
[ordered]@{verified=$true;applied=$apply;case_id=$case;attempts=$rows.Count;helper_id=$helper;decision_id=$decision}|ConvertTo-Json -Compress
[ordered]@{verified=$true;applied=$apply;never_applied_empty_captures=$emptyCaptures;case_id=$case;attempts=$rows.Count;helper_id=$helper;decision_id=$decision}|ConvertTo-Json -Compress
} finally {
if($db){if($transaction){$db.Exec('ROLLBACK')};$db.Dispose()}
if($gate -and (Get-Service $agent).Status -ne 'Running'){Start-Service $agent}