everruns-serve 0.41.0

Experimental agent framework over everruns: attribute macros, a build-time manifest, and a /v1 wire API
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
//! `serve::start`: one binary, several commands.
//!
//! ```text
//! cargo run -- dev                 # local server + live console (default)
//! cargo run -- start               # production mode, same wire API
//! cargo run -- manifest [--out f]  # the host contract, as JSON
//! cargo run -- eval [--against URL] [FILTER]
//! cargo run -- deploy              # what a host would provision (stub)
//! ```
//!
//! Decision: the commands live in the app binary rather than a separate
//! `serve` CLI, because only the linked binary knows its registrations. A
//! future `cargo serve` would be a thin wrapper that builds and runs these.

use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;

use anyhow::bail;
use clap::{Parser, Subcommand};
use everruns::{EventStreamError, SessionEvent};
use serde_json::Value;

use crate::app::{App, Mode};
use crate::host::Host;
use crate::host::Notice;

#[derive(Parser)]
#[command(about = "A serve app (experimental). Runs `dev` when no command is given.")]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(Subcommand)]
enum Command {
    /// Local server with SQLite, simulator fallback, Markdown hot reload and a live console.
    Dev {
        #[arg(long, env = "PORT", default_value_t = 3000)]
        port: u16,
    },
    /// Production mode: every model must route through a gateway, secrets must be set.
    Start {
        #[arg(long, env = "PORT", default_value_t = 3000)]
        port: u16,
    },
    /// Print the manifest (the contract with the host).
    Manifest {
        /// Write to this file instead of stdout.
        #[arg(long)]
        out: Option<PathBuf>,
    },
    /// Run the #[eval]s in-process, or against a running deployment.
    Eval {
        /// Base URL of a running app, e.g. a preview deploy.
        #[arg(long)]
        against: Option<String>,
        /// Only evals whose name contains this.
        filter: Option<String>,
    },
    /// Show what a host would provision for this build. No cloud target yet.
    Deploy,
}

/// Run the app according to the command line. See the module docs.
pub async fn start(app: App) -> crate::Result {
    let cli = Cli::parse();
    if !app.errors().is_empty() {
        eprintln!("serve: this app cannot start:");
        for error in app.errors() {
            eprintln!("  ✗ {error}");
        }
        bail!("{} problem(s) found during discovery", app.errors().len());
    }
    match cli.command.unwrap_or(Command::Dev { port: 3000 }) {
        Command::Dev { port } => serve(app, Mode::Dev, port).await,
        Command::Start { port } => serve(app, Mode::Start, port).await,
        Command::Manifest { out } => {
            let json = serde_json::to_string_pretty(&app.manifest())?;
            match out {
                Some(path) => std::fs::write(&path, json + "\n")?,
                None => println!("{json}"),
            }
            Ok(())
        }
        Command::Eval { against, filter } => {
            println!(
                "serve eval · {} · {}",
                app.name(),
                against.as_deref().unwrap_or("in-process")
            );
            let report = crate::eval::run(&app, against.as_deref(), filter.as_deref()).await?;
            let failed = report.results.iter().filter(|r| !r.passed).count();
            println!("{} passed, {failed} failed", report.results.len() - failed);
            if failed > 0 {
                bail!("{failed} eval(s) failed");
            }
            Ok(())
        }
        Command::Deploy => {
            deploy_plan(&app);
            Ok(())
        }
    }
}

async fn serve(app: App, mode: Mode, port: u16) -> crate::Result {
    let missing = crate::hosting::missing_secrets(&app);
    let data_dir = crate::hosting::data_dir()?;
    let server = crate::hosting::Server::new(app, mode, Some(data_dir.clone()))?;
    let host = server.host.clone();
    let missing: Vec<&str> = missing.iter().map(String::as_str).collect();

    let addr = SocketAddr::from(([0, 0, 0, 0], port));
    let listener = tokio::net::TcpListener::bind(addr).await?;
    banner(&host, mode, port, &data_dir, &missing);
    if mode == Mode::Dev {
        tokio::spawn(console(host.clone(), port));
    }
    server.spawn_schedules();
    axum::serve(listener, server.router())
        .with_graceful_shutdown(async {
            let _ = tokio::signal::ctrl_c().await;
        })
        .await?;
    Ok(())
}

