greentic-runner-host 1.2.2

Host runtime shim for Greentic runner: config, pack loading, activity handling
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
#![allow(clippy::unwrap_used, clippy::expect_used, unsafe_code)]

use super::*;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use greentic_ext_runtime::host_ports::{
    ArtifactPort, ArtifactPortError, ArtifactPutRequest, HostCallContext,
};
use wiremock::matchers::{header, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

use crate::host::ExtArtifactPort;

const VALID_ID: &str =
    "artifact://0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";

fn ctx(tenant: &str) -> HostCallContext {
    HostCallContext {
        tenant: Some(tenant.into()),
        ..HostCallContext::default()
    }
}

fn request() -> ArtifactPutRequest {
    ArtifactPutRequest {
        bytes: vec![1, 2, 3],
        mime_type: "image/png".into(),
        name: "a.png".into(),
    }
}

fn port_for(server: &MockServer, token: &str) -> HttpArtifactPort {
    HttpArtifactPort::new(
        format!("{}/artifacts", server.uri()),
        token.into(),
        tokio::runtime::Handle::current(),
    )
    .unwrap()
}

/// `put` is synchronous (wasmtime host fns are wired on the sync linker), so
/// call it from a blocking thread the way the host does.
async fn put(port: HttpArtifactPort, tenant: &'static str) -> Result<String, ArtifactPortError> {
    tokio::task::spawn_blocking(move || port.put("greentic.media", &ctx(tenant), request()))
        .await
        .unwrap()
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn put_posts_the_contract_body_and_returns_the_id() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/artifacts/put"))
        .and(header("authorization", "Bearer gtm_secret"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": VALID_ID, "sha256": "aa", "size_bytes": 3,
            "kind": "image", "mime_type": "image/png"
        })))
        .expect(1)
        .mount(&server)
        .await;
    assert_eq!(
        put(port_for(&server, "gtm_secret"), "acme").await.unwrap(),
        VALID_ID
    );
    let received = server.received_requests().await.unwrap();
    let body: serde_json::Value = serde_json::from_slice(&received[0].body).unwrap();
    assert_eq!(body["name"], "a.png");
    assert_eq!(body["mime_type"], "image/png");
    assert_eq!(body["data_base64"], "AQID");
    assert!(body["derived_from"].is_null());
    assert!(
        body["conversation_id"].is_null(),
        "an extension call has no conversation"
    );
    assert!(
        body.get("tenant").is_none(),
        "the tenant comes from the token, never the body"
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn door_statuses_map_onto_port_errors() {
    for (status, want) in [
        (415u16, "InvalidMediaType"),
        (422, "QuotaExceeded"),
        (401, "Unavailable"),
        (403, "Unavailable"),
        (413, "Unavailable"),
        (429, "Unavailable"),
        (503, "Unavailable"),
    ] {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(status))
            .mount(&server)
            .await;
        let err = put(port_for(&server, "t"), "acme").await.unwrap_err();
        assert!(format!("{err:?}").starts_with(want), "{status} -> {err:?}");
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_200_with_an_unreadable_body_is_unavailable() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(200).set_body_string("not json"))
        .mount(&server)
        .await;
    let err = put(port_for(&server, "t"), "acme").await.unwrap_err();
    assert!(matches!(err, ArtifactPortError::Unavailable(_)), "{err:?}");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_redirect_is_never_followed() {
    let server = MockServer::start().await;
    let elsewhere = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(
            ResponseTemplate::new(307)
                .insert_header("location", format!("{}/steal", elsewhere.uri()).as_str()),
        )
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(200))
        .expect(0)
        .mount(&elsewhere)
        .await;
    let err = put(port_for(&server, "gtm_secret"), "acme")
        .await
        .unwrap_err();
    assert!(matches!(err, ArtifactPortError::Unavailable(_)), "{err:?}");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_blank_tenant_is_refused_without_calling_the_door() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(200))
        .expect(0)
        .mount(&server)
        .await;
    let err = put(port_for(&server, "t"), "  ").await.unwrap_err();
    assert!(matches!(err, ArtifactPortError::Unavailable(_)), "{err:?}");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn the_token_never_appears_in_debug_or_in_an_error() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(503).set_body_string("gtm_topsecret echoed"))
        .mount(&server)
        .await;
    let port = port_for(&server, "gtm_topsecret");
    assert!(!format!("{port:?}").contains("gtm_topsecret"));
    let err = put(port, "acme").await.unwrap_err();
    assert!(!format!("{err:?}").contains("gtm_topsecret"));
    assert!(!err.to_string().contains("gtm_topsecret"));
}

