algeon-query-runtime 0.1.2

Native query runtime, lifecycle, ledger, and observation owners.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use query_planning::error::{AllocationSource, QueryError, QueryErrorCode};

use super::*;

fn failure(code: AttemptFailureCode, facts: Vec<AttemptFailureFact>) -> AttemptFailureSummary {
    AttemptFailureSummary {
        origin: AttemptFailureOrigin::Native,
        code,
        status: AttemptFailureStatus::Temporary,
        operation: AttemptFailureOperation::Execution,
        text: None,
        facts,
    }
}

fn reason(value: AttemptFailureFactValue) -> AttemptFailureFact {
    AttemptFailureFact { key: AttemptFailureFactKey::Reason, value }
}

#[test]
fn native_failure_summary_projects_only_known_cugraph_allocation_sources_in_key_order() {
    let error = QueryError::new(
        QueryErrorCode::DeviceAllocationFailed,
        "query_engine.test.allocation_source",
        "cuGraph allocation failed",
    )
    .with_fact("reason", "backend_closing")
    .with_allocation_source(AllocationSource::RmmBadAlloc);

    let summary = AttemptFailureSummary::from_native_error(&error, AttemptFailureOperation::Execution);
    assert_eq!(
        summary.facts(),
        &[
            reason(AttemptFailureFactValue::BackendClosing),
            AttemptFailureFact {
                key: AttemptFailureFactKey::AllocationSource,
                value: AttemptFailureFactValue::CugraphRmmBadAlloc,
            },
        ]
    );

    for (source, expected) in [
        (AllocationSource::RmmOutOfMemory, AttemptFailureFactValue::CugraphRmmOutOfMemory),
        (AllocationSource::StdBadAlloc, AttemptFailureFactValue::CugraphStdBadAlloc),
    ] {
        let error = QueryError::new(
            QueryErrorCode::DeviceAllocationFailed,
            "query_engine.test.allocation_source",
            "cuGraph allocation failed",
        )
        .with_allocation_source(source);
        assert_eq!(
            AttemptFailureSummary::from_native_error(&error, AttemptFailureOperation::Execution).facts(),
            &[AttemptFailureFact { key: AttemptFailureFactKey::AllocationSource, value: expected }]
        );
    }

    let unknown = QueryError::new(
        QueryErrorCode::DeviceAllocationFailed,
        "query_engine.test.allocation_source",
        "cuGraph allocation failed",
    )
    .with_allocation_source(AllocationSource::Unknown);
    assert!(AttemptFailureSummary::from_native_error(&unknown, AttemptFailureOperation::Execution).facts().is_empty());

    let untyped_fact = QueryError::new(
        QueryErrorCode::DeviceAllocationFailed,
        "query_engine.test.allocation_source",
        "cuGraph allocation failed",
    )
    .with_fact("cugraph_allocation_source", "rmm_bad_alloc");
    assert!(
        AttemptFailureSummary::from_native_error(&untyped_fact, AttemptFailureOperation::Execution).facts().is_empty(),
        "free-form diagnostic facts must not mint sealed allocation provenance"
    );
}

fn exact_allocator_evidence(source: AttemptAllocatorEventSource) -> AttemptResourceEvidence {
    let mut evidence = AttemptResourceEvidence::unavailable();
    evidence.allocator_event = AttemptEvidenceField::attempt_exact(AttemptAllocatorEvent {
        sequence: 1,
        monotonic_time_ns: AttemptEvidenceField::unavailable(),
        requested_bytes: 1024,
        source,
        action: AttemptEvidenceField::unavailable(),
        callback_attempt: 0,
        trim_retry_count: 0,
        pool_usage: AttemptEvidenceField::unavailable(),
    });
    evidence
}

fn device_local_fault_evidence() -> AttemptNativeFaultEvidence {
    AttemptNativeFaultEvidence {
        isolation: AttemptFaultIsolation::DeviceLocal,
        teardown_health: AttemptTeardownHealth::Completed,
        device_health: Some(AttemptDeviceHealth {
            status: AttemptDeviceHealthStatus::Unhealthy,
            fault_count: 1,
            rejected_operation_count: 1,
            quarantined_teardown_count: 0,
        }),
        first_fault: Some(AttemptNativeFault {
            kind: AttemptNativeFaultKind::FatalDeviceOperation,
            status: Some(AttemptNativeStatus::Cuda(700)),
        }),
    }
}