fn banner(host: &Arc<Host>, mode: Mode, port: u16, data_dir: &std::path::Path, missing: &[&str]) {
    let app = &host.app;
    let manifest = app.manifest();
    let env = crate::gateway::Env::from_process();
    println!();
    println!(
        "  serve · {} {} · {:?} · build {}",
        manifest.app.name, manifest.app.version, mode, host.build_id
    );
    println!("  ⚠ experimental proof of concept; APIs will change");
    println!();
    for agent in &manifest.agents {
        let route = crate::gateway::route(&agent.model, &env)
            .map_or("simulator (no gateway configured)", |route| route.label());
        let role = if agent.sub {
            " (sub)"
        } else if agent.default {
            " (default)"
        } else {
            ""
        };
        println!(
            "  agent    {}{role} · {} → {route}",
            agent.name, agent.model
        );
    }
    for tool in &manifest.tools {
        let gate = if tool.needs_approval == "never" {
            String::new()
        } else {
            format!(" · approval: {}", tool.needs_approval)
        };
        println!("  tool     {}{gate}", tool.name);
    }
    for skill in &manifest.skills {
        println!("  skill    {}", skill.name);
    }
    for channel in &manifest.channels {
        println!(
            "  channel  {} ({}) · POST {}",
            channel.name, channel.kind, channel.route
        );
    }
    for schedule in &manifest.schedules {
        println!("  schedule {} · {}", schedule.name, schedule.cron);
    }
    for connection in &manifest.connections {
        println!("  connect  {} ({})", connection.name, connection.kind);
    }
    println!("  sandbox  {}", manifest.sandbox.kind);
    if !missing.is_empty() {
        println!(
            "  secrets  missing: {} (features using them are off in dev)",
            missing.join(", ")
        );
    }
    for warning in app.warnings() {
        println!("  note     {warning}");
    }
    println!();
    println!("  data     {}", data_dir.display());
    println!("  listen   http://localhost:{port}");
    println!();
    println!(
        "  try:  curl -s localhost:{port}/v1/sessions -H 'content-type: application/json' -d '{{}}'"
    );
    println!(
        "        curl -s localhost:{port}/v1/sessions/<id>/messages -H 'content-type: application/json' \\\n          -d '{{\"message\":{{\"content\":[{{\"type\":\"text\",\"text\":\"hello\"}}]}}}}'"
    );
    println!("        curl -N 'localhost:{port}/v1/sessions/<id>/sse?after_sequence=0'");
    println!();
}

/// The dev "terminal UI": one line per meaningful event, as it happens. It
/// follows each live session's canonical events, plus the host's notices for
/// what has no event (pending approvals and questions, deliveries).
async fn console(host: Arc<Host>, port: u16) {
    let mut notices = host.notices.subscribe();
    loop {
        let notice = match notices.recv().await {
            Ok(notice) => notice,
            Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
            Err(_) => return,
        };
        if let Notice::Live {
            session_id, after, ..
        } = &notice
        {
            tokio::spawn(follow(host.clone(), session_id.clone(), *after));
        }
        if let Some((session, line)) = notice_line(&notice, port) {
            print_line(&stamp(), &session, &line);
        }
    }
}

fn stamp() -> String {
    chrono::Utc::now().format("%H:%M:%S").to_string()
}

fn print_line(at: &str, session: &str, line: &str) {
    // Session ids share a long time-ordered prefix; the tail tells them apart.
    let tail = session.len().saturating_sub(6);
    let short = session.get(tail..).unwrap_or(session);
    println!("{at} {short} {line}");
}

/// Print one session's canonical events after `after`, replay then live.
async fn follow(host: Arc<Host>, id: String, mut after: i32) {
    let Ok(session) = host.session(&id).await else {
        return;
    };
    let Ok(mut events) = session.events_from(after).await else {
        return;
    };
    loop {
        match events.recv().await {
            Ok(Some(event)) => {
                after = event.sequence().unwrap_or(after).max(after);
                if let Some(line) = event_line(&event) {
                    print_line(event.timestamp().get(11..19).unwrap_or(""), &id, &line);
                }
            }
            Err(EventStreamError::Lagged { .. }) => match session.events_from(after).await {
                Ok(fresh) => events = fresh,
                Err(_) => return,
            },
            _ => return,
        }
    }
}

fn clip(value: &str) -> String {
    let flat = value.replace('\n', " ");
    if flat.chars().count() > 160 {
        format!("{}…", flat.chars().take(160).collect::<String>())
    } else {
        flat
    }
}

/// Joined text parts of a canonical message.
fn message_text(message: &Value) -> String {
    message["content"]
        .as_array()
        .into_iter()
        .flatten()
        .filter(|part| part["type"] == "text")
        .filter_map(|part| part["text"].as_str())
        .collect::<Vec<_>>()
        .join("")
}

fn event_line(event: &SessionEvent) -> Option<String> {
    let data = &event.canonical_json()["data"];
    let text = |value: &Value| value.as_str().unwrap_or_default().to_string();
    Some(match event.event_type() {
        "input.message" => format!("▸ {}", clip(&message_text(&data["message"]))),
        "tool.started" => format!(
            "  ⚙ {} {}",
            text(&data["tool_call"]["name"]),
            clip(&data["tool_call"]["arguments"].to_string())
        ),
        "tool.progress" => format!("  … {}", text(&data["message"])),
        "tool.completed" => {
            let ok = data["success"].as_bool() == Some(true);
            format!(
                "  {} {}",
                if ok { "✓" } else { "✗" },
                text(&data["tool_name"])
            )
        }
        "output.message.completed" => {
            let reply = message_text(&data["message"]);
            if reply.is_empty() {
                return None;
            }
            format!("◂ {}", clip(&reply))
        }
        "turn.failed" => format!("✗ turn failed: {}", text(&data["error"])),
        "turn.cancelled" => "✗ turn cancelled".to_string(),
        _ => return None,
    })
}