#[test]
fn an_unusable_token_is_refused_at_construction() {
    let rt = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap();
    for bad in ["", "   ", "gtm\u{7}x"] {
        assert!(
            HttpArtifactPort::new(
                "https://admin.example/artifacts".into(),
                bad.into(),
                rt.handle().clone()
            )
            .is_err(),
            "{bad:?}"
        );
    }
}

#[test]
fn a_current_thread_runtime_is_refused_instead_of_panicking() {
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    let port = HttpArtifactPort::new(
        "http://127.0.0.1:1/artifacts".into(),
        "t".into(),
        rt.handle().clone(),
    )
    .unwrap();
    // `block_in_place` panics on a current-thread runtime; the port must say so.
    let err = rt
        .block_on(async { port.put("x", &ctx("acme"), request()) })
        .unwrap_err();
    assert!(matches!(err, ArtifactPortError::Unavailable(_)), "{err:?}");
}

struct Noop;
impl ArtifactPort for Noop {}

fn env_probe(called: &AtomicUsize) -> impl FnOnce() -> Option<ExtArtifactPort> + '_ {
    move || {
        called.fetch_add(1, Ordering::SeqCst);
        Some(Arc::new(Noop) as ExtArtifactPort)
    }
}

#[test]
fn a_single_tenant_host_uses_its_port_else_the_env_port() {
    let called = AtomicUsize::new(0);
    let host: ExtArtifactPort = Arc::new(Noop);
    let chosen =
        crate::host::host_ext_artifact_port(Some(host.clone()), 1, true, env_probe(&called))
            .unwrap();
    assert!(Arc::ptr_eq(&chosen, &host));
    assert_eq!(called.load(Ordering::SeqCst), 0, "env not read");
    assert!(crate::host::host_ext_artifact_port(None, 1, true, env_probe(&called)).is_some());
    assert_eq!(called.load(Ordering::SeqCst), 1);
}

#[test]
fn a_multi_tenant_host_has_no_port_and_never_reads_the_env() {
    let called = AtomicUsize::new(0);
    let host: ExtArtifactPort = Arc::new(Noop);
    assert!(crate::host::host_ext_artifact_port(Some(host), 2, true, env_probe(&called)).is_none());
    assert!(crate::host::host_ext_artifact_port(None, 2, true, env_probe(&called)).is_none());
    assert_eq!(called.load(Ordering::SeqCst), 0);
}

#[test]
#[serial_test::serial]
fn env_port_needs_both_variables_and_a_runtime() {
    unsafe {
        std::env::remove_var("GREENTIC_ARTIFACT_ENDPOINT");
        std::env::remove_var("GREENTIC_ARTIFACT_TOKEN");
    }
    assert!(artifact_port_from_env().is_none());
    unsafe {
        std::env::set_var(
            "GREENTIC_ARTIFACT_ENDPOINT",
            "https://admin.example/api/v1/ingest/artifacts",
        )
    };
    assert!(
        artifact_port_from_env().is_none(),
        "endpoint alone is not enough"
    );
    unsafe { std::env::set_var("GREENTIC_ARTIFACT_TOKEN", "gtm_x") };
    // Outside a tokio runtime there is no handle to block on: no port, not a panic.
    assert!(artifact_port_from_env().is_none());
    let rt = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap();
    assert!(rt.block_on(async { artifact_port_from_env() }).is_some());
    unsafe {
        std::env::remove_var("GREENTIC_ARTIFACT_ENDPOINT");
        std::env::remove_var("GREENTIC_ARTIFACT_TOKEN");
    }
}

fn squash(src: &str) -> String {
    src.split_whitespace().collect::<Vec<_>>().join(" ")
}

