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}
@@ -54,5 +54,36 @@
"system_caps": false,
"open_jobs": 0
},
"result": "passed"
"result": "passed",
"post_reboot_first_capture_recovery": {
"case_id": "104690e6-ac27-4ac5-bef1-15d2b3ba8424",
"centrally_activated": false,
"original_signed_head": {
"journal_id": "0c1ec1d3-c868-42f7-a783-70d99a4f9636",
"checkpoint": 638,
"helper_id": "630db545-3a26-427d-81aa-6e9b10ed732f",
"helper_checkpoint": 1,
"decision_id": "f61187e1-6b18-49fb-86e0-381c2bfaafca"
},
"request_matched_native_bytes": "49402c3a-809e-481b-b8de-aac713d5a047",
"boot_id": "9a106a1c-ecd9-8df8-9703-ebecdefe599e",
"signed_at": "2026-11-17T16:46:38.623012Z",
"first_helper_snapshot_at": "2026-11-17T16:46:39.100973Z",
"first_apply_error": "OLD_PROCESS_DECISION_OR_BOOT",
"archived_contiguous_empty_captures": 10,
"applied_helper_decisions": 0,
"window_system_jobs": 0,
"window_system_grants": 0,
"helper_consumptions": 0,
"helper_execution_evidence": 0,
"central_helper_detach_committed": true,
"native_helper_detach_committed": true,
"helper_service_registered": false,
"agent_service": "Running",
"release_pull_service": "Running",
"post_close_contact": "2026-11-17T16:53:36.146353+00:00",
"sync_ready": true,
"system_caps": false,
"open_jobs": 0
}
}
+13 -3
View File
@@ -100,7 +100,7 @@ def observation(ids, opened, cutoff=None):
"""
def verify(snapshot, ids):
def verify(snapshot, ids, *, prepared_only=False):
require(snapshot['open_jobs'] == 0, 'Device has open or uncertain jobs')
require(snapshot['helper_id'] and snapshot['helper_checkpoint'] > 0, 'Expected bound helper journal')
raw = bytes.fromhex(snapshot['signed'])
@@ -143,7 +143,17 @@ def verify(snapshot, ids):
attempts.append({**{k:j[k] for k in ('job','attempt','grant','state','payload_sha256')},
'outcome_sha256':a['outcome_sha256']})
require(not outcomes, 'The helper still owns SYSTEM attempts outside this window')
return {**ids, **{k:snapshot[k] for k in (*fields,'helper_id')}, 'attempts':attempts}
witness = {**ids, **{k:snapshot[k] for k in (*fields,'helper_id')}, 'attempts':attempts}
if prepared_only and not attempts and d['helper_checkpoint'] == 1:
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
# Binding is hashed in Rust's fixed declaration order.
binding = {k:ids[k] for k in ('tenant_id','asset_id','agent_id')}
binding.update(enrollment_generation=1,assignment_version=1,
public_key_hex=public.public_bytes(Encoding.Raw,PublicFormat.Raw).hex())
witness['prepared_first_capture'] = {
'binding_sha256':hashlib.sha256(json.dumps(binding,separators=(',',':')).encode()).hexdigest(),
'request_id':d['request_id'], 'request_hash':d['request_hash'], 'boot_id':d['boot_id']}
return witness
def prepared_baseline(case, data, ids):
@@ -268,7 +278,7 @@ def server(action, data):
require_native_verification(data, receipt)
scope.atomic(done,scope.encode(receipt))
return receipt
witness = verify(record,ids)
witness = verify(record,ids,prepared_only=prepared is not None and (receipt is None or 'prepared_first_capture' in receipt))
require(receipt in (None,witness) and data.get('witness',witness) == witness, 'Device or witness changed')
if action == 'witness':
return witness