symbi-runtime 1.11.0

Agent Runtime System for the Symbi platform
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
//! HTTP Input Module Integration Tests
//!
//! Tests for the HTTP Input module that validates functionality, security, and correctness
//! based on the original test plan.

#[cfg(feature = "http-input")]
use std::sync::Arc;
#[cfg(feature = "http-input")]
use std::time::Duration;

#[cfg(feature = "http-input")]
use serde_json::json;
#[cfg(feature = "http-input")]
use tokio::time::timeout;

#[cfg(feature = "http-input")]
use symbi_runtime::{
    http_input::{HttpInputConfig, HttpInputServer},
    AgentId, AgentRuntime, RuntimeConfig,
};

#[cfg(feature = "http-input")]
/// Create a test HTTP input configuration with a random port
fn create_test_config(port: u16) -> HttpInputConfig {
    HttpInputConfig {
        bind_address: "127.0.0.1".to_string(),
        port,
        path: "/webhook".to_string(),
        agent: AgentId::new(),
        auth_header: Some("Bearer test-token-123".to_string()),
        jwt_public_key_path: None,
        max_body_bytes: 1024, // 1KB for testing payload size limits
        concurrency: 5,
        routing_rules: None,
        response_control: None,
        forward_headers: vec![],
        cors_origins: vec!["*".to_string()],
        audit_enabled: true,
        webhook_verify: None,
    }
}

#[cfg(feature = "http-input")]
/// Create a minimal agent runtime for testing
async fn create_test_runtime() -> AgentRuntime {
    let config = RuntimeConfig::default();
    AgentRuntime::new(config)
        .await
        .expect("Failed to create runtime")
}

#[cfg(feature = "http-input")]
/// Find an available port for testing
async fn find_available_port() -> u16 {
    use tokio::net::TcpListener;

    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();
    drop(listener);
    port
}

#[cfg(feature = "http-input")]
/// Start a test HTTP server and return the server handle and base URL
async fn start_test_server() -> (tokio::task::JoinHandle<()>, String, u16) {
    let port = find_available_port().await;
    let config = create_test_config(port);
    let runtime = Arc::new(create_test_runtime().await);
    let base_url = format!("http://127.0.0.1:{}", port);

    let server = HttpInputServer::new(config).with_runtime(runtime);

    let handle = tokio::spawn(async move {
        if let Err(e) = server.start().await {
            eprintln!("Server error: {:?}", e);
        }
    });

    // Wait a moment for server to start
    tokio::time::sleep(Duration::from_millis(100)).await;

    (handle, base_url, port)
}

#[cfg(feature = "http-input")]
#[tokio::test]
async fn test_valid_request_is_accepted_and_processed() {
    // The test runtime does not actually register agents in a Running state,
    // so the webhook handler's agent-state check correctly rejects runtime
    // bus dispatch and falls through to the LLM invocation path. With no
    // LLM configured in tests, the handler returns a 500 error. Either
    // outcome (200 execution_started or 500 server error) indicates the
    // HTTP layer processed the request correctly — auth passed, JSON parsed,
    // body routed, and a JSON response emitted.
    let (_handle, base_url, _port) = start_test_server().await;
    let client = reqwest::Client::new();

    let payload = json!({
        "message": "Hello from webhook",
        "data": {
            "source": "test",
            "timestamp": "2024-01-01T00:00:00Z"
        }
    });

    let response = timeout(
        Duration::from_secs(5),
        client
            .post(format!("{}/webhook", base_url))
            .header("Authorization", "Bearer test-token-123")
            .header("Content-Type", "application/json")
            .json(&payload)
            .send(),
    )
    .await
    .expect("Request timeout")
    .expect("Request failed");

    let status = response.status();
    assert!(
        status == 200 || status.is_server_error(),
        "expected 200 (runtime dispatched) or 5xx (no runtime/LLM available), got {}",
        status,
    );
    assert!(response
        .headers()
        .get("content-type")
        .unwrap()
        .to_str()
        .unwrap()
        .contains("application/json"));

    // Response body must be a well-formed JSON object with a status/error
    // indicator regardless of which path was taken.
    let response_body: serde_json::Value = response
        .json()
        .await
        .expect("Failed to parse JSON response");
    assert!(
        response_body.get("status").is_some() || response_body.get("error").is_some(),
        "response body must contain status or error field: {}",
        response_body,
    );
}