fn advice_for(
    outcome: &RuntimeTerminalOutcome,
    primary_failure: Option<&AttemptFailureSummary>,
    capacity_disposition: AttemptCapacityDisposition,
    health_transition: Option<AttemptHealthTransition>,
    native_fault_evidence: Option<AttemptNativeFaultEvidence>,
    resource_evidence: Option<AttemptResourceEvidence>,
    alternate_device_eligibility: AttemptAlternateDeviceEligibility,
) -> AttemptRetryAdvice {
    AttemptRetryAdvice::from_sealed_attempt(
        outcome,
        primary_failure,
        capacity_disposition,
        health_transition,
        native_fault_evidence,
        resource_evidence,
        alternate_device_eligibility,
    )
}

#[test]
fn attempt_failure_text_is_utf8_safe_and_bounded() {
    let multibyte = "é".repeat(MAX_ATTEMPT_FAILURE_TEXT_BYTES);
    let text = AttemptFailureText::from_bytes(multibyte.as_bytes());
    assert!(text.value().len() <= MAX_ATTEMPT_FAILURE_TEXT_BYTES);
    assert!(text.truncated());
    assert!(text.value().is_char_boundary(text.value().len()));

    let invalid = AttemptFailureText::from_bytes(&[b'o', b'k', 0xff, b'!']);
    assert!(invalid.invalid_utf8());
    assert!(invalid.value().contains('\u{fffd}'));
    assert!(invalid.value().len() <= MAX_ATTEMPT_FAILURE_TEXT_BYTES);
}

#[test]
fn attempt_failure_text_redacts_uris_credentials_and_sql_shapes() {
    for sensitive in [
        "reader failed at s3://user:password@example.invalid/bucket/object",
        "authorization: bearer secret-value",
        "SELECT* FROM private_table",
    ] {
        let text = AttemptFailureText::from_bytes(sensitive.as_bytes());
        assert_eq!(text.value(), "<redacted>");
        assert!(text.redacted());
    }
}

#[test]
fn failure_summary_deserialization_enforces_all_bounds_before_publication() {
    let oversized_text = serde_json::json!({
        "value": "x".repeat(MAX_ATTEMPT_FAILURE_TEXT_BYTES + 1),
        "truncated": false,
        "redacted": false,
        "invalid_utf8": false,
    });
    assert!(serde_json::from_value::<AttemptFailureText>(oversized_text).is_err());

    let fact = serde_json::json!({ "key": "reason", "value": "backend_closing" });
    let too_many_facts = serde_json::json!({
        "origin": "native",
        "code": "internal",
        "status": "permanent",
        "operation": "execution",
        "facts": vec![fact; MAX_ATTEMPT_FAILURE_FACTS + 1],
    });
    assert!(serde_json::from_value::<AttemptFailureSummary>(too_many_facts).is_err());

    let raw_uri = serde_json::json!({
        "value": "failed at s3://private-bucket/object",
        "truncated": false,
        "redacted": false,
        "invalid_utf8": false,
    });
    assert!(serde_json::from_value::<AttemptFailureText>(raw_uri).is_err());

    let falsely_redacted = serde_json::json!({
        "value": "authorization: bearer secret-value",
        "truncated": false,
        "redacted": true,
        "invalid_utf8": false,
    });
    assert!(serde_json::from_value::<AttemptFailureText>(falsely_redacted).is_err());
}

