salvor-server 0.5.0

Salvor control plane: an HTTP + server-sent-events server over the durable runtime
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
//! Shared harness for the `salvor-server` integration tests.
//!
//! The tests drive the real router over loopback HTTP: a [`TestServer`] binds
//! `127.0.0.1:0`, and [`reqwest`] talks to it. Hermeticity comes from two
//! pieces with nothing on the network:
//!
//! - [`ScriptedModel`] is a wiremock server that answers `POST /v1/messages`
//!   by the number of `messages` in the request, so replay across a resume
//!   picks the right turn.
//! - [`CountingTool`] is an in-process `DynTool` with a shared execution
//!   counter, which is how a test proves a write ran exactly once across a
//!   kill and a recovery.
//!
//! The [`agent_factory`] wires those into the [`AgentFactory`] the server
//! builds agents through, so the definition a test submits is opaque (the
//! factory always builds the same agent) yet the agent's hash is stable.

#![allow(dead_code)]

use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use bytes::Bytes;
use futures_util::{Stream, StreamExt};
use salvor_core::Effect;
use salvor_llm::{Client, Config};
use salvor_runtime::{Agent, ClockFn, RandomFn};
use salvor_server::{AgentFactory, AppState, BuiltAgent, LlmModelExecutor, ModelExecutor};
use salvor_store::{EventStore, SqliteStore};
use salvor_tools::{DynTool, HandlerError, Suspension, ToolCtx, ToolError, ToolOutcome};
use serde_json::{Value, json};
use time::macros::datetime;
use tokio::net::TcpListener;
use tokio::task::JoinHandle;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, Request, Respond, ResponseTemplate};

/// A constant clock, so recorded logs compare equal across a control run and a
/// recovered one.
pub fn fixed_clock() -> ClockFn {
    Arc::new(|| datetime!(2026-07-10 12:00:00 UTC))
}

/// A constant random source.
pub fn fixed_random() -> RandomFn {
    Arc::new(|| 11)
}

/// Server state over `store`, driving agents through `factory`, with the fixed
/// hooks and a short stream poll so tests finish quickly.
pub fn app_state(store: Arc<dyn EventStore>, factory: AgentFactory) -> AppState {
    AppState::new(store, factory)
        .with_hooks(fixed_clock(), fixed_random())
        .with_poll_interval(Duration::from_millis(10))
}

/// A bound, serving control plane, plus the state and task handle the test
/// keeps for aborting runs or standing a fresh server on the same store.
pub struct TestServer {
    /// The base URL, `http://127.0.0.1:<port>`.
    pub base: String,
    /// The state the router shares, for direct store reads and run aborts.
    pub state: AppState,
    /// The serving task.
    pub handle: JoinHandle<()>,
}

impl TestServer {
    /// Binds a loopback port and serves `state` on it.
    pub async fn spawn(state: AppState) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind a loopback port");
        let addr = listener.local_addr().expect("read the bound address");
        let serve_state = state.clone();
        let handle = tokio::spawn(async move {
            let _ = salvor_server::serve(listener, serve_state).await;
        });
        Self {
            base: format!("http://{addr}"),
            state,
            handle,
        }
    }
}

/// What a [`CountingTool`] does when executed.
#[derive(Clone)]
pub enum CountBehavior {
    /// Increment the counter and return `{"recorded": <input>}`.
    Record,
    /// Fail every execution with this message.
    Fail(String),
    /// Suspend the run with this schema.
    Suspend(Value),
    /// Increment the counter, then never return. The intent is recorded before
    /// the tool runs, so aborting the task while this hangs leaves a dangling
    /// write intent, which is exactly what needs-reconciliation is.
    Hang,
}

/// An in-process tool with a shared execution counter.
pub struct CountingTool {
    name: String,
    effect: Effect,
    behavior: CountBehavior,
    calls: Arc<AtomicUsize>,
}