#[cfg(feature = "http-input")]
#[tokio::test]
async fn test_invalid_token_returns_401_unauthorized() {
    let (_handle, base_url, _port) = start_test_server().await;
    let client = reqwest::Client::new();

    let payload = json!({
        "message": "This should fail with wrong token"
    });

    let response = timeout(
        Duration::from_secs(5),
        client
            .post(format!("{}/webhook", base_url))
            .header("Authorization", "Bearer wrong-token")
            .header("Content-Type", "application/json")
            .json(&payload)
            .send(),
    )
    .await
    .expect("Request timeout")
    .expect("Request failed");

    // Assert unauthorized response
    assert_eq!(response.status(), 401);
}

#[cfg(feature = "http-input")]
#[tokio::test]
async fn test_missing_token_returns_401_unauthorized() {
    let (_handle, base_url, _port) = start_test_server().await;
    let client = reqwest::Client::new();

    let payload = json!({
        "message": "This should fail without token"
    });

    let response = timeout(
        Duration::from_secs(5),
        client
            .post(format!("{}/webhook", base_url))
            .header("Content-Type", "application/json")
            .json(&payload)
            .send(),
    )
    .await
    .expect("Request timeout")
    .expect("Request failed");

    // Assert unauthorized response
    assert_eq!(response.status(), 401);
}

#[cfg(feature = "http-input")]
#[tokio::test]
async fn test_payload_too_large_returns_413() {
    let (_handle, base_url, _port) = start_test_server().await;
    let client = reqwest::Client::new();

    // Create a payload larger than the configured max_body_bytes (1024 bytes)
    let large_data = "x".repeat(2048); // 2KB payload
    let payload = json!({
        "message": "Large payload test",
        "large_data": large_data
    });

    let response = timeout(
        Duration::from_secs(5),
        client
            .post(format!("{}/webhook", base_url))
            .header("Authorization", "Bearer test-token-123")
            .header("Content-Type", "application/json")
            .json(&payload)
            .send(),
    )
    .await
    .expect("Request timeout")
    .expect("Request failed");

    // Assert payload too large response
    assert_eq!(response.status(), 413);
}

#[cfg(feature = "http-input")]
#[tokio::test]
async fn test_malformed_json_returns_400_bad_request() {
    let (_handle, base_url, _port) = start_test_server().await;
    let client = reqwest::Client::new();

    // Send malformed JSON
    let malformed_json = r#"{"message": "incomplete json""#; // Missing closing brace

    let response = timeout(
        Duration::from_secs(5),
        client
            .post(format!("{}/webhook", base_url))
            .header("Authorization", "Bearer test-token-123")
            .header("Content-Type", "application/json")
            .body(malformed_json)
            .send(),
    )
    .await
    .expect("Request timeout")
    .expect("Request failed");

    // Assert bad request response
    assert_eq!(response.status(), 400);
}

#[cfg(feature = "http-input")]
#[tokio::test]
async fn test_agent_interaction_and_invocation() {
    let (_handle, base_url, _port) = start_test_server().await;
    let client = reqwest::Client::new();

    let payload = json!({
        "action": "test_agent_invocation",
        "parameters": {
            "test_param": "test_value"
        }
    });

    let response = timeout(
        Duration::from_secs(5),
        client
            .post(format!("{}/webhook", base_url))
            .header("Authorization", "Bearer test-token-123")
            .header("Content-Type", "application/json")
            .json(&payload)
            .send(),
    )
    .await
    .expect("Request timeout")
    .expect("Request failed");

    // The test runtime does not register agents as Running, so dispatch
    // falls through to the LLM path; with no LLM configured, the handler
    // returns 500. Either outcome indicates the HTTP layer routed the
    // request correctly.
    let status = response.status();
    assert!(
        status == 200 || status.is_server_error(),
        "expected 200 (runtime dispatched) or 5xx (no runtime/LLM available), got {}",
        status,
    );

    let response_body: serde_json::Value = response
        .json()
        .await
        .expect("Failed to parse JSON response");

    // Response must be a well-formed JSON object with a timestamp, and
    // one of status/error indicating the outcome.
    assert!(
        response_body.get("status").is_some() || response_body.get("error").is_some(),
        "response body must contain status or error field: {}",
        response_body,
    );
    assert!(response_body.get("timestamp").is_some());

    let timestamp_str = response_body["timestamp"].as_str().unwrap();
    assert!(chrono::DateTime::parse_from_rfc3339(timestamp_str).is_ok());
}