#[test]
fn resource_evidence_preserves_explicit_unavailable_provenance_through_serde() {
    let mut evidence = AttemptResourceEvidence::unavailable();
    evidence.grant_cap_bytes = AttemptEvidenceField::attempt_exact(4096);
    evidence.ledger_sequence = AttemptEvidenceField::device_snapshot(17);
    evidence.outcome_terminal_usage = AttemptEvidenceField::sampled_at_terminal_boundary(AttemptTimedDomainUsage {
        sampled_ns: 23,
        usage: AttemptFinalDomainUsage {
            domain_id: 7,
            active: true,
            active_members: 1,
            ceiling_bytes: 4096,
            allocation_limit_bytes: 4096,
            current_bytes: 32,
            peak_bytes: 32,
            total_bytes: 32,
            current_aligned_bytes: 32,
            peak_aligned_bytes: 32,
            total_allocations: 1,
            current_allocations: 1,
            peak_allocations: 1,
            failed_allocation_count: 0,
            largest_failed_allocation_bytes: 0,
        },
        pool_usage: Some(AttemptAllocatorPoolUsage {
            reserved_current_bytes: 64,
            reserved_high_bytes: 96,
            used_current_bytes: 32,
            used_high_bytes: 48,
        }),
    });

    let encoded = serde_json::to_value(evidence).expect("resource evidence serializes");
    assert_eq!(encoded["grant_cap_bytes"]["provenance"], "attempt_exact");
    assert_eq!(encoded["ledger_sequence"]["provenance"], "device_snapshot");
    assert_eq!(encoded["outcome_terminal_usage"]["provenance"], "sampled_at_terminal_boundary");
    assert_eq!(encoded["outcome_terminal_usage"]["value"]["pool_usage"]["reserved_high_bytes"], 96);
    assert_eq!(encoded["allocator_event"]["provenance"], "unavailable");
    assert!(encoded["allocator_event"]["value"].is_null());
    assert_eq!(
        serde_json::from_value::<AttemptResourceEvidence>(encoded).expect("resource evidence deserializes"),
        evidence
    );
}