#[async_trait::async_trait]
impl DynTool for CountingTool {
    fn name(&self) -> &str {
        &self.name
    }
    fn description(&self) -> &str {
        "a counting test tool"
    }
    fn effect(&self) -> Effect {
        self.effect
    }
    fn input_schema(&self) -> Value {
        json!({"type": "object"})
    }
    async fn call_json(
        &self,
        _ctx: &ToolCtx,
        input: Value,
    ) -> Result<ToolOutcome<Value>, ToolError> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        match &self.behavior {
            CountBehavior::Record => Ok(ToolOutcome::Output(json!({"recorded": input}))),
            CountBehavior::Fail(message) => Err(ToolError::Handler {
                tool: self.name.clone(),
                source: HandlerError::message(message.clone()),
            }),
            CountBehavior::Suspend(schema) => Ok(ToolOutcome::Suspend(Suspension {
                reason: "awaiting approval".to_owned(),
                input_schema: schema.clone(),
            })),
            CountBehavior::Hang => std::future::pending().await,
        }
    }
}

/// An agent factory that builds one agent: a mock model plus one counting
/// tool. The submitted definition is ignored, so the agent (and its hash) is
/// the same on every build, which is what a resume on a fresh server needs.
pub fn agent_factory(
    model_uri: String,
    tool_name: &str,
    effect: Effect,
    behavior: CountBehavior,
    calls: Arc<AtomicUsize>,
) -> AgentFactory {
    let tool_name = tool_name.to_owned();
    Arc::new(move |_definition| {
        let model_uri = model_uri.clone();
        let tool_name = tool_name.clone();
        let behavior = behavior.clone();
        let calls = calls.clone();
        Box::pin(async move {
            let tool = CountingTool {
                name: tool_name,
                effect,
                behavior,
                calls,
            };
            let agent = Agent::builder()
                .model(
                    Config::new().with_base_url(&model_uri).with_max_retries(0),
                    "test-model",
                )
                .system_prompt("You are a test agent.")
                .tool_dyn(Box::new(tool))
                .build()
                .map_err(|error| error.to_string())?;
            Ok(BuiltAgent {
                agent,
                servers: vec![],
            })
        })
    })
}

/// A fresh execution counter.
pub fn counter() -> Arc<AtomicUsize> {
    Arc::new(AtomicUsize::new(0))
}

/// The default model executor pointed at a mock provider `uri`, for the
/// server-performed model step. Retries are off (the scripts are
/// deterministic). Wraps the general `salvor_llm::Client`, exactly as
/// `salvor serve` wires its own out of the box.
pub fn model_executor(uri: &str) -> Arc<dyn ModelExecutor> {
    let client = Client::new(Config::new().with_base_url(uri).with_max_retries(0))
        .expect("model client builds");
    Arc::new(LlmModelExecutor::new(client))
}

/// A canned text response with fixed usage.
pub fn text_response(text: &str, input_tokens: u64, output_tokens: u64) -> Value {
    json!({
        "id": format!("msg_text_{input_tokens}_{output_tokens}"),
        "model": "test-model",
        "role": "assistant",
        "content": [{"type": "text", "text": text}],
        "stop_reason": "end_turn",
        "usage": {"input_tokens": input_tokens, "output_tokens": output_tokens}
    })
}

/// A canned response asking to call one tool.
pub fn tool_use_response(
    tool_use_id: &str,
    tool: &str,
    input: Value,
    input_tokens: u64,
    output_tokens: u64,
) -> Value {
    json!({
        "id": format!("msg_tool_{tool_use_id}"),
        "model": "test-model",
        "role": "assistant",
        "content": [{"type": "tool_use", "id": tool_use_id, "name": tool, "input": input}],
        "stop_reason": "tool_use",
        "usage": {"input_tokens": input_tokens, "output_tokens": output_tokens}
    })
}

/// A scripted model, matching on the number of `messages` in the request. Each
/// entry is `(message_count, response, optional_delay)`.
pub struct ScriptedModel {
    script: Vec<(usize, Value, Option<Duration>)>,
}

