fix: retain sync anchors and verify bound APT partial postconditions
Rust development slice / linux (push) Failing after 6s

This commit is contained in:
2026-11-13 17:20:50 +01:00
parent 435c4453fe
commit 998f16fde4
13 changed files with 1469 additions and 16 deletions
+54 -8
View File
@@ -52,6 +52,21 @@ fn clean_unstarted(local: &SyncAttempt, reserved: bool) -> bool {
&& local.grant_id.is_empty()
&& matches!(local.state.as_str(), "queued" | "accepted")
}
/// A missing reply and a request that never committed are different cases.
/// A committed pending request is the newer anchor. Otherwise only the last
/// applied decision can anchor the chain; the caller still compares it with
/// the authoritative head and performs all journal/generation checks.
fn previous_anchor(
last_applied: Option<Uuid>,
pending_request: Option<Uuid>,
committed_pending: Option<Uuid>,
) -> Option<Uuid> {
if pending_request.is_some() {
committed_pending.or(last_applied)
} else {
last_applied
}
}
pub async fn reconcile(
state: &crate::State,
tx: &mut Transaction<'_, Postgres>,
@@ -117,13 +132,17 @@ pub async fn reconcile(
reason = "JOURNAL_SEQUENCE_REGRESSION".into();
chain_valid = false;
}
let mut previous_decision = optional_id(&r.previous_decision_id)?;
if previous_decision.is_none() && !r.previous_request_id.is_empty() {
previous_decision=sqlx::query_scalar("SELECT decision_id FROM sync_checkpoints WHERE agent_id=$1 AND request_id=$2 AND journal_id=$3").bind(agent).bind(Uuid::parse_str(&r.previous_request_id)?).bind(journal_id).fetch_optional(&mut **tx).await?;
if previous_decision.is_none() {
reason = "SERVER_CHECKPOINT_MISSING".into();
chain_valid = false;
}
let last_applied = optional_id(&r.previous_decision_id)?;
let pending_request = optional_id(&r.previous_request_id)?;
let committed_pending = if let Some(pending) = pending_request {
sqlx::query_scalar("SELECT decision_id FROM sync_checkpoints WHERE agent_id=$1 AND request_id=$2 AND journal_id=$3").bind(agent).bind(pending).bind(journal_id).fetch_optional(&mut **tx).await?
} else {
None
};
let previous_decision = previous_anchor(last_applied, pending_request, committed_pending);
if pending_request.is_some() && previous_decision.is_none() {
reason = "SERVER_CHECKPOINT_MISSING".into();
chain_valid = false;
}
if let Some(old) = &previous {
if old.get::<Uuid, _>("journal_id") != journal_id
@@ -132,7 +151,9 @@ pub async fn reconcile(
reason = "JOURNAL_ROLLBACK_OR_REPLACEMENT".into();
chain_valid = false;
}
if previous_decision != Some(old.get::<Uuid, _>("decision_id")) {
if previous_decision != Some(old.get::<Uuid, _>("decision_id"))
&& reason != "SERVER_CHECKPOINT_MISSING"
{
reason = "CHECKPOINT_CHAIN_MISMATCH".into();
chain_valid = false;
}
@@ -582,6 +603,31 @@ async fn mark_unknown(tx: &mut Transaction<'_, Postgres>, job: Uuid, reason: &st
mod tests {
use super::*;
#[test]
fn uncertain_request_resolves_only_to_a_known_checkpoint_anchor() {
let applied = Uuid::new_v4();
let pending = Uuid::new_v4();
let committed = Uuid::new_v4();
// Transport cut before server commit: retain the applied anchor.
assert_eq!(
previous_anchor(Some(applied), Some(pending), None),
Some(applied)
);
// Server committed but its reply was lost: use the newer checkpoint,
// even while the agent also retains its older applied witness.
assert_eq!(
previous_anchor(Some(applied), Some(pending), Some(committed)),
Some(committed)
);
// An absent pending request cannot invent an anchor for a legacy
// request that discarded its last applied decision.
assert_eq!(previous_anchor(None, Some(pending), None), None);
assert_eq!(
previous_anchor(None, Some(pending), Some(committed)),
Some(committed)
);
assert_eq!(previous_anchor(Some(applied), None, None), Some(applied));
}
#[test]
fn accepted_is_not_a_new_attempt_or_grant() {
let mut local = SyncAttempt {
state: "accepted".into(),