rusty-agent-server 0.12.0

The durable agent runtime built in Rust — HTTP/SSE server: an Agent-Protocol subset with threads, background runs, checkpoints, and resumable streaming.
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
//! Integration tests for the rusty-agent-server HTTP API, driven in-process
//! via `tower::ServiceExt::oneshot` (no sockets).

use std::path::PathBuf;
use std::time::Duration;

use axum::body::{to_bytes, Body, Bytes};
use axum::http::{Request, StatusCode};
use axum::Router;
use rusty_agent_runtime::prelude::*;
use rusty_agent_server::{router, GraphRegistry, ServerConfig};
use serde_json::{json, Value};
use tower::ServiceExt;

// --------------------------------------------------------------------- //
// Test graphs
// --------------------------------------------------------------------- //

/// `first -> second`, appending to a `log` channel.
fn pipeline_graph() -> (Graph, StateSpec) {
    let spec = StateSpec::new().channel("log", Reducer::Append);
    let mut builder = GraphBuilder::new();
    builder.add_node("first", |_ctx: NodeContext| async {
        Ok(NodeOutput::update("log", json!("first")))
    });
    builder.add_node("second", |_ctx: NodeContext| async {
        Ok(NodeOutput::update("log", json!("second")))
    });
    builder.set_entry_point("first");
    builder.add_edge("first", "second");
    (builder.compile().unwrap(), spec)
}

/// A single gate node that interrupts until resumed.
fn interrupt_graph() -> (Graph, StateSpec) {
    let spec = StateSpec::new().channel("answer", Reducer::Overwrite);
    let mut builder = GraphBuilder::new();
    builder.add_node("gate", |ctx: NodeContext| async move {
        match ctx.resume_value() {
            Some(v) => Ok(NodeOutput::update("answer", v.clone())),
            None => Err(ctx.interrupt(json!({"question": "approve?"}))),
        }
    });
    builder.set_entry_point("gate");
    (builder.compile().unwrap(), spec)
}

/// A slow node, for multitask-strategy tests.
fn slow_graph() -> (Graph, StateSpec) {
    let spec = StateSpec::new().channel("done", Reducer::Overwrite);
    let mut builder = GraphBuilder::new();
    builder.add_node("slow", |_ctx: NodeContext| async {
        tokio::time::sleep(Duration::from_millis(400)).await;
        Ok(NodeOutput::update("done", json!(true)))
    });
    builder.set_entry_point("slow");
    (builder.compile().unwrap(), spec)
}

// --------------------------------------------------------------------- //
// App + request helpers
// --------------------------------------------------------------------- //

/// Unique temp store root, removed at the end of each test (best effort).
fn temp_store() -> PathBuf {
    std::env::temp_dir().join(format!("rusty-server-test-{}", uuid::Uuid::new_v4()))
}

fn test_app(api_key: Option<&str>) -> (Router, PathBuf) {
    let store = temp_store();
    let (pipeline, pipeline_spec) = pipeline_graph();
    let (gate, gate_spec) = interrupt_graph();
    let (slow, slow_spec) = slow_graph();

    let mut registry = GraphRegistry::new();
    registry.register("pipeline", pipeline, pipeline_spec);
    registry.register("interrupt_gate", gate, gate_spec);
    registry.register("slow", slow, slow_spec);

    let mut config = ServerConfig::new("127.0.0.1:0".parse().unwrap(), store.clone());
    config.api_key = api_key.map(str::to_owned);
    (router(registry, config), store)
}

/// Send a request and return `(status, parsed-json-body-or-null)`.
async fn call(app: &Router, method: &str, uri: &str, body: Option<Value>) -> (StatusCode, Value) {
    let (status, bytes) = call_raw(app, method, uri, body, &[]).await;
    let value = if bytes.is_empty() {
        Value::Null
    } else {
        serde_json::from_slice(&bytes).unwrap_or(Value::Null)
    };
    (status, value)
}

