actionqueue-executor-local 0.1.2

Local executor with dispatch queue, retry, backoff, and timeout for the ActionQueue task queue engine.
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
//! Hard-cap retry boundary tests for local attempt execution.
//!
//! These tests verify the max-attempt cap invariance: no N+1 attempts are
//! permitted under any retry path.

use std::time::Duration;

use actionqueue_core::ids::{AttemptId, RunId};
use actionqueue_core::task::constraints::TaskConstraints;
use actionqueue_executor_local::{
    can_retry, decide_retry_transition, AttemptOutcomeKind, AttemptRunner, AttemptTimer,
    ExecutorContext, ExecutorHandler, ExecutorRequest, HandlerOutput, RetryDecision,
    RetryDecisionError, RetryDecisionInput,
};

#[derive(Debug, Clone, Copy)]
struct FixedTimer {
    elapsed: Duration,
}

impl AttemptTimer for FixedTimer {
    type Mark = ();

    fn start(&self) -> Self::Mark {}

    fn elapsed_since(&self, _mark: Self::Mark) -> Duration {
        self.elapsed
    }
}

struct RetryableFailureHandler;

impl ExecutorHandler for RetryableFailureHandler {
    fn execute(&self, ctx: ExecutorContext) -> HandlerOutput {
        let _input = ctx.input;
        HandlerOutput::RetryableFailure {
            error: "deterministic retryable failure for cap boundary tests".to_string(),
            consumption: vec![],
        }
    }
}

struct TerminalFailureHandler;

impl ExecutorHandler for TerminalFailureHandler {
    fn execute(&self, ctx: ExecutorContext) -> HandlerOutput {
        let _input = ctx.input;
        HandlerOutput::TerminalFailure {
            error: "deterministic terminal failure for cap boundary tests".to_string(),
            consumption: vec![],
        }
    }
}

struct SuccessHandler;

impl ExecutorHandler for SuccessHandler {
    fn execute(&self, ctx: ExecutorContext) -> HandlerOutput {
        let _input = ctx.input;
        HandlerOutput::Success { output: None, consumption: vec![] }
    }
}

fn make_request(
    run_id: RunId,
    attempt_id: AttemptId,
    attempt_number: u32,
    max_attempts: u32,
) -> ExecutorRequest {
    ExecutorRequest {
        run_id,
        attempt_id,
        payload: vec![],
        constraints: TaskConstraints::new(max_attempts, Some(60), None)
            .expect("test constraints should be valid"),
        attempt_number,
        submission: None,
        children: None,
        cancellation_context: None,
    }
}

fn make_retry_input(
    attempt_number: u32,
    max_attempts: u32,
    outcome_kind: AttemptOutcomeKind,
) -> RetryDecisionInput {
    RetryDecisionInput {
        run_id: RunId::new(),
        attempt_id: AttemptId::new(),
        attempt_number,
        max_attempts,
        outcome_kind,
    }
}

#[test]
fn retry_helper_boundary_matrix_one_nominal_and_exhausted_values() {
    let boundary_one = make_retry_input(1, 1, AttemptOutcomeKind::RetryableFailure);
    assert_eq!(decide_retry_transition(&boundary_one), Ok(RetryDecision::Fail));
    assert_eq!(can_retry(&boundary_one), Ok(false));

    let nominal_under_cap = make_retry_input(2, 3, AttemptOutcomeKind::RetryableFailure);
    assert_eq!(decide_retry_transition(&nominal_under_cap), Ok(RetryDecision::Retry));
    assert_eq!(can_retry(&nominal_under_cap), Ok(true));

    let exhausted_at_cap = make_retry_input(3, 3, AttemptOutcomeKind::RetryableFailure);
    assert_eq!(decide_retry_transition(&exhausted_at_cap), Ok(RetryDecision::Fail));
    assert_eq!(can_retry(&exhausted_at_cap), Ok(false));
}

#[test]
fn runner_never_emits_n_plus_one_retry_decision() {
    let run_id = RunId::new();
    let first_attempt = AttemptId::new();
    let second_attempt = AttemptId::new();
    let cap = 2;
    let runner = AttemptRunner::with_timer(
        RetryableFailureHandler,
        FixedTimer { elapsed: Duration::from_millis(4) },
    );

    let first_record = runner.run_attempt(make_request(run_id, first_attempt, 1, cap));
    assert_eq!(first_record.retry_decision, Ok(RetryDecision::Retry));

    let second_record = runner.run_attempt(make_request(run_id, second_attempt, 2, cap));
    assert_eq!(second_record.retry_decision, Ok(RetryDecision::Fail));

    assert!(matches!(first_record.retry_decision, Ok(RetryDecision::Retry)));
    assert!(matches!(second_record.retry_decision, Ok(RetryDecision::Fail)));
}