/// The port reaches `build_ext_runtime` on both host paths; below the host
/// nothing falls back to the env.
#[test]
fn the_port_is_threaded_from_both_host_paths_and_never_from_the_env_below_them() {
    let agent = squash(include_str!("agent_node.rs"));
    assert!(
        !agent.contains("artifact_port_from_env"),
        "below the host, nothing may fall back to the env port"
    );
    let runtime = squash(include_str!("../runtime.rs"));
    assert!(
        runtime.contains("options.ext_artifact_port,"),
        "load_revision_with"
    );
    assert!(!runtime.contains("artifact_port_from_env"));
    let mut calls = 0;
    for needle in [
        "build_agent_node_wiring_metered(",
        "build_agent_node_wiring_ephemeral_metered(",
    ] {
        for (start, _) in runtime.match_indices(needle) {
            let rest = &runtime[start..];
            let end = rest.find(".await").expect("call has an .await");
            assert!(
                rest[..end].contains("ext_artifact_port.clone(),"),
                "`{needle}` must receive the port: {}",
                &rest[..end]
            );
            calls += 1;
        }
    }
    assert_eq!(calls, 3);
    let host = squash(include_str!("../host.rs"));
    assert!(
        host.contains("self.ext_artifact_port(),"),
        "host prepare_runtime"
    );
    let watcher = squash(include_str!("../watcher.rs"));
    assert!(
        watcher.contains("ext_artifact_port.clone(),"),
        "pack reload"
    );
}

/// The port shares the reader's endpoint rule: https, or loopback http only,
/// and never userinfo. A hostile endpoint gets no request at all.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_hostile_endpoint_gets_no_request_and_a_fixed_error() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(200))
        .expect(0)
        .mount(&server)
        .await;
    let port_no = server.address().port();
    for endpoint in [
        format!("http://user:pw@127.0.0.1:{port_no}/artifacts"),
        "http://admin.example/artifacts".to_string(),
        "file:///tmp/artifacts".to_string(),
        "admin.example/artifacts".to_string(),
    ] {
        let port = HttpArtifactPort::new(
            endpoint.clone(),
            "gtm_secret".into(),
            tokio::runtime::Handle::current(),
        )
        .unwrap();
        match put(port, "acme").await {
            Err(ArtifactPortError::Unavailable(msg)) => {
                assert_eq!(msg, "artifact endpoint is not usable", "{endpoint}")
            }
            other => panic!("{endpoint}: {other:?}"),
        }
    }
}

/// What matters is the runtime the CALL runs on, not the stored one:
/// `block_in_place` panics on a current-thread runtime even when the stored
/// handle is multi-thread. The port refuses instead.
#[test]
fn a_call_from_a_current_thread_runtime_is_refused_even_with_a_multi_thread_handle() {
    let stored = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(1)
        .enable_all()
        .build()
        .unwrap();
    let port = HttpArtifactPort::new(
        "http://127.0.0.1:1/artifacts".into(),
        "t".into(),
        stored.handle().clone(),
    )
    .unwrap();
    let caller = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    let err = caller
        .block_on(async { port.put("x", &ctx("acme"), request()) })
        .unwrap_err();
    match err {
        ArtifactPortError::Unavailable(msg) => {
            assert_eq!(msg, "artifact put needs a multi-thread runtime")
        }
        other => panic!("{other:?}"),
    }
}

/// The env fallback is OPT-IN: a single-tenant host that did not opt in gets
/// no env port, and a builder starts with the fallback off.
#[test]
fn the_env_port_fallback_is_off_unless_the_host_opts_in() {
    let called = AtomicUsize::new(0);
    assert!(crate::host::host_ext_artifact_port(None, 1, false, env_probe(&called)).is_none());
    assert_eq!(called.load(Ordering::SeqCst), 0, "env not read");
    assert!(!crate::host::HostBuilder::new().artifact_env_fallback_for_tests());
    assert!(
        crate::host::HostBuilder::new()
            .with_artifact_env_fallback(true)
            .artifact_env_fallback_for_tests()
    );
}

/// The door's reply is untrusted: an id that is not `artifact://<64 hex>` is
/// refused, and an oversized reply is not read into memory.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_bad_or_oversized_reply_is_refused() {
    for body in [
        serde_json::json!({ "id": "https://evil.example/x" }).to_string(),
        serde_json::json!({ "id": "artifact://abc" }).to_string(),
        serde_json::json!({ "id": VALID_ID, "pad": "x".repeat(64 * 1024) }).to_string(),
    ] {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(200).set_body_string(body.clone()))
            .mount(&server)
            .await;
        let err = put(port_for(&server, "t"), "acme").await.unwrap_err();
        assert!(
            matches!(err, ArtifactPortError::Unavailable(_)),
            "{err:?} for {}",
            &body[..40.min(body.len())]
        );
    }
}