/// Send a request and return `(status, raw-body-bytes)`.
async fn call_raw(
    app: &Router,
    method: &str,
    uri: &str,
    body: Option<Value>,
    extra_headers: &[(&str, &str)],
) -> (StatusCode, Bytes) {
    let mut builder = Request::builder().method(method).uri(uri);
    for (k, v) in extra_headers {
        builder = builder.header(*k, *v);
    }
    let body = match body {
        Some(v) => {
            builder = builder.header("content-type", "application/json");
            Body::from(v.to_string())
        }
        None => Body::empty(),
    };
    let response = app
        .clone()
        .oneshot(builder.body(body).unwrap())
        .await
        .unwrap();
    let status = response.status();
    let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    (status, bytes)
}

/// Create a thread on `graph`; returns its thread id.
async fn create_thread(app: &Router, graph: &str) -> String {
    let (status, v) = call(app, "POST", "/threads", Some(json!({"graph": graph}))).await;
    assert_eq!(status, StatusCode::CREATED, "thread creation failed: {v}");
    v["thread_id"].as_str().unwrap().to_string()
}

// --------------------------------------------------------------------- //
// Tests
// --------------------------------------------------------------------- //

#[tokio::test]
async fn ok_and_info_list_graphs() {
    let (app, store) = test_app(None);

    let (status, v) = call(&app, "GET", "/ok", None).await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(v["ok"], json!(true));

    let (status, v) = call(&app, "GET", "/info", None).await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(v["service"], json!("rusty-server"));
    let names: Vec<&str> = v["graphs"]
        .as_array()
        .unwrap()
        .iter()
        .map(|g| g["name"].as_str().unwrap())
        .collect();
    assert_eq!(names, ["interrupt_gate", "pipeline", "slow"]);
    let pipeline = &v["graphs"][1];
    assert_eq!(pipeline["channels"], json!(["log"]));

    let _ = std::fs::remove_dir_all(store);
}

#[tokio::test]
async fn unknown_graph_is_rejected() {
    let (app, store) = test_app(None);
    let (status, v) = call(&app, "POST", "/threads", Some(json!({"graph": "nope"}))).await;
    assert_eq!(status, StatusCode::BAD_REQUEST);
    assert_eq!(v["error"], json!("bad_request"));
    let _ = std::fs::remove_dir_all(store);
}

#[tokio::test]
async fn run_wait_completes_and_state_and_history_reflect_run() {
    let (app, store) = test_app(None);
    let thread = create_thread(&app, "pipeline").await;

    let (status, v) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/runs/wait"),
        Some(json!({})),
    )
    .await;
    assert_eq!(status, StatusCode::OK, "run/wait failed: {v}");
    assert_eq!(v["status"], json!("success"));
    assert_eq!(v["output"]["log"], json!(["first", "second"]));

    // GET state reflects the final checkpoint.
    let (status, v) = call(&app, "GET", &format!("/threads/{thread}/state"), None).await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(v["values"]["log"], json!(["first", "second"]));
    assert_eq!(v["next"], json!([]));
    assert!(v["checkpoint"]["checkpoint_id"].as_str().is_some());

    // History is newest-first: step 1 then step 0.
    let (status, v) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/history"),
        Some(json!({})),
    )
    .await;
    assert_eq!(status, StatusCode::OK);
    let items = v.as_array().unwrap();
    assert_eq!(items.len(), 2, "expected one checkpoint per super-step");
    assert_eq!(items[0]["checkpoint"]["step"], json!(1));
    assert_eq!(items[1]["checkpoint"]["step"], json!(0));
    assert_eq!(items[0]["values"]["log"], json!(["first", "second"]));
    assert_eq!(items[1]["values"]["log"], json!(["first"]));

    // limit + before filters.
    let first_id = items[0]["checkpoint"]["checkpoint_id"].as_str().unwrap();
    let (_, v) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/history"),
        Some(json!({"before": first_id})),
    )
    .await;
    assert_eq!(v.as_array().unwrap().len(), 1);
    let (_, v) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/history"),
        Some(json!({"limit": 1})),
    )
    .await;
    assert_eq!(v.as_array().unwrap().len(), 1);

    let _ = std::fs::remove_dir_all(store);
}