#[test]
fn sealed_retry_advice_is_exhaustive_and_evidence_gated() {
    let no_commit =
        (AttemptCapacityDisposition::NotAdmitted, None, None, None, AttemptAlternateDeviceEligibility::Unavailable);
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::Succeeded,
            None,
            no_commit.0,
            no_commit.1,
            no_commit.2,
            no_commit.3,
            no_commit.4,
        ),
        AttemptRetryAdvice::no_retry_needed()
    );
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::Cancelled,
            None,
            no_commit.0,
            no_commit.1,
            no_commit.2,
            no_commit.3,
            no_commit.4,
        ),
        AttemptRetryAdvice::do_not_retry()
    );

    let unsupported = failure(AttemptFailureCode::Query(QueryErrorCode::UnsupportedOperator), Vec::new());
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&unsupported),
            Some(&unsupported),
            no_commit.0,
            no_commit.1,
            no_commit.2,
            no_commit.3,
            no_commit.4,
        ),
        AttemptRetryAdvice::change_configuration()
    );

    let query_cap_exceeded = failure(AttemptFailureCode::Query(QueryErrorCode::QueryMemoryLimitExceeded), Vec::new());
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&query_cap_exceeded),
            Some(&query_cap_exceeded),
            no_commit.0,
            no_commit.1,
            no_commit.2,
            no_commit.3,
            no_commit.4,
        ),
        AttemptRetryAdvice::change_configuration()
    );

    let queue = failure(
        AttemptFailureCode::Query(QueryErrorCode::ServiceOverloaded),
        vec![reason(AttemptFailureFactValue::AdmissionQueueFull)],
    );
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&queue),
            Some(&queue),
            no_commit.0,
            no_commit.1,
            no_commit.2,
            no_commit.3,
            no_commit.4,
        ),
        AttemptRetryAdvice::backoff()
    );
    let impossible_floor = failure(
        AttemptFailureCode::Query(QueryErrorCode::ServiceOverloaded),
        vec![reason(AttemptFailureFactValue::RequiredFloorExceedsDeviceCapacity)],
    );
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&impossible_floor),
            Some(&impossible_floor),
            no_commit.0,
            no_commit.1,
            no_commit.2,
            no_commit.3,
            no_commit.4,
        ),
        AttemptRetryAdvice::change_configuration()
    );

    let resource_exhausted = failure(AttemptFailureCode::Query(QueryErrorCode::MemoryReservationFailed), Vec::new());
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&resource_exhausted),
            Some(&resource_exhausted),
            no_commit.0,
            no_commit.1,
            no_commit.2,
            Some(exact_allocator_evidence(AttemptAllocatorEventSource::ReservationLimit)),
            no_commit.4,
        ),
        AttemptRetryAdvice::change_configuration()
    );
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&resource_exhausted),
            Some(&resource_exhausted),
            no_commit.0,
            no_commit.1,
            no_commit.2,
            Some(exact_allocator_evidence(AttemptAllocatorEventSource::AllocationLimit)),
            no_commit.4,
        ),
        AttemptRetryAdvice::change_configuration(),
        "an exact cache allocation limit cannot resolve without configuration change"
    );
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&resource_exhausted),
            Some(&resource_exhausted),
            no_commit.0,
            no_commit.1,
            no_commit.2,
            Some(exact_allocator_evidence(AttemptAllocatorEventSource::Physical)),
            no_commit.4,
        ),
        AttemptRetryAdvice::indeterminate(),
        "a physical allocator failure alone does not prove capacity will become available"
    );
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&resource_exhausted),
            Some(&resource_exhausted),
            no_commit.0,
            no_commit.1,
            no_commit.2,
            None,
            no_commit.4,
        ),
        AttemptRetryAdvice::indeterminate(),
        "OOM status without exact allocator evidence cannot recommend a retry"
    );

    let temporary = failure(AttemptFailureCode::Query(QueryErrorCode::CudfTemporaryFailure), Vec::new());
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&temporary),
            Some(&temporary),
            no_commit.0,
            no_commit.1,
            no_commit.2,
            no_commit.3,
            no_commit.4,
        ),
        AttemptRetryAdvice::indeterminate(),
        "a temporary code alone is not retry evidence"
    );

    let unknown_isolation = AttemptNativeFaultEvidence {
        isolation: AttemptFaultIsolation::Unknown,
        teardown_health: AttemptTeardownHealth::CallbackAbandoned,
        device_health: None,
        first_fault: None,
    };
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::Succeeded,
            None,
            AttemptCapacityDisposition::RetainedQuarantine,
            Some(AttemptHealthTransition::BackendUnhealthy),
            Some(unknown_isolation),
            None,
            AttemptAlternateDeviceEligibility::NotApplicable,
        ),
        AttemptRetryAdvice::recover_backend(),
        "unknown isolation is a backend recovery requirement even when cleanup did not replace the outcome"
    );

    let local_fault = device_local_fault_evidence();
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&temporary),
            Some(&temporary),
            AttemptCapacityDisposition::RetainedQuarantine,
            Some(AttemptHealthTransition::DeviceQuarantined),
            Some(local_fault),
            None,
            AttemptAlternateDeviceEligibility::Eligible,
        ),
        AttemptRetryAdvice::retry_another_eligible_device()
    );
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&temporary),
            Some(&temporary),
            AttemptCapacityDisposition::RetainedQuarantine,
            Some(AttemptHealthTransition::DeviceQuarantined),
            Some(local_fault),
            None,
            AttemptAlternateDeviceEligibility::NoEligibleAlternate,
        ),
        AttemptRetryAdvice::do_not_retry()
    );
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&temporary),
            Some(&temporary),
            AttemptCapacityDisposition::RetainedQuarantine,
            Some(AttemptHealthTransition::DeviceQuarantined),
            Some(local_fault),
            None,
            AttemptAlternateDeviceEligibility::Unavailable,
        ),
        AttemptRetryAdvice::indeterminate()
    );

    let stream_synchronization_fault = AttemptNativeFaultEvidence {
        isolation: AttemptFaultIsolation::DeviceLocal,
        teardown_health: AttemptTeardownHealth::StreamSynchronizationFailed,
        device_health: Some(AttemptDeviceHealth {
            status: AttemptDeviceHealthStatus::Healthy,
            fault_count: 0,
            rejected_operation_count: 0,
            quarantined_teardown_count: 1,
        }),
        first_fault: None,
    };
    assert_eq!(
        advice_for(
            &RuntimeTerminalOutcome::failed(&temporary),
            Some(&temporary),
            AttemptCapacityDisposition::RetainedQuarantine,
            Some(AttemptHealthTransition::DeviceQuarantined),
            Some(stream_synchronization_fault),
            None,
            AttemptAlternateDeviceEligibility::Eligible,
        ),
        AttemptRetryAdvice::retry_another_eligible_device(),
        "a stream-synchronization failure is itself proven device-local cleanup evidence"
    );
}

#[test]
fn retry_advice_deserialization_rejects_inconsistent_pairs() {
    let error = serde_json::from_str::<AttemptRetryAdvice>(
        r#"{"recovery":"retry_another_eligible_device","prerequisite":"none"}"#,
    )
    .expect_err("retry advice must reject an impossible recovery/prerequisite pair");

    assert!(error.to_string().contains("retry recovery and prerequisite are inconsistent"));
}