Qualify atomic deny-only helper abandonment on actual Linux VM
Rust development slice / linux (push) Failing after 7s

This commit is contained in:
2026-11-14 06:48:32 +01:00
parent 44c45c15b9
commit 67370d21a3
7 changed files with 1174 additions and 14 deletions
+250 -1
View File
@@ -2,7 +2,7 @@
//! an operation or grants authority from an agent-provided ready/unlock flag.
//! Both general and APT listeners must use the SAME executor.lock and SQLite DB.
use crate::auth::HelperIdentity;
use crate::auth::{Binding, Request};
use crate::auth::{Binding, Outcome, Request};
use crate::crypto::{DefaultVerifier, Ed25519Verifier};
use anyhow::{Context, Result, ensure};
use chrono::{DateTime, Utc};
@@ -27,6 +27,72 @@ pub trait BootSource {
pub trait ExecutorLedgerGuard {
fn check(&self, db: &Connection) -> Result<()>;
}
/// OS adapter contract, not an IPC assertion. The implementation must pin the
/// live helper PID/creation and ledger/lock file identities, and independently
/// read the fixed protected intent store's immutable known_no_dispatch CAS.
/// The same executor guard stays held throughout this check and the commit.
pub trait NoStartGuard: ExecutorLedgerGuard {
fn live_instance_sha256(&self) -> Result<String>;
fn verify_no_dispatch(
&self,
original: &Request,
anchor: &NoStartAnchor,
) -> Result<NoDispatchObservation>;
}
/// Trusted adapter return value only: deliberately has no IPC deserializer.
#[derive(Debug, PartialEq, Eq)]
pub struct NoDispatchObservation {
pub intent_id: Uuid,
pub intent_sha256: String,
pub intent_recorded_at: DateTime<Utc>,
pub original_boot_id: Uuid,
}
/// Historical ready context, never a new lease or execution authorization.
/// Deserialization alone conveys no authority: every field is checked against
/// the still-held helper instance and the original independently stored history.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NoStartAnchor {
journal_id: Uuid,
checkpoint: u64,
decision_id: Uuid,
connection_generation: u64,
boot_id: Uuid,
binding_sha256: String,
execution_identity: String,
helper_instance_sha256: String,
helper_born_at: DateTime<Utc>,
sync_request_sha256: String,
signed_response_sha256: String,
ledger_fingerprint: String,
captured_at: DateTime<Utc>,
}
impl NoStartAnchor {
pub fn journal_id(&self) -> Uuid {
self.journal_id
}
pub fn checkpoint(&self) -> u64 {
self.checkpoint
}
pub fn decision_id(&self) -> Uuid {
self.decision_id
}
pub fn connection_generation(&self) -> u64 {
self.connection_generation
}
pub fn boot_id(&self) -> Uuid {
self.boot_id
}
pub fn helper_instance_sha256(&self) -> &str {
&self.helper_instance_sha256
}
pub fn sync_request_sha256(&self) -> &str {
&self.sync_request_sha256
}
pub fn signed_response_sha256(&self) -> &str {
&self.signed_response_sha256
}
}
/// Same two fields as public SyncResponse, without adding a new dependency or
/// wire type. RPC glue copies the ORIGINAL bytes/signature, never reserializes.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
@@ -122,6 +188,183 @@ impl<V: Ed25519Verifier> HelperSync<V> {
pub fn active_generation(&self) -> Option<u64> {
self.lease.as_ref().map(|lease| lease.generation)
}
/// Capture only an actually confirmed live ready lease, before an intent
/// or fence closes it. Retaining this anchor cannot keep that lease alive.
pub fn no_start_anchor(
&self,
db: &Connection,
guard: &impl NoStartGuard,
generation: u64,
clock: &impl Clock,
boot: &impl BootSource,
) -> Result<NoStartAnchor> {
ensure!(
self.can_start(db, guard, generation, clock, boot),
"DENIAL_READY_ANCHOR_REQUIRED"
);
let lease = self
.lease
.as_ref()
.context("DENIAL_READY_ANCHOR_REQUIRED")?;
let meta = metadata(db)?;
let (response, request): (Vec<u8>, Vec<u8>) = db.query_row(
"SELECT response,request FROM helper_sync_decisions WHERE decision_id=?1",
[lease.decision_id.to_string()],
|r| Ok((r.get(0)?, r.get(1)?)),
)?;
let instance = guard.live_instance_sha256()?;
ensure!(digest(&instance), "DENIAL_HELPER_INSTANCE_INVALID");
let capture: Vec<u8> = db.query_row(
"SELECT snapshot FROM helper_sync_captures WHERE checkpoint=?1",
[meta.checkpoint],
|r| r.get(0),
)?;
let capture: Snapshot = serde_json::from_slice(&capture)?;
let anchor = NoStartAnchor {
journal_id: meta.journal_id,
checkpoint: meta.checkpoint,
decision_id: lease.decision_id,
connection_generation: generation,
boot_id: lease.boot_id,
binding_sha256: hash(&serde_json::to_vec(&self.binding)?),
execution_identity: self.identity.as_str().into(),
helper_instance_sha256: instance,
helper_born_at: self.born,
sync_request_sha256: hash(&request),
signed_response_sha256: hash(&response),
ledger_fingerprint: lease.fingerprint.clone(),
captured_at: capture.captured_at,
};
self.verify_no_start_anchor(db, guard, &anchor, clock, boot)?;
Ok(anchor)
}
/// Deny-only: the grant may be expired, but no historical timestamp is ever
/// passed to an execute verifier. Existing consumption is immutable. A new
/// c/start/no-effect outcome is committed atomically and remains locked for
/// the ordinary independently signed Sync decision.
pub fn abandon_no_start(
&mut self,
db: &mut Connection,
guard: &impl NoStartGuard,
anchor: &NoStartAnchor,
request: &Request,
clock: &impl Clock,
boot: &impl BootSource,
) -> Result<Outcome> {
self.close();
guard.check(db)?;
let original = self.binding.verify_historical_for_denial_with(
request,
self.identity,
&self.verifier,
)?;
let tx = db.transaction_with_behavior(TransactionBehavior::Immediate)?;
if let Some(outcome) = crate::ledger::existing_no_start(&tx, &original)? {
// No changed row and no classification of a missing original start.
tx.commit()?;
return Ok(outcome);
}
self.verify_no_start_anchor(&tx, guard, anchor, clock, boot)?;
let observation = guard.verify_no_dispatch(request, anchor)?;
let now = self.verify_no_start_anchor(&tx, guard, anchor, clock, boot)?;
ensure!(
!observation.intent_id.is_nil()
&& digest(&observation.intent_sha256)
&& observation.original_boot_id == anchor.boot_id
&& observation.intent_recorded_at >= anchor.captured_at
&& observation.intent_recorded_at <= now,
"DENIAL_INTENT_BINDING"
);
let outcome =
crate::ledger::record_no_start_in(&tx, &original, anchor.boot_id, now, &observation)?;
guard.check(&tx)?;
ensure!(
guard.live_instance_sha256()? == anchor.helper_instance_sha256,
"DENIAL_HELPER_INSTANCE_CHANGED"
);
ensure!(
guard.verify_no_dispatch(request, anchor)? == observation,
"DENIAL_INTENT_CHANGED_DURING_COMMIT"
);
tx.commit()?;
Ok(outcome)
}
fn verify_no_start_anchor(
&self,
db: &Connection,
guard: &impl NoStartGuard,
anchor: &NoStartAnchor,
clock: &impl Clock,
boot: &impl BootSource,
) -> Result<DateTime<Utc>> {
guard.check(db)?;
let now = clock.now()?;
ensure!(
anchor.helper_born_at == self.born
&& now >= self.born
&& boot.current_boot()? == anchor.boot_id
&& !anchor.boot_id.is_nil()
&& anchor.execution_identity == self.identity.as_str()
&& anchor.binding_sha256 == hash(&serde_json::to_vec(&self.binding)?)
&& digest(&anchor.helper_instance_sha256)
&& guard.live_instance_sha256()? == anchor.helper_instance_sha256,
"DENIAL_HELPER_CONTEXT_CHANGED"
);
let meta = metadata(db)?;
ensure!(
meta.fault.is_none()
&& meta.journal_id == anchor.journal_id
&& meta.checkpoint == anchor.checkpoint
&& meta.decision == Some(anchor.decision_id)
&& meta.generation == anchor.connection_generation
&& 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,
)?;
let (response, raw): (Vec<u8>, Vec<u8>) = db.query_row(
"SELECT response,request FROM helper_sync_decisions WHERE decision_id=?1",
[anchor.decision_id.to_string()],
|r| Ok((r.get(0)?, r.get(1)?)),
)?;
ensure!(
hash(&response) == anchor.signed_response_sha256
&& hash(&raw) == anchor.sync_request_sha256,
"DENIAL_READY_BYTES_CHANGED"
);
let signed: SignedResponse = serde_json::from_slice(&response)?;
let parsed: ReconcileDecisionPayload = serde_json::from_slice(&signed.decision_bytes)?;
// Historical Sync evidence only, never a lease. Its original signature
// and immutable stored request remain mandatory after expiry.
let decision = verify(&self.verifier, &self.binding, &signed, parsed.issued_at)?;
let capture: Vec<u8> = db.query_row(
"SELECT snapshot FROM helper_sync_captures WHERE checkpoint=?1",
[anchor.checkpoint],
|r| r.get(0),
)?;
let capture: Snapshot = serde_json::from_slice(&capture)?;
ensure!(
decision.ready
&& decision.helper_ready
&& decision.decision_id == anchor.decision_id
&& decision.boot_id == anchor.boot_id
&& decision.helper_journal_id == Some(anchor.journal_id)
&& decision.helper_checkpoint == anchor.checkpoint
&& decision.connection_generation == anchor.connection_generation
&& capture.captured_at == anchor.captured_at
&& capture.boot_id == anchor.boot_id
&& capture.captured_at <= now,
"DENIAL_CONFIRMED_READY_REQUIRED"
);
Ok(now)
}
/// Every call creates a NEW durable checkpoint challenge and closes the old
/// lease. Use apply's returned snapshot for its ACK, not another snapshot().
pub fn snapshot(
@@ -494,6 +737,12 @@ impl<V: Ed25519Verifier> HelperSync<V> {
}
}
fn digest(value: &str) -> bool {
value.len() == 64
&& value
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}
fn pinned_key(binding: &Binding, verifier: &impl Ed25519Verifier) -> Result<Vec<u8>> {
let raw = hex::decode(&binding.public_key_hex)?;
ensure!(raw.len() == 32, "PINNED_HELPER_KEY_SIZE");