Add human WebUI closure of unknown jobs as final closed_unknown state (spec 1.31); bound agent sync restart interval
Rust development slice / linux (push) Failing after 7s

This commit is contained in:
2026-11-17 11:13:36 +01:00
parent 2660f6a88d
commit d6dfd01a9c
11 changed files with 363 additions and 29 deletions
+23 -8
View File
@@ -44,7 +44,10 @@ fn outcome(state: &str, value: &Value) -> Result<ReconciledOutcome> {
})
}
fn unsettled(state: &str) -> bool {
!matches!(state, "succeeded" | "failed" | "cancelled" | "expired")
!matches!(
state,
"succeeded" | "failed" | "cancelled" | "expired" | "closed_unknown"
)
}
fn clean_unstarted(local: &SyncAttempt, reserved: bool) -> bool {
!reserved
@@ -420,6 +423,7 @@ pub async fn reconcile(
| "verifying"
| "awaiting_reboot"
| "unknown"
| "closed_unknown"
| "succeeded"
| "failed"
| "cancelled"
@@ -444,7 +448,7 @@ pub async fn reconcile(
ensure!(parsed.state == local.state, "local outcome state mismatch");
}
}
let rows=sqlx::query("SELECT j.job_id,j.attempt_id,j.payload_hash,j.payload,j.state,j.revision,j.result,j.start_reserved,b.acknowledged_at FROM jobs j LEFT JOIN outbox b ON b.tenant_id=j.tenant_id AND b.job_id=j.job_id WHERE j.agent_id=$1 AND (j.state NOT IN ('succeeded','failed','cancelled','expired') OR j.job_id=ANY($2)) ORDER BY j.job_id LIMIT 257 FOR UPDATE OF j").bind(agent).bind(locals.keys().copied().collect::<Vec<_>>()).fetch_all(&mut **tx).await?;
let rows=sqlx::query("SELECT j.job_id,j.attempt_id,j.payload_hash,j.payload,j.state,j.revision,j.result,j.start_reserved,b.acknowledged_at FROM jobs j LEFT JOIN outbox b ON b.tenant_id=j.tenant_id AND b.job_id=j.job_id WHERE j.agent_id=$1 AND (j.state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown') OR j.job_id=ANY($2)) ORDER BY j.job_id LIMIT 257 FOR UPDATE OF j").bind(agent).bind(locals.keys().copied().collect::<Vec<_>>()).fetch_all(&mut **tx).await?;
if rows.len() > 256 {
reason = "SYNC_SERVER_JOB_LIMIT".into();
chain_valid = false;
@@ -743,7 +747,9 @@ pub async fn reconcile(
&& result.grant_id == local.grant_id,
"sync result binding"
);
if !manual {
// A human-closed attempt is final; late agent evidence is neither
// replayed into nor allowed to reinterpret the closed ledger row.
if !manual && server_state != "closed_unknown" {
let evidence = local
.result_envelope
.as_ref()
@@ -857,13 +863,22 @@ pub async fn reconcile(
if let Some(value) = final_row.get::<Option<Value>, _>("result") {
let bytes = canonical_outcome_bytes(&outcome(&terminal, &value)?)?;
let hash = hash(&bytes);
if prior_hash.as_deref() == Some(&hash) || reboot_reconciled {
decision.outcome_sha256 = Some(hash);
// Human WebUI closure: only an attempt still locally unknown (or
// already closed) receives the server-derived final bytes.
let closed = terminal == "closed_unknown"
&& matches!(local.state.as_str(), "unknown" | "closed_unknown");
if prior_hash.as_deref() == Some(&hash) || reboot_reconciled || closed {
decision.unlock = local.consumption == "consumed" && grant_matches;
if reboot_reconciled {
if reboot_reconciled || (closed && prior_hash.as_deref() != Some(&hash)) {
decision.outcome_bytes = bytes;
}
decision.reason = "DEFINITIVE_OUTCOME_RECONCILED".into();
decision.outcome_sha256 = Some(hash);
decision.reason = if closed {
"OPERATOR_CLOSED_UNKNOWN"
} else {
"DEFINITIVE_OUTCOME_RECONCILED"
}
.into();
} else {
ready = false;
decision.reason = "LOCAL_OUTCOME_MISMATCH".into();
@@ -968,7 +983,7 @@ pub async fn reconcile(
})
}
async fn mark_unknown(tx: &mut Transaction<'_, Postgres>, job: Uuid, reason: &str) -> Result<()> {
sqlx::query("UPDATE jobs SET state='unknown',start_reserved=true,pending_reason='reconciliation_required',result=COALESCE(result,'{}'::jsonb)||jsonb_build_object('error_code',$2::text,'source_id','authoritative_sync') WHERE job_id=$1 AND state NOT IN ('succeeded','failed','cancelled','expired')").bind(job).bind(reason).execute(&mut **tx).await?;
sqlx::query("UPDATE jobs SET state='unknown',start_reserved=true,pending_reason='reconciliation_required',result=COALESCE(result,'{}'::jsonb)||jsonb_build_object('error_code',$2::text,'source_id','authoritative_sync') WHERE job_id=$1 AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')").bind(job).bind(reason).execute(&mut **tx).await?;
Ok(())
}
#[cfg(test)]