impl ScriptedModel {
    /// Mounts the script on a fresh mock server and returns it.
    pub async fn mount(script: Vec<(usize, Value, Option<Duration>)>) -> MockServer {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1/messages"))
            .respond_with(Self { script })
            .mount(&server)
            .await;
        server
    }
}

impl Respond for ScriptedModel {
    fn respond(&self, request: &Request) -> ResponseTemplate {
        let body: Value = match serde_json::from_slice(&request.body) {
            Ok(body) => body,
            Err(_) => return ResponseTemplate::new(400),
        };
        let count = body
            .get("messages")
            .and_then(Value::as_array)
            .map_or(0, Vec::len);
        for (expected, response, delay) in &self.script {
            if *expected == count {
                let mut template = ResponseTemplate::new(200).set_body_json(response.clone());
                if let Some(delay) = delay {
                    template = template.set_delay(*delay);
                }
                return template;
            }
        }
        ResponseTemplate::new(500).set_body_json(json!({
            "error": {"type": "test_script", "message": format!("no response for {count} messages")}
        }))
    }
}

// ---------------------------------------------------------------------------
// HTTP helpers over the bound server.
// ---------------------------------------------------------------------------

/// A GET returning the status and decoded JSON body.
pub async fn get_json(
    client: &reqwest::Client,
    url: &str,
    auth: Option<&str>,
) -> (reqwest::StatusCode, Value) {
    let mut request = client.get(url);
    if let Some(token) = auth {
        request = request.bearer_auth(token);
    }
    let response = request.send().await.expect("GET sends");
    decode(response).await
}

/// A POST with a body and content type, returning the status and JSON body.
pub async fn post(
    client: &reqwest::Client,
    url: &str,
    content_type: &str,
    body: String,
    auth: Option<&str>,
) -> (reqwest::StatusCode, Value) {
    let mut request = client
        .post(url)
        .header(reqwest::header::CONTENT_TYPE, content_type)
        .body(body);
    if let Some(token) = auth {
        request = request.bearer_auth(token);
    }
    let response = request.send().await.expect("POST sends");
    decode(response).await
}

/// A JSON POST.
pub async fn post_json(
    client: &reqwest::Client,
    url: &str,
    body: Value,
    auth: Option<&str>,
) -> (reqwest::StatusCode, Value) {
    post(client, url, "application/json", body.to_string(), auth).await
}

async fn decode(response: reqwest::Response) -> (reqwest::StatusCode, Value) {
    let status = response.status();
    let text = response.text().await.unwrap_or_default();
    let body = serde_json::from_str(&text).unwrap_or(Value::Null);
    (status, body)
}

/// Registers a TOML definition, returning its assigned agent hash.
pub async fn register_agent(
    client: &reqwest::Client,
    base: &str,
    toml: &str,
    auth: Option<&str>,
) -> String {
    let (status, body) = post(
        client,
        &format!("{base}/v1/agents"),
        "application/toml",
        toml.to_owned(),
        auth,
    )
    .await;
    assert_eq!(status, reqwest::StatusCode::CREATED, "register: {body}");
    body["agent"].as_str().expect("agent hash").to_owned()
}

/// A trivial agent TOML. The factory ignores it, but registration parses it,
/// so it is real.
pub fn sample_toml() -> &'static str {
    "model = \"test-model\"\nsystem_prompt = \"You are a test agent.\"\n"
}

// ---------------------------------------------------------------------------
// A small server-sent-events reader.
// ---------------------------------------------------------------------------

/// One parsed server-sent-event frame.
#[derive(Debug)]
pub struct Frame {
    /// The `id:` field, parsed as a sequence number.
    pub id: Option<u64>,
    /// The `event:` field, when named.
    pub event: Option<String>,
    /// The `data:` payload.
    pub data: String,
}

impl Frame {
    /// Whether this is the terminal `end` frame.
    pub fn is_end(&self) -> bool {
        self.event.as_deref() == Some("end")
    }

    /// The frame's data decoded as JSON.
    pub fn json(&self) -> Value {
        serde_json::from_str(&self.data).unwrap_or(Value::Null)
    }
}

