agent-effects-http 0.1.0

HTTP effects for agent-effects: reqwest failures classified, Idempotency-Key sent
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! HTTP effects against a real (local) HTTP server.

use std::collections::{HashMap, VecDeque};
use std::fmt::Write as _;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use agent_effects::store::{EffectStore, ErrorRecord};
use agent_effects::{
    EffectKey, EffectKind, EffectName, EffectOutcome, FailureClass, LogicalKey, RetryPolicy,
    Runtime,
};
use agent_effects_http::HttpEffect;
use agent_effects_memory::MemoryStore;
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};

/// Extra response headers.
type Headers = Vec<(&'static str, String)>;

/// What the server does with the next scripted POST.
#[derive(Clone, Debug)]
enum Reply {
    /// Answer with this status (applying the POST first if it is 2xx).
    Status(u16, Headers, String),
    /// Apply the POST, then close the connection without answering.
    ApplyThenDrop,
    /// Apply the POST, then answer `303 See Other` to this location.
    ApplyThenRedirect(String),
    /// Never answer.
    Hang,
}

#[derive(Clone, Debug)]
struct Recorded {
    method: String,
    path: String,
    headers: HashMap<String, String>,
    body: String,
}

#[derive(Default)]
struct State {
    script: VecDeque<Reply>,
    requests: Vec<Recorded>,
    applied: u32,
    /// Idempotency key → the id it created.
    keys: HashMap<String, String>,
}

/// A tiny HTTP/1.1 server: POSTs create a resource (deduplicated on the
/// `Idempotency-Key`), `GET /lookup` finds it, `GET /broken` answers 405.
#[derive(Clone)]
struct Server {
    base: String,
    state: Arc<Mutex<State>>,
}

impl Server {
    async fn start(script: impl IntoIterator<Item = Reply>) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let base = format!("http://{}", listener.local_addr().unwrap());
        let state = Arc::new(Mutex::new(State {
            script: script.into_iter().collect(),
            ..State::default()
        }));
        let shared = Arc::clone(&state);
        tokio::spawn(async move {
            loop {
                let (stream, _) = listener.accept().await.unwrap();
                tokio::spawn(handle(stream, Arc::clone(&shared)));
            }
        });
        Self { base, state }
    }

    fn url(&self, path: &str) -> String {
        format!("{}{path}", self.base)
    }

    fn requests(&self) -> Vec<Recorded> {
        self.state.lock().unwrap().requests.clone()
    }

    fn applied(&self) -> u32 {
        self.state.lock().unwrap().applied
    }
}

/// Applies a POST once per idempotency key; returns the resource id.
fn apply(state: &mut State, key: Option<&String>) -> String {
    if let Some(id) = key.and_then(|k| state.keys.get(k)) {
        return id.clone();
    }
    state.applied += 1;
    let id = format!("res#{}", state.applied);
    if let Some(key) = key {
        state.keys.insert(key.clone(), id.clone());
    }
    id
}

