Acknowledge bound unknown follow-up results without releasing attempts
Rust development slice / linux (push) Failing after 6s

This commit is contained in:
2026-11-14 02:47:49 +01:00
parent 500cb7a33e
commit c80f4d4a6e
2 changed files with 228 additions and 3 deletions
+211 -3
View File
@@ -133,9 +133,12 @@ pub async fn event(
ensure!(
matches!(
outcome,
JobState::Succeeded | JobState::Failed | JobState::Cancelled
JobState::Succeeded
| JobState::Failed
| JobState::Cancelled
| JobState::Unknown
),
"unknown needs definitive reconciliation evidence"
"unknown needs bound unknown or definitive reconciliation evidence"
);
}
if r.grant_id.is_empty() {
@@ -202,7 +205,10 @@ pub async fn event(
.bind(result)
.execute(&mut **tx)
.await?;
if previous == JobState::Unknown {
// A newly persisted unknown result may follow a Sync decision
// that already marked the attempt unknown. Receipt/ACK records
// that evidence; it does not reconcile or release the attempt.
if previous == JobState::Unknown && outcome != JobState::Unknown {
sqlx::query("INSERT INTO audit_events VALUES($1,$2,'job-service','job_reconciled',$3,now())").bind(tenant).bind(Uuid::new_v4()).bind(json!({"job_id":r.job_id,"attempt_id":r.attempt_id,"grant_id":r.grant_id,"outcome":r.state,"evidence_source":"authenticated_durable_job_result"})).execute(&mut **tx).await?;
}
}
@@ -520,3 +526,205 @@ pub async fn next(
tx.commit().await?;
Ok(Some(Payload::JobOffer(offer)))
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
#[ignore = "requires dedicated isolated PostgreSQL with all migrations"]
async fn unknown_followup_result_is_durable_without_reconciliation_or_new_grant() {
let admin = sqlx::PgPool::connect(&std::env::var("RMM_TEST_REPAIR_ADMIN_URL").unwrap())
.await
.unwrap();
let app = sqlx::PgPool::connect(&std::env::var("RMM_TEST_REPAIR_APP_URL").unwrap())
.await
.unwrap();
rmm_persistence::verify_application_role(&app)
.await
.unwrap();
let tenant = Uuid::new_v4();
let agent = Uuid::new_v4();
let asset = Uuid::new_v4();
let job = Uuid::new_v4();
let attempt = Uuid::new_v4();
let grant = Uuid::new_v4();
let plan = Uuid::new_v4();
let operation = Uuid::new_v4();
let authorization = Uuid::new_v4();
let now = Utc::now();
let payload = JobPayload {
schema_version: 1,
tenant_id: tenant,
asset_id: asset,
agent_id: agent,
enrollment_generation: 1,
assignment_version: 1,
job_id: job,
attempt_id: attempt,
action: Action::RefreshInventory {
scope: "linux.base".into(),
},
execution_identity: "agent_service".into(),
not_before: now,
start_before: now + Duration::minutes(5),
max_runtime_seconds: 30,
max_output_bytes: 65536,
};
let bytes = serde_json::to_vec(&payload).unwrap();
sqlx::query("INSERT INTO tenants VALUES($1,'isolated unknown-result test')")
.bind(tenant)
.execute(&admin)
.await
.unwrap();
sqlx::query("INSERT INTO assets VALUES($1,$2)")
.bind(tenant)
.bind(asset)
.execute(&admin)
.await
.unwrap();
sqlx::query("INSERT INTO agent_installations(tenant_id,agent_id,asset_id,certificate_sha256) VALUES($1,$2,$3,$4)").bind(tenant).bind(agent).bind(asset).bind(hash(agent.as_bytes())).execute(&admin).await.unwrap();
sqlx::query("INSERT INTO operation_plans(tenant_id,plan_id,principal,payload,plan_hash,expires_at) VALUES($1,$2,'isolated-test',$3,$4,$5)").bind(tenant).bind(plan).bind(b"{}".as_slice()).bind("ab".repeat(32)).bind(now+Duration::minutes(10)).execute(&admin).await.unwrap();
sqlx::query("INSERT INTO approval_requests(tenant_id,operation_id,plan_id,principal,idempotency_key,request_hash,approval_state) VALUES($1,$2,$3,'isolated-test',$4,$5,'approved')").bind(tenant).bind(operation).bind(plan).bind(operation.to_string()).bind("cd".repeat(32)).execute(&admin).await.unwrap();
sqlx::query("INSERT INTO authorizations(tenant_id,authorization_id,operation_id,plan_id,plan_hash,principal,session_hash,approval_origin,testsimulation,policy_version) VALUES($1,$2,$3,$4,$5,'isolated-test',$6,'dev_test_browser_simulation',true,1)").bind(tenant).bind(authorization).bind(operation).bind(plan).bind("ab".repeat(32)).bind("ef".repeat(32)).execute(&admin).await.unwrap();
sqlx::query("INSERT INTO operations VALUES($1,$2,$3)")
.bind(tenant)
.bind(operation)
.bind(authorization)
.execute(&admin)
.await
.unwrap();
// This is an isolated ledger fixture, not an approval or executable job
// created through a deployed application. Sync already marked it unknown.
sqlx::query("INSERT INTO jobs(tenant_id,job_id,attempt_id,operation_id,agent_id,asset_id,payload,payload_hash,state,revision,start_reserved,resource) VALUES($1,$2,$3,$4,$5,$6,$7,$8,'unknown',2,true,'device_execution')").bind(tenant).bind(job).bind(attempt).bind(operation).bind(agent).bind(asset).bind(&bytes).bind(hash(&bytes)).execute(&admin).await.unwrap();
sqlx::query("INSERT INTO execution_permits VALUES($1,$2,$3,$4,$5,$6,$7)")
.bind(tenant)
.bind(grant)
.bind(job)
.bind(attempt)
.bind(b"original-bound-grant".as_slice())
.bind(vec![0u8; 64])
.bind(now)
.execute(&admin)
.await
.unwrap();
let mut result = JobResult {
job_id: job.to_string(),
attempt_id: attempt.to_string(),
grant_id: grant.to_string(),
revision: 3,
state: "unknown".into(),
error_code: "AGENT_PROCESS_INTERRUPTED".into(),
evidence_json: json!({"effect_complete":false,"postconditions_passed":false})
.to_string(),
..Default::default()
};
let mut envelope = Envelope {
message_id: Uuid::new_v4().to_string(),
agent_id: agent.to_string(),
connection_generation: 1,
payload: Some(Payload::JobResult(result.clone())),
..Default::default()
};
let mut tx = rmm_persistence::tenant_tx(&app, tenant).await.unwrap();
event(&mut tx, tenant, agent, &envelope).await.unwrap();
tx.commit().await.unwrap();
// The exact durable message is ACKable after reconnect without another
// receipt or any reinterpretation of its still-unknown effect.
envelope.connection_generation = 2;
let mut tx = rmm_persistence::tenant_tx(&app, tenant).await.unwrap();
event(&mut tx, tenant, agent, &envelope).await.unwrap();
tx.commit().await.unwrap();
result.revision = 4;
result.error_code = "EFFECT_STILL_UNRESOLVED".into();
envelope.message_id = Uuid::new_v4().to_string();
envelope.payload = Some(Payload::JobResult(result.clone()));
let mut tx = rmm_persistence::tenant_tx(&app, tenant).await.unwrap();
event(&mut tx, tenant, agent, &envelope).await.unwrap();
tx.commit().await.unwrap();
for case in 0..7 {
let mut invalid = result.clone();
invalid.revision = 5;
let mut target_tenant = tenant;
let mut target_agent = agent;
match case {
0 => invalid.grant_id.clear(),
1 => invalid.grant_id = Uuid::new_v4().to_string(),
2 => invalid.attempt_id = Uuid::new_v4().to_string(),
3 => {
invalid.revision = 3;
invalid.error_code = "CONFLICTING_STALE_EVIDENCE".into();
}
4 => invalid.state = "awaiting_reboot".into(),
5 => target_tenant = Uuid::new_v4(),
_ => target_agent = Uuid::new_v4(),
}
let mut invalid_envelope = envelope.clone();
invalid_envelope.message_id = Uuid::new_v4().to_string();
invalid_envelope.payload = Some(Payload::JobResult(invalid));
let mut tx = rmm_persistence::tenant_tx(&app, target_tenant)
.await
.unwrap();
assert!(
event(&mut tx, target_tenant, target_agent, &invalid_envelope)
.await
.is_err(),
"negative case {case}"
);
tx.rollback().await.unwrap();
}
let mut tx = rmm_persistence::tenant_tx(&app, tenant).await.unwrap();
let row = locked_job(&mut tx, agent, &job.to_string(), &attempt.to_string())
.await
.unwrap();
assert_eq!(row.get::<String, _>("state"), "unknown");
assert_eq!(row.get::<i64, _>("revision"), 4);
assert!(row.get::<bool, _>("start_reserved"));
assert_eq!(row.get::<Vec<u8>, _>("payload"), bytes);
let stored = row.get::<Value, _>("result");
assert_eq!(stored["grant_id"], grant.to_string());
assert_eq!(stored["error_code"], "EFFECT_STILL_UNRESOLVED");
assert_eq!(stored["evidence"]["postconditions_passed"], false);
assert_eq!(
sqlx::query_scalar::<_, i64>("SELECT count(*) FROM job_receipts")
.fetch_one(&mut *tx)
.await
.unwrap(),
2
);
assert_eq!(
sqlx::query_scalar::<_, i64>("SELECT count(*) FROM execution_permits")
.fetch_one(&mut *tx)
.await
.unwrap(),
1
);
assert_eq!(
sqlx::query_scalar::<_, Vec<u8>>(
"SELECT payload FROM execution_permits WHERE grant_id=$1"
)
.bind(grant)
.fetch_one(&mut *tx)
.await
.unwrap(),
b"original-bound-grant"
);
assert_eq!(
sqlx::query_scalar::<_, i64>("SELECT count(*) FROM outbox")
.fetch_one(&mut *tx)
.await
.unwrap(),
0
);
assert_eq!(
sqlx::query_scalar::<_, i64>(
"SELECT count(*) FROM audit_events WHERE event_type='job_reconciled'"
)
.fetch_one(&mut *tx)
.await
.unwrap(),
0
);
tx.commit().await.unwrap();
}
}
+17
View File
@@ -1,5 +1,22 @@
# Tatsächlicher Implementierungsstand
## Gebundenes Unknown-Folgeergebnis wird dauerhaft bestätigt — lokal geprüft
Ein nachträgliches authentisiertes JobResult mit höherer Revision darf jetzt
`unknown` bleiben, wenn der signierte Sync den Server bereits auf Unknown
gesetzt hat. Dieselben Tenant-/Agent-/Job-/Versuchs-/Grantprüfungen gelten
unverändert. Die konkrete Receipt/ACK kann das Ergebnisjournal leeren; Status,
Startreservierung und ursprünglicher Grant bleiben gesperrt. Kein neuer Versuch,
Unlock oder irreführender `job_reconciled`-Audit für unverändertes Unknown.
Tatsächlich ausgeführt: ein gezielter isolierter PostgreSQL-Integrationstest
mit allen Migrationen und RLS als Anwendungsrolle, ohne veröffentlichte Ports,
sowie Server-Clippy über alle Targets mit `-D warnings`: bestanden. Gleiche
Nachricht idempotent bestätigt; falsche/fehlende Grants, fremder Tenant/Agent,
anderer Versuch und widersprüchliche Revision verweigert. Eigene Testdatenbank,
kein Produktionsplan oder RMM-Job genehmigt. Clouddeployment und tatsächlicher
Reconnect dieses Pfads sind separat noch offen.
## Windows-Laufzeit erhaltend als LocalService aktualisiert — 2026-11-14
Zusätzlicher Helper-Quelllauf: zunächst20/21 native Fixtures bestanden; Serde