#[test]
fn max_attempts_one_boundary() {
    // With max_attempts=1, attempt 1 must fail (cannot retry)
    let input = make_retry_input(1, 1, AttemptOutcomeKind::RetryableFailure);
    assert_eq!(decide_retry_transition(&input), Ok(RetryDecision::Fail));
    assert_eq!(can_retry(&input), Ok(false));
}

#[test]
fn max_attempts_two_boundary_retries_under_cap() {
    // With max_attempts=2, attempt 1 may retry (1 < 2)
    let under_cap = make_retry_input(1, 2, AttemptOutcomeKind::RetryableFailure);
    assert_eq!(decide_retry_transition(&under_cap), Ok(RetryDecision::Retry));
    assert_eq!(can_retry(&under_cap), Ok(true));

    // Attempt 2 at cap must fail (2 == 2, not < 2)
    let at_cap = make_retry_input(2, 2, AttemptOutcomeKind::RetryableFailure);
    assert_eq!(decide_retry_transition(&at_cap), Ok(RetryDecision::Fail));
    assert_eq!(can_retry(&at_cap), Ok(false));
}

#[test]
fn max_attempts_three_boundary_exhaustive() {
    // With max_attempts=3, attempts 1 and 2 may retry, attempt 3 must fail
    let attempt_1 = make_retry_input(1, 3, AttemptOutcomeKind::RetryableFailure);
    assert_eq!(decide_retry_transition(&attempt_1), Ok(RetryDecision::Retry));
    assert_eq!(can_retry(&attempt_1), Ok(true));

    let attempt_2 = make_retry_input(2, 3, AttemptOutcomeKind::RetryableFailure);
    assert_eq!(decide_retry_transition(&attempt_2), Ok(RetryDecision::Retry));
    assert_eq!(can_retry(&attempt_2), Ok(true));

    let attempt_3 = make_retry_input(3, 3, AttemptOutcomeKind::RetryableFailure);
    assert_eq!(decide_retry_transition(&attempt_3), Ok(RetryDecision::Fail));
    assert_eq!(can_retry(&attempt_3), Ok(false));
}

#[test]
fn success_always_completes_regardless_of_cap() {
    // Success at any attempt number should always complete
    let at_attempt_1 = make_retry_input(1, 3, AttemptOutcomeKind::Success);
    assert_eq!(decide_retry_transition(&at_attempt_1), Ok(RetryDecision::Complete));
    assert_eq!(can_retry(&at_attempt_1), Ok(false));

    let at_attempt_3 = make_retry_input(3, 3, AttemptOutcomeKind::Success);
    assert_eq!(decide_retry_transition(&at_attempt_3), Ok(RetryDecision::Complete));
    assert_eq!(can_retry(&at_attempt_3), Ok(false));
}

#[test]
fn terminal_failure_always_fails_regardless_of_cap() {
    // Terminal failure at any attempt number should always fail
    let at_attempt_1 = make_retry_input(1, 3, AttemptOutcomeKind::TerminalFailure);
    assert_eq!(decide_retry_transition(&at_attempt_1), Ok(RetryDecision::Fail));
    assert_eq!(can_retry(&at_attempt_1), Ok(false));

    let at_attempt_3 = make_retry_input(3, 3, AttemptOutcomeKind::TerminalFailure);
    assert_eq!(decide_retry_transition(&at_attempt_3), Ok(RetryDecision::Fail));
    assert_eq!(can_retry(&at_attempt_3), Ok(false));
}

#[test]
fn timeout_retries_under_cap_and_fails_at_cap() {
    // Timeout behaves like retryable failure for cap enforcement
    let under_cap = make_retry_input(1, 2, AttemptOutcomeKind::Timeout);
    assert_eq!(decide_retry_transition(&under_cap), Ok(RetryDecision::Retry));
    assert_eq!(can_retry(&under_cap), Ok(true));

    let at_cap = make_retry_input(2, 2, AttemptOutcomeKind::Timeout);
    assert_eq!(decide_retry_transition(&at_cap), Ok(RetryDecision::Fail));
    assert_eq!(can_retry(&at_cap), Ok(false));
}