/// The bearer token must go to the door only, never through a proxy named by
/// the process environment (reqwest reads it when the client is built).
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[serial_test::serial]
async fn the_client_ignores_the_proxy_environment() {
    let proxy = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(200))
        .mount(&proxy)
        .await;
    let door = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/artifacts/put"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": VALID_ID, "sha256": "aa", "size_bytes": 3,
            "kind": "image", "mime_type": "image/png"
        })))
        .mount(&door)
        .await;
    let keys = [
        "HTTP_PROXY",
        "http_proxy",
        "HTTPS_PROXY",
        "https_proxy",
        "ALL_PROXY",
        "all_proxy",
        "NO_PROXY",
        "no_proxy",
    ];
    let saved: Vec<_> = keys.iter().map(|k| (*k, std::env::var(k).ok())).collect();
    unsafe {
        for k in keys {
            std::env::remove_var(k);
        }
        std::env::set_var("HTTP_PROXY", proxy.uri());
        std::env::set_var("HTTPS_PROXY", proxy.uri());
    }
    let port = port_for(&door, "gtm_secret");
    let got = put(port, "t1").await;
    unsafe {
        for (k, v) in saved {
            match v {
                Some(v) => std::env::set_var(k, v),
                None => std::env::remove_var(k),
            }
        }
    }
    assert!(
        proxy.received_requests().await.unwrap().is_empty(),
        "the bearer request went through the proxy"
    );
    assert!(got.is_ok(), "{got:?}");
}

fn put_ok() -> ResponseTemplate {
    ResponseTemplate::new(200).set_body_json(serde_json::json!({
        "id": VALID_ID, "sha256": "aa", "size_bytes": 3,
        "kind": "image", "mime_type": "image/png"
    }))
}

/// Mounts `first` for the first `n` calls, then a successful put.
async fn door_failing_then_ok(server: &MockServer, first: ResponseTemplate, n: u64) {
    Mock::given(method("POST"))
        .respond_with(first)
        .up_to_n_times(n)
        .with_priority(1)
        .mount(server)
        .await;
    Mock::given(method("POST"))
        .respond_with(put_ok())
        .with_priority(2)
        .mount(server)
        .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_busy_or_failing_door_is_retried_and_the_put_succeeds() {
    for status in [408u16, 429, 502, 503, 504] {
        let server = MockServer::start().await;
        door_failing_then_ok(&server, ResponseTemplate::new(status), 1).await;
        let got = put(port_for(&server, "t"), "acme").await;
        assert_eq!(got.ok().as_deref(), Some(VALID_ID), "{status}");
        assert_eq!(
            server.received_requests().await.unwrap().len(),
            2,
            "{status}"
        );
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn final_door_answers_are_not_retried() {
    for status in [400u16, 401, 403, 404, 413, 415, 422] {
        let server = MockServer::start().await;
        door_failing_then_ok(&server, ResponseTemplate::new(status), 1).await;
        assert!(
            put(port_for(&server, "t"), "acme").await.is_err(),
            "{status}"
        );
        assert_eq!(
            server.received_requests().await.unwrap().len(),
            1,
            "{status}"
        );
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn put_retries_stop_after_three_attempts() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(ResponseTemplate::new(503))
        .mount(&server)
        .await;
    let err = put(port_for(&server, "t"), "acme").await.unwrap_err();
    assert!(matches!(err, ArtifactPortError::Unavailable(_)), "{err:?}");
    assert_eq!(server.received_requests().await.unwrap().len(), 3);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn put_honours_retry_after() {
    let server = MockServer::start().await;
    door_failing_then_ok(
        &server,
        ResponseTemplate::new(429).insert_header("retry-after", "1"),
        1,
    )
    .await;
    let started = std::time::Instant::now();
    assert!(put(port_for(&server, "t"), "acme").await.is_ok());
    assert!(
        started.elapsed() >= Duration::from_millis(950),
        "{:?}",
        started.elapsed()
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn at_most_two_puts_are_in_flight_per_port() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .respond_with(put_ok().set_delay(Duration::from_millis(400)))
        .mount(&server)
        .await;
    let port = Arc::new(port_for(&server, "t"));
    let started = std::time::Instant::now();
    let calls: Vec<_> = (0..4)
        .map(|_| {
            let port = Arc::clone(&port);
            tokio::task::spawn_blocking(move || port.put("greentic.media", &ctx("acme"), request()))
        })
        .collect();
    for call in calls {
        assert!(call.await.unwrap().is_ok());
    }
    // Four 400 ms answers two at a time take two rounds; unbounded, one.
    assert!(
        started.elapsed() >= Duration::from_millis(780),
        "{:?}",
        started.elapsed()
    );
}