#[tokio::test]
async fn sse_stream_emits_frames_in_order() {
    let (app, store) = test_app(None);
    let thread = create_thread(&app, "pipeline").await;

    let (status, bytes) = call_raw(
        &app,
        "POST",
        &format!("/threads/{thread}/runs/stream"),
        Some(json!({})),
        &[],
    )
    .await;
    assert_eq!(status, StatusCode::OK);
    let body = String::from_utf8(bytes.to_vec()).unwrap();

    // Parse "event:/data:/id:" blocks.
    let mut frames: Vec<(String, Value, String)> = Vec::new();
    for block in body.split("\n\n") {
        let mut event = String::new();
        let mut data = String::new();
        let mut id = String::new();
        for line in block.lines() {
            if let Some(v) = line.strip_prefix("event:") {
                event = v.trim().to_string();
            } else if let Some(v) = line.strip_prefix("data:") {
                data = v.trim().to_string();
            } else if let Some(v) = line.strip_prefix("id:") {
                id = v.trim().to_string();
            }
        }
        if !event.is_empty() {
            frames.push((event, serde_json::from_str(&data).unwrap(), id));
        }
    }

    let events: Vec<&str> = frames.iter().map(|(e, _, _)| e.as_str()).collect();
    assert_eq!(
        events,
        ["metadata", "updates", "values", "updates", "values", "end"],
        "unexpected frame sequence; raw body:\n{body}"
    );

    // metadata carries run + thread identity.
    assert_eq!(frames[0].1["thread_id"], json!(thread));
    assert_eq!(frames[0].1["graph"], json!("pipeline"));
    assert!(frames[0].1["run_id"].as_str().is_some());

    // updates frames carry per-step POST-reducer values (core deliberately
    // changed `GraphEvent::StateUpdate.updates` to the merged state read-back:
    // the full appended list for an `Append` channel, not the raw per-node
    // partials).
    assert_eq!(frames[1].1["step"], json!(0));
    assert_eq!(frames[1].1["updates"]["log"], json!(["first"]));
    assert_eq!(frames[3].1["step"], json!(1));
    assert_eq!(frames[3].1["updates"]["log"], json!(["first", "second"]));

    // values frames carry the full state at each boundary.
    assert_eq!(frames[2].1["log"], json!(["first"]));
    assert_eq!(frames[4].1["log"], json!(["first", "second"]));

    // end frame reports success; frame ids are {checkpoint}:{step}:{seq}.
    assert_eq!(frames[5].1["status"], json!("success"));
    for (_, _, id) in &frames {
        let parts: Vec<&str> = id.split(':').collect();
        assert_eq!(parts.len(), 3, "bad frame id `{id}`");
        assert!(parts[2].parse::<u64>().is_ok(), "bad seq in id `{id}`");
    }

    let _ = std::fs::remove_dir_all(store);
}

#[tokio::test]
async fn resume_after_interrupt_round_trip() {
    let (app, store) = test_app(None);
    let thread = create_thread(&app, "interrupt_gate").await;

    // First run suspends on the gate's interrupt.
    let (status, v) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/runs/wait"),
        Some(json!({})),
    )
    .await;
    assert_eq!(status, StatusCode::OK);
    assert_eq!(v["status"], json!("interrupted"));
    assert_eq!(v["interrupt"], json!({"question": "approve?"}));
    assert!(v["checkpoint_id"].as_str().is_some());

    // GET state shows the suspension point scheduling the gate node.
    let (_, v) = call(&app, "GET", &format!("/threads/{thread}/state"), None).await;
    assert_eq!(v["next"], json!(["gate"]));

    // Resume via command.resume: the run completes with the payload applied.
    let (status, v) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/runs/wait"),
        Some(json!({"command": {"resume": {"approved": true}}})),
    )
    .await;
    assert_eq!(status, StatusCode::OK, "resume run failed: {v}");
    assert_eq!(v["status"], json!("success"));
    assert_eq!(v["output"]["answer"], json!({"approved": true}));

    let _ = std::fs::remove_dir_all(store);
}