async fn handle(mut stream: TcpStream, state: Arc<Mutex<State>>) {
    let mut buffer = Vec::new();
    let mut chunk = [0u8; 4096];
    let head_end = loop {
        let n = stream.read(&mut chunk).await.unwrap_or(0);
        if n == 0 {
            return;
        }
        buffer.extend_from_slice(&chunk[..n]);
        if let Some(i) = buffer.windows(4).position(|w| w == b"\r\n\r\n") {
            break i + 4;
        }
    };
    let head = String::from_utf8_lossy(&buffer[..head_end]).into_owned();
    let mut lines = head.lines();
    let mut request_line = lines.next().unwrap().split(' ');
    let (method, path) = (
        request_line.next().unwrap().to_owned(),
        request_line.next().unwrap().to_owned(),
    );
    let headers: HashMap<String, String> = lines
        .filter_map(|l| l.split_once(':'))
        .map(|(k, v)| (k.trim().to_ascii_lowercase(), v.trim().to_owned()))
        .collect();
    let length: usize = headers
        .get("content-length")
        .and_then(|v| v.parse().ok())
        .unwrap_or(0);
    while buffer.len() < head_end + length {
        let n = stream.read(&mut chunk).await.unwrap_or(0);
        if n == 0 {
            break;
        }
        buffer.extend_from_slice(&chunk[..n]);
    }
    let body = String::from_utf8_lossy(&buffer[head_end..]).into_owned();
    let key = headers.get("idempotency-key").cloned();

    let answer = {
        let mut state = state.lock().unwrap();
        state.requests.push(Recorded {
            method: method.clone(),
            path: path.clone(),
            headers: headers.clone(),
            body,
        });
        if method == "GET" {
            match (path.as_str(), state.applied) {
                ("/broken", _) => Some((405, Vec::new(), "no".to_owned())),
                (_, 0) => Some((404, Vec::new(), "not found".to_owned())),
                (_, _) => Some((200, Vec::new(), r#"{"id":"res#1"}"#.to_owned())),
            }
        } else {
            match state.script.pop_front() {
                None => {
                    let id = apply(&mut state, key.as_ref());
                    Some((200, Vec::new(), format!(r#"{{"id":"{id}"}}"#)))
                }
                Some(Reply::Status(code, extra, payload)) => {
                    if (200..300).contains(&code) {
                        apply(&mut state, key.as_ref());
                    }
                    Some((code, extra, payload))
                }
                Some(Reply::ApplyThenDrop) => {
                    apply(&mut state, key.as_ref());
                    return; // drop the connection unanswered
                }
                Some(Reply::ApplyThenRedirect(location)) => {
                    apply(&mut state, key.as_ref());
                    Some((303, vec![("location", location)], String::new()))
                }
                Some(Reply::Hang) => None,
            }
        }
    };
    let Some((status, extra, payload)) = answer else {
        return std::future::pending().await;
    };
    let mut response = format!(
        "HTTP/1.1 {status} X\r\ncontent-length: {}\r\nconnection: close\r\n",
        payload.len()
    );
    for (name, value) in extra {
        write!(response, "{name}: {value}\r\n").unwrap();
    }
    response.push_str("\r\n");
    response.push_str(&payload);
    let _ = stream.write_all(response.as_bytes()).await;
    let _ = stream.shutdown().await;
}

#[derive(Serialize)]
struct Charge {
    amount: u64,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Payment {
    id: String,
}

const NO_WAIT: RetryPolicy = RetryPolicy {
    max_attempts: 3,
    initial_delay: Duration::ZERO,
    max_delay: Duration::ZERO,
    multiplier: 1.0,
    jitter: false,
};

fn runtime(store: &MemoryStore, retry: RetryPolicy) -> Runtime<MemoryStore> {
    Runtime::builder(store.clone()).retry_policy(retry).build()
}

async fn last_error(store: &MemoryStore, logical: &str) -> ErrorRecord {
    let key = EffectKey::new(
        EffectName::new("http.call").unwrap(),
        LogicalKey::new(logical).unwrap(),
    );
    store
        .get_by_key(&key)
        .await
        .unwrap()
        .unwrap()
        .last_error
        .unwrap()
}

#[tokio::test]
async fn a_success_sends_json_and_the_idempotency_key() {
    let server = Server::start([]).await;
    let client = reqwest::Client::new();
    let rt = runtime(&MemoryStore::new(), NO_WAIT);
    let outcome = rt
        .effect("http.call", "order-1")
        .remote_idempotency(true)
        .run(
            HttpEffect::post(&client, server.url("/charges"))
                .json(&Charge { amount: 4200 })
                .header("authorization", "Bearer test")
                .idempotency_key_header()
                .send_json::<Payment>(),
        )
        .await
        .unwrap();
    assert_eq!(
        outcome,
        EffectOutcome::Committed(Payment { id: "res#1".into() })
    );

    let request = &server.requests()[0];
    assert_eq!(
        (request.method.as_str(), request.path.as_str()),
        ("POST", "/charges")
    );
    assert_eq!(request.body, r#"{"amount":4200}"#);
    assert_eq!(request.headers["content-type"], "application/json");
    assert_eq!(request.headers["authorization"], "Bearer test");
    let expected = EffectKey::new(
        EffectName::new("http.call").unwrap(),
        LogicalKey::new("order-1").unwrap(),
    )
    .idempotency_key()
    .to_string();
    assert_eq!(request.headers["idempotency-key"], expected);
}

#[tokio::test]
async fn statuses_reach_the_runtime_as_failure_classes() {
    let client = reqwest::Client::new();
    let cases: Vec<(u16, Headers, FailureClass)> = vec![
        (400, vec![], FailureClass::Validation),
        (401, vec![], FailureClass::Authentication),
        (403, vec![], FailureClass::Authorization),
        (404, vec![], FailureClass::Permanent),
        (408, vec![], FailureClass::Transient),
        (409, vec![], FailureClass::Ambiguous),
        (422, vec![], FailureClass::Validation),
        (
            429,
            vec![("retry-after", "7".to_owned())],
            FailureClass::RateLimited {
                retry_after: Some(Duration::from_secs(7)),
            },
        ),
        (500, vec![], FailureClass::Ambiguous),
        (501, vec![], FailureClass::Permanent),
        (503, vec![], FailureClass::Transient),
        (504, vec![], FailureClass::Ambiguous),
    ];
    for (status, headers, class) in cases {
        let server = Server::start([Reply::Status(status, headers, "server says no".into())]).await;
        let store = MemoryStore::new();
        let outcome = runtime(&store, RetryPolicy::NONE)
            .effect("http.call", status)
            .run(HttpEffect::post(&client, server.url("/x")).send_json::<Payment>())
            .await
            .unwrap();
        let error = last_error(&store, &status.to_string()).await;
        assert_eq!(error.class, Some(class), "HTTP {status}");
        assert!(
            error.message.contains("server says no"),
            "HTTP {status}: {}",
            error.message
        );
        if class == FailureClass::Ambiguous {
            assert!(
                matches!(outcome, EffectOutcome::NeedsIntervention { .. }),
                "HTTP {status}"
            );
        } else {
            assert!(
                matches!(outcome, EffectOutcome::Failed(_)),
                "HTTP {status}: {outcome:?}"
            );
        }
    }
}

/// An address nothing listens on.
async fn unreachable(path: &str) -> String {
    let free = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let url = format!("http://{}{path}", free.local_addr().unwrap());
    drop(free);
    url
}

fn client_without_redirects() -> reqwest::Client {
    reqwest::Client::builder()
        .redirect(reqwest::redirect::Policy::none())
        .build()
        .unwrap()
}

#[tokio::test]
async fn a_refused_connection_was_never_sent_by_a_client_without_redirects() {
    let url = unreachable("/x").await;
    let store = MemoryStore::new();
    let outcome = runtime(&store, RetryPolicy::NONE)
        .effect("http.call", "refused")
        .run(
            HttpEffect::post(&client_without_redirects(), url)
                .client_follows_no_redirects()
                .send_json::<Payment>(),
        )
        .await
        .unwrap();
    assert!(matches!(outcome, EffectOutcome::Failed(_)), "{outcome:?}");
    assert_eq!(
        last_error(&store, "refused").await.class,
        Some(FailureClass::Transient)
    );
}

#[tokio::test]
async fn a_refused_connection_is_ambiguous_for_a_client_that_may_follow_redirects() {
    let url = unreachable("/x").await;
    let store = MemoryStore::new();
    let outcome = runtime(&store, NO_WAIT)
        .effect("http.call", "maybe-redirected")
        .kind(EffectKind::IrreversibleWrite)
        .run(HttpEffect::post(&reqwest::Client::new(), url).send_json::<Payment>())
        .await
        .unwrap();
    assert!(
        matches!(outcome, EffectOutcome::NeedsIntervention { .. }),
        "{outcome:?}"
    );
    assert_eq!(
        last_error(&store, "maybe-redirected").await.class,
        Some(FailureClass::Ambiguous)
    );
}

#[tokio::test]
async fn an_unreachable_redirect_after_an_applied_request_is_not_resent() {
    let target = unreachable("/receipt").await;
    let server = Server::start([
        Reply::ApplyThenRedirect(target.clone()),
        Reply::ApplyThenRedirect(target),
    ])
    .await;
    let outcome = runtime(&MemoryStore::new(), NO_WAIT)
        .effect("http.call", "redirected")
        .kind(EffectKind::IrreversibleWrite)
        .run(HttpEffect::post(&reqwest::Client::new(), server.url("/charges")).send())
        .await
        .unwrap();
    assert!(
        matches!(outcome, EffectOutcome::NeedsIntervention { .. }),
        "{outcome:?}"
    );
    assert_eq!(server.applied(), 1, "applied once");
}

#[tokio::test]
async fn an_unfollowed_redirect_is_ambiguous() {
    let server = Server::start([
        Reply::ApplyThenRedirect("/receipt".into()),
        Reply::ApplyThenRedirect("/receipt".into()),
    ])
    .await;
    let store = MemoryStore::new();
    let outcome = runtime(&store, NO_WAIT)
        .effect("http.call", "unfollowed")
        .kind(EffectKind::IrreversibleWrite)
        .run(
            HttpEffect::post(&client_without_redirects(), server.url("/charges"))
                .client_follows_no_redirects()
                .send(),
        )
        .await
        .unwrap();
    assert!(
        matches!(outcome, EffectOutcome::NeedsIntervention { .. }),
        "{outcome:?}"
    );
    assert_eq!(server.applied(), 1, "applied once");
    assert_eq!(
        last_error(&store, "unfollowed").await.class,
        Some(FailureClass::Ambiguous)
    );
}

#[tokio::test]
async fn a_timeout_after_sending_is_ambiguous() {
    let server = Server::start([Reply::Hang]).await;
    let store = MemoryStore::new();
    let call = runtime(&store, RetryPolicy::NONE)
        .effect("http.call", "slow")
        .run(
            HttpEffect::post(&reqwest::Client::new(), server.url("/x"))
                .timeout(Duration::from_millis(300))
                .send_json::<Payment>(),
        );
    let outcome = tokio::time::timeout(Duration::from_secs(10), call)
        .await
        .expect("the request timeout fired")
        .unwrap();
    assert!(
        matches!(outcome, EffectOutcome::NeedsIntervention { .. }),
        "{outcome:?}"
    );
    assert_eq!(
        last_error(&store, "slow").await.class,
        Some(FailureClass::Ambiguous)
    );
    assert_eq!(server.requests().len(), 1, "it was sent");
}

#[tokio::test]
async fn a_dropped_answer_is_resent_under_the_same_key_and_applied_once() {
    let server = Server::start([Reply::ApplyThenDrop]).await;
    let outcome = runtime(&MemoryStore::new(), NO_WAIT)
        .effect("http.call", "dropped")
        .remote_idempotency(true)
        .run(
            HttpEffect::post(&reqwest::Client::new(), server.url("/charges"))
                .idempotency_key_header()
                .send_json::<Payment>(),
        )
        .await
        .unwrap();
    assert_eq!(
        outcome,
        EffectOutcome::Committed(Payment { id: "res#1".into() })
    );
    let requests = server.requests();
    assert_eq!(requests.len(), 2, "re-sent");
    assert_eq!(
        requests[0].headers["idempotency-key"],
        requests[1].headers["idempotency-key"]
    );
    assert_eq!(server.applied(), 1, "applied once");
}

#[tokio::test]
async fn a_lookup_confirms_a_dropped_answer_without_resending() {
    let server = Server::start([Reply::ApplyThenDrop]).await;
    let client = reqwest::Client::new();
    let outcome = runtime(&MemoryStore::new(), NO_WAIT)
        .effect("http.call", "verified")
        .kind(EffectKind::IrreversibleWrite)
        .verify(HttpEffect::get(&client, server.url("/lookup")).verify_json::<Payment>())
        .run(HttpEffect::post(&client, server.url("/charges")).send_json::<Payment>())
        .await
        .unwrap();
    assert_eq!(
        outcome,
        EffectOutcome::Committed(Payment { id: "res#1".into() })
    );
    let posts = server
        .requests()
        .iter()
        .filter(|r| r.method == "POST")
        .count();
    assert_eq!((posts, server.applied()), (1, 1));
}

#[tokio::test]
async fn a_lookup_that_finds_nothing_lets_the_effect_run_again() {
    // The first request is lost before it applies; the lookup's 404 proves
    // that, so the runtime sends it again.
    let server = Server::start([Reply::Hang]).await;
    let client = reqwest::Client::new();
    let outcome = runtime(&MemoryStore::new(), NO_WAIT)
        .effect("http.call", "lost")
        .kind(EffectKind::IrreversibleWrite)
        .verify(HttpEffect::get(&client, server.url("/lookup")).verify_json::<Payment>())
        .run(
            HttpEffect::post(&client, server.url("/charges"))
                .timeout(Duration::from_millis(300))
                .send_json::<Payment>(),
        )
        .await
        .unwrap();
    assert_eq!(
        outcome,
        EffectOutcome::Committed(Payment { id: "res#1".into() })
    );
    let posts = server
        .requests()
        .iter()
        .filter(|r| r.method == "POST")
        .count();
    assert_eq!((posts, server.applied()), (2, 1));
}

#[tokio::test]
async fn a_broken_lookup_is_never_read_as_not_applied() {
    // The POST applied and its answer was lost; the lookup answers 405. The
    // runtime must not take that as "not applied" and charge again.
    let server = Server::start([Reply::ApplyThenDrop]).await;
    let client = reqwest::Client::new();
    let outcome = runtime(&MemoryStore::new(), NO_WAIT)
        .effect("http.call", "broken-lookup")
        .verify(HttpEffect::get(&client, server.url("/broken")).verify_json::<Payment>())
        .run(HttpEffect::post(&client, server.url("/charges")).send_json::<Payment>())
        .await
        .unwrap();
    assert!(
        matches!(outcome, EffectOutcome::Unknown { .. }),
        "{outcome:?}"
    );
    let posts = server
        .requests()
        .iter()
        .filter(|r| r.method == "POST")
        .count();
    assert_eq!((posts, server.applied()), (1, 1), "never re-sent");
}

#[tokio::test]
async fn an_unreadable_success_is_ambiguous() {
    let server = Server::start([Reply::Status(200, vec![], "not json".into())]).await;
    let store = MemoryStore::new();
    let outcome = runtime(&store, RetryPolicy::NONE)
        .effect("http.call", "garbled")
        .run(HttpEffect::post(&reqwest::Client::new(), server.url("/x")).send_json::<Payment>())
        .await
        .unwrap();
    assert!(
        matches!(outcome, EffectOutcome::NeedsIntervention { .. }),
        "{outcome:?}"
    );
    assert_eq!(
        last_error(&store, "garbled").await.class,
        Some(FailureClass::Ambiguous)
    );
    assert_eq!(server.applied(), 1, "it did apply");
}

#[tokio::test]
async fn an_invalid_request_is_a_validation_failure_and_never_sent() {
    let server = Server::start([]).await;
    let store = MemoryStore::new();
    let outcome = runtime(&store, NO_WAIT)
        .effect("http.call", "invalid")
        .run(
            HttpEffect::post(&reqwest::Client::new(), server.url("/x"))
                .header("bad header", "x")
                .send_json::<Payment>(),
        )
        .await
        .unwrap();
    assert!(matches!(outcome, EffectOutcome::Failed(_)), "{outcome:?}");
    assert_eq!(
        last_error(&store, "invalid").await.class,
        Some(FailureClass::Validation)
    );
    assert!(server.requests().is_empty());
}