#[test]
fn n_plus_one_attempt_path_returns_error() {
    // Attempt number exceeds cap - this is a hard invariant violation
    let input = make_retry_input(4, 3, AttemptOutcomeKind::RetryableFailure);

    assert_eq!(
        decide_retry_transition(&input),
        Err(RetryDecisionError::AttemptExceedsCap { attempt_number: 4, max_attempts: 3 })
    );
}

#[test]
fn runner_enforces_cap_with_terminal_failure() {
    // Even with terminal failure, no N+1 should occur
    let run_id = RunId::new();
    let attempt_1 = AttemptId::new();
    let attempt_2 = AttemptId::new();
    let cap = 2;
    let runner = AttemptRunner::with_timer(
        TerminalFailureHandler,
        FixedTimer { elapsed: Duration::from_millis(4) },
    );

    let record_1 = runner.run_attempt(make_request(run_id, attempt_1, 1, cap));
    assert_eq!(record_1.retry_decision, Ok(RetryDecision::Fail));
    assert_eq!(record_1.retry_decision_input.attempt_number, 1);

    // Attempt 2 would fail, but this is still within cap (2 <= 2)
    let record_2 = runner.run_attempt(make_request(run_id, attempt_2, 2, cap));
    assert_eq!(record_2.retry_decision, Ok(RetryDecision::Fail));
    assert_eq!(record_2.retry_decision_input.attempt_number, 2);

    // Attempt 3 would exceed cap (3 > 2)
    let attempt_3 = AttemptId::new();
    let record_3 = runner.run_attempt(make_request(run_id, attempt_3, 3, cap));
    assert_eq!(
        record_3.retry_decision,
        Err(RetryDecisionError::AttemptExceedsCap { attempt_number: 3, max_attempts: 2 })
    );
}

#[test]
fn runner_enforces_cap_with_success() {
    // Success terminates the run - no further attempts should be attempted
    let run_id = RunId::new();
    let attempt_1 = AttemptId::new();
    let cap = 2;
    let runner =
        AttemptRunner::with_timer(SuccessHandler, FixedTimer { elapsed: Duration::from_millis(4) });

    let record_1 = runner.run_attempt(make_request(run_id, attempt_1, 1, cap));
    assert_eq!(record_1.retry_decision, Ok(RetryDecision::Complete));
    assert_eq!(record_1.retry_decision_input.attempt_number, 1);

    // The run is complete, so no more attempts should be made
    // This test verifies the contract: even with max_attempts=2, the run
    // completes after attempt 1 succeeds
}

#[test]
fn all_retryable_failure_paths_respect_cap() {
    // Test all outcome kinds that could trigger retry under cap
    let outcome_kinds = [AttemptOutcomeKind::RetryableFailure, AttemptOutcomeKind::Timeout];

    for outcome in outcome_kinds {
        // At cap, should fail
        let at_cap = make_retry_input(3, 3, outcome);
        assert_eq!(
            decide_retry_transition(&at_cap),
            Ok(RetryDecision::Fail),
            "outcome {outcome:?} at cap should fail"
        );
        assert_eq!(
            can_retry(&at_cap),
            Ok(false),
            "outcome {outcome:?} at cap should not be retryable"
        );

        // Under cap, should retry
        let under_cap = make_retry_input(2, 3, outcome);
        assert_eq!(
            decide_retry_transition(&under_cap),
            Ok(RetryDecision::Retry),
            "outcome {outcome:?} under cap should retry"
        );
        assert_eq!(
            can_retry(&under_cap),
            Ok(true),
            "outcome {outcome:?} under cap should be retryable"
        );
    }
}