#[tokio::test]
async fn multitask_reject_returns_409() {
    let (app, store) = test_app(None);
    let thread = create_thread(&app, "slow").await;

    // Background run occupies the thread slot.
    let (status, v) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/runs"),
        Some(json!({})),
    )
    .await;
    assert_eq!(status, StatusCode::ACCEPTED, "background run failed: {v}");
    assert_eq!(v["status"], json!("running"));
    assert!(v["run_id"].as_str().is_some());

    // A concurrent run with `reject` is refused.
    let (status, v) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/runs"),
        Some(json!({"multitask_strategy": "reject"})),
    )
    .await;
    assert_eq!(status, StatusCode::CONFLICT);
    assert_eq!(v["error"], json!("conflict"));

    let _ = std::fs::remove_dir_all(store);
}

#[tokio::test]
async fn multitask_enqueue_waits_for_its_turn() {
    let (app, store) = test_app(None);
    let thread = create_thread(&app, "slow").await;

    let (status, _) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/runs"),
        Some(json!({})),
    )
    .await;
    assert_eq!(status, StatusCode::ACCEPTED);

    // Default strategy is `enqueue`: this run queues behind the active one,
    // then runs and completes when the first run drains.
    let (status, v) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/runs/wait"),
        Some(json!({})),
    )
    .await;
    assert_eq!(status, StatusCode::OK, "enqueued run failed: {v}");
    assert_eq!(v["status"], json!("success"));
    assert_eq!(v["output"]["done"], json!(true));

    let _ = std::fs::remove_dir_all(store);
}

#[tokio::test]
async fn update_state_writes_a_checkpoint() {
    let (app, store) = test_app(None);
    let thread = create_thread(&app, "pipeline").await;

    let (status, v) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/state"),
        Some(json!({"values": {"log": ["manual"]}, "as_node": "first"})),
    )
    .await;
    assert_eq!(status, StatusCode::CREATED, "update_state failed: {v}");
    assert_eq!(v["checkpoint"]["step"], json!(0));

    let (_, v) = call(&app, "GET", &format!("/threads/{thread}/state"), None).await;
    assert_eq!(v["values"]["log"], json!(["manual"]));

    // Non-object values are rejected.
    let (status, _) = call(
        &app,
        "POST",
        &format!("/threads/{thread}/state"),
        Some(json!({"values": [1, 2, 3]})),
    )
    .await;
    assert_eq!(status, StatusCode::BAD_REQUEST);

    let _ = std::fs::remove_dir_all(store);
}

#[tokio::test]
async fn auth_rejects_missing_key_and_accepts_valid_key() {
    let (app, store) = test_app(Some("s3cret"));

    let (status, _) = call(&app, "GET", "/ok", None).await;
    assert_eq!(status, StatusCode::UNAUTHORIZED);

    let (status, _) = call_raw(&app, "GET", "/ok", None, &[("x-api-key", "wrong")]).await;
    assert_eq!(status, StatusCode::UNAUTHORIZED);

    let (status, bytes) = call_raw(&app, "GET", "/ok", None, &[("x-api-key", "s3cret")]).await;
    assert_eq!(status, StatusCode::OK);
    let v: Value = serde_json::from_slice(&bytes).unwrap();
    assert_eq!(v["ok"], json!(true));

    let _ = std::fs::remove_dir_all(store);
}

#[tokio::test]
async fn missing_thread_returns_404() {
    let (app, store) = test_app(None);
    let (status, v) = call(&app, "GET", "/threads/no-such-thread/state", None).await;
    assert_eq!(status, StatusCode::NOT_FOUND);
    assert_eq!(v["error"], json!("not_found"));
    let _ = std::fs::remove_dir_all(store);
}