/// Reads server-sent-event frames from a running stream.
pub struct SseReader {
    stream: Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send>>,
    buf: String,
}

impl SseReader {
    /// Opens the event stream. `from_seq` sets the `?from_seq=` cursor;
    /// `last_event_id` sets the `Last-Event-ID` header.
    pub async fn open(
        client: &reqwest::Client,
        base: &str,
        run_id: &str,
        from_seq: Option<u64>,
        last_event_id: Option<u64>,
        auth: Option<&str>,
    ) -> Self {
        let mut url = format!("{base}/v1/runs/{run_id}/events");
        if let Some(seq) = from_seq {
            url.push_str(&format!("?from_seq={seq}"));
        }
        let mut request = client.get(&url);
        if let Some(id) = last_event_id {
            request = request.header("last-event-id", id.to_string());
        }
        if let Some(token) = auth {
            request = request.bearer_auth(token);
        }
        let response = request.send().await.expect("stream connects");
        assert!(response.status().is_success(), "stream status");
        Self {
            stream: Box::pin(response.bytes_stream()),
            buf: String::new(),
        }
    }

    /// The next frame, or `None` when the stream ends.
    pub async fn next(&mut self) -> Option<Frame> {
        loop {
            if let Some(index) = self.buf.find("\n\n") {
                let raw = self.buf[..index].to_string();
                self.buf.drain(..index + 2);
                if let Some(frame) = parse_frame(&raw) {
                    return Some(frame);
                }
                continue;
            }
            match self.stream.next().await {
                Some(Ok(bytes)) => self.buf.push_str(&String::from_utf8_lossy(&bytes)),
                _ => return None,
            }
        }
    }

    /// Reads frames until (and including) the terminal `end` frame.
    pub async fn read_to_end(&mut self) -> Vec<Frame> {
        let mut frames = Vec::new();
        while let Some(frame) = self.next().await {
            let end = frame.is_end();
            frames.push(frame);
            if end {
                break;
            }
        }
        frames
    }

    /// Reads exactly `count` non-terminal frames (for a mid-stream disconnect).
    pub async fn read_frames(&mut self, count: usize) -> Vec<Frame> {
        let mut frames = Vec::new();
        while frames.len() < count {
            match self.next().await {
                Some(frame) => frames.push(frame),
                None => break,
            }
        }
        frames
    }
}

/// Parses one raw frame block into a [`Frame`], or `None` if it is only a
/// keep-alive comment.
fn parse_frame(raw: &str) -> Option<Frame> {
    let mut id = None;
    let mut event = None;
    let mut data = String::new();
    let mut has_field = false;
    for line in raw.lines() {
        if let Some(value) = line.strip_prefix("id:") {
            id = value.trim().parse().ok();
            has_field = true;
        } else if let Some(value) = line.strip_prefix("event:") {
            event = Some(value.trim().to_owned());
            has_field = true;
        } else if let Some(value) = line.strip_prefix("data:") {
            if !data.is_empty() {
                data.push('\n');
            }
            data.push_str(value.strip_prefix(' ').unwrap_or(value));
            has_field = true;
        }
        // A line starting with ':' is a comment (keep-alive); ignore it.
    }
    has_field.then_some(Frame { id, event, data })
}

/// Reads a run's whole log directly from a store (a test convenience).
pub async fn read_log(
    store: &Arc<dyn EventStore>,
    run_id: salvor_core::RunId,
) -> Vec<salvor_core::EventEnvelope> {
    store.read_log(run_id).await.expect("read log")
}

/// Opens a file-backed store at `path` as the trait object the state holds.
pub fn open_store(path: &std::path::Path) -> Arc<dyn EventStore> {
    Arc::new(SqliteStore::open(path).expect("open store"))
}

/// A private in-memory store.
pub fn memory_store() -> Arc<dyn EventStore> {
    Arc::new(SqliteStore::in_memory().expect("open in-memory store"))
}