#[test]
fn attempt_runner_deterministic_output_at_boundary() {
    // Verify that the AttemptRunner produces deterministic outputs at boundary
    let run_id = RunId::new();
    let attempt_1 = AttemptId::new();
    let attempt_2 = AttemptId::new();
    let attempt_3 = AttemptId::new();
    let cap = 3;

    let runner = AttemptRunner::with_timer(
        RetryableFailureHandler,
        FixedTimer { elapsed: Duration::from_millis(4) },
    );

    let record_1 = runner.run_attempt(make_request(run_id, attempt_1, 1, cap));
    assert_eq!(record_1.retry_decision, Ok(RetryDecision::Retry));
    assert_eq!(record_1.retry_decision_input.max_attempts, 3);
    assert_eq!(record_1.retry_decision_input.attempt_number, 1);

    let record_2 = runner.run_attempt(make_request(run_id, attempt_2, 2, cap));
    assert_eq!(record_2.retry_decision, Ok(RetryDecision::Retry));
    assert_eq!(record_2.retry_decision_input.max_attempts, 3);
    assert_eq!(record_2.retry_decision_input.attempt_number, 2);

    let record_3 = runner.run_attempt(make_request(run_id, attempt_3, 3, cap));
    assert_eq!(record_3.retry_decision, Ok(RetryDecision::Fail));
    assert_eq!(record_3.retry_decision_input.max_attempts, 3);
    assert_eq!(record_3.retry_decision_input.attempt_number, 3);
}

#[test]
fn invalid_max_attempts_is_rejected() {
    // max_attempts=0 is invalid
    let input = make_retry_input(1, 0, AttemptOutcomeKind::RetryableFailure);

    assert_eq!(
        decide_retry_transition(&input),
        Err(RetryDecisionError::InvalidMaxAttempts { max_attempts: 0 })
    );
}

#[test]
fn invalid_attempt_number_is_rejected() {
    // attempt_number=0 is invalid
    let input = make_retry_input(0, 1, AttemptOutcomeKind::RetryableFailure);

    assert_eq!(
        decide_retry_transition(&input),
        Err(RetryDecisionError::InvalidAttemptNumber { attempt_number: 0 })
    );
}

#[test]
fn can_retry_matches_decide_retry_transition() {
    // Verify that can_retry returns true iff decide_retry_transition returns Retry
    let test_cases = [
        (1, 1, AttemptOutcomeKind::RetryableFailure, false),
        (1, 2, AttemptOutcomeKind::RetryableFailure, true),
        (2, 2, AttemptOutcomeKind::RetryableFailure, false),
        (1, 3, AttemptOutcomeKind::Timeout, true),
        (3, 3, AttemptOutcomeKind::Timeout, false),
        (1, 3, AttemptOutcomeKind::Success, false),
        (1, 3, AttemptOutcomeKind::TerminalFailure, false),
        (2, 3, AttemptOutcomeKind::TerminalFailure, false),
    ];

    for (attempt_num, max, outcome, expected_can_retry) in test_cases {
        let input = make_retry_input(attempt_num, max, outcome);

        let decision = decide_retry_transition(&input).expect("decision should succeed");
        let can_retry_result = can_retry(&input).expect("can_retry should succeed");

        // can_retry returns true only if decision is Retry
        let expected_decision = if expected_can_retry {
            RetryDecision::Retry
        } else if outcome == AttemptOutcomeKind::Success {
            RetryDecision::Complete
        } else {
            RetryDecision::Fail
        };

        assert_eq!(decision, expected_decision, "mismatch for attempt {attempt_num}/{max}");
        assert_eq!(
            can_retry_result, expected_can_retry,
            "can_retry mismatch for attempt {attempt_num}/{max}"
        );
    }
}

#[test]
fn cap_boundary_consistency_across_various_outcomes() {
    // Verify consistent cap boundary behavior across different outcome kinds
    let outcomes = [
        AttemptOutcomeKind::Success,
        AttemptOutcomeKind::RetryableFailure,
        AttemptOutcomeKind::TerminalFailure,
        AttemptOutcomeKind::Timeout,
    ];

    for outcome in outcomes {
        // At cap (attempt 3 of 3)
        let at_cap = make_retry_input(3, 3, outcome);
        let decision = decide_retry_transition(&at_cap).expect("decision should succeed");

        match outcome {
            AttemptOutcomeKind::Success => {
                assert_eq!(decision, RetryDecision::Complete);
                assert_eq!(can_retry(&at_cap), Ok(false));
            }
            AttemptOutcomeKind::RetryableFailure | AttemptOutcomeKind::Timeout => {
                assert_eq!(decision, RetryDecision::Fail);
                assert_eq!(can_retry(&at_cap), Ok(false));
            }
            AttemptOutcomeKind::TerminalFailure => {
                assert_eq!(decision, RetryDecision::Fail);
                assert_eq!(can_retry(&at_cap), Ok(false));
            }
            // Suspended bypasses cap validation — tested separately.
            AttemptOutcomeKind::Suspended => {}
        }
    }
}