diff --git a/crates/agent-core/src/helper_reconcile.rs b/crates/agent-core/src/helper_reconcile.rs index 095428e..2b62374 100644 --- a/crates/agent-core/src/helper_reconcile.rs +++ b/crates/agent-core/src/helper_reconcile.rs @@ -296,7 +296,8 @@ impl HelperSync { ledger_fingerprint: lease.fingerprint.clone(), captured_at: capture.captured_at, }; - self.verify_no_start_anchor(db, guard, &anchor, clock, boot)?; + let audit_history = self.integrity.is_none(); + self.verify_no_start_anchor(db, guard, &anchor, clock, boot, audit_history)?; Ok(anchor) } /// Consume exactly the approved update attempt and delegate its ORIGINAL @@ -397,9 +398,9 @@ impl HelperSync { tx.commit()?; return Ok(outcome); } - self.verify_no_start_anchor(&tx, guard, anchor, clock, boot)?; + self.verify_no_start_anchor(&tx, guard, anchor, clock, boot, true)?; let observation = guard.verify_no_dispatch(request, anchor)?; - let now = self.verify_no_start_anchor(&tx, guard, anchor, clock, boot)?; + let now = self.verify_no_start_anchor(&tx, guard, anchor, clock, boot, true)?; ensure!( !observation.intent_id.is_nil() && digest(&observation.intent_sha256) @@ -429,6 +430,7 @@ impl HelperSync { anchor: &NoStartAnchor, clock: &impl Clock, boot: &impl BootSource, + audit_history: bool, ) -> Result> { guard.check(db)?; let now = clock.now()?; @@ -453,15 +455,18 @@ impl HelperSync { && ledger_fingerprint(db)? == anchor.ledger_fingerprint, "DENIAL_HELPER_LINEAGE_CHANGED" ); - // Exact equality is deliberate: a first implementation refuses even - // legitimate intervening progress rather than inventing lineage links. - verify_checkpoint_repair_history_with( - db, - &self.binding, - None, - self.identity, - &self.verifier, - )?; + // Idle anchor capture checks the current signed head. The native + // executor audits the full history before execution, and abandonment + // always audits it under the same transaction before recording evidence. + if audit_history { + verify_checkpoint_repair_history_with( + db, + &self.binding, + None, + self.identity, + &self.verifier, + )?; + } let (response, raw): (Vec, Vec) = db.query_row( "SELECT response,request FROM helper_sync_decisions WHERE decision_id=?1", [anchor.decision_id.to_string()], @@ -491,8 +496,12 @@ impl HelperSync { && decision.helper_journal_id == Some(anchor.journal_id) && decision.helper_checkpoint == anchor.checkpoint && decision.connection_generation == anchor.connection_generation + && decision.request_hash == anchor.sync_request_sha256 + && capture.journal_id == anchor.journal_id + && capture.checkpoint == anchor.checkpoint && capture.captured_at == anchor.captured_at && capture.boot_id == anchor.boot_id + && capture.captured_at <= decision.issued_at && capture.captured_at <= now, "DENIAL_CONFIRMED_READY_REQUIRED" ); @@ -1053,7 +1062,8 @@ fn initialize_schema(db: &Connection, binding: &Binding) -> Result<()> { CREATE TABLE IF NOT EXISTS helper_sync_meta(id INTEGER PRIMARY KEY CHECK(id=1),journal_id TEXT NOT NULL,checkpoint INTEGER NOT NULL DEFAULT 0,generation INTEGER NOT NULL DEFAULT 0,binding_sha256 TEXT NOT NULL,last_decision TEXT,fault TEXT,agent_journal_id TEXT,agent_checkpoint INTEGER NOT NULL DEFAULT 0); CREATE TABLE IF NOT EXISTS helper_sync_rows(grant_id TEXT PRIMARY KEY,revision INTEGER NOT NULL,fingerprint TEXT NOT NULL); CREATE TABLE IF NOT EXISTS helper_sync_captures(checkpoint INTEGER PRIMARY KEY,snapshot BLOB NOT NULL,fingerprint TEXT NOT NULL,captured_at TEXT NOT NULL); - CREATE TABLE IF NOT EXISTS helper_sync_decisions(decision_id TEXT PRIMARY KEY,checkpoint INTEGER NOT NULL,response BLOB NOT NULL,request BLOB NOT NULL,fingerprint TEXT NOT NULL);")?; + CREATE TABLE IF NOT EXISTS helper_sync_decisions(decision_id TEXT PRIMARY KEY,checkpoint INTEGER NOT NULL,response BLOB NOT NULL,request BLOB NOT NULL,fingerprint TEXT NOT NULL); + CREATE INDEX IF NOT EXISTS helper_sync_decisions_checkpoint ON helper_sync_decisions(checkpoint);")?; let legacy_captures: bool = db.query_row( "SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='table' AND name='helper_sync_captures')", [], |row| row.get(0), @@ -1191,23 +1201,23 @@ pub fn verify_checkpoint_repair_history_with( ) -> Result<()> { let meta = metadata(db)?; validate_history(db, binding, &meta, meta.generation, verifier)?; + let entry_count: u64 = db.query_row( + "SELECT count(*) FROM helper_sync_decisions", + [], + |row| row.get(0), + )?; + ensure!(entry_count <= 100000, "REPAIR_HISTORY_LIMIT"); let mut statement = db.prepare( "SELECT response,request,checkpoint FROM helper_sync_decisions ORDER BY checkpoint", )?; - let entries = statement - .query_map([], |r| { - Ok(( - r.get::<_, Vec>(0)?, - r.get::<_, Vec>(1)?, - r.get::<_, u64>(2)?, - )) - })? - .collect::>>()?; - ensure!(entries.len() <= 100000, "REPAIR_HISTORY_LIMIT"); + let mut entries = statement.query([])?; let mut previous = None; let mut previous_checkpoint = 0; let mut previous_agent_checkpoint = 0; - for (signed, raw, checkpoint) in entries { + while let Some(entry) = entries.next()? { + let signed: Vec = entry.get(0)?; + let raw: Vec = entry.get(1)?; + let checkpoint: u64 = entry.get(2)?; let response: SignedResponse = serde_json::from_slice(&signed)?; let parsed: ReconcileDecisionPayload = serde_json::from_slice(&response.decision_bytes)?; let decision = verify(verifier, binding, &response, parsed.issued_at)?; diff --git a/crates/agent-windows/src/helper_system.rs b/crates/agent-windows/src/helper_system.rs index 3d5bb36..98eed56 100644 --- a/crates/agent-windows/src/helper_system.rs +++ b/crates/agent-windows/src/helper_system.rs @@ -1293,6 +1293,13 @@ impl SystemHelper { verified_window(&self.settings.binding, request, &self.clock)?; if self.settings.managed_scripts() { ledger::check_integrity(&self.db, "ROOT_LEDGER_INTEGRITY")?; + rmm_agent_core::helper_reconcile::verify_checkpoint_repair_history_with( + &self.db, + &self.settings.binding, + None, + HelperIdentity::System, + &DalekVerifier, + )?; } self.sync()?; ensure!(