#[cfg(feature = "http-input")]
#[tokio::test]
async fn test_cors_headers_when_enabled() {
    let (_handle, base_url, _port) = start_test_server().await;
    let client = reqwest::Client::new();

    // Send an OPTIONS request to check CORS headers
    let response = timeout(
        Duration::from_secs(5),
        client
            .request(reqwest::Method::OPTIONS, format!("{}/webhook", base_url))
            .header("Origin", "https://example.com")
            .header("Access-Control-Request-Method", "POST")
            .send(),
    )
    .await
    .expect("Request timeout")
    .expect("Request failed");

    // Check for CORS headers (exact headers depend on tower-http CORS implementation)
    assert!(
        response
            .headers()
            .contains_key("access-control-allow-origin")
            || response
                .headers()
                .contains_key("Access-Control-Allow-Origin")
    );
}

#[cfg(feature = "http-input")]
#[tokio::test]
async fn test_content_type_enforcement() {
    let (_handle, base_url, _port) = start_test_server().await;
    let client = reqwest::Client::new();

    // Send request without proper content-type
    let response = timeout(
        Duration::from_secs(5),
        client
            .post(format!("{}/webhook", base_url))
            .header("Authorization", "Bearer test-token-123")
            .body(r#"{"message": "test"}"#)
            // Deliberately omit Content-Type header
            .send(),
    )
    .await
    .expect("Request timeout")
    .expect("Request failed");

    // The server should handle this gracefully: 4xx for bad content-type,
    // 2xx if it processes the body as JSON, or 5xx if the runtime/LLM
    // path isn't available (no registered agent in the test harness).
    // The key behavior is that it doesn't panic or hang.
    let status = response.status();
    assert!(
        status.is_client_error() || status.is_success() || status.is_server_error(),
        "unexpected response status: {}",
        status,
    );
}

#[cfg(feature = "http-input")]
#[tokio::test]
async fn test_concurrent_requests_within_limits() {
    let (_handle, base_url, _port) = start_test_server().await;
    let client = reqwest::Client::new();

    let _payload = json!({
        "message": "Concurrent request test"
    });

    // Send multiple concurrent requests (within the concurrency limit of 5)
    let mut handles = vec![];
    for i in 0..3 {
        let client = client.clone();
        let url = format!("{}/webhook", base_url);
        let payload = json!({
            "message": format!("Concurrent request {}", i)
        });

        let handle = tokio::spawn(async move {
            timeout(
                Duration::from_secs(5),
                client
                    .post(&url)
                    .header("Authorization", "Bearer test-token-123")
                    .header("Content-Type", "application/json")
                    .json(&payload)
                    .send(),
            )
            .await
            .expect("Request timeout")
            .expect("Request failed")
        });

        handles.push(handle);
    }

    // Wait for all requests to complete
    let responses = futures::future::join_all(handles).await;

    // All requests should be accepted and processed without being rejected
    // by the concurrency limiter (which would return 429). Each is either
    // 200 (runtime dispatched) or 5xx (no runtime/LLM path available in
    // the test harness) — both prove the limiter let the request through.
    for response in responses {
        let response = response.expect("Task failed");
        let status = response.status();
        assert!(
            status != reqwest::StatusCode::TOO_MANY_REQUESTS,
            "concurrency limiter rejected a request within limit: {}",
            status,
        );
        assert!(
            status.is_success() || status.is_server_error(),
            "unexpected status: {}",
            status,
        );
    }
}

// Test that only compiles when http-input feature is enabled
#[cfg(not(feature = "http-input"))]
#[tokio::test]
async fn test_http_input_feature_disabled() {
    // This test ensures that when the http-input feature is not enabled,
    // we can't access the http_input module

    // This should not compile if http-input feature is disabled:
    // use symbi_runtime::http_input::HttpInputConfig;

    // Instead, we just verify the test runs
    // Test completed successfully
}