fn notice_line(notice: &Notice, port: u16) -> Option<(String, String)> {
    Some(match notice {
        Notice::Live {
            session_id,
            agent,
            resumed,
            ..
        } => (
            session_id.clone(),
            if *resumed {
                format!("● resumed {agent} after restart")
            } else {
                format!("● new session on {agent}")
            },
        ),
        Notice::BuildChanged { session_id, from } => (
            session_id.clone(),
            format!("  ⚠ session started on build {from}; resuming on this build"),
        ),
        Notice::ApprovalRequested(view) => (
            view.session_id.clone(),
            format!(
                "  ⏸ {} needs approval: {}\n      curl -X POST localhost:{port}/v1/sessions/{}/approvals/{} -H 'content-type: application/json' -d '{{\"decision\":\"approve\"}}'",
                view.tool_name,
                clip(&view.arguments.to_string()),
                view.session_id,
                view.tool_call_id,
            ),
        ),
        Notice::QuestionAsked {
            session_id,
            tool_call_id,
            questions,
        } => {
            let asked: Vec<String> = questions
                .iter()
                .map(|question| {
                    let options: Vec<&str> = question
                        .options
                        .iter()
                        .map(|option| option.label.as_str())
                        .collect();
                    format!(
                        "{} [{}] ({})",
                        question.question,
                        question.id.as_deref().unwrap_or("?"),
                        options.join(" / ")
                    )
                })
                .collect();
            (
                session_id.clone(),
                format!(
                    "  ? {}\n      curl -X POST localhost:{port}/v1/sessions/{session_id}/question-answers -H 'content-type: application/json' -d '{{\"tool_call_id\":\"{tool_call_id}\",\"answers\":[{{\"id\":\"…\",\"selected\":[\"…\"]}}]}}'",
                    asked.join("; ")
                ),
            )
        }
        Notice::Delivered {
            session_id,
            to,
            error,
        } => (
            session_id.clone(),
            match error {
                None => format!("  ↳ delivered to {to}"),
                Some(error) => format!("  ✗ delivery to {to} failed: {error}"),
            },
        ),
    })
}

fn deploy_plan(app: &App) {
    let manifest = app.manifest();
    let target = manifest
        .app
        .deploy_target
        .as_deref()
        .unwrap_or("everruns-cloud");
    println!(
        "serve deploy · {} · build {} → {target}",
        manifest.app.name, manifest.build_id
    );
    println!();
    println!("This proof of concept has no cloud target. A host reading this manifest would:");
    println!("  1. build the binary and an OCI image; publish manifest.json beside it");
    for schedule in &manifest.schedules {
        println!(
            "  2. create cron `{}` → run schedule `{}`",
            schedule.cron, schedule.name
        );
    }
    for channel in &manifest.channels {
        println!(
            "  3. route https://<preview>/v1/channels/{} → this build ({})",
            channel.name, channel.kind
        );
    }
    for secret in &manifest.secrets {
        let state = if std::env::var_os(&secret.name).is_some() {
            "set locally"
        } else {
            "ask the owner"
        };
        println!("  4. secret {} ({state})", secret.name);
    }
    println!(
        "  5. provision DATABASE_URL, NATS_URL and the `{}` sandbox adapter",
        manifest.sandbox.kind
    );
    for model in &manifest.models {
        println!("  6. route model `{model}` through the gateway (SERVE_GATEWAY_URL)");
    }
    println!("  7. give the branch a preview URL; run `eval --against <preview>` before promotion");
    println!(
        "  8. pin new sessions to {}; keep the previous build until its sessions finish",
        manifest.build_id
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::host::PendingApprovalView;
    use serde_json::json;

    #[test]
    fn console_explains_approvals_with_a_ready_curl() {
        let notice = Notice::ApprovalRequested(PendingApprovalView {
            session_id: "session_x".into(),
            tool_call_id: "call_1".into(),
            tool_name: "run_sql".into(),
            arguments: json!({ "sql": "select 1" }),
        });
        let (session, line) = notice_line(&notice, 3000).unwrap();
        assert_eq!(session, "session_x");
        assert!(line.contains("run_sql needs approval"), "{line}");
        assert!(
            line.contains("/v1/sessions/session_x/approvals/call_1"),
            "{line}"
        );
    }

    #[test]
    fn message_text_joins_text_parts() {
        let message = json!({ "content": [
            { "type": "text", "text": "a" },
            { "type": "image", "url": "x" },
            { "type": "text", "text": "b" },
        ] });
        assert_eq!(message_text(&message), "ab");
    }
}